devio.c 43 KB

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