diff options
author | Alan Cox <alan@linux.intel.com> | 2012-02-28 09:49:23 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2012-03-08 13:50:35 -0500 |
commit | 079c9534a96da9a85a2a2f9715851050fbfbf749 (patch) | |
tree | 0e3782ff6d341f38c6f0b3840cb3c8f2bc922df8 /drivers/tty/vt/keyboard.c | |
parent | 0fb8379dab9f97e4c56de8f9ea772c10eda27561 (diff) |
vt:tackle kbd_table
Keyboard struct lifetime is easy, but the locking is not and is completely
ignored by the existing code. Tackle this one head on
- Make the kbd_table private so we can run down all direct users
- Hoick the relevant ioctl handlers into the keyboard layer
- Lock them with the keyboard lock so they don't change mid keypress
- Add helpers for things like console stop/start so we isolate the poking
around properly
- Tweak the braille console so it still builds
There are a couple of FIXME locking cases left for ioctls that are so hideous
they should be addressed in a later patch. After this patch the kbd_table is
private and all the keyboard jiggery pokery is in one place.
This update fixes speakup and also a memory leak in the original.
Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/tty/vt/keyboard.c')
-rw-r--r-- | drivers/tty/vt/keyboard.c | 621 |
1 files changed, 595 insertions, 26 deletions
diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c index 898e359c5424..70d0593d3bc6 100644 --- a/drivers/tty/vt/keyboard.c +++ b/drivers/tty/vt/keyboard.c | |||
@@ -68,8 +68,6 @@ extern void ctrl_alt_del(void); | |||
68 | 68 | ||
69 | #define KBD_DEFLOCK 0 | 69 | #define KBD_DEFLOCK 0 |
70 | 70 | ||
71 | void compute_shiftstate(void); | ||
72 | |||
73 | /* | 71 | /* |
74 | * Handler Tables. | 72 | * Handler Tables. |
75 | */ | 73 | */ |
@@ -100,35 +98,29 @@ static fn_handler_fn *fn_handler[] = { FN_HANDLERS }; | |||
100 | * Variables exported for vt_ioctl.c | 98 | * Variables exported for vt_ioctl.c |
101 | */ | 99 | */ |
102 | 100 | ||
103 | /* maximum values each key_handler can handle */ | ||
104 | const int max_vals[] = { | ||
105 | 255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1, | ||
106 | NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1, | ||
107 | 255, NR_LOCK - 1, 255, NR_BRL - 1 | ||
108 | }; | ||
109 | |||
110 | const int NR_TYPES = ARRAY_SIZE(max_vals); | ||
111 | |||
112 | struct kbd_struct kbd_table[MAX_NR_CONSOLES]; | ||
113 | EXPORT_SYMBOL_GPL(kbd_table); | ||
114 | static struct kbd_struct *kbd = kbd_table; | ||
115 | |||
116 | struct vt_spawn_console vt_spawn_con = { | 101 | struct vt_spawn_console vt_spawn_con = { |
117 | .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock), | 102 | .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock), |
118 | .pid = NULL, | 103 | .pid = NULL, |
119 | .sig = 0, | 104 | .sig = 0, |
120 | }; | 105 | }; |
121 | 106 | ||
122 | /* | ||
123 | * Variables exported for vt.c | ||
124 | */ | ||
125 | |||
126 | int shift_state = 0; | ||
127 | 107 | ||
128 | /* | 108 | /* |
129 | * Internal Data. | 109 | * Internal Data. |
130 | */ | 110 | */ |
131 | 111 | ||
112 | static struct kbd_struct kbd_table[MAX_NR_CONSOLES]; | ||
113 | static struct kbd_struct *kbd = kbd_table; | ||
114 | |||
115 | /* maximum values each key_handler can handle */ | ||
116 | static const int max_vals[] = { | ||
117 | 255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1, | ||
118 | NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1, | ||
119 | 255, NR_LOCK - 1, 255, NR_BRL - 1 | ||
120 | }; | ||
121 | |||
122 | static const int NR_TYPES = ARRAY_SIZE(max_vals); | ||
123 | |||
132 | static struct input_handler kbd_handler; | 124 | static struct input_handler kbd_handler; |
133 | static DEFINE_SPINLOCK(kbd_event_lock); | 125 | static DEFINE_SPINLOCK(kbd_event_lock); |
134 | static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)]; /* keyboard key bitmap */ | 126 | static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)]; /* keyboard key bitmap */ |
@@ -138,6 +130,8 @@ static int npadch = -1; /* -1 or number assembled on pad */ | |||
138 | static unsigned int diacr; | 130 | static unsigned int diacr; |
139 | static char rep; /* flag telling character repeat */ | 131 | static char rep; /* flag telling character repeat */ |
140 | 132 | ||
133 | static int shift_state = 0; | ||
134 | |||
141 | static unsigned char ledstate = 0xff; /* undefined */ | 135 | static unsigned char ledstate = 0xff; /* undefined */ |
142 | static unsigned char ledioctl; | 136 | static unsigned char ledioctl; |
143 | 137 | ||
@@ -188,7 +182,7 @@ static int getkeycode_helper(struct input_handle *handle, void *data) | |||
188 | return d->error == 0; /* stop as soon as we successfully get one */ | 182 | return d->error == 0; /* stop as soon as we successfully get one */ |
189 | } | 183 | } |
190 | 184 | ||
191 | int getkeycode(unsigned int scancode) | 185 | static int getkeycode(unsigned int scancode) |
192 | { | 186 | { |
193 | struct getset_keycode_data d = { | 187 | struct getset_keycode_data d = { |
194 | .ke = { | 188 | .ke = { |
@@ -215,7 +209,7 @@ static int setkeycode_helper(struct input_handle *handle, void *data) | |||
215 | return d->error == 0; /* stop as soon as we successfully set one */ | 209 | return d->error == 0; /* stop as soon as we successfully set one */ |
216 | } | 210 | } |
217 | 211 | ||
218 | int setkeycode(unsigned int scancode, unsigned int keycode) | 212 | static int setkeycode(unsigned int scancode, unsigned int keycode) |
219 | { | 213 | { |
220 | struct getset_keycode_data d = { | 214 | struct getset_keycode_data d = { |
221 | .ke = { | 215 | .ke = { |
@@ -383,9 +377,11 @@ static void to_utf8(struct vc_data *vc, uint c) | |||
383 | /* | 377 | /* |
384 | * Called after returning from RAW mode or when changing consoles - recompute | 378 | * Called after returning from RAW mode or when changing consoles - recompute |
385 | * shift_down[] and shift_state from key_down[] maybe called when keymap is | 379 | * shift_down[] and shift_state from key_down[] maybe called when keymap is |
386 | * undefined, so that shiftkey release is seen | 380 | * undefined, so that shiftkey release is seen. The caller must hold the |
381 | * kbd_event_lock. | ||
387 | */ | 382 | */ |
388 | void compute_shiftstate(void) | 383 | |
384 | static void do_compute_shiftstate(void) | ||
389 | { | 385 | { |
390 | unsigned int i, j, k, sym, val; | 386 | unsigned int i, j, k, sym, val; |
391 | 387 | ||
@@ -418,6 +414,15 @@ void compute_shiftstate(void) | |||
418 | } | 414 | } |
419 | } | 415 | } |
420 | 416 | ||
417 | /* We still have to export this method to vt.c */ | ||
418 | void compute_shiftstate(void) | ||
419 | { | ||
420 | unsigned long flags; | ||
421 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
422 | do_compute_shiftstate(); | ||
423 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
424 | } | ||
425 | |||
421 | /* | 426 | /* |
422 | * We have a combining character DIACR here, followed by the character CH. | 427 | * We have a combining character DIACR here, followed by the character CH. |
423 | * If the combination occurs in the table, return the corresponding value. | 428 | * If the combination occurs in the table, return the corresponding value. |
@@ -637,7 +642,7 @@ static void fn_SAK(struct vc_data *vc) | |||
637 | 642 | ||
638 | static void fn_null(struct vc_data *vc) | 643 | static void fn_null(struct vc_data *vc) |
639 | { | 644 | { |
640 | compute_shiftstate(); | 645 | do_compute_shiftstate(); |
641 | } | 646 | } |
642 | 647 | ||
643 | /* | 648 | /* |
@@ -990,6 +995,8 @@ unsigned char getledstate(void) | |||
990 | 995 | ||
991 | void setledstate(struct kbd_struct *kbd, unsigned int led) | 996 | void setledstate(struct kbd_struct *kbd, unsigned int led) |
992 | { | 997 | { |
998 | unsigned long flags; | ||
999 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
993 | if (!(led & ~7)) { | 1000 | if (!(led & ~7)) { |
994 | ledioctl = led; | 1001 | ledioctl = led; |
995 | kbd->ledmode = LED_SHOW_IOCTL; | 1002 | kbd->ledmode = LED_SHOW_IOCTL; |
@@ -997,6 +1004,7 @@ void setledstate(struct kbd_struct *kbd, unsigned int led) | |||
997 | kbd->ledmode = LED_SHOW_FLAGS; | 1004 | kbd->ledmode = LED_SHOW_FLAGS; |
998 | 1005 | ||
999 | set_leds(); | 1006 | set_leds(); |
1007 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1000 | } | 1008 | } |
1001 | 1009 | ||
1002 | static inline unsigned char getleds(void) | 1010 | static inline unsigned char getleds(void) |
@@ -1036,6 +1044,75 @@ static int kbd_update_leds_helper(struct input_handle *handle, void *data) | |||
1036 | return 0; | 1044 | return 0; |
1037 | } | 1045 | } |
1038 | 1046 | ||
1047 | /** | ||
1048 | * vt_get_leds - helper for braille console | ||
1049 | * @console: console to read | ||
1050 | * @flag: flag we want to check | ||
1051 | * | ||
1052 | * Check the status of a keyboard led flag and report it back | ||
1053 | */ | ||
1054 | int vt_get_leds(int console, int flag) | ||
1055 | { | ||
1056 | unsigned long flags; | ||
1057 | struct kbd_struct * kbd = kbd_table + console; | ||
1058 | int ret; | ||
1059 | |||
1060 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
1061 | ret = vc_kbd_led(kbd, flag); | ||
1062 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1063 | |||
1064 | return ret; | ||
1065 | } | ||
1066 | EXPORT_SYMBOL_GPL(vt_get_leds); | ||
1067 | |||
1068 | /** | ||
1069 | * vt_set_led_state - set LED state of a console | ||
1070 | * @console: console to set | ||
1071 | * @leds: LED bits | ||
1072 | * | ||
1073 | * Set the LEDs on a console. This is a wrapper for the VT layer | ||
1074 | * so that we can keep kbd knowledge internal | ||
1075 | */ | ||
1076 | void vt_set_led_state(int console, int leds) | ||
1077 | { | ||
1078 | struct kbd_struct * kbd = kbd_table + console; | ||
1079 | setledstate(kbd, leds); | ||
1080 | } | ||
1081 | |||
1082 | /** | ||
1083 | * vt_kbd_con_start - Keyboard side of console start | ||
1084 | * @console: console | ||
1085 | * | ||
1086 | * Handle console start. This is a wrapper for the VT layer | ||
1087 | * so that we can keep kbd knowledge internal | ||
1088 | */ | ||
1089 | void vt_kbd_con_start(int console) | ||
1090 | { | ||
1091 | struct kbd_struct * kbd = kbd_table + console; | ||
1092 | unsigned long flags; | ||
1093 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
1094 | clr_vc_kbd_led(kbd, VC_SCROLLOCK); | ||
1095 | set_leds(); | ||
1096 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1097 | } | ||
1098 | |||
1099 | /** | ||
1100 | * vt_kbd_con_stop - Keyboard side of console stop | ||
1101 | * @console: console | ||
1102 | * | ||
1103 | * Handle console stop. This is a wrapper for the VT layer | ||
1104 | * so that we can keep kbd knowledge internal | ||
1105 | */ | ||
1106 | void vt_kbd_con_stop(int console) | ||
1107 | { | ||
1108 | struct kbd_struct * kbd = kbd_table + console; | ||
1109 | unsigned long flags; | ||
1110 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
1111 | set_vc_kbd_led(kbd, VC_SCROLLOCK); | ||
1112 | set_leds(); | ||
1113 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1114 | } | ||
1115 | |||
1039 | /* | 1116 | /* |
1040 | * This is the tasklet that updates LED state on all keyboards | 1117 | * This is the tasklet that updates LED state on all keyboards |
1041 | * attached to the box. The reason we use tasklet is that we | 1118 | * attached to the box. The reason we use tasklet is that we |
@@ -1255,7 +1332,7 @@ static void kbd_keycode(unsigned int keycode, int down, int hw_raw) | |||
1255 | if (rc == NOTIFY_STOP || !key_map) { | 1332 | if (rc == NOTIFY_STOP || !key_map) { |
1256 | atomic_notifier_call_chain(&keyboard_notifier_list, | 1333 | atomic_notifier_call_chain(&keyboard_notifier_list, |
1257 | KBD_UNBOUND_KEYCODE, ¶m); | 1334 | KBD_UNBOUND_KEYCODE, ¶m); |
1258 | compute_shiftstate(); | 1335 | do_compute_shiftstate(); |
1259 | kbd->slockstate = 0; | 1336 | kbd->slockstate = 0; |
1260 | return; | 1337 | return; |
1261 | } | 1338 | } |
@@ -1615,3 +1692,495 @@ int vt_do_diacrit(unsigned int cmd, void __user *up, int perm) | |||
1615 | } | 1692 | } |
1616 | return ret; | 1693 | return ret; |
1617 | } | 1694 | } |
1695 | |||
1696 | /** | ||
1697 | * vt_do_kdskbmode - set keyboard mode ioctl | ||
1698 | * @console: the console to use | ||
1699 | * @arg: the requested mode | ||
1700 | * | ||
1701 | * Update the keyboard mode bits while holding the correct locks. | ||
1702 | * Return 0 for success or an error code. | ||
1703 | */ | ||
1704 | int vt_do_kdskbmode(int console, unsigned int arg) | ||
1705 | { | ||
1706 | struct kbd_struct * kbd = kbd_table + console; | ||
1707 | int ret = 0; | ||
1708 | unsigned long flags; | ||
1709 | |||
1710 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
1711 | switch(arg) { | ||
1712 | case K_RAW: | ||
1713 | kbd->kbdmode = VC_RAW; | ||
1714 | break; | ||
1715 | case K_MEDIUMRAW: | ||
1716 | kbd->kbdmode = VC_MEDIUMRAW; | ||
1717 | break; | ||
1718 | case K_XLATE: | ||
1719 | kbd->kbdmode = VC_XLATE; | ||
1720 | do_compute_shiftstate(); | ||
1721 | break; | ||
1722 | case K_UNICODE: | ||
1723 | kbd->kbdmode = VC_UNICODE; | ||
1724 | do_compute_shiftstate(); | ||
1725 | break; | ||
1726 | case K_OFF: | ||
1727 | kbd->kbdmode = VC_OFF; | ||
1728 | break; | ||
1729 | default: | ||
1730 | ret = -EINVAL; | ||
1731 | } | ||
1732 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1733 | return ret; | ||
1734 | } | ||
1735 | |||
1736 | /** | ||
1737 | * vt_do_kdskbmeta - set keyboard meta state | ||
1738 | * @console: the console to use | ||
1739 | * @arg: the requested meta state | ||
1740 | * | ||
1741 | * Update the keyboard meta bits while holding the correct locks. | ||
1742 | * Return 0 for success or an error code. | ||
1743 | */ | ||
1744 | int vt_do_kdskbmeta(int console, unsigned int arg) | ||
1745 | { | ||
1746 | struct kbd_struct * kbd = kbd_table + console; | ||
1747 | int ret = 0; | ||
1748 | unsigned long flags; | ||
1749 | |||
1750 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
1751 | switch(arg) { | ||
1752 | case K_METABIT: | ||
1753 | clr_vc_kbd_mode(kbd, VC_META); | ||
1754 | break; | ||
1755 | case K_ESCPREFIX: | ||
1756 | set_vc_kbd_mode(kbd, VC_META); | ||
1757 | break; | ||
1758 | default: | ||
1759 | ret = -EINVAL; | ||
1760 | } | ||
1761 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1762 | return ret; | ||
1763 | } | ||
1764 | |||
1765 | int vt_do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, | ||
1766 | int perm) | ||
1767 | { | ||
1768 | struct kbkeycode tmp; | ||
1769 | int kc = 0; | ||
1770 | |||
1771 | if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode))) | ||
1772 | return -EFAULT; | ||
1773 | switch (cmd) { | ||
1774 | case KDGETKEYCODE: | ||
1775 | kc = getkeycode(tmp.scancode); | ||
1776 | if (kc >= 0) | ||
1777 | kc = put_user(kc, &user_kbkc->keycode); | ||
1778 | break; | ||
1779 | case KDSETKEYCODE: | ||
1780 | if (!perm) | ||
1781 | return -EPERM; | ||
1782 | kc = setkeycode(tmp.scancode, tmp.keycode); | ||
1783 | break; | ||
1784 | } | ||
1785 | return kc; | ||
1786 | } | ||
1787 | |||
1788 | #define i (tmp.kb_index) | ||
1789 | #define s (tmp.kb_table) | ||
1790 | #define v (tmp.kb_value) | ||
1791 | |||
1792 | int vt_do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, | ||
1793 | int console) | ||
1794 | { | ||
1795 | struct kbd_struct * kbd = kbd_table + console; | ||
1796 | struct kbentry tmp; | ||
1797 | ushort *key_map, *new_map, val, ov; | ||
1798 | unsigned long flags; | ||
1799 | |||
1800 | if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry))) | ||
1801 | return -EFAULT; | ||
1802 | |||
1803 | if (!capable(CAP_SYS_TTY_CONFIG)) | ||
1804 | perm = 0; | ||
1805 | |||
1806 | switch (cmd) { | ||
1807 | case KDGKBENT: | ||
1808 | /* Ensure another thread doesn't free it under us */ | ||
1809 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
1810 | key_map = key_maps[s]; | ||
1811 | if (key_map) { | ||
1812 | val = U(key_map[i]); | ||
1813 | if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES) | ||
1814 | val = K_HOLE; | ||
1815 | } else | ||
1816 | val = (i ? K_HOLE : K_NOSUCHMAP); | ||
1817 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1818 | return put_user(val, &user_kbe->kb_value); | ||
1819 | case KDSKBENT: | ||
1820 | if (!perm) | ||
1821 | return -EPERM; | ||
1822 | if (!i && v == K_NOSUCHMAP) { | ||
1823 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
1824 | /* deallocate map */ | ||
1825 | key_map = key_maps[s]; | ||
1826 | if (s && key_map) { | ||
1827 | key_maps[s] = NULL; | ||
1828 | if (key_map[0] == U(K_ALLOCATED)) { | ||
1829 | kfree(key_map); | ||
1830 | keymap_count--; | ||
1831 | } | ||
1832 | } | ||
1833 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1834 | break; | ||
1835 | } | ||
1836 | |||
1837 | if (KTYP(v) < NR_TYPES) { | ||
1838 | if (KVAL(v) > max_vals[KTYP(v)]) | ||
1839 | return -EINVAL; | ||
1840 | } else | ||
1841 | if (kbd->kbdmode != VC_UNICODE) | ||
1842 | return -EINVAL; | ||
1843 | |||
1844 | /* ++Geert: non-PC keyboards may generate keycode zero */ | ||
1845 | #if !defined(__mc68000__) && !defined(__powerpc__) | ||
1846 | /* assignment to entry 0 only tests validity of args */ | ||
1847 | if (!i) | ||
1848 | break; | ||
1849 | #endif | ||
1850 | |||
1851 | new_map = kmalloc(sizeof(plain_map), GFP_KERNEL); | ||
1852 | if (!new_map) | ||
1853 | return -ENOMEM; | ||
1854 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
1855 | key_map = key_maps[s]; | ||
1856 | if (key_map == NULL) { | ||
1857 | int j; | ||
1858 | |||
1859 | if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && | ||
1860 | !capable(CAP_SYS_RESOURCE)) { | ||
1861 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1862 | kfree(new_map); | ||
1863 | return -EPERM; | ||
1864 | } | ||
1865 | key_maps[s] = new_map; | ||
1866 | key_map[0] = U(K_ALLOCATED); | ||
1867 | for (j = 1; j < NR_KEYS; j++) | ||
1868 | key_map[j] = U(K_HOLE); | ||
1869 | keymap_count++; | ||
1870 | } else | ||
1871 | kfree(new_map); | ||
1872 | |||
1873 | ov = U(key_map[i]); | ||
1874 | if (v == ov) | ||
1875 | goto out; | ||
1876 | /* | ||
1877 | * Attention Key. | ||
1878 | */ | ||
1879 | if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN)) { | ||
1880 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1881 | return -EPERM; | ||
1882 | } | ||
1883 | key_map[i] = U(v); | ||
1884 | if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT)) | ||
1885 | do_compute_shiftstate(); | ||
1886 | out: | ||
1887 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
1888 | break; | ||
1889 | } | ||
1890 | return 0; | ||
1891 | } | ||
1892 | #undef i | ||
1893 | #undef s | ||
1894 | #undef v | ||
1895 | |||
1896 | /* FIXME: This one needs untangling and locking */ | ||
1897 | int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) | ||
1898 | { | ||
1899 | struct kbsentry *kbs; | ||
1900 | char *p; | ||
1901 | u_char *q; | ||
1902 | u_char __user *up; | ||
1903 | int sz; | ||
1904 | int delta; | ||
1905 | char *first_free, *fj, *fnw; | ||
1906 | int i, j, k; | ||
1907 | int ret; | ||
1908 | |||
1909 | if (!capable(CAP_SYS_TTY_CONFIG)) | ||
1910 | perm = 0; | ||
1911 | |||
1912 | kbs = kmalloc(sizeof(*kbs), GFP_KERNEL); | ||
1913 | if (!kbs) { | ||
1914 | ret = -ENOMEM; | ||
1915 | goto reterr; | ||
1916 | } | ||
1917 | |||
1918 | /* we mostly copy too much here (512bytes), but who cares ;) */ | ||
1919 | if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) { | ||
1920 | ret = -EFAULT; | ||
1921 | goto reterr; | ||
1922 | } | ||
1923 | kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0'; | ||
1924 | i = kbs->kb_func; | ||
1925 | |||
1926 | switch (cmd) { | ||
1927 | case KDGKBSENT: | ||
1928 | sz = sizeof(kbs->kb_string) - 1; /* sz should have been | ||
1929 | a struct member */ | ||
1930 | up = user_kdgkb->kb_string; | ||
1931 | p = func_table[i]; | ||
1932 | if(p) | ||
1933 | for ( ; *p && sz; p++, sz--) | ||
1934 | if (put_user(*p, up++)) { | ||
1935 | ret = -EFAULT; | ||
1936 | goto reterr; | ||
1937 | } | ||
1938 | if (put_user('\0', up)) { | ||
1939 | ret = -EFAULT; | ||
1940 | goto reterr; | ||
1941 | } | ||
1942 | kfree(kbs); | ||
1943 | return ((p && *p) ? -EOVERFLOW : 0); | ||
1944 | case KDSKBSENT: | ||
1945 | if (!perm) { | ||
1946 | ret = -EPERM; | ||
1947 | goto reterr; | ||
1948 | } | ||
1949 | |||
1950 | q = func_table[i]; | ||
1951 | first_free = funcbufptr + (funcbufsize - funcbufleft); | ||
1952 | for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++) | ||
1953 | ; | ||
1954 | if (j < MAX_NR_FUNC) | ||
1955 | fj = func_table[j]; | ||
1956 | else | ||
1957 | fj = first_free; | ||
1958 | |||
1959 | delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string); | ||
1960 | if (delta <= funcbufleft) { /* it fits in current buf */ | ||
1961 | if (j < MAX_NR_FUNC) { | ||
1962 | memmove(fj + delta, fj, first_free - fj); | ||
1963 | for (k = j; k < MAX_NR_FUNC; k++) | ||
1964 | if (func_table[k]) | ||
1965 | func_table[k] += delta; | ||
1966 | } | ||
1967 | if (!q) | ||
1968 | func_table[i] = fj; | ||
1969 | funcbufleft -= delta; | ||
1970 | } else { /* allocate a larger buffer */ | ||
1971 | sz = 256; | ||
1972 | while (sz < funcbufsize - funcbufleft + delta) | ||
1973 | sz <<= 1; | ||
1974 | fnw = kmalloc(sz, GFP_KERNEL); | ||
1975 | if(!fnw) { | ||
1976 | ret = -ENOMEM; | ||
1977 | goto reterr; | ||
1978 | } | ||
1979 | |||
1980 | if (!q) | ||
1981 | func_table[i] = fj; | ||
1982 | if (fj > funcbufptr) | ||
1983 | memmove(fnw, funcbufptr, fj - funcbufptr); | ||
1984 | for (k = 0; k < j; k++) | ||
1985 | if (func_table[k]) | ||
1986 | func_table[k] = fnw + (func_table[k] - funcbufptr); | ||
1987 | |||
1988 | if (first_free > fj) { | ||
1989 | memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj); | ||
1990 | for (k = j; k < MAX_NR_FUNC; k++) | ||
1991 | if (func_table[k]) | ||
1992 | func_table[k] = fnw + (func_table[k] - funcbufptr) + delta; | ||
1993 | } | ||
1994 | if (funcbufptr != func_buf) | ||
1995 | kfree(funcbufptr); | ||
1996 | funcbufptr = fnw; | ||
1997 | funcbufleft = funcbufleft - delta + sz - funcbufsize; | ||
1998 | funcbufsize = sz; | ||
1999 | } | ||
2000 | strcpy(func_table[i], kbs->kb_string); | ||
2001 | break; | ||
2002 | } | ||
2003 | ret = 0; | ||
2004 | reterr: | ||
2005 | kfree(kbs); | ||
2006 | return ret; | ||
2007 | } | ||
2008 | |||
2009 | int vt_do_kdskled(int console, int cmd, unsigned long arg, int perm) | ||
2010 | { | ||
2011 | struct kbd_struct * kbd = kbd_table + console; | ||
2012 | unsigned long flags; | ||
2013 | unsigned char ucval; | ||
2014 | |||
2015 | switch(cmd) { | ||
2016 | /* the ioctls below read/set the flags usually shown in the leds */ | ||
2017 | /* don't use them - they will go away without warning */ | ||
2018 | case KDGKBLED: | ||
2019 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
2020 | ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4); | ||
2021 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
2022 | return put_user(ucval, (char __user *)arg); | ||
2023 | |||
2024 | case KDSKBLED: | ||
2025 | if (!perm) | ||
2026 | return -EPERM; | ||
2027 | if (arg & ~0x77) | ||
2028 | return -EINVAL; | ||
2029 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
2030 | kbd->ledflagstate = (arg & 7); | ||
2031 | kbd->default_ledflagstate = ((arg >> 4) & 7); | ||
2032 | set_leds(); | ||
2033 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
2034 | break; | ||
2035 | |||
2036 | /* the ioctls below only set the lights, not the functions */ | ||
2037 | /* for those, see KDGKBLED and KDSKBLED above */ | ||
2038 | case KDGETLED: | ||
2039 | ucval = getledstate(); | ||
2040 | return put_user(ucval, (char __user *)arg); | ||
2041 | |||
2042 | case KDSETLED: | ||
2043 | if (!perm) | ||
2044 | return -EPERM; | ||
2045 | setledstate(kbd, arg); | ||
2046 | return 0; | ||
2047 | } | ||
2048 | return -ENOIOCTLCMD; | ||
2049 | } | ||
2050 | |||
2051 | int vt_do_kdgkbmode(int console) | ||
2052 | { | ||
2053 | struct kbd_struct * kbd = kbd_table + console; | ||
2054 | /* This is a spot read so needs no locking */ | ||
2055 | switch (kbd->kbdmode) { | ||
2056 | case VC_RAW: | ||
2057 | return K_RAW; | ||
2058 | case VC_MEDIUMRAW: | ||
2059 | return K_MEDIUMRAW; | ||
2060 | case VC_UNICODE: | ||
2061 | return K_UNICODE; | ||
2062 | case VC_OFF: | ||
2063 | return K_OFF; | ||
2064 | default: | ||
2065 | return K_XLATE; | ||
2066 | } | ||
2067 | } | ||
2068 | |||
2069 | /** | ||
2070 | * vt_do_kdgkbmeta - report meta status | ||
2071 | * @console: console to report | ||
2072 | * | ||
2073 | * Report the meta flag status of this console | ||
2074 | */ | ||
2075 | int vt_do_kdgkbmeta(int console) | ||
2076 | { | ||
2077 | struct kbd_struct * kbd = kbd_table + console; | ||
2078 | /* Again a spot read so no locking */ | ||
2079 | return vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT; | ||
2080 | } | ||
2081 | |||
2082 | /** | ||
2083 | * vt_reset_unicode - reset the unicode status | ||
2084 | * @console: console being reset | ||
2085 | * | ||
2086 | * Restore the unicode console state to its default | ||
2087 | */ | ||
2088 | void vt_reset_unicode(int console) | ||
2089 | { | ||
2090 | unsigned long flags; | ||
2091 | |||
2092 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
2093 | kbd_table[console].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; | ||
2094 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
2095 | } | ||
2096 | |||
2097 | /** | ||
2098 | * vt_get_shiftstate - shift bit state | ||
2099 | * | ||
2100 | * Report the shift bits from the keyboard state. We have to export | ||
2101 | * this to support some oddities in the vt layer. | ||
2102 | */ | ||
2103 | int vt_get_shift_state(void) | ||
2104 | { | ||
2105 | /* Don't lock as this is a transient report */ | ||
2106 | return shift_state; | ||
2107 | } | ||
2108 | |||
2109 | /** | ||
2110 | * vt_reset_keyboard - reset keyboard state | ||
2111 | * @console: console to reset | ||
2112 | * | ||
2113 | * Reset the keyboard bits for a console as part of a general console | ||
2114 | * reset event | ||
2115 | */ | ||
2116 | void vt_reset_keyboard(int console) | ||
2117 | { | ||
2118 | struct kbd_struct * kbd = kbd_table + console; | ||
2119 | unsigned long flags; | ||
2120 | |||
2121 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
2122 | set_vc_kbd_mode(kbd, VC_REPEAT); | ||
2123 | clr_vc_kbd_mode(kbd, VC_CKMODE); | ||
2124 | clr_vc_kbd_mode(kbd, VC_APPLIC); | ||
2125 | clr_vc_kbd_mode(kbd, VC_CRLF); | ||
2126 | kbd->lockstate = 0; | ||
2127 | kbd->slockstate = 0; | ||
2128 | kbd->ledmode = LED_SHOW_FLAGS; | ||
2129 | kbd->ledflagstate = kbd->default_ledflagstate; | ||
2130 | /* do not do set_leds here because this causes an endless tasklet loop | ||
2131 | when the keyboard hasn't been initialized yet */ | ||
2132 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
2133 | } | ||
2134 | |||
2135 | /** | ||
2136 | * vt_get_kbd_mode_bit - read keyboard status bits | ||
2137 | * @console: console to read from | ||
2138 | * @bit: mode bit to read | ||
2139 | * | ||
2140 | * Report back a vt mode bit. We do this without locking so the | ||
2141 | * caller must be sure that there are no synchronization needs | ||
2142 | */ | ||
2143 | |||
2144 | int vt_get_kbd_mode_bit(int console, int bit) | ||
2145 | { | ||
2146 | struct kbd_struct * kbd = kbd_table + console; | ||
2147 | return vc_kbd_mode(kbd, bit); | ||
2148 | } | ||
2149 | |||
2150 | /** | ||
2151 | * vt_set_kbd_mode_bit - read keyboard status bits | ||
2152 | * @console: console to read from | ||
2153 | * @bit: mode bit to read | ||
2154 | * | ||
2155 | * Set a vt mode bit. We do this without locking so the | ||
2156 | * caller must be sure that there are no synchronization needs | ||
2157 | */ | ||
2158 | |||
2159 | void vt_set_kbd_mode_bit(int console, int bit) | ||
2160 | { | ||
2161 | struct kbd_struct * kbd = kbd_table + console; | ||
2162 | unsigned long flags; | ||
2163 | |||
2164 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
2165 | set_vc_kbd_mode(kbd, bit); | ||
2166 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
2167 | } | ||
2168 | |||
2169 | /** | ||
2170 | * vt_clr_kbd_mode_bit - read keyboard status bits | ||
2171 | * @console: console to read from | ||
2172 | * @bit: mode bit to read | ||
2173 | * | ||
2174 | * Report back a vt mode bit. We do this without locking so the | ||
2175 | * caller must be sure that there are no synchronization needs | ||
2176 | */ | ||
2177 | |||
2178 | void vt_clr_kbd_mode_bit(int console, int bit) | ||
2179 | { | ||
2180 | struct kbd_struct * kbd = kbd_table + console; | ||
2181 | unsigned long flags; | ||
2182 | |||
2183 | spin_lock_irqsave(&kbd_event_lock, flags); | ||
2184 | clr_vc_kbd_mode(kbd, bit); | ||
2185 | spin_unlock_irqrestore(&kbd_event_lock, flags); | ||
2186 | } | ||