devio.c 66 KB

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