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