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/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. };
  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 = memdup_user(buf, len);
  374. if (IS_ERR(kbuf)) {
  375. value = PTR_ERR(kbuf);
  376. goto free1;
  377. }
  378. value = ep_io (data, kbuf, len);
  379. VDEBUG (data->dev, "%s write %zu IN, status %d\n",
  380. data->name, len, (int) value);
  381. free1:
  382. mutex_unlock(&data->lock);
  383. return value;
  384. }
  385. static int
  386. ep_release (struct inode *inode, struct file *fd)
  387. {
  388. struct ep_data *data = fd->private_data;
  389. int value;
  390. value = mutex_lock_interruptible(&data->lock);
  391. if (value < 0)
  392. return value;
  393. /* clean up if this can be reopened */
  394. if (data->state != STATE_EP_UNBOUND) {
  395. data->state = STATE_EP_DISABLED;
  396. data->desc.bDescriptorType = 0;
  397. data->hs_desc.bDescriptorType = 0;
  398. usb_ep_disable(data->ep);
  399. }
  400. mutex_unlock(&data->lock);
  401. put_ep (data);
  402. return 0;
  403. }
  404. static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
  405. {
  406. struct ep_data *data = fd->private_data;
  407. int status;
  408. if ((status = get_ready_ep (fd->f_flags, data)) < 0)
  409. return status;
  410. spin_lock_irq (&data->dev->lock);
  411. if (likely (data->ep != NULL)) {
  412. switch (code) {
  413. case GADGETFS_FIFO_STATUS:
  414. status = usb_ep_fifo_status (data->ep);
  415. break;
  416. case GADGETFS_FIFO_FLUSH:
  417. usb_ep_fifo_flush (data->ep);
  418. break;
  419. case GADGETFS_CLEAR_HALT:
  420. status = usb_ep_clear_halt (data->ep);
  421. break;
  422. default:
  423. status = -ENOTTY;
  424. }
  425. } else
  426. status = -ENODEV;
  427. spin_unlock_irq (&data->dev->lock);
  428. mutex_unlock(&data->lock);
  429. return status;
  430. }
  431. /*----------------------------------------------------------------------*/
  432. /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
  433. struct kiocb_priv {
  434. struct usb_request *req;
  435. struct ep_data *epdata;
  436. struct kiocb *iocb;
  437. struct mm_struct *mm;
  438. struct work_struct work;
  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)
  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. if (likely(epdata && epdata->ep && priv->req))
  453. value = usb_ep_dequeue (epdata->ep, priv->req);
  454. else
  455. value = -EINVAL;
  456. // spin_unlock(&epdata->dev->lock);
  457. local_irq_enable();
  458. return value;
  459. }
  460. static ssize_t ep_copy_to_user(struct kiocb_priv *priv)
  461. {
  462. ssize_t len, total;
  463. void *to_copy;
  464. int i;
  465. /* copy stuff into user buffers */
  466. total = priv->actual;
  467. len = 0;
  468. to_copy = priv->buf;
  469. for (i=0; i < priv->nr_segs; i++) {
  470. ssize_t this = min((ssize_t)(priv->iv[i].iov_len), total);
  471. if (copy_to_user(priv->iv[i].iov_base, to_copy, this)) {
  472. if (len == 0)
  473. len = -EFAULT;
  474. break;
  475. }
  476. total -= this;
  477. len += this;
  478. to_copy += this;
  479. if (total == 0)
  480. break;
  481. }
  482. return len;
  483. }
  484. static void ep_user_copy_worker(struct work_struct *work)
  485. {
  486. struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work);
  487. struct mm_struct *mm = priv->mm;
  488. struct kiocb *iocb = priv->iocb;
  489. size_t ret;
  490. use_mm(mm);
  491. ret = ep_copy_to_user(priv);
  492. unuse_mm(mm);
  493. /* completing the iocb can drop the ctx and mm, don't touch mm after */
  494. aio_complete(iocb, ret, ret);
  495. kfree(priv->buf);
  496. kfree(priv);
  497. }
  498. static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
  499. {
  500. struct kiocb *iocb = req->context;
  501. struct kiocb_priv *priv = iocb->private;
  502. struct ep_data *epdata = priv->epdata;
  503. /* lock against disconnect (and ideally, cancel) */
  504. spin_lock(&epdata->dev->lock);
  505. priv->req = NULL;
  506. priv->epdata = NULL;
  507. /* if this was a write or a read returning no data then we
  508. * don't need to copy anything to userspace, so we can
  509. * complete the aio request immediately.
  510. */
  511. if (priv->iv == NULL || unlikely(req->actual == 0)) {
  512. kfree(req->buf);
  513. kfree(priv);
  514. iocb->private = NULL;
  515. /* aio_complete() reports bytes-transferred _and_ faults */
  516. aio_complete(iocb, req->actual ? req->actual : req->status,
  517. req->status);
  518. } else {
  519. /* ep_copy_to_user() won't report both; we hide some faults */
  520. if (unlikely(0 != req->status))
  521. DBG(epdata->dev, "%s fault %d len %d\n",
  522. ep->name, req->status, req->actual);
  523. priv->buf = req->buf;
  524. priv->actual = req->actual;
  525. schedule_work(&priv->work);
  526. }
  527. spin_unlock(&epdata->dev->lock);
  528. usb_ep_free_request(ep, req);
  529. put_ep(epdata);
  530. }
  531. static ssize_t
  532. ep_aio_rwtail(
  533. struct kiocb *iocb,
  534. char *buf,
  535. size_t len,
  536. struct ep_data *epdata,
  537. const struct iovec *iv,
  538. unsigned long nr_segs
  539. )
  540. {
  541. struct kiocb_priv *priv;
  542. struct usb_request *req;
  543. ssize_t value;
  544. priv = kmalloc(sizeof *priv, GFP_KERNEL);
  545. if (!priv) {
  546. value = -ENOMEM;
  547. fail:
  548. kfree(buf);
  549. return value;
  550. }
  551. iocb->private = priv;
  552. priv->iocb = iocb;
  553. priv->iv = iv;
  554. priv->nr_segs = nr_segs;
  555. INIT_WORK(&priv->work, ep_user_copy_worker);
  556. value = get_ready_ep(iocb->ki_filp->f_flags, epdata);
  557. if (unlikely(value < 0)) {
  558. kfree(priv);
  559. goto fail;
  560. }
  561. kiocb_set_cancel_fn(iocb, ep_aio_cancel);
  562. get_ep(epdata);
  563. priv->epdata = epdata;
  564. priv->actual = 0;
  565. priv->mm = current->mm; /* mm teardown waits for iocbs in exit_aio() */
  566. /* each kiocb is coupled to one usb_request, but we can't
  567. * allocate or submit those if the host disconnected.
  568. */
  569. spin_lock_irq(&epdata->dev->lock);
  570. if (likely(epdata->ep)) {
  571. req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
  572. if (likely(req)) {
  573. priv->req = req;
  574. req->buf = buf;
  575. req->length = len;
  576. req->complete = ep_aio_complete;
  577. req->context = iocb;
  578. value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
  579. if (unlikely(0 != value))
  580. usb_ep_free_request(epdata->ep, req);
  581. } else
  582. value = -EAGAIN;
  583. } else
  584. value = -ENODEV;
  585. spin_unlock_irq(&epdata->dev->lock);
  586. mutex_unlock(&epdata->lock);
  587. if (unlikely(value)) {
  588. kfree(priv);
  589. put_ep(epdata);
  590. } else
  591. value = -EIOCBQUEUED;
  592. return value;
  593. }
  594. static ssize_t
  595. ep_aio_read(struct kiocb *iocb, const struct iovec *iov,
  596. unsigned long nr_segs, loff_t o)
  597. {
  598. struct ep_data *epdata = iocb->ki_filp->private_data;
  599. char *buf;
  600. if (unlikely(usb_endpoint_dir_in(&epdata->desc)))
  601. return -EINVAL;
  602. buf = kmalloc(iocb->ki_nbytes, GFP_KERNEL);
  603. if (unlikely(!buf))
  604. return -ENOMEM;
  605. return ep_aio_rwtail(iocb, buf, iocb->ki_nbytes, epdata, iov, nr_segs);
  606. }
  607. static ssize_t
  608. ep_aio_write(struct kiocb *iocb, const struct iovec *iov,
  609. unsigned long nr_segs, loff_t o)
  610. {
  611. struct ep_data *epdata = iocb->ki_filp->private_data;
  612. char *buf;
  613. size_t len = 0;
  614. int i = 0;
  615. if (unlikely(!usb_endpoint_dir_in(&epdata->desc)))
  616. return -EINVAL;
  617. buf = kmalloc(iocb->ki_nbytes, GFP_KERNEL);
  618. if (unlikely(!buf))
  619. return -ENOMEM;
  620. for (i=0; i < nr_segs; i++) {
  621. if (unlikely(copy_from_user(&buf[len], iov[i].iov_base,
  622. iov[i].iov_len) != 0)) {
  623. kfree(buf);
  624. return -EFAULT;
  625. }
  626. len += iov[i].iov_len;
  627. }
  628. return ep_aio_rwtail(iocb, buf, len, epdata, NULL, 0);
  629. }
  630. /*----------------------------------------------------------------------*/
  631. /* used after endpoint configuration */
  632. static const struct file_operations ep_io_operations = {
  633. .owner = THIS_MODULE,
  634. .llseek = no_llseek,
  635. .read = ep_read,
  636. .write = ep_write,
  637. .unlocked_ioctl = ep_ioctl,
  638. .release = ep_release,
  639. .aio_read = ep_aio_read,
  640. .aio_write = ep_aio_write,
  641. };
  642. /* ENDPOINT INITIALIZATION
  643. *
  644. * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
  645. * status = write (fd, descriptors, sizeof descriptors)
  646. *
  647. * That write establishes the endpoint configuration, configuring
  648. * the controller to process bulk, interrupt, or isochronous transfers
  649. * at the right maxpacket size, and so on.
  650. *
  651. * The descriptors are message type 1, identified by a host order u32
  652. * at the beginning of what's written. Descriptor order is: full/low
  653. * speed descriptor, then optional high speed descriptor.
  654. */
  655. static ssize_t
  656. ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  657. {
  658. struct ep_data *data = fd->private_data;
  659. struct usb_ep *ep;
  660. u32 tag;
  661. int value, length = len;
  662. value = mutex_lock_interruptible(&data->lock);
  663. if (value < 0)
  664. return value;
  665. if (data->state != STATE_EP_READY) {
  666. value = -EL2HLT;
  667. goto fail;
  668. }
  669. value = len;
  670. if (len < USB_DT_ENDPOINT_SIZE + 4)
  671. goto fail0;
  672. /* we might need to change message format someday */
  673. if (copy_from_user (&tag, buf, 4)) {
  674. goto fail1;
  675. }
  676. if (tag != 1) {
  677. DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
  678. goto fail0;
  679. }
  680. buf += 4;
  681. len -= 4;
  682. /* NOTE: audio endpoint extensions not accepted here;
  683. * just don't include the extra bytes.
  684. */
  685. /* full/low speed descriptor, then high speed */
  686. if (copy_from_user (&data->desc, buf, USB_DT_ENDPOINT_SIZE)) {
  687. goto fail1;
  688. }
  689. if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
  690. || data->desc.bDescriptorType != USB_DT_ENDPOINT)
  691. goto fail0;
  692. if (len != USB_DT_ENDPOINT_SIZE) {
  693. if (len != 2 * USB_DT_ENDPOINT_SIZE)
  694. goto fail0;
  695. if (copy_from_user (&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
  696. USB_DT_ENDPOINT_SIZE)) {
  697. goto fail1;
  698. }
  699. if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
  700. || data->hs_desc.bDescriptorType
  701. != USB_DT_ENDPOINT) {
  702. DBG(data->dev, "config %s, bad hs length or type\n",
  703. data->name);
  704. goto fail0;
  705. }
  706. }
  707. spin_lock_irq (&data->dev->lock);
  708. if (data->dev->state == STATE_DEV_UNBOUND) {
  709. value = -ENOENT;
  710. goto gone;
  711. } else if ((ep = data->ep) == NULL) {
  712. value = -ENODEV;
  713. goto gone;
  714. }
  715. switch (data->dev->gadget->speed) {
  716. case USB_SPEED_LOW:
  717. case USB_SPEED_FULL:
  718. ep->desc = &data->desc;
  719. value = usb_ep_enable(ep);
  720. if (value == 0)
  721. data->state = STATE_EP_ENABLED;
  722. break;
  723. case USB_SPEED_HIGH:
  724. /* fails if caller didn't provide that descriptor... */
  725. ep->desc = &data->hs_desc;
  726. value = usb_ep_enable(ep);
  727. if (value == 0)
  728. data->state = STATE_EP_ENABLED;
  729. break;
  730. default:
  731. DBG(data->dev, "unconnected, %s init abandoned\n",
  732. data->name);
  733. value = -EINVAL;
  734. }
  735. if (value == 0) {
  736. fd->f_op = &ep_io_operations;
  737. value = length;
  738. }
  739. gone:
  740. spin_unlock_irq (&data->dev->lock);
  741. if (value < 0) {
  742. fail:
  743. data->desc.bDescriptorType = 0;
  744. data->hs_desc.bDescriptorType = 0;
  745. }
  746. mutex_unlock(&data->lock);
  747. return value;
  748. fail0:
  749. value = -EINVAL;
  750. goto fail;
  751. fail1:
  752. value = -EFAULT;
  753. goto fail;
  754. }
  755. static int
  756. ep_open (struct inode *inode, struct file *fd)
  757. {
  758. struct ep_data *data = inode->i_private;
  759. int value = -EBUSY;
  760. if (mutex_lock_interruptible(&data->lock) != 0)
  761. return -EINTR;
  762. spin_lock_irq (&data->dev->lock);
  763. if (data->dev->state == STATE_DEV_UNBOUND)
  764. value = -ENOENT;
  765. else if (data->state == STATE_EP_DISABLED) {
  766. value = 0;
  767. data->state = STATE_EP_READY;
  768. get_ep (data);
  769. fd->private_data = data;
  770. VDEBUG (data->dev, "%s ready\n", data->name);
  771. } else
  772. DBG (data->dev, "%s state %d\n",
  773. data->name, data->state);
  774. spin_unlock_irq (&data->dev->lock);
  775. mutex_unlock(&data->lock);
  776. return value;
  777. }
  778. /* used before endpoint configuration */
  779. static const struct file_operations ep_config_operations = {
  780. .llseek = no_llseek,
  781. .open = ep_open,
  782. .write = ep_config,
  783. .release = ep_release,
  784. };
  785. /*----------------------------------------------------------------------*/
  786. /* EP0 IMPLEMENTATION can be partly in userspace.
  787. *
  788. * Drivers that use this facility receive various events, including
  789. * control requests the kernel doesn't handle. Drivers that don't
  790. * use this facility may be too simple-minded for real applications.
  791. */
  792. static inline void ep0_readable (struct dev_data *dev)
  793. {
  794. wake_up (&dev->wait);
  795. kill_fasync (&dev->fasync, SIGIO, POLL_IN);
  796. }
  797. static void clean_req (struct usb_ep *ep, struct usb_request *req)
  798. {
  799. struct dev_data *dev = ep->driver_data;
  800. if (req->buf != dev->rbuf) {
  801. kfree(req->buf);
  802. req->buf = dev->rbuf;
  803. }
  804. req->complete = epio_complete;
  805. dev->setup_out_ready = 0;
  806. }
  807. static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
  808. {
  809. struct dev_data *dev = ep->driver_data;
  810. unsigned long flags;
  811. int free = 1;
  812. /* for control OUT, data must still get to userspace */
  813. spin_lock_irqsave(&dev->lock, flags);
  814. if (!dev->setup_in) {
  815. dev->setup_out_error = (req->status != 0);
  816. if (!dev->setup_out_error)
  817. free = 0;
  818. dev->setup_out_ready = 1;
  819. ep0_readable (dev);
  820. }
  821. /* clean up as appropriate */
  822. if (free && req->buf != &dev->rbuf)
  823. clean_req (ep, req);
  824. req->complete = epio_complete;
  825. spin_unlock_irqrestore(&dev->lock, flags);
  826. }
  827. static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
  828. {
  829. struct dev_data *dev = ep->driver_data;
  830. if (dev->setup_out_ready) {
  831. DBG (dev, "ep0 request busy!\n");
  832. return -EBUSY;
  833. }
  834. if (len > sizeof (dev->rbuf))
  835. req->buf = kmalloc(len, GFP_ATOMIC);
  836. if (req->buf == NULL) {
  837. req->buf = dev->rbuf;
  838. return -ENOMEM;
  839. }
  840. req->complete = ep0_complete;
  841. req->length = len;
  842. req->zero = 0;
  843. return 0;
  844. }
  845. static ssize_t
  846. ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
  847. {
  848. struct dev_data *dev = fd->private_data;
  849. ssize_t retval;
  850. enum ep0_state state;
  851. spin_lock_irq (&dev->lock);
  852. /* report fd mode change before acting on it */
  853. if (dev->setup_abort) {
  854. dev->setup_abort = 0;
  855. retval = -EIDRM;
  856. goto done;
  857. }
  858. /* control DATA stage */
  859. if ((state = dev->state) == STATE_DEV_SETUP) {
  860. if (dev->setup_in) { /* stall IN */
  861. VDEBUG(dev, "ep0in stall\n");
  862. (void) usb_ep_set_halt (dev->gadget->ep0);
  863. retval = -EL2HLT;
  864. dev->state = STATE_DEV_CONNECTED;
  865. } else if (len == 0) { /* ack SET_CONFIGURATION etc */
  866. struct usb_ep *ep = dev->gadget->ep0;
  867. struct usb_request *req = dev->req;
  868. if ((retval = setup_req (ep, req, 0)) == 0)
  869. retval = usb_ep_queue (ep, req, GFP_ATOMIC);
  870. dev->state = STATE_DEV_CONNECTED;
  871. /* assume that was SET_CONFIGURATION */
  872. if (dev->current_config) {
  873. unsigned power;
  874. if (gadget_is_dualspeed(dev->gadget)
  875. && (dev->gadget->speed
  876. == USB_SPEED_HIGH))
  877. power = dev->hs_config->bMaxPower;
  878. else
  879. power = dev->config->bMaxPower;
  880. usb_gadget_vbus_draw(dev->gadget, 2 * power);
  881. }
  882. } else { /* collect OUT data */
  883. if ((fd->f_flags & O_NONBLOCK) != 0
  884. && !dev->setup_out_ready) {
  885. retval = -EAGAIN;
  886. goto done;
  887. }
  888. spin_unlock_irq (&dev->lock);
  889. retval = wait_event_interruptible (dev->wait,
  890. dev->setup_out_ready != 0);
  891. /* FIXME state could change from under us */
  892. spin_lock_irq (&dev->lock);
  893. if (retval)
  894. goto done;
  895. if (dev->state != STATE_DEV_SETUP) {
  896. retval = -ECANCELED;
  897. goto done;
  898. }
  899. dev->state = STATE_DEV_CONNECTED;
  900. if (dev->setup_out_error)
  901. retval = -EIO;
  902. else {
  903. len = min (len, (size_t)dev->req->actual);
  904. // FIXME don't call this with the spinlock held ...
  905. if (copy_to_user (buf, dev->req->buf, len))
  906. retval = -EFAULT;
  907. else
  908. retval = len;
  909. clean_req (dev->gadget->ep0, dev->req);
  910. /* NOTE userspace can't yet choose to stall */
  911. }
  912. }
  913. goto done;
  914. }
  915. /* else normal: return event data */
  916. if (len < sizeof dev->event [0]) {
  917. retval = -EINVAL;
  918. goto done;
  919. }
  920. len -= len % sizeof (struct usb_gadgetfs_event);
  921. dev->usermode_setup = 1;
  922. scan:
  923. /* return queued events right away */
  924. if (dev->ev_next != 0) {
  925. unsigned i, n;
  926. n = len / sizeof (struct usb_gadgetfs_event);
  927. if (dev->ev_next < n)
  928. n = dev->ev_next;
  929. /* ep0 i/o has special semantics during STATE_DEV_SETUP */
  930. for (i = 0; i < n; i++) {
  931. if (dev->event [i].type == GADGETFS_SETUP) {
  932. dev->state = STATE_DEV_SETUP;
  933. n = i + 1;
  934. break;
  935. }
  936. }
  937. spin_unlock_irq (&dev->lock);
  938. len = n * sizeof (struct usb_gadgetfs_event);
  939. if (copy_to_user (buf, &dev->event, len))
  940. retval = -EFAULT;
  941. else
  942. retval = len;
  943. if (len > 0) {
  944. /* NOTE this doesn't guard against broken drivers;
  945. * concurrent ep0 readers may lose events.
  946. */
  947. spin_lock_irq (&dev->lock);
  948. if (dev->ev_next > n) {
  949. memmove(&dev->event[0], &dev->event[n],
  950. sizeof (struct usb_gadgetfs_event)
  951. * (dev->ev_next - n));
  952. }
  953. dev->ev_next -= n;
  954. spin_unlock_irq (&dev->lock);
  955. }
  956. return retval;
  957. }
  958. if (fd->f_flags & O_NONBLOCK) {
  959. retval = -EAGAIN;
  960. goto done;
  961. }
  962. switch (state) {
  963. default:
  964. DBG (dev, "fail %s, state %d\n", __func__, state);
  965. retval = -ESRCH;
  966. break;
  967. case STATE_DEV_UNCONNECTED:
  968. case STATE_DEV_CONNECTED:
  969. spin_unlock_irq (&dev->lock);
  970. DBG (dev, "%s wait\n", __func__);
  971. /* wait for events */
  972. retval = wait_event_interruptible (dev->wait,
  973. dev->ev_next != 0);
  974. if (retval < 0)
  975. return retval;
  976. spin_lock_irq (&dev->lock);
  977. goto scan;
  978. }
  979. done:
  980. spin_unlock_irq (&dev->lock);
  981. return retval;
  982. }
  983. static struct usb_gadgetfs_event *
  984. next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
  985. {
  986. struct usb_gadgetfs_event *event;
  987. unsigned i;
  988. switch (type) {
  989. /* these events purge the queue */
  990. case GADGETFS_DISCONNECT:
  991. if (dev->state == STATE_DEV_SETUP)
  992. dev->setup_abort = 1;
  993. // FALL THROUGH
  994. case GADGETFS_CONNECT:
  995. dev->ev_next = 0;
  996. break;
  997. case GADGETFS_SETUP: /* previous request timed out */
  998. case GADGETFS_SUSPEND: /* same effect */
  999. /* these events can't be repeated */
  1000. for (i = 0; i != dev->ev_next; i++) {
  1001. if (dev->event [i].type != type)
  1002. continue;
  1003. DBG(dev, "discard old event[%d] %d\n", i, type);
  1004. dev->ev_next--;
  1005. if (i == dev->ev_next)
  1006. break;
  1007. /* indices start at zero, for simplicity */
  1008. memmove (&dev->event [i], &dev->event [i + 1],
  1009. sizeof (struct usb_gadgetfs_event)
  1010. * (dev->ev_next - i));
  1011. }
  1012. break;
  1013. default:
  1014. BUG ();
  1015. }
  1016. VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
  1017. event = &dev->event [dev->ev_next++];
  1018. BUG_ON (dev->ev_next > N_EVENT);
  1019. memset (event, 0, sizeof *event);
  1020. event->type = type;
  1021. return event;
  1022. }
  1023. static ssize_t
  1024. ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  1025. {
  1026. struct dev_data *dev = fd->private_data;
  1027. ssize_t retval = -ESRCH;
  1028. spin_lock_irq (&dev->lock);
  1029. /* report fd mode change before acting on it */
  1030. if (dev->setup_abort) {
  1031. dev->setup_abort = 0;
  1032. retval = -EIDRM;
  1033. /* data and/or status stage for control request */
  1034. } else if (dev->state == STATE_DEV_SETUP) {
  1035. /* IN DATA+STATUS caller makes len <= wLength */
  1036. if (dev->setup_in) {
  1037. retval = setup_req (dev->gadget->ep0, dev->req, len);
  1038. if (retval == 0) {
  1039. dev->state = STATE_DEV_CONNECTED;
  1040. spin_unlock_irq (&dev->lock);
  1041. if (copy_from_user (dev->req->buf, buf, len))
  1042. retval = -EFAULT;
  1043. else {
  1044. if (len < dev->setup_wLength)
  1045. dev->req->zero = 1;
  1046. retval = usb_ep_queue (
  1047. dev->gadget->ep0, dev->req,
  1048. GFP_KERNEL);
  1049. }
  1050. if (retval < 0) {
  1051. spin_lock_irq (&dev->lock);
  1052. clean_req (dev->gadget->ep0, dev->req);
  1053. spin_unlock_irq (&dev->lock);
  1054. } else
  1055. retval = len;
  1056. return retval;
  1057. }
  1058. /* can stall some OUT transfers */
  1059. } else if (dev->setup_can_stall) {
  1060. VDEBUG(dev, "ep0out stall\n");
  1061. (void) usb_ep_set_halt (dev->gadget->ep0);
  1062. retval = -EL2HLT;
  1063. dev->state = STATE_DEV_CONNECTED;
  1064. } else {
  1065. DBG(dev, "bogus ep0out stall!\n");
  1066. }
  1067. } else
  1068. DBG (dev, "fail %s, state %d\n", __func__, dev->state);
  1069. spin_unlock_irq (&dev->lock);
  1070. return retval;
  1071. }
  1072. static int
  1073. ep0_fasync (int f, struct file *fd, int on)
  1074. {
  1075. struct dev_data *dev = fd->private_data;
  1076. // caller must F_SETOWN before signal delivery happens
  1077. VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
  1078. return fasync_helper (f, fd, on, &dev->fasync);
  1079. }
  1080. static struct usb_gadget_driver gadgetfs_driver;
  1081. static int
  1082. dev_release (struct inode *inode, struct file *fd)
  1083. {
  1084. struct dev_data *dev = fd->private_data;
  1085. /* closing ep0 === shutdown all */
  1086. usb_gadget_unregister_driver (&gadgetfs_driver);
  1087. /* at this point "good" hardware has disconnected the
  1088. * device from USB; the host won't see it any more.
  1089. * alternatively, all host requests will time out.
  1090. */
  1091. kfree (dev->buf);
  1092. dev->buf = NULL;
  1093. /* other endpoints were all decoupled from this device */
  1094. spin_lock_irq(&dev->lock);
  1095. dev->state = STATE_DEV_DISABLED;
  1096. spin_unlock_irq(&dev->lock);
  1097. put_dev (dev);
  1098. return 0;
  1099. }
  1100. static unsigned int
  1101. ep0_poll (struct file *fd, poll_table *wait)
  1102. {
  1103. struct dev_data *dev = fd->private_data;
  1104. int mask = 0;
  1105. poll_wait(fd, &dev->wait, wait);
  1106. spin_lock_irq (&dev->lock);
  1107. /* report fd mode change before acting on it */
  1108. if (dev->setup_abort) {
  1109. dev->setup_abort = 0;
  1110. mask = POLLHUP;
  1111. goto out;
  1112. }
  1113. if (dev->state == STATE_DEV_SETUP) {
  1114. if (dev->setup_in || dev->setup_can_stall)
  1115. mask = POLLOUT;
  1116. } else {
  1117. if (dev->ev_next != 0)
  1118. mask = POLLIN;
  1119. }
  1120. out:
  1121. spin_unlock_irq(&dev->lock);
  1122. return mask;
  1123. }
  1124. static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
  1125. {
  1126. struct dev_data *dev = fd->private_data;
  1127. struct usb_gadget *gadget = dev->gadget;
  1128. long ret = -ENOTTY;
  1129. if (gadget->ops->ioctl)
  1130. ret = gadget->ops->ioctl (gadget, code, value);
  1131. return ret;
  1132. }
  1133. /* used after device configuration */
  1134. static const struct file_operations ep0_io_operations = {
  1135. .owner = THIS_MODULE,
  1136. .llseek = no_llseek,
  1137. .read = ep0_read,
  1138. .write = ep0_write,
  1139. .fasync = ep0_fasync,
  1140. .poll = ep0_poll,
  1141. .unlocked_ioctl = dev_ioctl,
  1142. .release = dev_release,
  1143. };
  1144. /*----------------------------------------------------------------------*/
  1145. /* The in-kernel gadget driver handles most ep0 issues, in particular
  1146. * enumerating the single configuration (as provided from user space).
  1147. *
  1148. * Unrecognized ep0 requests may be handled in user space.
  1149. */
  1150. static void make_qualifier (struct dev_data *dev)
  1151. {
  1152. struct usb_qualifier_descriptor qual;
  1153. struct usb_device_descriptor *desc;
  1154. qual.bLength = sizeof qual;
  1155. qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  1156. qual.bcdUSB = cpu_to_le16 (0x0200);
  1157. desc = dev->dev;
  1158. qual.bDeviceClass = desc->bDeviceClass;
  1159. qual.bDeviceSubClass = desc->bDeviceSubClass;
  1160. qual.bDeviceProtocol = desc->bDeviceProtocol;
  1161. /* assumes ep0 uses the same value for both speeds ... */
  1162. qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
  1163. qual.bNumConfigurations = 1;
  1164. qual.bRESERVED = 0;
  1165. memcpy (dev->rbuf, &qual, sizeof qual);
  1166. }
  1167. static int
  1168. config_buf (struct dev_data *dev, u8 type, unsigned index)
  1169. {
  1170. int len;
  1171. int hs = 0;
  1172. /* only one configuration */
  1173. if (index > 0)
  1174. return -EINVAL;
  1175. if (gadget_is_dualspeed(dev->gadget)) {
  1176. hs = (dev->gadget->speed == USB_SPEED_HIGH);
  1177. if (type == USB_DT_OTHER_SPEED_CONFIG)
  1178. hs = !hs;
  1179. }
  1180. if (hs) {
  1181. dev->req->buf = dev->hs_config;
  1182. len = le16_to_cpu(dev->hs_config->wTotalLength);
  1183. } else {
  1184. dev->req->buf = dev->config;
  1185. len = le16_to_cpu(dev->config->wTotalLength);
  1186. }
  1187. ((u8 *)dev->req->buf) [1] = type;
  1188. return len;
  1189. }
  1190. static int
  1191. gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1192. {
  1193. struct dev_data *dev = get_gadget_data (gadget);
  1194. struct usb_request *req = dev->req;
  1195. int value = -EOPNOTSUPP;
  1196. struct usb_gadgetfs_event *event;
  1197. u16 w_value = le16_to_cpu(ctrl->wValue);
  1198. u16 w_length = le16_to_cpu(ctrl->wLength);
  1199. spin_lock (&dev->lock);
  1200. dev->setup_abort = 0;
  1201. if (dev->state == STATE_DEV_UNCONNECTED) {
  1202. if (gadget_is_dualspeed(gadget)
  1203. && gadget->speed == USB_SPEED_HIGH
  1204. && dev->hs_config == NULL) {
  1205. spin_unlock(&dev->lock);
  1206. ERROR (dev, "no high speed config??\n");
  1207. return -EINVAL;
  1208. }
  1209. dev->state = STATE_DEV_CONNECTED;
  1210. INFO (dev, "connected\n");
  1211. event = next_event (dev, GADGETFS_CONNECT);
  1212. event->u.speed = gadget->speed;
  1213. ep0_readable (dev);
  1214. /* host may have given up waiting for response. we can miss control
  1215. * requests handled lower down (device/endpoint status and features);
  1216. * then ep0_{read,write} will report the wrong status. controller
  1217. * driver will have aborted pending i/o.
  1218. */
  1219. } else if (dev->state == STATE_DEV_SETUP)
  1220. dev->setup_abort = 1;
  1221. req->buf = dev->rbuf;
  1222. req->context = NULL;
  1223. value = -EOPNOTSUPP;
  1224. switch (ctrl->bRequest) {
  1225. case USB_REQ_GET_DESCRIPTOR:
  1226. if (ctrl->bRequestType != USB_DIR_IN)
  1227. goto unrecognized;
  1228. switch (w_value >> 8) {
  1229. case USB_DT_DEVICE:
  1230. value = min (w_length, (u16) sizeof *dev->dev);
  1231. dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
  1232. req->buf = dev->dev;
  1233. break;
  1234. case USB_DT_DEVICE_QUALIFIER:
  1235. if (!dev->hs_config)
  1236. break;
  1237. value = min (w_length, (u16)
  1238. sizeof (struct usb_qualifier_descriptor));
  1239. make_qualifier (dev);
  1240. break;
  1241. case USB_DT_OTHER_SPEED_CONFIG:
  1242. // FALLTHROUGH
  1243. case USB_DT_CONFIG:
  1244. value = config_buf (dev,
  1245. w_value >> 8,
  1246. w_value & 0xff);
  1247. if (value >= 0)
  1248. value = min (w_length, (u16) value);
  1249. break;
  1250. case USB_DT_STRING:
  1251. goto unrecognized;
  1252. default: // all others are errors
  1253. break;
  1254. }
  1255. break;
  1256. /* currently one config, two speeds */
  1257. case USB_REQ_SET_CONFIGURATION:
  1258. if (ctrl->bRequestType != 0)
  1259. goto unrecognized;
  1260. if (0 == (u8) w_value) {
  1261. value = 0;
  1262. dev->current_config = 0;
  1263. usb_gadget_vbus_draw(gadget, 8 /* mA */ );
  1264. // user mode expected to disable endpoints
  1265. } else {
  1266. u8 config, power;
  1267. if (gadget_is_dualspeed(gadget)
  1268. && gadget->speed == USB_SPEED_HIGH) {
  1269. config = dev->hs_config->bConfigurationValue;
  1270. power = dev->hs_config->bMaxPower;
  1271. } else {
  1272. config = dev->config->bConfigurationValue;
  1273. power = dev->config->bMaxPower;
  1274. }
  1275. if (config == (u8) w_value) {
  1276. value = 0;
  1277. dev->current_config = config;
  1278. usb_gadget_vbus_draw(gadget, 2 * power);
  1279. }
  1280. }
  1281. /* report SET_CONFIGURATION like any other control request,
  1282. * except that usermode may not stall this. the next
  1283. * request mustn't be allowed start until this finishes:
  1284. * endpoints and threads set up, etc.
  1285. *
  1286. * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
  1287. * has bad/racey automagic that prevents synchronizing here.
  1288. * even kernel mode drivers often miss them.
  1289. */
  1290. if (value == 0) {
  1291. INFO (dev, "configuration #%d\n", dev->current_config);
  1292. usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
  1293. if (dev->usermode_setup) {
  1294. dev->setup_can_stall = 0;
  1295. goto delegate;
  1296. }
  1297. }
  1298. break;
  1299. #ifndef CONFIG_USB_PXA25X
  1300. /* PXA automagically handles this request too */
  1301. case USB_REQ_GET_CONFIGURATION:
  1302. if (ctrl->bRequestType != 0x80)
  1303. goto unrecognized;
  1304. *(u8 *)req->buf = dev->current_config;
  1305. value = min (w_length, (u16) 1);
  1306. break;
  1307. #endif
  1308. default:
  1309. unrecognized:
  1310. VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
  1311. dev->usermode_setup ? "delegate" : "fail",
  1312. ctrl->bRequestType, ctrl->bRequest,
  1313. w_value, le16_to_cpu(ctrl->wIndex), w_length);
  1314. /* if there's an ep0 reader, don't stall */
  1315. if (dev->usermode_setup) {
  1316. dev->setup_can_stall = 1;
  1317. delegate:
  1318. dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
  1319. ? 1 : 0;
  1320. dev->setup_wLength = w_length;
  1321. dev->setup_out_ready = 0;
  1322. dev->setup_out_error = 0;
  1323. value = 0;
  1324. /* read DATA stage for OUT right away */
  1325. if (unlikely (!dev->setup_in && w_length)) {
  1326. value = setup_req (gadget->ep0, dev->req,
  1327. w_length);
  1328. if (value < 0)
  1329. break;
  1330. value = usb_ep_queue (gadget->ep0, dev->req,
  1331. GFP_ATOMIC);
  1332. if (value < 0) {
  1333. clean_req (gadget->ep0, dev->req);
  1334. break;
  1335. }
  1336. /* we can't currently stall these */
  1337. dev->setup_can_stall = 0;
  1338. }
  1339. /* state changes when reader collects event */
  1340. event = next_event (dev, GADGETFS_SETUP);
  1341. event->u.setup = *ctrl;
  1342. ep0_readable (dev);
  1343. spin_unlock (&dev->lock);
  1344. return 0;
  1345. }
  1346. }
  1347. /* proceed with data transfer and status phases? */
  1348. if (value >= 0 && dev->state != STATE_DEV_SETUP) {
  1349. req->length = value;
  1350. req->zero = value < w_length;
  1351. value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  1352. if (value < 0) {
  1353. DBG (dev, "ep_queue --> %d\n", value);
  1354. req->status = 0;
  1355. }
  1356. }
  1357. /* device stalls when value < 0 */
  1358. spin_unlock (&dev->lock);
  1359. return value;
  1360. }
  1361. static void destroy_ep_files (struct dev_data *dev)
  1362. {
  1363. DBG (dev, "%s %d\n", __func__, dev->state);
  1364. /* dev->state must prevent interference */
  1365. spin_lock_irq (&dev->lock);
  1366. while (!list_empty(&dev->epfiles)) {
  1367. struct ep_data *ep;
  1368. struct inode *parent;
  1369. struct dentry *dentry;
  1370. /* break link to FS */
  1371. ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
  1372. list_del_init (&ep->epfiles);
  1373. dentry = ep->dentry;
  1374. ep->dentry = NULL;
  1375. parent = dentry->d_parent->d_inode;
  1376. /* break link to controller */
  1377. if (ep->state == STATE_EP_ENABLED)
  1378. (void) usb_ep_disable (ep->ep);
  1379. ep->state = STATE_EP_UNBOUND;
  1380. usb_ep_free_request (ep->ep, ep->req);
  1381. ep->ep = NULL;
  1382. wake_up (&ep->wait);
  1383. put_ep (ep);
  1384. spin_unlock_irq (&dev->lock);
  1385. /* break link to dcache */
  1386. mutex_lock (&parent->i_mutex);
  1387. d_delete (dentry);
  1388. dput (dentry);
  1389. mutex_unlock (&parent->i_mutex);
  1390. spin_lock_irq (&dev->lock);
  1391. }
  1392. spin_unlock_irq (&dev->lock);
  1393. }
  1394. static struct dentry *
  1395. gadgetfs_create_file (struct super_block *sb, char const *name,
  1396. void *data, const struct file_operations *fops);
  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->dentry = gadgetfs_create_file (dev->sb, data->name,
  1418. data, &ep_config_operations);
  1419. if (!data->dentry)
  1420. goto enomem2;
  1421. list_add_tail (&data->epfiles, &dev->epfiles);
  1422. }
  1423. return 0;
  1424. enomem2:
  1425. usb_ep_free_request (ep, data->req);
  1426. enomem1:
  1427. put_dev (dev);
  1428. kfree (data);
  1429. enomem0:
  1430. DBG (dev, "%s enomem\n", __func__);
  1431. destroy_ep_files (dev);
  1432. return -ENOMEM;
  1433. }
  1434. static void
  1435. gadgetfs_unbind (struct usb_gadget *gadget)
  1436. {
  1437. struct dev_data *dev = get_gadget_data (gadget);
  1438. DBG (dev, "%s\n", __func__);
  1439. spin_lock_irq (&dev->lock);
  1440. dev->state = STATE_DEV_UNBOUND;
  1441. spin_unlock_irq (&dev->lock);
  1442. destroy_ep_files (dev);
  1443. gadget->ep0->driver_data = NULL;
  1444. set_gadget_data (gadget, NULL);
  1445. /* we've already been disconnected ... no i/o is active */
  1446. if (dev->req)
  1447. usb_ep_free_request (gadget->ep0, dev->req);
  1448. DBG (dev, "%s done\n", __func__);
  1449. put_dev (dev);
  1450. }
  1451. static struct dev_data *the_device;
  1452. static int gadgetfs_bind(struct usb_gadget *gadget,
  1453. struct usb_gadget_driver *driver)
  1454. {
  1455. struct dev_data *dev = the_device;
  1456. if (!dev)
  1457. return -ESRCH;
  1458. if (0 != strcmp (CHIP, gadget->name)) {
  1459. pr_err("%s expected %s controller not %s\n",
  1460. shortname, CHIP, gadget->name);
  1461. return -ENODEV;
  1462. }
  1463. set_gadget_data (gadget, dev);
  1464. dev->gadget = gadget;
  1465. gadget->ep0->driver_data = dev;
  1466. /* preallocate control response and buffer */
  1467. dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  1468. if (!dev->req)
  1469. goto enomem;
  1470. dev->req->context = NULL;
  1471. dev->req->complete = epio_complete;
  1472. if (activate_ep_files (dev) < 0)
  1473. goto enomem;
  1474. INFO (dev, "bound to %s driver\n", gadget->name);
  1475. spin_lock_irq(&dev->lock);
  1476. dev->state = STATE_DEV_UNCONNECTED;
  1477. spin_unlock_irq(&dev->lock);
  1478. get_dev (dev);
  1479. return 0;
  1480. enomem:
  1481. gadgetfs_unbind (gadget);
  1482. return -ENOMEM;
  1483. }
  1484. static void
  1485. gadgetfs_disconnect (struct usb_gadget *gadget)
  1486. {
  1487. struct dev_data *dev = get_gadget_data (gadget);
  1488. unsigned long flags;
  1489. spin_lock_irqsave (&dev->lock, flags);
  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_irqrestore (&dev->lock, flags);
  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. .function = (char *) driver_desc,
  1519. .bind = gadgetfs_bind,
  1520. .unbind = gadgetfs_unbind,
  1521. .setup = gadgetfs_setup,
  1522. .reset = gadgetfs_disconnect,
  1523. .disconnect = gadgetfs_disconnect,
  1524. .suspend = gadgetfs_suspend,
  1525. .driver = {
  1526. .name = (char *) shortname,
  1527. },
  1528. };
  1529. /*----------------------------------------------------------------------*/
  1530. static void gadgetfs_nop(struct usb_gadget *arg) { }
  1531. static int gadgetfs_probe(struct usb_gadget *gadget,
  1532. struct usb_gadget_driver *driver)
  1533. {
  1534. CHIP = gadget->name;
  1535. return -EISNAM;
  1536. }
  1537. static struct usb_gadget_driver probe_driver = {
  1538. .max_speed = USB_SPEED_HIGH,
  1539. .bind = gadgetfs_probe,
  1540. .unbind = gadgetfs_nop,
  1541. .setup = (void *)gadgetfs_nop,
  1542. .disconnect = gadgetfs_nop,
  1543. .driver = {
  1544. .name = "nop",
  1545. },
  1546. };
  1547. /* DEVICE INITIALIZATION
  1548. *
  1549. * fd = open ("/dev/gadget/$CHIP", O_RDWR)
  1550. * status = write (fd, descriptors, sizeof descriptors)
  1551. *
  1552. * That write establishes the device configuration, so the kernel can
  1553. * bind to the controller ... guaranteeing it can handle enumeration
  1554. * at all necessary speeds. Descriptor order is:
  1555. *
  1556. * . message tag (u32, host order) ... for now, must be zero; it
  1557. * would change to support features like multi-config devices
  1558. * . full/low speed config ... all wTotalLength bytes (with interface,
  1559. * class, altsetting, endpoint, and other descriptors)
  1560. * . high speed config ... all descriptors, for high speed operation;
  1561. * this one's optional except for high-speed hardware
  1562. * . device descriptor
  1563. *
  1564. * Endpoints are not yet enabled. Drivers must wait until device
  1565. * configuration and interface altsetting changes create
  1566. * the need to configure (or unconfigure) them.
  1567. *
  1568. * After initialization, the device stays active for as long as that
  1569. * $CHIP file is open. Events must then be read from that descriptor,
  1570. * such as configuration notifications.
  1571. */
  1572. static int is_valid_config (struct usb_config_descriptor *config)
  1573. {
  1574. return config->bDescriptorType == USB_DT_CONFIG
  1575. && config->bLength == USB_DT_CONFIG_SIZE
  1576. && config->bConfigurationValue != 0
  1577. && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
  1578. && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
  1579. /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
  1580. /* FIXME check lengths: walk to end */
  1581. }
  1582. static ssize_t
  1583. dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  1584. {
  1585. struct dev_data *dev = fd->private_data;
  1586. ssize_t value = len, length = len;
  1587. unsigned total;
  1588. u32 tag;
  1589. char *kbuf;
  1590. if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
  1591. return -EINVAL;
  1592. /* we might need to change message format someday */
  1593. if (copy_from_user (&tag, buf, 4))
  1594. return -EFAULT;
  1595. if (tag != 0)
  1596. return -EINVAL;
  1597. buf += 4;
  1598. length -= 4;
  1599. kbuf = memdup_user(buf, length);
  1600. if (IS_ERR(kbuf))
  1601. return PTR_ERR(kbuf);
  1602. spin_lock_irq (&dev->lock);
  1603. value = -EINVAL;
  1604. if (dev->buf)
  1605. goto fail;
  1606. dev->buf = kbuf;
  1607. /* full or low speed config */
  1608. dev->config = (void *) kbuf;
  1609. total = le16_to_cpu(dev->config->wTotalLength);
  1610. if (!is_valid_config (dev->config) || total >= length)
  1611. goto fail;
  1612. kbuf += total;
  1613. length -= total;
  1614. /* optional high speed config */
  1615. if (kbuf [1] == USB_DT_CONFIG) {
  1616. dev->hs_config = (void *) kbuf;
  1617. total = le16_to_cpu(dev->hs_config->wTotalLength);
  1618. if (!is_valid_config (dev->hs_config) || total >= length)
  1619. goto fail;
  1620. kbuf += total;
  1621. length -= total;
  1622. }
  1623. /* could support multiple configs, using another encoding! */
  1624. /* device descriptor (tweaked for paranoia) */
  1625. if (length != USB_DT_DEVICE_SIZE)
  1626. goto fail;
  1627. dev->dev = (void *)kbuf;
  1628. if (dev->dev->bLength != USB_DT_DEVICE_SIZE
  1629. || dev->dev->bDescriptorType != USB_DT_DEVICE
  1630. || dev->dev->bNumConfigurations != 1)
  1631. goto fail;
  1632. dev->dev->bNumConfigurations = 1;
  1633. dev->dev->bcdUSB = cpu_to_le16 (0x0200);
  1634. /* triggers gadgetfs_bind(); then we can enumerate. */
  1635. spin_unlock_irq (&dev->lock);
  1636. if (dev->hs_config)
  1637. gadgetfs_driver.max_speed = USB_SPEED_HIGH;
  1638. else
  1639. gadgetfs_driver.max_speed = USB_SPEED_FULL;
  1640. value = usb_gadget_probe_driver(&gadgetfs_driver);
  1641. if (value != 0) {
  1642. kfree (dev->buf);
  1643. dev->buf = NULL;
  1644. } else {
  1645. /* at this point "good" hardware has for the first time
  1646. * let the USB the host see us. alternatively, if users
  1647. * unplug/replug that will clear all the error state.
  1648. *
  1649. * note: everything running before here was guaranteed
  1650. * to choke driver model style diagnostics. from here
  1651. * on, they can work ... except in cleanup paths that
  1652. * kick in after the ep0 descriptor is closed.
  1653. */
  1654. fd->f_op = &ep0_io_operations;
  1655. value = len;
  1656. }
  1657. return value;
  1658. fail:
  1659. spin_unlock_irq (&dev->lock);
  1660. pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
  1661. kfree (dev->buf);
  1662. dev->buf = NULL;
  1663. return value;
  1664. }
  1665. static int
  1666. dev_open (struct inode *inode, struct file *fd)
  1667. {
  1668. struct dev_data *dev = inode->i_private;
  1669. int value = -EBUSY;
  1670. spin_lock_irq(&dev->lock);
  1671. if (dev->state == STATE_DEV_DISABLED) {
  1672. dev->ev_next = 0;
  1673. dev->state = STATE_DEV_OPENED;
  1674. fd->private_data = dev;
  1675. get_dev (dev);
  1676. value = 0;
  1677. }
  1678. spin_unlock_irq(&dev->lock);
  1679. return value;
  1680. }
  1681. static const struct file_operations dev_init_operations = {
  1682. .llseek = no_llseek,
  1683. .open = dev_open,
  1684. .write = dev_config,
  1685. .fasync = ep0_fasync,
  1686. .unlocked_ioctl = dev_ioctl,
  1687. .release = dev_release,
  1688. };
  1689. /*----------------------------------------------------------------------*/
  1690. /* FILESYSTEM AND SUPERBLOCK OPERATIONS
  1691. *
  1692. * Mounting the filesystem creates a controller file, used first for
  1693. * device configuration then later for event monitoring.
  1694. */
  1695. /* FIXME PAM etc could set this security policy without mount options
  1696. * if epfiles inherited ownership and permissons from ep0 ...
  1697. */
  1698. static unsigned default_uid;
  1699. static unsigned default_gid;
  1700. static unsigned default_perm = S_IRUSR | S_IWUSR;
  1701. module_param (default_uid, uint, 0644);
  1702. module_param (default_gid, uint, 0644);
  1703. module_param (default_perm, uint, 0644);
  1704. static struct inode *
  1705. gadgetfs_make_inode (struct super_block *sb,
  1706. void *data, const struct file_operations *fops,
  1707. int mode)
  1708. {
  1709. struct inode *inode = new_inode (sb);
  1710. if (inode) {
  1711. inode->i_ino = get_next_ino();
  1712. inode->i_mode = mode;
  1713. inode->i_uid = make_kuid(&init_user_ns, default_uid);
  1714. inode->i_gid = make_kgid(&init_user_ns, default_gid);
  1715. inode->i_atime = inode->i_mtime = inode->i_ctime
  1716. = CURRENT_TIME;
  1717. inode->i_private = data;
  1718. inode->i_fop = fops;
  1719. }
  1720. return inode;
  1721. }
  1722. /* creates in fs root directory, so non-renamable and non-linkable.
  1723. * so inode and dentry are paired, until device reconfig.
  1724. */
  1725. static struct dentry *
  1726. gadgetfs_create_file (struct super_block *sb, char const *name,
  1727. void *data, const struct file_operations *fops)
  1728. {
  1729. struct dentry *dentry;
  1730. struct inode *inode;
  1731. dentry = d_alloc_name(sb->s_root, name);
  1732. if (!dentry)
  1733. return NULL;
  1734. inode = gadgetfs_make_inode (sb, data, fops,
  1735. S_IFREG | (default_perm & S_IRWXUGO));
  1736. if (!inode) {
  1737. dput(dentry);
  1738. return NULL;
  1739. }
  1740. d_add (dentry, inode);
  1741. return dentry;
  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 dev_data *dev;
  1752. if (the_device)
  1753. return -ESRCH;
  1754. /* fake probe to determine $CHIP */
  1755. CHIP = NULL;
  1756. usb_gadget_probe_driver(&probe_driver);
  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 Enomem;
  1771. inode->i_op = &simple_dir_inode_operations;
  1772. if (!(sb->s_root = d_make_root (inode)))
  1773. goto Enomem;
  1774. /* the ep0 file is named after the controller we expect;
  1775. * user mode code can use it for sanity checks, like we do.
  1776. */
  1777. dev = dev_new ();
  1778. if (!dev)
  1779. goto Enomem;
  1780. dev->sb = sb;
  1781. dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &dev_init_operations);
  1782. if (!dev->dentry) {
  1783. put_dev(dev);
  1784. goto Enomem;
  1785. }
  1786. /* other endpoint files are available after hardware setup,
  1787. * from binding to a controller.
  1788. */
  1789. the_device = dev;
  1790. return 0;
  1791. Enomem:
  1792. return -ENOMEM;
  1793. }
  1794. /* "mount -t gadgetfs path /dev/gadget" ends up here */
  1795. static struct dentry *
  1796. gadgetfs_mount (struct file_system_type *t, int flags,
  1797. const char *path, void *opts)
  1798. {
  1799. return mount_single (t, flags, opts, gadgetfs_fill_super);
  1800. }
  1801. static void
  1802. gadgetfs_kill_sb (struct super_block *sb)
  1803. {
  1804. kill_litter_super (sb);
  1805. if (the_device) {
  1806. put_dev (the_device);
  1807. the_device = NULL;
  1808. }
  1809. }
  1810. /*----------------------------------------------------------------------*/
  1811. static struct file_system_type gadgetfs_type = {
  1812. .owner = THIS_MODULE,
  1813. .name = shortname,
  1814. .mount = gadgetfs_mount,
  1815. .kill_sb = gadgetfs_kill_sb,
  1816. };
  1817. MODULE_ALIAS_FS("gadgetfs");
  1818. /*----------------------------------------------------------------------*/
  1819. static int __init init (void)
  1820. {
  1821. int status;
  1822. status = register_filesystem (&gadgetfs_type);
  1823. if (status == 0)
  1824. pr_info ("%s: %s, version " DRIVER_VERSION "\n",
  1825. shortname, driver_desc);
  1826. return status;
  1827. }
  1828. module_init (init);
  1829. static void __exit cleanup (void)
  1830. {
  1831. pr_debug ("unregister %s\n", shortname);
  1832. unregister_filesystem (&gadgetfs_type);
  1833. }
  1834. module_exit (cleanup);