devio.c 56 KB

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