devio.c 40 KB

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