vt_ioctl.c 40 KB

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