VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / usb / gadget / inode.c
1 /*
2  * inode.c -- user mode filesystem api for usb gadget controllers
3  *
4  * Copyright (C) 2003 David Brownell
5  * Copyright (C) 2003 Agilent Technologies
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22
23 // #define      DEBUG                   /* data to help fault diagnosis */
24 // #define      VERBOSE         /* extra debug messages (success too) */
25
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/fs.h>
29 #include <linux/pagemap.h>
30 #include <linux/uts.h>
31 #include <linux/wait.h>
32 #include <linux/compiler.h>
33 #include <asm/uaccess.h>
34 #include <linux/slab.h>
35
36 #include <linux/device.h>
37 #include <linux/moduleparam.h>
38
39 #include <linux/usb_gadgetfs.h>
40 #include <linux/usb_gadget.h>
41
42
43 /*
44  * The gadgetfs API maps each endpoint to a file descriptor so that you
45  * can use standard synchronous read/write calls for I/O.  There's some
46  * O_NONBLOCK and O_ASYNC/FASYNC style i/o support.  Example usermode
47  * drivers show how this works in practice.  You can also use AIO to
48  * eliminate I/O gaps between requests, to help when streaming data.
49  *
50  * Key parts that must be USB-specific are protocols defining how the
51  * read/write operations relate to the hardware state machines.  There
52  * are two types of files.  One type is for the device, implementing ep0.
53  * The other type is for each IN or OUT endpoint.  In both cases, the
54  * user mode driver must configure the hardware before using it.
55  *
56  * - First, dev_config() is called when /dev/gadget/$CHIP is configured
57  *   (by writing configuration and device descriptors).  Afterwards it
58  *   may serve as a source of device events, used to handle all control
59  *   requests other than basic enumeration.
60  *
61  * - Then either immediately, or after a SET_CONFIGURATION control request,
62  *   ep_config() is called when each /dev/gadget/ep* file is configured
63  *   (by writing endpoint descriptors).  Afterwards these files are used
64  *   to write() IN data or to read() OUT data.  To halt the endpoint, a
65  *   "wrong direction" request is issued (like reading an IN endpoint).
66  *
67  * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
68  * not possible on all hardware.  For example, precise fault handling with
69  * respect to data left in endpoint fifos after aborted operations; or
70  * selective clearing of endpoint halts, to implement SET_INTERFACE.
71  */
72
73 #define DRIVER_DESC     "USB Gadget filesystem"
74 #define DRIVER_VERSION  "18 Nov 2003"
75
76 static const char driver_desc [] = DRIVER_DESC;
77 static const char shortname [] = "gadgetfs";
78
79 MODULE_DESCRIPTION (DRIVER_DESC);
80 MODULE_AUTHOR ("David Brownell");
81 MODULE_LICENSE ("GPL");
82
83
84 /*----------------------------------------------------------------------*/
85
86 #define GADGETFS_MAGIC          0xaee71ee7
87 #define DMA_ADDR_INVALID        (~(dma_addr_t)0)
88
89 /* /dev/gadget/$CHIP represents ep0 and the whole device */
90 enum ep0_state {
91         /* DISBLED is the initial state.
92          */
93         STATE_DEV_DISABLED = 0,
94
95         /* Only one open() of /dev/gadget/$CHIP; only one file tracks
96          * ep0/device i/o modes and binding to the controller.  Driver
97          * must always write descriptors to initialize the device, then
98          * the device becomes UNCONNECTED until enumeration.
99          */
100         STATE_OPENED,
101
102         /* From then on, ep0 fd is in either of two basic modes:
103          * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
104          * - SETUP: read/write will transfer control data and succeed;
105          *   or if "wrong direction", performs protocol stall
106          */
107         STATE_UNCONNECTED,
108         STATE_CONNECTED,
109         STATE_SETUP,
110
111         /* UNBOUND means the driver closed ep0, so the device won't be
112          * accessible again (DEV_DISABLED) until all fds are closed.
113          */
114         STATE_DEV_UNBOUND,
115 };
116
117 /* enough for the whole queue: most events invalidate others */
118 #define N_EVENT                 5
119
120 struct dev_data {
121         spinlock_t                      lock;
122         atomic_t                        count;
123         enum ep0_state                  state;
124         struct usb_gadgetfs_event       event [N_EVENT];
125         unsigned                        ev_next;
126         struct fasync_struct            *fasync;
127         u8                              current_config;
128
129         /* drivers reading ep0 MUST handle control requests (SETUP)
130          * reported that way; else the host will time out.
131          */
132         unsigned                        usermode_setup : 1,
133                                         setup_in : 1,
134                                         setup_can_stall : 1,
135                                         setup_out_ready : 1,
136                                         setup_out_error : 1,
137                                         setup_abort : 1;
138
139         /* the rest is basically write-once */
140         struct usb_config_descriptor    *config, *hs_config;
141         struct usb_device_descriptor    *dev;
142         struct usb_request              *req;
143         struct usb_gadget               *gadget;
144         struct list_head                epfiles;
145         void                            *buf;
146         wait_queue_head_t               wait;
147         struct super_block              *sb;
148         struct dentry                   *dentry;
149
150         /* except this scratch i/o buffer for ep0 */
151         u8                              rbuf [256];
152 };
153
154 static inline void get_dev (struct dev_data *data)
155 {
156         atomic_inc (&data->count);
157 }
158
159 static void put_dev (struct dev_data *data)
160 {
161         if (likely (!atomic_dec_and_test (&data->count)))
162                 return;
163         /* needs no more cleanup */
164         BUG_ON (waitqueue_active (&data->wait));
165         kfree (data);
166 }
167
168 static struct dev_data *dev_new (void)
169 {
170         struct dev_data         *dev;
171
172         dev = kmalloc (sizeof *dev, GFP_KERNEL);
173         if (!dev)
174                 return NULL;
175         memset (dev, 0, sizeof *dev);
176         dev->state = STATE_DEV_DISABLED;
177         atomic_set (&dev->count, 1);
178         spin_lock_init (&dev->lock);
179         INIT_LIST_HEAD (&dev->epfiles);
180         init_waitqueue_head (&dev->wait);
181         return dev;
182 }
183
184 /*----------------------------------------------------------------------*/
185
186 /* other /dev/gadget/$ENDPOINT files represent endpoints */
187 enum ep_state {
188         STATE_EP_DISABLED = 0,
189         STATE_EP_READY,
190         STATE_EP_DEFER_ENABLE,
191         STATE_EP_ENABLED,
192         STATE_EP_UNBOUND,
193 };
194
195 struct ep_data {
196         struct semaphore                lock;
197         enum ep_state                   state;
198         atomic_t                        count;
199         struct dev_data                 *dev;
200         /* must hold dev->lock before accessing ep or req */
201         struct usb_ep                   *ep;
202         struct usb_request              *req;
203         ssize_t                         status;
204         char                            name [16];
205         struct usb_endpoint_descriptor  desc, hs_desc;
206         struct list_head                epfiles;
207         wait_queue_head_t               wait;
208         struct dentry                   *dentry;
209         struct inode                    *inode;
210 };
211
212 static inline void get_ep (struct ep_data *data)
213 {
214         atomic_inc (&data->count);
215 }
216
217 static void put_ep (struct ep_data *data)
218 {
219         if (likely (!atomic_dec_and_test (&data->count)))
220                 return;
221         put_dev (data->dev);
222         /* needs no more cleanup */
223         BUG_ON (!list_empty (&data->epfiles));
224         BUG_ON (waitqueue_active (&data->wait));
225         BUG_ON (down_trylock (&data->lock) != 0);
226         kfree (data);
227 }
228
229 /*----------------------------------------------------------------------*/
230
231 /* most "how to use the hardware" policy choices are in userspace:
232  * mapping endpoint roles the driver needs to the capabilities that
233  * the usb controller exposes.
234  */
235
236 #ifdef  CONFIG_USB_GADGET_DUMMY_HCD
237 /* act (mostly) like a net2280 */
238 #define CONFIG_USB_GADGET_NET2280
239 #endif
240
241 #ifdef  CONFIG_USB_GADGET_NET2280
242 #define CHIP                    "net2280"
243 #define HIGHSPEED
244 #endif
245
246 #ifdef  CONFIG_USB_GADGET_PXA2XX
247 #define CHIP                    "pxa2xx_udc"
248 /* earlier hardware doesn't have UDCCFR, races set_{config,interface} */
249 #warning works best with pxa255 or newer
250 #endif
251
252 #ifdef  CONFIG_USB_GADGET_GOKU
253 #define CHIP                    "goku_udc"
254 #endif
255
256 #ifdef  CONFIG_USB_GADGET_OMAP
257 #define CHIP                    "omap_udc"
258 #endif
259
260 #ifdef  CONFIG_USB_GADGET_SA1100
261 #define CHIP                    "sa1100"
262 #endif
263
264 /*----------------------------------------------------------------------*/
265
266 /* NOTE:  don't use dev_printk calls before binding to the gadget
267  * at the end of ep0 configuration, or after unbind.
268  */
269
270 /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
271 #define xprintk(d,level,fmt,args...) \
272         printk(level "%s: " fmt , shortname , ## args)
273
274 #ifdef DEBUG
275 #define DBG(dev,fmt,args...) \
276         xprintk(dev , KERN_DEBUG , fmt , ## args)
277 #else
278 #define DBG(dev,fmt,args...) \
279         do { } while (0)
280 #endif /* DEBUG */
281
282 #ifdef VERBOSE
283 #define VDEBUG  DBG
284 #else
285 #define VDEBUG(dev,fmt,args...) \
286         do { } while (0)
287 #endif /* DEBUG */
288
289 #define ERROR(dev,fmt,args...) \
290         xprintk(dev , KERN_ERR , fmt , ## args)
291 #define WARN(dev,fmt,args...) \
292         xprintk(dev , KERN_WARNING , fmt , ## args)
293 #define INFO(dev,fmt,args...) \
294         xprintk(dev , KERN_INFO , fmt , ## args)
295
296
297 /*----------------------------------------------------------------------*/
298
299 /* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
300  *
301  * After opening, configure non-control endpoints.  Then use normal
302  * stream read() and write() requests; and maybe ioctl() to get more
303  * precise FIFO status when recovering from cancelation.
304  */
305
306 static void epio_complete (struct usb_ep *ep, struct usb_request *req)
307 {
308         struct ep_data  *epdata = ep->driver_data;
309
310         if (!req->context)
311                 return;
312         if (req->status)
313                 epdata->status = req->status;
314         else
315                 epdata->status = req->actual;
316         complete ((struct completion *)req->context);
317 }
318
319 /* tasklock endpoint, returning when it's connected.
320  * still need dev->lock to use epdata->ep.
321  */
322 static int
323 get_ready_ep (unsigned f_flags, struct ep_data *epdata)
324 {
325         int     val;
326
327         if (f_flags & O_NONBLOCK) {
328                 if (down_trylock (&epdata->lock) != 0)
329                         goto nonblock;
330                 if (epdata->state != STATE_EP_ENABLED) {
331                         up (&epdata->lock);
332 nonblock:
333                         val = -EAGAIN;
334                 } else
335                         val = 0;
336                 return val;
337         }
338
339         if ((val = down_interruptible (&epdata->lock)) < 0)
340                 return val;
341 newstate:
342         switch (epdata->state) {
343         case STATE_EP_ENABLED:
344                 break;
345         case STATE_EP_DEFER_ENABLE:
346                 DBG (epdata->dev, "%s wait for host\n", epdata->name);
347                 if ((val = wait_event_interruptible (epdata->wait, 
348                                 epdata->state != STATE_EP_DEFER_ENABLE
349                                 || epdata->dev->state == STATE_DEV_UNBOUND
350                                 )) < 0)
351                         goto fail;
352                 goto newstate;
353         // case STATE_EP_DISABLED:              /* "can't happen" */
354         // case STATE_EP_READY:                 /* "can't happen" */
355         default:                                /* error! */
356                 pr_debug ("%s: ep %p not available, state %d\n",
357                                 shortname, epdata, epdata->state);
358                 // FALLTHROUGH
359         case STATE_EP_UNBOUND:                  /* clean disconnect */
360                 val = -ENODEV;
361 fail:
362                 up (&epdata->lock);
363         }
364         return val;
365 }
366
367 static ssize_t
368 ep_io (struct ep_data *epdata, void *buf, unsigned len)
369 {
370         DECLARE_COMPLETION (done);
371         int value;
372
373         spin_lock_irq (&epdata->dev->lock);
374         if (likely (epdata->ep != NULL)) {
375                 struct usb_request      *req = epdata->req;
376
377                 req->context = &done;
378                 req->complete = epio_complete;
379                 req->buf = buf;
380                 req->length = len;
381                 value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
382         } else
383                 value = -ENODEV;
384         spin_unlock_irq (&epdata->dev->lock);
385
386         if (likely (value == 0)) {
387                 value = wait_event_interruptible (done.wait, done.done);
388                 if (value != 0) {
389                         spin_lock_irq (&epdata->dev->lock);
390                         if (likely (epdata->ep != NULL)) {
391                                 DBG (epdata->dev, "%s i/o interrupted\n",
392                                                 epdata->name);
393                                 usb_ep_dequeue (epdata->ep, epdata->req);
394                                 spin_unlock_irq (&epdata->dev->lock);
395
396                                 wait_event (done.wait, done.done);
397                                 if (epdata->status == -ECONNRESET)
398                                         epdata->status = -EINTR;
399                         } else {
400                                 spin_unlock_irq (&epdata->dev->lock);
401
402                                 DBG (epdata->dev, "endpoint gone\n");
403                                 epdata->status = -ENODEV;
404                         }
405                 }
406                 return epdata->status;
407         }
408         return value;
409 }
410
411
412 /* handle a synchronous OUT bulk/intr/iso transfer */
413 static ssize_t
414 ep_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
415 {
416         struct ep_data          *data = fd->private_data;
417         void                    *kbuf;
418         ssize_t                 value;
419
420         if ((value = get_ready_ep (fd->f_flags, data)) < 0)
421                 return value;
422
423         /* halt any endpoint by doing a "wrong direction" i/o call */
424         if (data->desc.bEndpointAddress & USB_DIR_IN) {
425                 if ((data->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
426                                 == USB_ENDPOINT_XFER_ISOC)
427                         return -EINVAL;
428                 DBG (data->dev, "%s halt\n", data->name);
429                 spin_lock_irq (&data->dev->lock);
430                 if (likely (data->ep != NULL))
431                         usb_ep_set_halt (data->ep);
432                 spin_unlock_irq (&data->dev->lock);
433                 up (&data->lock);
434                 return -EBADMSG;
435         }
436
437         /* FIXME readahead for O_NONBLOCK and poll(); careful with ZLPs */
438
439         value = -ENOMEM;
440         kbuf = kmalloc (len, SLAB_KERNEL);
441         if (unlikely (!kbuf))
442                 goto free1;
443
444         value = ep_io (data, kbuf, len);
445         VDEBUG (data->dev, "%s read %d OUT, status %d\n",
446                 data->name, len, value);
447         if (value >= 0 && copy_to_user (buf, kbuf, value))
448                 value = -EFAULT;
449
450 free1:
451         up (&data->lock);
452         kfree (kbuf);
453         return value;
454 }
455
456 /* handle a synchronous IN bulk/intr/iso transfer */
457 static ssize_t
458 ep_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
459 {
460         struct ep_data          *data = fd->private_data;
461         void                    *kbuf;
462         ssize_t                 value;
463
464         if ((value = get_ready_ep (fd->f_flags, data)) < 0)
465                 return value;
466
467         /* halt any endpoint by doing a "wrong direction" i/o call */
468         if (!(data->desc.bEndpointAddress & USB_DIR_IN)) {
469                 if ((data->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
470                                 == USB_ENDPOINT_XFER_ISOC)
471                         return -EINVAL;
472                 DBG (data->dev, "%s halt\n", data->name);
473                 spin_lock_irq (&data->dev->lock);
474                 if (likely (data->ep != NULL))
475                         usb_ep_set_halt (data->ep);
476                 spin_unlock_irq (&data->dev->lock);
477                 up (&data->lock);
478                 return -EBADMSG;
479         }
480
481         /* FIXME writebehind for O_NONBLOCK and poll(), qlen = 1 */
482
483         value = -ENOMEM;
484         kbuf = kmalloc (len, SLAB_KERNEL);
485         if (!kbuf)
486                 goto free1;
487         if (copy_from_user (kbuf, buf, len)) {
488                 value = -EFAULT;
489                 goto free1;
490         }
491
492         value = ep_io (data, kbuf, len);
493         VDEBUG (data->dev, "%s write %d IN, status %d\n",
494                 data->name, len, value);
495 free1:
496         up (&data->lock);
497         kfree (kbuf);
498         return value;
499 }
500
501 static int
502 ep_release (struct inode *inode, struct file *fd)
503 {
504         struct ep_data          *data = fd->private_data;
505
506         /* clean up if this can be reopened */
507         if (data->state != STATE_EP_UNBOUND) {
508                 data->state = STATE_EP_DISABLED;
509                 data->desc.bDescriptorType = 0;
510                 data->hs_desc.bDescriptorType = 0;
511         }
512         put_ep (data);
513         return 0;
514 }
515
516 static int ep_ioctl (struct inode *inode, struct file *fd,
517                 unsigned code, unsigned long value)
518 {
519         struct ep_data          *data = fd->private_data;
520         int                     status;
521
522         if ((status = get_ready_ep (fd->f_flags, data)) < 0)
523                 return status;
524
525         spin_lock_irq (&data->dev->lock);
526         if (likely (data->ep != NULL)) {
527                 switch (code) {
528                 case GADGETFS_FIFO_STATUS:
529                         status = usb_ep_fifo_status (data->ep);
530                         break;
531                 case GADGETFS_FIFO_FLUSH:
532                         usb_ep_fifo_flush (data->ep);
533                         break;
534                 case GADGETFS_CLEAR_HALT:
535                         status = usb_ep_clear_halt (data->ep);
536                         break;
537                 default:
538                         status = -ENOTTY;
539                 }
540         } else
541                 status = -ENODEV;
542         spin_unlock_irq (&data->dev->lock);
543         up (&data->lock);
544         return status;
545 }
546
547 /*----------------------------------------------------------------------*/
548
549 /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
550
551 struct kiocb_priv {
552         struct usb_request      *req;
553         struct ep_data          *epdata;
554         void                    *buf;
555         char __user             *ubuf;
556         unsigned                actual;
557 };
558
559 static int ep_aio_cancel(struct kiocb *iocb, struct io_event *e)
560 {
561         struct kiocb_priv       *priv = (void *) &iocb->private;
562         struct ep_data          *epdata;
563         int                     value;
564
565         local_irq_disable();
566         epdata = priv->epdata;
567         // spin_lock(&epdata->dev->lock);
568         kiocbSetCancelled(iocb);
569         if (likely(epdata && epdata->ep && priv->req))
570                 value = usb_ep_dequeue (epdata->ep, priv->req);
571         else
572                 value = -EINVAL;
573         // spin_unlock(&epdata->dev->lock);
574         local_irq_enable();
575
576         aio_put_req(iocb);
577         return value;
578 }
579
580 static long ep_aio_read_retry(struct kiocb *iocb)
581 {
582         struct kiocb_priv       *priv = (void *) &iocb->private;
583         int                     status = priv->actual;
584
585         /* we "retry" to get the right mm context for this: */
586         status = copy_to_user(priv->ubuf, priv->buf, priv->actual);
587         if (unlikely(0 != status))
588                 status = -EFAULT;
589         else
590                 status = priv->actual;
591         kfree(priv->buf);
592         aio_put_req(iocb);
593         return status;
594 }
595
596 static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
597 {
598         struct kiocb            *iocb = req->context;
599         struct kiocb_priv       *priv = (void *) &iocb->private;
600         struct ep_data          *epdata = priv->epdata;
601
602         /* lock against disconnect (and ideally, cancel) */
603         spin_lock(&epdata->dev->lock);
604         priv->req = NULL;
605         priv->epdata = NULL;
606         if (NULL == iocb->ki_retry
607                         || unlikely(0 == req->actual)
608                         || unlikely(kiocbIsCancelled(iocb))) {
609                 kfree(req->buf);
610                 /* aio_complete() reports bytes-transferred _and_ faults */
611                 if (unlikely(kiocbIsCancelled(iocb)))
612                         aio_put_req(iocb);
613                 else
614                         aio_complete(iocb,
615                                 req->actual ? req->actual : req->status,
616                                 req->status);
617         } else {
618                 /* retry() won't report both; so we hide some faults */
619                 if (unlikely(0 != req->status))
620                         DBG(epdata->dev, "%s fault %d len %d\n",
621                                 ep->name, req->status, req->actual);
622
623                 priv->buf = req->buf;
624                 priv->actual = req->actual;
625                 kick_iocb(iocb);
626         }
627         spin_unlock(&epdata->dev->lock);
628
629         usb_ep_free_request(ep, req);
630         put_ep(epdata);
631 }
632
633 static ssize_t
634 ep_aio_rwtail(struct kiocb *iocb, char *buf, size_t len, struct ep_data *epdata)
635 {
636         struct kiocb_priv       *priv = (void *) &iocb->private;
637         struct usb_request      *req;
638         ssize_t                 value;
639
640         value = get_ready_ep(iocb->ki_filp->f_flags, epdata);
641         if (unlikely(value < 0)) {
642                 kfree(buf);
643                 return value;
644         }
645
646         iocb->ki_cancel = ep_aio_cancel;
647         get_ep(epdata);
648         priv->epdata = epdata;
649         priv->actual = 0;
650
651         /* each kiocb is coupled to one usb_request, but we can't
652          * allocate or submit those if the host disconnected.
653          */
654         spin_lock_irq(&epdata->dev->lock);
655         if (likely(epdata->ep)) {
656                 req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
657                 if (likely(req)) {
658                         priv->req = req;
659                         req->buf = buf;
660                         req->length = len;
661                         req->complete = ep_aio_complete;
662                         req->context = iocb;
663                         value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
664                         if (unlikely(0 != value))
665                                 usb_ep_free_request(epdata->ep, req);
666                 } else
667                         value = -EAGAIN;
668         } else
669                 value = -ENODEV;
670         spin_unlock_irq(&epdata->dev->lock);
671
672         up(&epdata->lock);
673
674         if (unlikely(value))
675                 put_ep(epdata);
676         else
677                 value = -EIOCBQUEUED;
678         return value;
679 }
680
681 static ssize_t
682 ep_aio_read(struct kiocb *iocb, char __user *ubuf, size_t len, loff_t o)
683 {
684         struct kiocb_priv       *priv = (void *) &iocb->private;
685         struct ep_data          *epdata = iocb->ki_filp->private_data;
686         char                    *buf;
687
688         if (unlikely(epdata->desc.bEndpointAddress & USB_DIR_IN))
689                 return -EINVAL;
690         buf = kmalloc(len, GFP_KERNEL);
691         if (unlikely(!buf))
692                 return -ENOMEM;
693         iocb->ki_retry = ep_aio_read_retry;
694         priv->ubuf = ubuf;
695         return ep_aio_rwtail(iocb, buf, len, epdata);
696 }
697
698 static ssize_t
699 ep_aio_write(struct kiocb *iocb, const char __user *ubuf, size_t len, loff_t o)
700 {
701         struct ep_data          *epdata = iocb->ki_filp->private_data;
702         char                    *buf;
703
704         if (unlikely(!(epdata->desc.bEndpointAddress & USB_DIR_IN)))
705                 return -EINVAL;
706         buf = kmalloc(len, GFP_KERNEL);
707         if (unlikely(!buf))
708                 return -ENOMEM;
709         if (unlikely(copy_from_user(buf, ubuf, len) != 0)) {
710                 kfree(buf);
711                 return -EFAULT;
712         }
713         return ep_aio_rwtail(iocb, buf, len, epdata);
714 }
715
716 /*----------------------------------------------------------------------*/
717
718 /* used after endpoint configuration */
719 static struct file_operations ep_io_operations = {
720         .owner =        THIS_MODULE,
721         .read =         ep_read,
722         .write =        ep_write,
723         .ioctl =        ep_ioctl,
724         .release =      ep_release,
725
726         .aio_read =     ep_aio_read,
727         .aio_write =    ep_aio_write,
728 };
729
730 /* ENDPOINT INITIALIZATION
731  *
732  *     fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
733  *     status = write (fd, descriptors, sizeof descriptors)
734  *
735  * That write establishes the endpoint configuration, configuring
736  * the controller to process bulk, interrupt, or isochronous transfers
737  * at the right maxpacket size, and so on.
738  *
739  * The descriptors are message type 1, identified by a host order u32
740  * at the beginning of what's written.  Descriptor order is: full/low
741  * speed descriptor, then optional high speed descriptor.
742  */
743 static ssize_t
744 ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
745 {
746         struct ep_data          *data = fd->private_data;
747         struct usb_ep           *ep;
748         u32                     tag;
749         int                     value;
750
751         if ((value = down_interruptible (&data->lock)) < 0)
752                 return value;
753
754         if (data->state != STATE_EP_READY) {
755                 value = -EL2HLT;
756                 goto fail;
757         }
758
759         value = len;
760         if (len < USB_DT_ENDPOINT_SIZE + 4)
761                 goto fail0;
762
763         /* we might need to change message format someday */
764         if (copy_from_user (&tag, buf, 4)) {
765                 goto fail1;
766         }
767         if (tag != 1) {
768                 DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
769                 goto fail0;
770         }
771         buf += 4;
772         len -= 4;
773
774         /* NOTE:  audio endpoint extensions not accepted here;
775          * just don't include the extra bytes.
776          */
777
778         /* full/low speed descriptor, then high speed */
779         if (copy_from_user (&data->desc, buf, USB_DT_ENDPOINT_SIZE)) {
780                 goto fail1;
781         }
782         if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
783                         || data->desc.bDescriptorType != USB_DT_ENDPOINT)
784                 goto fail0;
785         if (len != USB_DT_ENDPOINT_SIZE) {
786                 if (len != 2 * USB_DT_ENDPOINT_SIZE)
787                         goto fail0;
788                 if (copy_from_user (&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
789                                         USB_DT_ENDPOINT_SIZE)) {
790                         goto fail1;
791                 }
792                 if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
793                                 || data->hs_desc.bDescriptorType
794                                         != USB_DT_ENDPOINT) {
795                         DBG(data->dev, "config %s, bad hs length or type\n",
796                                         data->name);
797                         goto fail0;
798                 }
799         }
800         value = len;
801
802         spin_lock_irq (&data->dev->lock);
803         if (data->dev->state == STATE_DEV_UNBOUND) {
804                 value = -ENOENT;
805                 goto gone;
806         } else if ((ep = data->ep) == NULL) {
807                 value = -ENODEV;
808                 goto gone;
809         }
810         switch (data->dev->gadget->speed) {
811         case USB_SPEED_LOW:
812         case USB_SPEED_FULL:
813                 value = usb_ep_enable (ep, &data->desc);
814                 if (value == 0)
815                         data->state = STATE_EP_ENABLED;
816                 break;
817 #ifdef  HIGHSPEED
818         case USB_SPEED_HIGH:
819                 /* fails if caller didn't provide that descriptor... */
820                 value = usb_ep_enable (ep, &data->hs_desc);
821                 if (value == 0)
822                         data->state = STATE_EP_ENABLED;
823                 break;
824 #endif
825         default:
826                 DBG (data->dev, "unconnected, %s init deferred\n",
827                                 data->name);
828                 data->state = STATE_EP_DEFER_ENABLE;
829         }
830         if (value == 0)
831                 fd->f_op = &ep_io_operations;
832 gone:
833         spin_unlock_irq (&data->dev->lock);
834         if (value < 0) {
835 fail:
836                 data->desc.bDescriptorType = 0;
837                 data->hs_desc.bDescriptorType = 0;
838         }
839         up (&data->lock);
840         return value;
841 fail0:
842         value = -EINVAL;
843         goto fail;
844 fail1:
845         value = -EFAULT;
846         goto fail;
847 }
848
849 static int
850 ep_open (struct inode *inode, struct file *fd)
851 {
852         struct ep_data          *data = inode->u.generic_ip;
853         int                     value = -EBUSY;
854
855         if (down_interruptible (&data->lock) != 0)
856                 return -EINTR;
857         spin_lock_irq (&data->dev->lock);
858         if (data->dev->state == STATE_DEV_UNBOUND)
859                 value = -ENOENT;
860         else if (data->state == STATE_EP_DISABLED) {
861                 value = 0;
862                 data->state = STATE_EP_READY;
863                 get_ep (data);
864                 fd->private_data = data;
865                 VDEBUG (data->dev, "%s ready\n", data->name);
866         } else
867                 DBG (data->dev, "%s state %d\n",
868                         data->name, data->state);
869         spin_unlock_irq (&data->dev->lock);
870         up (&data->lock);
871         return value;
872 }
873
874 /* used before endpoint configuration */
875 static struct file_operations ep_config_operations = {
876         .owner =        THIS_MODULE,
877         .open =         ep_open,
878         .write =        ep_config,
879         .release =      ep_release,
880 };
881
882 /*----------------------------------------------------------------------*/
883
884 /* EP0 IMPLEMENTATION can be partly in userspace.
885  *
886  * Drivers that use this facility receive various events, including
887  * control requests the kernel doesn't handle.  Drivers that don't
888  * use this facility may be too simple-minded for real applications.
889  */
890
891 static inline void ep0_readable (struct dev_data *dev)
892 {
893         wake_up (&dev->wait);
894         kill_fasync (&dev->fasync, SIGIO, POLL_IN);
895 }
896
897 static void clean_req (struct usb_ep *ep, struct usb_request *req)
898 {
899         struct dev_data         *dev = ep->driver_data;
900
901         if (req->buf != dev->rbuf) {
902                 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
903                 req->buf = dev->rbuf;
904                 req->dma = DMA_ADDR_INVALID;
905         }
906         req->complete = epio_complete;
907         dev->setup_out_ready = 0;
908 }
909
910 static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
911 {
912         struct dev_data         *dev = ep->driver_data;
913         int                     free = 1;
914
915         /* for control OUT, data must still get to userspace */
916         if (!dev->setup_in) {
917                 dev->setup_out_error = (req->status != 0);
918                 if (!dev->setup_out_error)
919                         free = 0;
920                 dev->setup_out_ready = 1;
921                 ep0_readable (dev);
922         } else if (dev->state == STATE_SETUP)
923                 dev->state = STATE_CONNECTED;
924
925         /* clean up as appropriate */
926         if (free && req->buf != &dev->rbuf)
927                 clean_req (ep, req);
928         req->complete = epio_complete;
929 }
930
931 static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
932 {
933         struct dev_data *dev = ep->driver_data;
934
935         if (dev->setup_out_ready) {
936                 DBG (dev, "ep0 request busy!\n");
937                 return -EBUSY;
938         }
939         if (len > sizeof (dev->rbuf))
940                 req->buf = usb_ep_alloc_buffer (ep, len, &req->dma, GFP_ATOMIC);
941         if (req->buf == 0) {
942                 req->buf = dev->rbuf;
943                 return -ENOMEM;
944         }
945         req->complete = ep0_complete;
946         req->length = len;
947         return 0;
948 }
949
950 static ssize_t
951 ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
952 {
953         struct dev_data                 *dev = fd->private_data;
954         ssize_t                         retval;
955         enum ep0_state                  state;
956
957         spin_lock_irq (&dev->lock);
958
959         /* report fd mode change before acting on it */
960         if (dev->setup_abort) {
961                 dev->setup_abort = 0;
962                 retval = -EIDRM;
963                 goto done;
964         }
965
966         /* control DATA stage */
967         if ((state = dev->state) == STATE_SETUP) {
968
969                 if (dev->setup_in) {            /* stall IN */
970                         VDEBUG(dev, "ep0in stall\n");
971                         (void) usb_ep_set_halt (dev->gadget->ep0);
972                         retval = -EL2HLT;
973                         dev->state = STATE_CONNECTED;
974
975                 } else if (len == 0) {          /* ack SET_CONFIGURATION etc */
976                         struct usb_ep           *ep = dev->gadget->ep0;
977                         struct usb_request      *req = dev->req;
978
979                         if ((retval = setup_req (ep, req, 0)) == 0)
980                                 retval = usb_ep_queue (ep, req, GFP_ATOMIC);
981                         dev->state = STATE_CONNECTED;
982
983                 } else {                        /* collect OUT data */
984                         if ((fd->f_flags & O_NONBLOCK) != 0
985                                         && !dev->setup_out_ready) {
986                                 retval = -EAGAIN;
987                                 goto done;
988                         }
989                         spin_unlock_irq (&dev->lock);
990                         retval = wait_event_interruptible (dev->wait,
991                                         dev->setup_out_ready != 0);
992
993                         /* FIXME state could change from under us */
994                         spin_lock_irq (&dev->lock);
995                         if (retval)
996                                 goto done;
997                         if (dev->setup_out_error)
998                                 retval = -EIO;
999                         else {
1000                                 len = min (len, (size_t)dev->req->actual);
1001 // FIXME don't call this with the spinlock held ...
1002                                 if (copy_to_user (buf, &dev->req->buf, len))
1003                                         retval = -EFAULT;
1004                                 clean_req (dev->gadget->ep0, dev->req);
1005                                 /* NOTE userspace can't yet choose to stall */
1006                         }
1007                 }
1008                 goto done;
1009         }
1010
1011         /* else normal: return event data */
1012         if (len < sizeof dev->event [0]) {
1013                 retval = -EINVAL;
1014                 goto done;
1015         }
1016         len -= len % sizeof (struct usb_gadgetfs_event);
1017         dev->usermode_setup = 1;
1018
1019 scan:
1020         /* return queued events right away */
1021         if (dev->ev_next != 0) {
1022                 unsigned                i, n;
1023                 int                     tmp = dev->ev_next;
1024
1025                 len = min (len, tmp * sizeof (struct usb_gadgetfs_event));
1026                 n = len / sizeof (struct usb_gadgetfs_event);
1027
1028                 /* ep0 can't deliver events when STATE_SETUP */
1029                 for (i = 0; i < n; i++) {
1030                         if (dev->event [i].type == GADGETFS_SETUP) {
1031                                 len = n = i + 1;
1032                                 len *= sizeof (struct usb_gadgetfs_event);
1033                                 n = 0;
1034                                 break;
1035                         }
1036                 }
1037                 spin_unlock_irq (&dev->lock);
1038                 if (copy_to_user (buf, &dev->event, len))
1039                         retval = -EFAULT;
1040                 else
1041                         retval = len;
1042                 if (len > 0) {
1043                         len /= sizeof (struct usb_gadgetfs_event);
1044
1045                         /* NOTE this doesn't guard against broken drivers;
1046                          * concurrent ep0 readers may lose events.
1047                          */
1048                         spin_lock_irq (&dev->lock);
1049                         dev->ev_next -= len;
1050                         if (dev->ev_next != 0)
1051                                 memmove (&dev->event, &dev->event [len],
1052                                         sizeof (struct usb_gadgetfs_event)
1053                                                 * (tmp - len));
1054                         if (n == 0)
1055                                 dev->state = STATE_SETUP;
1056                         spin_unlock_irq (&dev->lock);
1057                 }
1058                 return retval;
1059         }
1060         if (fd->f_flags & O_NONBLOCK) {
1061                 retval = -EAGAIN;
1062                 goto done;
1063         }
1064
1065         switch (state) {
1066         default:
1067                 DBG (dev, "fail %s, state %d\n", __FUNCTION__, state);
1068                 retval = -ESRCH;
1069                 break;
1070         case STATE_UNCONNECTED:
1071         case STATE_CONNECTED:
1072                 spin_unlock_irq (&dev->lock);
1073                 DBG (dev, "%s wait\n", __FUNCTION__);
1074
1075                 /* wait for events */
1076                 retval = wait_event_interruptible (dev->wait,
1077                                 dev->ev_next != 0);
1078                 if (retval < 0)
1079                         return retval;
1080                 spin_lock_irq (&dev->lock);
1081                 goto scan;
1082         }
1083
1084 done:
1085         spin_unlock_irq (&dev->lock);
1086         return retval;
1087 }
1088
1089 static struct usb_gadgetfs_event *
1090 next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1091 {
1092         struct usb_gadgetfs_event       *event;
1093         unsigned                        i;
1094
1095         switch (type) {
1096         /* these events purge the queue */
1097         case GADGETFS_DISCONNECT:
1098                 if (dev->state == STATE_SETUP)
1099                         dev->setup_abort = 1;
1100                 // FALL THROUGH
1101         case GADGETFS_CONNECT:
1102                 dev->ev_next = 0;
1103                 break;
1104         case GADGETFS_SETUP:            /* previous request timed out */
1105         case GADGETFS_SUSPEND:          /* same effect */
1106                 /* these events can't be repeated */
1107                 for (i = 0; i != dev->ev_next; i++) {
1108                         if (dev->event [i].type != type)
1109                                 continue;
1110                         DBG (dev, "discard old event %d\n", type);
1111                         dev->ev_next--;
1112                         if (i == dev->ev_next)
1113                                 break;
1114                         /* indices start at zero, for simplicity */
1115                         memmove (&dev->event [i], &dev->event [i + 1],
1116                                 sizeof (struct usb_gadgetfs_event)
1117                                         * (dev->ev_next - i));
1118                 }
1119                 break;
1120         default:
1121                 BUG ();
1122         }
1123         event = &dev->event [dev->ev_next++];
1124         BUG_ON (dev->ev_next > N_EVENT);
1125         VDEBUG (dev, "ev %d, next %d\n", type, dev->ev_next);
1126         memset (event, 0, sizeof *event);
1127         event->type = type;
1128         return event;
1129 }
1130
1131 static ssize_t
1132 ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1133 {
1134         struct dev_data         *dev = fd->private_data;
1135         ssize_t                 retval = -ESRCH;
1136
1137         spin_lock_irq (&dev->lock);
1138
1139         /* report fd mode change before acting on it */
1140         if (dev->setup_abort) {
1141                 dev->setup_abort = 0;
1142                 retval = -EIDRM;
1143
1144         /* data and/or status stage for control request */
1145         } else if (dev->state == STATE_SETUP) {
1146
1147                 /* IN DATA+STATUS caller makes len <= wLength */
1148                 if (dev->setup_in) {
1149                         retval = setup_req (dev->gadget->ep0, dev->req, len);
1150                         if (retval == 0) {
1151                                 spin_unlock_irq (&dev->lock);
1152                                 if (copy_from_user (dev->req->buf, buf, len))
1153                                         retval = -EFAULT;
1154                                 else
1155                                         retval = usb_ep_queue (
1156                                                 dev->gadget->ep0, dev->req,
1157                                                 GFP_KERNEL);
1158                                 if (retval < 0) {
1159                                         spin_lock_irq (&dev->lock);
1160                                         clean_req (dev->gadget->ep0, dev->req);
1161                                         spin_unlock_irq (&dev->lock);
1162                                 } else
1163                                         retval = len;
1164
1165                                 return retval;
1166                         }
1167
1168                 /* can stall some OUT transfers */
1169                 } else if (dev->setup_can_stall) {
1170                         VDEBUG(dev, "ep0out stall\n");
1171                         (void) usb_ep_set_halt (dev->gadget->ep0);
1172                         retval = -EL2HLT;
1173                         dev->state = STATE_CONNECTED;
1174                 } else {
1175                         DBG(dev, "bogus ep0out stall!\n");
1176                 }
1177         } else
1178                 DBG (dev, "fail %s, state %d\n", __FUNCTION__, dev->state);
1179
1180         spin_unlock_irq (&dev->lock);
1181         return retval;
1182 }
1183
1184 static int
1185 ep0_fasync (int f, struct file *fd, int on)
1186 {
1187         struct dev_data         *dev = fd->private_data;
1188         // caller must F_SETOWN before signal delivery happens
1189         VDEBUG (dev, "%s %s\n", __FUNCTION__, on ? "on" : "off");
1190         return fasync_helper (f, fd, on, &dev->fasync);
1191 }
1192
1193 static struct usb_gadget_driver gadgetfs_driver;
1194
1195 static int
1196 dev_release (struct inode *inode, struct file *fd)
1197 {
1198         struct dev_data         *dev = fd->private_data;
1199
1200         /* closing ep0 === shutdown all */
1201
1202         usb_gadget_unregister_driver (&gadgetfs_driver);
1203
1204         /* at this point "good" hardware has disconnected the
1205          * device from USB; the host won't see it any more.
1206          * alternatively, all host requests will time out.
1207          */
1208
1209         fasync_helper (-1, fd, 0, &dev->fasync);
1210         kfree (dev->buf);
1211         dev->buf = NULL;
1212         put_dev (dev);
1213
1214         /* other endpoints were all decoupled from this device */
1215         dev->state = STATE_DEV_DISABLED;
1216         return 0;
1217 }
1218
1219 static int dev_ioctl (struct inode *inode, struct file *fd,
1220                 unsigned code, unsigned long value)
1221 {
1222         struct dev_data         *dev = fd->private_data;
1223         struct usb_gadget       *gadget = dev->gadget;
1224
1225         if (gadget->ops->ioctl)
1226                 return gadget->ops->ioctl (gadget, code, value);
1227         return -ENOTTY;
1228 }
1229
1230 /* used after device configuration */
1231 static struct file_operations ep0_io_operations = {
1232         .owner =        THIS_MODULE,
1233         .read =         ep0_read,
1234         .write =        ep0_write,
1235         .fasync =       ep0_fasync,
1236         // .poll =      ep0_poll,
1237         .ioctl =        dev_ioctl,
1238         .release =      dev_release,
1239 };
1240
1241 /*----------------------------------------------------------------------*/
1242
1243 /* The in-kernel gadget driver handles most ep0 issues, in particular
1244  * enumerating the single configuration (as provided from user space).
1245  *
1246  * Unrecognized ep0 requests may be handled in user space.
1247  */
1248
1249 #ifdef  HIGHSPEED
1250 static void make_qualifier (struct dev_data *dev)
1251 {
1252         struct usb_qualifier_descriptor         qual;
1253         struct usb_device_descriptor            *desc;
1254
1255         qual.bLength = sizeof qual;
1256         qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
1257         qual.bcdUSB = __constant_cpu_to_le16 (0x0200);
1258
1259         desc = dev->dev;
1260         qual.bDeviceClass = desc->bDeviceClass;
1261         qual.bDeviceSubClass = desc->bDeviceSubClass;
1262         qual.bDeviceProtocol = desc->bDeviceProtocol;
1263
1264         /* assumes ep0 uses the same value for both speeds ... */
1265         qual.bMaxPacketSize0 = desc->bMaxPacketSize0;
1266
1267         qual.bNumConfigurations = 1;
1268         qual.bRESERVED = 0;
1269
1270         memcpy (dev->rbuf, &qual, sizeof qual);
1271 }
1272 #endif
1273
1274 static int
1275 config_buf (struct dev_data *dev, u8 type, unsigned index)
1276 {
1277         int             len;
1278 #ifdef HIGHSPEED
1279         int             hs;
1280 #endif
1281
1282         /* only one configuration */
1283         if (index > 0)
1284                 return -EINVAL;
1285
1286 #ifdef HIGHSPEED
1287         hs = (dev->gadget->speed == USB_SPEED_HIGH);
1288         if (type == USB_DT_OTHER_SPEED_CONFIG)
1289                 hs = !hs;
1290         if (hs) {
1291                 dev->req->buf = dev->hs_config;
1292                 len = le16_to_cpup (&dev->hs_config->wTotalLength);
1293         } else
1294 #endif
1295         {
1296                 dev->req->buf = dev->config;
1297                 len = le16_to_cpup (&dev->config->wTotalLength);
1298         }
1299         ((u8 *)dev->req->buf) [1] = type;
1300         return len;
1301 }
1302
1303 static int
1304 gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1305 {
1306         struct dev_data                 *dev = get_gadget_data (gadget);
1307         struct usb_request              *req = dev->req;
1308         int                             value = -EOPNOTSUPP;
1309         struct usb_gadgetfs_event       *event;
1310
1311         spin_lock (&dev->lock);
1312         dev->setup_abort = 0;
1313         if (dev->state == STATE_UNCONNECTED) {
1314                 struct usb_ep   *ep;
1315                 struct ep_data  *data;
1316
1317                 dev->state = STATE_CONNECTED;
1318                 dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
1319
1320 #ifdef  HIGHSPEED
1321                 if (gadget->speed == USB_SPEED_HIGH && dev->hs_config == 0) {
1322                         ERROR (dev, "no high speed config??\n");
1323                         return -EINVAL;
1324                 }
1325 #endif  /* HIGHSPEED */
1326
1327                 INFO (dev, "connected\n");
1328                 event = next_event (dev, GADGETFS_CONNECT);
1329                 event->u.speed = gadget->speed;
1330                 ep0_readable (dev);
1331
1332                 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
1333                         data = ep->driver_data;
1334                         /* ... down_trylock (&data->lock) ... */
1335                         if (data->state != STATE_EP_DEFER_ENABLE)
1336                                 continue;
1337 #ifdef  HIGHSPEED
1338                         if (gadget->speed == USB_SPEED_HIGH)
1339                                 value = usb_ep_enable (ep, &data->hs_desc);
1340                         else
1341 #endif  /* HIGHSPEED */
1342                                 value = usb_ep_enable (ep, &data->desc);
1343                         if (value) {
1344                                 ERROR (dev, "deferred %s enable --> %d\n",
1345                                         data->name, value);
1346                                 continue;
1347                         }
1348                         data->state = STATE_EP_ENABLED;
1349                         wake_up (&data->wait);
1350                         DBG (dev, "woke up %s waiters\n", data->name);
1351                 }
1352
1353         /* host may have given up waiting for response.  we can miss control
1354          * requests handled lower down (device/endpoint status and features);
1355          * then ep0_{read,write} will report the wrong status. controller
1356          * driver will have aborted pending i/o.
1357          */
1358         } else if (dev->state == STATE_SETUP)
1359                 dev->setup_abort = 1;
1360
1361         req->buf = dev->rbuf;
1362         req->dma = DMA_ADDR_INVALID;
1363         req->context = NULL;
1364         value = -EOPNOTSUPP;
1365         switch (ctrl->bRequest) {
1366
1367         case USB_REQ_GET_DESCRIPTOR:
1368                 if (ctrl->bRequestType != USB_DIR_IN)
1369                         goto unrecognized;
1370                 switch (ctrl->wValue >> 8) {
1371
1372                 case USB_DT_DEVICE:
1373                         value = min (ctrl->wLength, (u16) sizeof *dev->dev);
1374                         req->buf = dev->dev;
1375                         break;
1376 #ifdef  HIGHSPEED
1377                 case USB_DT_DEVICE_QUALIFIER:
1378                         if (!dev->hs_config)
1379                                 break;
1380                         value = min (ctrl->wLength, (u16)
1381                                 sizeof (struct usb_qualifier_descriptor));
1382                         make_qualifier (dev);
1383                         break;
1384                 case USB_DT_OTHER_SPEED_CONFIG:
1385                         // FALLTHROUGH
1386 #endif
1387                 case USB_DT_CONFIG:
1388                         value = config_buf (dev,
1389                                         ctrl->wValue >> 8,
1390                                         ctrl->wValue & 0xff);
1391                         if (value >= 0)
1392                                 value = min (ctrl->wLength, (u16) value);
1393                         break;
1394                 case USB_DT_STRING:
1395                         goto unrecognized;
1396
1397                 default:                // all others are errors
1398                         break;
1399                 }
1400                 break;
1401
1402         /* currently one config, two speeds */
1403         case USB_REQ_SET_CONFIGURATION:
1404                 if (ctrl->bRequestType != 0)
1405                         break;
1406                 if (0 == (u8) ctrl->wValue) {
1407                         value = 0;
1408                         dev->current_config = 0;
1409                         // user mode expected to disable endpoints
1410                 } else {
1411                         u8      config;
1412 #ifdef  HIGHSPEED
1413                         if (gadget->speed == USB_SPEED_HIGH)
1414                                 config = dev->hs_config->bConfigurationValue;
1415                         else
1416 #endif
1417                                 config = dev->config->bConfigurationValue;
1418
1419                         if (config == (u8) ctrl->wValue) {
1420                                 value = 0;
1421                                 dev->current_config = config;
1422                         }
1423                 }
1424
1425                 /* report SET_CONFIGURATION like any other control request,
1426                  * except that usermode may not stall this.  the next
1427                  * request mustn't be allowed start until this finishes:
1428                  * endpoints and threads set up, etc.
1429                  *
1430                  * NOTE:  older PXA hardware (before PXA 255: without UDCCFR)
1431                  * has bad/racey automagic that prevents synchronizing here.
1432                  * even kernel mode drivers often miss them.
1433                  */
1434                 if (value == 0) {
1435                         INFO (dev, "configuration #%d\n", dev->current_config);
1436                         if (dev->usermode_setup) {
1437                                 dev->setup_can_stall = 0;
1438                                 goto delegate;
1439                         }
1440                 }
1441                 break;
1442
1443 #ifndef CONFIG_USB_GADGETFS_PXA2XX
1444         /* PXA automagically handles this request too */
1445         case USB_REQ_GET_CONFIGURATION:
1446                 if (ctrl->bRequestType != 0x80)
1447                         break;
1448                 *(u8 *)req->buf = dev->current_config;
1449                 value = min (ctrl->wLength, (u16) 1);
1450                 break;
1451 #endif
1452
1453         default:
1454 unrecognized:
1455                 VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
1456                         dev->usermode_setup ? "delegate" : "fail",
1457                         ctrl->bRequestType, ctrl->bRequest,
1458                         ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1459
1460                 /* if there's an ep0 reader, don't stall */
1461                 if (dev->usermode_setup) {
1462                         dev->setup_can_stall = 1;
1463 delegate:
1464                         dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
1465                                                 ? 1 : 0;
1466                         dev->setup_out_ready = 0;
1467                         dev->setup_out_error = 0;
1468                         value = 0;
1469
1470                         /* read DATA stage for OUT right away */
1471                         if (unlikely (!dev->setup_in && ctrl->wLength)) {
1472                                 value = setup_req (gadget->ep0, dev->req,
1473                                                         ctrl->wLength);
1474                                 if (value < 0)
1475                                         break;
1476                                 value = usb_ep_queue (gadget->ep0, dev->req,
1477                                                         GFP_ATOMIC);
1478                                 if (value < 0) {
1479                                         clean_req (gadget->ep0, dev->req);
1480                                         break;
1481                                 }
1482
1483                                 /* we can't currently stall these */
1484                                 dev->setup_can_stall = 0;
1485                         }
1486
1487                         /* state changes when reader collects event */
1488                         event = next_event (dev, GADGETFS_SETUP);
1489                         event->u.setup = *ctrl;
1490                         ep0_readable (dev);
1491                         spin_unlock (&dev->lock);
1492                         return 0;
1493                 }
1494         }
1495
1496         /* proceed with data transfer and status phases? */
1497         if (value >= 0 && dev->state != STATE_SETUP) {
1498                 req->length = value;
1499                 req->zero = value < ctrl->wLength
1500                                 && (value % gadget->ep0->maxpacket) == 0;
1501                 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1502                 if (value < 0) {
1503                         DBG (dev, "ep_queue --> %d\n", value);
1504                         req->status = 0;
1505                 }
1506         }
1507
1508         /* device stalls when value < 0 */
1509         spin_unlock (&dev->lock);
1510         return value;
1511 }
1512
1513 static void destroy_ep_files (struct dev_data *dev)
1514 {
1515         struct list_head        *entry, *tmp;
1516
1517         DBG (dev, "%s %d\n", __FUNCTION__, dev->state);
1518
1519         /* dev->state must prevent interference */
1520 restart:
1521         spin_lock_irq (&dev->lock);
1522         list_for_each_safe (entry, tmp, &dev->epfiles) {
1523                 struct ep_data  *ep;
1524                 struct inode    *parent;
1525                 struct dentry   *dentry;
1526
1527                 /* break link to FS */
1528                 ep = list_entry (entry, struct ep_data, epfiles);
1529                 list_del_init (&ep->epfiles);
1530                 dentry = ep->dentry;
1531                 ep->dentry = NULL;
1532                 parent = dentry->d_parent->d_inode;
1533
1534                 /* break link to controller */
1535                 if (ep->state == STATE_EP_ENABLED)
1536                         (void) usb_ep_disable (ep->ep);
1537                 ep->state = STATE_EP_UNBOUND;
1538                 usb_ep_free_request (ep->ep, ep->req);
1539                 ep->ep = NULL;
1540                 wake_up (&ep->wait);
1541                 put_ep (ep);
1542
1543                 spin_unlock_irq (&dev->lock);
1544
1545                 /* break link to dcache */
1546                 down (&parent->i_sem);
1547                 d_delete (dentry);
1548                 dput (dentry);
1549                 up (&parent->i_sem);
1550
1551                 /* fds may still be open */
1552                 goto restart;
1553         }
1554         spin_unlock_irq (&dev->lock);
1555 }
1556
1557
1558 static struct inode *
1559 gadgetfs_create_file (struct super_block *sb, char const *name,
1560                 void *data, struct file_operations *fops,
1561                 struct dentry **dentry_p);
1562
1563 static int activate_ep_files (struct dev_data *dev)
1564 {
1565         struct usb_ep   *ep;
1566
1567         gadget_for_each_ep (ep, dev->gadget) {
1568                 struct ep_data  *data;
1569
1570                 data = kmalloc (sizeof *data, GFP_KERNEL);
1571                 if (!data)
1572                         goto enomem;
1573                 memset (data, 0, sizeof data);
1574                 data->state = STATE_EP_DISABLED;
1575                 init_MUTEX (&data->lock);
1576                 init_waitqueue_head (&data->wait);
1577
1578                 strncpy (data->name, ep->name, sizeof (data->name) - 1);
1579                 atomic_set (&data->count, 1);
1580                 data->dev = dev;
1581                 get_dev (dev);
1582
1583                 data->ep = ep;
1584                 ep->driver_data = data;
1585
1586                 data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
1587                 if (!data->req)
1588                         goto enomem;
1589
1590                 data->inode = gadgetfs_create_file (dev->sb, data->name,
1591                                 data, &ep_config_operations,
1592                                 &data->dentry);
1593                 if (!data->inode) {
1594                         kfree (data);
1595                         goto enomem;
1596                 }
1597                 list_add_tail (&data->epfiles, &dev->epfiles);
1598         }
1599         return 0;
1600
1601 enomem:
1602         DBG (dev, "%s enomem\n", __FUNCTION__);
1603         destroy_ep_files (dev);
1604         return -ENOMEM;
1605 }
1606
1607 static void
1608 gadgetfs_unbind (struct usb_gadget *gadget)
1609 {
1610         struct dev_data         *dev = get_gadget_data (gadget);
1611
1612         DBG (dev, "%s\n", __FUNCTION__);
1613
1614         spin_lock_irq (&dev->lock);
1615         dev->state = STATE_DEV_UNBOUND;
1616         spin_unlock_irq (&dev->lock);
1617
1618         destroy_ep_files (dev);
1619         gadget->ep0->driver_data = NULL;
1620         set_gadget_data (gadget, NULL);
1621
1622         /* we've already been disconnected ... no i/o is active */
1623         if (dev->req)
1624                 usb_ep_free_request (gadget->ep0, dev->req);
1625         DBG (dev, "%s done\n", __FUNCTION__);
1626         put_dev (dev);
1627 }
1628
1629 static struct dev_data          *the_device;
1630
1631 static int
1632 gadgetfs_bind (struct usb_gadget *gadget)
1633 {
1634         struct dev_data         *dev = the_device;
1635
1636         if (!dev)
1637                 return -ESRCH;
1638         if (0 != strcmp (CHIP, gadget->name)) {
1639                 printk (KERN_ERR "%s expected " CHIP " controller not %s\n",
1640                         shortname, gadget->name);
1641                 return -ENODEV;
1642         }
1643
1644         set_gadget_data (gadget, dev);
1645         dev->gadget = gadget;
1646         gadget->ep0->driver_data = dev;
1647         dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
1648
1649         /* preallocate control response and buffer */
1650         dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1651         if (!dev->req)
1652                 goto enomem;
1653         dev->req->context = NULL;
1654         dev->req->complete = epio_complete;
1655
1656         if (activate_ep_files (dev) < 0)
1657                 goto enomem;
1658
1659         INFO (dev, "bound to %s driver\n", gadget->name);
1660         dev->state = STATE_UNCONNECTED;
1661         get_dev (dev);
1662         return 0;
1663
1664 enomem:
1665         gadgetfs_unbind (gadget);
1666         return -ENOMEM;
1667 }
1668
1669 static void
1670 gadgetfs_disconnect (struct usb_gadget *gadget)
1671 {
1672         struct dev_data         *dev = get_gadget_data (gadget);
1673
1674         if (dev->state == STATE_UNCONNECTED) {
1675                 DBG (dev, "already unconnected\n");
1676                 return;
1677         }
1678         dev->state = STATE_UNCONNECTED;
1679
1680         INFO (dev, "disconnected\n");
1681         spin_lock (&dev->lock);
1682         next_event (dev, GADGETFS_DISCONNECT);
1683         ep0_readable (dev);
1684         spin_unlock (&dev->lock);
1685 }
1686
1687 static void
1688 gadgetfs_suspend (struct usb_gadget *gadget)
1689 {
1690         struct dev_data         *dev = get_gadget_data (gadget);
1691
1692         INFO (dev, "suspended from state %d\n", dev->state);
1693         spin_lock (&dev->lock);
1694         switch (dev->state) {
1695         case STATE_SETUP:               // VERY odd... host died??
1696         case STATE_CONNECTED:
1697         case STATE_UNCONNECTED:
1698                 next_event (dev, GADGETFS_SUSPEND);
1699                 ep0_readable (dev);
1700                 /* FALLTHROUGH */
1701         default:
1702                 break;
1703         }
1704         spin_unlock (&dev->lock);
1705 }
1706
1707 static struct usb_gadget_driver gadgetfs_driver = {
1708 #ifdef  HIGHSPEED
1709         .speed          = USB_SPEED_HIGH,
1710 #else
1711         .speed          = USB_SPEED_FULL,
1712 #endif
1713         .function       = (char *) driver_desc,
1714         .bind           = gadgetfs_bind,
1715         .unbind         = gadgetfs_unbind,
1716         .setup          = gadgetfs_setup,
1717         .disconnect     = gadgetfs_disconnect,
1718         .suspend        = gadgetfs_suspend,
1719
1720         .driver         = {
1721                 .name           = (char *) shortname,
1722                 // .shutdown = ...
1723                 // .suspend = ...
1724                 // .resume = ...
1725         },
1726 };
1727
1728 /*----------------------------------------------------------------------*/
1729
1730 /* DEVICE INITIALIZATION
1731  *
1732  *     fd = open ("/dev/gadget/$CHIP", O_RDWR)
1733  *     status = write (fd, descriptors, sizeof descriptors)
1734  *
1735  * That write establishes the device configuration, so the kernel can
1736  * bind to the controller ... guaranteeing it can handle enumeration
1737  * at all necessary speeds.  Descriptor order is:
1738  *
1739  * . message tag (u32, host order) ... for now, must be zero; it
1740  *      would change to support features like multi-config devices
1741  * . full/low speed config ... all wTotalLength bytes (with interface,
1742  *      class, altsetting, endpoint, and other descriptors)
1743  * . high speed config ... all descriptors, for high speed operation;
1744  *      this one's optional except for high-speed hardware
1745  * . device descriptor
1746  *
1747  * Endpoints are not yet enabled. Drivers may want to immediately
1748  * initialize them, using the /dev/gadget/ep* files that are available
1749  * as soon as the kernel sees the configuration, or they can wait
1750  * until device configuration and interface altsetting changes create
1751  * the need to configure (or unconfigure) them.
1752  *
1753  * After initialization, the device stays active for as long as that
1754  * $CHIP file is open.  Events may then be read from that descriptor,
1755  * such configuration notifications.  More complex drivers will handle
1756  * some control requests in user space.
1757  */
1758
1759 static int is_valid_config (struct usb_config_descriptor *config)
1760 {
1761         return config->bDescriptorType == USB_DT_CONFIG
1762                 && config->bLength == USB_DT_CONFIG_SIZE
1763                 && config->bConfigurationValue != 0
1764                 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
1765                 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
1766         /* FIXME check lengths: walk to end */
1767 }
1768
1769 static ssize_t
1770 dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1771 {
1772         struct dev_data         *dev = fd->private_data;
1773         ssize_t                 value = len, length = len;
1774         unsigned                total;
1775         u32                     tag;
1776         char                    *kbuf;
1777
1778         if (dev->state != STATE_OPENED)
1779                 return -EEXIST;
1780
1781         if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
1782                 return -EINVAL;
1783
1784         /* we might need to change message format someday */
1785         if (copy_from_user (&tag, buf, 4))
1786                 return -EFAULT;
1787         if (tag != 0)
1788                 return -EINVAL;
1789         buf += 4;
1790         length -= 4;
1791
1792         kbuf = kmalloc (length, SLAB_KERNEL);
1793         if (!kbuf)
1794                 return -ENOMEM;
1795         if (copy_from_user (kbuf, buf, length)) {
1796                 kfree (kbuf);
1797                 return -EFAULT;
1798         }
1799
1800         spin_lock_irq (&dev->lock);
1801         value = -EINVAL;
1802         if (dev->buf)
1803                 goto fail;
1804         dev->buf = kbuf;
1805
1806         /* full or low speed config */
1807         dev->config = (void *) kbuf;
1808         total = le16_to_cpup (&dev->config->wTotalLength);
1809         if (!is_valid_config (dev->config) || total >= length)
1810                 goto fail;
1811         kbuf += total;
1812         length -= total;
1813
1814         /* optional high speed config */
1815         if (kbuf [1] == USB_DT_CONFIG) {
1816                 dev->hs_config = (void *) kbuf;
1817                 total = le16_to_cpup (&dev->hs_config->wTotalLength);
1818                 if (!is_valid_config (dev->hs_config) || total >= length)
1819                         goto fail;
1820                 kbuf += total;
1821                 length -= total;
1822         }
1823
1824         /* could support multiple configs, using another encoding! */
1825
1826         /* device descriptor (tweaked for paranoia) */
1827         if (length != USB_DT_DEVICE_SIZE)
1828                 goto fail;
1829         dev->dev = (void *)kbuf;
1830         if (dev->dev->bLength != USB_DT_DEVICE_SIZE
1831                         || dev->dev->bDescriptorType != USB_DT_DEVICE
1832                         || dev->dev->bNumConfigurations != 1)
1833                 goto fail;
1834         dev->dev->bNumConfigurations = 1;
1835         dev->dev->bcdUSB = __constant_cpu_to_le16 (0x0200);
1836
1837         /* triggers gadgetfs_bind(); then we can enumerate. */
1838         spin_unlock_irq (&dev->lock);
1839         value = usb_gadget_register_driver (&gadgetfs_driver);
1840         if (value != 0) {
1841                 kfree (dev->buf);
1842                 dev->buf = NULL;
1843         } else {
1844                 /* at this point "good" hardware has for the first time
1845                  * let the USB the host see us.  alternatively, if users
1846                  * unplug/replug that will clear all the error state.
1847                  *
1848                  * note:  everything running before here was guaranteed
1849                  * to choke driver model style diagnostics.  from here
1850                  * on, they can work ... except in cleanup paths that
1851                  * kick in after the ep0 descriptor is closed.
1852                  */
1853                 fd->f_op = &ep0_io_operations;
1854                 value = len;
1855         }
1856         return value;
1857
1858 fail:
1859         spin_unlock_irq (&dev->lock);
1860         pr_debug ("%s: %s fail %Zd, %p\n", shortname, __FUNCTION__, value, dev);
1861         kfree (dev->buf);
1862         dev->buf = NULL;
1863         return value;
1864 }
1865
1866 static int
1867 dev_open (struct inode *inode, struct file *fd)
1868 {
1869         struct dev_data         *dev = inode->u.generic_ip;
1870         int                     value = -EBUSY;
1871
1872         if (dev->state == STATE_DEV_DISABLED) {
1873                 dev->ev_next = 0;
1874                 dev->state = STATE_OPENED;
1875                 fd->private_data = dev;
1876                 get_dev (dev);
1877                 value = 0;
1878         }
1879         return value;
1880 }
1881
1882 static struct file_operations dev_init_operations = {
1883         .owner =        THIS_MODULE,
1884         .open =         dev_open,
1885         .write =        dev_config,
1886         .fasync =       ep0_fasync,
1887         .ioctl =        dev_ioctl,
1888         .release =      dev_release,
1889 };
1890
1891 /*----------------------------------------------------------------------*/
1892
1893 /* FILESYSTEM AND SUPERBLOCK OPERATIONS
1894  *
1895  * Mounting the filesystem creates a controller file, used first for
1896  * device configuration then later for event monitoring.
1897  */
1898
1899
1900 /* FIXME PAM etc could set this security policy without mount options
1901  * if epfiles inherited ownership and permissons from ep0 ...
1902  */
1903
1904 static unsigned default_uid;
1905 static unsigned default_gid;
1906 static unsigned default_perm = S_IRUSR | S_IWUSR;
1907
1908 module_param (default_uid, uint, 0644);
1909 module_param (default_gid, uint, 0644);
1910 module_param (default_perm, uint, 0644);
1911
1912
1913 static struct inode *
1914 gadgetfs_make_inode (struct super_block *sb,
1915                 void *data, struct file_operations *fops,
1916                 int mode)
1917 {
1918         struct inode *inode = new_inode (sb);
1919
1920         if (inode) {
1921                 inode->i_mode = mode;
1922                 inode->i_uid = default_uid;
1923                 inode->i_gid = default_gid;
1924                 inode->i_blksize = PAGE_CACHE_SIZE;
1925                 inode->i_blocks = 0;
1926                 inode->i_atime = inode->i_mtime = inode->i_ctime
1927                                 = CURRENT_TIME;
1928                 inode->u.generic_ip = data;
1929                 inode->i_fop = fops;
1930         }
1931         return inode;
1932 }
1933
1934 /* creates in fs root directory, so non-renamable and non-linkable.
1935  * so inode and dentry are paired, until device reconfig.
1936  */
1937 static struct inode *
1938 gadgetfs_create_file (struct super_block *sb, char const *name,
1939                 void *data, struct file_operations *fops,
1940                 struct dentry **dentry_p)
1941 {
1942         struct dentry   *dentry;
1943         struct inode    *inode;
1944         struct qstr     qname;
1945
1946         qname.name = name;
1947         qname.len = strlen (name);
1948         qname.hash = full_name_hash (qname.name, qname.len);
1949         dentry = d_alloc (sb->s_root, &qname);
1950         if (!dentry)
1951                 return NULL;
1952
1953         inode = gadgetfs_make_inode (sb, data, fops,
1954                         S_IFREG | (default_perm & S_IRWXUGO));
1955         if (!inode) {
1956                 dput(dentry);
1957                 return NULL;
1958         }
1959         d_add (dentry, inode);
1960         *dentry_p = dentry;
1961         return inode;
1962 }
1963
1964 static struct super_operations gadget_fs_operations = {
1965         .statfs =       simple_statfs,
1966         .drop_inode =   generic_delete_inode,
1967 };
1968
1969 static int
1970 gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
1971 {
1972         struct inode    *inode;
1973         struct dentry   *d;
1974         struct dev_data *dev;
1975
1976         if (the_device)
1977                 return -ESRCH;
1978
1979         /* superblock */
1980         sb->s_blocksize = PAGE_CACHE_SIZE;
1981         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1982         sb->s_magic = GADGETFS_MAGIC;
1983         sb->s_op = &gadget_fs_operations;
1984
1985         /* root inode */
1986         inode = gadgetfs_make_inode (sb,
1987                         NULL, &simple_dir_operations,
1988                         S_IFDIR | S_IRUGO | S_IXUGO);
1989         if (!inode)
1990                 return -ENOMEM;
1991         inode->i_op = &simple_dir_inode_operations;
1992         if (!(d = d_alloc_root (inode))) {
1993                 iput (inode);
1994                 return -ENOMEM;
1995         }
1996         sb->s_root = d;
1997
1998         /* the ep0 file is named after the controller we expect;
1999          * user mode code can use it for sanity checks, like we do.
2000          */
2001         dev = dev_new ();
2002         if (!dev)
2003                 return -ENOMEM;
2004
2005         dev->sb = sb;
2006         if (!(inode = gadgetfs_create_file (sb, CHIP,
2007                                 dev, &dev_init_operations,
2008                                 &dev->dentry))) {
2009                 put_dev(dev);
2010                 return -ENOMEM;
2011         }
2012
2013         /* other endpoint files are available after hardware setup,
2014          * from binding to a controller.
2015          */
2016         the_device = dev;
2017         return 0;
2018 }
2019
2020 /* "mount -t gadgetfs path /dev/gadget" ends up here */
2021 static struct super_block *
2022 gadgetfs_get_sb (struct file_system_type *t, int flags,
2023                 const char *path, void *opts)
2024 {
2025         return get_sb_single (t, flags, opts, gadgetfs_fill_super);
2026 }
2027
2028 static void
2029 gadgetfs_kill_sb (struct super_block *sb)
2030 {
2031         kill_litter_super (sb);
2032         if (the_device) {
2033                 put_dev (the_device);
2034                 the_device = NULL;
2035         }
2036 }
2037
2038 /*----------------------------------------------------------------------*/
2039
2040 static struct file_system_type gadgetfs_type = {
2041         .owner          = THIS_MODULE,
2042         .name           = shortname,
2043         .get_sb         = gadgetfs_get_sb,
2044         .kill_sb        = gadgetfs_kill_sb,
2045 };
2046
2047 /*----------------------------------------------------------------------*/
2048
2049 static int __init init (void)
2050 {
2051         int status;
2052
2053         status = register_filesystem (&gadgetfs_type);
2054         if (status == 0)
2055                 pr_info ("%s: %s, version " DRIVER_VERSION "\n",
2056                         shortname, driver_desc);
2057         return status;
2058 }
2059 module_init (init);
2060
2061 static void __exit cleanup (void)
2062 {
2063         pr_debug ("unregister %s\n", shortname);
2064         unregister_filesystem (&gadgetfs_type);
2065 }
2066 module_exit (cleanup);
2067