devio.c 53 KB

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