devio.c 65 KB

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