devio.c 47 KB

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