devio.c 43 KB

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