devio.c 51 KB

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