inode.c 52 KB

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