vt_ioctl.c 33 KB

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