devio.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  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. as = async_getcompleted(ps);
  311. while (as) {
  312. free_async(as);
  313. as = async_getcompleted(ps);
  314. }
  315. }
  316. static void destroy_async_on_interface(struct dev_state *ps,
  317. unsigned int ifnum)
  318. {
  319. struct list_head *p, *q, hitlist;
  320. unsigned long flags;
  321. INIT_LIST_HEAD(&hitlist);
  322. spin_lock_irqsave(&ps->lock, flags);
  323. list_for_each_safe(p, q, &ps->async_pending)
  324. if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
  325. list_move_tail(p, &hitlist);
  326. spin_unlock_irqrestore(&ps->lock, flags);
  327. destroy_async(ps, &hitlist);
  328. }
  329. static inline void destroy_all_async(struct dev_state *ps)
  330. {
  331. destroy_async(ps, &ps->async_pending);
  332. }
  333. /*
  334. * interface claims are made only at the request of user level code,
  335. * which can also release them (explicitly or by closing files).
  336. * they're also undone when devices disconnect.
  337. */
  338. static int driver_probe(struct usb_interface *intf,
  339. const struct usb_device_id *id)
  340. {
  341. return -ENODEV;
  342. }
  343. static void driver_disconnect(struct usb_interface *intf)
  344. {
  345. struct dev_state *ps = usb_get_intfdata(intf);
  346. unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
  347. if (!ps)
  348. return;
  349. /* NOTE: this relies on usbcore having canceled and completed
  350. * all pending I/O requests; 2.6 does that.
  351. */
  352. if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
  353. clear_bit(ifnum, &ps->ifclaimed);
  354. else
  355. dev_warn(&intf->dev, "interface number %u out of range\n",
  356. ifnum);
  357. usb_set_intfdata(intf, NULL);
  358. /* force async requests to complete */
  359. destroy_async_on_interface(ps, ifnum);
  360. }
  361. /* The following routines are merely placeholders. There is no way
  362. * to inform a user task about suspend or resumes.
  363. */
  364. static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
  365. {
  366. return 0;
  367. }
  368. static int driver_resume(struct usb_interface *intf)
  369. {
  370. return 0;
  371. }
  372. struct usb_driver usbfs_driver = {
  373. .name = "usbfs",
  374. .probe = driver_probe,
  375. .disconnect = driver_disconnect,
  376. .suspend = driver_suspend,
  377. .resume = driver_resume,
  378. };
  379. static int claimintf(struct dev_state *ps, unsigned int ifnum)
  380. {
  381. struct usb_device *dev = ps->dev;
  382. struct usb_interface *intf;
  383. int err;
  384. if (ifnum >= 8*sizeof(ps->ifclaimed))
  385. return -EINVAL;
  386. /* already claimed */
  387. if (test_bit(ifnum, &ps->ifclaimed))
  388. return 0;
  389. intf = usb_ifnum_to_if(dev, ifnum);
  390. if (!intf)
  391. err = -ENOENT;
  392. else
  393. err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
  394. if (err == 0)
  395. set_bit(ifnum, &ps->ifclaimed);
  396. return err;
  397. }
  398. static int releaseintf(struct dev_state *ps, unsigned int ifnum)
  399. {
  400. struct usb_device *dev;
  401. struct usb_interface *intf;
  402. int err;
  403. err = -EINVAL;
  404. if (ifnum >= 8*sizeof(ps->ifclaimed))
  405. return err;
  406. dev = ps->dev;
  407. intf = usb_ifnum_to_if(dev, ifnum);
  408. if (!intf)
  409. err = -ENOENT;
  410. else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
  411. usb_driver_release_interface(&usbfs_driver, intf);
  412. err = 0;
  413. }
  414. return err;
  415. }
  416. static int checkintf(struct dev_state *ps, unsigned int ifnum)
  417. {
  418. if (ps->dev->state != USB_STATE_CONFIGURED)
  419. return -EHOSTUNREACH;
  420. if (ifnum >= 8*sizeof(ps->ifclaimed))
  421. return -EINVAL;
  422. if (test_bit(ifnum, &ps->ifclaimed))
  423. return 0;
  424. /* if not yet claimed, claim it for the driver */
  425. dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
  426. "interface %u before use\n", task_pid_nr(current),
  427. current->comm, ifnum);
  428. return claimintf(ps, ifnum);
  429. }
  430. static int findintfep(struct usb_device *dev, unsigned int ep)
  431. {
  432. unsigned int i, j, e;
  433. struct usb_interface *intf;
  434. struct usb_host_interface *alts;
  435. struct usb_endpoint_descriptor *endpt;
  436. if (ep & ~(USB_DIR_IN|0xf))
  437. return -EINVAL;
  438. if (!dev->actconfig)
  439. return -ESRCH;
  440. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  441. intf = dev->actconfig->interface[i];
  442. for (j = 0; j < intf->num_altsetting; j++) {
  443. alts = &intf->altsetting[j];
  444. for (e = 0; e < alts->desc.bNumEndpoints; e++) {
  445. endpt = &alts->endpoint[e].desc;
  446. if (endpt->bEndpointAddress == ep)
  447. return alts->desc.bInterfaceNumber;
  448. }
  449. }
  450. }
  451. return -ENOENT;
  452. }
  453. static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
  454. unsigned int index)
  455. {
  456. int ret = 0;
  457. if (ps->dev->state != USB_STATE_ADDRESS
  458. && ps->dev->state != USB_STATE_CONFIGURED)
  459. return -EHOSTUNREACH;
  460. if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
  461. return 0;
  462. index &= 0xff;
  463. switch (requesttype & USB_RECIP_MASK) {
  464. case USB_RECIP_ENDPOINT:
  465. ret = findintfep(ps->dev, index);
  466. if (ret >= 0)
  467. ret = checkintf(ps, ret);
  468. break;
  469. case USB_RECIP_INTERFACE:
  470. ret = checkintf(ps, index);
  471. break;
  472. }
  473. return ret;
  474. }
  475. static int match_devt(struct device *dev, void *data)
  476. {
  477. return dev->devt == (dev_t) (unsigned long) data;
  478. }
  479. static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
  480. {
  481. struct device *dev;
  482. dev = bus_find_device(&usb_bus_type, NULL,
  483. (void *) (unsigned long) devt, match_devt);
  484. if (!dev)
  485. return NULL;
  486. return container_of(dev, struct usb_device, dev);
  487. }
  488. /*
  489. * file operations
  490. */
  491. static int usbdev_open(struct inode *inode, struct file *file)
  492. {
  493. struct usb_device *dev = NULL;
  494. struct dev_state *ps;
  495. const struct cred *cred = current_cred();
  496. int ret;
  497. lock_kernel();
  498. /* Protect against simultaneous removal or release */
  499. mutex_lock(&usbfs_mutex);
  500. ret = -ENOMEM;
  501. ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
  502. if (!ps)
  503. goto out;
  504. ret = -ENOENT;
  505. /* usbdev device-node */
  506. if (imajor(inode) == USB_DEVICE_MAJOR)
  507. dev = usbdev_lookup_by_devt(inode->i_rdev);
  508. #ifdef CONFIG_USB_DEVICEFS
  509. /* procfs file */
  510. if (!dev) {
  511. dev = inode->i_private;
  512. if (dev && dev->usbfs_dentry &&
  513. dev->usbfs_dentry->d_inode == inode)
  514. usb_get_dev(dev);
  515. else
  516. dev = NULL;
  517. }
  518. #endif
  519. if (!dev || dev->state == USB_STATE_NOTATTACHED)
  520. goto out;
  521. ret = usb_autoresume_device(dev);
  522. if (ret)
  523. goto out;
  524. ret = 0;
  525. ps->dev = dev;
  526. ps->file = file;
  527. spin_lock_init(&ps->lock);
  528. INIT_LIST_HEAD(&ps->list);
  529. INIT_LIST_HEAD(&ps->async_pending);
  530. INIT_LIST_HEAD(&ps->async_completed);
  531. init_waitqueue_head(&ps->wait);
  532. ps->discsignr = 0;
  533. ps->disc_pid = get_pid(task_pid(current));
  534. ps->disc_uid = cred->uid;
  535. ps->disc_euid = cred->euid;
  536. ps->disccontext = NULL;
  537. ps->ifclaimed = 0;
  538. security_task_getsecid(current, &ps->secid);
  539. smp_wmb();
  540. list_add_tail(&ps->list, &dev->filelist);
  541. file->private_data = ps;
  542. snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
  543. current->comm);
  544. out:
  545. if (ret) {
  546. kfree(ps);
  547. usb_put_dev(dev);
  548. }
  549. mutex_unlock(&usbfs_mutex);
  550. unlock_kernel();
  551. return ret;
  552. }
  553. static int usbdev_release(struct inode *inode, struct file *file)
  554. {
  555. struct dev_state *ps = file->private_data;
  556. struct usb_device *dev = ps->dev;
  557. unsigned int ifnum;
  558. usb_lock_device(dev);
  559. /* Protect against simultaneous open */
  560. mutex_lock(&usbfs_mutex);
  561. list_del_init(&ps->list);
  562. mutex_unlock(&usbfs_mutex);
  563. for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
  564. ifnum++) {
  565. if (test_bit(ifnum, &ps->ifclaimed))
  566. releaseintf(ps, ifnum);
  567. }
  568. destroy_all_async(ps);
  569. usb_autosuspend_device(dev);
  570. usb_unlock_device(dev);
  571. usb_put_dev(dev);
  572. put_pid(ps->disc_pid);
  573. kfree(ps);
  574. return 0;
  575. }
  576. static int proc_control(struct dev_state *ps, void __user *arg)
  577. {
  578. struct usb_device *dev = ps->dev;
  579. struct usbdevfs_ctrltransfer ctrl;
  580. unsigned int tmo;
  581. unsigned char *tbuf;
  582. unsigned wLength;
  583. int i, j, ret;
  584. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  585. return -EFAULT;
  586. ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
  587. if (ret)
  588. return ret;
  589. wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
  590. if (wLength > PAGE_SIZE)
  591. return -EINVAL;
  592. tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  593. if (!tbuf)
  594. return -ENOMEM;
  595. tmo = ctrl.timeout;
  596. if (ctrl.bRequestType & 0x80) {
  597. if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
  598. ctrl.wLength)) {
  599. free_page((unsigned long)tbuf);
  600. return -EINVAL;
  601. }
  602. snoop(&dev->dev, "control read: bRequest=%02x "
  603. "bRrequestType=%02x wValue=%04x "
  604. "wIndex=%04x wLength=%04x\n",
  605. ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
  606. ctrl.wIndex, ctrl.wLength);
  607. usb_unlock_device(dev);
  608. i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
  609. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  610. tbuf, ctrl.wLength, tmo);
  611. usb_lock_device(dev);
  612. if ((i > 0) && ctrl.wLength) {
  613. if (usbfs_snoop) {
  614. dev_info(&dev->dev, "control read: data ");
  615. for (j = 0; j < i; ++j)
  616. printk("%02x ", (u8)(tbuf)[j]);
  617. printk("\n");
  618. }
  619. if (copy_to_user(ctrl.data, tbuf, i)) {
  620. free_page((unsigned long)tbuf);
  621. return -EFAULT;
  622. }
  623. }
  624. } else {
  625. if (ctrl.wLength) {
  626. if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
  627. free_page((unsigned long)tbuf);
  628. return -EFAULT;
  629. }
  630. }
  631. snoop(&dev->dev, "control write: bRequest=%02x "
  632. "bRrequestType=%02x wValue=%04x "
  633. "wIndex=%04x wLength=%04x\n",
  634. ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
  635. ctrl.wIndex, ctrl.wLength);
  636. if (usbfs_snoop) {
  637. dev_info(&dev->dev, "control write: data: ");
  638. for (j = 0; j < ctrl.wLength; ++j)
  639. printk("%02x ", (unsigned char)(tbuf)[j]);
  640. printk("\n");
  641. }
  642. usb_unlock_device(dev);
  643. i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
  644. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  645. tbuf, ctrl.wLength, tmo);
  646. usb_lock_device(dev);
  647. }
  648. free_page((unsigned long)tbuf);
  649. if (i < 0 && i != -EPIPE) {
  650. dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
  651. "failed cmd %s rqt %u rq %u len %u ret %d\n",
  652. current->comm, ctrl.bRequestType, ctrl.bRequest,
  653. ctrl.wLength, i);
  654. }
  655. return i;
  656. }
  657. static int proc_bulk(struct dev_state *ps, void __user *arg)
  658. {
  659. struct usb_device *dev = ps->dev;
  660. struct usbdevfs_bulktransfer bulk;
  661. unsigned int tmo, len1, pipe;
  662. int len2;
  663. unsigned char *tbuf;
  664. int i, j, ret;
  665. if (copy_from_user(&bulk, arg, sizeof(bulk)))
  666. return -EFAULT;
  667. ret = findintfep(ps->dev, bulk.ep);
  668. if (ret < 0)
  669. return ret;
  670. ret = checkintf(ps, ret);
  671. if (ret)
  672. return ret;
  673. if (bulk.ep & USB_DIR_IN)
  674. pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
  675. else
  676. pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
  677. if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
  678. return -EINVAL;
  679. len1 = bulk.len;
  680. if (len1 > MAX_USBFS_BUFFER_SIZE)
  681. return -EINVAL;
  682. if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
  683. return -ENOMEM;
  684. tmo = bulk.timeout;
  685. if (bulk.ep & 0x80) {
  686. if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
  687. kfree(tbuf);
  688. return -EINVAL;
  689. }
  690. snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
  691. bulk.len, bulk.timeout);
  692. usb_unlock_device(dev);
  693. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  694. usb_lock_device(dev);
  695. if (!i && len2) {
  696. if (usbfs_snoop) {
  697. dev_info(&dev->dev, "bulk read: data ");
  698. for (j = 0; j < len2; ++j)
  699. printk("%02x ", (u8)(tbuf)[j]);
  700. printk("\n");
  701. }
  702. if (copy_to_user(bulk.data, tbuf, len2)) {
  703. kfree(tbuf);
  704. return -EFAULT;
  705. }
  706. }
  707. } else {
  708. if (len1) {
  709. if (copy_from_user(tbuf, bulk.data, len1)) {
  710. kfree(tbuf);
  711. return -EFAULT;
  712. }
  713. }
  714. snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
  715. bulk.len, bulk.timeout);
  716. if (usbfs_snoop) {
  717. dev_info(&dev->dev, "bulk write: data: ");
  718. for (j = 0; j < len1; ++j)
  719. printk("%02x ", (unsigned char)(tbuf)[j]);
  720. printk("\n");
  721. }
  722. usb_unlock_device(dev);
  723. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  724. usb_lock_device(dev);
  725. }
  726. kfree(tbuf);
  727. if (i < 0)
  728. return i;
  729. return len2;
  730. }
  731. static int proc_resetep(struct dev_state *ps, void __user *arg)
  732. {
  733. unsigned int ep;
  734. int ret;
  735. if (get_user(ep, (unsigned int __user *)arg))
  736. return -EFAULT;
  737. ret = findintfep(ps->dev, ep);
  738. if (ret < 0)
  739. return ret;
  740. ret = checkintf(ps, ret);
  741. if (ret)
  742. return ret;
  743. usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
  744. return 0;
  745. }
  746. static int proc_clearhalt(struct dev_state *ps, void __user *arg)
  747. {
  748. unsigned int ep;
  749. int pipe;
  750. int ret;
  751. if (get_user(ep, (unsigned int __user *)arg))
  752. return -EFAULT;
  753. ret = findintfep(ps->dev, ep);
  754. if (ret < 0)
  755. return ret;
  756. ret = checkintf(ps, ret);
  757. if (ret)
  758. return ret;
  759. if (ep & USB_DIR_IN)
  760. pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
  761. else
  762. pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
  763. return usb_clear_halt(ps->dev, pipe);
  764. }
  765. static int proc_getdriver(struct dev_state *ps, void __user *arg)
  766. {
  767. struct usbdevfs_getdriver gd;
  768. struct usb_interface *intf;
  769. int ret;
  770. if (copy_from_user(&gd, arg, sizeof(gd)))
  771. return -EFAULT;
  772. intf = usb_ifnum_to_if(ps->dev, gd.interface);
  773. if (!intf || !intf->dev.driver)
  774. ret = -ENODATA;
  775. else {
  776. strncpy(gd.driver, intf->dev.driver->name,
  777. sizeof(gd.driver));
  778. ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
  779. }
  780. return ret;
  781. }
  782. static int proc_connectinfo(struct dev_state *ps, void __user *arg)
  783. {
  784. struct usbdevfs_connectinfo ci;
  785. ci.devnum = ps->dev->devnum;
  786. ci.slow = ps->dev->speed == USB_SPEED_LOW;
  787. if (copy_to_user(arg, &ci, sizeof(ci)))
  788. return -EFAULT;
  789. return 0;
  790. }
  791. static int proc_resetdevice(struct dev_state *ps)
  792. {
  793. return usb_reset_device(ps->dev);
  794. }
  795. static int proc_setintf(struct dev_state *ps, void __user *arg)
  796. {
  797. struct usbdevfs_setinterface setintf;
  798. int ret;
  799. if (copy_from_user(&setintf, arg, sizeof(setintf)))
  800. return -EFAULT;
  801. if ((ret = checkintf(ps, setintf.interface)))
  802. return ret;
  803. return usb_set_interface(ps->dev, setintf.interface,
  804. setintf.altsetting);
  805. }
  806. static int proc_setconfig(struct dev_state *ps, void __user *arg)
  807. {
  808. int u;
  809. int status = 0;
  810. struct usb_host_config *actconfig;
  811. if (get_user(u, (int __user *)arg))
  812. return -EFAULT;
  813. actconfig = ps->dev->actconfig;
  814. /* Don't touch the device if any interfaces are claimed.
  815. * It could interfere with other drivers' operations, and if
  816. * an interface is claimed by usbfs it could easily deadlock.
  817. */
  818. if (actconfig) {
  819. int i;
  820. for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
  821. if (usb_interface_claimed(actconfig->interface[i])) {
  822. dev_warn(&ps->dev->dev,
  823. "usbfs: interface %d claimed by %s "
  824. "while '%s' sets config #%d\n",
  825. actconfig->interface[i]
  826. ->cur_altsetting
  827. ->desc.bInterfaceNumber,
  828. actconfig->interface[i]
  829. ->dev.driver->name,
  830. current->comm, u);
  831. status = -EBUSY;
  832. break;
  833. }
  834. }
  835. }
  836. /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
  837. * so avoid usb_set_configuration()'s kick to sysfs
  838. */
  839. if (status == 0) {
  840. if (actconfig && actconfig->desc.bConfigurationValue == u)
  841. status = usb_reset_configuration(ps->dev);
  842. else
  843. status = usb_set_configuration(ps->dev, u);
  844. }
  845. return status;
  846. }
  847. static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
  848. struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
  849. void __user *arg)
  850. {
  851. struct usbdevfs_iso_packet_desc *isopkt = NULL;
  852. struct usb_host_endpoint *ep;
  853. struct async *as;
  854. struct usb_ctrlrequest *dr = NULL;
  855. const struct cred *cred = current_cred();
  856. unsigned int u, totlen, isofrmlen;
  857. int ret, ifnum = -1;
  858. int is_in;
  859. if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
  860. USBDEVFS_URB_SHORT_NOT_OK |
  861. USBDEVFS_URB_NO_FSBR |
  862. USBDEVFS_URB_ZERO_PACKET |
  863. USBDEVFS_URB_NO_INTERRUPT))
  864. return -EINVAL;
  865. if (!uurb->buffer)
  866. return -EINVAL;
  867. if (uurb->signr != 0 && (uurb->signr < SIGRTMIN ||
  868. uurb->signr > SIGRTMAX))
  869. return -EINVAL;
  870. if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
  871. (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
  872. ifnum = findintfep(ps->dev, uurb->endpoint);
  873. if (ifnum < 0)
  874. return ifnum;
  875. ret = checkintf(ps, ifnum);
  876. if (ret)
  877. return ret;
  878. }
  879. if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
  880. is_in = 1;
  881. ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  882. } else {
  883. is_in = 0;
  884. ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  885. }
  886. if (!ep)
  887. return -ENOENT;
  888. switch(uurb->type) {
  889. case USBDEVFS_URB_TYPE_CONTROL:
  890. if (!usb_endpoint_xfer_control(&ep->desc))
  891. return -EINVAL;
  892. /* min 8 byte setup packet,
  893. * max 8 byte setup plus an arbitrary data stage */
  894. if (uurb->buffer_length < 8 ||
  895. uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
  896. return -EINVAL;
  897. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  898. if (!dr)
  899. return -ENOMEM;
  900. if (copy_from_user(dr, uurb->buffer, 8)) {
  901. kfree(dr);
  902. return -EFAULT;
  903. }
  904. if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
  905. kfree(dr);
  906. return -EINVAL;
  907. }
  908. ret = check_ctrlrecip(ps, dr->bRequestType,
  909. le16_to_cpup(&dr->wIndex));
  910. if (ret) {
  911. kfree(dr);
  912. return ret;
  913. }
  914. uurb->number_of_packets = 0;
  915. uurb->buffer_length = le16_to_cpup(&dr->wLength);
  916. uurb->buffer += 8;
  917. if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
  918. is_in = 1;
  919. uurb->endpoint |= USB_DIR_IN;
  920. } else {
  921. is_in = 0;
  922. uurb->endpoint &= ~USB_DIR_IN;
  923. }
  924. if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  925. uurb->buffer, uurb->buffer_length)) {
  926. kfree(dr);
  927. return -EFAULT;
  928. }
  929. snoop(&ps->dev->dev, "control urb: bRequest=%02x "
  930. "bRrequestType=%02x wValue=%04x "
  931. "wIndex=%04x wLength=%04x\n",
  932. dr->bRequest, dr->bRequestType,
  933. __le16_to_cpup(&dr->wValue),
  934. __le16_to_cpup(&dr->wIndex),
  935. __le16_to_cpup(&dr->wLength));
  936. break;
  937. case USBDEVFS_URB_TYPE_BULK:
  938. switch (usb_endpoint_type(&ep->desc)) {
  939. case USB_ENDPOINT_XFER_CONTROL:
  940. case USB_ENDPOINT_XFER_ISOC:
  941. return -EINVAL;
  942. /* allow single-shot interrupt transfers, at bogus rates */
  943. }
  944. uurb->number_of_packets = 0;
  945. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  946. return -EINVAL;
  947. if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  948. uurb->buffer, uurb->buffer_length))
  949. return -EFAULT;
  950. snoop(&ps->dev->dev, "bulk urb\n");
  951. break;
  952. case USBDEVFS_URB_TYPE_ISO:
  953. /* arbitrary limit */
  954. if (uurb->number_of_packets < 1 ||
  955. uurb->number_of_packets > 128)
  956. return -EINVAL;
  957. if (!usb_endpoint_xfer_isoc(&ep->desc))
  958. return -EINVAL;
  959. isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
  960. uurb->number_of_packets;
  961. if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
  962. return -ENOMEM;
  963. if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
  964. kfree(isopkt);
  965. return -EFAULT;
  966. }
  967. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  968. /* arbitrary limit,
  969. * sufficient for USB 2.0 high-bandwidth iso */
  970. if (isopkt[u].length > 8192) {
  971. kfree(isopkt);
  972. return -EINVAL;
  973. }
  974. totlen += isopkt[u].length;
  975. }
  976. if (totlen > 32768) {
  977. kfree(isopkt);
  978. return -EINVAL;
  979. }
  980. uurb->buffer_length = totlen;
  981. snoop(&ps->dev->dev, "iso urb\n");
  982. break;
  983. case USBDEVFS_URB_TYPE_INTERRUPT:
  984. uurb->number_of_packets = 0;
  985. if (!usb_endpoint_xfer_int(&ep->desc))
  986. return -EINVAL;
  987. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  988. return -EINVAL;
  989. if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  990. uurb->buffer, uurb->buffer_length))
  991. return -EFAULT;
  992. snoop(&ps->dev->dev, "interrupt urb\n");
  993. break;
  994. default:
  995. return -EINVAL;
  996. }
  997. as = alloc_async(uurb->number_of_packets);
  998. if (!as) {
  999. kfree(isopkt);
  1000. kfree(dr);
  1001. return -ENOMEM;
  1002. }
  1003. as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
  1004. if (!as->urb->transfer_buffer) {
  1005. kfree(isopkt);
  1006. kfree(dr);
  1007. free_async(as);
  1008. return -ENOMEM;
  1009. }
  1010. as->urb->dev = ps->dev;
  1011. as->urb->pipe = (uurb->type << 30) |
  1012. __create_pipe(ps->dev, uurb->endpoint & 0xf) |
  1013. (uurb->endpoint & USB_DIR_IN);
  1014. /* This tedious sequence is necessary because the URB_* flags
  1015. * are internal to the kernel and subject to change, whereas
  1016. * the USBDEVFS_URB_* flags are a user API and must not be changed.
  1017. */
  1018. u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
  1019. if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
  1020. u |= URB_ISO_ASAP;
  1021. if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
  1022. u |= URB_SHORT_NOT_OK;
  1023. if (uurb->flags & USBDEVFS_URB_NO_FSBR)
  1024. u |= URB_NO_FSBR;
  1025. if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
  1026. u |= URB_ZERO_PACKET;
  1027. if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
  1028. u |= URB_NO_INTERRUPT;
  1029. as->urb->transfer_flags = u;
  1030. as->urb->transfer_buffer_length = uurb->buffer_length;
  1031. as->urb->setup_packet = (unsigned char *)dr;
  1032. as->urb->start_frame = uurb->start_frame;
  1033. as->urb->number_of_packets = uurb->number_of_packets;
  1034. if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
  1035. ps->dev->speed == USB_SPEED_HIGH)
  1036. as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
  1037. else
  1038. as->urb->interval = ep->desc.bInterval;
  1039. as->urb->context = as;
  1040. as->urb->complete = async_completed;
  1041. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1042. as->urb->iso_frame_desc[u].offset = totlen;
  1043. as->urb->iso_frame_desc[u].length = isopkt[u].length;
  1044. totlen += isopkt[u].length;
  1045. }
  1046. kfree(isopkt);
  1047. as->ps = ps;
  1048. as->userurb = arg;
  1049. if (uurb->endpoint & USB_DIR_IN)
  1050. as->userbuffer = uurb->buffer;
  1051. else
  1052. as->userbuffer = NULL;
  1053. as->signr = uurb->signr;
  1054. as->ifnum = ifnum;
  1055. as->pid = get_pid(task_pid(current));
  1056. as->uid = cred->uid;
  1057. as->euid = cred->euid;
  1058. security_task_getsecid(current, &as->secid);
  1059. if (!is_in) {
  1060. if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
  1061. as->urb->transfer_buffer_length)) {
  1062. free_async(as);
  1063. return -EFAULT;
  1064. }
  1065. }
  1066. snoop_urb(as->urb, as->userurb);
  1067. async_newpending(as);
  1068. if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
  1069. dev_printk(KERN_DEBUG, &ps->dev->dev,
  1070. "usbfs: usb_submit_urb returned %d\n", ret);
  1071. async_removepending(as);
  1072. free_async(as);
  1073. return ret;
  1074. }
  1075. return 0;
  1076. }
  1077. static int proc_submiturb(struct dev_state *ps, void __user *arg)
  1078. {
  1079. struct usbdevfs_urb uurb;
  1080. if (copy_from_user(&uurb, arg, sizeof(uurb)))
  1081. return -EFAULT;
  1082. return proc_do_submiturb(ps, &uurb,
  1083. (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
  1084. arg);
  1085. }
  1086. static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
  1087. {
  1088. struct async *as;
  1089. as = async_getpending(ps, arg);
  1090. if (!as)
  1091. return -EINVAL;
  1092. usb_kill_urb(as->urb);
  1093. return 0;
  1094. }
  1095. static int processcompl(struct async *as, void __user * __user *arg)
  1096. {
  1097. struct urb *urb = as->urb;
  1098. struct usbdevfs_urb __user *userurb = as->userurb;
  1099. void __user *addr = as->userurb;
  1100. unsigned int i;
  1101. if (as->userbuffer)
  1102. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1103. urb->transfer_buffer_length))
  1104. return -EFAULT;
  1105. if (put_user(as->status, &userurb->status))
  1106. return -EFAULT;
  1107. if (put_user(urb->actual_length, &userurb->actual_length))
  1108. return -EFAULT;
  1109. if (put_user(urb->error_count, &userurb->error_count))
  1110. return -EFAULT;
  1111. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1112. for (i = 0; i < urb->number_of_packets; i++) {
  1113. if (put_user(urb->iso_frame_desc[i].actual_length,
  1114. &userurb->iso_frame_desc[i].actual_length))
  1115. return -EFAULT;
  1116. if (put_user(urb->iso_frame_desc[i].status,
  1117. &userurb->iso_frame_desc[i].status))
  1118. return -EFAULT;
  1119. }
  1120. }
  1121. free_async(as);
  1122. if (put_user(addr, (void __user * __user *)arg))
  1123. return -EFAULT;
  1124. return 0;
  1125. }
  1126. static struct async *reap_as(struct dev_state *ps)
  1127. {
  1128. DECLARE_WAITQUEUE(wait, current);
  1129. struct async *as = NULL;
  1130. struct usb_device *dev = ps->dev;
  1131. add_wait_queue(&ps->wait, &wait);
  1132. for (;;) {
  1133. __set_current_state(TASK_INTERRUPTIBLE);
  1134. as = async_getcompleted(ps);
  1135. if (as)
  1136. break;
  1137. if (signal_pending(current))
  1138. break;
  1139. usb_unlock_device(dev);
  1140. schedule();
  1141. usb_lock_device(dev);
  1142. }
  1143. remove_wait_queue(&ps->wait, &wait);
  1144. set_current_state(TASK_RUNNING);
  1145. return as;
  1146. }
  1147. static int proc_reapurb(struct dev_state *ps, void __user *arg)
  1148. {
  1149. struct async *as = reap_as(ps);
  1150. if (as)
  1151. return processcompl(as, (void __user * __user *)arg);
  1152. if (signal_pending(current))
  1153. return -EINTR;
  1154. return -EIO;
  1155. }
  1156. static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
  1157. {
  1158. struct async *as;
  1159. if (!(as = async_getcompleted(ps)))
  1160. return -EAGAIN;
  1161. return processcompl(as, (void __user * __user *)arg);
  1162. }
  1163. #ifdef CONFIG_COMPAT
  1164. static int get_urb32(struct usbdevfs_urb *kurb,
  1165. struct usbdevfs_urb32 __user *uurb)
  1166. {
  1167. __u32 uptr;
  1168. if (get_user(kurb->type, &uurb->type) ||
  1169. __get_user(kurb->endpoint, &uurb->endpoint) ||
  1170. __get_user(kurb->status, &uurb->status) ||
  1171. __get_user(kurb->flags, &uurb->flags) ||
  1172. __get_user(kurb->buffer_length, &uurb->buffer_length) ||
  1173. __get_user(kurb->actual_length, &uurb->actual_length) ||
  1174. __get_user(kurb->start_frame, &uurb->start_frame) ||
  1175. __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
  1176. __get_user(kurb->error_count, &uurb->error_count) ||
  1177. __get_user(kurb->signr, &uurb->signr))
  1178. return -EFAULT;
  1179. if (__get_user(uptr, &uurb->buffer))
  1180. return -EFAULT;
  1181. kurb->buffer = compat_ptr(uptr);
  1182. if (__get_user(uptr, &uurb->buffer))
  1183. return -EFAULT;
  1184. kurb->usercontext = compat_ptr(uptr);
  1185. return 0;
  1186. }
  1187. static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
  1188. {
  1189. struct usbdevfs_urb uurb;
  1190. if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
  1191. return -EFAULT;
  1192. return proc_do_submiturb(ps, &uurb,
  1193. ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
  1194. arg);
  1195. }
  1196. static int processcompl_compat(struct async *as, void __user * __user *arg)
  1197. {
  1198. struct urb *urb = as->urb;
  1199. struct usbdevfs_urb32 __user *userurb = as->userurb;
  1200. void __user *addr = as->userurb;
  1201. unsigned int i;
  1202. if (as->userbuffer)
  1203. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1204. urb->transfer_buffer_length))
  1205. return -EFAULT;
  1206. if (put_user(as->status, &userurb->status))
  1207. return -EFAULT;
  1208. if (put_user(urb->actual_length, &userurb->actual_length))
  1209. return -EFAULT;
  1210. if (put_user(urb->error_count, &userurb->error_count))
  1211. return -EFAULT;
  1212. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1213. for (i = 0; i < urb->number_of_packets; i++) {
  1214. if (put_user(urb->iso_frame_desc[i].actual_length,
  1215. &userurb->iso_frame_desc[i].actual_length))
  1216. return -EFAULT;
  1217. if (put_user(urb->iso_frame_desc[i].status,
  1218. &userurb->iso_frame_desc[i].status))
  1219. return -EFAULT;
  1220. }
  1221. }
  1222. free_async(as);
  1223. if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
  1224. return -EFAULT;
  1225. return 0;
  1226. }
  1227. static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
  1228. {
  1229. struct async *as = reap_as(ps);
  1230. if (as)
  1231. return processcompl_compat(as, (void __user * __user *)arg);
  1232. if (signal_pending(current))
  1233. return -EINTR;
  1234. return -EIO;
  1235. }
  1236. static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
  1237. {
  1238. struct async *as;
  1239. if (!(as = async_getcompleted(ps)))
  1240. return -EAGAIN;
  1241. return processcompl_compat(as, (void __user * __user *)arg);
  1242. }
  1243. #endif
  1244. static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
  1245. {
  1246. struct usbdevfs_disconnectsignal ds;
  1247. if (copy_from_user(&ds, arg, sizeof(ds)))
  1248. return -EFAULT;
  1249. if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
  1250. return -EINVAL;
  1251. ps->discsignr = ds.signr;
  1252. ps->disccontext = ds.context;
  1253. return 0;
  1254. }
  1255. static int proc_claiminterface(struct dev_state *ps, void __user *arg)
  1256. {
  1257. unsigned int ifnum;
  1258. if (get_user(ifnum, (unsigned int __user *)arg))
  1259. return -EFAULT;
  1260. return claimintf(ps, ifnum);
  1261. }
  1262. static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
  1263. {
  1264. unsigned int ifnum;
  1265. int ret;
  1266. if (get_user(ifnum, (unsigned int __user *)arg))
  1267. return -EFAULT;
  1268. if ((ret = releaseintf(ps, ifnum)) < 0)
  1269. return ret;
  1270. destroy_async_on_interface (ps, ifnum);
  1271. return 0;
  1272. }
  1273. static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
  1274. {
  1275. int size;
  1276. void *buf = NULL;
  1277. int retval = 0;
  1278. struct usb_interface *intf = NULL;
  1279. struct usb_driver *driver = NULL;
  1280. /* alloc buffer */
  1281. if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
  1282. if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
  1283. return -ENOMEM;
  1284. if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
  1285. if (copy_from_user(buf, ctl->data, size)) {
  1286. kfree(buf);
  1287. return -EFAULT;
  1288. }
  1289. } else {
  1290. memset(buf, 0, size);
  1291. }
  1292. }
  1293. if (!connected(ps)) {
  1294. kfree(buf);
  1295. return -ENODEV;
  1296. }
  1297. if (ps->dev->state != USB_STATE_CONFIGURED)
  1298. retval = -EHOSTUNREACH;
  1299. else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
  1300. retval = -EINVAL;
  1301. else switch (ctl->ioctl_code) {
  1302. /* disconnect kernel driver from interface */
  1303. case USBDEVFS_DISCONNECT:
  1304. if (intf->dev.driver) {
  1305. driver = to_usb_driver(intf->dev.driver);
  1306. dev_dbg(&intf->dev, "disconnect by usbfs\n");
  1307. usb_driver_release_interface(driver, intf);
  1308. } else
  1309. retval = -ENODATA;
  1310. break;
  1311. /* let kernel drivers try to (re)bind to the interface */
  1312. case USBDEVFS_CONNECT:
  1313. if (!intf->dev.driver)
  1314. retval = device_attach(&intf->dev);
  1315. else
  1316. retval = -EBUSY;
  1317. break;
  1318. /* talk directly to the interface's driver */
  1319. default:
  1320. if (intf->dev.driver)
  1321. driver = to_usb_driver(intf->dev.driver);
  1322. if (driver == NULL || driver->ioctl == NULL) {
  1323. retval = -ENOTTY;
  1324. } else {
  1325. retval = driver->ioctl(intf, ctl->ioctl_code, buf);
  1326. if (retval == -ENOIOCTLCMD)
  1327. retval = -ENOTTY;
  1328. }
  1329. }
  1330. /* cleanup and return */
  1331. if (retval >= 0
  1332. && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
  1333. && size > 0
  1334. && copy_to_user(ctl->data, buf, size) != 0)
  1335. retval = -EFAULT;
  1336. kfree(buf);
  1337. return retval;
  1338. }
  1339. static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
  1340. {
  1341. struct usbdevfs_ioctl ctrl;
  1342. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1343. return -EFAULT;
  1344. return proc_ioctl(ps, &ctrl);
  1345. }
  1346. #ifdef CONFIG_COMPAT
  1347. static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
  1348. {
  1349. struct usbdevfs_ioctl32 __user *uioc;
  1350. struct usbdevfs_ioctl ctrl;
  1351. u32 udata;
  1352. uioc = compat_ptr((long)arg);
  1353. if (get_user(ctrl.ifno, &uioc->ifno) ||
  1354. get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
  1355. __get_user(udata, &uioc->data))
  1356. return -EFAULT;
  1357. ctrl.data = compat_ptr(udata);
  1358. return proc_ioctl(ps, &ctrl);
  1359. }
  1360. #endif
  1361. /*
  1362. * NOTE: All requests here that have interface numbers as parameters
  1363. * are assuming that somehow the configuration has been prevented from
  1364. * changing. But there's no mechanism to ensure that...
  1365. */
  1366. static int usbdev_ioctl(struct inode *inode, struct file *file,
  1367. unsigned int cmd, unsigned long arg)
  1368. {
  1369. struct dev_state *ps = file->private_data;
  1370. struct usb_device *dev = ps->dev;
  1371. void __user *p = (void __user *)arg;
  1372. int ret = -ENOTTY;
  1373. if (!(file->f_mode & FMODE_WRITE))
  1374. return -EPERM;
  1375. usb_lock_device(dev);
  1376. if (!connected(ps)) {
  1377. usb_unlock_device(dev);
  1378. return -ENODEV;
  1379. }
  1380. switch (cmd) {
  1381. case USBDEVFS_CONTROL:
  1382. snoop(&dev->dev, "%s: CONTROL\n", __func__);
  1383. ret = proc_control(ps, p);
  1384. if (ret >= 0)
  1385. inode->i_mtime = CURRENT_TIME;
  1386. break;
  1387. case USBDEVFS_BULK:
  1388. snoop(&dev->dev, "%s: BULK\n", __func__);
  1389. ret = proc_bulk(ps, p);
  1390. if (ret >= 0)
  1391. inode->i_mtime = CURRENT_TIME;
  1392. break;
  1393. case USBDEVFS_RESETEP:
  1394. snoop(&dev->dev, "%s: RESETEP\n", __func__);
  1395. ret = proc_resetep(ps, p);
  1396. if (ret >= 0)
  1397. inode->i_mtime = CURRENT_TIME;
  1398. break;
  1399. case USBDEVFS_RESET:
  1400. snoop(&dev->dev, "%s: RESET\n", __func__);
  1401. ret = proc_resetdevice(ps);
  1402. break;
  1403. case USBDEVFS_CLEAR_HALT:
  1404. snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
  1405. ret = proc_clearhalt(ps, p);
  1406. if (ret >= 0)
  1407. inode->i_mtime = CURRENT_TIME;
  1408. break;
  1409. case USBDEVFS_GETDRIVER:
  1410. snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
  1411. ret = proc_getdriver(ps, p);
  1412. break;
  1413. case USBDEVFS_CONNECTINFO:
  1414. snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
  1415. ret = proc_connectinfo(ps, p);
  1416. break;
  1417. case USBDEVFS_SETINTERFACE:
  1418. snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
  1419. ret = proc_setintf(ps, p);
  1420. break;
  1421. case USBDEVFS_SETCONFIGURATION:
  1422. snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
  1423. ret = proc_setconfig(ps, p);
  1424. break;
  1425. case USBDEVFS_SUBMITURB:
  1426. snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
  1427. ret = proc_submiturb(ps, p);
  1428. if (ret >= 0)
  1429. inode->i_mtime = CURRENT_TIME;
  1430. break;
  1431. #ifdef CONFIG_COMPAT
  1432. case USBDEVFS_SUBMITURB32:
  1433. snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
  1434. ret = proc_submiturb_compat(ps, p);
  1435. if (ret >= 0)
  1436. inode->i_mtime = CURRENT_TIME;
  1437. break;
  1438. case USBDEVFS_REAPURB32:
  1439. snoop(&dev->dev, "%s: REAPURB32\n", __func__);
  1440. ret = proc_reapurb_compat(ps, p);
  1441. break;
  1442. case USBDEVFS_REAPURBNDELAY32:
  1443. snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__);
  1444. ret = proc_reapurbnonblock_compat(ps, p);
  1445. break;
  1446. case USBDEVFS_IOCTL32:
  1447. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  1448. ret = proc_ioctl_compat(ps, ptr_to_compat(p));
  1449. break;
  1450. #endif
  1451. case USBDEVFS_DISCARDURB:
  1452. snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
  1453. ret = proc_unlinkurb(ps, p);
  1454. break;
  1455. case USBDEVFS_REAPURB:
  1456. snoop(&dev->dev, "%s: REAPURB\n", __func__);
  1457. ret = proc_reapurb(ps, p);
  1458. break;
  1459. case USBDEVFS_REAPURBNDELAY:
  1460. snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__);
  1461. ret = proc_reapurbnonblock(ps, p);
  1462. break;
  1463. case USBDEVFS_DISCSIGNAL:
  1464. snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
  1465. ret = proc_disconnectsignal(ps, p);
  1466. break;
  1467. case USBDEVFS_CLAIMINTERFACE:
  1468. snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
  1469. ret = proc_claiminterface(ps, p);
  1470. break;
  1471. case USBDEVFS_RELEASEINTERFACE:
  1472. snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
  1473. ret = proc_releaseinterface(ps, p);
  1474. break;
  1475. case USBDEVFS_IOCTL:
  1476. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  1477. ret = proc_ioctl_default(ps, p);
  1478. break;
  1479. }
  1480. usb_unlock_device(dev);
  1481. if (ret >= 0)
  1482. inode->i_atime = CURRENT_TIME;
  1483. return ret;
  1484. }
  1485. /* No kernel lock - fine */
  1486. static unsigned int usbdev_poll(struct file *file,
  1487. struct poll_table_struct *wait)
  1488. {
  1489. struct dev_state *ps = file->private_data;
  1490. unsigned int mask = 0;
  1491. poll_wait(file, &ps->wait, wait);
  1492. if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
  1493. mask |= POLLOUT | POLLWRNORM;
  1494. if (!connected(ps))
  1495. mask |= POLLERR | POLLHUP;
  1496. return mask;
  1497. }
  1498. const struct file_operations usbdev_file_operations = {
  1499. .owner = THIS_MODULE,
  1500. .llseek = usbdev_lseek,
  1501. .read = usbdev_read,
  1502. .poll = usbdev_poll,
  1503. .ioctl = usbdev_ioctl,
  1504. .open = usbdev_open,
  1505. .release = usbdev_release,
  1506. };
  1507. void usb_fs_classdev_common_remove(struct usb_device *udev)
  1508. {
  1509. struct dev_state *ps;
  1510. struct siginfo sinfo;
  1511. while (!list_empty(&udev->filelist)) {
  1512. ps = list_entry(udev->filelist.next, struct dev_state, list);
  1513. destroy_all_async(ps);
  1514. wake_up_all(&ps->wait);
  1515. list_del_init(&ps->list);
  1516. if (ps->discsignr) {
  1517. sinfo.si_signo = ps->discsignr;
  1518. sinfo.si_errno = EPIPE;
  1519. sinfo.si_code = SI_ASYNCIO;
  1520. sinfo.si_addr = ps->disccontext;
  1521. kill_pid_info_as_uid(ps->discsignr, &sinfo,
  1522. ps->disc_pid, ps->disc_uid,
  1523. ps->disc_euid, ps->secid);
  1524. }
  1525. }
  1526. }
  1527. #ifdef CONFIG_USB_DEVICE_CLASS
  1528. static struct class *usb_classdev_class;
  1529. static int usb_classdev_add(struct usb_device *dev)
  1530. {
  1531. struct device *cldev;
  1532. cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
  1533. NULL, "usbdev%d.%d", dev->bus->busnum,
  1534. dev->devnum);
  1535. if (IS_ERR(cldev))
  1536. return PTR_ERR(cldev);
  1537. dev->usb_classdev = cldev;
  1538. return 0;
  1539. }
  1540. static void usb_classdev_remove(struct usb_device *dev)
  1541. {
  1542. if (dev->usb_classdev)
  1543. device_unregister(dev->usb_classdev);
  1544. usb_fs_classdev_common_remove(dev);
  1545. }
  1546. static int usb_classdev_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. break;
  1557. }
  1558. return NOTIFY_OK;
  1559. }
  1560. static struct notifier_block usbdev_nb = {
  1561. .notifier_call = usb_classdev_notify,
  1562. };
  1563. #endif
  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. usb_register_notify(&usbdev_nb);
  1596. #endif
  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. #ifdef CONFIG_USB_DEVICE_CLASS
  1606. usb_unregister_notify(&usbdev_nb);
  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. }