inode.c 52 KB

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