devio.c 43 KB

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