inode.c 53 KB

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