devio.c 43 KB

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