devio.c 71 KB

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