devio.c 65 KB

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