devio.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. /*****************************************************************************/
  2. /*
  3. * devio.c -- User space communication with USB devices.
  4. *
  5. * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * This file implements the usbfs/x/y files, where
  22. * x is the bus number and y the device number.
  23. *
  24. * It allows user space programs/"drivers" to communicate directly
  25. * with USB devices without intervening kernel driver.
  26. *
  27. * Revision history
  28. * 22.12.1999 0.1 Initial release (split from proc_usb.c)
  29. * 04.01.2000 0.2 Turned into its own filesystem
  30. * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
  31. * (CAN-2005-3055)
  32. */
  33. /*****************************************************************************/
  34. #include <linux/fs.h>
  35. #include <linux/mm.h>
  36. #include <linux/slab.h>
  37. #include <linux/smp_lock.h>
  38. #include <linux/signal.h>
  39. #include <linux/poll.h>
  40. #include <linux/module.h>
  41. #include <linux/usb.h>
  42. #include <linux/usbdevice_fs.h>
  43. #include <linux/cdev.h>
  44. #include <linux/notifier.h>
  45. #include <linux/security.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/byteorder.h>
  48. #include <linux/moduleparam.h>
  49. #include "hcd.h" /* for usbcore internals */
  50. #include "usb.h"
  51. #define USB_MAXBUS 64
  52. #define USB_DEVICE_MAX USB_MAXBUS * 128
  53. /* Mutual exclusion for removal, open, and release */
  54. DEFINE_MUTEX(usbfs_mutex);
  55. struct dev_state {
  56. struct list_head list; /* state list */
  57. struct usb_device *dev;
  58. struct file *file;
  59. spinlock_t lock; /* protects the async urb lists */
  60. struct list_head async_pending;
  61. struct list_head async_completed;
  62. wait_queue_head_t wait; /* wake up if a request completed */
  63. unsigned int discsignr;
  64. struct pid *disc_pid;
  65. uid_t disc_uid, disc_euid;
  66. void __user *disccontext;
  67. unsigned long ifclaimed;
  68. u32 secid;
  69. };
  70. struct async {
  71. struct list_head asynclist;
  72. struct dev_state *ps;
  73. struct pid *pid;
  74. uid_t uid, euid;
  75. unsigned int signr;
  76. unsigned int ifnum;
  77. void __user *userbuffer;
  78. void __user *userurb;
  79. struct urb *urb;
  80. int status;
  81. u32 secid;
  82. };
  83. static int usbfs_snoop;
  84. module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
  85. MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
  86. #define snoop(dev, format, arg...) \
  87. do { \
  88. if (usbfs_snoop) \
  89. dev_info(dev , format , ## arg); \
  90. } while (0)
  91. #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
  92. #define MAX_USBFS_BUFFER_SIZE 16384
  93. static inline int connected(struct dev_state *ps)
  94. {
  95. return (!list_empty(&ps->list) &&
  96. ps->dev->state != USB_STATE_NOTATTACHED);
  97. }
  98. static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
  99. {
  100. loff_t ret;
  101. lock_kernel();
  102. switch (orig) {
  103. case 0:
  104. file->f_pos = offset;
  105. ret = file->f_pos;
  106. break;
  107. case 1:
  108. file->f_pos += offset;
  109. ret = file->f_pos;
  110. break;
  111. case 2:
  112. default:
  113. ret = -EINVAL;
  114. }
  115. unlock_kernel();
  116. return ret;
  117. }
  118. static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
  119. loff_t *ppos)
  120. {
  121. struct dev_state *ps = file->private_data;
  122. struct usb_device *dev = ps->dev;
  123. ssize_t ret = 0;
  124. unsigned len;
  125. loff_t pos;
  126. int i;
  127. pos = *ppos;
  128. usb_lock_device(dev);
  129. if (!connected(ps)) {
  130. ret = -ENODEV;
  131. goto err;
  132. } else if (pos < 0) {
  133. ret = -EINVAL;
  134. goto err;
  135. }
  136. if (pos < sizeof(struct usb_device_descriptor)) {
  137. /* 18 bytes - fits on the stack */
  138. struct usb_device_descriptor temp_desc;
  139. memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
  140. le16_to_cpus(&temp_desc.bcdUSB);
  141. le16_to_cpus(&temp_desc.idVendor);
  142. le16_to_cpus(&temp_desc.idProduct);
  143. le16_to_cpus(&temp_desc.bcdDevice);
  144. len = sizeof(struct usb_device_descriptor) - pos;
  145. if (len > nbytes)
  146. len = nbytes;
  147. if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
  148. ret = -EFAULT;
  149. goto err;
  150. }
  151. *ppos += len;
  152. buf += len;
  153. nbytes -= len;
  154. ret += len;
  155. }
  156. pos = sizeof(struct usb_device_descriptor);
  157. for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
  158. struct usb_config_descriptor *config =
  159. (struct usb_config_descriptor *)dev->rawdescriptors[i];
  160. unsigned int length = le16_to_cpu(config->wTotalLength);
  161. if (*ppos < pos + length) {
  162. /* The descriptor may claim to be longer than it
  163. * really is. Here is the actual allocated length. */
  164. unsigned alloclen =
  165. le16_to_cpu(dev->config[i].desc.wTotalLength);
  166. len = length - (*ppos - pos);
  167. if (len > nbytes)
  168. len = nbytes;
  169. /* Simply don't write (skip over) unallocated parts */
  170. if (alloclen > (*ppos - pos)) {
  171. alloclen -= (*ppos - pos);
  172. if (copy_to_user(buf,
  173. dev->rawdescriptors[i] + (*ppos - pos),
  174. min(len, alloclen))) {
  175. ret = -EFAULT;
  176. goto err;
  177. }
  178. }
  179. *ppos += len;
  180. buf += len;
  181. nbytes -= len;
  182. ret += len;
  183. }
  184. pos += length;
  185. }
  186. err:
  187. usb_unlock_device(dev);
  188. return ret;
  189. }
  190. /*
  191. * async list handling
  192. */
  193. static struct async *alloc_async(unsigned int numisoframes)
  194. {
  195. struct async *as;
  196. as = kzalloc(sizeof(struct async), GFP_KERNEL);
  197. if (!as)
  198. return NULL;
  199. as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
  200. if (!as->urb) {
  201. kfree(as);
  202. return NULL;
  203. }
  204. return as;
  205. }
  206. static void free_async(struct async *as)
  207. {
  208. put_pid(as->pid);
  209. kfree(as->urb->transfer_buffer);
  210. kfree(as->urb->setup_packet);
  211. usb_free_urb(as->urb);
  212. kfree(as);
  213. }
  214. static inline void async_newpending(struct async *as)
  215. {
  216. struct dev_state *ps = as->ps;
  217. unsigned long flags;
  218. spin_lock_irqsave(&ps->lock, flags);
  219. list_add_tail(&as->asynclist, &ps->async_pending);
  220. spin_unlock_irqrestore(&ps->lock, flags);
  221. }
  222. static inline void async_removepending(struct async *as)
  223. {
  224. struct dev_state *ps = as->ps;
  225. unsigned long flags;
  226. spin_lock_irqsave(&ps->lock, flags);
  227. list_del_init(&as->asynclist);
  228. spin_unlock_irqrestore(&ps->lock, flags);
  229. }
  230. static inline struct async *async_getcompleted(struct dev_state *ps)
  231. {
  232. unsigned long flags;
  233. struct async *as = NULL;
  234. spin_lock_irqsave(&ps->lock, flags);
  235. if (!list_empty(&ps->async_completed)) {
  236. as = list_entry(ps->async_completed.next, struct async,
  237. asynclist);
  238. list_del_init(&as->asynclist);
  239. }
  240. spin_unlock_irqrestore(&ps->lock, flags);
  241. return as;
  242. }
  243. static inline struct async *async_getpending(struct dev_state *ps,
  244. void __user *userurb)
  245. {
  246. unsigned long flags;
  247. struct async *as;
  248. spin_lock_irqsave(&ps->lock, flags);
  249. list_for_each_entry(as, &ps->async_pending, asynclist)
  250. if (as->userurb == userurb) {
  251. list_del_init(&as->asynclist);
  252. spin_unlock_irqrestore(&ps->lock, flags);
  253. return as;
  254. }
  255. spin_unlock_irqrestore(&ps->lock, flags);
  256. return NULL;
  257. }
  258. static void snoop_urb(struct urb *urb, void __user *userurb)
  259. {
  260. int j;
  261. unsigned char *data = urb->transfer_buffer;
  262. if (!usbfs_snoop)
  263. return;
  264. dev_info(&urb->dev->dev, "direction=%s\n",
  265. usb_urb_dir_in(urb) ? "IN" : "OUT");
  266. dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
  267. dev_info(&urb->dev->dev, "transfer_buffer_length=%d\n",
  268. urb->transfer_buffer_length);
  269. dev_info(&urb->dev->dev, "actual_length=%d\n", urb->actual_length);
  270. dev_info(&urb->dev->dev, "data: ");
  271. for (j = 0; j < urb->transfer_buffer_length; ++j)
  272. printk("%02x ", data[j]);
  273. printk("\n");
  274. }
  275. static void async_completed(struct urb *urb)
  276. {
  277. struct async *as = urb->context;
  278. struct dev_state *ps = as->ps;
  279. struct siginfo sinfo;
  280. spin_lock(&ps->lock);
  281. list_move_tail(&as->asynclist, &ps->async_completed);
  282. spin_unlock(&ps->lock);
  283. as->status = urb->status;
  284. if (as->signr) {
  285. sinfo.si_signo = as->signr;
  286. sinfo.si_errno = as->status;
  287. sinfo.si_code = SI_ASYNCIO;
  288. sinfo.si_addr = as->userurb;
  289. kill_pid_info_as_uid(as->signr, &sinfo, as->pid, as->uid,
  290. as->euid, as->secid);
  291. }
  292. snoop(&urb->dev->dev, "urb complete\n");
  293. snoop_urb(urb, as->userurb);
  294. wake_up(&ps->wait);
  295. }
  296. static void destroy_async(struct dev_state *ps, struct list_head *list)
  297. {
  298. struct async *as;
  299. unsigned long flags;
  300. spin_lock_irqsave(&ps->lock, flags);
  301. while (!list_empty(list)) {
  302. as = list_entry(list->next, struct async, asynclist);
  303. list_del_init(&as->asynclist);
  304. /* drop the spinlock so the completion handler can run */
  305. spin_unlock_irqrestore(&ps->lock, flags);
  306. usb_kill_urb(as->urb);
  307. spin_lock_irqsave(&ps->lock, flags);
  308. }
  309. spin_unlock_irqrestore(&ps->lock, flags);
  310. }
  311. static void destroy_async_on_interface(struct dev_state *ps,
  312. unsigned int ifnum)
  313. {
  314. struct list_head *p, *q, hitlist;
  315. unsigned long flags;
  316. INIT_LIST_HEAD(&hitlist);
  317. spin_lock_irqsave(&ps->lock, flags);
  318. list_for_each_safe(p, q, &ps->async_pending)
  319. if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
  320. list_move_tail(p, &hitlist);
  321. spin_unlock_irqrestore(&ps->lock, flags);
  322. destroy_async(ps, &hitlist);
  323. }
  324. static inline void destroy_all_async(struct dev_state *ps)
  325. {
  326. destroy_async(ps, &ps->async_pending);
  327. }
  328. /*
  329. * interface claims are made only at the request of user level code,
  330. * which can also release them (explicitly or by closing files).
  331. * they're also undone when devices disconnect.
  332. */
  333. static int driver_probe(struct usb_interface *intf,
  334. const struct usb_device_id *id)
  335. {
  336. return -ENODEV;
  337. }
  338. static void driver_disconnect(struct usb_interface *intf)
  339. {
  340. struct dev_state *ps = usb_get_intfdata(intf);
  341. unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
  342. if (!ps)
  343. return;
  344. /* NOTE: this relies on usbcore having canceled and completed
  345. * all pending I/O requests; 2.6 does that.
  346. */
  347. if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
  348. clear_bit(ifnum, &ps->ifclaimed);
  349. else
  350. dev_warn(&intf->dev, "interface number %u out of range\n",
  351. ifnum);
  352. usb_set_intfdata(intf, NULL);
  353. /* force async requests to complete */
  354. destroy_async_on_interface(ps, ifnum);
  355. }
  356. /* The following routines are merely placeholders. There is no way
  357. * to inform a user task about suspend or resumes.
  358. */
  359. static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
  360. {
  361. return 0;
  362. }
  363. static int driver_resume(struct usb_interface *intf)
  364. {
  365. return 0;
  366. }
  367. struct usb_driver usbfs_driver = {
  368. .name = "usbfs",
  369. .probe = driver_probe,
  370. .disconnect = driver_disconnect,
  371. .suspend = driver_suspend,
  372. .resume = driver_resume,
  373. };
  374. static int claimintf(struct dev_state *ps, unsigned int ifnum)
  375. {
  376. struct usb_device *dev = ps->dev;
  377. struct usb_interface *intf;
  378. int err;
  379. if (ifnum >= 8*sizeof(ps->ifclaimed))
  380. return -EINVAL;
  381. /* already claimed */
  382. if (test_bit(ifnum, &ps->ifclaimed))
  383. return 0;
  384. intf = usb_ifnum_to_if(dev, ifnum);
  385. if (!intf)
  386. err = -ENOENT;
  387. else
  388. err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
  389. if (err == 0)
  390. set_bit(ifnum, &ps->ifclaimed);
  391. return err;
  392. }
  393. static int releaseintf(struct dev_state *ps, unsigned int ifnum)
  394. {
  395. struct usb_device *dev;
  396. struct usb_interface *intf;
  397. int err;
  398. err = -EINVAL;
  399. if (ifnum >= 8*sizeof(ps->ifclaimed))
  400. return err;
  401. dev = ps->dev;
  402. intf = usb_ifnum_to_if(dev, ifnum);
  403. if (!intf)
  404. err = -ENOENT;
  405. else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
  406. usb_driver_release_interface(&usbfs_driver, intf);
  407. err = 0;
  408. }
  409. return err;
  410. }
  411. static int checkintf(struct dev_state *ps, unsigned int ifnum)
  412. {
  413. if (ps->dev->state != USB_STATE_CONFIGURED)
  414. return -EHOSTUNREACH;
  415. if (ifnum >= 8*sizeof(ps->ifclaimed))
  416. return -EINVAL;
  417. if (test_bit(ifnum, &ps->ifclaimed))
  418. return 0;
  419. /* if not yet claimed, claim it for the driver */
  420. dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
  421. "interface %u before use\n", task_pid_nr(current),
  422. current->comm, ifnum);
  423. return claimintf(ps, ifnum);
  424. }
  425. static int findintfep(struct usb_device *dev, unsigned int ep)
  426. {
  427. unsigned int i, j, e;
  428. struct usb_interface *intf;
  429. struct usb_host_interface *alts;
  430. struct usb_endpoint_descriptor *endpt;
  431. if (ep & ~(USB_DIR_IN|0xf))
  432. return -EINVAL;
  433. if (!dev->actconfig)
  434. return -ESRCH;
  435. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  436. intf = dev->actconfig->interface[i];
  437. for (j = 0; j < intf->num_altsetting; j++) {
  438. alts = &intf->altsetting[j];
  439. for (e = 0; e < alts->desc.bNumEndpoints; e++) {
  440. endpt = &alts->endpoint[e].desc;
  441. if (endpt->bEndpointAddress == ep)
  442. return alts->desc.bInterfaceNumber;
  443. }
  444. }
  445. }
  446. return -ENOENT;
  447. }
  448. static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
  449. unsigned int index)
  450. {
  451. int ret = 0;
  452. if (ps->dev->state != USB_STATE_UNAUTHENTICATED
  453. && ps->dev->state != USB_STATE_ADDRESS
  454. && ps->dev->state != USB_STATE_CONFIGURED)
  455. return -EHOSTUNREACH;
  456. if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
  457. return 0;
  458. index &= 0xff;
  459. switch (requesttype & USB_RECIP_MASK) {
  460. case USB_RECIP_ENDPOINT:
  461. ret = findintfep(ps->dev, index);
  462. if (ret >= 0)
  463. ret = checkintf(ps, ret);
  464. break;
  465. case USB_RECIP_INTERFACE:
  466. ret = checkintf(ps, index);
  467. break;
  468. }
  469. return ret;
  470. }
  471. static int match_devt(struct device *dev, void *data)
  472. {
  473. return dev->devt == (dev_t) (unsigned long) data;
  474. }
  475. static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
  476. {
  477. struct device *dev;
  478. dev = bus_find_device(&usb_bus_type, NULL,
  479. (void *) (unsigned long) devt, match_devt);
  480. if (!dev)
  481. return NULL;
  482. return container_of(dev, struct usb_device, dev);
  483. }
  484. /*
  485. * file operations
  486. */
  487. static int usbdev_open(struct inode *inode, struct file *file)
  488. {
  489. struct usb_device *dev = NULL;
  490. struct dev_state *ps;
  491. const struct cred *cred = current_cred();
  492. int ret;
  493. lock_kernel();
  494. /* Protect against simultaneous removal or release */
  495. mutex_lock(&usbfs_mutex);
  496. ret = -ENOMEM;
  497. ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
  498. if (!ps)
  499. goto out;
  500. ret = -ENOENT;
  501. /* usbdev device-node */
  502. if (imajor(inode) == USB_DEVICE_MAJOR)
  503. dev = usbdev_lookup_by_devt(inode->i_rdev);
  504. #ifdef CONFIG_USB_DEVICEFS
  505. /* procfs file */
  506. if (!dev) {
  507. dev = inode->i_private;
  508. if (dev && dev->usbfs_dentry &&
  509. dev->usbfs_dentry->d_inode == inode)
  510. usb_get_dev(dev);
  511. else
  512. dev = NULL;
  513. }
  514. #endif
  515. if (!dev || dev->state == USB_STATE_NOTATTACHED)
  516. goto out;
  517. ret = usb_autoresume_device(dev);
  518. if (ret)
  519. goto out;
  520. ret = 0;
  521. ps->dev = dev;
  522. ps->file = file;
  523. spin_lock_init(&ps->lock);
  524. INIT_LIST_HEAD(&ps->list);
  525. INIT_LIST_HEAD(&ps->async_pending);
  526. INIT_LIST_HEAD(&ps->async_completed);
  527. init_waitqueue_head(&ps->wait);
  528. ps->discsignr = 0;
  529. ps->disc_pid = get_pid(task_pid(current));
  530. ps->disc_uid = cred->uid;
  531. ps->disc_euid = cred->euid;
  532. ps->disccontext = NULL;
  533. ps->ifclaimed = 0;
  534. security_task_getsecid(current, &ps->secid);
  535. smp_wmb();
  536. list_add_tail(&ps->list, &dev->filelist);
  537. file->private_data = ps;
  538. snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
  539. current->comm);
  540. out:
  541. if (ret) {
  542. kfree(ps);
  543. usb_put_dev(dev);
  544. }
  545. mutex_unlock(&usbfs_mutex);
  546. unlock_kernel();
  547. return ret;
  548. }
  549. static int usbdev_release(struct inode *inode, struct file *file)
  550. {
  551. struct dev_state *ps = file->private_data;
  552. struct usb_device *dev = ps->dev;
  553. unsigned int ifnum;
  554. struct async *as;
  555. usb_lock_device(dev);
  556. /* Protect against simultaneous open */
  557. mutex_lock(&usbfs_mutex);
  558. list_del_init(&ps->list);
  559. mutex_unlock(&usbfs_mutex);
  560. for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
  561. ifnum++) {
  562. if (test_bit(ifnum, &ps->ifclaimed))
  563. releaseintf(ps, ifnum);
  564. }
  565. destroy_all_async(ps);
  566. usb_autosuspend_device(dev);
  567. usb_unlock_device(dev);
  568. usb_put_dev(dev);
  569. put_pid(ps->disc_pid);
  570. as = async_getcompleted(ps);
  571. while (as) {
  572. free_async(as);
  573. as = async_getcompleted(ps);
  574. }
  575. kfree(ps);
  576. return 0;
  577. }
  578. static int proc_control(struct dev_state *ps, void __user *arg)
  579. {
  580. struct usb_device *dev = ps->dev;
  581. struct usbdevfs_ctrltransfer ctrl;
  582. unsigned int tmo;
  583. unsigned char *tbuf;
  584. unsigned wLength;
  585. int i, j, ret;
  586. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  587. return -EFAULT;
  588. ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
  589. if (ret)
  590. return ret;
  591. wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
  592. if (wLength > PAGE_SIZE)
  593. return -EINVAL;
  594. tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  595. if (!tbuf)
  596. return -ENOMEM;
  597. tmo = ctrl.timeout;
  598. if (ctrl.bRequestType & 0x80) {
  599. if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
  600. ctrl.wLength)) {
  601. free_page((unsigned long)tbuf);
  602. return -EINVAL;
  603. }
  604. snoop(&dev->dev, "control read: bRequest=%02x "
  605. "bRrequestType=%02x wValue=%04x "
  606. "wIndex=%04x wLength=%04x\n",
  607. ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
  608. ctrl.wIndex, ctrl.wLength);
  609. usb_unlock_device(dev);
  610. i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
  611. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  612. tbuf, ctrl.wLength, tmo);
  613. usb_lock_device(dev);
  614. if ((i > 0) && ctrl.wLength) {
  615. if (usbfs_snoop) {
  616. dev_info(&dev->dev, "control read: data ");
  617. for (j = 0; j < i; ++j)
  618. printk("%02x ", (u8)(tbuf)[j]);
  619. printk("\n");
  620. }
  621. if (copy_to_user(ctrl.data, tbuf, i)) {
  622. free_page((unsigned long)tbuf);
  623. return -EFAULT;
  624. }
  625. }
  626. } else {
  627. if (ctrl.wLength) {
  628. if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
  629. free_page((unsigned long)tbuf);
  630. return -EFAULT;
  631. }
  632. }
  633. snoop(&dev->dev, "control write: bRequest=%02x "
  634. "bRrequestType=%02x wValue=%04x "
  635. "wIndex=%04x wLength=%04x\n",
  636. ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
  637. ctrl.wIndex, ctrl.wLength);
  638. if (usbfs_snoop) {
  639. dev_info(&dev->dev, "control write: data: ");
  640. for (j = 0; j < ctrl.wLength; ++j)
  641. printk("%02x ", (unsigned char)(tbuf)[j]);
  642. printk("\n");
  643. }
  644. usb_unlock_device(dev);
  645. i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
  646. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  647. tbuf, ctrl.wLength, tmo);
  648. usb_lock_device(dev);
  649. }
  650. free_page((unsigned long)tbuf);
  651. if (i < 0 && i != -EPIPE) {
  652. dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
  653. "failed cmd %s rqt %u rq %u len %u ret %d\n",
  654. current->comm, ctrl.bRequestType, ctrl.bRequest,
  655. ctrl.wLength, i);
  656. }
  657. return i;
  658. }
  659. static int proc_bulk(struct dev_state *ps, void __user *arg)
  660. {
  661. struct usb_device *dev = ps->dev;
  662. struct usbdevfs_bulktransfer bulk;
  663. unsigned int tmo, len1, pipe;
  664. int len2;
  665. unsigned char *tbuf;
  666. int i, j, ret;
  667. if (copy_from_user(&bulk, arg, sizeof(bulk)))
  668. return -EFAULT;
  669. ret = findintfep(ps->dev, bulk.ep);
  670. if (ret < 0)
  671. return ret;
  672. ret = checkintf(ps, ret);
  673. if (ret)
  674. return ret;
  675. if (bulk.ep & USB_DIR_IN)
  676. pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
  677. else
  678. pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
  679. if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
  680. return -EINVAL;
  681. len1 = bulk.len;
  682. if (len1 > MAX_USBFS_BUFFER_SIZE)
  683. return -EINVAL;
  684. if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
  685. return -ENOMEM;
  686. tmo = bulk.timeout;
  687. if (bulk.ep & 0x80) {
  688. if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
  689. kfree(tbuf);
  690. return -EINVAL;
  691. }
  692. snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
  693. bulk.len, bulk.timeout);
  694. usb_unlock_device(dev);
  695. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  696. usb_lock_device(dev);
  697. if (!i && len2) {
  698. if (usbfs_snoop) {
  699. dev_info(&dev->dev, "bulk read: data ");
  700. for (j = 0; j < len2; ++j)
  701. printk("%02x ", (u8)(tbuf)[j]);
  702. printk("\n");
  703. }
  704. if (copy_to_user(bulk.data, tbuf, len2)) {
  705. kfree(tbuf);
  706. return -EFAULT;
  707. }
  708. }
  709. } else {
  710. if (len1) {
  711. if (copy_from_user(tbuf, bulk.data, len1)) {
  712. kfree(tbuf);
  713. return -EFAULT;
  714. }
  715. }
  716. snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
  717. bulk.len, bulk.timeout);
  718. if (usbfs_snoop) {
  719. dev_info(&dev->dev, "bulk write: data: ");
  720. for (j = 0; j < len1; ++j)
  721. printk("%02x ", (unsigned char)(tbuf)[j]);
  722. printk("\n");
  723. }
  724. usb_unlock_device(dev);
  725. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  726. usb_lock_device(dev);
  727. }
  728. kfree(tbuf);
  729. if (i < 0)
  730. return i;
  731. return len2;
  732. }
  733. static int proc_resetep(struct dev_state *ps, void __user *arg)
  734. {
  735. unsigned int ep;
  736. int ret;
  737. if (get_user(ep, (unsigned int __user *)arg))
  738. return -EFAULT;
  739. ret = findintfep(ps->dev, ep);
  740. if (ret < 0)
  741. return ret;
  742. ret = checkintf(ps, ret);
  743. if (ret)
  744. return ret;
  745. usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
  746. return 0;
  747. }
  748. static int proc_clearhalt(struct dev_state *ps, void __user *arg)
  749. {
  750. unsigned int ep;
  751. int pipe;
  752. int ret;
  753. if (get_user(ep, (unsigned int __user *)arg))
  754. return -EFAULT;
  755. ret = findintfep(ps->dev, ep);
  756. if (ret < 0)
  757. return ret;
  758. ret = checkintf(ps, ret);
  759. if (ret)
  760. return ret;
  761. if (ep & USB_DIR_IN)
  762. pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
  763. else
  764. pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
  765. return usb_clear_halt(ps->dev, pipe);
  766. }
  767. static int proc_getdriver(struct dev_state *ps, void __user *arg)
  768. {
  769. struct usbdevfs_getdriver gd;
  770. struct usb_interface *intf;
  771. int ret;
  772. if (copy_from_user(&gd, arg, sizeof(gd)))
  773. return -EFAULT;
  774. intf = usb_ifnum_to_if(ps->dev, gd.interface);
  775. if (!intf || !intf->dev.driver)
  776. ret = -ENODATA;
  777. else {
  778. strncpy(gd.driver, intf->dev.driver->name,
  779. sizeof(gd.driver));
  780. ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
  781. }
  782. return ret;
  783. }
  784. static int proc_connectinfo(struct dev_state *ps, void __user *arg)
  785. {
  786. struct usbdevfs_connectinfo ci;
  787. ci.devnum = ps->dev->devnum;
  788. ci.slow = ps->dev->speed == USB_SPEED_LOW;
  789. if (copy_to_user(arg, &ci, sizeof(ci)))
  790. return -EFAULT;
  791. return 0;
  792. }
  793. static int proc_resetdevice(struct dev_state *ps)
  794. {
  795. return usb_reset_device(ps->dev);
  796. }
  797. static int proc_setintf(struct dev_state *ps, void __user *arg)
  798. {
  799. struct usbdevfs_setinterface setintf;
  800. int ret;
  801. if (copy_from_user(&setintf, arg, sizeof(setintf)))
  802. return -EFAULT;
  803. if ((ret = checkintf(ps, setintf.interface)))
  804. return ret;
  805. return usb_set_interface(ps->dev, setintf.interface,
  806. setintf.altsetting);
  807. }
  808. static int proc_setconfig(struct dev_state *ps, void __user *arg)
  809. {
  810. int u;
  811. int status = 0;
  812. struct usb_host_config *actconfig;
  813. if (get_user(u, (int __user *)arg))
  814. return -EFAULT;
  815. actconfig = ps->dev->actconfig;
  816. /* Don't touch the device if any interfaces are claimed.
  817. * It could interfere with other drivers' operations, and if
  818. * an interface is claimed by usbfs it could easily deadlock.
  819. */
  820. if (actconfig) {
  821. int i;
  822. for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
  823. if (usb_interface_claimed(actconfig->interface[i])) {
  824. dev_warn(&ps->dev->dev,
  825. "usbfs: interface %d claimed by %s "
  826. "while '%s' sets config #%d\n",
  827. actconfig->interface[i]
  828. ->cur_altsetting
  829. ->desc.bInterfaceNumber,
  830. actconfig->interface[i]
  831. ->dev.driver->name,
  832. current->comm, u);
  833. status = -EBUSY;
  834. break;
  835. }
  836. }
  837. }
  838. /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
  839. * so avoid usb_set_configuration()'s kick to sysfs
  840. */
  841. if (status == 0) {
  842. if (actconfig && actconfig->desc.bConfigurationValue == u)
  843. status = usb_reset_configuration(ps->dev);
  844. else
  845. status = usb_set_configuration(ps->dev, u);
  846. }
  847. return status;
  848. }
  849. static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
  850. struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
  851. void __user *arg)
  852. {
  853. struct usbdevfs_iso_packet_desc *isopkt = NULL;
  854. struct usb_host_endpoint *ep;
  855. struct async *as;
  856. struct usb_ctrlrequest *dr = NULL;
  857. const struct cred *cred = current_cred();
  858. unsigned int u, totlen, isofrmlen;
  859. int ret, ifnum = -1;
  860. int is_in;
  861. if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
  862. USBDEVFS_URB_SHORT_NOT_OK |
  863. USBDEVFS_URB_NO_FSBR |
  864. USBDEVFS_URB_ZERO_PACKET |
  865. USBDEVFS_URB_NO_INTERRUPT))
  866. return -EINVAL;
  867. if (!uurb->buffer)
  868. return -EINVAL;
  869. if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
  870. (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
  871. ifnum = findintfep(ps->dev, uurb->endpoint);
  872. if (ifnum < 0)
  873. return ifnum;
  874. ret = checkintf(ps, ifnum);
  875. if (ret)
  876. return ret;
  877. }
  878. if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
  879. is_in = 1;
  880. ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  881. } else {
  882. is_in = 0;
  883. ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  884. }
  885. if (!ep)
  886. return -ENOENT;
  887. switch(uurb->type) {
  888. case USBDEVFS_URB_TYPE_CONTROL:
  889. if (!usb_endpoint_xfer_control(&ep->desc))
  890. return -EINVAL;
  891. /* min 8 byte setup packet,
  892. * max 8 byte setup plus an arbitrary data stage */
  893. if (uurb->buffer_length < 8 ||
  894. uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
  895. return -EINVAL;
  896. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  897. if (!dr)
  898. return -ENOMEM;
  899. if (copy_from_user(dr, uurb->buffer, 8)) {
  900. kfree(dr);
  901. return -EFAULT;
  902. }
  903. if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
  904. kfree(dr);
  905. return -EINVAL;
  906. }
  907. ret = check_ctrlrecip(ps, dr->bRequestType,
  908. le16_to_cpup(&dr->wIndex));
  909. if (ret) {
  910. kfree(dr);
  911. return ret;
  912. }
  913. uurb->number_of_packets = 0;
  914. uurb->buffer_length = le16_to_cpup(&dr->wLength);
  915. uurb->buffer += 8;
  916. if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
  917. is_in = 1;
  918. uurb->endpoint |= USB_DIR_IN;
  919. } else {
  920. is_in = 0;
  921. uurb->endpoint &= ~USB_DIR_IN;
  922. }
  923. if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  924. uurb->buffer, uurb->buffer_length)) {
  925. kfree(dr);
  926. return -EFAULT;
  927. }
  928. snoop(&ps->dev->dev, "control urb: bRequest=%02x "
  929. "bRrequestType=%02x wValue=%04x "
  930. "wIndex=%04x wLength=%04x\n",
  931. dr->bRequest, dr->bRequestType,
  932. __le16_to_cpup(&dr->wValue),
  933. __le16_to_cpup(&dr->wIndex),
  934. __le16_to_cpup(&dr->wLength));
  935. break;
  936. case USBDEVFS_URB_TYPE_BULK:
  937. switch (usb_endpoint_type(&ep->desc)) {
  938. case USB_ENDPOINT_XFER_CONTROL:
  939. case USB_ENDPOINT_XFER_ISOC:
  940. return -EINVAL;
  941. /* allow single-shot interrupt transfers, at bogus rates */
  942. }
  943. uurb->number_of_packets = 0;
  944. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  945. return -EINVAL;
  946. if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  947. uurb->buffer, uurb->buffer_length))
  948. return -EFAULT;
  949. snoop(&ps->dev->dev, "bulk urb\n");
  950. break;
  951. case USBDEVFS_URB_TYPE_ISO:
  952. /* arbitrary limit */
  953. if (uurb->number_of_packets < 1 ||
  954. uurb->number_of_packets > 128)
  955. return -EINVAL;
  956. if (!usb_endpoint_xfer_isoc(&ep->desc))
  957. return -EINVAL;
  958. isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
  959. uurb->number_of_packets;
  960. if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
  961. return -ENOMEM;
  962. if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
  963. kfree(isopkt);
  964. return -EFAULT;
  965. }
  966. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  967. /* arbitrary limit,
  968. * sufficient for USB 2.0 high-bandwidth iso */
  969. if (isopkt[u].length > 8192) {
  970. kfree(isopkt);
  971. return -EINVAL;
  972. }
  973. totlen += isopkt[u].length;
  974. }
  975. if (totlen > 32768) {
  976. kfree(isopkt);
  977. return -EINVAL;
  978. }
  979. uurb->buffer_length = totlen;
  980. snoop(&ps->dev->dev, "iso urb\n");
  981. break;
  982. case USBDEVFS_URB_TYPE_INTERRUPT:
  983. uurb->number_of_packets = 0;
  984. if (!usb_endpoint_xfer_int(&ep->desc))
  985. return -EINVAL;
  986. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  987. return -EINVAL;
  988. if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  989. uurb->buffer, uurb->buffer_length))
  990. return -EFAULT;
  991. snoop(&ps->dev->dev, "interrupt urb\n");
  992. break;
  993. default:
  994. return -EINVAL;
  995. }
  996. as = alloc_async(uurb->number_of_packets);
  997. if (!as) {
  998. kfree(isopkt);
  999. kfree(dr);
  1000. return -ENOMEM;
  1001. }
  1002. as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
  1003. if (!as->urb->transfer_buffer) {
  1004. kfree(isopkt);
  1005. kfree(dr);
  1006. free_async(as);
  1007. return -ENOMEM;
  1008. }
  1009. as->urb->dev = ps->dev;
  1010. as->urb->pipe = (uurb->type << 30) |
  1011. __create_pipe(ps->dev, uurb->endpoint & 0xf) |
  1012. (uurb->endpoint & USB_DIR_IN);
  1013. /* This tedious sequence is necessary because the URB_* flags
  1014. * are internal to the kernel and subject to change, whereas
  1015. * the USBDEVFS_URB_* flags are a user API and must not be changed.
  1016. */
  1017. u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
  1018. if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
  1019. u |= URB_ISO_ASAP;
  1020. if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
  1021. u |= URB_SHORT_NOT_OK;
  1022. if (uurb->flags & USBDEVFS_URB_NO_FSBR)
  1023. u |= URB_NO_FSBR;
  1024. if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
  1025. u |= URB_ZERO_PACKET;
  1026. if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
  1027. u |= URB_NO_INTERRUPT;
  1028. as->urb->transfer_flags = u;
  1029. as->urb->transfer_buffer_length = uurb->buffer_length;
  1030. as->urb->setup_packet = (unsigned char *)dr;
  1031. as->urb->start_frame = uurb->start_frame;
  1032. as->urb->number_of_packets = uurb->number_of_packets;
  1033. if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
  1034. ps->dev->speed == USB_SPEED_HIGH)
  1035. as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
  1036. else
  1037. as->urb->interval = ep->desc.bInterval;
  1038. as->urb->context = as;
  1039. as->urb->complete = async_completed;
  1040. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1041. as->urb->iso_frame_desc[u].offset = totlen;
  1042. as->urb->iso_frame_desc[u].length = isopkt[u].length;
  1043. totlen += isopkt[u].length;
  1044. }
  1045. kfree(isopkt);
  1046. as->ps = ps;
  1047. as->userurb = arg;
  1048. if (uurb->endpoint & USB_DIR_IN)
  1049. as->userbuffer = uurb->buffer;
  1050. else
  1051. as->userbuffer = NULL;
  1052. as->signr = uurb->signr;
  1053. as->ifnum = ifnum;
  1054. as->pid = get_pid(task_pid(current));
  1055. as->uid = cred->uid;
  1056. as->euid = cred->euid;
  1057. security_task_getsecid(current, &as->secid);
  1058. if (!is_in) {
  1059. if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
  1060. as->urb->transfer_buffer_length)) {
  1061. free_async(as);
  1062. return -EFAULT;
  1063. }
  1064. }
  1065. snoop_urb(as->urb, as->userurb);
  1066. async_newpending(as);
  1067. if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
  1068. dev_printk(KERN_DEBUG, &ps->dev->dev,
  1069. "usbfs: usb_submit_urb returned %d\n", ret);
  1070. async_removepending(as);
  1071. free_async(as);
  1072. return ret;
  1073. }
  1074. return 0;
  1075. }
  1076. static int proc_submiturb(struct dev_state *ps, void __user *arg)
  1077. {
  1078. struct usbdevfs_urb uurb;
  1079. if (copy_from_user(&uurb, arg, sizeof(uurb)))
  1080. return -EFAULT;
  1081. return proc_do_submiturb(ps, &uurb,
  1082. (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
  1083. arg);
  1084. }
  1085. static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
  1086. {
  1087. struct async *as;
  1088. as = async_getpending(ps, arg);
  1089. if (!as)
  1090. return -EINVAL;
  1091. usb_kill_urb(as->urb);
  1092. return 0;
  1093. }
  1094. static int processcompl(struct async *as, void __user * __user *arg)
  1095. {
  1096. struct urb *urb = as->urb;
  1097. struct usbdevfs_urb __user *userurb = as->userurb;
  1098. void __user *addr = as->userurb;
  1099. unsigned int i;
  1100. if (as->userbuffer)
  1101. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1102. urb->transfer_buffer_length))
  1103. return -EFAULT;
  1104. if (put_user(as->status, &userurb->status))
  1105. return -EFAULT;
  1106. if (put_user(urb->actual_length, &userurb->actual_length))
  1107. return -EFAULT;
  1108. if (put_user(urb->error_count, &userurb->error_count))
  1109. return -EFAULT;
  1110. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1111. for (i = 0; i < urb->number_of_packets; i++) {
  1112. if (put_user(urb->iso_frame_desc[i].actual_length,
  1113. &userurb->iso_frame_desc[i].actual_length))
  1114. return -EFAULT;
  1115. if (put_user(urb->iso_frame_desc[i].status,
  1116. &userurb->iso_frame_desc[i].status))
  1117. return -EFAULT;
  1118. }
  1119. }
  1120. free_async(as);
  1121. if (put_user(addr, (void __user * __user *)arg))
  1122. return -EFAULT;
  1123. return 0;
  1124. }
  1125. static struct async *reap_as(struct dev_state *ps)
  1126. {
  1127. DECLARE_WAITQUEUE(wait, current);
  1128. struct async *as = NULL;
  1129. struct usb_device *dev = ps->dev;
  1130. add_wait_queue(&ps->wait, &wait);
  1131. for (;;) {
  1132. __set_current_state(TASK_INTERRUPTIBLE);
  1133. as = async_getcompleted(ps);
  1134. if (as)
  1135. break;
  1136. if (signal_pending(current))
  1137. break;
  1138. usb_unlock_device(dev);
  1139. schedule();
  1140. usb_lock_device(dev);
  1141. }
  1142. remove_wait_queue(&ps->wait, &wait);
  1143. set_current_state(TASK_RUNNING);
  1144. return as;
  1145. }
  1146. static int proc_reapurb(struct dev_state *ps, void __user *arg)
  1147. {
  1148. struct async *as = reap_as(ps);
  1149. if (as)
  1150. return processcompl(as, (void __user * __user *)arg);
  1151. if (signal_pending(current))
  1152. return -EINTR;
  1153. return -EIO;
  1154. }
  1155. static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
  1156. {
  1157. struct async *as;
  1158. if (!(as = async_getcompleted(ps)))
  1159. return -EAGAIN;
  1160. return processcompl(as, (void __user * __user *)arg);
  1161. }
  1162. #ifdef CONFIG_COMPAT
  1163. static int get_urb32(struct usbdevfs_urb *kurb,
  1164. struct usbdevfs_urb32 __user *uurb)
  1165. {
  1166. __u32 uptr;
  1167. if (get_user(kurb->type, &uurb->type) ||
  1168. __get_user(kurb->endpoint, &uurb->endpoint) ||
  1169. __get_user(kurb->status, &uurb->status) ||
  1170. __get_user(kurb->flags, &uurb->flags) ||
  1171. __get_user(kurb->buffer_length, &uurb->buffer_length) ||
  1172. __get_user(kurb->actual_length, &uurb->actual_length) ||
  1173. __get_user(kurb->start_frame, &uurb->start_frame) ||
  1174. __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
  1175. __get_user(kurb->error_count, &uurb->error_count) ||
  1176. __get_user(kurb->signr, &uurb->signr))
  1177. return -EFAULT;
  1178. if (__get_user(uptr, &uurb->buffer))
  1179. return -EFAULT;
  1180. kurb->buffer = compat_ptr(uptr);
  1181. if (__get_user(uptr, &uurb->usercontext))
  1182. return -EFAULT;
  1183. kurb->usercontext = compat_ptr(uptr);
  1184. return 0;
  1185. }
  1186. static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
  1187. {
  1188. struct usbdevfs_urb uurb;
  1189. if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
  1190. return -EFAULT;
  1191. return proc_do_submiturb(ps, &uurb,
  1192. ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
  1193. arg);
  1194. }
  1195. static int processcompl_compat(struct async *as, void __user * __user *arg)
  1196. {
  1197. struct urb *urb = as->urb;
  1198. struct usbdevfs_urb32 __user *userurb = as->userurb;
  1199. void __user *addr = as->userurb;
  1200. unsigned int i;
  1201. if (as->userbuffer)
  1202. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1203. urb->transfer_buffer_length))
  1204. return -EFAULT;
  1205. if (put_user(as->status, &userurb->status))
  1206. return -EFAULT;
  1207. if (put_user(urb->actual_length, &userurb->actual_length))
  1208. return -EFAULT;
  1209. if (put_user(urb->error_count, &userurb->error_count))
  1210. return -EFAULT;
  1211. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1212. for (i = 0; i < urb->number_of_packets; i++) {
  1213. if (put_user(urb->iso_frame_desc[i].actual_length,
  1214. &userurb->iso_frame_desc[i].actual_length))
  1215. return -EFAULT;
  1216. if (put_user(urb->iso_frame_desc[i].status,
  1217. &userurb->iso_frame_desc[i].status))
  1218. return -EFAULT;
  1219. }
  1220. }
  1221. free_async(as);
  1222. if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
  1223. return -EFAULT;
  1224. return 0;
  1225. }
  1226. static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
  1227. {
  1228. struct async *as = reap_as(ps);
  1229. if (as)
  1230. return processcompl_compat(as, (void __user * __user *)arg);
  1231. if (signal_pending(current))
  1232. return -EINTR;
  1233. return -EIO;
  1234. }
  1235. static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
  1236. {
  1237. struct async *as;
  1238. if (!(as = async_getcompleted(ps)))
  1239. return -EAGAIN;
  1240. return processcompl_compat(as, (void __user * __user *)arg);
  1241. }
  1242. #endif
  1243. static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
  1244. {
  1245. struct usbdevfs_disconnectsignal ds;
  1246. if (copy_from_user(&ds, arg, sizeof(ds)))
  1247. return -EFAULT;
  1248. ps->discsignr = ds.signr;
  1249. ps->disccontext = ds.context;
  1250. return 0;
  1251. }
  1252. static int proc_claiminterface(struct dev_state *ps, void __user *arg)
  1253. {
  1254. unsigned int ifnum;
  1255. if (get_user(ifnum, (unsigned int __user *)arg))
  1256. return -EFAULT;
  1257. return claimintf(ps, ifnum);
  1258. }
  1259. static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
  1260. {
  1261. unsigned int ifnum;
  1262. int ret;
  1263. if (get_user(ifnum, (unsigned int __user *)arg))
  1264. return -EFAULT;
  1265. if ((ret = releaseintf(ps, ifnum)) < 0)
  1266. return ret;
  1267. destroy_async_on_interface (ps, ifnum);
  1268. return 0;
  1269. }
  1270. static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
  1271. {
  1272. int size;
  1273. void *buf = NULL;
  1274. int retval = 0;
  1275. struct usb_interface *intf = NULL;
  1276. struct usb_driver *driver = NULL;
  1277. /* alloc buffer */
  1278. if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
  1279. if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
  1280. return -ENOMEM;
  1281. if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
  1282. if (copy_from_user(buf, ctl->data, size)) {
  1283. kfree(buf);
  1284. return -EFAULT;
  1285. }
  1286. } else {
  1287. memset(buf, 0, size);
  1288. }
  1289. }
  1290. if (!connected(ps)) {
  1291. kfree(buf);
  1292. return -ENODEV;
  1293. }
  1294. if (ps->dev->state != USB_STATE_CONFIGURED)
  1295. retval = -EHOSTUNREACH;
  1296. else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
  1297. retval = -EINVAL;
  1298. else switch (ctl->ioctl_code) {
  1299. /* disconnect kernel driver from interface */
  1300. case USBDEVFS_DISCONNECT:
  1301. if (intf->dev.driver) {
  1302. driver = to_usb_driver(intf->dev.driver);
  1303. dev_dbg(&intf->dev, "disconnect by usbfs\n");
  1304. usb_driver_release_interface(driver, intf);
  1305. } else
  1306. retval = -ENODATA;
  1307. break;
  1308. /* let kernel drivers try to (re)bind to the interface */
  1309. case USBDEVFS_CONNECT:
  1310. if (!intf->dev.driver)
  1311. retval = device_attach(&intf->dev);
  1312. else
  1313. retval = -EBUSY;
  1314. break;
  1315. /* talk directly to the interface's driver */
  1316. default:
  1317. if (intf->dev.driver)
  1318. driver = to_usb_driver(intf->dev.driver);
  1319. if (driver == NULL || driver->ioctl == NULL) {
  1320. retval = -ENOTTY;
  1321. } else {
  1322. retval = driver->ioctl(intf, ctl->ioctl_code, buf);
  1323. if (retval == -ENOIOCTLCMD)
  1324. retval = -ENOTTY;
  1325. }
  1326. }
  1327. /* cleanup and return */
  1328. if (retval >= 0
  1329. && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
  1330. && size > 0
  1331. && copy_to_user(ctl->data, buf, size) != 0)
  1332. retval = -EFAULT;
  1333. kfree(buf);
  1334. return retval;
  1335. }
  1336. static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
  1337. {
  1338. struct usbdevfs_ioctl ctrl;
  1339. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1340. return -EFAULT;
  1341. return proc_ioctl(ps, &ctrl);
  1342. }
  1343. #ifdef CONFIG_COMPAT
  1344. static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
  1345. {
  1346. struct usbdevfs_ioctl32 __user *uioc;
  1347. struct usbdevfs_ioctl ctrl;
  1348. u32 udata;
  1349. uioc = compat_ptr((long)arg);
  1350. if (get_user(ctrl.ifno, &uioc->ifno) ||
  1351. get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
  1352. __get_user(udata, &uioc->data))
  1353. return -EFAULT;
  1354. ctrl.data = compat_ptr(udata);
  1355. return proc_ioctl(ps, &ctrl);
  1356. }
  1357. #endif
  1358. /*
  1359. * NOTE: All requests here that have interface numbers as parameters
  1360. * are assuming that somehow the configuration has been prevented from
  1361. * changing. But there's no mechanism to ensure that...
  1362. */
  1363. static int usbdev_ioctl(struct inode *inode, struct file *file,
  1364. unsigned int cmd, unsigned long arg)
  1365. {
  1366. struct dev_state *ps = file->private_data;
  1367. struct usb_device *dev = ps->dev;
  1368. void __user *p = (void __user *)arg;
  1369. int ret = -ENOTTY;
  1370. if (!(file->f_mode & FMODE_WRITE))
  1371. return -EPERM;
  1372. usb_lock_device(dev);
  1373. if (!connected(ps)) {
  1374. usb_unlock_device(dev);
  1375. return -ENODEV;
  1376. }
  1377. switch (cmd) {
  1378. case USBDEVFS_CONTROL:
  1379. snoop(&dev->dev, "%s: CONTROL\n", __func__);
  1380. ret = proc_control(ps, p);
  1381. if (ret >= 0)
  1382. inode->i_mtime = CURRENT_TIME;
  1383. break;
  1384. case USBDEVFS_BULK:
  1385. snoop(&dev->dev, "%s: BULK\n", __func__);
  1386. ret = proc_bulk(ps, p);
  1387. if (ret >= 0)
  1388. inode->i_mtime = CURRENT_TIME;
  1389. break;
  1390. case USBDEVFS_RESETEP:
  1391. snoop(&dev->dev, "%s: RESETEP\n", __func__);
  1392. ret = proc_resetep(ps, p);
  1393. if (ret >= 0)
  1394. inode->i_mtime = CURRENT_TIME;
  1395. break;
  1396. case USBDEVFS_RESET:
  1397. snoop(&dev->dev, "%s: RESET\n", __func__);
  1398. ret = proc_resetdevice(ps);
  1399. break;
  1400. case USBDEVFS_CLEAR_HALT:
  1401. snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
  1402. ret = proc_clearhalt(ps, p);
  1403. if (ret >= 0)
  1404. inode->i_mtime = CURRENT_TIME;
  1405. break;
  1406. case USBDEVFS_GETDRIVER:
  1407. snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
  1408. ret = proc_getdriver(ps, p);
  1409. break;
  1410. case USBDEVFS_CONNECTINFO:
  1411. snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
  1412. ret = proc_connectinfo(ps, p);
  1413. break;
  1414. case USBDEVFS_SETINTERFACE:
  1415. snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
  1416. ret = proc_setintf(ps, p);
  1417. break;
  1418. case USBDEVFS_SETCONFIGURATION:
  1419. snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
  1420. ret = proc_setconfig(ps, p);
  1421. break;
  1422. case USBDEVFS_SUBMITURB:
  1423. snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
  1424. ret = proc_submiturb(ps, p);
  1425. if (ret >= 0)
  1426. inode->i_mtime = CURRENT_TIME;
  1427. break;
  1428. #ifdef CONFIG_COMPAT
  1429. case USBDEVFS_SUBMITURB32:
  1430. snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
  1431. ret = proc_submiturb_compat(ps, p);
  1432. if (ret >= 0)
  1433. inode->i_mtime = CURRENT_TIME;
  1434. break;
  1435. case USBDEVFS_REAPURB32:
  1436. snoop(&dev->dev, "%s: REAPURB32\n", __func__);
  1437. ret = proc_reapurb_compat(ps, p);
  1438. break;
  1439. case USBDEVFS_REAPURBNDELAY32:
  1440. snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__);
  1441. ret = proc_reapurbnonblock_compat(ps, p);
  1442. break;
  1443. case USBDEVFS_IOCTL32:
  1444. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  1445. ret = proc_ioctl_compat(ps, ptr_to_compat(p));
  1446. break;
  1447. #endif
  1448. case USBDEVFS_DISCARDURB:
  1449. snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
  1450. ret = proc_unlinkurb(ps, p);
  1451. break;
  1452. case USBDEVFS_REAPURB:
  1453. snoop(&dev->dev, "%s: REAPURB\n", __func__);
  1454. ret = proc_reapurb(ps, p);
  1455. break;
  1456. case USBDEVFS_REAPURBNDELAY:
  1457. snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__);
  1458. ret = proc_reapurbnonblock(ps, p);
  1459. break;
  1460. case USBDEVFS_DISCSIGNAL:
  1461. snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
  1462. ret = proc_disconnectsignal(ps, p);
  1463. break;
  1464. case USBDEVFS_CLAIMINTERFACE:
  1465. snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
  1466. ret = proc_claiminterface(ps, p);
  1467. break;
  1468. case USBDEVFS_RELEASEINTERFACE:
  1469. snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
  1470. ret = proc_releaseinterface(ps, p);
  1471. break;
  1472. case USBDEVFS_IOCTL:
  1473. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  1474. ret = proc_ioctl_default(ps, p);
  1475. break;
  1476. }
  1477. usb_unlock_device(dev);
  1478. if (ret >= 0)
  1479. inode->i_atime = CURRENT_TIME;
  1480. return ret;
  1481. }
  1482. /* No kernel lock - fine */
  1483. static unsigned int usbdev_poll(struct file *file,
  1484. struct poll_table_struct *wait)
  1485. {
  1486. struct dev_state *ps = file->private_data;
  1487. unsigned int mask = 0;
  1488. poll_wait(file, &ps->wait, wait);
  1489. if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
  1490. mask |= POLLOUT | POLLWRNORM;
  1491. if (!connected(ps))
  1492. mask |= POLLERR | POLLHUP;
  1493. return mask;
  1494. }
  1495. const struct file_operations usbdev_file_operations = {
  1496. .owner = THIS_MODULE,
  1497. .llseek = usbdev_lseek,
  1498. .read = usbdev_read,
  1499. .poll = usbdev_poll,
  1500. .ioctl = usbdev_ioctl,
  1501. .open = usbdev_open,
  1502. .release = usbdev_release,
  1503. };
  1504. static void usbdev_remove(struct usb_device *udev)
  1505. {
  1506. struct dev_state *ps;
  1507. struct siginfo sinfo;
  1508. while (!list_empty(&udev->filelist)) {
  1509. ps = list_entry(udev->filelist.next, struct dev_state, list);
  1510. destroy_all_async(ps);
  1511. wake_up_all(&ps->wait);
  1512. list_del_init(&ps->list);
  1513. if (ps->discsignr) {
  1514. sinfo.si_signo = ps->discsignr;
  1515. sinfo.si_errno = EPIPE;
  1516. sinfo.si_code = SI_ASYNCIO;
  1517. sinfo.si_addr = ps->disccontext;
  1518. kill_pid_info_as_uid(ps->discsignr, &sinfo,
  1519. ps->disc_pid, ps->disc_uid,
  1520. ps->disc_euid, ps->secid);
  1521. }
  1522. }
  1523. }
  1524. #ifdef CONFIG_USB_DEVICE_CLASS
  1525. static struct class *usb_classdev_class;
  1526. static int usb_classdev_add(struct usb_device *dev)
  1527. {
  1528. struct device *cldev;
  1529. cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
  1530. NULL, "usbdev%d.%d", dev->bus->busnum,
  1531. dev->devnum);
  1532. if (IS_ERR(cldev))
  1533. return PTR_ERR(cldev);
  1534. dev->usb_classdev = cldev;
  1535. return 0;
  1536. }
  1537. static void usb_classdev_remove(struct usb_device *dev)
  1538. {
  1539. if (dev->usb_classdev)
  1540. device_unregister(dev->usb_classdev);
  1541. }
  1542. #else
  1543. #define usb_classdev_add(dev) 0
  1544. #define usb_classdev_remove(dev) do {} while (0)
  1545. #endif
  1546. static int usbdev_notify(struct notifier_block *self,
  1547. unsigned long action, void *dev)
  1548. {
  1549. switch (action) {
  1550. case USB_DEVICE_ADD:
  1551. if (usb_classdev_add(dev))
  1552. return NOTIFY_BAD;
  1553. break;
  1554. case USB_DEVICE_REMOVE:
  1555. usb_classdev_remove(dev);
  1556. usbdev_remove(dev);
  1557. break;
  1558. }
  1559. return NOTIFY_OK;
  1560. }
  1561. static struct notifier_block usbdev_nb = {
  1562. .notifier_call = usbdev_notify,
  1563. };
  1564. static struct cdev usb_device_cdev;
  1565. int __init usb_devio_init(void)
  1566. {
  1567. int retval;
  1568. retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
  1569. "usb_device");
  1570. if (retval) {
  1571. printk(KERN_ERR "Unable to register minors for usb_device\n");
  1572. goto out;
  1573. }
  1574. cdev_init(&usb_device_cdev, &usbdev_file_operations);
  1575. retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
  1576. if (retval) {
  1577. printk(KERN_ERR "Unable to get usb_device major %d\n",
  1578. USB_DEVICE_MAJOR);
  1579. goto error_cdev;
  1580. }
  1581. #ifdef CONFIG_USB_DEVICE_CLASS
  1582. usb_classdev_class = class_create(THIS_MODULE, "usb_device");
  1583. if (IS_ERR(usb_classdev_class)) {
  1584. printk(KERN_ERR "Unable to register usb_device class\n");
  1585. retval = PTR_ERR(usb_classdev_class);
  1586. cdev_del(&usb_device_cdev);
  1587. usb_classdev_class = NULL;
  1588. goto out;
  1589. }
  1590. /* devices of this class shadow the major:minor of their parent
  1591. * device, so clear ->dev_kobj to prevent adding duplicate entries
  1592. * to /sys/dev
  1593. */
  1594. usb_classdev_class->dev_kobj = NULL;
  1595. #endif
  1596. usb_register_notify(&usbdev_nb);
  1597. out:
  1598. return retval;
  1599. error_cdev:
  1600. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1601. goto out;
  1602. }
  1603. void usb_devio_cleanup(void)
  1604. {
  1605. usb_unregister_notify(&usbdev_nb);
  1606. #ifdef CONFIG_USB_DEVICE_CLASS
  1607. class_destroy(usb_classdev_class);
  1608. #endif
  1609. cdev_del(&usb_device_cdev);
  1610. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1611. }