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