vt_ioctl.c 40 KB

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