vt_ioctl.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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(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. ret = vt_waitactive(arg);
  601. break;
  602. /*
  603. * If a vt is under process control, the kernel will not switch to it
  604. * immediately, but postpone the operation until the process calls this
  605. * ioctl, allowing the switch to complete.
  606. *
  607. * According to the X sources this is the behavior:
  608. * 0: pending switch-from not OK
  609. * 1: pending switch-from OK
  610. * 2: completed switch-to OK
  611. */
  612. case VT_RELDISP:
  613. if (!perm)
  614. return -EPERM;
  615. console_lock();
  616. if (vc->vt_mode.mode != VT_PROCESS) {
  617. console_unlock();
  618. ret = -EINVAL;
  619. break;
  620. }
  621. /*
  622. * Switching-from response
  623. */
  624. if (vc->vt_newvt >= 0) {
  625. if (arg == 0)
  626. /*
  627. * Switch disallowed, so forget we were trying
  628. * to do it.
  629. */
  630. vc->vt_newvt = -1;
  631. else {
  632. /*
  633. * The current vt has been released, so
  634. * complete the switch.
  635. */
  636. int newvt;
  637. newvt = vc->vt_newvt;
  638. vc->vt_newvt = -1;
  639. ret = vc_allocate(newvt);
  640. if (ret) {
  641. console_unlock();
  642. break;
  643. }
  644. /*
  645. * When we actually do the console switch,
  646. * make sure we are atomic with respect to
  647. * other console switches..
  648. */
  649. complete_change_console(vc_cons[newvt].d);
  650. }
  651. } else {
  652. /*
  653. * Switched-to response
  654. */
  655. /*
  656. * If it's just an ACK, ignore it
  657. */
  658. if (arg != VT_ACKACQ)
  659. ret = -EINVAL;
  660. }
  661. console_unlock();
  662. break;
  663. /*
  664. * Disallocate memory associated to VT (but leave VT1)
  665. */
  666. case VT_DISALLOCATE:
  667. if (arg > MAX_NR_CONSOLES) {
  668. ret = -ENXIO;
  669. break;
  670. }
  671. if (arg == 0) {
  672. /* deallocate all unused consoles, but leave 0 */
  673. console_lock();
  674. for (i=1; i<MAX_NR_CONSOLES; i++)
  675. if (! VT_BUSY(i))
  676. vc_deallocate(i);
  677. console_unlock();
  678. } else {
  679. /* deallocate a single console, if possible */
  680. arg--;
  681. if (VT_BUSY(arg))
  682. ret = -EBUSY;
  683. else if (arg) { /* leave 0 */
  684. console_lock();
  685. vc_deallocate(arg);
  686. console_unlock();
  687. }
  688. }
  689. break;
  690. case VT_RESIZE:
  691. {
  692. struct vt_sizes __user *vtsizes = up;
  693. struct vc_data *vc;
  694. ushort ll,cc;
  695. if (!perm)
  696. return -EPERM;
  697. if (get_user(ll, &vtsizes->v_rows) ||
  698. get_user(cc, &vtsizes->v_cols))
  699. ret = -EFAULT;
  700. else {
  701. console_lock();
  702. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  703. vc = vc_cons[i].d;
  704. if (vc) {
  705. vc->vc_resize_user = 1;
  706. /* FIXME: review v tty lock */
  707. vc_resize(vc_cons[i].d, cc, ll);
  708. }
  709. }
  710. console_unlock();
  711. }
  712. break;
  713. }
  714. case VT_RESIZEX:
  715. {
  716. struct vt_consize __user *vtconsize = up;
  717. ushort ll,cc,vlin,clin,vcol,ccol;
  718. if (!perm)
  719. return -EPERM;
  720. if (!access_ok(VERIFY_READ, vtconsize,
  721. sizeof(struct vt_consize))) {
  722. ret = -EFAULT;
  723. break;
  724. }
  725. /* FIXME: Should check the copies properly */
  726. __get_user(ll, &vtconsize->v_rows);
  727. __get_user(cc, &vtconsize->v_cols);
  728. __get_user(vlin, &vtconsize->v_vlin);
  729. __get_user(clin, &vtconsize->v_clin);
  730. __get_user(vcol, &vtconsize->v_vcol);
  731. __get_user(ccol, &vtconsize->v_ccol);
  732. vlin = vlin ? vlin : vc->vc_scan_lines;
  733. if (clin) {
  734. if (ll) {
  735. if (ll != vlin/clin) {
  736. /* Parameters don't add up */
  737. ret = -EINVAL;
  738. break;
  739. }
  740. } else
  741. ll = vlin/clin;
  742. }
  743. if (vcol && ccol) {
  744. if (cc) {
  745. if (cc != vcol/ccol) {
  746. ret = -EINVAL;
  747. break;
  748. }
  749. } else
  750. cc = vcol/ccol;
  751. }
  752. if (clin > 32) {
  753. ret = -EINVAL;
  754. break;
  755. }
  756. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  757. if (!vc_cons[i].d)
  758. continue;
  759. console_lock();
  760. if (vlin)
  761. vc_cons[i].d->vc_scan_lines = vlin;
  762. if (clin)
  763. vc_cons[i].d->vc_font.height = clin;
  764. vc_cons[i].d->vc_resize_user = 1;
  765. vc_resize(vc_cons[i].d, cc, ll);
  766. console_unlock();
  767. }
  768. break;
  769. }
  770. case PIO_FONT: {
  771. if (!perm)
  772. return -EPERM;
  773. op.op = KD_FONT_OP_SET;
  774. op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
  775. op.width = 8;
  776. op.height = 0;
  777. op.charcount = 256;
  778. op.data = up;
  779. ret = con_font_op(vc_cons[fg_console].d, &op);
  780. break;
  781. }
  782. case GIO_FONT: {
  783. op.op = KD_FONT_OP_GET;
  784. op.flags = KD_FONT_FLAG_OLD;
  785. op.width = 8;
  786. op.height = 32;
  787. op.charcount = 256;
  788. op.data = up;
  789. ret = con_font_op(vc_cons[fg_console].d, &op);
  790. break;
  791. }
  792. case PIO_CMAP:
  793. if (!perm)
  794. ret = -EPERM;
  795. else
  796. ret = con_set_cmap(up);
  797. break;
  798. case GIO_CMAP:
  799. ret = con_get_cmap(up);
  800. break;
  801. case PIO_FONTX:
  802. case GIO_FONTX:
  803. ret = do_fontx_ioctl(cmd, up, perm, &op);
  804. break;
  805. case PIO_FONTRESET:
  806. {
  807. if (!perm)
  808. return -EPERM;
  809. #ifdef BROKEN_GRAPHICS_PROGRAMS
  810. /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
  811. font is not saved. */
  812. ret = -ENOSYS;
  813. break;
  814. #else
  815. {
  816. op.op = KD_FONT_OP_SET_DEFAULT;
  817. op.data = NULL;
  818. ret = con_font_op(vc_cons[fg_console].d, &op);
  819. if (ret)
  820. break;
  821. con_set_default_unimap(vc_cons[fg_console].d);
  822. break;
  823. }
  824. #endif
  825. }
  826. case KDFONTOP: {
  827. if (copy_from_user(&op, up, sizeof(op))) {
  828. ret = -EFAULT;
  829. break;
  830. }
  831. if (!perm && op.op != KD_FONT_OP_GET)
  832. return -EPERM;
  833. ret = con_font_op(vc, &op);
  834. if (ret)
  835. break;
  836. if (copy_to_user(up, &op, sizeof(op)))
  837. ret = -EFAULT;
  838. break;
  839. }
  840. case PIO_SCRNMAP:
  841. if (!perm)
  842. ret = -EPERM;
  843. else {
  844. tty_lock();
  845. ret = con_set_trans_old(up);
  846. tty_unlock();
  847. }
  848. break;
  849. case GIO_SCRNMAP:
  850. tty_lock();
  851. ret = con_get_trans_old(up);
  852. tty_unlock();
  853. break;
  854. case PIO_UNISCRNMAP:
  855. if (!perm)
  856. ret = -EPERM;
  857. else {
  858. tty_lock();
  859. ret = con_set_trans_new(up);
  860. tty_unlock();
  861. }
  862. break;
  863. case GIO_UNISCRNMAP:
  864. tty_lock();
  865. ret = con_get_trans_new(up);
  866. tty_unlock();
  867. break;
  868. case PIO_UNIMAPCLR:
  869. { struct unimapinit ui;
  870. if (!perm)
  871. return -EPERM;
  872. ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
  873. if (ret)
  874. ret = -EFAULT;
  875. else {
  876. tty_lock();
  877. con_clear_unimap(vc, &ui);
  878. tty_unlock();
  879. }
  880. break;
  881. }
  882. case PIO_UNIMAP:
  883. case GIO_UNIMAP:
  884. tty_lock();
  885. ret = do_unimap_ioctl(cmd, up, perm, vc);
  886. tty_unlock();
  887. break;
  888. case VT_LOCKSWITCH:
  889. if (!capable(CAP_SYS_TTY_CONFIG))
  890. return -EPERM;
  891. vt_dont_switch = 1;
  892. break;
  893. case VT_UNLOCKSWITCH:
  894. if (!capable(CAP_SYS_TTY_CONFIG))
  895. return -EPERM;
  896. vt_dont_switch = 0;
  897. break;
  898. case VT_GETHIFONTMASK:
  899. ret = put_user(vc->vc_hi_font_mask,
  900. (unsigned short __user *)arg);
  901. break;
  902. case VT_WAITEVENT:
  903. ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
  904. break;
  905. default:
  906. ret = -ENOIOCTLCMD;
  907. }
  908. out:
  909. return ret;
  910. }
  911. void reset_vc(struct vc_data *vc)
  912. {
  913. vc->vc_mode = KD_TEXT;
  914. vt_reset_unicode(vc->vc_num);
  915. vc->vt_mode.mode = VT_AUTO;
  916. vc->vt_mode.waitv = 0;
  917. vc->vt_mode.relsig = 0;
  918. vc->vt_mode.acqsig = 0;
  919. vc->vt_mode.frsig = 0;
  920. put_pid(vc->vt_pid);
  921. vc->vt_pid = NULL;
  922. vc->vt_newvt = -1;
  923. if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
  924. reset_palette(vc);
  925. }
  926. void vc_SAK(struct work_struct *work)
  927. {
  928. struct vc *vc_con =
  929. container_of(work, struct vc, SAK_work);
  930. struct vc_data *vc;
  931. struct tty_struct *tty;
  932. console_lock();
  933. vc = vc_con->d;
  934. if (vc) {
  935. /* FIXME: review tty ref counting */
  936. tty = vc->port.tty;
  937. /*
  938. * SAK should also work in all raw modes and reset
  939. * them properly.
  940. */
  941. if (tty)
  942. __do_SAK(tty);
  943. reset_vc(vc);
  944. }
  945. console_unlock();
  946. }
  947. #ifdef CONFIG_COMPAT
  948. struct compat_consolefontdesc {
  949. unsigned short charcount; /* characters in font (256 or 512) */
  950. unsigned short charheight; /* scan lines per character (1-32) */
  951. compat_caddr_t chardata; /* font data in expanded form */
  952. };
  953. static inline int
  954. compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
  955. int perm, struct console_font_op *op)
  956. {
  957. struct compat_consolefontdesc cfdarg;
  958. int i;
  959. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
  960. return -EFAULT;
  961. switch (cmd) {
  962. case PIO_FONTX:
  963. if (!perm)
  964. return -EPERM;
  965. op->op = KD_FONT_OP_SET;
  966. op->flags = KD_FONT_FLAG_OLD;
  967. op->width = 8;
  968. op->height = cfdarg.charheight;
  969. op->charcount = cfdarg.charcount;
  970. op->data = compat_ptr(cfdarg.chardata);
  971. return con_font_op(vc_cons[fg_console].d, op);
  972. case GIO_FONTX:
  973. op->op = KD_FONT_OP_GET;
  974. op->flags = KD_FONT_FLAG_OLD;
  975. op->width = 8;
  976. op->height = cfdarg.charheight;
  977. op->charcount = cfdarg.charcount;
  978. op->data = compat_ptr(cfdarg.chardata);
  979. i = con_font_op(vc_cons[fg_console].d, op);
  980. if (i)
  981. return i;
  982. cfdarg.charheight = op->height;
  983. cfdarg.charcount = op->charcount;
  984. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
  985. return -EFAULT;
  986. return 0;
  987. }
  988. return -EINVAL;
  989. }
  990. struct compat_console_font_op {
  991. compat_uint_t op; /* operation code KD_FONT_OP_* */
  992. compat_uint_t flags; /* KD_FONT_FLAG_* */
  993. compat_uint_t width, height; /* font size */
  994. compat_uint_t charcount;
  995. compat_caddr_t data; /* font data with height fixed to 32 */
  996. };
  997. static inline int
  998. compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
  999. int perm, struct console_font_op *op, struct vc_data *vc)
  1000. {
  1001. int i;
  1002. if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
  1003. return -EFAULT;
  1004. if (!perm && op->op != KD_FONT_OP_GET)
  1005. return -EPERM;
  1006. op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
  1007. i = con_font_op(vc, op);
  1008. if (i)
  1009. return i;
  1010. ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
  1011. if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
  1012. return -EFAULT;
  1013. return 0;
  1014. }
  1015. struct compat_unimapdesc {
  1016. unsigned short entry_ct;
  1017. compat_caddr_t entries;
  1018. };
  1019. static inline int
  1020. compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
  1021. int perm, struct vc_data *vc)
  1022. {
  1023. struct compat_unimapdesc tmp;
  1024. struct unipair __user *tmp_entries;
  1025. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  1026. return -EFAULT;
  1027. tmp_entries = compat_ptr(tmp.entries);
  1028. if (tmp_entries)
  1029. if (!access_ok(VERIFY_WRITE, tmp_entries,
  1030. tmp.entry_ct*sizeof(struct unipair)))
  1031. return -EFAULT;
  1032. switch (cmd) {
  1033. case PIO_UNIMAP:
  1034. if (!perm)
  1035. return -EPERM;
  1036. return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
  1037. case GIO_UNIMAP:
  1038. if (!perm && fg_console != vc->vc_num)
  1039. return -EPERM;
  1040. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
  1041. }
  1042. return 0;
  1043. }
  1044. long vt_compat_ioctl(struct tty_struct *tty,
  1045. unsigned int cmd, unsigned long arg)
  1046. {
  1047. struct vc_data *vc = tty->driver_data;
  1048. struct console_font_op op; /* used in multiple places here */
  1049. unsigned int console;
  1050. void __user *up = (void __user *)arg;
  1051. int perm;
  1052. int ret = 0;
  1053. console = vc->vc_num;
  1054. if (!vc_cons_allocated(console)) { /* impossible? */
  1055. ret = -ENOIOCTLCMD;
  1056. goto out;
  1057. }
  1058. /*
  1059. * To have permissions to do most of the vt ioctls, we either have
  1060. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  1061. */
  1062. perm = 0;
  1063. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  1064. perm = 1;
  1065. switch (cmd) {
  1066. /*
  1067. * these need special handlers for incompatible data structures
  1068. */
  1069. case PIO_FONTX:
  1070. case GIO_FONTX:
  1071. ret = compat_fontx_ioctl(cmd, up, perm, &op);
  1072. break;
  1073. case KDFONTOP:
  1074. ret = compat_kdfontop_ioctl(up, perm, &op, vc);
  1075. break;
  1076. case PIO_UNIMAP:
  1077. case GIO_UNIMAP:
  1078. tty_lock();
  1079. ret = compat_unimap_ioctl(cmd, up, perm, vc);
  1080. tty_unlock();
  1081. break;
  1082. /*
  1083. * all these treat 'arg' as an integer
  1084. */
  1085. case KIOCSOUND:
  1086. case KDMKTONE:
  1087. #ifdef CONFIG_X86
  1088. case KDADDIO:
  1089. case KDDELIO:
  1090. #endif
  1091. case KDSETMODE:
  1092. case KDMAPDISP:
  1093. case KDUNMAPDISP:
  1094. case KDSKBMODE:
  1095. case KDSKBMETA:
  1096. case KDSKBLED:
  1097. case KDSETLED:
  1098. case KDSIGACCEPT:
  1099. case VT_ACTIVATE:
  1100. case VT_WAITACTIVE:
  1101. case VT_RELDISP:
  1102. case VT_DISALLOCATE:
  1103. case VT_RESIZE:
  1104. case VT_RESIZEX:
  1105. goto fallback;
  1106. /*
  1107. * the rest has a compatible data structure behind arg,
  1108. * but we have to convert it to a proper 64 bit pointer.
  1109. */
  1110. default:
  1111. arg = (unsigned long)compat_ptr(arg);
  1112. goto fallback;
  1113. }
  1114. out:
  1115. return ret;
  1116. fallback:
  1117. return vt_ioctl(tty, cmd, arg);
  1118. }
  1119. #endif /* CONFIG_COMPAT */
  1120. /*
  1121. * Performs the back end of a vt switch. Called under the console
  1122. * semaphore.
  1123. */
  1124. static void complete_change_console(struct vc_data *vc)
  1125. {
  1126. unsigned char old_vc_mode;
  1127. int old = fg_console;
  1128. last_console = fg_console;
  1129. /*
  1130. * If we're switching, we could be going from KD_GRAPHICS to
  1131. * KD_TEXT mode or vice versa, which means we need to blank or
  1132. * unblank the screen later.
  1133. */
  1134. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  1135. switch_screen(vc);
  1136. /*
  1137. * This can't appear below a successful kill_pid(). If it did,
  1138. * then the *blank_screen operation could occur while X, having
  1139. * received acqsig, is waking up on another processor. This
  1140. * condition can lead to overlapping accesses to the VGA range
  1141. * and the framebuffer (causing system lockups).
  1142. *
  1143. * To account for this we duplicate this code below only if the
  1144. * controlling process is gone and we've called reset_vc.
  1145. */
  1146. if (old_vc_mode != vc->vc_mode) {
  1147. if (vc->vc_mode == KD_TEXT)
  1148. do_unblank_screen(1);
  1149. else
  1150. do_blank_screen(1);
  1151. }
  1152. /*
  1153. * If this new console is under process control, send it a signal
  1154. * telling it that it has acquired. Also check if it has died and
  1155. * clean up (similar to logic employed in change_console())
  1156. */
  1157. if (vc->vt_mode.mode == VT_PROCESS) {
  1158. /*
  1159. * Send the signal as privileged - kill_pid() will
  1160. * tell us if the process has gone or something else
  1161. * is awry
  1162. */
  1163. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  1164. /*
  1165. * The controlling process has died, so we revert back to
  1166. * normal operation. In this case, we'll also change back
  1167. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1168. * but it saves the agony when the X server dies and the screen
  1169. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1170. * this outside of VT_PROCESS but there is no single process
  1171. * to account for and tracking tty count may be undesirable.
  1172. */
  1173. reset_vc(vc);
  1174. if (old_vc_mode != vc->vc_mode) {
  1175. if (vc->vc_mode == KD_TEXT)
  1176. do_unblank_screen(1);
  1177. else
  1178. do_blank_screen(1);
  1179. }
  1180. }
  1181. }
  1182. /*
  1183. * Wake anyone waiting for their VT to activate
  1184. */
  1185. vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
  1186. return;
  1187. }
  1188. /*
  1189. * Performs the front-end of a vt switch
  1190. */
  1191. void change_console(struct vc_data *new_vc)
  1192. {
  1193. struct vc_data *vc;
  1194. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1195. return;
  1196. /*
  1197. * If this vt is in process mode, then we need to handshake with
  1198. * that process before switching. Essentially, we store where that
  1199. * vt wants to switch to and wait for it to tell us when it's done
  1200. * (via VT_RELDISP ioctl).
  1201. *
  1202. * We also check to see if the controlling process still exists.
  1203. * If it doesn't, we reset this vt to auto mode and continue.
  1204. * This is a cheap way to track process control. The worst thing
  1205. * that can happen is: we send a signal to a process, it dies, and
  1206. * the switch gets "lost" waiting for a response; hopefully, the
  1207. * user will try again, we'll detect the process is gone (unless
  1208. * the user waits just the right amount of time :-) and revert the
  1209. * vt to auto control.
  1210. */
  1211. vc = vc_cons[fg_console].d;
  1212. if (vc->vt_mode.mode == VT_PROCESS) {
  1213. /*
  1214. * Send the signal as privileged - kill_pid() will
  1215. * tell us if the process has gone or something else
  1216. * is awry.
  1217. *
  1218. * We need to set vt_newvt *before* sending the signal or we
  1219. * have a race.
  1220. */
  1221. vc->vt_newvt = new_vc->vc_num;
  1222. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1223. /*
  1224. * It worked. Mark the vt to switch to and
  1225. * return. The process needs to send us a
  1226. * VT_RELDISP ioctl to complete the switch.
  1227. */
  1228. return;
  1229. }
  1230. /*
  1231. * The controlling process has died, so we revert back to
  1232. * normal operation. In this case, we'll also change back
  1233. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1234. * but it saves the agony when the X server dies and the screen
  1235. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1236. * this outside of VT_PROCESS but there is no single process
  1237. * to account for and tracking tty count may be undesirable.
  1238. */
  1239. reset_vc(vc);
  1240. /*
  1241. * Fall through to normal (VT_AUTO) handling of the switch...
  1242. */
  1243. }
  1244. /*
  1245. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1246. */
  1247. if (vc->vc_mode == KD_GRAPHICS)
  1248. return;
  1249. complete_change_console(new_vc);
  1250. }
  1251. /* Perform a kernel triggered VT switch for suspend/resume */
  1252. static int disable_vt_switch;
  1253. int vt_move_to_console(unsigned int vt, int alloc)
  1254. {
  1255. int prev;
  1256. console_lock();
  1257. /* Graphics mode - up to X */
  1258. if (disable_vt_switch) {
  1259. console_unlock();
  1260. return 0;
  1261. }
  1262. prev = fg_console;
  1263. if (alloc && vc_allocate(vt)) {
  1264. /* we can't have a free VC for now. Too bad,
  1265. * we don't want to mess the screen for now. */
  1266. console_unlock();
  1267. return -ENOSPC;
  1268. }
  1269. if (set_console(vt)) {
  1270. /*
  1271. * We're unable to switch to the SUSPEND_CONSOLE.
  1272. * Let the calling function know so it can decide
  1273. * what to do.
  1274. */
  1275. console_unlock();
  1276. return -EIO;
  1277. }
  1278. console_unlock();
  1279. if (vt_waitactive(vt + 1)) {
  1280. pr_debug("Suspend: Can't switch VCs.");
  1281. return -EINTR;
  1282. }
  1283. return prev;
  1284. }
  1285. /*
  1286. * Normally during a suspend, we allocate a new console and switch to it.
  1287. * When we resume, we switch back to the original console. This switch
  1288. * can be slow, so on systems where the framebuffer can handle restoration
  1289. * of video registers anyways, there's little point in doing the console
  1290. * switch. This function allows you to disable it by passing it '0'.
  1291. */
  1292. void pm_set_vt_switch(int do_switch)
  1293. {
  1294. console_lock();
  1295. disable_vt_switch = !do_switch;
  1296. console_unlock();
  1297. }
  1298. EXPORT_SYMBOL(pm_set_vt_switch);