vt_ioctl.c 40 KB

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