devio.c 42 KB

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