devio.c 52 KB

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