devio.c 52 KB

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