inode.c 52 KB

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