inode.c 53 KB

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