devio.c 60 KB

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