inode.c 53 KB

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