inode.c 52 KB

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