devio.c 60 KB

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