devio.c 64 KB

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