devio.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  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. /* 3072 * 64 microframes */
  966. if (totlen > 196608) {
  967. kfree(isopkt);
  968. return -EINVAL;
  969. }
  970. uurb->buffer_length = totlen;
  971. break;
  972. case USBDEVFS_URB_TYPE_INTERRUPT:
  973. uurb->number_of_packets = 0;
  974. if (!usb_endpoint_xfer_int(&ep->desc))
  975. return -EINVAL;
  976. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  977. return -EINVAL;
  978. break;
  979. default:
  980. return -EINVAL;
  981. }
  982. if (uurb->buffer_length > 0 &&
  983. !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  984. uurb->buffer, uurb->buffer_length)) {
  985. kfree(isopkt);
  986. kfree(dr);
  987. return -EFAULT;
  988. }
  989. as = alloc_async(uurb->number_of_packets);
  990. if (!as) {
  991. kfree(isopkt);
  992. kfree(dr);
  993. return -ENOMEM;
  994. }
  995. if (uurb->buffer_length > 0) {
  996. as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
  997. GFP_KERNEL);
  998. if (!as->urb->transfer_buffer) {
  999. kfree(isopkt);
  1000. kfree(dr);
  1001. free_async(as);
  1002. return -ENOMEM;
  1003. }
  1004. }
  1005. as->urb->dev = ps->dev;
  1006. as->urb->pipe = (uurb->type << 30) |
  1007. __create_pipe(ps->dev, uurb->endpoint & 0xf) |
  1008. (uurb->endpoint & USB_DIR_IN);
  1009. /* This tedious sequence is necessary because the URB_* flags
  1010. * are internal to the kernel and subject to change, whereas
  1011. * the USBDEVFS_URB_* flags are a user API and must not be changed.
  1012. */
  1013. u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
  1014. if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
  1015. u |= URB_ISO_ASAP;
  1016. if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
  1017. u |= URB_SHORT_NOT_OK;
  1018. if (uurb->flags & USBDEVFS_URB_NO_FSBR)
  1019. u |= URB_NO_FSBR;
  1020. if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
  1021. u |= URB_ZERO_PACKET;
  1022. if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
  1023. u |= URB_NO_INTERRUPT;
  1024. as->urb->transfer_flags = u;
  1025. as->urb->transfer_buffer_length = uurb->buffer_length;
  1026. as->urb->setup_packet = (unsigned char *)dr;
  1027. as->urb->start_frame = uurb->start_frame;
  1028. as->urb->number_of_packets = uurb->number_of_packets;
  1029. if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
  1030. ps->dev->speed == USB_SPEED_HIGH)
  1031. as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
  1032. else
  1033. as->urb->interval = ep->desc.bInterval;
  1034. as->urb->context = as;
  1035. as->urb->complete = async_completed;
  1036. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1037. as->urb->iso_frame_desc[u].offset = totlen;
  1038. as->urb->iso_frame_desc[u].length = isopkt[u].length;
  1039. totlen += isopkt[u].length;
  1040. }
  1041. kfree(isopkt);
  1042. as->ps = ps;
  1043. as->userurb = arg;
  1044. if (is_in && uurb->buffer_length > 0)
  1045. as->userbuffer = uurb->buffer;
  1046. else
  1047. as->userbuffer = NULL;
  1048. as->signr = uurb->signr;
  1049. as->ifnum = ifnum;
  1050. as->pid = get_pid(task_pid(current));
  1051. as->uid = cred->uid;
  1052. as->euid = cred->euid;
  1053. security_task_getsecid(current, &as->secid);
  1054. if (!is_in && uurb->buffer_length > 0) {
  1055. if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
  1056. uurb->buffer_length)) {
  1057. free_async(as);
  1058. return -EFAULT;
  1059. }
  1060. }
  1061. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1062. as->urb->transfer_buffer_length, 0, SUBMIT);
  1063. async_newpending(as);
  1064. if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
  1065. dev_printk(KERN_DEBUG, &ps->dev->dev,
  1066. "usbfs: usb_submit_urb returned %d\n", ret);
  1067. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1068. 0, ret, COMPLETE);
  1069. async_removepending(as);
  1070. free_async(as);
  1071. return ret;
  1072. }
  1073. return 0;
  1074. }
  1075. static int proc_submiturb(struct dev_state *ps, void __user *arg)
  1076. {
  1077. struct usbdevfs_urb uurb;
  1078. if (copy_from_user(&uurb, arg, sizeof(uurb)))
  1079. return -EFAULT;
  1080. return proc_do_submiturb(ps, &uurb,
  1081. (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
  1082. arg);
  1083. }
  1084. static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
  1085. {
  1086. struct async *as;
  1087. as = async_getpending(ps, arg);
  1088. if (!as)
  1089. return -EINVAL;
  1090. usb_kill_urb(as->urb);
  1091. return 0;
  1092. }
  1093. static int processcompl(struct async *as, void __user * __user *arg)
  1094. {
  1095. struct urb *urb = as->urb;
  1096. struct usbdevfs_urb __user *userurb = as->userurb;
  1097. void __user *addr = as->userurb;
  1098. unsigned int i;
  1099. if (as->userbuffer)
  1100. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1101. urb->transfer_buffer_length))
  1102. goto err_out;
  1103. if (put_user(as->status, &userurb->status))
  1104. goto err_out;
  1105. if (put_user(urb->actual_length, &userurb->actual_length))
  1106. goto err_out;
  1107. if (put_user(urb->error_count, &userurb->error_count))
  1108. goto err_out;
  1109. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1110. for (i = 0; i < urb->number_of_packets; i++) {
  1111. if (put_user(urb->iso_frame_desc[i].actual_length,
  1112. &userurb->iso_frame_desc[i].actual_length))
  1113. goto err_out;
  1114. if (put_user(urb->iso_frame_desc[i].status,
  1115. &userurb->iso_frame_desc[i].status))
  1116. goto err_out;
  1117. }
  1118. }
  1119. free_async(as);
  1120. if (put_user(addr, (void __user * __user *)arg))
  1121. return -EFAULT;
  1122. return 0;
  1123. err_out:
  1124. free_async(as);
  1125. return -EFAULT;
  1126. }
  1127. static struct async *reap_as(struct dev_state *ps)
  1128. {
  1129. DECLARE_WAITQUEUE(wait, current);
  1130. struct async *as = NULL;
  1131. struct usb_device *dev = ps->dev;
  1132. add_wait_queue(&ps->wait, &wait);
  1133. for (;;) {
  1134. __set_current_state(TASK_INTERRUPTIBLE);
  1135. as = async_getcompleted(ps);
  1136. if (as)
  1137. break;
  1138. if (signal_pending(current))
  1139. break;
  1140. usb_unlock_device(dev);
  1141. schedule();
  1142. usb_lock_device(dev);
  1143. }
  1144. remove_wait_queue(&ps->wait, &wait);
  1145. set_current_state(TASK_RUNNING);
  1146. return as;
  1147. }
  1148. static int proc_reapurb(struct dev_state *ps, void __user *arg)
  1149. {
  1150. struct async *as = reap_as(ps);
  1151. if (as)
  1152. return processcompl(as, (void __user * __user *)arg);
  1153. if (signal_pending(current))
  1154. return -EINTR;
  1155. return -EIO;
  1156. }
  1157. static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
  1158. {
  1159. struct async *as;
  1160. if (!(as = async_getcompleted(ps)))
  1161. return -EAGAIN;
  1162. return processcompl(as, (void __user * __user *)arg);
  1163. }
  1164. #ifdef CONFIG_COMPAT
  1165. static int get_urb32(struct usbdevfs_urb *kurb,
  1166. struct usbdevfs_urb32 __user *uurb)
  1167. {
  1168. __u32 uptr;
  1169. if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
  1170. __get_user(kurb->type, &uurb->type) ||
  1171. __get_user(kurb->endpoint, &uurb->endpoint) ||
  1172. __get_user(kurb->status, &uurb->status) ||
  1173. __get_user(kurb->flags, &uurb->flags) ||
  1174. __get_user(kurb->buffer_length, &uurb->buffer_length) ||
  1175. __get_user(kurb->actual_length, &uurb->actual_length) ||
  1176. __get_user(kurb->start_frame, &uurb->start_frame) ||
  1177. __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
  1178. __get_user(kurb->error_count, &uurb->error_count) ||
  1179. __get_user(kurb->signr, &uurb->signr))
  1180. return -EFAULT;
  1181. if (__get_user(uptr, &uurb->buffer))
  1182. return -EFAULT;
  1183. kurb->buffer = compat_ptr(uptr);
  1184. if (__get_user(uptr, &uurb->usercontext))
  1185. return -EFAULT;
  1186. kurb->usercontext = compat_ptr(uptr);
  1187. return 0;
  1188. }
  1189. static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
  1190. {
  1191. struct usbdevfs_urb uurb;
  1192. if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
  1193. return -EFAULT;
  1194. return proc_do_submiturb(ps, &uurb,
  1195. ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
  1196. arg);
  1197. }
  1198. static int processcompl_compat(struct async *as, void __user * __user *arg)
  1199. {
  1200. struct urb *urb = as->urb;
  1201. struct usbdevfs_urb32 __user *userurb = as->userurb;
  1202. void __user *addr = as->userurb;
  1203. unsigned int i;
  1204. if (as->userbuffer)
  1205. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1206. urb->transfer_buffer_length))
  1207. return -EFAULT;
  1208. if (put_user(as->status, &userurb->status))
  1209. return -EFAULT;
  1210. if (put_user(urb->actual_length, &userurb->actual_length))
  1211. return -EFAULT;
  1212. if (put_user(urb->error_count, &userurb->error_count))
  1213. return -EFAULT;
  1214. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1215. for (i = 0; i < urb->number_of_packets; i++) {
  1216. if (put_user(urb->iso_frame_desc[i].actual_length,
  1217. &userurb->iso_frame_desc[i].actual_length))
  1218. return -EFAULT;
  1219. if (put_user(urb->iso_frame_desc[i].status,
  1220. &userurb->iso_frame_desc[i].status))
  1221. return -EFAULT;
  1222. }
  1223. }
  1224. free_async(as);
  1225. if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
  1226. return -EFAULT;
  1227. return 0;
  1228. }
  1229. static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
  1230. {
  1231. struct async *as = reap_as(ps);
  1232. if (as)
  1233. return processcompl_compat(as, (void __user * __user *)arg);
  1234. if (signal_pending(current))
  1235. return -EINTR;
  1236. return -EIO;
  1237. }
  1238. static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
  1239. {
  1240. struct async *as;
  1241. if (!(as = async_getcompleted(ps)))
  1242. return -EAGAIN;
  1243. return processcompl_compat(as, (void __user * __user *)arg);
  1244. }
  1245. #endif
  1246. static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
  1247. {
  1248. struct usbdevfs_disconnectsignal ds;
  1249. if (copy_from_user(&ds, arg, sizeof(ds)))
  1250. return -EFAULT;
  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 (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
  1354. __get_user(ctrl.ifno, &uioc->ifno) ||
  1355. __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
  1356. __get_user(udata, &uioc->data))
  1357. return -EFAULT;
  1358. ctrl.data = compat_ptr(udata);
  1359. return proc_ioctl(ps, &ctrl);
  1360. }
  1361. #endif
  1362. static int proc_claim_port(struct dev_state *ps, void __user *arg)
  1363. {
  1364. unsigned portnum;
  1365. int rc;
  1366. if (get_user(portnum, (unsigned __user *) arg))
  1367. return -EFAULT;
  1368. rc = usb_hub_claim_port(ps->dev, portnum, ps);
  1369. if (rc == 0)
  1370. snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
  1371. portnum, task_pid_nr(current), current->comm);
  1372. return rc;
  1373. }
  1374. static int proc_release_port(struct dev_state *ps, void __user *arg)
  1375. {
  1376. unsigned portnum;
  1377. if (get_user(portnum, (unsigned __user *) arg))
  1378. return -EFAULT;
  1379. return usb_hub_release_port(ps->dev, portnum, ps);
  1380. }
  1381. /*
  1382. * NOTE: All requests here that have interface numbers as parameters
  1383. * are assuming that somehow the configuration has been prevented from
  1384. * changing. But there's no mechanism to ensure that...
  1385. */
  1386. static int usbdev_ioctl(struct inode *inode, struct file *file,
  1387. unsigned int cmd, unsigned long arg)
  1388. {
  1389. struct dev_state *ps = file->private_data;
  1390. struct usb_device *dev = ps->dev;
  1391. void __user *p = (void __user *)arg;
  1392. int ret = -ENOTTY;
  1393. if (!(file->f_mode & FMODE_WRITE))
  1394. return -EPERM;
  1395. usb_lock_device(dev);
  1396. if (!connected(ps)) {
  1397. usb_unlock_device(dev);
  1398. return -ENODEV;
  1399. }
  1400. switch (cmd) {
  1401. case USBDEVFS_CONTROL:
  1402. snoop(&dev->dev, "%s: CONTROL\n", __func__);
  1403. ret = proc_control(ps, p);
  1404. if (ret >= 0)
  1405. inode->i_mtime = CURRENT_TIME;
  1406. break;
  1407. case USBDEVFS_BULK:
  1408. snoop(&dev->dev, "%s: BULK\n", __func__);
  1409. ret = proc_bulk(ps, p);
  1410. if (ret >= 0)
  1411. inode->i_mtime = CURRENT_TIME;
  1412. break;
  1413. case USBDEVFS_RESETEP:
  1414. snoop(&dev->dev, "%s: RESETEP\n", __func__);
  1415. ret = proc_resetep(ps, p);
  1416. if (ret >= 0)
  1417. inode->i_mtime = CURRENT_TIME;
  1418. break;
  1419. case USBDEVFS_RESET:
  1420. snoop(&dev->dev, "%s: RESET\n", __func__);
  1421. ret = proc_resetdevice(ps);
  1422. break;
  1423. case USBDEVFS_CLEAR_HALT:
  1424. snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
  1425. ret = proc_clearhalt(ps, p);
  1426. if (ret >= 0)
  1427. inode->i_mtime = CURRENT_TIME;
  1428. break;
  1429. case USBDEVFS_GETDRIVER:
  1430. snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
  1431. ret = proc_getdriver(ps, p);
  1432. break;
  1433. case USBDEVFS_CONNECTINFO:
  1434. snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
  1435. ret = proc_connectinfo(ps, p);
  1436. break;
  1437. case USBDEVFS_SETINTERFACE:
  1438. snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
  1439. ret = proc_setintf(ps, p);
  1440. break;
  1441. case USBDEVFS_SETCONFIGURATION:
  1442. snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
  1443. ret = proc_setconfig(ps, p);
  1444. break;
  1445. case USBDEVFS_SUBMITURB:
  1446. snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
  1447. ret = proc_submiturb(ps, p);
  1448. if (ret >= 0)
  1449. inode->i_mtime = CURRENT_TIME;
  1450. break;
  1451. #ifdef CONFIG_COMPAT
  1452. case USBDEVFS_SUBMITURB32:
  1453. snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
  1454. ret = proc_submiturb_compat(ps, p);
  1455. if (ret >= 0)
  1456. inode->i_mtime = CURRENT_TIME;
  1457. break;
  1458. case USBDEVFS_REAPURB32:
  1459. snoop(&dev->dev, "%s: REAPURB32\n", __func__);
  1460. ret = proc_reapurb_compat(ps, p);
  1461. break;
  1462. case USBDEVFS_REAPURBNDELAY32:
  1463. snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
  1464. ret = proc_reapurbnonblock_compat(ps, p);
  1465. break;
  1466. case USBDEVFS_IOCTL32:
  1467. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  1468. ret = proc_ioctl_compat(ps, ptr_to_compat(p));
  1469. break;
  1470. #endif
  1471. case USBDEVFS_DISCARDURB:
  1472. snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
  1473. ret = proc_unlinkurb(ps, p);
  1474. break;
  1475. case USBDEVFS_REAPURB:
  1476. snoop(&dev->dev, "%s: REAPURB\n", __func__);
  1477. ret = proc_reapurb(ps, p);
  1478. break;
  1479. case USBDEVFS_REAPURBNDELAY:
  1480. snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
  1481. ret = proc_reapurbnonblock(ps, p);
  1482. break;
  1483. case USBDEVFS_DISCSIGNAL:
  1484. snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
  1485. ret = proc_disconnectsignal(ps, p);
  1486. break;
  1487. case USBDEVFS_CLAIMINTERFACE:
  1488. snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
  1489. ret = proc_claiminterface(ps, p);
  1490. break;
  1491. case USBDEVFS_RELEASEINTERFACE:
  1492. snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
  1493. ret = proc_releaseinterface(ps, p);
  1494. break;
  1495. case USBDEVFS_IOCTL:
  1496. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  1497. ret = proc_ioctl_default(ps, p);
  1498. break;
  1499. case USBDEVFS_CLAIM_PORT:
  1500. snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
  1501. ret = proc_claim_port(ps, p);
  1502. break;
  1503. case USBDEVFS_RELEASE_PORT:
  1504. snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
  1505. ret = proc_release_port(ps, p);
  1506. break;
  1507. }
  1508. usb_unlock_device(dev);
  1509. if (ret >= 0)
  1510. inode->i_atime = CURRENT_TIME;
  1511. return ret;
  1512. }
  1513. /* No kernel lock - fine */
  1514. static unsigned int usbdev_poll(struct file *file,
  1515. struct poll_table_struct *wait)
  1516. {
  1517. struct dev_state *ps = file->private_data;
  1518. unsigned int mask = 0;
  1519. poll_wait(file, &ps->wait, wait);
  1520. if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
  1521. mask |= POLLOUT | POLLWRNORM;
  1522. if (!connected(ps))
  1523. mask |= POLLERR | POLLHUP;
  1524. return mask;
  1525. }
  1526. const struct file_operations usbdev_file_operations = {
  1527. .owner = THIS_MODULE,
  1528. .llseek = usbdev_lseek,
  1529. .read = usbdev_read,
  1530. .poll = usbdev_poll,
  1531. .ioctl = usbdev_ioctl,
  1532. .open = usbdev_open,
  1533. .release = usbdev_release,
  1534. };
  1535. static void usbdev_remove(struct usb_device *udev)
  1536. {
  1537. struct dev_state *ps;
  1538. struct siginfo sinfo;
  1539. while (!list_empty(&udev->filelist)) {
  1540. ps = list_entry(udev->filelist.next, struct dev_state, list);
  1541. destroy_all_async(ps);
  1542. wake_up_all(&ps->wait);
  1543. list_del_init(&ps->list);
  1544. if (ps->discsignr) {
  1545. sinfo.si_signo = ps->discsignr;
  1546. sinfo.si_errno = EPIPE;
  1547. sinfo.si_code = SI_ASYNCIO;
  1548. sinfo.si_addr = ps->disccontext;
  1549. kill_pid_info_as_uid(ps->discsignr, &sinfo,
  1550. ps->disc_pid, ps->disc_uid,
  1551. ps->disc_euid, ps->secid);
  1552. }
  1553. }
  1554. }
  1555. #ifdef CONFIG_USB_DEVICE_CLASS
  1556. static struct class *usb_classdev_class;
  1557. static int usb_classdev_add(struct usb_device *dev)
  1558. {
  1559. struct device *cldev;
  1560. cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
  1561. NULL, "usbdev%d.%d", dev->bus->busnum,
  1562. dev->devnum);
  1563. if (IS_ERR(cldev))
  1564. return PTR_ERR(cldev);
  1565. dev->usb_classdev = cldev;
  1566. return 0;
  1567. }
  1568. static void usb_classdev_remove(struct usb_device *dev)
  1569. {
  1570. if (dev->usb_classdev)
  1571. device_unregister(dev->usb_classdev);
  1572. }
  1573. #else
  1574. #define usb_classdev_add(dev) 0
  1575. #define usb_classdev_remove(dev) do {} while (0)
  1576. #endif
  1577. static int usbdev_notify(struct notifier_block *self,
  1578. unsigned long action, void *dev)
  1579. {
  1580. switch (action) {
  1581. case USB_DEVICE_ADD:
  1582. if (usb_classdev_add(dev))
  1583. return NOTIFY_BAD;
  1584. break;
  1585. case USB_DEVICE_REMOVE:
  1586. usb_classdev_remove(dev);
  1587. usbdev_remove(dev);
  1588. break;
  1589. }
  1590. return NOTIFY_OK;
  1591. }
  1592. static struct notifier_block usbdev_nb = {
  1593. .notifier_call = usbdev_notify,
  1594. };
  1595. static struct cdev usb_device_cdev;
  1596. int __init usb_devio_init(void)
  1597. {
  1598. int retval;
  1599. retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
  1600. "usb_device");
  1601. if (retval) {
  1602. printk(KERN_ERR "Unable to register minors for usb_device\n");
  1603. goto out;
  1604. }
  1605. cdev_init(&usb_device_cdev, &usbdev_file_operations);
  1606. retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
  1607. if (retval) {
  1608. printk(KERN_ERR "Unable to get usb_device major %d\n",
  1609. USB_DEVICE_MAJOR);
  1610. goto error_cdev;
  1611. }
  1612. #ifdef CONFIG_USB_DEVICE_CLASS
  1613. usb_classdev_class = class_create(THIS_MODULE, "usb_device");
  1614. if (IS_ERR(usb_classdev_class)) {
  1615. printk(KERN_ERR "Unable to register usb_device class\n");
  1616. retval = PTR_ERR(usb_classdev_class);
  1617. cdev_del(&usb_device_cdev);
  1618. usb_classdev_class = NULL;
  1619. goto out;
  1620. }
  1621. /* devices of this class shadow the major:minor of their parent
  1622. * device, so clear ->dev_kobj to prevent adding duplicate entries
  1623. * to /sys/dev
  1624. */
  1625. usb_classdev_class->dev_kobj = NULL;
  1626. #endif
  1627. usb_register_notify(&usbdev_nb);
  1628. out:
  1629. return retval;
  1630. error_cdev:
  1631. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1632. goto out;
  1633. }
  1634. void usb_devio_cleanup(void)
  1635. {
  1636. usb_unregister_notify(&usbdev_nb);
  1637. #ifdef CONFIG_USB_DEVICE_CLASS
  1638. class_destroy(usb_classdev_class);
  1639. #endif
  1640. cdev_del(&usb_device_cdev);
  1641. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1642. }