devio.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  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. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  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. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  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. ret = -ENOMEM;
  566. ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
  567. if (!ps)
  568. goto out_free_ps;
  569. ret = -ENODEV;
  570. /* Protect against simultaneous removal or release */
  571. mutex_lock(&usbfs_mutex);
  572. /* usbdev device-node */
  573. if (imajor(inode) == USB_DEVICE_MAJOR)
  574. dev = usbdev_lookup_by_devt(inode->i_rdev);
  575. #ifdef CONFIG_USB_DEVICEFS
  576. /* procfs file */
  577. if (!dev) {
  578. dev = inode->i_private;
  579. if (dev && dev->usbfs_dentry &&
  580. dev->usbfs_dentry->d_inode == inode)
  581. usb_get_dev(dev);
  582. else
  583. dev = NULL;
  584. }
  585. #endif
  586. mutex_unlock(&usbfs_mutex);
  587. if (!dev)
  588. goto out_free_ps;
  589. usb_lock_device(dev);
  590. if (dev->state == USB_STATE_NOTATTACHED)
  591. goto out_unlock_device;
  592. ret = usb_autoresume_device(dev);
  593. if (ret)
  594. goto out_unlock_device;
  595. ps->dev = dev;
  596. ps->file = file;
  597. spin_lock_init(&ps->lock);
  598. INIT_LIST_HEAD(&ps->list);
  599. INIT_LIST_HEAD(&ps->async_pending);
  600. INIT_LIST_HEAD(&ps->async_completed);
  601. init_waitqueue_head(&ps->wait);
  602. ps->discsignr = 0;
  603. ps->disc_pid = get_pid(task_pid(current));
  604. ps->disc_uid = cred->uid;
  605. ps->disc_euid = cred->euid;
  606. ps->disccontext = NULL;
  607. ps->ifclaimed = 0;
  608. security_task_getsecid(current, &ps->secid);
  609. smp_wmb();
  610. list_add_tail(&ps->list, &dev->filelist);
  611. file->private_data = ps;
  612. usb_unlock_device(dev);
  613. snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
  614. current->comm);
  615. return ret;
  616. out_unlock_device:
  617. usb_unlock_device(dev);
  618. usb_put_dev(dev);
  619. out_free_ps:
  620. kfree(ps);
  621. return ret;
  622. }
  623. static int usbdev_release(struct inode *inode, struct file *file)
  624. {
  625. struct dev_state *ps = file->private_data;
  626. struct usb_device *dev = ps->dev;
  627. unsigned int ifnum;
  628. struct async *as;
  629. usb_lock_device(dev);
  630. usb_hub_release_all_ports(dev, ps);
  631. list_del_init(&ps->list);
  632. for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
  633. ifnum++) {
  634. if (test_bit(ifnum, &ps->ifclaimed))
  635. releaseintf(ps, ifnum);
  636. }
  637. destroy_all_async(ps);
  638. usb_autosuspend_device(dev);
  639. usb_unlock_device(dev);
  640. usb_put_dev(dev);
  641. put_pid(ps->disc_pid);
  642. as = async_getcompleted(ps);
  643. while (as) {
  644. free_async(as);
  645. as = async_getcompleted(ps);
  646. }
  647. kfree(ps);
  648. return 0;
  649. }
  650. static int proc_control(struct dev_state *ps, void __user *arg)
  651. {
  652. struct usb_device *dev = ps->dev;
  653. struct usbdevfs_ctrltransfer ctrl;
  654. unsigned int tmo;
  655. unsigned char *tbuf;
  656. unsigned wLength;
  657. int i, pipe, ret;
  658. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  659. return -EFAULT;
  660. ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
  661. if (ret)
  662. return ret;
  663. wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
  664. if (wLength > PAGE_SIZE)
  665. return -EINVAL;
  666. tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  667. if (!tbuf)
  668. return -ENOMEM;
  669. tmo = ctrl.timeout;
  670. if (ctrl.bRequestType & 0x80) {
  671. if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
  672. ctrl.wLength)) {
  673. free_page((unsigned long)tbuf);
  674. return -EINVAL;
  675. }
  676. pipe = usb_rcvctrlpipe(dev, 0);
  677. snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT);
  678. usb_unlock_device(dev);
  679. i = usb_control_msg(dev, pipe, ctrl.bRequest,
  680. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  681. tbuf, ctrl.wLength, tmo);
  682. usb_lock_device(dev);
  683. snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE);
  684. if ((i > 0) && ctrl.wLength) {
  685. if (copy_to_user(ctrl.data, tbuf, i)) {
  686. free_page((unsigned long)tbuf);
  687. return -EFAULT;
  688. }
  689. }
  690. } else {
  691. if (ctrl.wLength) {
  692. if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
  693. free_page((unsigned long)tbuf);
  694. return -EFAULT;
  695. }
  696. }
  697. pipe = usb_sndctrlpipe(dev, 0);
  698. snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT);
  699. usb_unlock_device(dev);
  700. i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
  701. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  702. tbuf, ctrl.wLength, tmo);
  703. usb_lock_device(dev);
  704. snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE);
  705. }
  706. free_page((unsigned long)tbuf);
  707. if (i < 0 && i != -EPIPE) {
  708. dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
  709. "failed cmd %s rqt %u rq %u len %u ret %d\n",
  710. current->comm, ctrl.bRequestType, ctrl.bRequest,
  711. ctrl.wLength, i);
  712. }
  713. return i;
  714. }
  715. static int proc_bulk(struct dev_state *ps, void __user *arg)
  716. {
  717. struct usb_device *dev = ps->dev;
  718. struct usbdevfs_bulktransfer bulk;
  719. unsigned int tmo, len1, pipe;
  720. int len2;
  721. unsigned char *tbuf;
  722. int i, ret;
  723. if (copy_from_user(&bulk, arg, sizeof(bulk)))
  724. return -EFAULT;
  725. ret = findintfep(ps->dev, bulk.ep);
  726. if (ret < 0)
  727. return ret;
  728. ret = checkintf(ps, ret);
  729. if (ret)
  730. return ret;
  731. if (bulk.ep & USB_DIR_IN)
  732. pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
  733. else
  734. pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
  735. if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
  736. return -EINVAL;
  737. len1 = bulk.len;
  738. if (len1 > MAX_USBFS_BUFFER_SIZE)
  739. return -EINVAL;
  740. if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
  741. return -ENOMEM;
  742. tmo = bulk.timeout;
  743. if (bulk.ep & 0x80) {
  744. if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
  745. kfree(tbuf);
  746. return -EINVAL;
  747. }
  748. snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT);
  749. usb_unlock_device(dev);
  750. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  751. usb_lock_device(dev);
  752. snoop_urb(dev, NULL, pipe, len2, i, COMPLETE);
  753. if (!i && len2) {
  754. if (copy_to_user(bulk.data, tbuf, len2)) {
  755. kfree(tbuf);
  756. return -EFAULT;
  757. }
  758. }
  759. } else {
  760. if (len1) {
  761. if (copy_from_user(tbuf, bulk.data, len1)) {
  762. kfree(tbuf);
  763. return -EFAULT;
  764. }
  765. }
  766. snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT);
  767. usb_unlock_device(dev);
  768. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  769. usb_lock_device(dev);
  770. snoop_urb(dev, NULL, pipe, len2, i, COMPLETE);
  771. }
  772. kfree(tbuf);
  773. if (i < 0)
  774. return i;
  775. return len2;
  776. }
  777. static int proc_resetep(struct dev_state *ps, void __user *arg)
  778. {
  779. unsigned int ep;
  780. int ret;
  781. if (get_user(ep, (unsigned int __user *)arg))
  782. return -EFAULT;
  783. ret = findintfep(ps->dev, ep);
  784. if (ret < 0)
  785. return ret;
  786. ret = checkintf(ps, ret);
  787. if (ret)
  788. return ret;
  789. usb_reset_endpoint(ps->dev, ep);
  790. return 0;
  791. }
  792. static int proc_clearhalt(struct dev_state *ps, void __user *arg)
  793. {
  794. unsigned int ep;
  795. int pipe;
  796. int ret;
  797. if (get_user(ep, (unsigned int __user *)arg))
  798. return -EFAULT;
  799. ret = findintfep(ps->dev, ep);
  800. if (ret < 0)
  801. return ret;
  802. ret = checkintf(ps, ret);
  803. if (ret)
  804. return ret;
  805. if (ep & USB_DIR_IN)
  806. pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
  807. else
  808. pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
  809. return usb_clear_halt(ps->dev, pipe);
  810. }
  811. static int proc_getdriver(struct dev_state *ps, void __user *arg)
  812. {
  813. struct usbdevfs_getdriver gd;
  814. struct usb_interface *intf;
  815. int ret;
  816. if (copy_from_user(&gd, arg, sizeof(gd)))
  817. return -EFAULT;
  818. intf = usb_ifnum_to_if(ps->dev, gd.interface);
  819. if (!intf || !intf->dev.driver)
  820. ret = -ENODATA;
  821. else {
  822. strncpy(gd.driver, intf->dev.driver->name,
  823. sizeof(gd.driver));
  824. ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
  825. }
  826. return ret;
  827. }
  828. static int proc_connectinfo(struct dev_state *ps, void __user *arg)
  829. {
  830. struct usbdevfs_connectinfo ci;
  831. ci.devnum = ps->dev->devnum;
  832. ci.slow = ps->dev->speed == USB_SPEED_LOW;
  833. if (copy_to_user(arg, &ci, sizeof(ci)))
  834. return -EFAULT;
  835. return 0;
  836. }
  837. static int proc_resetdevice(struct dev_state *ps)
  838. {
  839. return usb_reset_device(ps->dev);
  840. }
  841. static int proc_setintf(struct dev_state *ps, void __user *arg)
  842. {
  843. struct usbdevfs_setinterface setintf;
  844. int ret;
  845. if (copy_from_user(&setintf, arg, sizeof(setintf)))
  846. return -EFAULT;
  847. if ((ret = checkintf(ps, setintf.interface)))
  848. return ret;
  849. return usb_set_interface(ps->dev, setintf.interface,
  850. setintf.altsetting);
  851. }
  852. static int proc_setconfig(struct dev_state *ps, void __user *arg)
  853. {
  854. int u;
  855. int status = 0;
  856. struct usb_host_config *actconfig;
  857. if (get_user(u, (int __user *)arg))
  858. return -EFAULT;
  859. actconfig = ps->dev->actconfig;
  860. /* Don't touch the device if any interfaces are claimed.
  861. * It could interfere with other drivers' operations, and if
  862. * an interface is claimed by usbfs it could easily deadlock.
  863. */
  864. if (actconfig) {
  865. int i;
  866. for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
  867. if (usb_interface_claimed(actconfig->interface[i])) {
  868. dev_warn(&ps->dev->dev,
  869. "usbfs: interface %d claimed by %s "
  870. "while '%s' sets config #%d\n",
  871. actconfig->interface[i]
  872. ->cur_altsetting
  873. ->desc.bInterfaceNumber,
  874. actconfig->interface[i]
  875. ->dev.driver->name,
  876. current->comm, u);
  877. status = -EBUSY;
  878. break;
  879. }
  880. }
  881. }
  882. /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
  883. * so avoid usb_set_configuration()'s kick to sysfs
  884. */
  885. if (status == 0) {
  886. if (actconfig && actconfig->desc.bConfigurationValue == u)
  887. status = usb_reset_configuration(ps->dev);
  888. else
  889. status = usb_set_configuration(ps->dev, u);
  890. }
  891. return status;
  892. }
  893. static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
  894. struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
  895. void __user *arg)
  896. {
  897. struct usbdevfs_iso_packet_desc *isopkt = NULL;
  898. struct usb_host_endpoint *ep;
  899. struct async *as;
  900. struct usb_ctrlrequest *dr = NULL;
  901. const struct cred *cred = current_cred();
  902. unsigned int u, totlen, isofrmlen;
  903. int ret, ifnum = -1;
  904. int is_in;
  905. if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
  906. USBDEVFS_URB_SHORT_NOT_OK |
  907. USBDEVFS_URB_BULK_CONTINUATION |
  908. USBDEVFS_URB_NO_FSBR |
  909. USBDEVFS_URB_ZERO_PACKET |
  910. USBDEVFS_URB_NO_INTERRUPT))
  911. return -EINVAL;
  912. if (uurb->buffer_length > 0 && !uurb->buffer)
  913. return -EINVAL;
  914. if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
  915. (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
  916. ifnum = findintfep(ps->dev, uurb->endpoint);
  917. if (ifnum < 0)
  918. return ifnum;
  919. ret = checkintf(ps, ifnum);
  920. if (ret)
  921. return ret;
  922. }
  923. if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
  924. is_in = 1;
  925. ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  926. } else {
  927. is_in = 0;
  928. ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  929. }
  930. if (!ep)
  931. return -ENOENT;
  932. switch(uurb->type) {
  933. case USBDEVFS_URB_TYPE_CONTROL:
  934. if (!usb_endpoint_xfer_control(&ep->desc))
  935. return -EINVAL;
  936. /* min 8 byte setup packet,
  937. * max 8 byte setup plus an arbitrary data stage */
  938. if (uurb->buffer_length < 8 ||
  939. uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
  940. return -EINVAL;
  941. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  942. if (!dr)
  943. return -ENOMEM;
  944. if (copy_from_user(dr, uurb->buffer, 8)) {
  945. kfree(dr);
  946. return -EFAULT;
  947. }
  948. if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
  949. kfree(dr);
  950. return -EINVAL;
  951. }
  952. ret = check_ctrlrecip(ps, dr->bRequestType,
  953. le16_to_cpup(&dr->wIndex));
  954. if (ret) {
  955. kfree(dr);
  956. return ret;
  957. }
  958. uurb->number_of_packets = 0;
  959. uurb->buffer_length = le16_to_cpup(&dr->wLength);
  960. uurb->buffer += 8;
  961. if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
  962. is_in = 1;
  963. uurb->endpoint |= USB_DIR_IN;
  964. } else {
  965. is_in = 0;
  966. uurb->endpoint &= ~USB_DIR_IN;
  967. }
  968. break;
  969. case USBDEVFS_URB_TYPE_BULK:
  970. switch (usb_endpoint_type(&ep->desc)) {
  971. case USB_ENDPOINT_XFER_CONTROL:
  972. case USB_ENDPOINT_XFER_ISOC:
  973. return -EINVAL;
  974. case USB_ENDPOINT_XFER_INT:
  975. /* allow single-shot interrupt transfers */
  976. uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
  977. goto interrupt_urb;
  978. }
  979. uurb->number_of_packets = 0;
  980. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  981. return -EINVAL;
  982. break;
  983. case USBDEVFS_URB_TYPE_INTERRUPT:
  984. if (!usb_endpoint_xfer_int(&ep->desc))
  985. return -EINVAL;
  986. interrupt_urb:
  987. uurb->number_of_packets = 0;
  988. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  989. return -EINVAL;
  990. break;
  991. case USBDEVFS_URB_TYPE_ISO:
  992. /* arbitrary limit */
  993. if (uurb->number_of_packets < 1 ||
  994. uurb->number_of_packets > 128)
  995. return -EINVAL;
  996. if (!usb_endpoint_xfer_isoc(&ep->desc))
  997. return -EINVAL;
  998. isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
  999. uurb->number_of_packets;
  1000. if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
  1001. return -ENOMEM;
  1002. if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
  1003. kfree(isopkt);
  1004. return -EFAULT;
  1005. }
  1006. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1007. /* arbitrary limit,
  1008. * sufficient for USB 2.0 high-bandwidth iso */
  1009. if (isopkt[u].length > 8192) {
  1010. kfree(isopkt);
  1011. return -EINVAL;
  1012. }
  1013. totlen += isopkt[u].length;
  1014. }
  1015. /* 3072 * 64 microframes */
  1016. if (totlen > 196608) {
  1017. kfree(isopkt);
  1018. return -EINVAL;
  1019. }
  1020. uurb->buffer_length = totlen;
  1021. break;
  1022. default:
  1023. return -EINVAL;
  1024. }
  1025. if (uurb->buffer_length > 0 &&
  1026. !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  1027. uurb->buffer, uurb->buffer_length)) {
  1028. kfree(isopkt);
  1029. kfree(dr);
  1030. return -EFAULT;
  1031. }
  1032. as = alloc_async(uurb->number_of_packets);
  1033. if (!as) {
  1034. kfree(isopkt);
  1035. kfree(dr);
  1036. return -ENOMEM;
  1037. }
  1038. if (uurb->buffer_length > 0) {
  1039. as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
  1040. GFP_KERNEL);
  1041. if (!as->urb->transfer_buffer) {
  1042. kfree(isopkt);
  1043. kfree(dr);
  1044. free_async(as);
  1045. return -ENOMEM;
  1046. }
  1047. }
  1048. as->urb->dev = ps->dev;
  1049. as->urb->pipe = (uurb->type << 30) |
  1050. __create_pipe(ps->dev, uurb->endpoint & 0xf) |
  1051. (uurb->endpoint & USB_DIR_IN);
  1052. /* This tedious sequence is necessary because the URB_* flags
  1053. * are internal to the kernel and subject to change, whereas
  1054. * the USBDEVFS_URB_* flags are a user API and must not be changed.
  1055. */
  1056. u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
  1057. if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
  1058. u |= URB_ISO_ASAP;
  1059. if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
  1060. u |= URB_SHORT_NOT_OK;
  1061. if (uurb->flags & USBDEVFS_URB_NO_FSBR)
  1062. u |= URB_NO_FSBR;
  1063. if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
  1064. u |= URB_ZERO_PACKET;
  1065. if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
  1066. u |= URB_NO_INTERRUPT;
  1067. as->urb->transfer_flags = u;
  1068. as->urb->transfer_buffer_length = uurb->buffer_length;
  1069. as->urb->setup_packet = (unsigned char *)dr;
  1070. as->urb->start_frame = uurb->start_frame;
  1071. as->urb->number_of_packets = uurb->number_of_packets;
  1072. if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
  1073. ps->dev->speed == USB_SPEED_HIGH)
  1074. as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
  1075. else
  1076. as->urb->interval = ep->desc.bInterval;
  1077. as->urb->context = as;
  1078. as->urb->complete = async_completed;
  1079. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1080. as->urb->iso_frame_desc[u].offset = totlen;
  1081. as->urb->iso_frame_desc[u].length = isopkt[u].length;
  1082. totlen += isopkt[u].length;
  1083. }
  1084. kfree(isopkt);
  1085. as->ps = ps;
  1086. as->userurb = arg;
  1087. if (is_in && uurb->buffer_length > 0)
  1088. as->userbuffer = uurb->buffer;
  1089. else
  1090. as->userbuffer = NULL;
  1091. as->signr = uurb->signr;
  1092. as->ifnum = ifnum;
  1093. as->pid = get_pid(task_pid(current));
  1094. as->uid = cred->uid;
  1095. as->euid = cred->euid;
  1096. security_task_getsecid(current, &as->secid);
  1097. if (!is_in && uurb->buffer_length > 0) {
  1098. if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
  1099. uurb->buffer_length)) {
  1100. free_async(as);
  1101. return -EFAULT;
  1102. }
  1103. }
  1104. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1105. as->urb->transfer_buffer_length, 0, SUBMIT);
  1106. async_newpending(as);
  1107. if (usb_endpoint_xfer_bulk(&ep->desc)) {
  1108. spin_lock_irq(&ps->lock);
  1109. /* Not exactly the endpoint address; the direction bit is
  1110. * shifted to the 0x10 position so that the value will be
  1111. * between 0 and 31.
  1112. */
  1113. as->bulk_addr = usb_endpoint_num(&ep->desc) |
  1114. ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1115. >> 3);
  1116. /* If this bulk URB is the start of a new transfer, re-enable
  1117. * the endpoint. Otherwise mark it as a continuation URB.
  1118. */
  1119. if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
  1120. as->bulk_status = AS_CONTINUATION;
  1121. else
  1122. ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
  1123. /* Don't accept continuation URBs if the endpoint is
  1124. * disabled because of an earlier error.
  1125. */
  1126. if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
  1127. ret = -EREMOTEIO;
  1128. else
  1129. ret = usb_submit_urb(as->urb, GFP_ATOMIC);
  1130. spin_unlock_irq(&ps->lock);
  1131. } else {
  1132. ret = usb_submit_urb(as->urb, GFP_KERNEL);
  1133. }
  1134. if (ret) {
  1135. dev_printk(KERN_DEBUG, &ps->dev->dev,
  1136. "usbfs: usb_submit_urb returned %d\n", ret);
  1137. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1138. 0, ret, COMPLETE);
  1139. async_removepending(as);
  1140. free_async(as);
  1141. return ret;
  1142. }
  1143. return 0;
  1144. }
  1145. static int proc_submiturb(struct dev_state *ps, void __user *arg)
  1146. {
  1147. struct usbdevfs_urb uurb;
  1148. if (copy_from_user(&uurb, arg, sizeof(uurb)))
  1149. return -EFAULT;
  1150. return proc_do_submiturb(ps, &uurb,
  1151. (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
  1152. arg);
  1153. }
  1154. static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
  1155. {
  1156. struct async *as;
  1157. as = async_getpending(ps, arg);
  1158. if (!as)
  1159. return -EINVAL;
  1160. usb_kill_urb(as->urb);
  1161. return 0;
  1162. }
  1163. static int processcompl(struct async *as, void __user * __user *arg)
  1164. {
  1165. struct urb *urb = as->urb;
  1166. struct usbdevfs_urb __user *userurb = as->userurb;
  1167. void __user *addr = as->userurb;
  1168. unsigned int i;
  1169. if (as->userbuffer && urb->actual_length)
  1170. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1171. urb->actual_length))
  1172. goto err_out;
  1173. if (put_user(as->status, &userurb->status))
  1174. goto err_out;
  1175. if (put_user(urb->actual_length, &userurb->actual_length))
  1176. goto err_out;
  1177. if (put_user(urb->error_count, &userurb->error_count))
  1178. goto err_out;
  1179. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1180. for (i = 0; i < urb->number_of_packets; i++) {
  1181. if (put_user(urb->iso_frame_desc[i].actual_length,
  1182. &userurb->iso_frame_desc[i].actual_length))
  1183. goto err_out;
  1184. if (put_user(urb->iso_frame_desc[i].status,
  1185. &userurb->iso_frame_desc[i].status))
  1186. goto err_out;
  1187. }
  1188. }
  1189. if (put_user(addr, (void __user * __user *)arg))
  1190. return -EFAULT;
  1191. return 0;
  1192. err_out:
  1193. return -EFAULT;
  1194. }
  1195. static struct async *reap_as(struct dev_state *ps)
  1196. {
  1197. DECLARE_WAITQUEUE(wait, current);
  1198. struct async *as = NULL;
  1199. struct usb_device *dev = ps->dev;
  1200. add_wait_queue(&ps->wait, &wait);
  1201. for (;;) {
  1202. __set_current_state(TASK_INTERRUPTIBLE);
  1203. as = async_getcompleted(ps);
  1204. if (as)
  1205. break;
  1206. if (signal_pending(current))
  1207. break;
  1208. usb_unlock_device(dev);
  1209. schedule();
  1210. usb_lock_device(dev);
  1211. }
  1212. remove_wait_queue(&ps->wait, &wait);
  1213. set_current_state(TASK_RUNNING);
  1214. return as;
  1215. }
  1216. static int proc_reapurb(struct dev_state *ps, void __user *arg)
  1217. {
  1218. struct async *as = reap_as(ps);
  1219. if (as) {
  1220. int retval = processcompl(as, (void __user * __user *)arg);
  1221. free_async(as);
  1222. return retval;
  1223. }
  1224. if (signal_pending(current))
  1225. return -EINTR;
  1226. return -EIO;
  1227. }
  1228. static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
  1229. {
  1230. int retval;
  1231. struct async *as;
  1232. as = async_getcompleted(ps);
  1233. retval = -EAGAIN;
  1234. if (as) {
  1235. retval = processcompl(as, (void __user * __user *)arg);
  1236. free_async(as);
  1237. }
  1238. return retval;
  1239. }
  1240. #ifdef CONFIG_COMPAT
  1241. static int proc_control_compat(struct dev_state *ps,
  1242. struct usbdevfs_ctrltransfer32 __user *p32)
  1243. {
  1244. struct usbdevfs_ctrltransfer __user *p;
  1245. __u32 udata;
  1246. p = compat_alloc_user_space(sizeof(*p));
  1247. if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
  1248. get_user(udata, &p32->data) ||
  1249. put_user(compat_ptr(udata), &p->data))
  1250. return -EFAULT;
  1251. return proc_control(ps, p);
  1252. }
  1253. static int proc_bulk_compat(struct dev_state *ps,
  1254. struct usbdevfs_bulktransfer32 __user *p32)
  1255. {
  1256. struct usbdevfs_bulktransfer __user *p;
  1257. compat_uint_t n;
  1258. compat_caddr_t addr;
  1259. p = compat_alloc_user_space(sizeof(*p));
  1260. if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
  1261. get_user(n, &p32->len) || put_user(n, &p->len) ||
  1262. get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
  1263. get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
  1264. return -EFAULT;
  1265. return proc_bulk(ps, p);
  1266. }
  1267. static int proc_disconnectsignal_compat(struct dev_state *ps, void __user *arg)
  1268. {
  1269. struct usbdevfs_disconnectsignal32 ds;
  1270. if (copy_from_user(&ds, arg, sizeof(ds)))
  1271. return -EFAULT;
  1272. ps->discsignr = ds.signr;
  1273. ps->disccontext = compat_ptr(ds.context);
  1274. return 0;
  1275. }
  1276. static int get_urb32(struct usbdevfs_urb *kurb,
  1277. struct usbdevfs_urb32 __user *uurb)
  1278. {
  1279. __u32 uptr;
  1280. if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
  1281. __get_user(kurb->type, &uurb->type) ||
  1282. __get_user(kurb->endpoint, &uurb->endpoint) ||
  1283. __get_user(kurb->status, &uurb->status) ||
  1284. __get_user(kurb->flags, &uurb->flags) ||
  1285. __get_user(kurb->buffer_length, &uurb->buffer_length) ||
  1286. __get_user(kurb->actual_length, &uurb->actual_length) ||
  1287. __get_user(kurb->start_frame, &uurb->start_frame) ||
  1288. __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
  1289. __get_user(kurb->error_count, &uurb->error_count) ||
  1290. __get_user(kurb->signr, &uurb->signr))
  1291. return -EFAULT;
  1292. if (__get_user(uptr, &uurb->buffer))
  1293. return -EFAULT;
  1294. kurb->buffer = compat_ptr(uptr);
  1295. if (__get_user(uptr, &uurb->usercontext))
  1296. return -EFAULT;
  1297. kurb->usercontext = compat_ptr(uptr);
  1298. return 0;
  1299. }
  1300. static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
  1301. {
  1302. struct usbdevfs_urb uurb;
  1303. if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
  1304. return -EFAULT;
  1305. return proc_do_submiturb(ps, &uurb,
  1306. ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
  1307. arg);
  1308. }
  1309. static int processcompl_compat(struct async *as, void __user * __user *arg)
  1310. {
  1311. struct urb *urb = as->urb;
  1312. struct usbdevfs_urb32 __user *userurb = as->userurb;
  1313. void __user *addr = as->userurb;
  1314. unsigned int i;
  1315. if (as->userbuffer && urb->actual_length)
  1316. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1317. urb->actual_length))
  1318. return -EFAULT;
  1319. if (put_user(as->status, &userurb->status))
  1320. return -EFAULT;
  1321. if (put_user(urb->actual_length, &userurb->actual_length))
  1322. return -EFAULT;
  1323. if (put_user(urb->error_count, &userurb->error_count))
  1324. return -EFAULT;
  1325. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1326. for (i = 0; i < urb->number_of_packets; i++) {
  1327. if (put_user(urb->iso_frame_desc[i].actual_length,
  1328. &userurb->iso_frame_desc[i].actual_length))
  1329. return -EFAULT;
  1330. if (put_user(urb->iso_frame_desc[i].status,
  1331. &userurb->iso_frame_desc[i].status))
  1332. return -EFAULT;
  1333. }
  1334. }
  1335. if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
  1336. return -EFAULT;
  1337. return 0;
  1338. }
  1339. static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
  1340. {
  1341. struct async *as = reap_as(ps);
  1342. if (as) {
  1343. int retval = processcompl_compat(as, (void __user * __user *)arg);
  1344. free_async(as);
  1345. return retval;
  1346. }
  1347. if (signal_pending(current))
  1348. return -EINTR;
  1349. return -EIO;
  1350. }
  1351. static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
  1352. {
  1353. int retval;
  1354. struct async *as;
  1355. retval = -EAGAIN;
  1356. as = async_getcompleted(ps);
  1357. if (as) {
  1358. retval = processcompl_compat(as, (void __user * __user *)arg);
  1359. free_async(as);
  1360. }
  1361. return retval;
  1362. }
  1363. #endif
  1364. static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
  1365. {
  1366. struct usbdevfs_disconnectsignal ds;
  1367. if (copy_from_user(&ds, arg, sizeof(ds)))
  1368. return -EFAULT;
  1369. ps->discsignr = ds.signr;
  1370. ps->disccontext = ds.context;
  1371. return 0;
  1372. }
  1373. static int proc_claiminterface(struct dev_state *ps, void __user *arg)
  1374. {
  1375. unsigned int ifnum;
  1376. if (get_user(ifnum, (unsigned int __user *)arg))
  1377. return -EFAULT;
  1378. return claimintf(ps, ifnum);
  1379. }
  1380. static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
  1381. {
  1382. unsigned int ifnum;
  1383. int ret;
  1384. if (get_user(ifnum, (unsigned int __user *)arg))
  1385. return -EFAULT;
  1386. if ((ret = releaseintf(ps, ifnum)) < 0)
  1387. return ret;
  1388. destroy_async_on_interface (ps, ifnum);
  1389. return 0;
  1390. }
  1391. static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
  1392. {
  1393. int size;
  1394. void *buf = NULL;
  1395. int retval = 0;
  1396. struct usb_interface *intf = NULL;
  1397. struct usb_driver *driver = NULL;
  1398. /* alloc buffer */
  1399. if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
  1400. if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
  1401. return -ENOMEM;
  1402. if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
  1403. if (copy_from_user(buf, ctl->data, size)) {
  1404. kfree(buf);
  1405. return -EFAULT;
  1406. }
  1407. } else {
  1408. memset(buf, 0, size);
  1409. }
  1410. }
  1411. if (!connected(ps)) {
  1412. kfree(buf);
  1413. return -ENODEV;
  1414. }
  1415. if (ps->dev->state != USB_STATE_CONFIGURED)
  1416. retval = -EHOSTUNREACH;
  1417. else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
  1418. retval = -EINVAL;
  1419. else switch (ctl->ioctl_code) {
  1420. /* disconnect kernel driver from interface */
  1421. case USBDEVFS_DISCONNECT:
  1422. if (intf->dev.driver) {
  1423. driver = to_usb_driver(intf->dev.driver);
  1424. dev_dbg(&intf->dev, "disconnect by usbfs\n");
  1425. usb_driver_release_interface(driver, intf);
  1426. } else
  1427. retval = -ENODATA;
  1428. break;
  1429. /* let kernel drivers try to (re)bind to the interface */
  1430. case USBDEVFS_CONNECT:
  1431. if (!intf->dev.driver)
  1432. retval = device_attach(&intf->dev);
  1433. else
  1434. retval = -EBUSY;
  1435. break;
  1436. /* talk directly to the interface's driver */
  1437. default:
  1438. if (intf->dev.driver)
  1439. driver = to_usb_driver(intf->dev.driver);
  1440. if (driver == NULL || driver->ioctl == NULL) {
  1441. retval = -ENOTTY;
  1442. } else {
  1443. retval = driver->ioctl(intf, ctl->ioctl_code, buf);
  1444. if (retval == -ENOIOCTLCMD)
  1445. retval = -ENOTTY;
  1446. }
  1447. }
  1448. /* cleanup and return */
  1449. if (retval >= 0
  1450. && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
  1451. && size > 0
  1452. && copy_to_user(ctl->data, buf, size) != 0)
  1453. retval = -EFAULT;
  1454. kfree(buf);
  1455. return retval;
  1456. }
  1457. static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
  1458. {
  1459. struct usbdevfs_ioctl ctrl;
  1460. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1461. return -EFAULT;
  1462. return proc_ioctl(ps, &ctrl);
  1463. }
  1464. #ifdef CONFIG_COMPAT
  1465. static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
  1466. {
  1467. struct usbdevfs_ioctl32 __user *uioc;
  1468. struct usbdevfs_ioctl ctrl;
  1469. u32 udata;
  1470. uioc = compat_ptr((long)arg);
  1471. if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
  1472. __get_user(ctrl.ifno, &uioc->ifno) ||
  1473. __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
  1474. __get_user(udata, &uioc->data))
  1475. return -EFAULT;
  1476. ctrl.data = compat_ptr(udata);
  1477. return proc_ioctl(ps, &ctrl);
  1478. }
  1479. #endif
  1480. static int proc_claim_port(struct dev_state *ps, void __user *arg)
  1481. {
  1482. unsigned portnum;
  1483. int rc;
  1484. if (get_user(portnum, (unsigned __user *) arg))
  1485. return -EFAULT;
  1486. rc = usb_hub_claim_port(ps->dev, portnum, ps);
  1487. if (rc == 0)
  1488. snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
  1489. portnum, task_pid_nr(current), current->comm);
  1490. return rc;
  1491. }
  1492. static int proc_release_port(struct dev_state *ps, void __user *arg)
  1493. {
  1494. unsigned portnum;
  1495. if (get_user(portnum, (unsigned __user *) arg))
  1496. return -EFAULT;
  1497. return usb_hub_release_port(ps->dev, portnum, ps);
  1498. }
  1499. /*
  1500. * NOTE: All requests here that have interface numbers as parameters
  1501. * are assuming that somehow the configuration has been prevented from
  1502. * changing. But there's no mechanism to ensure that...
  1503. */
  1504. static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
  1505. void __user *p)
  1506. {
  1507. struct dev_state *ps = file->private_data;
  1508. struct inode *inode = file->f_path.dentry->d_inode;
  1509. struct usb_device *dev = ps->dev;
  1510. int ret = -ENOTTY;
  1511. if (!(file->f_mode & FMODE_WRITE))
  1512. return -EPERM;
  1513. usb_lock_device(dev);
  1514. if (!connected(ps)) {
  1515. usb_unlock_device(dev);
  1516. return -ENODEV;
  1517. }
  1518. switch (cmd) {
  1519. case USBDEVFS_CONTROL:
  1520. snoop(&dev->dev, "%s: CONTROL\n", __func__);
  1521. ret = proc_control(ps, p);
  1522. if (ret >= 0)
  1523. inode->i_mtime = CURRENT_TIME;
  1524. break;
  1525. case USBDEVFS_BULK:
  1526. snoop(&dev->dev, "%s: BULK\n", __func__);
  1527. ret = proc_bulk(ps, p);
  1528. if (ret >= 0)
  1529. inode->i_mtime = CURRENT_TIME;
  1530. break;
  1531. case USBDEVFS_RESETEP:
  1532. snoop(&dev->dev, "%s: RESETEP\n", __func__);
  1533. ret = proc_resetep(ps, p);
  1534. if (ret >= 0)
  1535. inode->i_mtime = CURRENT_TIME;
  1536. break;
  1537. case USBDEVFS_RESET:
  1538. snoop(&dev->dev, "%s: RESET\n", __func__);
  1539. ret = proc_resetdevice(ps);
  1540. break;
  1541. case USBDEVFS_CLEAR_HALT:
  1542. snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
  1543. ret = proc_clearhalt(ps, p);
  1544. if (ret >= 0)
  1545. inode->i_mtime = CURRENT_TIME;
  1546. break;
  1547. case USBDEVFS_GETDRIVER:
  1548. snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
  1549. ret = proc_getdriver(ps, p);
  1550. break;
  1551. case USBDEVFS_CONNECTINFO:
  1552. snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
  1553. ret = proc_connectinfo(ps, p);
  1554. break;
  1555. case USBDEVFS_SETINTERFACE:
  1556. snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
  1557. ret = proc_setintf(ps, p);
  1558. break;
  1559. case USBDEVFS_SETCONFIGURATION:
  1560. snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
  1561. ret = proc_setconfig(ps, p);
  1562. break;
  1563. case USBDEVFS_SUBMITURB:
  1564. snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
  1565. ret = proc_submiturb(ps, p);
  1566. if (ret >= 0)
  1567. inode->i_mtime = CURRENT_TIME;
  1568. break;
  1569. #ifdef CONFIG_COMPAT
  1570. case USBDEVFS_CONTROL32:
  1571. snoop(&dev->dev, "%s: CONTROL32\n", __func__);
  1572. ret = proc_control_compat(ps, p);
  1573. if (ret >= 0)
  1574. inode->i_mtime = CURRENT_TIME;
  1575. break;
  1576. case USBDEVFS_BULK32:
  1577. snoop(&dev->dev, "%s: BULK32\n", __func__);
  1578. ret = proc_bulk_compat(ps, p);
  1579. if (ret >= 0)
  1580. inode->i_mtime = CURRENT_TIME;
  1581. break;
  1582. case USBDEVFS_DISCSIGNAL32:
  1583. snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
  1584. ret = proc_disconnectsignal_compat(ps, p);
  1585. break;
  1586. case USBDEVFS_SUBMITURB32:
  1587. snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
  1588. ret = proc_submiturb_compat(ps, p);
  1589. if (ret >= 0)
  1590. inode->i_mtime = CURRENT_TIME;
  1591. break;
  1592. case USBDEVFS_REAPURB32:
  1593. snoop(&dev->dev, "%s: REAPURB32\n", __func__);
  1594. ret = proc_reapurb_compat(ps, p);
  1595. break;
  1596. case USBDEVFS_REAPURBNDELAY32:
  1597. snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
  1598. ret = proc_reapurbnonblock_compat(ps, p);
  1599. break;
  1600. case USBDEVFS_IOCTL32:
  1601. snoop(&dev->dev, "%s: IOCTL32\n", __func__);
  1602. ret = proc_ioctl_compat(ps, ptr_to_compat(p));
  1603. break;
  1604. #endif
  1605. case USBDEVFS_DISCARDURB:
  1606. snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
  1607. ret = proc_unlinkurb(ps, p);
  1608. break;
  1609. case USBDEVFS_REAPURB:
  1610. snoop(&dev->dev, "%s: REAPURB\n", __func__);
  1611. ret = proc_reapurb(ps, p);
  1612. break;
  1613. case USBDEVFS_REAPURBNDELAY:
  1614. snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
  1615. ret = proc_reapurbnonblock(ps, p);
  1616. break;
  1617. case USBDEVFS_DISCSIGNAL:
  1618. snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
  1619. ret = proc_disconnectsignal(ps, p);
  1620. break;
  1621. case USBDEVFS_CLAIMINTERFACE:
  1622. snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
  1623. ret = proc_claiminterface(ps, p);
  1624. break;
  1625. case USBDEVFS_RELEASEINTERFACE:
  1626. snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
  1627. ret = proc_releaseinterface(ps, p);
  1628. break;
  1629. case USBDEVFS_IOCTL:
  1630. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  1631. ret = proc_ioctl_default(ps, p);
  1632. break;
  1633. case USBDEVFS_CLAIM_PORT:
  1634. snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
  1635. ret = proc_claim_port(ps, p);
  1636. break;
  1637. case USBDEVFS_RELEASE_PORT:
  1638. snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
  1639. ret = proc_release_port(ps, p);
  1640. break;
  1641. }
  1642. usb_unlock_device(dev);
  1643. if (ret >= 0)
  1644. inode->i_atime = CURRENT_TIME;
  1645. return ret;
  1646. }
  1647. static long usbdev_ioctl(struct file *file, unsigned int cmd,
  1648. unsigned long arg)
  1649. {
  1650. int ret;
  1651. lock_kernel();
  1652. ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
  1653. unlock_kernel();
  1654. return ret;
  1655. }
  1656. #ifdef CONFIG_COMPAT
  1657. static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
  1658. unsigned long arg)
  1659. {
  1660. int ret;
  1661. lock_kernel();
  1662. ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
  1663. unlock_kernel();
  1664. return ret;
  1665. }
  1666. #endif
  1667. /* No kernel lock - fine */
  1668. static unsigned int usbdev_poll(struct file *file,
  1669. struct poll_table_struct *wait)
  1670. {
  1671. struct dev_state *ps = file->private_data;
  1672. unsigned int mask = 0;
  1673. poll_wait(file, &ps->wait, wait);
  1674. if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
  1675. mask |= POLLOUT | POLLWRNORM;
  1676. if (!connected(ps))
  1677. mask |= POLLERR | POLLHUP;
  1678. return mask;
  1679. }
  1680. const struct file_operations usbdev_file_operations = {
  1681. .owner = THIS_MODULE,
  1682. .llseek = usbdev_lseek,
  1683. .read = usbdev_read,
  1684. .poll = usbdev_poll,
  1685. .unlocked_ioctl = usbdev_ioctl,
  1686. #ifdef CONFIG_COMPAT
  1687. .compat_ioctl = usbdev_compat_ioctl,
  1688. #endif
  1689. .open = usbdev_open,
  1690. .release = usbdev_release,
  1691. };
  1692. static void usbdev_remove(struct usb_device *udev)
  1693. {
  1694. struct dev_state *ps;
  1695. struct siginfo sinfo;
  1696. while (!list_empty(&udev->filelist)) {
  1697. ps = list_entry(udev->filelist.next, struct dev_state, list);
  1698. destroy_all_async(ps);
  1699. wake_up_all(&ps->wait);
  1700. list_del_init(&ps->list);
  1701. if (ps->discsignr) {
  1702. sinfo.si_signo = ps->discsignr;
  1703. sinfo.si_errno = EPIPE;
  1704. sinfo.si_code = SI_ASYNCIO;
  1705. sinfo.si_addr = ps->disccontext;
  1706. kill_pid_info_as_uid(ps->discsignr, &sinfo,
  1707. ps->disc_pid, ps->disc_uid,
  1708. ps->disc_euid, ps->secid);
  1709. }
  1710. }
  1711. }
  1712. #ifdef CONFIG_USB_DEVICE_CLASS
  1713. static struct class *usb_classdev_class;
  1714. static int usb_classdev_add(struct usb_device *dev)
  1715. {
  1716. struct device *cldev;
  1717. cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
  1718. NULL, "usbdev%d.%d", dev->bus->busnum,
  1719. dev->devnum);
  1720. if (IS_ERR(cldev))
  1721. return PTR_ERR(cldev);
  1722. dev->usb_classdev = cldev;
  1723. return 0;
  1724. }
  1725. static void usb_classdev_remove(struct usb_device *dev)
  1726. {
  1727. if (dev->usb_classdev)
  1728. device_unregister(dev->usb_classdev);
  1729. }
  1730. #else
  1731. #define usb_classdev_add(dev) 0
  1732. #define usb_classdev_remove(dev) do {} while (0)
  1733. #endif
  1734. static int usbdev_notify(struct notifier_block *self,
  1735. unsigned long action, void *dev)
  1736. {
  1737. switch (action) {
  1738. case USB_DEVICE_ADD:
  1739. if (usb_classdev_add(dev))
  1740. return NOTIFY_BAD;
  1741. break;
  1742. case USB_DEVICE_REMOVE:
  1743. usb_classdev_remove(dev);
  1744. usbdev_remove(dev);
  1745. break;
  1746. }
  1747. return NOTIFY_OK;
  1748. }
  1749. static struct notifier_block usbdev_nb = {
  1750. .notifier_call = usbdev_notify,
  1751. };
  1752. static struct cdev usb_device_cdev;
  1753. int __init usb_devio_init(void)
  1754. {
  1755. int retval;
  1756. retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
  1757. "usb_device");
  1758. if (retval) {
  1759. printk(KERN_ERR "Unable to register minors for usb_device\n");
  1760. goto out;
  1761. }
  1762. cdev_init(&usb_device_cdev, &usbdev_file_operations);
  1763. retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
  1764. if (retval) {
  1765. printk(KERN_ERR "Unable to get usb_device major %d\n",
  1766. USB_DEVICE_MAJOR);
  1767. goto error_cdev;
  1768. }
  1769. #ifdef CONFIG_USB_DEVICE_CLASS
  1770. usb_classdev_class = class_create(THIS_MODULE, "usb_device");
  1771. if (IS_ERR(usb_classdev_class)) {
  1772. printk(KERN_ERR "Unable to register usb_device class\n");
  1773. retval = PTR_ERR(usb_classdev_class);
  1774. cdev_del(&usb_device_cdev);
  1775. usb_classdev_class = NULL;
  1776. goto out;
  1777. }
  1778. /* devices of this class shadow the major:minor of their parent
  1779. * device, so clear ->dev_kobj to prevent adding duplicate entries
  1780. * to /sys/dev
  1781. */
  1782. usb_classdev_class->dev_kobj = NULL;
  1783. #endif
  1784. usb_register_notify(&usbdev_nb);
  1785. out:
  1786. return retval;
  1787. error_cdev:
  1788. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1789. goto out;
  1790. }
  1791. void usb_devio_cleanup(void)
  1792. {
  1793. usb_unregister_notify(&usbdev_nb);
  1794. #ifdef CONFIG_USB_DEVICE_CLASS
  1795. class_destroy(usb_classdev_class);
  1796. #endif
  1797. cdev_del(&usb_device_cdev);
  1798. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1799. }