devio.c 60 KB

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