devio.c 61 KB

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