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