vt_ioctl.c 33 KB

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