inode.c 51 KB

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