Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
usb.h
Go to the documentation of this file.
1 #ifndef __LINUX_USB_H
2 #define __LINUX_USB_H
3 
5 #include <linux/usb/ch9.h>
6 
7 #define USB_MAJOR 180
8 #define USB_DEVICE_MAJOR 189
9 
10 
11 #ifdef __KERNEL__
12 
13 #include <linux/errno.h> /* for -ENODEV */
14 #include <linux/delay.h> /* for mdelay() */
15 #include <linux/interrupt.h> /* for in_interrupt() */
16 #include <linux/list.h> /* for struct list_head */
17 #include <linux/kref.h> /* for struct kref */
18 #include <linux/device.h> /* for struct device */
19 #include <linux/fs.h> /* for struct file_operations */
20 #include <linux/completion.h> /* for struct completion */
21 #include <linux/sched.h> /* for current && schedule_timeout */
22 #include <linux/mutex.h> /* for struct mutex */
23 #include <linux/pm_runtime.h> /* for runtime PM */
24 
25 struct usb_device;
26 struct usb_driver;
27 struct wusb_dev;
28 
29 /*-------------------------------------------------------------------------*/
30 
31 /*
32  * Host-side wrappers for standard USB descriptors ... these are parsed
33  * from the data provided by devices. Parsing turns them from a flat
34  * sequence of descriptors into a hierarchy:
35  *
36  * - devices have one (usually) or more configs;
37  * - configs have one (often) or more interfaces;
38  * - interfaces have one (usually) or more settings;
39  * - each interface setting has zero or (usually) more endpoints.
40  * - a SuperSpeed endpoint has a companion descriptor
41  *
42  * And there might be other descriptors mixed in with those.
43  *
44  * Devices may also have class-specific or vendor-specific descriptors.
45  */
46 
47 struct ep_device;
48 
64 struct usb_host_endpoint {
66  struct usb_ss_ep_comp_descriptor ss_ep_comp;
67  struct list_head urb_list;
68  void *hcpriv;
69  struct ep_device *ep_dev; /* For sysfs info */
70 
71  unsigned char *extra; /* Extra descriptors */
72  int extralen;
73  int enabled;
74 };
75 
76 /* host-side wrapper for one interface setting's parsed descriptors */
77 struct usb_host_interface {
79 
80  int extralen;
81  unsigned char *extra; /* Extra descriptors */
82 
83  /* array of desc.bNumEndpoint endpoints associated with this
84  * interface setting. these will be in no particular order.
85  */
86  struct usb_host_endpoint *endpoint;
87 
88  char *string; /* iInterface string, if present */
89 };
90 
91 enum usb_interface_condition {
92  USB_INTERFACE_UNBOUND = 0,
93  USB_INTERFACE_BINDING,
94  USB_INTERFACE_BOUND,
95  USB_INTERFACE_UNBINDING,
96 };
97 
160 struct usb_interface {
161  /* array of alternate settings for this interface,
162  * stored in no particular order */
163  struct usb_host_interface *altsetting;
164 
165  struct usb_host_interface *cur_altsetting; /* the currently
166  * active alternate setting */
167  unsigned num_altsetting; /* number of alternate settings */
168 
169  /* If there is an interface association descriptor then it will list
170  * the associated interfaces */
171  struct usb_interface_assoc_descriptor *intf_assoc;
172 
173  int minor; /* minor number this interface is
174  * bound to */
175  enum usb_interface_condition condition; /* state of binding */
176  unsigned sysfs_files_created:1; /* the sysfs attributes exist */
177  unsigned ep_devs_created:1; /* endpoint "devices" exist */
178  unsigned unregistering:1; /* unregistration is in progress */
179  unsigned needs_remote_wakeup:1; /* driver requires remote wakeup */
180  unsigned needs_altsetting0:1; /* switch to altsetting 0 is pending */
181  unsigned needs_binding:1; /* needs delayed unbind/rebind */
182  unsigned reset_running:1;
183  unsigned resetting_device:1; /* true: bandwidth alloc after reset */
184 
185  struct device dev; /* interface specific device info */
186  struct device *usb_dev;
187  atomic_t pm_usage_cnt; /* usage counter for autosuspend */
188  struct work_struct reset_ws; /* for resets in atomic context */
189 };
190 #define to_usb_interface(d) container_of(d, struct usb_interface, dev)
191 
192 static inline void *usb_get_intfdata(struct usb_interface *intf)
193 {
194  return dev_get_drvdata(&intf->dev);
195 }
196 
197 static inline void usb_set_intfdata(struct usb_interface *intf, void *data)
198 {
199  dev_set_drvdata(&intf->dev, data);
200 }
201 
203 void usb_put_intf(struct usb_interface *intf);
204 
205 /* this maximum is arbitrary */
206 #define USB_MAXINTERFACES 32
207 #define USB_MAXIADS (USB_MAXINTERFACES/2)
208 
223 struct usb_interface_cache {
224  unsigned num_altsetting; /* number of alternate settings */
225  struct kref ref; /* reference counter */
226 
227  /* variable-length array of alternate settings for this interface,
228  * stored in no particular order */
229  struct usb_host_interface altsetting[0];
230 };
231 #define ref_to_usb_interface_cache(r) \
232  container_of(r, struct usb_interface_cache, ref)
233 #define altsetting_to_usb_interface_cache(a) \
234  container_of(a, struct usb_interface_cache, altsetting[0])
235 
275 struct usb_host_config {
277 
278  char *string; /* iConfiguration string, if present */
279 
280  /* List of any Interface Association Descriptors in this
281  * configuration. */
282  struct usb_interface_assoc_descriptor *intf_assoc[USB_MAXIADS];
283 
284  /* the interfaces associated with this configuration,
285  * stored in no particular order */
286  struct usb_interface *interface[USB_MAXINTERFACES];
287 
288  /* Interface information available even when this is not the
289  * active configuration */
290  struct usb_interface_cache *intf_cache[USB_MAXINTERFACES];
291 
292  unsigned char *extra; /* Extra descriptors */
293  int extralen;
294 };
295 
296 /* USB2.0 and USB3.0 device BOS descriptor set */
297 struct usb_host_bos {
298  struct usb_bos_descriptor *desc;
299 
300  /* wireless cap descriptor is handled by wusb */
301  struct usb_ext_cap_descriptor *ext_cap;
303  struct usb_ss_container_id_descriptor *ss_id;
304 };
305 
306 int __usb_get_extra_descriptor(char *buffer, unsigned size,
307  unsigned char type, void **ptr);
308 #define usb_get_extra_descriptor(ifpoint, type, ptr) \
309  __usb_get_extra_descriptor((ifpoint)->extra, \
310  (ifpoint)->extralen, \
311  type, (void **)ptr)
312 
313 /* ----------------------------------------------------------------------- */
314 
315 /* USB device number allocation bitmap */
316 struct usb_devmap {
317  unsigned long devicemap[128 / (8*sizeof(unsigned long))];
318 };
319 
320 /*
321  * Allocated per bus (tree of devices) we have:
322  */
323 struct usb_bus {
324  struct device *controller; /* host/master side hardware */
325  int busnum; /* Bus number (in order of reg) */
326  const char *bus_name; /* stable id (PCI slot_name etc) */
327  u8 uses_dma; /* Does the host controller use DMA? */
328  u8 uses_pio_for_control; /*
329  * Does the host controller use PIO
330  * for control transfers?
331  */
332  u8 otg_port; /* 0, or number of OTG/HNP port */
333  unsigned is_b_host:1; /* true during some HNP roleswitches */
334  unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */
335  unsigned no_stop_on_short:1; /*
336  * Quirk: some controllers don't stop
337  * the ep queue on a short transfer
338  * with the URB_SHORT_NOT_OK flag set.
339  */
340  unsigned sg_tablesize; /* 0 or largest number of sg list entries */
341 
342  int devnum_next; /* Next open device number in
343  * round-robin allocation */
344 
345  struct usb_devmap devmap; /* device address allocation map */
346  struct usb_device *root_hub; /* Root hub */
347  struct usb_bus *hs_companion; /* Companion EHCI bus, if any */
348  struct list_head bus_list; /* list of busses */
349 
350  int bandwidth_allocated; /* on this bus: how much of the time
351  * reserved for periodic (intr/iso)
352  * requests is used, on average?
353  * Units: microseconds/frame.
354  * Limits: Full/low speed reserve 90%,
355  * while high speed reserves 80%.
356  */
357  int bandwidth_int_reqs; /* number of Interrupt requests */
358  int bandwidth_isoc_reqs; /* number of Isoc. requests */
359 
360 #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
361  struct mon_bus *mon_bus; /* non-null when associated */
362  int monitored; /* non-zero when monitored */
363 #endif
364 };
365 
366 /* ----------------------------------------------------------------------- */
367 
368 /* This is arbitrary.
369  * From USB 2.0 spec Table 11-13, offset 7, a hub can
370  * have up to 255 ports. The most yet reported is 10.
371  *
372  * Current Wireless USB host hardware (Intel i1480 for example) allows
373  * up to 22 devices to connect. Upcoming hardware might raise that
374  * limit. Because the arrays need to add a bit for hub status data, we
375  * do 31, so plus one evens out to four bytes.
376  */
377 #define USB_MAXCHILDREN (31)
378 
379 struct usb_tt;
380 
381 enum usb_device_removable {
382  USB_DEVICE_REMOVABLE_UNKNOWN = 0,
383  USB_DEVICE_REMOVABLE,
384  USB_DEVICE_FIXED,
385 };
386 
387 enum usb_port_connect_type {
388  USB_PORT_CONNECT_TYPE_UNKNOWN = 0,
389  USB_PORT_CONNECT_TYPE_HOT_PLUG,
390  USB_PORT_CONNECT_TYPE_HARD_WIRED,
391  USB_PORT_NOT_USED,
392 };
393 
394 /*
395  * USB 3.0 Link Power Management (LPM) parameters.
396  *
397  * PEL and SEL are USB 3.0 Link PM latencies for device-initiated LPM exit.
398  * MEL is the USB 3.0 Link PM latency for host-initiated LPM exit.
399  * All three are stored in nanoseconds.
400  */
401 struct usb3_lpm_parameters {
402  /*
403  * Maximum exit latency (MEL) for the host to send a packet to the
404  * device (either a Ping for isoc endpoints, or a data packet for
405  * interrupt endpoints), the hubs to decode the packet, and for all hubs
406  * in the path to transition the links to U0.
407  */
408  unsigned int mel;
409  /*
410  * Maximum exit latency for a device-initiated LPM transition to bring
411  * all links into U0. Abbreviated as "PEL" in section 9.4.12 of the USB
412  * 3.0 spec, with no explanation of what "P" stands for. "Path"?
413  */
414  unsigned int pel;
415 
416  /*
417  * The System Exit Latency (SEL) includes PEL, and three other
418  * latencies. After a device initiates a U0 transition, it will take
419  * some time from when the device sends the ERDY to when it will finally
420  * receive the data packet. Basically, SEL should be the worse-case
421  * latency from when a device starts initiating a U0 transition to when
422  * it will get data.
423  */
424  unsigned int sel;
425  /*
426  * The idle timeout value that is currently programmed into the parent
427  * hub for this device. When the timer counts to zero, the parent hub
428  * will initiate an LPM transition to either U1 or U2.
429  */
430  int timeout;
431 };
432 
500 struct usb_device {
501  int devnum;
502  char devpath[16];
503  u32 route;
504  enum usb_device_state state;
505  enum usb_device_speed speed;
506 
507  struct usb_tt *tt;
508  int ttport;
509 
510  unsigned int toggle[2];
511 
512  struct usb_device *parent;
513  struct usb_bus *bus;
514  struct usb_host_endpoint ep0;
515 
516  struct device dev;
517 
519  struct usb_host_bos *bos;
520  struct usb_host_config *config;
521 
522  struct usb_host_config *actconfig;
523  struct usb_host_endpoint *ep_in[16];
524  struct usb_host_endpoint *ep_out[16];
525 
526  char **rawdescriptors;
527 
528  unsigned short bus_mA;
529  u8 portnum;
530  u8 level;
531 
532  unsigned can_submit:1;
533  unsigned persist_enabled:1;
534  unsigned have_langid:1;
535  unsigned authorized:1;
536  unsigned authenticated:1;
537  unsigned wusb:1;
538  unsigned lpm_capable:1;
539  unsigned usb2_hw_lpm_capable:1;
540  unsigned usb2_hw_lpm_enabled:1;
541  unsigned usb3_lpm_enabled:1;
542  int string_langid;
543 
544  /* static strings from the device */
545  char *product;
546  char *manufacturer;
547  char *serial;
548 
549  struct list_head filelist;
550 
551  int maxchild;
552 
553  u32 quirks;
554  atomic_t urbnum;
555 
556  unsigned long active_duration;
557 
558 #ifdef CONFIG_PM
559  unsigned long connect_time;
560 
561  unsigned do_remote_wakeup:1;
562  unsigned reset_resume:1;
563 #endif
564  struct wusb_dev *wusb_dev;
565  int slot_id;
566  enum usb_device_removable removable;
567  struct usb3_lpm_parameters u1_params;
568  struct usb3_lpm_parameters u2_params;
569  unsigned lpm_disable_count;
570 };
571 #define to_usb_device(d) container_of(d, struct usb_device, dev)
572 
573 static inline struct usb_device *interface_to_usbdev(struct usb_interface *intf)
574 {
575  return to_usb_device(intf->dev.parent);
576 }
577 
578 extern struct usb_device *usb_get_dev(struct usb_device *dev);
579 extern void usb_put_dev(struct usb_device *dev);
580 extern struct usb_device *usb_hub_find_child(struct usb_device *hdev,
581  int port1);
582 
589 #define usb_hub_for_each_child(hdev, port1, child) \
590  for (port1 = 1, child = usb_hub_find_child(hdev, port1); \
591  port1 <= hdev->maxchild; \
592  child = usb_hub_find_child(hdev, ++port1))
593 
594 /* USB device locking */
595 #define usb_lock_device(udev) device_lock(&(udev)->dev)
596 #define usb_unlock_device(udev) device_unlock(&(udev)->dev)
597 #define usb_trylock_device(udev) device_trylock(&(udev)->dev)
598 extern int usb_lock_device_for_reset(struct usb_device *udev,
599  const struct usb_interface *iface);
600 
601 /* USB port reset for device reinitialization */
602 extern int usb_reset_device(struct usb_device *dev);
603 extern void usb_queue_reset_device(struct usb_interface *dev);
604 
605 #ifdef CONFIG_ACPI
606 extern int usb_acpi_set_power_state(struct usb_device *hdev, int index,
607  bool enable);
608 extern bool usb_acpi_power_manageable(struct usb_device *hdev, int index);
609 #else
610 static inline int usb_acpi_set_power_state(struct usb_device *hdev, int index,
611  bool enable) { return 0; }
612 static inline bool usb_acpi_power_manageable(struct usb_device *hdev, int index)
613  { return true; }
614 #endif
615 
616 /* USB autosuspend and autoresume */
617 #ifdef CONFIG_USB_SUSPEND
618 extern void usb_enable_autosuspend(struct usb_device *udev);
619 extern void usb_disable_autosuspend(struct usb_device *udev);
620 
621 extern int usb_autopm_get_interface(struct usb_interface *intf);
622 extern void usb_autopm_put_interface(struct usb_interface *intf);
623 extern int usb_autopm_get_interface_async(struct usb_interface *intf);
624 extern void usb_autopm_put_interface_async(struct usb_interface *intf);
625 extern void usb_autopm_get_interface_no_resume(struct usb_interface *intf);
626 extern void usb_autopm_put_interface_no_suspend(struct usb_interface *intf);
627 
628 static inline void usb_mark_last_busy(struct usb_device *udev)
629 {
630  pm_runtime_mark_last_busy(&udev->dev);
631 }
632 
633 #else
634 
635 static inline int usb_enable_autosuspend(struct usb_device *udev)
636 { return 0; }
637 static inline int usb_disable_autosuspend(struct usb_device *udev)
638 { return 0; }
639 
640 static inline int usb_autopm_get_interface(struct usb_interface *intf)
641 { return 0; }
642 static inline int usb_autopm_get_interface_async(struct usb_interface *intf)
643 { return 0; }
644 
645 static inline void usb_autopm_put_interface(struct usb_interface *intf)
646 { }
647 static inline void usb_autopm_put_interface_async(struct usb_interface *intf)
648 { }
649 static inline void usb_autopm_get_interface_no_resume(
650  struct usb_interface *intf)
651 { }
652 static inline void usb_autopm_put_interface_no_suspend(
653  struct usb_interface *intf)
654 { }
655 static inline void usb_mark_last_busy(struct usb_device *udev)
656 { }
657 #endif
658 
659 extern int usb_disable_lpm(struct usb_device *udev);
660 extern void usb_enable_lpm(struct usb_device *udev);
661 /* Same as above, but these functions lock/unlock the bandwidth_mutex. */
662 extern int usb_unlocked_disable_lpm(struct usb_device *udev);
663 extern void usb_unlocked_enable_lpm(struct usb_device *udev);
664 
665 extern int usb_disable_ltm(struct usb_device *udev);
666 extern void usb_enable_ltm(struct usb_device *udev);
667 
668 static inline bool usb_device_supports_ltm(struct usb_device *udev)
669 {
670  if (udev->speed != USB_SPEED_SUPER || !udev->bos || !udev->bos->ss_cap)
671  return false;
672  return udev->bos->ss_cap->bmAttributes & USB_LTM_SUPPORT;
673 }
674 
675 
676 /*-------------------------------------------------------------------------*/
677 
678 /* for drivers using iso endpoints */
679 extern int usb_get_current_frame_number(struct usb_device *usb_dev);
680 
681 /* Sets up a group of bulk endpoints to support multiple stream IDs. */
682 extern int usb_alloc_streams(struct usb_interface *interface,
683  struct usb_host_endpoint **eps, unsigned int num_eps,
684  unsigned int num_streams, gfp_t mem_flags);
685 
686 /* Reverts a group of bulk endpoints back to not using stream IDs. */
687 extern void usb_free_streams(struct usb_interface *interface,
688  struct usb_host_endpoint **eps, unsigned int num_eps,
689  gfp_t mem_flags);
690 
691 /* used these for multi-interface device registration */
692 extern int usb_driver_claim_interface(struct usb_driver *driver,
693  struct usb_interface *iface, void *priv);
694 
705 static inline int usb_interface_claimed(struct usb_interface *iface)
706 {
707  return (iface->dev.driver != NULL);
708 }
709 
710 extern void usb_driver_release_interface(struct usb_driver *driver,
711  struct usb_interface *iface);
712 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
713  const struct usb_device_id *id);
714 extern int usb_match_one_id(struct usb_interface *interface,
715  const struct usb_device_id *id);
716 
717 extern struct usb_interface *usb_find_interface(struct usb_driver *drv,
718  int minor);
719 extern struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,
720  unsigned ifnum);
721 extern struct usb_host_interface *usb_altnum_to_altsetting(
722  const struct usb_interface *intf, unsigned int altnum);
723 extern struct usb_host_interface *usb_find_alt_setting(
724  struct usb_host_config *config,
725  unsigned int iface_num,
726  unsigned int alt_num);
727 
728 
753 static inline int usb_make_path(struct usb_device *dev, char *buf, size_t size)
754 {
755  int actual;
756  actual = snprintf(buf, size, "usb-%s-%s", dev->bus->bus_name,
757  dev->devpath);
758  return (actual >= (int)size) ? -1 : actual;
759 }
760 
761 /*-------------------------------------------------------------------------*/
762 
763 #define USB_DEVICE_ID_MATCH_DEVICE \
764  (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
765 #define USB_DEVICE_ID_MATCH_DEV_RANGE \
766  (USB_DEVICE_ID_MATCH_DEV_LO | USB_DEVICE_ID_MATCH_DEV_HI)
767 #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION \
768  (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_RANGE)
769 #define USB_DEVICE_ID_MATCH_DEV_INFO \
770  (USB_DEVICE_ID_MATCH_DEV_CLASS | \
771  USB_DEVICE_ID_MATCH_DEV_SUBCLASS | \
772  USB_DEVICE_ID_MATCH_DEV_PROTOCOL)
773 #define USB_DEVICE_ID_MATCH_INT_INFO \
774  (USB_DEVICE_ID_MATCH_INT_CLASS | \
775  USB_DEVICE_ID_MATCH_INT_SUBCLASS | \
776  USB_DEVICE_ID_MATCH_INT_PROTOCOL)
777 
786 #define USB_DEVICE(vend, prod) \
787  .match_flags = USB_DEVICE_ID_MATCH_DEVICE, \
788  .idVendor = (vend), \
789  .idProduct = (prod)
790 
800 #define USB_DEVICE_VER(vend, prod, lo, hi) \
801  .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, \
802  .idVendor = (vend), \
803  .idProduct = (prod), \
804  .bcdDevice_lo = (lo), \
805  .bcdDevice_hi = (hi)
806 
816 #define USB_DEVICE_INTERFACE_PROTOCOL(vend, prod, pr) \
817  .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
818  USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
819  .idVendor = (vend), \
820  .idProduct = (prod), \
821  .bInterfaceProtocol = (pr)
822 
832 #define USB_DEVICE_INTERFACE_NUMBER(vend, prod, num) \
833  .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
834  USB_DEVICE_ID_MATCH_INT_NUMBER, \
835  .idVendor = (vend), \
836  .idProduct = (prod), \
837  .bInterfaceNumber = (num)
838 
848 #define USB_DEVICE_INFO(cl, sc, pr) \
849  .match_flags = USB_DEVICE_ID_MATCH_DEV_INFO, \
850  .bDeviceClass = (cl), \
851  .bDeviceSubClass = (sc), \
852  .bDeviceProtocol = (pr)
853 
863 #define USB_INTERFACE_INFO(cl, sc, pr) \
864  .match_flags = USB_DEVICE_ID_MATCH_INT_INFO, \
865  .bInterfaceClass = (cl), \
866  .bInterfaceSubClass = (sc), \
867  .bInterfaceProtocol = (pr)
868 
883 #define USB_DEVICE_AND_INTERFACE_INFO(vend, prod, cl, sc, pr) \
884  .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \
885  | USB_DEVICE_ID_MATCH_DEVICE, \
886  .idVendor = (vend), \
887  .idProduct = (prod), \
888  .bInterfaceClass = (cl), \
889  .bInterfaceSubClass = (sc), \
890  .bInterfaceProtocol = (pr)
891 
905 #define USB_VENDOR_AND_INTERFACE_INFO(vend, cl, sc, pr) \
906  .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \
907  | USB_DEVICE_ID_MATCH_VENDOR, \
908  .idVendor = (vend), \
909  .bInterfaceClass = (cl), \
910  .bInterfaceSubClass = (sc), \
911  .bInterfaceProtocol = (pr)
912 
913 /* ----------------------------------------------------------------------- */
914 
915 /* Stuff for dynamic usb ids */
916 struct usb_dynids {
918  struct list_head list;
919 };
920 
921 struct usb_dynid {
922  struct list_head node;
923  struct usb_device_id id;
924 };
925 
926 extern ssize_t usb_store_new_id(struct usb_dynids *dynids,
927  struct device_driver *driver,
928  const char *buf, size_t count);
929 
930 extern ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf);
931 
937 struct usbdrv_wrap {
938  struct device_driver driver;
939  int for_devices;
940 };
941 
1001 struct usb_driver {
1002  const char *name;
1003 
1004  int (*probe) (struct usb_interface *intf,
1005  const struct usb_device_id *id);
1006 
1007  void (*disconnect) (struct usb_interface *intf);
1008 
1009  int (*unlocked_ioctl) (struct usb_interface *intf, unsigned int code,
1010  void *buf);
1011 
1013  int (*resume) (struct usb_interface *intf);
1014  int (*reset_resume)(struct usb_interface *intf);
1015 
1016  int (*pre_reset)(struct usb_interface *intf);
1017  int (*post_reset)(struct usb_interface *intf);
1018 
1019  const struct usb_device_id *id_table;
1020 
1021  struct usb_dynids dynids;
1022  struct usbdrv_wrap drvwrap;
1023  unsigned int no_dynamic_id:1;
1024  unsigned int supports_autosuspend:1;
1025  unsigned int disable_hub_initiated_lpm:1;
1026  unsigned int soft_unbind:1;
1027 };
1028 #define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver)
1029 
1049 struct usb_device_driver {
1050  const char *name;
1051 
1052  int (*probe) (struct usb_device *udev);
1053  void (*disconnect) (struct usb_device *udev);
1054 
1055  int (*suspend) (struct usb_device *udev, pm_message_t message);
1056  int (*resume) (struct usb_device *udev, pm_message_t message);
1057  struct usbdrv_wrap drvwrap;
1058  unsigned int supports_autosuspend:1;
1059 };
1060 #define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \
1061  drvwrap.driver)
1062 
1063 extern struct bus_type usb_bus_type;
1064 
1077 struct usb_class_driver {
1078  char *name;
1079  char *(*devnode)(struct device *dev, umode_t *mode);
1080  const struct file_operations *fops;
1081  int minor_base;
1082 };
1083 
1084 /*
1085  * use these in module_init()/module_exit()
1086  * and don't forget MODULE_DEVICE_TABLE(usb, ...)
1087  */
1088 extern int usb_register_driver(struct usb_driver *, struct module *,
1089  const char *);
1090 
1091 /* use a define to avoid include chaining to get THIS_MODULE & friends */
1092 #define usb_register(driver) \
1093  usb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
1094 
1095 extern void usb_deregister(struct usb_driver *);
1096 
1105 #define module_usb_driver(__usb_driver) \
1106  module_driver(__usb_driver, usb_register, \
1107  usb_deregister)
1108 
1109 extern int usb_register_device_driver(struct usb_device_driver *,
1110  struct module *);
1111 extern void usb_deregister_device_driver(struct usb_device_driver *);
1112 
1113 extern int usb_register_dev(struct usb_interface *intf,
1114  struct usb_class_driver *class_driver);
1115 extern void usb_deregister_dev(struct usb_interface *intf,
1116  struct usb_class_driver *class_driver);
1117 
1118 extern int usb_disabled(void);
1119 
1120 /* ----------------------------------------------------------------------- */
1121 
1122 /*
1123  * URB support, for asynchronous request completions
1124  */
1125 
1126 /*
1127  * urb->transfer_flags:
1128  *
1129  * Note: URB_DIR_IN/OUT is automatically set in usb_submit_urb().
1130  */
1131 #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */
1132 #define URB_ISO_ASAP 0x0002 /* iso-only, urb->start_frame
1133  * ignored */
1134 #define URB_NO_TRANSFER_DMA_MAP 0x0004 /* urb->transfer_dma valid on submit */
1135 #define URB_NO_FSBR 0x0020 /* UHCI-specific */
1136 #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */
1137 #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt
1138  * needed */
1139 #define URB_FREE_BUFFER 0x0100 /* Free transfer buffer with the URB */
1140 
1141 /* The following flags are used internally by usbcore and HCDs */
1142 #define URB_DIR_IN 0x0200 /* Transfer from device to host */
1143 #define URB_DIR_OUT 0
1144 #define URB_DIR_MASK URB_DIR_IN
1145 
1146 #define URB_DMA_MAP_SINGLE 0x00010000 /* Non-scatter-gather mapping */
1147 #define URB_DMA_MAP_PAGE 0x00020000 /* HCD-unsupported S-G */
1148 #define URB_DMA_MAP_SG 0x00040000 /* HCD-supported S-G */
1149 #define URB_MAP_LOCAL 0x00080000 /* HCD-local-memory mapping */
1150 #define URB_SETUP_MAP_SINGLE 0x00100000 /* Setup packet DMA mapped */
1151 #define URB_SETUP_MAP_LOCAL 0x00200000 /* HCD-local setup packet */
1152 #define URB_DMA_SG_COMBINED 0x00400000 /* S-G entries were combined */
1153 #define URB_ALIGNED_TEMP_BUFFER 0x00800000 /* Temp buffer was alloc'd */
1154 
1155 struct usb_iso_packet_descriptor {
1156  unsigned int offset;
1157  unsigned int length; /* expected length */
1158  unsigned int actual_length;
1159  int status;
1160 };
1161 
1162 struct urb;
1163 
1164 struct usb_anchor {
1165  struct list_head urb_list;
1167  spinlock_t lock;
1168  unsigned int poisoned:1;
1169 };
1170 
1171 static inline void init_usb_anchor(struct usb_anchor *anchor)
1172 {
1173  INIT_LIST_HEAD(&anchor->urb_list);
1174  init_waitqueue_head(&anchor->wait);
1175  spin_lock_init(&anchor->lock);
1176 }
1177 
1178 typedef void (*usb_complete_t)(struct urb *);
1179 
1356 struct urb {
1357  /* private: usb core and host controller only fields in the urb */
1358  struct kref kref; /* reference count of the URB */
1359  void *hcpriv; /* private data for host controller */
1360  atomic_t use_count; /* concurrent submissions counter */
1361  atomic_t reject; /* submissions will fail */
1362  int unlinked; /* unlink error code */
1363 
1364  /* public: documented fields in the urb that can be used by drivers */
1365  struct list_head urb_list; /* list head for use by the urb's
1366  * current owner */
1367  struct list_head anchor_list; /* the URB may be anchored */
1368  struct usb_anchor *anchor;
1369  struct usb_device *dev; /* (in) pointer to associated device */
1370  struct usb_host_endpoint *ep; /* (internal) pointer to endpoint */
1371  unsigned int pipe; /* (in) pipe information */
1372  unsigned int stream_id; /* (in) stream ID */
1373  int status; /* (return) non-ISO status */
1374  unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/
1375  void *transfer_buffer; /* (in) associated data buffer */
1376  dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */
1377  struct scatterlist *sg; /* (in) scatter gather buffer list */
1378  int num_mapped_sgs; /* (internal) mapped sg entries */
1379  int num_sgs; /* (in) number of entries in the sg list */
1380  u32 transfer_buffer_length; /* (in) data buffer length */
1381  u32 actual_length; /* (return) actual transfer length */
1382  unsigned char *setup_packet; /* (in) setup packet (control only) */
1383  dma_addr_t setup_dma; /* (in) dma addr for setup_packet */
1384  int start_frame; /* (modify) start frame (ISO) */
1385  int number_of_packets; /* (in) number of ISO packets */
1386  int interval; /* (modify) transfer interval
1387  * (INT/ISO) */
1388  int error_count; /* (return) number of ISO errors */
1389  void *context; /* (in) context for completion */
1390  usb_complete_t complete; /* (in) completion routine */
1391  struct usb_iso_packet_descriptor iso_frame_desc[0];
1392  /* (in) ISO ONLY */
1393 };
1394 
1395 /* ----------------------------------------------------------------------- */
1396 
1411 static inline void usb_fill_control_urb(struct urb *urb,
1412  struct usb_device *dev,
1413  unsigned int pipe,
1414  unsigned char *setup_packet,
1415  void *transfer_buffer,
1416  int buffer_length,
1417  usb_complete_t complete_fn,
1418  void *context)
1419 {
1420  urb->dev = dev;
1421  urb->pipe = pipe;
1422  urb->setup_packet = setup_packet;
1423  urb->transfer_buffer = transfer_buffer;
1424  urb->transfer_buffer_length = buffer_length;
1425  urb->complete = complete_fn;
1426  urb->context = context;
1427 }
1428 
1442 static inline void usb_fill_bulk_urb(struct urb *urb,
1443  struct usb_device *dev,
1444  unsigned int pipe,
1445  void *transfer_buffer,
1446  int buffer_length,
1447  usb_complete_t complete_fn,
1448  void *context)
1449 {
1450  urb->dev = dev;
1451  urb->pipe = pipe;
1452  urb->transfer_buffer = transfer_buffer;
1453  urb->transfer_buffer_length = buffer_length;
1454  urb->complete = complete_fn;
1455  urb->context = context;
1456 }
1457 
1483 static inline void usb_fill_int_urb(struct urb *urb,
1484  struct usb_device *dev,
1485  unsigned int pipe,
1486  void *transfer_buffer,
1487  int buffer_length,
1488  usb_complete_t complete_fn,
1489  void *context,
1490  int interval)
1491 {
1492  urb->dev = dev;
1493  urb->pipe = pipe;
1494  urb->transfer_buffer = transfer_buffer;
1495  urb->transfer_buffer_length = buffer_length;
1496  urb->complete = complete_fn;
1497  urb->context = context;
1498  if (dev->speed == USB_SPEED_HIGH || dev->speed == USB_SPEED_SUPER)
1499  urb->interval = 1 << (interval - 1);
1500  else
1501  urb->interval = interval;
1502  urb->start_frame = -1;
1503 }
1504 
1505 extern void usb_init_urb(struct urb *urb);
1506 extern struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags);
1507 extern void usb_free_urb(struct urb *urb);
1508 #define usb_put_urb usb_free_urb
1509 extern struct urb *usb_get_urb(struct urb *urb);
1510 extern int usb_submit_urb(struct urb *urb, gfp_t mem_flags);
1511 extern int usb_unlink_urb(struct urb *urb);
1512 extern void usb_kill_urb(struct urb *urb);
1513 extern void usb_poison_urb(struct urb *urb);
1514 extern void usb_unpoison_urb(struct urb *urb);
1515 extern void usb_block_urb(struct urb *urb);
1516 extern void usb_kill_anchored_urbs(struct usb_anchor *anchor);
1517 extern void usb_poison_anchored_urbs(struct usb_anchor *anchor);
1518 extern void usb_unpoison_anchored_urbs(struct usb_anchor *anchor);
1519 extern void usb_unlink_anchored_urbs(struct usb_anchor *anchor);
1520 extern void usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor);
1521 extern void usb_unanchor_urb(struct urb *urb);
1522 extern int usb_wait_anchor_empty_timeout(struct usb_anchor *anchor,
1523  unsigned int timeout);
1524 extern struct urb *usb_get_from_anchor(struct usb_anchor *anchor);
1525 extern void usb_scuttle_anchored_urbs(struct usb_anchor *anchor);
1526 extern int usb_anchor_empty(struct usb_anchor *anchor);
1527 
1528 #define usb_unblock_urb usb_unpoison_urb
1529 
1537 static inline int usb_urb_dir_in(struct urb *urb)
1538 {
1539  return (urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN;
1540 }
1541 
1549 static inline int usb_urb_dir_out(struct urb *urb)
1550 {
1551  return (urb->transfer_flags & URB_DIR_MASK) == URB_DIR_OUT;
1552 }
1553 
1554 void *usb_alloc_coherent(struct usb_device *dev, size_t size,
1555  gfp_t mem_flags, dma_addr_t *dma);
1556 void usb_free_coherent(struct usb_device *dev, size_t size,
1557  void *addr, dma_addr_t dma);
1558 
1559 #if 0
1560 struct urb *usb_buffer_map(struct urb *urb);
1561 void usb_buffer_dmasync(struct urb *urb);
1562 void usb_buffer_unmap(struct urb *urb);
1563 #endif
1564 
1565 struct scatterlist;
1566 int usb_buffer_map_sg(const struct usb_device *dev, int is_in,
1567  struct scatterlist *sg, int nents);
1568 #if 0
1569 void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
1570  struct scatterlist *sg, int n_hw_ents);
1571 #endif
1572 void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
1573  struct scatterlist *sg, int n_hw_ents);
1574 
1575 /*-------------------------------------------------------------------*
1576  * SYNCHRONOUS CALL SUPPORT *
1577  *-------------------------------------------------------------------*/
1578 
1579 extern int usb_control_msg(struct usb_device *dev, unsigned int pipe,
1580  __u8 request, __u8 requesttype, __u16 value, __u16 index,
1581  void *data, __u16 size, int timeout);
1582 extern int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
1583  void *data, int len, int *actual_length, int timeout);
1584 extern int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
1585  void *data, int len, int *actual_length,
1586  int timeout);
1587 
1588 /* wrappers around usb_control_msg() for the most common standard requests */
1589 extern int usb_get_descriptor(struct usb_device *dev, unsigned char desctype,
1590  unsigned char descindex, void *buf, int size);
1591 extern int usb_get_status(struct usb_device *dev,
1592  int type, int target, void *data);
1593 extern int usb_string(struct usb_device *dev, int index,
1594  char *buf, size_t size);
1595 
1596 /* wrappers that also update important state inside usbcore */
1597 extern int usb_clear_halt(struct usb_device *dev, int pipe);
1598 extern int usb_reset_configuration(struct usb_device *dev);
1599 extern int usb_set_interface(struct usb_device *dev, int ifnum, int alternate);
1600 extern void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr);
1601 
1602 /* this request isn't really synchronous, but it belongs with the others */
1603 extern int usb_driver_set_configuration(struct usb_device *udev, int config);
1604 
1605 /*
1606  * timeouts, in milliseconds, used for sending/receiving control messages
1607  * they typically complete within a few frames (msec) after they're issued
1608  * USB identifies 5 second timeouts, maybe more in a few cases, and a few
1609  * slow devices (like some MGE Ellipse UPSes) actually push that limit.
1610  */
1611 #define USB_CTRL_GET_TIMEOUT 5000
1612 #define USB_CTRL_SET_TIMEOUT 5000
1613 
1614 
1631 struct usb_sg_request {
1632  int status;
1633  size_t bytes;
1634 
1635  /* private:
1636  * members below are private to usbcore,
1637  * and are not provided for driver access!
1638  */
1639  spinlock_t lock;
1640 
1641  struct usb_device *dev;
1642  int pipe;
1643 
1644  int entries;
1645  struct urb **urbs;
1646 
1647  int count;
1648  struct completion complete;
1649 };
1650 
1651 int usb_sg_init(
1652  struct usb_sg_request *io,
1653  struct usb_device *dev,
1654  unsigned pipe,
1655  unsigned period,
1656  struct scatterlist *sg,
1657  int nents,
1658  size_t length,
1659  gfp_t mem_flags
1660 );
1661 void usb_sg_cancel(struct usb_sg_request *io);
1662 void usb_sg_wait(struct usb_sg_request *io);
1663 
1664 
1665 /* ----------------------------------------------------------------------- */
1666 
1667 /*
1668  * For various legacy reasons, Linux has a small cookie that's paired with
1669  * a struct usb_device to identify an endpoint queue. Queue characteristics
1670  * are defined by the endpoint's descriptor. This cookie is called a "pipe",
1671  * an unsigned int encoded as:
1672  *
1673  * - direction: bit 7 (0 = Host-to-Device [Out],
1674  * 1 = Device-to-Host [In] ...
1675  * like endpoint bEndpointAddress)
1676  * - device address: bits 8-14 ... bit positions known to uhci-hcd
1677  * - endpoint: bits 15-18 ... bit positions known to uhci-hcd
1678  * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
1679  * 10 = control, 11 = bulk)
1680  *
1681  * Given the device address and endpoint descriptor, pipes are redundant.
1682  */
1683 
1684 /* NOTE: these are not the standard USB_ENDPOINT_XFER_* values!! */
1685 /* (yet ... they're the values used by usbfs) */
1686 #define PIPE_ISOCHRONOUS 0
1687 #define PIPE_INTERRUPT 1
1688 #define PIPE_CONTROL 2
1689 #define PIPE_BULK 3
1690 
1691 #define usb_pipein(pipe) ((pipe) & USB_DIR_IN)
1692 #define usb_pipeout(pipe) (!usb_pipein(pipe))
1693 
1694 #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
1695 #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
1696 
1697 #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
1698 #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
1699 #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
1700 #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
1701 #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
1702 
1703 static inline unsigned int __create_pipe(struct usb_device *dev,
1704  unsigned int endpoint)
1705 {
1706  return (dev->devnum << 8) | (endpoint << 15);
1707 }
1708 
1709 /* Create various pipes... */
1710 #define usb_sndctrlpipe(dev, endpoint) \
1711  ((PIPE_CONTROL << 30) | __create_pipe(dev, endpoint))
1712 #define usb_rcvctrlpipe(dev, endpoint) \
1713  ((PIPE_CONTROL << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN)
1714 #define usb_sndisocpipe(dev, endpoint) \
1715  ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev, endpoint))
1716 #define usb_rcvisocpipe(dev, endpoint) \
1717  ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN)
1718 #define usb_sndbulkpipe(dev, endpoint) \
1719  ((PIPE_BULK << 30) | __create_pipe(dev, endpoint))
1720 #define usb_rcvbulkpipe(dev, endpoint) \
1721  ((PIPE_BULK << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN)
1722 #define usb_sndintpipe(dev, endpoint) \
1723  ((PIPE_INTERRUPT << 30) | __create_pipe(dev, endpoint))
1724 #define usb_rcvintpipe(dev, endpoint) \
1725  ((PIPE_INTERRUPT << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN)
1726 
1727 static inline struct usb_host_endpoint *
1728 usb_pipe_endpoint(struct usb_device *dev, unsigned int pipe)
1729 {
1730  struct usb_host_endpoint **eps;
1731  eps = usb_pipein(pipe) ? dev->ep_in : dev->ep_out;
1732  return eps[usb_pipeendpoint(pipe)];
1733 }
1734 
1735 /*-------------------------------------------------------------------------*/
1736 
1737 static inline __u16
1738 usb_maxpacket(struct usb_device *udev, int pipe, int is_out)
1739 {
1740  struct usb_host_endpoint *ep;
1741  unsigned epnum = usb_pipeendpoint(pipe);
1742 
1743  if (is_out) {
1744  WARN_ON(usb_pipein(pipe));
1745  ep = udev->ep_out[epnum];
1746  } else {
1747  WARN_ON(usb_pipeout(pipe));
1748  ep = udev->ep_in[epnum];
1749  }
1750  if (!ep)
1751  return 0;
1752 
1753  /* NOTE: only 0x07ff bits are for packet size... */
1754  return usb_endpoint_maxp(&ep->desc);
1755 }
1756 
1757 /* ----------------------------------------------------------------------- */
1758 
1759 /* translate USB error codes to codes user space understands */
1760 static inline int usb_translate_errors(int error_code)
1761 {
1762  switch (error_code) {
1763  case 0:
1764  case -ENOMEM:
1765  case -ENODEV:
1766  case -EOPNOTSUPP:
1767  return error_code;
1768  default:
1769  return -EIO;
1770  }
1771 }
1772 
1773 /* Events from the usb core */
1774 #define USB_DEVICE_ADD 0x0001
1775 #define USB_DEVICE_REMOVE 0x0002
1776 #define USB_BUS_ADD 0x0003
1777 #define USB_BUS_REMOVE 0x0004
1778 extern void usb_register_notify(struct notifier_block *nb);
1779 extern void usb_unregister_notify(struct notifier_block *nb);
1780 
1781 /* debugfs stuff */
1782 extern struct dentry *usb_debug_root;
1783 
1784 #endif /* __KERNEL__ */
1785 
1786 #endif