vt_ioctl.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. /*
  2. * linux/drivers/char/vt_ioctl.c
  3. *
  4. * Copyright (C) 1992 obz under the linux copyright
  5. *
  6. * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
  7. * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
  8. * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
  9. * Some code moved for less code duplication - Andi Kleen - Mar 1997
  10. * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
  11. */
  12. #include <linux/types.h>
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/tty.h>
  16. #include <linux/timer.h>
  17. #include <linux/kernel.h>
  18. #include <linux/compat.h>
  19. #include <linux/module.h>
  20. #include <linux/kd.h>
  21. #include <linux/vt.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <linux/major.h>
  25. #include <linux/fs.h>
  26. #include <linux/console.h>
  27. #include <linux/consolemap.h>
  28. #include <linux/signal.h>
  29. #include <linux/timex.h>
  30. #include <asm/io.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/kbd_kern.h>
  33. #include <linux/vt_kern.h>
  34. #include <linux/kbd_diacr.h>
  35. #include <linux/selection.h>
  36. char vt_dont_switch;
  37. extern struct tty_driver *console_driver;
  38. #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
  39. #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
  40. /*
  41. * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
  42. * experimentation and study of X386 SYSV handling.
  43. *
  44. * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
  45. * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
  46. * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
  47. * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
  48. * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
  49. * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
  50. * to the current console is done by the main ioctl code.
  51. */
  52. #ifdef CONFIG_X86
  53. #include <linux/syscalls.h>
  54. #endif
  55. static void complete_change_console(struct vc_data *vc);
  56. /*
  57. * User space VT_EVENT handlers
  58. */
  59. struct vt_event_wait {
  60. struct list_head list;
  61. struct vt_event event;
  62. int done;
  63. };
  64. static LIST_HEAD(vt_events);
  65. static DEFINE_SPINLOCK(vt_event_lock);
  66. static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
  67. /**
  68. * vt_event_post
  69. * @event: the event that occurred
  70. * @old: old console
  71. * @new: new console
  72. *
  73. * Post an VT event to interested VT handlers
  74. */
  75. void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
  76. {
  77. struct list_head *pos, *head;
  78. unsigned long flags;
  79. int wake = 0;
  80. spin_lock_irqsave(&vt_event_lock, flags);
  81. head = &vt_events;
  82. list_for_each(pos, head) {
  83. struct vt_event_wait *ve = list_entry(pos,
  84. struct vt_event_wait, list);
  85. if (!(ve->event.event & event))
  86. continue;
  87. ve->event.event = event;
  88. /* kernel view is consoles 0..n-1, user space view is
  89. console 1..n with 0 meaning current, so we must bias */
  90. ve->event.oldev = old + 1;
  91. ve->event.newev = new + 1;
  92. wake = 1;
  93. ve->done = 1;
  94. }
  95. spin_unlock_irqrestore(&vt_event_lock, flags);
  96. if (wake)
  97. wake_up_interruptible(&vt_event_waitqueue);
  98. }
  99. /**
  100. * vt_event_wait - wait for an event
  101. * @vw: our event
  102. *
  103. * Waits for an event to occur which completes our vt_event_wait
  104. * structure. On return the structure has wv->done set to 1 for success
  105. * or 0 if some event such as a signal ended the wait.
  106. */
  107. static void vt_event_wait(struct vt_event_wait *vw)
  108. {
  109. unsigned long flags;
  110. /* Prepare the event */
  111. INIT_LIST_HEAD(&vw->list);
  112. vw->done = 0;
  113. /* Queue our event */
  114. spin_lock_irqsave(&vt_event_lock, flags);
  115. list_add(&vw->list, &vt_events);
  116. spin_unlock_irqrestore(&vt_event_lock, flags);
  117. /* Wait for it to pass */
  118. wait_event_interruptible_tty(vt_event_waitqueue, vw->done);
  119. /* Dequeue it */
  120. spin_lock_irqsave(&vt_event_lock, flags);
  121. list_del(&vw->list);
  122. spin_unlock_irqrestore(&vt_event_lock, flags);
  123. }
  124. /**
  125. * vt_event_wait_ioctl - event ioctl handler
  126. * @arg: argument to ioctl
  127. *
  128. * Implement the VT_WAITEVENT ioctl using the VT event interface
  129. */
  130. static int vt_event_wait_ioctl(struct vt_event __user *event)
  131. {
  132. struct vt_event_wait vw;
  133. if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
  134. return -EFAULT;
  135. /* Highest supported event for now */
  136. if (vw.event.event & ~VT_MAX_EVENT)
  137. return -EINVAL;
  138. vt_event_wait(&vw);
  139. /* If it occurred report it */
  140. if (vw.done) {
  141. if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
  142. return -EFAULT;
  143. return 0;
  144. }
  145. return -EINTR;
  146. }
  147. /**
  148. * vt_waitactive - active console wait
  149. * @event: event code
  150. * @n: new console
  151. *
  152. * Helper for event waits. Used to implement the legacy
  153. * event waiting ioctls in terms of events
  154. */
  155. int vt_waitactive(int n)
  156. {
  157. struct vt_event_wait vw;
  158. do {
  159. if (n == fg_console + 1)
  160. break;
  161. vw.event.event = VT_EVENT_SWITCH;
  162. vt_event_wait(&vw);
  163. if (vw.done == 0)
  164. return -EINTR;
  165. } while (vw.event.newev != n);
  166. return 0;
  167. }
  168. /*
  169. * these are the valid i/o ports we're allowed to change. they map all the
  170. * video ports
  171. */
  172. #define GPFIRST 0x3b4
  173. #define GPLAST 0x3df
  174. #define GPNUM (GPLAST - GPFIRST + 1)
  175. #define i (tmp.kb_index)
  176. #define s (tmp.kb_table)
  177. #define v (tmp.kb_value)
  178. static inline int
  179. do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
  180. {
  181. struct kbentry tmp;
  182. ushort *key_map, val, ov;
  183. if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
  184. return -EFAULT;
  185. if (!capable(CAP_SYS_TTY_CONFIG))
  186. perm = 0;
  187. switch (cmd) {
  188. case KDGKBENT:
  189. key_map = key_maps[s];
  190. if (key_map) {
  191. val = U(key_map[i]);
  192. if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
  193. val = K_HOLE;
  194. } else
  195. val = (i ? K_HOLE : K_NOSUCHMAP);
  196. return put_user(val, &user_kbe->kb_value);
  197. case KDSKBENT:
  198. if (!perm)
  199. return -EPERM;
  200. if (!i && v == K_NOSUCHMAP) {
  201. /* deallocate map */
  202. key_map = key_maps[s];
  203. if (s && key_map) {
  204. key_maps[s] = NULL;
  205. if (key_map[0] == U(K_ALLOCATED)) {
  206. kfree(key_map);
  207. keymap_count--;
  208. }
  209. }
  210. break;
  211. }
  212. if (KTYP(v) < NR_TYPES) {
  213. if (KVAL(v) > max_vals[KTYP(v)])
  214. return -EINVAL;
  215. } else
  216. if (kbd->kbdmode != VC_UNICODE)
  217. return -EINVAL;
  218. /* ++Geert: non-PC keyboards may generate keycode zero */
  219. #if !defined(__mc68000__) && !defined(__powerpc__)
  220. /* assignment to entry 0 only tests validity of args */
  221. if (!i)
  222. break;
  223. #endif
  224. if (!(key_map = key_maps[s])) {
  225. int j;
  226. if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
  227. !capable(CAP_SYS_RESOURCE))
  228. return -EPERM;
  229. key_map = kmalloc(sizeof(plain_map),
  230. GFP_KERNEL);
  231. if (!key_map)
  232. return -ENOMEM;
  233. key_maps[s] = key_map;
  234. key_map[0] = U(K_ALLOCATED);
  235. for (j = 1; j < NR_KEYS; j++)
  236. key_map[j] = U(K_HOLE);
  237. keymap_count++;
  238. }
  239. ov = U(key_map[i]);
  240. if (v == ov)
  241. break; /* nothing to do */
  242. /*
  243. * Attention Key.
  244. */
  245. if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
  246. return -EPERM;
  247. key_map[i] = U(v);
  248. if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
  249. compute_shiftstate();
  250. break;
  251. }
  252. return 0;
  253. }
  254. #undef i
  255. #undef s
  256. #undef v
  257. static inline int
  258. do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
  259. {
  260. struct kbkeycode tmp;
  261. int kc = 0;
  262. if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
  263. return -EFAULT;
  264. switch (cmd) {
  265. case KDGETKEYCODE:
  266. kc = getkeycode(tmp.scancode);
  267. if (kc >= 0)
  268. kc = put_user(kc, &user_kbkc->keycode);
  269. break;
  270. case KDSETKEYCODE:
  271. if (!perm)
  272. return -EPERM;
  273. kc = setkeycode(tmp.scancode, tmp.keycode);
  274. break;
  275. }
  276. return kc;
  277. }
  278. static inline int
  279. do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
  280. {
  281. struct kbsentry *kbs;
  282. char *p;
  283. u_char *q;
  284. u_char __user *up;
  285. int sz;
  286. int delta;
  287. char *first_free, *fj, *fnw;
  288. int i, j, k;
  289. int ret;
  290. if (!capable(CAP_SYS_TTY_CONFIG))
  291. perm = 0;
  292. kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
  293. if (!kbs) {
  294. ret = -ENOMEM;
  295. goto reterr;
  296. }
  297. /* we mostly copy too much here (512bytes), but who cares ;) */
  298. if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
  299. ret = -EFAULT;
  300. goto reterr;
  301. }
  302. kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
  303. i = kbs->kb_func;
  304. switch (cmd) {
  305. case KDGKBSENT:
  306. sz = sizeof(kbs->kb_string) - 1; /* sz should have been
  307. a struct member */
  308. up = user_kdgkb->kb_string;
  309. p = func_table[i];
  310. if(p)
  311. for ( ; *p && sz; p++, sz--)
  312. if (put_user(*p, up++)) {
  313. ret = -EFAULT;
  314. goto reterr;
  315. }
  316. if (put_user('\0', up)) {
  317. ret = -EFAULT;
  318. goto reterr;
  319. }
  320. kfree(kbs);
  321. return ((p && *p) ? -EOVERFLOW : 0);
  322. case KDSKBSENT:
  323. if (!perm) {
  324. ret = -EPERM;
  325. goto reterr;
  326. }
  327. q = func_table[i];
  328. first_free = funcbufptr + (funcbufsize - funcbufleft);
  329. for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
  330. ;
  331. if (j < MAX_NR_FUNC)
  332. fj = func_table[j];
  333. else
  334. fj = first_free;
  335. delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
  336. if (delta <= funcbufleft) { /* it fits in current buf */
  337. if (j < MAX_NR_FUNC) {
  338. memmove(fj + delta, fj, first_free - fj);
  339. for (k = j; k < MAX_NR_FUNC; k++)
  340. if (func_table[k])
  341. func_table[k] += delta;
  342. }
  343. if (!q)
  344. func_table[i] = fj;
  345. funcbufleft -= delta;
  346. } else { /* allocate a larger buffer */
  347. sz = 256;
  348. while (sz < funcbufsize - funcbufleft + delta)
  349. sz <<= 1;
  350. fnw = kmalloc(sz, GFP_KERNEL);
  351. if(!fnw) {
  352. ret = -ENOMEM;
  353. goto reterr;
  354. }
  355. if (!q)
  356. func_table[i] = fj;
  357. if (fj > funcbufptr)
  358. memmove(fnw, funcbufptr, fj - funcbufptr);
  359. for (k = 0; k < j; k++)
  360. if (func_table[k])
  361. func_table[k] = fnw + (func_table[k] - funcbufptr);
  362. if (first_free > fj) {
  363. memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
  364. for (k = j; k < MAX_NR_FUNC; k++)
  365. if (func_table[k])
  366. func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
  367. }
  368. if (funcbufptr != func_buf)
  369. kfree(funcbufptr);
  370. funcbufptr = fnw;
  371. funcbufleft = funcbufleft - delta + sz - funcbufsize;
  372. funcbufsize = sz;
  373. }
  374. strcpy(func_table[i], kbs->kb_string);
  375. break;
  376. }
  377. ret = 0;
  378. reterr:
  379. kfree(kbs);
  380. return ret;
  381. }
  382. static inline int
  383. do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
  384. {
  385. struct consolefontdesc cfdarg;
  386. int i;
  387. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
  388. return -EFAULT;
  389. switch (cmd) {
  390. case PIO_FONTX:
  391. if (!perm)
  392. return -EPERM;
  393. op->op = KD_FONT_OP_SET;
  394. op->flags = KD_FONT_FLAG_OLD;
  395. op->width = 8;
  396. op->height = cfdarg.charheight;
  397. op->charcount = cfdarg.charcount;
  398. op->data = cfdarg.chardata;
  399. return con_font_op(vc_cons[fg_console].d, op);
  400. case GIO_FONTX: {
  401. op->op = KD_FONT_OP_GET;
  402. op->flags = KD_FONT_FLAG_OLD;
  403. op->width = 8;
  404. op->height = cfdarg.charheight;
  405. op->charcount = cfdarg.charcount;
  406. op->data = cfdarg.chardata;
  407. i = con_font_op(vc_cons[fg_console].d, op);
  408. if (i)
  409. return i;
  410. cfdarg.charheight = op->height;
  411. cfdarg.charcount = op->charcount;
  412. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
  413. return -EFAULT;
  414. return 0;
  415. }
  416. }
  417. return -EINVAL;
  418. }
  419. static inline int
  420. do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
  421. {
  422. struct unimapdesc tmp;
  423. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  424. return -EFAULT;
  425. if (tmp.entries)
  426. if (!access_ok(VERIFY_WRITE, tmp.entries,
  427. tmp.entry_ct*sizeof(struct unipair)))
  428. return -EFAULT;
  429. switch (cmd) {
  430. case PIO_UNIMAP:
  431. if (!perm)
  432. return -EPERM;
  433. return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
  434. case GIO_UNIMAP:
  435. if (!perm && fg_console != vc->vc_num)
  436. return -EPERM;
  437. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
  438. }
  439. return 0;
  440. }
  441. /*
  442. * We handle the console-specific ioctl's here. We allow the
  443. * capability to modify any console, not just the fg_console.
  444. */
  445. int vt_ioctl(struct tty_struct *tty,
  446. unsigned int cmd, unsigned long arg)
  447. {
  448. struct vc_data *vc = tty->driver_data;
  449. struct console_font_op op; /* used in multiple places here */
  450. struct kbd_struct * kbd;
  451. unsigned int console;
  452. unsigned char ucval;
  453. unsigned int uival;
  454. void __user *up = (void __user *)arg;
  455. int i, perm;
  456. int ret = 0;
  457. console = vc->vc_num;
  458. tty_lock();
  459. if (!vc_cons_allocated(console)) { /* impossible? */
  460. ret = -ENOIOCTLCMD;
  461. goto out;
  462. }
  463. /*
  464. * To have permissions to do most of the vt ioctls, we either have
  465. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  466. */
  467. perm = 0;
  468. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  469. perm = 1;
  470. kbd = kbd_table + console;
  471. switch (cmd) {
  472. case TIOCLINUX:
  473. ret = tioclinux(tty, arg);
  474. break;
  475. case KIOCSOUND:
  476. if (!perm)
  477. goto eperm;
  478. /*
  479. * The use of PIT_TICK_RATE is historic, it used to be
  480. * the platform-dependent CLOCK_TICK_RATE between 2.6.12
  481. * and 2.6.36, which was a minor but unfortunate ABI
  482. * change.
  483. */
  484. if (arg)
  485. arg = PIT_TICK_RATE / arg;
  486. kd_mksound(arg, 0);
  487. break;
  488. case KDMKTONE:
  489. if (!perm)
  490. goto eperm;
  491. {
  492. unsigned int ticks, count;
  493. /*
  494. * Generate the tone for the appropriate number of ticks.
  495. * If the time is zero, turn off sound ourselves.
  496. */
  497. ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
  498. count = ticks ? (arg & 0xffff) : 0;
  499. if (count)
  500. count = PIT_TICK_RATE / count;
  501. kd_mksound(count, ticks);
  502. break;
  503. }
  504. case KDGKBTYPE:
  505. /*
  506. * this is naive.
  507. */
  508. ucval = KB_101;
  509. goto setchar;
  510. /*
  511. * These cannot be implemented on any machine that implements
  512. * ioperm() in user level (such as Alpha PCs) or not at all.
  513. *
  514. * XXX: you should never use these, just call ioperm directly..
  515. */
  516. #ifdef CONFIG_X86
  517. case KDADDIO:
  518. case KDDELIO:
  519. /*
  520. * KDADDIO and KDDELIO may be able to add ports beyond what
  521. * we reject here, but to be safe...
  522. */
  523. if (arg < GPFIRST || arg > GPLAST) {
  524. ret = -EINVAL;
  525. break;
  526. }
  527. ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
  528. break;
  529. case KDENABIO:
  530. case KDDISABIO:
  531. ret = sys_ioperm(GPFIRST, GPNUM,
  532. (cmd == KDENABIO)) ? -ENXIO : 0;
  533. break;
  534. #endif
  535. /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
  536. case KDKBDREP:
  537. {
  538. struct kbd_repeat kbrep;
  539. if (!capable(CAP_SYS_TTY_CONFIG))
  540. goto eperm;
  541. if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
  542. ret = -EFAULT;
  543. break;
  544. }
  545. ret = kbd_rate(&kbrep);
  546. if (ret)
  547. break;
  548. if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
  549. ret = -EFAULT;
  550. break;
  551. }
  552. case KDSETMODE:
  553. /*
  554. * currently, setting the mode from KD_TEXT to KD_GRAPHICS
  555. * doesn't do a whole lot. i'm not sure if it should do any
  556. * restoration of modes or what...
  557. *
  558. * XXX It should at least call into the driver, fbdev's definitely
  559. * need to restore their engine state. --BenH
  560. */
  561. if (!perm)
  562. goto eperm;
  563. switch (arg) {
  564. case KD_GRAPHICS:
  565. break;
  566. case KD_TEXT0:
  567. case KD_TEXT1:
  568. arg = KD_TEXT;
  569. case KD_TEXT:
  570. break;
  571. default:
  572. ret = -EINVAL;
  573. goto out;
  574. }
  575. if (vc->vc_mode == (unsigned char) arg)
  576. break;
  577. vc->vc_mode = (unsigned char) arg;
  578. if (console != fg_console)
  579. break;
  580. /*
  581. * explicitly blank/unblank the screen if switching modes
  582. */
  583. console_lock();
  584. if (arg == KD_TEXT)
  585. do_unblank_screen(1);
  586. else
  587. do_blank_screen(1);
  588. console_unlock();
  589. break;
  590. case KDGETMODE:
  591. uival = vc->vc_mode;
  592. goto setint;
  593. case KDMAPDISP:
  594. case KDUNMAPDISP:
  595. /*
  596. * these work like a combination of mmap and KDENABIO.
  597. * this could be easily finished.
  598. */
  599. ret = -EINVAL;
  600. break;
  601. case KDSKBMODE:
  602. if (!perm)
  603. goto eperm;
  604. switch(arg) {
  605. case K_RAW:
  606. kbd->kbdmode = VC_RAW;
  607. break;
  608. case K_MEDIUMRAW:
  609. kbd->kbdmode = VC_MEDIUMRAW;
  610. break;
  611. case K_XLATE:
  612. kbd->kbdmode = VC_XLATE;
  613. compute_shiftstate();
  614. break;
  615. case K_UNICODE:
  616. kbd->kbdmode = VC_UNICODE;
  617. compute_shiftstate();
  618. break;
  619. case K_OFF:
  620. kbd->kbdmode = VC_OFF;
  621. break;
  622. default:
  623. ret = -EINVAL;
  624. goto out;
  625. }
  626. tty_ldisc_flush(tty);
  627. break;
  628. case KDGKBMODE:
  629. uival = ((kbd->kbdmode == VC_RAW) ? K_RAW :
  630. (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
  631. (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
  632. (kbd->kbdmode == VC_OFF) ? K_OFF :
  633. K_XLATE);
  634. goto setint;
  635. /* this could be folded into KDSKBMODE, but for compatibility
  636. reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
  637. case KDSKBMETA:
  638. switch(arg) {
  639. case K_METABIT:
  640. clr_vc_kbd_mode(kbd, VC_META);
  641. break;
  642. case K_ESCPREFIX:
  643. set_vc_kbd_mode(kbd, VC_META);
  644. break;
  645. default:
  646. ret = -EINVAL;
  647. }
  648. break;
  649. case KDGKBMETA:
  650. uival = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
  651. setint:
  652. ret = put_user(uival, (int __user *)arg);
  653. break;
  654. case KDGETKEYCODE:
  655. case KDSETKEYCODE:
  656. if(!capable(CAP_SYS_TTY_CONFIG))
  657. perm = 0;
  658. ret = do_kbkeycode_ioctl(cmd, up, perm);
  659. break;
  660. case KDGKBENT:
  661. case KDSKBENT:
  662. ret = do_kdsk_ioctl(cmd, up, perm, kbd);
  663. break;
  664. case KDGKBSENT:
  665. case KDSKBSENT:
  666. ret = do_kdgkb_ioctl(cmd, up, perm);
  667. break;
  668. case KDGKBDIACR:
  669. {
  670. struct kbdiacrs __user *a = up;
  671. struct kbdiacr diacr;
  672. int i;
  673. if (put_user(accent_table_size, &a->kb_cnt)) {
  674. ret = -EFAULT;
  675. break;
  676. }
  677. for (i = 0; i < accent_table_size; i++) {
  678. diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
  679. diacr.base = conv_uni_to_8bit(accent_table[i].base);
  680. diacr.result = conv_uni_to_8bit(accent_table[i].result);
  681. if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
  682. ret = -EFAULT;
  683. break;
  684. }
  685. }
  686. break;
  687. }
  688. case KDGKBDIACRUC:
  689. {
  690. struct kbdiacrsuc __user *a = up;
  691. if (put_user(accent_table_size, &a->kb_cnt))
  692. ret = -EFAULT;
  693. else if (copy_to_user(a->kbdiacruc, accent_table,
  694. accent_table_size*sizeof(struct kbdiacruc)))
  695. ret = -EFAULT;
  696. break;
  697. }
  698. case KDSKBDIACR:
  699. {
  700. struct kbdiacrs __user *a = up;
  701. struct kbdiacr diacr;
  702. unsigned int ct;
  703. int i;
  704. if (!perm)
  705. goto eperm;
  706. if (get_user(ct,&a->kb_cnt)) {
  707. ret = -EFAULT;
  708. break;
  709. }
  710. if (ct >= MAX_DIACR) {
  711. ret = -EINVAL;
  712. break;
  713. }
  714. accent_table_size = ct;
  715. for (i = 0; i < ct; i++) {
  716. if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
  717. ret = -EFAULT;
  718. break;
  719. }
  720. accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
  721. accent_table[i].base = conv_8bit_to_uni(diacr.base);
  722. accent_table[i].result = conv_8bit_to_uni(diacr.result);
  723. }
  724. break;
  725. }
  726. case KDSKBDIACRUC:
  727. {
  728. struct kbdiacrsuc __user *a = up;
  729. unsigned int ct;
  730. if (!perm)
  731. goto eperm;
  732. if (get_user(ct,&a->kb_cnt)) {
  733. ret = -EFAULT;
  734. break;
  735. }
  736. if (ct >= MAX_DIACR) {
  737. ret = -EINVAL;
  738. break;
  739. }
  740. accent_table_size = ct;
  741. if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
  742. ret = -EFAULT;
  743. break;
  744. }
  745. /* the ioctls below read/set the flags usually shown in the leds */
  746. /* don't use them - they will go away without warning */
  747. case KDGKBLED:
  748. ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
  749. goto setchar;
  750. case KDSKBLED:
  751. if (!perm)
  752. goto eperm;
  753. if (arg & ~0x77) {
  754. ret = -EINVAL;
  755. break;
  756. }
  757. kbd->ledflagstate = (arg & 7);
  758. kbd->default_ledflagstate = ((arg >> 4) & 7);
  759. set_leds();
  760. break;
  761. /* the ioctls below only set the lights, not the functions */
  762. /* for those, see KDGKBLED and KDSKBLED above */
  763. case KDGETLED:
  764. ucval = getledstate();
  765. setchar:
  766. ret = put_user(ucval, (char __user *)arg);
  767. break;
  768. case KDSETLED:
  769. if (!perm)
  770. goto eperm;
  771. setledstate(kbd, arg);
  772. break;
  773. /*
  774. * A process can indicate its willingness to accept signals
  775. * generated by pressing an appropriate key combination.
  776. * Thus, one can have a daemon that e.g. spawns a new console
  777. * upon a keypress and then changes to it.
  778. * See also the kbrequest field of inittab(5).
  779. */
  780. case KDSIGACCEPT:
  781. {
  782. if (!perm || !capable(CAP_KILL))
  783. goto eperm;
  784. if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
  785. ret = -EINVAL;
  786. else {
  787. spin_lock_irq(&vt_spawn_con.lock);
  788. put_pid(vt_spawn_con.pid);
  789. vt_spawn_con.pid = get_pid(task_pid(current));
  790. vt_spawn_con.sig = arg;
  791. spin_unlock_irq(&vt_spawn_con.lock);
  792. }
  793. break;
  794. }
  795. case VT_SETMODE:
  796. {
  797. struct vt_mode tmp;
  798. if (!perm)
  799. goto eperm;
  800. if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
  801. ret = -EFAULT;
  802. goto out;
  803. }
  804. if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
  805. ret = -EINVAL;
  806. goto out;
  807. }
  808. console_lock();
  809. vc->vt_mode = tmp;
  810. /* the frsig is ignored, so we set it to 0 */
  811. vc->vt_mode.frsig = 0;
  812. put_pid(vc->vt_pid);
  813. vc->vt_pid = get_pid(task_pid(current));
  814. /* no switch is required -- saw@shade.msu.ru */
  815. vc->vt_newvt = -1;
  816. console_unlock();
  817. break;
  818. }
  819. case VT_GETMODE:
  820. {
  821. struct vt_mode tmp;
  822. int rc;
  823. console_lock();
  824. memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
  825. console_unlock();
  826. rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
  827. if (rc)
  828. ret = -EFAULT;
  829. break;
  830. }
  831. /*
  832. * Returns global vt state. Note that VT 0 is always open, since
  833. * it's an alias for the current VT, and people can't use it here.
  834. * We cannot return state for more than 16 VTs, since v_state is short.
  835. */
  836. case VT_GETSTATE:
  837. {
  838. struct vt_stat __user *vtstat = up;
  839. unsigned short state, mask;
  840. if (put_user(fg_console + 1, &vtstat->v_active))
  841. ret = -EFAULT;
  842. else {
  843. state = 1; /* /dev/tty0 is always open */
  844. for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
  845. ++i, mask <<= 1)
  846. if (VT_IS_IN_USE(i))
  847. state |= mask;
  848. ret = put_user(state, &vtstat->v_state);
  849. }
  850. break;
  851. }
  852. /*
  853. * Returns the first available (non-opened) console.
  854. */
  855. case VT_OPENQRY:
  856. for (i = 0; i < MAX_NR_CONSOLES; ++i)
  857. if (! VT_IS_IN_USE(i))
  858. break;
  859. uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
  860. goto setint;
  861. /*
  862. * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
  863. * with num >= 1 (switches to vt 0, our console, are not allowed, just
  864. * to preserve sanity).
  865. */
  866. case VT_ACTIVATE:
  867. if (!perm)
  868. goto eperm;
  869. if (arg == 0 || arg > MAX_NR_CONSOLES)
  870. ret = -ENXIO;
  871. else {
  872. arg--;
  873. console_lock();
  874. ret = vc_allocate(arg);
  875. console_unlock();
  876. if (ret)
  877. break;
  878. set_console(arg);
  879. }
  880. break;
  881. case VT_SETACTIVATE:
  882. {
  883. struct vt_setactivate vsa;
  884. if (!perm)
  885. goto eperm;
  886. if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
  887. sizeof(struct vt_setactivate))) {
  888. ret = -EFAULT;
  889. goto out;
  890. }
  891. if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
  892. ret = -ENXIO;
  893. else {
  894. vsa.console--;
  895. console_lock();
  896. ret = vc_allocate(vsa.console);
  897. if (ret == 0) {
  898. struct vc_data *nvc;
  899. /* This is safe providing we don't drop the
  900. console sem between vc_allocate and
  901. finishing referencing nvc */
  902. nvc = vc_cons[vsa.console].d;
  903. nvc->vt_mode = vsa.mode;
  904. nvc->vt_mode.frsig = 0;
  905. put_pid(nvc->vt_pid);
  906. nvc->vt_pid = get_pid(task_pid(current));
  907. }
  908. console_unlock();
  909. if (ret)
  910. break;
  911. /* Commence switch and lock */
  912. set_console(vsa.console);
  913. }
  914. break;
  915. }
  916. /*
  917. * wait until the specified VT has been activated
  918. */
  919. case VT_WAITACTIVE:
  920. if (!perm)
  921. goto eperm;
  922. if (arg == 0 || arg > MAX_NR_CONSOLES)
  923. ret = -ENXIO;
  924. else
  925. ret = vt_waitactive(arg);
  926. break;
  927. /*
  928. * If a vt is under process control, the kernel will not switch to it
  929. * immediately, but postpone the operation until the process calls this
  930. * ioctl, allowing the switch to complete.
  931. *
  932. * According to the X sources this is the behavior:
  933. * 0: pending switch-from not OK
  934. * 1: pending switch-from OK
  935. * 2: completed switch-to OK
  936. */
  937. case VT_RELDISP:
  938. if (!perm)
  939. goto eperm;
  940. if (vc->vt_mode.mode != VT_PROCESS) {
  941. ret = -EINVAL;
  942. break;
  943. }
  944. /*
  945. * Switching-from response
  946. */
  947. console_lock();
  948. if (vc->vt_newvt >= 0) {
  949. if (arg == 0)
  950. /*
  951. * Switch disallowed, so forget we were trying
  952. * to do it.
  953. */
  954. vc->vt_newvt = -1;
  955. else {
  956. /*
  957. * The current vt has been released, so
  958. * complete the switch.
  959. */
  960. int newvt;
  961. newvt = vc->vt_newvt;
  962. vc->vt_newvt = -1;
  963. ret = vc_allocate(newvt);
  964. if (ret) {
  965. console_unlock();
  966. break;
  967. }
  968. /*
  969. * When we actually do the console switch,
  970. * make sure we are atomic with respect to
  971. * other console switches..
  972. */
  973. complete_change_console(vc_cons[newvt].d);
  974. }
  975. } else {
  976. /*
  977. * Switched-to response
  978. */
  979. /*
  980. * If it's just an ACK, ignore it
  981. */
  982. if (arg != VT_ACKACQ)
  983. ret = -EINVAL;
  984. }
  985. console_unlock();
  986. break;
  987. /*
  988. * Disallocate memory associated to VT (but leave VT1)
  989. */
  990. case VT_DISALLOCATE:
  991. if (arg > MAX_NR_CONSOLES) {
  992. ret = -ENXIO;
  993. break;
  994. }
  995. if (arg == 0) {
  996. /* deallocate all unused consoles, but leave 0 */
  997. console_lock();
  998. for (i=1; i<MAX_NR_CONSOLES; i++)
  999. if (! VT_BUSY(i))
  1000. vc_deallocate(i);
  1001. console_unlock();
  1002. } else {
  1003. /* deallocate a single console, if possible */
  1004. arg--;
  1005. if (VT_BUSY(arg))
  1006. ret = -EBUSY;
  1007. else if (arg) { /* leave 0 */
  1008. console_lock();
  1009. vc_deallocate(arg);
  1010. console_unlock();
  1011. }
  1012. }
  1013. break;
  1014. case VT_RESIZE:
  1015. {
  1016. struct vt_sizes __user *vtsizes = up;
  1017. struct vc_data *vc;
  1018. ushort ll,cc;
  1019. if (!perm)
  1020. goto eperm;
  1021. if (get_user(ll, &vtsizes->v_rows) ||
  1022. get_user(cc, &vtsizes->v_cols))
  1023. ret = -EFAULT;
  1024. else {
  1025. console_lock();
  1026. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1027. vc = vc_cons[i].d;
  1028. if (vc) {
  1029. vc->vc_resize_user = 1;
  1030. vc_resize(vc_cons[i].d, cc, ll);
  1031. }
  1032. }
  1033. console_unlock();
  1034. }
  1035. break;
  1036. }
  1037. case VT_RESIZEX:
  1038. {
  1039. struct vt_consize __user *vtconsize = up;
  1040. ushort ll,cc,vlin,clin,vcol,ccol;
  1041. if (!perm)
  1042. goto eperm;
  1043. if (!access_ok(VERIFY_READ, vtconsize,
  1044. sizeof(struct vt_consize))) {
  1045. ret = -EFAULT;
  1046. break;
  1047. }
  1048. /* FIXME: Should check the copies properly */
  1049. __get_user(ll, &vtconsize->v_rows);
  1050. __get_user(cc, &vtconsize->v_cols);
  1051. __get_user(vlin, &vtconsize->v_vlin);
  1052. __get_user(clin, &vtconsize->v_clin);
  1053. __get_user(vcol, &vtconsize->v_vcol);
  1054. __get_user(ccol, &vtconsize->v_ccol);
  1055. vlin = vlin ? vlin : vc->vc_scan_lines;
  1056. if (clin) {
  1057. if (ll) {
  1058. if (ll != vlin/clin) {
  1059. /* Parameters don't add up */
  1060. ret = -EINVAL;
  1061. break;
  1062. }
  1063. } else
  1064. ll = vlin/clin;
  1065. }
  1066. if (vcol && ccol) {
  1067. if (cc) {
  1068. if (cc != vcol/ccol) {
  1069. ret = -EINVAL;
  1070. break;
  1071. }
  1072. } else
  1073. cc = vcol/ccol;
  1074. }
  1075. if (clin > 32) {
  1076. ret = -EINVAL;
  1077. break;
  1078. }
  1079. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1080. if (!vc_cons[i].d)
  1081. continue;
  1082. console_lock();
  1083. if (vlin)
  1084. vc_cons[i].d->vc_scan_lines = vlin;
  1085. if (clin)
  1086. vc_cons[i].d->vc_font.height = clin;
  1087. vc_cons[i].d->vc_resize_user = 1;
  1088. vc_resize(vc_cons[i].d, cc, ll);
  1089. console_unlock();
  1090. }
  1091. break;
  1092. }
  1093. case PIO_FONT: {
  1094. if (!perm)
  1095. goto eperm;
  1096. op.op = KD_FONT_OP_SET;
  1097. op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
  1098. op.width = 8;
  1099. op.height = 0;
  1100. op.charcount = 256;
  1101. op.data = up;
  1102. ret = con_font_op(vc_cons[fg_console].d, &op);
  1103. break;
  1104. }
  1105. case GIO_FONT: {
  1106. op.op = KD_FONT_OP_GET;
  1107. op.flags = KD_FONT_FLAG_OLD;
  1108. op.width = 8;
  1109. op.height = 32;
  1110. op.charcount = 256;
  1111. op.data = up;
  1112. ret = con_font_op(vc_cons[fg_console].d, &op);
  1113. break;
  1114. }
  1115. case PIO_CMAP:
  1116. if (!perm)
  1117. ret = -EPERM;
  1118. else
  1119. ret = con_set_cmap(up);
  1120. break;
  1121. case GIO_CMAP:
  1122. ret = con_get_cmap(up);
  1123. break;
  1124. case PIO_FONTX:
  1125. case GIO_FONTX:
  1126. ret = do_fontx_ioctl(cmd, up, perm, &op);
  1127. break;
  1128. case PIO_FONTRESET:
  1129. {
  1130. if (!perm)
  1131. goto eperm;
  1132. #ifdef BROKEN_GRAPHICS_PROGRAMS
  1133. /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
  1134. font is not saved. */
  1135. ret = -ENOSYS;
  1136. break;
  1137. #else
  1138. {
  1139. op.op = KD_FONT_OP_SET_DEFAULT;
  1140. op.data = NULL;
  1141. ret = con_font_op(vc_cons[fg_console].d, &op);
  1142. if (ret)
  1143. break;
  1144. con_set_default_unimap(vc_cons[fg_console].d);
  1145. break;
  1146. }
  1147. #endif
  1148. }
  1149. case KDFONTOP: {
  1150. if (copy_from_user(&op, up, sizeof(op))) {
  1151. ret = -EFAULT;
  1152. break;
  1153. }
  1154. if (!perm && op.op != KD_FONT_OP_GET)
  1155. goto eperm;
  1156. ret = con_font_op(vc, &op);
  1157. if (ret)
  1158. break;
  1159. if (copy_to_user(up, &op, sizeof(op)))
  1160. ret = -EFAULT;
  1161. break;
  1162. }
  1163. case PIO_SCRNMAP:
  1164. if (!perm)
  1165. ret = -EPERM;
  1166. else
  1167. ret = con_set_trans_old(up);
  1168. break;
  1169. case GIO_SCRNMAP:
  1170. ret = con_get_trans_old(up);
  1171. break;
  1172. case PIO_UNISCRNMAP:
  1173. if (!perm)
  1174. ret = -EPERM;
  1175. else
  1176. ret = con_set_trans_new(up);
  1177. break;
  1178. case GIO_UNISCRNMAP:
  1179. ret = con_get_trans_new(up);
  1180. break;
  1181. case PIO_UNIMAPCLR:
  1182. { struct unimapinit ui;
  1183. if (!perm)
  1184. goto eperm;
  1185. ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
  1186. if (ret)
  1187. ret = -EFAULT;
  1188. else
  1189. con_clear_unimap(vc, &ui);
  1190. break;
  1191. }
  1192. case PIO_UNIMAP:
  1193. case GIO_UNIMAP:
  1194. ret = do_unimap_ioctl(cmd, up, perm, vc);
  1195. break;
  1196. case VT_LOCKSWITCH:
  1197. if (!capable(CAP_SYS_TTY_CONFIG))
  1198. goto eperm;
  1199. vt_dont_switch = 1;
  1200. break;
  1201. case VT_UNLOCKSWITCH:
  1202. if (!capable(CAP_SYS_TTY_CONFIG))
  1203. goto eperm;
  1204. vt_dont_switch = 0;
  1205. break;
  1206. case VT_GETHIFONTMASK:
  1207. ret = put_user(vc->vc_hi_font_mask,
  1208. (unsigned short __user *)arg);
  1209. break;
  1210. case VT_WAITEVENT:
  1211. ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
  1212. break;
  1213. default:
  1214. ret = -ENOIOCTLCMD;
  1215. }
  1216. out:
  1217. tty_unlock();
  1218. return ret;
  1219. eperm:
  1220. ret = -EPERM;
  1221. goto out;
  1222. }
  1223. void reset_vc(struct vc_data *vc)
  1224. {
  1225. vc->vc_mode = KD_TEXT;
  1226. kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
  1227. vc->vt_mode.mode = VT_AUTO;
  1228. vc->vt_mode.waitv = 0;
  1229. vc->vt_mode.relsig = 0;
  1230. vc->vt_mode.acqsig = 0;
  1231. vc->vt_mode.frsig = 0;
  1232. put_pid(vc->vt_pid);
  1233. vc->vt_pid = NULL;
  1234. vc->vt_newvt = -1;
  1235. if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
  1236. reset_palette(vc);
  1237. }
  1238. void vc_SAK(struct work_struct *work)
  1239. {
  1240. struct vc *vc_con =
  1241. container_of(work, struct vc, SAK_work);
  1242. struct vc_data *vc;
  1243. struct tty_struct *tty;
  1244. console_lock();
  1245. vc = vc_con->d;
  1246. if (vc) {
  1247. tty = vc->port.tty;
  1248. /*
  1249. * SAK should also work in all raw modes and reset
  1250. * them properly.
  1251. */
  1252. if (tty)
  1253. __do_SAK(tty);
  1254. reset_vc(vc);
  1255. }
  1256. console_unlock();
  1257. }
  1258. #ifdef CONFIG_COMPAT
  1259. struct compat_consolefontdesc {
  1260. unsigned short charcount; /* characters in font (256 or 512) */
  1261. unsigned short charheight; /* scan lines per character (1-32) */
  1262. compat_caddr_t chardata; /* font data in expanded form */
  1263. };
  1264. static inline int
  1265. compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
  1266. int perm, struct console_font_op *op)
  1267. {
  1268. struct compat_consolefontdesc cfdarg;
  1269. int i;
  1270. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
  1271. return -EFAULT;
  1272. switch (cmd) {
  1273. case PIO_FONTX:
  1274. if (!perm)
  1275. return -EPERM;
  1276. op->op = KD_FONT_OP_SET;
  1277. op->flags = KD_FONT_FLAG_OLD;
  1278. op->width = 8;
  1279. op->height = cfdarg.charheight;
  1280. op->charcount = cfdarg.charcount;
  1281. op->data = compat_ptr(cfdarg.chardata);
  1282. return con_font_op(vc_cons[fg_console].d, op);
  1283. case GIO_FONTX:
  1284. op->op = KD_FONT_OP_GET;
  1285. op->flags = KD_FONT_FLAG_OLD;
  1286. op->width = 8;
  1287. op->height = cfdarg.charheight;
  1288. op->charcount = cfdarg.charcount;
  1289. op->data = compat_ptr(cfdarg.chardata);
  1290. i = con_font_op(vc_cons[fg_console].d, op);
  1291. if (i)
  1292. return i;
  1293. cfdarg.charheight = op->height;
  1294. cfdarg.charcount = op->charcount;
  1295. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
  1296. return -EFAULT;
  1297. return 0;
  1298. }
  1299. return -EINVAL;
  1300. }
  1301. struct compat_console_font_op {
  1302. compat_uint_t op; /* operation code KD_FONT_OP_* */
  1303. compat_uint_t flags; /* KD_FONT_FLAG_* */
  1304. compat_uint_t width, height; /* font size */
  1305. compat_uint_t charcount;
  1306. compat_caddr_t data; /* font data with height fixed to 32 */
  1307. };
  1308. static inline int
  1309. compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
  1310. int perm, struct console_font_op *op, struct vc_data *vc)
  1311. {
  1312. int i;
  1313. if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
  1314. return -EFAULT;
  1315. if (!perm && op->op != KD_FONT_OP_GET)
  1316. return -EPERM;
  1317. op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
  1318. op->flags |= KD_FONT_FLAG_OLD;
  1319. i = con_font_op(vc, op);
  1320. if (i)
  1321. return i;
  1322. ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
  1323. if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
  1324. return -EFAULT;
  1325. return 0;
  1326. }
  1327. struct compat_unimapdesc {
  1328. unsigned short entry_ct;
  1329. compat_caddr_t entries;
  1330. };
  1331. static inline int
  1332. compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
  1333. int perm, struct vc_data *vc)
  1334. {
  1335. struct compat_unimapdesc tmp;
  1336. struct unipair __user *tmp_entries;
  1337. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  1338. return -EFAULT;
  1339. tmp_entries = compat_ptr(tmp.entries);
  1340. if (tmp_entries)
  1341. if (!access_ok(VERIFY_WRITE, tmp_entries,
  1342. tmp.entry_ct*sizeof(struct unipair)))
  1343. return -EFAULT;
  1344. switch (cmd) {
  1345. case PIO_UNIMAP:
  1346. if (!perm)
  1347. return -EPERM;
  1348. return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
  1349. case GIO_UNIMAP:
  1350. if (!perm && fg_console != vc->vc_num)
  1351. return -EPERM;
  1352. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
  1353. }
  1354. return 0;
  1355. }
  1356. long vt_compat_ioctl(struct tty_struct *tty,
  1357. unsigned int cmd, unsigned long arg)
  1358. {
  1359. struct vc_data *vc = tty->driver_data;
  1360. struct console_font_op op; /* used in multiple places here */
  1361. struct kbd_struct *kbd;
  1362. unsigned int console;
  1363. void __user *up = (void __user *)arg;
  1364. int perm;
  1365. int ret = 0;
  1366. console = vc->vc_num;
  1367. tty_lock();
  1368. if (!vc_cons_allocated(console)) { /* impossible? */
  1369. ret = -ENOIOCTLCMD;
  1370. goto out;
  1371. }
  1372. /*
  1373. * To have permissions to do most of the vt ioctls, we either have
  1374. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  1375. */
  1376. perm = 0;
  1377. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  1378. perm = 1;
  1379. kbd = kbd_table + console;
  1380. switch (cmd) {
  1381. /*
  1382. * these need special handlers for incompatible data structures
  1383. */
  1384. case PIO_FONTX:
  1385. case GIO_FONTX:
  1386. ret = compat_fontx_ioctl(cmd, up, perm, &op);
  1387. break;
  1388. case KDFONTOP:
  1389. ret = compat_kdfontop_ioctl(up, perm, &op, vc);
  1390. break;
  1391. case PIO_UNIMAP:
  1392. case GIO_UNIMAP:
  1393. ret = compat_unimap_ioctl(cmd, up, perm, vc);
  1394. break;
  1395. /*
  1396. * all these treat 'arg' as an integer
  1397. */
  1398. case KIOCSOUND:
  1399. case KDMKTONE:
  1400. #ifdef CONFIG_X86
  1401. case KDADDIO:
  1402. case KDDELIO:
  1403. #endif
  1404. case KDSETMODE:
  1405. case KDMAPDISP:
  1406. case KDUNMAPDISP:
  1407. case KDSKBMODE:
  1408. case KDSKBMETA:
  1409. case KDSKBLED:
  1410. case KDSETLED:
  1411. case KDSIGACCEPT:
  1412. case VT_ACTIVATE:
  1413. case VT_WAITACTIVE:
  1414. case VT_RELDISP:
  1415. case VT_DISALLOCATE:
  1416. case VT_RESIZE:
  1417. case VT_RESIZEX:
  1418. goto fallback;
  1419. /*
  1420. * the rest has a compatible data structure behind arg,
  1421. * but we have to convert it to a proper 64 bit pointer.
  1422. */
  1423. default:
  1424. arg = (unsigned long)compat_ptr(arg);
  1425. goto fallback;
  1426. }
  1427. out:
  1428. tty_unlock();
  1429. return ret;
  1430. fallback:
  1431. tty_unlock();
  1432. return vt_ioctl(tty, cmd, arg);
  1433. }
  1434. #endif /* CONFIG_COMPAT */
  1435. /*
  1436. * Performs the back end of a vt switch. Called under the console
  1437. * semaphore.
  1438. */
  1439. static void complete_change_console(struct vc_data *vc)
  1440. {
  1441. unsigned char old_vc_mode;
  1442. int old = fg_console;
  1443. last_console = fg_console;
  1444. /*
  1445. * If we're switching, we could be going from KD_GRAPHICS to
  1446. * KD_TEXT mode or vice versa, which means we need to blank or
  1447. * unblank the screen later.
  1448. */
  1449. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  1450. switch_screen(vc);
  1451. /*
  1452. * This can't appear below a successful kill_pid(). If it did,
  1453. * then the *blank_screen operation could occur while X, having
  1454. * received acqsig, is waking up on another processor. This
  1455. * condition can lead to overlapping accesses to the VGA range
  1456. * and the framebuffer (causing system lockups).
  1457. *
  1458. * To account for this we duplicate this code below only if the
  1459. * controlling process is gone and we've called reset_vc.
  1460. */
  1461. if (old_vc_mode != vc->vc_mode) {
  1462. if (vc->vc_mode == KD_TEXT)
  1463. do_unblank_screen(1);
  1464. else
  1465. do_blank_screen(1);
  1466. }
  1467. /*
  1468. * If this new console is under process control, send it a signal
  1469. * telling it that it has acquired. Also check if it has died and
  1470. * clean up (similar to logic employed in change_console())
  1471. */
  1472. if (vc->vt_mode.mode == VT_PROCESS) {
  1473. /*
  1474. * Send the signal as privileged - kill_pid() will
  1475. * tell us if the process has gone or something else
  1476. * is awry
  1477. */
  1478. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  1479. /*
  1480. * The controlling process has died, so we revert back to
  1481. * normal operation. In this case, we'll also change back
  1482. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1483. * but it saves the agony when the X server dies and the screen
  1484. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1485. * this outside of VT_PROCESS but there is no single process
  1486. * to account for and tracking tty count may be undesirable.
  1487. */
  1488. reset_vc(vc);
  1489. if (old_vc_mode != vc->vc_mode) {
  1490. if (vc->vc_mode == KD_TEXT)
  1491. do_unblank_screen(1);
  1492. else
  1493. do_blank_screen(1);
  1494. }
  1495. }
  1496. }
  1497. /*
  1498. * Wake anyone waiting for their VT to activate
  1499. */
  1500. vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
  1501. return;
  1502. }
  1503. /*
  1504. * Performs the front-end of a vt switch
  1505. */
  1506. void change_console(struct vc_data *new_vc)
  1507. {
  1508. struct vc_data *vc;
  1509. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1510. return;
  1511. /*
  1512. * If this vt is in process mode, then we need to handshake with
  1513. * that process before switching. Essentially, we store where that
  1514. * vt wants to switch to and wait for it to tell us when it's done
  1515. * (via VT_RELDISP ioctl).
  1516. *
  1517. * We also check to see if the controlling process still exists.
  1518. * If it doesn't, we reset this vt to auto mode and continue.
  1519. * This is a cheap way to track process control. The worst thing
  1520. * that can happen is: we send a signal to a process, it dies, and
  1521. * the switch gets "lost" waiting for a response; hopefully, the
  1522. * user will try again, we'll detect the process is gone (unless
  1523. * the user waits just the right amount of time :-) and revert the
  1524. * vt to auto control.
  1525. */
  1526. vc = vc_cons[fg_console].d;
  1527. if (vc->vt_mode.mode == VT_PROCESS) {
  1528. /*
  1529. * Send the signal as privileged - kill_pid() will
  1530. * tell us if the process has gone or something else
  1531. * is awry.
  1532. *
  1533. * We need to set vt_newvt *before* sending the signal or we
  1534. * have a race.
  1535. */
  1536. vc->vt_newvt = new_vc->vc_num;
  1537. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1538. /*
  1539. * It worked. Mark the vt to switch to and
  1540. * return. The process needs to send us a
  1541. * VT_RELDISP ioctl to complete the switch.
  1542. */
  1543. return;
  1544. }
  1545. /*
  1546. * The controlling process has died, so we revert back to
  1547. * normal operation. In this case, we'll also change back
  1548. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1549. * but it saves the agony when the X server dies and the screen
  1550. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1551. * this outside of VT_PROCESS but there is no single process
  1552. * to account for and tracking tty count may be undesirable.
  1553. */
  1554. reset_vc(vc);
  1555. /*
  1556. * Fall through to normal (VT_AUTO) handling of the switch...
  1557. */
  1558. }
  1559. /*
  1560. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1561. */
  1562. if (vc->vc_mode == KD_GRAPHICS)
  1563. return;
  1564. complete_change_console(new_vc);
  1565. }
  1566. /* Perform a kernel triggered VT switch for suspend/resume */
  1567. static int disable_vt_switch;
  1568. int vt_move_to_console(unsigned int vt, int alloc)
  1569. {
  1570. int prev;
  1571. console_lock();
  1572. /* Graphics mode - up to X */
  1573. if (disable_vt_switch) {
  1574. console_unlock();
  1575. return 0;
  1576. }
  1577. prev = fg_console;
  1578. if (alloc && vc_allocate(vt)) {
  1579. /* we can't have a free VC for now. Too bad,
  1580. * we don't want to mess the screen for now. */
  1581. console_unlock();
  1582. return -ENOSPC;
  1583. }
  1584. if (set_console(vt)) {
  1585. /*
  1586. * We're unable to switch to the SUSPEND_CONSOLE.
  1587. * Let the calling function know so it can decide
  1588. * what to do.
  1589. */
  1590. console_unlock();
  1591. return -EIO;
  1592. }
  1593. console_unlock();
  1594. tty_lock();
  1595. if (vt_waitactive(vt + 1)) {
  1596. pr_debug("Suspend: Can't switch VCs.");
  1597. tty_unlock();
  1598. return -EINTR;
  1599. }
  1600. tty_unlock();
  1601. return prev;
  1602. }
  1603. /*
  1604. * Normally during a suspend, we allocate a new console and switch to it.
  1605. * When we resume, we switch back to the original console. This switch
  1606. * can be slow, so on systems where the framebuffer can handle restoration
  1607. * of video registers anyways, there's little point in doing the console
  1608. * switch. This function allows you to disable it by passing it '0'.
  1609. */
  1610. void pm_set_vt_switch(int do_switch)
  1611. {
  1612. console_lock();
  1613. disable_vt_switch = !do_switch;
  1614. console_unlock();
  1615. }
  1616. EXPORT_SYMBOL(pm_set_vt_switch);