devio.c 45 KB

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