devio.c 43 KB

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