inode.c 52 KB

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