devio.c 40 KB

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