Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gadget.h
Go to the documentation of this file.
1 /*
2  * <linux/usb/gadget.h>
3  *
4  * We call the USB code inside a Linux-based peripheral device a "gadget"
5  * driver, except for the hardware-specific bus glue. One USB host can
6  * master many USB gadgets, but the gadgets are only slaved to one host.
7  *
8  *
9  * (C) Copyright 2002-2004 by David Brownell
10  * All Rights Reserved.
11  *
12  * This software is licensed under the GNU GPL version 2.
13  */
14 
15 #ifndef __LINUX_USB_GADGET_H
16 #define __LINUX_USB_GADGET_H
17 
18 #include <linux/device.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/slab.h>
23 #include <linux/scatterlist.h>
24 #include <linux/types.h>
25 #include <linux/usb/ch9.h>
26 
27 struct usb_ep;
28 
90 struct usb_request {
91  void *buf;
92  unsigned length;
94 
95  struct scatterlist *sg;
96  unsigned num_sgs;
97  unsigned num_mapped_sgs;
98 
99  unsigned stream_id:16;
100  unsigned no_interrupt:1;
101  unsigned zero:1;
102  unsigned short_not_ok:1;
103 
104  void (*complete)(struct usb_ep *ep,
105  struct usb_request *req);
106  void *context;
107  struct list_head list;
108 
109  int status;
110  unsigned actual;
111 };
112 
113 /*-------------------------------------------------------------------------*/
114 
115 /* endpoint-specific parts of the api to the usb controller hardware.
116  * unlike the urb model, (de)multiplexing layers are not required.
117  * (so this api could slash overhead if used on the host side...)
118  *
119  * note that device side usb controllers commonly differ in how many
120  * endpoints they support, as well as their capabilities.
121  */
122 struct usb_ep_ops {
123  int (*enable) (struct usb_ep *ep,
124  const struct usb_endpoint_descriptor *desc);
125  int (*disable) (struct usb_ep *ep);
126 
127  struct usb_request *(*alloc_request) (struct usb_ep *ep,
128  gfp_t gfp_flags);
129  void (*free_request) (struct usb_ep *ep, struct usb_request *req);
130 
131  int (*queue) (struct usb_ep *ep, struct usb_request *req,
132  gfp_t gfp_flags);
133  int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
134 
135  int (*set_halt) (struct usb_ep *ep, int value);
136  int (*set_wedge) (struct usb_ep *ep);
137 
138  int (*fifo_status) (struct usb_ep *ep);
139  void (*fifo_flush) (struct usb_ep *ep);
140 };
141 
166 struct usb_ep {
167  void *driver_data;
168 
169  const char *name;
170  const struct usb_ep_ops *ops;
172  unsigned maxpacket:16;
173  unsigned max_streams:16;
174  unsigned mult:2;
175  unsigned maxburst:5;
179 };
180 
181 /*-------------------------------------------------------------------------*/
182 
203 static inline int usb_ep_enable(struct usb_ep *ep)
204 {
205  return ep->ops->enable(ep, ep->desc);
206 }
207 
220 static inline int usb_ep_disable(struct usb_ep *ep)
221 {
222  return ep->ops->disable(ep);
223 }
224 
239 static inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
240  gfp_t gfp_flags)
241 {
242  return ep->ops->alloc_request(ep, gfp_flags);
243 }
244 
254 static inline void usb_ep_free_request(struct usb_ep *ep,
255  struct usb_request *req)
256 {
257  ep->ops->free_request(ep, req);
258 }
259 
317 static inline int usb_ep_queue(struct usb_ep *ep,
318  struct usb_request *req, gfp_t gfp_flags)
319 {
320  return ep->ops->queue(ep, req, gfp_flags);
321 }
322 
337 static inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
338 {
339  return ep->ops->dequeue(ep, req);
340 }
341 
363 static inline int usb_ep_set_halt(struct usb_ep *ep)
364 {
365  return ep->ops->set_halt(ep, 1);
366 }
367 
381 static inline int usb_ep_clear_halt(struct usb_ep *ep)
382 {
383  return ep->ops->set_halt(ep, 0);
384 }
385 
396 static inline int
397 usb_ep_set_wedge(struct usb_ep *ep)
398 {
399  if (ep->ops->set_wedge)
400  return ep->ops->set_wedge(ep);
401  else
402  return ep->ops->set_halt(ep, 1);
403 }
404 
420 static inline int usb_ep_fifo_status(struct usb_ep *ep)
421 {
422  if (ep->ops->fifo_status)
423  return ep->ops->fifo_status(ep);
424  else
425  return -EOPNOTSUPP;
426 }
427 
437 static inline void usb_ep_fifo_flush(struct usb_ep *ep)
438 {
439  if (ep->ops->fifo_flush)
440  ep->ops->fifo_flush(ep);
441 }
442 
443 
444 /*-------------------------------------------------------------------------*/
445 
447  __u8 bU1devExitLat; /* U1 Device exit Latency */
448 #define USB_DEFAULT_U1_DEV_EXIT_LAT 0x01 /* Less then 1 microsec */
449  __le16 bU2DevExitLat; /* U2 Device exit Latency */
450 #define USB_DEFAULT_U2_DEV_EXIT_LAT 0x1F4 /* Less then 500 microsec */
451 };
452 
453 
454 struct usb_gadget;
455 struct usb_gadget_driver;
456 
457 /* the rest of the api to the controller hardware: device operations,
458  * which don't involve endpoints (or i/o).
459  */
461  int (*get_frame)(struct usb_gadget *);
462  int (*wakeup)(struct usb_gadget *);
463  int (*set_selfpowered) (struct usb_gadget *, int is_selfpowered);
464  int (*vbus_session) (struct usb_gadget *, int is_active);
465  int (*vbus_draw) (struct usb_gadget *, unsigned mA);
466  int (*pullup) (struct usb_gadget *, int is_on);
467  int (*ioctl)(struct usb_gadget *,
468  unsigned code, unsigned long param);
470  int (*udc_start)(struct usb_gadget *,
471  struct usb_gadget_driver *);
472  int (*udc_stop)(struct usb_gadget *,
473  struct usb_gadget_driver *);
474 
475  /* Those two are deprecated */
477  int (*bind)(struct usb_gadget *,
478  struct usb_gadget_driver *driver));
479  int (*stop)(struct usb_gadget_driver *);
480 };
481 
527 struct usb_gadget {
528  /* readonly to gadget driver */
529  const struct usb_gadget_ops *ops;
530  struct usb_ep *ep0;
531  struct list_head ep_list; /* of usb_ep */
534  unsigned sg_supported:1;
535  unsigned is_otg:1;
536  unsigned is_a_peripheral:1;
537  unsigned b_hnp_enable:1;
538  unsigned a_hnp_support:1;
539  unsigned a_alt_hnp_support:1;
540  const char *name;
541  struct device dev;
542  unsigned out_epnum;
543  unsigned in_epnum;
544 };
545 
546 static inline void set_gadget_data(struct usb_gadget *gadget, void *data)
547  { dev_set_drvdata(&gadget->dev, data); }
548 static inline void *get_gadget_data(struct usb_gadget *gadget)
549  { return dev_get_drvdata(&gadget->dev); }
550 static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
551 {
552  return container_of(dev, struct usb_gadget, dev);
553 }
554 
555 /* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */
556 #define gadget_for_each_ep(tmp, gadget) \
557  list_for_each_entry(tmp, &(gadget)->ep_list, ep_list)
558 
559 
564 static inline int gadget_is_dualspeed(struct usb_gadget *g)
565 {
566  return g->max_speed >= USB_SPEED_HIGH;
567 }
568 
574 static inline int gadget_is_superspeed(struct usb_gadget *g)
575 {
576  return g->max_speed >= USB_SPEED_SUPER;
577 }
578 
586 static inline int gadget_is_otg(struct usb_gadget *g)
587 {
588 #ifdef CONFIG_USB_OTG
589  return g->is_otg;
590 #else
591  return 0;
592 #endif
593 }
594 
602 static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
603 {
604  return gadget->ops->get_frame(gadget);
605 }
606 
620 static inline int usb_gadget_wakeup(struct usb_gadget *gadget)
621 {
622  if (!gadget->ops->wakeup)
623  return -EOPNOTSUPP;
624  return gadget->ops->wakeup(gadget);
625 }
626 
636 static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
637 {
638  if (!gadget->ops->set_selfpowered)
639  return -EOPNOTSUPP;
640  return gadget->ops->set_selfpowered(gadget, 1);
641 }
642 
653 static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
654 {
655  if (!gadget->ops->set_selfpowered)
656  return -EOPNOTSUPP;
657  return gadget->ops->set_selfpowered(gadget, 0);
658 }
659 
673 static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget)
674 {
675  if (!gadget->ops->vbus_session)
676  return -EOPNOTSUPP;
677  return gadget->ops->vbus_session(gadget, 1);
678 }
679 
692 static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
693 {
694  if (!gadget->ops->vbus_draw)
695  return -EOPNOTSUPP;
696  return gadget->ops->vbus_draw(gadget, mA);
697 }
698 
710 static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
711 {
712  if (!gadget->ops->vbus_session)
713  return -EOPNOTSUPP;
714  return gadget->ops->vbus_session(gadget, 0);
715 }
716 
728 static inline int usb_gadget_connect(struct usb_gadget *gadget)
729 {
730  if (!gadget->ops->pullup)
731  return -EOPNOTSUPP;
732  return gadget->ops->pullup(gadget, 1);
733 }
734 
750 static inline int usb_gadget_disconnect(struct usb_gadget *gadget)
751 {
752  if (!gadget->ops->pullup)
753  return -EOPNOTSUPP;
754  return gadget->ops->pullup(gadget, 0);
755 }
756 
757 
758 /*-------------------------------------------------------------------------*/
759 
827  char *function;
829  int (*bind)(struct usb_gadget *gadget,
830  struct usb_gadget_driver *driver);
831  void (*unbind)(struct usb_gadget *);
832  int (*setup)(struct usb_gadget *,
833  const struct usb_ctrlrequest *);
834  void (*disconnect)(struct usb_gadget *);
835  void (*suspend)(struct usb_gadget *);
836  void (*resume)(struct usb_gadget *);
837 
838  /* FIXME support safe rmmod */
840 };
841 
842 
843 
844 /*-------------------------------------------------------------------------*/
845 
846 /* driver modules register and unregister, as usual.
847  * these calls must be made in a context that can sleep.
848  *
849  * these will usually be implemented directly by the hardware-dependent
850  * usb bus interface driver, which will only support a single driver.
851  */
852 
865 
880 
881 extern int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget);
882 extern void usb_del_gadget_udc(struct usb_gadget *gadget);
883 
884 /*-------------------------------------------------------------------------*/
885 
886 /* utility to simplify dealing with string descriptors */
887 
896 struct usb_string {
898  const char *s;
899 };
900 
910  u16 language; /* 0x0409 for en-us */
912 };
913 
914 /* put descriptor for string with that id into buf (buflen >= 256) */
915 int usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf);
916 
917 /*-------------------------------------------------------------------------*/
918 
919 /* utility to simplify managing config descriptors */
920 
921 /* write vector of descriptors into buffer */
922 int usb_descriptor_fillbuf(void *, unsigned,
923  const struct usb_descriptor_header **);
924 
925 /* build config descriptor from single descriptor vector */
927  void *buf, unsigned buflen, const struct usb_descriptor_header **desc);
928 
929 /* copy a NULL-terminated vector of descriptors */
931  struct usb_descriptor_header **);
932 
937 static inline void usb_free_descriptors(struct usb_descriptor_header **v)
938 {
939  kfree(v);
940 }
941 
942 /*-------------------------------------------------------------------------*/
943 
944 /* utility to simplify map/unmap of usb_requests to/from DMA */
945 
946 extern int usb_gadget_map_request(struct usb_gadget *gadget,
947  struct usb_request *req, int is_in);
948 
949 extern void usb_gadget_unmap_request(struct usb_gadget *gadget,
950  struct usb_request *req, int is_in);
951 
952 /*-------------------------------------------------------------------------*/
953 
954 /* utility wrapping a simple endpoint selection policy */
955 
956 extern struct usb_ep *usb_ep_autoconfig(struct usb_gadget *,
957  struct usb_endpoint_descriptor *);
958 
959 
960 extern struct usb_ep *usb_ep_autoconfig_ss(struct usb_gadget *,
961  struct usb_endpoint_descriptor *,
962  struct usb_ss_ep_comp_descriptor *);
963 
964 extern void usb_ep_autoconfig_reset(struct usb_gadget *);
965 
966 #endif /* __LINUX_USB_GADGET_H */