devio.c 65 KB

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