inode.c 52 KB

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