devio.c 65 KB

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