blob: 7656136fb6c14015f0b27bf289902020c3b69e18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13
David Brownella4e3ef52007-08-01 23:58:22 -070014/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/pagemap.h>
20#include <linux/uts.h>
21#include <linux/wait.h>
22#include <linux/compiler.h>
23#include <asm/uaccess.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040024#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
Milan Svobodae22fc272006-08-08 22:23:12 -070026#include <linux/poll.h>
Zach Browna80bf612013-05-07 16:18:23 -070027#include <linux/mmu_context.h>
Kent Overstreet4e179bc2013-05-07 16:18:33 -070028#include <linux/aio.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080029#include <linux/uio.h>
Alan Sternfd5336c2017-09-21 13:23:58 -040030#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/device.h>
32#include <linux/moduleparam.h>
33
David Brownella5262dc2007-05-14 19:36:41 -070034#include <linux/usb/gadgetfs.h>
David Brownell9454a572007-10-04 18:05:17 -070035#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37
38/*
39 * The gadgetfs API maps each endpoint to a file descriptor so that you
40 * can use standard synchronous read/write calls for I/O. There's some
41 * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
42 * drivers show how this works in practice. You can also use AIO to
43 * eliminate I/O gaps between requests, to help when streaming data.
44 *
45 * Key parts that must be USB-specific are protocols defining how the
46 * read/write operations relate to the hardware state machines. There
47 * are two types of files. One type is for the device, implementing ep0.
48 * The other type is for each IN or OUT endpoint. In both cases, the
49 * user mode driver must configure the hardware before using it.
50 *
51 * - First, dev_config() is called when /dev/gadget/$CHIP is configured
52 * (by writing configuration and device descriptors). Afterwards it
53 * may serve as a source of device events, used to handle all control
54 * requests other than basic enumeration.
55 *
Phil Endecott511779f2007-01-15 11:35:01 -080056 * - Then, after a SET_CONFIGURATION control request, ep_config() is
57 * called when each /dev/gadget/ep* file is configured (by writing
58 * endpoint descriptors). Afterwards these files are used to write()
59 * IN data or to read() OUT data. To halt the endpoint, a "wrong
60 * direction" request is issued (like reading an IN endpoint).
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
63 * not possible on all hardware. For example, precise fault handling with
64 * respect to data left in endpoint fifos after aborted operations; or
65 * selective clearing of endpoint halts, to implement SET_INTERFACE.
66 */
67
68#define DRIVER_DESC "USB Gadget filesystem"
69#define DRIVER_VERSION "24 Aug 2004"
70
71static const char driver_desc [] = DRIVER_DESC;
72static const char shortname [] = "gadgetfs";
73
74MODULE_DESCRIPTION (DRIVER_DESC);
75MODULE_AUTHOR ("David Brownell");
76MODULE_LICENSE ("GPL");
77
Al Virod4461a62015-03-03 08:39:34 +000078static int ep_open(struct inode *, struct file *);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*----------------------------------------------------------------------*/
82
83#define GADGETFS_MAGIC 0xaee71ee7
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* /dev/gadget/$CHIP represents ep0 and the whole device */
86enum ep0_state {
87 /* DISBLED is the initial state.
88 */
89 STATE_DEV_DISABLED = 0,
90
91 /* Only one open() of /dev/gadget/$CHIP; only one file tracks
92 * ep0/device i/o modes and binding to the controller. Driver
93 * must always write descriptors to initialize the device, then
94 * the device becomes UNCONNECTED until enumeration.
95 */
David Brownell7489d142007-01-16 22:51:04 -080096 STATE_DEV_OPENED,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* From then on, ep0 fd is in either of two basic modes:
99 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
100 * - SETUP: read/write will transfer control data and succeed;
101 * or if "wrong direction", performs protocol stall
102 */
David Brownell7489d142007-01-16 22:51:04 -0800103 STATE_DEV_UNCONNECTED,
104 STATE_DEV_CONNECTED,
105 STATE_DEV_SETUP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* UNBOUND means the driver closed ep0, so the device won't be
108 * accessible again (DEV_DISABLED) until all fds are closed.
109 */
110 STATE_DEV_UNBOUND,
111};
112
113/* enough for the whole queue: most events invalidate others */
114#define N_EVENT 5
115
Greg Kroah-Hartman33dd9bc2021-12-09 18:59:27 +0100116#define RBUF_SIZE 256
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118struct dev_data {
119 spinlock_t lock;
120 atomic_t count;
Alan Sternfd5336c2017-09-21 13:23:58 -0400121 int udc_usage;
David Brownell7489d142007-01-16 22:51:04 -0800122 enum ep0_state state; /* P: lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct usb_gadgetfs_event event [N_EVENT];
124 unsigned ev_next;
125 struct fasync_struct *fasync;
126 u8 current_config;
127
128 /* drivers reading ep0 MUST handle control requests (SETUP)
129 * reported that way; else the host will time out.
130 */
131 unsigned usermode_setup : 1,
132 setup_in : 1,
133 setup_can_stall : 1,
134 setup_out_ready : 1,
135 setup_out_error : 1,
Marek Szyprowski7b0a2712016-02-18 08:59:26 +0100136 setup_abort : 1,
137 gadget_registered : 1;
Alan Stern97906362006-01-03 10:30:31 -0500138 unsigned setup_wLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* the rest is basically write-once */
141 struct usb_config_descriptor *config, *hs_config;
142 struct usb_device_descriptor *dev;
143 struct usb_request *req;
144 struct usb_gadget *gadget;
145 struct list_head epfiles;
146 void *buf;
147 wait_queue_head_t wait;
148 struct super_block *sb;
149 struct dentry *dentry;
150
151 /* except this scratch i/o buffer for ep0 */
Greg Kroah-Hartman33dd9bc2021-12-09 18:59:27 +0100152 u8 rbuf[RBUF_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
155static inline void get_dev (struct dev_data *data)
156{
157 atomic_inc (&data->count);
158}
159
160static void put_dev (struct dev_data *data)
161{
162 if (likely (!atomic_dec_and_test (&data->count)))
163 return;
164 /* needs no more cleanup */
165 BUG_ON (waitqueue_active (&data->wait));
166 kfree (data);
167}
168
169static struct dev_data *dev_new (void)
170{
171 struct dev_data *dev;
172
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800173 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (!dev)
175 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 dev->state = STATE_DEV_DISABLED;
177 atomic_set (&dev->count, 1);
178 spin_lock_init (&dev->lock);
179 INIT_LIST_HEAD (&dev->epfiles);
180 init_waitqueue_head (&dev->wait);
181 return dev;
182}
183
184/*----------------------------------------------------------------------*/
185
186/* other /dev/gadget/$ENDPOINT files represent endpoints */
187enum ep_state {
188 STATE_EP_DISABLED = 0,
189 STATE_EP_READY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 STATE_EP_ENABLED,
191 STATE_EP_UNBOUND,
192};
193
194struct ep_data {
Thomas Gleixnera79df502010-01-29 20:38:59 +0000195 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 enum ep_state state;
197 atomic_t count;
198 struct dev_data *dev;
199 /* must hold dev->lock before accessing ep or req */
200 struct usb_ep *ep;
201 struct usb_request *req;
202 ssize_t status;
203 char name [16];
204 struct usb_endpoint_descriptor desc, hs_desc;
205 struct list_head epfiles;
206 wait_queue_head_t wait;
207 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
210static inline void get_ep (struct ep_data *data)
211{
212 atomic_inc (&data->count);
213}
214
215static void put_ep (struct ep_data *data)
216{
217 if (likely (!atomic_dec_and_test (&data->count)))
218 return;
219 put_dev (data->dev);
220 /* needs no more cleanup */
221 BUG_ON (!list_empty (&data->epfiles));
222 BUG_ON (waitqueue_active (&data->wait));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 kfree (data);
224}
225
226/*----------------------------------------------------------------------*/
227
228/* most "how to use the hardware" policy choices are in userspace:
229 * mapping endpoint roles (which the driver needs) to the capabilities
230 * which the usb controller has. most of those capabilities are exposed
231 * implicitly, starting with the driver name and then endpoint names.
232 */
233
234static const char *CHIP;
235
236/*----------------------------------------------------------------------*/
237
238/* NOTE: don't use dev_printk calls before binding to the gadget
239 * at the end of ep0 configuration, or after unbind.
240 */
241
242/* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
243#define xprintk(d,level,fmt,args...) \
244 printk(level "%s: " fmt , shortname , ## args)
245
246#ifdef DEBUG
247#define DBG(dev,fmt,args...) \
248 xprintk(dev , KERN_DEBUG , fmt , ## args)
249#else
250#define DBG(dev,fmt,args...) \
251 do { } while (0)
252#endif /* DEBUG */
253
David Brownella4e3ef52007-08-01 23:58:22 -0700254#ifdef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#define VDEBUG DBG
256#else
257#define VDEBUG(dev,fmt,args...) \
258 do { } while (0)
259#endif /* DEBUG */
260
261#define ERROR(dev,fmt,args...) \
262 xprintk(dev , KERN_ERR , fmt , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#define INFO(dev,fmt,args...) \
264 xprintk(dev , KERN_INFO , fmt , ## args)
265
266
267/*----------------------------------------------------------------------*/
268
269/* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
270 *
271 * After opening, configure non-control endpoints. Then use normal
272 * stream read() and write() requests; and maybe ioctl() to get more
Steven Cole093cf722005-05-03 19:07:24 -0600273 * precise FIFO status when recovering from cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
275
276static void epio_complete (struct usb_ep *ep, struct usb_request *req)
277{
278 struct ep_data *epdata = ep->driver_data;
279
280 if (!req->context)
281 return;
282 if (req->status)
283 epdata->status = req->status;
284 else
285 epdata->status = req->actual;
286 complete ((struct completion *)req->context);
287}
288
289/* tasklock endpoint, returning when it's connected.
290 * still need dev->lock to use epdata->ep.
291 */
292static int
Al Virod4461a62015-03-03 08:39:34 +0000293get_ready_ep (unsigned f_flags, struct ep_data *epdata, bool is_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 int val;
296
297 if (f_flags & O_NONBLOCK) {
Thomas Gleixnera79df502010-01-29 20:38:59 +0000298 if (!mutex_trylock(&epdata->lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto nonblock;
Al Virod4461a62015-03-03 08:39:34 +0000300 if (epdata->state != STATE_EP_ENABLED &&
301 (!is_write || epdata->state != STATE_EP_READY)) {
Thomas Gleixnera79df502010-01-29 20:38:59 +0000302 mutex_unlock(&epdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303nonblock:
304 val = -EAGAIN;
305 } else
306 val = 0;
307 return val;
308 }
309
Thomas Gleixnera79df502010-01-29 20:38:59 +0000310 val = mutex_lock_interruptible(&epdata->lock);
311 if (val < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return val;
Phil Endecott511779f2007-01-15 11:35:01 -0800313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 switch (epdata->state) {
315 case STATE_EP_ENABLED:
Al Virod4461a62015-03-03 08:39:34 +0000316 return 0;
317 case STATE_EP_READY: /* not configured yet */
318 if (is_write)
319 return 0;
320 // FALLTHRU
321 case STATE_EP_UNBOUND: /* clean disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 // case STATE_EP_DISABLED: /* "can't happen" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 default: /* error! */
325 pr_debug ("%s: ep %p not available, state %d\n",
326 shortname, epdata, epdata->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Al Virod4461a62015-03-03 08:39:34 +0000328 mutex_unlock(&epdata->lock);
329 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332static ssize_t
333ep_io (struct ep_data *epdata, void *buf, unsigned len)
334{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700335 DECLARE_COMPLETION_ONSTACK (done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int value;
337
338 spin_lock_irq (&epdata->dev->lock);
339 if (likely (epdata->ep != NULL)) {
340 struct usb_request *req = epdata->req;
341
342 req->context = &done;
343 req->complete = epio_complete;
344 req->buf = buf;
345 req->length = len;
346 value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
347 } else
348 value = -ENODEV;
349 spin_unlock_irq (&epdata->dev->lock);
350
351 if (likely (value == 0)) {
352 value = wait_event_interruptible (done.wait, done.done);
353 if (value != 0) {
354 spin_lock_irq (&epdata->dev->lock);
355 if (likely (epdata->ep != NULL)) {
356 DBG (epdata->dev, "%s i/o interrupted\n",
357 epdata->name);
358 usb_ep_dequeue (epdata->ep, epdata->req);
359 spin_unlock_irq (&epdata->dev->lock);
360
361 wait_event (done.wait, done.done);
362 if (epdata->status == -ECONNRESET)
363 epdata->status = -EINTR;
364 } else {
365 spin_unlock_irq (&epdata->dev->lock);
366
367 DBG (epdata->dev, "endpoint gone\n");
368 epdata->status = -ENODEV;
369 }
370 }
371 return epdata->status;
372 }
373 return value;
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static int
377ep_release (struct inode *inode, struct file *fd)
378{
379 struct ep_data *data = fd->private_data;
Milan Svobodaba307f52006-06-26 07:48:00 -0700380 int value;
381
Thomas Gleixnera79df502010-01-29 20:38:59 +0000382 value = mutex_lock_interruptible(&data->lock);
383 if (value < 0)
Milan Svobodaba307f52006-06-26 07:48:00 -0700384 return value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* clean up if this can be reopened */
387 if (data->state != STATE_EP_UNBOUND) {
388 data->state = STATE_EP_DISABLED;
389 data->desc.bDescriptorType = 0;
390 data->hs_desc.bDescriptorType = 0;
Pavol Kurina4809ecc2005-09-07 09:49:34 -0700391 usb_ep_disable(data->ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Thomas Gleixnera79df502010-01-29 20:38:59 +0000393 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 put_ep (data);
395 return 0;
396}
397
Alan Cox44c389a2008-05-22 22:03:27 +0100398static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct ep_data *data = fd->private_data;
401 int status;
402
Al Virod4461a62015-03-03 08:39:34 +0000403 if ((status = get_ready_ep (fd->f_flags, data, false)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return status;
405
406 spin_lock_irq (&data->dev->lock);
407 if (likely (data->ep != NULL)) {
408 switch (code) {
409 case GADGETFS_FIFO_STATUS:
410 status = usb_ep_fifo_status (data->ep);
411 break;
412 case GADGETFS_FIFO_FLUSH:
413 usb_ep_fifo_flush (data->ep);
414 break;
415 case GADGETFS_CLEAR_HALT:
416 status = usb_ep_clear_halt (data->ep);
417 break;
418 default:
419 status = -ENOTTY;
420 }
421 } else
422 status = -ENODEV;
423 spin_unlock_irq (&data->dev->lock);
Thomas Gleixnera79df502010-01-29 20:38:59 +0000424 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return status;
426}
427
428/*----------------------------------------------------------------------*/
429
430/* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
431
432struct kiocb_priv {
433 struct usb_request *req;
434 struct ep_data *epdata;
Zach Browna80bf612013-05-07 16:18:23 -0700435 struct kiocb *iocb;
436 struct mm_struct *mm;
437 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 void *buf;
Al Viro7fe39762015-02-07 00:30:23 -0500439 struct iov_iter to;
440 const void *to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned actual;
442};
443
Kent Overstreetbec68faa2013-05-13 14:45:08 -0700444static int ep_aio_cancel(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct kiocb_priv *priv = iocb->private;
447 struct ep_data *epdata;
448 int value;
449
450 local_irq_disable();
451 epdata = priv->epdata;
452 // spin_lock(&epdata->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (likely(epdata && epdata->ep && priv->req))
454 value = usb_ep_dequeue (epdata->ep, priv->req);
455 else
456 value = -EINVAL;
457 // spin_unlock(&epdata->dev->lock);
458 local_irq_enable();
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return value;
461}
462
Zach Browna80bf612013-05-07 16:18:23 -0700463static void ep_user_copy_worker(struct work_struct *work)
464{
465 struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work);
466 struct mm_struct *mm = priv->mm;
467 struct kiocb *iocb = priv->iocb;
468 size_t ret;
469
470 use_mm(mm);
Al Viro7fe39762015-02-07 00:30:23 -0500471 ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
Zach Browna80bf612013-05-07 16:18:23 -0700472 unuse_mm(mm);
Al Viro7fe39762015-02-07 00:30:23 -0500473 if (!ret)
474 ret = -EFAULT;
Zach Browna80bf612013-05-07 16:18:23 -0700475
476 /* completing the iocb can drop the ctx and mm, don't touch mm after */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100477 iocb->ki_complete(iocb, ret, ret);
Zach Browna80bf612013-05-07 16:18:23 -0700478
David Brownell25051072007-01-15 11:30:28 -0800479 kfree(priv->buf);
Al Viro7fe39762015-02-07 00:30:23 -0500480 kfree(priv->to_free);
David Brownell25051072007-01-15 11:30:28 -0800481 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
485{
486 struct kiocb *iocb = req->context;
487 struct kiocb_priv *priv = iocb->private;
488 struct ep_data *epdata = priv->epdata;
489
490 /* lock against disconnect (and ideally, cancel) */
491 spin_lock(&epdata->dev->lock);
492 priv->req = NULL;
493 priv->epdata = NULL;
Alan Stern49631ca2007-01-16 23:28:48 -0800494
495 /* if this was a write or a read returning no data then we
496 * don't need to copy anything to userspace, so we can
497 * complete the aio request immediately.
498 */
Al Viro7fe39762015-02-07 00:30:23 -0500499 if (priv->to_free == NULL || unlikely(req->actual == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 kfree(req->buf);
Al Viro7fe39762015-02-07 00:30:23 -0500501 kfree(priv->to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 kfree(priv);
503 iocb->private = NULL;
504 /* aio_complete() reports bytes-transferred _and_ faults */
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100505
506 iocb->ki_complete(iocb, req->actual ? req->actual : req->status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 req->status);
508 } else {
Zach Browna80bf612013-05-07 16:18:23 -0700509 /* ep_copy_to_user() won't report both; we hide some faults */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (unlikely(0 != req->status))
511 DBG(epdata->dev, "%s fault %d len %d\n",
512 ep->name, req->status, req->actual);
513
514 priv->buf = req->buf;
515 priv->actual = req->actual;
Al Viro7fe39762015-02-07 00:30:23 -0500516 INIT_WORK(&priv->work, ep_user_copy_worker);
Zach Browna80bf612013-05-07 16:18:23 -0700517 schedule_work(&priv->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 usb_ep_free_request(ep, req);
Alan Sternfd5336c2017-09-21 13:23:58 -0400521 spin_unlock(&epdata->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 put_ep(epdata);
523}
524
Al Viro7fe39762015-02-07 00:30:23 -0500525static ssize_t ep_aio(struct kiocb *iocb,
526 struct kiocb_priv *priv,
527 struct ep_data *epdata,
528 char *buf,
529 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Al Viro7fe39762015-02-07 00:30:23 -0500531 struct usb_request *req;
532 ssize_t value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 iocb->private = priv;
Zach Browna80bf612013-05-07 16:18:23 -0700535 priv->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Kent Overstreet0460fef2013-05-07 16:18:49 -0700537 kiocb_set_cancel_fn(iocb, ep_aio_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 get_ep(epdata);
539 priv->epdata = epdata;
540 priv->actual = 0;
Zach Browna80bf612013-05-07 16:18:23 -0700541 priv->mm = current->mm; /* mm teardown waits for iocbs in exit_aio() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* each kiocb is coupled to one usb_request, but we can't
544 * allocate or submit those if the host disconnected.
545 */
546 spin_lock_irq(&epdata->dev->lock);
Al Viro7fe39762015-02-07 00:30:23 -0500547 value = -ENODEV;
Mathieu Laurendeau327b21da2016-07-15 14:58:41 +0200548 if (unlikely(epdata->ep == NULL))
Al Viro7fe39762015-02-07 00:30:23 -0500549 goto fail;
550
551 req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
552 value = -ENOMEM;
553 if (unlikely(!req))
554 goto fail;
555
556 priv->req = req;
557 req->buf = buf;
558 req->length = len;
559 req->complete = ep_aio_complete;
560 req->context = iocb;
561 value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
562 if (unlikely(0 != value)) {
563 usb_ep_free_request(epdata->ep, req);
564 goto fail;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_unlock_irq(&epdata->dev->lock);
Al Viro7fe39762015-02-07 00:30:23 -0500567 return -EIOCBQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Al Viro7fe39762015-02-07 00:30:23 -0500569fail:
570 spin_unlock_irq(&epdata->dev->lock);
571 kfree(priv->to_free);
572 kfree(priv);
573 put_ep(epdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return value;
575}
576
577static ssize_t
Al Viro7fe39762015-02-07 00:30:23 -0500578ep_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Al Viro7fe39762015-02-07 00:30:23 -0500580 struct file *file = iocb->ki_filp;
581 struct ep_data *epdata = file->private_data;
582 size_t len = iov_iter_count(to);
583 ssize_t value;
584 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Al Virod4461a62015-03-03 08:39:34 +0000586 if ((value = get_ready_ep(file->f_flags, epdata, false)) < 0)
Al Viro7fe39762015-02-07 00:30:23 -0500587 return value;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700588
Al Viro7fe39762015-02-07 00:30:23 -0500589 /* halt any endpoint by doing a "wrong direction" i/o call */
590 if (usb_endpoint_dir_in(&epdata->desc)) {
591 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
592 !is_sync_kiocb(iocb)) {
593 mutex_unlock(&epdata->lock);
594 return -EINVAL;
595 }
596 DBG (epdata->dev, "%s halt\n", epdata->name);
597 spin_lock_irq(&epdata->dev->lock);
598 if (likely(epdata->ep != NULL))
599 usb_ep_set_halt(epdata->ep);
600 spin_unlock_irq(&epdata->dev->lock);
601 mutex_unlock(&epdata->lock);
602 return -EBADMSG;
603 }
604
605 buf = kmalloc(len, GFP_KERNEL);
606 if (unlikely(!buf)) {
607 mutex_unlock(&epdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return -ENOMEM;
Al Viro7fe39762015-02-07 00:30:23 -0500609 }
610 if (is_sync_kiocb(iocb)) {
611 value = ep_io(epdata, buf, len);
Binyamin Sharet63196e92016-07-07 22:22:04 +0300612 if (value >= 0 && (copy_to_iter(buf, value, to) != value))
Al Viro7fe39762015-02-07 00:30:23 -0500613 value = -EFAULT;
614 } else {
615 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
616 value = -ENOMEM;
617 if (!priv)
618 goto fail;
619 priv->to_free = dup_iter(&priv->to, to, GFP_KERNEL);
620 if (!priv->to_free) {
621 kfree(priv);
622 goto fail;
623 }
624 value = ep_aio(iocb, priv, epdata, buf, len);
625 if (value == -EIOCBQUEUED)
626 buf = NULL;
627 }
628fail:
629 kfree(buf);
630 mutex_unlock(&epdata->lock);
631 return value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Al Virod4461a62015-03-03 08:39:34 +0000634static ssize_t ep_config(struct ep_data *, const char *, size_t);
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static ssize_t
Al Viro7fe39762015-02-07 00:30:23 -0500637ep_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Al Viro7fe39762015-02-07 00:30:23 -0500639 struct file *file = iocb->ki_filp;
640 struct ep_data *epdata = file->private_data;
641 size_t len = iov_iter_count(from);
Al Virod4461a62015-03-03 08:39:34 +0000642 bool configured;
Al Viro7fe39762015-02-07 00:30:23 -0500643 ssize_t value;
644 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Al Virod4461a62015-03-03 08:39:34 +0000646 if ((value = get_ready_ep(file->f_flags, epdata, true)) < 0)
Al Viro7fe39762015-02-07 00:30:23 -0500647 return value;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700648
Al Virod4461a62015-03-03 08:39:34 +0000649 configured = epdata->state == STATE_EP_ENABLED;
650
Al Viro7fe39762015-02-07 00:30:23 -0500651 /* halt any endpoint by doing a "wrong direction" i/o call */
Al Virod4461a62015-03-03 08:39:34 +0000652 if (configured && !usb_endpoint_dir_in(&epdata->desc)) {
Al Viro7fe39762015-02-07 00:30:23 -0500653 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
654 !is_sync_kiocb(iocb)) {
655 mutex_unlock(&epdata->lock);
656 return -EINVAL;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700657 }
Al Viro7fe39762015-02-07 00:30:23 -0500658 DBG (epdata->dev, "%s halt\n", epdata->name);
659 spin_lock_irq(&epdata->dev->lock);
660 if (likely(epdata->ep != NULL))
661 usb_ep_set_halt(epdata->ep);
662 spin_unlock_irq(&epdata->dev->lock);
663 mutex_unlock(&epdata->lock);
664 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Al Viro7fe39762015-02-07 00:30:23 -0500666
667 buf = kmalloc(len, GFP_KERNEL);
668 if (unlikely(!buf)) {
669 mutex_unlock(&epdata->lock);
670 return -ENOMEM;
671 }
672
673 if (unlikely(copy_from_iter(buf, len, from) != len)) {
674 value = -EFAULT;
675 goto out;
676 }
677
Al Virod4461a62015-03-03 08:39:34 +0000678 if (unlikely(!configured)) {
679 value = ep_config(epdata, buf, len);
680 } else if (is_sync_kiocb(iocb)) {
Al Viro7fe39762015-02-07 00:30:23 -0500681 value = ep_io(epdata, buf, len);
682 } else {
683 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
684 value = -ENOMEM;
685 if (priv) {
686 value = ep_aio(iocb, priv, epdata, buf, len);
687 if (value == -EIOCBQUEUED)
688 buf = NULL;
689 }
690 }
691out:
692 kfree(buf);
693 mutex_unlock(&epdata->lock);
694 return value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/*----------------------------------------------------------------------*/
698
699/* used after endpoint configuration */
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300700static const struct file_operations ep_io_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Al Virod4461a62015-03-03 08:39:34 +0000703 .open = ep_open,
704 .release = ep_release,
705 .llseek = no_llseek,
Alan Cox44c389a2008-05-22 22:03:27 +0100706 .unlocked_ioctl = ep_ioctl,
Al Viro7fe39762015-02-07 00:30:23 -0500707 .read_iter = ep_read_iter,
708 .write_iter = ep_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709};
710
711/* ENDPOINT INITIALIZATION
712 *
713 * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
714 * status = write (fd, descriptors, sizeof descriptors)
715 *
716 * That write establishes the endpoint configuration, configuring
717 * the controller to process bulk, interrupt, or isochronous transfers
718 * at the right maxpacket size, and so on.
719 *
720 * The descriptors are message type 1, identified by a host order u32
721 * at the beginning of what's written. Descriptor order is: full/low
722 * speed descriptor, then optional high speed descriptor.
723 */
724static ssize_t
Al Virod4461a62015-03-03 08:39:34 +0000725ep_config (struct ep_data *data, const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct usb_ep *ep;
728 u32 tag;
Milan Svoboda8a7471a2006-06-26 07:19:00 -0700729 int value, length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (data->state != STATE_EP_READY) {
732 value = -EL2HLT;
733 goto fail;
734 }
735
736 value = len;
737 if (len < USB_DT_ENDPOINT_SIZE + 4)
738 goto fail0;
739
740 /* we might need to change message format someday */
Al Virod4461a62015-03-03 08:39:34 +0000741 memcpy(&tag, buf, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (tag != 1) {
743 DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
744 goto fail0;
745 }
746 buf += 4;
747 len -= 4;
748
749 /* NOTE: audio endpoint extensions not accepted here;
750 * just don't include the extra bytes.
751 */
752
753 /* full/low speed descriptor, then high speed */
Al Virod4461a62015-03-03 08:39:34 +0000754 memcpy(&data->desc, buf, USB_DT_ENDPOINT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
756 || data->desc.bDescriptorType != USB_DT_ENDPOINT)
757 goto fail0;
758 if (len != USB_DT_ENDPOINT_SIZE) {
759 if (len != 2 * USB_DT_ENDPOINT_SIZE)
760 goto fail0;
Al Virod4461a62015-03-03 08:39:34 +0000761 memcpy(&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
762 USB_DT_ENDPOINT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
764 || data->hs_desc.bDescriptorType
765 != USB_DT_ENDPOINT) {
766 DBG(data->dev, "config %s, bad hs length or type\n",
767 data->name);
768 goto fail0;
769 }
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 spin_lock_irq (&data->dev->lock);
773 if (data->dev->state == STATE_DEV_UNBOUND) {
774 value = -ENOENT;
775 goto gone;
Greg Kroah-Hartman1ee7eea2015-04-30 11:32:54 +0200776 } else {
777 ep = data->ep;
778 if (ep == NULL) {
779 value = -ENODEV;
780 goto gone;
781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783 switch (data->dev->gadget->speed) {
784 case USB_SPEED_LOW:
785 case USB_SPEED_FULL:
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300786 ep->desc = &data->desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 case USB_SPEED_HIGH:
789 /* fails if caller didn't provide that descriptor... */
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300790 ep->desc = &data->hs_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 default:
Phil Endecott511779f2007-01-15 11:35:01 -0800793 DBG(data->dev, "unconnected, %s init abandoned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 data->name);
Phil Endecott511779f2007-01-15 11:35:01 -0800795 value = -EINVAL;
Al Virod4461a62015-03-03 08:39:34 +0000796 goto gone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Al Virod4461a62015-03-03 08:39:34 +0000798 value = usb_ep_enable(ep);
Milan Svoboda8a7471a2006-06-26 07:19:00 -0700799 if (value == 0) {
Al Virod4461a62015-03-03 08:39:34 +0000800 data->state = STATE_EP_ENABLED;
Milan Svoboda8a7471a2006-06-26 07:19:00 -0700801 value = length;
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803gone:
804 spin_unlock_irq (&data->dev->lock);
805 if (value < 0) {
806fail:
807 data->desc.bDescriptorType = 0;
808 data->hs_desc.bDescriptorType = 0;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return value;
811fail0:
812 value = -EINVAL;
813 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816static int
817ep_open (struct inode *inode, struct file *fd)
818{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700819 struct ep_data *data = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 int value = -EBUSY;
821
Thomas Gleixnera79df502010-01-29 20:38:59 +0000822 if (mutex_lock_interruptible(&data->lock) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -EINTR;
824 spin_lock_irq (&data->dev->lock);
825 if (data->dev->state == STATE_DEV_UNBOUND)
826 value = -ENOENT;
827 else if (data->state == STATE_EP_DISABLED) {
828 value = 0;
829 data->state = STATE_EP_READY;
830 get_ep (data);
831 fd->private_data = data;
832 VDEBUG (data->dev, "%s ready\n", data->name);
833 } else
834 DBG (data->dev, "%s state %d\n",
835 data->name, data->state);
836 spin_unlock_irq (&data->dev->lock);
Thomas Gleixnera79df502010-01-29 20:38:59 +0000837 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return value;
839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841/*----------------------------------------------------------------------*/
842
843/* EP0 IMPLEMENTATION can be partly in userspace.
844 *
845 * Drivers that use this facility receive various events, including
846 * control requests the kernel doesn't handle. Drivers that don't
847 * use this facility may be too simple-minded for real applications.
848 */
849
850static inline void ep0_readable (struct dev_data *dev)
851{
852 wake_up (&dev->wait);
853 kill_fasync (&dev->fasync, SIGIO, POLL_IN);
854}
855
856static void clean_req (struct usb_ep *ep, struct usb_request *req)
857{
858 struct dev_data *dev = ep->driver_data;
859
860 if (req->buf != dev->rbuf) {
David Brownell9d8bab52007-07-01 11:04:54 -0700861 kfree(req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 req->buf = dev->rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 req->complete = epio_complete;
865 dev->setup_out_ready = 0;
866}
867
868static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
869{
870 struct dev_data *dev = ep->driver_data;
David Brownell5b89db022007-01-16 22:56:26 -0800871 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int free = 1;
873
874 /* for control OUT, data must still get to userspace */
David Brownell5b89db022007-01-16 22:56:26 -0800875 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (!dev->setup_in) {
877 dev->setup_out_error = (req->status != 0);
878 if (!dev->setup_out_error)
879 free = 0;
880 dev->setup_out_ready = 1;
881 ep0_readable (dev);
David Brownell7489d142007-01-16 22:51:04 -0800882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /* clean up as appropriate */
885 if (free && req->buf != &dev->rbuf)
886 clean_req (ep, req);
887 req->complete = epio_complete;
David Brownell5b89db022007-01-16 22:56:26 -0800888 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
892{
893 struct dev_data *dev = ep->driver_data;
894
895 if (dev->setup_out_ready) {
896 DBG (dev, "ep0 request busy!\n");
897 return -EBUSY;
898 }
899 if (len > sizeof (dev->rbuf))
David Brownell9d8bab52007-07-01 11:04:54 -0700900 req->buf = kmalloc(len, GFP_ATOMIC);
David Brownella9475222007-07-30 12:31:07 -0700901 if (req->buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 req->buf = dev->rbuf;
903 return -ENOMEM;
904 }
905 req->complete = ep0_complete;
906 req->length = len;
Alan Stern97906362006-01-03 10:30:31 -0500907 req->zero = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return 0;
909}
910
911static ssize_t
912ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
913{
914 struct dev_data *dev = fd->private_data;
915 ssize_t retval;
916 enum ep0_state state;
917
918 spin_lock_irq (&dev->lock);
Alan Stern96b62a52015-03-04 10:31:50 -0500919 if (dev->state <= STATE_DEV_OPENED) {
920 retval = -EINVAL;
921 goto done;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /* report fd mode change before acting on it */
925 if (dev->setup_abort) {
926 dev->setup_abort = 0;
927 retval = -EIDRM;
928 goto done;
929 }
930
931 /* control DATA stage */
David Brownell7489d142007-01-16 22:51:04 -0800932 if ((state = dev->state) == STATE_DEV_SETUP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 if (dev->setup_in) { /* stall IN */
935 VDEBUG(dev, "ep0in stall\n");
936 (void) usb_ep_set_halt (dev->gadget->ep0);
937 retval = -EL2HLT;
David Brownell7489d142007-01-16 22:51:04 -0800938 dev->state = STATE_DEV_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 } else if (len == 0) { /* ack SET_CONFIGURATION etc */
941 struct usb_ep *ep = dev->gadget->ep0;
942 struct usb_request *req = dev->req;
943
Bin Liud246dcb2016-05-26 11:43:45 -0500944 if ((retval = setup_req (ep, req, 0)) == 0) {
Alan Sternfd5336c2017-09-21 13:23:58 -0400945 ++dev->udc_usage;
Bin Liud246dcb2016-05-26 11:43:45 -0500946 spin_unlock_irq (&dev->lock);
947 retval = usb_ep_queue (ep, req, GFP_KERNEL);
948 spin_lock_irq (&dev->lock);
Alan Sternfd5336c2017-09-21 13:23:58 -0400949 --dev->udc_usage;
Bin Liud246dcb2016-05-26 11:43:45 -0500950 }
David Brownell7489d142007-01-16 22:51:04 -0800951 dev->state = STATE_DEV_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* assume that was SET_CONFIGURATION */
954 if (dev->current_config) {
955 unsigned power;
David Brownella4e3ef52007-08-01 23:58:22 -0700956
957 if (gadget_is_dualspeed(dev->gadget)
958 && (dev->gadget->speed
959 == USB_SPEED_HIGH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 power = dev->hs_config->bMaxPower;
961 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 power = dev->config->bMaxPower;
963 usb_gadget_vbus_draw(dev->gadget, 2 * power);
964 }
965
966 } else { /* collect OUT data */
967 if ((fd->f_flags & O_NONBLOCK) != 0
968 && !dev->setup_out_ready) {
969 retval = -EAGAIN;
970 goto done;
971 }
972 spin_unlock_irq (&dev->lock);
973 retval = wait_event_interruptible (dev->wait,
974 dev->setup_out_ready != 0);
975
976 /* FIXME state could change from under us */
977 spin_lock_irq (&dev->lock);
978 if (retval)
979 goto done;
David Brownell5b89db022007-01-16 22:56:26 -0800980
981 if (dev->state != STATE_DEV_SETUP) {
982 retval = -ECANCELED;
983 goto done;
984 }
985 dev->state = STATE_DEV_CONNECTED;
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (dev->setup_out_error)
988 retval = -EIO;
989 else {
990 len = min (len, (size_t)dev->req->actual);
Alan Stern7f850032017-09-21 16:12:01 -0400991 ++dev->udc_usage;
992 spin_unlock_irq(&dev->lock);
Skip Hansen997694d2006-09-01 15:26:27 -0700993 if (copy_to_user (buf, dev->req->buf, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 retval = -EFAULT;
Thomas Faber85b4b3c2012-03-02 09:41:50 +0100995 else
996 retval = len;
Alan Stern7f850032017-09-21 16:12:01 -0400997 spin_lock_irq(&dev->lock);
998 --dev->udc_usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 clean_req (dev->gadget->ep0, dev->req);
1000 /* NOTE userspace can't yet choose to stall */
1001 }
1002 }
1003 goto done;
1004 }
1005
1006 /* else normal: return event data */
1007 if (len < sizeof dev->event [0]) {
1008 retval = -EINVAL;
1009 goto done;
1010 }
1011 len -= len % sizeof (struct usb_gadgetfs_event);
1012 dev->usermode_setup = 1;
1013
1014scan:
1015 /* return queued events right away */
1016 if (dev->ev_next != 0) {
1017 unsigned i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 n = len / sizeof (struct usb_gadgetfs_event);
David Brownell0864c7a2007-01-16 22:53:58 -08001020 if (dev->ev_next < n)
1021 n = dev->ev_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
David Brownell0864c7a2007-01-16 22:53:58 -08001023 /* ep0 i/o has special semantics during STATE_DEV_SETUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 for (i = 0; i < n; i++) {
1025 if (dev->event [i].type == GADGETFS_SETUP) {
David Brownell0864c7a2007-01-16 22:53:58 -08001026 dev->state = STATE_DEV_SETUP;
1027 n = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 break;
1029 }
1030 }
1031 spin_unlock_irq (&dev->lock);
David Brownell0864c7a2007-01-16 22:53:58 -08001032 len = n * sizeof (struct usb_gadgetfs_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (copy_to_user (buf, &dev->event, len))
1034 retval = -EFAULT;
1035 else
1036 retval = len;
1037 if (len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* NOTE this doesn't guard against broken drivers;
1039 * concurrent ep0 readers may lose events.
1040 */
1041 spin_lock_irq (&dev->lock);
David Brownell0864c7a2007-01-16 22:53:58 -08001042 if (dev->ev_next > n) {
1043 memmove(&dev->event[0], &dev->event[n],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 sizeof (struct usb_gadgetfs_event)
David Brownell0864c7a2007-01-16 22:53:58 -08001045 * (dev->ev_next - n));
1046 }
1047 dev->ev_next -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 spin_unlock_irq (&dev->lock);
1049 }
1050 return retval;
1051 }
1052 if (fd->f_flags & O_NONBLOCK) {
1053 retval = -EAGAIN;
1054 goto done;
1055 }
1056
1057 switch (state) {
1058 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001059 DBG (dev, "fail %s, state %d\n", __func__, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 retval = -ESRCH;
1061 break;
David Brownell7489d142007-01-16 22:51:04 -08001062 case STATE_DEV_UNCONNECTED:
1063 case STATE_DEV_CONNECTED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_unlock_irq (&dev->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001065 DBG (dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 /* wait for events */
1068 retval = wait_event_interruptible (dev->wait,
1069 dev->ev_next != 0);
1070 if (retval < 0)
1071 return retval;
1072 spin_lock_irq (&dev->lock);
1073 goto scan;
1074 }
1075
1076done:
1077 spin_unlock_irq (&dev->lock);
1078 return retval;
1079}
1080
1081static struct usb_gadgetfs_event *
1082next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1083{
1084 struct usb_gadgetfs_event *event;
1085 unsigned i;
1086
1087 switch (type) {
1088 /* these events purge the queue */
1089 case GADGETFS_DISCONNECT:
David Brownell7489d142007-01-16 22:51:04 -08001090 if (dev->state == STATE_DEV_SETUP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 dev->setup_abort = 1;
1092 // FALL THROUGH
1093 case GADGETFS_CONNECT:
1094 dev->ev_next = 0;
1095 break;
1096 case GADGETFS_SETUP: /* previous request timed out */
1097 case GADGETFS_SUSPEND: /* same effect */
1098 /* these events can't be repeated */
1099 for (i = 0; i != dev->ev_next; i++) {
1100 if (dev->event [i].type != type)
1101 continue;
David Brownell0864c7a2007-01-16 22:53:58 -08001102 DBG(dev, "discard old event[%d] %d\n", i, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 dev->ev_next--;
1104 if (i == dev->ev_next)
1105 break;
1106 /* indices start at zero, for simplicity */
1107 memmove (&dev->event [i], &dev->event [i + 1],
1108 sizeof (struct usb_gadgetfs_event)
1109 * (dev->ev_next - i));
1110 }
1111 break;
1112 default:
1113 BUG ();
1114 }
David Brownell0864c7a2007-01-16 22:53:58 -08001115 VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 event = &dev->event [dev->ev_next++];
1117 BUG_ON (dev->ev_next > N_EVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 memset (event, 0, sizeof *event);
1119 event->type = type;
1120 return event;
1121}
1122
1123static ssize_t
1124ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1125{
1126 struct dev_data *dev = fd->private_data;
1127 ssize_t retval = -ESRCH;
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* report fd mode change before acting on it */
1130 if (dev->setup_abort) {
1131 dev->setup_abort = 0;
1132 retval = -EIDRM;
1133
1134 /* data and/or status stage for control request */
David Brownell7489d142007-01-16 22:51:04 -08001135 } else if (dev->state == STATE_DEV_SETUP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Alan Sternb9467772016-12-09 15:17:46 -05001137 len = min_t(size_t, len, dev->setup_wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (dev->setup_in) {
1139 retval = setup_req (dev->gadget->ep0, dev->req, len);
1140 if (retval == 0) {
David Brownell5b89db022007-01-16 22:56:26 -08001141 dev->state = STATE_DEV_CONNECTED;
Alan Sternfd5336c2017-09-21 13:23:58 -04001142 ++dev->udc_usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 spin_unlock_irq (&dev->lock);
1144 if (copy_from_user (dev->req->buf, buf, len))
1145 retval = -EFAULT;
Alan Stern97906362006-01-03 10:30:31 -05001146 else {
1147 if (len < dev->setup_wLength)
1148 dev->req->zero = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 retval = usb_ep_queue (
1150 dev->gadget->ep0, dev->req,
1151 GFP_KERNEL);
Alan Stern97906362006-01-03 10:30:31 -05001152 }
David Eccherb7bd98b2015-12-11 22:13:55 +01001153 spin_lock_irq(&dev->lock);
Alan Sternfd5336c2017-09-21 13:23:58 -04001154 --dev->udc_usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 clean_req (dev->gadget->ep0, dev->req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 } else
1158 retval = len;
1159
1160 return retval;
1161 }
1162
1163 /* can stall some OUT transfers */
1164 } else if (dev->setup_can_stall) {
1165 VDEBUG(dev, "ep0out stall\n");
1166 (void) usb_ep_set_halt (dev->gadget->ep0);
1167 retval = -EL2HLT;
David Brownell7489d142007-01-16 22:51:04 -08001168 dev->state = STATE_DEV_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 } else {
1170 DBG(dev, "bogus ep0out stall!\n");
1171 }
1172 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -08001173 DBG (dev, "fail %s, state %d\n", __func__, dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return retval;
1176}
1177
1178static int
1179ep0_fasync (int f, struct file *fd, int on)
1180{
1181 struct dev_data *dev = fd->private_data;
1182 // caller must F_SETOWN before signal delivery happens
Harvey Harrison441b62c2008-03-03 16:08:34 -08001183 VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return fasync_helper (f, fd, on, &dev->fasync);
1185}
1186
1187static struct usb_gadget_driver gadgetfs_driver;
1188
1189static int
1190dev_release (struct inode *inode, struct file *fd)
1191{
1192 struct dev_data *dev = fd->private_data;
1193
1194 /* closing ep0 === shutdown all */
1195
Alan Stern3ff5f4f2017-06-08 13:55:59 -04001196 if (dev->gadget_registered) {
Marek Szyprowski7b0a2712016-02-18 08:59:26 +01001197 usb_gadget_unregister_driver (&gadgetfs_driver);
Alan Stern3ff5f4f2017-06-08 13:55:59 -04001198 dev->gadget_registered = false;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /* at this point "good" hardware has disconnected the
1202 * device from USB; the host won't see it any more.
1203 * alternatively, all host requests will time out.
1204 */
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 kfree (dev->buf);
1207 dev->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Marcus Nutzingerf0cae932014-06-05 17:17:06 +02001209 /* other endpoints were all decoupled from this device */
1210 spin_lock_irq(&dev->lock);
1211 dev->state = STATE_DEV_DISABLED;
1212 spin_unlock_irq(&dev->lock);
1213
1214 put_dev (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return 0;
1216}
1217
Milan Svobodae22fc272006-08-08 22:23:12 -07001218static unsigned int
1219ep0_poll (struct file *fd, poll_table *wait)
1220{
1221 struct dev_data *dev = fd->private_data;
1222 int mask = 0;
1223
Alan Stern96b62a52015-03-04 10:31:50 -05001224 if (dev->state <= STATE_DEV_OPENED)
1225 return DEFAULT_POLLMASK;
1226
Milan Svobodae22fc272006-08-08 22:23:12 -07001227 poll_wait(fd, &dev->wait, wait);
1228
1229 spin_lock_irq (&dev->lock);
1230
1231 /* report fd mode change before acting on it */
1232 if (dev->setup_abort) {
1233 dev->setup_abort = 0;
1234 mask = POLLHUP;
1235 goto out;
1236 }
1237
David Brownell7489d142007-01-16 22:51:04 -08001238 if (dev->state == STATE_DEV_SETUP) {
Milan Svobodae22fc272006-08-08 22:23:12 -07001239 if (dev->setup_in || dev->setup_can_stall)
1240 mask = POLLOUT;
1241 } else {
1242 if (dev->ev_next != 0)
1243 mask = POLLIN;
1244 }
1245out:
1246 spin_unlock_irq(&dev->lock);
1247 return mask;
1248}
1249
Alan Cox44c389a2008-05-22 22:03:27 +01001250static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 struct dev_data *dev = fd->private_data;
1253 struct usb_gadget *gadget = dev->gadget;
Alan Cox44c389a2008-05-22 22:03:27 +01001254 long ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Alan Sternfd5336c2017-09-21 13:23:58 -04001256 spin_lock_irq(&dev->lock);
1257 if (dev->state == STATE_DEV_OPENED ||
1258 dev->state == STATE_DEV_UNBOUND) {
1259 /* Not bound to a UDC */
1260 } else if (gadget->ops->ioctl) {
1261 ++dev->udc_usage;
1262 spin_unlock_irq(&dev->lock);
1263
Alan Cox44c389a2008-05-22 22:03:27 +01001264 ret = gadget->ops->ioctl (gadget, code, value);
Arnd Bergmann1548b132010-06-01 23:04:44 +02001265
Alan Sternfd5336c2017-09-21 13:23:58 -04001266 spin_lock_irq(&dev->lock);
1267 --dev->udc_usage;
1268 }
1269 spin_unlock_irq(&dev->lock);
1270
Alan Cox44c389a2008-05-22 22:03:27 +01001271 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/*----------------------------------------------------------------------*/
1275
1276/* The in-kernel gadget driver handles most ep0 issues, in particular
1277 * enumerating the single configuration (as provided from user space).
1278 *
1279 * Unrecognized ep0 requests may be handled in user space.
1280 */
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282static void make_qualifier (struct dev_data *dev)
1283{
1284 struct usb_qualifier_descriptor qual;
1285 struct usb_device_descriptor *desc;
1286
1287 qual.bLength = sizeof qual;
1288 qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
Harvey Harrison551509d2009-02-11 14:11:36 -08001289 qual.bcdUSB = cpu_to_le16 (0x0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 desc = dev->dev;
1292 qual.bDeviceClass = desc->bDeviceClass;
1293 qual.bDeviceSubClass = desc->bDeviceSubClass;
1294 qual.bDeviceProtocol = desc->bDeviceProtocol;
1295
1296 /* assumes ep0 uses the same value for both speeds ... */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +02001297 qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 qual.bNumConfigurations = 1;
1300 qual.bRESERVED = 0;
1301
1302 memcpy (dev->rbuf, &qual, sizeof qual);
1303}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305static int
1306config_buf (struct dev_data *dev, u8 type, unsigned index)
1307{
1308 int len;
David Brownella4e3ef52007-08-01 23:58:22 -07001309 int hs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /* only one configuration */
1312 if (index > 0)
1313 return -EINVAL;
1314
David Brownella4e3ef52007-08-01 23:58:22 -07001315 if (gadget_is_dualspeed(dev->gadget)) {
1316 hs = (dev->gadget->speed == USB_SPEED_HIGH);
1317 if (type == USB_DT_OTHER_SPEED_CONFIG)
1318 hs = !hs;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (hs) {
1321 dev->req->buf = dev->hs_config;
David Brownell01ee7d72007-05-25 20:40:14 -07001322 len = le16_to_cpu(dev->hs_config->wTotalLength);
David Brownella4e3ef52007-08-01 23:58:22 -07001323 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 dev->req->buf = dev->config;
David Brownell01ee7d72007-05-25 20:40:14 -07001325 len = le16_to_cpu(dev->config->wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 ((u8 *)dev->req->buf) [1] = type;
1328 return len;
1329}
1330
1331static int
1332gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1333{
1334 struct dev_data *dev = get_gadget_data (gadget);
1335 struct usb_request *req = dev->req;
1336 int value = -EOPNOTSUPP;
1337 struct usb_gadgetfs_event *event;
David Brownell1bbc1692005-05-07 13:05:13 -07001338 u16 w_value = le16_to_cpu(ctrl->wValue);
1339 u16 w_length = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Greg Kroah-Hartman33dd9bc2021-12-09 18:59:27 +01001341 if (w_length > RBUF_SIZE) {
Greg Kroah-Hartman55b65fd2021-12-14 19:46:21 +01001342 if (ctrl->bRequestType & USB_DIR_IN) {
Greg Kroah-Hartman33dd9bc2021-12-09 18:59:27 +01001343 /* Cast away the const, we are going to overwrite on purpose. */
1344 __le16 *temp = (__le16 *)&ctrl->wLength;
1345
1346 *temp = cpu_to_le16(RBUF_SIZE);
1347 w_length = RBUF_SIZE;
Greg Kroah-Hartman55b65fd2021-12-14 19:46:21 +01001348 } else {
1349 return value;
Greg Kroah-Hartman33dd9bc2021-12-09 18:59:27 +01001350 }
1351 }
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 spin_lock (&dev->lock);
1354 dev->setup_abort = 0;
David Brownell7489d142007-01-16 22:51:04 -08001355 if (dev->state == STATE_DEV_UNCONNECTED) {
David Brownella4e3ef52007-08-01 23:58:22 -07001356 if (gadget_is_dualspeed(gadget)
1357 && gadget->speed == USB_SPEED_HIGH
1358 && dev->hs_config == NULL) {
David Brownellce467942007-01-16 23:06:07 -08001359 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 ERROR (dev, "no high speed config??\n");
1361 return -EINVAL;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
David Brownellce467942007-01-16 23:06:07 -08001364 dev->state = STATE_DEV_CONNECTED;
David Brownellce467942007-01-16 23:06:07 -08001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 INFO (dev, "connected\n");
1367 event = next_event (dev, GADGETFS_CONNECT);
1368 event->u.speed = gadget->speed;
1369 ep0_readable (dev);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /* host may have given up waiting for response. we can miss control
1372 * requests handled lower down (device/endpoint status and features);
1373 * then ep0_{read,write} will report the wrong status. controller
1374 * driver will have aborted pending i/o.
1375 */
David Brownell7489d142007-01-16 22:51:04 -08001376 } else if (dev->state == STATE_DEV_SETUP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 dev->setup_abort = 1;
1378
1379 req->buf = dev->rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 req->context = NULL;
1381 value = -EOPNOTSUPP;
1382 switch (ctrl->bRequest) {
1383
1384 case USB_REQ_GET_DESCRIPTOR:
1385 if (ctrl->bRequestType != USB_DIR_IN)
1386 goto unrecognized;
1387 switch (w_value >> 8) {
1388
1389 case USB_DT_DEVICE:
1390 value = min (w_length, (u16) sizeof *dev->dev);
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +02001391 dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 req->buf = dev->dev;
1393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 case USB_DT_DEVICE_QUALIFIER:
1395 if (!dev->hs_config)
1396 break;
1397 value = min (w_length, (u16)
1398 sizeof (struct usb_qualifier_descriptor));
1399 make_qualifier (dev);
1400 break;
1401 case USB_DT_OTHER_SPEED_CONFIG:
1402 // FALLTHROUGH
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 case USB_DT_CONFIG:
1404 value = config_buf (dev,
1405 w_value >> 8,
1406 w_value & 0xff);
1407 if (value >= 0)
1408 value = min (w_length, (u16) value);
1409 break;
1410 case USB_DT_STRING:
1411 goto unrecognized;
1412
1413 default: // all others are errors
1414 break;
1415 }
1416 break;
1417
1418 /* currently one config, two speeds */
1419 case USB_REQ_SET_CONFIGURATION:
1420 if (ctrl->bRequestType != 0)
Roy Hashimoto12cd5b92008-03-12 13:55:31 -08001421 goto unrecognized;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (0 == (u8) w_value) {
1423 value = 0;
1424 dev->current_config = 0;
1425 usb_gadget_vbus_draw(gadget, 8 /* mA */ );
1426 // user mode expected to disable endpoints
1427 } else {
1428 u8 config, power;
David Brownella4e3ef52007-08-01 23:58:22 -07001429
1430 if (gadget_is_dualspeed(gadget)
1431 && gadget->speed == USB_SPEED_HIGH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 config = dev->hs_config->bConfigurationValue;
1433 power = dev->hs_config->bMaxPower;
David Brownella4e3ef52007-08-01 23:58:22 -07001434 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 config = dev->config->bConfigurationValue;
1436 power = dev->config->bMaxPower;
1437 }
1438
1439 if (config == (u8) w_value) {
1440 value = 0;
1441 dev->current_config = config;
1442 usb_gadget_vbus_draw(gadget, 2 * power);
1443 }
1444 }
1445
1446 /* report SET_CONFIGURATION like any other control request,
1447 * except that usermode may not stall this. the next
1448 * request mustn't be allowed start until this finishes:
1449 * endpoints and threads set up, etc.
1450 *
1451 * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
1452 * has bad/racey automagic that prevents synchronizing here.
1453 * even kernel mode drivers often miss them.
1454 */
1455 if (value == 0) {
1456 INFO (dev, "configuration #%d\n", dev->current_config);
Peter Chen6027f312014-04-29 13:26:28 +08001457 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (dev->usermode_setup) {
1459 dev->setup_can_stall = 0;
1460 goto delegate;
1461 }
1462 }
1463 break;
1464
Paul Bolled30f2062014-05-26 23:37:09 +02001465#ifndef CONFIG_USB_PXA25X
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /* PXA automagically handles this request too */
1467 case USB_REQ_GET_CONFIGURATION:
1468 if (ctrl->bRequestType != 0x80)
Roy Hashimoto12cd5b92008-03-12 13:55:31 -08001469 goto unrecognized;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 *(u8 *)req->buf = dev->current_config;
1471 value = min (w_length, (u16) 1);
1472 break;
1473#endif
1474
1475 default:
1476unrecognized:
1477 VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
1478 dev->usermode_setup ? "delegate" : "fail",
1479 ctrl->bRequestType, ctrl->bRequest,
1480 w_value, le16_to_cpu(ctrl->wIndex), w_length);
1481
1482 /* if there's an ep0 reader, don't stall */
1483 if (dev->usermode_setup) {
1484 dev->setup_can_stall = 1;
1485delegate:
1486 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
1487 ? 1 : 0;
Alan Stern97906362006-01-03 10:30:31 -05001488 dev->setup_wLength = w_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 dev->setup_out_ready = 0;
1490 dev->setup_out_error = 0;
1491 value = 0;
1492
1493 /* read DATA stage for OUT right away */
1494 if (unlikely (!dev->setup_in && w_length)) {
1495 value = setup_req (gadget->ep0, dev->req,
1496 w_length);
1497 if (value < 0)
1498 break;
Bin Liud246dcb2016-05-26 11:43:45 -05001499
Alan Sternfd5336c2017-09-21 13:23:58 -04001500 ++dev->udc_usage;
Bin Liud246dcb2016-05-26 11:43:45 -05001501 spin_unlock (&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 value = usb_ep_queue (gadget->ep0, dev->req,
Bin Liud246dcb2016-05-26 11:43:45 -05001503 GFP_KERNEL);
1504 spin_lock (&dev->lock);
Alan Sternfd5336c2017-09-21 13:23:58 -04001505 --dev->udc_usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (value < 0) {
1507 clean_req (gadget->ep0, dev->req);
1508 break;
1509 }
1510
1511 /* we can't currently stall these */
1512 dev->setup_can_stall = 0;
1513 }
1514
1515 /* state changes when reader collects event */
1516 event = next_event (dev, GADGETFS_SETUP);
1517 event->u.setup = *ctrl;
1518 ep0_readable (dev);
1519 spin_unlock (&dev->lock);
1520 return 0;
1521 }
1522 }
1523
1524 /* proceed with data transfer and status phases? */
David Brownell7489d142007-01-16 22:51:04 -08001525 if (value >= 0 && dev->state != STATE_DEV_SETUP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 req->length = value;
1527 req->zero = value < w_length;
Bin Liud246dcb2016-05-26 11:43:45 -05001528
Alan Sternfd5336c2017-09-21 13:23:58 -04001529 ++dev->udc_usage;
Bin Liud246dcb2016-05-26 11:43:45 -05001530 spin_unlock (&dev->lock);
1531 value = usb_ep_queue (gadget->ep0, req, GFP_KERNEL);
Alan Sternfd5336c2017-09-21 13:23:58 -04001532 spin_lock(&dev->lock);
1533 --dev->udc_usage;
1534 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (value < 0) {
1536 DBG (dev, "ep_queue --> %d\n", value);
1537 req->status = 0;
1538 }
Bin Liud246dcb2016-05-26 11:43:45 -05001539 return value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541
1542 /* device stalls when value < 0 */
1543 spin_unlock (&dev->lock);
1544 return value;
1545}
1546
1547static void destroy_ep_files (struct dev_data *dev)
1548{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001549 DBG (dev, "%s %d\n", __func__, dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* dev->state must prevent interference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 spin_lock_irq (&dev->lock);
Al Viro104bb372012-01-08 16:13:28 -05001553 while (!list_empty(&dev->epfiles)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 struct ep_data *ep;
1555 struct inode *parent;
1556 struct dentry *dentry;
1557
1558 /* break link to FS */
Al Viro104bb372012-01-08 16:13:28 -05001559 ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 list_del_init (&ep->epfiles);
Alan Sternfd5336c2017-09-21 13:23:58 -04001561 spin_unlock_irq (&dev->lock);
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 dentry = ep->dentry;
1564 ep->dentry = NULL;
David Howells75c3cfa2015-03-17 22:26:12 +00001565 parent = d_inode(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 /* break link to controller */
Alan Sternfd5336c2017-09-21 13:23:58 -04001568 mutex_lock(&ep->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (ep->state == STATE_EP_ENABLED)
1570 (void) usb_ep_disable (ep->ep);
1571 ep->state = STATE_EP_UNBOUND;
1572 usb_ep_free_request (ep->ep, ep->req);
1573 ep->ep = NULL;
Alan Sternfd5336c2017-09-21 13:23:58 -04001574 mutex_unlock(&ep->lock);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 wake_up (&ep->wait);
1577 put_ep (ep);
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* break link to dcache */
Al Viro59551022016-01-22 15:40:57 -05001580 inode_lock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 d_delete (dentry);
1582 dput (dentry);
Al Viro59551022016-01-22 15:40:57 -05001583 inode_unlock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Al Viro104bb372012-01-08 16:13:28 -05001585 spin_lock_irq (&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 spin_unlock_irq (&dev->lock);
1588}
1589
1590
Al Virofb6c3222014-09-03 13:37:56 -04001591static struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592gadgetfs_create_file (struct super_block *sb, char const *name,
Al Virofb6c3222014-09-03 13:37:56 -04001593 void *data, const struct file_operations *fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595static int activate_ep_files (struct dev_data *dev)
1596{
1597 struct usb_ep *ep;
Alan Stern0ae4ea82006-05-22 12:27:38 -04001598 struct ep_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 gadget_for_each_ep (ep, dev->gadget) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Eric Sesterhenn7039f422006-02-27 13:34:10 -08001602 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (!data)
Alan Stern0ae4ea82006-05-22 12:27:38 -04001604 goto enomem0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 data->state = STATE_EP_DISABLED;
Thomas Gleixnera79df502010-01-29 20:38:59 +00001606 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 init_waitqueue_head (&data->wait);
1608
1609 strncpy (data->name, ep->name, sizeof (data->name) - 1);
1610 atomic_set (&data->count, 1);
1611 data->dev = dev;
1612 get_dev (dev);
1613
1614 data->ep = ep;
1615 ep->driver_data = data;
1616
1617 data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
1618 if (!data->req)
Alan Stern0ae4ea82006-05-22 12:27:38 -04001619 goto enomem1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Al Virofb6c3222014-09-03 13:37:56 -04001621 data->dentry = gadgetfs_create_file (dev->sb, data->name,
Al Virod4461a62015-03-03 08:39:34 +00001622 data, &ep_io_operations);
Al Virofb6c3222014-09-03 13:37:56 -04001623 if (!data->dentry)
Alan Stern0ae4ea82006-05-22 12:27:38 -04001624 goto enomem2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 list_add_tail (&data->epfiles, &dev->epfiles);
1626 }
1627 return 0;
1628
Alan Stern0ae4ea82006-05-22 12:27:38 -04001629enomem2:
1630 usb_ep_free_request (ep, data->req);
1631enomem1:
1632 put_dev (dev);
1633 kfree (data);
1634enomem0:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001635 DBG (dev, "%s enomem\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 destroy_ep_files (dev);
1637 return -ENOMEM;
1638}
1639
1640static void
1641gadgetfs_unbind (struct usb_gadget *gadget)
1642{
1643 struct dev_data *dev = get_gadget_data (gadget);
1644
Harvey Harrison441b62c2008-03-03 16:08:34 -08001645 DBG (dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 spin_lock_irq (&dev->lock);
1648 dev->state = STATE_DEV_UNBOUND;
Alan Sternfd5336c2017-09-21 13:23:58 -04001649 while (dev->udc_usage > 0) {
1650 spin_unlock_irq(&dev->lock);
1651 usleep_range(1000, 2000);
1652 spin_lock_irq(&dev->lock);
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 spin_unlock_irq (&dev->lock);
1655
1656 destroy_ep_files (dev);
1657 gadget->ep0->driver_data = NULL;
1658 set_gadget_data (gadget, NULL);
1659
1660 /* we've already been disconnected ... no i/o is active */
1661 if (dev->req)
1662 usb_ep_free_request (gadget->ep0, dev->req);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001663 DBG (dev, "%s done\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 put_dev (dev);
1665}
1666
1667static struct dev_data *the_device;
1668
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001669static int gadgetfs_bind(struct usb_gadget *gadget,
1670 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct dev_data *dev = the_device;
1673
1674 if (!dev)
1675 return -ESRCH;
1676 if (0 != strcmp (CHIP, gadget->name)) {
David Brownell00274922007-11-19 12:58:36 -08001677 pr_err("%s expected %s controller not %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 shortname, CHIP, gadget->name);
1679 return -ENODEV;
1680 }
1681
1682 set_gadget_data (gadget, dev);
1683 dev->gadget = gadget;
1684 gadget->ep0->driver_data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 /* preallocate control response and buffer */
1687 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1688 if (!dev->req)
1689 goto enomem;
1690 dev->req->context = NULL;
1691 dev->req->complete = epio_complete;
1692
1693 if (activate_ep_files (dev) < 0)
1694 goto enomem;
1695
1696 INFO (dev, "bound to %s driver\n", gadget->name);
David Brownell7489d142007-01-16 22:51:04 -08001697 spin_lock_irq(&dev->lock);
1698 dev->state = STATE_DEV_UNCONNECTED;
1699 spin_unlock_irq(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 get_dev (dev);
1701 return 0;
1702
1703enomem:
1704 gadgetfs_unbind (gadget);
1705 return -ENOMEM;
1706}
1707
1708static void
1709gadgetfs_disconnect (struct usb_gadget *gadget)
1710{
1711 struct dev_data *dev = get_gadget_data (gadget);
Marc Kleine-Budde001428e2011-10-10 18:38:05 +02001712 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Marc Kleine-Budde001428e2011-10-10 18:38:05 +02001714 spin_lock_irqsave (&dev->lock, flags);
David Brownell7489d142007-01-16 22:51:04 -08001715 if (dev->state == STATE_DEV_UNCONNECTED)
Milan Svoboda07cb7f22006-06-26 07:46:00 -07001716 goto exit;
David Brownell7489d142007-01-16 22:51:04 -08001717 dev->state = STATE_DEV_UNCONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 INFO (dev, "disconnected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 next_event (dev, GADGETFS_DISCONNECT);
1721 ep0_readable (dev);
Milan Svoboda07cb7f22006-06-26 07:46:00 -07001722exit:
Marc Kleine-Budde001428e2011-10-10 18:38:05 +02001723 spin_unlock_irqrestore (&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726static void
1727gadgetfs_suspend (struct usb_gadget *gadget)
1728{
1729 struct dev_data *dev = get_gadget_data (gadget);
Alan Stern0c0d3d82017-06-13 15:23:42 -04001730 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 INFO (dev, "suspended from state %d\n", dev->state);
Alan Stern0c0d3d82017-06-13 15:23:42 -04001733 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 switch (dev->state) {
David Brownell7489d142007-01-16 22:51:04 -08001735 case STATE_DEV_SETUP: // VERY odd... host died??
1736 case STATE_DEV_CONNECTED:
1737 case STATE_DEV_UNCONNECTED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 next_event (dev, GADGETFS_SUSPEND);
1739 ep0_readable (dev);
1740 /* FALLTHROUGH */
1741 default:
1742 break;
1743 }
Alan Stern0c0d3d82017-06-13 15:23:42 -04001744 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747static struct usb_gadget_driver gadgetfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 .function = (char *) driver_desc,
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001749 .bind = gadgetfs_bind,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 .unbind = gadgetfs_unbind,
1751 .setup = gadgetfs_setup,
Peter Chen0eba4552014-09-09 08:56:51 +08001752 .reset = gadgetfs_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 .disconnect = gadgetfs_disconnect,
1754 .suspend = gadgetfs_suspend,
1755
David Brownell25051072007-01-15 11:30:28 -08001756 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 .name = (char *) shortname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 },
1759};
1760
1761/*----------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762/* DEVICE INITIALIZATION
1763 *
1764 * fd = open ("/dev/gadget/$CHIP", O_RDWR)
1765 * status = write (fd, descriptors, sizeof descriptors)
1766 *
1767 * That write establishes the device configuration, so the kernel can
1768 * bind to the controller ... guaranteeing it can handle enumeration
1769 * at all necessary speeds. Descriptor order is:
1770 *
1771 * . message tag (u32, host order) ... for now, must be zero; it
1772 * would change to support features like multi-config devices
1773 * . full/low speed config ... all wTotalLength bytes (with interface,
1774 * class, altsetting, endpoint, and other descriptors)
1775 * . high speed config ... all descriptors, for high speed operation;
David Brownell25051072007-01-15 11:30:28 -08001776 * this one's optional except for high-speed hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 * . device descriptor
1778 *
Phil Endecott511779f2007-01-15 11:35:01 -08001779 * Endpoints are not yet enabled. Drivers must wait until device
1780 * configuration and interface altsetting changes create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 * the need to configure (or unconfigure) them.
1782 *
1783 * After initialization, the device stays active for as long as that
Phil Endecott511779f2007-01-15 11:35:01 -08001784 * $CHIP file is open. Events must then be read from that descriptor,
1785 * such as configuration notifications.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 */
1787
Alan Sternda4543b2016-12-09 15:24:24 -05001788static int is_valid_config(struct usb_config_descriptor *config,
1789 unsigned int total)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 return config->bDescriptorType == USB_DT_CONFIG
1792 && config->bLength == USB_DT_CONFIG_SIZE
Alan Sternda4543b2016-12-09 15:24:24 -05001793 && total >= USB_DT_CONFIG_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 && config->bConfigurationValue != 0
1795 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
1796 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
1797 /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
1798 /* FIXME check lengths: walk to end */
1799}
1800
1801static ssize_t
1802dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1803{
1804 struct dev_data *dev = fd->private_data;
1805 ssize_t value = len, length = len;
1806 unsigned total;
1807 u32 tag;
1808 char *kbuf;
1809
Alan Stern96b62a52015-03-04 10:31:50 -05001810 spin_lock_irq(&dev->lock);
1811 if (dev->state > STATE_DEV_OPENED) {
1812 value = ep0_write(fd, buf, len, ptr);
1813 spin_unlock_irq(&dev->lock);
1814 return value;
1815 }
1816 spin_unlock_irq(&dev->lock);
1817
Greg Kroah-Hartman404954e2016-12-06 08:36:29 +01001818 if ((len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4)) ||
1819 (len > PAGE_SIZE * 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -EINVAL;
1821
1822 /* we might need to change message format someday */
1823 if (copy_from_user (&tag, buf, 4))
1824 return -EFAULT;
1825 if (tag != 0)
1826 return -EINVAL;
1827 buf += 4;
1828 length -= 4;
1829
Julia Lawallbe8a0582010-05-22 10:26:22 +02001830 kbuf = memdup_user(buf, length);
1831 if (IS_ERR(kbuf))
1832 return PTR_ERR(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 spin_lock_irq (&dev->lock);
1835 value = -EINVAL;
Christophe JAILLET8fe9ea82017-02-21 22:33:11 +01001836 if (dev->buf) {
1837 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 goto fail;
Christophe JAILLET8fe9ea82017-02-21 22:33:11 +01001839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 dev->buf = kbuf;
1841
1842 /* full or low speed config */
1843 dev->config = (void *) kbuf;
David Brownell01ee7d72007-05-25 20:40:14 -07001844 total = le16_to_cpu(dev->config->wTotalLength);
Alan Sternda4543b2016-12-09 15:24:24 -05001845 if (!is_valid_config(dev->config, total) ||
1846 total > length - USB_DT_DEVICE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto fail;
1848 kbuf += total;
1849 length -= total;
1850
1851 /* optional high speed config */
1852 if (kbuf [1] == USB_DT_CONFIG) {
1853 dev->hs_config = (void *) kbuf;
David Brownell01ee7d72007-05-25 20:40:14 -07001854 total = le16_to_cpu(dev->hs_config->wTotalLength);
Alan Sternda4543b2016-12-09 15:24:24 -05001855 if (!is_valid_config(dev->hs_config, total) ||
1856 total > length - USB_DT_DEVICE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto fail;
1858 kbuf += total;
1859 length -= total;
Alan Stern46427c22016-12-09 15:18:43 -05001860 } else {
1861 dev->hs_config = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863
1864 /* could support multiple configs, using another encoding! */
1865
1866 /* device descriptor (tweaked for paranoia) */
1867 if (length != USB_DT_DEVICE_SIZE)
1868 goto fail;
1869 dev->dev = (void *)kbuf;
1870 if (dev->dev->bLength != USB_DT_DEVICE_SIZE
1871 || dev->dev->bDescriptorType != USB_DT_DEVICE
1872 || dev->dev->bNumConfigurations != 1)
1873 goto fail;
1874 dev->dev->bNumConfigurations = 1;
Harvey Harrison551509d2009-02-11 14:11:36 -08001875 dev->dev->bcdUSB = cpu_to_le16 (0x0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* triggers gadgetfs_bind(); then we can enumerate. */
1878 spin_unlock_irq (&dev->lock);
Michal Nazarewicz85b86142012-08-24 20:46:18 +02001879 if (dev->hs_config)
1880 gadgetfs_driver.max_speed = USB_SPEED_HIGH;
1881 else
1882 gadgetfs_driver.max_speed = USB_SPEED_FULL;
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001883
1884 value = usb_gadget_probe_driver(&gadgetfs_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (value != 0) {
1886 kfree (dev->buf);
1887 dev->buf = NULL;
1888 } else {
1889 /* at this point "good" hardware has for the first time
1890 * let the USB the host see us. alternatively, if users
1891 * unplug/replug that will clear all the error state.
1892 *
1893 * note: everything running before here was guaranteed
1894 * to choke driver model style diagnostics. from here
1895 * on, they can work ... except in cleanup paths that
1896 * kick in after the ep0 descriptor is closed.
1897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 value = len;
Marek Szyprowski7b0a2712016-02-18 08:59:26 +01001899 dev->gadget_registered = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901 return value;
1902
1903fail:
1904 spin_unlock_irq (&dev->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001905 pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 kfree (dev->buf);
1907 dev->buf = NULL;
1908 return value;
1909}
1910
1911static int
1912dev_open (struct inode *inode, struct file *fd)
1913{
Theodore Ts'o8e18e292006-09-27 01:50:46 -07001914 struct dev_data *dev = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 int value = -EBUSY;
1916
David Brownell7489d142007-01-16 22:51:04 -08001917 spin_lock_irq(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (dev->state == STATE_DEV_DISABLED) {
1919 dev->ev_next = 0;
David Brownell7489d142007-01-16 22:51:04 -08001920 dev->state = STATE_DEV_OPENED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 fd->private_data = dev;
1922 get_dev (dev);
1923 value = 0;
1924 }
David Brownell7489d142007-01-16 22:51:04 -08001925 spin_unlock_irq(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return value;
1927}
1928
Alan Stern96b62a52015-03-04 10:31:50 -05001929static const struct file_operations ep0_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 .llseek = no_llseek,
1931
1932 .open = dev_open,
Alan Stern96b62a52015-03-04 10:31:50 -05001933 .read = ep0_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 .write = dev_config,
1935 .fasync = ep0_fasync,
Alan Stern96b62a52015-03-04 10:31:50 -05001936 .poll = ep0_poll,
Alan Cox44c389a2008-05-22 22:03:27 +01001937 .unlocked_ioctl = dev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 .release = dev_release,
1939};
1940
1941/*----------------------------------------------------------------------*/
1942
1943/* FILESYSTEM AND SUPERBLOCK OPERATIONS
1944 *
1945 * Mounting the filesystem creates a controller file, used first for
1946 * device configuration then later for event monitoring.
1947 */
1948
1949
1950/* FIXME PAM etc could set this security policy without mount options
1951 * if epfiles inherited ownership and permissons from ep0 ...
1952 */
1953
1954static unsigned default_uid;
1955static unsigned default_gid;
1956static unsigned default_perm = S_IRUSR | S_IWUSR;
1957
1958module_param (default_uid, uint, 0644);
1959module_param (default_gid, uint, 0644);
1960module_param (default_perm, uint, 0644);
1961
1962
1963static struct inode *
1964gadgetfs_make_inode (struct super_block *sb,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001965 void *data, const struct file_operations *fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int mode)
1967{
1968 struct inode *inode = new_inode (sb);
1969
1970 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001971 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 inode->i_mode = mode;
Eric W. Biederman32d639c2012-02-07 16:32:04 -08001973 inode->i_uid = make_kuid(&init_user_ns, default_uid);
1974 inode->i_gid = make_kgid(&init_user_ns, default_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 inode->i_atime = inode->i_mtime = inode->i_ctime
Deepa Dinamani078cd822016-09-14 07:48:04 -07001976 = current_time(inode);
Theodore Ts'o8e18e292006-09-27 01:50:46 -07001977 inode->i_private = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 inode->i_fop = fops;
1979 }
1980 return inode;
1981}
1982
1983/* creates in fs root directory, so non-renamable and non-linkable.
1984 * so inode and dentry are paired, until device reconfig.
1985 */
Al Virofb6c3222014-09-03 13:37:56 -04001986static struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987gadgetfs_create_file (struct super_block *sb, char const *name,
Al Virofb6c3222014-09-03 13:37:56 -04001988 void *data, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 struct dentry *dentry;
1991 struct inode *inode;
1992
1993 dentry = d_alloc_name(sb->s_root, name);
1994 if (!dentry)
1995 return NULL;
1996
1997 inode = gadgetfs_make_inode (sb, data, fops,
1998 S_IFREG | (default_perm & S_IRWXUGO));
1999 if (!inode) {
2000 dput(dentry);
2001 return NULL;
2002 }
2003 d_add (dentry, inode);
Al Virofb6c3222014-09-03 13:37:56 -04002004 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07002007static const struct super_operations gadget_fs_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 .statfs = simple_statfs,
2009 .drop_inode = generic_delete_inode,
2010};
2011
2012static int
2013gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
2014{
2015 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 struct dev_data *dev;
2017
2018 if (the_device)
2019 return -ESRCH;
2020
Marek Szyprowski175f7122016-02-18 11:34:43 +01002021 CHIP = usb_get_gadget_udc_name();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (!CHIP)
2023 return -ENODEV;
2024
2025 /* superblock */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002026 sb->s_blocksize = PAGE_SIZE;
2027 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 sb->s_magic = GADGETFS_MAGIC;
2029 sb->s_op = &gadget_fs_operations;
2030 sb->s_time_gran = 1;
2031
2032 /* root inode */
2033 inode = gadgetfs_make_inode (sb,
2034 NULL, &simple_dir_operations,
2035 S_IFDIR | S_IRUGO | S_IXUGO);
2036 if (!inode)
Al Viro87da5b32012-01-08 15:59:45 -05002037 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 inode->i_op = &simple_dir_inode_operations;
Al Viro48fde702012-01-08 22:15:13 -05002039 if (!(sb->s_root = d_make_root (inode)))
Al Viro87da5b32012-01-08 15:59:45 -05002040 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* the ep0 file is named after the controller we expect;
2043 * user mode code can use it for sanity checks, like we do.
2044 */
2045 dev = dev_new ();
2046 if (!dev)
Al Viro87da5b32012-01-08 15:59:45 -05002047 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 dev->sb = sb;
Alan Stern96b62a52015-03-04 10:31:50 -05002050 dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &ep0_operations);
Al Virofb6c3222014-09-03 13:37:56 -04002051 if (!dev->dentry) {
Al Viro87da5b32012-01-08 15:59:45 -05002052 put_dev(dev);
2053 goto Enomem;
2054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 /* other endpoint files are available after hardware setup,
2057 * from binding to a controller.
2058 */
2059 the_device = dev;
2060 return 0;
Alan Stern0ae4ea82006-05-22 12:27:38 -04002061
Al Viro87da5b32012-01-08 15:59:45 -05002062Enomem:
Alan Stern0ae4ea82006-05-22 12:27:38 -04002063 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066/* "mount -t gadgetfs path /dev/gadget" ends up here */
Al Virofc14f2f2010-07-25 01:48:30 +04002067static struct dentry *
2068gadgetfs_mount (struct file_system_type *t, int flags,
2069 const char *path, void *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Al Virofc14f2f2010-07-25 01:48:30 +04002071 return mount_single (t, flags, opts, gadgetfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074static void
2075gadgetfs_kill_sb (struct super_block *sb)
2076{
2077 kill_litter_super (sb);
2078 if (the_device) {
2079 put_dev (the_device);
2080 the_device = NULL;
2081 }
Marek Szyprowski175f7122016-02-18 11:34:43 +01002082 kfree(CHIP);
2083 CHIP = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
2086/*----------------------------------------------------------------------*/
2087
2088static struct file_system_type gadgetfs_type = {
2089 .owner = THIS_MODULE,
2090 .name = shortname,
Al Virofc14f2f2010-07-25 01:48:30 +04002091 .mount = gadgetfs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 .kill_sb = gadgetfs_kill_sb,
2093};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002094MODULE_ALIAS_FS("gadgetfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096/*----------------------------------------------------------------------*/
2097
2098static int __init init (void)
2099{
2100 int status;
2101
2102 status = register_filesystem (&gadgetfs_type);
2103 if (status == 0)
2104 pr_info ("%s: %s, version " DRIVER_VERSION "\n",
2105 shortname, driver_desc);
2106 return status;
2107}
2108module_init (init);
2109
2110static void __exit cleanup (void)
2111{
2112 pr_debug ("unregister %s\n", shortname);
2113 unregister_filesystem (&gadgetfs_type);
2114}
2115module_exit (cleanup);
2116