devio.c 45 KB

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