devio.c 52 KB

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