devio.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  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. ret = -ENOMEM;
  567. ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
  568. if (!ps)
  569. goto out_free_ps;
  570. ret = -ENODEV;
  571. /* Protect against simultaneous removal or release */
  572. mutex_lock(&usbfs_mutex);
  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. mutex_unlock(&usbfs_mutex);
  588. if (!dev)
  589. goto out_free_ps;
  590. usb_lock_device(dev);
  591. if (dev->state == USB_STATE_NOTATTACHED)
  592. goto out_unlock_device;
  593. ret = usb_autoresume_device(dev);
  594. if (ret)
  595. goto out_unlock_device;
  596. ps->dev = dev;
  597. ps->file = file;
  598. spin_lock_init(&ps->lock);
  599. INIT_LIST_HEAD(&ps->list);
  600. INIT_LIST_HEAD(&ps->async_pending);
  601. INIT_LIST_HEAD(&ps->async_completed);
  602. init_waitqueue_head(&ps->wait);
  603. ps->discsignr = 0;
  604. ps->disc_pid = get_pid(task_pid(current));
  605. ps->disc_uid = cred->uid;
  606. ps->disc_euid = cred->euid;
  607. ps->disccontext = NULL;
  608. ps->ifclaimed = 0;
  609. security_task_getsecid(current, &ps->secid);
  610. smp_wmb();
  611. list_add_tail(&ps->list, &dev->filelist);
  612. file->private_data = ps;
  613. usb_unlock_device(dev);
  614. snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
  615. current->comm);
  616. unlock_kernel();
  617. return ret;
  618. out_unlock_device:
  619. usb_unlock_device(dev);
  620. usb_put_dev(dev);
  621. out_free_ps:
  622. kfree(ps);
  623. unlock_kernel();
  624. return ret;
  625. }
  626. static int usbdev_release(struct inode *inode, struct file *file)
  627. {
  628. struct dev_state *ps = file->private_data;
  629. struct usb_device *dev = ps->dev;
  630. unsigned int ifnum;
  631. struct async *as;
  632. usb_lock_device(dev);
  633. usb_hub_release_all_ports(dev, ps);
  634. list_del_init(&ps->list);
  635. for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
  636. ifnum++) {
  637. if (test_bit(ifnum, &ps->ifclaimed))
  638. releaseintf(ps, ifnum);
  639. }
  640. destroy_all_async(ps);
  641. usb_autosuspend_device(dev);
  642. usb_unlock_device(dev);
  643. usb_put_dev(dev);
  644. put_pid(ps->disc_pid);
  645. as = async_getcompleted(ps);
  646. while (as) {
  647. free_async(as);
  648. as = async_getcompleted(ps);
  649. }
  650. kfree(ps);
  651. return 0;
  652. }
  653. static int proc_control(struct dev_state *ps, void __user *arg)
  654. {
  655. struct usb_device *dev = ps->dev;
  656. struct usbdevfs_ctrltransfer ctrl;
  657. unsigned int tmo;
  658. unsigned char *tbuf;
  659. unsigned wLength;
  660. int i, pipe, ret;
  661. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  662. return -EFAULT;
  663. ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
  664. if (ret)
  665. return ret;
  666. wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
  667. if (wLength > PAGE_SIZE)
  668. return -EINVAL;
  669. tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  670. if (!tbuf)
  671. return -ENOMEM;
  672. tmo = ctrl.timeout;
  673. if (ctrl.bRequestType & 0x80) {
  674. if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
  675. ctrl.wLength)) {
  676. free_page((unsigned long)tbuf);
  677. return -EINVAL;
  678. }
  679. pipe = usb_rcvctrlpipe(dev, 0);
  680. snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT);
  681. usb_unlock_device(dev);
  682. i = usb_control_msg(dev, pipe, ctrl.bRequest,
  683. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  684. tbuf, ctrl.wLength, tmo);
  685. usb_lock_device(dev);
  686. snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE);
  687. if ((i > 0) && ctrl.wLength) {
  688. if (copy_to_user(ctrl.data, tbuf, i)) {
  689. free_page((unsigned long)tbuf);
  690. return -EFAULT;
  691. }
  692. }
  693. } else {
  694. if (ctrl.wLength) {
  695. if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
  696. free_page((unsigned long)tbuf);
  697. return -EFAULT;
  698. }
  699. }
  700. pipe = usb_sndctrlpipe(dev, 0);
  701. snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT);
  702. usb_unlock_device(dev);
  703. i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
  704. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  705. tbuf, ctrl.wLength, tmo);
  706. usb_lock_device(dev);
  707. snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE);
  708. }
  709. free_page((unsigned long)tbuf);
  710. if (i < 0 && i != -EPIPE) {
  711. dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
  712. "failed cmd %s rqt %u rq %u len %u ret %d\n",
  713. current->comm, ctrl.bRequestType, ctrl.bRequest,
  714. ctrl.wLength, i);
  715. }
  716. return i;
  717. }
  718. static int proc_bulk(struct dev_state *ps, void __user *arg)
  719. {
  720. struct usb_device *dev = ps->dev;
  721. struct usbdevfs_bulktransfer bulk;
  722. unsigned int tmo, len1, pipe;
  723. int len2;
  724. unsigned char *tbuf;
  725. int i, ret;
  726. if (copy_from_user(&bulk, arg, sizeof(bulk)))
  727. return -EFAULT;
  728. ret = findintfep(ps->dev, bulk.ep);
  729. if (ret < 0)
  730. return ret;
  731. ret = checkintf(ps, ret);
  732. if (ret)
  733. return ret;
  734. if (bulk.ep & USB_DIR_IN)
  735. pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
  736. else
  737. pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
  738. if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
  739. return -EINVAL;
  740. len1 = bulk.len;
  741. if (len1 > MAX_USBFS_BUFFER_SIZE)
  742. return -EINVAL;
  743. if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
  744. return -ENOMEM;
  745. tmo = bulk.timeout;
  746. if (bulk.ep & 0x80) {
  747. if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
  748. kfree(tbuf);
  749. return -EINVAL;
  750. }
  751. snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT);
  752. usb_unlock_device(dev);
  753. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  754. usb_lock_device(dev);
  755. snoop_urb(dev, NULL, pipe, len2, i, COMPLETE);
  756. if (!i && len2) {
  757. if (copy_to_user(bulk.data, tbuf, len2)) {
  758. kfree(tbuf);
  759. return -EFAULT;
  760. }
  761. }
  762. } else {
  763. if (len1) {
  764. if (copy_from_user(tbuf, bulk.data, len1)) {
  765. kfree(tbuf);
  766. return -EFAULT;
  767. }
  768. }
  769. snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT);
  770. usb_unlock_device(dev);
  771. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  772. usb_lock_device(dev);
  773. snoop_urb(dev, NULL, pipe, len2, i, COMPLETE);
  774. }
  775. kfree(tbuf);
  776. if (i < 0)
  777. return i;
  778. return len2;
  779. }
  780. static int proc_resetep(struct dev_state *ps, void __user *arg)
  781. {
  782. unsigned int ep;
  783. int ret;
  784. if (get_user(ep, (unsigned int __user *)arg))
  785. return -EFAULT;
  786. ret = findintfep(ps->dev, ep);
  787. if (ret < 0)
  788. return ret;
  789. ret = checkintf(ps, ret);
  790. if (ret)
  791. return ret;
  792. usb_reset_endpoint(ps->dev, ep);
  793. return 0;
  794. }
  795. static int proc_clearhalt(struct dev_state *ps, void __user *arg)
  796. {
  797. unsigned int ep;
  798. int pipe;
  799. int ret;
  800. if (get_user(ep, (unsigned int __user *)arg))
  801. return -EFAULT;
  802. ret = findintfep(ps->dev, ep);
  803. if (ret < 0)
  804. return ret;
  805. ret = checkintf(ps, ret);
  806. if (ret)
  807. return ret;
  808. if (ep & USB_DIR_IN)
  809. pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
  810. else
  811. pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
  812. return usb_clear_halt(ps->dev, pipe);
  813. }
  814. static int proc_getdriver(struct dev_state *ps, void __user *arg)
  815. {
  816. struct usbdevfs_getdriver gd;
  817. struct usb_interface *intf;
  818. int ret;
  819. if (copy_from_user(&gd, arg, sizeof(gd)))
  820. return -EFAULT;
  821. intf = usb_ifnum_to_if(ps->dev, gd.interface);
  822. if (!intf || !intf->dev.driver)
  823. ret = -ENODATA;
  824. else {
  825. strncpy(gd.driver, intf->dev.driver->name,
  826. sizeof(gd.driver));
  827. ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
  828. }
  829. return ret;
  830. }
  831. static int proc_connectinfo(struct dev_state *ps, void __user *arg)
  832. {
  833. struct usbdevfs_connectinfo ci;
  834. ci.devnum = ps->dev->devnum;
  835. ci.slow = ps->dev->speed == USB_SPEED_LOW;
  836. if (copy_to_user(arg, &ci, sizeof(ci)))
  837. return -EFAULT;
  838. return 0;
  839. }
  840. static int proc_resetdevice(struct dev_state *ps)
  841. {
  842. return usb_reset_device(ps->dev);
  843. }
  844. static int proc_setintf(struct dev_state *ps, void __user *arg)
  845. {
  846. struct usbdevfs_setinterface setintf;
  847. int ret;
  848. if (copy_from_user(&setintf, arg, sizeof(setintf)))
  849. return -EFAULT;
  850. if ((ret = checkintf(ps, setintf.interface)))
  851. return ret;
  852. return usb_set_interface(ps->dev, setintf.interface,
  853. setintf.altsetting);
  854. }
  855. static int proc_setconfig(struct dev_state *ps, void __user *arg)
  856. {
  857. int u;
  858. int status = 0;
  859. struct usb_host_config *actconfig;
  860. if (get_user(u, (int __user *)arg))
  861. return -EFAULT;
  862. actconfig = ps->dev->actconfig;
  863. /* Don't touch the device if any interfaces are claimed.
  864. * It could interfere with other drivers' operations, and if
  865. * an interface is claimed by usbfs it could easily deadlock.
  866. */
  867. if (actconfig) {
  868. int i;
  869. for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
  870. if (usb_interface_claimed(actconfig->interface[i])) {
  871. dev_warn(&ps->dev->dev,
  872. "usbfs: interface %d claimed by %s "
  873. "while '%s' sets config #%d\n",
  874. actconfig->interface[i]
  875. ->cur_altsetting
  876. ->desc.bInterfaceNumber,
  877. actconfig->interface[i]
  878. ->dev.driver->name,
  879. current->comm, u);
  880. status = -EBUSY;
  881. break;
  882. }
  883. }
  884. }
  885. /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
  886. * so avoid usb_set_configuration()'s kick to sysfs
  887. */
  888. if (status == 0) {
  889. if (actconfig && actconfig->desc.bConfigurationValue == u)
  890. status = usb_reset_configuration(ps->dev);
  891. else
  892. status = usb_set_configuration(ps->dev, u);
  893. }
  894. return status;
  895. }
  896. static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
  897. struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
  898. void __user *arg)
  899. {
  900. struct usbdevfs_iso_packet_desc *isopkt = NULL;
  901. struct usb_host_endpoint *ep;
  902. struct async *as;
  903. struct usb_ctrlrequest *dr = NULL;
  904. const struct cred *cred = current_cred();
  905. unsigned int u, totlen, isofrmlen;
  906. int ret, ifnum = -1;
  907. int is_in;
  908. if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
  909. USBDEVFS_URB_SHORT_NOT_OK |
  910. USBDEVFS_URB_BULK_CONTINUATION |
  911. USBDEVFS_URB_NO_FSBR |
  912. USBDEVFS_URB_ZERO_PACKET |
  913. USBDEVFS_URB_NO_INTERRUPT))
  914. return -EINVAL;
  915. if (uurb->buffer_length > 0 && !uurb->buffer)
  916. return -EINVAL;
  917. if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
  918. (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
  919. ifnum = findintfep(ps->dev, uurb->endpoint);
  920. if (ifnum < 0)
  921. return ifnum;
  922. ret = checkintf(ps, ifnum);
  923. if (ret)
  924. return ret;
  925. }
  926. if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
  927. is_in = 1;
  928. ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  929. } else {
  930. is_in = 0;
  931. ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  932. }
  933. if (!ep)
  934. return -ENOENT;
  935. switch(uurb->type) {
  936. case USBDEVFS_URB_TYPE_CONTROL:
  937. if (!usb_endpoint_xfer_control(&ep->desc))
  938. return -EINVAL;
  939. /* min 8 byte setup packet,
  940. * max 8 byte setup plus an arbitrary data stage */
  941. if (uurb->buffer_length < 8 ||
  942. uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
  943. return -EINVAL;
  944. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  945. if (!dr)
  946. return -ENOMEM;
  947. if (copy_from_user(dr, uurb->buffer, 8)) {
  948. kfree(dr);
  949. return -EFAULT;
  950. }
  951. if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
  952. kfree(dr);
  953. return -EINVAL;
  954. }
  955. ret = check_ctrlrecip(ps, dr->bRequestType,
  956. le16_to_cpup(&dr->wIndex));
  957. if (ret) {
  958. kfree(dr);
  959. return ret;
  960. }
  961. uurb->number_of_packets = 0;
  962. uurb->buffer_length = le16_to_cpup(&dr->wLength);
  963. uurb->buffer += 8;
  964. if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
  965. is_in = 1;
  966. uurb->endpoint |= USB_DIR_IN;
  967. } else {
  968. is_in = 0;
  969. uurb->endpoint &= ~USB_DIR_IN;
  970. }
  971. break;
  972. case USBDEVFS_URB_TYPE_BULK:
  973. switch (usb_endpoint_type(&ep->desc)) {
  974. case USB_ENDPOINT_XFER_CONTROL:
  975. case USB_ENDPOINT_XFER_ISOC:
  976. return -EINVAL;
  977. case USB_ENDPOINT_XFER_INT:
  978. /* allow single-shot interrupt transfers */
  979. uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
  980. goto interrupt_urb;
  981. }
  982. uurb->number_of_packets = 0;
  983. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  984. return -EINVAL;
  985. break;
  986. case USBDEVFS_URB_TYPE_INTERRUPT:
  987. if (!usb_endpoint_xfer_int(&ep->desc))
  988. return -EINVAL;
  989. interrupt_urb:
  990. uurb->number_of_packets = 0;
  991. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  992. return -EINVAL;
  993. break;
  994. case USBDEVFS_URB_TYPE_ISO:
  995. /* arbitrary limit */
  996. if (uurb->number_of_packets < 1 ||
  997. uurb->number_of_packets > 128)
  998. return -EINVAL;
  999. if (!usb_endpoint_xfer_isoc(&ep->desc))
  1000. return -EINVAL;
  1001. isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
  1002. uurb->number_of_packets;
  1003. if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
  1004. return -ENOMEM;
  1005. if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
  1006. kfree(isopkt);
  1007. return -EFAULT;
  1008. }
  1009. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1010. /* arbitrary limit,
  1011. * sufficient for USB 2.0 high-bandwidth iso */
  1012. if (isopkt[u].length > 8192) {
  1013. kfree(isopkt);
  1014. return -EINVAL;
  1015. }
  1016. totlen += isopkt[u].length;
  1017. }
  1018. /* 3072 * 64 microframes */
  1019. if (totlen > 196608) {
  1020. kfree(isopkt);
  1021. return -EINVAL;
  1022. }
  1023. uurb->buffer_length = totlen;
  1024. break;
  1025. default:
  1026. return -EINVAL;
  1027. }
  1028. if (uurb->buffer_length > 0 &&
  1029. !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  1030. uurb->buffer, uurb->buffer_length)) {
  1031. kfree(isopkt);
  1032. kfree(dr);
  1033. return -EFAULT;
  1034. }
  1035. as = alloc_async(uurb->number_of_packets);
  1036. if (!as) {
  1037. kfree(isopkt);
  1038. kfree(dr);
  1039. return -ENOMEM;
  1040. }
  1041. if (uurb->buffer_length > 0) {
  1042. as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
  1043. GFP_KERNEL);
  1044. if (!as->urb->transfer_buffer) {
  1045. kfree(isopkt);
  1046. kfree(dr);
  1047. free_async(as);
  1048. return -ENOMEM;
  1049. }
  1050. }
  1051. as->urb->dev = ps->dev;
  1052. as->urb->pipe = (uurb->type << 30) |
  1053. __create_pipe(ps->dev, uurb->endpoint & 0xf) |
  1054. (uurb->endpoint & USB_DIR_IN);
  1055. /* This tedious sequence is necessary because the URB_* flags
  1056. * are internal to the kernel and subject to change, whereas
  1057. * the USBDEVFS_URB_* flags are a user API and must not be changed.
  1058. */
  1059. u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
  1060. if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
  1061. u |= URB_ISO_ASAP;
  1062. if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
  1063. u |= URB_SHORT_NOT_OK;
  1064. if (uurb->flags & USBDEVFS_URB_NO_FSBR)
  1065. u |= URB_NO_FSBR;
  1066. if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
  1067. u |= URB_ZERO_PACKET;
  1068. if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
  1069. u |= URB_NO_INTERRUPT;
  1070. as->urb->transfer_flags = u;
  1071. as->urb->transfer_buffer_length = uurb->buffer_length;
  1072. as->urb->setup_packet = (unsigned char *)dr;
  1073. as->urb->start_frame = uurb->start_frame;
  1074. as->urb->number_of_packets = uurb->number_of_packets;
  1075. if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
  1076. ps->dev->speed == USB_SPEED_HIGH)
  1077. as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
  1078. else
  1079. as->urb->interval = ep->desc.bInterval;
  1080. as->urb->context = as;
  1081. as->urb->complete = async_completed;
  1082. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1083. as->urb->iso_frame_desc[u].offset = totlen;
  1084. as->urb->iso_frame_desc[u].length = isopkt[u].length;
  1085. totlen += isopkt[u].length;
  1086. }
  1087. kfree(isopkt);
  1088. as->ps = ps;
  1089. as->userurb = arg;
  1090. if (is_in && uurb->buffer_length > 0)
  1091. as->userbuffer = uurb->buffer;
  1092. else
  1093. as->userbuffer = NULL;
  1094. as->signr = uurb->signr;
  1095. as->ifnum = ifnum;
  1096. as->pid = get_pid(task_pid(current));
  1097. as->uid = cred->uid;
  1098. as->euid = cred->euid;
  1099. security_task_getsecid(current, &as->secid);
  1100. if (!is_in && uurb->buffer_length > 0) {
  1101. if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
  1102. uurb->buffer_length)) {
  1103. free_async(as);
  1104. return -EFAULT;
  1105. }
  1106. }
  1107. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1108. as->urb->transfer_buffer_length, 0, SUBMIT);
  1109. async_newpending(as);
  1110. if (usb_endpoint_xfer_bulk(&ep->desc)) {
  1111. spin_lock_irq(&ps->lock);
  1112. /* Not exactly the endpoint address; the direction bit is
  1113. * shifted to the 0x10 position so that the value will be
  1114. * between 0 and 31.
  1115. */
  1116. as->bulk_addr = usb_endpoint_num(&ep->desc) |
  1117. ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1118. >> 3);
  1119. /* If this bulk URB is the start of a new transfer, re-enable
  1120. * the endpoint. Otherwise mark it as a continuation URB.
  1121. */
  1122. if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
  1123. as->bulk_status = AS_CONTINUATION;
  1124. else
  1125. ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
  1126. /* Don't accept continuation URBs if the endpoint is
  1127. * disabled because of an earlier error.
  1128. */
  1129. if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
  1130. ret = -EREMOTEIO;
  1131. else
  1132. ret = usb_submit_urb(as->urb, GFP_ATOMIC);
  1133. spin_unlock_irq(&ps->lock);
  1134. } else {
  1135. ret = usb_submit_urb(as->urb, GFP_KERNEL);
  1136. }
  1137. if (ret) {
  1138. dev_printk(KERN_DEBUG, &ps->dev->dev,
  1139. "usbfs: usb_submit_urb returned %d\n", ret);
  1140. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1141. 0, ret, COMPLETE);
  1142. async_removepending(as);
  1143. free_async(as);
  1144. return ret;
  1145. }
  1146. return 0;
  1147. }
  1148. static int proc_submiturb(struct dev_state *ps, void __user *arg)
  1149. {
  1150. struct usbdevfs_urb uurb;
  1151. if (copy_from_user(&uurb, arg, sizeof(uurb)))
  1152. return -EFAULT;
  1153. return proc_do_submiturb(ps, &uurb,
  1154. (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
  1155. arg);
  1156. }
  1157. static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
  1158. {
  1159. struct async *as;
  1160. as = async_getpending(ps, arg);
  1161. if (!as)
  1162. return -EINVAL;
  1163. usb_kill_urb(as->urb);
  1164. return 0;
  1165. }
  1166. static int processcompl(struct async *as, void __user * __user *arg)
  1167. {
  1168. struct urb *urb = as->urb;
  1169. struct usbdevfs_urb __user *userurb = as->userurb;
  1170. void __user *addr = as->userurb;
  1171. unsigned int i;
  1172. if (as->userbuffer && urb->actual_length)
  1173. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1174. urb->actual_length))
  1175. goto err_out;
  1176. if (put_user(as->status, &userurb->status))
  1177. goto err_out;
  1178. if (put_user(urb->actual_length, &userurb->actual_length))
  1179. goto err_out;
  1180. if (put_user(urb->error_count, &userurb->error_count))
  1181. goto err_out;
  1182. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1183. for (i = 0; i < urb->number_of_packets; i++) {
  1184. if (put_user(urb->iso_frame_desc[i].actual_length,
  1185. &userurb->iso_frame_desc[i].actual_length))
  1186. goto err_out;
  1187. if (put_user(urb->iso_frame_desc[i].status,
  1188. &userurb->iso_frame_desc[i].status))
  1189. goto err_out;
  1190. }
  1191. }
  1192. if (put_user(addr, (void __user * __user *)arg))
  1193. return -EFAULT;
  1194. return 0;
  1195. err_out:
  1196. return -EFAULT;
  1197. }
  1198. static struct async *reap_as(struct dev_state *ps)
  1199. {
  1200. DECLARE_WAITQUEUE(wait, current);
  1201. struct async *as = NULL;
  1202. struct usb_device *dev = ps->dev;
  1203. add_wait_queue(&ps->wait, &wait);
  1204. for (;;) {
  1205. __set_current_state(TASK_INTERRUPTIBLE);
  1206. as = async_getcompleted(ps);
  1207. if (as)
  1208. break;
  1209. if (signal_pending(current))
  1210. break;
  1211. usb_unlock_device(dev);
  1212. schedule();
  1213. usb_lock_device(dev);
  1214. }
  1215. remove_wait_queue(&ps->wait, &wait);
  1216. set_current_state(TASK_RUNNING);
  1217. return as;
  1218. }
  1219. static int proc_reapurb(struct dev_state *ps, void __user *arg)
  1220. {
  1221. struct async *as = reap_as(ps);
  1222. if (as) {
  1223. int retval = processcompl(as, (void __user * __user *)arg);
  1224. free_async(as);
  1225. return retval;
  1226. }
  1227. if (signal_pending(current))
  1228. return -EINTR;
  1229. return -EIO;
  1230. }
  1231. static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
  1232. {
  1233. int retval;
  1234. struct async *as;
  1235. as = async_getcompleted(ps);
  1236. retval = -EAGAIN;
  1237. if (as) {
  1238. retval = processcompl(as, (void __user * __user *)arg);
  1239. free_async(as);
  1240. }
  1241. return retval;
  1242. }
  1243. #ifdef CONFIG_COMPAT
  1244. static int proc_control_compat(struct dev_state *ps,
  1245. struct usbdevfs_ctrltransfer32 __user *p32)
  1246. {
  1247. struct usbdevfs_ctrltransfer __user *p;
  1248. __u32 udata;
  1249. p = compat_alloc_user_space(sizeof(*p));
  1250. if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
  1251. get_user(udata, &p32->data) ||
  1252. put_user(compat_ptr(udata), &p->data))
  1253. return -EFAULT;
  1254. return proc_control(ps, p);
  1255. }
  1256. static int proc_bulk_compat(struct dev_state *ps,
  1257. struct usbdevfs_bulktransfer32 __user *p32)
  1258. {
  1259. struct usbdevfs_bulktransfer __user *p;
  1260. compat_uint_t n;
  1261. compat_caddr_t addr;
  1262. p = compat_alloc_user_space(sizeof(*p));
  1263. if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
  1264. get_user(n, &p32->len) || put_user(n, &p->len) ||
  1265. get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
  1266. get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
  1267. return -EFAULT;
  1268. return proc_bulk(ps, p);
  1269. }
  1270. static int proc_disconnectsignal_compat(struct dev_state *ps, void __user *arg)
  1271. {
  1272. struct usbdevfs_disconnectsignal32 ds;
  1273. if (copy_from_user(&ds, arg, sizeof(ds)))
  1274. return -EFAULT;
  1275. ps->discsignr = ds.signr;
  1276. ps->disccontext = compat_ptr(ds.context);
  1277. return 0;
  1278. }
  1279. static int get_urb32(struct usbdevfs_urb *kurb,
  1280. struct usbdevfs_urb32 __user *uurb)
  1281. {
  1282. __u32 uptr;
  1283. if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
  1284. __get_user(kurb->type, &uurb->type) ||
  1285. __get_user(kurb->endpoint, &uurb->endpoint) ||
  1286. __get_user(kurb->status, &uurb->status) ||
  1287. __get_user(kurb->flags, &uurb->flags) ||
  1288. __get_user(kurb->buffer_length, &uurb->buffer_length) ||
  1289. __get_user(kurb->actual_length, &uurb->actual_length) ||
  1290. __get_user(kurb->start_frame, &uurb->start_frame) ||
  1291. __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
  1292. __get_user(kurb->error_count, &uurb->error_count) ||
  1293. __get_user(kurb->signr, &uurb->signr))
  1294. return -EFAULT;
  1295. if (__get_user(uptr, &uurb->buffer))
  1296. return -EFAULT;
  1297. kurb->buffer = compat_ptr(uptr);
  1298. if (__get_user(uptr, &uurb->usercontext))
  1299. return -EFAULT;
  1300. kurb->usercontext = compat_ptr(uptr);
  1301. return 0;
  1302. }
  1303. static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
  1304. {
  1305. struct usbdevfs_urb uurb;
  1306. if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
  1307. return -EFAULT;
  1308. return proc_do_submiturb(ps, &uurb,
  1309. ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
  1310. arg);
  1311. }
  1312. static int processcompl_compat(struct async *as, void __user * __user *arg)
  1313. {
  1314. struct urb *urb = as->urb;
  1315. struct usbdevfs_urb32 __user *userurb = as->userurb;
  1316. void __user *addr = as->userurb;
  1317. unsigned int i;
  1318. if (as->userbuffer && urb->actual_length)
  1319. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1320. urb->actual_length))
  1321. return -EFAULT;
  1322. if (put_user(as->status, &userurb->status))
  1323. return -EFAULT;
  1324. if (put_user(urb->actual_length, &userurb->actual_length))
  1325. return -EFAULT;
  1326. if (put_user(urb->error_count, &userurb->error_count))
  1327. return -EFAULT;
  1328. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1329. for (i = 0; i < urb->number_of_packets; i++) {
  1330. if (put_user(urb->iso_frame_desc[i].actual_length,
  1331. &userurb->iso_frame_desc[i].actual_length))
  1332. return -EFAULT;
  1333. if (put_user(urb->iso_frame_desc[i].status,
  1334. &userurb->iso_frame_desc[i].status))
  1335. return -EFAULT;
  1336. }
  1337. }
  1338. if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
  1339. return -EFAULT;
  1340. return 0;
  1341. }
  1342. static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
  1343. {
  1344. struct async *as = reap_as(ps);
  1345. if (as) {
  1346. int retval = processcompl_compat(as, (void __user * __user *)arg);
  1347. free_async(as);
  1348. return retval;
  1349. }
  1350. if (signal_pending(current))
  1351. return -EINTR;
  1352. return -EIO;
  1353. }
  1354. static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
  1355. {
  1356. int retval;
  1357. struct async *as;
  1358. retval = -EAGAIN;
  1359. as = async_getcompleted(ps);
  1360. if (as) {
  1361. retval = processcompl_compat(as, (void __user * __user *)arg);
  1362. free_async(as);
  1363. }
  1364. return retval;
  1365. }
  1366. #endif
  1367. static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
  1368. {
  1369. struct usbdevfs_disconnectsignal ds;
  1370. if (copy_from_user(&ds, arg, sizeof(ds)))
  1371. return -EFAULT;
  1372. ps->discsignr = ds.signr;
  1373. ps->disccontext = ds.context;
  1374. return 0;
  1375. }
  1376. static int proc_claiminterface(struct dev_state *ps, void __user *arg)
  1377. {
  1378. unsigned int ifnum;
  1379. if (get_user(ifnum, (unsigned int __user *)arg))
  1380. return -EFAULT;
  1381. return claimintf(ps, ifnum);
  1382. }
  1383. static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
  1384. {
  1385. unsigned int ifnum;
  1386. int ret;
  1387. if (get_user(ifnum, (unsigned int __user *)arg))
  1388. return -EFAULT;
  1389. if ((ret = releaseintf(ps, ifnum)) < 0)
  1390. return ret;
  1391. destroy_async_on_interface (ps, ifnum);
  1392. return 0;
  1393. }
  1394. static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
  1395. {
  1396. int size;
  1397. void *buf = NULL;
  1398. int retval = 0;
  1399. struct usb_interface *intf = NULL;
  1400. struct usb_driver *driver = NULL;
  1401. /* alloc buffer */
  1402. if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
  1403. if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
  1404. return -ENOMEM;
  1405. if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
  1406. if (copy_from_user(buf, ctl->data, size)) {
  1407. kfree(buf);
  1408. return -EFAULT;
  1409. }
  1410. } else {
  1411. memset(buf, 0, size);
  1412. }
  1413. }
  1414. if (!connected(ps)) {
  1415. kfree(buf);
  1416. return -ENODEV;
  1417. }
  1418. if (ps->dev->state != USB_STATE_CONFIGURED)
  1419. retval = -EHOSTUNREACH;
  1420. else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
  1421. retval = -EINVAL;
  1422. else switch (ctl->ioctl_code) {
  1423. /* disconnect kernel driver from interface */
  1424. case USBDEVFS_DISCONNECT:
  1425. if (intf->dev.driver) {
  1426. driver = to_usb_driver(intf->dev.driver);
  1427. dev_dbg(&intf->dev, "disconnect by usbfs\n");
  1428. usb_driver_release_interface(driver, intf);
  1429. } else
  1430. retval = -ENODATA;
  1431. break;
  1432. /* let kernel drivers try to (re)bind to the interface */
  1433. case USBDEVFS_CONNECT:
  1434. if (!intf->dev.driver)
  1435. retval = device_attach(&intf->dev);
  1436. else
  1437. retval = -EBUSY;
  1438. break;
  1439. /* talk directly to the interface's driver */
  1440. default:
  1441. if (intf->dev.driver)
  1442. driver = to_usb_driver(intf->dev.driver);
  1443. if (driver == NULL || driver->ioctl == NULL) {
  1444. retval = -ENOTTY;
  1445. } else {
  1446. retval = driver->ioctl(intf, ctl->ioctl_code, buf);
  1447. if (retval == -ENOIOCTLCMD)
  1448. retval = -ENOTTY;
  1449. }
  1450. }
  1451. /* cleanup and return */
  1452. if (retval >= 0
  1453. && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
  1454. && size > 0
  1455. && copy_to_user(ctl->data, buf, size) != 0)
  1456. retval = -EFAULT;
  1457. kfree(buf);
  1458. return retval;
  1459. }
  1460. static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
  1461. {
  1462. struct usbdevfs_ioctl ctrl;
  1463. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1464. return -EFAULT;
  1465. return proc_ioctl(ps, &ctrl);
  1466. }
  1467. #ifdef CONFIG_COMPAT
  1468. static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
  1469. {
  1470. struct usbdevfs_ioctl32 __user *uioc;
  1471. struct usbdevfs_ioctl ctrl;
  1472. u32 udata;
  1473. uioc = compat_ptr((long)arg);
  1474. if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
  1475. __get_user(ctrl.ifno, &uioc->ifno) ||
  1476. __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
  1477. __get_user(udata, &uioc->data))
  1478. return -EFAULT;
  1479. ctrl.data = compat_ptr(udata);
  1480. return proc_ioctl(ps, &ctrl);
  1481. }
  1482. #endif
  1483. static int proc_claim_port(struct dev_state *ps, void __user *arg)
  1484. {
  1485. unsigned portnum;
  1486. int rc;
  1487. if (get_user(portnum, (unsigned __user *) arg))
  1488. return -EFAULT;
  1489. rc = usb_hub_claim_port(ps->dev, portnum, ps);
  1490. if (rc == 0)
  1491. snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
  1492. portnum, task_pid_nr(current), current->comm);
  1493. return rc;
  1494. }
  1495. static int proc_release_port(struct dev_state *ps, void __user *arg)
  1496. {
  1497. unsigned portnum;
  1498. if (get_user(portnum, (unsigned __user *) arg))
  1499. return -EFAULT;
  1500. return usb_hub_release_port(ps->dev, portnum, ps);
  1501. }
  1502. /*
  1503. * NOTE: All requests here that have interface numbers as parameters
  1504. * are assuming that somehow the configuration has been prevented from
  1505. * changing. But there's no mechanism to ensure that...
  1506. */
  1507. static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
  1508. void __user *p)
  1509. {
  1510. struct dev_state *ps = file->private_data;
  1511. struct inode *inode = file->f_path.dentry->d_inode;
  1512. struct usb_device *dev = ps->dev;
  1513. int ret = -ENOTTY;
  1514. if (!(file->f_mode & FMODE_WRITE))
  1515. return -EPERM;
  1516. usb_lock_device(dev);
  1517. if (!connected(ps)) {
  1518. usb_unlock_device(dev);
  1519. return -ENODEV;
  1520. }
  1521. switch (cmd) {
  1522. case USBDEVFS_CONTROL:
  1523. snoop(&dev->dev, "%s: CONTROL\n", __func__);
  1524. ret = proc_control(ps, p);
  1525. if (ret >= 0)
  1526. inode->i_mtime = CURRENT_TIME;
  1527. break;
  1528. case USBDEVFS_BULK:
  1529. snoop(&dev->dev, "%s: BULK\n", __func__);
  1530. ret = proc_bulk(ps, p);
  1531. if (ret >= 0)
  1532. inode->i_mtime = CURRENT_TIME;
  1533. break;
  1534. case USBDEVFS_RESETEP:
  1535. snoop(&dev->dev, "%s: RESETEP\n", __func__);
  1536. ret = proc_resetep(ps, p);
  1537. if (ret >= 0)
  1538. inode->i_mtime = CURRENT_TIME;
  1539. break;
  1540. case USBDEVFS_RESET:
  1541. snoop(&dev->dev, "%s: RESET\n", __func__);
  1542. ret = proc_resetdevice(ps);
  1543. break;
  1544. case USBDEVFS_CLEAR_HALT:
  1545. snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
  1546. ret = proc_clearhalt(ps, p);
  1547. if (ret >= 0)
  1548. inode->i_mtime = CURRENT_TIME;
  1549. break;
  1550. case USBDEVFS_GETDRIVER:
  1551. snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
  1552. ret = proc_getdriver(ps, p);
  1553. break;
  1554. case USBDEVFS_CONNECTINFO:
  1555. snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
  1556. ret = proc_connectinfo(ps, p);
  1557. break;
  1558. case USBDEVFS_SETINTERFACE:
  1559. snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
  1560. ret = proc_setintf(ps, p);
  1561. break;
  1562. case USBDEVFS_SETCONFIGURATION:
  1563. snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
  1564. ret = proc_setconfig(ps, p);
  1565. break;
  1566. case USBDEVFS_SUBMITURB:
  1567. snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
  1568. ret = proc_submiturb(ps, p);
  1569. if (ret >= 0)
  1570. inode->i_mtime = CURRENT_TIME;
  1571. break;
  1572. #ifdef CONFIG_COMPAT
  1573. case USBDEVFS_CONTROL32:
  1574. snoop(&dev->dev, "%s: CONTROL32\n", __func__);
  1575. ret = proc_control_compat(ps, p);
  1576. if (ret >= 0)
  1577. inode->i_mtime = CURRENT_TIME;
  1578. break;
  1579. case USBDEVFS_BULK32:
  1580. snoop(&dev->dev, "%s: BULK32\n", __func__);
  1581. ret = proc_bulk_compat(ps, p);
  1582. if (ret >= 0)
  1583. inode->i_mtime = CURRENT_TIME;
  1584. break;
  1585. case USBDEVFS_DISCSIGNAL32:
  1586. snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
  1587. ret = proc_disconnectsignal_compat(ps, p);
  1588. break;
  1589. case USBDEVFS_SUBMITURB32:
  1590. snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
  1591. ret = proc_submiturb_compat(ps, p);
  1592. if (ret >= 0)
  1593. inode->i_mtime = CURRENT_TIME;
  1594. break;
  1595. case USBDEVFS_REAPURB32:
  1596. snoop(&dev->dev, "%s: REAPURB32\n", __func__);
  1597. ret = proc_reapurb_compat(ps, p);
  1598. break;
  1599. case USBDEVFS_REAPURBNDELAY32:
  1600. snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
  1601. ret = proc_reapurbnonblock_compat(ps, p);
  1602. break;
  1603. case USBDEVFS_IOCTL32:
  1604. snoop(&dev->dev, "%s: IOCTL32\n", __func__);
  1605. ret = proc_ioctl_compat(ps, ptr_to_compat(p));
  1606. break;
  1607. #endif
  1608. case USBDEVFS_DISCARDURB:
  1609. snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
  1610. ret = proc_unlinkurb(ps, p);
  1611. break;
  1612. case USBDEVFS_REAPURB:
  1613. snoop(&dev->dev, "%s: REAPURB\n", __func__);
  1614. ret = proc_reapurb(ps, p);
  1615. break;
  1616. case USBDEVFS_REAPURBNDELAY:
  1617. snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
  1618. ret = proc_reapurbnonblock(ps, p);
  1619. break;
  1620. case USBDEVFS_DISCSIGNAL:
  1621. snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
  1622. ret = proc_disconnectsignal(ps, p);
  1623. break;
  1624. case USBDEVFS_CLAIMINTERFACE:
  1625. snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
  1626. ret = proc_claiminterface(ps, p);
  1627. break;
  1628. case USBDEVFS_RELEASEINTERFACE:
  1629. snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
  1630. ret = proc_releaseinterface(ps, p);
  1631. break;
  1632. case USBDEVFS_IOCTL:
  1633. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  1634. ret = proc_ioctl_default(ps, p);
  1635. break;
  1636. case USBDEVFS_CLAIM_PORT:
  1637. snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
  1638. ret = proc_claim_port(ps, p);
  1639. break;
  1640. case USBDEVFS_RELEASE_PORT:
  1641. snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
  1642. ret = proc_release_port(ps, p);
  1643. break;
  1644. }
  1645. usb_unlock_device(dev);
  1646. if (ret >= 0)
  1647. inode->i_atime = CURRENT_TIME;
  1648. return ret;
  1649. }
  1650. static long usbdev_ioctl(struct file *file, unsigned int cmd,
  1651. unsigned long arg)
  1652. {
  1653. int ret;
  1654. lock_kernel();
  1655. ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
  1656. unlock_kernel();
  1657. return ret;
  1658. }
  1659. #ifdef CONFIG_COMPAT
  1660. static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
  1661. unsigned long arg)
  1662. {
  1663. int ret;
  1664. lock_kernel();
  1665. ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
  1666. unlock_kernel();
  1667. return ret;
  1668. }
  1669. #endif
  1670. /* No kernel lock - fine */
  1671. static unsigned int usbdev_poll(struct file *file,
  1672. struct poll_table_struct *wait)
  1673. {
  1674. struct dev_state *ps = file->private_data;
  1675. unsigned int mask = 0;
  1676. poll_wait(file, &ps->wait, wait);
  1677. if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
  1678. mask |= POLLOUT | POLLWRNORM;
  1679. if (!connected(ps))
  1680. mask |= POLLERR | POLLHUP;
  1681. return mask;
  1682. }
  1683. const struct file_operations usbdev_file_operations = {
  1684. .owner = THIS_MODULE,
  1685. .llseek = usbdev_lseek,
  1686. .read = usbdev_read,
  1687. .poll = usbdev_poll,
  1688. .unlocked_ioctl = usbdev_ioctl,
  1689. #ifdef CONFIG_COMPAT
  1690. .compat_ioctl = usbdev_compat_ioctl,
  1691. #endif
  1692. .open = usbdev_open,
  1693. .release = usbdev_release,
  1694. };
  1695. static void usbdev_remove(struct usb_device *udev)
  1696. {
  1697. struct dev_state *ps;
  1698. struct siginfo sinfo;
  1699. while (!list_empty(&udev->filelist)) {
  1700. ps = list_entry(udev->filelist.next, struct dev_state, list);
  1701. destroy_all_async(ps);
  1702. wake_up_all(&ps->wait);
  1703. list_del_init(&ps->list);
  1704. if (ps->discsignr) {
  1705. sinfo.si_signo = ps->discsignr;
  1706. sinfo.si_errno = EPIPE;
  1707. sinfo.si_code = SI_ASYNCIO;
  1708. sinfo.si_addr = ps->disccontext;
  1709. kill_pid_info_as_uid(ps->discsignr, &sinfo,
  1710. ps->disc_pid, ps->disc_uid,
  1711. ps->disc_euid, ps->secid);
  1712. }
  1713. }
  1714. }
  1715. #ifdef CONFIG_USB_DEVICE_CLASS
  1716. static struct class *usb_classdev_class;
  1717. static int usb_classdev_add(struct usb_device *dev)
  1718. {
  1719. struct device *cldev;
  1720. cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
  1721. NULL, "usbdev%d.%d", dev->bus->busnum,
  1722. dev->devnum);
  1723. if (IS_ERR(cldev))
  1724. return PTR_ERR(cldev);
  1725. dev->usb_classdev = cldev;
  1726. return 0;
  1727. }
  1728. static void usb_classdev_remove(struct usb_device *dev)
  1729. {
  1730. if (dev->usb_classdev)
  1731. device_unregister(dev->usb_classdev);
  1732. }
  1733. #else
  1734. #define usb_classdev_add(dev) 0
  1735. #define usb_classdev_remove(dev) do {} while (0)
  1736. #endif
  1737. static int usbdev_notify(struct notifier_block *self,
  1738. unsigned long action, void *dev)
  1739. {
  1740. switch (action) {
  1741. case USB_DEVICE_ADD:
  1742. if (usb_classdev_add(dev))
  1743. return NOTIFY_BAD;
  1744. break;
  1745. case USB_DEVICE_REMOVE:
  1746. usb_classdev_remove(dev);
  1747. usbdev_remove(dev);
  1748. break;
  1749. }
  1750. return NOTIFY_OK;
  1751. }
  1752. static struct notifier_block usbdev_nb = {
  1753. .notifier_call = usbdev_notify,
  1754. };
  1755. static struct cdev usb_device_cdev;
  1756. int __init usb_devio_init(void)
  1757. {
  1758. int retval;
  1759. retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
  1760. "usb_device");
  1761. if (retval) {
  1762. printk(KERN_ERR "Unable to register minors for usb_device\n");
  1763. goto out;
  1764. }
  1765. cdev_init(&usb_device_cdev, &usbdev_file_operations);
  1766. retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
  1767. if (retval) {
  1768. printk(KERN_ERR "Unable to get usb_device major %d\n",
  1769. USB_DEVICE_MAJOR);
  1770. goto error_cdev;
  1771. }
  1772. #ifdef CONFIG_USB_DEVICE_CLASS
  1773. usb_classdev_class = class_create(THIS_MODULE, "usb_device");
  1774. if (IS_ERR(usb_classdev_class)) {
  1775. printk(KERN_ERR "Unable to register usb_device class\n");
  1776. retval = PTR_ERR(usb_classdev_class);
  1777. cdev_del(&usb_device_cdev);
  1778. usb_classdev_class = NULL;
  1779. goto out;
  1780. }
  1781. /* devices of this class shadow the major:minor of their parent
  1782. * device, so clear ->dev_kobj to prevent adding duplicate entries
  1783. * to /sys/dev
  1784. */
  1785. usb_classdev_class->dev_kobj = NULL;
  1786. #endif
  1787. usb_register_notify(&usbdev_nb);
  1788. out:
  1789. return retval;
  1790. error_cdev:
  1791. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1792. goto out;
  1793. }
  1794. void usb_devio_cleanup(void)
  1795. {
  1796. usb_unregister_notify(&usbdev_nb);
  1797. #ifdef CONFIG_USB_DEVICE_CLASS
  1798. class_destroy(usb_classdev_class);
  1799. #endif
  1800. cdev_del(&usb_device_cdev);
  1801. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1802. }