devio.c 42 KB

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