Import 2.3.4pre3
[davej-history.git] / drivers / usb / usb.h
blob06efc1a2810181977b0e6546fea3fd8d143c8e4d
1 #ifndef __LINUX_USB_H
2 #define __LINUX_USB_H
4 #include <linux/config.h>
5 #include <linux/types.h>
6 #include <linux/list.h>
7 #include <linux/sched.h>
9 extern int usb_hub_init(void);
10 extern int usb_kbd_init(void);
11 extern int usb_cpia_init(void);
12 extern int usb_mouse_init(void);
13 extern int usb_printer_init(void);
15 extern void hub_cleanup(void);
16 extern void usb_mouse_cleanup(void);
18 static __inline__ void wait_ms(unsigned int ms)
20 current->state = TASK_UNINTERRUPTIBLE;
21 schedule_timeout(1 + ms * HZ / 1000);
25 typedef struct {
26 unsigned char requesttype;
27 unsigned char request;
28 unsigned short value;
29 unsigned short index;
30 unsigned short length;
31 } devrequest;
34 * Class codes
36 #define USB_CLASS_HUB 9
39 * Descriptor types
41 #define USB_DT_DEVICE 0x01
42 #define USB_DT_CONFIG 0x02
43 #define USB_DT_STRING 0x03
44 #define USB_DT_INTERFACE 0x04
45 #define USB_DT_ENDPOINT 0x05
47 #define USB_DT_HUB 0x29
48 #define USB_DT_HID 0x21
51 * Standard requests
53 #define USB_REQ_GET_STATUS 0x00
54 #define USB_REQ_CLEAR_FEATURE 0x01
55 /* 0x02 is reserved */
56 #define USB_REQ_SET_FEATURE 0x03
57 /* 0x04 is reserved */
58 #define USB_REQ_SET_ADDRESS 0x05
59 #define USB_REQ_GET_DESCRIPTOR 0x06
60 #define USB_REQ_SET_DESCRIPTOR 0x07
61 #define USB_REQ_GET_CONFIGURATION 0x08
62 #define USB_REQ_SET_CONFIGURATION 0x09
63 #define USB_REQ_GET_INTERFACE 0x0A
64 #define USB_REQ_SET_INTERFACE 0x0B
65 #define USB_REQ_SYNCH_FRAME 0x0C
68 * HIDD requests
70 #define USB_REQ_GET_REPORT 0x01
71 #define USB_REQ_GET_IDLE 0x02
72 #define USB_REQ_GET_PROTOCOL 0x03
73 #define USB_REQ_SET_REPORT 0x09
74 #define USB_REQ_SET_IDLE 0x0A
75 #define USB_REQ_SET_PROTOCOL 0x0B
77 #define USB_TYPE_STANDARD (0x00 << 5)
78 #define USB_TYPE_CLASS (0x01 << 5)
79 #define USB_TYPE_VENDOR (0x02 << 5)
80 #define USB_TYPE_RESERVED (0x03 << 5)
82 #define USB_RECIP_DEVICE 0x00
83 #define USB_RECIP_INTERFACE 0x01
84 #define USB_RECIP_ENDPOINT 0x02
85 #define USB_RECIP_OTHER 0x03
88 * Request target types.
90 #define USB_RT_DEVICE 0x00
91 #define USB_RT_INTERFACE 0x01
92 #define USB_RT_ENDPOINT 0x02
94 #define USB_RT_HUB (USB_TYPE_CLASS | USB_RECIP_DEVICE)
95 #define USB_RT_PORT (USB_TYPE_CLASS | USB_RECIP_OTHER)
97 #define USB_RT_HIDD (USB_TYPE_CLASS | USB_RECIP_INTERFACE)
100 * USB device number allocation bitmap. There's one bitmap
101 * per USB tree.
103 struct usb_devmap {
104 unsigned long devicemap[128 / (8*sizeof(unsigned long))];
108 * This is a USB device descriptor.
110 * USB device information
114 #define USB_MAXCONFIG 8
115 #define USB_MAXINTERFACES 32
116 #define USB_MAXENDPOINTS 32
117 #define USB_MAXSTRINGS 16
119 struct usb_device_descriptor {
120 __u8 bLength;
121 __u8 bDescriptorType;
122 __u16 bcdUSB;
123 __u8 bDeviceClass;
124 __u8 bDeviceSubClass;
125 __u8 bDeviceProtocol;
126 __u8 bMaxPacketSize0;
127 __u16 idVendor;
128 __u16 idProduct;
129 __u16 bcdDevice;
130 __u8 iManufacturer;
131 __u8 iProduct;
132 __u8 iSerialNumber;
133 __u8 bNumConfigurations;
136 /* Endpoint descriptor */
137 struct usb_endpoint_descriptor {
138 __u8 bLength;
139 __u8 bDescriptorType;
140 __u8 bEndpointAddress;
141 __u8 bmAttributes;
142 __u16 wMaxPacketSize;
143 __u8 bInterval;
144 void *audio;
147 /* Interface descriptor */
148 struct usb_interface_descriptor {
149 __u8 bLength;
150 __u8 bDescriptorType;
151 __u8 bInterfaceNumber;
152 __u8 bAlternateSetting;
153 __u8 bNumEndpoints;
154 __u8 bInterfaceClass;
155 __u8 bInterfaceSubClass;
156 __u8 bInterfaceProtocol;
157 __u8 iInterface;
159 struct usb_endpoint_descriptor *endpoint;
160 void *audio;
163 /* Configuration descriptor information.. */
164 struct usb_config_descriptor {
165 __u8 bLength;
166 __u8 bDescriptorType;
167 __u16 wTotalLength;
168 __u8 bNumInterfaces;
169 __u8 bConfigurationValue;
170 __u8 iConfiguration;
171 __u8 bmAttributes;
172 __u8 MaxPower;
174 struct usb_interface_descriptor *interface;
177 /* String descriptor */
178 struct usb_string_descriptor {
179 __u8 bLength;
180 __u8 bDescriptorType;
181 __u16 wData[1];
184 /* Hub descriptor */
185 struct usb_hub_descriptor {
186 __u8 bLength;
187 __u8 bDescriptorType;
188 __u8 bNbrPorts;
189 __u16 wHubCharacteristics;
190 __u8 bPwrOn2PwrGood;
191 __u8 bHubContrCurrent;
192 /* DeviceRemovable and PortPwrCtrlMask want to be variable-length
193 bitmaps that hold max 256 entries, but for now they're ignored */
194 __u8 filler;
197 struct usb_device;
199 struct usb_driver {
200 const char * name;
201 int (*probe)(struct usb_device *);
202 void (*disconnect)(struct usb_device *);
203 struct list_head driver_list;
207 * Pointer to a device endpoint interrupt function -greg
208 * Parameters:
209 * int status - This needs to be defined. Right now each HCD
210 * passes different transfer status bits back. Don't use it
211 * until we come up with a common meaning.
212 * void *buffer - This is a pointer to the data used in this
213 * USB transfer.
214 * void *dev_id - This is a user defined pointer set when the IRQ
215 * is requested that is passed back.
217 typedef int (*usb_device_irq)(int, void *, void *);
219 struct usb_operations {
220 struct usb_device *(*allocate)(struct usb_device *);
221 int (*deallocate)(struct usb_device *);
222 int (*control_msg)(struct usb_device *, unsigned int, void *, void *, int);
223 int (*bulk_msg)(struct usb_device *, unsigned int, void *, int,unsigned long *);
224 int (*request_irq)(struct usb_device *, unsigned int, usb_device_irq, int, void *);
228 * Allocated per bus we have
230 struct usb_bus {
231 struct usb_devmap devmap; /* Device map */
232 struct usb_operations *op; /* Operations (specific to the HC) */
233 struct usb_device *root_hub; /* Root hub */
234 struct list_head bus_list;
235 void *hcpriv; /* Host Controller private data */
239 #define USB_MAXCHILDREN (8)
241 struct usb_device {
242 int devnum; /* Device number on USB bus */
243 int slow; /* Slow device? */
244 int maxpacketsize; /* Maximum packet size */
245 __u16 toggle; /* one bit for each endpoint */
246 struct usb_config_descriptor *actconfig; /* the active configuration */
247 int epmaxpacket[16]; /* endpoint specific maximums */
248 int ifnum; /* active interface number */
249 struct usb_bus *bus; /* Bus we're apart of */
250 struct usb_driver *driver; /* Driver */
251 struct usb_device_descriptor descriptor; /* Descriptor */
252 struct usb_config_descriptor *config; /* All of the configs */
253 struct usb_device *parent;
254 char *stringtable; /* Strings (multiple, null term) */
255 char **stringindex; /* pointers to strings */
256 int maxstring; /* max valid index */
259 * Child devices - these can be either new devices
260 * (if this is a hub device), or different instances
261 * of this same device.
263 * Each instance needs its own set of data structures.
266 int maxchild; /* Number of ports if hub */
267 struct usb_device *children[USB_MAXCHILDREN];
269 void *hcpriv; /* Host Controller private data */
270 void *private; /* Upper layer private data */
273 extern int usb_register(struct usb_driver *);
274 extern void usb_deregister(struct usb_driver *);
276 extern void usb_register_bus(struct usb_bus *);
277 extern void usb_deregister_bus(struct usb_bus *);
279 extern int usb_request_irq(struct usb_device *, unsigned int, usb_device_irq, int, void *);
281 extern void usb_init_root_hub(struct usb_device *dev);
282 extern void usb_connect(struct usb_device *dev);
283 extern void usb_disconnect(struct usb_device **);
285 extern int usb_device_descriptor(struct usb_device *dev);
286 void usb_check_support(struct usb_device *);
287 void usb_driver_purge(struct usb_driver *,struct usb_device *);
288 extern int usb_parse_configuration(struct usb_device *dev, void *buf, int len);
289 extern void usb_destroy_configuration(struct usb_device *dev);
292 * Calling this entity a "pipe" is glorifying it. A USB pipe
293 * is something embarrassingly simple: it basically consists
294 * of the following information:
295 * - device number (7 bits)
296 * - endpoint number (4 bits)
297 * - current Data0/1 state (1 bit)
298 * - direction (1 bit)
299 * - speed (1 bit)
300 * - max packet size (2 bits: 8, 16, 32 or 64)
301 * - pipe type (2 bits: control, interrupt, bulk, isochronous)
303 * That's 18 bits. Really. Nothing more. And the USB people have
304 * documented these eighteen bits as some kind of glorious
305 * virtual data structure.
307 * Let's not fall in that trap. We'll just encode it as a simple
308 * unsigned int. The encoding is:
310 * - device: bits 8-14
311 * - endpoint: bits 15-18
312 * - Data0/1: bit 19
313 * - direction: bit 7 (0 = Host-to-Device, 1 = Device-to-Host)
314 * - speed: bit 26 (0 = High, 1 = Low Speed)
315 * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64)
316 * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt, 10 = control, 11 = bulk)
318 * Why? Because it's arbitrary, and whatever encoding we select is really
319 * up to us. This one happens to share a lot of bit positions with the UCHI
320 * specification, so that much of the uhci driver can just mask the bits
321 * appropriately.
324 #define usb_maxpacket(dev,pipe) ((dev)->epmaxpacket[usb_pipeendpoint(pipe)])
325 #define usb_packetid(pipe) (((pipe) & 0x80) ? 0x69 : 0xE1)
327 #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
328 #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
329 #define usb_pipedata(pipe) (((pipe) >> 19) & 1)
330 #define usb_pipeout(pipe) (((pipe) & 0x80) == 0)
331 #define usb_pipeslow(pipe) (((pipe) >> 26) & 1)
333 #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
334 #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == 0)
335 #define usb_pipeint(pipe) (usb_pipetype((pipe)) == 1)
336 #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == 2)
337 #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == 3)
339 #define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff)
341 /* The D0/D1 toggle bits */
342 #define usb_gettoggle(dev, ep) (((dev)->toggle >> ep) & 1)
343 #define usb_dotoggle(dev, ep) ((dev)->toggle ^= (1 << ep))
344 #define usb_settoggle(dev, ep, bit) ((dev)->toggle = ((dev)->toggle & (0xfffe << ep)) | (bit << ep))
346 static inline unsigned int __create_pipe(struct usb_device *dev, unsigned int endpoint)
348 return (dev->devnum << 8) | (endpoint << 15) | (dev->slow << 26) | dev->maxpacketsize;
351 static inline unsigned int __default_pipe(struct usb_device *dev)
353 return (dev->slow << 26);
356 /* Create control pipes.. */
357 #define usb_sndctrlpipe(dev,endpoint) ((2 << 30) | __create_pipe(dev,endpoint))
358 #define usb_rcvctrlpipe(dev,endpoint) ((2 << 30) | __create_pipe(dev,endpoint) | 0x80)
359 #define usb_sndisocpipe(dev,endpoint) ((0 << 30) | __create_pipe(dev,endpoint))
360 #define usb_rcvisocpipe(dev,endpoint) ((0 << 30) | __create_pipe(dev,endpoint) | 0x80)
361 #define usb_sndbulkpipe(dev,endpoint) ((3 << 30) | __create_pipe(dev,endpoint))
362 #define usb_rcvbulkpipe(dev,endpoint) ((3 << 30) | __create_pipe(dev,endpoint) | 0x80)
363 #define usb_snddefctrl(dev) ((2 << 30) | __default_pipe(dev))
364 #define usb_rcvdefctrl(dev) ((2 << 30) | __default_pipe(dev) | 0x80)
367 * Send and receive control messages..
369 void usb_new_device(struct usb_device *dev);
370 int usb_set_address(struct usb_device *dev);
371 int usb_get_descriptor(struct usb_device *dev, unsigned char desctype, unsigned
372 char descindex, void *buf, int size);
373 int usb_get_device_descriptor(struct usb_device *dev);
374 int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size);
375 int usb_clear_port_feature(struct usb_device *dev, int port, int feature);
376 int usb_set_port_feature(struct usb_device *dev, int port, int feature);
377 int usb_get_hub_status(struct usb_device *dev, void *data);
378 int usb_get_port_status(struct usb_device *dev, int port, void *data);
379 int usb_get_protocol(struct usb_device *dev);
380 int usb_set_protocol(struct usb_device *dev, int protocol);
381 int usb_set_idle(struct usb_device *dev, int duration, int report_id);
382 int usb_set_configuration(struct usb_device *dev, int configuration);
383 int usb_get_report(struct usb_device *dev);
386 * Debugging helpers..
388 void usb_show_device_descriptor(struct usb_device_descriptor *);
389 void usb_show_config_descriptor(struct usb_config_descriptor *);
390 void usb_show_interface_descriptor(struct usb_interface_descriptor *);
391 void usb_show_endpoint_descriptor(struct usb_endpoint_descriptor *);
392 void usb_show_hub_descriptor(struct usb_hub_descriptor *);
393 void usb_show_device(struct usb_device *);
394 void usb_show_string(struct usb_device* dev, char *id, int index);
397 * Audio parsing helpers
400 #ifdef CONFIG_USB_AUDIO
401 void usb_audio_interface(struct usb_interface_descriptor *, u8 *);
402 void usb_audio_endpoint(struct usb_endpoint_descriptor *, u8 *);
403 #else
404 extern inline void usb_audio_interface(struct usb_interface_descriptor *interface, u8 *data) {}
405 extern inline void usb_audio_endpoint(struct usb_endpoint_descriptor *interface, u8 *data) {}
406 #endif
408 #endif