devio.c 65 KB

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