inode.c 51 KB

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