vt_ioctl.c 32 KB

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