vt_ioctl.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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_tty(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. unsigned int uival;
  455. void __user *up = (void __user *)arg;
  456. int i, perm;
  457. int ret = 0;
  458. console = vc->vc_num;
  459. tty_lock();
  460. if (!vc_cons_allocated(console)) { /* impossible? */
  461. ret = -ENOIOCTLCMD;
  462. goto out;
  463. }
  464. /*
  465. * To have permissions to do most of the vt ioctls, we either have
  466. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  467. */
  468. perm = 0;
  469. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  470. perm = 1;
  471. kbd = kbd_table + console;
  472. switch (cmd) {
  473. case TIOCLINUX:
  474. ret = tioclinux(tty, arg);
  475. break;
  476. case KIOCSOUND:
  477. if (!perm)
  478. goto eperm;
  479. /*
  480. * The use of PIT_TICK_RATE is historic, it used to be
  481. * the platform-dependent CLOCK_TICK_RATE between 2.6.12
  482. * and 2.6.36, which was a minor but unfortunate ABI
  483. * change.
  484. */
  485. if (arg)
  486. arg = PIT_TICK_RATE / arg;
  487. kd_mksound(arg, 0);
  488. break;
  489. case KDMKTONE:
  490. if (!perm)
  491. goto eperm;
  492. {
  493. unsigned int ticks, count;
  494. /*
  495. * Generate the tone for the appropriate number of ticks.
  496. * If the time is zero, turn off sound ourselves.
  497. */
  498. ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
  499. count = ticks ? (arg & 0xffff) : 0;
  500. if (count)
  501. count = PIT_TICK_RATE / count;
  502. kd_mksound(count, ticks);
  503. break;
  504. }
  505. case KDGKBTYPE:
  506. /*
  507. * this is naive.
  508. */
  509. ucval = KB_101;
  510. goto setchar;
  511. /*
  512. * These cannot be implemented on any machine that implements
  513. * ioperm() in user level (such as Alpha PCs) or not at all.
  514. *
  515. * XXX: you should never use these, just call ioperm directly..
  516. */
  517. #ifdef CONFIG_X86
  518. case KDADDIO:
  519. case KDDELIO:
  520. /*
  521. * KDADDIO and KDDELIO may be able to add ports beyond what
  522. * we reject here, but to be safe...
  523. */
  524. if (arg < GPFIRST || arg > GPLAST) {
  525. ret = -EINVAL;
  526. break;
  527. }
  528. ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
  529. break;
  530. case KDENABIO:
  531. case KDDISABIO:
  532. ret = sys_ioperm(GPFIRST, GPNUM,
  533. (cmd == KDENABIO)) ? -ENXIO : 0;
  534. break;
  535. #endif
  536. /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
  537. case KDKBDREP:
  538. {
  539. struct kbd_repeat kbrep;
  540. if (!capable(CAP_SYS_TTY_CONFIG))
  541. goto eperm;
  542. if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
  543. ret = -EFAULT;
  544. break;
  545. }
  546. ret = kbd_rate(&kbrep);
  547. if (ret)
  548. break;
  549. if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
  550. ret = -EFAULT;
  551. break;
  552. }
  553. case KDSETMODE:
  554. /*
  555. * currently, setting the mode from KD_TEXT to KD_GRAPHICS
  556. * doesn't do a whole lot. i'm not sure if it should do any
  557. * restoration of modes or what...
  558. *
  559. * XXX It should at least call into the driver, fbdev's definitely
  560. * need to restore their engine state. --BenH
  561. */
  562. if (!perm)
  563. goto eperm;
  564. switch (arg) {
  565. case KD_GRAPHICS:
  566. break;
  567. case KD_TEXT0:
  568. case KD_TEXT1:
  569. arg = KD_TEXT;
  570. case KD_TEXT:
  571. break;
  572. default:
  573. ret = -EINVAL;
  574. goto out;
  575. }
  576. if (vc->vc_mode == (unsigned char) arg)
  577. break;
  578. vc->vc_mode = (unsigned char) arg;
  579. if (console != fg_console)
  580. break;
  581. /*
  582. * explicitly blank/unblank the screen if switching modes
  583. */
  584. acquire_console_sem();
  585. if (arg == KD_TEXT)
  586. do_unblank_screen(1);
  587. else
  588. do_blank_screen(1);
  589. release_console_sem();
  590. break;
  591. case KDGETMODE:
  592. uival = vc->vc_mode;
  593. goto setint;
  594. case KDMAPDISP:
  595. case KDUNMAPDISP:
  596. /*
  597. * these work like a combination of mmap and KDENABIO.
  598. * this could be easily finished.
  599. */
  600. ret = -EINVAL;
  601. break;
  602. case KDSKBMODE:
  603. if (!perm)
  604. goto eperm;
  605. switch(arg) {
  606. case K_RAW:
  607. kbd->kbdmode = VC_RAW;
  608. break;
  609. case K_MEDIUMRAW:
  610. kbd->kbdmode = VC_MEDIUMRAW;
  611. break;
  612. case K_XLATE:
  613. kbd->kbdmode = VC_XLATE;
  614. compute_shiftstate();
  615. break;
  616. case K_UNICODE:
  617. kbd->kbdmode = VC_UNICODE;
  618. compute_shiftstate();
  619. break;
  620. default:
  621. ret = -EINVAL;
  622. goto out;
  623. }
  624. tty_ldisc_flush(tty);
  625. break;
  626. case KDGKBMODE:
  627. uival = ((kbd->kbdmode == VC_RAW) ? K_RAW :
  628. (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
  629. (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
  630. K_XLATE);
  631. goto setint;
  632. /* this could be folded into KDSKBMODE, but for compatibility
  633. reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
  634. case KDSKBMETA:
  635. switch(arg) {
  636. case K_METABIT:
  637. clr_vc_kbd_mode(kbd, VC_META);
  638. break;
  639. case K_ESCPREFIX:
  640. set_vc_kbd_mode(kbd, VC_META);
  641. break;
  642. default:
  643. ret = -EINVAL;
  644. }
  645. break;
  646. case KDGKBMETA:
  647. uival = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
  648. setint:
  649. ret = put_user(uival, (int __user *)arg);
  650. break;
  651. case KDGETKEYCODE:
  652. case KDSETKEYCODE:
  653. if(!capable(CAP_SYS_TTY_CONFIG))
  654. perm = 0;
  655. ret = do_kbkeycode_ioctl(cmd, up, perm);
  656. break;
  657. case KDGKBENT:
  658. case KDSKBENT:
  659. ret = do_kdsk_ioctl(cmd, up, perm, kbd);
  660. break;
  661. case KDGKBSENT:
  662. case KDSKBSENT:
  663. ret = do_kdgkb_ioctl(cmd, up, perm);
  664. break;
  665. case KDGKBDIACR:
  666. {
  667. struct kbdiacrs __user *a = up;
  668. struct kbdiacr diacr;
  669. int i;
  670. if (put_user(accent_table_size, &a->kb_cnt)) {
  671. ret = -EFAULT;
  672. break;
  673. }
  674. for (i = 0; i < accent_table_size; i++) {
  675. diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
  676. diacr.base = conv_uni_to_8bit(accent_table[i].base);
  677. diacr.result = conv_uni_to_8bit(accent_table[i].result);
  678. if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
  679. ret = -EFAULT;
  680. break;
  681. }
  682. }
  683. break;
  684. }
  685. case KDGKBDIACRUC:
  686. {
  687. struct kbdiacrsuc __user *a = up;
  688. if (put_user(accent_table_size, &a->kb_cnt))
  689. ret = -EFAULT;
  690. else if (copy_to_user(a->kbdiacruc, accent_table,
  691. accent_table_size*sizeof(struct kbdiacruc)))
  692. ret = -EFAULT;
  693. break;
  694. }
  695. case KDSKBDIACR:
  696. {
  697. struct kbdiacrs __user *a = up;
  698. struct kbdiacr diacr;
  699. unsigned int ct;
  700. int i;
  701. if (!perm)
  702. goto eperm;
  703. if (get_user(ct,&a->kb_cnt)) {
  704. ret = -EFAULT;
  705. break;
  706. }
  707. if (ct >= MAX_DIACR) {
  708. ret = -EINVAL;
  709. break;
  710. }
  711. accent_table_size = ct;
  712. for (i = 0; i < ct; i++) {
  713. if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
  714. ret = -EFAULT;
  715. break;
  716. }
  717. accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
  718. accent_table[i].base = conv_8bit_to_uni(diacr.base);
  719. accent_table[i].result = conv_8bit_to_uni(diacr.result);
  720. }
  721. break;
  722. }
  723. case KDSKBDIACRUC:
  724. {
  725. struct kbdiacrsuc __user *a = up;
  726. unsigned int ct;
  727. if (!perm)
  728. goto eperm;
  729. if (get_user(ct,&a->kb_cnt)) {
  730. ret = -EFAULT;
  731. break;
  732. }
  733. if (ct >= MAX_DIACR) {
  734. ret = -EINVAL;
  735. break;
  736. }
  737. accent_table_size = ct;
  738. if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
  739. ret = -EFAULT;
  740. break;
  741. }
  742. /* the ioctls below read/set the flags usually shown in the leds */
  743. /* don't use them - they will go away without warning */
  744. case KDGKBLED:
  745. ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
  746. goto setchar;
  747. case KDSKBLED:
  748. if (!perm)
  749. goto eperm;
  750. if (arg & ~0x77) {
  751. ret = -EINVAL;
  752. break;
  753. }
  754. kbd->ledflagstate = (arg & 7);
  755. kbd->default_ledflagstate = ((arg >> 4) & 7);
  756. set_leds();
  757. break;
  758. /* the ioctls below only set the lights, not the functions */
  759. /* for those, see KDGKBLED and KDSKBLED above */
  760. case KDGETLED:
  761. ucval = getledstate();
  762. setchar:
  763. ret = put_user(ucval, (char __user *)arg);
  764. break;
  765. case KDSETLED:
  766. if (!perm)
  767. goto eperm;
  768. setledstate(kbd, arg);
  769. break;
  770. /*
  771. * A process can indicate its willingness to accept signals
  772. * generated by pressing an appropriate key combination.
  773. * Thus, one can have a daemon that e.g. spawns a new console
  774. * upon a keypress and then changes to it.
  775. * See also the kbrequest field of inittab(5).
  776. */
  777. case KDSIGACCEPT:
  778. {
  779. if (!perm || !capable(CAP_KILL))
  780. goto eperm;
  781. if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
  782. ret = -EINVAL;
  783. else {
  784. spin_lock_irq(&vt_spawn_con.lock);
  785. put_pid(vt_spawn_con.pid);
  786. vt_spawn_con.pid = get_pid(task_pid(current));
  787. vt_spawn_con.sig = arg;
  788. spin_unlock_irq(&vt_spawn_con.lock);
  789. }
  790. break;
  791. }
  792. case VT_SETMODE:
  793. {
  794. struct vt_mode tmp;
  795. if (!perm)
  796. goto eperm;
  797. if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
  798. ret = -EFAULT;
  799. goto out;
  800. }
  801. if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
  802. ret = -EINVAL;
  803. goto out;
  804. }
  805. acquire_console_sem();
  806. vc->vt_mode = tmp;
  807. /* the frsig is ignored, so we set it to 0 */
  808. vc->vt_mode.frsig = 0;
  809. put_pid(vc->vt_pid);
  810. vc->vt_pid = get_pid(task_pid(current));
  811. /* no switch is required -- saw@shade.msu.ru */
  812. vc->vt_newvt = -1;
  813. release_console_sem();
  814. break;
  815. }
  816. case VT_GETMODE:
  817. {
  818. struct vt_mode tmp;
  819. int rc;
  820. acquire_console_sem();
  821. memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
  822. release_console_sem();
  823. rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
  824. if (rc)
  825. ret = -EFAULT;
  826. break;
  827. }
  828. /*
  829. * Returns global vt state. Note that VT 0 is always open, since
  830. * it's an alias for the current VT, and people can't use it here.
  831. * We cannot return state for more than 16 VTs, since v_state is short.
  832. */
  833. case VT_GETSTATE:
  834. {
  835. struct vt_stat __user *vtstat = up;
  836. unsigned short state, mask;
  837. if (put_user(fg_console + 1, &vtstat->v_active))
  838. ret = -EFAULT;
  839. else {
  840. state = 1; /* /dev/tty0 is always open */
  841. for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
  842. ++i, mask <<= 1)
  843. if (VT_IS_IN_USE(i))
  844. state |= mask;
  845. ret = put_user(state, &vtstat->v_state);
  846. }
  847. break;
  848. }
  849. /*
  850. * Returns the first available (non-opened) console.
  851. */
  852. case VT_OPENQRY:
  853. for (i = 0; i < MAX_NR_CONSOLES; ++i)
  854. if (! VT_IS_IN_USE(i))
  855. break;
  856. uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
  857. goto setint;
  858. /*
  859. * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
  860. * with num >= 1 (switches to vt 0, our console, are not allowed, just
  861. * to preserve sanity).
  862. */
  863. case VT_ACTIVATE:
  864. if (!perm)
  865. goto eperm;
  866. if (arg == 0 || arg > MAX_NR_CONSOLES)
  867. ret = -ENXIO;
  868. else {
  869. arg--;
  870. acquire_console_sem();
  871. ret = vc_allocate(arg);
  872. release_console_sem();
  873. if (ret)
  874. break;
  875. set_console(arg);
  876. }
  877. break;
  878. case VT_SETACTIVATE:
  879. {
  880. struct vt_setactivate vsa;
  881. if (!perm)
  882. goto eperm;
  883. if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
  884. sizeof(struct vt_setactivate))) {
  885. ret = -EFAULT;
  886. goto out;
  887. }
  888. if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
  889. ret = -ENXIO;
  890. else {
  891. vsa.console--;
  892. acquire_console_sem();
  893. ret = vc_allocate(vsa.console);
  894. if (ret == 0) {
  895. struct vc_data *nvc;
  896. /* This is safe providing we don't drop the
  897. console sem between vc_allocate and
  898. finishing referencing nvc */
  899. nvc = vc_cons[vsa.console].d;
  900. nvc->vt_mode = vsa.mode;
  901. nvc->vt_mode.frsig = 0;
  902. put_pid(nvc->vt_pid);
  903. nvc->vt_pid = get_pid(task_pid(current));
  904. }
  905. release_console_sem();
  906. if (ret)
  907. break;
  908. /* Commence switch and lock */
  909. set_console(arg);
  910. }
  911. }
  912. /*
  913. * wait until the specified VT has been activated
  914. */
  915. case VT_WAITACTIVE:
  916. if (!perm)
  917. goto eperm;
  918. if (arg == 0 || arg > MAX_NR_CONSOLES)
  919. ret = -ENXIO;
  920. else
  921. ret = vt_waitactive(arg);
  922. break;
  923. /*
  924. * If a vt is under process control, the kernel will not switch to it
  925. * immediately, but postpone the operation until the process calls this
  926. * ioctl, allowing the switch to complete.
  927. *
  928. * According to the X sources this is the behavior:
  929. * 0: pending switch-from not OK
  930. * 1: pending switch-from OK
  931. * 2: completed switch-to OK
  932. */
  933. case VT_RELDISP:
  934. if (!perm)
  935. goto eperm;
  936. if (vc->vt_mode.mode != VT_PROCESS) {
  937. ret = -EINVAL;
  938. break;
  939. }
  940. /*
  941. * Switching-from response
  942. */
  943. acquire_console_sem();
  944. if (vc->vt_newvt >= 0) {
  945. if (arg == 0)
  946. /*
  947. * Switch disallowed, so forget we were trying
  948. * to do it.
  949. */
  950. vc->vt_newvt = -1;
  951. else {
  952. /*
  953. * The current vt has been released, so
  954. * complete the switch.
  955. */
  956. int newvt;
  957. newvt = vc->vt_newvt;
  958. vc->vt_newvt = -1;
  959. ret = vc_allocate(newvt);
  960. if (ret) {
  961. release_console_sem();
  962. break;
  963. }
  964. /*
  965. * When we actually do the console switch,
  966. * make sure we are atomic with respect to
  967. * other console switches..
  968. */
  969. complete_change_console(vc_cons[newvt].d);
  970. }
  971. } else {
  972. /*
  973. * Switched-to response
  974. */
  975. /*
  976. * If it's just an ACK, ignore it
  977. */
  978. if (arg != VT_ACKACQ)
  979. ret = -EINVAL;
  980. }
  981. release_console_sem();
  982. break;
  983. /*
  984. * Disallocate memory associated to VT (but leave VT1)
  985. */
  986. case VT_DISALLOCATE:
  987. if (arg > MAX_NR_CONSOLES) {
  988. ret = -ENXIO;
  989. break;
  990. }
  991. if (arg == 0) {
  992. /* deallocate all unused consoles, but leave 0 */
  993. acquire_console_sem();
  994. for (i=1; i<MAX_NR_CONSOLES; i++)
  995. if (! VT_BUSY(i))
  996. vc_deallocate(i);
  997. release_console_sem();
  998. } else {
  999. /* deallocate a single console, if possible */
  1000. arg--;
  1001. if (VT_BUSY(arg))
  1002. ret = -EBUSY;
  1003. else if (arg) { /* leave 0 */
  1004. acquire_console_sem();
  1005. vc_deallocate(arg);
  1006. release_console_sem();
  1007. }
  1008. }
  1009. break;
  1010. case VT_RESIZE:
  1011. {
  1012. struct vt_sizes __user *vtsizes = up;
  1013. struct vc_data *vc;
  1014. ushort ll,cc;
  1015. if (!perm)
  1016. goto eperm;
  1017. if (get_user(ll, &vtsizes->v_rows) ||
  1018. get_user(cc, &vtsizes->v_cols))
  1019. ret = -EFAULT;
  1020. else {
  1021. acquire_console_sem();
  1022. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1023. vc = vc_cons[i].d;
  1024. if (vc) {
  1025. vc->vc_resize_user = 1;
  1026. vc_resize(vc_cons[i].d, cc, ll);
  1027. }
  1028. }
  1029. release_console_sem();
  1030. }
  1031. break;
  1032. }
  1033. case VT_RESIZEX:
  1034. {
  1035. struct vt_consize __user *vtconsize = up;
  1036. ushort ll,cc,vlin,clin,vcol,ccol;
  1037. if (!perm)
  1038. goto eperm;
  1039. if (!access_ok(VERIFY_READ, vtconsize,
  1040. sizeof(struct vt_consize))) {
  1041. ret = -EFAULT;
  1042. break;
  1043. }
  1044. /* FIXME: Should check the copies properly */
  1045. __get_user(ll, &vtconsize->v_rows);
  1046. __get_user(cc, &vtconsize->v_cols);
  1047. __get_user(vlin, &vtconsize->v_vlin);
  1048. __get_user(clin, &vtconsize->v_clin);
  1049. __get_user(vcol, &vtconsize->v_vcol);
  1050. __get_user(ccol, &vtconsize->v_ccol);
  1051. vlin = vlin ? vlin : vc->vc_scan_lines;
  1052. if (clin) {
  1053. if (ll) {
  1054. if (ll != vlin/clin) {
  1055. /* Parameters don't add up */
  1056. ret = -EINVAL;
  1057. break;
  1058. }
  1059. } else
  1060. ll = vlin/clin;
  1061. }
  1062. if (vcol && ccol) {
  1063. if (cc) {
  1064. if (cc != vcol/ccol) {
  1065. ret = -EINVAL;
  1066. break;
  1067. }
  1068. } else
  1069. cc = vcol/ccol;
  1070. }
  1071. if (clin > 32) {
  1072. ret = -EINVAL;
  1073. break;
  1074. }
  1075. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1076. if (!vc_cons[i].d)
  1077. continue;
  1078. acquire_console_sem();
  1079. if (vlin)
  1080. vc_cons[i].d->vc_scan_lines = vlin;
  1081. if (clin)
  1082. vc_cons[i].d->vc_font.height = clin;
  1083. vc_cons[i].d->vc_resize_user = 1;
  1084. vc_resize(vc_cons[i].d, cc, ll);
  1085. release_console_sem();
  1086. }
  1087. break;
  1088. }
  1089. case PIO_FONT: {
  1090. if (!perm)
  1091. goto eperm;
  1092. op.op = KD_FONT_OP_SET;
  1093. op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
  1094. op.width = 8;
  1095. op.height = 0;
  1096. op.charcount = 256;
  1097. op.data = up;
  1098. ret = con_font_op(vc_cons[fg_console].d, &op);
  1099. break;
  1100. }
  1101. case GIO_FONT: {
  1102. op.op = KD_FONT_OP_GET;
  1103. op.flags = KD_FONT_FLAG_OLD;
  1104. op.width = 8;
  1105. op.height = 32;
  1106. op.charcount = 256;
  1107. op.data = up;
  1108. ret = con_font_op(vc_cons[fg_console].d, &op);
  1109. break;
  1110. }
  1111. case PIO_CMAP:
  1112. if (!perm)
  1113. ret = -EPERM;
  1114. else
  1115. ret = con_set_cmap(up);
  1116. break;
  1117. case GIO_CMAP:
  1118. ret = con_get_cmap(up);
  1119. break;
  1120. case PIO_FONTX:
  1121. case GIO_FONTX:
  1122. ret = do_fontx_ioctl(cmd, up, perm, &op);
  1123. break;
  1124. case PIO_FONTRESET:
  1125. {
  1126. if (!perm)
  1127. goto eperm;
  1128. #ifdef BROKEN_GRAPHICS_PROGRAMS
  1129. /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
  1130. font is not saved. */
  1131. ret = -ENOSYS;
  1132. break;
  1133. #else
  1134. {
  1135. op.op = KD_FONT_OP_SET_DEFAULT;
  1136. op.data = NULL;
  1137. ret = con_font_op(vc_cons[fg_console].d, &op);
  1138. if (ret)
  1139. break;
  1140. con_set_default_unimap(vc_cons[fg_console].d);
  1141. break;
  1142. }
  1143. #endif
  1144. }
  1145. case KDFONTOP: {
  1146. if (copy_from_user(&op, up, sizeof(op))) {
  1147. ret = -EFAULT;
  1148. break;
  1149. }
  1150. if (!perm && op.op != KD_FONT_OP_GET)
  1151. goto eperm;
  1152. ret = con_font_op(vc, &op);
  1153. if (ret)
  1154. break;
  1155. if (copy_to_user(up, &op, sizeof(op)))
  1156. ret = -EFAULT;
  1157. break;
  1158. }
  1159. case PIO_SCRNMAP:
  1160. if (!perm)
  1161. ret = -EPERM;
  1162. else
  1163. ret = con_set_trans_old(up);
  1164. break;
  1165. case GIO_SCRNMAP:
  1166. ret = con_get_trans_old(up);
  1167. break;
  1168. case PIO_UNISCRNMAP:
  1169. if (!perm)
  1170. ret = -EPERM;
  1171. else
  1172. ret = con_set_trans_new(up);
  1173. break;
  1174. case GIO_UNISCRNMAP:
  1175. ret = con_get_trans_new(up);
  1176. break;
  1177. case PIO_UNIMAPCLR:
  1178. { struct unimapinit ui;
  1179. if (!perm)
  1180. goto eperm;
  1181. ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
  1182. if (ret)
  1183. ret = -EFAULT;
  1184. else
  1185. con_clear_unimap(vc, &ui);
  1186. break;
  1187. }
  1188. case PIO_UNIMAP:
  1189. case GIO_UNIMAP:
  1190. ret = do_unimap_ioctl(cmd, up, perm, vc);
  1191. break;
  1192. case VT_LOCKSWITCH:
  1193. if (!capable(CAP_SYS_TTY_CONFIG))
  1194. goto eperm;
  1195. vt_dont_switch = 1;
  1196. break;
  1197. case VT_UNLOCKSWITCH:
  1198. if (!capable(CAP_SYS_TTY_CONFIG))
  1199. goto eperm;
  1200. vt_dont_switch = 0;
  1201. break;
  1202. case VT_GETHIFONTMASK:
  1203. ret = put_user(vc->vc_hi_font_mask,
  1204. (unsigned short __user *)arg);
  1205. break;
  1206. case VT_WAITEVENT:
  1207. ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
  1208. break;
  1209. default:
  1210. ret = -ENOIOCTLCMD;
  1211. }
  1212. out:
  1213. tty_unlock();
  1214. return ret;
  1215. eperm:
  1216. ret = -EPERM;
  1217. goto out;
  1218. }
  1219. void reset_vc(struct vc_data *vc)
  1220. {
  1221. vc->vc_mode = KD_TEXT;
  1222. kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
  1223. vc->vt_mode.mode = VT_AUTO;
  1224. vc->vt_mode.waitv = 0;
  1225. vc->vt_mode.relsig = 0;
  1226. vc->vt_mode.acqsig = 0;
  1227. vc->vt_mode.frsig = 0;
  1228. put_pid(vc->vt_pid);
  1229. vc->vt_pid = NULL;
  1230. vc->vt_newvt = -1;
  1231. if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
  1232. reset_palette(vc);
  1233. }
  1234. void vc_SAK(struct work_struct *work)
  1235. {
  1236. struct vc *vc_con =
  1237. container_of(work, struct vc, SAK_work);
  1238. struct vc_data *vc;
  1239. struct tty_struct *tty;
  1240. acquire_console_sem();
  1241. vc = vc_con->d;
  1242. if (vc) {
  1243. tty = vc->port.tty;
  1244. /*
  1245. * SAK should also work in all raw modes and reset
  1246. * them properly.
  1247. */
  1248. if (tty)
  1249. __do_SAK(tty);
  1250. reset_vc(vc);
  1251. }
  1252. release_console_sem();
  1253. }
  1254. #ifdef CONFIG_COMPAT
  1255. struct compat_consolefontdesc {
  1256. unsigned short charcount; /* characters in font (256 or 512) */
  1257. unsigned short charheight; /* scan lines per character (1-32) */
  1258. compat_caddr_t chardata; /* font data in expanded form */
  1259. };
  1260. static inline int
  1261. compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
  1262. int perm, struct console_font_op *op)
  1263. {
  1264. struct compat_consolefontdesc cfdarg;
  1265. int i;
  1266. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
  1267. return -EFAULT;
  1268. switch (cmd) {
  1269. case PIO_FONTX:
  1270. if (!perm)
  1271. return -EPERM;
  1272. op->op = KD_FONT_OP_SET;
  1273. op->flags = KD_FONT_FLAG_OLD;
  1274. op->width = 8;
  1275. op->height = cfdarg.charheight;
  1276. op->charcount = cfdarg.charcount;
  1277. op->data = compat_ptr(cfdarg.chardata);
  1278. return con_font_op(vc_cons[fg_console].d, op);
  1279. case GIO_FONTX:
  1280. op->op = KD_FONT_OP_GET;
  1281. op->flags = KD_FONT_FLAG_OLD;
  1282. op->width = 8;
  1283. op->height = cfdarg.charheight;
  1284. op->charcount = cfdarg.charcount;
  1285. op->data = compat_ptr(cfdarg.chardata);
  1286. i = con_font_op(vc_cons[fg_console].d, op);
  1287. if (i)
  1288. return i;
  1289. cfdarg.charheight = op->height;
  1290. cfdarg.charcount = op->charcount;
  1291. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
  1292. return -EFAULT;
  1293. return 0;
  1294. }
  1295. return -EINVAL;
  1296. }
  1297. struct compat_console_font_op {
  1298. compat_uint_t op; /* operation code KD_FONT_OP_* */
  1299. compat_uint_t flags; /* KD_FONT_FLAG_* */
  1300. compat_uint_t width, height; /* font size */
  1301. compat_uint_t charcount;
  1302. compat_caddr_t data; /* font data with height fixed to 32 */
  1303. };
  1304. static inline int
  1305. compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
  1306. int perm, struct console_font_op *op, struct vc_data *vc)
  1307. {
  1308. int i;
  1309. if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
  1310. return -EFAULT;
  1311. if (!perm && op->op != KD_FONT_OP_GET)
  1312. return -EPERM;
  1313. op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
  1314. op->flags |= KD_FONT_FLAG_OLD;
  1315. i = con_font_op(vc, op);
  1316. if (i)
  1317. return i;
  1318. ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
  1319. if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
  1320. return -EFAULT;
  1321. return 0;
  1322. }
  1323. struct compat_unimapdesc {
  1324. unsigned short entry_ct;
  1325. compat_caddr_t entries;
  1326. };
  1327. static inline int
  1328. compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
  1329. int perm, struct vc_data *vc)
  1330. {
  1331. struct compat_unimapdesc tmp;
  1332. struct unipair __user *tmp_entries;
  1333. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  1334. return -EFAULT;
  1335. tmp_entries = compat_ptr(tmp.entries);
  1336. if (tmp_entries)
  1337. if (!access_ok(VERIFY_WRITE, tmp_entries,
  1338. tmp.entry_ct*sizeof(struct unipair)))
  1339. return -EFAULT;
  1340. switch (cmd) {
  1341. case PIO_UNIMAP:
  1342. if (!perm)
  1343. return -EPERM;
  1344. return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
  1345. case GIO_UNIMAP:
  1346. if (!perm && fg_console != vc->vc_num)
  1347. return -EPERM;
  1348. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
  1349. }
  1350. return 0;
  1351. }
  1352. long vt_compat_ioctl(struct tty_struct *tty, struct file * file,
  1353. unsigned int cmd, unsigned long arg)
  1354. {
  1355. struct vc_data *vc = tty->driver_data;
  1356. struct console_font_op op; /* used in multiple places here */
  1357. struct kbd_struct *kbd;
  1358. unsigned int console;
  1359. void __user *up = (void __user *)arg;
  1360. int perm;
  1361. int ret = 0;
  1362. console = vc->vc_num;
  1363. tty_lock();
  1364. if (!vc_cons_allocated(console)) { /* impossible? */
  1365. ret = -ENOIOCTLCMD;
  1366. goto out;
  1367. }
  1368. /*
  1369. * To have permissions to do most of the vt ioctls, we either have
  1370. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  1371. */
  1372. perm = 0;
  1373. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  1374. perm = 1;
  1375. kbd = kbd_table + console;
  1376. switch (cmd) {
  1377. /*
  1378. * these need special handlers for incompatible data structures
  1379. */
  1380. case PIO_FONTX:
  1381. case GIO_FONTX:
  1382. ret = compat_fontx_ioctl(cmd, up, perm, &op);
  1383. break;
  1384. case KDFONTOP:
  1385. ret = compat_kdfontop_ioctl(up, perm, &op, vc);
  1386. break;
  1387. case PIO_UNIMAP:
  1388. case GIO_UNIMAP:
  1389. ret = compat_unimap_ioctl(cmd, up, perm, vc);
  1390. break;
  1391. /*
  1392. * all these treat 'arg' as an integer
  1393. */
  1394. case KIOCSOUND:
  1395. case KDMKTONE:
  1396. #ifdef CONFIG_X86
  1397. case KDADDIO:
  1398. case KDDELIO:
  1399. #endif
  1400. case KDSETMODE:
  1401. case KDMAPDISP:
  1402. case KDUNMAPDISP:
  1403. case KDSKBMODE:
  1404. case KDSKBMETA:
  1405. case KDSKBLED:
  1406. case KDSETLED:
  1407. case KDSIGACCEPT:
  1408. case VT_ACTIVATE:
  1409. case VT_WAITACTIVE:
  1410. case VT_RELDISP:
  1411. case VT_DISALLOCATE:
  1412. case VT_RESIZE:
  1413. case VT_RESIZEX:
  1414. goto fallback;
  1415. /*
  1416. * the rest has a compatible data structure behind arg,
  1417. * but we have to convert it to a proper 64 bit pointer.
  1418. */
  1419. default:
  1420. arg = (unsigned long)compat_ptr(arg);
  1421. goto fallback;
  1422. }
  1423. out:
  1424. tty_unlock();
  1425. return ret;
  1426. fallback:
  1427. tty_unlock();
  1428. return vt_ioctl(tty, file, cmd, arg);
  1429. }
  1430. #endif /* CONFIG_COMPAT */
  1431. /*
  1432. * Performs the back end of a vt switch. Called under the console
  1433. * semaphore.
  1434. */
  1435. static void complete_change_console(struct vc_data *vc)
  1436. {
  1437. unsigned char old_vc_mode;
  1438. int old = fg_console;
  1439. last_console = fg_console;
  1440. /*
  1441. * If we're switching, we could be going from KD_GRAPHICS to
  1442. * KD_TEXT mode or vice versa, which means we need to blank or
  1443. * unblank the screen later.
  1444. */
  1445. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  1446. switch_screen(vc);
  1447. /*
  1448. * This can't appear below a successful kill_pid(). If it did,
  1449. * then the *blank_screen operation could occur while X, having
  1450. * received acqsig, is waking up on another processor. This
  1451. * condition can lead to overlapping accesses to the VGA range
  1452. * and the framebuffer (causing system lockups).
  1453. *
  1454. * To account for this we duplicate this code below only if the
  1455. * controlling process is gone and we've called reset_vc.
  1456. */
  1457. if (old_vc_mode != vc->vc_mode) {
  1458. if (vc->vc_mode == KD_TEXT)
  1459. do_unblank_screen(1);
  1460. else
  1461. do_blank_screen(1);
  1462. }
  1463. /*
  1464. * If this new console is under process control, send it a signal
  1465. * telling it that it has acquired. Also check if it has died and
  1466. * clean up (similar to logic employed in change_console())
  1467. */
  1468. if (vc->vt_mode.mode == VT_PROCESS) {
  1469. /*
  1470. * Send the signal as privileged - kill_pid() will
  1471. * tell us if the process has gone or something else
  1472. * is awry
  1473. */
  1474. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  1475. /*
  1476. * The controlling process has died, so we revert back to
  1477. * normal operation. In this case, we'll also change back
  1478. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1479. * but it saves the agony when the X server dies and the screen
  1480. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1481. * this outside of VT_PROCESS but there is no single process
  1482. * to account for and tracking tty count may be undesirable.
  1483. */
  1484. reset_vc(vc);
  1485. if (old_vc_mode != vc->vc_mode) {
  1486. if (vc->vc_mode == KD_TEXT)
  1487. do_unblank_screen(1);
  1488. else
  1489. do_blank_screen(1);
  1490. }
  1491. }
  1492. }
  1493. /*
  1494. * Wake anyone waiting for their VT to activate
  1495. */
  1496. vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
  1497. return;
  1498. }
  1499. /*
  1500. * Performs the front-end of a vt switch
  1501. */
  1502. void change_console(struct vc_data *new_vc)
  1503. {
  1504. struct vc_data *vc;
  1505. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1506. return;
  1507. /*
  1508. * If this vt is in process mode, then we need to handshake with
  1509. * that process before switching. Essentially, we store where that
  1510. * vt wants to switch to and wait for it to tell us when it's done
  1511. * (via VT_RELDISP ioctl).
  1512. *
  1513. * We also check to see if the controlling process still exists.
  1514. * If it doesn't, we reset this vt to auto mode and continue.
  1515. * This is a cheap way to track process control. The worst thing
  1516. * that can happen is: we send a signal to a process, it dies, and
  1517. * the switch gets "lost" waiting for a response; hopefully, the
  1518. * user will try again, we'll detect the process is gone (unless
  1519. * the user waits just the right amount of time :-) and revert the
  1520. * vt to auto control.
  1521. */
  1522. vc = vc_cons[fg_console].d;
  1523. if (vc->vt_mode.mode == VT_PROCESS) {
  1524. /*
  1525. * Send the signal as privileged - kill_pid() will
  1526. * tell us if the process has gone or something else
  1527. * is awry.
  1528. *
  1529. * We need to set vt_newvt *before* sending the signal or we
  1530. * have a race.
  1531. */
  1532. vc->vt_newvt = new_vc->vc_num;
  1533. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1534. /*
  1535. * It worked. Mark the vt to switch to and
  1536. * return. The process needs to send us a
  1537. * VT_RELDISP ioctl to complete the switch.
  1538. */
  1539. return;
  1540. }
  1541. /*
  1542. * The controlling process has died, so we revert back to
  1543. * normal operation. In this case, we'll also change back
  1544. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1545. * but it saves the agony when the X server dies and the screen
  1546. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1547. * this outside of VT_PROCESS but there is no single process
  1548. * to account for and tracking tty count may be undesirable.
  1549. */
  1550. reset_vc(vc);
  1551. /*
  1552. * Fall through to normal (VT_AUTO) handling of the switch...
  1553. */
  1554. }
  1555. /*
  1556. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1557. */
  1558. if (vc->vc_mode == KD_GRAPHICS)
  1559. return;
  1560. complete_change_console(new_vc);
  1561. }
  1562. /* Perform a kernel triggered VT switch for suspend/resume */
  1563. static int disable_vt_switch;
  1564. int vt_move_to_console(unsigned int vt, int alloc)
  1565. {
  1566. int prev;
  1567. acquire_console_sem();
  1568. /* Graphics mode - up to X */
  1569. if (disable_vt_switch) {
  1570. release_console_sem();
  1571. return 0;
  1572. }
  1573. prev = fg_console;
  1574. if (alloc && vc_allocate(vt)) {
  1575. /* we can't have a free VC for now. Too bad,
  1576. * we don't want to mess the screen for now. */
  1577. release_console_sem();
  1578. return -ENOSPC;
  1579. }
  1580. if (set_console(vt)) {
  1581. /*
  1582. * We're unable to switch to the SUSPEND_CONSOLE.
  1583. * Let the calling function know so it can decide
  1584. * what to do.
  1585. */
  1586. release_console_sem();
  1587. return -EIO;
  1588. }
  1589. release_console_sem();
  1590. tty_lock();
  1591. if (vt_waitactive(vt + 1)) {
  1592. pr_debug("Suspend: Can't switch VCs.");
  1593. tty_unlock();
  1594. return -EINTR;
  1595. }
  1596. tty_unlock();
  1597. return prev;
  1598. }
  1599. /*
  1600. * Normally during a suspend, we allocate a new console and switch to it.
  1601. * When we resume, we switch back to the original console. This switch
  1602. * can be slow, so on systems where the framebuffer can handle restoration
  1603. * of video registers anyways, there's little point in doing the console
  1604. * switch. This function allows you to disable it by passing it '0'.
  1605. */
  1606. void pm_set_vt_switch(int do_switch)
  1607. {
  1608. acquire_console_sem();
  1609. disable_vt_switch = !do_switch;
  1610. release_console_sem();
  1611. }
  1612. EXPORT_SYMBOL(pm_set_vt_switch);