inode.c 52 KB

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