vt_ioctl.c 40 KB

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