vt_ioctl.c 33 KB

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