devio.c 54 KB

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