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