~ubuntu-branches/ubuntu/vivid/linux-fsl-imx51/vivid

« back to all changes in this revision

Viewing changes to drivers/usb/gadget/arm_mxc_ubuntu_inode.c

  • Committer: Bazaar Package Importer
  • Author(s): Andy Whitcroft, Amit Kucheria, Andy Whitcroft, Bryan Wu, Upstream Kernel Changes
  • Date: 2010-01-11 16:26:27 UTC
  • Revision ID: james.westby@ubuntu.com-20100111162627-1q2fl9tcuwcywt1e
Tags: 2.6.31-602.4
[ Amit Kucheria ]

* Update to official 2.6.31 BSP release from Freescale

[ Andy Whitcroft ]

* drop a number of modules no longer built

[ Bryan Wu ]

* [Config] Update configs after applying .31 patchset from Freescale
* [Config] Sync with imx51_defconfig from Freescale BSP

[ Upstream Kernel Changes ]

* Update to official 2.6.31 BSP release from Freescale.

Show diffs side-by-side

added added

removed removed

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