devio.c 43 KB

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