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