devio.c 71 KB

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