devio.c 49 KB

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