Chapter intention of communicating with line printers USB makes no such assumptions and is suitable for just about any peripheral type For communicating with common peripherals such as printers, keyboards, and drives, USB classes specify device requirements and protocols Developers can program a device to conform to a class specification instead of having to reinvent everything from the ground up 1RGTCVKPI 5[UVGO 5WRRQTV This book focuses on Windows programming for PCs, but other computers and operating systems also have USB support, including Linux and Apple Computer’s Macintosh Some real-time kernels also support USB At the most basic level, an operating system that supports USB must three things: • Detect when devices are attached to and removed from the system • Communicate with newly attached devices to find out how to exchange data with them • Provide a mechanism that enables software drivers to pass communications between the USB hardware and applications that want to access USB peripherals At a higher level, operating-system support may also mean the inclusion of class drivers that enable applications to access specific types of devices If the operating system doesn’t include a driver appropriate for a specific device, the device vendor must provide the driver Microsoft continues to improve and add to the class drivers included with Windows Supported device types include human interface devices (keyboards, mice, game controllers), speakers and other audio devices, modems, drives, still-image and video cameras, scanners, printers, and smart-card readers Filter drivers can support device-specific features and abilities within a class Applications use Application Programming Interface (API) functions or other software components to access devices via their drivers Devices that have vendor-specific functions can sometimes use a supported class such as the communications-device or human-interface device class Other options for vendor-specific functions include Microsoft’s WinUSB driver and generic drivers from other sources Some chip companies provide drivers that developers can use with the company’s chips USB Basics Writers of USB device drivers for Windows can use Microsoft’s Windows Driver Foundation (WDF) model The WDF provides a framework that simplifies the task of writing drivers &GXKEG 5WRRQTV On the device side, the hardware must include a controller chip that manages USB communications The device is responsible for responding to requests that identify and configure the device and for reading and writing other data on the bus Some controllers perform some functions entirely in hardware Many USB controllers are based on popular microcontroller architectures such as Intel Corporation’s 8051 or Microchip Technology’s PIC® with added hardware support for USB communications Other controllers don’t contain a CPU but instead provide a serial or parallel interface to an external microcontroller If you’re already familiar with a chip architecture that has a USB-capable variant, you don’t need to learn a new architecture Most chip companies provide example code to help you get started 75$ +ORNGOGPVGTU (QTWO The USB Implementers Forum, Inc., or USB-IF (www.usb.org), is the non-profit corporation founded by the companies that developed the USB specification The USB-IF’s mission is to support the advancement and adoption of USB technology To that end, the USB-IF offers information, tools, and testing support The information includes the specification documents, white papers, FAQs, and a Web forum The tools include software and hardware to help in developing and testing products The support for testing includes compliance tests to verify proper operation and compliance workshops where developers can have their products tested and certified to display a USB logo 9JCV 75$ %CPŏV &Q All of USB’s advantages mean that it’s a good candidate for many devices But a single interface can’t handle every task +PVGTHCEG KOKVU Limits of USB include distance constraints, no support for peer-to-peer communications or broadcasting, and lack of support in older hardware and operating systems Chapter Distance USB was designed as a desktop-expansion bus where devices are relatively close at hand Other interfaces, including RS-232, RS-485, IEEE-1394b, and Ethernet, allow much longer cables To extend the distance between a device and its host computer, an option is to use USB to connect to a nearby device that functions as a bridge to a long-distance interface to the end circuits Peer-to-Peer Communications Every USB communication is between a host computer and a device (except for one option introduced with USB 3.0) The host is a PC or other computer with host-controller hardware The device contains device-controller hardware Hosts can’t talk to each other directly, and devices can’t talk to each other directly Other interfaces, such as IEEE-1394, allow direct device-to-device communication USB provides a partial solution with the USB On-The-Go option An On-The-Go device can function as both a device and a limited-capability host that communicates with other devices Two USB hosts can communicate with each other via a bridge cable that contains two USB devices with a shared buffer USB 3.0 defines a new host-to-host cable for SuperSpeed With driver support, this cable can support host-to-host communications Broadcasting USB doesn’t support sending data simultaneously to multiple devices (except for USB 3.0 timestamp packets) The host must send the data to each device individually If you need broadcasting ability, use IEEE-1394 or Ethernet Legacy Hardware Older “legacy” computers and peripherals don’t have USB ports The issue of supporting legacy equipment has faded, however, as older systems are retired If you need to connect a legacy peripheral to a USB port, a solution is an intelligent adapter that converts between USB and the older interface Several sources have adapters for use with peripherals with RS-232, RS-485, and parallel ports An adapter is useful only for devices that use protocols supported by the adapter’s device driver For example, most parallel-port adapters support communications only with printers, not with other parallel-port peripherals RS-232 adapters work with most RS-232 devices If you want to use a USB device with a computer that doesn’t support USB, a solution is to add USB capabilities to the computer To so, you need to add USB host-controller hardware and use an operating system that supports USB The hardware is available on expansion cards that plug into a PCI slot or on a USB Basics replacement motherboard For Windows systems, the edition must be Windows 98 or later If upgrading the PC to support USB isn’t feasible, you might think an adapter would be available to translate a peripheral’s USB interface to the PC’s RS-232, parallel, or other interface An adapter is rarely an option when the computer has the legacy interface because an adapter that contains host-controller hardware and code is too expensive to design and manufacture for its limited market Even on new systems, users may occasionally run applications on older operating systems such as DOS Without a driver, the operating system can’t access a USB device Although it’s possible to write a USB driver for DOS, few device vendors provide one An exception is mice and keyboards, which the system BIOS typically supports to ensure that the devices are usable any time, including from within DOS and from the BIOS screens that you can view on boot-up &GXGNQRGT %JCNNGPIGU For developers, challenges to USB are the complexity of the protocols, operating-system support for some applications, and for small-scale developers, the need to obtain a Vendor ID Protocol Complexity A USB device is an intelligent device that must respond to requests and other events on the bus Controller chips vary in how much firmware support they require to perform USB communications In most cases, to program a USB device, you need to be familiar with the USB protocols for exchanging data on the bus On the host-computer side, device drivers insulate application programmers from having to know many of the low-level details about the protocols and hardware interface Device-driver writers need to be familiar with USB protocols In contrast, some older interfaces can connect to very simple circuits that software addresses directly For example, the PC’s original parallel printer port is a series of digital inputs and outputs You can connect to input and output circuits such as relays, switches, and analog-to-digital converters with no computer intelligence required on the device side With a driver to enable port access, applications can monitor and control the individual bits on the ports USB is a shared bus with defined protocols, and the operating system prevents applications from directly accessing the hardware To access a USB device, applications must communicate with a class or device driver that in turn com- Chapter municates with the lower-level USB drivers that manage communications on the bus Devices must support protocols that enable the PC to detect, identify, and communicate with the device Evolving Support in the Operating System The class drivers included with Windows enable applications to communicate with many devices Often, you can design a device to use one of the provided drivers If not, you may be able to use or adapt a driver provided by a chip company or other source If you need to provide your own driver, a third-party driver toolkit can help in developing the driver Fees The USB-IF’s website provides the USB specifications, related documents, software for compliance testing, and much more at no charge Anyone can develop USB software without paying a licensing fee Every USB device contains a Vendor ID and a Product ID that identify the device to the operating system At this writing, the USB-IF charges a $2000 administrative fee for the rights to a Vendor ID The owner of the Vendor ID assigns Product IDs Joining the USB-IF (at $4000/year) gets you a Vendor ID along with other benefits such as admittance to compliance workshops Devices that don’t undergo compliance testing and don’t display the USB-IF logo have lower-cost options Some chip companies, including Future Technology Devices International Limited (FTDI) and Microchip Technology, will assign a range of Product IDs to a customer for use in products with the company's Vendor ID, typically at no charge Chips that perform all of their USB communications in hardware can use a Vendor ID and Product ID embedded in the hardware An example is FTDI's USB device controllers Companies that sell products that implement a USB specification must sign an adopters agreement The agreement grants a royalty-free, non-exclusive patent license to implement the specification You must submit a signed agreement within the later of one year after first sale of a product or one year after the specification’s release See the agreements (at www.usb.org) for the legal specifics 75$ XGTUWU 'VJGTPGV For some devices, the choice is between USB and Ethernet Ethernet’s advantages include the ability to use very long cables, support for broadcasting, and familiar Internet protocols Ethernet hardware is more complex and expensive than typical USB device hardware, however USB is also more versatile, with four transfer types and defined classes for different device functions 10 USB Basics 75$ XGTUWU +''' Another interface option for some devices is IEEE-1394 Apple Computer’s implementation of the interface is called Firewire Advantages to IEEE-1394 are support for peer-to-peer communications and broadcasting and more bus power available to devices (up to 1.5A at 30V) Compared to USB, where a host computer manages the interface, IEEE-1394 devices have more responsibilities and thus tend to be more complex and expensive to implement SuperSpeed USB exceeds IEEE-1394b’s bus speed of 3.2 Gbps While every new PC has USB ports, IEEE-1394 ports are less common and thus may require adding ports on expansion cards For some devices, such as drives, either interface works well, and some devices support both interfaces 'XQNWVKQP QH CP +PVGTHCEG The main reason why new interfaces don’t appear very often is that existing interfaces have the advantage of all of the peripherals that users don’t want to scrap By choosing compatibility with the existing Centronics parallel interface and RS-232 serial-port interface, the developers of the original IBM PC sped up the design process and enabled users to connect to printers and modems already on the market These interfaces proved serviceable for close to two decades But as computers became more powerful and the number and kinds of peripherals increased, the older interfaces became a bottleneck of slow communications with limited options for expansion A break with tradition makes sense when the desire for enhancements is greater than the inconvenience and expense of change This is the situation that prompted the development of USB 75$ The Universal Serial Bus Specification Revision 1.0 was released in January 1996 USB capability first became available on PCs with the release of Windows 95’s OEM Service Release 2, available only to vendors installing Windows 95 on PCs they sold The USB support in these versions was limited and buggy, and there weren’t many USB peripherals available, so use of USB was limited in this era The situation improved with the release of Windows 98 in June 1998 By this time, many more vendors had USB peripherals available, and USB began to 11 Chapter take hold as a popular interface Windows 98 Second Edition (SE) fixed bugs and further enhanced the USB support The original edition of Windows 98 is called Windows 98 Gold to distinguish it from Windows 98 SE This book concentrates on PCs running Windows XP and later Much of the information also applies to Windows 98, Windows 2000, and Windows Me Windows NT never supported USB except via third-party software However, all of the editions mentioned above except Windows 95/98/Me are considered NT-based Windows editions because they build on the Windows NT kernel In this book, the term PC encompasses all of the various computers that share the common ancestor of the original IBM PC A host computer is any computer that can communicate with USB devices 75$ The Universal Serial Bus Specification Revision 1.1 (September 1998) added one new transfer type (interrupt OUT) In this book, USB 1.x refers to USB 1.0 and 1.1 75$ As USB gained in popularity and PCs became more powerful, demand grew for a faster bus speed Investigation showed that a bus speed 40× faster than full speed could remain backwards-compatible with the low- and full-speed interfaces April 2000 saw the release of the Universal Serial Bus Specification Revision 2.0, which added high speed at 480 Mbps High speed made USB more attractive for peripherals such as printers, disk drives, and video cameras Windows added support for USB 2.0 in Windows XP SP2 The USB 2.0 specification replaced USB 1.1 Except for hubs, a USB 2.0 device can support low speed, full speed, or high speed, and a high-speed-capable device can support full speed when connected to a USB 1.x bus A USB 2.0 hub must support all three USB 2.0 speeds The ability to communicate at any speed increases the complexity of the hubs but conserves bus bandwidth and eliminates a need to use different hubs for different speeds USB 2.0 is backwards compatible with USB 1.x In other words, USB 2.0 devices can use the same connectors and cables as 1.x devices, and a USB 2.0 device works when connected to a PC that supports USB 1.x or USB 2.0, except for a few devices that function only at high speed and thus require USB 2.0 support 12 USB Basics When USB 2.0 devices first became available, there was confusion among users about whether all USB 2.0 devices supported high speed To reduce confusion, the USB-IF released naming and packaging recommendations that emphasize speed and compatibility rather than USB version numbers A product that supports high speed should be labeled “Hi-Speed USB,” and messages on the packaging might include Fully compatible with Original USB and Compatible with the USB 2.0 Specification For products that support low or full speed only, the recommended messages on packaging are Compatible with the USB 2.0 Specification and Works with USB and Hi-Speed USB systems, peripherals and cables The recommendations advise avoiding references to low or full speed on consumer packaging To use high speed, a high-speed-capable device must connect to a USB 2.0 or USB 3.0 host computer with only USB 2.0 or USB 3.0 hubs between the host and device USB 2.0 and USB 3.0 hosts and hubs can also communicate with USB 1.x devices The USB-IF releases revisions and additions to the USB specification via Engineering Change Notices (ECNs) Table 1-2 lists ECNs to the USB 2.0 specification 75$ The Universal Serial Bus 3.0 Specification Revision 1.0 was released in November 2008, with the first USB 3.0 device-controller hardware expected to follow about a year later Windows will likely support USB 3.0 sometime after the release of Windows 7, the successor to Windows Vista USB 3.0 defines a new dual-bus architecture with two physical buses that operate in parallel USB 3.0 provides a pair of wires for USB 2.0 traffic and additional wires to support the new SuperSpeed bus at Gbps SuperSpeed offers a more than 10× increase over USB 2.0’s high speed Plus, unlike USB 2.0, SuperSpeed has a pair of wires for each direction and can transfer data in both directions at the same time USB 3.0 also increases the amount of bus current devices can draw and defines protocols for more aggressive power saving and more efficient transfers USB 3.0 is backwards compatible with USB 2.0 USB 3.0 hosts and hubs support all four speeds USB 2.0 cables fit USB 3.0 receptacles USB 3.0 supplements, but doesn’t replace, USB 2.0 Low, full, and high-speed devices continue to comply with USB 2.0 and can’t take advantage of USB 3.0’s features such as higher bus-current limits and larger data packets 13 Chapter Table 1-2: Engineering change notices (ECNs) correct, add to, and clarify the USB 2.0 specification 6KVNG &CVG Mini-B Connector 10/2000 Errata 12/2000 Pull-up/Pull-Down Resistors (loosened tolerances) 05/2002 Interface Association Descriptor 05/2003 Rounded Chamfer for the Mini-B Plug (recommendation) 10/2003 Unicode UTF-16LE for String Descriptors 02/2005 Inter-Chip USB Supplement (chip-to-chip interconnects without external cables) 03/2006 USB On-The-Go V1.3 (defines devices that can also function as hosts) 12/2006 Micro-USB connector 04/2007 Link Power Management (optional power saving capabilities) 07/2007 Hi-Speed Interchip Electrical Specification (chip-to-chip interconnects without external cables) 09/2007 Suspend Current Limit Changes 04/2008 USB 2.0 Phase-locked SOFs 12/2008 5V Short Circuit Withstand Requirement Change 12/2008 Device Capacitance 12/2008 Material Change 12/2008 MicroUSB Micro-B ID Pin Resistance and Tolerance stack-up between D+ and D- 12/2008 75$ 1P6JG)Q As USB became the interface of choice for all kinds of peripherals, developers began to ask for a way for USB peripherals to access other USB devices For example, a user might want to attach a printer to a camera or a keyboard to a PDA The On-The-Go (OTG) Supplement to the USB 2.0 Specification defines a limited-capability host function that devices can implement to enable communicating with USB peripherals 9KTGNGUU 75$ Developers who want to design devices with wireless interfaces have several choices The Wireless USB Promoter Group’s Wireless Universal Serial Bus Specification defines the Certified Wireless USB (WUSB) interface for communicating at up to 480 Mbps Cypress Semiconductor's WirelessUSB enables 14 USB Basics implementing wireless devices that function as low-speed USB devices Another option is to use an adapter that converts between USB and a wireless interface such as Zigbee, Bluetooth, or WiFi $WU %QORQPGPVU USB communications require a host computer with USB support, one or more devices with USB ports, and hubs, connectors, and cables as needed to connect the devices to the host computer The host computer is a PC or other computer that contains a USB host controller and a root hub The host controller formats data for transmitting on the bus and translates received data to a format that operating-system components understand The host controller also helps manage communications on the bus The root hub has one or more connectors for attaching devices The root hub and host controller together detect attached and removed devices, carry out requests from the host controller, and pass data between devices and the host controller In addition to the root hub, a bus may have one or more external hubs Each device has hardware and firmware as needed to communicate with the host computer The USB specifications define the cables and connectors that connect devices to their hubs 6QRQNQI[ The topology, or arrangement of connections, on the bus is a tiered star (Figure 1-1) At the center of each star is a hub, and each connection to the hub is a point on the star The root hub is in the host An external hub has one upstream (host-side) connector for communicating with the host and one or more downstream (device-side) connectors or internal connections to embedded devices A typical hub has two, four, or seven ports When multiple hubs connect in series, you can think of the series as a tier, one above the next The tiered star describes only the physical connections In programming, all that matters is the logical connection Host applications and device firmware don’t need to know or care whether the communication passes through one hub or five Up to five external hubs can connect in series with a limit of 127 peripherals and hubs including the root hub However, bandwidth and scheduling limits can prevent a single host controller from communicating with this many 15 ... connect to a USB 2.0 or USB 3.0 host computer with only USB 2.0 or USB 3.0 hubs between the host and device USB 2.0 and USB 3.0 hosts and hubs can also communicate with USB 1.x devices The USB- IF releases... transfers USB 3.0 is backwards compatible with USB 2.0 USB 3.0 hosts and hubs support all four speeds USB 2.0 cables fit USB 3.0 receptacles USB 3.0 supplements, but doesn’t replace, USB 2.0 Low,... connected to a PC that supports USB 1.x or USB 2.0, except for a few devices that function only at high speed and thus require USB 2.0 support 12 USB Basics When USB 2.0 devices first became available,