devio.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*****************************************************************************/
  3. /*
  4. * devio.c -- User space communication with USB devices.
  5. *
  6. * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
  7. *
  8. * This file implements the usbfs/x/y files, where
  9. * x is the bus number and y the device number.
  10. *
  11. * It allows user space programs/"drivers" to communicate directly
  12. * with USB devices without intervening kernel driver.
  13. *
  14. * Revision history
  15. * 22.12.1999 0.1 Initial release (split from proc_usb.c)
  16. * 04.01.2000 0.2 Turned into its own filesystem
  17. * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
  18. * (CAN-2005-3055)
  19. */
  20. /*****************************************************************************/
  21. #include <linux/fs.h>
  22. #include <linux/mm.h>
  23. #include <linux/sched/signal.h>
  24. #include <linux/slab.h>
  25. #include <linux/signal.h>
  26. #include <linux/poll.h>
  27. #include <linux/module.h>
  28. #include <linux/string.h>
  29. #include <linux/usb.h>
  30. #include <linux/usbdevice_fs.h>
  31. #include <linux/usb/hcd.h> /* for usbcore internals */
  32. #include <linux/cdev.h>
  33. #include <linux/notifier.h>
  34. #include <linux/security.h>
  35. #include <linux/user_namespace.h>
  36. #include <linux/scatterlist.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/dma-mapping.h>
  39. #include <asm/byteorder.h>
  40. #include <linux/moduleparam.h>
  41. #include "usb.h"
  42. #define USB_MAXBUS 64
  43. #define USB_DEVICE_MAX (USB_MAXBUS * 128)
  44. #define USB_SG_SIZE 16384 /* split-size for large txs */
  45. /* Mutual exclusion for removal, open, and release */
  46. DEFINE_MUTEX(usbfs_mutex);
  47. struct usb_dev_state {
  48. struct list_head list; /* state list */
  49. struct usb_device *dev;
  50. struct file *file;
  51. spinlock_t lock; /* protects the async urb lists */
  52. struct list_head async_pending;
  53. struct list_head async_completed;
  54. struct list_head memory_list;
  55. wait_queue_head_t wait; /* wake up if a request completed */
  56. unsigned int discsignr;
  57. struct pid *disc_pid;
  58. const struct cred *cred;
  59. void __user *disccontext;
  60. unsigned long ifclaimed;
  61. u32 disabled_bulk_eps;
  62. bool privileges_dropped;
  63. unsigned long interface_allowed_mask;
  64. };
  65. struct usb_memory {
  66. struct list_head memlist;
  67. int vma_use_count;
  68. int urb_use_count;
  69. u32 size;
  70. void *mem;
  71. dma_addr_t dma_handle;
  72. unsigned long vm_start;
  73. struct usb_dev_state *ps;
  74. };
  75. struct async {
  76. struct list_head asynclist;
  77. struct usb_dev_state *ps;
  78. struct pid *pid;
  79. const struct cred *cred;
  80. unsigned int signr;
  81. unsigned int ifnum;
  82. void __user *userbuffer;
  83. void __user *userurb;
  84. struct urb *urb;
  85. struct usb_memory *usbm;
  86. unsigned int mem_usage;
  87. int status;
  88. u8 bulk_addr;
  89. u8 bulk_status;
  90. };
  91. static bool usbfs_snoop;
  92. module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
  93. MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
  94. static unsigned usbfs_snoop_max = 65536;
  95. module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR);
  96. MODULE_PARM_DESC(usbfs_snoop_max,
  97. "maximum number of bytes to print while snooping");
  98. #define snoop(dev, format, arg...) \
  99. do { \
  100. if (usbfs_snoop) \
  101. dev_info(dev, format, ## arg); \
  102. } while (0)
  103. enum snoop_when {
  104. SUBMIT, COMPLETE
  105. };
  106. #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
  107. /* Limit on the total amount of memory we can allocate for transfers */
  108. static u32 usbfs_memory_mb = 16;
  109. module_param(usbfs_memory_mb, uint, 0644);
  110. MODULE_PARM_DESC(usbfs_memory_mb,
  111. "maximum MB allowed for usbfs buffers (0 = no limit)");
  112. /* Hard limit, necessary to avoid arithmetic overflow */
  113. #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
  114. static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */
  115. /* Check whether it's okay to allocate more memory for a transfer */
  116. static int usbfs_increase_memory_usage(u64 amount)
  117. {
  118. u64 lim;
  119. lim = READ_ONCE(usbfs_memory_mb);
  120. lim <<= 20;
  121. atomic64_add(amount, &usbfs_memory_usage);
  122. if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) {
  123. atomic64_sub(amount, &usbfs_memory_usage);
  124. return -ENOMEM;
  125. }
  126. return 0;
  127. }
  128. /* Memory for a transfer is being deallocated */
  129. static void usbfs_decrease_memory_usage(u64 amount)
  130. {
  131. atomic64_sub(amount, &usbfs_memory_usage);
  132. }
  133. static int connected(struct usb_dev_state *ps)
  134. {
  135. return (!list_empty(&ps->list) &&
  136. ps->dev->state != USB_STATE_NOTATTACHED);
  137. }
  138. static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count)
  139. {
  140. struct usb_dev_state *ps = usbm->ps;
  141. unsigned long flags;
  142. spin_lock_irqsave(&ps->lock, flags);
  143. --*count;
  144. if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) {
  145. list_del(&usbm->memlist);
  146. spin_unlock_irqrestore(&ps->lock, flags);
  147. usb_free_coherent(ps->dev, usbm->size, usbm->mem,
  148. usbm->dma_handle);
  149. usbfs_decrease_memory_usage(
  150. usbm->size + sizeof(struct usb_memory));
  151. kfree(usbm);
  152. } else {
  153. spin_unlock_irqrestore(&ps->lock, flags);
  154. }
  155. }
  156. static void usbdev_vm_open(struct vm_area_struct *vma)
  157. {
  158. struct usb_memory *usbm = vma->vm_private_data;
  159. unsigned long flags;
  160. spin_lock_irqsave(&usbm->ps->lock, flags);
  161. ++usbm->vma_use_count;
  162. spin_unlock_irqrestore(&usbm->ps->lock, flags);
  163. }
  164. static void usbdev_vm_close(struct vm_area_struct *vma)
  165. {
  166. struct usb_memory *usbm = vma->vm_private_data;
  167. dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
  168. }
  169. static const struct vm_operations_struct usbdev_vm_ops = {
  170. .open = usbdev_vm_open,
  171. .close = usbdev_vm_close
  172. };
  173. static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
  174. {
  175. struct usb_memory *usbm = NULL;
  176. struct usb_dev_state *ps = file->private_data;
  177. size_t size = vma->vm_end - vma->vm_start;
  178. void *mem;
  179. unsigned long flags;
  180. dma_addr_t dma_handle;
  181. int ret;
  182. ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory));
  183. if (ret)
  184. goto error;
  185. usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL);
  186. if (!usbm) {
  187. ret = -ENOMEM;
  188. goto error_decrease_mem;
  189. }
  190. mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN,
  191. &dma_handle);
  192. if (!mem) {
  193. ret = -ENOMEM;
  194. goto error_free_usbm;
  195. }
  196. memset(mem, 0, size);
  197. usbm->mem = mem;
  198. usbm->dma_handle = dma_handle;
  199. usbm->size = size;
  200. usbm->ps = ps;
  201. usbm->vm_start = vma->vm_start;
  202. usbm->vma_use_count = 1;
  203. INIT_LIST_HEAD(&usbm->memlist);
  204. if (remap_pfn_range(vma, vma->vm_start,
  205. virt_to_phys(usbm->mem) >> PAGE_SHIFT,
  206. size, vma->vm_page_prot) < 0) {
  207. dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
  208. return -EAGAIN;
  209. }
  210. vma->vm_flags |= VM_IO;
  211. vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
  212. vma->vm_ops = &usbdev_vm_ops;
  213. vma->vm_private_data = usbm;
  214. spin_lock_irqsave(&ps->lock, flags);
  215. list_add_tail(&usbm->memlist, &ps->memory_list);
  216. spin_unlock_irqrestore(&ps->lock, flags);
  217. return 0;
  218. error_free_usbm:
  219. kfree(usbm);
  220. error_decrease_mem:
  221. usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
  222. error:
  223. return ret;
  224. }
  225. static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
  226. loff_t *ppos)
  227. {
  228. struct usb_dev_state *ps = file->private_data;
  229. struct usb_device *dev = ps->dev;
  230. ssize_t ret = 0;
  231. unsigned len;
  232. loff_t pos;
  233. int i;
  234. pos = *ppos;
  235. usb_lock_device(dev);
  236. if (!connected(ps)) {
  237. ret = -ENODEV;
  238. goto err;
  239. } else if (pos < 0) {
  240. ret = -EINVAL;
  241. goto err;
  242. }
  243. if (pos < sizeof(struct usb_device_descriptor)) {
  244. /* 18 bytes - fits on the stack */
  245. struct usb_device_descriptor temp_desc;
  246. memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
  247. le16_to_cpus(&temp_desc.bcdUSB);
  248. le16_to_cpus(&temp_desc.idVendor);
  249. le16_to_cpus(&temp_desc.idProduct);
  250. le16_to_cpus(&temp_desc.bcdDevice);
  251. len = sizeof(struct usb_device_descriptor) - pos;
  252. if (len > nbytes)
  253. len = nbytes;
  254. if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
  255. ret = -EFAULT;
  256. goto err;
  257. }
  258. *ppos += len;
  259. buf += len;
  260. nbytes -= len;
  261. ret += len;
  262. }
  263. pos = sizeof(struct usb_device_descriptor);
  264. for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
  265. struct usb_config_descriptor *config =
  266. (struct usb_config_descriptor *)dev->rawdescriptors[i];
  267. unsigned int length = le16_to_cpu(config->wTotalLength);
  268. if (*ppos < pos + length) {
  269. /* The descriptor may claim to be longer than it
  270. * really is. Here is the actual allocated length. */
  271. unsigned alloclen =
  272. le16_to_cpu(dev->config[i].desc.wTotalLength);
  273. len = length - (*ppos - pos);
  274. if (len > nbytes)
  275. len = nbytes;
  276. /* Simply don't write (skip over) unallocated parts */
  277. if (alloclen > (*ppos - pos)) {
  278. alloclen -= (*ppos - pos);
  279. if (copy_to_user(buf,
  280. dev->rawdescriptors[i] + (*ppos - pos),
  281. min(len, alloclen))) {
  282. ret = -EFAULT;
  283. goto err;
  284. }
  285. }
  286. *ppos += len;
  287. buf += len;
  288. nbytes -= len;
  289. ret += len;
  290. }
  291. pos += length;
  292. }
  293. err:
  294. usb_unlock_device(dev);
  295. return ret;
  296. }
  297. /*
  298. * async list handling
  299. */
  300. static struct async *alloc_async(unsigned int numisoframes)
  301. {
  302. struct async *as;
  303. as = kzalloc(sizeof(struct async), GFP_KERNEL);
  304. if (!as)
  305. return NULL;
  306. as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
  307. if (!as->urb) {
  308. kfree(as);
  309. return NULL;
  310. }
  311. return as;
  312. }
  313. static void free_async(struct async *as)
  314. {
  315. int i;
  316. put_pid(as->pid);
  317. if (as->cred)
  318. put_cred(as->cred);
  319. for (i = 0; i < as->urb->num_sgs; i++) {
  320. if (sg_page(&as->urb->sg[i]))
  321. kfree(sg_virt(&as->urb->sg[i]));
  322. }
  323. kfree(as->urb->sg);
  324. if (as->usbm == NULL)
  325. kfree(as->urb->transfer_buffer);
  326. else
  327. dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
  328. kfree(as->urb->setup_packet);
  329. usb_free_urb(as->urb);
  330. usbfs_decrease_memory_usage(as->mem_usage);
  331. kfree(as);
  332. }
  333. static void async_newpending(struct async *as)
  334. {
  335. struct usb_dev_state *ps = as->ps;
  336. unsigned long flags;
  337. spin_lock_irqsave(&ps->lock, flags);
  338. list_add_tail(&as->asynclist, &ps->async_pending);
  339. spin_unlock_irqrestore(&ps->lock, flags);
  340. }
  341. static void async_removepending(struct async *as)
  342. {
  343. struct usb_dev_state *ps = as->ps;
  344. unsigned long flags;
  345. spin_lock_irqsave(&ps->lock, flags);
  346. list_del_init(&as->asynclist);
  347. spin_unlock_irqrestore(&ps->lock, flags);
  348. }
  349. static struct async *async_getcompleted(struct usb_dev_state *ps)
  350. {
  351. unsigned long flags;
  352. struct async *as = NULL;
  353. spin_lock_irqsave(&ps->lock, flags);
  354. if (!list_empty(&ps->async_completed)) {
  355. as = list_entry(ps->async_completed.next, struct async,
  356. asynclist);
  357. list_del_init(&as->asynclist);
  358. }
  359. spin_unlock_irqrestore(&ps->lock, flags);
  360. return as;
  361. }
  362. static struct async *async_getpending(struct usb_dev_state *ps,
  363. void __user *userurb)
  364. {
  365. struct async *as;
  366. list_for_each_entry(as, &ps->async_pending, asynclist)
  367. if (as->userurb == userurb) {
  368. list_del_init(&as->asynclist);
  369. return as;
  370. }
  371. return NULL;
  372. }
  373. static void snoop_urb(struct usb_device *udev,
  374. void __user *userurb, int pipe, unsigned length,
  375. int timeout_or_status, enum snoop_when when,
  376. unsigned char *data, unsigned data_len)
  377. {
  378. static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
  379. static const char *dirs[] = {"out", "in"};
  380. int ep;
  381. const char *t, *d;
  382. if (!usbfs_snoop)
  383. return;
  384. ep = usb_pipeendpoint(pipe);
  385. t = types[usb_pipetype(pipe)];
  386. d = dirs[!!usb_pipein(pipe)];
  387. if (userurb) { /* Async */
  388. if (when == SUBMIT)
  389. dev_info(&udev->dev, "userurb %px, ep%d %s-%s, "
  390. "length %u\n",
  391. userurb, ep, t, d, length);
  392. else
  393. dev_info(&udev->dev, "userurb %px, ep%d %s-%s, "
  394. "actual_length %u status %d\n",
  395. userurb, ep, t, d, length,
  396. timeout_or_status);
  397. } else {
  398. if (when == SUBMIT)
  399. dev_info(&udev->dev, "ep%d %s-%s, length %u, "
  400. "timeout %d\n",
  401. ep, t, d, length, timeout_or_status);
  402. else
  403. dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
  404. "status %d\n",
  405. ep, t, d, length, timeout_or_status);
  406. }
  407. data_len = min(data_len, usbfs_snoop_max);
  408. if (data && data_len > 0) {
  409. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
  410. data, data_len, 1);
  411. }
  412. }
  413. static void snoop_urb_data(struct urb *urb, unsigned len)
  414. {
  415. int i, size;
  416. len = min(len, usbfs_snoop_max);
  417. if (!usbfs_snoop || len == 0)
  418. return;
  419. if (urb->num_sgs == 0) {
  420. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
  421. urb->transfer_buffer, len, 1);
  422. return;
  423. }
  424. for (i = 0; i < urb->num_sgs && len; i++) {
  425. size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
  426. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
  427. sg_virt(&urb->sg[i]), size, 1);
  428. len -= size;
  429. }
  430. }
  431. static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
  432. {
  433. unsigned i, len, size;
  434. if (urb->number_of_packets > 0) /* Isochronous */
  435. len = urb->transfer_buffer_length;
  436. else /* Non-Isoc */
  437. len = urb->actual_length;
  438. if (urb->num_sgs == 0) {
  439. if (copy_to_user(userbuffer, urb->transfer_buffer, len))
  440. return -EFAULT;
  441. return 0;
  442. }
  443. for (i = 0; i < urb->num_sgs && len; i++) {
  444. size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
  445. if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
  446. return -EFAULT;
  447. userbuffer += size;
  448. len -= size;
  449. }
  450. return 0;
  451. }
  452. #define AS_CONTINUATION 1
  453. #define AS_UNLINK 2
  454. static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
  455. __releases(ps->lock)
  456. __acquires(ps->lock)
  457. {
  458. struct urb *urb;
  459. struct async *as;
  460. /* Mark all the pending URBs that match bulk_addr, up to but not
  461. * including the first one without AS_CONTINUATION. If such an
  462. * URB is encountered then a new transfer has already started so
  463. * the endpoint doesn't need to be disabled; otherwise it does.
  464. */
  465. list_for_each_entry(as, &ps->async_pending, asynclist) {
  466. if (as->bulk_addr == bulk_addr) {
  467. if (as->bulk_status != AS_CONTINUATION)
  468. goto rescan;
  469. as->bulk_status = AS_UNLINK;
  470. as->bulk_addr = 0;
  471. }
  472. }
  473. ps->disabled_bulk_eps |= (1 << bulk_addr);
  474. /* Now carefully unlink all the marked pending URBs */
  475. rescan:
  476. list_for_each_entry(as, &ps->async_pending, asynclist) {
  477. if (as->bulk_status == AS_UNLINK) {
  478. as->bulk_status = 0; /* Only once */
  479. urb = as->urb;
  480. usb_get_urb(urb);
  481. spin_unlock(&ps->lock); /* Allow completions */
  482. usb_unlink_urb(urb);
  483. usb_put_urb(urb);
  484. spin_lock(&ps->lock);
  485. goto rescan;
  486. }
  487. }
  488. }
  489. static void async_completed(struct urb *urb)
  490. {
  491. struct async *as = urb->context;
  492. struct usb_dev_state *ps = as->ps;
  493. struct siginfo sinfo;
  494. struct pid *pid = NULL;
  495. const struct cred *cred = NULL;
  496. unsigned long flags;
  497. int signr;
  498. spin_lock_irqsave(&ps->lock, flags);
  499. list_move_tail(&as->asynclist, &ps->async_completed);
  500. as->status = urb->status;
  501. signr = as->signr;
  502. if (signr) {
  503. clear_siginfo(&sinfo);
  504. sinfo.si_signo = as->signr;
  505. sinfo.si_errno = as->status;
  506. sinfo.si_code = SI_ASYNCIO;
  507. sinfo.si_addr = as->userurb;
  508. pid = get_pid(as->pid);
  509. cred = get_cred(as->cred);
  510. }
  511. snoop(&urb->dev->dev, "urb complete\n");
  512. snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
  513. as->status, COMPLETE, NULL, 0);
  514. if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN)
  515. snoop_urb_data(urb, urb->actual_length);
  516. if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
  517. as->status != -ENOENT)
  518. cancel_bulk_urbs(ps, as->bulk_addr);
  519. wake_up(&ps->wait);
  520. spin_unlock_irqrestore(&ps->lock, flags);
  521. if (signr) {
  522. kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred);
  523. put_pid(pid);
  524. put_cred(cred);
  525. }
  526. }
  527. static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
  528. {
  529. struct urb *urb;
  530. struct async *as;
  531. unsigned long flags;
  532. spin_lock_irqsave(&ps->lock, flags);
  533. while (!list_empty(list)) {
  534. as = list_entry(list->next, struct async, asynclist);
  535. list_del_init(&as->asynclist);
  536. urb = as->urb;
  537. usb_get_urb(urb);
  538. /* drop the spinlock so the completion handler can run */
  539. spin_unlock_irqrestore(&ps->lock, flags);
  540. usb_kill_urb(urb);
  541. usb_put_urb(urb);
  542. spin_lock_irqsave(&ps->lock, flags);
  543. }
  544. spin_unlock_irqrestore(&ps->lock, flags);
  545. }
  546. static void destroy_async_on_interface(struct usb_dev_state *ps,
  547. unsigned int ifnum)
  548. {
  549. struct list_head *p, *q, hitlist;
  550. unsigned long flags;
  551. INIT_LIST_HEAD(&hitlist);
  552. spin_lock_irqsave(&ps->lock, flags);
  553. list_for_each_safe(p, q, &ps->async_pending)
  554. if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
  555. list_move_tail(p, &hitlist);
  556. spin_unlock_irqrestore(&ps->lock, flags);
  557. destroy_async(ps, &hitlist);
  558. }
  559. static void destroy_all_async(struct usb_dev_state *ps)
  560. {
  561. destroy_async(ps, &ps->async_pending);
  562. }
  563. /*
  564. * interface claims are made only at the request of user level code,
  565. * which can also release them (explicitly or by closing files).
  566. * they're also undone when devices disconnect.
  567. */
  568. static int driver_probe(struct usb_interface *intf,
  569. const struct usb_device_id *id)
  570. {
  571. return -ENODEV;
  572. }
  573. static void driver_disconnect(struct usb_interface *intf)
  574. {
  575. struct usb_dev_state *ps = usb_get_intfdata(intf);
  576. unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
  577. if (!ps)
  578. return;
  579. /* NOTE: this relies on usbcore having canceled and completed
  580. * all pending I/O requests; 2.6 does that.
  581. */
  582. if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
  583. clear_bit(ifnum, &ps->ifclaimed);
  584. else
  585. dev_warn(&intf->dev, "interface number %u out of range\n",
  586. ifnum);
  587. usb_set_intfdata(intf, NULL);
  588. /* force async requests to complete */
  589. destroy_async_on_interface(ps, ifnum);
  590. }
  591. /* The following routines are merely placeholders. There is no way
  592. * to inform a user task about suspend or resumes.
  593. */
  594. static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
  595. {
  596. return 0;
  597. }
  598. static int driver_resume(struct usb_interface *intf)
  599. {
  600. return 0;
  601. }
  602. struct usb_driver usbfs_driver = {
  603. .name = "usbfs",
  604. .probe = driver_probe,
  605. .disconnect = driver_disconnect,
  606. .suspend = driver_suspend,
  607. .resume = driver_resume,
  608. };
  609. static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
  610. {
  611. struct usb_device *dev = ps->dev;
  612. struct usb_interface *intf;
  613. int err;
  614. if (ifnum >= 8*sizeof(ps->ifclaimed))
  615. return -EINVAL;
  616. /* already claimed */
  617. if (test_bit(ifnum, &ps->ifclaimed))
  618. return 0;
  619. if (ps->privileges_dropped &&
  620. !test_bit(ifnum, &ps->interface_allowed_mask))
  621. return -EACCES;
  622. intf = usb_ifnum_to_if(dev, ifnum);
  623. if (!intf)
  624. err = -ENOENT;
  625. else {
  626. unsigned int old_suppress;
  627. /* suppress uevents while claiming interface */
  628. old_suppress = dev_get_uevent_suppress(&intf->dev);
  629. dev_set_uevent_suppress(&intf->dev, 1);
  630. err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
  631. dev_set_uevent_suppress(&intf->dev, old_suppress);
  632. }
  633. if (err == 0)
  634. set_bit(ifnum, &ps->ifclaimed);
  635. return err;
  636. }
  637. static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
  638. {
  639. struct usb_device *dev;
  640. struct usb_interface *intf;
  641. int err;
  642. err = -EINVAL;
  643. if (ifnum >= 8*sizeof(ps->ifclaimed))
  644. return err;
  645. dev = ps->dev;
  646. intf = usb_ifnum_to_if(dev, ifnum);
  647. if (!intf)
  648. err = -ENOENT;
  649. else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
  650. unsigned int old_suppress;
  651. /* suppress uevents while releasing interface */
  652. old_suppress = dev_get_uevent_suppress(&intf->dev);
  653. dev_set_uevent_suppress(&intf->dev, 1);
  654. usb_driver_release_interface(&usbfs_driver, intf);
  655. dev_set_uevent_suppress(&intf->dev, old_suppress);
  656. err = 0;
  657. }
  658. return err;
  659. }
  660. static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
  661. {
  662. if (ps->dev->state != USB_STATE_CONFIGURED)
  663. return -EHOSTUNREACH;
  664. if (ifnum >= 8*sizeof(ps->ifclaimed))
  665. return -EINVAL;
  666. if (test_bit(ifnum, &ps->ifclaimed))
  667. return 0;
  668. /* if not yet claimed, claim it for the driver */
  669. dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
  670. "interface %u before use\n", task_pid_nr(current),
  671. current->comm, ifnum);
  672. return claimintf(ps, ifnum);
  673. }
  674. static int findintfep(struct usb_device *dev, unsigned int ep)
  675. {
  676. unsigned int i, j, e;
  677. struct usb_interface *intf;
  678. struct usb_host_interface *alts;
  679. struct usb_endpoint_descriptor *endpt;
  680. if (ep & ~(USB_DIR_IN|0xf))
  681. return -EINVAL;
  682. if (!dev->actconfig)
  683. return -ESRCH;
  684. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  685. intf = dev->actconfig->interface[i];
  686. for (j = 0; j < intf->num_altsetting; j++) {
  687. alts = &intf->altsetting[j];
  688. for (e = 0; e < alts->desc.bNumEndpoints; e++) {
  689. endpt = &alts->endpoint[e].desc;
  690. if (endpt->bEndpointAddress == ep)
  691. return alts->desc.bInterfaceNumber;
  692. }
  693. }
  694. }
  695. return -ENOENT;
  696. }
  697. static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
  698. unsigned int request, unsigned int index)
  699. {
  700. int ret = 0;
  701. struct usb_host_interface *alt_setting;
  702. if (ps->dev->state != USB_STATE_UNAUTHENTICATED
  703. && ps->dev->state != USB_STATE_ADDRESS
  704. && ps->dev->state != USB_STATE_CONFIGURED)
  705. return -EHOSTUNREACH;
  706. if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
  707. return 0;
  708. /*
  709. * check for the special corner case 'get_device_id' in the printer
  710. * class specification, which we always want to allow as it is used
  711. * to query things like ink level, etc.
  712. */
  713. if (requesttype == 0xa1 && request == 0) {
  714. alt_setting = usb_find_alt_setting(ps->dev->actconfig,
  715. index >> 8, index & 0xff);
  716. if (alt_setting
  717. && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
  718. return 0;
  719. }
  720. index &= 0xff;
  721. switch (requesttype & USB_RECIP_MASK) {
  722. case USB_RECIP_ENDPOINT:
  723. if ((index & ~USB_DIR_IN) == 0)
  724. return 0;
  725. ret = findintfep(ps->dev, index);
  726. if (ret < 0) {
  727. /*
  728. * Some not fully compliant Win apps seem to get
  729. * index wrong and have the endpoint number here
  730. * rather than the endpoint address (with the
  731. * correct direction). Win does let this through,
  732. * so we'll not reject it here but leave it to
  733. * the device to not break KVM. But we warn.
  734. */
  735. ret = findintfep(ps->dev, index ^ 0x80);
  736. if (ret >= 0)
  737. dev_info(&ps->dev->dev,
  738. "%s: process %i (%s) requesting ep %02x but needs %02x\n",
  739. __func__, task_pid_nr(current),
  740. current->comm, index, index ^ 0x80);
  741. }
  742. if (ret >= 0)
  743. ret = checkintf(ps, ret);
  744. break;
  745. case USB_RECIP_INTERFACE:
  746. ret = checkintf(ps, index);
  747. break;
  748. }
  749. return ret;
  750. }
  751. static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
  752. unsigned char ep)
  753. {
  754. if (ep & USB_ENDPOINT_DIR_MASK)
  755. return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
  756. else
  757. return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
  758. }
  759. static int parse_usbdevfs_streams(struct usb_dev_state *ps,
  760. struct usbdevfs_streams __user *streams,
  761. unsigned int *num_streams_ret,
  762. unsigned int *num_eps_ret,
  763. struct usb_host_endpoint ***eps_ret,
  764. struct usb_interface **intf_ret)
  765. {
  766. unsigned int i, num_streams, num_eps;
  767. struct usb_host_endpoint **eps;
  768. struct usb_interface *intf = NULL;
  769. unsigned char ep;
  770. int ifnum, ret;
  771. if (get_user(num_streams, &streams->num_streams) ||
  772. get_user(num_eps, &streams->num_eps))
  773. return -EFAULT;
  774. if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
  775. return -EINVAL;
  776. /* The XHCI controller allows max 2 ^ 16 streams */
  777. if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
  778. return -EINVAL;
  779. eps = kmalloc_array(num_eps, sizeof(*eps), GFP_KERNEL);
  780. if (!eps)
  781. return -ENOMEM;
  782. for (i = 0; i < num_eps; i++) {
  783. if (get_user(ep, &streams->eps[i])) {
  784. ret = -EFAULT;
  785. goto error;
  786. }
  787. eps[i] = ep_to_host_endpoint(ps->dev, ep);
  788. if (!eps[i]) {
  789. ret = -EINVAL;
  790. goto error;
  791. }
  792. /* usb_alloc/free_streams operate on an usb_interface */
  793. ifnum = findintfep(ps->dev, ep);
  794. if (ifnum < 0) {
  795. ret = ifnum;
  796. goto error;
  797. }
  798. if (i == 0) {
  799. ret = checkintf(ps, ifnum);
  800. if (ret < 0)
  801. goto error;
  802. intf = usb_ifnum_to_if(ps->dev, ifnum);
  803. } else {
  804. /* Verify all eps belong to the same interface */
  805. if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
  806. ret = -EINVAL;
  807. goto error;
  808. }
  809. }
  810. }
  811. if (num_streams_ret)
  812. *num_streams_ret = num_streams;
  813. *num_eps_ret = num_eps;
  814. *eps_ret = eps;
  815. *intf_ret = intf;
  816. return 0;
  817. error:
  818. kfree(eps);
  819. return ret;
  820. }
  821. static int match_devt(struct device *dev, void *data)
  822. {
  823. return dev->devt == (dev_t) (unsigned long) data;
  824. }
  825. static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
  826. {
  827. struct device *dev;
  828. dev = bus_find_device(&usb_bus_type, NULL,
  829. (void *) (unsigned long) devt, match_devt);
  830. if (!dev)
  831. return NULL;
  832. return to_usb_device(dev);
  833. }
  834. /*
  835. * file operations
  836. */
  837. static int usbdev_open(struct inode *inode, struct file *file)
  838. {
  839. struct usb_device *dev = NULL;
  840. struct usb_dev_state *ps;
  841. int ret;
  842. ret = -ENOMEM;
  843. ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
  844. if (!ps)
  845. goto out_free_ps;
  846. ret = -ENODEV;
  847. /* Protect against simultaneous removal or release */
  848. mutex_lock(&usbfs_mutex);
  849. /* usbdev device-node */
  850. if (imajor(inode) == USB_DEVICE_MAJOR)
  851. dev = usbdev_lookup_by_devt(inode->i_rdev);
  852. mutex_unlock(&usbfs_mutex);
  853. if (!dev)
  854. goto out_free_ps;
  855. usb_lock_device(dev);
  856. if (dev->state == USB_STATE_NOTATTACHED)
  857. goto out_unlock_device;
  858. ret = usb_autoresume_device(dev);
  859. if (ret)
  860. goto out_unlock_device;
  861. ps->dev = dev;
  862. ps->file = file;
  863. ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */
  864. spin_lock_init(&ps->lock);
  865. INIT_LIST_HEAD(&ps->list);
  866. INIT_LIST_HEAD(&ps->async_pending);
  867. INIT_LIST_HEAD(&ps->async_completed);
  868. INIT_LIST_HEAD(&ps->memory_list);
  869. init_waitqueue_head(&ps->wait);
  870. ps->disc_pid = get_pid(task_pid(current));
  871. ps->cred = get_current_cred();
  872. smp_wmb();
  873. list_add_tail(&ps->list, &dev->filelist);
  874. file->private_data = ps;
  875. usb_unlock_device(dev);
  876. snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
  877. current->comm);
  878. return ret;
  879. out_unlock_device:
  880. usb_unlock_device(dev);
  881. usb_put_dev(dev);
  882. out_free_ps:
  883. kfree(ps);
  884. return ret;
  885. }
  886. static int usbdev_release(struct inode *inode, struct file *file)
  887. {
  888. struct usb_dev_state *ps = file->private_data;
  889. struct usb_device *dev = ps->dev;
  890. unsigned int ifnum;
  891. struct async *as;
  892. usb_lock_device(dev);
  893. usb_hub_release_all_ports(dev, ps);
  894. list_del_init(&ps->list);
  895. for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
  896. ifnum++) {
  897. if (test_bit(ifnum, &ps->ifclaimed))
  898. releaseintf(ps, ifnum);
  899. }
  900. destroy_all_async(ps);
  901. usb_autosuspend_device(dev);
  902. usb_unlock_device(dev);
  903. usb_put_dev(dev);
  904. put_pid(ps->disc_pid);
  905. put_cred(ps->cred);
  906. as = async_getcompleted(ps);
  907. while (as) {
  908. free_async(as);
  909. as = async_getcompleted(ps);
  910. }
  911. kfree(ps);
  912. return 0;
  913. }
  914. static int proc_control(struct usb_dev_state *ps, void __user *arg)
  915. {
  916. struct usb_device *dev = ps->dev;
  917. struct usbdevfs_ctrltransfer ctrl;
  918. unsigned int tmo;
  919. unsigned char *tbuf;
  920. unsigned wLength;
  921. int i, pipe, ret;
  922. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  923. return -EFAULT;
  924. ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
  925. ctrl.wIndex);
  926. if (ret)
  927. return ret;
  928. wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
  929. if (wLength > PAGE_SIZE)
  930. return -EINVAL;
  931. ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
  932. sizeof(struct usb_ctrlrequest));
  933. if (ret)
  934. return ret;
  935. tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  936. if (!tbuf) {
  937. ret = -ENOMEM;
  938. goto done;
  939. }
  940. tmo = ctrl.timeout;
  941. snoop(&dev->dev, "control urb: bRequestType=%02x "
  942. "bRequest=%02x wValue=%04x "
  943. "wIndex=%04x wLength=%04x\n",
  944. ctrl.bRequestType, ctrl.bRequest, ctrl.wValue,
  945. ctrl.wIndex, ctrl.wLength);
  946. if (ctrl.bRequestType & 0x80) {
  947. if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
  948. ctrl.wLength)) {
  949. ret = -EINVAL;
  950. goto done;
  951. }
  952. pipe = usb_rcvctrlpipe(dev, 0);
  953. snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
  954. usb_unlock_device(dev);
  955. i = usb_control_msg(dev, pipe, ctrl.bRequest,
  956. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  957. tbuf, ctrl.wLength, tmo);
  958. usb_lock_device(dev);
  959. snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
  960. tbuf, max(i, 0));
  961. if ((i > 0) && ctrl.wLength) {
  962. if (copy_to_user(ctrl.data, tbuf, i)) {
  963. ret = -EFAULT;
  964. goto done;
  965. }
  966. }
  967. } else {
  968. if (ctrl.wLength) {
  969. if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
  970. ret = -EFAULT;
  971. goto done;
  972. }
  973. }
  974. pipe = usb_sndctrlpipe(dev, 0);
  975. snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
  976. tbuf, ctrl.wLength);
  977. usb_unlock_device(dev);
  978. i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
  979. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  980. tbuf, ctrl.wLength, tmo);
  981. usb_lock_device(dev);
  982. snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
  983. }
  984. if (i < 0 && i != -EPIPE) {
  985. dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
  986. "failed cmd %s rqt %u rq %u len %u ret %d\n",
  987. current->comm, ctrl.bRequestType, ctrl.bRequest,
  988. ctrl.wLength, i);
  989. }
  990. ret = i;
  991. done:
  992. free_page((unsigned long) tbuf);
  993. usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
  994. sizeof(struct usb_ctrlrequest));
  995. return ret;
  996. }
  997. static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
  998. {
  999. struct usb_device *dev = ps->dev;
  1000. struct usbdevfs_bulktransfer bulk;
  1001. unsigned int tmo, len1, pipe;
  1002. int len2;
  1003. unsigned char *tbuf;
  1004. int i, ret;
  1005. if (copy_from_user(&bulk, arg, sizeof(bulk)))
  1006. return -EFAULT;
  1007. ret = findintfep(ps->dev, bulk.ep);
  1008. if (ret < 0)
  1009. return ret;
  1010. ret = checkintf(ps, ret);
  1011. if (ret)
  1012. return ret;
  1013. if (bulk.ep & USB_DIR_IN)
  1014. pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
  1015. else
  1016. pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
  1017. if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
  1018. return -EINVAL;
  1019. len1 = bulk.len;
  1020. if (len1 >= (INT_MAX - sizeof(struct urb)))
  1021. return -EINVAL;
  1022. ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
  1023. if (ret)
  1024. return ret;
  1025. tbuf = kmalloc(len1, GFP_KERNEL);
  1026. if (!tbuf) {
  1027. ret = -ENOMEM;
  1028. goto done;
  1029. }
  1030. tmo = bulk.timeout;
  1031. if (bulk.ep & 0x80) {
  1032. if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
  1033. ret = -EINVAL;
  1034. goto done;
  1035. }
  1036. snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
  1037. usb_unlock_device(dev);
  1038. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  1039. usb_lock_device(dev);
  1040. snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
  1041. if (!i && len2) {
  1042. if (copy_to_user(bulk.data, tbuf, len2)) {
  1043. ret = -EFAULT;
  1044. goto done;
  1045. }
  1046. }
  1047. } else {
  1048. if (len1) {
  1049. if (copy_from_user(tbuf, bulk.data, len1)) {
  1050. ret = -EFAULT;
  1051. goto done;
  1052. }
  1053. }
  1054. snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
  1055. usb_unlock_device(dev);
  1056. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  1057. usb_lock_device(dev);
  1058. snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
  1059. }
  1060. ret = (i < 0 ? i : len2);
  1061. done:
  1062. kfree(tbuf);
  1063. usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
  1064. return ret;
  1065. }
  1066. static void check_reset_of_active_ep(struct usb_device *udev,
  1067. unsigned int epnum, char *ioctl_name)
  1068. {
  1069. struct usb_host_endpoint **eps;
  1070. struct usb_host_endpoint *ep;
  1071. eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
  1072. ep = eps[epnum & 0x0f];
  1073. if (ep && !list_empty(&ep->urb_list))
  1074. dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
  1075. task_pid_nr(current), current->comm,
  1076. ioctl_name, epnum);
  1077. }
  1078. static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
  1079. {
  1080. unsigned int ep;
  1081. int ret;
  1082. if (get_user(ep, (unsigned int __user *)arg))
  1083. return -EFAULT;
  1084. ret = findintfep(ps->dev, ep);
  1085. if (ret < 0)
  1086. return ret;
  1087. ret = checkintf(ps, ret);
  1088. if (ret)
  1089. return ret;
  1090. check_reset_of_active_ep(ps->dev, ep, "RESETEP");
  1091. usb_reset_endpoint(ps->dev, ep);
  1092. return 0;
  1093. }
  1094. static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
  1095. {
  1096. unsigned int ep;
  1097. int pipe;
  1098. int ret;
  1099. if (get_user(ep, (unsigned int __user *)arg))
  1100. return -EFAULT;
  1101. ret = findintfep(ps->dev, ep);
  1102. if (ret < 0)
  1103. return ret;
  1104. ret = checkintf(ps, ret);
  1105. if (ret)
  1106. return ret;
  1107. check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
  1108. if (ep & USB_DIR_IN)
  1109. pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
  1110. else
  1111. pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
  1112. return usb_clear_halt(ps->dev, pipe);
  1113. }
  1114. static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
  1115. {
  1116. struct usbdevfs_getdriver gd;
  1117. struct usb_interface *intf;
  1118. int ret;
  1119. if (copy_from_user(&gd, arg, sizeof(gd)))
  1120. return -EFAULT;
  1121. intf = usb_ifnum_to_if(ps->dev, gd.interface);
  1122. if (!intf || !intf->dev.driver)
  1123. ret = -ENODATA;
  1124. else {
  1125. strlcpy(gd.driver, intf->dev.driver->name,
  1126. sizeof(gd.driver));
  1127. ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
  1128. }
  1129. return ret;
  1130. }
  1131. static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
  1132. {
  1133. struct usbdevfs_connectinfo ci;
  1134. memset(&ci, 0, sizeof(ci));
  1135. ci.devnum = ps->dev->devnum;
  1136. ci.slow = ps->dev->speed == USB_SPEED_LOW;
  1137. if (copy_to_user(arg, &ci, sizeof(ci)))
  1138. return -EFAULT;
  1139. return 0;
  1140. }
  1141. static int proc_resetdevice(struct usb_dev_state *ps)
  1142. {
  1143. struct usb_host_config *actconfig = ps->dev->actconfig;
  1144. struct usb_interface *interface;
  1145. int i, number;
  1146. /* Don't allow a device reset if the process has dropped the
  1147. * privilege to do such things and any of the interfaces are
  1148. * currently claimed.
  1149. */
  1150. if (ps->privileges_dropped && actconfig) {
  1151. for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
  1152. interface = actconfig->interface[i];
  1153. number = interface->cur_altsetting->desc.bInterfaceNumber;
  1154. if (usb_interface_claimed(interface) &&
  1155. !test_bit(number, &ps->ifclaimed)) {
  1156. dev_warn(&ps->dev->dev,
  1157. "usbfs: interface %d claimed by %s while '%s' resets device\n",
  1158. number, interface->dev.driver->name, current->comm);
  1159. return -EACCES;
  1160. }
  1161. }
  1162. }
  1163. return usb_reset_device(ps->dev);
  1164. }
  1165. static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
  1166. {
  1167. struct usbdevfs_setinterface setintf;
  1168. int ret;
  1169. if (copy_from_user(&setintf, arg, sizeof(setintf)))
  1170. return -EFAULT;
  1171. ret = checkintf(ps, setintf.interface);
  1172. if (ret)
  1173. return ret;
  1174. destroy_async_on_interface(ps, setintf.interface);
  1175. return usb_set_interface(ps->dev, setintf.interface,
  1176. setintf.altsetting);
  1177. }
  1178. static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
  1179. {
  1180. int u;
  1181. int status = 0;
  1182. struct usb_host_config *actconfig;
  1183. if (get_user(u, (int __user *)arg))
  1184. return -EFAULT;
  1185. actconfig = ps->dev->actconfig;
  1186. /* Don't touch the device if any interfaces are claimed.
  1187. * It could interfere with other drivers' operations, and if
  1188. * an interface is claimed by usbfs it could easily deadlock.
  1189. */
  1190. if (actconfig) {
  1191. int i;
  1192. for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
  1193. if (usb_interface_claimed(actconfig->interface[i])) {
  1194. dev_warn(&ps->dev->dev,
  1195. "usbfs: interface %d claimed by %s "
  1196. "while '%s' sets config #%d\n",
  1197. actconfig->interface[i]
  1198. ->cur_altsetting
  1199. ->desc.bInterfaceNumber,
  1200. actconfig->interface[i]
  1201. ->dev.driver->name,
  1202. current->comm, u);
  1203. status = -EBUSY;
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
  1209. * so avoid usb_set_configuration()'s kick to sysfs
  1210. */
  1211. if (status == 0) {
  1212. if (actconfig && actconfig->desc.bConfigurationValue == u)
  1213. status = usb_reset_configuration(ps->dev);
  1214. else
  1215. status = usb_set_configuration(ps->dev, u);
  1216. }
  1217. return status;
  1218. }
  1219. static struct usb_memory *
  1220. find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)
  1221. {
  1222. struct usb_memory *usbm = NULL, *iter;
  1223. unsigned long flags;
  1224. unsigned long uurb_start = (unsigned long)uurb->buffer;
  1225. spin_lock_irqsave(&ps->lock, flags);
  1226. list_for_each_entry(iter, &ps->memory_list, memlist) {
  1227. if (uurb_start >= iter->vm_start &&
  1228. uurb_start < iter->vm_start + iter->size) {
  1229. if (uurb->buffer_length > iter->vm_start + iter->size -
  1230. uurb_start) {
  1231. usbm = ERR_PTR(-EINVAL);
  1232. } else {
  1233. usbm = iter;
  1234. usbm->urb_use_count++;
  1235. }
  1236. break;
  1237. }
  1238. }
  1239. spin_unlock_irqrestore(&ps->lock, flags);
  1240. return usbm;
  1241. }
  1242. static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
  1243. struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
  1244. void __user *arg)
  1245. {
  1246. struct usbdevfs_iso_packet_desc *isopkt = NULL;
  1247. struct usb_host_endpoint *ep;
  1248. struct async *as = NULL;
  1249. struct usb_ctrlrequest *dr = NULL;
  1250. unsigned int u, totlen, isofrmlen;
  1251. int i, ret, num_sgs = 0, ifnum = -1;
  1252. int number_of_packets = 0;
  1253. unsigned int stream_id = 0;
  1254. void *buf;
  1255. bool is_in;
  1256. bool allow_short = false;
  1257. bool allow_zero = false;
  1258. unsigned long mask = USBDEVFS_URB_SHORT_NOT_OK |
  1259. USBDEVFS_URB_BULK_CONTINUATION |
  1260. USBDEVFS_URB_NO_FSBR |
  1261. USBDEVFS_URB_ZERO_PACKET |
  1262. USBDEVFS_URB_NO_INTERRUPT;
  1263. /* USBDEVFS_URB_ISO_ASAP is a special case */
  1264. if (uurb->type == USBDEVFS_URB_TYPE_ISO)
  1265. mask |= USBDEVFS_URB_ISO_ASAP;
  1266. if (uurb->flags & ~mask)
  1267. return -EINVAL;
  1268. if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX)
  1269. return -EINVAL;
  1270. if (uurb->buffer_length > 0 && !uurb->buffer)
  1271. return -EINVAL;
  1272. if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
  1273. (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
  1274. ifnum = findintfep(ps->dev, uurb->endpoint);
  1275. if (ifnum < 0)
  1276. return ifnum;
  1277. ret = checkintf(ps, ifnum);
  1278. if (ret)
  1279. return ret;
  1280. }
  1281. ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
  1282. if (!ep)
  1283. return -ENOENT;
  1284. is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
  1285. u = 0;
  1286. switch (uurb->type) {
  1287. case USBDEVFS_URB_TYPE_CONTROL:
  1288. if (!usb_endpoint_xfer_control(&ep->desc))
  1289. return -EINVAL;
  1290. /* min 8 byte setup packet */
  1291. if (uurb->buffer_length < 8)
  1292. return -EINVAL;
  1293. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  1294. if (!dr)
  1295. return -ENOMEM;
  1296. if (copy_from_user(dr, uurb->buffer, 8)) {
  1297. ret = -EFAULT;
  1298. goto error;
  1299. }
  1300. if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
  1301. ret = -EINVAL;
  1302. goto error;
  1303. }
  1304. ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
  1305. le16_to_cpup(&dr->wIndex));
  1306. if (ret)
  1307. goto error;
  1308. uurb->buffer_length = le16_to_cpup(&dr->wLength);
  1309. uurb->buffer += 8;
  1310. if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
  1311. is_in = 1;
  1312. uurb->endpoint |= USB_DIR_IN;
  1313. } else {
  1314. is_in = 0;
  1315. uurb->endpoint &= ~USB_DIR_IN;
  1316. }
  1317. if (is_in)
  1318. allow_short = true;
  1319. snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
  1320. "bRequest=%02x wValue=%04x "
  1321. "wIndex=%04x wLength=%04x\n",
  1322. dr->bRequestType, dr->bRequest,
  1323. __le16_to_cpup(&dr->wValue),
  1324. __le16_to_cpup(&dr->wIndex),
  1325. __le16_to_cpup(&dr->wLength));
  1326. u = sizeof(struct usb_ctrlrequest);
  1327. break;
  1328. case USBDEVFS_URB_TYPE_BULK:
  1329. if (!is_in)
  1330. allow_zero = true;
  1331. else
  1332. allow_short = true;
  1333. switch (usb_endpoint_type(&ep->desc)) {
  1334. case USB_ENDPOINT_XFER_CONTROL:
  1335. case USB_ENDPOINT_XFER_ISOC:
  1336. return -EINVAL;
  1337. case USB_ENDPOINT_XFER_INT:
  1338. /* allow single-shot interrupt transfers */
  1339. uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
  1340. goto interrupt_urb;
  1341. }
  1342. num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
  1343. if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
  1344. num_sgs = 0;
  1345. if (ep->streams)
  1346. stream_id = uurb->stream_id;
  1347. break;
  1348. case USBDEVFS_URB_TYPE_INTERRUPT:
  1349. if (!usb_endpoint_xfer_int(&ep->desc))
  1350. return -EINVAL;
  1351. interrupt_urb:
  1352. if (!is_in)
  1353. allow_zero = true;
  1354. else
  1355. allow_short = true;
  1356. break;
  1357. case USBDEVFS_URB_TYPE_ISO:
  1358. /* arbitrary limit */
  1359. if (uurb->number_of_packets < 1 ||
  1360. uurb->number_of_packets > 128)
  1361. return -EINVAL;
  1362. if (!usb_endpoint_xfer_isoc(&ep->desc))
  1363. return -EINVAL;
  1364. number_of_packets = uurb->number_of_packets;
  1365. isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
  1366. number_of_packets;
  1367. isopkt = memdup_user(iso_frame_desc, isofrmlen);
  1368. if (IS_ERR(isopkt)) {
  1369. ret = PTR_ERR(isopkt);
  1370. isopkt = NULL;
  1371. goto error;
  1372. }
  1373. for (totlen = u = 0; u < number_of_packets; u++) {
  1374. /*
  1375. * arbitrary limit need for USB 3.0
  1376. * bMaxBurst (0~15 allowed, 1~16 packets)
  1377. * bmAttributes (bit 1:0, mult 0~2, 1~3 packets)
  1378. * sizemax: 1024 * 16 * 3 = 49152
  1379. */
  1380. if (isopkt[u].length > 49152) {
  1381. ret = -EINVAL;
  1382. goto error;
  1383. }
  1384. totlen += isopkt[u].length;
  1385. }
  1386. u *= sizeof(struct usb_iso_packet_descriptor);
  1387. uurb->buffer_length = totlen;
  1388. break;
  1389. default:
  1390. return -EINVAL;
  1391. }
  1392. if (uurb->buffer_length > 0 &&
  1393. !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  1394. uurb->buffer, uurb->buffer_length)) {
  1395. ret = -EFAULT;
  1396. goto error;
  1397. }
  1398. as = alloc_async(number_of_packets);
  1399. if (!as) {
  1400. ret = -ENOMEM;
  1401. goto error;
  1402. }
  1403. as->usbm = find_memory_area(ps, uurb);
  1404. if (IS_ERR(as->usbm)) {
  1405. ret = PTR_ERR(as->usbm);
  1406. as->usbm = NULL;
  1407. goto error;
  1408. }
  1409. /* do not use SG buffers when memory mapped segments
  1410. * are in use
  1411. */
  1412. if (as->usbm)
  1413. num_sgs = 0;
  1414. u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
  1415. num_sgs * sizeof(struct scatterlist);
  1416. ret = usbfs_increase_memory_usage(u);
  1417. if (ret)
  1418. goto error;
  1419. as->mem_usage = u;
  1420. if (num_sgs) {
  1421. as->urb->sg = kmalloc_array(num_sgs,
  1422. sizeof(struct scatterlist),
  1423. GFP_KERNEL);
  1424. if (!as->urb->sg) {
  1425. ret = -ENOMEM;
  1426. goto error;
  1427. }
  1428. as->urb->num_sgs = num_sgs;
  1429. sg_init_table(as->urb->sg, as->urb->num_sgs);
  1430. totlen = uurb->buffer_length;
  1431. for (i = 0; i < as->urb->num_sgs; i++) {
  1432. u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
  1433. buf = kmalloc(u, GFP_KERNEL);
  1434. if (!buf) {
  1435. ret = -ENOMEM;
  1436. goto error;
  1437. }
  1438. sg_set_buf(&as->urb->sg[i], buf, u);
  1439. if (!is_in) {
  1440. if (copy_from_user(buf, uurb->buffer, u)) {
  1441. ret = -EFAULT;
  1442. goto error;
  1443. }
  1444. uurb->buffer += u;
  1445. }
  1446. totlen -= u;
  1447. }
  1448. } else if (uurb->buffer_length > 0) {
  1449. if (as->usbm) {
  1450. unsigned long uurb_start = (unsigned long)uurb->buffer;
  1451. as->urb->transfer_buffer = as->usbm->mem +
  1452. (uurb_start - as->usbm->vm_start);
  1453. } else {
  1454. as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
  1455. GFP_KERNEL);
  1456. if (!as->urb->transfer_buffer) {
  1457. ret = -ENOMEM;
  1458. goto error;
  1459. }
  1460. if (!is_in) {
  1461. if (copy_from_user(as->urb->transfer_buffer,
  1462. uurb->buffer,
  1463. uurb->buffer_length)) {
  1464. ret = -EFAULT;
  1465. goto error;
  1466. }
  1467. } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
  1468. /*
  1469. * Isochronous input data may end up being
  1470. * discontiguous if some of the packets are
  1471. * short. Clear the buffer so that the gaps
  1472. * don't leak kernel data to userspace.
  1473. */
  1474. memset(as->urb->transfer_buffer, 0,
  1475. uurb->buffer_length);
  1476. }
  1477. }
  1478. }
  1479. as->urb->dev = ps->dev;
  1480. as->urb->pipe = (uurb->type << 30) |
  1481. __create_pipe(ps->dev, uurb->endpoint & 0xf) |
  1482. (uurb->endpoint & USB_DIR_IN);
  1483. /* This tedious sequence is necessary because the URB_* flags
  1484. * are internal to the kernel and subject to change, whereas
  1485. * the USBDEVFS_URB_* flags are a user API and must not be changed.
  1486. */
  1487. u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
  1488. if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
  1489. u |= URB_ISO_ASAP;
  1490. if (allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
  1491. u |= URB_SHORT_NOT_OK;
  1492. if (allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
  1493. u |= URB_ZERO_PACKET;
  1494. if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
  1495. u |= URB_NO_INTERRUPT;
  1496. as->urb->transfer_flags = u;
  1497. if (!allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
  1498. dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_SHORT_NOT_OK.\n");
  1499. if (!allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
  1500. dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_ZERO_PACKET.\n");
  1501. as->urb->transfer_buffer_length = uurb->buffer_length;
  1502. as->urb->setup_packet = (unsigned char *)dr;
  1503. dr = NULL;
  1504. as->urb->start_frame = uurb->start_frame;
  1505. as->urb->number_of_packets = number_of_packets;
  1506. as->urb->stream_id = stream_id;
  1507. if (ep->desc.bInterval) {
  1508. if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
  1509. ps->dev->speed == USB_SPEED_HIGH ||
  1510. ps->dev->speed >= USB_SPEED_SUPER)
  1511. as->urb->interval = 1 <<
  1512. min(15, ep->desc.bInterval - 1);
  1513. else
  1514. as->urb->interval = ep->desc.bInterval;
  1515. }
  1516. as->urb->context = as;
  1517. as->urb->complete = async_completed;
  1518. for (totlen = u = 0; u < number_of_packets; u++) {
  1519. as->urb->iso_frame_desc[u].offset = totlen;
  1520. as->urb->iso_frame_desc[u].length = isopkt[u].length;
  1521. totlen += isopkt[u].length;
  1522. }
  1523. kfree(isopkt);
  1524. isopkt = NULL;
  1525. as->ps = ps;
  1526. as->userurb = arg;
  1527. if (as->usbm) {
  1528. unsigned long uurb_start = (unsigned long)uurb->buffer;
  1529. as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1530. as->urb->transfer_dma = as->usbm->dma_handle +
  1531. (uurb_start - as->usbm->vm_start);
  1532. } else if (is_in && uurb->buffer_length > 0)
  1533. as->userbuffer = uurb->buffer;
  1534. as->signr = uurb->signr;
  1535. as->ifnum = ifnum;
  1536. as->pid = get_pid(task_pid(current));
  1537. as->cred = get_current_cred();
  1538. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1539. as->urb->transfer_buffer_length, 0, SUBMIT,
  1540. NULL, 0);
  1541. if (!is_in)
  1542. snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
  1543. async_newpending(as);
  1544. if (usb_endpoint_xfer_bulk(&ep->desc)) {
  1545. spin_lock_irq(&ps->lock);
  1546. /* Not exactly the endpoint address; the direction bit is
  1547. * shifted to the 0x10 position so that the value will be
  1548. * between 0 and 31.
  1549. */
  1550. as->bulk_addr = usb_endpoint_num(&ep->desc) |
  1551. ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1552. >> 3);
  1553. /* If this bulk URB is the start of a new transfer, re-enable
  1554. * the endpoint. Otherwise mark it as a continuation URB.
  1555. */
  1556. if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
  1557. as->bulk_status = AS_CONTINUATION;
  1558. else
  1559. ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
  1560. /* Don't accept continuation URBs if the endpoint is
  1561. * disabled because of an earlier error.
  1562. */
  1563. if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
  1564. ret = -EREMOTEIO;
  1565. else
  1566. ret = usb_submit_urb(as->urb, GFP_ATOMIC);
  1567. spin_unlock_irq(&ps->lock);
  1568. } else {
  1569. ret = usb_submit_urb(as->urb, GFP_KERNEL);
  1570. }
  1571. if (ret) {
  1572. dev_printk(KERN_DEBUG, &ps->dev->dev,
  1573. "usbfs: usb_submit_urb returned %d\n", ret);
  1574. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1575. 0, ret, COMPLETE, NULL, 0);
  1576. async_removepending(as);
  1577. goto error;
  1578. }
  1579. return 0;
  1580. error:
  1581. kfree(isopkt);
  1582. kfree(dr);
  1583. if (as)
  1584. free_async(as);
  1585. return ret;
  1586. }
  1587. static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
  1588. {
  1589. struct usbdevfs_urb uurb;
  1590. if (copy_from_user(&uurb, arg, sizeof(uurb)))
  1591. return -EFAULT;
  1592. return proc_do_submiturb(ps, &uurb,
  1593. (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
  1594. arg);
  1595. }
  1596. static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
  1597. {
  1598. struct urb *urb;
  1599. struct async *as;
  1600. unsigned long flags;
  1601. spin_lock_irqsave(&ps->lock, flags);
  1602. as = async_getpending(ps, arg);
  1603. if (!as) {
  1604. spin_unlock_irqrestore(&ps->lock, flags);
  1605. return -EINVAL;
  1606. }
  1607. urb = as->urb;
  1608. usb_get_urb(urb);
  1609. spin_unlock_irqrestore(&ps->lock, flags);
  1610. usb_kill_urb(urb);
  1611. usb_put_urb(urb);
  1612. return 0;
  1613. }
  1614. static void compute_isochronous_actual_length(struct urb *urb)
  1615. {
  1616. unsigned int i;
  1617. if (urb->number_of_packets > 0) {
  1618. urb->actual_length = 0;
  1619. for (i = 0; i < urb->number_of_packets; i++)
  1620. urb->actual_length +=
  1621. urb->iso_frame_desc[i].actual_length;
  1622. }
  1623. }
  1624. static int processcompl(struct async *as, void __user * __user *arg)
  1625. {
  1626. struct urb *urb = as->urb;
  1627. struct usbdevfs_urb __user *userurb = as->userurb;
  1628. void __user *addr = as->userurb;
  1629. unsigned int i;
  1630. compute_isochronous_actual_length(urb);
  1631. if (as->userbuffer && urb->actual_length) {
  1632. if (copy_urb_data_to_user(as->userbuffer, urb))
  1633. goto err_out;
  1634. }
  1635. if (put_user(as->status, &userurb->status))
  1636. goto err_out;
  1637. if (put_user(urb->actual_length, &userurb->actual_length))
  1638. goto err_out;
  1639. if (put_user(urb->error_count, &userurb->error_count))
  1640. goto err_out;
  1641. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1642. for (i = 0; i < urb->number_of_packets; i++) {
  1643. if (put_user(urb->iso_frame_desc[i].actual_length,
  1644. &userurb->iso_frame_desc[i].actual_length))
  1645. goto err_out;
  1646. if (put_user(urb->iso_frame_desc[i].status,
  1647. &userurb->iso_frame_desc[i].status))
  1648. goto err_out;
  1649. }
  1650. }
  1651. if (put_user(addr, (void __user * __user *)arg))
  1652. return -EFAULT;
  1653. return 0;
  1654. err_out:
  1655. return -EFAULT;
  1656. }
  1657. static struct async *reap_as(struct usb_dev_state *ps)
  1658. {
  1659. DECLARE_WAITQUEUE(wait, current);
  1660. struct async *as = NULL;
  1661. struct usb_device *dev = ps->dev;
  1662. add_wait_queue(&ps->wait, &wait);
  1663. for (;;) {
  1664. __set_current_state(TASK_INTERRUPTIBLE);
  1665. as = async_getcompleted(ps);
  1666. if (as || !connected(ps))
  1667. break;
  1668. if (signal_pending(current))
  1669. break;
  1670. usb_unlock_device(dev);
  1671. schedule();
  1672. usb_lock_device(dev);
  1673. }
  1674. remove_wait_queue(&ps->wait, &wait);
  1675. set_current_state(TASK_RUNNING);
  1676. return as;
  1677. }
  1678. static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
  1679. {
  1680. struct async *as = reap_as(ps);
  1681. if (as) {
  1682. int retval;
  1683. snoop(&ps->dev->dev, "reap %px\n", as->userurb);
  1684. retval = processcompl(as, (void __user * __user *)arg);
  1685. free_async(as);
  1686. return retval;
  1687. }
  1688. if (signal_pending(current))
  1689. return -EINTR;
  1690. return -ENODEV;
  1691. }
  1692. static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
  1693. {
  1694. int retval;
  1695. struct async *as;
  1696. as = async_getcompleted(ps);
  1697. if (as) {
  1698. snoop(&ps->dev->dev, "reap %px\n", as->userurb);
  1699. retval = processcompl(as, (void __user * __user *)arg);
  1700. free_async(as);
  1701. } else {
  1702. retval = (connected(ps) ? -EAGAIN : -ENODEV);
  1703. }
  1704. return retval;
  1705. }
  1706. #ifdef CONFIG_COMPAT
  1707. static int proc_control_compat(struct usb_dev_state *ps,
  1708. struct usbdevfs_ctrltransfer32 __user *p32)
  1709. {
  1710. struct usbdevfs_ctrltransfer __user *p;
  1711. __u32 udata;
  1712. p = compat_alloc_user_space(sizeof(*p));
  1713. if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
  1714. get_user(udata, &p32->data) ||
  1715. put_user(compat_ptr(udata), &p->data))
  1716. return -EFAULT;
  1717. return proc_control(ps, p);
  1718. }
  1719. static int proc_bulk_compat(struct usb_dev_state *ps,
  1720. struct usbdevfs_bulktransfer32 __user *p32)
  1721. {
  1722. struct usbdevfs_bulktransfer __user *p;
  1723. compat_uint_t n;
  1724. compat_caddr_t addr;
  1725. p = compat_alloc_user_space(sizeof(*p));
  1726. if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
  1727. get_user(n, &p32->len) || put_user(n, &p->len) ||
  1728. get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
  1729. get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
  1730. return -EFAULT;
  1731. return proc_bulk(ps, p);
  1732. }
  1733. static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
  1734. {
  1735. struct usbdevfs_disconnectsignal32 ds;
  1736. if (copy_from_user(&ds, arg, sizeof(ds)))
  1737. return -EFAULT;
  1738. ps->discsignr = ds.signr;
  1739. ps->disccontext = compat_ptr(ds.context);
  1740. return 0;
  1741. }
  1742. static int get_urb32(struct usbdevfs_urb *kurb,
  1743. struct usbdevfs_urb32 __user *uurb)
  1744. {
  1745. struct usbdevfs_urb32 urb32;
  1746. if (copy_from_user(&urb32, uurb, sizeof(*uurb)))
  1747. return -EFAULT;
  1748. kurb->type = urb32.type;
  1749. kurb->endpoint = urb32.endpoint;
  1750. kurb->status = urb32.status;
  1751. kurb->flags = urb32.flags;
  1752. kurb->buffer = compat_ptr(urb32.buffer);
  1753. kurb->buffer_length = urb32.buffer_length;
  1754. kurb->actual_length = urb32.actual_length;
  1755. kurb->start_frame = urb32.start_frame;
  1756. kurb->number_of_packets = urb32.number_of_packets;
  1757. kurb->error_count = urb32.error_count;
  1758. kurb->signr = urb32.signr;
  1759. kurb->usercontext = compat_ptr(urb32.usercontext);
  1760. return 0;
  1761. }
  1762. static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
  1763. {
  1764. struct usbdevfs_urb uurb;
  1765. if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
  1766. return -EFAULT;
  1767. return proc_do_submiturb(ps, &uurb,
  1768. ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
  1769. arg);
  1770. }
  1771. static int processcompl_compat(struct async *as, void __user * __user *arg)
  1772. {
  1773. struct urb *urb = as->urb;
  1774. struct usbdevfs_urb32 __user *userurb = as->userurb;
  1775. void __user *addr = as->userurb;
  1776. unsigned int i;
  1777. compute_isochronous_actual_length(urb);
  1778. if (as->userbuffer && urb->actual_length) {
  1779. if (copy_urb_data_to_user(as->userbuffer, urb))
  1780. return -EFAULT;
  1781. }
  1782. if (put_user(as->status, &userurb->status))
  1783. return -EFAULT;
  1784. if (put_user(urb->actual_length, &userurb->actual_length))
  1785. return -EFAULT;
  1786. if (put_user(urb->error_count, &userurb->error_count))
  1787. return -EFAULT;
  1788. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1789. for (i = 0; i < urb->number_of_packets; i++) {
  1790. if (put_user(urb->iso_frame_desc[i].actual_length,
  1791. &userurb->iso_frame_desc[i].actual_length))
  1792. return -EFAULT;
  1793. if (put_user(urb->iso_frame_desc[i].status,
  1794. &userurb->iso_frame_desc[i].status))
  1795. return -EFAULT;
  1796. }
  1797. }
  1798. if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
  1799. return -EFAULT;
  1800. return 0;
  1801. }
  1802. static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
  1803. {
  1804. struct async *as = reap_as(ps);
  1805. if (as) {
  1806. int retval;
  1807. snoop(&ps->dev->dev, "reap %px\n", as->userurb);
  1808. retval = processcompl_compat(as, (void __user * __user *)arg);
  1809. free_async(as);
  1810. return retval;
  1811. }
  1812. if (signal_pending(current))
  1813. return -EINTR;
  1814. return -ENODEV;
  1815. }
  1816. static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
  1817. {
  1818. int retval;
  1819. struct async *as;
  1820. as = async_getcompleted(ps);
  1821. if (as) {
  1822. snoop(&ps->dev->dev, "reap %px\n", as->userurb);
  1823. retval = processcompl_compat(as, (void __user * __user *)arg);
  1824. free_async(as);
  1825. } else {
  1826. retval = (connected(ps) ? -EAGAIN : -ENODEV);
  1827. }
  1828. return retval;
  1829. }
  1830. #endif
  1831. static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
  1832. {
  1833. struct usbdevfs_disconnectsignal ds;
  1834. if (copy_from_user(&ds, arg, sizeof(ds)))
  1835. return -EFAULT;
  1836. ps->discsignr = ds.signr;
  1837. ps->disccontext = ds.context;
  1838. return 0;
  1839. }
  1840. static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
  1841. {
  1842. unsigned int ifnum;
  1843. if (get_user(ifnum, (unsigned int __user *)arg))
  1844. return -EFAULT;
  1845. return claimintf(ps, ifnum);
  1846. }
  1847. static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
  1848. {
  1849. unsigned int ifnum;
  1850. int ret;
  1851. if (get_user(ifnum, (unsigned int __user *)arg))
  1852. return -EFAULT;
  1853. ret = releaseintf(ps, ifnum);
  1854. if (ret < 0)
  1855. return ret;
  1856. destroy_async_on_interface(ps, ifnum);
  1857. return 0;
  1858. }
  1859. static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
  1860. {
  1861. int size;
  1862. void *buf = NULL;
  1863. int retval = 0;
  1864. struct usb_interface *intf = NULL;
  1865. struct usb_driver *driver = NULL;
  1866. if (ps->privileges_dropped)
  1867. return -EACCES;
  1868. /* alloc buffer */
  1869. size = _IOC_SIZE(ctl->ioctl_code);
  1870. if (size > 0) {
  1871. buf = kmalloc(size, GFP_KERNEL);
  1872. if (buf == NULL)
  1873. return -ENOMEM;
  1874. if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
  1875. if (copy_from_user(buf, ctl->data, size)) {
  1876. kfree(buf);
  1877. return -EFAULT;
  1878. }
  1879. } else {
  1880. memset(buf, 0, size);
  1881. }
  1882. }
  1883. if (!connected(ps)) {
  1884. kfree(buf);
  1885. return -ENODEV;
  1886. }
  1887. if (ps->dev->state != USB_STATE_CONFIGURED)
  1888. retval = -EHOSTUNREACH;
  1889. else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
  1890. retval = -EINVAL;
  1891. else switch (ctl->ioctl_code) {
  1892. /* disconnect kernel driver from interface */
  1893. case USBDEVFS_DISCONNECT:
  1894. if (intf->dev.driver) {
  1895. driver = to_usb_driver(intf->dev.driver);
  1896. dev_dbg(&intf->dev, "disconnect by usbfs\n");
  1897. usb_driver_release_interface(driver, intf);
  1898. } else
  1899. retval = -ENODATA;
  1900. break;
  1901. /* let kernel drivers try to (re)bind to the interface */
  1902. case USBDEVFS_CONNECT:
  1903. if (!intf->dev.driver)
  1904. retval = device_attach(&intf->dev);
  1905. else
  1906. retval = -EBUSY;
  1907. break;
  1908. /* talk directly to the interface's driver */
  1909. default:
  1910. if (intf->dev.driver)
  1911. driver = to_usb_driver(intf->dev.driver);
  1912. if (driver == NULL || driver->unlocked_ioctl == NULL) {
  1913. retval = -ENOTTY;
  1914. } else {
  1915. retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
  1916. if (retval == -ENOIOCTLCMD)
  1917. retval = -ENOTTY;
  1918. }
  1919. }
  1920. /* cleanup and return */
  1921. if (retval >= 0
  1922. && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
  1923. && size > 0
  1924. && copy_to_user(ctl->data, buf, size) != 0)
  1925. retval = -EFAULT;
  1926. kfree(buf);
  1927. return retval;
  1928. }
  1929. static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
  1930. {
  1931. struct usbdevfs_ioctl ctrl;
  1932. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1933. return -EFAULT;
  1934. return proc_ioctl(ps, &ctrl);
  1935. }
  1936. #ifdef CONFIG_COMPAT
  1937. static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
  1938. {
  1939. struct usbdevfs_ioctl32 ioc32;
  1940. struct usbdevfs_ioctl ctrl;
  1941. if (copy_from_user(&ioc32, compat_ptr(arg), sizeof(ioc32)))
  1942. return -EFAULT;
  1943. ctrl.ifno = ioc32.ifno;
  1944. ctrl.ioctl_code = ioc32.ioctl_code;
  1945. ctrl.data = compat_ptr(ioc32.data);
  1946. return proc_ioctl(ps, &ctrl);
  1947. }
  1948. #endif
  1949. static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
  1950. {
  1951. unsigned portnum;
  1952. int rc;
  1953. if (get_user(portnum, (unsigned __user *) arg))
  1954. return -EFAULT;
  1955. rc = usb_hub_claim_port(ps->dev, portnum, ps);
  1956. if (rc == 0)
  1957. snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
  1958. portnum, task_pid_nr(current), current->comm);
  1959. return rc;
  1960. }
  1961. static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
  1962. {
  1963. unsigned portnum;
  1964. if (get_user(portnum, (unsigned __user *) arg))
  1965. return -EFAULT;
  1966. return usb_hub_release_port(ps->dev, portnum, ps);
  1967. }
  1968. static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
  1969. {
  1970. __u32 caps;
  1971. caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
  1972. USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP |
  1973. USBDEVFS_CAP_DROP_PRIVILEGES;
  1974. if (!ps->dev->bus->no_stop_on_short)
  1975. caps |= USBDEVFS_CAP_BULK_CONTINUATION;
  1976. if (ps->dev->bus->sg_tablesize)
  1977. caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
  1978. if (put_user(caps, (__u32 __user *)arg))
  1979. return -EFAULT;
  1980. return 0;
  1981. }
  1982. static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
  1983. {
  1984. struct usbdevfs_disconnect_claim dc;
  1985. struct usb_interface *intf;
  1986. if (copy_from_user(&dc, arg, sizeof(dc)))
  1987. return -EFAULT;
  1988. intf = usb_ifnum_to_if(ps->dev, dc.interface);
  1989. if (!intf)
  1990. return -EINVAL;
  1991. if (intf->dev.driver) {
  1992. struct usb_driver *driver = to_usb_driver(intf->dev.driver);
  1993. if (ps->privileges_dropped)
  1994. return -EACCES;
  1995. if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
  1996. strncmp(dc.driver, intf->dev.driver->name,
  1997. sizeof(dc.driver)) != 0)
  1998. return -EBUSY;
  1999. if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
  2000. strncmp(dc.driver, intf->dev.driver->name,
  2001. sizeof(dc.driver)) == 0)
  2002. return -EBUSY;
  2003. dev_dbg(&intf->dev, "disconnect by usbfs\n");
  2004. usb_driver_release_interface(driver, intf);
  2005. }
  2006. return claimintf(ps, dc.interface);
  2007. }
  2008. static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
  2009. {
  2010. unsigned num_streams, num_eps;
  2011. struct usb_host_endpoint **eps;
  2012. struct usb_interface *intf;
  2013. int r;
  2014. r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
  2015. &eps, &intf);
  2016. if (r)
  2017. return r;
  2018. destroy_async_on_interface(ps,
  2019. intf->altsetting[0].desc.bInterfaceNumber);
  2020. r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
  2021. kfree(eps);
  2022. return r;
  2023. }
  2024. static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
  2025. {
  2026. unsigned num_eps;
  2027. struct usb_host_endpoint **eps;
  2028. struct usb_interface *intf;
  2029. int r;
  2030. r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
  2031. if (r)
  2032. return r;
  2033. destroy_async_on_interface(ps,
  2034. intf->altsetting[0].desc.bInterfaceNumber);
  2035. r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
  2036. kfree(eps);
  2037. return r;
  2038. }
  2039. static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
  2040. {
  2041. u32 data;
  2042. if (copy_from_user(&data, arg, sizeof(data)))
  2043. return -EFAULT;
  2044. /* This is a one way operation. Once privileges are
  2045. * dropped, you cannot regain them. You may however reissue
  2046. * this ioctl to shrink the allowed interfaces mask.
  2047. */
  2048. ps->interface_allowed_mask &= data;
  2049. ps->privileges_dropped = true;
  2050. return 0;
  2051. }
  2052. /*
  2053. * NOTE: All requests here that have interface numbers as parameters
  2054. * are assuming that somehow the configuration has been prevented from
  2055. * changing. But there's no mechanism to ensure that...
  2056. */
  2057. static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
  2058. void __user *p)
  2059. {
  2060. struct usb_dev_state *ps = file->private_data;
  2061. struct inode *inode = file_inode(file);
  2062. struct usb_device *dev = ps->dev;
  2063. int ret = -ENOTTY;
  2064. if (!(file->f_mode & FMODE_WRITE))
  2065. return -EPERM;
  2066. usb_lock_device(dev);
  2067. /* Reap operations are allowed even after disconnection */
  2068. switch (cmd) {
  2069. case USBDEVFS_REAPURB:
  2070. snoop(&dev->dev, "%s: REAPURB\n", __func__);
  2071. ret = proc_reapurb(ps, p);
  2072. goto done;
  2073. case USBDEVFS_REAPURBNDELAY:
  2074. snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
  2075. ret = proc_reapurbnonblock(ps, p);
  2076. goto done;
  2077. #ifdef CONFIG_COMPAT
  2078. case USBDEVFS_REAPURB32:
  2079. snoop(&dev->dev, "%s: REAPURB32\n", __func__);
  2080. ret = proc_reapurb_compat(ps, p);
  2081. goto done;
  2082. case USBDEVFS_REAPURBNDELAY32:
  2083. snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
  2084. ret = proc_reapurbnonblock_compat(ps, p);
  2085. goto done;
  2086. #endif
  2087. }
  2088. if (!connected(ps)) {
  2089. usb_unlock_device(dev);
  2090. return -ENODEV;
  2091. }
  2092. switch (cmd) {
  2093. case USBDEVFS_CONTROL:
  2094. snoop(&dev->dev, "%s: CONTROL\n", __func__);
  2095. ret = proc_control(ps, p);
  2096. if (ret >= 0)
  2097. inode->i_mtime = current_time(inode);
  2098. break;
  2099. case USBDEVFS_BULK:
  2100. snoop(&dev->dev, "%s: BULK\n", __func__);
  2101. ret = proc_bulk(ps, p);
  2102. if (ret >= 0)
  2103. inode->i_mtime = current_time(inode);
  2104. break;
  2105. case USBDEVFS_RESETEP:
  2106. snoop(&dev->dev, "%s: RESETEP\n", __func__);
  2107. ret = proc_resetep(ps, p);
  2108. if (ret >= 0)
  2109. inode->i_mtime = current_time(inode);
  2110. break;
  2111. case USBDEVFS_RESET:
  2112. snoop(&dev->dev, "%s: RESET\n", __func__);
  2113. ret = proc_resetdevice(ps);
  2114. break;
  2115. case USBDEVFS_CLEAR_HALT:
  2116. snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
  2117. ret = proc_clearhalt(ps, p);
  2118. if (ret >= 0)
  2119. inode->i_mtime = current_time(inode);
  2120. break;
  2121. case USBDEVFS_GETDRIVER:
  2122. snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
  2123. ret = proc_getdriver(ps, p);
  2124. break;
  2125. case USBDEVFS_CONNECTINFO:
  2126. snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
  2127. ret = proc_connectinfo(ps, p);
  2128. break;
  2129. case USBDEVFS_SETINTERFACE:
  2130. snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
  2131. ret = proc_setintf(ps, p);
  2132. break;
  2133. case USBDEVFS_SETCONFIGURATION:
  2134. snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
  2135. ret = proc_setconfig(ps, p);
  2136. break;
  2137. case USBDEVFS_SUBMITURB:
  2138. snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
  2139. ret = proc_submiturb(ps, p);
  2140. if (ret >= 0)
  2141. inode->i_mtime = current_time(inode);
  2142. break;
  2143. #ifdef CONFIG_COMPAT
  2144. case USBDEVFS_CONTROL32:
  2145. snoop(&dev->dev, "%s: CONTROL32\n", __func__);
  2146. ret = proc_control_compat(ps, p);
  2147. if (ret >= 0)
  2148. inode->i_mtime = current_time(inode);
  2149. break;
  2150. case USBDEVFS_BULK32:
  2151. snoop(&dev->dev, "%s: BULK32\n", __func__);
  2152. ret = proc_bulk_compat(ps, p);
  2153. if (ret >= 0)
  2154. inode->i_mtime = current_time(inode);
  2155. break;
  2156. case USBDEVFS_DISCSIGNAL32:
  2157. snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
  2158. ret = proc_disconnectsignal_compat(ps, p);
  2159. break;
  2160. case USBDEVFS_SUBMITURB32:
  2161. snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
  2162. ret = proc_submiturb_compat(ps, p);
  2163. if (ret >= 0)
  2164. inode->i_mtime = current_time(inode);
  2165. break;
  2166. case USBDEVFS_IOCTL32:
  2167. snoop(&dev->dev, "%s: IOCTL32\n", __func__);
  2168. ret = proc_ioctl_compat(ps, ptr_to_compat(p));
  2169. break;
  2170. #endif
  2171. case USBDEVFS_DISCARDURB:
  2172. snoop(&dev->dev, "%s: DISCARDURB %px\n", __func__, p);
  2173. ret = proc_unlinkurb(ps, p);
  2174. break;
  2175. case USBDEVFS_DISCSIGNAL:
  2176. snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
  2177. ret = proc_disconnectsignal(ps, p);
  2178. break;
  2179. case USBDEVFS_CLAIMINTERFACE:
  2180. snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
  2181. ret = proc_claiminterface(ps, p);
  2182. break;
  2183. case USBDEVFS_RELEASEINTERFACE:
  2184. snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
  2185. ret = proc_releaseinterface(ps, p);
  2186. break;
  2187. case USBDEVFS_IOCTL:
  2188. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  2189. ret = proc_ioctl_default(ps, p);
  2190. break;
  2191. case USBDEVFS_CLAIM_PORT:
  2192. snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
  2193. ret = proc_claim_port(ps, p);
  2194. break;
  2195. case USBDEVFS_RELEASE_PORT:
  2196. snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
  2197. ret = proc_release_port(ps, p);
  2198. break;
  2199. case USBDEVFS_GET_CAPABILITIES:
  2200. ret = proc_get_capabilities(ps, p);
  2201. break;
  2202. case USBDEVFS_DISCONNECT_CLAIM:
  2203. ret = proc_disconnect_claim(ps, p);
  2204. break;
  2205. case USBDEVFS_ALLOC_STREAMS:
  2206. ret = proc_alloc_streams(ps, p);
  2207. break;
  2208. case USBDEVFS_FREE_STREAMS:
  2209. ret = proc_free_streams(ps, p);
  2210. break;
  2211. case USBDEVFS_DROP_PRIVILEGES:
  2212. ret = proc_drop_privileges(ps, p);
  2213. break;
  2214. case USBDEVFS_GET_SPEED:
  2215. ret = ps->dev->speed;
  2216. break;
  2217. }
  2218. done:
  2219. usb_unlock_device(dev);
  2220. if (ret >= 0)
  2221. inode->i_atime = current_time(inode);
  2222. return ret;
  2223. }
  2224. static long usbdev_ioctl(struct file *file, unsigned int cmd,
  2225. unsigned long arg)
  2226. {
  2227. int ret;
  2228. ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
  2229. return ret;
  2230. }
  2231. #ifdef CONFIG_COMPAT
  2232. static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
  2233. unsigned long arg)
  2234. {
  2235. int ret;
  2236. ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
  2237. return ret;
  2238. }
  2239. #endif
  2240. /* No kernel lock - fine */
  2241. static __poll_t usbdev_poll(struct file *file,
  2242. struct poll_table_struct *wait)
  2243. {
  2244. struct usb_dev_state *ps = file->private_data;
  2245. __poll_t mask = 0;
  2246. poll_wait(file, &ps->wait, wait);
  2247. if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
  2248. mask |= EPOLLOUT | EPOLLWRNORM;
  2249. if (!connected(ps))
  2250. mask |= EPOLLHUP;
  2251. if (list_empty(&ps->list))
  2252. mask |= EPOLLERR;
  2253. return mask;
  2254. }
  2255. const struct file_operations usbdev_file_operations = {
  2256. .owner = THIS_MODULE,
  2257. .llseek = no_seek_end_llseek,
  2258. .read = usbdev_read,
  2259. .poll = usbdev_poll,
  2260. .unlocked_ioctl = usbdev_ioctl,
  2261. #ifdef CONFIG_COMPAT
  2262. .compat_ioctl = usbdev_compat_ioctl,
  2263. #endif
  2264. .mmap = usbdev_mmap,
  2265. .open = usbdev_open,
  2266. .release = usbdev_release,
  2267. };
  2268. static void usbdev_remove(struct usb_device *udev)
  2269. {
  2270. struct usb_dev_state *ps;
  2271. struct siginfo sinfo;
  2272. while (!list_empty(&udev->filelist)) {
  2273. ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
  2274. destroy_all_async(ps);
  2275. wake_up_all(&ps->wait);
  2276. list_del_init(&ps->list);
  2277. if (ps->discsignr) {
  2278. clear_siginfo(&sinfo);
  2279. sinfo.si_signo = ps->discsignr;
  2280. sinfo.si_errno = EPIPE;
  2281. sinfo.si_code = SI_ASYNCIO;
  2282. sinfo.si_addr = ps->disccontext;
  2283. kill_pid_info_as_cred(ps->discsignr, &sinfo,
  2284. ps->disc_pid, ps->cred);
  2285. }
  2286. }
  2287. }
  2288. static int usbdev_notify(struct notifier_block *self,
  2289. unsigned long action, void *dev)
  2290. {
  2291. switch (action) {
  2292. case USB_DEVICE_ADD:
  2293. break;
  2294. case USB_DEVICE_REMOVE:
  2295. usbdev_remove(dev);
  2296. break;
  2297. }
  2298. return NOTIFY_OK;
  2299. }
  2300. static struct notifier_block usbdev_nb = {
  2301. .notifier_call = usbdev_notify,
  2302. };
  2303. static struct cdev usb_device_cdev;
  2304. int __init usb_devio_init(void)
  2305. {
  2306. int retval;
  2307. retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
  2308. "usb_device");
  2309. if (retval) {
  2310. printk(KERN_ERR "Unable to register minors for usb_device\n");
  2311. goto out;
  2312. }
  2313. cdev_init(&usb_device_cdev, &usbdev_file_operations);
  2314. retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
  2315. if (retval) {
  2316. printk(KERN_ERR "Unable to get usb_device major %d\n",
  2317. USB_DEVICE_MAJOR);
  2318. goto error_cdev;
  2319. }
  2320. usb_register_notify(&usbdev_nb);
  2321. out:
  2322. return retval;
  2323. error_cdev:
  2324. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  2325. goto out;
  2326. }
  2327. void usb_devio_cleanup(void)
  2328. {
  2329. usb_unregister_notify(&usbdev_nb);
  2330. cdev_del(&usb_device_cdev);
  2331. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  2332. }