inode.c 52 KB

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