devio.c 46 KB

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