devio.c 65 KB

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