devio.c 42 KB

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