devio.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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. /* get input parameters and alloc buffer */
  1098. if (copy_from_user(&ctrl, arg, sizeof (ctrl)))
  1099. return -EFAULT;
  1100. if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) {
  1101. if ((buf = kmalloc (size, GFP_KERNEL)) == NULL)
  1102. return -ENOMEM;
  1103. if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) {
  1104. if (copy_from_user (buf, ctrl.data, size)) {
  1105. kfree(buf);
  1106. return -EFAULT;
  1107. }
  1108. } else {
  1109. memset (buf, 0, size);
  1110. }
  1111. }
  1112. if (!connected(ps->dev)) {
  1113. kfree(buf);
  1114. return -ENODEV;
  1115. }
  1116. if (ps->dev->state != USB_STATE_CONFIGURED)
  1117. retval = -EHOSTUNREACH;
  1118. else if (!(intf = usb_ifnum_to_if (ps->dev, ctrl.ifno)))
  1119. retval = -EINVAL;
  1120. else switch (ctrl.ioctl_code) {
  1121. /* disconnect kernel driver from interface */
  1122. case USBDEVFS_DISCONNECT:
  1123. down_write(&usb_bus_type.subsys.rwsem);
  1124. if (intf->dev.driver) {
  1125. driver = to_usb_driver(intf->dev.driver);
  1126. dev_dbg (&intf->dev, "disconnect by usbfs\n");
  1127. usb_driver_release_interface(driver, intf);
  1128. } else
  1129. retval = -ENODATA;
  1130. up_write(&usb_bus_type.subsys.rwsem);
  1131. break;
  1132. /* let kernel drivers try to (re)bind to the interface */
  1133. case USBDEVFS_CONNECT:
  1134. usb_unlock_device(ps->dev);
  1135. usb_lock_all_devices();
  1136. bus_rescan_devices(intf->dev.bus);
  1137. usb_unlock_all_devices();
  1138. usb_lock_device(ps->dev);
  1139. break;
  1140. /* talk directly to the interface's driver */
  1141. default:
  1142. down_read(&usb_bus_type.subsys.rwsem);
  1143. if (intf->dev.driver)
  1144. driver = to_usb_driver(intf->dev.driver);
  1145. if (driver == NULL || driver->ioctl == NULL) {
  1146. retval = -ENOTTY;
  1147. } else {
  1148. retval = driver->ioctl (intf, ctrl.ioctl_code, buf);
  1149. if (retval == -ENOIOCTLCMD)
  1150. retval = -ENOTTY;
  1151. }
  1152. up_read(&usb_bus_type.subsys.rwsem);
  1153. }
  1154. /* cleanup and return */
  1155. if (retval >= 0
  1156. && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0
  1157. && size > 0
  1158. && copy_to_user (ctrl.data, buf, size) != 0)
  1159. retval = -EFAULT;
  1160. kfree(buf);
  1161. return retval;
  1162. }
  1163. /*
  1164. * NOTE: All requests here that have interface numbers as parameters
  1165. * are assuming that somehow the configuration has been prevented from
  1166. * changing. But there's no mechanism to ensure that...
  1167. */
  1168. static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  1169. {
  1170. struct dev_state *ps = (struct dev_state *)file->private_data;
  1171. struct usb_device *dev = ps->dev;
  1172. void __user *p = (void __user *)arg;
  1173. int ret = -ENOTTY;
  1174. if (!(file->f_mode & FMODE_WRITE))
  1175. return -EPERM;
  1176. usb_lock_device(dev);
  1177. if (!connected(dev)) {
  1178. usb_unlock_device(dev);
  1179. return -ENODEV;
  1180. }
  1181. switch (cmd) {
  1182. case USBDEVFS_CONTROL:
  1183. snoop(&dev->dev, "%s: CONTROL\n", __FUNCTION__);
  1184. ret = proc_control(ps, p);
  1185. if (ret >= 0)
  1186. inode->i_mtime = CURRENT_TIME;
  1187. break;
  1188. case USBDEVFS_BULK:
  1189. snoop(&dev->dev, "%s: BULK\n", __FUNCTION__);
  1190. ret = proc_bulk(ps, p);
  1191. if (ret >= 0)
  1192. inode->i_mtime = CURRENT_TIME;
  1193. break;
  1194. case USBDEVFS_RESETEP:
  1195. snoop(&dev->dev, "%s: RESETEP\n", __FUNCTION__);
  1196. ret = proc_resetep(ps, p);
  1197. if (ret >= 0)
  1198. inode->i_mtime = CURRENT_TIME;
  1199. break;
  1200. case USBDEVFS_RESET:
  1201. snoop(&dev->dev, "%s: RESET\n", __FUNCTION__);
  1202. ret = proc_resetdevice(ps);
  1203. break;
  1204. case USBDEVFS_CLEAR_HALT:
  1205. snoop(&dev->dev, "%s: CLEAR_HALT\n", __FUNCTION__);
  1206. ret = proc_clearhalt(ps, p);
  1207. if (ret >= 0)
  1208. inode->i_mtime = CURRENT_TIME;
  1209. break;
  1210. case USBDEVFS_GETDRIVER:
  1211. snoop(&dev->dev, "%s: GETDRIVER\n", __FUNCTION__);
  1212. ret = proc_getdriver(ps, p);
  1213. break;
  1214. case USBDEVFS_CONNECTINFO:
  1215. snoop(&dev->dev, "%s: CONNECTINFO\n", __FUNCTION__);
  1216. ret = proc_connectinfo(ps, p);
  1217. break;
  1218. case USBDEVFS_SETINTERFACE:
  1219. snoop(&dev->dev, "%s: SETINTERFACE\n", __FUNCTION__);
  1220. ret = proc_setintf(ps, p);
  1221. break;
  1222. case USBDEVFS_SETCONFIGURATION:
  1223. snoop(&dev->dev, "%s: SETCONFIGURATION\n", __FUNCTION__);
  1224. ret = proc_setconfig(ps, p);
  1225. break;
  1226. case USBDEVFS_SUBMITURB:
  1227. snoop(&dev->dev, "%s: SUBMITURB\n", __FUNCTION__);
  1228. ret = proc_submiturb(ps, p);
  1229. if (ret >= 0)
  1230. inode->i_mtime = CURRENT_TIME;
  1231. break;
  1232. #ifdef CONFIG_COMPAT
  1233. case USBDEVFS_SUBMITURB32:
  1234. snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__);
  1235. ret = proc_submiturb_compat(ps, p);
  1236. if (ret >= 0)
  1237. inode->i_mtime = CURRENT_TIME;
  1238. break;
  1239. case USBDEVFS_REAPURB32:
  1240. snoop(&dev->dev, "%s: REAPURB32\n", __FUNCTION__);
  1241. ret = proc_reapurb_compat(ps, p);
  1242. break;
  1243. case USBDEVFS_REAPURBNDELAY32:
  1244. snoop(&dev->dev, "%s: REAPURBDELAY32\n", __FUNCTION__);
  1245. ret = proc_reapurbnonblock_compat(ps, p);
  1246. break;
  1247. #endif
  1248. case USBDEVFS_DISCARDURB:
  1249. snoop(&dev->dev, "%s: DISCARDURB\n", __FUNCTION__);
  1250. ret = proc_unlinkurb(ps, p);
  1251. break;
  1252. case USBDEVFS_REAPURB:
  1253. snoop(&dev->dev, "%s: REAPURB\n", __FUNCTION__);
  1254. ret = proc_reapurb(ps, p);
  1255. break;
  1256. case USBDEVFS_REAPURBNDELAY:
  1257. snoop(&dev->dev, "%s: REAPURBDELAY\n", __FUNCTION__);
  1258. ret = proc_reapurbnonblock(ps, p);
  1259. break;
  1260. case USBDEVFS_DISCSIGNAL:
  1261. snoop(&dev->dev, "%s: DISCSIGNAL\n", __FUNCTION__);
  1262. ret = proc_disconnectsignal(ps, p);
  1263. break;
  1264. case USBDEVFS_CLAIMINTERFACE:
  1265. snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __FUNCTION__);
  1266. ret = proc_claiminterface(ps, p);
  1267. break;
  1268. case USBDEVFS_RELEASEINTERFACE:
  1269. snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __FUNCTION__);
  1270. ret = proc_releaseinterface(ps, p);
  1271. break;
  1272. case USBDEVFS_IOCTL:
  1273. snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
  1274. ret = proc_ioctl(ps, p);
  1275. break;
  1276. }
  1277. usb_unlock_device(dev);
  1278. if (ret >= 0)
  1279. inode->i_atime = CURRENT_TIME;
  1280. return ret;
  1281. }
  1282. /* No kernel lock - fine */
  1283. static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
  1284. {
  1285. struct dev_state *ps = (struct dev_state *)file->private_data;
  1286. unsigned int mask = 0;
  1287. poll_wait(file, &ps->wait, wait);
  1288. if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
  1289. mask |= POLLOUT | POLLWRNORM;
  1290. if (!connected(ps->dev))
  1291. mask |= POLLERR | POLLHUP;
  1292. return mask;
  1293. }
  1294. struct file_operations usbfs_device_file_operations = {
  1295. .llseek = usbdev_lseek,
  1296. .read = usbdev_read,
  1297. .poll = usbdev_poll,
  1298. .ioctl = usbdev_ioctl,
  1299. .open = usbdev_open,
  1300. .release = usbdev_release,
  1301. };
  1302. struct usb_device *usbdev_lookup_minor(int minor)
  1303. {
  1304. struct class_device *class_dev;
  1305. struct usb_device *dev = NULL;
  1306. down(&usb_device_class->sem);
  1307. list_for_each_entry(class_dev, &usb_device_class->children, node) {
  1308. if (class_dev->devt == MKDEV(USB_DEVICE_MAJOR, minor)) {
  1309. dev = class_dev->class_data;
  1310. break;
  1311. }
  1312. }
  1313. up(&usb_device_class->sem);
  1314. return dev;
  1315. };
  1316. void usbdev_add(struct usb_device *dev)
  1317. {
  1318. int minor = ((dev->bus->busnum-1) * 128) + (dev->devnum-1);
  1319. dev->class_dev = class_device_create(usb_device_class,
  1320. MKDEV(USB_DEVICE_MAJOR, minor), &dev->dev,
  1321. "usbdev%d.%d", dev->bus->busnum, dev->devnum);
  1322. dev->class_dev->class_data = dev;
  1323. }
  1324. void usbdev_remove(struct usb_device *dev)
  1325. {
  1326. class_device_unregister(dev->class_dev);
  1327. }
  1328. static struct cdev usb_device_cdev = {
  1329. .kobj = {.name = "usb_device", },
  1330. .owner = THIS_MODULE,
  1331. };
  1332. int __init usbdev_init(void)
  1333. {
  1334. int retval;
  1335. retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
  1336. "usb_device");
  1337. if (retval) {
  1338. err("unable to register minors for usb_device");
  1339. goto out;
  1340. }
  1341. cdev_init(&usb_device_cdev, &usbfs_device_file_operations);
  1342. retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
  1343. if (retval) {
  1344. err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
  1345. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1346. goto out;
  1347. }
  1348. usb_device_class = class_create(THIS_MODULE, "usb_device");
  1349. if (IS_ERR(usb_device_class)) {
  1350. err("unable to register usb_device class");
  1351. retval = PTR_ERR(usb_device_class);
  1352. usb_device_class = NULL;
  1353. cdev_del(&usb_device_cdev);
  1354. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1355. }
  1356. out:
  1357. return retval;
  1358. }
  1359. void usbdev_cleanup(void)
  1360. {
  1361. class_destroy(usb_device_class);
  1362. cdev_del(&usb_device_cdev);
  1363. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1364. }