inode.c 52 KB

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