inode.c 51 KB

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