/kernel/2.6.32_froyo_photon_nightly/drivers/usb/gadget/inode.c

http://photon-android.googlecode.com/ · C · 2151 lines · 1556 code · 302 blank · 293 comment · 309 complexity · 9421a9b1e54ff2b6b64766c3a8da5fda MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * inode.c -- user mode filesystem api for usb gadget controllers
  3. *
  4. * Copyright (C) 2003-2004 David Brownell
  5. * Copyright (C) 2003 Agilent Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* #define VERBOSE_DEBUG */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/fs.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/uts.h>
  27. #include <linux/wait.h>
  28. #include <linux/compiler.h>
  29. #include <asm/uaccess.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/poll.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/device.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/usb/gadgetfs.h>
  37. #include <linux/usb/gadget.h>
  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. *
  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).
  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. #define DRIVER_DESC "USB Gadget filesystem"
  68. #define DRIVER_VERSION "24 Aug 2004"
  69. static const char driver_desc [] = DRIVER_DESC;
  70. static const char shortname [] = "gadgetfs";
  71. MODULE_DESCRIPTION (DRIVER_DESC);
  72. MODULE_AUTHOR ("David Brownell");
  73. MODULE_LICENSE ("GPL");
  74. /*----------------------------------------------------------------------*/
  75. #define GADGETFS_MAGIC 0xaee71ee7
  76. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  77. /* /dev/gadget/$CHIP represents ep0 and the whole device */
  78. enum ep0_state {
  79. /* DISBLED is the initial state.
  80. */
  81. STATE_DEV_DISABLED = 0,
  82. /* Only one open() of /dev/gadget/$CHIP; only one file tracks
  83. * ep0/device i/o modes and binding to the controller. Driver
  84. * must always write descriptors to initialize the device, then
  85. * the device becomes UNCONNECTED until enumeration.
  86. */
  87. STATE_DEV_OPENED,
  88. /* From then on, ep0 fd is in either of two basic modes:
  89. * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
  90. * - SETUP: read/write will transfer control data and succeed;
  91. * or if "wrong direction", performs protocol stall
  92. */
  93. STATE_DEV_UNCONNECTED,
  94. STATE_DEV_CONNECTED,
  95. STATE_DEV_SETUP,
  96. /* UNBOUND means the driver closed ep0, so the device won't be
  97. * accessible again (DEV_DISABLED) until all fds are closed.
  98. */
  99. STATE_DEV_UNBOUND,
  100. };
  101. /* enough for the whole queue: most events invalidate others */
  102. #define N_EVENT 5
  103. struct dev_data {
  104. spinlock_t lock;
  105. atomic_t count;
  106. enum ep0_state state; /* P: lock */
  107. struct usb_gadgetfs_event event [N_EVENT];
  108. unsigned ev_next;
  109. struct fasync_struct *fasync;
  110. u8 current_config;
  111. /* drivers reading ep0 MUST handle control requests (SETUP)
  112. * reported that way; else the host will time out.
  113. */
  114. unsigned usermode_setup : 1,
  115. setup_in : 1,
  116. setup_can_stall : 1,
  117. setup_out_ready : 1,
  118. setup_out_error : 1,
  119. setup_abort : 1;
  120. unsigned setup_wLength;
  121. /* the rest is basically write-once */
  122. struct usb_config_descriptor *config, *hs_config;
  123. struct usb_device_descriptor *dev;
  124. struct usb_request *req;
  125. struct usb_gadget *gadget;
  126. struct list_head epfiles;
  127. void *buf;
  128. wait_queue_head_t wait;
  129. struct super_block *sb;
  130. struct dentry *dentry;
  131. /* except this scratch i/o buffer for ep0 */
  132. u8 rbuf [256];
  133. };
  134. static inline void get_dev (struct dev_data *data)
  135. {
  136. atomic_inc (&data->count);
  137. }
  138. static void put_dev (struct dev_data *data)
  139. {
  140. if (likely (!atomic_dec_and_test (&data->count)))
  141. return;
  142. /* needs no more cleanup */
  143. BUG_ON (waitqueue_active (&data->wait));
  144. kfree (data);
  145. }
  146. static struct dev_data *dev_new (void)
  147. {
  148. struct dev_data *dev;
  149. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  150. if (!dev)
  151. return NULL;
  152. dev->state = STATE_DEV_DISABLED;
  153. atomic_set (&dev->count, 1);
  154. spin_lock_init (&dev->lock);
  155. INIT_LIST_HEAD (&dev->epfiles);
  156. init_waitqueue_head (&dev->wait);
  157. return dev;
  158. }
  159. /*----------------------------------------------------------------------*/
  160. /* other /dev/gadget/$ENDPOINT files represent endpoints */
  161. enum ep_state {
  162. STATE_EP_DISABLED = 0,
  163. STATE_EP_READY,
  164. STATE_EP_ENABLED,
  165. STATE_EP_UNBOUND,
  166. };
  167. struct ep_data {
  168. struct semaphore lock;
  169. enum ep_state state;
  170. atomic_t count;
  171. struct dev_data *dev;
  172. /* must hold dev->lock before accessing ep or req */
  173. struct usb_ep *ep;
  174. struct usb_request *req;
  175. ssize_t status;
  176. char name [16];
  177. struct usb_endpoint_descriptor desc, hs_desc;
  178. struct list_head epfiles;
  179. wait_queue_head_t wait;
  180. struct dentry *dentry;
  181. struct inode *inode;
  182. };
  183. static inline void get_ep (struct ep_data *data)
  184. {
  185. atomic_inc (&data->count);
  186. }
  187. static void put_ep (struct ep_data *data)
  188. {
  189. if (likely (!atomic_dec_and_test (&data->count)))
  190. return;
  191. put_dev (data->dev);
  192. /* needs no more cleanup */
  193. BUG_ON (!list_empty (&data->epfiles));
  194. BUG_ON (waitqueue_active (&data->wait));
  195. kfree (data);
  196. }
  197. /*----------------------------------------------------------------------*/
  198. /* most "how to use the hardware" policy choices are in userspace:
  199. * mapping endpoint roles (which the driver needs) to the capabilities
  200. * which the usb controller has. most of those capabilities are exposed
  201. * implicitly, starting with the driver name and then endpoint names.
  202. */
  203. static const char *CHIP;
  204. /*----------------------------------------------------------------------*/
  205. /* NOTE: don't use dev_printk calls before binding to the gadget
  206. * at the end of ep0 configuration, or after unbind.
  207. */
  208. /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
  209. #define xprintk(d,level,fmt,args...) \
  210. printk(level "%s: " fmt , shortname , ## args)
  211. #ifdef DEBUG
  212. #define DBG(dev,fmt,args...) \
  213. xprintk(dev , KERN_DEBUG , fmt , ## args)
  214. #else
  215. #define DBG(dev,fmt,args...) \
  216. do { } while (0)
  217. #endif /* DEBUG */
  218. #ifdef VERBOSE_DEBUG
  219. #define VDEBUG DBG
  220. #else
  221. #define VDEBUG(dev,fmt,args...) \
  222. do { } while (0)
  223. #endif /* DEBUG */
  224. #define ERROR(dev,fmt,args...) \
  225. xprintk(dev , KERN_ERR , fmt , ## args)
  226. #define INFO(dev,fmt,args...) \
  227. xprintk(dev , KERN_INFO , fmt , ## args)
  228. /*----------------------------------------------------------------------*/
  229. /* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
  230. *
  231. * After opening, configure non-control endpoints. Then use normal
  232. * stream read() and write() requests; and maybe ioctl() to get more
  233. * precise FIFO status when recovering from cancellation.
  234. */
  235. static void epio_complete (struct usb_ep *ep, struct usb_request *req)
  236. {
  237. struct ep_data *epdata = ep->driver_data;
  238. if (!req->context)
  239. return;
  240. if (req->status)
  241. epdata->status = req->status;
  242. else
  243. epdata->status = req->actual;
  244. complete ((struct completion *)req->context);
  245. }
  246. /* tasklock endpoint, returning when it's connected.
  247. * still need dev->lock to use epdata->ep.
  248. */
  249. static int
  250. get_ready_ep (unsigned f_flags, struct ep_data *epdata)
  251. {
  252. int val;
  253. if (f_flags & O_NONBLOCK) {
  254. if (down_trylock (&epdata->lock) != 0)
  255. goto nonblock;
  256. if (epdata->state != STATE_EP_ENABLED) {
  257. up (&epdata->lock);
  258. nonblock:
  259. val = -EAGAIN;
  260. } else
  261. val = 0;
  262. return val;
  263. }
  264. if ((val = down_interruptible (&epdata->lock)) < 0)
  265. return val;
  266. switch (epdata->state) {
  267. case STATE_EP_ENABLED:
  268. break;
  269. // case STATE_EP_DISABLED: /* "can't happen" */
  270. // case STATE_EP_READY: /* "can't happen" */
  271. default: /* error! */
  272. pr_debug ("%s: ep %p not available, state %d\n",
  273. shortname, epdata, epdata->state);
  274. // FALLTHROUGH
  275. case STATE_EP_UNBOUND: /* clean disconnect */
  276. val = -ENODEV;
  277. up (&epdata->lock);
  278. }
  279. return val;
  280. }
  281. static ssize_t
  282. ep_io (struct ep_data *epdata, void *buf, unsigned len)
  283. {
  284. DECLARE_COMPLETION_ONSTACK (done);
  285. int value;
  286. spin_lock_irq (&epdata->dev->lock);
  287. if (likely (epdata->ep != NULL)) {
  288. struct usb_request *req = epdata->req;
  289. req->context = &done;
  290. req->complete = epio_complete;
  291. req->buf = buf;
  292. req->length = len;
  293. value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
  294. } else
  295. value = -ENODEV;
  296. spin_unlock_irq (&epdata->dev->lock);
  297. if (likely (value == 0)) {
  298. value = wait_event_interruptible (done.wait, done.done);
  299. if (value != 0) {
  300. spin_lock_irq (&epdata->dev->lock);
  301. if (likely (epdata->ep != NULL)) {
  302. DBG (epdata->dev, "%s i/o interrupted\n",
  303. epdata->name);
  304. usb_ep_dequeue (epdata->ep, epdata->req);
  305. spin_unlock_irq (&epdata->dev->lock);
  306. wait_event (done.wait, done.done);
  307. if (epdata->status == -ECONNRESET)
  308. epdata->status = -EINTR;
  309. } else {
  310. spin_unlock_irq (&epdata->dev->lock);
  311. DBG (epdata->dev, "endpoint gone\n");
  312. epdata->status = -ENODEV;
  313. }
  314. }
  315. return epdata->status;
  316. }
  317. return value;
  318. }
  319. /* handle a synchronous OUT bulk/intr/iso transfer */
  320. static ssize_t
  321. ep_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
  322. {
  323. struct ep_data *data = fd->private_data;
  324. void *kbuf;
  325. ssize_t value;
  326. if ((value = get_ready_ep (fd->f_flags, data)) < 0)
  327. return value;
  328. /* halt any endpoint by doing a "wrong direction" i/o call */
  329. if (usb_endpoint_dir_in(&data->desc)) {
  330. if (usb_endpoint_xfer_isoc(&data->desc))
  331. return -EINVAL;
  332. DBG (data->dev, "%s halt\n", data->name);
  333. spin_lock_irq (&data->dev->lock);
  334. if (likely (data->ep != NULL))
  335. usb_ep_set_halt (data->ep);
  336. spin_unlock_irq (&data->dev->lock);
  337. up (&data->lock);
  338. return -EBADMSG;
  339. }
  340. /* FIXME readahead for O_NONBLOCK and poll(); careful with ZLPs */
  341. value = -ENOMEM;
  342. kbuf = kmalloc (len, GFP_KERNEL);
  343. if (unlikely (!kbuf))
  344. goto free1;
  345. value = ep_io (data, kbuf, len);
  346. VDEBUG (data->dev, "%s read %zu OUT, status %d\n",
  347. data->name, len, (int) value);
  348. if (value >= 0 && copy_to_user (buf, kbuf, value))
  349. value = -EFAULT;
  350. free1:
  351. up (&data->lock);
  352. kfree (kbuf);
  353. return value;
  354. }
  355. /* handle a synchronous IN bulk/intr/iso transfer */
  356. static ssize_t
  357. ep_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  358. {
  359. struct ep_data *data = fd->private_data;
  360. void *kbuf;
  361. ssize_t value;
  362. if ((value = get_ready_ep (fd->f_flags, data)) < 0)
  363. return value;
  364. /* halt any endpoint by doing a "wrong direction" i/o call */
  365. if (!usb_endpoint_dir_in(&data->desc)) {
  366. if (usb_endpoint_xfer_isoc(&data->desc))
  367. return -EINVAL;
  368. DBG (data->dev, "%s halt\n", data->name);
  369. spin_lock_irq (&data->dev->lock);
  370. if (likely (data->ep != NULL))
  371. usb_ep_set_halt (data->ep);
  372. spin_unlock_irq (&data->dev->lock);
  373. up (&data->lock);
  374. return -EBADMSG;
  375. }
  376. /* FIXME writebehind for O_NONBLOCK and poll(), qlen = 1 */
  377. value = -ENOMEM;
  378. kbuf = kmalloc (len, GFP_KERNEL);
  379. if (!kbuf)
  380. goto free1;
  381. if (copy_from_user (kbuf, buf, len)) {
  382. value = -EFAULT;
  383. goto free1;
  384. }
  385. value = ep_io (data, kbuf, len);
  386. VDEBUG (data->dev, "%s write %zu IN, status %d\n",
  387. data->name, len, (int) value);
  388. free1:
  389. up (&data->lock);
  390. kfree (kbuf);
  391. return value;
  392. }
  393. static int
  394. ep_release (struct inode *inode, struct file *fd)
  395. {
  396. struct ep_data *data = fd->private_data;
  397. int value;
  398. if ((value = down_interruptible(&data->lock)) < 0)
  399. return value;
  400. /* clean up if this can be reopened */
  401. if (data->state != STATE_EP_UNBOUND) {
  402. data->state = STATE_EP_DISABLED;
  403. data->desc.bDescriptorType = 0;
  404. data->hs_desc.bDescriptorType = 0;
  405. usb_ep_disable(data->ep);
  406. }
  407. up (&data->lock);
  408. put_ep (data);
  409. return 0;
  410. }
  411. static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
  412. {
  413. struct ep_data *data = fd->private_data;
  414. int status;
  415. if ((status = get_ready_ep (fd->f_flags, data)) < 0)
  416. return status;
  417. spin_lock_irq (&data->dev->lock);
  418. if (likely (data->ep != NULL)) {
  419. switch (code) {
  420. case GADGETFS_FIFO_STATUS:
  421. status = usb_ep_fifo_status (data->ep);
  422. break;
  423. case GADGETFS_FIFO_FLUSH:
  424. usb_ep_fifo_flush (data->ep);
  425. break;
  426. case GADGETFS_CLEAR_HALT:
  427. status = usb_ep_clear_halt (data->ep);
  428. break;
  429. default:
  430. status = -ENOTTY;
  431. }
  432. } else
  433. status = -ENODEV;
  434. spin_unlock_irq (&data->dev->lock);
  435. up (&data->lock);
  436. return status;
  437. }
  438. /*----------------------------------------------------------------------*/
  439. /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
  440. struct kiocb_priv {
  441. struct usb_request *req;
  442. struct ep_data *epdata;
  443. void *buf;
  444. const struct iovec *iv;
  445. unsigned long nr_segs;
  446. unsigned actual;
  447. };
  448. static int ep_aio_cancel(struct kiocb *iocb, struct io_event *e)
  449. {
  450. struct kiocb_priv *priv = iocb->private;
  451. struct ep_data *epdata;
  452. int value;
  453. local_irq_disable();
  454. epdata = priv->epdata;
  455. // spin_lock(&epdata->dev->lock);
  456. kiocbSetCancelled(iocb);
  457. if (likely(epdata && epdata->ep && priv->req))
  458. value = usb_ep_dequeue (epdata->ep, priv->req);
  459. else
  460. value = -EINVAL;
  461. // spin_unlock(&epdata->dev->lock);
  462. local_irq_enable();
  463. aio_put_req(iocb);
  464. return value;
  465. }
  466. static ssize_t ep_aio_read_retry(struct kiocb *iocb)
  467. {
  468. struct kiocb_priv *priv = iocb->private;
  469. ssize_t len, total;
  470. void *to_copy;
  471. int i;
  472. /* we "retry" to get the right mm context for this: */
  473. /* copy stuff into user buffers */
  474. total = priv->actual;
  475. len = 0;
  476. to_copy = priv->buf;
  477. for (i=0; i < priv->nr_segs; i++) {
  478. ssize_t this = min((ssize_t)(priv->iv[i].iov_len), total);
  479. if (copy_to_user(priv->iv[i].iov_base, to_copy, this)) {
  480. if (len == 0)
  481. len = -EFAULT;
  482. break;
  483. }
  484. total -= this;
  485. len += this;
  486. to_copy += this;
  487. if (total == 0)
  488. break;
  489. }
  490. kfree(priv->buf);
  491. kfree(priv);
  492. return len;
  493. }
  494. static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
  495. {
  496. struct kiocb *iocb = req->context;
  497. struct kiocb_priv *priv = iocb->private;
  498. struct ep_data *epdata = priv->epdata;
  499. /* lock against disconnect (and ideally, cancel) */
  500. spin_lock(&epdata->dev->lock);
  501. priv->req = NULL;
  502. priv->epdata = NULL;
  503. /* if this was a write or a read returning no data then we
  504. * don't need to copy anything to userspace, so we can
  505. * complete the aio request immediately.
  506. */
  507. if (priv->iv == NULL || unlikely(req->actual == 0)) {
  508. kfree(req->buf);
  509. kfree(priv);
  510. iocb->private = NULL;
  511. /* aio_complete() reports bytes-transferred _and_ faults */
  512. aio_complete(iocb, req->actual ? req->actual : req->status,
  513. req->status);
  514. } else {
  515. /* retry() won't report both; so we hide some faults */
  516. if (unlikely(0 != req->status))
  517. DBG(epdata->dev, "%s fault %d len %d\n",
  518. ep->name, req->status, req->actual);
  519. priv->buf = req->buf;
  520. priv->actual = req->actual;
  521. kick_iocb(iocb);
  522. }
  523. spin_unlock(&epdata->dev->lock);
  524. usb_ep_free_request(ep, req);
  525. put_ep(epdata);
  526. }
  527. static ssize_t
  528. ep_aio_rwtail(
  529. struct kiocb *iocb,
  530. char *buf,
  531. size_t len,
  532. struct ep_data *epdata,
  533. const struct iovec *iv,
  534. unsigned long nr_segs
  535. )
  536. {
  537. struct kiocb_priv *priv;
  538. struct usb_request *req;
  539. ssize_t value;
  540. priv = kmalloc(sizeof *priv, GFP_KERNEL);
  541. if (!priv) {
  542. value = -ENOMEM;
  543. fail:
  544. kfree(buf);
  545. return value;
  546. }
  547. iocb->private = priv;
  548. priv->iv = iv;
  549. priv->nr_segs = nr_segs;
  550. value = get_ready_ep(iocb->ki_filp->f_flags, epdata);
  551. if (unlikely(value < 0)) {
  552. kfree(priv);
  553. goto fail;
  554. }
  555. iocb->ki_cancel = ep_aio_cancel;
  556. get_ep(epdata);
  557. priv->epdata = epdata;
  558. priv->actual = 0;
  559. /* each kiocb is coupled to one usb_request, but we can't
  560. * allocate or submit those if the host disconnected.
  561. */
  562. spin_lock_irq(&epdata->dev->lock);
  563. if (likely(epdata->ep)) {
  564. req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
  565. if (likely(req)) {
  566. priv->req = req;
  567. req->buf = buf;
  568. req->length = len;
  569. req->complete = ep_aio_complete;
  570. req->context = iocb;
  571. value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
  572. if (unlikely(0 != value))
  573. usb_ep_free_request(epdata->ep, req);
  574. } else
  575. value = -EAGAIN;
  576. } else
  577. value = -ENODEV;
  578. spin_unlock_irq(&epdata->dev->lock);
  579. up(&epdata->lock);
  580. if (unlikely(value)) {
  581. kfree(priv);
  582. put_ep(epdata);
  583. } else
  584. value = (iv ? -EIOCBRETRY : -EIOCBQUEUED);
  585. return value;
  586. }
  587. static ssize_t
  588. ep_aio_read(struct kiocb *iocb, const struct iovec *iov,
  589. unsigned long nr_segs, loff_t o)
  590. {
  591. struct ep_data *epdata = iocb->ki_filp->private_data;
  592. char *buf;
  593. if (unlikely(usb_endpoint_dir_in(&epdata->desc)))
  594. return -EINVAL;
  595. buf = kmalloc(iocb->ki_left, GFP_KERNEL);
  596. if (unlikely(!buf))
  597. return -ENOMEM;
  598. iocb->ki_retry = ep_aio_read_retry;
  599. return ep_aio_rwtail(iocb, buf, iocb->ki_left, epdata, iov, nr_segs);
  600. }
  601. static ssize_t
  602. ep_aio_write(struct kiocb *iocb, const struct iovec *iov,
  603. unsigned long nr_segs, loff_t o)
  604. {
  605. struct ep_data *epdata = iocb->ki_filp->private_data;
  606. char *buf;
  607. size_t len = 0;
  608. int i = 0;
  609. if (unlikely(!usb_endpoint_dir_in(&epdata->desc)))
  610. return -EINVAL;
  611. buf = kmalloc(iocb->ki_left, GFP_KERNEL);
  612. if (unlikely(!buf))
  613. return -ENOMEM;
  614. for (i=0; i < nr_segs; i++) {
  615. if (unlikely(copy_from_user(&buf[len], iov[i].iov_base,
  616. iov[i].iov_len) != 0)) {
  617. kfree(buf);
  618. return -EFAULT;
  619. }
  620. len += iov[i].iov_len;
  621. }
  622. return ep_aio_rwtail(iocb, buf, len, epdata, NULL, 0);
  623. }
  624. /*----------------------------------------------------------------------*/
  625. /* used after endpoint configuration */
  626. static const struct file_operations ep_io_operations = {
  627. .owner = THIS_MODULE,
  628. .llseek = no_llseek,
  629. .read = ep_read,
  630. .write = ep_write,
  631. .unlocked_ioctl = ep_ioctl,
  632. .release = ep_release,
  633. .aio_read = ep_aio_read,
  634. .aio_write = ep_aio_write,
  635. };
  636. /* ENDPOINT INITIALIZATION
  637. *
  638. * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
  639. * status = write (fd, descriptors, sizeof descriptors)
  640. *
  641. * That write establishes the endpoint configuration, configuring
  642. * the controller to process bulk, interrupt, or isochronous transfers
  643. * at the right maxpacket size, and so on.
  644. *
  645. * The descriptors are message type 1, identified by a host order u32
  646. * at the beginning of what's written. Descriptor order is: full/low
  647. * speed descriptor, then optional high speed descriptor.
  648. */
  649. static ssize_t
  650. ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  651. {
  652. struct ep_data *data = fd->private_data;
  653. struct usb_ep *ep;
  654. u32 tag;
  655. int value, length = len;
  656. if ((value = down_interruptible (&data->lock)) < 0)
  657. return value;
  658. if (data->state != STATE_EP_READY) {
  659. value = -EL2HLT;
  660. goto fail;
  661. }
  662. value = len;
  663. if (len < USB_DT_ENDPOINT_SIZE + 4)
  664. goto fail0;
  665. /* we might need to change message format someday */
  666. if (copy_from_user (&tag, buf, 4)) {
  667. goto fail1;
  668. }
  669. if (tag != 1) {
  670. DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
  671. goto fail0;
  672. }
  673. buf += 4;
  674. len -= 4;
  675. /* NOTE: audio endpoint extensions not accepted here;
  676. * just don't include the extra bytes.
  677. */
  678. /* full/low speed descriptor, then high speed */
  679. if (copy_from_user (&data->desc, buf, USB_DT_ENDPOINT_SIZE)) {
  680. goto fail1;
  681. }
  682. if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
  683. || data->desc.bDescriptorType != USB_DT_ENDPOINT)
  684. goto fail0;
  685. if (len != USB_DT_ENDPOINT_SIZE) {
  686. if (len != 2 * USB_DT_ENDPOINT_SIZE)
  687. goto fail0;
  688. if (copy_from_user (&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
  689. USB_DT_ENDPOINT_SIZE)) {
  690. goto fail1;
  691. }
  692. if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
  693. || data->hs_desc.bDescriptorType
  694. != USB_DT_ENDPOINT) {
  695. DBG(data->dev, "config %s, bad hs length or type\n",
  696. data->name);
  697. goto fail0;
  698. }
  699. }
  700. spin_lock_irq (&data->dev->lock);
  701. if (data->dev->state == STATE_DEV_UNBOUND) {
  702. value = -ENOENT;
  703. goto gone;
  704. } else if ((ep = data->ep) == NULL) {
  705. value = -ENODEV;
  706. goto gone;
  707. }
  708. switch (data->dev->gadget->speed) {
  709. case USB_SPEED_LOW:
  710. case USB_SPEED_FULL:
  711. value = usb_ep_enable (ep, &data->desc);
  712. if (value == 0)
  713. data->state = STATE_EP_ENABLED;
  714. break;
  715. #ifdef CONFIG_USB_GADGET_DUALSPEED
  716. case USB_SPEED_HIGH:
  717. /* fails if caller didn't provide that descriptor... */
  718. value = usb_ep_enable (ep, &data->hs_desc);
  719. if (value == 0)
  720. data->state = STATE_EP_ENABLED;
  721. break;
  722. #endif
  723. default:
  724. DBG(data->dev, "unconnected, %s init abandoned\n",
  725. data->name);
  726. value = -EINVAL;
  727. }
  728. if (value == 0) {
  729. fd->f_op = &ep_io_operations;
  730. value = length;
  731. }
  732. gone:
  733. spin_unlock_irq (&data->dev->lock);
  734. if (value < 0) {
  735. fail:
  736. data->desc.bDescriptorType = 0;
  737. data->hs_desc.bDescriptorType = 0;
  738. }
  739. up (&data->lock);
  740. return value;
  741. fail0:
  742. value = -EINVAL;
  743. goto fail;
  744. fail1:
  745. value = -EFAULT;
  746. goto fail;
  747. }
  748. static int
  749. ep_open (struct inode *inode, struct file *fd)
  750. {
  751. struct ep_data *data = inode->i_private;
  752. int value = -EBUSY;
  753. if (down_interruptible (&data->lock) != 0)
  754. return -EINTR;
  755. spin_lock_irq (&data->dev->lock);
  756. if (data->dev->state == STATE_DEV_UNBOUND)
  757. value = -ENOENT;
  758. else if (data->state == STATE_EP_DISABLED) {
  759. value = 0;
  760. data->state = STATE_EP_READY;
  761. get_ep (data);
  762. fd->private_data = data;
  763. VDEBUG (data->dev, "%s ready\n", data->name);
  764. } else
  765. DBG (data->dev, "%s state %d\n",
  766. data->name, data->state);
  767. spin_unlock_irq (&data->dev->lock);
  768. up (&data->lock);
  769. return value;
  770. }
  771. /* used before endpoint configuration */
  772. static const struct file_operations ep_config_operations = {
  773. .owner = THIS_MODULE,
  774. .llseek = no_llseek,
  775. .open = ep_open,
  776. .write = ep_config,
  777. .release = ep_release,
  778. };
  779. /*----------------------------------------------------------------------*/
  780. /* EP0 IMPLEMENTATION can be partly in userspace.
  781. *
  782. * Drivers that use this facility receive various events, including
  783. * control requests the kernel doesn't handle. Drivers that don't
  784. * use this facility may be too simple-minded for real applications.
  785. */
  786. static inline void ep0_readable (struct dev_data *dev)
  787. {
  788. wake_up (&dev->wait);
  789. kill_fasync (&dev->fasync, SIGIO, POLL_IN);
  790. }
  791. static void clean_req (struct usb_ep *ep, struct usb_request *req)
  792. {
  793. struct dev_data *dev = ep->driver_data;
  794. if (req->buf != dev->rbuf) {
  795. kfree(req->buf);
  796. req->buf = dev->rbuf;
  797. req->dma = DMA_ADDR_INVALID;
  798. }
  799. req->complete = epio_complete;
  800. dev->setup_out_ready = 0;
  801. }
  802. static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
  803. {
  804. struct dev_data *dev = ep->driver_data;
  805. unsigned long flags;
  806. int free = 1;
  807. /* for control OUT, data must still get to userspace */
  808. spin_lock_irqsave(&dev->lock, flags);
  809. if (!dev->setup_in) {
  810. dev->setup_out_error = (req->status != 0);
  811. if (!dev->setup_out_error)
  812. free = 0;
  813. dev->setup_out_ready = 1;
  814. ep0_readable (dev);
  815. }
  816. /* clean up as appropriate */
  817. if (free && req->buf != &dev->rbuf)
  818. clean_req (ep, req);
  819. req->complete = epio_complete;
  820. spin_unlock_irqrestore(&dev->lock, flags);
  821. }
  822. static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
  823. {
  824. struct dev_data *dev = ep->driver_data;
  825. if (dev->setup_out_ready) {
  826. DBG (dev, "ep0 request busy!\n");
  827. return -EBUSY;
  828. }
  829. if (len > sizeof (dev->rbuf))
  830. req->buf = kmalloc(len, GFP_ATOMIC);
  831. if (req->buf == NULL) {
  832. req->buf = dev->rbuf;
  833. return -ENOMEM;
  834. }
  835. req->complete = ep0_complete;
  836. req->length = len;
  837. req->zero = 0;
  838. return 0;
  839. }
  840. static ssize_t
  841. ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
  842. {
  843. struct dev_data *dev = fd->private_data;
  844. ssize_t retval;
  845. enum ep0_state state;
  846. spin_lock_irq (&dev->lock);
  847. /* report fd mode change before acting on it */
  848. if (dev->setup_abort) {
  849. dev->setup_abort = 0;
  850. retval = -EIDRM;
  851. goto done;
  852. }
  853. /* control DATA stage */
  854. if ((state = dev->state) == STATE_DEV_SETUP) {
  855. if (dev->setup_in) { /* stall IN */
  856. VDEBUG(dev, "ep0in stall\n");
  857. (void) usb_ep_set_halt (dev->gadget->ep0);
  858. retval = -EL2HLT;
  859. dev->state = STATE_DEV_CONNECTED;
  860. } else if (len == 0) { /* ack SET_CONFIGURATION etc */
  861. struct usb_ep *ep = dev->gadget->ep0;
  862. struct usb_request *req = dev->req;
  863. if ((retval = setup_req (ep, req, 0)) == 0)
  864. retval = usb_ep_queue (ep, req, GFP_ATOMIC);
  865. dev->state = STATE_DEV_CONNECTED;
  866. /* assume that was SET_CONFIGURATION */
  867. if (dev->current_config) {
  868. unsigned power;
  869. if (gadget_is_dualspeed(dev->gadget)
  870. && (dev->gadget->speed
  871. == USB_SPEED_HIGH))
  872. power = dev->hs_config->bMaxPower;
  873. else
  874. power = dev->config->bMaxPower;
  875. usb_gadget_vbus_draw(dev->gadget, 2 * power);
  876. }
  877. } else { /* collect OUT data */
  878. if ((fd->f_flags & O_NONBLOCK) != 0
  879. && !dev->setup_out_ready) {
  880. retval = -EAGAIN;
  881. goto done;
  882. }
  883. spin_unlock_irq (&dev->lock);
  884. retval = wait_event_interruptible (dev->wait,
  885. dev->setup_out_ready != 0);
  886. /* FIXME state could change from under us */
  887. spin_lock_irq (&dev->lock);
  888. if (retval)
  889. goto done;
  890. if (dev->state != STATE_DEV_SETUP) {
  891. retval = -ECANCELED;
  892. goto done;
  893. }
  894. dev->state = STATE_DEV_CONNECTED;
  895. if (dev->setup_out_error)
  896. retval = -EIO;
  897. else {
  898. len = min (len, (size_t)dev->req->actual);
  899. // FIXME don't call this with the spinlock held ...
  900. if (copy_to_user (buf, dev->req->buf, len))
  901. retval = -EFAULT;
  902. clean_req (dev->gadget->ep0, dev->req);
  903. /* NOTE userspace can't yet choose to stall */
  904. }
  905. }
  906. goto done;
  907. }
  908. /* else normal: return event data */
  909. if (len < sizeof dev->event [0]) {
  910. retval = -EINVAL;
  911. goto done;
  912. }
  913. len -= len % sizeof (struct usb_gadgetfs_event);
  914. dev->usermode_setup = 1;
  915. scan:
  916. /* return queued events right away */
  917. if (dev->ev_next != 0) {
  918. unsigned i, n;
  919. n = len / sizeof (struct usb_gadgetfs_event);
  920. if (dev->ev_next < n)
  921. n = dev->ev_next;
  922. /* ep0 i/o has special semantics during STATE_DEV_SETUP */
  923. for (i = 0; i < n; i++) {
  924. if (dev->event [i].type == GADGETFS_SETUP) {
  925. dev->state = STATE_DEV_SETUP;
  926. n = i + 1;
  927. break;
  928. }
  929. }
  930. spin_unlock_irq (&dev->lock);
  931. len = n * sizeof (struct usb_gadgetfs_event);
  932. if (copy_to_user (buf, &dev->event, len))
  933. retval = -EFAULT;
  934. else
  935. retval = len;
  936. if (len > 0) {
  937. /* NOTE this doesn't guard against broken drivers;
  938. * concurrent ep0 readers may lose events.
  939. */
  940. spin_lock_irq (&dev->lock);
  941. if (dev->ev_next > n) {
  942. memmove(&dev->event[0], &dev->event[n],
  943. sizeof (struct usb_gadgetfs_event)
  944. * (dev->ev_next - n));
  945. }
  946. dev->ev_next -= n;
  947. spin_unlock_irq (&dev->lock);
  948. }
  949. return retval;
  950. }
  951. if (fd->f_flags & O_NONBLOCK) {
  952. retval = -EAGAIN;
  953. goto done;
  954. }
  955. switch (state) {
  956. default:
  957. DBG (dev, "fail %s, state %d\n", __func__, state);
  958. retval = -ESRCH;
  959. break;
  960. case STATE_DEV_UNCONNECTED:
  961. case STATE_DEV_CONNECTED:
  962. spin_unlock_irq (&dev->lock);
  963. DBG (dev, "%s wait\n", __func__);
  964. /* wait for events */
  965. retval = wait_event_interruptible (dev->wait,
  966. dev->ev_next != 0);
  967. if (retval < 0)
  968. return retval;
  969. spin_lock_irq (&dev->lock);
  970. goto scan;
  971. }
  972. done:
  973. spin_unlock_irq (&dev->lock);
  974. return retval;
  975. }
  976. static struct usb_gadgetfs_event *
  977. next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
  978. {
  979. struct usb_gadgetfs_event *event;
  980. unsigned i;
  981. switch (type) {
  982. /* these events purge the queue */
  983. case GADGETFS_DISCONNECT:
  984. if (dev->state == STATE_DEV_SETUP)
  985. dev->setup_abort = 1;
  986. // FALL THROUGH
  987. case GADGETFS_CONNECT:
  988. dev->ev_next = 0;
  989. break;
  990. case GADGETFS_SETUP: /* previous request timed out */
  991. case GADGETFS_SUSPEND: /* same effect */
  992. /* these events can't be repeated */
  993. for (i = 0; i != dev->ev_next; i++) {
  994. if (dev->event [i].type != type)
  995. continue;
  996. DBG(dev, "discard old event[%d] %d\n", i, type);
  997. dev->ev_next--;
  998. if (i == dev->ev_next)
  999. break;
  1000. /* indices start at zero, for simplicity */
  1001. memmove (&dev->event [i], &dev->event [i + 1],
  1002. sizeof (struct usb_gadgetfs_event)
  1003. * (dev->ev_next - i));
  1004. }
  1005. break;
  1006. default:
  1007. BUG ();
  1008. }
  1009. VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
  1010. event = &dev->event [dev->ev_next++];
  1011. BUG_ON (dev->ev_next > N_EVENT);
  1012. memset (event, 0, sizeof *event);
  1013. event->type = type;
  1014. return event;
  1015. }
  1016. static ssize_t
  1017. ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  1018. {
  1019. struct dev_data *dev = fd->private_data;
  1020. ssize_t retval = -ESRCH;
  1021. spin_lock_irq (&dev->lock);
  1022. /* report fd mode change before acting on it */
  1023. if (dev->setup_abort) {
  1024. dev->setup_abort = 0;
  1025. retval = -EIDRM;
  1026. /* data and/or status stage for control request */
  1027. } else if (dev->state == STATE_DEV_SETUP) {
  1028. /* IN DATA+STATUS caller makes len <= wLength */
  1029. if (dev->setup_in) {
  1030. retval = setup_req (dev->gadget->ep0, dev->req, len);
  1031. if (retval == 0) {
  1032. dev->state = STATE_DEV_CONNECTED;
  1033. spin_unlock_irq (&dev->lock);
  1034. if (copy_from_user (dev->req->buf, buf, len))
  1035. retval = -EFAULT;
  1036. else {
  1037. if (len < dev->setup_wLength)
  1038. dev->req->zero = 1;
  1039. retval = usb_ep_queue (
  1040. dev->gadget->ep0, dev->req,
  1041. GFP_KERNEL);
  1042. }
  1043. if (retval < 0) {
  1044. spin_lock_irq (&dev->lock);
  1045. clean_req (dev->gadget->ep0, dev->req);
  1046. spin_unlock_irq (&dev->lock);
  1047. } else
  1048. retval = len;
  1049. return retval;
  1050. }
  1051. /* can stall some OUT transfers */
  1052. } else if (dev->setup_can_stall) {
  1053. VDEBUG(dev, "ep0out stall\n");
  1054. (void) usb_ep_set_halt (dev->gadget->ep0);
  1055. retval = -EL2HLT;
  1056. dev->state = STATE_DEV_CONNECTED;
  1057. } else {
  1058. DBG(dev, "bogus ep0out stall!\n");
  1059. }
  1060. } else
  1061. DBG (dev, "fail %s, state %d\n", __func__, dev->state);
  1062. spin_unlock_irq (&dev->lock);
  1063. return retval;
  1064. }
  1065. static int
  1066. ep0_fasync (int f, struct file *fd, int on)
  1067. {
  1068. struct dev_data *dev = fd->private_data;
  1069. // caller must F_SETOWN before signal delivery happens
  1070. VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
  1071. return fasync_helper (f, fd, on, &dev->fasync);
  1072. }
  1073. static struct usb_gadget_driver gadgetfs_driver;
  1074. static int
  1075. dev_release (struct inode *inode, struct file *fd)
  1076. {
  1077. struct dev_data *dev = fd->private_data;
  1078. /* closing ep0 === shutdown all */
  1079. usb_gadget_unregister_driver (&gadgetfs_driver);
  1080. /* at this point "good" hardware has disconnected the
  1081. * device from USB; the host won't see it any more.
  1082. * alternatively, all host requests will time out.
  1083. */
  1084. kfree (dev->buf);
  1085. dev->buf = NULL;
  1086. put_dev (dev);
  1087. /* other endpoints were all decoupled from this device */
  1088. spin_lock_irq(&dev->lock);
  1089. dev->state = STATE_DEV_DISABLED;
  1090. spin_unlock_irq(&dev->lock);
  1091. return 0;
  1092. }
  1093. static unsigned int
  1094. ep0_poll (struct file *fd, poll_table *wait)
  1095. {
  1096. struct dev_data *dev = fd->private_data;
  1097. int mask = 0;
  1098. poll_wait(fd, &dev->wait, wait);
  1099. spin_lock_irq (&dev->lock);
  1100. /* report fd mode change before acting on it */
  1101. if (dev->setup_abort) {
  1102. dev->setup_abort = 0;
  1103. mask = POLLHUP;
  1104. goto out;
  1105. }
  1106. if (dev->state == STATE_DEV_SETUP) {
  1107. if (dev->setup_in || dev->setup_can_stall)
  1108. mask = POLLOUT;
  1109. } else {
  1110. if (dev->ev_next != 0)
  1111. mask = POLLIN;
  1112. }
  1113. out:
  1114. spin_unlock_irq(&dev->lock);
  1115. return mask;
  1116. }
  1117. static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
  1118. {
  1119. struct dev_data *dev = fd->private_data;
  1120. struct usb_gadget *gadget = dev->gadget;
  1121. long ret = -ENOTTY;
  1122. if (gadget->ops->ioctl) {
  1123. lock_kernel();
  1124. ret = gadget->ops->ioctl (gadget, code, value);
  1125. unlock_kernel();
  1126. }
  1127. return ret;
  1128. }
  1129. /* used after device configuration */
  1130. static const struct file_operations ep0_io_operations = {
  1131. .owner = THIS_MODULE,
  1132. .llseek = no_llseek,
  1133. .read = ep0_read,
  1134. .write = ep0_write,
  1135. .fasync = ep0_fasync,
  1136. .poll = ep0_poll,
  1137. .unlocked_ioctl = dev_ioctl,
  1138. .release = dev_release,
  1139. };
  1140. /*----------------------------------------------------------------------*/
  1141. /* The in-kernel gadget driver handles most ep0 issues, in particular
  1142. * enumerating the single configuration (as provided from user space).
  1143. *
  1144. * Unrecognized ep0 requests may be handled in user space.
  1145. */
  1146. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1147. static void make_qualifier (struct dev_data *dev)
  1148. {
  1149. struct usb_qualifier_descriptor qual;
  1150. struct usb_device_descriptor *desc;
  1151. qual.bLength = sizeof qual;
  1152. qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  1153. qual.bcdUSB = cpu_to_le16 (0x0200);
  1154. desc = dev->dev;
  1155. qual.bDeviceClass = desc->bDeviceClass;
  1156. qual.bDeviceSubClass = desc->bDeviceSubClass;
  1157. qual.bDeviceProtocol = desc->bDeviceProtocol;
  1158. /* assumes ep0 uses the same value for both speeds ... */
  1159. qual.bMaxPacketSize0 = desc->bMaxPacketSize0;
  1160. qual.bNumConfigurations = 1;
  1161. qual.bRESERVED = 0;
  1162. memcpy (dev->rbuf, &qual, sizeof qual);
  1163. }
  1164. #endif
  1165. static int
  1166. config_buf (struct dev_data *dev, u8 type, unsigned index)
  1167. {
  1168. int len;
  1169. int hs = 0;
  1170. /* only one configuration */
  1171. if (index > 0)
  1172. return -EINVAL;
  1173. if (gadget_is_dualspeed(dev->gadget)) {
  1174. hs = (dev->gadget->speed == USB_SPEED_HIGH);
  1175. if (type == USB_DT_OTHER_SPEED_CONFIG)
  1176. hs = !hs;
  1177. }
  1178. if (hs) {
  1179. dev->req->buf = dev->hs_config;
  1180. len = le16_to_cpu(dev->hs_config->wTotalLength);
  1181. } else {
  1182. dev->req->buf = dev->config;
  1183. len = le16_to_cpu(dev->config->wTotalLength);
  1184. }
  1185. ((u8 *)dev->req->buf) [1] = type;
  1186. return len;
  1187. }
  1188. static int
  1189. gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1190. {
  1191. struct dev_data *dev = get_gadget_data (gadget);
  1192. struct usb_request *req = dev->req;
  1193. int value = -EOPNOTSUPP;
  1194. struct usb_gadgetfs_event *event;
  1195. u16 w_value = le16_to_cpu(ctrl->wValue);
  1196. u16 w_length = le16_to_cpu(ctrl->wLength);
  1197. spin_lock (&dev->lock);
  1198. dev->setup_abort = 0;
  1199. if (dev->state == STATE_DEV_UNCONNECTED) {
  1200. if (gadget_is_dualspeed(gadget)
  1201. && gadget->speed == USB_SPEED_HIGH
  1202. && dev->hs_config == NULL) {
  1203. spin_unlock(&dev->lock);
  1204. ERROR (dev, "no high speed config??\n");
  1205. return -EINVAL;
  1206. }
  1207. dev->state = STATE_DEV_CONNECTED;
  1208. dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
  1209. INFO (dev, "connected\n");
  1210. event = next_event (dev, GADGETFS_CONNECT);
  1211. event->u.speed = gadget->speed;
  1212. ep0_readable (dev);
  1213. /* host may have given up waiting for response. we can miss control
  1214. * requests handled lower down (device/endpoint status and features);
  1215. * then ep0_{read,write} will report the wrong status. controller
  1216. * driver will have aborted pending i/o.
  1217. */
  1218. } else if (dev->state == STATE_DEV_SETUP)
  1219. dev->setup_abort = 1;
  1220. req->buf = dev->rbuf;
  1221. req->dma = DMA_ADDR_INVALID;
  1222. req->context = NULL;
  1223. value = -EOPNOTSUPP;
  1224. switch (ctrl->bRequest) {
  1225. case USB_REQ_GET_DESCRIPTOR:
  1226. if (ctrl->bRequestType != USB_DIR_IN)
  1227. goto unrecognized;
  1228. switch (w_value >> 8) {
  1229. case USB_DT_DEVICE:
  1230. value = min (w_length, (u16) sizeof *dev->dev);
  1231. req->buf = dev->dev;
  1232. break;
  1233. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1234. case USB_DT_DEVICE_QUALIFIER:
  1235. if (!dev->hs_config)
  1236. break;
  1237. value = min (w_length, (u16)
  1238. sizeof (struct usb_qualifier_descriptor));
  1239. make_qualifier (dev);
  1240. break;
  1241. case USB_DT_OTHER_SPEED_CONFIG:
  1242. // FALLTHROUGH
  1243. #endif
  1244. case USB_DT_CONFIG:
  1245. value = config_buf (dev,
  1246. w_value >> 8,
  1247. w_value & 0xff);
  1248. if (value >= 0)
  1249. value = min (w_length, (u16) value);
  1250. break;
  1251. case USB_DT_STRING:
  1252. goto unrecognized;
  1253. default: // all others are errors
  1254. break;
  1255. }
  1256. break;
  1257. /* currently one config, two speeds */
  1258. case USB_REQ_SET_CONFIGURATION:
  1259. if (ctrl->bRequestType != 0)
  1260. goto unrecognized;
  1261. if (0 == (u8) w_value) {
  1262. value = 0;
  1263. dev->current_config = 0;
  1264. usb_gadget_vbus_draw(gadget, 8 /* mA */ );
  1265. // user mode expected to disable endpoints
  1266. } else {
  1267. u8 config, power;
  1268. if (gadget_is_dualspeed(gadget)
  1269. && gadget->speed == USB_SPEED_HIGH) {
  1270. config = dev->hs_config->bConfigurationValue;
  1271. power = dev->hs_config->bMaxPower;
  1272. } else {
  1273. config = dev->config->bConfigurationValue;
  1274. power = dev->config->bMaxPower;
  1275. }
  1276. if (config == (u8) w_value) {
  1277. value = 0;
  1278. dev->current_config = config;
  1279. usb_gadget_vbus_draw(gadget, 2 * power);
  1280. }
  1281. }
  1282. /* report SET_CONFIGURATION like any other control request,
  1283. * except that usermode may not stall this. the next
  1284. * request mustn't be allowed start until this finishes:
  1285. * endpoints and threads set up, etc.
  1286. *
  1287. * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
  1288. * has bad/racey automagic that prevents synchronizing here.
  1289. * even kernel mode drivers often miss them.
  1290. */
  1291. if (value == 0) {
  1292. INFO (dev, "configuration #%d\n", dev->current_config);
  1293. if (dev->usermode_setup) {
  1294. dev->setup_can_stall = 0;
  1295. goto delegate;
  1296. }
  1297. }
  1298. break;
  1299. #ifndef CONFIG_USB_GADGET_PXA25X
  1300. /* PXA automagically handles this request too */
  1301. case USB_REQ_GET_CONFIGURATION:
  1302. if (ctrl->bRequestType != 0x80)
  1303. goto unrecognized;
  1304. *(u8 *)req->buf = dev->current_config;
  1305. value = min (w_length, (u16) 1);
  1306. break;
  1307. #endif
  1308. default:
  1309. unrecognized:
  1310. VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
  1311. dev->usermode_setup ? "delegate" : "fail",
  1312. ctrl->bRequestType, ctrl->bRequest,
  1313. w_value, le16_to_cpu(ctrl->wIndex), w_length);
  1314. /* if there's an ep0 reader, don't stall */
  1315. if (dev->usermode_setup) {
  1316. dev->setup_can_stall = 1;
  1317. delegate:
  1318. dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
  1319. ? 1 : 0;
  1320. dev->setup_wLength = w_length;
  1321. dev->setup_out_ready = 0;
  1322. dev->setup_out_error = 0;
  1323. value = 0;
  1324. /* read DATA stage for OUT right away */
  1325. if (unlikely (!dev->setup_in && w_length)) {
  1326. value = setup_req (gadget->ep0, dev->req,
  1327. w_length);
  1328. if (value < 0)
  1329. break;
  1330. value = usb_ep_queue (gadget->ep0, dev->req,
  1331. GFP_ATOMIC);
  1332. if (value < 0) {
  1333. clean_req (gadget->ep0, dev->req);
  1334. break;
  1335. }
  1336. /* we can't currently stall these */
  1337. dev->setup_can_stall = 0;
  1338. }
  1339. /* state changes when reader collects event */
  1340. event = next_event (dev, GADGETFS_SETUP);
  1341. event->u.setup = *ctrl;
  1342. ep0_readable (dev);
  1343. spin_unlock (&dev->lock);
  1344. return 0;
  1345. }
  1346. }
  1347. /* proceed with data transfer and status phases? */
  1348. if (value >= 0 && dev->state != STATE_DEV_SETUP) {
  1349. req->length = value;
  1350. req->zero = value < w_length;
  1351. value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  1352. if (value < 0) {
  1353. DBG (dev, "ep_queue --> %d\n", value);
  1354. req->status = 0;
  1355. }
  1356. }
  1357. /* device stalls when value < 0 */
  1358. spin_unlock (&dev->lock);
  1359. return value;
  1360. }
  1361. static void destroy_ep_files (struct dev_data *dev)
  1362. {
  1363. struct list_head *entry, *tmp;
  1364. DBG (dev, "%s %d\n", __func__, dev->state);
  1365. /* dev->state must prevent interference */
  1366. restart:
  1367. spin_lock_irq (&dev->lock);
  1368. list_for_each_safe (entry, tmp, &dev->epfiles) {
  1369. struct ep_data *ep;
  1370. struct inode *parent;
  1371. struct dentry *dentry;
  1372. /* break link to FS */
  1373. ep = list_entry (entry, struct ep_data, epfiles);
  1374. list_del_init (&ep->epfiles);
  1375. dentry = ep->dentry;
  1376. ep->dentry = NULL;
  1377. parent = dentry->d_parent->d_inode;
  1378. /* break link to controller */
  1379. if (ep->state == STATE_EP_ENABLED)
  1380. (void) usb_ep_disable (ep->ep);
  1381. ep->state = STATE_EP_UNBOUND;
  1382. usb_ep_free_request (ep->ep, ep->req);
  1383. ep->ep = NULL;
  1384. wake_up (&ep->wait);
  1385. put_ep (ep);
  1386. spin_unlock_irq (&dev->lock);
  1387. /* break link to dcache */
  1388. mutex_lock (&parent->i_mutex);
  1389. d_delete (dentry);
  1390. dput (dentry);
  1391. mutex_unlock (&parent->i_mutex);
  1392. /* fds may still be open */
  1393. goto restart;
  1394. }
  1395. spin_unlock_irq (&dev->lock);
  1396. }
  1397. static struct inode *
  1398. gadgetfs_create_file (struct super_block *sb, char const *name,
  1399. void *data, const struct file_operations *fops,
  1400. struct dentry **dentry_p);
  1401. static int activate_ep_files (struct dev_data *dev)
  1402. {
  1403. struct usb_ep *ep;
  1404. struct ep_data *data;
  1405. gadget_for_each_ep (ep, dev->gadget) {
  1406. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1407. if (!data)
  1408. goto enomem0;
  1409. data->state = STATE_EP_DISABLED;
  1410. init_MUTEX (&data->lock);
  1411. init_waitqueue_head (&data->wait);
  1412. strncpy (data->name, ep->name, sizeof (data->name) - 1);
  1413. atomic_set (&data->count, 1);
  1414. data->dev = dev;
  1415. get_dev (dev);
  1416. data->ep = ep;
  1417. ep->driver_data = data;
  1418. data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
  1419. if (!data->req)
  1420. goto enomem1;
  1421. data->inode = gadgetfs_create_file (dev->sb, data->name,
  1422. data, &ep_config_operations,
  1423. &data->dentry);
  1424. if (!data->inode)
  1425. goto enomem2;
  1426. list_add_tail (&data->epfiles, &dev->epfiles);
  1427. }
  1428. return 0;
  1429. enomem2:
  1430. usb_ep_free_request (ep, data->req);
  1431. enomem1:
  1432. put_dev (dev);
  1433. kfree (data);
  1434. enomem0:
  1435. DBG (dev, "%s enomem\n", __func__);
  1436. destroy_ep_files (dev);
  1437. return -ENOMEM;
  1438. }
  1439. static void
  1440. gadgetfs_unbind (struct usb_gadget *gadget)
  1441. {
  1442. struct dev_data *dev = get_gadget_data (gadget);
  1443. DBG (dev, "%s\n", __func__);
  1444. spin_lock_irq (&dev->lock);
  1445. dev->state = STATE_DEV_UNBOUND;
  1446. spin_unlock_irq (&dev->lock);
  1447. destroy_ep_files (dev);
  1448. gadget->ep0->driver_data = NULL;
  1449. set_gadget_data (gadget, NULL);
  1450. /* we've already been disconnected ... no i/o is active */
  1451. if (dev->req)
  1452. usb_ep_free_request (gadget->ep0, dev->req);
  1453. DBG (dev, "%s done\n", __func__);
  1454. put_dev (dev);
  1455. }
  1456. static struct dev_data *the_device;
  1457. static int
  1458. gadgetfs_bind (struct usb_gadget *gadget)
  1459. {
  1460. struct dev_data *dev = the_device;
  1461. if (!dev)
  1462. return -ESRCH;
  1463. if (0 != strcmp (CHIP, gadget->name)) {
  1464. pr_err("%s expected %s controller not %s\n",
  1465. shortname, CHIP, gadget->name);
  1466. return -ENODEV;
  1467. }
  1468. set_gadget_data (gadget, dev);
  1469. dev->gadget = gadget;
  1470. gadget->ep0->driver_data = dev;
  1471. dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
  1472. /* preallocate control response and buffer */
  1473. dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  1474. if (!dev->req)
  1475. goto enomem;
  1476. dev->req->context = NULL;
  1477. dev->req->complete = epio_complete;
  1478. if (activate_ep_files (dev) < 0)
  1479. goto enomem;
  1480. INFO (dev, "bound to %s driver\n", gadget->name);
  1481. spin_lock_irq(&dev->lock);
  1482. dev->state = STATE_DEV_UNCONNECTED;
  1483. spin_unlock_irq(&dev->lock);
  1484. get_dev (dev);
  1485. return 0;
  1486. enomem:
  1487. gadgetfs_unbind (gadget);
  1488. return -ENOMEM;
  1489. }
  1490. static void
  1491. gadgetfs_disconnect (struct usb_gadget *gadget)
  1492. {
  1493. struct dev_data *dev = get_gadget_data (gadget);
  1494. spin_lock (&dev->lock);
  1495. if (dev->state == STATE_DEV_UNCONNECTED)
  1496. goto exit;
  1497. dev->state = STATE_DEV_UNCONNECTED;
  1498. INFO (dev, "disconnected\n");
  1499. next_event (dev, GADGETFS_DISCONNECT);
  1500. ep0_readable (dev);
  1501. exit:
  1502. spin_unlock (&dev->lock);
  1503. }
  1504. static void
  1505. gadgetfs_suspend (struct usb_gadget *gadget)
  1506. {
  1507. struct dev_data *dev = get_gadget_data (gadget);
  1508. INFO (dev, "suspended from state %d\n", dev->state);
  1509. spin_lock (&dev->lock);
  1510. switch (dev->state) {
  1511. case STATE_DEV_SETUP: // VERY odd... host died??
  1512. case STATE_DEV_CONNECTED:
  1513. case STATE_DEV_UNCONNECTED:
  1514. next_event (dev, GADGETFS_SUSPEND);
  1515. ep0_readable (dev);
  1516. /* FALLTHROUGH */
  1517. default:
  1518. break;
  1519. }
  1520. spin_unlock (&dev->lock);
  1521. }
  1522. static struct usb_gadget_driver gadgetfs_driver = {
  1523. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1524. .speed = USB_SPEED_HIGH,
  1525. #else
  1526. .speed = USB_SPEED_FULL,
  1527. #endif
  1528. .function = (char *) driver_desc,
  1529. .bind = gadgetfs_bind,
  1530. .unbind = gadgetfs_unbind,
  1531. .setup = gadgetfs_setup,
  1532. .disconnect = gadgetfs_disconnect,
  1533. .suspend = gadgetfs_suspend,
  1534. .driver = {
  1535. .name = (char *) shortname,
  1536. },
  1537. };
  1538. /*----------------------------------------------------------------------*/
  1539. static void gadgetfs_nop(struct usb_gadget *arg) { }
  1540. static int gadgetfs_probe (struct usb_gadget *gadget)
  1541. {
  1542. CHIP = gadget->name;
  1543. return -EISNAM;
  1544. }
  1545. static struct usb_gadget_driver probe_driver = {
  1546. .speed = USB_SPEED_HIGH,
  1547. .bind = gadgetfs_probe,
  1548. .unbind = gadgetfs_nop,
  1549. .setup = (void *)gadgetfs_nop,
  1550. .disconnect = gadgetfs_nop,
  1551. .driver = {
  1552. .name = "nop",
  1553. },
  1554. };
  1555. /* DEVICE INITIALIZATION
  1556. *
  1557. * fd = open ("/dev/gadget/$CHIP", O_RDWR)
  1558. * status = write (fd, descriptors, sizeof descriptors)
  1559. *
  1560. * That write establishes the device configuration, so the kernel can
  1561. * bind to the controller ... guaranteeing it can handle enumeration
  1562. * at all necessary speeds. Descriptor order is:
  1563. *
  1564. * . message tag (u32, host order) ... for now, must be zero; it
  1565. * would change to support features like multi-config devices
  1566. * . full/low speed config ... all wTotalLength bytes (with interface,
  1567. * class, altsetting, endpoint, and other descriptors)
  1568. * . high speed config ... all descriptors, for high speed operation;
  1569. * this one's optional except for high-speed hardware
  1570. * . device descriptor
  1571. *
  1572. * Endpoints are not yet enabled. Drivers must wait until device
  1573. * configuration and interface altsetting changes create
  1574. * the need to configure (or unconfigure) them.
  1575. *
  1576. * After initialization, the device stays active for as long as that
  1577. * $CHIP file is open. Events must then be read from that descriptor,
  1578. * such as configuration notifications.
  1579. */
  1580. static int is_valid_config (struct usb_config_descriptor *config)
  1581. {
  1582. return config->bDescriptorType == USB_DT_CONFIG
  1583. && config->bLength == USB_DT_CONFIG_SIZE
  1584. && config->bConfigurationValue != 0
  1585. && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
  1586. && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
  1587. /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
  1588. /* FIXME check lengths: walk to end */
  1589. }
  1590. static ssize_t
  1591. dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  1592. {
  1593. struct dev_data *dev = fd->private_data;
  1594. ssize_t value = len, length = len;
  1595. unsigned total;
  1596. u32 tag;
  1597. char *kbuf;
  1598. if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
  1599. return -EINVAL;
  1600. /* we might need to change message format someday */
  1601. if (copy_from_user (&tag, buf, 4))
  1602. return -EFAULT;
  1603. if (tag != 0)
  1604. return -EINVAL;
  1605. buf += 4;
  1606. length -= 4;
  1607. kbuf = kmalloc (length, GFP_KERNEL);
  1608. if (!kbuf)
  1609. return -ENOMEM;
  1610. if (copy_from_user (kbuf, buf, length)) {
  1611. kfree (kbuf);
  1612. return -EFAULT;
  1613. }
  1614. spin_lock_irq (&dev->lock);
  1615. value = -EINVAL;
  1616. if (dev->buf)
  1617. goto fail;
  1618. dev->buf = kbuf;
  1619. /* full or low speed config */
  1620. dev->config = (void *) kbuf;
  1621. total = le16_to_cpu(dev->config->wTotalLength);
  1622. if (!is_valid_config (dev->config) || total >= length)
  1623. goto fail;
  1624. kbuf += total;
  1625. length -= total;
  1626. /* optional high speed config */
  1627. if (kbuf [1] == USB_DT_CONFIG) {
  1628. dev->hs_config = (void *) kbuf;
  1629. total = le16_to_cpu(dev->hs_config->wTotalLength);
  1630. if (!is_valid_config (dev->hs_config) || total >= length)
  1631. goto fail;
  1632. kbuf += total;
  1633. length -= total;
  1634. }
  1635. /* could support multiple configs, using another encoding! */
  1636. /* device descriptor (tweaked for paranoia) */
  1637. if (length != USB_DT_DEVICE_SIZE)
  1638. goto fail;
  1639. dev->dev = (void *)kbuf;
  1640. if (dev->dev->bLength != USB_DT_DEVICE_SIZE
  1641. || dev->dev->bDescriptorType != USB_DT_DEVICE
  1642. || dev->dev->bNumConfigurations != 1)
  1643. goto fail;
  1644. dev->dev->bNumConfigurations = 1;
  1645. dev->dev->bcdUSB = cpu_to_le16 (0x0200);
  1646. /* triggers gadgetfs_bind(); then we can enumerate. */
  1647. spin_unlock_irq (&dev->lock);
  1648. value = usb_gadget_register_driver (&gadgetfs_driver);
  1649. if (value != 0) {
  1650. kfree (dev->buf);
  1651. dev->buf = NULL;
  1652. } else {
  1653. /* at this point "good" hardware has for the first time
  1654. * let the USB the host see us. alternatively, if users
  1655. * unplug/replug that will clear all the error state.
  1656. *
  1657. * note: everything running before here was guaranteed
  1658. * to choke driver model style diagnostics. from here
  1659. * on, they can work ... except in cleanup paths that
  1660. * kick in after the ep0 descriptor is closed.
  1661. */
  1662. fd->f_op = &ep0_io_operations;
  1663. value = len;
  1664. }
  1665. return value;
  1666. fail:
  1667. spin_unlock_irq (&dev->lock);
  1668. pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
  1669. kfree (dev->buf);
  1670. dev->buf = NULL;
  1671. return value;
  1672. }
  1673. static int
  1674. dev_open (struct inode *inode, struct file *fd)
  1675. {
  1676. struct dev_data *dev = inode->i_private;
  1677. int value = -EBUSY;
  1678. spin_lock_irq(&dev->lock);
  1679. if (dev->state == STATE_DEV_DISABLED) {
  1680. dev->ev_next = 0;
  1681. dev->state = STATE_DEV_OPENED;
  1682. fd->private_data = dev;
  1683. get_dev (dev);
  1684. value = 0;
  1685. }
  1686. spin_unlock_irq(&dev->lock);
  1687. return value;
  1688. }
  1689. static const struct file_operations dev_init_operations = {
  1690. .owner = THIS_MODULE,
  1691. .llseek = no_llseek,
  1692. .open = dev_open,
  1693. .write = dev_config,
  1694. .fasync = ep0_fasync,
  1695. .unlocked_ioctl = dev_ioctl,
  1696. .release = dev_release,
  1697. };
  1698. /*----------------------------------------------------------------------*/
  1699. /* FILESYSTEM AND SUPERBLOCK OPERATIONS
  1700. *
  1701. * Mounting the filesystem creates a controller file, used first for
  1702. * device configuration then later for event monitoring.
  1703. */
  1704. /* FIXME PAM etc could set this security policy without mount options
  1705. * if epfiles inherited ownership and permissons from ep0 ...
  1706. */
  1707. static unsigned default_uid;
  1708. static unsigned default_gid;
  1709. static