USB设备类型代码以及详细说明

说明:由于链接是相对路径,下载和点击会不正常,如果需要下载文中提到的文档,请去官方网站相关地址下载

以下是USB所有支持的设备类型代码.

地址:http://www.usb.org/developers/defined_class

USB Class Codes                    

November 17, 2009

 


USB defines class code information that is used to identify a device’s functionality and to nominally load a device driver based on that functionality. The information is contained in three bytes with the names Base Class, SubClass, and Protocol. (Note that ‘Base Class’ is used in this description to identify the first byte of the Class Code triple. That terminology is not used in the USB Specification). There are two places on a device where class code information can be placed.One place is in the Device Descriptor, and the other is in Interface Descriptors. Some defined class codes are allowed to be used only in a Device Descriptor, others can be used in both Device and Interface Descriptors, and some can only be used in Interface Descriptors. The table below shows the currently defined set of  Base Class values, what the generic usage is, and where that Base Class can be used (either Device or Interface Descriptors or both).

 

 

Base Class

Descriptor Usage

Description

00h

Device

Use class information in the Interface Descriptors

01h

Interface

Audio  

02h

Both

Communications and CDC Control

03h

Interface

HID (Human Interface Device)

05h

Interface

Physical

06h

Interface

Image

07h

Interface

Printer

08h

Interface

Mass Storage

09h

Device

Hub

0Ah

Interface

CDC-Data

0Bh

Interface

Smart Card

0Dh

Interface

Content Security

0Eh

Interface

Video

0Fh

Interface

Personal Healthcare

DCh

Both

Diagnostic Device

E0h

Interface

Wireless Controller

EFh

Both

Miscellaneous

FEh

Interface

Application Specific

FFh

Both

Vendor Specific

 

 


Base Class 00h (Device)

This base class is defined to be used in Device Descriptors to indicate that class information should be determined from the Interface Descriptors in the device. There is one class code definition in this base class.  All other values are reserved.

 

This value is also used in Interface Descriptors to indicate a null class code triple.

 

 

Base Class

SubClass

Protocol

Meaning

00h

00h

00h

Use class code info from Interface Descriptors

 


Base Class 01h (Audio)

This base class is defined for Audio capable devices that conform to the Audio Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values. Values outside of that defined spec are reserved. These class codes may only be used in Interface Descriptors.

 

 

Base Class

SubClass

Protocol

Meaning

01h

xxh

xxh

Audio device

 

 

Base Class 02h (Communications and CDC Control)

 

This base class is defined for devices that conform to the Communications Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values. Values outside of that defined spec are reserved.  Note that the Communication Device Class spec requires some class code values (triples) to be used in Device Descriptors and some to be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

02h

xxh

xxh

Communication device class

 

Base Class 03h (HID – Human Interface Device)

This base class is defined for devices that conform to the HID Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values.   Values outside of that defined spec are reserved. These class codes can only be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

03h

xxh

xxh

HID device class

 

Base Class 05h (Physical)

This base class is defined for devices that conform to the Physical Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values.  Values outside of that defined spec are reserved. These class codes can only be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

05h

xxh

xxh

Physical device class

 

 

Base Class 06h (Still Imaging)

This base class is defined for devices that conform to the Imaging Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values.   Values outside of that defined spec are reserved.

 

Base Class

SubClass

Protocol

Meaning

06h

01h

01h

Still Imaging device

 

 

Base Class 07h (Printer)

This base class is defined for devices that conform to the Printer Device Class Specification found on the USB-IF website.  That specification defines the usable set of SubClass and Protocol values.  Values outside of that defined spec are reserved.  These class codes can only be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

07h

xxh

xxh

Printer device

 

 

Base Class 08h (Mass Storage)

This base class is defined for devices that conform to the Mass Storage Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values. Values outside of that defined spec are reserved. These class codes can only be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

08h

xxh

xxh

Mass Storage device

 

 

Base Class 09h (Hub)

This base class is defined for devices that are USB hubs and conform to the definition in the USB specification.  That specification defines the complete triples as shown below.  All other values are reserved.  These class codes can only be used in Device Descriptors.

 

Base Class

SubClass

Protocol

Meaning

09h

00h

00h

Full speed Hub

01h

Hi-speed hub with single TT

02h

Hi-speed hub with multiple TTs

 

 

Base Class 0Ah (CDC-Data)

This base class is defined for devices that conform to the Communications Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values.Values outside of that defined spec are reserved. These class codes can only be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

0Ah

xxh

xxh

CDC data device

 

 

Base Class 0Bh (Smart Card)

This base class is defined for devices that conform to the Smart Card Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values.Values outside of that defined spec are reserved. These class codes can only be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

0Bh

xxh

xxh

Smart Card device

 

 

Base Class 0Dh (Content Security)

This base class is defined for devices that conform to the Content Security Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values. Values outside of that defined spec are reserved. These class codes can only be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

0Dh

00h

00h

Content Security device

 

 

Base Class 0Eh (Video)

This base class is defined for devices that conform to the Video Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values. Values outside of that defined spec are reserved. These class codes can only be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

0Eh

xxh

xxh

Video device

 

 

Base Class 0Fh (Personal Healthcare)

This base class is defined for devices that conform to the Personal Healthcare Device Class Specification found on the USB-IF website. That specification defines the usable set of SubClass and Protocol values. Values outside of that defined spec are reserved. These class codes should only be used in Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

0Fh

xxh

xxh

Personal Healthcare device

 

 

Base Class DCh (Diagnostic Device)

This base class is defined for devices that diagnostic devices. There is currently only one value defined. All other values are reserved. This class code can be used in Device or Interface Descriptors.

 

Base Class

SubClass

Protocol

Meaning

DCh

01h

01h

USB2 Compliance Device.  Definition for this device can be found at http://www.intel.com/technology/usb/spec.htm

 

 

Base Class E0h (Wireless Controller)

This base class is defined for devices that are Wireless controllers. Values not shown in the table below are reserved. These class codes are to be used in Interface Descriptors, with the exception of the Bluetooth class code which can also be used in a Device Descriptor.

 

Base Class

SubClass

Protocol

Meaning

E0h

01h

01h

Bluetooth Programming Interface.  Get specific information from www.bluetooth.com.

02h

UWB Radio Control Interface.  Definition for this is found in the Wireless USB Specification in Chapter 8.

03h

Remote NDIS.  Information can be found at: http://www.microsoft.com/windowsmobile/mobileoperators/default.mspx

04h

 

Bluetooth AMP Controller. Get specific information from www.bluetooth.com.

2h

01h

Host Wire Adapter Control/Data interface.  Definition can be found in the Wireless USB Specification in Chapter 8.

02h

Device Wire Adapter Control/Data interface.  Definition can be found in the Wireless USB Specification in Chapter 8.

03h

Device Wire Adapter Isochronous interface.  Definition can be found in the Wireless USB Specification in Chapter 8.

 

Base Class EFh (Miscellaneous)

This base class is defined for miscellaneous device definitions. Values not shown in the table below are reserved.  The use of these class codes (Device or Interface descriptor) are specifically annotated in each entry below.

 

 

Base Class

SubClass

Protocol

Meaning

EFh

01h

01h

Active Sync device.  This class code can be used in either Device or Interface Descriptors.  Contact Microsoft for more information on this class.

02h

Palm Sync.  This class code can be used in either Device or Interface Descriptors.

02h

01h

Interface Association Descriptor.  The usage of this class code triple is defined in the Interface Association Descriptor ECN that is provided on www.usb.org .  This class code may only be used in Device Descriptors.

02h

Wire Adapter Multifunction Peripheral programming interface. Definition can be found in the Wireless USB Specification in Chapter 8.  This class code may only be used in Device Descriptors

03h

01h

Cable Based Association Framework.  This is defined in the Association Model addendum to the Wireless USB specification.  This class code may only be used in Interface Descriptors.

 

 

 

Base Class FEh (Application Specific)

This base class is defined for devices that conform to several class specifications found on the USB-IF website.  That specification defines the usable set of SubClass and Protocol values.  Values outside of that defined spec are reserved.  These class codes can only be used in Interface Descriptors.

 

 

Base Class

SubClass

Protocol

Meaning

FEh

01h

01h

Device Firmware Upgrade.  Device class definition provided on www.usb.org .

02h

00h

IRDA Bridge device.  Device class definition provided on www.usb.org .

03h

00h

USB Test and Measurement Device.  Definition provided in the USB Test and Measurement Class spec found on www.usb.org .

01h

USB Test and Measurement Device conforming to the USBTMC USB488 Subclass Specification found on www.usb.org.

 

 

Base Class FFh (Vendor Specific)

This base class is defined for vendors to use as they please.  These class codes can be used in both Device and Interface Descriptors.

 

 

Base Class

SubClass

Protocol

Meaning

FFh

xxh

xxh

Vendor specific

 

以下是相关类型的详细说明:

地址:http://www.usb.org/developers/devclass_docs

Approved Class Specification Documents 

If you have questions regarding any of these documents please contact the DWG chair (dwg-wg-chair@usb.org) and your questions will be channeled to the appropriate group.

Audio Class

Audio Device Class Spec for Basic Audio Devices and Adopters Agreement (.zip format, size 1.33MB)
Audio Device Document 1.0
Audio Data Formats 1.0
Audio Terminal Types 1.0
USB MIDI Devices 1.0
Audio Devices Rev. 2.0 Spec and Adopters Agreement (.zip format, size 1.28MB) 

Battery Charging

Battery Charging v1.1 Spec and Adopters Agreement (.zip format, size 292 KB)

Cable and Connector

Cable and Connector Class 2.0 
Series 'A' Plug form factor Guideline 1.0
USB Connector for Mezzanine Applications Guidelines Rev. 1.0
Micro-USB Cables and Connectors v1.01 Spec and Adopters Agreement

Common Class (CCS)

Common Class Base Specification 1.0

Communications Device Class

Class definitions for Communication Devices 1.2 (.zip file format, size 2.61 MB)
the components of CDC 1.1 have been reorganized as five separate documents:
ATM120.pdf -- CDC Subclass for Asynchronous Transfer Mode Devices
CDC120.pdf -- CDC Subclass for Communications Devices
ECM120.pdf -- CDC Subclass for Ethernet Control Model Devices
ISDN120.pdf -- CDC Subclass for ISDN Devices
PSTN120.pdf -- CDC Subclass for PSTN devices
CDC Subclass for Wireless Mobile Communication Devices 1.1
CDC v1.2 WMC v1.1 Adopters Agreement


CDC Subclass Specification for Ethernet Emulation Model Devices 1.0

Network Control Model Devices Specification v1.0 and Adopters Agreement (.zip format, size 492 KB)

Content Security

Device Class Definition for Content Security Devices 1.0
Content Security Method 1 - Basic Authentication Protocol 1.0
Content Security Method 2 - USB Digital Transmission Content Protection Implementation 1.0

Device Firmware Upgrade Group

Device Firmware Upgrade 1.1 (new version 31-Aug-2004)

HID Class

HID Information

Imaging Class

Still Image Capture Device Definition 1.0 and Errata as of 16-Mar-2007 (.zip format, size 200 KB)

IrDA

IrDA Bridge Device Definition 1.0

Mass Storage

Mass Storage Class Specification Overview 1.3
Mass Storage Bulk Only 1.0
Mass Storage Control/Bulk/Interrupt (CBI) Specification 1.1  
Mass Storage UFI Command Specification 1.0
Mass Storage Bootability Specification 1.0
Lockable Mass Storage Specification 1.0 and Adopters Agreement (.zip format, size 288 KB)
  - Lockable Mass Storage IP Disclosure

Media Transfer Protocol

Media Transfer Protocol v.1.0 Spec and MTP v.1.0 Adopters Agreement (.zip format, size 1.86MB)

Monitor

Monitor Device Document 1.0

OpenUSBDI

OpenUSBDI Specification 1.0

Personal Healthcare

Personal Healthcare Rev. 1.0, Errata for USB Personal Health Care Device Class and Personal Healthcare Adopters Agreement (.zip format, size 386 KB)

Physical Interface Devices

Device Class Definition for PID 1.0

Power

Power Device Class Document 1.0

Printer Class

Printer Device Class Document 1.1

Smart Card Class

Smart Card CCID version 1.1
Smart Card ICCD version 1.0
 

Test & Measurement Class

Test & Measurement Class Specifications

Video Class

Video Class 1.1 document set
    USB_Video_Class_1.1
    USB_Video_Identifiers_1.1
    USB_Video_Payload_DV_1.1
    USB_Video_Payload_Frame_Based_1.1
    USB_Video_Payload_MJPEG_1.1
    USB_Video_Payload_MPEG-2_TS_1.1
    USB_Video_Payload_Uncompressed_1.1
    USB_Video_Payload_Stream_Based_1.1
    USB_Video_Transport_1.1
    USB_Video_Example_1.1
    USB_Video_FAQ_1.1

Draft Class Specification Documents

PC Legacy Compatibility Specification 0.9

USB_Video_Compliance_1.09b -- draft for public review

 

  • 0
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
第一章 USB概述及协议基础 1 1.1 USB是什么 1 1.2 USB的特点 1 1.3 USB的拓扑结构 2 1.4 USB的电气特性 5 1.5 USB的线缆以及插头、插座 5 1.6 USB的插入检测机制 7 1.7 USB的描述符及其之间的关系 9 1.8 USB设备的枚举过程 10 1.9 USB的包结构及传输过程 11 1.9.1 USB包的结构及包的分类 11 1.9.2 令牌包 13 1.9.3 数据包 14 1.9.4 握手包 14 1.9.5 特殊包 15 1.9.6 如何处理数据包 15 1.10 USB的四种传输类型 16 1.10.1 USB事务 16 1.10.2 批量传输 16 1.10.3 中断传输 18 1.10.4 等时传输(同步传输) 19 1.10.5 控制传输 20 1.10.6 端点类型与传输类型的关系 21 1.10.7 传输类型与端点支持的最大包长 21 1.11 本章小结 21 第二章 硬件系统设计 1 2.1 方案以及芯片的选定 1 2.2 D12引脚功能说明 2 2.3 D12与89S52的连接 4 2.4 串口部分电路 6 2.5 按键部分 7 2.6 指示灯部分 7 2.7 IDE接口部分 8 2.8 单片机部分 8 2.9 元件安装 8 2.10 电路调试 11 2.11 测试程序的编写和调试 12 2.11.1 建立一个工程 12 2.11.2 为工程添加源文件 14 2.11.3 KEIL工具栏及仿真介绍 15 2.11.4 按键驱动的编写 18 2.11.5 串口驱动的编写 24 2.11.6 PDIUSBD12读写函数及读ID的实现 28 2.12 本章小结 33 第三章 USB鼠标的实现 1 3.1 USB鼠标工程的建立 1 3.2 USB的断开与连接 1 3.3 USB中断的处理 4 3.4 读取从主机发送到端点0的数据 6 3.5 USB标准请求 12 3.5.1 USB标准设备请求的结构 13 3.5.2 GET_DESCRIPTOR请求 15 3.5.3 SET_ADDRESS请求 16 3.5.6 SET_CONFIGURATION请求 16 3.6 设备描述符的实现 17 3.7 设备描述符的返回 20 3.8 设置地址请求的处理 30 3.9 配置描述符集合的结构 32 3.9.1 配置描述符的结构 32 3.9.2 接口描述符的结构 33 3.9.3 端点描述符的结构 33 3.9.4 HID描述符的结构 34 3.10 配置描述符集合的实现以及返回 35 3.11 字符串及语言ID请求的实现 39 3.12 设置配置请求的实现 45 3.13 报告描述符的结构及实现 48 3.14 报告的返回 54 3.15 Bus Hound工具的简介 57 3.16 本章小结 59 第四章 USB键盘的实现 1 4.1 USB键盘工程的建立 1 4.2 设备描述符的实现 1 4.4 配置描述符集合的实现 2 4.4.1 配置描述符 3 4.4.2 接口描述符 3 4.4.3 HID描述符 3 4.4.4 端点描述 3 4.5 字符串描述符 6 4.6 报告描述符 6 4.7 输入和输出报告的实现 10 4.8 USB键盘实例的测试 13 4.9 再谈USB HID的报告描述符 14 4.10 带鼠标功能的USB键盘(方法一) 16 4.11 带鼠标功能的键盘(方法二) 22 4.12 多媒体USB键盘 29 4.13 本章小结 34 第五章 用户自定义的USB HID设备 1 5.1 MyUsbHid工程的建立 1 5.2 描述符的修改 1 5.3 报告的实现 3 5.4 对用户自定义的USB HID设备的访问 5 5.5 访问HID设备时所用到的相关函数 5 5.5.1 获取HID设备的接口类GUID的函数 5 5.5.2 获取指定类的所有设备信息集合的函数 6 5.5.3 从设备信息集合中获取一个设备接口信息的函数 6 5.5.4 获取指定设备接口详细信息的函数 7 5.5.5 打开设备的函数 8 5.5.6 获取HID设备属性的函数 8 5.5.7 从设备读取数据的函数 9 5.5.8 往设备写数据的函数 9 5.5.9 通过控制端点0读取报告的函数 10 5.5.10 通过控制端点0发送报告的函数 10 5.5.11 关闭设备的函数 10 5.5.12 需要包含的库文件 11 5.6 访问USB HID设备的上位机软件的实现 11 5
/* 也是从其他地方搞过来的,花了不少积分,我就1分奉献了吧,绝对是好书~ */ 第1章USB概述及协议基础 1.1USB是什么 1.2USB的特点 1.3USB的拓扑结构 1.4USB的电气特性 1.5USB的线缆、插头及插座 1.6USB的插入检测机制 1.7USB的描述符及其之间的关系 1.8USB设备的枚举过程 1.9USB的包结构及传输过程 1.9.1USB的包结构及包的分类 1.9.2令牌包 1.9.3数据包 1.9.4握手包 1.9.5特殊包 1.9.6如何处理数据包 1.10USB的四种传输类型 1.10.1USB事务 1.10.2批量传输 1.10.3中断传输 1.10.4等时传输 1.10.5控制传输 1.10.6端点类型与传输类型的关系 1.10.7传输类型与端点支持的最大包长 1.11本章小结 第2章硬件系统设计 2.1方案及芯片的选定 2.2D12引脚功能说明 2.3D12与AT89S52的连接 2.4串口部分电路 2.5按键部分 2.6指示灯部分 2.7IDE接口部分 2.8单片机部分 2.9元件安装 2.10电路调试 2.11测试程序的编写和调试 2.11.1建立一个工程 2.11.2为工程添加源文件 2.11.3KEIL工具栏及仿真介绍 2.11.4按键驱动的编写 2.11.5串口驱动的编写 2.11.6PDIUSBDl2读写函数及读ID的实现 2.12本章小结 第3章USB鼠标的实现 3.1USB鼠标工程的建立 3.2USB的断开与连接 3.3USB中断的处理 3.4读取从主机发送到端点O的数据 3.5USB标准请求 3.5.1USB标准设备请求的结构 3.5.2GET_DESCRIPTOR请求 3.5.3SET_ADDRESS请求 3.5.4SETCONFIGURATION请求 3.6设备描述符的实现 3.7设备描述符的返回 3.8设置地址请求的处理 3.9配置描述符集合的结构 3.9.1配置描述符的结构 3.9.2接口描述符的结构 3.9.3端点描述符的结构 3.9.4HID描述符的结构 3.10配置描述符集合的实现以及返回 3.11字符串及语言ID请求的实现 3.12设置配置请求的实现 3.13报告描述符的结构及实现 3.14报告的返回 3.15BusHound工具的简介 3.16本章小结 第4章USB键盘的实现 4.1USB键盘工程的建立 4.2设备描述符的实现 4.3配置描述符集合的实现 4.3.1配置描述符 4.3.2接口描述符 4.3.3HID描述符_ 4.3.4端点描述符 4.4字符串描述符 4.5报告描述符 4.6输入和输出报告的实现 4.7 USB键盘实例的测试 4.8再谈USBHID的报告描述符 4.9带鼠标功能的USB键盘(方法一) 4.10带鼠标功能的USB键盘(方法二) 4.11多媒体USB键盘 4.12本章小结 第5章用户自定义的USBHID备 5.1MyUsbHid工程的建立 5.2描述符的修改 5.3报告的实现 5.4对用户自定义的USBHID设备的访问 5.5访问HID设备时所用到的相关函数 5.5.1 获取HID设备的接口类GUID的函数 5.5.2获取指定类的所有设备信息集合的函数 5.5.3从设备信息集合中获取一个设备接口信息的函数 5.5.4获取指定设备接口详细信息的函数 5.5.5打开设备的函数 5.5.6获取HID设备属性的函数 5.5.7从设备读取数据的函数 5.5.8往设备写数据的函数 5.5.9通过控制端点O读取报告的函数 5.5.10通过控制端点O发送报告的函数 5.5.11关闭句柄的函数 5.5.12需要包含的库文件 5.6访问USBHID设备的上位机软件的实现 5.6.1上位机程序编写的思路 5.6.2查找及打开HID设备代码 5.6.3读输入报告线程的代码 5.6.4写输出报告的代码(发送LED的状态) 5.6.5写输出报告线程的代码 5.6.6线程的创建以及设备插拔事件的注册 5.6.7对设备状态改变事件的处理 5.7软件界面以及使用方法 5.8本章小结 第6章USB转串口 6.1串口家族历史 6.2串口接头的引脚分布及功能 6.3USB转串口的实现方法 6.4设备描述符 6.5字符串描述符 …… 第7章USBMIDI键盘 第8章U盘 第9章自定义USB设备及驱动开发 第10章USB过滤驱动开发 附录第3章实例的完整调试信息 参考文献 后记 ……
dW 登录 | 注册 IBM developerWorks® 技术主题 软件下载 社区 技术讲座 搜索 developerWorks 打印本页面用电子邮件发送本页面新浪微博人人网腾讯微博搜狐微博网易微博DiggFacebookTwitterDeliciousLinked In developerWorks 中国技术主题Java technology文档库 在 Java 应用程序中访问 USB 设备 介绍 USB、jUSB 和 JSR-80 Java 平台一直都以其平台无关性自豪。虽然这种无关性有许多好处,但是它也使得编写与硬件交互的 Java 应用程序的过程变得相当复杂。在本文中,研究科学家蒋清野讨论了两个项目,它们通过提供使Java 应用程序可以使用 USB 设备的 API 而使这个过程变得更容易。虽然这两个项目仍然处于萌芽状态,但是它们都显示了良好的前景,并已经成为一些实用应用程序的基础。 1 评论: 蒋清野 ([email protected]), 研究科学家, HappyFox Engineering Solutions 2003 年 10 月 25 日 + 内容 在 IBM Bluemix 云平台上开发并部署您的下一个应用。 现在就开始免费试用 通用串行总线(Universal Serial Bus USB)规范的第一个版本发表于 1996年 1月。因为它的低成本、高数据传输率、使用容易和灵活性,USB 在计算机行业里获得了广泛接受。今天,许多周边设备和装置都是通过 USB 接口连接到计算机上的。目前,大多数一般用途的操作系统都提供了对 USB 设备的支持,并且用 C 或者 C++ 可以相对容易地开发访问这些外设的应用程序。不过,Java 编程语言在设计上对硬件访问提供的支持很少,所以编写与 USB 设备交互的应用程序是相当困难的。 IBM 的 Dan Streetman 最早开始了在 Java 语言中提供对 USB 设备的访问的努力。2001年,他的项目通过 Java 规范请求(Java Specification Request,JSR)过程被接受为 Java 语言的候选扩展标准。这个项目现在称为 JSR-80 并且指定了官方包 javax.usb 。同时,在 2000年 6月,Mojo Jojo 和 David Brownell 在 SourceForge 开始了 jUSB 项目。这两个项目都开发出了 Linux 开发人员可以使用的包,尽管它们都还很不完善。这两个项目也都开始试图向其他操作系统上的 Java 应用程序提供对 USB 设备的访问,尽管它们都还没有开发出可以使用的包(参阅 参考资料 中有关本文中讨论的这两个项目及其他项目的资料)。 在本文中,将对 jUSB 和 JSR-80 项目作一个简要介绍,不过,我们首先要看一下 USB 协议的具体细节,这样您就可以理解这两个项目是如何与 USB 设备交互的。我们还将提供代码片段以展示如何用这两个项目的 API 访问 USB 设备USB 介绍 1994年,一个由四个行业伙伴(Compaq、Intel、Microsoft 和 NEC)组成的联盟开始制定 USB 协议。该协议最初的目的是将 PC 与电话相连并提供容易扩展和重新配置的 I/O 接口。1996年 1月,发表了 USB 规范的第一个版本,1998年 9月发表了后续版本(版本 1.1)。这个规范允许 127台设备同时连接到一起,总的通信带宽限制为 12 Mbps。后来,又有三个成员(Hewlett-Packard、Lucent 和 Philips)加入了这个联盟。2000年 4月,发表了 USB 规范的 2.0版本,它支持高达 480 Mbps 的传输率。今天,USB 在高速(视频、图像、储存)和全速(音频、宽带、麦克风)数据传输应用中起了关键作用。它还使各种低速设备(键盘、鼠标、游戏外设、虚拟现实外设)连接到 PC 上。 USB 协议有严格的层次结构。在所有 USB 系统中,只有一个主设备,到主计算机的的 USB 接口称为 主控器(host controller)。主控器有两个标准――开放主控器接口(Compaq 的 Open Host Controller Interface,OHCI)和通用主控器接口(Intel 的 Universal Host Controller Interface,UHCI)。这两个标准提供了同样的能力,并可用于所有的 USB 设备,UHCI 的硬件实现更简单一些,但是需要更复杂的设备驱动程序(因而 CPU 的负荷更大一些)。 USB 物理互连是分层的星形拓朴,最多有七层。一个 hub 是每个星形的中心,USB 主机被认为是 root hub。每一段连线都是 hub 与 USB 设备的点对点连接,后者可以是为系统提供更多附加点的另一个 hub,也可以是一个提供功能的某种设备。主机使用主/从协议与 USB 设备通信。这种方式解决了包冲突的问题,但是同时也阻止了附加的设备彼此建立直接通信。 所有传输的数据都是由主控器发起的。数据从主机流向设备称为 下行(downstream)或者 输出(out)传输,数据从设备流向主机称为 上 行(upstream)或者 输入(in)传输。数据传输发生在主机和 USB 设备上特定的 端点(endpoint) 之间,主机与端点之间的数据链接称为 管道(pipe)。 一个给定的 USB 设备可以有许多个端点,主机与设备之间数据管道的数量与该设备上端点的数量相同。一个管道可以是单向或者是双向的,一个管道中的数据流与所有其他管道中的数据流无关。 USB 网络中的通信可以使用下面四种数据传输类型中的任意一种: 控制传输:这些是一些短的数据包,用于设备控制和配置,特别是在设备附加到主机上时。 批量传输:这些是数量相对大的数据包。像扫描仪或者 SCSI 适配器这样的设备使用这种传输类型。 中断传输:这些是定期轮询的数据包。主控器会以特定的间隔自动发出一个中断。 等时传输:这些是实时的数据流,它们对带宽的要求高于可靠性要求。音频和视频设备一般使用这种传输类型。 像串行端口一样,计算机上每一个 USB 端口都由 USB 控制器指定了一个惟一的标识数字(端口 ID)。当 USB 设备附加到 USB 端口上时,就将这个 惟一端口 ID 分配给这台设备,并且 USB 控制器会读取 设备描述符。设备描述符包括适用于该设备的全局信息、以及设备的 配置信息。配置定义了一台 USB 设备的功能和 I/O 行为。一台 USB 设备可以有一个或者多个配置,这由它们相应的配置描述符所描述。每一个配置都有一个或者多个 接口,它可以视为一个物理通信渠道 ;每一个接口有零个或者多个端点,它可以是数据提供者或者数据消费者,或者同时具有这两种身份。接口由接口描述符描述,端点由端点描述符描述。并且一台 USB 设备可能还有字符串描述符以提供像厂商名、设备名或者序列号这样的附加信息。 正如您所看到的,像 USB 这样的协议为使用 Java 这种强调平台和硬件无关性的语言的开发人员提出了挑战。现在让我们看两个试图解决这个问题的项目。 回页首 jUSB API jUSB 项目是由 Mojo Jojo 和 David Brownell 于 2000年 6月创立的。其目标是提供一组免费的、在 Linux 平台上访问 USB 设备的 Java API。这个 API 是按照 Lesser GPL (LGPL)条款发表的,这意味着您可以在专有和免费软件项目中使用它。这个 API 提供了对多个物理 USB 设备的多线程访问,并支持本机和远程设备。具有多个接口的设备可以同时被多个应用程序(或者设备驱动程序)所访问,其中每一个应用程序(或者设备驱动程序)都占据一个不同的接口。该 API 支持控制传输、批量传输和中断传输,不支持等时传输,因为等时传输用于媒体数据(如音频和视频),JMF API 已经在其他标准设备驱动程序上对此提供了很好的支持(参阅 参考资料)。当前,该 API 可以在具有 Linux 2.4 核心或者以前的 2.2.18 核心的 GNU/Linux 版本上工作。因此可支持大多数最新的版本,例如,该 API 可以在没有任何补丁或者升级的 Red Hat 7.2 和 9.0 上工作。 jUSB API 包括以下包: usb.core : 这个包是 jUSB API 的核心部分。它使得 Java 应用程序可以从 USB 主机访问 USB 设备usb.linux : 这个包包含 usb.core.Host 对象的 Linux 实现、bootstrapping 支持和其他可以提升 Linux USB 支持的类。这个实现通过虚拟 USB 文件系统( usbdevfs )访问 USB 设备usb.windows : 这个包包含 usb.core.Host 对象的 Windows 实现、bootstrapping 支持和其他可以提升 Windows USB 支持的类。这个实现仍然处于非常初级的阶段。 usb.remote : 这个包是 usb.core API 的远程版本。它包括一个 RMI proxy 和一个 daemon 应用程序,它让 Java 应用程序可以访问远程计算机上的 USB 设备usb.util : 这个包提供了一些有用的实用程序,可以将 firmware下载到 USB 设备上、将 USB 系统的内容转储到 XML 中、以及将只有 bulk I/O 的 USB 设备工具转换成一个套接字(socket)。 usb.devices : 这个可选包收集了用 jUSB API 访问不同 USB 设备的 Java 代码,包括柯达数码相机和 Rio 500 MP3 播放器。这些 API 经过特别编写以简化访问特定 USB 设备的过程,并且不能用于访问其他设备。这些 API 是在 usb.core API 之上构建的,它们可以工作在所有支持 jUSB 的操作系统上。 usb.view : 这个可选包提供了基于 Swing 的 USB 树简单浏览器。它是一个展示 jUSB API 应用的很好的示例程序。 尽管 usb.core.Host 对象的实现对于不同的操作系统是不同的,但是 Java 程序员只需要理解 usb.core 包就可以用 jUSB API 开始应用程序的开发。表 1 列出了 usb.core 的接口和类,Java 程序员应该熟悉它们: 表 1. jUSB 中的接口和类 接口 说明 Bus 将一组 USB 设备连接到 Host 上 Host 表示具有一个或者多个 Bus 的 USB 控制器 类 说明 Configuration 提供对设备所支持的 USB 配置的访问,以及对与该配置关联的接口的访问 Descriptor 具有 USB 类型的描述符的实体的基类 Device 提供对 USB 设备的访问 DeviceDescriptor 提供对 USB 设备描述符的访问 EndPoint 提供对 USB 端点描述符的访问、在给定设备配置中构造设备数据输入或者输出 HostFactory 包含 bootstrapping 方法 Hub 提供对 USB hub 描述符以及一些 hub 操作的访问 Interface 描述一组端点,并与一个特定设备配置相关联 PortIdentifier 为 USB 设备提供稳定的字符串标识符,以便在操作和故障诊断时使用 用 jUSB API 访问一台 USB 设备的正常过程如下: 通过从 HostFactory 得到 USB Host 进行 Bootstrap。 从 Host 访问 USB Bus ,然后从这个 Bus 访问 USB root hub(即 USB Device )。 得到 hub 上可用的 USB 端口数量,遍历所有端口以找到正确的 Device 。 访问附加到特定端口上的 USB Device 。可以用一台 Device 的 PortIdentifier 直接从 Host 访问它,也可以通过从 root hub 开始遍历 USB Bus 找到它。 用 ControlMessage 与该 Device 直接交互,或者从该 Device 的当前 Configuration 中要求一个 Interface, 并与该 Interface 上可用的 Endpoint 进行 I/O 。 清单 1 展示了如何用 jUSB API 获得 USB 系统中的内容。这个程序编写为只是查看 root hub 上可用的 USB 设备,但是很容易将它改为遍历整个 USB 树。这里的逻辑对应于上述步骤 1 到步骤 4。 清单 1. 用 jUSB API 获得 USB 系统的内容 import usb.core.*; public class ListUSB { public static void main(String[] args) { try { // Bootstrap by getting the USB Host from the HostFactory. Host host = HostFactory.getHost(); // Obtain a list of the USB buses available on the Host. Bus[] bus = host.getBusses(); int total_bus = bus.length; // Traverse through all the USB buses. for (int i=0; i<total_bus; i++) { // Access the root hub on the USB bus and obtain the // number of USB ports available on the root hub. Device root = bus[i].getRootHub(); int total_port = root.getNumPorts(); // Traverse through all the USB ports available on the // root hub. It should be mentioned that the numbering // starts from 1, not 0. for (int j=1; j<=total_port; j++) { // Obtain the Device connected to the port. Device device = root.getChild(j); if (device != null) { // USB device available, do something here. } } } } catch (Exception e) { System.out.println(e.getMessage()); } } 清单 2 展示了在应用程序成功地找到了 Device 的条件下,如何与 Interface 和 EndPoint 进行批量 I/O。 这个代码段也可以修改为执行控制或者中断 I/O。它对应于上述步骤 5。 清单 2. 用 jUSB API 执行批量 I/O if (device != null) { // Obtain the current Configuration of the device and the number of // Interfaces available under the current Configuration. Configuration config = device.getConfiguration(); int total_interface = config.getNumInterfaces(); // Traverse through the Interfaces for (int k=0; k<total_interface; k++) { // Access the currently Interface and obtain the number of // endpoints available on the Interface. Interface itf = config.getInterface(k, 0); int total_ep = itf.getNumEndpoints(); // Traverse through all the endpoints. for (int l=0; l<total_ep; l++) { // Access the endpoint, and obtain its I/O type. Endpoint ep = itf.getEndpoint(l); String io_type = ep.getType(); boolean input = ep.isInput(); // If the endpoint is an input endpoint, obtain its // InputStream and read in data. if (input) { InputStream in; in = ep.getInputStream(); // Read in data here in.close(); } // If the Endpoint is and output Endpoint, obtain its // OutputStream and write out data. else { OutputStream out; out = ep.getOutputStream(); // Write out data here. out.close(); } } } } jUSB 项目在 2000年 6月到 2001年 2月期间非常活跃。该 API 的最新的版本 0.4.4发表于 2001年 2月 14日。从那以后只提出了很少的改进,原因可能是 IBM 小组成功地成为了 Java 语言的候选扩展标准。不过,基于 jUSB 已经开发出一些第三方应用程序,包括 JPhoto 项目(这是一个用 jUSB 连接到数码照相机的应用程序)和 jSyncManager 项目(这是一个用 jUSB 与使用 Palm 操作系统的 PDA 同步的应用程序)。 回页首 JSR-80 API (javax.usb) 正如前面提到的,JSR-80 项目是由 IBM 的 Dan Streetman 于 1999年创立的。2001年,这个项目通过 Java 规范请求(JSR)过程被接受为 Java 语言的候选扩展标准。这个项目现在称为 JSR-80 并且被正式分派了 Java 包 javax.usb 。这个项目使用 Common Public License 的许可证形式,并通过 Java Community Process 进行开发。这个项目的目标是为 Java 平台开发一个 USB 接口,可以从任何 Java 应用程序中完全访问 USB 系统。JSR-80 API 支持 USB 规范所定义的全部四种传输类型。目前,该 API 的 Linux 实现可以在支持 2.4 核心的大多数最新 GNU/Linux 版本上工作,如 Red Hat 7.2 和 9.0。 JSR-80 项目包括三个包: javax-usb ( javax.usb API)、 javax-usb-ri (操作系统无关的基准实现的公共部分)以及 javax-usb-ri-linux (Linux 平台的基准实现,它将公共基准实现链接到 Linux USB 堆栈)。所有这三个部分都是构成 Linux 平台上 java.usb API 完整功能所必需的。在该项目的电子邮件列表中可以看到有人正在致力于将这个 API 移植到其他操作系统上(主要是 Microsoft Windows),但是还没有可以工作的版本发表。 尽管 JSR-80 API 的操作系统无关的实现在不同的操作系统上是不同的,但是 Java 程序员只需要理解 javax.usb 包就可以开始开发应用程序了。表 2 列出了 javax.usb 中的接口和类, Java 程序员应该熟悉它们: 表 2. JSR-80 API 中的接口和类 接口 说明 UsbConfiguration 表示 USB 设备的配置 UsbConfigurationDescriptor USB 配置描述符的接口 UsbDevice USB 设备的接口 UsbDeviceDescriptor USB 设备描述符的接口 UsbEndpoint USB 端点的接口 UsbEndpointDescriptor USB 端点描述符的接口 UsbHub USB hub 的接口 UsbInterface USB 接口的接口 UsbInterfaceDescriptor USB 接口描述符的接口 UsbPipe USB 管道的接口 UsbPort USB 端口的接口 UsbServices javax.usb 实现的接口 类 说明 UsbHostManager javax.usb 的入口点 用 JSR-80 API 访问 USB 设备的正常过程如下: 通过从 UsbHostManager 得到相应的 UsbServices 进行 Bootstrap。 通过 UsbServices 访问 root hub。在应用程序中 root hub 就是一个 UsbHub 。 获得连接到 root hub 的 UsbDevice s 清单。遍历所有低级 hub 以找到正确的 UsbDevice 。 用控制消息( UsbControlIrp )与 UsbDevice 直接交互,或者从 UsbDevice 的相应 UsbConfiguration 中要求一个 UsbInterface 并与该 UsbInterface 上可用的 UsbEndpoint 进行 I/O。 如果一个 UsbEndpoint 用于进行 I/O,那么打开与它关联的 UsbPipe 。通过这个 UsbPipe 可以同步或者异步提交上行数据(从 USB 设备到主计算机)和下行数据(从主计算机到 USB 设备)。 当应用程序不再需要访问该 UsbDevice 时,关闭这个 UsbPipe 并释放相应的 UsbInterface 。 在清单 3 中,我们用 JSR-80 API 获得 USB 系统的内容。这个程序递归地遍历 USB 系统上的所有 USB hub 并找出连接到主机计算机上的所有 USB 设备。这段代码对应于上述步骤 1 到步骤 3。 清单 3. 用 JSR-80 API 获得 USB 系统的内容 import javax.usb.*; import java.util.List; public class TraverseUSB { public static void main(String argv[]) { try { // Access the system USB services, and access to the root // hub. Then traverse through the root hub. UsbServices services = UsbHostManager.getUsbServices(); UsbHub rootHub = services.getRootUsbHub(); traverse(rootHub); } catch (Exception e) {} } public static void traverse(UsbDevice device) { if (device.isUsbHub()) { // This is a USB Hub, traverse through the hub. List attachedDevices = ((UsbHub) device).getAttachedUsbDevices(); for (int i=0; i<attachedDevices.size(); i++) { traverse((UsbDevice) attachedDevices.get(i)); } } else { // This is a USB function, not a hub. // Do something. } } } 清单 4 展示了在应用程序成功地找到 Device 后,如何与 Interface 和 EndPoint 进行 I/O。这段代码还可以修改为进行所有四种数据传输类型的 I/O。它对应于上述步骤 4 到步骤 6。 清单 4. 用 JSR-80 API 进行 I/O public static void testIO(UsbDevice device) { try { // Access to the active configuration of the USB device, obtain // all the interfaces available in that configuration. UsbConfiguration config = device.getActiveUsbConfiguration(); List totalInterfaces = config.getUsbInterfaces(); // Traverse through all the interfaces, and access the endpoints // available to that interface for I/O. for (int i=0; i<totalInterfaces.size(); i++) { UsbInterface interf = (UsbInterface) totalInterfaces.get(i); interf.claim(); List totalEndpoints = interf.getUsbEndpoints(); for (int j=0; j<totalEndpoints.size(); j++) { // Access the particular endpoint, determine the direction // of its data flow, and type of data transfer, and open the // data pipe for I/O. UsbEndpoint ep = (UsbEndpoint) totalEndpoints.get(i); int direction = ep.getDirection(); int type = ep.getType(); UsbPipe pipe = ep.getUsbPipe(); pipe.open(); // Perform I/O through the USB pipe here. pipe.close(); } interf.release(); } } catch (Exception e) {} } JSR-80 项目从一开始就非常活跃。2003年 2月发表了 javax.usb API、RI 和 RI 的 0.10.0 版本。看起来这一版本会提交给 JSR-80 委员会做最终批准。预计正式成为 Java 语言的扩展标准后,其他操作系统上的实现会很快出现。Linux 开发者团体看来对 JSR-80 项目的兴趣比 jUSB 项目更大,使用 Linux 平台的 javax.usb API 的项目数量在不断地增加。 回页首 结束语 jUSB API 和 JSR-80 API 都为应用程序提供了从运行 Linux 操作系统的计算机中访问 USB 设备的能力。JSR-80 API 提供了比 jUSB API 更多的功能,很有可能成为 Java 语言的扩展标准。目前,只有 Linux 开发人员可以利用 jUSB 和 JSR-80 API 的功能。不过,有人正在积极地将这两种 API 移植到其他操作系统上。Java 开发人员应该在不久就可以在其他操作系统上访问 USB 设备。从现在起就熟悉这些 API,当这些项目可以在多个平台上发挥作用时,您就可以在自己的应用程序中加入 USB 功能了。 参考资料 您可以参阅本文在 developerWorks 全球站点上的 英文原文. 有关 USB 规范的更多信息,请访问 USB.org。 访问 SourceForge 上的 jUSB 项目主页。 有关 JSR-80 项目的更多信息,请访问其 主页或者其 在 Java Community Process 中的页面。 查找更多有关 jPhoto 项目的内容。 了解 jSyncManager项目。 有关 JMF 项目的更多内容,参阅 Eric Olson 的全面性的“ Java Media Framework 基础”教程( developerWorks,2002年 5月)。 可以在 developerWorks Java 技术专区 中找到关于 Java 编程各个方面的数百篇文章。 加入 developerWorks 中文社区,查看开发人员推动的博客、论坛、组和维基,并与其他 developerWorks 用户交流。 条评论 请 登录 或 注册 后发表评论。 添加评论: 注意:评论中不支持 HTML 语法 有新评论时提醒我剩余 1000 字符 共有评论 (1) 非常不错! 由 javaku 于 2012年05月28日发布 报告滥用 IBM PureSystems IBM PureSystems™ 系列解决方案是一个专家集成系统 developerWorks 学习路线图 通过学习路线图系统掌握软件开发技能 软件下载资源中心 软件下载、试用版及云计算 回页首 帮助 联系编辑 提交内容 订阅源 在线浏览每周时事通讯 新浪微博 报告滥用 使用条款 第三方提示 隐私条约 浏览辅助 IBM 教育学院教育培养计划 IBM 创业企业全球扶持计划 ISV 资源 (英语) dW 中国每周时事通讯 选择语言: English 中文 日本語

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值