devio.c 65 KB

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