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