inode.c 52 KB

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