aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget/inode.c')
-rw-r--r--drivers/usb/gadget/inode.c240
1 files changed, 105 insertions, 135 deletions
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index 3fb1044a4db0..34296e79edcf 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -20,7 +20,7 @@
20 */ 20 */
21 21
22 22
23// #define DEBUG /* data to help fault diagnosis */ 23// #define DEBUG /* data to help fault diagnosis */
24// #define VERBOSE /* extra debug messages (success too) */ 24// #define VERBOSE /* extra debug messages (success too) */
25 25
26#include <linux/init.h> 26#include <linux/init.h>
@@ -59,11 +59,11 @@
59 * may serve as a source of device events, used to handle all control 59 * may serve as a source of device events, used to handle all control
60 * requests other than basic enumeration. 60 * requests other than basic enumeration.
61 * 61 *
62 * - Then either immediately, or after a SET_CONFIGURATION control request, 62 * - Then, after a SET_CONFIGURATION control request, ep_config() is
63 * ep_config() is called when each /dev/gadget/ep* file is configured 63 * called when each /dev/gadget/ep* file is configured (by writing
64 * (by writing endpoint descriptors). Afterwards these files are used 64 * endpoint descriptors). Afterwards these files are used to write()
65 * to write() IN data or to read() OUT data. To halt the endpoint, a 65 * IN data or to read() OUT data. To halt the endpoint, a "wrong
66 * "wrong direction" request is issued (like reading an IN endpoint). 66 * direction" request is issued (like reading an IN endpoint).
67 * 67 *
68 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe 68 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
69 * not possible on all hardware. For example, precise fault handling with 69 * not possible on all hardware. For example, precise fault handling with
@@ -98,16 +98,16 @@ enum ep0_state {
98 * must always write descriptors to initialize the device, then 98 * must always write descriptors to initialize the device, then
99 * the device becomes UNCONNECTED until enumeration. 99 * the device becomes UNCONNECTED until enumeration.
100 */ 100 */
101 STATE_OPENED, 101 STATE_DEV_OPENED,
102 102
103 /* From then on, ep0 fd is in either of two basic modes: 103 /* From then on, ep0 fd is in either of two basic modes:
104 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it 104 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
105 * - SETUP: read/write will transfer control data and succeed; 105 * - SETUP: read/write will transfer control data and succeed;
106 * or if "wrong direction", performs protocol stall 106 * or if "wrong direction", performs protocol stall
107 */ 107 */
108 STATE_UNCONNECTED, 108 STATE_DEV_UNCONNECTED,
109 STATE_CONNECTED, 109 STATE_DEV_CONNECTED,
110 STATE_SETUP, 110 STATE_DEV_SETUP,
111 111
112 /* UNBOUND means the driver closed ep0, so the device won't be 112 /* UNBOUND means the driver closed ep0, so the device won't be
113 * accessible again (DEV_DISABLED) until all fds are closed. 113 * accessible again (DEV_DISABLED) until all fds are closed.
@@ -121,7 +121,7 @@ enum ep0_state {
121struct dev_data { 121struct dev_data {
122 spinlock_t lock; 122 spinlock_t lock;
123 atomic_t count; 123 atomic_t count;
124 enum ep0_state state; 124 enum ep0_state state; /* P: lock */
125 struct usb_gadgetfs_event event [N_EVENT]; 125 struct usb_gadgetfs_event event [N_EVENT];
126 unsigned ev_next; 126 unsigned ev_next;
127 struct fasync_struct *fasync; 127 struct fasync_struct *fasync;
@@ -188,7 +188,6 @@ static struct dev_data *dev_new (void)
188enum ep_state { 188enum ep_state {
189 STATE_EP_DISABLED = 0, 189 STATE_EP_DISABLED = 0,
190 STATE_EP_READY, 190 STATE_EP_READY,
191 STATE_EP_DEFER_ENABLE,
192 STATE_EP_ENABLED, 191 STATE_EP_ENABLED,
193 STATE_EP_UNBOUND, 192 STATE_EP_UNBOUND,
194}; 193};
@@ -313,18 +312,10 @@ nonblock:
313 312
314 if ((val = down_interruptible (&epdata->lock)) < 0) 313 if ((val = down_interruptible (&epdata->lock)) < 0)
315 return val; 314 return val;
316newstate: 315
317 switch (epdata->state) { 316 switch (epdata->state) {
318 case STATE_EP_ENABLED: 317 case STATE_EP_ENABLED:
319 break; 318 break;
320 case STATE_EP_DEFER_ENABLE:
321 DBG (epdata->dev, "%s wait for host\n", epdata->name);
322 if ((val = wait_event_interruptible (epdata->wait,
323 epdata->state != STATE_EP_DEFER_ENABLE
324 || epdata->dev->state == STATE_DEV_UNBOUND
325 )) < 0)
326 goto fail;
327 goto newstate;
328 // case STATE_EP_DISABLED: /* "can't happen" */ 319 // case STATE_EP_DISABLED: /* "can't happen" */
329 // case STATE_EP_READY: /* "can't happen" */ 320 // case STATE_EP_READY: /* "can't happen" */
330 default: /* error! */ 321 default: /* error! */
@@ -333,7 +324,6 @@ newstate:
333 // FALLTHROUGH 324 // FALLTHROUGH
334 case STATE_EP_UNBOUND: /* clean disconnect */ 325 case STATE_EP_UNBOUND: /* clean disconnect */
335 val = -ENODEV; 326 val = -ENODEV;
336fail:
337 up (&epdata->lock); 327 up (&epdata->lock);
338 } 328 }
339 return val; 329 return val;
@@ -565,29 +555,28 @@ static ssize_t ep_aio_read_retry(struct kiocb *iocb)
565 ssize_t len, total; 555 ssize_t len, total;
566 int i; 556 int i;
567 557
568 /* we "retry" to get the right mm context for this: */ 558 /* we "retry" to get the right mm context for this: */
569 559
570 /* copy stuff into user buffers */ 560 /* copy stuff into user buffers */
571 total = priv->actual; 561 total = priv->actual;
572 len = 0; 562 len = 0;
573 for (i=0; i < priv->nr_segs; i++) { 563 for (i=0; i < priv->nr_segs; i++) {
574 ssize_t this = min((ssize_t)(priv->iv[i].iov_len), total); 564 ssize_t this = min((ssize_t)(priv->iv[i].iov_len), total);
575 565
576 if (copy_to_user(priv->iv[i].iov_base, priv->buf, this)) { 566 if (copy_to_user(priv->iv[i].iov_base, priv->buf, this)) {
577 if (len == 0) 567 if (len == 0)
578 len = -EFAULT; 568 len = -EFAULT;
579 break; 569 break;
580 } 570 }
581 571
582 total -= this; 572 total -= this;
583 len += this; 573 len += this;
584 if (total == 0) 574 if (total == 0)
585 break; 575 break;
586 } 576 }
587 kfree(priv->buf); 577 kfree(priv->buf);
588 kfree(priv); 578 kfree(priv);
589 aio_put_req(iocb); 579 return len;
590 return len;
591} 580}
592 581
593static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req) 582static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
@@ -600,18 +589,17 @@ static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
600 spin_lock(&epdata->dev->lock); 589 spin_lock(&epdata->dev->lock);
601 priv->req = NULL; 590 priv->req = NULL;
602 priv->epdata = NULL; 591 priv->epdata = NULL;
603 if (priv->iv == NULL 592
604 || unlikely(req->actual == 0) 593 /* if this was a write or a read returning no data then we
605 || unlikely(kiocbIsCancelled(iocb))) { 594 * don't need to copy anything to userspace, so we can
595 * complete the aio request immediately.
596 */
597 if (priv->iv == NULL || unlikely(req->actual == 0)) {
606 kfree(req->buf); 598 kfree(req->buf);
607 kfree(priv); 599 kfree(priv);
608 iocb->private = NULL; 600 iocb->private = NULL;
609 /* aio_complete() reports bytes-transferred _and_ faults */ 601 /* aio_complete() reports bytes-transferred _and_ faults */
610 if (unlikely(kiocbIsCancelled(iocb))) 602 aio_complete(iocb, req->actual ? req->actual : req->status,
611 aio_put_req(iocb);
612 else
613 aio_complete(iocb,
614 req->actual ? req->actual : req->status,
615 req->status); 603 req->status);
616 } else { 604 } else {
617 /* retry() won't report both; so we hide some faults */ 605 /* retry() won't report both; so we hide some faults */
@@ -636,7 +624,7 @@ ep_aio_rwtail(
636 size_t len, 624 size_t len,
637 struct ep_data *epdata, 625 struct ep_data *epdata,
638 const struct iovec *iv, 626 const struct iovec *iv,
639 unsigned long nr_segs 627 unsigned long nr_segs
640) 628)
641{ 629{
642 struct kiocb_priv *priv; 630 struct kiocb_priv *priv;
@@ -852,9 +840,9 @@ ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
852 break; 840 break;
853#endif 841#endif
854 default: 842 default:
855 DBG (data->dev, "unconnected, %s init deferred\n", 843 DBG(data->dev, "unconnected, %s init abandoned\n",
856 data->name); 844 data->name);
857 data->state = STATE_EP_DEFER_ENABLE; 845 value = -EINVAL;
858 } 846 }
859 if (value == 0) { 847 if (value == 0) {
860 fd->f_op = &ep_io_operations; 848 fd->f_op = &ep_io_operations;
@@ -943,22 +931,24 @@ static void clean_req (struct usb_ep *ep, struct usb_request *req)
943static void ep0_complete (struct usb_ep *ep, struct usb_request *req) 931static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
944{ 932{
945 struct dev_data *dev = ep->driver_data; 933 struct dev_data *dev = ep->driver_data;
934 unsigned long flags;
946 int free = 1; 935 int free = 1;
947 936
948 /* for control OUT, data must still get to userspace */ 937 /* for control OUT, data must still get to userspace */
938 spin_lock_irqsave(&dev->lock, flags);
949 if (!dev->setup_in) { 939 if (!dev->setup_in) {
950 dev->setup_out_error = (req->status != 0); 940 dev->setup_out_error = (req->status != 0);
951 if (!dev->setup_out_error) 941 if (!dev->setup_out_error)
952 free = 0; 942 free = 0;
953 dev->setup_out_ready = 1; 943 dev->setup_out_ready = 1;
954 ep0_readable (dev); 944 ep0_readable (dev);
955 } else if (dev->state == STATE_SETUP) 945 }
956 dev->state = STATE_CONNECTED;
957 946
958 /* clean up as appropriate */ 947 /* clean up as appropriate */
959 if (free && req->buf != &dev->rbuf) 948 if (free && req->buf != &dev->rbuf)
960 clean_req (ep, req); 949 clean_req (ep, req);
961 req->complete = epio_complete; 950 req->complete = epio_complete;
951 spin_unlock_irqrestore(&dev->lock, flags);
962} 952}
963 953
964static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len) 954static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
@@ -998,13 +988,13 @@ ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
998 } 988 }
999 989
1000 /* control DATA stage */ 990 /* control DATA stage */
1001 if ((state = dev->state) == STATE_SETUP) { 991 if ((state = dev->state) == STATE_DEV_SETUP) {
1002 992
1003 if (dev->setup_in) { /* stall IN */ 993 if (dev->setup_in) { /* stall IN */
1004 VDEBUG(dev, "ep0in stall\n"); 994 VDEBUG(dev, "ep0in stall\n");
1005 (void) usb_ep_set_halt (dev->gadget->ep0); 995 (void) usb_ep_set_halt (dev->gadget->ep0);
1006 retval = -EL2HLT; 996 retval = -EL2HLT;
1007 dev->state = STATE_CONNECTED; 997 dev->state = STATE_DEV_CONNECTED;
1008 998
1009 } else if (len == 0) { /* ack SET_CONFIGURATION etc */ 999 } else if (len == 0) { /* ack SET_CONFIGURATION etc */
1010 struct usb_ep *ep = dev->gadget->ep0; 1000 struct usb_ep *ep = dev->gadget->ep0;
@@ -1012,7 +1002,7 @@ ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
1012 1002
1013 if ((retval = setup_req (ep, req, 0)) == 0) 1003 if ((retval = setup_req (ep, req, 0)) == 0)
1014 retval = usb_ep_queue (ep, req, GFP_ATOMIC); 1004 retval = usb_ep_queue (ep, req, GFP_ATOMIC);
1015 dev->state = STATE_CONNECTED; 1005 dev->state = STATE_DEV_CONNECTED;
1016 1006
1017 /* assume that was SET_CONFIGURATION */ 1007 /* assume that was SET_CONFIGURATION */
1018 if (dev->current_config) { 1008 if (dev->current_config) {
@@ -1040,6 +1030,13 @@ ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
1040 spin_lock_irq (&dev->lock); 1030 spin_lock_irq (&dev->lock);
1041 if (retval) 1031 if (retval)
1042 goto done; 1032 goto done;
1033
1034 if (dev->state != STATE_DEV_SETUP) {
1035 retval = -ECANCELED;
1036 goto done;
1037 }
1038 dev->state = STATE_DEV_CONNECTED;
1039
1043 if (dev->setup_out_error) 1040 if (dev->setup_out_error)
1044 retval = -EIO; 1041 retval = -EIO;
1045 else { 1042 else {
@@ -1066,39 +1063,36 @@ scan:
1066 /* return queued events right away */ 1063 /* return queued events right away */
1067 if (dev->ev_next != 0) { 1064 if (dev->ev_next != 0) {
1068 unsigned i, n; 1065 unsigned i, n;
1069 int tmp = dev->ev_next;
1070 1066
1071 len = min (len, tmp * sizeof (struct usb_gadgetfs_event));
1072 n = len / sizeof (struct usb_gadgetfs_event); 1067 n = len / sizeof (struct usb_gadgetfs_event);
1068 if (dev->ev_next < n)
1069 n = dev->ev_next;
1073 1070
1074 /* ep0 can't deliver events when STATE_SETUP */ 1071 /* ep0 i/o has special semantics during STATE_DEV_SETUP */
1075 for (i = 0; i < n; i++) { 1072 for (i = 0; i < n; i++) {
1076 if (dev->event [i].type == GADGETFS_SETUP) { 1073 if (dev->event [i].type == GADGETFS_SETUP) {
1077 len = i + 1; 1074 dev->state = STATE_DEV_SETUP;
1078 len *= sizeof (struct usb_gadgetfs_event); 1075 n = i + 1;
1079 n = 0;
1080 break; 1076 break;
1081 } 1077 }
1082 } 1078 }
1083 spin_unlock_irq (&dev->lock); 1079 spin_unlock_irq (&dev->lock);
1080 len = n * sizeof (struct usb_gadgetfs_event);
1084 if (copy_to_user (buf, &dev->event, len)) 1081 if (copy_to_user (buf, &dev->event, len))
1085 retval = -EFAULT; 1082 retval = -EFAULT;
1086 else 1083 else
1087 retval = len; 1084 retval = len;
1088 if (len > 0) { 1085 if (len > 0) {
1089 len /= sizeof (struct usb_gadgetfs_event);
1090
1091 /* NOTE this doesn't guard against broken drivers; 1086 /* NOTE this doesn't guard against broken drivers;
1092 * concurrent ep0 readers may lose events. 1087 * concurrent ep0 readers may lose events.
1093 */ 1088 */
1094 spin_lock_irq (&dev->lock); 1089 spin_lock_irq (&dev->lock);
1095 dev->ev_next -= len; 1090 if (dev->ev_next > n) {
1096 if (dev->ev_next != 0) 1091 memmove(&dev->event[0], &dev->event[n],
1097 memmove (&dev->event, &dev->event [len],
1098 sizeof (struct usb_gadgetfs_event) 1092 sizeof (struct usb_gadgetfs_event)
1099 * (tmp - len)); 1093 * (dev->ev_next - n));
1100 if (n == 0) 1094 }
1101 dev->state = STATE_SETUP; 1095 dev->ev_next -= n;
1102 spin_unlock_irq (&dev->lock); 1096 spin_unlock_irq (&dev->lock);
1103 } 1097 }
1104 return retval; 1098 return retval;
@@ -1113,8 +1107,8 @@ scan:
1113 DBG (dev, "fail %s, state %d\n", __FUNCTION__, state); 1107 DBG (dev, "fail %s, state %d\n", __FUNCTION__, state);
1114 retval = -ESRCH; 1108 retval = -ESRCH;
1115 break; 1109 break;
1116 case STATE_UNCONNECTED: 1110 case STATE_DEV_UNCONNECTED:
1117 case STATE_CONNECTED: 1111 case STATE_DEV_CONNECTED:
1118 spin_unlock_irq (&dev->lock); 1112 spin_unlock_irq (&dev->lock);
1119 DBG (dev, "%s wait\n", __FUNCTION__); 1113 DBG (dev, "%s wait\n", __FUNCTION__);
1120 1114
@@ -1141,7 +1135,7 @@ next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1141 switch (type) { 1135 switch (type) {
1142 /* these events purge the queue */ 1136 /* these events purge the queue */
1143 case GADGETFS_DISCONNECT: 1137 case GADGETFS_DISCONNECT:
1144 if (dev->state == STATE_SETUP) 1138 if (dev->state == STATE_DEV_SETUP)
1145 dev->setup_abort = 1; 1139 dev->setup_abort = 1;
1146 // FALL THROUGH 1140 // FALL THROUGH
1147 case GADGETFS_CONNECT: 1141 case GADGETFS_CONNECT:
@@ -1153,7 +1147,7 @@ next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1153 for (i = 0; i != dev->ev_next; i++) { 1147 for (i = 0; i != dev->ev_next; i++) {
1154 if (dev->event [i].type != type) 1148 if (dev->event [i].type != type)
1155 continue; 1149 continue;
1156 DBG (dev, "discard old event %d\n", type); 1150 DBG(dev, "discard old event[%d] %d\n", i, type);
1157 dev->ev_next--; 1151 dev->ev_next--;
1158 if (i == dev->ev_next) 1152 if (i == dev->ev_next)
1159 break; 1153 break;
@@ -1166,9 +1160,9 @@ next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1166 default: 1160 default:
1167 BUG (); 1161 BUG ();
1168 } 1162 }
1163 VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
1169 event = &dev->event [dev->ev_next++]; 1164 event = &dev->event [dev->ev_next++];
1170 BUG_ON (dev->ev_next > N_EVENT); 1165 BUG_ON (dev->ev_next > N_EVENT);
1171 VDEBUG (dev, "ev %d, next %d\n", type, dev->ev_next);
1172 memset (event, 0, sizeof *event); 1166 memset (event, 0, sizeof *event);
1173 event->type = type; 1167 event->type = type;
1174 return event; 1168 return event;
@@ -1188,12 +1182,13 @@ ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1188 retval = -EIDRM; 1182 retval = -EIDRM;
1189 1183
1190 /* data and/or status stage for control request */ 1184 /* data and/or status stage for control request */
1191 } else if (dev->state == STATE_SETUP) { 1185 } else if (dev->state == STATE_DEV_SETUP) {
1192 1186
1193 /* IN DATA+STATUS caller makes len <= wLength */ 1187 /* IN DATA+STATUS caller makes len <= wLength */
1194 if (dev->setup_in) { 1188 if (dev->setup_in) {
1195 retval = setup_req (dev->gadget->ep0, dev->req, len); 1189 retval = setup_req (dev->gadget->ep0, dev->req, len);
1196 if (retval == 0) { 1190 if (retval == 0) {
1191 dev->state = STATE_DEV_CONNECTED;
1197 spin_unlock_irq (&dev->lock); 1192 spin_unlock_irq (&dev->lock);
1198 if (copy_from_user (dev->req->buf, buf, len)) 1193 if (copy_from_user (dev->req->buf, buf, len))
1199 retval = -EFAULT; 1194 retval = -EFAULT;
@@ -1219,7 +1214,7 @@ ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1219 VDEBUG(dev, "ep0out stall\n"); 1214 VDEBUG(dev, "ep0out stall\n");
1220 (void) usb_ep_set_halt (dev->gadget->ep0); 1215 (void) usb_ep_set_halt (dev->gadget->ep0);
1221 retval = -EL2HLT; 1216 retval = -EL2HLT;
1222 dev->state = STATE_CONNECTED; 1217 dev->state = STATE_DEV_CONNECTED;
1223 } else { 1218 } else {
1224 DBG(dev, "bogus ep0out stall!\n"); 1219 DBG(dev, "bogus ep0out stall!\n");
1225 } 1220 }
@@ -1261,7 +1256,9 @@ dev_release (struct inode *inode, struct file *fd)
1261 put_dev (dev); 1256 put_dev (dev);
1262 1257
1263 /* other endpoints were all decoupled from this device */ 1258 /* other endpoints were all decoupled from this device */
1259 spin_lock_irq(&dev->lock);
1264 dev->state = STATE_DEV_DISABLED; 1260 dev->state = STATE_DEV_DISABLED;
1261 spin_unlock_irq(&dev->lock);
1265 return 0; 1262 return 0;
1266} 1263}
1267 1264
@@ -1282,7 +1279,7 @@ ep0_poll (struct file *fd, poll_table *wait)
1282 goto out; 1279 goto out;
1283 } 1280 }
1284 1281
1285 if (dev->state == STATE_SETUP) { 1282 if (dev->state == STATE_DEV_SETUP) {
1286 if (dev->setup_in || dev->setup_can_stall) 1283 if (dev->setup_in || dev->setup_can_stall)
1287 mask = POLLOUT; 1284 mask = POLLOUT;
1288 } else { 1285 } else {
@@ -1392,52 +1389,29 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1392 1389
1393 spin_lock (&dev->lock); 1390 spin_lock (&dev->lock);
1394 dev->setup_abort = 0; 1391 dev->setup_abort = 0;
1395 if (dev->state == STATE_UNCONNECTED) { 1392 if (dev->state == STATE_DEV_UNCONNECTED) {
1396 struct usb_ep *ep;
1397 struct ep_data *data;
1398
1399 dev->state = STATE_CONNECTED;
1400 dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
1401
1402#ifdef CONFIG_USB_GADGET_DUALSPEED 1393#ifdef CONFIG_USB_GADGET_DUALSPEED
1403 if (gadget->speed == USB_SPEED_HIGH && dev->hs_config == 0) { 1394 if (gadget->speed == USB_SPEED_HIGH && dev->hs_config == 0) {
1395 spin_unlock(&dev->lock);
1404 ERROR (dev, "no high speed config??\n"); 1396 ERROR (dev, "no high speed config??\n");
1405 return -EINVAL; 1397 return -EINVAL;
1406 } 1398 }
1407#endif /* CONFIG_USB_GADGET_DUALSPEED */ 1399#endif /* CONFIG_USB_GADGET_DUALSPEED */
1408 1400
1401 dev->state = STATE_DEV_CONNECTED;
1402 dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
1403
1409 INFO (dev, "connected\n"); 1404 INFO (dev, "connected\n");
1410 event = next_event (dev, GADGETFS_CONNECT); 1405 event = next_event (dev, GADGETFS_CONNECT);
1411 event->u.speed = gadget->speed; 1406 event->u.speed = gadget->speed;
1412 ep0_readable (dev); 1407 ep0_readable (dev);
1413 1408
1414 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
1415 data = ep->driver_data;
1416 /* ... down_trylock (&data->lock) ... */
1417 if (data->state != STATE_EP_DEFER_ENABLE)
1418 continue;
1419#ifdef CONFIG_USB_GADGET_DUALSPEED
1420 if (gadget->speed == USB_SPEED_HIGH)
1421 value = usb_ep_enable (ep, &data->hs_desc);
1422 else
1423#endif /* CONFIG_USB_GADGET_DUALSPEED */
1424 value = usb_ep_enable (ep, &data->desc);
1425 if (value) {
1426 ERROR (dev, "deferred %s enable --> %d\n",
1427 data->name, value);
1428 continue;
1429 }
1430 data->state = STATE_EP_ENABLED;
1431 wake_up (&data->wait);
1432 DBG (dev, "woke up %s waiters\n", data->name);
1433 }
1434
1435 /* host may have given up waiting for response. we can miss control 1409 /* host may have given up waiting for response. we can miss control
1436 * requests handled lower down (device/endpoint status and features); 1410 * requests handled lower down (device/endpoint status and features);
1437 * then ep0_{read,write} will report the wrong status. controller 1411 * then ep0_{read,write} will report the wrong status. controller
1438 * driver will have aborted pending i/o. 1412 * driver will have aborted pending i/o.
1439 */ 1413 */
1440 } else if (dev->state == STATE_SETUP) 1414 } else if (dev->state == STATE_DEV_SETUP)
1441 dev->setup_abort = 1; 1415 dev->setup_abort = 1;
1442 1416
1443 req->buf = dev->rbuf; 1417 req->buf = dev->rbuf;
@@ -1583,7 +1557,7 @@ delegate:
1583 } 1557 }
1584 1558
1585 /* proceed with data transfer and status phases? */ 1559 /* proceed with data transfer and status phases? */
1586 if (value >= 0 && dev->state != STATE_SETUP) { 1560 if (value >= 0 && dev->state != STATE_DEV_SETUP) {
1587 req->length = value; 1561 req->length = value;
1588 req->zero = value < w_length; 1562 req->zero = value < w_length;
1589 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC); 1563 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
@@ -1747,7 +1721,9 @@ gadgetfs_bind (struct usb_gadget *gadget)
1747 goto enomem; 1721 goto enomem;
1748 1722
1749 INFO (dev, "bound to %s driver\n", gadget->name); 1723 INFO (dev, "bound to %s driver\n", gadget->name);
1750 dev->state = STATE_UNCONNECTED; 1724 spin_lock_irq(&dev->lock);
1725 dev->state = STATE_DEV_UNCONNECTED;
1726 spin_unlock_irq(&dev->lock);
1751 get_dev (dev); 1727 get_dev (dev);
1752 return 0; 1728 return 0;
1753 1729
@@ -1762,11 +1738,9 @@ gadgetfs_disconnect (struct usb_gadget *gadget)
1762 struct dev_data *dev = get_gadget_data (gadget); 1738 struct dev_data *dev = get_gadget_data (gadget);
1763 1739
1764 spin_lock (&dev->lock); 1740 spin_lock (&dev->lock);
1765 if (dev->state == STATE_UNCONNECTED) { 1741 if (dev->state == STATE_DEV_UNCONNECTED)
1766 DBG (dev, "already unconnected\n");
1767 goto exit; 1742 goto exit;
1768 } 1743 dev->state = STATE_DEV_UNCONNECTED;
1769 dev->state = STATE_UNCONNECTED;
1770 1744
1771 INFO (dev, "disconnected\n"); 1745 INFO (dev, "disconnected\n");
1772 next_event (dev, GADGETFS_DISCONNECT); 1746 next_event (dev, GADGETFS_DISCONNECT);
@@ -1783,9 +1757,9 @@ gadgetfs_suspend (struct usb_gadget *gadget)
1783 INFO (dev, "suspended from state %d\n", dev->state); 1757 INFO (dev, "suspended from state %d\n", dev->state);
1784 spin_lock (&dev->lock); 1758 spin_lock (&dev->lock);
1785 switch (dev->state) { 1759 switch (dev->state) {
1786 case STATE_SETUP: // VERY odd... host died?? 1760 case STATE_DEV_SETUP: // VERY odd... host died??
1787 case STATE_CONNECTED: 1761 case STATE_DEV_CONNECTED:
1788 case STATE_UNCONNECTED: 1762 case STATE_DEV_UNCONNECTED:
1789 next_event (dev, GADGETFS_SUSPEND); 1763 next_event (dev, GADGETFS_SUSPEND);
1790 ep0_readable (dev); 1764 ep0_readable (dev);
1791 /* FALLTHROUGH */ 1765 /* FALLTHROUGH */
@@ -1808,7 +1782,7 @@ static struct usb_gadget_driver gadgetfs_driver = {
1808 .disconnect = gadgetfs_disconnect, 1782 .disconnect = gadgetfs_disconnect,
1809 .suspend = gadgetfs_suspend, 1783 .suspend = gadgetfs_suspend,
1810 1784
1811 .driver = { 1785 .driver = {
1812 .name = (char *) shortname, 1786 .name = (char *) shortname,
1813 }, 1787 },
1814}; 1788};
@@ -1829,7 +1803,7 @@ static struct usb_gadget_driver probe_driver = {
1829 .unbind = gadgetfs_nop, 1803 .unbind = gadgetfs_nop,
1830 .setup = (void *)gadgetfs_nop, 1804 .setup = (void *)gadgetfs_nop,
1831 .disconnect = gadgetfs_nop, 1805 .disconnect = gadgetfs_nop,
1832 .driver = { 1806 .driver = {
1833 .name = "nop", 1807 .name = "nop",
1834 }, 1808 },
1835}; 1809};
@@ -1849,19 +1823,16 @@ static struct usb_gadget_driver probe_driver = {
1849 * . full/low speed config ... all wTotalLength bytes (with interface, 1823 * . full/low speed config ... all wTotalLength bytes (with interface,
1850 * class, altsetting, endpoint, and other descriptors) 1824 * class, altsetting, endpoint, and other descriptors)
1851 * . high speed config ... all descriptors, for high speed operation; 1825 * . high speed config ... all descriptors, for high speed operation;
1852 * this one's optional except for high-speed hardware 1826 * this one's optional except for high-speed hardware
1853 * . device descriptor 1827 * . device descriptor
1854 * 1828 *
1855 * Endpoints are not yet enabled. Drivers may want to immediately 1829 * Endpoints are not yet enabled. Drivers must wait until device
1856 * initialize them, using the /dev/gadget/ep* files that are available 1830 * configuration and interface altsetting changes create
1857 * as soon as the kernel sees the configuration, or they can wait
1858 * until device configuration and interface altsetting changes create
1859 * the need to configure (or unconfigure) them. 1831 * the need to configure (or unconfigure) them.
1860 * 1832 *
1861 * After initialization, the device stays active for as long as that 1833 * After initialization, the device stays active for as long as that
1862 * $CHIP file is open. Events may then be read from that descriptor, 1834 * $CHIP file is open. Events must then be read from that descriptor,
1863 * such as configuration notifications. More complex drivers will handle 1835 * such as configuration notifications.
1864 * some control requests in user space.
1865 */ 1836 */
1866 1837
1867static int is_valid_config (struct usb_config_descriptor *config) 1838static int is_valid_config (struct usb_config_descriptor *config)
@@ -1884,9 +1855,6 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1884 u32 tag; 1855 u32 tag;
1885 char *kbuf; 1856 char *kbuf;
1886 1857
1887 if (dev->state != STATE_OPENED)
1888 return -EEXIST;
1889
1890 if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4)) 1858 if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
1891 return -EINVAL; 1859 return -EINVAL;
1892 1860
@@ -1978,13 +1946,15 @@ dev_open (struct inode *inode, struct file *fd)
1978 struct dev_data *dev = inode->i_private; 1946 struct dev_data *dev = inode->i_private;
1979 int value = -EBUSY; 1947 int value = -EBUSY;
1980 1948
1949 spin_lock_irq(&dev->lock);
1981 if (dev->state == STATE_DEV_DISABLED) { 1950 if (dev->state == STATE_DEV_DISABLED) {
1982 dev->ev_next = 0; 1951 dev->ev_next = 0;
1983 dev->state = STATE_OPENED; 1952 dev->state = STATE_DEV_OPENED;
1984 fd->private_data = dev; 1953 fd->private_data = dev;
1985 get_dev (dev); 1954 get_dev (dev);
1986 value = 0; 1955 value = 0;
1987 } 1956 }
1957 spin_unlock_irq(&dev->lock);
1988 return value; 1958 return value;
1989} 1959}
1990 1960