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