diff options
author | Michal Marek <mmarek@suse.cz> | 2010-12-01 07:13:56 -0500 |
---|---|---|
committer | Michal Marek <mmarek@suse.cz> | 2010-12-01 07:13:56 -0500 |
commit | 307991055b0ce65d53dc1eb501c456c10eb36360 (patch) | |
tree | 71bb9263dc71344e5d7b97ca6d5cf223f1f1a856 /drivers/tty | |
parent | 1198c6d45a1ef5f4f7fdfbf33ef7d270493ec575 (diff) | |
parent | 9b4320b77bab4031649c484da1c595f39c2e43cd (diff) |
Merge branch 'kconfig_fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-next into kbuild/rc-fixes
Diffstat (limited to 'drivers/tty')
-rw-r--r-- | drivers/tty/Makefile | 11 | ||||
-rw-r--r-- | drivers/tty/n_gsm.c | 2764 | ||||
-rw-r--r-- | drivers/tty/n_hdlc.c | 1007 | ||||
-rw-r--r-- | drivers/tty/n_r3964.c | 1264 | ||||
-rw-r--r-- | drivers/tty/n_tty.c | 2121 | ||||
-rw-r--r-- | drivers/tty/pty.c | 777 | ||||
-rw-r--r-- | drivers/tty/sysrq.c | 811 | ||||
-rw-r--r-- | drivers/tty/tty_audit.c | 358 | ||||
-rw-r--r-- | drivers/tty/tty_buffer.c | 534 | ||||
-rw-r--r-- | drivers/tty/tty_io.c | 3263 | ||||
-rw-r--r-- | drivers/tty/tty_ioctl.c | 1179 | ||||
-rw-r--r-- | drivers/tty/tty_ldisc.c | 950 | ||||
-rw-r--r-- | drivers/tty/tty_mutex.c | 47 | ||||
-rw-r--r-- | drivers/tty/tty_port.c | 446 | ||||
-rw-r--r-- | drivers/tty/vt/.gitignore | 2 | ||||
-rw-r--r-- | drivers/tty/vt/Makefile | 34 | ||||
-rw-r--r-- | drivers/tty/vt/consolemap.c | 745 | ||||
-rw-r--r-- | drivers/tty/vt/cp437.uni | 291 | ||||
-rw-r--r-- | drivers/tty/vt/defkeymap.c_shipped | 262 | ||||
-rw-r--r-- | drivers/tty/vt/defkeymap.map | 357 | ||||
-rw-r--r-- | drivers/tty/vt/keyboard.c | 1454 | ||||
-rw-r--r-- | drivers/tty/vt/selection.c | 348 | ||||
-rw-r--r-- | drivers/tty/vt/vc_screen.c | 644 | ||||
-rw-r--r-- | drivers/tty/vt/vt.c | 4209 | ||||
-rw-r--r-- | drivers/tty/vt/vt_ioctl.c | 1788 |
25 files changed, 25666 insertions, 0 deletions
diff --git a/drivers/tty/Makefile b/drivers/tty/Makefile new file mode 100644 index 000000000000..c43ef48b1a0f --- /dev/null +++ b/drivers/tty/Makefile | |||
@@ -0,0 +1,11 @@ | |||
1 | obj-y += tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o \ | ||
2 | tty_buffer.o tty_port.o tty_mutex.o | ||
3 | obj-$(CONFIG_LEGACY_PTYS) += pty.o | ||
4 | obj-$(CONFIG_UNIX98_PTYS) += pty.o | ||
5 | obj-$(CONFIG_AUDIT) += tty_audit.o | ||
6 | obj-$(CONFIG_MAGIC_SYSRQ) += sysrq.o | ||
7 | obj-$(CONFIG_N_HDLC) += n_hdlc.o | ||
8 | obj-$(CONFIG_N_GSM) += n_gsm.o | ||
9 | obj-$(CONFIG_R3964) += n_r3964.o | ||
10 | |||
11 | obj-y += vt/ | ||
diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c new file mode 100644 index 000000000000..81b46585edf7 --- /dev/null +++ b/drivers/tty/n_gsm.c | |||
@@ -0,0 +1,2764 @@ | |||
1 | /* | ||
2 | * n_gsm.c GSM 0710 tty multiplexor | ||
3 | * Copyright (c) 2009/10 Intel Corporation | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License version 2 as | ||
7 | * published by the Free Software Foundation. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
17 | * | ||
18 | * * THIS IS A DEVELOPMENT SNAPSHOT IT IS NOT A FINAL RELEASE * | ||
19 | * | ||
20 | * TO DO: | ||
21 | * Mostly done: ioctls for setting modes/timing | ||
22 | * Partly done: hooks so you can pull off frames to non tty devs | ||
23 | * Restart DLCI 0 when it closes ? | ||
24 | * Test basic encoding | ||
25 | * Improve the tx engine | ||
26 | * Resolve tx side locking by adding a queue_head and routing | ||
27 | * all control traffic via it | ||
28 | * General tidy/document | ||
29 | * Review the locking/move to refcounts more (mux now moved to an | ||
30 | * alloc/free model ready) | ||
31 | * Use newest tty open/close port helpers and install hooks | ||
32 | * What to do about power functions ? | ||
33 | * Termios setting and negotiation | ||
34 | * Do we need a 'which mux are you' ioctl to correlate mux and tty sets | ||
35 | * | ||
36 | */ | ||
37 | |||
38 | #include <linux/types.h> | ||
39 | #include <linux/major.h> | ||
40 | #include <linux/errno.h> | ||
41 | #include <linux/signal.h> | ||
42 | #include <linux/fcntl.h> | ||
43 | #include <linux/sched.h> | ||
44 | #include <linux/interrupt.h> | ||
45 | #include <linux/tty.h> | ||
46 | #include <linux/ctype.h> | ||
47 | #include <linux/mm.h> | ||
48 | #include <linux/string.h> | ||
49 | #include <linux/slab.h> | ||
50 | #include <linux/poll.h> | ||
51 | #include <linux/bitops.h> | ||
52 | #include <linux/file.h> | ||
53 | #include <linux/uaccess.h> | ||
54 | #include <linux/module.h> | ||
55 | #include <linux/timer.h> | ||
56 | #include <linux/tty_flip.h> | ||
57 | #include <linux/tty_driver.h> | ||
58 | #include <linux/serial.h> | ||
59 | #include <linux/kfifo.h> | ||
60 | #include <linux/skbuff.h> | ||
61 | #include <linux/gsmmux.h> | ||
62 | |||
63 | static int debug; | ||
64 | module_param(debug, int, 0600); | ||
65 | |||
66 | #define T1 (HZ/10) | ||
67 | #define T2 (HZ/3) | ||
68 | #define N2 3 | ||
69 | |||
70 | /* Use long timers for testing at low speed with debug on */ | ||
71 | #ifdef DEBUG_TIMING | ||
72 | #define T1 HZ | ||
73 | #define T2 (2 * HZ) | ||
74 | #endif | ||
75 | |||
76 | /* Semi-arbitary buffer size limits. 0710 is normally run with 32-64 byte | ||
77 | limits so this is plenty */ | ||
78 | #define MAX_MRU 512 | ||
79 | #define MAX_MTU 512 | ||
80 | |||
81 | /* | ||
82 | * Each block of data we have queued to go out is in the form of | ||
83 | * a gsm_msg which holds everything we need in a link layer independant | ||
84 | * format | ||
85 | */ | ||
86 | |||
87 | struct gsm_msg { | ||
88 | struct gsm_msg *next; | ||
89 | u8 addr; /* DLCI address + flags */ | ||
90 | u8 ctrl; /* Control byte + flags */ | ||
91 | unsigned int len; /* Length of data block (can be zero) */ | ||
92 | unsigned char *data; /* Points into buffer but not at the start */ | ||
93 | unsigned char buffer[0]; | ||
94 | }; | ||
95 | |||
96 | /* | ||
97 | * Each active data link has a gsm_dlci structure associated which ties | ||
98 | * the link layer to an optional tty (if the tty side is open). To avoid | ||
99 | * complexity right now these are only ever freed up when the mux is | ||
100 | * shut down. | ||
101 | * | ||
102 | * At the moment we don't free DLCI objects until the mux is torn down | ||
103 | * this avoid object life time issues but might be worth review later. | ||
104 | */ | ||
105 | |||
106 | struct gsm_dlci { | ||
107 | struct gsm_mux *gsm; | ||
108 | int addr; | ||
109 | int state; | ||
110 | #define DLCI_CLOSED 0 | ||
111 | #define DLCI_OPENING 1 /* Sending SABM not seen UA */ | ||
112 | #define DLCI_OPEN 2 /* SABM/UA complete */ | ||
113 | #define DLCI_CLOSING 3 /* Sending DISC not seen UA/DM */ | ||
114 | |||
115 | /* Link layer */ | ||
116 | spinlock_t lock; /* Protects the internal state */ | ||
117 | struct timer_list t1; /* Retransmit timer for SABM and UA */ | ||
118 | int retries; | ||
119 | /* Uplink tty if active */ | ||
120 | struct tty_port port; /* The tty bound to this DLCI if there is one */ | ||
121 | struct kfifo *fifo; /* Queue fifo for the DLCI */ | ||
122 | struct kfifo _fifo; /* For new fifo API porting only */ | ||
123 | int adaption; /* Adaption layer in use */ | ||
124 | u32 modem_rx; /* Our incoming virtual modem lines */ | ||
125 | u32 modem_tx; /* Our outgoing modem lines */ | ||
126 | int dead; /* Refuse re-open */ | ||
127 | /* Flow control */ | ||
128 | int throttled; /* Private copy of throttle state */ | ||
129 | int constipated; /* Throttle status for outgoing */ | ||
130 | /* Packetised I/O */ | ||
131 | struct sk_buff *skb; /* Frame being sent */ | ||
132 | struct sk_buff_head skb_list; /* Queued frames */ | ||
133 | /* Data handling callback */ | ||
134 | void (*data)(struct gsm_dlci *dlci, u8 *data, int len); | ||
135 | }; | ||
136 | |||
137 | /* DLCI 0, 62/63 are special or reseved see gsmtty_open */ | ||
138 | |||
139 | #define NUM_DLCI 64 | ||
140 | |||
141 | /* | ||
142 | * DLCI 0 is used to pass control blocks out of band of the data | ||
143 | * flow (and with a higher link priority). One command can be outstanding | ||
144 | * at a time and we use this structure to manage them. They are created | ||
145 | * and destroyed by the user context, and updated by the receive paths | ||
146 | * and timers | ||
147 | */ | ||
148 | |||
149 | struct gsm_control { | ||
150 | u8 cmd; /* Command we are issuing */ | ||
151 | u8 *data; /* Data for the command in case we retransmit */ | ||
152 | int len; /* Length of block for retransmission */ | ||
153 | int done; /* Done flag */ | ||
154 | int error; /* Error if any */ | ||
155 | }; | ||
156 | |||
157 | /* | ||
158 | * Each GSM mux we have is represented by this structure. If we are | ||
159 | * operating as an ldisc then we use this structure as our ldisc | ||
160 | * state. We need to sort out lifetimes and locking with respect | ||
161 | * to the gsm mux array. For now we don't free DLCI objects that | ||
162 | * have been instantiated until the mux itself is terminated. | ||
163 | * | ||
164 | * To consider further: tty open versus mux shutdown. | ||
165 | */ | ||
166 | |||
167 | struct gsm_mux { | ||
168 | struct tty_struct *tty; /* The tty our ldisc is bound to */ | ||
169 | spinlock_t lock; | ||
170 | |||
171 | /* Events on the GSM channel */ | ||
172 | wait_queue_head_t event; | ||
173 | |||
174 | /* Bits for GSM mode decoding */ | ||
175 | |||
176 | /* Framing Layer */ | ||
177 | unsigned char *buf; | ||
178 | int state; | ||
179 | #define GSM_SEARCH 0 | ||
180 | #define GSM_START 1 | ||
181 | #define GSM_ADDRESS 2 | ||
182 | #define GSM_CONTROL 3 | ||
183 | #define GSM_LEN 4 | ||
184 | #define GSM_DATA 5 | ||
185 | #define GSM_FCS 6 | ||
186 | #define GSM_OVERRUN 7 | ||
187 | unsigned int len; | ||
188 | unsigned int address; | ||
189 | unsigned int count; | ||
190 | int escape; | ||
191 | int encoding; | ||
192 | u8 control; | ||
193 | u8 fcs; | ||
194 | u8 *txframe; /* TX framing buffer */ | ||
195 | |||
196 | /* Methods for the receiver side */ | ||
197 | void (*receive)(struct gsm_mux *gsm, u8 ch); | ||
198 | void (*error)(struct gsm_mux *gsm, u8 ch, u8 flag); | ||
199 | /* And transmit side */ | ||
200 | int (*output)(struct gsm_mux *mux, u8 *data, int len); | ||
201 | |||
202 | /* Link Layer */ | ||
203 | unsigned int mru; | ||
204 | unsigned int mtu; | ||
205 | int initiator; /* Did we initiate connection */ | ||
206 | int dead; /* Has the mux been shut down */ | ||
207 | struct gsm_dlci *dlci[NUM_DLCI]; | ||
208 | int constipated; /* Asked by remote to shut up */ | ||
209 | |||
210 | spinlock_t tx_lock; | ||
211 | unsigned int tx_bytes; /* TX data outstanding */ | ||
212 | #define TX_THRESH_HI 8192 | ||
213 | #define TX_THRESH_LO 2048 | ||
214 | struct gsm_msg *tx_head; /* Pending data packets */ | ||
215 | struct gsm_msg *tx_tail; | ||
216 | |||
217 | /* Control messages */ | ||
218 | struct timer_list t2_timer; /* Retransmit timer for commands */ | ||
219 | int cretries; /* Command retry counter */ | ||
220 | struct gsm_control *pending_cmd;/* Our current pending command */ | ||
221 | spinlock_t control_lock; /* Protects the pending command */ | ||
222 | |||
223 | /* Configuration */ | ||
224 | int adaption; /* 1 or 2 supported */ | ||
225 | u8 ftype; /* UI or UIH */ | ||
226 | int t1, t2; /* Timers in 1/100th of a sec */ | ||
227 | int n2; /* Retry count */ | ||
228 | |||
229 | /* Statistics (not currently exposed) */ | ||
230 | unsigned long bad_fcs; | ||
231 | unsigned long malformed; | ||
232 | unsigned long io_error; | ||
233 | unsigned long bad_size; | ||
234 | unsigned long unsupported; | ||
235 | }; | ||
236 | |||
237 | |||
238 | /* | ||
239 | * Mux objects - needed so that we can translate a tty index into the | ||
240 | * relevant mux and DLCI. | ||
241 | */ | ||
242 | |||
243 | #define MAX_MUX 4 /* 256 minors */ | ||
244 | static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */ | ||
245 | static spinlock_t gsm_mux_lock; | ||
246 | |||
247 | /* | ||
248 | * This section of the driver logic implements the GSM encodings | ||
249 | * both the basic and the 'advanced'. Reliable transport is not | ||
250 | * supported. | ||
251 | */ | ||
252 | |||
253 | #define CR 0x02 | ||
254 | #define EA 0x01 | ||
255 | #define PF 0x10 | ||
256 | |||
257 | /* I is special: the rest are ..*/ | ||
258 | #define RR 0x01 | ||
259 | #define UI 0x03 | ||
260 | #define RNR 0x05 | ||
261 | #define REJ 0x09 | ||
262 | #define DM 0x0F | ||
263 | #define SABM 0x2F | ||
264 | #define DISC 0x43 | ||
265 | #define UA 0x63 | ||
266 | #define UIH 0xEF | ||
267 | |||
268 | /* Channel commands */ | ||
269 | #define CMD_NSC 0x09 | ||
270 | #define CMD_TEST 0x11 | ||
271 | #define CMD_PSC 0x21 | ||
272 | #define CMD_RLS 0x29 | ||
273 | #define CMD_FCOFF 0x31 | ||
274 | #define CMD_PN 0x41 | ||
275 | #define CMD_RPN 0x49 | ||
276 | #define CMD_FCON 0x51 | ||
277 | #define CMD_CLD 0x61 | ||
278 | #define CMD_SNC 0x69 | ||
279 | #define CMD_MSC 0x71 | ||
280 | |||
281 | /* Virtual modem bits */ | ||
282 | #define MDM_FC 0x01 | ||
283 | #define MDM_RTC 0x02 | ||
284 | #define MDM_RTR 0x04 | ||
285 | #define MDM_IC 0x20 | ||
286 | #define MDM_DV 0x40 | ||
287 | |||
288 | #define GSM0_SOF 0xF9 | ||
289 | #define GSM1_SOF 0x7E | ||
290 | #define GSM1_ESCAPE 0x7D | ||
291 | #define GSM1_ESCAPE_BITS 0x20 | ||
292 | #define XON 0x11 | ||
293 | #define XOFF 0x13 | ||
294 | |||
295 | static const struct tty_port_operations gsm_port_ops; | ||
296 | |||
297 | /* | ||
298 | * CRC table for GSM 0710 | ||
299 | */ | ||
300 | |||
301 | static const u8 gsm_fcs8[256] = { | ||
302 | 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, | ||
303 | 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B, | ||
304 | 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69, | ||
305 | 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67, | ||
306 | 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D, | ||
307 | 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43, | ||
308 | 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51, | ||
309 | 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F, | ||
310 | 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05, | ||
311 | 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B, | ||
312 | 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19, | ||
313 | 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17, | ||
314 | 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D, | ||
315 | 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33, | ||
316 | 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21, | ||
317 | 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F, | ||
318 | 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95, | ||
319 | 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B, | ||
320 | 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89, | ||
321 | 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87, | ||
322 | 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD, | ||
323 | 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3, | ||
324 | 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1, | ||
325 | 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF, | ||
326 | 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5, | ||
327 | 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB, | ||
328 | 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9, | ||
329 | 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7, | ||
330 | 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD, | ||
331 | 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3, | ||
332 | 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1, | ||
333 | 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF | ||
334 | }; | ||
335 | |||
336 | #define INIT_FCS 0xFF | ||
337 | #define GOOD_FCS 0xCF | ||
338 | |||
339 | /** | ||
340 | * gsm_fcs_add - update FCS | ||
341 | * @fcs: Current FCS | ||
342 | * @c: Next data | ||
343 | * | ||
344 | * Update the FCS to include c. Uses the algorithm in the specification | ||
345 | * notes. | ||
346 | */ | ||
347 | |||
348 | static inline u8 gsm_fcs_add(u8 fcs, u8 c) | ||
349 | { | ||
350 | return gsm_fcs8[fcs ^ c]; | ||
351 | } | ||
352 | |||
353 | /** | ||
354 | * gsm_fcs_add_block - update FCS for a block | ||
355 | * @fcs: Current FCS | ||
356 | * @c: buffer of data | ||
357 | * @len: length of buffer | ||
358 | * | ||
359 | * Update the FCS to include c. Uses the algorithm in the specification | ||
360 | * notes. | ||
361 | */ | ||
362 | |||
363 | static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len) | ||
364 | { | ||
365 | while (len--) | ||
366 | fcs = gsm_fcs8[fcs ^ *c++]; | ||
367 | return fcs; | ||
368 | } | ||
369 | |||
370 | /** | ||
371 | * gsm_read_ea - read a byte into an EA | ||
372 | * @val: variable holding value | ||
373 | * c: byte going into the EA | ||
374 | * | ||
375 | * Processes one byte of an EA. Updates the passed variable | ||
376 | * and returns 1 if the EA is now completely read | ||
377 | */ | ||
378 | |||
379 | static int gsm_read_ea(unsigned int *val, u8 c) | ||
380 | { | ||
381 | /* Add the next 7 bits into the value */ | ||
382 | *val <<= 7; | ||
383 | *val |= c >> 1; | ||
384 | /* Was this the last byte of the EA 1 = yes*/ | ||
385 | return c & EA; | ||
386 | } | ||
387 | |||
388 | /** | ||
389 | * gsm_encode_modem - encode modem data bits | ||
390 | * @dlci: DLCI to encode from | ||
391 | * | ||
392 | * Returns the correct GSM encoded modem status bits (6 bit field) for | ||
393 | * the current status of the DLCI and attached tty object | ||
394 | */ | ||
395 | |||
396 | static u8 gsm_encode_modem(const struct gsm_dlci *dlci) | ||
397 | { | ||
398 | u8 modembits = 0; | ||
399 | /* FC is true flow control not modem bits */ | ||
400 | if (dlci->throttled) | ||
401 | modembits |= MDM_FC; | ||
402 | if (dlci->modem_tx & TIOCM_DTR) | ||
403 | modembits |= MDM_RTC; | ||
404 | if (dlci->modem_tx & TIOCM_RTS) | ||
405 | modembits |= MDM_RTR; | ||
406 | if (dlci->modem_tx & TIOCM_RI) | ||
407 | modembits |= MDM_IC; | ||
408 | if (dlci->modem_tx & TIOCM_CD) | ||
409 | modembits |= MDM_DV; | ||
410 | return modembits; | ||
411 | } | ||
412 | |||
413 | /** | ||
414 | * gsm_print_packet - display a frame for debug | ||
415 | * @hdr: header to print before decode | ||
416 | * @addr: address EA from the frame | ||
417 | * @cr: C/R bit from the frame | ||
418 | * @control: control including PF bit | ||
419 | * @data: following data bytes | ||
420 | * @dlen: length of data | ||
421 | * | ||
422 | * Displays a packet in human readable format for debugging purposes. The | ||
423 | * style is based on amateur radio LAP-B dump display. | ||
424 | */ | ||
425 | |||
426 | static void gsm_print_packet(const char *hdr, int addr, int cr, | ||
427 | u8 control, const u8 *data, int dlen) | ||
428 | { | ||
429 | if (!(debug & 1)) | ||
430 | return; | ||
431 | |||
432 | printk(KERN_INFO "%s %d) %c: ", hdr, addr, "RC"[cr]); | ||
433 | |||
434 | switch (control & ~PF) { | ||
435 | case SABM: | ||
436 | printk(KERN_CONT "SABM"); | ||
437 | break; | ||
438 | case UA: | ||
439 | printk(KERN_CONT "UA"); | ||
440 | break; | ||
441 | case DISC: | ||
442 | printk(KERN_CONT "DISC"); | ||
443 | break; | ||
444 | case DM: | ||
445 | printk(KERN_CONT "DM"); | ||
446 | break; | ||
447 | case UI: | ||
448 | printk(KERN_CONT "UI"); | ||
449 | break; | ||
450 | case UIH: | ||
451 | printk(KERN_CONT "UIH"); | ||
452 | break; | ||
453 | default: | ||
454 | if (!(control & 0x01)) { | ||
455 | printk(KERN_CONT "I N(S)%d N(R)%d", | ||
456 | (control & 0x0E) >> 1, (control & 0xE)>> 5); | ||
457 | } else switch (control & 0x0F) { | ||
458 | case RR: | ||
459 | printk("RR(%d)", (control & 0xE0) >> 5); | ||
460 | break; | ||
461 | case RNR: | ||
462 | printk("RNR(%d)", (control & 0xE0) >> 5); | ||
463 | break; | ||
464 | case REJ: | ||
465 | printk("REJ(%d)", (control & 0xE0) >> 5); | ||
466 | break; | ||
467 | default: | ||
468 | printk(KERN_CONT "[%02X]", control); | ||
469 | } | ||
470 | } | ||
471 | |||
472 | if (control & PF) | ||
473 | printk(KERN_CONT "(P)"); | ||
474 | else | ||
475 | printk(KERN_CONT "(F)"); | ||
476 | |||
477 | if (dlen) { | ||
478 | int ct = 0; | ||
479 | while (dlen--) { | ||
480 | if (ct % 8 == 0) | ||
481 | printk(KERN_CONT "\n "); | ||
482 | printk(KERN_CONT "%02X ", *data++); | ||
483 | ct++; | ||
484 | } | ||
485 | } | ||
486 | printk(KERN_CONT "\n"); | ||
487 | } | ||
488 | |||
489 | |||
490 | /* | ||
491 | * Link level transmission side | ||
492 | */ | ||
493 | |||
494 | /** | ||
495 | * gsm_stuff_packet - bytestuff a packet | ||
496 | * @ibuf: input | ||
497 | * @obuf: output | ||
498 | * @len: length of input | ||
499 | * | ||
500 | * Expand a buffer by bytestuffing it. The worst case size change | ||
501 | * is doubling and the caller is responsible for handing out | ||
502 | * suitable sized buffers. | ||
503 | */ | ||
504 | |||
505 | static int gsm_stuff_frame(const u8 *input, u8 *output, int len) | ||
506 | { | ||
507 | int olen = 0; | ||
508 | while (len--) { | ||
509 | if (*input == GSM1_SOF || *input == GSM1_ESCAPE | ||
510 | || *input == XON || *input == XOFF) { | ||
511 | *output++ = GSM1_ESCAPE; | ||
512 | *output++ = *input++ ^ GSM1_ESCAPE_BITS; | ||
513 | olen++; | ||
514 | } else | ||
515 | *output++ = *input++; | ||
516 | olen++; | ||
517 | } | ||
518 | return olen; | ||
519 | } | ||
520 | |||
521 | static void hex_packet(const unsigned char *p, int len) | ||
522 | { | ||
523 | int i; | ||
524 | for (i = 0; i < len; i++) { | ||
525 | if (i && (i % 16) == 0) | ||
526 | printk("\n"); | ||
527 | printk("%02X ", *p++); | ||
528 | } | ||
529 | printk("\n"); | ||
530 | } | ||
531 | |||
532 | /** | ||
533 | * gsm_send - send a control frame | ||
534 | * @gsm: our GSM mux | ||
535 | * @addr: address for control frame | ||
536 | * @cr: command/response bit | ||
537 | * @control: control byte including PF bit | ||
538 | * | ||
539 | * Format up and transmit a control frame. These do not go via the | ||
540 | * queueing logic as they should be transmitted ahead of data when | ||
541 | * they are needed. | ||
542 | * | ||
543 | * FIXME: Lock versus data TX path | ||
544 | */ | ||
545 | |||
546 | static void gsm_send(struct gsm_mux *gsm, int addr, int cr, int control) | ||
547 | { | ||
548 | int len; | ||
549 | u8 cbuf[10]; | ||
550 | u8 ibuf[3]; | ||
551 | |||
552 | switch (gsm->encoding) { | ||
553 | case 0: | ||
554 | cbuf[0] = GSM0_SOF; | ||
555 | cbuf[1] = (addr << 2) | (cr << 1) | EA; | ||
556 | cbuf[2] = control; | ||
557 | cbuf[3] = EA; /* Length of data = 0 */ | ||
558 | cbuf[4] = 0xFF - gsm_fcs_add_block(INIT_FCS, cbuf + 1, 3); | ||
559 | cbuf[5] = GSM0_SOF; | ||
560 | len = 6; | ||
561 | break; | ||
562 | case 1: | ||
563 | case 2: | ||
564 | /* Control frame + packing (but not frame stuffing) in mode 1 */ | ||
565 | ibuf[0] = (addr << 2) | (cr << 1) | EA; | ||
566 | ibuf[1] = control; | ||
567 | ibuf[2] = 0xFF - gsm_fcs_add_block(INIT_FCS, ibuf, 2); | ||
568 | /* Stuffing may double the size worst case */ | ||
569 | len = gsm_stuff_frame(ibuf, cbuf + 1, 3); | ||
570 | /* Now add the SOF markers */ | ||
571 | cbuf[0] = GSM1_SOF; | ||
572 | cbuf[len + 1] = GSM1_SOF; | ||
573 | /* FIXME: we can omit the lead one in many cases */ | ||
574 | len += 2; | ||
575 | break; | ||
576 | default: | ||
577 | WARN_ON(1); | ||
578 | return; | ||
579 | } | ||
580 | gsm->output(gsm, cbuf, len); | ||
581 | gsm_print_packet("-->", addr, cr, control, NULL, 0); | ||
582 | } | ||
583 | |||
584 | /** | ||
585 | * gsm_response - send a control response | ||
586 | * @gsm: our GSM mux | ||
587 | * @addr: address for control frame | ||
588 | * @control: control byte including PF bit | ||
589 | * | ||
590 | * Format up and transmit a link level response frame. | ||
591 | */ | ||
592 | |||
593 | static inline void gsm_response(struct gsm_mux *gsm, int addr, int control) | ||
594 | { | ||
595 | gsm_send(gsm, addr, 0, control); | ||
596 | } | ||
597 | |||
598 | /** | ||
599 | * gsm_command - send a control command | ||
600 | * @gsm: our GSM mux | ||
601 | * @addr: address for control frame | ||
602 | * @control: control byte including PF bit | ||
603 | * | ||
604 | * Format up and transmit a link level command frame. | ||
605 | */ | ||
606 | |||
607 | static inline void gsm_command(struct gsm_mux *gsm, int addr, int control) | ||
608 | { | ||
609 | gsm_send(gsm, addr, 1, control); | ||
610 | } | ||
611 | |||
612 | /* Data transmission */ | ||
613 | |||
614 | #define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */ | ||
615 | |||
616 | /** | ||
617 | * gsm_data_alloc - allocate data frame | ||
618 | * @gsm: GSM mux | ||
619 | * @addr: DLCI address | ||
620 | * @len: length excluding header and FCS | ||
621 | * @ctrl: control byte | ||
622 | * | ||
623 | * Allocate a new data buffer for sending frames with data. Space is left | ||
624 | * at the front for header bytes but that is treated as an implementation | ||
625 | * detail and not for the high level code to use | ||
626 | */ | ||
627 | |||
628 | static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len, | ||
629 | u8 ctrl) | ||
630 | { | ||
631 | struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN, | ||
632 | GFP_ATOMIC); | ||
633 | if (m == NULL) | ||
634 | return NULL; | ||
635 | m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */ | ||
636 | m->len = len; | ||
637 | m->addr = addr; | ||
638 | m->ctrl = ctrl; | ||
639 | m->next = NULL; | ||
640 | return m; | ||
641 | } | ||
642 | |||
643 | /** | ||
644 | * gsm_data_kick - poke the queue | ||
645 | * @gsm: GSM Mux | ||
646 | * | ||
647 | * The tty device has called us to indicate that room has appeared in | ||
648 | * the transmit queue. Ram more data into the pipe if we have any | ||
649 | * | ||
650 | * FIXME: lock against link layer control transmissions | ||
651 | */ | ||
652 | |||
653 | static void gsm_data_kick(struct gsm_mux *gsm) | ||
654 | { | ||
655 | struct gsm_msg *msg = gsm->tx_head; | ||
656 | int len; | ||
657 | int skip_sof = 0; | ||
658 | |||
659 | /* FIXME: We need to apply this solely to data messages */ | ||
660 | if (gsm->constipated) | ||
661 | return; | ||
662 | |||
663 | while (gsm->tx_head != NULL) { | ||
664 | msg = gsm->tx_head; | ||
665 | if (gsm->encoding != 0) { | ||
666 | gsm->txframe[0] = GSM1_SOF; | ||
667 | len = gsm_stuff_frame(msg->data, | ||
668 | gsm->txframe + 1, msg->len); | ||
669 | gsm->txframe[len + 1] = GSM1_SOF; | ||
670 | len += 2; | ||
671 | } else { | ||
672 | gsm->txframe[0] = GSM0_SOF; | ||
673 | memcpy(gsm->txframe + 1 , msg->data, msg->len); | ||
674 | gsm->txframe[msg->len + 1] = GSM0_SOF; | ||
675 | len = msg->len + 2; | ||
676 | } | ||
677 | |||
678 | if (debug & 4) { | ||
679 | printk("gsm_data_kick: \n"); | ||
680 | hex_packet(gsm->txframe, len); | ||
681 | } | ||
682 | |||
683 | if (gsm->output(gsm, gsm->txframe + skip_sof, | ||
684 | len - skip_sof) < 0) | ||
685 | break; | ||
686 | /* FIXME: Can eliminate one SOF in many more cases */ | ||
687 | gsm->tx_head = msg->next; | ||
688 | if (gsm->tx_head == NULL) | ||
689 | gsm->tx_tail = NULL; | ||
690 | gsm->tx_bytes -= msg->len; | ||
691 | kfree(msg); | ||
692 | /* For a burst of frames skip the extra SOF within the | ||
693 | burst */ | ||
694 | skip_sof = 1; | ||
695 | } | ||
696 | } | ||
697 | |||
698 | /** | ||
699 | * __gsm_data_queue - queue a UI or UIH frame | ||
700 | * @dlci: DLCI sending the data | ||
701 | * @msg: message queued | ||
702 | * | ||
703 | * Add data to the transmit queue and try and get stuff moving | ||
704 | * out of the mux tty if not already doing so. The Caller must hold | ||
705 | * the gsm tx lock. | ||
706 | */ | ||
707 | |||
708 | static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg) | ||
709 | { | ||
710 | struct gsm_mux *gsm = dlci->gsm; | ||
711 | u8 *dp = msg->data; | ||
712 | u8 *fcs = dp + msg->len; | ||
713 | |||
714 | /* Fill in the header */ | ||
715 | if (gsm->encoding == 0) { | ||
716 | if (msg->len < 128) | ||
717 | *--dp = (msg->len << 1) | EA; | ||
718 | else { | ||
719 | *--dp = ((msg->len & 127) << 1) | EA; | ||
720 | *--dp = (msg->len >> 6) & 0xfe; | ||
721 | } | ||
722 | } | ||
723 | |||
724 | *--dp = msg->ctrl; | ||
725 | if (gsm->initiator) | ||
726 | *--dp = (msg->addr << 2) | 2 | EA; | ||
727 | else | ||
728 | *--dp = (msg->addr << 2) | EA; | ||
729 | *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp); | ||
730 | /* Ugly protocol layering violation */ | ||
731 | if (msg->ctrl == UI || msg->ctrl == (UI|PF)) | ||
732 | *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len); | ||
733 | *fcs = 0xFF - *fcs; | ||
734 | |||
735 | gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl, | ||
736 | msg->data, msg->len); | ||
737 | |||
738 | /* Move the header back and adjust the length, also allow for the FCS | ||
739 | now tacked on the end */ | ||
740 | msg->len += (msg->data - dp) + 1; | ||
741 | msg->data = dp; | ||
742 | |||
743 | /* Add to the actual output queue */ | ||
744 | if (gsm->tx_tail) | ||
745 | gsm->tx_tail->next = msg; | ||
746 | else | ||
747 | gsm->tx_head = msg; | ||
748 | gsm->tx_tail = msg; | ||
749 | gsm->tx_bytes += msg->len; | ||
750 | gsm_data_kick(gsm); | ||
751 | } | ||
752 | |||
753 | /** | ||
754 | * gsm_data_queue - queue a UI or UIH frame | ||
755 | * @dlci: DLCI sending the data | ||
756 | * @msg: message queued | ||
757 | * | ||
758 | * Add data to the transmit queue and try and get stuff moving | ||
759 | * out of the mux tty if not already doing so. Take the | ||
760 | * the gsm tx lock and dlci lock. | ||
761 | */ | ||
762 | |||
763 | static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg) | ||
764 | { | ||
765 | unsigned long flags; | ||
766 | spin_lock_irqsave(&dlci->gsm->tx_lock, flags); | ||
767 | __gsm_data_queue(dlci, msg); | ||
768 | spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags); | ||
769 | } | ||
770 | |||
771 | /** | ||
772 | * gsm_dlci_data_output - try and push data out of a DLCI | ||
773 | * @gsm: mux | ||
774 | * @dlci: the DLCI to pull data from | ||
775 | * | ||
776 | * Pull data from a DLCI and send it into the transmit queue if there | ||
777 | * is data. Keep to the MRU of the mux. This path handles the usual tty | ||
778 | * interface which is a byte stream with optional modem data. | ||
779 | * | ||
780 | * Caller must hold the tx_lock of the mux. | ||
781 | */ | ||
782 | |||
783 | static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci) | ||
784 | { | ||
785 | struct gsm_msg *msg; | ||
786 | u8 *dp; | ||
787 | int len, size; | ||
788 | int h = dlci->adaption - 1; | ||
789 | |||
790 | len = kfifo_len(dlci->fifo); | ||
791 | if (len == 0) | ||
792 | return 0; | ||
793 | |||
794 | /* MTU/MRU count only the data bits */ | ||
795 | if (len > gsm->mtu) | ||
796 | len = gsm->mtu; | ||
797 | |||
798 | size = len + h; | ||
799 | |||
800 | msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype); | ||
801 | /* FIXME: need a timer or something to kick this so it can't | ||
802 | get stuck with no work outstanding and no buffer free */ | ||
803 | if (msg == NULL) | ||
804 | return -ENOMEM; | ||
805 | dp = msg->data; | ||
806 | switch (dlci->adaption) { | ||
807 | case 1: /* Unstructured */ | ||
808 | break; | ||
809 | case 2: /* Unstructed with modem bits. Always one byte as we never | ||
810 | send inline break data */ | ||
811 | *dp += gsm_encode_modem(dlci); | ||
812 | len--; | ||
813 | break; | ||
814 | } | ||
815 | WARN_ON(kfifo_out_locked(dlci->fifo, dp , len, &dlci->lock) != len); | ||
816 | __gsm_data_queue(dlci, msg); | ||
817 | /* Bytes of data we used up */ | ||
818 | return size; | ||
819 | } | ||
820 | |||
821 | /** | ||
822 | * gsm_dlci_data_output_framed - try and push data out of a DLCI | ||
823 | * @gsm: mux | ||
824 | * @dlci: the DLCI to pull data from | ||
825 | * | ||
826 | * Pull data from a DLCI and send it into the transmit queue if there | ||
827 | * is data. Keep to the MRU of the mux. This path handles framed data | ||
828 | * queued as skbuffs to the DLCI. | ||
829 | * | ||
830 | * Caller must hold the tx_lock of the mux. | ||
831 | */ | ||
832 | |||
833 | static int gsm_dlci_data_output_framed(struct gsm_mux *gsm, | ||
834 | struct gsm_dlci *dlci) | ||
835 | { | ||
836 | struct gsm_msg *msg; | ||
837 | u8 *dp; | ||
838 | int len, size; | ||
839 | int last = 0, first = 0; | ||
840 | int overhead = 0; | ||
841 | |||
842 | /* One byte per frame is used for B/F flags */ | ||
843 | if (dlci->adaption == 4) | ||
844 | overhead = 1; | ||
845 | |||
846 | /* dlci->skb is locked by tx_lock */ | ||
847 | if (dlci->skb == NULL) { | ||
848 | dlci->skb = skb_dequeue(&dlci->skb_list); | ||
849 | if (dlci->skb == NULL) | ||
850 | return 0; | ||
851 | first = 1; | ||
852 | } | ||
853 | len = dlci->skb->len + overhead; | ||
854 | |||
855 | /* MTU/MRU count only the data bits */ | ||
856 | if (len > gsm->mtu) { | ||
857 | if (dlci->adaption == 3) { | ||
858 | /* Over long frame, bin it */ | ||
859 | kfree_skb(dlci->skb); | ||
860 | dlci->skb = NULL; | ||
861 | return 0; | ||
862 | } | ||
863 | len = gsm->mtu; | ||
864 | } else | ||
865 | last = 1; | ||
866 | |||
867 | size = len + overhead; | ||
868 | msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype); | ||
869 | |||
870 | /* FIXME: need a timer or something to kick this so it can't | ||
871 | get stuck with no work outstanding and no buffer free */ | ||
872 | if (msg == NULL) | ||
873 | return -ENOMEM; | ||
874 | dp = msg->data; | ||
875 | |||
876 | if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */ | ||
877 | /* Flag byte to carry the start/end info */ | ||
878 | *dp++ = last << 7 | first << 6 | 1; /* EA */ | ||
879 | len--; | ||
880 | } | ||
881 | memcpy(dp, skb_pull(dlci->skb, len), len); | ||
882 | __gsm_data_queue(dlci, msg); | ||
883 | if (last) | ||
884 | dlci->skb = NULL; | ||
885 | return size; | ||
886 | } | ||
887 | |||
888 | /** | ||
889 | * gsm_dlci_data_sweep - look for data to send | ||
890 | * @gsm: the GSM mux | ||
891 | * | ||
892 | * Sweep the GSM mux channels in priority order looking for ones with | ||
893 | * data to send. We could do with optimising this scan a bit. We aim | ||
894 | * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit | ||
895 | * TX_THRESH_LO we get called again | ||
896 | * | ||
897 | * FIXME: We should round robin between groups and in theory you can | ||
898 | * renegotiate DLCI priorities with optional stuff. Needs optimising. | ||
899 | */ | ||
900 | |||
901 | static void gsm_dlci_data_sweep(struct gsm_mux *gsm) | ||
902 | { | ||
903 | int len; | ||
904 | /* Priority ordering: We should do priority with RR of the groups */ | ||
905 | int i = 1; | ||
906 | |||
907 | while (i < NUM_DLCI) { | ||
908 | struct gsm_dlci *dlci; | ||
909 | |||
910 | if (gsm->tx_bytes > TX_THRESH_HI) | ||
911 | break; | ||
912 | dlci = gsm->dlci[i]; | ||
913 | if (dlci == NULL || dlci->constipated) { | ||
914 | i++; | ||
915 | continue; | ||
916 | } | ||
917 | if (dlci->adaption < 3) | ||
918 | len = gsm_dlci_data_output(gsm, dlci); | ||
919 | else | ||
920 | len = gsm_dlci_data_output_framed(gsm, dlci); | ||
921 | if (len < 0) | ||
922 | break; | ||
923 | /* DLCI empty - try the next */ | ||
924 | if (len == 0) | ||
925 | i++; | ||
926 | } | ||
927 | } | ||
928 | |||
929 | /** | ||
930 | * gsm_dlci_data_kick - transmit if possible | ||
931 | * @dlci: DLCI to kick | ||
932 | * | ||
933 | * Transmit data from this DLCI if the queue is empty. We can't rely on | ||
934 | * a tty wakeup except when we filled the pipe so we need to fire off | ||
935 | * new data ourselves in other cases. | ||
936 | */ | ||
937 | |||
938 | static void gsm_dlci_data_kick(struct gsm_dlci *dlci) | ||
939 | { | ||
940 | unsigned long flags; | ||
941 | |||
942 | spin_lock_irqsave(&dlci->gsm->tx_lock, flags); | ||
943 | /* If we have nothing running then we need to fire up */ | ||
944 | if (dlci->gsm->tx_bytes == 0) | ||
945 | gsm_dlci_data_output(dlci->gsm, dlci); | ||
946 | else if (dlci->gsm->tx_bytes < TX_THRESH_LO) | ||
947 | gsm_dlci_data_sweep(dlci->gsm); | ||
948 | spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags); | ||
949 | } | ||
950 | |||
951 | /* | ||
952 | * Control message processing | ||
953 | */ | ||
954 | |||
955 | |||
956 | /** | ||
957 | * gsm_control_reply - send a response frame to a control | ||
958 | * @gsm: gsm channel | ||
959 | * @cmd: the command to use | ||
960 | * @data: data to follow encoded info | ||
961 | * @dlen: length of data | ||
962 | * | ||
963 | * Encode up and queue a UI/UIH frame containing our response. | ||
964 | */ | ||
965 | |||
966 | static void gsm_control_reply(struct gsm_mux *gsm, int cmd, u8 *data, | ||
967 | int dlen) | ||
968 | { | ||
969 | struct gsm_msg *msg; | ||
970 | msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->ftype); | ||
971 | msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */ | ||
972 | msg->data[1] = (dlen << 1) | EA; | ||
973 | memcpy(msg->data + 2, data, dlen); | ||
974 | gsm_data_queue(gsm->dlci[0], msg); | ||
975 | } | ||
976 | |||
977 | /** | ||
978 | * gsm_process_modem - process received modem status | ||
979 | * @tty: virtual tty bound to the DLCI | ||
980 | * @dlci: DLCI to affect | ||
981 | * @modem: modem bits (full EA) | ||
982 | * | ||
983 | * Used when a modem control message or line state inline in adaption | ||
984 | * layer 2 is processed. Sort out the local modem state and throttles | ||
985 | */ | ||
986 | |||
987 | static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci, | ||
988 | u32 modem) | ||
989 | { | ||
990 | int mlines = 0; | ||
991 | u8 brk = modem >> 6; | ||
992 | |||
993 | /* Flow control/ready to communicate */ | ||
994 | if (modem & MDM_FC) { | ||
995 | /* Need to throttle our output on this device */ | ||
996 | dlci->constipated = 1; | ||
997 | } | ||
998 | if (modem & MDM_RTC) { | ||
999 | mlines |= TIOCM_DSR | TIOCM_DTR; | ||
1000 | dlci->constipated = 0; | ||
1001 | gsm_dlci_data_kick(dlci); | ||
1002 | } | ||
1003 | /* Map modem bits */ | ||
1004 | if (modem & MDM_RTR) | ||
1005 | mlines |= TIOCM_RTS | TIOCM_CTS; | ||
1006 | if (modem & MDM_IC) | ||
1007 | mlines |= TIOCM_RI; | ||
1008 | if (modem & MDM_DV) | ||
1009 | mlines |= TIOCM_CD; | ||
1010 | |||
1011 | /* Carrier drop -> hangup */ | ||
1012 | if (tty) { | ||
1013 | if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD)) | ||
1014 | if (!(tty->termios->c_cflag & CLOCAL)) | ||
1015 | tty_hangup(tty); | ||
1016 | if (brk & 0x01) | ||
1017 | tty_insert_flip_char(tty, 0, TTY_BREAK); | ||
1018 | } | ||
1019 | dlci->modem_rx = mlines; | ||
1020 | } | ||
1021 | |||
1022 | /** | ||
1023 | * gsm_control_modem - modem status received | ||
1024 | * @gsm: GSM channel | ||
1025 | * @data: data following command | ||
1026 | * @clen: command length | ||
1027 | * | ||
1028 | * We have received a modem status control message. This is used by | ||
1029 | * the GSM mux protocol to pass virtual modem line status and optionally | ||
1030 | * to indicate break signals. Unpack it, convert to Linux representation | ||
1031 | * and if need be stuff a break message down the tty. | ||
1032 | */ | ||
1033 | |||
1034 | static void gsm_control_modem(struct gsm_mux *gsm, u8 *data, int clen) | ||
1035 | { | ||
1036 | unsigned int addr = 0; | ||
1037 | unsigned int modem = 0; | ||
1038 | struct gsm_dlci *dlci; | ||
1039 | int len = clen; | ||
1040 | u8 *dp = data; | ||
1041 | struct tty_struct *tty; | ||
1042 | |||
1043 | while (gsm_read_ea(&addr, *dp++) == 0) { | ||
1044 | len--; | ||
1045 | if (len == 0) | ||
1046 | return; | ||
1047 | } | ||
1048 | /* Must be at least one byte following the EA */ | ||
1049 | len--; | ||
1050 | if (len <= 0) | ||
1051 | return; | ||
1052 | |||
1053 | addr >>= 1; | ||
1054 | /* Closed port, or invalid ? */ | ||
1055 | if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL) | ||
1056 | return; | ||
1057 | dlci = gsm->dlci[addr]; | ||
1058 | |||
1059 | while (gsm_read_ea(&modem, *dp++) == 0) { | ||
1060 | len--; | ||
1061 | if (len == 0) | ||
1062 | return; | ||
1063 | } | ||
1064 | tty = tty_port_tty_get(&dlci->port); | ||
1065 | gsm_process_modem(tty, dlci, modem); | ||
1066 | if (tty) { | ||
1067 | tty_wakeup(tty); | ||
1068 | tty_kref_put(tty); | ||
1069 | } | ||
1070 | gsm_control_reply(gsm, CMD_MSC, data, clen); | ||
1071 | } | ||
1072 | |||
1073 | /** | ||
1074 | * gsm_control_rls - remote line status | ||
1075 | * @gsm: GSM channel | ||
1076 | * @data: data bytes | ||
1077 | * @clen: data length | ||
1078 | * | ||
1079 | * The modem sends us a two byte message on the control channel whenever | ||
1080 | * it wishes to send us an error state from the virtual link. Stuff | ||
1081 | * this into the uplink tty if present | ||
1082 | */ | ||
1083 | |||
1084 | static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen) | ||
1085 | { | ||
1086 | struct tty_struct *tty; | ||
1087 | unsigned int addr = 0 ; | ||
1088 | u8 bits; | ||
1089 | int len = clen; | ||
1090 | u8 *dp = data; | ||
1091 | |||
1092 | while (gsm_read_ea(&addr, *dp++) == 0) { | ||
1093 | len--; | ||
1094 | if (len == 0) | ||
1095 | return; | ||
1096 | } | ||
1097 | /* Must be at least one byte following ea */ | ||
1098 | len--; | ||
1099 | if (len <= 0) | ||
1100 | return; | ||
1101 | addr >>= 1; | ||
1102 | /* Closed port, or invalid ? */ | ||
1103 | if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL) | ||
1104 | return; | ||
1105 | /* No error ? */ | ||
1106 | bits = *dp; | ||
1107 | if ((bits & 1) == 0) | ||
1108 | return; | ||
1109 | /* See if we have an uplink tty */ | ||
1110 | tty = tty_port_tty_get(&gsm->dlci[addr]->port); | ||
1111 | |||
1112 | if (tty) { | ||
1113 | if (bits & 2) | ||
1114 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); | ||
1115 | if (bits & 4) | ||
1116 | tty_insert_flip_char(tty, 0, TTY_PARITY); | ||
1117 | if (bits & 8) | ||
1118 | tty_insert_flip_char(tty, 0, TTY_FRAME); | ||
1119 | tty_flip_buffer_push(tty); | ||
1120 | tty_kref_put(tty); | ||
1121 | } | ||
1122 | gsm_control_reply(gsm, CMD_RLS, data, clen); | ||
1123 | } | ||
1124 | |||
1125 | static void gsm_dlci_begin_close(struct gsm_dlci *dlci); | ||
1126 | |||
1127 | /** | ||
1128 | * gsm_control_message - DLCI 0 control processing | ||
1129 | * @gsm: our GSM mux | ||
1130 | * @command: the command EA | ||
1131 | * @data: data beyond the command/length EAs | ||
1132 | * @clen: length | ||
1133 | * | ||
1134 | * Input processor for control messages from the other end of the link. | ||
1135 | * Processes the incoming request and queues a response frame or an | ||
1136 | * NSC response if not supported | ||
1137 | */ | ||
1138 | |||
1139 | static void gsm_control_message(struct gsm_mux *gsm, unsigned int command, | ||
1140 | u8 *data, int clen) | ||
1141 | { | ||
1142 | u8 buf[1]; | ||
1143 | switch (command) { | ||
1144 | case CMD_CLD: { | ||
1145 | struct gsm_dlci *dlci = gsm->dlci[0]; | ||
1146 | /* Modem wishes to close down */ | ||
1147 | if (dlci) { | ||
1148 | dlci->dead = 1; | ||
1149 | gsm->dead = 1; | ||
1150 | gsm_dlci_begin_close(dlci); | ||
1151 | } | ||
1152 | } | ||
1153 | break; | ||
1154 | case CMD_TEST: | ||
1155 | /* Modem wishes to test, reply with the data */ | ||
1156 | gsm_control_reply(gsm, CMD_TEST, data, clen); | ||
1157 | break; | ||
1158 | case CMD_FCON: | ||
1159 | /* Modem wants us to STFU */ | ||
1160 | gsm->constipated = 1; | ||
1161 | gsm_control_reply(gsm, CMD_FCON, NULL, 0); | ||
1162 | break; | ||
1163 | case CMD_FCOFF: | ||
1164 | /* Modem can accept data again */ | ||
1165 | gsm->constipated = 0; | ||
1166 | gsm_control_reply(gsm, CMD_FCOFF, NULL, 0); | ||
1167 | /* Kick the link in case it is idling */ | ||
1168 | gsm_data_kick(gsm); | ||
1169 | break; | ||
1170 | case CMD_MSC: | ||
1171 | /* Out of band modem line change indicator for a DLCI */ | ||
1172 | gsm_control_modem(gsm, data, clen); | ||
1173 | break; | ||
1174 | case CMD_RLS: | ||
1175 | /* Out of band error reception for a DLCI */ | ||
1176 | gsm_control_rls(gsm, data, clen); | ||
1177 | break; | ||
1178 | case CMD_PSC: | ||
1179 | /* Modem wishes to enter power saving state */ | ||
1180 | gsm_control_reply(gsm, CMD_PSC, NULL, 0); | ||
1181 | break; | ||
1182 | /* Optional unsupported commands */ | ||
1183 | case CMD_PN: /* Parameter negotiation */ | ||
1184 | case CMD_RPN: /* Remote port negotation */ | ||
1185 | case CMD_SNC: /* Service negotation command */ | ||
1186 | default: | ||
1187 | /* Reply to bad commands with an NSC */ | ||
1188 | buf[0] = command; | ||
1189 | gsm_control_reply(gsm, CMD_NSC, buf, 1); | ||
1190 | break; | ||
1191 | } | ||
1192 | } | ||
1193 | |||
1194 | /** | ||
1195 | * gsm_control_response - process a response to our control | ||
1196 | * @gsm: our GSM mux | ||
1197 | * @command: the command (response) EA | ||
1198 | * @data: data beyond the command/length EA | ||
1199 | * @clen: length | ||
1200 | * | ||
1201 | * Process a response to an outstanding command. We only allow a single | ||
1202 | * control message in flight so this is fairly easy. All the clean up | ||
1203 | * is done by the caller, we just update the fields, flag it as done | ||
1204 | * and return | ||
1205 | */ | ||
1206 | |||
1207 | static void gsm_control_response(struct gsm_mux *gsm, unsigned int command, | ||
1208 | u8 *data, int clen) | ||
1209 | { | ||
1210 | struct gsm_control *ctrl; | ||
1211 | unsigned long flags; | ||
1212 | |||
1213 | spin_lock_irqsave(&gsm->control_lock, flags); | ||
1214 | |||
1215 | ctrl = gsm->pending_cmd; | ||
1216 | /* Does the reply match our command */ | ||
1217 | command |= 1; | ||
1218 | if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) { | ||
1219 | /* Our command was replied to, kill the retry timer */ | ||
1220 | del_timer(&gsm->t2_timer); | ||
1221 | gsm->pending_cmd = NULL; | ||
1222 | /* Rejected by the other end */ | ||
1223 | if (command == CMD_NSC) | ||
1224 | ctrl->error = -EOPNOTSUPP; | ||
1225 | ctrl->done = 1; | ||
1226 | wake_up(&gsm->event); | ||
1227 | } | ||
1228 | spin_unlock_irqrestore(&gsm->control_lock, flags); | ||
1229 | } | ||
1230 | |||
1231 | /** | ||
1232 | * gsm_control_transmit - send control packet | ||
1233 | * @gsm: gsm mux | ||
1234 | * @ctrl: frame to send | ||
1235 | * | ||
1236 | * Send out a pending control command (called under control lock) | ||
1237 | */ | ||
1238 | |||
1239 | static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl) | ||
1240 | { | ||
1241 | struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 1, | ||
1242 | gsm->ftype|PF); | ||
1243 | if (msg == NULL) | ||
1244 | return; | ||
1245 | msg->data[0] = (ctrl->cmd << 1) | 2 | EA; /* command */ | ||
1246 | memcpy(msg->data + 1, ctrl->data, ctrl->len); | ||
1247 | gsm_data_queue(gsm->dlci[0], msg); | ||
1248 | } | ||
1249 | |||
1250 | /** | ||
1251 | * gsm_control_retransmit - retransmit a control frame | ||
1252 | * @data: pointer to our gsm object | ||
1253 | * | ||
1254 | * Called off the T2 timer expiry in order to retransmit control frames | ||
1255 | * that have been lost in the system somewhere. The control_lock protects | ||
1256 | * us from colliding with another sender or a receive completion event. | ||
1257 | * In that situation the timer may still occur in a small window but | ||
1258 | * gsm->pending_cmd will be NULL and we just let the timer expire. | ||
1259 | */ | ||
1260 | |||
1261 | static void gsm_control_retransmit(unsigned long data) | ||
1262 | { | ||
1263 | struct gsm_mux *gsm = (struct gsm_mux *)data; | ||
1264 | struct gsm_control *ctrl; | ||
1265 | unsigned long flags; | ||
1266 | spin_lock_irqsave(&gsm->control_lock, flags); | ||
1267 | ctrl = gsm->pending_cmd; | ||
1268 | if (ctrl) { | ||
1269 | gsm->cretries--; | ||
1270 | if (gsm->cretries == 0) { | ||
1271 | gsm->pending_cmd = NULL; | ||
1272 | ctrl->error = -ETIMEDOUT; | ||
1273 | ctrl->done = 1; | ||
1274 | spin_unlock_irqrestore(&gsm->control_lock, flags); | ||
1275 | wake_up(&gsm->event); | ||
1276 | return; | ||
1277 | } | ||
1278 | gsm_control_transmit(gsm, ctrl); | ||
1279 | mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100); | ||
1280 | } | ||
1281 | spin_unlock_irqrestore(&gsm->control_lock, flags); | ||
1282 | } | ||
1283 | |||
1284 | /** | ||
1285 | * gsm_control_send - send a control frame on DLCI 0 | ||
1286 | * @gsm: the GSM channel | ||
1287 | * @command: command to send including CR bit | ||
1288 | * @data: bytes of data (must be kmalloced) | ||
1289 | * @len: length of the block to send | ||
1290 | * | ||
1291 | * Queue and dispatch a control command. Only one command can be | ||
1292 | * active at a time. In theory more can be outstanding but the matching | ||
1293 | * gets really complicated so for now stick to one outstanding. | ||
1294 | */ | ||
1295 | |||
1296 | static struct gsm_control *gsm_control_send(struct gsm_mux *gsm, | ||
1297 | unsigned int command, u8 *data, int clen) | ||
1298 | { | ||
1299 | struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control), | ||
1300 | GFP_KERNEL); | ||
1301 | unsigned long flags; | ||
1302 | if (ctrl == NULL) | ||
1303 | return NULL; | ||
1304 | retry: | ||
1305 | wait_event(gsm->event, gsm->pending_cmd == NULL); | ||
1306 | spin_lock_irqsave(&gsm->control_lock, flags); | ||
1307 | if (gsm->pending_cmd != NULL) { | ||
1308 | spin_unlock_irqrestore(&gsm->control_lock, flags); | ||
1309 | goto retry; | ||
1310 | } | ||
1311 | ctrl->cmd = command; | ||
1312 | ctrl->data = data; | ||
1313 | ctrl->len = clen; | ||
1314 | gsm->pending_cmd = ctrl; | ||
1315 | gsm->cretries = gsm->n2; | ||
1316 | mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100); | ||
1317 | gsm_control_transmit(gsm, ctrl); | ||
1318 | spin_unlock_irqrestore(&gsm->control_lock, flags); | ||
1319 | return ctrl; | ||
1320 | } | ||
1321 | |||
1322 | /** | ||
1323 | * gsm_control_wait - wait for a control to finish | ||
1324 | * @gsm: GSM mux | ||
1325 | * @control: control we are waiting on | ||
1326 | * | ||
1327 | * Waits for the control to complete or time out. Frees any used | ||
1328 | * resources and returns 0 for success, or an error if the remote | ||
1329 | * rejected or ignored the request. | ||
1330 | */ | ||
1331 | |||
1332 | static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control) | ||
1333 | { | ||
1334 | int err; | ||
1335 | wait_event(gsm->event, control->done == 1); | ||
1336 | err = control->error; | ||
1337 | kfree(control); | ||
1338 | return err; | ||
1339 | } | ||
1340 | |||
1341 | |||
1342 | /* | ||
1343 | * DLCI level handling: Needs krefs | ||
1344 | */ | ||
1345 | |||
1346 | /* | ||
1347 | * State transitions and timers | ||
1348 | */ | ||
1349 | |||
1350 | /** | ||
1351 | * gsm_dlci_close - a DLCI has closed | ||
1352 | * @dlci: DLCI that closed | ||
1353 | * | ||
1354 | * Perform processing when moving a DLCI into closed state. If there | ||
1355 | * is an attached tty this is hung up | ||
1356 | */ | ||
1357 | |||
1358 | static void gsm_dlci_close(struct gsm_dlci *dlci) | ||
1359 | { | ||
1360 | del_timer(&dlci->t1); | ||
1361 | if (debug & 8) | ||
1362 | printk("DLCI %d goes closed.\n", dlci->addr); | ||
1363 | dlci->state = DLCI_CLOSED; | ||
1364 | if (dlci->addr != 0) { | ||
1365 | struct tty_struct *tty = tty_port_tty_get(&dlci->port); | ||
1366 | if (tty) { | ||
1367 | tty_hangup(tty); | ||
1368 | tty_kref_put(tty); | ||
1369 | } | ||
1370 | kfifo_reset(dlci->fifo); | ||
1371 | } else | ||
1372 | dlci->gsm->dead = 1; | ||
1373 | wake_up(&dlci->gsm->event); | ||
1374 | /* A DLCI 0 close is a MUX termination so we need to kick that | ||
1375 | back to userspace somehow */ | ||
1376 | } | ||
1377 | |||
1378 | /** | ||
1379 | * gsm_dlci_open - a DLCI has opened | ||
1380 | * @dlci: DLCI that opened | ||
1381 | * | ||
1382 | * Perform processing when moving a DLCI into open state. | ||
1383 | */ | ||
1384 | |||
1385 | static void gsm_dlci_open(struct gsm_dlci *dlci) | ||
1386 | { | ||
1387 | /* Note that SABM UA .. SABM UA first UA lost can mean that we go | ||
1388 | open -> open */ | ||
1389 | del_timer(&dlci->t1); | ||
1390 | /* This will let a tty open continue */ | ||
1391 | dlci->state = DLCI_OPEN; | ||
1392 | if (debug & 8) | ||
1393 | printk("DLCI %d goes open.\n", dlci->addr); | ||
1394 | wake_up(&dlci->gsm->event); | ||
1395 | } | ||
1396 | |||
1397 | /** | ||
1398 | * gsm_dlci_t1 - T1 timer expiry | ||
1399 | * @dlci: DLCI that opened | ||
1400 | * | ||
1401 | * The T1 timer handles retransmits of control frames (essentially of | ||
1402 | * SABM and DISC). We resend the command until the retry count runs out | ||
1403 | * in which case an opening port goes back to closed and a closing port | ||
1404 | * is simply put into closed state (any further frames from the other | ||
1405 | * end will get a DM response) | ||
1406 | */ | ||
1407 | |||
1408 | static void gsm_dlci_t1(unsigned long data) | ||
1409 | { | ||
1410 | struct gsm_dlci *dlci = (struct gsm_dlci *)data; | ||
1411 | struct gsm_mux *gsm = dlci->gsm; | ||
1412 | |||
1413 | switch (dlci->state) { | ||
1414 | case DLCI_OPENING: | ||
1415 | dlci->retries--; | ||
1416 | if (dlci->retries) { | ||
1417 | gsm_command(dlci->gsm, dlci->addr, SABM|PF); | ||
1418 | mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100); | ||
1419 | } else | ||
1420 | gsm_dlci_close(dlci); | ||
1421 | break; | ||
1422 | case DLCI_CLOSING: | ||
1423 | dlci->retries--; | ||
1424 | if (dlci->retries) { | ||
1425 | gsm_command(dlci->gsm, dlci->addr, DISC|PF); | ||
1426 | mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100); | ||
1427 | } else | ||
1428 | gsm_dlci_close(dlci); | ||
1429 | break; | ||
1430 | } | ||
1431 | } | ||
1432 | |||
1433 | /** | ||
1434 | * gsm_dlci_begin_open - start channel open procedure | ||
1435 | * @dlci: DLCI to open | ||
1436 | * | ||
1437 | * Commence opening a DLCI from the Linux side. We issue SABM messages | ||
1438 | * to the modem which should then reply with a UA, at which point we | ||
1439 | * will move into open state. Opening is done asynchronously with retry | ||
1440 | * running off timers and the responses. | ||
1441 | */ | ||
1442 | |||
1443 | static void gsm_dlci_begin_open(struct gsm_dlci *dlci) | ||
1444 | { | ||
1445 | struct gsm_mux *gsm = dlci->gsm; | ||
1446 | if (dlci->state == DLCI_OPEN || dlci->state == DLCI_OPENING) | ||
1447 | return; | ||
1448 | dlci->retries = gsm->n2; | ||
1449 | dlci->state = DLCI_OPENING; | ||
1450 | gsm_command(dlci->gsm, dlci->addr, SABM|PF); | ||
1451 | mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100); | ||
1452 | } | ||
1453 | |||
1454 | /** | ||
1455 | * gsm_dlci_begin_close - start channel open procedure | ||
1456 | * @dlci: DLCI to open | ||
1457 | * | ||
1458 | * Commence closing a DLCI from the Linux side. We issue DISC messages | ||
1459 | * to the modem which should then reply with a UA, at which point we | ||
1460 | * will move into closed state. Closing is done asynchronously with retry | ||
1461 | * off timers. We may also receive a DM reply from the other end which | ||
1462 | * indicates the channel was already closed. | ||
1463 | */ | ||
1464 | |||
1465 | static void gsm_dlci_begin_close(struct gsm_dlci *dlci) | ||
1466 | { | ||
1467 | struct gsm_mux *gsm = dlci->gsm; | ||
1468 | if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING) | ||
1469 | return; | ||
1470 | dlci->retries = gsm->n2; | ||
1471 | dlci->state = DLCI_CLOSING; | ||
1472 | gsm_command(dlci->gsm, dlci->addr, DISC|PF); | ||
1473 | mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100); | ||
1474 | } | ||
1475 | |||
1476 | /** | ||
1477 | * gsm_dlci_data - data arrived | ||
1478 | * @dlci: channel | ||
1479 | * @data: block of bytes received | ||
1480 | * @len: length of received block | ||
1481 | * | ||
1482 | * A UI or UIH frame has arrived which contains data for a channel | ||
1483 | * other than the control channel. If the relevant virtual tty is | ||
1484 | * open we shovel the bits down it, if not we drop them. | ||
1485 | */ | ||
1486 | |||
1487 | static void gsm_dlci_data(struct gsm_dlci *dlci, u8 *data, int len) | ||
1488 | { | ||
1489 | /* krefs .. */ | ||
1490 | struct tty_port *port = &dlci->port; | ||
1491 | struct tty_struct *tty = tty_port_tty_get(port); | ||
1492 | unsigned int modem = 0; | ||
1493 | |||
1494 | if (debug & 16) | ||
1495 | printk("%d bytes for tty %p\n", len, tty); | ||
1496 | if (tty) { | ||
1497 | switch (dlci->adaption) { | ||
1498 | /* Unsupported types */ | ||
1499 | /* Packetised interruptible data */ | ||
1500 | case 4: | ||
1501 | break; | ||
1502 | /* Packetised uininterruptible voice/data */ | ||
1503 | case 3: | ||
1504 | break; | ||
1505 | /* Asynchronous serial with line state in each frame */ | ||
1506 | case 2: | ||
1507 | while (gsm_read_ea(&modem, *data++) == 0) { | ||
1508 | len--; | ||
1509 | if (len == 0) | ||
1510 | return; | ||
1511 | } | ||
1512 | gsm_process_modem(tty, dlci, modem); | ||
1513 | /* Line state will go via DLCI 0 controls only */ | ||
1514 | case 1: | ||
1515 | default: | ||
1516 | tty_insert_flip_string(tty, data, len); | ||
1517 | tty_flip_buffer_push(tty); | ||
1518 | } | ||
1519 | tty_kref_put(tty); | ||
1520 | } | ||
1521 | } | ||
1522 | |||
1523 | /** | ||
1524 | * gsm_dlci_control - data arrived on control channel | ||
1525 | * @dlci: channel | ||
1526 | * @data: block of bytes received | ||
1527 | * @len: length of received block | ||
1528 | * | ||
1529 | * A UI or UIH frame has arrived which contains data for DLCI 0 the | ||
1530 | * control channel. This should contain a command EA followed by | ||
1531 | * control data bytes. The command EA contains a command/response bit | ||
1532 | * and we divide up the work accordingly. | ||
1533 | */ | ||
1534 | |||
1535 | static void gsm_dlci_command(struct gsm_dlci *dlci, u8 *data, int len) | ||
1536 | { | ||
1537 | /* See what command is involved */ | ||
1538 | unsigned int command = 0; | ||
1539 | while (len-- > 0) { | ||
1540 | if (gsm_read_ea(&command, *data++) == 1) { | ||
1541 | int clen = *data++; | ||
1542 | len--; | ||
1543 | /* FIXME: this is properly an EA */ | ||
1544 | clen >>= 1; | ||
1545 | /* Malformed command ? */ | ||
1546 | if (clen > len) | ||
1547 | return; | ||
1548 | if (command & 1) | ||
1549 | gsm_control_message(dlci->gsm, command, | ||
1550 | data, clen); | ||
1551 | else | ||
1552 | gsm_control_response(dlci->gsm, command, | ||
1553 | data, clen); | ||
1554 | return; | ||
1555 | } | ||
1556 | } | ||
1557 | } | ||
1558 | |||
1559 | /* | ||
1560 | * Allocate/Free DLCI channels | ||
1561 | */ | ||
1562 | |||
1563 | /** | ||
1564 | * gsm_dlci_alloc - allocate a DLCI | ||
1565 | * @gsm: GSM mux | ||
1566 | * @addr: address of the DLCI | ||
1567 | * | ||
1568 | * Allocate and install a new DLCI object into the GSM mux. | ||
1569 | * | ||
1570 | * FIXME: review locking races | ||
1571 | */ | ||
1572 | |||
1573 | static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr) | ||
1574 | { | ||
1575 | struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC); | ||
1576 | if (dlci == NULL) | ||
1577 | return NULL; | ||
1578 | spin_lock_init(&dlci->lock); | ||
1579 | dlci->fifo = &dlci->_fifo; | ||
1580 | if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) { | ||
1581 | kfree(dlci); | ||
1582 | return NULL; | ||
1583 | } | ||
1584 | |||
1585 | skb_queue_head_init(&dlci->skb_list); | ||
1586 | init_timer(&dlci->t1); | ||
1587 | dlci->t1.function = gsm_dlci_t1; | ||
1588 | dlci->t1.data = (unsigned long)dlci; | ||
1589 | tty_port_init(&dlci->port); | ||
1590 | dlci->port.ops = &gsm_port_ops; | ||
1591 | dlci->gsm = gsm; | ||
1592 | dlci->addr = addr; | ||
1593 | dlci->adaption = gsm->adaption; | ||
1594 | dlci->state = DLCI_CLOSED; | ||
1595 | if (addr) | ||
1596 | dlci->data = gsm_dlci_data; | ||
1597 | else | ||
1598 | dlci->data = gsm_dlci_command; | ||
1599 | gsm->dlci[addr] = dlci; | ||
1600 | return dlci; | ||
1601 | } | ||
1602 | |||
1603 | /** | ||
1604 | * gsm_dlci_free - release DLCI | ||
1605 | * @dlci: DLCI to destroy | ||
1606 | * | ||
1607 | * Free up a DLCI. Currently to keep the lifetime rules sane we only | ||
1608 | * clean up DLCI objects when the MUX closes rather than as the port | ||
1609 | * is closed down on both the tty and mux levels. | ||
1610 | * | ||
1611 | * Can sleep. | ||
1612 | */ | ||
1613 | static void gsm_dlci_free(struct gsm_dlci *dlci) | ||
1614 | { | ||
1615 | struct tty_struct *tty = tty_port_tty_get(&dlci->port); | ||
1616 | if (tty) { | ||
1617 | tty_vhangup(tty); | ||
1618 | tty_kref_put(tty); | ||
1619 | } | ||
1620 | del_timer_sync(&dlci->t1); | ||
1621 | dlci->gsm->dlci[dlci->addr] = NULL; | ||
1622 | kfifo_free(dlci->fifo); | ||
1623 | kfree(dlci); | ||
1624 | } | ||
1625 | |||
1626 | |||
1627 | /* | ||
1628 | * LAPBish link layer logic | ||
1629 | */ | ||
1630 | |||
1631 | /** | ||
1632 | * gsm_queue - a GSM frame is ready to process | ||
1633 | * @gsm: pointer to our gsm mux | ||
1634 | * | ||
1635 | * At this point in time a frame has arrived and been demangled from | ||
1636 | * the line encoding. All the differences between the encodings have | ||
1637 | * been handled below us and the frame is unpacked into the structures. | ||
1638 | * The fcs holds the header FCS but any data FCS must be added here. | ||
1639 | */ | ||
1640 | |||
1641 | static void gsm_queue(struct gsm_mux *gsm) | ||
1642 | { | ||
1643 | struct gsm_dlci *dlci; | ||
1644 | u8 cr; | ||
1645 | int address; | ||
1646 | /* We have to sneak a look at the packet body to do the FCS. | ||
1647 | A somewhat layering violation in the spec */ | ||
1648 | |||
1649 | if ((gsm->control & ~PF) == UI) | ||
1650 | gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len); | ||
1651 | if (gsm->fcs != GOOD_FCS) { | ||
1652 | gsm->bad_fcs++; | ||
1653 | if (debug & 4) | ||
1654 | printk("BAD FCS %02x\n", gsm->fcs); | ||
1655 | return; | ||
1656 | } | ||
1657 | address = gsm->address >> 1; | ||
1658 | if (address >= NUM_DLCI) | ||
1659 | goto invalid; | ||
1660 | |||
1661 | cr = gsm->address & 1; /* C/R bit */ | ||
1662 | |||
1663 | gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len); | ||
1664 | |||
1665 | cr ^= 1 - gsm->initiator; /* Flip so 1 always means command */ | ||
1666 | dlci = gsm->dlci[address]; | ||
1667 | |||
1668 | switch (gsm->control) { | ||
1669 | case SABM|PF: | ||
1670 | if (cr == 0) | ||
1671 | goto invalid; | ||
1672 | if (dlci == NULL) | ||
1673 | dlci = gsm_dlci_alloc(gsm, address); | ||
1674 | if (dlci == NULL) | ||
1675 | return; | ||
1676 | if (dlci->dead) | ||
1677 | gsm_response(gsm, address, DM); | ||
1678 | else { | ||
1679 | gsm_response(gsm, address, UA); | ||
1680 | gsm_dlci_open(dlci); | ||
1681 | } | ||
1682 | break; | ||
1683 | case DISC|PF: | ||
1684 | if (cr == 0) | ||
1685 | goto invalid; | ||
1686 | if (dlci == NULL || dlci->state == DLCI_CLOSED) { | ||
1687 | gsm_response(gsm, address, DM); | ||
1688 | return; | ||
1689 | } | ||
1690 | /* Real close complete */ | ||
1691 | gsm_response(gsm, address, UA); | ||
1692 | gsm_dlci_close(dlci); | ||
1693 | break; | ||
1694 | case UA: | ||
1695 | case UA|PF: | ||
1696 | if (cr == 0 || dlci == NULL) | ||
1697 | break; | ||
1698 | switch (dlci->state) { | ||
1699 | case DLCI_CLOSING: | ||
1700 | gsm_dlci_close(dlci); | ||
1701 | break; | ||
1702 | case DLCI_OPENING: | ||
1703 | gsm_dlci_open(dlci); | ||
1704 | break; | ||
1705 | } | ||
1706 | break; | ||
1707 | case DM: /* DM can be valid unsolicited */ | ||
1708 | case DM|PF: | ||
1709 | if (cr) | ||
1710 | goto invalid; | ||
1711 | if (dlci == NULL) | ||
1712 | return; | ||
1713 | gsm_dlci_close(dlci); | ||
1714 | break; | ||
1715 | case UI: | ||
1716 | case UI|PF: | ||
1717 | case UIH: | ||
1718 | case UIH|PF: | ||
1719 | #if 0 | ||
1720 | if (cr) | ||
1721 | goto invalid; | ||
1722 | #endif | ||
1723 | if (dlci == NULL || dlci->state != DLCI_OPEN) { | ||
1724 | gsm_command(gsm, address, DM|PF); | ||
1725 | return; | ||
1726 | } | ||
1727 | dlci->data(dlci, gsm->buf, gsm->len); | ||
1728 | break; | ||
1729 | default: | ||
1730 | goto invalid; | ||
1731 | } | ||
1732 | return; | ||
1733 | invalid: | ||
1734 | gsm->malformed++; | ||
1735 | return; | ||
1736 | } | ||
1737 | |||
1738 | |||
1739 | /** | ||
1740 | * gsm0_receive - perform processing for non-transparency | ||
1741 | * @gsm: gsm data for this ldisc instance | ||
1742 | * @c: character | ||
1743 | * | ||
1744 | * Receive bytes in gsm mode 0 | ||
1745 | */ | ||
1746 | |||
1747 | static void gsm0_receive(struct gsm_mux *gsm, unsigned char c) | ||
1748 | { | ||
1749 | switch (gsm->state) { | ||
1750 | case GSM_SEARCH: /* SOF marker */ | ||
1751 | if (c == GSM0_SOF) { | ||
1752 | gsm->state = GSM_ADDRESS; | ||
1753 | gsm->address = 0; | ||
1754 | gsm->len = 0; | ||
1755 | gsm->fcs = INIT_FCS; | ||
1756 | } | ||
1757 | break; /* Address EA */ | ||
1758 | case GSM_ADDRESS: | ||
1759 | gsm->fcs = gsm_fcs_add(gsm->fcs, c); | ||
1760 | if (gsm_read_ea(&gsm->address, c)) | ||
1761 | gsm->state = GSM_CONTROL; | ||
1762 | break; | ||
1763 | case GSM_CONTROL: /* Control Byte */ | ||
1764 | gsm->fcs = gsm_fcs_add(gsm->fcs, c); | ||
1765 | gsm->control = c; | ||
1766 | gsm->state = GSM_LEN; | ||
1767 | break; | ||
1768 | case GSM_LEN: /* Length EA */ | ||
1769 | gsm->fcs = gsm_fcs_add(gsm->fcs, c); | ||
1770 | if (gsm_read_ea(&gsm->len, c)) { | ||
1771 | if (gsm->len > gsm->mru) { | ||
1772 | gsm->bad_size++; | ||
1773 | gsm->state = GSM_SEARCH; | ||
1774 | break; | ||
1775 | } | ||
1776 | gsm->count = 0; | ||
1777 | gsm->state = GSM_DATA; | ||
1778 | } | ||
1779 | break; | ||
1780 | case GSM_DATA: /* Data */ | ||
1781 | gsm->buf[gsm->count++] = c; | ||
1782 | if (gsm->count == gsm->len) | ||
1783 | gsm->state = GSM_FCS; | ||
1784 | break; | ||
1785 | case GSM_FCS: /* FCS follows the packet */ | ||
1786 | gsm->fcs = c; | ||
1787 | gsm_queue(gsm); | ||
1788 | /* And then back for the next frame */ | ||
1789 | gsm->state = GSM_SEARCH; | ||
1790 | break; | ||
1791 | } | ||
1792 | } | ||
1793 | |||
1794 | /** | ||
1795 | * gsm0_receive - perform processing for non-transparency | ||
1796 | * @gsm: gsm data for this ldisc instance | ||
1797 | * @c: character | ||
1798 | * | ||
1799 | * Receive bytes in mode 1 (Advanced option) | ||
1800 | */ | ||
1801 | |||
1802 | static void gsm1_receive(struct gsm_mux *gsm, unsigned char c) | ||
1803 | { | ||
1804 | if (c == GSM1_SOF) { | ||
1805 | /* EOF is only valid in frame if we have got to the data state | ||
1806 | and received at least one byte (the FCS) */ | ||
1807 | if (gsm->state == GSM_DATA && gsm->count) { | ||
1808 | /* Extract the FCS */ | ||
1809 | gsm->count--; | ||
1810 | gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]); | ||
1811 | gsm->len = gsm->count; | ||
1812 | gsm_queue(gsm); | ||
1813 | gsm->state = GSM_START; | ||
1814 | return; | ||
1815 | } | ||
1816 | /* Any partial frame was a runt so go back to start */ | ||
1817 | if (gsm->state != GSM_START) { | ||
1818 | gsm->malformed++; | ||
1819 | gsm->state = GSM_START; | ||
1820 | } | ||
1821 | /* A SOF in GSM_START means we are still reading idling or | ||
1822 | framing bytes */ | ||
1823 | return; | ||
1824 | } | ||
1825 | |||
1826 | if (c == GSM1_ESCAPE) { | ||
1827 | gsm->escape = 1; | ||
1828 | return; | ||
1829 | } | ||
1830 | |||
1831 | /* Only an unescaped SOF gets us out of GSM search */ | ||
1832 | if (gsm->state == GSM_SEARCH) | ||
1833 | return; | ||
1834 | |||
1835 | if (gsm->escape) { | ||
1836 | c ^= GSM1_ESCAPE_BITS; | ||
1837 | gsm->escape = 0; | ||
1838 | } | ||
1839 | switch (gsm->state) { | ||
1840 | case GSM_START: /* First byte after SOF */ | ||
1841 | gsm->address = 0; | ||
1842 | gsm->state = GSM_ADDRESS; | ||
1843 | gsm->fcs = INIT_FCS; | ||
1844 | /* Drop through */ | ||
1845 | case GSM_ADDRESS: /* Address continuation */ | ||
1846 | gsm->fcs = gsm_fcs_add(gsm->fcs, c); | ||
1847 | if (gsm_read_ea(&gsm->address, c)) | ||
1848 | gsm->state = GSM_CONTROL; | ||
1849 | break; | ||
1850 | case GSM_CONTROL: /* Control Byte */ | ||
1851 | gsm->fcs = gsm_fcs_add(gsm->fcs, c); | ||
1852 | gsm->control = c; | ||
1853 | gsm->count = 0; | ||
1854 | gsm->state = GSM_DATA; | ||
1855 | break; | ||
1856 | case GSM_DATA: /* Data */ | ||
1857 | if (gsm->count > gsm->mru ) { /* Allow one for the FCS */ | ||
1858 | gsm->state = GSM_OVERRUN; | ||
1859 | gsm->bad_size++; | ||
1860 | } else | ||
1861 | gsm->buf[gsm->count++] = c; | ||
1862 | break; | ||
1863 | case GSM_OVERRUN: /* Over-long - eg a dropped SOF */ | ||
1864 | break; | ||
1865 | } | ||
1866 | } | ||
1867 | |||
1868 | /** | ||
1869 | * gsm_error - handle tty error | ||
1870 | * @gsm: ldisc data | ||
1871 | * @data: byte received (may be invalid) | ||
1872 | * @flag: error received | ||
1873 | * | ||
1874 | * Handle an error in the receipt of data for a frame. Currently we just | ||
1875 | * go back to hunting for a SOF. | ||
1876 | * | ||
1877 | * FIXME: better diagnostics ? | ||
1878 | */ | ||
1879 | |||
1880 | static void gsm_error(struct gsm_mux *gsm, | ||
1881 | unsigned char data, unsigned char flag) | ||
1882 | { | ||
1883 | gsm->state = GSM_SEARCH; | ||
1884 | gsm->io_error++; | ||
1885 | } | ||
1886 | |||
1887 | /** | ||
1888 | * gsm_cleanup_mux - generic GSM protocol cleanup | ||
1889 | * @gsm: our mux | ||
1890 | * | ||
1891 | * Clean up the bits of the mux which are the same for all framing | ||
1892 | * protocols. Remove the mux from the mux table, stop all the timers | ||
1893 | * and then shut down each device hanging up the channels as we go. | ||
1894 | */ | ||
1895 | |||
1896 | void gsm_cleanup_mux(struct gsm_mux *gsm) | ||
1897 | { | ||
1898 | int i; | ||
1899 | struct gsm_dlci *dlci = gsm->dlci[0]; | ||
1900 | struct gsm_msg *txq; | ||
1901 | |||
1902 | gsm->dead = 1; | ||
1903 | |||
1904 | spin_lock(&gsm_mux_lock); | ||
1905 | for (i = 0; i < MAX_MUX; i++) { | ||
1906 | if (gsm_mux[i] == gsm) { | ||
1907 | gsm_mux[i] = NULL; | ||
1908 | break; | ||
1909 | } | ||
1910 | } | ||
1911 | spin_unlock(&gsm_mux_lock); | ||
1912 | WARN_ON(i == MAX_MUX); | ||
1913 | |||
1914 | del_timer_sync(&gsm->t2_timer); | ||
1915 | /* Now we are sure T2 has stopped */ | ||
1916 | if (dlci) { | ||
1917 | dlci->dead = 1; | ||
1918 | gsm_dlci_begin_close(dlci); | ||
1919 | wait_event_interruptible(gsm->event, | ||
1920 | dlci->state == DLCI_CLOSED); | ||
1921 | } | ||
1922 | /* Free up any link layer users */ | ||
1923 | for (i = 0; i < NUM_DLCI; i++) | ||
1924 | if (gsm->dlci[i]) | ||
1925 | gsm_dlci_free(gsm->dlci[i]); | ||
1926 | /* Now wipe the queues */ | ||
1927 | for (txq = gsm->tx_head; txq != NULL; txq = gsm->tx_head) { | ||
1928 | gsm->tx_head = txq->next; | ||
1929 | kfree(txq); | ||
1930 | } | ||
1931 | gsm->tx_tail = NULL; | ||
1932 | } | ||
1933 | EXPORT_SYMBOL_GPL(gsm_cleanup_mux); | ||
1934 | |||
1935 | /** | ||
1936 | * gsm_activate_mux - generic GSM setup | ||
1937 | * @gsm: our mux | ||
1938 | * | ||
1939 | * Set up the bits of the mux which are the same for all framing | ||
1940 | * protocols. Add the mux to the mux table so it can be opened and | ||
1941 | * finally kick off connecting to DLCI 0 on the modem. | ||
1942 | */ | ||
1943 | |||
1944 | int gsm_activate_mux(struct gsm_mux *gsm) | ||
1945 | { | ||
1946 | struct gsm_dlci *dlci; | ||
1947 | int i = 0; | ||
1948 | |||
1949 | init_timer(&gsm->t2_timer); | ||
1950 | gsm->t2_timer.function = gsm_control_retransmit; | ||
1951 | gsm->t2_timer.data = (unsigned long)gsm; | ||
1952 | init_waitqueue_head(&gsm->event); | ||
1953 | spin_lock_init(&gsm->control_lock); | ||
1954 | spin_lock_init(&gsm->tx_lock); | ||
1955 | |||
1956 | if (gsm->encoding == 0) | ||
1957 | gsm->receive = gsm0_receive; | ||
1958 | else | ||
1959 | gsm->receive = gsm1_receive; | ||
1960 | gsm->error = gsm_error; | ||
1961 | |||
1962 | spin_lock(&gsm_mux_lock); | ||
1963 | for (i = 0; i < MAX_MUX; i++) { | ||
1964 | if (gsm_mux[i] == NULL) { | ||
1965 | gsm_mux[i] = gsm; | ||
1966 | break; | ||
1967 | } | ||
1968 | } | ||
1969 | spin_unlock(&gsm_mux_lock); | ||
1970 | if (i == MAX_MUX) | ||
1971 | return -EBUSY; | ||
1972 | |||
1973 | dlci = gsm_dlci_alloc(gsm, 0); | ||
1974 | if (dlci == NULL) | ||
1975 | return -ENOMEM; | ||
1976 | gsm->dead = 0; /* Tty opens are now permissible */ | ||
1977 | return 0; | ||
1978 | } | ||
1979 | EXPORT_SYMBOL_GPL(gsm_activate_mux); | ||
1980 | |||
1981 | /** | ||
1982 | * gsm_free_mux - free up a mux | ||
1983 | * @mux: mux to free | ||
1984 | * | ||
1985 | * Dispose of allocated resources for a dead mux. No refcounting | ||
1986 | * at present so the mux must be truely dead. | ||
1987 | */ | ||
1988 | void gsm_free_mux(struct gsm_mux *gsm) | ||
1989 | { | ||
1990 | kfree(gsm->txframe); | ||
1991 | kfree(gsm->buf); | ||
1992 | kfree(gsm); | ||
1993 | } | ||
1994 | EXPORT_SYMBOL_GPL(gsm_free_mux); | ||
1995 | |||
1996 | /** | ||
1997 | * gsm_alloc_mux - allocate a mux | ||
1998 | * | ||
1999 | * Creates a new mux ready for activation. | ||
2000 | */ | ||
2001 | |||
2002 | struct gsm_mux *gsm_alloc_mux(void) | ||
2003 | { | ||
2004 | struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL); | ||
2005 | if (gsm == NULL) | ||
2006 | return NULL; | ||
2007 | gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL); | ||
2008 | if (gsm->buf == NULL) { | ||
2009 | kfree(gsm); | ||
2010 | return NULL; | ||
2011 | } | ||
2012 | gsm->txframe = kmalloc(2 * MAX_MRU + 2, GFP_KERNEL); | ||
2013 | if (gsm->txframe == NULL) { | ||
2014 | kfree(gsm->buf); | ||
2015 | kfree(gsm); | ||
2016 | return NULL; | ||
2017 | } | ||
2018 | spin_lock_init(&gsm->lock); | ||
2019 | |||
2020 | gsm->t1 = T1; | ||
2021 | gsm->t2 = T2; | ||
2022 | gsm->n2 = N2; | ||
2023 | gsm->ftype = UIH; | ||
2024 | gsm->initiator = 0; | ||
2025 | gsm->adaption = 1; | ||
2026 | gsm->encoding = 1; | ||
2027 | gsm->mru = 64; /* Default to encoding 1 so these should be 64 */ | ||
2028 | gsm->mtu = 64; | ||
2029 | gsm->dead = 1; /* Avoid early tty opens */ | ||
2030 | |||
2031 | return gsm; | ||
2032 | } | ||
2033 | EXPORT_SYMBOL_GPL(gsm_alloc_mux); | ||
2034 | |||
2035 | |||
2036 | |||
2037 | |||
2038 | /** | ||
2039 | * gsmld_output - write to link | ||
2040 | * @gsm: our mux | ||
2041 | * @data: bytes to output | ||
2042 | * @len: size | ||
2043 | * | ||
2044 | * Write a block of data from the GSM mux to the data channel. This | ||
2045 | * will eventually be serialized from above but at the moment isn't. | ||
2046 | */ | ||
2047 | |||
2048 | static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len) | ||
2049 | { | ||
2050 | if (tty_write_room(gsm->tty) < len) { | ||
2051 | set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags); | ||
2052 | return -ENOSPC; | ||
2053 | } | ||
2054 | if (debug & 4) { | ||
2055 | printk("-->%d bytes out\n", len); | ||
2056 | hex_packet(data, len); | ||
2057 | } | ||
2058 | gsm->tty->ops->write(gsm->tty, data, len); | ||
2059 | return len; | ||
2060 | } | ||
2061 | |||
2062 | /** | ||
2063 | * gsmld_attach_gsm - mode set up | ||
2064 | * @tty: our tty structure | ||
2065 | * @gsm: our mux | ||
2066 | * | ||
2067 | * Set up the MUX for basic mode and commence connecting to the | ||
2068 | * modem. Currently called from the line discipline set up but | ||
2069 | * will need moving to an ioctl path. | ||
2070 | */ | ||
2071 | |||
2072 | static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm) | ||
2073 | { | ||
2074 | int ret; | ||
2075 | |||
2076 | gsm->tty = tty_kref_get(tty); | ||
2077 | gsm->output = gsmld_output; | ||
2078 | ret = gsm_activate_mux(gsm); | ||
2079 | if (ret != 0) | ||
2080 | tty_kref_put(gsm->tty); | ||
2081 | return ret; | ||
2082 | } | ||
2083 | |||
2084 | |||
2085 | /** | ||
2086 | * gsmld_detach_gsm - stop doing 0710 mux | ||
2087 | * @tty: tty atttached to the mux | ||
2088 | * @gsm: mux | ||
2089 | * | ||
2090 | * Shutdown and then clean up the resources used by the line discipline | ||
2091 | */ | ||
2092 | |||
2093 | static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm) | ||
2094 | { | ||
2095 | WARN_ON(tty != gsm->tty); | ||
2096 | gsm_cleanup_mux(gsm); | ||
2097 | tty_kref_put(gsm->tty); | ||
2098 | gsm->tty = NULL; | ||
2099 | } | ||
2100 | |||
2101 | static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp, | ||
2102 | char *fp, int count) | ||
2103 | { | ||
2104 | struct gsm_mux *gsm = tty->disc_data; | ||
2105 | const unsigned char *dp; | ||
2106 | char *f; | ||
2107 | int i; | ||
2108 | char buf[64]; | ||
2109 | char flags; | ||
2110 | |||
2111 | if (debug & 4) { | ||
2112 | printk("Inbytes %dd\n", count); | ||
2113 | hex_packet(cp, count); | ||
2114 | } | ||
2115 | |||
2116 | for (i = count, dp = cp, f = fp; i; i--, dp++) { | ||
2117 | flags = *f++; | ||
2118 | switch (flags) { | ||
2119 | case TTY_NORMAL: | ||
2120 | gsm->receive(gsm, *dp); | ||
2121 | break; | ||
2122 | case TTY_OVERRUN: | ||
2123 | case TTY_BREAK: | ||
2124 | case TTY_PARITY: | ||
2125 | case TTY_FRAME: | ||
2126 | gsm->error(gsm, *dp, flags); | ||
2127 | break; | ||
2128 | default: | ||
2129 | printk(KERN_ERR "%s: unknown flag %d\n", | ||
2130 | tty_name(tty, buf), flags); | ||
2131 | break; | ||
2132 | } | ||
2133 | } | ||
2134 | /* FASYNC if needed ? */ | ||
2135 | /* If clogged call tty_throttle(tty); */ | ||
2136 | } | ||
2137 | |||
2138 | /** | ||
2139 | * gsmld_chars_in_buffer - report available bytes | ||
2140 | * @tty: tty device | ||
2141 | * | ||
2142 | * Report the number of characters buffered to be delivered to user | ||
2143 | * at this instant in time. | ||
2144 | * | ||
2145 | * Locking: gsm lock | ||
2146 | */ | ||
2147 | |||
2148 | static ssize_t gsmld_chars_in_buffer(struct tty_struct *tty) | ||
2149 | { | ||
2150 | return 0; | ||
2151 | } | ||
2152 | |||
2153 | /** | ||
2154 | * gsmld_flush_buffer - clean input queue | ||
2155 | * @tty: terminal device | ||
2156 | * | ||
2157 | * Flush the input buffer. Called when the line discipline is | ||
2158 | * being closed, when the tty layer wants the buffer flushed (eg | ||
2159 | * at hangup). | ||
2160 | */ | ||
2161 | |||
2162 | static void gsmld_flush_buffer(struct tty_struct *tty) | ||
2163 | { | ||
2164 | } | ||
2165 | |||
2166 | /** | ||
2167 | * gsmld_close - close the ldisc for this tty | ||
2168 | * @tty: device | ||
2169 | * | ||
2170 | * Called from the terminal layer when this line discipline is | ||
2171 | * being shut down, either because of a close or becsuse of a | ||
2172 | * discipline change. The function will not be called while other | ||
2173 | * ldisc methods are in progress. | ||
2174 | */ | ||
2175 | |||
2176 | static void gsmld_close(struct tty_struct *tty) | ||
2177 | { | ||
2178 | struct gsm_mux *gsm = tty->disc_data; | ||
2179 | |||
2180 | gsmld_detach_gsm(tty, gsm); | ||
2181 | |||
2182 | gsmld_flush_buffer(tty); | ||
2183 | /* Do other clean up here */ | ||
2184 | gsm_free_mux(gsm); | ||
2185 | } | ||
2186 | |||
2187 | /** | ||
2188 | * gsmld_open - open an ldisc | ||
2189 | * @tty: terminal to open | ||
2190 | * | ||
2191 | * Called when this line discipline is being attached to the | ||
2192 | * terminal device. Can sleep. Called serialized so that no | ||
2193 | * other events will occur in parallel. No further open will occur | ||
2194 | * until a close. | ||
2195 | */ | ||
2196 | |||
2197 | static int gsmld_open(struct tty_struct *tty) | ||
2198 | { | ||
2199 | struct gsm_mux *gsm; | ||
2200 | |||
2201 | if (tty->ops->write == NULL) | ||
2202 | return -EINVAL; | ||
2203 | |||
2204 | /* Attach our ldisc data */ | ||
2205 | gsm = gsm_alloc_mux(); | ||
2206 | if (gsm == NULL) | ||
2207 | return -ENOMEM; | ||
2208 | |||
2209 | tty->disc_data = gsm; | ||
2210 | tty->receive_room = 65536; | ||
2211 | |||
2212 | /* Attach the initial passive connection */ | ||
2213 | gsm->encoding = 1; | ||
2214 | return gsmld_attach_gsm(tty, gsm); | ||
2215 | } | ||
2216 | |||
2217 | /** | ||
2218 | * gsmld_write_wakeup - asynchronous I/O notifier | ||
2219 | * @tty: tty device | ||
2220 | * | ||
2221 | * Required for the ptys, serial driver etc. since processes | ||
2222 | * that attach themselves to the master and rely on ASYNC | ||
2223 | * IO must be woken up | ||
2224 | */ | ||
2225 | |||
2226 | static void gsmld_write_wakeup(struct tty_struct *tty) | ||
2227 | { | ||
2228 | struct gsm_mux *gsm = tty->disc_data; | ||
2229 | unsigned long flags; | ||
2230 | |||
2231 | /* Queue poll */ | ||
2232 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | ||
2233 | gsm_data_kick(gsm); | ||
2234 | if (gsm->tx_bytes < TX_THRESH_LO) { | ||
2235 | spin_lock_irqsave(&gsm->tx_lock, flags); | ||
2236 | gsm_dlci_data_sweep(gsm); | ||
2237 | spin_unlock_irqrestore(&gsm->tx_lock, flags); | ||
2238 | } | ||
2239 | } | ||
2240 | |||
2241 | /** | ||
2242 | * gsmld_read - read function for tty | ||
2243 | * @tty: tty device | ||
2244 | * @file: file object | ||
2245 | * @buf: userspace buffer pointer | ||
2246 | * @nr: size of I/O | ||
2247 | * | ||
2248 | * Perform reads for the line discipline. We are guaranteed that the | ||
2249 | * line discipline will not be closed under us but we may get multiple | ||
2250 | * parallel readers and must handle this ourselves. We may also get | ||
2251 | * a hangup. Always called in user context, may sleep. | ||
2252 | * | ||
2253 | * This code must be sure never to sleep through a hangup. | ||
2254 | */ | ||
2255 | |||
2256 | static ssize_t gsmld_read(struct tty_struct *tty, struct file *file, | ||
2257 | unsigned char __user *buf, size_t nr) | ||
2258 | { | ||
2259 | return -EOPNOTSUPP; | ||
2260 | } | ||
2261 | |||
2262 | /** | ||
2263 | * gsmld_write - write function for tty | ||
2264 | * @tty: tty device | ||
2265 | * @file: file object | ||
2266 | * @buf: userspace buffer pointer | ||
2267 | * @nr: size of I/O | ||
2268 | * | ||
2269 | * Called when the owner of the device wants to send a frame | ||
2270 | * itself (or some other control data). The data is transferred | ||
2271 | * as-is and must be properly framed and checksummed as appropriate | ||
2272 | * by userspace. Frames are either sent whole or not at all as this | ||
2273 | * avoids pain user side. | ||
2274 | */ | ||
2275 | |||
2276 | static ssize_t gsmld_write(struct tty_struct *tty, struct file *file, | ||
2277 | const unsigned char *buf, size_t nr) | ||
2278 | { | ||
2279 | int space = tty_write_room(tty); | ||
2280 | if (space >= nr) | ||
2281 | return tty->ops->write(tty, buf, nr); | ||
2282 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | ||
2283 | return -ENOBUFS; | ||
2284 | } | ||
2285 | |||
2286 | /** | ||
2287 | * gsmld_poll - poll method for N_GSM0710 | ||
2288 | * @tty: terminal device | ||
2289 | * @file: file accessing it | ||
2290 | * @wait: poll table | ||
2291 | * | ||
2292 | * Called when the line discipline is asked to poll() for data or | ||
2293 | * for special events. This code is not serialized with respect to | ||
2294 | * other events save open/close. | ||
2295 | * | ||
2296 | * This code must be sure never to sleep through a hangup. | ||
2297 | * Called without the kernel lock held - fine | ||
2298 | */ | ||
2299 | |||
2300 | static unsigned int gsmld_poll(struct tty_struct *tty, struct file *file, | ||
2301 | poll_table *wait) | ||
2302 | { | ||
2303 | unsigned int mask = 0; | ||
2304 | struct gsm_mux *gsm = tty->disc_data; | ||
2305 | |||
2306 | poll_wait(file, &tty->read_wait, wait); | ||
2307 | poll_wait(file, &tty->write_wait, wait); | ||
2308 | if (tty_hung_up_p(file)) | ||
2309 | mask |= POLLHUP; | ||
2310 | if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0) | ||
2311 | mask |= POLLOUT | POLLWRNORM; | ||
2312 | if (gsm->dead) | ||
2313 | mask |= POLLHUP; | ||
2314 | return mask; | ||
2315 | } | ||
2316 | |||
2317 | static int gsmld_config(struct tty_struct *tty, struct gsm_mux *gsm, | ||
2318 | struct gsm_config *c) | ||
2319 | { | ||
2320 | int need_close = 0; | ||
2321 | int need_restart = 0; | ||
2322 | |||
2323 | /* Stuff we don't support yet - UI or I frame transport, windowing */ | ||
2324 | if ((c->adaption !=1 && c->adaption != 2) || c->k) | ||
2325 | return -EOPNOTSUPP; | ||
2326 | /* Check the MRU/MTU range looks sane */ | ||
2327 | if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8) | ||
2328 | return -EINVAL; | ||
2329 | if (c->n2 < 3) | ||
2330 | return -EINVAL; | ||
2331 | if (c->encapsulation > 1) /* Basic, advanced, no I */ | ||
2332 | return -EINVAL; | ||
2333 | if (c->initiator > 1) | ||
2334 | return -EINVAL; | ||
2335 | if (c->i == 0 || c->i > 2) /* UIH and UI only */ | ||
2336 | return -EINVAL; | ||
2337 | /* | ||
2338 | * See what is needed for reconfiguration | ||
2339 | */ | ||
2340 | |||
2341 | /* Timing fields */ | ||
2342 | if (c->t1 != 0 && c->t1 != gsm->t1) | ||
2343 | need_restart = 1; | ||
2344 | if (c->t2 != 0 && c->t2 != gsm->t2) | ||
2345 | need_restart = 1; | ||
2346 | if (c->encapsulation != gsm->encoding) | ||
2347 | need_restart = 1; | ||
2348 | if (c->adaption != gsm->adaption) | ||
2349 | need_restart = 1; | ||
2350 | /* Requires care */ | ||
2351 | if (c->initiator != gsm->initiator) | ||
2352 | need_close = 1; | ||
2353 | if (c->mru != gsm->mru) | ||
2354 | need_restart = 1; | ||
2355 | if (c->mtu != gsm->mtu) | ||
2356 | need_restart = 1; | ||
2357 | |||
2358 | /* | ||
2359 | * Close down what is needed, restart and initiate the new | ||
2360 | * configuration | ||
2361 | */ | ||
2362 | |||
2363 | if (need_close || need_restart) { | ||
2364 | gsm_dlci_begin_close(gsm->dlci[0]); | ||
2365 | /* This will timeout if the link is down due to N2 expiring */ | ||
2366 | wait_event_interruptible(gsm->event, | ||
2367 | gsm->dlci[0]->state == DLCI_CLOSED); | ||
2368 | if (signal_pending(current)) | ||
2369 | return -EINTR; | ||
2370 | } | ||
2371 | if (need_restart) | ||
2372 | gsm_cleanup_mux(gsm); | ||
2373 | |||
2374 | gsm->initiator = c->initiator; | ||
2375 | gsm->mru = c->mru; | ||
2376 | gsm->encoding = c->encapsulation; | ||
2377 | gsm->adaption = c->adaption; | ||
2378 | gsm->n2 = c->n2; | ||
2379 | |||
2380 | if (c->i == 1) | ||
2381 | gsm->ftype = UIH; | ||
2382 | else if (c->i == 2) | ||
2383 | gsm->ftype = UI; | ||
2384 | |||
2385 | if (c->t1) | ||
2386 | gsm->t1 = c->t1; | ||
2387 | if (c->t2) | ||
2388 | gsm->t2 = c->t2; | ||
2389 | |||
2390 | /* FIXME: We need to separate activation/deactivation from adding | ||
2391 | and removing from the mux array */ | ||
2392 | if (need_restart) | ||
2393 | gsm_activate_mux(gsm); | ||
2394 | if (gsm->initiator && need_close) | ||
2395 | gsm_dlci_begin_open(gsm->dlci[0]); | ||
2396 | return 0; | ||
2397 | } | ||
2398 | |||
2399 | static int gsmld_ioctl(struct tty_struct *tty, struct file *file, | ||
2400 | unsigned int cmd, unsigned long arg) | ||
2401 | { | ||
2402 | struct gsm_config c; | ||
2403 | struct gsm_mux *gsm = tty->disc_data; | ||
2404 | |||
2405 | switch (cmd) { | ||
2406 | case GSMIOC_GETCONF: | ||
2407 | memset(&c, 0, sizeof(c)); | ||
2408 | c.adaption = gsm->adaption; | ||
2409 | c.encapsulation = gsm->encoding; | ||
2410 | c.initiator = gsm->initiator; | ||
2411 | c.t1 = gsm->t1; | ||
2412 | c.t2 = gsm->t2; | ||
2413 | c.t3 = 0; /* Not supported */ | ||
2414 | c.n2 = gsm->n2; | ||
2415 | if (gsm->ftype == UIH) | ||
2416 | c.i = 1; | ||
2417 | else | ||
2418 | c.i = 2; | ||
2419 | printk("Ftype %d i %d\n", gsm->ftype, c.i); | ||
2420 | c.mru = gsm->mru; | ||
2421 | c.mtu = gsm->mtu; | ||
2422 | c.k = 0; | ||
2423 | if (copy_to_user((void *)arg, &c, sizeof(c))) | ||
2424 | return -EFAULT; | ||
2425 | return 0; | ||
2426 | case GSMIOC_SETCONF: | ||
2427 | if (copy_from_user(&c, (void *)arg, sizeof(c))) | ||
2428 | return -EFAULT; | ||
2429 | return gsmld_config(tty, gsm, &c); | ||
2430 | default: | ||
2431 | return n_tty_ioctl_helper(tty, file, cmd, arg); | ||
2432 | } | ||
2433 | } | ||
2434 | |||
2435 | |||
2436 | /* Line discipline for real tty */ | ||
2437 | struct tty_ldisc_ops tty_ldisc_packet = { | ||
2438 | .owner = THIS_MODULE, | ||
2439 | .magic = TTY_LDISC_MAGIC, | ||
2440 | .name = "n_gsm", | ||
2441 | .open = gsmld_open, | ||
2442 | .close = gsmld_close, | ||
2443 | .flush_buffer = gsmld_flush_buffer, | ||
2444 | .chars_in_buffer = gsmld_chars_in_buffer, | ||
2445 | .read = gsmld_read, | ||
2446 | .write = gsmld_write, | ||
2447 | .ioctl = gsmld_ioctl, | ||
2448 | .poll = gsmld_poll, | ||
2449 | .receive_buf = gsmld_receive_buf, | ||
2450 | .write_wakeup = gsmld_write_wakeup | ||
2451 | }; | ||
2452 | |||
2453 | /* | ||
2454 | * Virtual tty side | ||
2455 | */ | ||
2456 | |||
2457 | #define TX_SIZE 512 | ||
2458 | |||
2459 | static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk) | ||
2460 | { | ||
2461 | u8 modembits[5]; | ||
2462 | struct gsm_control *ctrl; | ||
2463 | int len = 2; | ||
2464 | |||
2465 | if (brk) | ||
2466 | len++; | ||
2467 | |||
2468 | modembits[0] = len << 1 | EA; /* Data bytes */ | ||
2469 | modembits[1] = dlci->addr << 2 | 3; /* DLCI, EA, 1 */ | ||
2470 | modembits[2] = gsm_encode_modem(dlci) << 1 | EA; | ||
2471 | if (brk) | ||
2472 | modembits[3] = brk << 4 | 2 | EA; /* Valid, EA */ | ||
2473 | ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len + 1); | ||
2474 | if (ctrl == NULL) | ||
2475 | return -ENOMEM; | ||
2476 | return gsm_control_wait(dlci->gsm, ctrl); | ||
2477 | } | ||
2478 | |||
2479 | static int gsm_carrier_raised(struct tty_port *port) | ||
2480 | { | ||
2481 | struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port); | ||
2482 | /* Not yet open so no carrier info */ | ||
2483 | if (dlci->state != DLCI_OPEN) | ||
2484 | return 0; | ||
2485 | if (debug & 2) | ||
2486 | return 1; | ||
2487 | return dlci->modem_rx & TIOCM_CD; | ||
2488 | } | ||
2489 | |||
2490 | static void gsm_dtr_rts(struct tty_port *port, int onoff) | ||
2491 | { | ||
2492 | struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port); | ||
2493 | unsigned int modem_tx = dlci->modem_tx; | ||
2494 | if (onoff) | ||
2495 | modem_tx |= TIOCM_DTR | TIOCM_RTS; | ||
2496 | else | ||
2497 | modem_tx &= ~(TIOCM_DTR | TIOCM_RTS); | ||
2498 | if (modem_tx != dlci->modem_tx) { | ||
2499 | dlci->modem_tx = modem_tx; | ||
2500 | gsmtty_modem_update(dlci, 0); | ||
2501 | } | ||
2502 | } | ||
2503 | |||
2504 | static const struct tty_port_operations gsm_port_ops = { | ||
2505 | .carrier_raised = gsm_carrier_raised, | ||
2506 | .dtr_rts = gsm_dtr_rts, | ||
2507 | }; | ||
2508 | |||
2509 | |||
2510 | static int gsmtty_open(struct tty_struct *tty, struct file *filp) | ||
2511 | { | ||
2512 | struct gsm_mux *gsm; | ||
2513 | struct gsm_dlci *dlci; | ||
2514 | struct tty_port *port; | ||
2515 | unsigned int line = tty->index; | ||
2516 | unsigned int mux = line >> 6; | ||
2517 | |||
2518 | line = line & 0x3F; | ||
2519 | |||
2520 | if (mux >= MAX_MUX) | ||
2521 | return -ENXIO; | ||
2522 | /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */ | ||
2523 | if (gsm_mux[mux] == NULL) | ||
2524 | return -EUNATCH; | ||
2525 | if (line == 0 || line > 61) /* 62/63 reserved */ | ||
2526 | return -ECHRNG; | ||
2527 | gsm = gsm_mux[mux]; | ||
2528 | if (gsm->dead) | ||
2529 | return -EL2HLT; | ||
2530 | dlci = gsm->dlci[line]; | ||
2531 | if (dlci == NULL) | ||
2532 | dlci = gsm_dlci_alloc(gsm, line); | ||
2533 | if (dlci == NULL) | ||
2534 | return -ENOMEM; | ||
2535 | port = &dlci->port; | ||
2536 | port->count++; | ||
2537 | tty->driver_data = dlci; | ||
2538 | tty_port_tty_set(port, tty); | ||
2539 | |||
2540 | dlci->modem_rx = 0; | ||
2541 | /* We could in theory open and close before we wait - eg if we get | ||
2542 | a DM straight back. This is ok as that will have caused a hangup */ | ||
2543 | set_bit(ASYNCB_INITIALIZED, &port->flags); | ||
2544 | /* Start sending off SABM messages */ | ||
2545 | gsm_dlci_begin_open(dlci); | ||
2546 | /* And wait for virtual carrier */ | ||
2547 | return tty_port_block_til_ready(port, tty, filp); | ||
2548 | } | ||
2549 | |||
2550 | static void gsmtty_close(struct tty_struct *tty, struct file *filp) | ||
2551 | { | ||
2552 | struct gsm_dlci *dlci = tty->driver_data; | ||
2553 | if (dlci == NULL) | ||
2554 | return; | ||
2555 | if (tty_port_close_start(&dlci->port, tty, filp) == 0) | ||
2556 | return; | ||
2557 | gsm_dlci_begin_close(dlci); | ||
2558 | tty_port_close_end(&dlci->port, tty); | ||
2559 | tty_port_tty_set(&dlci->port, NULL); | ||
2560 | } | ||
2561 | |||
2562 | static void gsmtty_hangup(struct tty_struct *tty) | ||
2563 | { | ||
2564 | struct gsm_dlci *dlci = tty->driver_data; | ||
2565 | tty_port_hangup(&dlci->port); | ||
2566 | gsm_dlci_begin_close(dlci); | ||
2567 | } | ||
2568 | |||
2569 | static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf, | ||
2570 | int len) | ||
2571 | { | ||
2572 | struct gsm_dlci *dlci = tty->driver_data; | ||
2573 | /* Stuff the bytes into the fifo queue */ | ||
2574 | int sent = kfifo_in_locked(dlci->fifo, buf, len, &dlci->lock); | ||
2575 | /* Need to kick the channel */ | ||
2576 | gsm_dlci_data_kick(dlci); | ||
2577 | return sent; | ||
2578 | } | ||
2579 | |||
2580 | static int gsmtty_write_room(struct tty_struct *tty) | ||
2581 | { | ||
2582 | struct gsm_dlci *dlci = tty->driver_data; | ||
2583 | return TX_SIZE - kfifo_len(dlci->fifo); | ||
2584 | } | ||
2585 | |||
2586 | static int gsmtty_chars_in_buffer(struct tty_struct *tty) | ||
2587 | { | ||
2588 | struct gsm_dlci *dlci = tty->driver_data; | ||
2589 | return kfifo_len(dlci->fifo); | ||
2590 | } | ||
2591 | |||
2592 | static void gsmtty_flush_buffer(struct tty_struct *tty) | ||
2593 | { | ||
2594 | struct gsm_dlci *dlci = tty->driver_data; | ||
2595 | /* Caution needed: If we implement reliable transport classes | ||
2596 | then the data being transmitted can't simply be junked once | ||
2597 | it has first hit the stack. Until then we can just blow it | ||
2598 | away */ | ||
2599 | kfifo_reset(dlci->fifo); | ||
2600 | /* Need to unhook this DLCI from the transmit queue logic */ | ||
2601 | } | ||
2602 | |||
2603 | static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout) | ||
2604 | { | ||
2605 | /* The FIFO handles the queue so the kernel will do the right | ||
2606 | thing waiting on chars_in_buffer before calling us. No work | ||
2607 | to do here */ | ||
2608 | } | ||
2609 | |||
2610 | static int gsmtty_tiocmget(struct tty_struct *tty, struct file *filp) | ||
2611 | { | ||
2612 | struct gsm_dlci *dlci = tty->driver_data; | ||
2613 | return dlci->modem_rx; | ||
2614 | } | ||
2615 | |||
2616 | static int gsmtty_tiocmset(struct tty_struct *tty, struct file *filp, | ||
2617 | unsigned int set, unsigned int clear) | ||
2618 | { | ||
2619 | struct gsm_dlci *dlci = tty->driver_data; | ||
2620 | unsigned int modem_tx = dlci->modem_tx; | ||
2621 | |||
2622 | modem_tx &= clear; | ||
2623 | modem_tx |= set; | ||
2624 | |||
2625 | if (modem_tx != dlci->modem_tx) { | ||
2626 | dlci->modem_tx = modem_tx; | ||
2627 | return gsmtty_modem_update(dlci, 0); | ||
2628 | } | ||
2629 | return 0; | ||
2630 | } | ||
2631 | |||
2632 | |||
2633 | static int gsmtty_ioctl(struct tty_struct *tty, struct file *filp, | ||
2634 | unsigned int cmd, unsigned long arg) | ||
2635 | { | ||
2636 | return -ENOIOCTLCMD; | ||
2637 | } | ||
2638 | |||
2639 | static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old) | ||
2640 | { | ||
2641 | /* For the moment its fixed. In actual fact the speed information | ||
2642 | for the virtual channel can be propogated in both directions by | ||
2643 | the RPN control message. This however rapidly gets nasty as we | ||
2644 | then have to remap modem signals each way according to whether | ||
2645 | our virtual cable is null modem etc .. */ | ||
2646 | tty_termios_copy_hw(tty->termios, old); | ||
2647 | } | ||
2648 | |||
2649 | static void gsmtty_throttle(struct tty_struct *tty) | ||
2650 | { | ||
2651 | struct gsm_dlci *dlci = tty->driver_data; | ||
2652 | if (tty->termios->c_cflag & CRTSCTS) | ||
2653 | dlci->modem_tx &= ~TIOCM_DTR; | ||
2654 | dlci->throttled = 1; | ||
2655 | /* Send an MSC with DTR cleared */ | ||
2656 | gsmtty_modem_update(dlci, 0); | ||
2657 | } | ||
2658 | |||
2659 | static void gsmtty_unthrottle(struct tty_struct *tty) | ||
2660 | { | ||
2661 | struct gsm_dlci *dlci = tty->driver_data; | ||
2662 | if (tty->termios->c_cflag & CRTSCTS) | ||
2663 | dlci->modem_tx |= TIOCM_DTR; | ||
2664 | dlci->throttled = 0; | ||
2665 | /* Send an MSC with DTR set */ | ||
2666 | gsmtty_modem_update(dlci, 0); | ||
2667 | } | ||
2668 | |||
2669 | static int gsmtty_break_ctl(struct tty_struct *tty, int state) | ||
2670 | { | ||
2671 | struct gsm_dlci *dlci = tty->driver_data; | ||
2672 | int encode = 0; /* Off */ | ||
2673 | |||
2674 | if (state == -1) /* "On indefinitely" - we can't encode this | ||
2675 | properly */ | ||
2676 | encode = 0x0F; | ||
2677 | else if (state > 0) { | ||
2678 | encode = state / 200; /* mS to encoding */ | ||
2679 | if (encode > 0x0F) | ||
2680 | encode = 0x0F; /* Best effort */ | ||
2681 | } | ||
2682 | return gsmtty_modem_update(dlci, encode); | ||
2683 | } | ||
2684 | |||
2685 | static struct tty_driver *gsm_tty_driver; | ||
2686 | |||
2687 | /* Virtual ttys for the demux */ | ||
2688 | static const struct tty_operations gsmtty_ops = { | ||
2689 | .open = gsmtty_open, | ||
2690 | .close = gsmtty_close, | ||
2691 | .write = gsmtty_write, | ||
2692 | .write_room = gsmtty_write_room, | ||
2693 | .chars_in_buffer = gsmtty_chars_in_buffer, | ||
2694 | .flush_buffer = gsmtty_flush_buffer, | ||
2695 | .ioctl = gsmtty_ioctl, | ||
2696 | .throttle = gsmtty_throttle, | ||
2697 | .unthrottle = gsmtty_unthrottle, | ||
2698 | .set_termios = gsmtty_set_termios, | ||
2699 | .hangup = gsmtty_hangup, | ||
2700 | .wait_until_sent = gsmtty_wait_until_sent, | ||
2701 | .tiocmget = gsmtty_tiocmget, | ||
2702 | .tiocmset = gsmtty_tiocmset, | ||
2703 | .break_ctl = gsmtty_break_ctl, | ||
2704 | }; | ||
2705 | |||
2706 | |||
2707 | |||
2708 | static int __init gsm_init(void) | ||
2709 | { | ||
2710 | /* Fill in our line protocol discipline, and register it */ | ||
2711 | int status = tty_register_ldisc(N_GSM0710, &tty_ldisc_packet); | ||
2712 | if (status != 0) { | ||
2713 | printk(KERN_ERR "n_gsm: can't register line discipline (err = %d)\n", status); | ||
2714 | return status; | ||
2715 | } | ||
2716 | |||
2717 | gsm_tty_driver = alloc_tty_driver(256); | ||
2718 | if (!gsm_tty_driver) { | ||
2719 | tty_unregister_ldisc(N_GSM0710); | ||
2720 | printk(KERN_ERR "gsm_init: tty allocation failed.\n"); | ||
2721 | return -EINVAL; | ||
2722 | } | ||
2723 | gsm_tty_driver->owner = THIS_MODULE; | ||
2724 | gsm_tty_driver->driver_name = "gsmtty"; | ||
2725 | gsm_tty_driver->name = "gsmtty"; | ||
2726 | gsm_tty_driver->major = 0; /* Dynamic */ | ||
2727 | gsm_tty_driver->minor_start = 0; | ||
2728 | gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; | ||
2729 | gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL; | ||
2730 | gsm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | ||
2731 | | TTY_DRIVER_HARDWARE_BREAK; | ||
2732 | gsm_tty_driver->init_termios = tty_std_termios; | ||
2733 | /* Fixme */ | ||
2734 | gsm_tty_driver->init_termios.c_lflag &= ~ECHO; | ||
2735 | tty_set_operations(gsm_tty_driver, &gsmtty_ops); | ||
2736 | |||
2737 | spin_lock_init(&gsm_mux_lock); | ||
2738 | |||
2739 | if (tty_register_driver(gsm_tty_driver)) { | ||
2740 | put_tty_driver(gsm_tty_driver); | ||
2741 | tty_unregister_ldisc(N_GSM0710); | ||
2742 | printk(KERN_ERR "gsm_init: tty registration failed.\n"); | ||
2743 | return -EBUSY; | ||
2744 | } | ||
2745 | printk(KERN_INFO "gsm_init: loaded as %d,%d.\n", gsm_tty_driver->major, gsm_tty_driver->minor_start); | ||
2746 | return 0; | ||
2747 | } | ||
2748 | |||
2749 | static void __exit gsm_exit(void) | ||
2750 | { | ||
2751 | int status = tty_unregister_ldisc(N_GSM0710); | ||
2752 | if (status != 0) | ||
2753 | printk(KERN_ERR "n_gsm: can't unregister line discipline (err = %d)\n", status); | ||
2754 | tty_unregister_driver(gsm_tty_driver); | ||
2755 | put_tty_driver(gsm_tty_driver); | ||
2756 | printk(KERN_INFO "gsm_init: unloaded.\n"); | ||
2757 | } | ||
2758 | |||
2759 | module_init(gsm_init); | ||
2760 | module_exit(gsm_exit); | ||
2761 | |||
2762 | |||
2763 | MODULE_LICENSE("GPL"); | ||
2764 | MODULE_ALIAS_LDISC(N_GSM0710); | ||
diff --git a/drivers/tty/n_hdlc.c b/drivers/tty/n_hdlc.c new file mode 100644 index 000000000000..47d32281032c --- /dev/null +++ b/drivers/tty/n_hdlc.c | |||
@@ -0,0 +1,1007 @@ | |||
1 | /* generic HDLC line discipline for Linux | ||
2 | * | ||
3 | * Written by Paul Fulghum paulkf@microgate.com | ||
4 | * for Microgate Corporation | ||
5 | * | ||
6 | * Microgate and SyncLink are registered trademarks of Microgate Corporation | ||
7 | * | ||
8 | * Adapted from ppp.c, written by Michael Callahan <callahan@maths.ox.ac.uk>, | ||
9 | * Al Longyear <longyear@netcom.com>, | ||
10 | * Paul Mackerras <Paul.Mackerras@cs.anu.edu.au> | ||
11 | * | ||
12 | * Original release 01/11/99 | ||
13 | * | ||
14 | * This code is released under the GNU General Public License (GPL) | ||
15 | * | ||
16 | * This module implements the tty line discipline N_HDLC for use with | ||
17 | * tty device drivers that support bit-synchronous HDLC communications. | ||
18 | * | ||
19 | * All HDLC data is frame oriented which means: | ||
20 | * | ||
21 | * 1. tty write calls represent one complete transmit frame of data | ||
22 | * The device driver should accept the complete frame or none of | ||
23 | * the frame (busy) in the write method. Each write call should have | ||
24 | * a byte count in the range of 2-65535 bytes (2 is min HDLC frame | ||
25 | * with 1 addr byte and 1 ctrl byte). The max byte count of 65535 | ||
26 | * should include any crc bytes required. For example, when using | ||
27 | * CCITT CRC32, 4 crc bytes are required, so the maximum size frame | ||
28 | * the application may transmit is limited to 65531 bytes. For CCITT | ||
29 | * CRC16, the maximum application frame size would be 65533. | ||
30 | * | ||
31 | * | ||
32 | * 2. receive callbacks from the device driver represents | ||
33 | * one received frame. The device driver should bypass | ||
34 | * the tty flip buffer and call the line discipline receive | ||
35 | * callback directly to avoid fragmenting or concatenating | ||
36 | * multiple frames into a single receive callback. | ||
37 | * | ||
38 | * The HDLC line discipline queues the receive frames in separate | ||
39 | * buffers so complete receive frames can be returned by the | ||
40 | * tty read calls. | ||
41 | * | ||
42 | * 3. tty read calls returns an entire frame of data or nothing. | ||
43 | * | ||
44 | * 4. all send and receive data is considered raw. No processing | ||
45 | * or translation is performed by the line discipline, regardless | ||
46 | * of the tty flags | ||
47 | * | ||
48 | * 5. When line discipline is queried for the amount of receive | ||
49 | * data available (FIOC), 0 is returned if no data available, | ||
50 | * otherwise the count of the next available frame is returned. | ||
51 | * (instead of the sum of all received frame counts). | ||
52 | * | ||
53 | * These conventions allow the standard tty programming interface | ||
54 | * to be used for synchronous HDLC applications when used with | ||
55 | * this line discipline (or another line discipline that is frame | ||
56 | * oriented such as N_PPP). | ||
57 | * | ||
58 | * The SyncLink driver (synclink.c) implements both asynchronous | ||
59 | * (using standard line discipline N_TTY) and synchronous HDLC | ||
60 | * (using N_HDLC) communications, with the latter using the above | ||
61 | * conventions. | ||
62 | * | ||
63 | * This implementation is very basic and does not maintain | ||
64 | * any statistics. The main point is to enforce the raw data | ||
65 | * and frame orientation of HDLC communications. | ||
66 | * | ||
67 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | ||
68 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
69 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
70 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | ||
71 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
72 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
73 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
74 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
75 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
76 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
77 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
78 | */ | ||
79 | |||
80 | #define HDLC_MAGIC 0x239e | ||
81 | |||
82 | #include <linux/module.h> | ||
83 | #include <linux/init.h> | ||
84 | #include <linux/kernel.h> | ||
85 | #include <linux/sched.h> | ||
86 | #include <linux/types.h> | ||
87 | #include <linux/fcntl.h> | ||
88 | #include <linux/interrupt.h> | ||
89 | #include <linux/ptrace.h> | ||
90 | |||
91 | #undef VERSION | ||
92 | #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch)) | ||
93 | |||
94 | #include <linux/poll.h> | ||
95 | #include <linux/in.h> | ||
96 | #include <linux/ioctl.h> | ||
97 | #include <linux/slab.h> | ||
98 | #include <linux/tty.h> | ||
99 | #include <linux/errno.h> | ||
100 | #include <linux/smp_lock.h> | ||
101 | #include <linux/string.h> /* used in new tty drivers */ | ||
102 | #include <linux/signal.h> /* used in new tty drivers */ | ||
103 | #include <linux/if.h> | ||
104 | #include <linux/bitops.h> | ||
105 | |||
106 | #include <asm/system.h> | ||
107 | #include <asm/termios.h> | ||
108 | #include <asm/uaccess.h> | ||
109 | |||
110 | /* | ||
111 | * Buffers for individual HDLC frames | ||
112 | */ | ||
113 | #define MAX_HDLC_FRAME_SIZE 65535 | ||
114 | #define DEFAULT_RX_BUF_COUNT 10 | ||
115 | #define MAX_RX_BUF_COUNT 60 | ||
116 | #define DEFAULT_TX_BUF_COUNT 3 | ||
117 | |||
118 | struct n_hdlc_buf { | ||
119 | struct n_hdlc_buf *link; | ||
120 | int count; | ||
121 | char buf[1]; | ||
122 | }; | ||
123 | |||
124 | #define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe) | ||
125 | |||
126 | struct n_hdlc_buf_list { | ||
127 | struct n_hdlc_buf *head; | ||
128 | struct n_hdlc_buf *tail; | ||
129 | int count; | ||
130 | spinlock_t spinlock; | ||
131 | }; | ||
132 | |||
133 | /** | ||
134 | * struct n_hdlc - per device instance data structure | ||
135 | * @magic - magic value for structure | ||
136 | * @flags - miscellaneous control flags | ||
137 | * @tty - ptr to TTY structure | ||
138 | * @backup_tty - TTY to use if tty gets closed | ||
139 | * @tbusy - reentrancy flag for tx wakeup code | ||
140 | * @woke_up - FIXME: describe this field | ||
141 | * @tbuf - currently transmitting tx buffer | ||
142 | * @tx_buf_list - list of pending transmit frame buffers | ||
143 | * @rx_buf_list - list of received frame buffers | ||
144 | * @tx_free_buf_list - list unused transmit frame buffers | ||
145 | * @rx_free_buf_list - list unused received frame buffers | ||
146 | */ | ||
147 | struct n_hdlc { | ||
148 | int magic; | ||
149 | __u32 flags; | ||
150 | struct tty_struct *tty; | ||
151 | struct tty_struct *backup_tty; | ||
152 | int tbusy; | ||
153 | int woke_up; | ||
154 | struct n_hdlc_buf *tbuf; | ||
155 | struct n_hdlc_buf_list tx_buf_list; | ||
156 | struct n_hdlc_buf_list rx_buf_list; | ||
157 | struct n_hdlc_buf_list tx_free_buf_list; | ||
158 | struct n_hdlc_buf_list rx_free_buf_list; | ||
159 | }; | ||
160 | |||
161 | /* | ||
162 | * HDLC buffer list manipulation functions | ||
163 | */ | ||
164 | static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list); | ||
165 | static void n_hdlc_buf_put(struct n_hdlc_buf_list *list, | ||
166 | struct n_hdlc_buf *buf); | ||
167 | static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list); | ||
168 | |||
169 | /* Local functions */ | ||
170 | |||
171 | static struct n_hdlc *n_hdlc_alloc (void); | ||
172 | |||
173 | /* debug level can be set by insmod for debugging purposes */ | ||
174 | #define DEBUG_LEVEL_INFO 1 | ||
175 | static int debuglevel; | ||
176 | |||
177 | /* max frame size for memory allocations */ | ||
178 | static int maxframe = 4096; | ||
179 | |||
180 | /* TTY callbacks */ | ||
181 | |||
182 | static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, | ||
183 | __u8 __user *buf, size_t nr); | ||
184 | static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, | ||
185 | const unsigned char *buf, size_t nr); | ||
186 | static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, | ||
187 | unsigned int cmd, unsigned long arg); | ||
188 | static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp, | ||
189 | poll_table *wait); | ||
190 | static int n_hdlc_tty_open(struct tty_struct *tty); | ||
191 | static void n_hdlc_tty_close(struct tty_struct *tty); | ||
192 | static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp, | ||
193 | char *fp, int count); | ||
194 | static void n_hdlc_tty_wakeup(struct tty_struct *tty); | ||
195 | |||
196 | #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f))) | ||
197 | |||
198 | #define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data)) | ||
199 | #define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty) | ||
200 | |||
201 | static void flush_rx_queue(struct tty_struct *tty) | ||
202 | { | ||
203 | struct n_hdlc *n_hdlc = tty2n_hdlc(tty); | ||
204 | struct n_hdlc_buf *buf; | ||
205 | |||
206 | while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list))) | ||
207 | n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf); | ||
208 | } | ||
209 | |||
210 | static void flush_tx_queue(struct tty_struct *tty) | ||
211 | { | ||
212 | struct n_hdlc *n_hdlc = tty2n_hdlc(tty); | ||
213 | struct n_hdlc_buf *buf; | ||
214 | unsigned long flags; | ||
215 | |||
216 | while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list))) | ||
217 | n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf); | ||
218 | spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); | ||
219 | if (n_hdlc->tbuf) { | ||
220 | n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, n_hdlc->tbuf); | ||
221 | n_hdlc->tbuf = NULL; | ||
222 | } | ||
223 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); | ||
224 | } | ||
225 | |||
226 | static struct tty_ldisc_ops n_hdlc_ldisc = { | ||
227 | .owner = THIS_MODULE, | ||
228 | .magic = TTY_LDISC_MAGIC, | ||
229 | .name = "hdlc", | ||
230 | .open = n_hdlc_tty_open, | ||
231 | .close = n_hdlc_tty_close, | ||
232 | .read = n_hdlc_tty_read, | ||
233 | .write = n_hdlc_tty_write, | ||
234 | .ioctl = n_hdlc_tty_ioctl, | ||
235 | .poll = n_hdlc_tty_poll, | ||
236 | .receive_buf = n_hdlc_tty_receive, | ||
237 | .write_wakeup = n_hdlc_tty_wakeup, | ||
238 | .flush_buffer = flush_rx_queue, | ||
239 | }; | ||
240 | |||
241 | /** | ||
242 | * n_hdlc_release - release an n_hdlc per device line discipline info structure | ||
243 | * @n_hdlc - per device line discipline info structure | ||
244 | */ | ||
245 | static void n_hdlc_release(struct n_hdlc *n_hdlc) | ||
246 | { | ||
247 | struct tty_struct *tty = n_hdlc2tty (n_hdlc); | ||
248 | struct n_hdlc_buf *buf; | ||
249 | |||
250 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
251 | printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__); | ||
252 | |||
253 | /* Ensure that the n_hdlcd process is not hanging on select()/poll() */ | ||
254 | wake_up_interruptible (&tty->read_wait); | ||
255 | wake_up_interruptible (&tty->write_wait); | ||
256 | |||
257 | if (tty->disc_data == n_hdlc) | ||
258 | tty->disc_data = NULL; /* Break the tty->n_hdlc link */ | ||
259 | |||
260 | /* Release transmit and receive buffers */ | ||
261 | for(;;) { | ||
262 | buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list); | ||
263 | if (buf) { | ||
264 | kfree(buf); | ||
265 | } else | ||
266 | break; | ||
267 | } | ||
268 | for(;;) { | ||
269 | buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list); | ||
270 | if (buf) { | ||
271 | kfree(buf); | ||
272 | } else | ||
273 | break; | ||
274 | } | ||
275 | for(;;) { | ||
276 | buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list); | ||
277 | if (buf) { | ||
278 | kfree(buf); | ||
279 | } else | ||
280 | break; | ||
281 | } | ||
282 | for(;;) { | ||
283 | buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); | ||
284 | if (buf) { | ||
285 | kfree(buf); | ||
286 | } else | ||
287 | break; | ||
288 | } | ||
289 | kfree(n_hdlc->tbuf); | ||
290 | kfree(n_hdlc); | ||
291 | |||
292 | } /* end of n_hdlc_release() */ | ||
293 | |||
294 | /** | ||
295 | * n_hdlc_tty_close - line discipline close | ||
296 | * @tty - pointer to tty info structure | ||
297 | * | ||
298 | * Called when the line discipline is changed to something | ||
299 | * else, the tty is closed, or the tty detects a hangup. | ||
300 | */ | ||
301 | static void n_hdlc_tty_close(struct tty_struct *tty) | ||
302 | { | ||
303 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | ||
304 | |||
305 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
306 | printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__); | ||
307 | |||
308 | if (n_hdlc != NULL) { | ||
309 | if (n_hdlc->magic != HDLC_MAGIC) { | ||
310 | printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n"); | ||
311 | return; | ||
312 | } | ||
313 | #if defined(TTY_NO_WRITE_SPLIT) | ||
314 | clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags); | ||
315 | #endif | ||
316 | tty->disc_data = NULL; | ||
317 | if (tty == n_hdlc->backup_tty) | ||
318 | n_hdlc->backup_tty = NULL; | ||
319 | if (tty != n_hdlc->tty) | ||
320 | return; | ||
321 | if (n_hdlc->backup_tty) { | ||
322 | n_hdlc->tty = n_hdlc->backup_tty; | ||
323 | } else { | ||
324 | n_hdlc_release (n_hdlc); | ||
325 | } | ||
326 | } | ||
327 | |||
328 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
329 | printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__); | ||
330 | |||
331 | } /* end of n_hdlc_tty_close() */ | ||
332 | |||
333 | /** | ||
334 | * n_hdlc_tty_open - called when line discipline changed to n_hdlc | ||
335 | * @tty - pointer to tty info structure | ||
336 | * | ||
337 | * Returns 0 if success, otherwise error code | ||
338 | */ | ||
339 | static int n_hdlc_tty_open (struct tty_struct *tty) | ||
340 | { | ||
341 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | ||
342 | |||
343 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
344 | printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n", | ||
345 | __FILE__,__LINE__, | ||
346 | tty->name); | ||
347 | |||
348 | /* There should not be an existing table for this slot. */ | ||
349 | if (n_hdlc) { | ||
350 | printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" ); | ||
351 | return -EEXIST; | ||
352 | } | ||
353 | |||
354 | n_hdlc = n_hdlc_alloc(); | ||
355 | if (!n_hdlc) { | ||
356 | printk (KERN_ERR "n_hdlc_alloc failed\n"); | ||
357 | return -ENFILE; | ||
358 | } | ||
359 | |||
360 | tty->disc_data = n_hdlc; | ||
361 | n_hdlc->tty = tty; | ||
362 | tty->receive_room = 65536; | ||
363 | |||
364 | #if defined(TTY_NO_WRITE_SPLIT) | ||
365 | /* change tty_io write() to not split large writes into 8K chunks */ | ||
366 | set_bit(TTY_NO_WRITE_SPLIT,&tty->flags); | ||
367 | #endif | ||
368 | |||
369 | /* flush receive data from driver */ | ||
370 | tty_driver_flush_buffer(tty); | ||
371 | |||
372 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
373 | printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__); | ||
374 | |||
375 | return 0; | ||
376 | |||
377 | } /* end of n_tty_hdlc_open() */ | ||
378 | |||
379 | /** | ||
380 | * n_hdlc_send_frames - send frames on pending send buffer list | ||
381 | * @n_hdlc - pointer to ldisc instance data | ||
382 | * @tty - pointer to tty instance data | ||
383 | * | ||
384 | * Send frames on pending send buffer list until the driver does not accept a | ||
385 | * frame (busy) this function is called after adding a frame to the send buffer | ||
386 | * list and by the tty wakeup callback. | ||
387 | */ | ||
388 | static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty) | ||
389 | { | ||
390 | register int actual; | ||
391 | unsigned long flags; | ||
392 | struct n_hdlc_buf *tbuf; | ||
393 | |||
394 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
395 | printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__); | ||
396 | check_again: | ||
397 | |||
398 | spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); | ||
399 | if (n_hdlc->tbusy) { | ||
400 | n_hdlc->woke_up = 1; | ||
401 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); | ||
402 | return; | ||
403 | } | ||
404 | n_hdlc->tbusy = 1; | ||
405 | n_hdlc->woke_up = 0; | ||
406 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); | ||
407 | |||
408 | /* get current transmit buffer or get new transmit */ | ||
409 | /* buffer from list of pending transmit buffers */ | ||
410 | |||
411 | tbuf = n_hdlc->tbuf; | ||
412 | if (!tbuf) | ||
413 | tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); | ||
414 | |||
415 | while (tbuf) { | ||
416 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
417 | printk("%s(%d)sending frame %p, count=%d\n", | ||
418 | __FILE__,__LINE__,tbuf,tbuf->count); | ||
419 | |||
420 | /* Send the next block of data to device */ | ||
421 | tty->flags |= (1 << TTY_DO_WRITE_WAKEUP); | ||
422 | actual = tty->ops->write(tty, tbuf->buf, tbuf->count); | ||
423 | |||
424 | /* rollback was possible and has been done */ | ||
425 | if (actual == -ERESTARTSYS) { | ||
426 | n_hdlc->tbuf = tbuf; | ||
427 | break; | ||
428 | } | ||
429 | /* if transmit error, throw frame away by */ | ||
430 | /* pretending it was accepted by driver */ | ||
431 | if (actual < 0) | ||
432 | actual = tbuf->count; | ||
433 | |||
434 | if (actual == tbuf->count) { | ||
435 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
436 | printk("%s(%d)frame %p completed\n", | ||
437 | __FILE__,__LINE__,tbuf); | ||
438 | |||
439 | /* free current transmit buffer */ | ||
440 | n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf); | ||
441 | |||
442 | /* this tx buffer is done */ | ||
443 | n_hdlc->tbuf = NULL; | ||
444 | |||
445 | /* wait up sleeping writers */ | ||
446 | wake_up_interruptible(&tty->write_wait); | ||
447 | |||
448 | /* get next pending transmit buffer */ | ||
449 | tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); | ||
450 | } else { | ||
451 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
452 | printk("%s(%d)frame %p pending\n", | ||
453 | __FILE__,__LINE__,tbuf); | ||
454 | |||
455 | /* buffer not accepted by driver */ | ||
456 | /* set this buffer as pending buffer */ | ||
457 | n_hdlc->tbuf = tbuf; | ||
458 | break; | ||
459 | } | ||
460 | } | ||
461 | |||
462 | if (!tbuf) | ||
463 | tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); | ||
464 | |||
465 | /* Clear the re-entry flag */ | ||
466 | spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); | ||
467 | n_hdlc->tbusy = 0; | ||
468 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); | ||
469 | |||
470 | if (n_hdlc->woke_up) | ||
471 | goto check_again; | ||
472 | |||
473 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
474 | printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__); | ||
475 | |||
476 | } /* end of n_hdlc_send_frames() */ | ||
477 | |||
478 | /** | ||
479 | * n_hdlc_tty_wakeup - Callback for transmit wakeup | ||
480 | * @tty - pointer to associated tty instance data | ||
481 | * | ||
482 | * Called when low level device driver can accept more send data. | ||
483 | */ | ||
484 | static void n_hdlc_tty_wakeup(struct tty_struct *tty) | ||
485 | { | ||
486 | struct n_hdlc *n_hdlc = tty2n_hdlc(tty); | ||
487 | |||
488 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
489 | printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__); | ||
490 | |||
491 | if (!n_hdlc) | ||
492 | return; | ||
493 | |||
494 | if (tty != n_hdlc->tty) { | ||
495 | tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); | ||
496 | return; | ||
497 | } | ||
498 | |||
499 | n_hdlc_send_frames (n_hdlc, tty); | ||
500 | |||
501 | } /* end of n_hdlc_tty_wakeup() */ | ||
502 | |||
503 | /** | ||
504 | * n_hdlc_tty_receive - Called by tty driver when receive data is available | ||
505 | * @tty - pointer to tty instance data | ||
506 | * @data - pointer to received data | ||
507 | * @flags - pointer to flags for data | ||
508 | * @count - count of received data in bytes | ||
509 | * | ||
510 | * Called by tty low level driver when receive data is available. Data is | ||
511 | * interpreted as one HDLC frame. | ||
512 | */ | ||
513 | static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data, | ||
514 | char *flags, int count) | ||
515 | { | ||
516 | register struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | ||
517 | register struct n_hdlc_buf *buf; | ||
518 | |||
519 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
520 | printk("%s(%d)n_hdlc_tty_receive() called count=%d\n", | ||
521 | __FILE__,__LINE__, count); | ||
522 | |||
523 | /* This can happen if stuff comes in on the backup tty */ | ||
524 | if (!n_hdlc || tty != n_hdlc->tty) | ||
525 | return; | ||
526 | |||
527 | /* verify line is using HDLC discipline */ | ||
528 | if (n_hdlc->magic != HDLC_MAGIC) { | ||
529 | printk("%s(%d) line not using HDLC discipline\n", | ||
530 | __FILE__,__LINE__); | ||
531 | return; | ||
532 | } | ||
533 | |||
534 | if ( count>maxframe ) { | ||
535 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
536 | printk("%s(%d) rx count>maxframesize, data discarded\n", | ||
537 | __FILE__,__LINE__); | ||
538 | return; | ||
539 | } | ||
540 | |||
541 | /* get a free HDLC buffer */ | ||
542 | buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list); | ||
543 | if (!buf) { | ||
544 | /* no buffers in free list, attempt to allocate another rx buffer */ | ||
545 | /* unless the maximum count has been reached */ | ||
546 | if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT) | ||
547 | buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC); | ||
548 | } | ||
549 | |||
550 | if (!buf) { | ||
551 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
552 | printk("%s(%d) no more rx buffers, data discarded\n", | ||
553 | __FILE__,__LINE__); | ||
554 | return; | ||
555 | } | ||
556 | |||
557 | /* copy received data to HDLC buffer */ | ||
558 | memcpy(buf->buf,data,count); | ||
559 | buf->count=count; | ||
560 | |||
561 | /* add HDLC buffer to list of received frames */ | ||
562 | n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf); | ||
563 | |||
564 | /* wake up any blocked reads and perform async signalling */ | ||
565 | wake_up_interruptible (&tty->read_wait); | ||
566 | if (n_hdlc->tty->fasync != NULL) | ||
567 | kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN); | ||
568 | |||
569 | } /* end of n_hdlc_tty_receive() */ | ||
570 | |||
571 | /** | ||
572 | * n_hdlc_tty_read - Called to retrieve one frame of data (if available) | ||
573 | * @tty - pointer to tty instance data | ||
574 | * @file - pointer to open file object | ||
575 | * @buf - pointer to returned data buffer | ||
576 | * @nr - size of returned data buffer | ||
577 | * | ||
578 | * Returns the number of bytes returned or error code. | ||
579 | */ | ||
580 | static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, | ||
581 | __u8 __user *buf, size_t nr) | ||
582 | { | ||
583 | struct n_hdlc *n_hdlc = tty2n_hdlc(tty); | ||
584 | int ret; | ||
585 | struct n_hdlc_buf *rbuf; | ||
586 | |||
587 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
588 | printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__); | ||
589 | |||
590 | /* Validate the pointers */ | ||
591 | if (!n_hdlc) | ||
592 | return -EIO; | ||
593 | |||
594 | /* verify user access to buffer */ | ||
595 | if (!access_ok(VERIFY_WRITE, buf, nr)) { | ||
596 | printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user " | ||
597 | "buffer\n", __FILE__, __LINE__); | ||
598 | return -EFAULT; | ||
599 | } | ||
600 | |||
601 | tty_lock(); | ||
602 | |||
603 | for (;;) { | ||
604 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { | ||
605 | tty_unlock(); | ||
606 | return -EIO; | ||
607 | } | ||
608 | |||
609 | n_hdlc = tty2n_hdlc (tty); | ||
610 | if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || | ||
611 | tty != n_hdlc->tty) { | ||
612 | tty_unlock(); | ||
613 | return 0; | ||
614 | } | ||
615 | |||
616 | rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list); | ||
617 | if (rbuf) | ||
618 | break; | ||
619 | |||
620 | /* no data */ | ||
621 | if (file->f_flags & O_NONBLOCK) { | ||
622 | tty_unlock(); | ||
623 | return -EAGAIN; | ||
624 | } | ||
625 | |||
626 | interruptible_sleep_on (&tty->read_wait); | ||
627 | if (signal_pending(current)) { | ||
628 | tty_unlock(); | ||
629 | return -EINTR; | ||
630 | } | ||
631 | } | ||
632 | |||
633 | if (rbuf->count > nr) | ||
634 | /* frame too large for caller's buffer (discard frame) */ | ||
635 | ret = -EOVERFLOW; | ||
636 | else { | ||
637 | /* Copy the data to the caller's buffer */ | ||
638 | if (copy_to_user(buf, rbuf->buf, rbuf->count)) | ||
639 | ret = -EFAULT; | ||
640 | else | ||
641 | ret = rbuf->count; | ||
642 | } | ||
643 | |||
644 | /* return HDLC buffer to free list unless the free list */ | ||
645 | /* count has exceeded the default value, in which case the */ | ||
646 | /* buffer is freed back to the OS to conserve memory */ | ||
647 | if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT) | ||
648 | kfree(rbuf); | ||
649 | else | ||
650 | n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf); | ||
651 | tty_unlock(); | ||
652 | return ret; | ||
653 | |||
654 | } /* end of n_hdlc_tty_read() */ | ||
655 | |||
656 | /** | ||
657 | * n_hdlc_tty_write - write a single frame of data to device | ||
658 | * @tty - pointer to associated tty device instance data | ||
659 | * @file - pointer to file object data | ||
660 | * @data - pointer to transmit data (one frame) | ||
661 | * @count - size of transmit frame in bytes | ||
662 | * | ||
663 | * Returns the number of bytes written (or error code). | ||
664 | */ | ||
665 | static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, | ||
666 | const unsigned char *data, size_t count) | ||
667 | { | ||
668 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | ||
669 | int error = 0; | ||
670 | DECLARE_WAITQUEUE(wait, current); | ||
671 | struct n_hdlc_buf *tbuf; | ||
672 | |||
673 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
674 | printk("%s(%d)n_hdlc_tty_write() called count=%Zd\n", | ||
675 | __FILE__,__LINE__,count); | ||
676 | |||
677 | /* Verify pointers */ | ||
678 | if (!n_hdlc) | ||
679 | return -EIO; | ||
680 | |||
681 | if (n_hdlc->magic != HDLC_MAGIC) | ||
682 | return -EIO; | ||
683 | |||
684 | /* verify frame size */ | ||
685 | if (count > maxframe ) { | ||
686 | if (debuglevel & DEBUG_LEVEL_INFO) | ||
687 | printk (KERN_WARNING | ||
688 | "n_hdlc_tty_write: truncating user packet " | ||
689 | "from %lu to %d\n", (unsigned long) count, | ||
690 | maxframe ); | ||
691 | count = maxframe; | ||
692 | } | ||
693 | |||
694 | tty_lock(); | ||
695 | |||
696 | add_wait_queue(&tty->write_wait, &wait); | ||
697 | set_current_state(TASK_INTERRUPTIBLE); | ||
698 | |||
699 | /* Allocate transmit buffer */ | ||
700 | /* sleep until transmit buffer available */ | ||
701 | while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) { | ||
702 | if (file->f_flags & O_NONBLOCK) { | ||
703 | error = -EAGAIN; | ||
704 | break; | ||
705 | } | ||
706 | schedule(); | ||
707 | |||
708 | n_hdlc = tty2n_hdlc (tty); | ||
709 | if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || | ||
710 | tty != n_hdlc->tty) { | ||
711 | printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc); | ||
712 | error = -EIO; | ||
713 | break; | ||
714 | } | ||
715 | |||
716 | if (signal_pending(current)) { | ||
717 | error = -EINTR; | ||
718 | break; | ||
719 | } | ||
720 | } | ||
721 | |||
722 | set_current_state(TASK_RUNNING); | ||
723 | remove_wait_queue(&tty->write_wait, &wait); | ||
724 | |||
725 | if (!error) { | ||
726 | /* Retrieve the user's buffer */ | ||
727 | memcpy(tbuf->buf, data, count); | ||
728 | |||
729 | /* Send the data */ | ||
730 | tbuf->count = error = count; | ||
731 | n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf); | ||
732 | n_hdlc_send_frames(n_hdlc,tty); | ||
733 | } | ||
734 | tty_unlock(); | ||
735 | return error; | ||
736 | |||
737 | } /* end of n_hdlc_tty_write() */ | ||
738 | |||
739 | /** | ||
740 | * n_hdlc_tty_ioctl - process IOCTL system call for the tty device. | ||
741 | * @tty - pointer to tty instance data | ||
742 | * @file - pointer to open file object for device | ||
743 | * @cmd - IOCTL command code | ||
744 | * @arg - argument for IOCTL call (cmd dependent) | ||
745 | * | ||
746 | * Returns command dependent result. | ||
747 | */ | ||
748 | static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, | ||
749 | unsigned int cmd, unsigned long arg) | ||
750 | { | ||
751 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | ||
752 | int error = 0; | ||
753 | int count; | ||
754 | unsigned long flags; | ||
755 | |||
756 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
757 | printk("%s(%d)n_hdlc_tty_ioctl() called %d\n", | ||
758 | __FILE__,__LINE__,cmd); | ||
759 | |||
760 | /* Verify the status of the device */ | ||
761 | if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC) | ||
762 | return -EBADF; | ||
763 | |||
764 | switch (cmd) { | ||
765 | case FIONREAD: | ||
766 | /* report count of read data available */ | ||
767 | /* in next available frame (if any) */ | ||
768 | spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags); | ||
769 | if (n_hdlc->rx_buf_list.head) | ||
770 | count = n_hdlc->rx_buf_list.head->count; | ||
771 | else | ||
772 | count = 0; | ||
773 | spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags); | ||
774 | error = put_user(count, (int __user *)arg); | ||
775 | break; | ||
776 | |||
777 | case TIOCOUTQ: | ||
778 | /* get the pending tx byte count in the driver */ | ||
779 | count = tty_chars_in_buffer(tty); | ||
780 | /* add size of next output frame in queue */ | ||
781 | spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags); | ||
782 | if (n_hdlc->tx_buf_list.head) | ||
783 | count += n_hdlc->tx_buf_list.head->count; | ||
784 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags); | ||
785 | error = put_user(count, (int __user *)arg); | ||
786 | break; | ||
787 | |||
788 | case TCFLSH: | ||
789 | switch (arg) { | ||
790 | case TCIOFLUSH: | ||
791 | case TCOFLUSH: | ||
792 | flush_tx_queue(tty); | ||
793 | } | ||
794 | /* fall through to default */ | ||
795 | |||
796 | default: | ||
797 | error = n_tty_ioctl_helper(tty, file, cmd, arg); | ||
798 | break; | ||
799 | } | ||
800 | return error; | ||
801 | |||
802 | } /* end of n_hdlc_tty_ioctl() */ | ||
803 | |||
804 | /** | ||
805 | * n_hdlc_tty_poll - TTY callback for poll system call | ||
806 | * @tty - pointer to tty instance data | ||
807 | * @filp - pointer to open file object for device | ||
808 | * @poll_table - wait queue for operations | ||
809 | * | ||
810 | * Determine which operations (read/write) will not block and return info | ||
811 | * to caller. | ||
812 | * Returns a bit mask containing info on which ops will not block. | ||
813 | */ | ||
814 | static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp, | ||
815 | poll_table *wait) | ||
816 | { | ||
817 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | ||
818 | unsigned int mask = 0; | ||
819 | |||
820 | if (debuglevel >= DEBUG_LEVEL_INFO) | ||
821 | printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__); | ||
822 | |||
823 | if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) { | ||
824 | /* queue current process into any wait queue that */ | ||
825 | /* may awaken in the future (read and write) */ | ||
826 | |||
827 | poll_wait(filp, &tty->read_wait, wait); | ||
828 | poll_wait(filp, &tty->write_wait, wait); | ||
829 | |||
830 | /* set bits for operations that won't block */ | ||
831 | if (n_hdlc->rx_buf_list.head) | ||
832 | mask |= POLLIN | POLLRDNORM; /* readable */ | ||
833 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | ||
834 | mask |= POLLHUP; | ||
835 | if (tty_hung_up_p(filp)) | ||
836 | mask |= POLLHUP; | ||
837 | if (!tty_is_writelocked(tty) && | ||
838 | n_hdlc->tx_free_buf_list.head) | ||
839 | mask |= POLLOUT | POLLWRNORM; /* writable */ | ||
840 | } | ||
841 | return mask; | ||
842 | } /* end of n_hdlc_tty_poll() */ | ||
843 | |||
844 | /** | ||
845 | * n_hdlc_alloc - allocate an n_hdlc instance data structure | ||
846 | * | ||
847 | * Returns a pointer to newly created structure if success, otherwise %NULL | ||
848 | */ | ||
849 | static struct n_hdlc *n_hdlc_alloc(void) | ||
850 | { | ||
851 | struct n_hdlc_buf *buf; | ||
852 | int i; | ||
853 | struct n_hdlc *n_hdlc = kmalloc(sizeof(*n_hdlc), GFP_KERNEL); | ||
854 | |||
855 | if (!n_hdlc) | ||
856 | return NULL; | ||
857 | |||
858 | memset(n_hdlc, 0, sizeof(*n_hdlc)); | ||
859 | |||
860 | n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list); | ||
861 | n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list); | ||
862 | n_hdlc_buf_list_init(&n_hdlc->rx_buf_list); | ||
863 | n_hdlc_buf_list_init(&n_hdlc->tx_buf_list); | ||
864 | |||
865 | /* allocate free rx buffer list */ | ||
866 | for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) { | ||
867 | buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL); | ||
868 | if (buf) | ||
869 | n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf); | ||
870 | else if (debuglevel >= DEBUG_LEVEL_INFO) | ||
871 | printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i); | ||
872 | } | ||
873 | |||
874 | /* allocate free tx buffer list */ | ||
875 | for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) { | ||
876 | buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL); | ||
877 | if (buf) | ||
878 | n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf); | ||
879 | else if (debuglevel >= DEBUG_LEVEL_INFO) | ||
880 | printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i); | ||
881 | } | ||
882 | |||
883 | /* Initialize the control block */ | ||
884 | n_hdlc->magic = HDLC_MAGIC; | ||
885 | n_hdlc->flags = 0; | ||
886 | |||
887 | return n_hdlc; | ||
888 | |||
889 | } /* end of n_hdlc_alloc() */ | ||
890 | |||
891 | /** | ||
892 | * n_hdlc_buf_list_init - initialize specified HDLC buffer list | ||
893 | * @list - pointer to buffer list | ||
894 | */ | ||
895 | static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list) | ||
896 | { | ||
897 | memset(list, 0, sizeof(*list)); | ||
898 | spin_lock_init(&list->spinlock); | ||
899 | } /* end of n_hdlc_buf_list_init() */ | ||
900 | |||
901 | /** | ||
902 | * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list | ||
903 | * @list - pointer to buffer list | ||
904 | * @buf - pointer to buffer | ||
905 | */ | ||
906 | static void n_hdlc_buf_put(struct n_hdlc_buf_list *list, | ||
907 | struct n_hdlc_buf *buf) | ||
908 | { | ||
909 | unsigned long flags; | ||
910 | spin_lock_irqsave(&list->spinlock,flags); | ||
911 | |||
912 | buf->link=NULL; | ||
913 | if (list->tail) | ||
914 | list->tail->link = buf; | ||
915 | else | ||
916 | list->head = buf; | ||
917 | list->tail = buf; | ||
918 | (list->count)++; | ||
919 | |||
920 | spin_unlock_irqrestore(&list->spinlock,flags); | ||
921 | |||
922 | } /* end of n_hdlc_buf_put() */ | ||
923 | |||
924 | /** | ||
925 | * n_hdlc_buf_get - remove and return an HDLC buffer from list | ||
926 | * @list - pointer to HDLC buffer list | ||
927 | * | ||
928 | * Remove and return an HDLC buffer from the head of the specified HDLC buffer | ||
929 | * list. | ||
930 | * Returns a pointer to HDLC buffer if available, otherwise %NULL. | ||
931 | */ | ||
932 | static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list) | ||
933 | { | ||
934 | unsigned long flags; | ||
935 | struct n_hdlc_buf *buf; | ||
936 | spin_lock_irqsave(&list->spinlock,flags); | ||
937 | |||
938 | buf = list->head; | ||
939 | if (buf) { | ||
940 | list->head = buf->link; | ||
941 | (list->count)--; | ||
942 | } | ||
943 | if (!list->head) | ||
944 | list->tail = NULL; | ||
945 | |||
946 | spin_unlock_irqrestore(&list->spinlock,flags); | ||
947 | return buf; | ||
948 | |||
949 | } /* end of n_hdlc_buf_get() */ | ||
950 | |||
951 | static char hdlc_banner[] __initdata = | ||
952 | KERN_INFO "HDLC line discipline maxframe=%u\n"; | ||
953 | static char hdlc_register_ok[] __initdata = | ||
954 | KERN_INFO "N_HDLC line discipline registered.\n"; | ||
955 | static char hdlc_register_fail[] __initdata = | ||
956 | KERN_ERR "error registering line discipline: %d\n"; | ||
957 | static char hdlc_init_fail[] __initdata = | ||
958 | KERN_INFO "N_HDLC: init failure %d\n"; | ||
959 | |||
960 | static int __init n_hdlc_init(void) | ||
961 | { | ||
962 | int status; | ||
963 | |||
964 | /* range check maxframe arg */ | ||
965 | if (maxframe < 4096) | ||
966 | maxframe = 4096; | ||
967 | else if (maxframe > 65535) | ||
968 | maxframe = 65535; | ||
969 | |||
970 | printk(hdlc_banner, maxframe); | ||
971 | |||
972 | status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc); | ||
973 | if (!status) | ||
974 | printk(hdlc_register_ok); | ||
975 | else | ||
976 | printk(hdlc_register_fail, status); | ||
977 | |||
978 | if (status) | ||
979 | printk(hdlc_init_fail, status); | ||
980 | return status; | ||
981 | |||
982 | } /* end of init_module() */ | ||
983 | |||
984 | static char hdlc_unregister_ok[] __exitdata = | ||
985 | KERN_INFO "N_HDLC: line discipline unregistered\n"; | ||
986 | static char hdlc_unregister_fail[] __exitdata = | ||
987 | KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n"; | ||
988 | |||
989 | static void __exit n_hdlc_exit(void) | ||
990 | { | ||
991 | /* Release tty registration of line discipline */ | ||
992 | int status = tty_unregister_ldisc(N_HDLC); | ||
993 | |||
994 | if (status) | ||
995 | printk(hdlc_unregister_fail, status); | ||
996 | else | ||
997 | printk(hdlc_unregister_ok); | ||
998 | } | ||
999 | |||
1000 | module_init(n_hdlc_init); | ||
1001 | module_exit(n_hdlc_exit); | ||
1002 | |||
1003 | MODULE_LICENSE("GPL"); | ||
1004 | MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com"); | ||
1005 | module_param(debuglevel, int, 0); | ||
1006 | module_param(maxframe, int, 0); | ||
1007 | MODULE_ALIAS_LDISC(N_HDLC); | ||
diff --git a/drivers/tty/n_r3964.c b/drivers/tty/n_r3964.c new file mode 100644 index 000000000000..88dda0c45ee0 --- /dev/null +++ b/drivers/tty/n_r3964.c | |||
@@ -0,0 +1,1264 @@ | |||
1 | /* r3964 linediscipline for linux | ||
2 | * | ||
3 | * ----------------------------------------------------------- | ||
4 | * Copyright by | ||
5 | * Philips Automation Projects | ||
6 | * Kassel (Germany) | ||
7 | * ----------------------------------------------------------- | ||
8 | * This software may be used and distributed according to the terms of | ||
9 | * the GNU General Public License, incorporated herein by reference. | ||
10 | * | ||
11 | * Author: | ||
12 | * L. Haag | ||
13 | * | ||
14 | * $Log: n_r3964.c,v $ | ||
15 | * Revision 1.10 2001/03/18 13:02:24 dwmw2 | ||
16 | * Fix timer usage, use spinlocks properly. | ||
17 | * | ||
18 | * Revision 1.9 2001/03/18 12:52:14 dwmw2 | ||
19 | * Merge changes in 2.4.2 | ||
20 | * | ||
21 | * Revision 1.8 2000/03/23 14:14:54 dwmw2 | ||
22 | * Fix race in sleeping in r3964_read() | ||
23 | * | ||
24 | * Revision 1.7 1999/28/08 11:41:50 dwmw2 | ||
25 | * Port to 2.3 kernel | ||
26 | * | ||
27 | * Revision 1.6 1998/09/30 00:40:40 dwmw2 | ||
28 | * Fixed compilation on 2.0.x kernels | ||
29 | * Updated to newly registered tty-ldisc number 9 | ||
30 | * | ||
31 | * Revision 1.5 1998/09/04 21:57:36 dwmw2 | ||
32 | * Signal handling bug fixes, port to 2.1.x. | ||
33 | * | ||
34 | * Revision 1.4 1998/04/02 20:26:59 lhaag | ||
35 | * select, blocking, ... | ||
36 | * | ||
37 | * Revision 1.3 1998/02/12 18:58:43 root | ||
38 | * fixed some memory leaks | ||
39 | * calculation of checksum characters | ||
40 | * | ||
41 | * Revision 1.2 1998/02/07 13:03:34 root | ||
42 | * ioctl read_telegram | ||
43 | * | ||
44 | * Revision 1.1 1998/02/06 19:21:03 root | ||
45 | * Initial revision | ||
46 | * | ||
47 | * | ||
48 | */ | ||
49 | |||
50 | #include <linux/module.h> | ||
51 | #include <linux/kernel.h> | ||
52 | #include <linux/sched.h> | ||
53 | #include <linux/types.h> | ||
54 | #include <linux/fcntl.h> | ||
55 | #include <linux/interrupt.h> | ||
56 | #include <linux/ptrace.h> | ||
57 | #include <linux/ioport.h> | ||
58 | #include <linux/in.h> | ||
59 | #include <linux/slab.h> | ||
60 | #include <linux/smp_lock.h> | ||
61 | #include <linux/tty.h> | ||
62 | #include <linux/errno.h> | ||
63 | #include <linux/string.h> /* used in new tty drivers */ | ||
64 | #include <linux/signal.h> /* used in new tty drivers */ | ||
65 | #include <linux/ioctl.h> | ||
66 | #include <linux/n_r3964.h> | ||
67 | #include <linux/poll.h> | ||
68 | #include <linux/init.h> | ||
69 | #include <asm/uaccess.h> | ||
70 | |||
71 | /*#define DEBUG_QUEUE*/ | ||
72 | |||
73 | /* Log successful handshake and protocol operations */ | ||
74 | /*#define DEBUG_PROTO_S*/ | ||
75 | |||
76 | /* Log handshake and protocol errors: */ | ||
77 | /*#define DEBUG_PROTO_E*/ | ||
78 | |||
79 | /* Log Linediscipline operations (open, close, read, write...): */ | ||
80 | /*#define DEBUG_LDISC*/ | ||
81 | |||
82 | /* Log module and memory operations (init, cleanup; kmalloc, kfree): */ | ||
83 | /*#define DEBUG_MODUL*/ | ||
84 | |||
85 | /* Macro helpers for debug output: */ | ||
86 | #define TRACE(format, args...) printk("r3964: " format "\n" , ## args) | ||
87 | |||
88 | #ifdef DEBUG_MODUL | ||
89 | #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args) | ||
90 | #else | ||
91 | #define TRACE_M(fmt, arg...) do {} while (0) | ||
92 | #endif | ||
93 | #ifdef DEBUG_PROTO_S | ||
94 | #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args) | ||
95 | #else | ||
96 | #define TRACE_PS(fmt, arg...) do {} while (0) | ||
97 | #endif | ||
98 | #ifdef DEBUG_PROTO_E | ||
99 | #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args) | ||
100 | #else | ||
101 | #define TRACE_PE(fmt, arg...) do {} while (0) | ||
102 | #endif | ||
103 | #ifdef DEBUG_LDISC | ||
104 | #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args) | ||
105 | #else | ||
106 | #define TRACE_L(fmt, arg...) do {} while (0) | ||
107 | #endif | ||
108 | #ifdef DEBUG_QUEUE | ||
109 | #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args) | ||
110 | #else | ||
111 | #define TRACE_Q(fmt, arg...) do {} while (0) | ||
112 | #endif | ||
113 | static void add_tx_queue(struct r3964_info *, struct r3964_block_header *); | ||
114 | static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code); | ||
115 | static void put_char(struct r3964_info *pInfo, unsigned char ch); | ||
116 | static void trigger_transmit(struct r3964_info *pInfo); | ||
117 | static void retry_transmit(struct r3964_info *pInfo); | ||
118 | static void transmit_block(struct r3964_info *pInfo); | ||
119 | static void receive_char(struct r3964_info *pInfo, const unsigned char c); | ||
120 | static void receive_error(struct r3964_info *pInfo, const char flag); | ||
121 | static void on_timeout(unsigned long priv); | ||
122 | static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg); | ||
123 | static int read_telegram(struct r3964_info *pInfo, struct pid *pid, | ||
124 | unsigned char __user * buf); | ||
125 | static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg, | ||
126 | int error_code, struct r3964_block_header *pBlock); | ||
127 | static struct r3964_message *remove_msg(struct r3964_info *pInfo, | ||
128 | struct r3964_client_info *pClient); | ||
129 | static void remove_client_block(struct r3964_info *pInfo, | ||
130 | struct r3964_client_info *pClient); | ||
131 | |||
132 | static int r3964_open(struct tty_struct *tty); | ||
133 | static void r3964_close(struct tty_struct *tty); | ||
134 | static ssize_t r3964_read(struct tty_struct *tty, struct file *file, | ||
135 | unsigned char __user * buf, size_t nr); | ||
136 | static ssize_t r3964_write(struct tty_struct *tty, struct file *file, | ||
137 | const unsigned char *buf, size_t nr); | ||
138 | static int r3964_ioctl(struct tty_struct *tty, struct file *file, | ||
139 | unsigned int cmd, unsigned long arg); | ||
140 | static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old); | ||
141 | static unsigned int r3964_poll(struct tty_struct *tty, struct file *file, | ||
142 | struct poll_table_struct *wait); | ||
143 | static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp, | ||
144 | char *fp, int count); | ||
145 | |||
146 | static struct tty_ldisc_ops tty_ldisc_N_R3964 = { | ||
147 | .owner = THIS_MODULE, | ||
148 | .magic = TTY_LDISC_MAGIC, | ||
149 | .name = "R3964", | ||
150 | .open = r3964_open, | ||
151 | .close = r3964_close, | ||
152 | .read = r3964_read, | ||
153 | .write = r3964_write, | ||
154 | .ioctl = r3964_ioctl, | ||
155 | .set_termios = r3964_set_termios, | ||
156 | .poll = r3964_poll, | ||
157 | .receive_buf = r3964_receive_buf, | ||
158 | }; | ||
159 | |||
160 | static void dump_block(const unsigned char *block, unsigned int length) | ||
161 | { | ||
162 | unsigned int i, j; | ||
163 | char linebuf[16 * 3 + 1]; | ||
164 | |||
165 | for (i = 0; i < length; i += 16) { | ||
166 | for (j = 0; (j < 16) && (j + i < length); j++) { | ||
167 | sprintf(linebuf + 3 * j, "%02x ", block[i + j]); | ||
168 | } | ||
169 | linebuf[3 * j] = '\0'; | ||
170 | TRACE_PS("%s", linebuf); | ||
171 | } | ||
172 | } | ||
173 | |||
174 | /************************************************************* | ||
175 | * Driver initialisation | ||
176 | *************************************************************/ | ||
177 | |||
178 | /************************************************************* | ||
179 | * Module support routines | ||
180 | *************************************************************/ | ||
181 | |||
182 | static void __exit r3964_exit(void) | ||
183 | { | ||
184 | int status; | ||
185 | |||
186 | TRACE_M("cleanup_module()"); | ||
187 | |||
188 | status = tty_unregister_ldisc(N_R3964); | ||
189 | |||
190 | if (status != 0) { | ||
191 | printk(KERN_ERR "r3964: error unregistering linediscipline: " | ||
192 | "%d\n", status); | ||
193 | } else { | ||
194 | TRACE_L("linediscipline successfully unregistered"); | ||
195 | } | ||
196 | } | ||
197 | |||
198 | static int __init r3964_init(void) | ||
199 | { | ||
200 | int status; | ||
201 | |||
202 | printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n"); | ||
203 | |||
204 | /* | ||
205 | * Register the tty line discipline | ||
206 | */ | ||
207 | |||
208 | status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964); | ||
209 | if (status == 0) { | ||
210 | TRACE_L("line discipline %d registered", N_R3964); | ||
211 | TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags, | ||
212 | tty_ldisc_N_R3964.num); | ||
213 | TRACE_L("open=%p", tty_ldisc_N_R3964.open); | ||
214 | TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964); | ||
215 | } else { | ||
216 | printk(KERN_ERR "r3964: error registering line discipline: " | ||
217 | "%d\n", status); | ||
218 | } | ||
219 | return status; | ||
220 | } | ||
221 | |||
222 | module_init(r3964_init); | ||
223 | module_exit(r3964_exit); | ||
224 | |||
225 | /************************************************************* | ||
226 | * Protocol implementation routines | ||
227 | *************************************************************/ | ||
228 | |||
229 | static void add_tx_queue(struct r3964_info *pInfo, | ||
230 | struct r3964_block_header *pHeader) | ||
231 | { | ||
232 | unsigned long flags; | ||
233 | |||
234 | spin_lock_irqsave(&pInfo->lock, flags); | ||
235 | |||
236 | pHeader->next = NULL; | ||
237 | |||
238 | if (pInfo->tx_last == NULL) { | ||
239 | pInfo->tx_first = pInfo->tx_last = pHeader; | ||
240 | } else { | ||
241 | pInfo->tx_last->next = pHeader; | ||
242 | pInfo->tx_last = pHeader; | ||
243 | } | ||
244 | |||
245 | spin_unlock_irqrestore(&pInfo->lock, flags); | ||
246 | |||
247 | TRACE_Q("add_tx_queue %p, length %d, tx_first = %p", | ||
248 | pHeader, pHeader->length, pInfo->tx_first); | ||
249 | } | ||
250 | |||
251 | static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code) | ||
252 | { | ||
253 | struct r3964_block_header *pHeader; | ||
254 | unsigned long flags; | ||
255 | #ifdef DEBUG_QUEUE | ||
256 | struct r3964_block_header *pDump; | ||
257 | #endif | ||
258 | |||
259 | pHeader = pInfo->tx_first; | ||
260 | |||
261 | if (pHeader == NULL) | ||
262 | return; | ||
263 | |||
264 | #ifdef DEBUG_QUEUE | ||
265 | printk("r3964: remove_from_tx_queue: %p, length %u - ", | ||
266 | pHeader, pHeader->length); | ||
267 | for (pDump = pHeader; pDump; pDump = pDump->next) | ||
268 | printk("%p ", pDump); | ||
269 | printk("\n"); | ||
270 | #endif | ||
271 | |||
272 | if (pHeader->owner) { | ||
273 | if (error_code) { | ||
274 | add_msg(pHeader->owner, R3964_MSG_ACK, 0, | ||
275 | error_code, NULL); | ||
276 | } else { | ||
277 | add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length, | ||
278 | error_code, NULL); | ||
279 | } | ||
280 | wake_up_interruptible(&pInfo->read_wait); | ||
281 | } | ||
282 | |||
283 | spin_lock_irqsave(&pInfo->lock, flags); | ||
284 | |||
285 | pInfo->tx_first = pHeader->next; | ||
286 | if (pInfo->tx_first == NULL) { | ||
287 | pInfo->tx_last = NULL; | ||
288 | } | ||
289 | |||
290 | spin_unlock_irqrestore(&pInfo->lock, flags); | ||
291 | |||
292 | kfree(pHeader); | ||
293 | TRACE_M("remove_from_tx_queue - kfree %p", pHeader); | ||
294 | |||
295 | TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p", | ||
296 | pInfo->tx_first, pInfo->tx_last); | ||
297 | } | ||
298 | |||
299 | static void add_rx_queue(struct r3964_info *pInfo, | ||
300 | struct r3964_block_header *pHeader) | ||
301 | { | ||
302 | unsigned long flags; | ||
303 | |||
304 | spin_lock_irqsave(&pInfo->lock, flags); | ||
305 | |||
306 | pHeader->next = NULL; | ||
307 | |||
308 | if (pInfo->rx_last == NULL) { | ||
309 | pInfo->rx_first = pInfo->rx_last = pHeader; | ||
310 | } else { | ||
311 | pInfo->rx_last->next = pHeader; | ||
312 | pInfo->rx_last = pHeader; | ||
313 | } | ||
314 | pInfo->blocks_in_rx_queue++; | ||
315 | |||
316 | spin_unlock_irqrestore(&pInfo->lock, flags); | ||
317 | |||
318 | TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d", | ||
319 | pHeader, pHeader->length, | ||
320 | pInfo->rx_first, pInfo->blocks_in_rx_queue); | ||
321 | } | ||
322 | |||
323 | static void remove_from_rx_queue(struct r3964_info *pInfo, | ||
324 | struct r3964_block_header *pHeader) | ||
325 | { | ||
326 | unsigned long flags; | ||
327 | struct r3964_block_header *pFind; | ||
328 | |||
329 | if (pHeader == NULL) | ||
330 | return; | ||
331 | |||
332 | TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d", | ||
333 | pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue); | ||
334 | TRACE_Q("remove_from_rx_queue: %p, length %u", | ||
335 | pHeader, pHeader->length); | ||
336 | |||
337 | spin_lock_irqsave(&pInfo->lock, flags); | ||
338 | |||
339 | if (pInfo->rx_first == pHeader) { | ||
340 | /* Remove the first block in the linked list: */ | ||
341 | pInfo->rx_first = pHeader->next; | ||
342 | |||
343 | if (pInfo->rx_first == NULL) { | ||
344 | pInfo->rx_last = NULL; | ||
345 | } | ||
346 | pInfo->blocks_in_rx_queue--; | ||
347 | } else { | ||
348 | /* Find block to remove: */ | ||
349 | for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) { | ||
350 | if (pFind->next == pHeader) { | ||
351 | /* Got it. */ | ||
352 | pFind->next = pHeader->next; | ||
353 | pInfo->blocks_in_rx_queue--; | ||
354 | if (pFind->next == NULL) { | ||
355 | /* Oh, removed the last one! */ | ||
356 | pInfo->rx_last = pFind; | ||
357 | } | ||
358 | break; | ||
359 | } | ||
360 | } | ||
361 | } | ||
362 | |||
363 | spin_unlock_irqrestore(&pInfo->lock, flags); | ||
364 | |||
365 | kfree(pHeader); | ||
366 | TRACE_M("remove_from_rx_queue - kfree %p", pHeader); | ||
367 | |||
368 | TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d", | ||
369 | pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue); | ||
370 | } | ||
371 | |||
372 | static void put_char(struct r3964_info *pInfo, unsigned char ch) | ||
373 | { | ||
374 | struct tty_struct *tty = pInfo->tty; | ||
375 | /* FIXME: put_char should not be called from an IRQ */ | ||
376 | tty_put_char(tty, ch); | ||
377 | pInfo->bcc ^= ch; | ||
378 | } | ||
379 | |||
380 | static void flush(struct r3964_info *pInfo) | ||
381 | { | ||
382 | struct tty_struct *tty = pInfo->tty; | ||
383 | |||
384 | if (tty == NULL || tty->ops->flush_chars == NULL) | ||
385 | return; | ||
386 | tty->ops->flush_chars(tty); | ||
387 | } | ||
388 | |||
389 | static void trigger_transmit(struct r3964_info *pInfo) | ||
390 | { | ||
391 | unsigned long flags; | ||
392 | |||
393 | spin_lock_irqsave(&pInfo->lock, flags); | ||
394 | |||
395 | if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) { | ||
396 | pInfo->state = R3964_TX_REQUEST; | ||
397 | pInfo->nRetry = 0; | ||
398 | pInfo->flags &= ~R3964_ERROR; | ||
399 | mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); | ||
400 | |||
401 | spin_unlock_irqrestore(&pInfo->lock, flags); | ||
402 | |||
403 | TRACE_PS("trigger_transmit - sent STX"); | ||
404 | |||
405 | put_char(pInfo, STX); | ||
406 | flush(pInfo); | ||
407 | |||
408 | pInfo->bcc = 0; | ||
409 | } else { | ||
410 | spin_unlock_irqrestore(&pInfo->lock, flags); | ||
411 | } | ||
412 | } | ||
413 | |||
414 | static void retry_transmit(struct r3964_info *pInfo) | ||
415 | { | ||
416 | if (pInfo->nRetry < R3964_MAX_RETRIES) { | ||
417 | TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry); | ||
418 | pInfo->bcc = 0; | ||
419 | put_char(pInfo, STX); | ||
420 | flush(pInfo); | ||
421 | pInfo->state = R3964_TX_REQUEST; | ||
422 | pInfo->nRetry++; | ||
423 | mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); | ||
424 | } else { | ||
425 | TRACE_PE("transmission failed after %d retries", | ||
426 | R3964_MAX_RETRIES); | ||
427 | |||
428 | remove_from_tx_queue(pInfo, R3964_TX_FAIL); | ||
429 | |||
430 | put_char(pInfo, NAK); | ||
431 | flush(pInfo); | ||
432 | pInfo->state = R3964_IDLE; | ||
433 | |||
434 | trigger_transmit(pInfo); | ||
435 | } | ||
436 | } | ||
437 | |||
438 | static void transmit_block(struct r3964_info *pInfo) | ||
439 | { | ||
440 | struct tty_struct *tty = pInfo->tty; | ||
441 | struct r3964_block_header *pBlock = pInfo->tx_first; | ||
442 | int room = 0; | ||
443 | |||
444 | if (tty == NULL || pBlock == NULL) { | ||
445 | return; | ||
446 | } | ||
447 | |||
448 | room = tty_write_room(tty); | ||
449 | |||
450 | TRACE_PS("transmit_block %p, room %d, length %d", | ||
451 | pBlock, room, pBlock->length); | ||
452 | |||
453 | while (pInfo->tx_position < pBlock->length) { | ||
454 | if (room < 2) | ||
455 | break; | ||
456 | |||
457 | if (pBlock->data[pInfo->tx_position] == DLE) { | ||
458 | /* send additional DLE char: */ | ||
459 | put_char(pInfo, DLE); | ||
460 | } | ||
461 | put_char(pInfo, pBlock->data[pInfo->tx_position++]); | ||
462 | |||
463 | room--; | ||
464 | } | ||
465 | |||
466 | if ((pInfo->tx_position == pBlock->length) && (room >= 3)) { | ||
467 | put_char(pInfo, DLE); | ||
468 | put_char(pInfo, ETX); | ||
469 | if (pInfo->flags & R3964_BCC) { | ||
470 | put_char(pInfo, pInfo->bcc); | ||
471 | } | ||
472 | pInfo->state = R3964_WAIT_FOR_TX_ACK; | ||
473 | mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); | ||
474 | } | ||
475 | flush(pInfo); | ||
476 | } | ||
477 | |||
478 | static void on_receive_block(struct r3964_info *pInfo) | ||
479 | { | ||
480 | unsigned int length; | ||
481 | struct r3964_client_info *pClient; | ||
482 | struct r3964_block_header *pBlock; | ||
483 | |||
484 | length = pInfo->rx_position; | ||
485 | |||
486 | /* compare byte checksum characters: */ | ||
487 | if (pInfo->flags & R3964_BCC) { | ||
488 | if (pInfo->bcc != pInfo->last_rx) { | ||
489 | TRACE_PE("checksum error - got %x but expected %x", | ||
490 | pInfo->last_rx, pInfo->bcc); | ||
491 | pInfo->flags |= R3964_CHECKSUM; | ||
492 | } | ||
493 | } | ||
494 | |||
495 | /* check for errors (parity, overrun,...): */ | ||
496 | if (pInfo->flags & R3964_ERROR) { | ||
497 | TRACE_PE("on_receive_block - transmission failed error %x", | ||
498 | pInfo->flags & R3964_ERROR); | ||
499 | |||
500 | put_char(pInfo, NAK); | ||
501 | flush(pInfo); | ||
502 | if (pInfo->nRetry < R3964_MAX_RETRIES) { | ||
503 | pInfo->state = R3964_WAIT_FOR_RX_REPEAT; | ||
504 | pInfo->nRetry++; | ||
505 | mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC); | ||
506 | } else { | ||
507 | TRACE_PE("on_receive_block - failed after max retries"); | ||
508 | pInfo->state = R3964_IDLE; | ||
509 | } | ||
510 | return; | ||
511 | } | ||
512 | |||
513 | /* received block; submit DLE: */ | ||
514 | put_char(pInfo, DLE); | ||
515 | flush(pInfo); | ||
516 | del_timer_sync(&pInfo->tmr); | ||
517 | TRACE_PS(" rx success: got %d chars", length); | ||
518 | |||
519 | /* prepare struct r3964_block_header: */ | ||
520 | pBlock = kmalloc(length + sizeof(struct r3964_block_header), | ||
521 | GFP_KERNEL); | ||
522 | TRACE_M("on_receive_block - kmalloc %p", pBlock); | ||
523 | |||
524 | if (pBlock == NULL) | ||
525 | return; | ||
526 | |||
527 | pBlock->length = length; | ||
528 | pBlock->data = ((unsigned char *)pBlock) + | ||
529 | sizeof(struct r3964_block_header); | ||
530 | pBlock->locks = 0; | ||
531 | pBlock->next = NULL; | ||
532 | pBlock->owner = NULL; | ||
533 | |||
534 | memcpy(pBlock->data, pInfo->rx_buf, length); | ||
535 | |||
536 | /* queue block into rx_queue: */ | ||
537 | add_rx_queue(pInfo, pBlock); | ||
538 | |||
539 | /* notify attached client processes: */ | ||
540 | for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) { | ||
541 | if (pClient->sig_flags & R3964_SIG_DATA) { | ||
542 | add_msg(pClient, R3964_MSG_DATA, length, R3964_OK, | ||
543 | pBlock); | ||
544 | } | ||
545 | } | ||
546 | wake_up_interruptible(&pInfo->read_wait); | ||
547 | |||
548 | pInfo->state = R3964_IDLE; | ||
549 | |||
550 | trigger_transmit(pInfo); | ||
551 | } | ||
552 | |||
553 | static void receive_char(struct r3964_info *pInfo, const unsigned char c) | ||
554 | { | ||
555 | switch (pInfo->state) { | ||
556 | case R3964_TX_REQUEST: | ||
557 | if (c == DLE) { | ||
558 | TRACE_PS("TX_REQUEST - got DLE"); | ||
559 | |||
560 | pInfo->state = R3964_TRANSMITTING; | ||
561 | pInfo->tx_position = 0; | ||
562 | |||
563 | transmit_block(pInfo); | ||
564 | } else if (c == STX) { | ||
565 | if (pInfo->nRetry == 0) { | ||
566 | TRACE_PE("TX_REQUEST - init conflict"); | ||
567 | if (pInfo->priority == R3964_SLAVE) { | ||
568 | goto start_receiving; | ||
569 | } | ||
570 | } else { | ||
571 | TRACE_PE("TX_REQUEST - secondary init " | ||
572 | "conflict!? Switching to SLAVE mode " | ||
573 | "for next rx."); | ||
574 | goto start_receiving; | ||
575 | } | ||
576 | } else { | ||
577 | TRACE_PE("TX_REQUEST - char != DLE: %x", c); | ||
578 | retry_transmit(pInfo); | ||
579 | } | ||
580 | break; | ||
581 | case R3964_TRANSMITTING: | ||
582 | if (c == NAK) { | ||
583 | TRACE_PE("TRANSMITTING - got NAK"); | ||
584 | retry_transmit(pInfo); | ||
585 | } else { | ||
586 | TRACE_PE("TRANSMITTING - got invalid char"); | ||
587 | |||
588 | pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY; | ||
589 | mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); | ||
590 | } | ||
591 | break; | ||
592 | case R3964_WAIT_FOR_TX_ACK: | ||
593 | if (c == DLE) { | ||
594 | TRACE_PS("WAIT_FOR_TX_ACK - got DLE"); | ||
595 | remove_from_tx_queue(pInfo, R3964_OK); | ||
596 | |||
597 | pInfo->state = R3964_IDLE; | ||
598 | trigger_transmit(pInfo); | ||
599 | } else { | ||
600 | retry_transmit(pInfo); | ||
601 | } | ||
602 | break; | ||
603 | case R3964_WAIT_FOR_RX_REPEAT: | ||
604 | /* FALLTHROUGH */ | ||
605 | case R3964_IDLE: | ||
606 | if (c == STX) { | ||
607 | /* Prevent rx_queue from overflow: */ | ||
608 | if (pInfo->blocks_in_rx_queue >= | ||
609 | R3964_MAX_BLOCKS_IN_RX_QUEUE) { | ||
610 | TRACE_PE("IDLE - got STX but no space in " | ||
611 | "rx_queue!"); | ||
612 | pInfo->state = R3964_WAIT_FOR_RX_BUF; | ||
613 | mod_timer(&pInfo->tmr, | ||
614 | jiffies + R3964_TO_NO_BUF); | ||
615 | break; | ||
616 | } | ||
617 | start_receiving: | ||
618 | /* Ok, start receiving: */ | ||
619 | TRACE_PS("IDLE - got STX"); | ||
620 | pInfo->rx_position = 0; | ||
621 | pInfo->last_rx = 0; | ||
622 | pInfo->flags &= ~R3964_ERROR; | ||
623 | pInfo->state = R3964_RECEIVING; | ||
624 | mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); | ||
625 | pInfo->nRetry = 0; | ||
626 | put_char(pInfo, DLE); | ||
627 | flush(pInfo); | ||
628 | pInfo->bcc = 0; | ||
629 | } | ||
630 | break; | ||
631 | case R3964_RECEIVING: | ||
632 | if (pInfo->rx_position < RX_BUF_SIZE) { | ||
633 | pInfo->bcc ^= c; | ||
634 | |||
635 | if (c == DLE) { | ||
636 | if (pInfo->last_rx == DLE) { | ||
637 | pInfo->last_rx = 0; | ||
638 | goto char_to_buf; | ||
639 | } | ||
640 | pInfo->last_rx = DLE; | ||
641 | break; | ||
642 | } else if ((c == ETX) && (pInfo->last_rx == DLE)) { | ||
643 | if (pInfo->flags & R3964_BCC) { | ||
644 | pInfo->state = R3964_WAIT_FOR_BCC; | ||
645 | mod_timer(&pInfo->tmr, | ||
646 | jiffies + R3964_TO_ZVZ); | ||
647 | } else { | ||
648 | on_receive_block(pInfo); | ||
649 | } | ||
650 | } else { | ||
651 | pInfo->last_rx = c; | ||
652 | char_to_buf: | ||
653 | pInfo->rx_buf[pInfo->rx_position++] = c; | ||
654 | mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); | ||
655 | } | ||
656 | } | ||
657 | /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */ | ||
658 | break; | ||
659 | case R3964_WAIT_FOR_BCC: | ||
660 | pInfo->last_rx = c; | ||
661 | on_receive_block(pInfo); | ||
662 | break; | ||
663 | } | ||
664 | } | ||
665 | |||
666 | static void receive_error(struct r3964_info *pInfo, const char flag) | ||
667 | { | ||
668 | switch (flag) { | ||
669 | case TTY_NORMAL: | ||
670 | break; | ||
671 | case TTY_BREAK: | ||
672 | TRACE_PE("received break"); | ||
673 | pInfo->flags |= R3964_BREAK; | ||
674 | break; | ||
675 | case TTY_PARITY: | ||
676 | TRACE_PE("parity error"); | ||
677 | pInfo->flags |= R3964_PARITY; | ||
678 | break; | ||
679 | case TTY_FRAME: | ||
680 | TRACE_PE("frame error"); | ||
681 | pInfo->flags |= R3964_FRAME; | ||
682 | break; | ||
683 | case TTY_OVERRUN: | ||
684 | TRACE_PE("frame overrun"); | ||
685 | pInfo->flags |= R3964_OVERRUN; | ||
686 | break; | ||
687 | default: | ||
688 | TRACE_PE("receive_error - unknown flag %d", flag); | ||
689 | pInfo->flags |= R3964_UNKNOWN; | ||
690 | break; | ||
691 | } | ||
692 | } | ||
693 | |||
694 | static void on_timeout(unsigned long priv) | ||
695 | { | ||
696 | struct r3964_info *pInfo = (void *)priv; | ||
697 | |||
698 | switch (pInfo->state) { | ||
699 | case R3964_TX_REQUEST: | ||
700 | TRACE_PE("TX_REQUEST - timeout"); | ||
701 | retry_transmit(pInfo); | ||
702 | break; | ||
703 | case R3964_WAIT_ZVZ_BEFORE_TX_RETRY: | ||
704 | put_char(pInfo, NAK); | ||
705 | flush(pInfo); | ||
706 | retry_transmit(pInfo); | ||
707 | break; | ||
708 | case R3964_WAIT_FOR_TX_ACK: | ||
709 | TRACE_PE("WAIT_FOR_TX_ACK - timeout"); | ||
710 | retry_transmit(pInfo); | ||
711 | break; | ||
712 | case R3964_WAIT_FOR_RX_BUF: | ||
713 | TRACE_PE("WAIT_FOR_RX_BUF - timeout"); | ||
714 | put_char(pInfo, NAK); | ||
715 | flush(pInfo); | ||
716 | pInfo->state = R3964_IDLE; | ||
717 | break; | ||
718 | case R3964_RECEIVING: | ||
719 | TRACE_PE("RECEIVING - timeout after %d chars", | ||
720 | pInfo->rx_position); | ||
721 | put_char(pInfo, NAK); | ||
722 | flush(pInfo); | ||
723 | pInfo->state = R3964_IDLE; | ||
724 | break; | ||
725 | case R3964_WAIT_FOR_RX_REPEAT: | ||
726 | TRACE_PE("WAIT_FOR_RX_REPEAT - timeout"); | ||
727 | pInfo->state = R3964_IDLE; | ||
728 | break; | ||
729 | case R3964_WAIT_FOR_BCC: | ||
730 | TRACE_PE("WAIT_FOR_BCC - timeout"); | ||
731 | put_char(pInfo, NAK); | ||
732 | flush(pInfo); | ||
733 | pInfo->state = R3964_IDLE; | ||
734 | break; | ||
735 | } | ||
736 | } | ||
737 | |||
738 | static struct r3964_client_info *findClient(struct r3964_info *pInfo, | ||
739 | struct pid *pid) | ||
740 | { | ||
741 | struct r3964_client_info *pClient; | ||
742 | |||
743 | for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) { | ||
744 | if (pClient->pid == pid) { | ||
745 | return pClient; | ||
746 | } | ||
747 | } | ||
748 | return NULL; | ||
749 | } | ||
750 | |||
751 | static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg) | ||
752 | { | ||
753 | struct r3964_client_info *pClient; | ||
754 | struct r3964_client_info **ppClient; | ||
755 | struct r3964_message *pMsg; | ||
756 | |||
757 | if ((arg & R3964_SIG_ALL) == 0) { | ||
758 | /* Remove client from client list */ | ||
759 | for (ppClient = &pInfo->firstClient; *ppClient; | ||
760 | ppClient = &(*ppClient)->next) { | ||
761 | pClient = *ppClient; | ||
762 | |||
763 | if (pClient->pid == pid) { | ||
764 | TRACE_PS("removing client %d from client list", | ||
765 | pid_nr(pid)); | ||
766 | *ppClient = pClient->next; | ||
767 | while (pClient->msg_count) { | ||
768 | pMsg = remove_msg(pInfo, pClient); | ||
769 | if (pMsg) { | ||
770 | kfree(pMsg); | ||
771 | TRACE_M("enable_signals - msg " | ||
772 | "kfree %p", pMsg); | ||
773 | } | ||
774 | } | ||
775 | put_pid(pClient->pid); | ||
776 | kfree(pClient); | ||
777 | TRACE_M("enable_signals - kfree %p", pClient); | ||
778 | return 0; | ||
779 | } | ||
780 | } | ||
781 | return -EINVAL; | ||
782 | } else { | ||
783 | pClient = findClient(pInfo, pid); | ||
784 | if (pClient) { | ||
785 | /* update signal options */ | ||
786 | pClient->sig_flags = arg; | ||
787 | } else { | ||
788 | /* add client to client list */ | ||
789 | pClient = kmalloc(sizeof(struct r3964_client_info), | ||
790 | GFP_KERNEL); | ||
791 | TRACE_M("enable_signals - kmalloc %p", pClient); | ||
792 | if (pClient == NULL) | ||
793 | return -ENOMEM; | ||
794 | |||
795 | TRACE_PS("add client %d to client list", pid_nr(pid)); | ||
796 | spin_lock_init(&pClient->lock); | ||
797 | pClient->sig_flags = arg; | ||
798 | pClient->pid = get_pid(pid); | ||
799 | pClient->next = pInfo->firstClient; | ||
800 | pClient->first_msg = NULL; | ||
801 | pClient->last_msg = NULL; | ||
802 | pClient->next_block_to_read = NULL; | ||
803 | pClient->msg_count = 0; | ||
804 | pInfo->firstClient = pClient; | ||
805 | } | ||
806 | } | ||
807 | |||
808 | return 0; | ||
809 | } | ||
810 | |||
811 | static int read_telegram(struct r3964_info *pInfo, struct pid *pid, | ||
812 | unsigned char __user * buf) | ||
813 | { | ||
814 | struct r3964_client_info *pClient; | ||
815 | struct r3964_block_header *block; | ||
816 | |||
817 | if (!buf) { | ||
818 | return -EINVAL; | ||
819 | } | ||
820 | |||
821 | pClient = findClient(pInfo, pid); | ||
822 | if (pClient == NULL) { | ||
823 | return -EINVAL; | ||
824 | } | ||
825 | |||
826 | block = pClient->next_block_to_read; | ||
827 | if (!block) { | ||
828 | return 0; | ||
829 | } else { | ||
830 | if (copy_to_user(buf, block->data, block->length)) | ||
831 | return -EFAULT; | ||
832 | |||
833 | remove_client_block(pInfo, pClient); | ||
834 | return block->length; | ||
835 | } | ||
836 | |||
837 | return -EINVAL; | ||
838 | } | ||
839 | |||
840 | static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg, | ||
841 | int error_code, struct r3964_block_header *pBlock) | ||
842 | { | ||
843 | struct r3964_message *pMsg; | ||
844 | unsigned long flags; | ||
845 | |||
846 | if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) { | ||
847 | queue_the_message: | ||
848 | |||
849 | pMsg = kmalloc(sizeof(struct r3964_message), | ||
850 | error_code ? GFP_ATOMIC : GFP_KERNEL); | ||
851 | TRACE_M("add_msg - kmalloc %p", pMsg); | ||
852 | if (pMsg == NULL) { | ||
853 | return; | ||
854 | } | ||
855 | |||
856 | spin_lock_irqsave(&pClient->lock, flags); | ||
857 | |||
858 | pMsg->msg_id = msg_id; | ||
859 | pMsg->arg = arg; | ||
860 | pMsg->error_code = error_code; | ||
861 | pMsg->block = pBlock; | ||
862 | pMsg->next = NULL; | ||
863 | |||
864 | if (pClient->last_msg == NULL) { | ||
865 | pClient->first_msg = pClient->last_msg = pMsg; | ||
866 | } else { | ||
867 | pClient->last_msg->next = pMsg; | ||
868 | pClient->last_msg = pMsg; | ||
869 | } | ||
870 | |||
871 | pClient->msg_count++; | ||
872 | |||
873 | if (pBlock != NULL) { | ||
874 | pBlock->locks++; | ||
875 | } | ||
876 | spin_unlock_irqrestore(&pClient->lock, flags); | ||
877 | } else { | ||
878 | if ((pClient->last_msg->msg_id == R3964_MSG_ACK) | ||
879 | && (pClient->last_msg->error_code == R3964_OVERFLOW)) { | ||
880 | pClient->last_msg->arg++; | ||
881 | TRACE_PE("add_msg - inc prev OVERFLOW-msg"); | ||
882 | } else { | ||
883 | msg_id = R3964_MSG_ACK; | ||
884 | arg = 0; | ||
885 | error_code = R3964_OVERFLOW; | ||
886 | pBlock = NULL; | ||
887 | TRACE_PE("add_msg - queue OVERFLOW-msg"); | ||
888 | goto queue_the_message; | ||
889 | } | ||
890 | } | ||
891 | /* Send SIGIO signal to client process: */ | ||
892 | if (pClient->sig_flags & R3964_USE_SIGIO) { | ||
893 | kill_pid(pClient->pid, SIGIO, 1); | ||
894 | } | ||
895 | } | ||
896 | |||
897 | static struct r3964_message *remove_msg(struct r3964_info *pInfo, | ||
898 | struct r3964_client_info *pClient) | ||
899 | { | ||
900 | struct r3964_message *pMsg = NULL; | ||
901 | unsigned long flags; | ||
902 | |||
903 | if (pClient->first_msg) { | ||
904 | spin_lock_irqsave(&pClient->lock, flags); | ||
905 | |||
906 | pMsg = pClient->first_msg; | ||
907 | pClient->first_msg = pMsg->next; | ||
908 | if (pClient->first_msg == NULL) { | ||
909 | pClient->last_msg = NULL; | ||
910 | } | ||
911 | |||
912 | pClient->msg_count--; | ||
913 | if (pMsg->block) { | ||
914 | remove_client_block(pInfo, pClient); | ||
915 | pClient->next_block_to_read = pMsg->block; | ||
916 | } | ||
917 | spin_unlock_irqrestore(&pClient->lock, flags); | ||
918 | } | ||
919 | return pMsg; | ||
920 | } | ||
921 | |||
922 | static void remove_client_block(struct r3964_info *pInfo, | ||
923 | struct r3964_client_info *pClient) | ||
924 | { | ||
925 | struct r3964_block_header *block; | ||
926 | |||
927 | TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid)); | ||
928 | |||
929 | block = pClient->next_block_to_read; | ||
930 | if (block) { | ||
931 | block->locks--; | ||
932 | if (block->locks == 0) { | ||
933 | remove_from_rx_queue(pInfo, block); | ||
934 | } | ||
935 | } | ||
936 | pClient->next_block_to_read = NULL; | ||
937 | } | ||
938 | |||
939 | /************************************************************* | ||
940 | * Line discipline routines | ||
941 | *************************************************************/ | ||
942 | |||
943 | static int r3964_open(struct tty_struct *tty) | ||
944 | { | ||
945 | struct r3964_info *pInfo; | ||
946 | |||
947 | TRACE_L("open"); | ||
948 | TRACE_L("tty=%p, PID=%d, disc_data=%p", | ||
949 | tty, current->pid, tty->disc_data); | ||
950 | |||
951 | pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL); | ||
952 | TRACE_M("r3964_open - info kmalloc %p", pInfo); | ||
953 | |||
954 | if (!pInfo) { | ||
955 | printk(KERN_ERR "r3964: failed to alloc info structure\n"); | ||
956 | return -ENOMEM; | ||
957 | } | ||
958 | |||
959 | pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL); | ||
960 | TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf); | ||
961 | |||
962 | if (!pInfo->rx_buf) { | ||
963 | printk(KERN_ERR "r3964: failed to alloc receive buffer\n"); | ||
964 | kfree(pInfo); | ||
965 | TRACE_M("r3964_open - info kfree %p", pInfo); | ||
966 | return -ENOMEM; | ||
967 | } | ||
968 | |||
969 | pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL); | ||
970 | TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf); | ||
971 | |||
972 | if (!pInfo->tx_buf) { | ||
973 | printk(KERN_ERR "r3964: failed to alloc transmit buffer\n"); | ||
974 | kfree(pInfo->rx_buf); | ||
975 | TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf); | ||
976 | kfree(pInfo); | ||
977 | TRACE_M("r3964_open - info kfree %p", pInfo); | ||
978 | return -ENOMEM; | ||
979 | } | ||
980 | |||
981 | spin_lock_init(&pInfo->lock); | ||
982 | pInfo->tty = tty; | ||
983 | init_waitqueue_head(&pInfo->read_wait); | ||
984 | pInfo->priority = R3964_MASTER; | ||
985 | pInfo->rx_first = pInfo->rx_last = NULL; | ||
986 | pInfo->tx_first = pInfo->tx_last = NULL; | ||
987 | pInfo->rx_position = 0; | ||
988 | pInfo->tx_position = 0; | ||
989 | pInfo->last_rx = 0; | ||
990 | pInfo->blocks_in_rx_queue = 0; | ||
991 | pInfo->firstClient = NULL; | ||
992 | pInfo->state = R3964_IDLE; | ||
993 | pInfo->flags = R3964_DEBUG; | ||
994 | pInfo->nRetry = 0; | ||
995 | |||
996 | tty->disc_data = pInfo; | ||
997 | tty->receive_room = 65536; | ||
998 | |||
999 | setup_timer(&pInfo->tmr, on_timeout, (unsigned long)pInfo); | ||
1000 | |||
1001 | return 0; | ||
1002 | } | ||
1003 | |||
1004 | static void r3964_close(struct tty_struct *tty) | ||
1005 | { | ||
1006 | struct r3964_info *pInfo = tty->disc_data; | ||
1007 | struct r3964_client_info *pClient, *pNext; | ||
1008 | struct r3964_message *pMsg; | ||
1009 | struct r3964_block_header *pHeader, *pNextHeader; | ||
1010 | unsigned long flags; | ||
1011 | |||
1012 | TRACE_L("close"); | ||
1013 | |||
1014 | /* | ||
1015 | * Make sure that our task queue isn't activated. If it | ||
1016 | * is, take it out of the linked list. | ||
1017 | */ | ||
1018 | del_timer_sync(&pInfo->tmr); | ||
1019 | |||
1020 | /* Remove client-structs and message queues: */ | ||
1021 | pClient = pInfo->firstClient; | ||
1022 | while (pClient) { | ||
1023 | pNext = pClient->next; | ||
1024 | while (pClient->msg_count) { | ||
1025 | pMsg = remove_msg(pInfo, pClient); | ||
1026 | if (pMsg) { | ||
1027 | kfree(pMsg); | ||
1028 | TRACE_M("r3964_close - msg kfree %p", pMsg); | ||
1029 | } | ||
1030 | } | ||
1031 | put_pid(pClient->pid); | ||
1032 | kfree(pClient); | ||
1033 | TRACE_M("r3964_close - client kfree %p", pClient); | ||
1034 | pClient = pNext; | ||
1035 | } | ||
1036 | /* Remove jobs from tx_queue: */ | ||
1037 | spin_lock_irqsave(&pInfo->lock, flags); | ||
1038 | pHeader = pInfo->tx_first; | ||
1039 | pInfo->tx_first = pInfo->tx_last = NULL; | ||
1040 | spin_unlock_irqrestore(&pInfo->lock, flags); | ||
1041 | |||
1042 | while (pHeader) { | ||
1043 | pNextHeader = pHeader->next; | ||
1044 | kfree(pHeader); | ||
1045 | pHeader = pNextHeader; | ||
1046 | } | ||
1047 | |||
1048 | /* Free buffers: */ | ||
1049 | wake_up_interruptible(&pInfo->read_wait); | ||
1050 | kfree(pInfo->rx_buf); | ||
1051 | TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf); | ||
1052 | kfree(pInfo->tx_buf); | ||
1053 | TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf); | ||
1054 | kfree(pInfo); | ||
1055 | TRACE_M("r3964_close - info kfree %p", pInfo); | ||
1056 | } | ||
1057 | |||
1058 | static ssize_t r3964_read(struct tty_struct *tty, struct file *file, | ||
1059 | unsigned char __user * buf, size_t nr) | ||
1060 | { | ||
1061 | struct r3964_info *pInfo = tty->disc_data; | ||
1062 | struct r3964_client_info *pClient; | ||
1063 | struct r3964_message *pMsg; | ||
1064 | struct r3964_client_message theMsg; | ||
1065 | int ret; | ||
1066 | |||
1067 | TRACE_L("read()"); | ||
1068 | |||
1069 | tty_lock(); | ||
1070 | |||
1071 | pClient = findClient(pInfo, task_pid(current)); | ||
1072 | if (pClient) { | ||
1073 | pMsg = remove_msg(pInfo, pClient); | ||
1074 | if (pMsg == NULL) { | ||
1075 | /* no messages available. */ | ||
1076 | if (file->f_flags & O_NONBLOCK) { | ||
1077 | ret = -EAGAIN; | ||
1078 | goto unlock; | ||
1079 | } | ||
1080 | /* block until there is a message: */ | ||
1081 | wait_event_interruptible_tty(pInfo->read_wait, | ||
1082 | (pMsg = remove_msg(pInfo, pClient))); | ||
1083 | } | ||
1084 | |||
1085 | /* If we still haven't got a message, we must have been signalled */ | ||
1086 | |||
1087 | if (!pMsg) { | ||
1088 | ret = -EINTR; | ||
1089 | goto unlock; | ||
1090 | } | ||
1091 | |||
1092 | /* deliver msg to client process: */ | ||
1093 | theMsg.msg_id = pMsg->msg_id; | ||
1094 | theMsg.arg = pMsg->arg; | ||
1095 | theMsg.error_code = pMsg->error_code; | ||
1096 | ret = sizeof(struct r3964_client_message); | ||
1097 | |||
1098 | kfree(pMsg); | ||
1099 | TRACE_M("r3964_read - msg kfree %p", pMsg); | ||
1100 | |||
1101 | if (copy_to_user(buf, &theMsg, ret)) { | ||
1102 | ret = -EFAULT; | ||
1103 | goto unlock; | ||
1104 | } | ||
1105 | |||
1106 | TRACE_PS("read - return %d", ret); | ||
1107 | goto unlock; | ||
1108 | } | ||
1109 | ret = -EPERM; | ||
1110 | unlock: | ||
1111 | tty_unlock(); | ||
1112 | return ret; | ||
1113 | } | ||
1114 | |||
1115 | static ssize_t r3964_write(struct tty_struct *tty, struct file *file, | ||
1116 | const unsigned char *data, size_t count) | ||
1117 | { | ||
1118 | struct r3964_info *pInfo = tty->disc_data; | ||
1119 | struct r3964_block_header *pHeader; | ||
1120 | struct r3964_client_info *pClient; | ||
1121 | unsigned char *new_data; | ||
1122 | |||
1123 | TRACE_L("write request, %d characters", count); | ||
1124 | /* | ||
1125 | * Verify the pointers | ||
1126 | */ | ||
1127 | |||
1128 | if (!pInfo) | ||
1129 | return -EIO; | ||
1130 | |||
1131 | /* | ||
1132 | * Ensure that the caller does not wish to send too much. | ||
1133 | */ | ||
1134 | if (count > R3964_MTU) { | ||
1135 | if (pInfo->flags & R3964_DEBUG) { | ||
1136 | TRACE_L(KERN_WARNING "r3964_write: truncating user " | ||
1137 | "packet from %u to mtu %d", count, R3964_MTU); | ||
1138 | } | ||
1139 | count = R3964_MTU; | ||
1140 | } | ||
1141 | /* | ||
1142 | * Allocate a buffer for the data and copy it from the buffer with header prepended | ||
1143 | */ | ||
1144 | new_data = kmalloc(count + sizeof(struct r3964_block_header), | ||
1145 | GFP_KERNEL); | ||
1146 | TRACE_M("r3964_write - kmalloc %p", new_data); | ||
1147 | if (new_data == NULL) { | ||
1148 | if (pInfo->flags & R3964_DEBUG) { | ||
1149 | printk(KERN_ERR "r3964_write: no memory\n"); | ||
1150 | } | ||
1151 | return -ENOSPC; | ||
1152 | } | ||
1153 | |||
1154 | pHeader = (struct r3964_block_header *)new_data; | ||
1155 | pHeader->data = new_data + sizeof(struct r3964_block_header); | ||
1156 | pHeader->length = count; | ||
1157 | pHeader->locks = 0; | ||
1158 | pHeader->owner = NULL; | ||
1159 | |||
1160 | tty_lock(); | ||
1161 | |||
1162 | pClient = findClient(pInfo, task_pid(current)); | ||
1163 | if (pClient) { | ||
1164 | pHeader->owner = pClient; | ||
1165 | } | ||
1166 | |||
1167 | memcpy(pHeader->data, data, count); /* We already verified this */ | ||
1168 | |||
1169 | if (pInfo->flags & R3964_DEBUG) { | ||
1170 | dump_block(pHeader->data, count); | ||
1171 | } | ||
1172 | |||
1173 | /* | ||
1174 | * Add buffer to transmit-queue: | ||
1175 | */ | ||
1176 | add_tx_queue(pInfo, pHeader); | ||
1177 | trigger_transmit(pInfo); | ||
1178 | |||
1179 | tty_unlock(); | ||
1180 | |||
1181 | return 0; | ||
1182 | } | ||
1183 | |||
1184 | static int r3964_ioctl(struct tty_struct *tty, struct file *file, | ||
1185 | unsigned int cmd, unsigned long arg) | ||
1186 | { | ||
1187 | struct r3964_info *pInfo = tty->disc_data; | ||
1188 | if (pInfo == NULL) | ||
1189 | return -EINVAL; | ||
1190 | switch (cmd) { | ||
1191 | case R3964_ENABLE_SIGNALS: | ||
1192 | return enable_signals(pInfo, task_pid(current), arg); | ||
1193 | case R3964_SETPRIORITY: | ||
1194 | if (arg < R3964_MASTER || arg > R3964_SLAVE) | ||
1195 | return -EINVAL; | ||
1196 | pInfo->priority = arg & 0xff; | ||
1197 | return 0; | ||
1198 | case R3964_USE_BCC: | ||
1199 | if (arg) | ||
1200 | pInfo->flags |= R3964_BCC; | ||
1201 | else | ||
1202 | pInfo->flags &= ~R3964_BCC; | ||
1203 | return 0; | ||
1204 | case R3964_READ_TELEGRAM: | ||
1205 | return read_telegram(pInfo, task_pid(current), | ||
1206 | (unsigned char __user *)arg); | ||
1207 | default: | ||
1208 | return -ENOIOCTLCMD; | ||
1209 | } | ||
1210 | } | ||
1211 | |||
1212 | static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old) | ||
1213 | { | ||
1214 | TRACE_L("set_termios"); | ||
1215 | } | ||
1216 | |||
1217 | /* Called without the kernel lock held - fine */ | ||
1218 | static unsigned int r3964_poll(struct tty_struct *tty, struct file *file, | ||
1219 | struct poll_table_struct *wait) | ||
1220 | { | ||
1221 | struct r3964_info *pInfo = tty->disc_data; | ||
1222 | struct r3964_client_info *pClient; | ||
1223 | struct r3964_message *pMsg = NULL; | ||
1224 | unsigned long flags; | ||
1225 | int result = POLLOUT; | ||
1226 | |||
1227 | TRACE_L("POLL"); | ||
1228 | |||
1229 | pClient = findClient(pInfo, task_pid(current)); | ||
1230 | if (pClient) { | ||
1231 | poll_wait(file, &pInfo->read_wait, wait); | ||
1232 | spin_lock_irqsave(&pInfo->lock, flags); | ||
1233 | pMsg = pClient->first_msg; | ||
1234 | spin_unlock_irqrestore(&pInfo->lock, flags); | ||
1235 | if (pMsg) | ||
1236 | result |= POLLIN | POLLRDNORM; | ||
1237 | } else { | ||
1238 | result = -EINVAL; | ||
1239 | } | ||
1240 | return result; | ||
1241 | } | ||
1242 | |||
1243 | static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp, | ||
1244 | char *fp, int count) | ||
1245 | { | ||
1246 | struct r3964_info *pInfo = tty->disc_data; | ||
1247 | const unsigned char *p; | ||
1248 | char *f, flags = 0; | ||
1249 | int i; | ||
1250 | |||
1251 | for (i = count, p = cp, f = fp; i; i--, p++) { | ||
1252 | if (f) | ||
1253 | flags = *f++; | ||
1254 | if (flags == TTY_NORMAL) { | ||
1255 | receive_char(pInfo, *p); | ||
1256 | } else { | ||
1257 | receive_error(pInfo, flags); | ||
1258 | } | ||
1259 | |||
1260 | } | ||
1261 | } | ||
1262 | |||
1263 | MODULE_LICENSE("GPL"); | ||
1264 | MODULE_ALIAS_LDISC(N_R3964); | ||
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c new file mode 100644 index 000000000000..428f4fe0b5f7 --- /dev/null +++ b/drivers/tty/n_tty.c | |||
@@ -0,0 +1,2121 @@ | |||
1 | /* | ||
2 | * n_tty.c --- implements the N_TTY line discipline. | ||
3 | * | ||
4 | * This code used to be in tty_io.c, but things are getting hairy | ||
5 | * enough that it made sense to split things off. (The N_TTY | ||
6 | * processing has changed so much that it's hardly recognizable, | ||
7 | * anyway...) | ||
8 | * | ||
9 | * Note that the open routine for N_TTY is guaranteed never to return | ||
10 | * an error. This is because Linux will fall back to setting a line | ||
11 | * to N_TTY if it can not switch to any other line discipline. | ||
12 | * | ||
13 | * Written by Theodore Ts'o, Copyright 1994. | ||
14 | * | ||
15 | * This file also contains code originally written by Linus Torvalds, | ||
16 | * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994. | ||
17 | * | ||
18 | * This file may be redistributed under the terms of the GNU General Public | ||
19 | * License. | ||
20 | * | ||
21 | * Reduced memory usage for older ARM systems - Russell King. | ||
22 | * | ||
23 | * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of | ||
24 | * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu> | ||
25 | * who actually finally proved there really was a race. | ||
26 | * | ||
27 | * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to | ||
28 | * waiting writing processes-Sapan Bhatia <sapan@corewars.org>. | ||
29 | * Also fixed a bug in BLOCKING mode where n_tty_write returns | ||
30 | * EAGAIN | ||
31 | */ | ||
32 | |||
33 | #include <linux/types.h> | ||
34 | #include <linux/major.h> | ||
35 | #include <linux/errno.h> | ||
36 | #include <linux/signal.h> | ||
37 | #include <linux/fcntl.h> | ||
38 | #include <linux/sched.h> | ||
39 | #include <linux/interrupt.h> | ||
40 | #include <linux/tty.h> | ||
41 | #include <linux/timer.h> | ||
42 | #include <linux/ctype.h> | ||
43 | #include <linux/mm.h> | ||
44 | #include <linux/string.h> | ||
45 | #include <linux/slab.h> | ||
46 | #include <linux/poll.h> | ||
47 | #include <linux/bitops.h> | ||
48 | #include <linux/audit.h> | ||
49 | #include <linux/file.h> | ||
50 | #include <linux/uaccess.h> | ||
51 | #include <linux/module.h> | ||
52 | |||
53 | #include <asm/system.h> | ||
54 | |||
55 | /* number of characters left in xmit buffer before select has we have room */ | ||
56 | #define WAKEUP_CHARS 256 | ||
57 | |||
58 | /* | ||
59 | * This defines the low- and high-watermarks for throttling and | ||
60 | * unthrottling the TTY driver. These watermarks are used for | ||
61 | * controlling the space in the read buffer. | ||
62 | */ | ||
63 | #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */ | ||
64 | #define TTY_THRESHOLD_UNTHROTTLE 128 | ||
65 | |||
66 | /* | ||
67 | * Special byte codes used in the echo buffer to represent operations | ||
68 | * or special handling of characters. Bytes in the echo buffer that | ||
69 | * are not part of such special blocks are treated as normal character | ||
70 | * codes. | ||
71 | */ | ||
72 | #define ECHO_OP_START 0xff | ||
73 | #define ECHO_OP_MOVE_BACK_COL 0x80 | ||
74 | #define ECHO_OP_SET_CANON_COL 0x81 | ||
75 | #define ECHO_OP_ERASE_TAB 0x82 | ||
76 | |||
77 | static inline int tty_put_user(struct tty_struct *tty, unsigned char x, | ||
78 | unsigned char __user *ptr) | ||
79 | { | ||
80 | tty_audit_add_data(tty, &x, 1); | ||
81 | return put_user(x, ptr); | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * n_tty_set__room - receive space | ||
86 | * @tty: terminal | ||
87 | * | ||
88 | * Called by the driver to find out how much data it is | ||
89 | * permitted to feed to the line discipline without any being lost | ||
90 | * and thus to manage flow control. Not serialized. Answers for the | ||
91 | * "instant". | ||
92 | */ | ||
93 | |||
94 | static void n_tty_set_room(struct tty_struct *tty) | ||
95 | { | ||
96 | /* tty->read_cnt is not read locked ? */ | ||
97 | int left = N_TTY_BUF_SIZE - tty->read_cnt - 1; | ||
98 | |||
99 | /* | ||
100 | * If we are doing input canonicalization, and there are no | ||
101 | * pending newlines, let characters through without limit, so | ||
102 | * that erase characters will be handled. Other excess | ||
103 | * characters will be beeped. | ||
104 | */ | ||
105 | if (left <= 0) | ||
106 | left = tty->icanon && !tty->canon_data; | ||
107 | tty->receive_room = left; | ||
108 | } | ||
109 | |||
110 | static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty) | ||
111 | { | ||
112 | if (tty->read_cnt < N_TTY_BUF_SIZE) { | ||
113 | tty->read_buf[tty->read_head] = c; | ||
114 | tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1); | ||
115 | tty->read_cnt++; | ||
116 | } | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * put_tty_queue - add character to tty | ||
121 | * @c: character | ||
122 | * @tty: tty device | ||
123 | * | ||
124 | * Add a character to the tty read_buf queue. This is done under the | ||
125 | * read_lock to serialize character addition and also to protect us | ||
126 | * against parallel reads or flushes | ||
127 | */ | ||
128 | |||
129 | static void put_tty_queue(unsigned char c, struct tty_struct *tty) | ||
130 | { | ||
131 | unsigned long flags; | ||
132 | /* | ||
133 | * The problem of stomping on the buffers ends here. | ||
134 | * Why didn't anyone see this one coming? --AJK | ||
135 | */ | ||
136 | spin_lock_irqsave(&tty->read_lock, flags); | ||
137 | put_tty_queue_nolock(c, tty); | ||
138 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
139 | } | ||
140 | |||
141 | /** | ||
142 | * check_unthrottle - allow new receive data | ||
143 | * @tty; tty device | ||
144 | * | ||
145 | * Check whether to call the driver unthrottle functions | ||
146 | * | ||
147 | * Can sleep, may be called under the atomic_read_lock mutex but | ||
148 | * this is not guaranteed. | ||
149 | */ | ||
150 | static void check_unthrottle(struct tty_struct *tty) | ||
151 | { | ||
152 | if (tty->count) | ||
153 | tty_unthrottle(tty); | ||
154 | } | ||
155 | |||
156 | /** | ||
157 | * reset_buffer_flags - reset buffer state | ||
158 | * @tty: terminal to reset | ||
159 | * | ||
160 | * Reset the read buffer counters, clear the flags, | ||
161 | * and make sure the driver is unthrottled. Called | ||
162 | * from n_tty_open() and n_tty_flush_buffer(). | ||
163 | * | ||
164 | * Locking: tty_read_lock for read fields. | ||
165 | */ | ||
166 | |||
167 | static void reset_buffer_flags(struct tty_struct *tty) | ||
168 | { | ||
169 | unsigned long flags; | ||
170 | |||
171 | spin_lock_irqsave(&tty->read_lock, flags); | ||
172 | tty->read_head = tty->read_tail = tty->read_cnt = 0; | ||
173 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
174 | |||
175 | mutex_lock(&tty->echo_lock); | ||
176 | tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0; | ||
177 | mutex_unlock(&tty->echo_lock); | ||
178 | |||
179 | tty->canon_head = tty->canon_data = tty->erasing = 0; | ||
180 | memset(&tty->read_flags, 0, sizeof tty->read_flags); | ||
181 | n_tty_set_room(tty); | ||
182 | check_unthrottle(tty); | ||
183 | } | ||
184 | |||
185 | /** | ||
186 | * n_tty_flush_buffer - clean input queue | ||
187 | * @tty: terminal device | ||
188 | * | ||
189 | * Flush the input buffer. Called when the line discipline is | ||
190 | * being closed, when the tty layer wants the buffer flushed (eg | ||
191 | * at hangup) or when the N_TTY line discipline internally has to | ||
192 | * clean the pending queue (for example some signals). | ||
193 | * | ||
194 | * Locking: ctrl_lock, read_lock. | ||
195 | */ | ||
196 | |||
197 | static void n_tty_flush_buffer(struct tty_struct *tty) | ||
198 | { | ||
199 | unsigned long flags; | ||
200 | /* clear everything and unthrottle the driver */ | ||
201 | reset_buffer_flags(tty); | ||
202 | |||
203 | if (!tty->link) | ||
204 | return; | ||
205 | |||
206 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
207 | if (tty->link->packet) { | ||
208 | tty->ctrl_status |= TIOCPKT_FLUSHREAD; | ||
209 | wake_up_interruptible(&tty->link->read_wait); | ||
210 | } | ||
211 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
212 | } | ||
213 | |||
214 | /** | ||
215 | * n_tty_chars_in_buffer - report available bytes | ||
216 | * @tty: tty device | ||
217 | * | ||
218 | * Report the number of characters buffered to be delivered to user | ||
219 | * at this instant in time. | ||
220 | * | ||
221 | * Locking: read_lock | ||
222 | */ | ||
223 | |||
224 | static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty) | ||
225 | { | ||
226 | unsigned long flags; | ||
227 | ssize_t n = 0; | ||
228 | |||
229 | spin_lock_irqsave(&tty->read_lock, flags); | ||
230 | if (!tty->icanon) { | ||
231 | n = tty->read_cnt; | ||
232 | } else if (tty->canon_data) { | ||
233 | n = (tty->canon_head > tty->read_tail) ? | ||
234 | tty->canon_head - tty->read_tail : | ||
235 | tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail); | ||
236 | } | ||
237 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
238 | return n; | ||
239 | } | ||
240 | |||
241 | /** | ||
242 | * is_utf8_continuation - utf8 multibyte check | ||
243 | * @c: byte to check | ||
244 | * | ||
245 | * Returns true if the utf8 character 'c' is a multibyte continuation | ||
246 | * character. We use this to correctly compute the on screen size | ||
247 | * of the character when printing | ||
248 | */ | ||
249 | |||
250 | static inline int is_utf8_continuation(unsigned char c) | ||
251 | { | ||
252 | return (c & 0xc0) == 0x80; | ||
253 | } | ||
254 | |||
255 | /** | ||
256 | * is_continuation - multibyte check | ||
257 | * @c: byte to check | ||
258 | * | ||
259 | * Returns true if the utf8 character 'c' is a multibyte continuation | ||
260 | * character and the terminal is in unicode mode. | ||
261 | */ | ||
262 | |||
263 | static inline int is_continuation(unsigned char c, struct tty_struct *tty) | ||
264 | { | ||
265 | return I_IUTF8(tty) && is_utf8_continuation(c); | ||
266 | } | ||
267 | |||
268 | /** | ||
269 | * do_output_char - output one character | ||
270 | * @c: character (or partial unicode symbol) | ||
271 | * @tty: terminal device | ||
272 | * @space: space available in tty driver write buffer | ||
273 | * | ||
274 | * This is a helper function that handles one output character | ||
275 | * (including special characters like TAB, CR, LF, etc.), | ||
276 | * doing OPOST processing and putting the results in the | ||
277 | * tty driver's write buffer. | ||
278 | * | ||
279 | * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY | ||
280 | * and NLDLY. They simply aren't relevant in the world today. | ||
281 | * If you ever need them, add them here. | ||
282 | * | ||
283 | * Returns the number of bytes of buffer space used or -1 if | ||
284 | * no space left. | ||
285 | * | ||
286 | * Locking: should be called under the output_lock to protect | ||
287 | * the column state and space left in the buffer | ||
288 | */ | ||
289 | |||
290 | static int do_output_char(unsigned char c, struct tty_struct *tty, int space) | ||
291 | { | ||
292 | int spaces; | ||
293 | |||
294 | if (!space) | ||
295 | return -1; | ||
296 | |||
297 | switch (c) { | ||
298 | case '\n': | ||
299 | if (O_ONLRET(tty)) | ||
300 | tty->column = 0; | ||
301 | if (O_ONLCR(tty)) { | ||
302 | if (space < 2) | ||
303 | return -1; | ||
304 | tty->canon_column = tty->column = 0; | ||
305 | tty->ops->write(tty, "\r\n", 2); | ||
306 | return 2; | ||
307 | } | ||
308 | tty->canon_column = tty->column; | ||
309 | break; | ||
310 | case '\r': | ||
311 | if (O_ONOCR(tty) && tty->column == 0) | ||
312 | return 0; | ||
313 | if (O_OCRNL(tty)) { | ||
314 | c = '\n'; | ||
315 | if (O_ONLRET(tty)) | ||
316 | tty->canon_column = tty->column = 0; | ||
317 | break; | ||
318 | } | ||
319 | tty->canon_column = tty->column = 0; | ||
320 | break; | ||
321 | case '\t': | ||
322 | spaces = 8 - (tty->column & 7); | ||
323 | if (O_TABDLY(tty) == XTABS) { | ||
324 | if (space < spaces) | ||
325 | return -1; | ||
326 | tty->column += spaces; | ||
327 | tty->ops->write(tty, " ", spaces); | ||
328 | return spaces; | ||
329 | } | ||
330 | tty->column += spaces; | ||
331 | break; | ||
332 | case '\b': | ||
333 | if (tty->column > 0) | ||
334 | tty->column--; | ||
335 | break; | ||
336 | default: | ||
337 | if (!iscntrl(c)) { | ||
338 | if (O_OLCUC(tty)) | ||
339 | c = toupper(c); | ||
340 | if (!is_continuation(c, tty)) | ||
341 | tty->column++; | ||
342 | } | ||
343 | break; | ||
344 | } | ||
345 | |||
346 | tty_put_char(tty, c); | ||
347 | return 1; | ||
348 | } | ||
349 | |||
350 | /** | ||
351 | * process_output - output post processor | ||
352 | * @c: character (or partial unicode symbol) | ||
353 | * @tty: terminal device | ||
354 | * | ||
355 | * Output one character with OPOST processing. | ||
356 | * Returns -1 when the output device is full and the character | ||
357 | * must be retried. | ||
358 | * | ||
359 | * Locking: output_lock to protect column state and space left | ||
360 | * (also, this is called from n_tty_write under the | ||
361 | * tty layer write lock) | ||
362 | */ | ||
363 | |||
364 | static int process_output(unsigned char c, struct tty_struct *tty) | ||
365 | { | ||
366 | int space, retval; | ||
367 | |||
368 | mutex_lock(&tty->output_lock); | ||
369 | |||
370 | space = tty_write_room(tty); | ||
371 | retval = do_output_char(c, tty, space); | ||
372 | |||
373 | mutex_unlock(&tty->output_lock); | ||
374 | if (retval < 0) | ||
375 | return -1; | ||
376 | else | ||
377 | return 0; | ||
378 | } | ||
379 | |||
380 | /** | ||
381 | * process_output_block - block post processor | ||
382 | * @tty: terminal device | ||
383 | * @buf: character buffer | ||
384 | * @nr: number of bytes to output | ||
385 | * | ||
386 | * Output a block of characters with OPOST processing. | ||
387 | * Returns the number of characters output. | ||
388 | * | ||
389 | * This path is used to speed up block console writes, among other | ||
390 | * things when processing blocks of output data. It handles only | ||
391 | * the simple cases normally found and helps to generate blocks of | ||
392 | * symbols for the console driver and thus improve performance. | ||
393 | * | ||
394 | * Locking: output_lock to protect column state and space left | ||
395 | * (also, this is called from n_tty_write under the | ||
396 | * tty layer write lock) | ||
397 | */ | ||
398 | |||
399 | static ssize_t process_output_block(struct tty_struct *tty, | ||
400 | const unsigned char *buf, unsigned int nr) | ||
401 | { | ||
402 | int space; | ||
403 | int i; | ||
404 | const unsigned char *cp; | ||
405 | |||
406 | mutex_lock(&tty->output_lock); | ||
407 | |||
408 | space = tty_write_room(tty); | ||
409 | if (!space) { | ||
410 | mutex_unlock(&tty->output_lock); | ||
411 | return 0; | ||
412 | } | ||
413 | if (nr > space) | ||
414 | nr = space; | ||
415 | |||
416 | for (i = 0, cp = buf; i < nr; i++, cp++) { | ||
417 | unsigned char c = *cp; | ||
418 | |||
419 | switch (c) { | ||
420 | case '\n': | ||
421 | if (O_ONLRET(tty)) | ||
422 | tty->column = 0; | ||
423 | if (O_ONLCR(tty)) | ||
424 | goto break_out; | ||
425 | tty->canon_column = tty->column; | ||
426 | break; | ||
427 | case '\r': | ||
428 | if (O_ONOCR(tty) && tty->column == 0) | ||
429 | goto break_out; | ||
430 | if (O_OCRNL(tty)) | ||
431 | goto break_out; | ||
432 | tty->canon_column = tty->column = 0; | ||
433 | break; | ||
434 | case '\t': | ||
435 | goto break_out; | ||
436 | case '\b': | ||
437 | if (tty->column > 0) | ||
438 | tty->column--; | ||
439 | break; | ||
440 | default: | ||
441 | if (!iscntrl(c)) { | ||
442 | if (O_OLCUC(tty)) | ||
443 | goto break_out; | ||
444 | if (!is_continuation(c, tty)) | ||
445 | tty->column++; | ||
446 | } | ||
447 | break; | ||
448 | } | ||
449 | } | ||
450 | break_out: | ||
451 | i = tty->ops->write(tty, buf, i); | ||
452 | |||
453 | mutex_unlock(&tty->output_lock); | ||
454 | return i; | ||
455 | } | ||
456 | |||
457 | /** | ||
458 | * process_echoes - write pending echo characters | ||
459 | * @tty: terminal device | ||
460 | * | ||
461 | * Write previously buffered echo (and other ldisc-generated) | ||
462 | * characters to the tty. | ||
463 | * | ||
464 | * Characters generated by the ldisc (including echoes) need to | ||
465 | * be buffered because the driver's write buffer can fill during | ||
466 | * heavy program output. Echoing straight to the driver will | ||
467 | * often fail under these conditions, causing lost characters and | ||
468 | * resulting mismatches of ldisc state information. | ||
469 | * | ||
470 | * Since the ldisc state must represent the characters actually sent | ||
471 | * to the driver at the time of the write, operations like certain | ||
472 | * changes in column state are also saved in the buffer and executed | ||
473 | * here. | ||
474 | * | ||
475 | * A circular fifo buffer is used so that the most recent characters | ||
476 | * are prioritized. Also, when control characters are echoed with a | ||
477 | * prefixed "^", the pair is treated atomically and thus not separated. | ||
478 | * | ||
479 | * Locking: output_lock to protect column state and space left, | ||
480 | * echo_lock to protect the echo buffer | ||
481 | */ | ||
482 | |||
483 | static void process_echoes(struct tty_struct *tty) | ||
484 | { | ||
485 | int space, nr; | ||
486 | unsigned char c; | ||
487 | unsigned char *cp, *buf_end; | ||
488 | |||
489 | if (!tty->echo_cnt) | ||
490 | return; | ||
491 | |||
492 | mutex_lock(&tty->output_lock); | ||
493 | mutex_lock(&tty->echo_lock); | ||
494 | |||
495 | space = tty_write_room(tty); | ||
496 | |||
497 | buf_end = tty->echo_buf + N_TTY_BUF_SIZE; | ||
498 | cp = tty->echo_buf + tty->echo_pos; | ||
499 | nr = tty->echo_cnt; | ||
500 | while (nr > 0) { | ||
501 | c = *cp; | ||
502 | if (c == ECHO_OP_START) { | ||
503 | unsigned char op; | ||
504 | unsigned char *opp; | ||
505 | int no_space_left = 0; | ||
506 | |||
507 | /* | ||
508 | * If the buffer byte is the start of a multi-byte | ||
509 | * operation, get the next byte, which is either the | ||
510 | * op code or a control character value. | ||
511 | */ | ||
512 | opp = cp + 1; | ||
513 | if (opp == buf_end) | ||
514 | opp -= N_TTY_BUF_SIZE; | ||
515 | op = *opp; | ||
516 | |||
517 | switch (op) { | ||
518 | unsigned int num_chars, num_bs; | ||
519 | |||
520 | case ECHO_OP_ERASE_TAB: | ||
521 | if (++opp == buf_end) | ||
522 | opp -= N_TTY_BUF_SIZE; | ||
523 | num_chars = *opp; | ||
524 | |||
525 | /* | ||
526 | * Determine how many columns to go back | ||
527 | * in order to erase the tab. | ||
528 | * This depends on the number of columns | ||
529 | * used by other characters within the tab | ||
530 | * area. If this (modulo 8) count is from | ||
531 | * the start of input rather than from a | ||
532 | * previous tab, we offset by canon column. | ||
533 | * Otherwise, tab spacing is normal. | ||
534 | */ | ||
535 | if (!(num_chars & 0x80)) | ||
536 | num_chars += tty->canon_column; | ||
537 | num_bs = 8 - (num_chars & 7); | ||
538 | |||
539 | if (num_bs > space) { | ||
540 | no_space_left = 1; | ||
541 | break; | ||
542 | } | ||
543 | space -= num_bs; | ||
544 | while (num_bs--) { | ||
545 | tty_put_char(tty, '\b'); | ||
546 | if (tty->column > 0) | ||
547 | tty->column--; | ||
548 | } | ||
549 | cp += 3; | ||
550 | nr -= 3; | ||
551 | break; | ||
552 | |||
553 | case ECHO_OP_SET_CANON_COL: | ||
554 | tty->canon_column = tty->column; | ||
555 | cp += 2; | ||
556 | nr -= 2; | ||
557 | break; | ||
558 | |||
559 | case ECHO_OP_MOVE_BACK_COL: | ||
560 | if (tty->column > 0) | ||
561 | tty->column--; | ||
562 | cp += 2; | ||
563 | nr -= 2; | ||
564 | break; | ||
565 | |||
566 | case ECHO_OP_START: | ||
567 | /* This is an escaped echo op start code */ | ||
568 | if (!space) { | ||
569 | no_space_left = 1; | ||
570 | break; | ||
571 | } | ||
572 | tty_put_char(tty, ECHO_OP_START); | ||
573 | tty->column++; | ||
574 | space--; | ||
575 | cp += 2; | ||
576 | nr -= 2; | ||
577 | break; | ||
578 | |||
579 | default: | ||
580 | /* | ||
581 | * If the op is not a special byte code, | ||
582 | * it is a ctrl char tagged to be echoed | ||
583 | * as "^X" (where X is the letter | ||
584 | * representing the control char). | ||
585 | * Note that we must ensure there is | ||
586 | * enough space for the whole ctrl pair. | ||
587 | * | ||
588 | */ | ||
589 | if (space < 2) { | ||
590 | no_space_left = 1; | ||
591 | break; | ||
592 | } | ||
593 | tty_put_char(tty, '^'); | ||
594 | tty_put_char(tty, op ^ 0100); | ||
595 | tty->column += 2; | ||
596 | space -= 2; | ||
597 | cp += 2; | ||
598 | nr -= 2; | ||
599 | } | ||
600 | |||
601 | if (no_space_left) | ||
602 | break; | ||
603 | } else { | ||
604 | if (O_OPOST(tty) && | ||
605 | !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) { | ||
606 | int retval = do_output_char(c, tty, space); | ||
607 | if (retval < 0) | ||
608 | break; | ||
609 | space -= retval; | ||
610 | } else { | ||
611 | if (!space) | ||
612 | break; | ||
613 | tty_put_char(tty, c); | ||
614 | space -= 1; | ||
615 | } | ||
616 | cp += 1; | ||
617 | nr -= 1; | ||
618 | } | ||
619 | |||
620 | /* When end of circular buffer reached, wrap around */ | ||
621 | if (cp >= buf_end) | ||
622 | cp -= N_TTY_BUF_SIZE; | ||
623 | } | ||
624 | |||
625 | if (nr == 0) { | ||
626 | tty->echo_pos = 0; | ||
627 | tty->echo_cnt = 0; | ||
628 | tty->echo_overrun = 0; | ||
629 | } else { | ||
630 | int num_processed = tty->echo_cnt - nr; | ||
631 | tty->echo_pos += num_processed; | ||
632 | tty->echo_pos &= N_TTY_BUF_SIZE - 1; | ||
633 | tty->echo_cnt = nr; | ||
634 | if (num_processed > 0) | ||
635 | tty->echo_overrun = 0; | ||
636 | } | ||
637 | |||
638 | mutex_unlock(&tty->echo_lock); | ||
639 | mutex_unlock(&tty->output_lock); | ||
640 | |||
641 | if (tty->ops->flush_chars) | ||
642 | tty->ops->flush_chars(tty); | ||
643 | } | ||
644 | |||
645 | /** | ||
646 | * add_echo_byte - add a byte to the echo buffer | ||
647 | * @c: unicode byte to echo | ||
648 | * @tty: terminal device | ||
649 | * | ||
650 | * Add a character or operation byte to the echo buffer. | ||
651 | * | ||
652 | * Should be called under the echo lock to protect the echo buffer. | ||
653 | */ | ||
654 | |||
655 | static void add_echo_byte(unsigned char c, struct tty_struct *tty) | ||
656 | { | ||
657 | int new_byte_pos; | ||
658 | |||
659 | if (tty->echo_cnt == N_TTY_BUF_SIZE) { | ||
660 | /* Circular buffer is already at capacity */ | ||
661 | new_byte_pos = tty->echo_pos; | ||
662 | |||
663 | /* | ||
664 | * Since the buffer start position needs to be advanced, | ||
665 | * be sure to step by a whole operation byte group. | ||
666 | */ | ||
667 | if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) { | ||
668 | if (tty->echo_buf[(tty->echo_pos + 1) & | ||
669 | (N_TTY_BUF_SIZE - 1)] == | ||
670 | ECHO_OP_ERASE_TAB) { | ||
671 | tty->echo_pos += 3; | ||
672 | tty->echo_cnt -= 2; | ||
673 | } else { | ||
674 | tty->echo_pos += 2; | ||
675 | tty->echo_cnt -= 1; | ||
676 | } | ||
677 | } else { | ||
678 | tty->echo_pos++; | ||
679 | } | ||
680 | tty->echo_pos &= N_TTY_BUF_SIZE - 1; | ||
681 | |||
682 | tty->echo_overrun = 1; | ||
683 | } else { | ||
684 | new_byte_pos = tty->echo_pos + tty->echo_cnt; | ||
685 | new_byte_pos &= N_TTY_BUF_SIZE - 1; | ||
686 | tty->echo_cnt++; | ||
687 | } | ||
688 | |||
689 | tty->echo_buf[new_byte_pos] = c; | ||
690 | } | ||
691 | |||
692 | /** | ||
693 | * echo_move_back_col - add operation to move back a column | ||
694 | * @tty: terminal device | ||
695 | * | ||
696 | * Add an operation to the echo buffer to move back one column. | ||
697 | * | ||
698 | * Locking: echo_lock to protect the echo buffer | ||
699 | */ | ||
700 | |||
701 | static void echo_move_back_col(struct tty_struct *tty) | ||
702 | { | ||
703 | mutex_lock(&tty->echo_lock); | ||
704 | |||
705 | add_echo_byte(ECHO_OP_START, tty); | ||
706 | add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty); | ||
707 | |||
708 | mutex_unlock(&tty->echo_lock); | ||
709 | } | ||
710 | |||
711 | /** | ||
712 | * echo_set_canon_col - add operation to set the canon column | ||
713 | * @tty: terminal device | ||
714 | * | ||
715 | * Add an operation to the echo buffer to set the canon column | ||
716 | * to the current column. | ||
717 | * | ||
718 | * Locking: echo_lock to protect the echo buffer | ||
719 | */ | ||
720 | |||
721 | static void echo_set_canon_col(struct tty_struct *tty) | ||
722 | { | ||
723 | mutex_lock(&tty->echo_lock); | ||
724 | |||
725 | add_echo_byte(ECHO_OP_START, tty); | ||
726 | add_echo_byte(ECHO_OP_SET_CANON_COL, tty); | ||
727 | |||
728 | mutex_unlock(&tty->echo_lock); | ||
729 | } | ||
730 | |||
731 | /** | ||
732 | * echo_erase_tab - add operation to erase a tab | ||
733 | * @num_chars: number of character columns already used | ||
734 | * @after_tab: true if num_chars starts after a previous tab | ||
735 | * @tty: terminal device | ||
736 | * | ||
737 | * Add an operation to the echo buffer to erase a tab. | ||
738 | * | ||
739 | * Called by the eraser function, which knows how many character | ||
740 | * columns have been used since either a previous tab or the start | ||
741 | * of input. This information will be used later, along with | ||
742 | * canon column (if applicable), to go back the correct number | ||
743 | * of columns. | ||
744 | * | ||
745 | * Locking: echo_lock to protect the echo buffer | ||
746 | */ | ||
747 | |||
748 | static void echo_erase_tab(unsigned int num_chars, int after_tab, | ||
749 | struct tty_struct *tty) | ||
750 | { | ||
751 | mutex_lock(&tty->echo_lock); | ||
752 | |||
753 | add_echo_byte(ECHO_OP_START, tty); | ||
754 | add_echo_byte(ECHO_OP_ERASE_TAB, tty); | ||
755 | |||
756 | /* We only need to know this modulo 8 (tab spacing) */ | ||
757 | num_chars &= 7; | ||
758 | |||
759 | /* Set the high bit as a flag if num_chars is after a previous tab */ | ||
760 | if (after_tab) | ||
761 | num_chars |= 0x80; | ||
762 | |||
763 | add_echo_byte(num_chars, tty); | ||
764 | |||
765 | mutex_unlock(&tty->echo_lock); | ||
766 | } | ||
767 | |||
768 | /** | ||
769 | * echo_char_raw - echo a character raw | ||
770 | * @c: unicode byte to echo | ||
771 | * @tty: terminal device | ||
772 | * | ||
773 | * Echo user input back onto the screen. This must be called only when | ||
774 | * L_ECHO(tty) is true. Called from the driver receive_buf path. | ||
775 | * | ||
776 | * This variant does not treat control characters specially. | ||
777 | * | ||
778 | * Locking: echo_lock to protect the echo buffer | ||
779 | */ | ||
780 | |||
781 | static void echo_char_raw(unsigned char c, struct tty_struct *tty) | ||
782 | { | ||
783 | mutex_lock(&tty->echo_lock); | ||
784 | |||
785 | if (c == ECHO_OP_START) { | ||
786 | add_echo_byte(ECHO_OP_START, tty); | ||
787 | add_echo_byte(ECHO_OP_START, tty); | ||
788 | } else { | ||
789 | add_echo_byte(c, tty); | ||
790 | } | ||
791 | |||
792 | mutex_unlock(&tty->echo_lock); | ||
793 | } | ||
794 | |||
795 | /** | ||
796 | * echo_char - echo a character | ||
797 | * @c: unicode byte to echo | ||
798 | * @tty: terminal device | ||
799 | * | ||
800 | * Echo user input back onto the screen. This must be called only when | ||
801 | * L_ECHO(tty) is true. Called from the driver receive_buf path. | ||
802 | * | ||
803 | * This variant tags control characters to be echoed as "^X" | ||
804 | * (where X is the letter representing the control char). | ||
805 | * | ||
806 | * Locking: echo_lock to protect the echo buffer | ||
807 | */ | ||
808 | |||
809 | static void echo_char(unsigned char c, struct tty_struct *tty) | ||
810 | { | ||
811 | mutex_lock(&tty->echo_lock); | ||
812 | |||
813 | if (c == ECHO_OP_START) { | ||
814 | add_echo_byte(ECHO_OP_START, tty); | ||
815 | add_echo_byte(ECHO_OP_START, tty); | ||
816 | } else { | ||
817 | if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') | ||
818 | add_echo_byte(ECHO_OP_START, tty); | ||
819 | add_echo_byte(c, tty); | ||
820 | } | ||
821 | |||
822 | mutex_unlock(&tty->echo_lock); | ||
823 | } | ||
824 | |||
825 | /** | ||
826 | * finish_erasing - complete erase | ||
827 | * @tty: tty doing the erase | ||
828 | */ | ||
829 | |||
830 | static inline void finish_erasing(struct tty_struct *tty) | ||
831 | { | ||
832 | if (tty->erasing) { | ||
833 | echo_char_raw('/', tty); | ||
834 | tty->erasing = 0; | ||
835 | } | ||
836 | } | ||
837 | |||
838 | /** | ||
839 | * eraser - handle erase function | ||
840 | * @c: character input | ||
841 | * @tty: terminal device | ||
842 | * | ||
843 | * Perform erase and necessary output when an erase character is | ||
844 | * present in the stream from the driver layer. Handles the complexities | ||
845 | * of UTF-8 multibyte symbols. | ||
846 | * | ||
847 | * Locking: read_lock for tty buffers | ||
848 | */ | ||
849 | |||
850 | static void eraser(unsigned char c, struct tty_struct *tty) | ||
851 | { | ||
852 | enum { ERASE, WERASE, KILL } kill_type; | ||
853 | int head, seen_alnums, cnt; | ||
854 | unsigned long flags; | ||
855 | |||
856 | /* FIXME: locking needed ? */ | ||
857 | if (tty->read_head == tty->canon_head) { | ||
858 | /* process_output('\a', tty); */ /* what do you think? */ | ||
859 | return; | ||
860 | } | ||
861 | if (c == ERASE_CHAR(tty)) | ||
862 | kill_type = ERASE; | ||
863 | else if (c == WERASE_CHAR(tty)) | ||
864 | kill_type = WERASE; | ||
865 | else { | ||
866 | if (!L_ECHO(tty)) { | ||
867 | spin_lock_irqsave(&tty->read_lock, flags); | ||
868 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & | ||
869 | (N_TTY_BUF_SIZE - 1)); | ||
870 | tty->read_head = tty->canon_head; | ||
871 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
872 | return; | ||
873 | } | ||
874 | if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { | ||
875 | spin_lock_irqsave(&tty->read_lock, flags); | ||
876 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & | ||
877 | (N_TTY_BUF_SIZE - 1)); | ||
878 | tty->read_head = tty->canon_head; | ||
879 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
880 | finish_erasing(tty); | ||
881 | echo_char(KILL_CHAR(tty), tty); | ||
882 | /* Add a newline if ECHOK is on and ECHOKE is off. */ | ||
883 | if (L_ECHOK(tty)) | ||
884 | echo_char_raw('\n', tty); | ||
885 | return; | ||
886 | } | ||
887 | kill_type = KILL; | ||
888 | } | ||
889 | |||
890 | seen_alnums = 0; | ||
891 | /* FIXME: Locking ?? */ | ||
892 | while (tty->read_head != tty->canon_head) { | ||
893 | head = tty->read_head; | ||
894 | |||
895 | /* erase a single possibly multibyte character */ | ||
896 | do { | ||
897 | head = (head - 1) & (N_TTY_BUF_SIZE-1); | ||
898 | c = tty->read_buf[head]; | ||
899 | } while (is_continuation(c, tty) && head != tty->canon_head); | ||
900 | |||
901 | /* do not partially erase */ | ||
902 | if (is_continuation(c, tty)) | ||
903 | break; | ||
904 | |||
905 | if (kill_type == WERASE) { | ||
906 | /* Equivalent to BSD's ALTWERASE. */ | ||
907 | if (isalnum(c) || c == '_') | ||
908 | seen_alnums++; | ||
909 | else if (seen_alnums) | ||
910 | break; | ||
911 | } | ||
912 | cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1); | ||
913 | spin_lock_irqsave(&tty->read_lock, flags); | ||
914 | tty->read_head = head; | ||
915 | tty->read_cnt -= cnt; | ||
916 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
917 | if (L_ECHO(tty)) { | ||
918 | if (L_ECHOPRT(tty)) { | ||
919 | if (!tty->erasing) { | ||
920 | echo_char_raw('\\', tty); | ||
921 | tty->erasing = 1; | ||
922 | } | ||
923 | /* if cnt > 1, output a multi-byte character */ | ||
924 | echo_char(c, tty); | ||
925 | while (--cnt > 0) { | ||
926 | head = (head+1) & (N_TTY_BUF_SIZE-1); | ||
927 | echo_char_raw(tty->read_buf[head], tty); | ||
928 | echo_move_back_col(tty); | ||
929 | } | ||
930 | } else if (kill_type == ERASE && !L_ECHOE(tty)) { | ||
931 | echo_char(ERASE_CHAR(tty), tty); | ||
932 | } else if (c == '\t') { | ||
933 | unsigned int num_chars = 0; | ||
934 | int after_tab = 0; | ||
935 | unsigned long tail = tty->read_head; | ||
936 | |||
937 | /* | ||
938 | * Count the columns used for characters | ||
939 | * since the start of input or after a | ||
940 | * previous tab. | ||
941 | * This info is used to go back the correct | ||
942 | * number of columns. | ||
943 | */ | ||
944 | while (tail != tty->canon_head) { | ||
945 | tail = (tail-1) & (N_TTY_BUF_SIZE-1); | ||
946 | c = tty->read_buf[tail]; | ||
947 | if (c == '\t') { | ||
948 | after_tab = 1; | ||
949 | break; | ||
950 | } else if (iscntrl(c)) { | ||
951 | if (L_ECHOCTL(tty)) | ||
952 | num_chars += 2; | ||
953 | } else if (!is_continuation(c, tty)) { | ||
954 | num_chars++; | ||
955 | } | ||
956 | } | ||
957 | echo_erase_tab(num_chars, after_tab, tty); | ||
958 | } else { | ||
959 | if (iscntrl(c) && L_ECHOCTL(tty)) { | ||
960 | echo_char_raw('\b', tty); | ||
961 | echo_char_raw(' ', tty); | ||
962 | echo_char_raw('\b', tty); | ||
963 | } | ||
964 | if (!iscntrl(c) || L_ECHOCTL(tty)) { | ||
965 | echo_char_raw('\b', tty); | ||
966 | echo_char_raw(' ', tty); | ||
967 | echo_char_raw('\b', tty); | ||
968 | } | ||
969 | } | ||
970 | } | ||
971 | if (kill_type == ERASE) | ||
972 | break; | ||
973 | } | ||
974 | if (tty->read_head == tty->canon_head && L_ECHO(tty)) | ||
975 | finish_erasing(tty); | ||
976 | } | ||
977 | |||
978 | /** | ||
979 | * isig - handle the ISIG optio | ||
980 | * @sig: signal | ||
981 | * @tty: terminal | ||
982 | * @flush: force flush | ||
983 | * | ||
984 | * Called when a signal is being sent due to terminal input. This | ||
985 | * may caus terminal flushing to take place according to the termios | ||
986 | * settings and character used. Called from the driver receive_buf | ||
987 | * path so serialized. | ||
988 | * | ||
989 | * Locking: ctrl_lock, read_lock (both via flush buffer) | ||
990 | */ | ||
991 | |||
992 | static inline void isig(int sig, struct tty_struct *tty, int flush) | ||
993 | { | ||
994 | if (tty->pgrp) | ||
995 | kill_pgrp(tty->pgrp, sig, 1); | ||
996 | if (flush || !L_NOFLSH(tty)) { | ||
997 | n_tty_flush_buffer(tty); | ||
998 | tty_driver_flush_buffer(tty); | ||
999 | } | ||
1000 | } | ||
1001 | |||
1002 | /** | ||
1003 | * n_tty_receive_break - handle break | ||
1004 | * @tty: terminal | ||
1005 | * | ||
1006 | * An RS232 break event has been hit in the incoming bitstream. This | ||
1007 | * can cause a variety of events depending upon the termios settings. | ||
1008 | * | ||
1009 | * Called from the receive_buf path so single threaded. | ||
1010 | */ | ||
1011 | |||
1012 | static inline void n_tty_receive_break(struct tty_struct *tty) | ||
1013 | { | ||
1014 | if (I_IGNBRK(tty)) | ||
1015 | return; | ||
1016 | if (I_BRKINT(tty)) { | ||
1017 | isig(SIGINT, tty, 1); | ||
1018 | return; | ||
1019 | } | ||
1020 | if (I_PARMRK(tty)) { | ||
1021 | put_tty_queue('\377', tty); | ||
1022 | put_tty_queue('\0', tty); | ||
1023 | } | ||
1024 | put_tty_queue('\0', tty); | ||
1025 | wake_up_interruptible(&tty->read_wait); | ||
1026 | } | ||
1027 | |||
1028 | /** | ||
1029 | * n_tty_receive_overrun - handle overrun reporting | ||
1030 | * @tty: terminal | ||
1031 | * | ||
1032 | * Data arrived faster than we could process it. While the tty | ||
1033 | * driver has flagged this the bits that were missed are gone | ||
1034 | * forever. | ||
1035 | * | ||
1036 | * Called from the receive_buf path so single threaded. Does not | ||
1037 | * need locking as num_overrun and overrun_time are function | ||
1038 | * private. | ||
1039 | */ | ||
1040 | |||
1041 | static inline void n_tty_receive_overrun(struct tty_struct *tty) | ||
1042 | { | ||
1043 | char buf[64]; | ||
1044 | |||
1045 | tty->num_overrun++; | ||
1046 | if (time_before(tty->overrun_time, jiffies - HZ) || | ||
1047 | time_after(tty->overrun_time, jiffies)) { | ||
1048 | printk(KERN_WARNING "%s: %d input overrun(s)\n", | ||
1049 | tty_name(tty, buf), | ||
1050 | tty->num_overrun); | ||
1051 | tty->overrun_time = jiffies; | ||
1052 | tty->num_overrun = 0; | ||
1053 | } | ||
1054 | } | ||
1055 | |||
1056 | /** | ||
1057 | * n_tty_receive_parity_error - error notifier | ||
1058 | * @tty: terminal device | ||
1059 | * @c: character | ||
1060 | * | ||
1061 | * Process a parity error and queue the right data to indicate | ||
1062 | * the error case if necessary. Locking as per n_tty_receive_buf. | ||
1063 | */ | ||
1064 | static inline void n_tty_receive_parity_error(struct tty_struct *tty, | ||
1065 | unsigned char c) | ||
1066 | { | ||
1067 | if (I_IGNPAR(tty)) | ||
1068 | return; | ||
1069 | if (I_PARMRK(tty)) { | ||
1070 | put_tty_queue('\377', tty); | ||
1071 | put_tty_queue('\0', tty); | ||
1072 | put_tty_queue(c, tty); | ||
1073 | } else if (I_INPCK(tty)) | ||
1074 | put_tty_queue('\0', tty); | ||
1075 | else | ||
1076 | put_tty_queue(c, tty); | ||
1077 | wake_up_interruptible(&tty->read_wait); | ||
1078 | } | ||
1079 | |||
1080 | /** | ||
1081 | * n_tty_receive_char - perform processing | ||
1082 | * @tty: terminal device | ||
1083 | * @c: character | ||
1084 | * | ||
1085 | * Process an individual character of input received from the driver. | ||
1086 | * This is serialized with respect to itself by the rules for the | ||
1087 | * driver above. | ||
1088 | */ | ||
1089 | |||
1090 | static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c) | ||
1091 | { | ||
1092 | unsigned long flags; | ||
1093 | int parmrk; | ||
1094 | |||
1095 | if (tty->raw) { | ||
1096 | put_tty_queue(c, tty); | ||
1097 | return; | ||
1098 | } | ||
1099 | |||
1100 | if (I_ISTRIP(tty)) | ||
1101 | c &= 0x7f; | ||
1102 | if (I_IUCLC(tty) && L_IEXTEN(tty)) | ||
1103 | c = tolower(c); | ||
1104 | |||
1105 | if (L_EXTPROC(tty)) { | ||
1106 | put_tty_queue(c, tty); | ||
1107 | return; | ||
1108 | } | ||
1109 | |||
1110 | if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && | ||
1111 | I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) && | ||
1112 | c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) { | ||
1113 | start_tty(tty); | ||
1114 | process_echoes(tty); | ||
1115 | } | ||
1116 | |||
1117 | if (tty->closing) { | ||
1118 | if (I_IXON(tty)) { | ||
1119 | if (c == START_CHAR(tty)) { | ||
1120 | start_tty(tty); | ||
1121 | process_echoes(tty); | ||
1122 | } else if (c == STOP_CHAR(tty)) | ||
1123 | stop_tty(tty); | ||
1124 | } | ||
1125 | return; | ||
1126 | } | ||
1127 | |||
1128 | /* | ||
1129 | * If the previous character was LNEXT, or we know that this | ||
1130 | * character is not one of the characters that we'll have to | ||
1131 | * handle specially, do shortcut processing to speed things | ||
1132 | * up. | ||
1133 | */ | ||
1134 | if (!test_bit(c, tty->process_char_map) || tty->lnext) { | ||
1135 | tty->lnext = 0; | ||
1136 | parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; | ||
1137 | if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) { | ||
1138 | /* beep if no space */ | ||
1139 | if (L_ECHO(tty)) | ||
1140 | process_output('\a', tty); | ||
1141 | return; | ||
1142 | } | ||
1143 | if (L_ECHO(tty)) { | ||
1144 | finish_erasing(tty); | ||
1145 | /* Record the column of first canon char. */ | ||
1146 | if (tty->canon_head == tty->read_head) | ||
1147 | echo_set_canon_col(tty); | ||
1148 | echo_char(c, tty); | ||
1149 | process_echoes(tty); | ||
1150 | } | ||
1151 | if (parmrk) | ||
1152 | put_tty_queue(c, tty); | ||
1153 | put_tty_queue(c, tty); | ||
1154 | return; | ||
1155 | } | ||
1156 | |||
1157 | if (I_IXON(tty)) { | ||
1158 | if (c == START_CHAR(tty)) { | ||
1159 | start_tty(tty); | ||
1160 | process_echoes(tty); | ||
1161 | return; | ||
1162 | } | ||
1163 | if (c == STOP_CHAR(tty)) { | ||
1164 | stop_tty(tty); | ||
1165 | return; | ||
1166 | } | ||
1167 | } | ||
1168 | |||
1169 | if (L_ISIG(tty)) { | ||
1170 | int signal; | ||
1171 | signal = SIGINT; | ||
1172 | if (c == INTR_CHAR(tty)) | ||
1173 | goto send_signal; | ||
1174 | signal = SIGQUIT; | ||
1175 | if (c == QUIT_CHAR(tty)) | ||
1176 | goto send_signal; | ||
1177 | signal = SIGTSTP; | ||
1178 | if (c == SUSP_CHAR(tty)) { | ||
1179 | send_signal: | ||
1180 | /* | ||
1181 | * Note that we do not use isig() here because we want | ||
1182 | * the order to be: | ||
1183 | * 1) flush, 2) echo, 3) signal | ||
1184 | */ | ||
1185 | if (!L_NOFLSH(tty)) { | ||
1186 | n_tty_flush_buffer(tty); | ||
1187 | tty_driver_flush_buffer(tty); | ||
1188 | } | ||
1189 | if (I_IXON(tty)) | ||
1190 | start_tty(tty); | ||
1191 | if (L_ECHO(tty)) { | ||
1192 | echo_char(c, tty); | ||
1193 | process_echoes(tty); | ||
1194 | } | ||
1195 | if (tty->pgrp) | ||
1196 | kill_pgrp(tty->pgrp, signal, 1); | ||
1197 | return; | ||
1198 | } | ||
1199 | } | ||
1200 | |||
1201 | if (c == '\r') { | ||
1202 | if (I_IGNCR(tty)) | ||
1203 | return; | ||
1204 | if (I_ICRNL(tty)) | ||
1205 | c = '\n'; | ||
1206 | } else if (c == '\n' && I_INLCR(tty)) | ||
1207 | c = '\r'; | ||
1208 | |||
1209 | if (tty->icanon) { | ||
1210 | if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || | ||
1211 | (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { | ||
1212 | eraser(c, tty); | ||
1213 | process_echoes(tty); | ||
1214 | return; | ||
1215 | } | ||
1216 | if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { | ||
1217 | tty->lnext = 1; | ||
1218 | if (L_ECHO(tty)) { | ||
1219 | finish_erasing(tty); | ||
1220 | if (L_ECHOCTL(tty)) { | ||
1221 | echo_char_raw('^', tty); | ||
1222 | echo_char_raw('\b', tty); | ||
1223 | process_echoes(tty); | ||
1224 | } | ||
1225 | } | ||
1226 | return; | ||
1227 | } | ||
1228 | if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && | ||
1229 | L_IEXTEN(tty)) { | ||
1230 | unsigned long tail = tty->canon_head; | ||
1231 | |||
1232 | finish_erasing(tty); | ||
1233 | echo_char(c, tty); | ||
1234 | echo_char_raw('\n', tty); | ||
1235 | while (tail != tty->read_head) { | ||
1236 | echo_char(tty->read_buf[tail], tty); | ||
1237 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); | ||
1238 | } | ||
1239 | process_echoes(tty); | ||
1240 | return; | ||
1241 | } | ||
1242 | if (c == '\n') { | ||
1243 | if (tty->read_cnt >= N_TTY_BUF_SIZE) { | ||
1244 | if (L_ECHO(tty)) | ||
1245 | process_output('\a', tty); | ||
1246 | return; | ||
1247 | } | ||
1248 | if (L_ECHO(tty) || L_ECHONL(tty)) { | ||
1249 | echo_char_raw('\n', tty); | ||
1250 | process_echoes(tty); | ||
1251 | } | ||
1252 | goto handle_newline; | ||
1253 | } | ||
1254 | if (c == EOF_CHAR(tty)) { | ||
1255 | if (tty->read_cnt >= N_TTY_BUF_SIZE) | ||
1256 | return; | ||
1257 | if (tty->canon_head != tty->read_head) | ||
1258 | set_bit(TTY_PUSH, &tty->flags); | ||
1259 | c = __DISABLED_CHAR; | ||
1260 | goto handle_newline; | ||
1261 | } | ||
1262 | if ((c == EOL_CHAR(tty)) || | ||
1263 | (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { | ||
1264 | parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) | ||
1265 | ? 1 : 0; | ||
1266 | if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) { | ||
1267 | if (L_ECHO(tty)) | ||
1268 | process_output('\a', tty); | ||
1269 | return; | ||
1270 | } | ||
1271 | /* | ||
1272 | * XXX are EOL_CHAR and EOL2_CHAR echoed?!? | ||
1273 | */ | ||
1274 | if (L_ECHO(tty)) { | ||
1275 | /* Record the column of first canon char. */ | ||
1276 | if (tty->canon_head == tty->read_head) | ||
1277 | echo_set_canon_col(tty); | ||
1278 | echo_char(c, tty); | ||
1279 | process_echoes(tty); | ||
1280 | } | ||
1281 | /* | ||
1282 | * XXX does PARMRK doubling happen for | ||
1283 | * EOL_CHAR and EOL2_CHAR? | ||
1284 | */ | ||
1285 | if (parmrk) | ||
1286 | put_tty_queue(c, tty); | ||
1287 | |||
1288 | handle_newline: | ||
1289 | spin_lock_irqsave(&tty->read_lock, flags); | ||
1290 | set_bit(tty->read_head, tty->read_flags); | ||
1291 | put_tty_queue_nolock(c, tty); | ||
1292 | tty->canon_head = tty->read_head; | ||
1293 | tty->canon_data++; | ||
1294 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
1295 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); | ||
1296 | if (waitqueue_active(&tty->read_wait)) | ||
1297 | wake_up_interruptible(&tty->read_wait); | ||
1298 | return; | ||
1299 | } | ||
1300 | } | ||
1301 | |||
1302 | parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; | ||
1303 | if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) { | ||
1304 | /* beep if no space */ | ||
1305 | if (L_ECHO(tty)) | ||
1306 | process_output('\a', tty); | ||
1307 | return; | ||
1308 | } | ||
1309 | if (L_ECHO(tty)) { | ||
1310 | finish_erasing(tty); | ||
1311 | if (c == '\n') | ||
1312 | echo_char_raw('\n', tty); | ||
1313 | else { | ||
1314 | /* Record the column of first canon char. */ | ||
1315 | if (tty->canon_head == tty->read_head) | ||
1316 | echo_set_canon_col(tty); | ||
1317 | echo_char(c, tty); | ||
1318 | } | ||
1319 | process_echoes(tty); | ||
1320 | } | ||
1321 | |||
1322 | if (parmrk) | ||
1323 | put_tty_queue(c, tty); | ||
1324 | |||
1325 | put_tty_queue(c, tty); | ||
1326 | } | ||
1327 | |||
1328 | |||
1329 | /** | ||
1330 | * n_tty_write_wakeup - asynchronous I/O notifier | ||
1331 | * @tty: tty device | ||
1332 | * | ||
1333 | * Required for the ptys, serial driver etc. since processes | ||
1334 | * that attach themselves to the master and rely on ASYNC | ||
1335 | * IO must be woken up | ||
1336 | */ | ||
1337 | |||
1338 | static void n_tty_write_wakeup(struct tty_struct *tty) | ||
1339 | { | ||
1340 | if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) | ||
1341 | kill_fasync(&tty->fasync, SIGIO, POLL_OUT); | ||
1342 | } | ||
1343 | |||
1344 | /** | ||
1345 | * n_tty_receive_buf - data receive | ||
1346 | * @tty: terminal device | ||
1347 | * @cp: buffer | ||
1348 | * @fp: flag buffer | ||
1349 | * @count: characters | ||
1350 | * | ||
1351 | * Called by the terminal driver when a block of characters has | ||
1352 | * been received. This function must be called from soft contexts | ||
1353 | * not from interrupt context. The driver is responsible for making | ||
1354 | * calls one at a time and in order (or using flush_to_ldisc) | ||
1355 | */ | ||
1356 | |||
1357 | static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, | ||
1358 | char *fp, int count) | ||
1359 | { | ||
1360 | const unsigned char *p; | ||
1361 | char *f, flags = TTY_NORMAL; | ||
1362 | int i; | ||
1363 | char buf[64]; | ||
1364 | unsigned long cpuflags; | ||
1365 | |||
1366 | if (!tty->read_buf) | ||
1367 | return; | ||
1368 | |||
1369 | if (tty->real_raw) { | ||
1370 | spin_lock_irqsave(&tty->read_lock, cpuflags); | ||
1371 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, | ||
1372 | N_TTY_BUF_SIZE - tty->read_head); | ||
1373 | i = min(count, i); | ||
1374 | memcpy(tty->read_buf + tty->read_head, cp, i); | ||
1375 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); | ||
1376 | tty->read_cnt += i; | ||
1377 | cp += i; | ||
1378 | count -= i; | ||
1379 | |||
1380 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, | ||
1381 | N_TTY_BUF_SIZE - tty->read_head); | ||
1382 | i = min(count, i); | ||
1383 | memcpy(tty->read_buf + tty->read_head, cp, i); | ||
1384 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); | ||
1385 | tty->read_cnt += i; | ||
1386 | spin_unlock_irqrestore(&tty->read_lock, cpuflags); | ||
1387 | } else { | ||
1388 | for (i = count, p = cp, f = fp; i; i--, p++) { | ||
1389 | if (f) | ||
1390 | flags = *f++; | ||
1391 | switch (flags) { | ||
1392 | case TTY_NORMAL: | ||
1393 | n_tty_receive_char(tty, *p); | ||
1394 | break; | ||
1395 | case TTY_BREAK: | ||
1396 | n_tty_receive_break(tty); | ||
1397 | break; | ||
1398 | case TTY_PARITY: | ||
1399 | case TTY_FRAME: | ||
1400 | n_tty_receive_parity_error(tty, *p); | ||
1401 | break; | ||
1402 | case TTY_OVERRUN: | ||
1403 | n_tty_receive_overrun(tty); | ||
1404 | break; | ||
1405 | default: | ||
1406 | printk(KERN_ERR "%s: unknown flag %d\n", | ||
1407 | tty_name(tty, buf), flags); | ||
1408 | break; | ||
1409 | } | ||
1410 | } | ||
1411 | if (tty->ops->flush_chars) | ||
1412 | tty->ops->flush_chars(tty); | ||
1413 | } | ||
1414 | |||
1415 | n_tty_set_room(tty); | ||
1416 | |||
1417 | if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) || | ||
1418 | L_EXTPROC(tty)) { | ||
1419 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); | ||
1420 | if (waitqueue_active(&tty->read_wait)) | ||
1421 | wake_up_interruptible(&tty->read_wait); | ||
1422 | } | ||
1423 | |||
1424 | /* | ||
1425 | * Check the remaining room for the input canonicalization | ||
1426 | * mode. We don't want to throttle the driver if we're in | ||
1427 | * canonical mode and don't have a newline yet! | ||
1428 | */ | ||
1429 | if (tty->receive_room < TTY_THRESHOLD_THROTTLE) | ||
1430 | tty_throttle(tty); | ||
1431 | } | ||
1432 | |||
1433 | int is_ignored(int sig) | ||
1434 | { | ||
1435 | return (sigismember(¤t->blocked, sig) || | ||
1436 | current->sighand->action[sig-1].sa.sa_handler == SIG_IGN); | ||
1437 | } | ||
1438 | |||
1439 | /** | ||
1440 | * n_tty_set_termios - termios data changed | ||
1441 | * @tty: terminal | ||
1442 | * @old: previous data | ||
1443 | * | ||
1444 | * Called by the tty layer when the user changes termios flags so | ||
1445 | * that the line discipline can plan ahead. This function cannot sleep | ||
1446 | * and is protected from re-entry by the tty layer. The user is | ||
1447 | * guaranteed that this function will not be re-entered or in progress | ||
1448 | * when the ldisc is closed. | ||
1449 | * | ||
1450 | * Locking: Caller holds tty->termios_mutex | ||
1451 | */ | ||
1452 | |||
1453 | static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old) | ||
1454 | { | ||
1455 | int canon_change = 1; | ||
1456 | BUG_ON(!tty); | ||
1457 | |||
1458 | if (old) | ||
1459 | canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON; | ||
1460 | if (canon_change) { | ||
1461 | memset(&tty->read_flags, 0, sizeof tty->read_flags); | ||
1462 | tty->canon_head = tty->read_tail; | ||
1463 | tty->canon_data = 0; | ||
1464 | tty->erasing = 0; | ||
1465 | } | ||
1466 | |||
1467 | if (canon_change && !L_ICANON(tty) && tty->read_cnt) | ||
1468 | wake_up_interruptible(&tty->read_wait); | ||
1469 | |||
1470 | tty->icanon = (L_ICANON(tty) != 0); | ||
1471 | if (test_bit(TTY_HW_COOK_IN, &tty->flags)) { | ||
1472 | tty->raw = 1; | ||
1473 | tty->real_raw = 1; | ||
1474 | n_tty_set_room(tty); | ||
1475 | return; | ||
1476 | } | ||
1477 | if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || | ||
1478 | I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || | ||
1479 | I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || | ||
1480 | I_PARMRK(tty)) { | ||
1481 | memset(tty->process_char_map, 0, 256/8); | ||
1482 | |||
1483 | if (I_IGNCR(tty) || I_ICRNL(tty)) | ||
1484 | set_bit('\r', tty->process_char_map); | ||
1485 | if (I_INLCR(tty)) | ||
1486 | set_bit('\n', tty->process_char_map); | ||
1487 | |||
1488 | if (L_ICANON(tty)) { | ||
1489 | set_bit(ERASE_CHAR(tty), tty->process_char_map); | ||
1490 | set_bit(KILL_CHAR(tty), tty->process_char_map); | ||
1491 | set_bit(EOF_CHAR(tty), tty->process_char_map); | ||
1492 | set_bit('\n', tty->process_char_map); | ||
1493 | set_bit(EOL_CHAR(tty), tty->process_char_map); | ||
1494 | if (L_IEXTEN(tty)) { | ||
1495 | set_bit(WERASE_CHAR(tty), | ||
1496 | tty->process_char_map); | ||
1497 | set_bit(LNEXT_CHAR(tty), | ||
1498 | tty->process_char_map); | ||
1499 | set_bit(EOL2_CHAR(tty), | ||
1500 | tty->process_char_map); | ||
1501 | if (L_ECHO(tty)) | ||
1502 | set_bit(REPRINT_CHAR(tty), | ||
1503 | tty->process_char_map); | ||
1504 | } | ||
1505 | } | ||
1506 | if (I_IXON(tty)) { | ||
1507 | set_bit(START_CHAR(tty), tty->process_char_map); | ||
1508 | set_bit(STOP_CHAR(tty), tty->process_char_map); | ||
1509 | } | ||
1510 | if (L_ISIG(tty)) { | ||
1511 | set_bit(INTR_CHAR(tty), tty->process_char_map); | ||
1512 | set_bit(QUIT_CHAR(tty), tty->process_char_map); | ||
1513 | set_bit(SUSP_CHAR(tty), tty->process_char_map); | ||
1514 | } | ||
1515 | clear_bit(__DISABLED_CHAR, tty->process_char_map); | ||
1516 | tty->raw = 0; | ||
1517 | tty->real_raw = 0; | ||
1518 | } else { | ||
1519 | tty->raw = 1; | ||
1520 | if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && | ||
1521 | (I_IGNPAR(tty) || !I_INPCK(tty)) && | ||
1522 | (tty->driver->flags & TTY_DRIVER_REAL_RAW)) | ||
1523 | tty->real_raw = 1; | ||
1524 | else | ||
1525 | tty->real_raw = 0; | ||
1526 | } | ||
1527 | n_tty_set_room(tty); | ||
1528 | /* The termios change make the tty ready for I/O */ | ||
1529 | wake_up_interruptible(&tty->write_wait); | ||
1530 | wake_up_interruptible(&tty->read_wait); | ||
1531 | } | ||
1532 | |||
1533 | /** | ||
1534 | * n_tty_close - close the ldisc for this tty | ||
1535 | * @tty: device | ||
1536 | * | ||
1537 | * Called from the terminal layer when this line discipline is | ||
1538 | * being shut down, either because of a close or becsuse of a | ||
1539 | * discipline change. The function will not be called while other | ||
1540 | * ldisc methods are in progress. | ||
1541 | */ | ||
1542 | |||
1543 | static void n_tty_close(struct tty_struct *tty) | ||
1544 | { | ||
1545 | n_tty_flush_buffer(tty); | ||
1546 | if (tty->read_buf) { | ||
1547 | kfree(tty->read_buf); | ||
1548 | tty->read_buf = NULL; | ||
1549 | } | ||
1550 | if (tty->echo_buf) { | ||
1551 | kfree(tty->echo_buf); | ||
1552 | tty->echo_buf = NULL; | ||
1553 | } | ||
1554 | } | ||
1555 | |||
1556 | /** | ||
1557 | * n_tty_open - open an ldisc | ||
1558 | * @tty: terminal to open | ||
1559 | * | ||
1560 | * Called when this line discipline is being attached to the | ||
1561 | * terminal device. Can sleep. Called serialized so that no | ||
1562 | * other events will occur in parallel. No further open will occur | ||
1563 | * until a close. | ||
1564 | */ | ||
1565 | |||
1566 | static int n_tty_open(struct tty_struct *tty) | ||
1567 | { | ||
1568 | if (!tty) | ||
1569 | return -EINVAL; | ||
1570 | |||
1571 | /* These are ugly. Currently a malloc failure here can panic */ | ||
1572 | if (!tty->read_buf) { | ||
1573 | tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL); | ||
1574 | if (!tty->read_buf) | ||
1575 | return -ENOMEM; | ||
1576 | } | ||
1577 | if (!tty->echo_buf) { | ||
1578 | tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL); | ||
1579 | |||
1580 | if (!tty->echo_buf) | ||
1581 | return -ENOMEM; | ||
1582 | } | ||
1583 | reset_buffer_flags(tty); | ||
1584 | tty->column = 0; | ||
1585 | n_tty_set_termios(tty, NULL); | ||
1586 | tty->minimum_to_wake = 1; | ||
1587 | tty->closing = 0; | ||
1588 | return 0; | ||
1589 | } | ||
1590 | |||
1591 | static inline int input_available_p(struct tty_struct *tty, int amt) | ||
1592 | { | ||
1593 | tty_flush_to_ldisc(tty); | ||
1594 | if (tty->icanon && !L_EXTPROC(tty)) { | ||
1595 | if (tty->canon_data) | ||
1596 | return 1; | ||
1597 | } else if (tty->read_cnt >= (amt ? amt : 1)) | ||
1598 | return 1; | ||
1599 | |||
1600 | return 0; | ||
1601 | } | ||
1602 | |||
1603 | /** | ||
1604 | * copy_from_read_buf - copy read data directly | ||
1605 | * @tty: terminal device | ||
1606 | * @b: user data | ||
1607 | * @nr: size of data | ||
1608 | * | ||
1609 | * Helper function to speed up n_tty_read. It is only called when | ||
1610 | * ICANON is off; it copies characters straight from the tty queue to | ||
1611 | * user space directly. It can be profitably called twice; once to | ||
1612 | * drain the space from the tail pointer to the (physical) end of the | ||
1613 | * buffer, and once to drain the space from the (physical) beginning of | ||
1614 | * the buffer to head pointer. | ||
1615 | * | ||
1616 | * Called under the tty->atomic_read_lock sem | ||
1617 | * | ||
1618 | */ | ||
1619 | |||
1620 | static int copy_from_read_buf(struct tty_struct *tty, | ||
1621 | unsigned char __user **b, | ||
1622 | size_t *nr) | ||
1623 | |||
1624 | { | ||
1625 | int retval; | ||
1626 | size_t n; | ||
1627 | unsigned long flags; | ||
1628 | |||
1629 | retval = 0; | ||
1630 | spin_lock_irqsave(&tty->read_lock, flags); | ||
1631 | n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail); | ||
1632 | n = min(*nr, n); | ||
1633 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
1634 | if (n) { | ||
1635 | retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n); | ||
1636 | n -= retval; | ||
1637 | tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n); | ||
1638 | spin_lock_irqsave(&tty->read_lock, flags); | ||
1639 | tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1); | ||
1640 | tty->read_cnt -= n; | ||
1641 | /* Turn single EOF into zero-length read */ | ||
1642 | if (L_EXTPROC(tty) && tty->icanon && n == 1) { | ||
1643 | if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty)) | ||
1644 | n--; | ||
1645 | } | ||
1646 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
1647 | *b += n; | ||
1648 | *nr -= n; | ||
1649 | } | ||
1650 | return retval; | ||
1651 | } | ||
1652 | |||
1653 | extern ssize_t redirected_tty_write(struct file *, const char __user *, | ||
1654 | size_t, loff_t *); | ||
1655 | |||
1656 | /** | ||
1657 | * job_control - check job control | ||
1658 | * @tty: tty | ||
1659 | * @file: file handle | ||
1660 | * | ||
1661 | * Perform job control management checks on this file/tty descriptor | ||
1662 | * and if appropriate send any needed signals and return a negative | ||
1663 | * error code if action should be taken. | ||
1664 | * | ||
1665 | * FIXME: | ||
1666 | * Locking: None - redirected write test is safe, testing | ||
1667 | * current->signal should possibly lock current->sighand | ||
1668 | * pgrp locking ? | ||
1669 | */ | ||
1670 | |||
1671 | static int job_control(struct tty_struct *tty, struct file *file) | ||
1672 | { | ||
1673 | /* Job control check -- must be done at start and after | ||
1674 | every sleep (POSIX.1 7.1.1.4). */ | ||
1675 | /* NOTE: not yet done after every sleep pending a thorough | ||
1676 | check of the logic of this change. -- jlc */ | ||
1677 | /* don't stop on /dev/console */ | ||
1678 | if (file->f_op->write != redirected_tty_write && | ||
1679 | current->signal->tty == tty) { | ||
1680 | if (!tty->pgrp) | ||
1681 | printk(KERN_ERR "n_tty_read: no tty->pgrp!\n"); | ||
1682 | else if (task_pgrp(current) != tty->pgrp) { | ||
1683 | if (is_ignored(SIGTTIN) || | ||
1684 | is_current_pgrp_orphaned()) | ||
1685 | return -EIO; | ||
1686 | kill_pgrp(task_pgrp(current), SIGTTIN, 1); | ||
1687 | set_thread_flag(TIF_SIGPENDING); | ||
1688 | return -ERESTARTSYS; | ||
1689 | } | ||
1690 | } | ||
1691 | return 0; | ||
1692 | } | ||
1693 | |||
1694 | |||
1695 | /** | ||
1696 | * n_tty_read - read function for tty | ||
1697 | * @tty: tty device | ||
1698 | * @file: file object | ||
1699 | * @buf: userspace buffer pointer | ||
1700 | * @nr: size of I/O | ||
1701 | * | ||
1702 | * Perform reads for the line discipline. We are guaranteed that the | ||
1703 | * line discipline will not be closed under us but we may get multiple | ||
1704 | * parallel readers and must handle this ourselves. We may also get | ||
1705 | * a hangup. Always called in user context, may sleep. | ||
1706 | * | ||
1707 | * This code must be sure never to sleep through a hangup. | ||
1708 | */ | ||
1709 | |||
1710 | static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, | ||
1711 | unsigned char __user *buf, size_t nr) | ||
1712 | { | ||
1713 | unsigned char __user *b = buf; | ||
1714 | DECLARE_WAITQUEUE(wait, current); | ||
1715 | int c; | ||
1716 | int minimum, time; | ||
1717 | ssize_t retval = 0; | ||
1718 | ssize_t size; | ||
1719 | long timeout; | ||
1720 | unsigned long flags; | ||
1721 | int packet; | ||
1722 | |||
1723 | do_it_again: | ||
1724 | |||
1725 | BUG_ON(!tty->read_buf); | ||
1726 | |||
1727 | c = job_control(tty, file); | ||
1728 | if (c < 0) | ||
1729 | return c; | ||
1730 | |||
1731 | minimum = time = 0; | ||
1732 | timeout = MAX_SCHEDULE_TIMEOUT; | ||
1733 | if (!tty->icanon) { | ||
1734 | time = (HZ / 10) * TIME_CHAR(tty); | ||
1735 | minimum = MIN_CHAR(tty); | ||
1736 | if (minimum) { | ||
1737 | if (time) | ||
1738 | tty->minimum_to_wake = 1; | ||
1739 | else if (!waitqueue_active(&tty->read_wait) || | ||
1740 | (tty->minimum_to_wake > minimum)) | ||
1741 | tty->minimum_to_wake = minimum; | ||
1742 | } else { | ||
1743 | timeout = 0; | ||
1744 | if (time) { | ||
1745 | timeout = time; | ||
1746 | time = 0; | ||
1747 | } | ||
1748 | tty->minimum_to_wake = minimum = 1; | ||
1749 | } | ||
1750 | } | ||
1751 | |||
1752 | /* | ||
1753 | * Internal serialization of reads. | ||
1754 | */ | ||
1755 | if (file->f_flags & O_NONBLOCK) { | ||
1756 | if (!mutex_trylock(&tty->atomic_read_lock)) | ||
1757 | return -EAGAIN; | ||
1758 | } else { | ||
1759 | if (mutex_lock_interruptible(&tty->atomic_read_lock)) | ||
1760 | return -ERESTARTSYS; | ||
1761 | } | ||
1762 | packet = tty->packet; | ||
1763 | |||
1764 | add_wait_queue(&tty->read_wait, &wait); | ||
1765 | while (nr) { | ||
1766 | /* First test for status change. */ | ||
1767 | if (packet && tty->link->ctrl_status) { | ||
1768 | unsigned char cs; | ||
1769 | if (b != buf) | ||
1770 | break; | ||
1771 | spin_lock_irqsave(&tty->link->ctrl_lock, flags); | ||
1772 | cs = tty->link->ctrl_status; | ||
1773 | tty->link->ctrl_status = 0; | ||
1774 | spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); | ||
1775 | if (tty_put_user(tty, cs, b++)) { | ||
1776 | retval = -EFAULT; | ||
1777 | b--; | ||
1778 | break; | ||
1779 | } | ||
1780 | nr--; | ||
1781 | break; | ||
1782 | } | ||
1783 | /* This statement must be first before checking for input | ||
1784 | so that any interrupt will set the state back to | ||
1785 | TASK_RUNNING. */ | ||
1786 | set_current_state(TASK_INTERRUPTIBLE); | ||
1787 | |||
1788 | if (((minimum - (b - buf)) < tty->minimum_to_wake) && | ||
1789 | ((minimum - (b - buf)) >= 1)) | ||
1790 | tty->minimum_to_wake = (minimum - (b - buf)); | ||
1791 | |||
1792 | if (!input_available_p(tty, 0)) { | ||
1793 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { | ||
1794 | retval = -EIO; | ||
1795 | break; | ||
1796 | } | ||
1797 | if (tty_hung_up_p(file)) | ||
1798 | break; | ||
1799 | if (!timeout) | ||
1800 | break; | ||
1801 | if (file->f_flags & O_NONBLOCK) { | ||
1802 | retval = -EAGAIN; | ||
1803 | break; | ||
1804 | } | ||
1805 | if (signal_pending(current)) { | ||
1806 | retval = -ERESTARTSYS; | ||
1807 | break; | ||
1808 | } | ||
1809 | /* FIXME: does n_tty_set_room need locking ? */ | ||
1810 | n_tty_set_room(tty); | ||
1811 | timeout = schedule_timeout(timeout); | ||
1812 | continue; | ||
1813 | } | ||
1814 | __set_current_state(TASK_RUNNING); | ||
1815 | |||
1816 | /* Deal with packet mode. */ | ||
1817 | if (packet && b == buf) { | ||
1818 | if (tty_put_user(tty, TIOCPKT_DATA, b++)) { | ||
1819 | retval = -EFAULT; | ||
1820 | b--; | ||
1821 | break; | ||
1822 | } | ||
1823 | nr--; | ||
1824 | } | ||
1825 | |||
1826 | if (tty->icanon && !L_EXTPROC(tty)) { | ||
1827 | /* N.B. avoid overrun if nr == 0 */ | ||
1828 | while (nr && tty->read_cnt) { | ||
1829 | int eol; | ||
1830 | |||
1831 | eol = test_and_clear_bit(tty->read_tail, | ||
1832 | tty->read_flags); | ||
1833 | c = tty->read_buf[tty->read_tail]; | ||
1834 | spin_lock_irqsave(&tty->read_lock, flags); | ||
1835 | tty->read_tail = ((tty->read_tail+1) & | ||
1836 | (N_TTY_BUF_SIZE-1)); | ||
1837 | tty->read_cnt--; | ||
1838 | if (eol) { | ||
1839 | /* this test should be redundant: | ||
1840 | * we shouldn't be reading data if | ||
1841 | * canon_data is 0 | ||
1842 | */ | ||
1843 | if (--tty->canon_data < 0) | ||
1844 | tty->canon_data = 0; | ||
1845 | } | ||
1846 | spin_unlock_irqrestore(&tty->read_lock, flags); | ||
1847 | |||
1848 | if (!eol || (c != __DISABLED_CHAR)) { | ||
1849 | if (tty_put_user(tty, c, b++)) { | ||
1850 | retval = -EFAULT; | ||
1851 | b--; | ||
1852 | break; | ||
1853 | } | ||
1854 | nr--; | ||
1855 | } | ||
1856 | if (eol) { | ||
1857 | tty_audit_push(tty); | ||
1858 | break; | ||
1859 | } | ||
1860 | } | ||
1861 | if (retval) | ||
1862 | break; | ||
1863 | } else { | ||
1864 | int uncopied; | ||
1865 | /* The copy function takes the read lock and handles | ||
1866 | locking internally for this case */ | ||
1867 | uncopied = copy_from_read_buf(tty, &b, &nr); | ||
1868 | uncopied += copy_from_read_buf(tty, &b, &nr); | ||
1869 | if (uncopied) { | ||
1870 | retval = -EFAULT; | ||
1871 | break; | ||
1872 | } | ||
1873 | } | ||
1874 | |||
1875 | /* If there is enough space in the read buffer now, let the | ||
1876 | * low-level driver know. We use n_tty_chars_in_buffer() to | ||
1877 | * check the buffer, as it now knows about canonical mode. | ||
1878 | * Otherwise, if the driver is throttled and the line is | ||
1879 | * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, | ||
1880 | * we won't get any more characters. | ||
1881 | */ | ||
1882 | if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) { | ||
1883 | n_tty_set_room(tty); | ||
1884 | check_unthrottle(tty); | ||
1885 | } | ||
1886 | |||
1887 | if (b - buf >= minimum) | ||
1888 | break; | ||
1889 | if (time) | ||
1890 | timeout = time; | ||
1891 | } | ||
1892 | mutex_unlock(&tty->atomic_read_lock); | ||
1893 | remove_wait_queue(&tty->read_wait, &wait); | ||
1894 | |||
1895 | if (!waitqueue_active(&tty->read_wait)) | ||
1896 | tty->minimum_to_wake = minimum; | ||
1897 | |||
1898 | __set_current_state(TASK_RUNNING); | ||
1899 | size = b - buf; | ||
1900 | if (size) { | ||
1901 | retval = size; | ||
1902 | if (nr) | ||
1903 | clear_bit(TTY_PUSH, &tty->flags); | ||
1904 | } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) | ||
1905 | goto do_it_again; | ||
1906 | |||
1907 | n_tty_set_room(tty); | ||
1908 | return retval; | ||
1909 | } | ||
1910 | |||
1911 | /** | ||
1912 | * n_tty_write - write function for tty | ||
1913 | * @tty: tty device | ||
1914 | * @file: file object | ||
1915 | * @buf: userspace buffer pointer | ||
1916 | * @nr: size of I/O | ||
1917 | * | ||
1918 | * Write function of the terminal device. This is serialized with | ||
1919 | * respect to other write callers but not to termios changes, reads | ||
1920 | * and other such events. Since the receive code will echo characters, | ||
1921 | * thus calling driver write methods, the output_lock is used in | ||
1922 | * the output processing functions called here as well as in the | ||
1923 | * echo processing function to protect the column state and space | ||
1924 | * left in the buffer. | ||
1925 | * | ||
1926 | * This code must be sure never to sleep through a hangup. | ||
1927 | * | ||
1928 | * Locking: output_lock to protect column state and space left | ||
1929 | * (note that the process_output*() functions take this | ||
1930 | * lock themselves) | ||
1931 | */ | ||
1932 | |||
1933 | static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, | ||
1934 | const unsigned char *buf, size_t nr) | ||
1935 | { | ||
1936 | const unsigned char *b = buf; | ||
1937 | DECLARE_WAITQUEUE(wait, current); | ||
1938 | int c; | ||
1939 | ssize_t retval = 0; | ||
1940 | |||
1941 | /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ | ||
1942 | if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { | ||
1943 | retval = tty_check_change(tty); | ||
1944 | if (retval) | ||
1945 | return retval; | ||
1946 | } | ||
1947 | |||
1948 | /* Write out any echoed characters that are still pending */ | ||
1949 | process_echoes(tty); | ||
1950 | |||
1951 | add_wait_queue(&tty->write_wait, &wait); | ||
1952 | while (1) { | ||
1953 | set_current_state(TASK_INTERRUPTIBLE); | ||
1954 | if (signal_pending(current)) { | ||
1955 | retval = -ERESTARTSYS; | ||
1956 | break; | ||
1957 | } | ||
1958 | if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { | ||
1959 | retval = -EIO; | ||
1960 | break; | ||
1961 | } | ||
1962 | if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) { | ||
1963 | while (nr > 0) { | ||
1964 | ssize_t num = process_output_block(tty, b, nr); | ||
1965 | if (num < 0) { | ||
1966 | if (num == -EAGAIN) | ||
1967 | break; | ||
1968 | retval = num; | ||
1969 | goto break_out; | ||
1970 | } | ||
1971 | b += num; | ||
1972 | nr -= num; | ||
1973 | if (nr == 0) | ||
1974 | break; | ||
1975 | c = *b; | ||
1976 | if (process_output(c, tty) < 0) | ||
1977 | break; | ||
1978 | b++; nr--; | ||
1979 | } | ||
1980 | if (tty->ops->flush_chars) | ||
1981 | tty->ops->flush_chars(tty); | ||
1982 | } else { | ||
1983 | while (nr > 0) { | ||
1984 | c = tty->ops->write(tty, b, nr); | ||
1985 | if (c < 0) { | ||
1986 | retval = c; | ||
1987 | goto break_out; | ||
1988 | } | ||
1989 | if (!c) | ||
1990 | break; | ||
1991 | b += c; | ||
1992 | nr -= c; | ||
1993 | } | ||
1994 | } | ||
1995 | if (!nr) | ||
1996 | break; | ||
1997 | if (file->f_flags & O_NONBLOCK) { | ||
1998 | retval = -EAGAIN; | ||
1999 | break; | ||
2000 | } | ||
2001 | schedule(); | ||
2002 | } | ||
2003 | break_out: | ||
2004 | __set_current_state(TASK_RUNNING); | ||
2005 | remove_wait_queue(&tty->write_wait, &wait); | ||
2006 | if (b - buf != nr && tty->fasync) | ||
2007 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | ||
2008 | return (b - buf) ? b - buf : retval; | ||
2009 | } | ||
2010 | |||
2011 | /** | ||
2012 | * n_tty_poll - poll method for N_TTY | ||
2013 | * @tty: terminal device | ||
2014 | * @file: file accessing it | ||
2015 | * @wait: poll table | ||
2016 | * | ||
2017 | * Called when the line discipline is asked to poll() for data or | ||
2018 | * for special events. This code is not serialized with respect to | ||
2019 | * other events save open/close. | ||
2020 | * | ||
2021 | * This code must be sure never to sleep through a hangup. | ||
2022 | * Called without the kernel lock held - fine | ||
2023 | */ | ||
2024 | |||
2025 | static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file, | ||
2026 | poll_table *wait) | ||
2027 | { | ||
2028 | unsigned int mask = 0; | ||
2029 | |||
2030 | poll_wait(file, &tty->read_wait, wait); | ||
2031 | poll_wait(file, &tty->write_wait, wait); | ||
2032 | if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty))) | ||
2033 | mask |= POLLIN | POLLRDNORM; | ||
2034 | if (tty->packet && tty->link->ctrl_status) | ||
2035 | mask |= POLLPRI | POLLIN | POLLRDNORM; | ||
2036 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | ||
2037 | mask |= POLLHUP; | ||
2038 | if (tty_hung_up_p(file)) | ||
2039 | mask |= POLLHUP; | ||
2040 | if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) { | ||
2041 | if (MIN_CHAR(tty) && !TIME_CHAR(tty)) | ||
2042 | tty->minimum_to_wake = MIN_CHAR(tty); | ||
2043 | else | ||
2044 | tty->minimum_to_wake = 1; | ||
2045 | } | ||
2046 | if (tty->ops->write && !tty_is_writelocked(tty) && | ||
2047 | tty_chars_in_buffer(tty) < WAKEUP_CHARS && | ||
2048 | tty_write_room(tty) > 0) | ||
2049 | mask |= POLLOUT | POLLWRNORM; | ||
2050 | return mask; | ||
2051 | } | ||
2052 | |||
2053 | static unsigned long inq_canon(struct tty_struct *tty) | ||
2054 | { | ||
2055 | int nr, head, tail; | ||
2056 | |||
2057 | if (!tty->canon_data) | ||
2058 | return 0; | ||
2059 | head = tty->canon_head; | ||
2060 | tail = tty->read_tail; | ||
2061 | nr = (head - tail) & (N_TTY_BUF_SIZE-1); | ||
2062 | /* Skip EOF-chars.. */ | ||
2063 | while (head != tail) { | ||
2064 | if (test_bit(tail, tty->read_flags) && | ||
2065 | tty->read_buf[tail] == __DISABLED_CHAR) | ||
2066 | nr--; | ||
2067 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); | ||
2068 | } | ||
2069 | return nr; | ||
2070 | } | ||
2071 | |||
2072 | static int n_tty_ioctl(struct tty_struct *tty, struct file *file, | ||
2073 | unsigned int cmd, unsigned long arg) | ||
2074 | { | ||
2075 | int retval; | ||
2076 | |||
2077 | switch (cmd) { | ||
2078 | case TIOCOUTQ: | ||
2079 | return put_user(tty_chars_in_buffer(tty), (int __user *) arg); | ||
2080 | case TIOCINQ: | ||
2081 | /* FIXME: Locking */ | ||
2082 | retval = tty->read_cnt; | ||
2083 | if (L_ICANON(tty)) | ||
2084 | retval = inq_canon(tty); | ||
2085 | return put_user(retval, (unsigned int __user *) arg); | ||
2086 | default: | ||
2087 | return n_tty_ioctl_helper(tty, file, cmd, arg); | ||
2088 | } | ||
2089 | } | ||
2090 | |||
2091 | struct tty_ldisc_ops tty_ldisc_N_TTY = { | ||
2092 | .magic = TTY_LDISC_MAGIC, | ||
2093 | .name = "n_tty", | ||
2094 | .open = n_tty_open, | ||
2095 | .close = n_tty_close, | ||
2096 | .flush_buffer = n_tty_flush_buffer, | ||
2097 | .chars_in_buffer = n_tty_chars_in_buffer, | ||
2098 | .read = n_tty_read, | ||
2099 | .write = n_tty_write, | ||
2100 | .ioctl = n_tty_ioctl, | ||
2101 | .set_termios = n_tty_set_termios, | ||
2102 | .poll = n_tty_poll, | ||
2103 | .receive_buf = n_tty_receive_buf, | ||
2104 | .write_wakeup = n_tty_write_wakeup | ||
2105 | }; | ||
2106 | |||
2107 | /** | ||
2108 | * n_tty_inherit_ops - inherit N_TTY methods | ||
2109 | * @ops: struct tty_ldisc_ops where to save N_TTY methods | ||
2110 | * | ||
2111 | * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY | ||
2112 | * methods. | ||
2113 | */ | ||
2114 | |||
2115 | void n_tty_inherit_ops(struct tty_ldisc_ops *ops) | ||
2116 | { | ||
2117 | *ops = tty_ldisc_N_TTY; | ||
2118 | ops->owner = NULL; | ||
2119 | ops->refcount = ops->flags = 0; | ||
2120 | } | ||
2121 | EXPORT_SYMBOL_GPL(n_tty_inherit_ops); | ||
diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c new file mode 100644 index 000000000000..923a48585501 --- /dev/null +++ b/drivers/tty/pty.c | |||
@@ -0,0 +1,777 @@ | |||
1 | /* | ||
2 | * linux/drivers/char/pty.c | ||
3 | * | ||
4 | * Copyright (C) 1991, 1992 Linus Torvalds | ||
5 | * | ||
6 | * Added support for a Unix98-style ptmx device. | ||
7 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 | ||
8 | * | ||
9 | * When reading this code see also fs/devpts. In particular note that the | ||
10 | * driver_data field is used by the devpts side as a binding to the devpts | ||
11 | * inode. | ||
12 | */ | ||
13 | |||
14 | #include <linux/module.h> | ||
15 | |||
16 | #include <linux/errno.h> | ||
17 | #include <linux/interrupt.h> | ||
18 | #include <linux/tty.h> | ||
19 | #include <linux/tty_flip.h> | ||
20 | #include <linux/fcntl.h> | ||
21 | #include <linux/sched.h> | ||
22 | #include <linux/string.h> | ||
23 | #include <linux/major.h> | ||
24 | #include <linux/mm.h> | ||
25 | #include <linux/init.h> | ||
26 | #include <linux/smp_lock.h> | ||
27 | #include <linux/sysctl.h> | ||
28 | #include <linux/device.h> | ||
29 | #include <linux/uaccess.h> | ||
30 | #include <linux/bitops.h> | ||
31 | #include <linux/devpts_fs.h> | ||
32 | #include <linux/slab.h> | ||
33 | |||
34 | #include <asm/system.h> | ||
35 | |||
36 | #ifdef CONFIG_UNIX98_PTYS | ||
37 | static struct tty_driver *ptm_driver; | ||
38 | static struct tty_driver *pts_driver; | ||
39 | #endif | ||
40 | |||
41 | static void pty_close(struct tty_struct *tty, struct file *filp) | ||
42 | { | ||
43 | BUG_ON(!tty); | ||
44 | if (tty->driver->subtype == PTY_TYPE_MASTER) | ||
45 | WARN_ON(tty->count > 1); | ||
46 | else { | ||
47 | if (tty->count > 2) | ||
48 | return; | ||
49 | } | ||
50 | wake_up_interruptible(&tty->read_wait); | ||
51 | wake_up_interruptible(&tty->write_wait); | ||
52 | tty->packet = 0; | ||
53 | if (!tty->link) | ||
54 | return; | ||
55 | tty->link->packet = 0; | ||
56 | set_bit(TTY_OTHER_CLOSED, &tty->link->flags); | ||
57 | wake_up_interruptible(&tty->link->read_wait); | ||
58 | wake_up_interruptible(&tty->link->write_wait); | ||
59 | if (tty->driver->subtype == PTY_TYPE_MASTER) { | ||
60 | set_bit(TTY_OTHER_CLOSED, &tty->flags); | ||
61 | #ifdef CONFIG_UNIX98_PTYS | ||
62 | if (tty->driver == ptm_driver) | ||
63 | devpts_pty_kill(tty->link); | ||
64 | #endif | ||
65 | tty_unlock(); | ||
66 | tty_vhangup(tty->link); | ||
67 | tty_lock(); | ||
68 | } | ||
69 | } | ||
70 | |||
71 | /* | ||
72 | * The unthrottle routine is called by the line discipline to signal | ||
73 | * that it can receive more characters. For PTY's, the TTY_THROTTLED | ||
74 | * flag is always set, to force the line discipline to always call the | ||
75 | * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE | ||
76 | * characters in the queue. This is necessary since each time this | ||
77 | * happens, we need to wake up any sleeping processes that could be | ||
78 | * (1) trying to send data to the pty, or (2) waiting in wait_until_sent() | ||
79 | * for the pty buffer to be drained. | ||
80 | */ | ||
81 | static void pty_unthrottle(struct tty_struct *tty) | ||
82 | { | ||
83 | tty_wakeup(tty->link); | ||
84 | set_bit(TTY_THROTTLED, &tty->flags); | ||
85 | } | ||
86 | |||
87 | /** | ||
88 | * pty_space - report space left for writing | ||
89 | * @to: tty we are writing into | ||
90 | * | ||
91 | * The tty buffers allow 64K but we sneak a peak and clip at 8K this | ||
92 | * allows a lot of overspill room for echo and other fun messes to | ||
93 | * be handled properly | ||
94 | */ | ||
95 | |||
96 | static int pty_space(struct tty_struct *to) | ||
97 | { | ||
98 | int n = 8192 - to->buf.memory_used; | ||
99 | if (n < 0) | ||
100 | return 0; | ||
101 | return n; | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * pty_write - write to a pty | ||
106 | * @tty: the tty we write from | ||
107 | * @buf: kernel buffer of data | ||
108 | * @count: bytes to write | ||
109 | * | ||
110 | * Our "hardware" write method. Data is coming from the ldisc which | ||
111 | * may be in a non sleeping state. We simply throw this at the other | ||
112 | * end of the link as if we were an IRQ handler receiving stuff for | ||
113 | * the other side of the pty/tty pair. | ||
114 | */ | ||
115 | |||
116 | static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c) | ||
117 | { | ||
118 | struct tty_struct *to = tty->link; | ||
119 | |||
120 | if (tty->stopped) | ||
121 | return 0; | ||
122 | |||
123 | if (c > 0) { | ||
124 | /* Stuff the data into the input queue of the other end */ | ||
125 | c = tty_insert_flip_string(to, buf, c); | ||
126 | /* And shovel */ | ||
127 | if (c) { | ||
128 | tty_flip_buffer_push(to); | ||
129 | tty_wakeup(tty); | ||
130 | } | ||
131 | } | ||
132 | return c; | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * pty_write_room - write space | ||
137 | * @tty: tty we are writing from | ||
138 | * | ||
139 | * Report how many bytes the ldisc can send into the queue for | ||
140 | * the other device. | ||
141 | */ | ||
142 | |||
143 | static int pty_write_room(struct tty_struct *tty) | ||
144 | { | ||
145 | if (tty->stopped) | ||
146 | return 0; | ||
147 | return pty_space(tty->link); | ||
148 | } | ||
149 | |||
150 | /** | ||
151 | * pty_chars_in_buffer - characters currently in our tx queue | ||
152 | * @tty: our tty | ||
153 | * | ||
154 | * Report how much we have in the transmit queue. As everything is | ||
155 | * instantly at the other end this is easy to implement. | ||
156 | */ | ||
157 | |||
158 | static int pty_chars_in_buffer(struct tty_struct *tty) | ||
159 | { | ||
160 | return 0; | ||
161 | } | ||
162 | |||
163 | /* Set the lock flag on a pty */ | ||
164 | static int pty_set_lock(struct tty_struct *tty, int __user *arg) | ||
165 | { | ||
166 | int val; | ||
167 | if (get_user(val, arg)) | ||
168 | return -EFAULT; | ||
169 | if (val) | ||
170 | set_bit(TTY_PTY_LOCK, &tty->flags); | ||
171 | else | ||
172 | clear_bit(TTY_PTY_LOCK, &tty->flags); | ||
173 | return 0; | ||
174 | } | ||
175 | |||
176 | /* Send a signal to the slave */ | ||
177 | static int pty_signal(struct tty_struct *tty, int sig) | ||
178 | { | ||
179 | unsigned long flags; | ||
180 | struct pid *pgrp; | ||
181 | |||
182 | if (tty->link) { | ||
183 | spin_lock_irqsave(&tty->link->ctrl_lock, flags); | ||
184 | pgrp = get_pid(tty->link->pgrp); | ||
185 | spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); | ||
186 | |||
187 | kill_pgrp(pgrp, sig, 1); | ||
188 | put_pid(pgrp); | ||
189 | } | ||
190 | return 0; | ||
191 | } | ||
192 | |||
193 | static void pty_flush_buffer(struct tty_struct *tty) | ||
194 | { | ||
195 | struct tty_struct *to = tty->link; | ||
196 | unsigned long flags; | ||
197 | |||
198 | if (!to) | ||
199 | return; | ||
200 | /* tty_buffer_flush(to); FIXME */ | ||
201 | if (to->packet) { | ||
202 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
203 | tty->ctrl_status |= TIOCPKT_FLUSHWRITE; | ||
204 | wake_up_interruptible(&to->read_wait); | ||
205 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
206 | } | ||
207 | } | ||
208 | |||
209 | static int pty_open(struct tty_struct *tty, struct file *filp) | ||
210 | { | ||
211 | int retval = -ENODEV; | ||
212 | |||
213 | if (!tty || !tty->link) | ||
214 | goto out; | ||
215 | |||
216 | retval = -EIO; | ||
217 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | ||
218 | goto out; | ||
219 | if (test_bit(TTY_PTY_LOCK, &tty->link->flags)) | ||
220 | goto out; | ||
221 | if (tty->link->count != 1) | ||
222 | goto out; | ||
223 | |||
224 | clear_bit(TTY_OTHER_CLOSED, &tty->link->flags); | ||
225 | set_bit(TTY_THROTTLED, &tty->flags); | ||
226 | retval = 0; | ||
227 | out: | ||
228 | return retval; | ||
229 | } | ||
230 | |||
231 | static void pty_set_termios(struct tty_struct *tty, | ||
232 | struct ktermios *old_termios) | ||
233 | { | ||
234 | tty->termios->c_cflag &= ~(CSIZE | PARENB); | ||
235 | tty->termios->c_cflag |= (CS8 | CREAD); | ||
236 | } | ||
237 | |||
238 | /** | ||
239 | * pty_do_resize - resize event | ||
240 | * @tty: tty being resized | ||
241 | * @ws: window size being set. | ||
242 | * | ||
243 | * Update the termios variables and send the necessary signals to | ||
244 | * peform a terminal resize correctly | ||
245 | */ | ||
246 | |||
247 | int pty_resize(struct tty_struct *tty, struct winsize *ws) | ||
248 | { | ||
249 | struct pid *pgrp, *rpgrp; | ||
250 | unsigned long flags; | ||
251 | struct tty_struct *pty = tty->link; | ||
252 | |||
253 | /* For a PTY we need to lock the tty side */ | ||
254 | mutex_lock(&tty->termios_mutex); | ||
255 | if (!memcmp(ws, &tty->winsize, sizeof(*ws))) | ||
256 | goto done; | ||
257 | |||
258 | /* Get the PID values and reference them so we can | ||
259 | avoid holding the tty ctrl lock while sending signals. | ||
260 | We need to lock these individually however. */ | ||
261 | |||
262 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
263 | pgrp = get_pid(tty->pgrp); | ||
264 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
265 | |||
266 | spin_lock_irqsave(&pty->ctrl_lock, flags); | ||
267 | rpgrp = get_pid(pty->pgrp); | ||
268 | spin_unlock_irqrestore(&pty->ctrl_lock, flags); | ||
269 | |||
270 | if (pgrp) | ||
271 | kill_pgrp(pgrp, SIGWINCH, 1); | ||
272 | if (rpgrp != pgrp && rpgrp) | ||
273 | kill_pgrp(rpgrp, SIGWINCH, 1); | ||
274 | |||
275 | put_pid(pgrp); | ||
276 | put_pid(rpgrp); | ||
277 | |||
278 | tty->winsize = *ws; | ||
279 | pty->winsize = *ws; /* Never used so will go away soon */ | ||
280 | done: | ||
281 | mutex_unlock(&tty->termios_mutex); | ||
282 | return 0; | ||
283 | } | ||
284 | |||
285 | /* Traditional BSD devices */ | ||
286 | #ifdef CONFIG_LEGACY_PTYS | ||
287 | |||
288 | static int pty_install(struct tty_driver *driver, struct tty_struct *tty) | ||
289 | { | ||
290 | struct tty_struct *o_tty; | ||
291 | int idx = tty->index; | ||
292 | int retval; | ||
293 | |||
294 | o_tty = alloc_tty_struct(); | ||
295 | if (!o_tty) | ||
296 | return -ENOMEM; | ||
297 | if (!try_module_get(driver->other->owner)) { | ||
298 | /* This cannot in fact currently happen */ | ||
299 | free_tty_struct(o_tty); | ||
300 | return -ENOMEM; | ||
301 | } | ||
302 | initialize_tty_struct(o_tty, driver->other, idx); | ||
303 | |||
304 | /* We always use new tty termios data so we can do this | ||
305 | the easy way .. */ | ||
306 | retval = tty_init_termios(tty); | ||
307 | if (retval) | ||
308 | goto free_mem_out; | ||
309 | |||
310 | retval = tty_init_termios(o_tty); | ||
311 | if (retval) { | ||
312 | tty_free_termios(tty); | ||
313 | goto free_mem_out; | ||
314 | } | ||
315 | |||
316 | /* | ||
317 | * Everything allocated ... set up the o_tty structure. | ||
318 | */ | ||
319 | driver->other->ttys[idx] = o_tty; | ||
320 | tty_driver_kref_get(driver->other); | ||
321 | if (driver->subtype == PTY_TYPE_MASTER) | ||
322 | o_tty->count++; | ||
323 | /* Establish the links in both directions */ | ||
324 | tty->link = o_tty; | ||
325 | o_tty->link = tty; | ||
326 | |||
327 | tty_driver_kref_get(driver); | ||
328 | tty->count++; | ||
329 | driver->ttys[idx] = tty; | ||
330 | return 0; | ||
331 | free_mem_out: | ||
332 | module_put(o_tty->driver->owner); | ||
333 | free_tty_struct(o_tty); | ||
334 | return -ENOMEM; | ||
335 | } | ||
336 | |||
337 | static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file, | ||
338 | unsigned int cmd, unsigned long arg) | ||
339 | { | ||
340 | switch (cmd) { | ||
341 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ | ||
342 | return pty_set_lock(tty, (int __user *) arg); | ||
343 | case TIOCSIG: /* Send signal to other side of pty */ | ||
344 | return pty_signal(tty, (int) arg); | ||
345 | } | ||
346 | return -ENOIOCTLCMD; | ||
347 | } | ||
348 | |||
349 | static int legacy_count = CONFIG_LEGACY_PTY_COUNT; | ||
350 | module_param(legacy_count, int, 0); | ||
351 | |||
352 | /* | ||
353 | * The master side of a pty can do TIOCSPTLCK and thus | ||
354 | * has pty_bsd_ioctl. | ||
355 | */ | ||
356 | static const struct tty_operations master_pty_ops_bsd = { | ||
357 | .install = pty_install, | ||
358 | .open = pty_open, | ||
359 | .close = pty_close, | ||
360 | .write = pty_write, | ||
361 | .write_room = pty_write_room, | ||
362 | .flush_buffer = pty_flush_buffer, | ||
363 | .chars_in_buffer = pty_chars_in_buffer, | ||
364 | .unthrottle = pty_unthrottle, | ||
365 | .set_termios = pty_set_termios, | ||
366 | .ioctl = pty_bsd_ioctl, | ||
367 | .resize = pty_resize | ||
368 | }; | ||
369 | |||
370 | static const struct tty_operations slave_pty_ops_bsd = { | ||
371 | .install = pty_install, | ||
372 | .open = pty_open, | ||
373 | .close = pty_close, | ||
374 | .write = pty_write, | ||
375 | .write_room = pty_write_room, | ||
376 | .flush_buffer = pty_flush_buffer, | ||
377 | .chars_in_buffer = pty_chars_in_buffer, | ||
378 | .unthrottle = pty_unthrottle, | ||
379 | .set_termios = pty_set_termios, | ||
380 | .resize = pty_resize | ||
381 | }; | ||
382 | |||
383 | static void __init legacy_pty_init(void) | ||
384 | { | ||
385 | struct tty_driver *pty_driver, *pty_slave_driver; | ||
386 | |||
387 | if (legacy_count <= 0) | ||
388 | return; | ||
389 | |||
390 | pty_driver = alloc_tty_driver(legacy_count); | ||
391 | if (!pty_driver) | ||
392 | panic("Couldn't allocate pty driver"); | ||
393 | |||
394 | pty_slave_driver = alloc_tty_driver(legacy_count); | ||
395 | if (!pty_slave_driver) | ||
396 | panic("Couldn't allocate pty slave driver"); | ||
397 | |||
398 | pty_driver->owner = THIS_MODULE; | ||
399 | pty_driver->driver_name = "pty_master"; | ||
400 | pty_driver->name = "pty"; | ||
401 | pty_driver->major = PTY_MASTER_MAJOR; | ||
402 | pty_driver->minor_start = 0; | ||
403 | pty_driver->type = TTY_DRIVER_TYPE_PTY; | ||
404 | pty_driver->subtype = PTY_TYPE_MASTER; | ||
405 | pty_driver->init_termios = tty_std_termios; | ||
406 | pty_driver->init_termios.c_iflag = 0; | ||
407 | pty_driver->init_termios.c_oflag = 0; | ||
408 | pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | ||
409 | pty_driver->init_termios.c_lflag = 0; | ||
410 | pty_driver->init_termios.c_ispeed = 38400; | ||
411 | pty_driver->init_termios.c_ospeed = 38400; | ||
412 | pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW; | ||
413 | pty_driver->other = pty_slave_driver; | ||
414 | tty_set_operations(pty_driver, &master_pty_ops_bsd); | ||
415 | |||
416 | pty_slave_driver->owner = THIS_MODULE; | ||
417 | pty_slave_driver->driver_name = "pty_slave"; | ||
418 | pty_slave_driver->name = "ttyp"; | ||
419 | pty_slave_driver->major = PTY_SLAVE_MAJOR; | ||
420 | pty_slave_driver->minor_start = 0; | ||
421 | pty_slave_driver->type = TTY_DRIVER_TYPE_PTY; | ||
422 | pty_slave_driver->subtype = PTY_TYPE_SLAVE; | ||
423 | pty_slave_driver->init_termios = tty_std_termios; | ||
424 | pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | ||
425 | pty_slave_driver->init_termios.c_ispeed = 38400; | ||
426 | pty_slave_driver->init_termios.c_ospeed = 38400; | ||
427 | pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS | | ||
428 | TTY_DRIVER_REAL_RAW; | ||
429 | pty_slave_driver->other = pty_driver; | ||
430 | tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd); | ||
431 | |||
432 | if (tty_register_driver(pty_driver)) | ||
433 | panic("Couldn't register pty driver"); | ||
434 | if (tty_register_driver(pty_slave_driver)) | ||
435 | panic("Couldn't register pty slave driver"); | ||
436 | } | ||
437 | #else | ||
438 | static inline void legacy_pty_init(void) { } | ||
439 | #endif | ||
440 | |||
441 | /* Unix98 devices */ | ||
442 | #ifdef CONFIG_UNIX98_PTYS | ||
443 | /* | ||
444 | * sysctl support for setting limits on the number of Unix98 ptys allocated. | ||
445 | * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly. | ||
446 | */ | ||
447 | int pty_limit = NR_UNIX98_PTY_DEFAULT; | ||
448 | static int pty_limit_min; | ||
449 | static int pty_limit_max = NR_UNIX98_PTY_MAX; | ||
450 | static int pty_count; | ||
451 | |||
452 | static struct cdev ptmx_cdev; | ||
453 | |||
454 | static struct ctl_table pty_table[] = { | ||
455 | { | ||
456 | .procname = "max", | ||
457 | .maxlen = sizeof(int), | ||
458 | .mode = 0644, | ||
459 | .data = &pty_limit, | ||
460 | .proc_handler = proc_dointvec_minmax, | ||
461 | .extra1 = &pty_limit_min, | ||
462 | .extra2 = &pty_limit_max, | ||
463 | }, { | ||
464 | .procname = "nr", | ||
465 | .maxlen = sizeof(int), | ||
466 | .mode = 0444, | ||
467 | .data = &pty_count, | ||
468 | .proc_handler = proc_dointvec, | ||
469 | }, | ||
470 | {} | ||
471 | }; | ||
472 | |||
473 | static struct ctl_table pty_kern_table[] = { | ||
474 | { | ||
475 | .procname = "pty", | ||
476 | .mode = 0555, | ||
477 | .child = pty_table, | ||
478 | }, | ||
479 | {} | ||
480 | }; | ||
481 | |||
482 | static struct ctl_table pty_root_table[] = { | ||
483 | { | ||
484 | .procname = "kernel", | ||
485 | .mode = 0555, | ||
486 | .child = pty_kern_table, | ||
487 | }, | ||
488 | {} | ||
489 | }; | ||
490 | |||
491 | |||
492 | static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file, | ||
493 | unsigned int cmd, unsigned long arg) | ||
494 | { | ||
495 | switch (cmd) { | ||
496 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ | ||
497 | return pty_set_lock(tty, (int __user *)arg); | ||
498 | case TIOCGPTN: /* Get PT Number */ | ||
499 | return put_user(tty->index, (unsigned int __user *)arg); | ||
500 | case TIOCSIG: /* Send signal to other side of pty */ | ||
501 | return pty_signal(tty, (int) arg); | ||
502 | } | ||
503 | |||
504 | return -ENOIOCTLCMD; | ||
505 | } | ||
506 | |||
507 | /** | ||
508 | * ptm_unix98_lookup - find a pty master | ||
509 | * @driver: ptm driver | ||
510 | * @idx: tty index | ||
511 | * | ||
512 | * Look up a pty master device. Called under the tty_mutex for now. | ||
513 | * This provides our locking. | ||
514 | */ | ||
515 | |||
516 | static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver, | ||
517 | struct inode *ptm_inode, int idx) | ||
518 | { | ||
519 | struct tty_struct *tty = devpts_get_tty(ptm_inode, idx); | ||
520 | if (tty) | ||
521 | tty = tty->link; | ||
522 | return tty; | ||
523 | } | ||
524 | |||
525 | /** | ||
526 | * pts_unix98_lookup - find a pty slave | ||
527 | * @driver: pts driver | ||
528 | * @idx: tty index | ||
529 | * | ||
530 | * Look up a pty master device. Called under the tty_mutex for now. | ||
531 | * This provides our locking. | ||
532 | */ | ||
533 | |||
534 | static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, | ||
535 | struct inode *pts_inode, int idx) | ||
536 | { | ||
537 | struct tty_struct *tty = devpts_get_tty(pts_inode, idx); | ||
538 | /* Master must be open before slave */ | ||
539 | if (!tty) | ||
540 | return ERR_PTR(-EIO); | ||
541 | return tty; | ||
542 | } | ||
543 | |||
544 | static void pty_unix98_shutdown(struct tty_struct *tty) | ||
545 | { | ||
546 | /* We have our own method as we don't use the tty index */ | ||
547 | kfree(tty->termios); | ||
548 | } | ||
549 | |||
550 | /* We have no need to install and remove our tty objects as devpts does all | ||
551 | the work for us */ | ||
552 | |||
553 | static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty) | ||
554 | { | ||
555 | struct tty_struct *o_tty; | ||
556 | int idx = tty->index; | ||
557 | |||
558 | o_tty = alloc_tty_struct(); | ||
559 | if (!o_tty) | ||
560 | return -ENOMEM; | ||
561 | if (!try_module_get(driver->other->owner)) { | ||
562 | /* This cannot in fact currently happen */ | ||
563 | free_tty_struct(o_tty); | ||
564 | return -ENOMEM; | ||
565 | } | ||
566 | initialize_tty_struct(o_tty, driver->other, idx); | ||
567 | |||
568 | tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); | ||
569 | if (tty->termios == NULL) | ||
570 | goto free_mem_out; | ||
571 | *tty->termios = driver->init_termios; | ||
572 | tty->termios_locked = tty->termios + 1; | ||
573 | |||
574 | o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); | ||
575 | if (o_tty->termios == NULL) | ||
576 | goto free_mem_out; | ||
577 | *o_tty->termios = driver->other->init_termios; | ||
578 | o_tty->termios_locked = o_tty->termios + 1; | ||
579 | |||
580 | tty_driver_kref_get(driver->other); | ||
581 | if (driver->subtype == PTY_TYPE_MASTER) | ||
582 | o_tty->count++; | ||
583 | /* Establish the links in both directions */ | ||
584 | tty->link = o_tty; | ||
585 | o_tty->link = tty; | ||
586 | /* | ||
587 | * All structures have been allocated, so now we install them. | ||
588 | * Failures after this point use release_tty to clean up, so | ||
589 | * there's no need to null out the local pointers. | ||
590 | */ | ||
591 | tty_driver_kref_get(driver); | ||
592 | tty->count++; | ||
593 | pty_count++; | ||
594 | return 0; | ||
595 | free_mem_out: | ||
596 | kfree(o_tty->termios); | ||
597 | module_put(o_tty->driver->owner); | ||
598 | free_tty_struct(o_tty); | ||
599 | kfree(tty->termios); | ||
600 | return -ENOMEM; | ||
601 | } | ||
602 | |||
603 | static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty) | ||
604 | { | ||
605 | pty_count--; | ||
606 | } | ||
607 | |||
608 | static const struct tty_operations ptm_unix98_ops = { | ||
609 | .lookup = ptm_unix98_lookup, | ||
610 | .install = pty_unix98_install, | ||
611 | .remove = pty_unix98_remove, | ||
612 | .open = pty_open, | ||
613 | .close = pty_close, | ||
614 | .write = pty_write, | ||
615 | .write_room = pty_write_room, | ||
616 | .flush_buffer = pty_flush_buffer, | ||
617 | .chars_in_buffer = pty_chars_in_buffer, | ||
618 | .unthrottle = pty_unthrottle, | ||
619 | .set_termios = pty_set_termios, | ||
620 | .ioctl = pty_unix98_ioctl, | ||
621 | .shutdown = pty_unix98_shutdown, | ||
622 | .resize = pty_resize | ||
623 | }; | ||
624 | |||
625 | static const struct tty_operations pty_unix98_ops = { | ||
626 | .lookup = pts_unix98_lookup, | ||
627 | .install = pty_unix98_install, | ||
628 | .remove = pty_unix98_remove, | ||
629 | .open = pty_open, | ||
630 | .close = pty_close, | ||
631 | .write = pty_write, | ||
632 | .write_room = pty_write_room, | ||
633 | .flush_buffer = pty_flush_buffer, | ||
634 | .chars_in_buffer = pty_chars_in_buffer, | ||
635 | .unthrottle = pty_unthrottle, | ||
636 | .set_termios = pty_set_termios, | ||
637 | .shutdown = pty_unix98_shutdown | ||
638 | }; | ||
639 | |||
640 | /** | ||
641 | * ptmx_open - open a unix 98 pty master | ||
642 | * @inode: inode of device file | ||
643 | * @filp: file pointer to tty | ||
644 | * | ||
645 | * Allocate a unix98 pty master device from the ptmx driver. | ||
646 | * | ||
647 | * Locking: tty_mutex protects the init_dev work. tty->count should | ||
648 | * protect the rest. | ||
649 | * allocated_ptys_lock handles the list of free pty numbers | ||
650 | */ | ||
651 | |||
652 | static int ptmx_open(struct inode *inode, struct file *filp) | ||
653 | { | ||
654 | struct tty_struct *tty; | ||
655 | int retval; | ||
656 | int index; | ||
657 | |||
658 | nonseekable_open(inode, filp); | ||
659 | |||
660 | /* find a device that is not in use. */ | ||
661 | tty_lock(); | ||
662 | index = devpts_new_index(inode); | ||
663 | tty_unlock(); | ||
664 | if (index < 0) | ||
665 | return index; | ||
666 | |||
667 | mutex_lock(&tty_mutex); | ||
668 | tty_lock(); | ||
669 | tty = tty_init_dev(ptm_driver, index, 1); | ||
670 | mutex_unlock(&tty_mutex); | ||
671 | |||
672 | if (IS_ERR(tty)) { | ||
673 | retval = PTR_ERR(tty); | ||
674 | goto out; | ||
675 | } | ||
676 | |||
677 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ | ||
678 | |||
679 | retval = tty_add_file(tty, filp); | ||
680 | if (retval) | ||
681 | goto out; | ||
682 | |||
683 | retval = devpts_pty_new(inode, tty->link); | ||
684 | if (retval) | ||
685 | goto out1; | ||
686 | |||
687 | retval = ptm_driver->ops->open(tty, filp); | ||
688 | if (retval) | ||
689 | goto out2; | ||
690 | out1: | ||
691 | tty_unlock(); | ||
692 | return retval; | ||
693 | out2: | ||
694 | tty_unlock(); | ||
695 | tty_release(inode, filp); | ||
696 | return retval; | ||
697 | out: | ||
698 | devpts_kill_index(inode, index); | ||
699 | tty_unlock(); | ||
700 | return retval; | ||
701 | } | ||
702 | |||
703 | static struct file_operations ptmx_fops; | ||
704 | |||
705 | static void __init unix98_pty_init(void) | ||
706 | { | ||
707 | ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); | ||
708 | if (!ptm_driver) | ||
709 | panic("Couldn't allocate Unix98 ptm driver"); | ||
710 | pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); | ||
711 | if (!pts_driver) | ||
712 | panic("Couldn't allocate Unix98 pts driver"); | ||
713 | |||
714 | ptm_driver->owner = THIS_MODULE; | ||
715 | ptm_driver->driver_name = "pty_master"; | ||
716 | ptm_driver->name = "ptm"; | ||
717 | ptm_driver->major = UNIX98_PTY_MASTER_MAJOR; | ||
718 | ptm_driver->minor_start = 0; | ||
719 | ptm_driver->type = TTY_DRIVER_TYPE_PTY; | ||
720 | ptm_driver->subtype = PTY_TYPE_MASTER; | ||
721 | ptm_driver->init_termios = tty_std_termios; | ||
722 | ptm_driver->init_termios.c_iflag = 0; | ||
723 | ptm_driver->init_termios.c_oflag = 0; | ||
724 | ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | ||
725 | ptm_driver->init_termios.c_lflag = 0; | ||
726 | ptm_driver->init_termios.c_ispeed = 38400; | ||
727 | ptm_driver->init_termios.c_ospeed = 38400; | ||
728 | ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | | ||
729 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; | ||
730 | ptm_driver->other = pts_driver; | ||
731 | tty_set_operations(ptm_driver, &ptm_unix98_ops); | ||
732 | |||
733 | pts_driver->owner = THIS_MODULE; | ||
734 | pts_driver->driver_name = "pty_slave"; | ||
735 | pts_driver->name = "pts"; | ||
736 | pts_driver->major = UNIX98_PTY_SLAVE_MAJOR; | ||
737 | pts_driver->minor_start = 0; | ||
738 | pts_driver->type = TTY_DRIVER_TYPE_PTY; | ||
739 | pts_driver->subtype = PTY_TYPE_SLAVE; | ||
740 | pts_driver->init_termios = tty_std_termios; | ||
741 | pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | ||
742 | pts_driver->init_termios.c_ispeed = 38400; | ||
743 | pts_driver->init_termios.c_ospeed = 38400; | ||
744 | pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | | ||
745 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; | ||
746 | pts_driver->other = ptm_driver; | ||
747 | tty_set_operations(pts_driver, &pty_unix98_ops); | ||
748 | |||
749 | if (tty_register_driver(ptm_driver)) | ||
750 | panic("Couldn't register Unix98 ptm driver"); | ||
751 | if (tty_register_driver(pts_driver)) | ||
752 | panic("Couldn't register Unix98 pts driver"); | ||
753 | |||
754 | register_sysctl_table(pty_root_table); | ||
755 | |||
756 | /* Now create the /dev/ptmx special device */ | ||
757 | tty_default_fops(&ptmx_fops); | ||
758 | ptmx_fops.open = ptmx_open; | ||
759 | |||
760 | cdev_init(&ptmx_cdev, &ptmx_fops); | ||
761 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || | ||
762 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) | ||
763 | panic("Couldn't register /dev/ptmx driver\n"); | ||
764 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); | ||
765 | } | ||
766 | |||
767 | #else | ||
768 | static inline void unix98_pty_init(void) { } | ||
769 | #endif | ||
770 | |||
771 | static int __init pty_init(void) | ||
772 | { | ||
773 | legacy_pty_init(); | ||
774 | unix98_pty_init(); | ||
775 | return 0; | ||
776 | } | ||
777 | module_init(pty_init); | ||
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c new file mode 100644 index 000000000000..eaa5d3efa79d --- /dev/null +++ b/drivers/tty/sysrq.c | |||
@@ -0,0 +1,811 @@ | |||
1 | /* | ||
2 | * Linux Magic System Request Key Hacks | ||
3 | * | ||
4 | * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz> | ||
5 | * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz> | ||
6 | * | ||
7 | * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com> | ||
8 | * overhauled to use key registration | ||
9 | * based upon discusions in irc://irc.openprojects.net/#kernelnewbies | ||
10 | * | ||
11 | * Copyright (c) 2010 Dmitry Torokhov | ||
12 | * Input handler conversion | ||
13 | */ | ||
14 | |||
15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
16 | |||
17 | #include <linux/sched.h> | ||
18 | #include <linux/interrupt.h> | ||
19 | #include <linux/mm.h> | ||
20 | #include <linux/fs.h> | ||
21 | #include <linux/mount.h> | ||
22 | #include <linux/kdev_t.h> | ||
23 | #include <linux/major.h> | ||
24 | #include <linux/reboot.h> | ||
25 | #include <linux/sysrq.h> | ||
26 | #include <linux/kbd_kern.h> | ||
27 | #include <linux/proc_fs.h> | ||
28 | #include <linux/nmi.h> | ||
29 | #include <linux/quotaops.h> | ||
30 | #include <linux/perf_event.h> | ||
31 | #include <linux/kernel.h> | ||
32 | #include <linux/module.h> | ||
33 | #include <linux/suspend.h> | ||
34 | #include <linux/writeback.h> | ||
35 | #include <linux/buffer_head.h> /* for fsync_bdev() */ | ||
36 | #include <linux/swap.h> | ||
37 | #include <linux/spinlock.h> | ||
38 | #include <linux/vt_kern.h> | ||
39 | #include <linux/workqueue.h> | ||
40 | #include <linux/hrtimer.h> | ||
41 | #include <linux/oom.h> | ||
42 | #include <linux/slab.h> | ||
43 | #include <linux/input.h> | ||
44 | |||
45 | #include <asm/ptrace.h> | ||
46 | #include <asm/irq_regs.h> | ||
47 | |||
48 | /* Whether we react on sysrq keys or just ignore them */ | ||
49 | static int __read_mostly sysrq_enabled = 1; | ||
50 | static bool __read_mostly sysrq_always_enabled; | ||
51 | |||
52 | static bool sysrq_on(void) | ||
53 | { | ||
54 | return sysrq_enabled || sysrq_always_enabled; | ||
55 | } | ||
56 | |||
57 | /* | ||
58 | * A value of 1 means 'all', other nonzero values are an op mask: | ||
59 | */ | ||
60 | static bool sysrq_on_mask(int mask) | ||
61 | { | ||
62 | return sysrq_always_enabled || | ||
63 | sysrq_enabled == 1 || | ||
64 | (sysrq_enabled & mask); | ||
65 | } | ||
66 | |||
67 | static int __init sysrq_always_enabled_setup(char *str) | ||
68 | { | ||
69 | sysrq_always_enabled = true; | ||
70 | pr_info("sysrq always enabled.\n"); | ||
71 | |||
72 | return 1; | ||
73 | } | ||
74 | |||
75 | __setup("sysrq_always_enabled", sysrq_always_enabled_setup); | ||
76 | |||
77 | |||
78 | static void sysrq_handle_loglevel(int key) | ||
79 | { | ||
80 | int i; | ||
81 | |||
82 | i = key - '0'; | ||
83 | console_loglevel = 7; | ||
84 | printk("Loglevel set to %d\n", i); | ||
85 | console_loglevel = i; | ||
86 | } | ||
87 | static struct sysrq_key_op sysrq_loglevel_op = { | ||
88 | .handler = sysrq_handle_loglevel, | ||
89 | .help_msg = "loglevel(0-9)", | ||
90 | .action_msg = "Changing Loglevel", | ||
91 | .enable_mask = SYSRQ_ENABLE_LOG, | ||
92 | }; | ||
93 | |||
94 | #ifdef CONFIG_VT | ||
95 | static void sysrq_handle_SAK(int key) | ||
96 | { | ||
97 | struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work; | ||
98 | schedule_work(SAK_work); | ||
99 | } | ||
100 | static struct sysrq_key_op sysrq_SAK_op = { | ||
101 | .handler = sysrq_handle_SAK, | ||
102 | .help_msg = "saK", | ||
103 | .action_msg = "SAK", | ||
104 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, | ||
105 | }; | ||
106 | #else | ||
107 | #define sysrq_SAK_op (*(struct sysrq_key_op *)NULL) | ||
108 | #endif | ||
109 | |||
110 | #ifdef CONFIG_VT | ||
111 | static void sysrq_handle_unraw(int key) | ||
112 | { | ||
113 | struct kbd_struct *kbd = &kbd_table[fg_console]; | ||
114 | |||
115 | if (kbd) | ||
116 | kbd->kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; | ||
117 | } | ||
118 | static struct sysrq_key_op sysrq_unraw_op = { | ||
119 | .handler = sysrq_handle_unraw, | ||
120 | .help_msg = "unRaw", | ||
121 | .action_msg = "Keyboard mode set to system default", | ||
122 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, | ||
123 | }; | ||
124 | #else | ||
125 | #define sysrq_unraw_op (*(struct sysrq_key_op *)NULL) | ||
126 | #endif /* CONFIG_VT */ | ||
127 | |||
128 | static void sysrq_handle_crash(int key) | ||
129 | { | ||
130 | char *killer = NULL; | ||
131 | |||
132 | panic_on_oops = 1; /* force panic */ | ||
133 | wmb(); | ||
134 | *killer = 1; | ||
135 | } | ||
136 | static struct sysrq_key_op sysrq_crash_op = { | ||
137 | .handler = sysrq_handle_crash, | ||
138 | .help_msg = "Crash", | ||
139 | .action_msg = "Trigger a crash", | ||
140 | .enable_mask = SYSRQ_ENABLE_DUMP, | ||
141 | }; | ||
142 | |||
143 | static void sysrq_handle_reboot(int key) | ||
144 | { | ||
145 | lockdep_off(); | ||
146 | local_irq_enable(); | ||
147 | emergency_restart(); | ||
148 | } | ||
149 | static struct sysrq_key_op sysrq_reboot_op = { | ||
150 | .handler = sysrq_handle_reboot, | ||
151 | .help_msg = "reBoot", | ||
152 | .action_msg = "Resetting", | ||
153 | .enable_mask = SYSRQ_ENABLE_BOOT, | ||
154 | }; | ||
155 | |||
156 | static void sysrq_handle_sync(int key) | ||
157 | { | ||
158 | emergency_sync(); | ||
159 | } | ||
160 | static struct sysrq_key_op sysrq_sync_op = { | ||
161 | .handler = sysrq_handle_sync, | ||
162 | .help_msg = "Sync", | ||
163 | .action_msg = "Emergency Sync", | ||
164 | .enable_mask = SYSRQ_ENABLE_SYNC, | ||
165 | }; | ||
166 | |||
167 | static void sysrq_handle_show_timers(int key) | ||
168 | { | ||
169 | sysrq_timer_list_show(); | ||
170 | } | ||
171 | |||
172 | static struct sysrq_key_op sysrq_show_timers_op = { | ||
173 | .handler = sysrq_handle_show_timers, | ||
174 | .help_msg = "show-all-timers(Q)", | ||
175 | .action_msg = "Show clockevent devices & pending hrtimers (no others)", | ||
176 | }; | ||
177 | |||
178 | static void sysrq_handle_mountro(int key) | ||
179 | { | ||
180 | emergency_remount(); | ||
181 | } | ||
182 | static struct sysrq_key_op sysrq_mountro_op = { | ||
183 | .handler = sysrq_handle_mountro, | ||
184 | .help_msg = "Unmount", | ||
185 | .action_msg = "Emergency Remount R/O", | ||
186 | .enable_mask = SYSRQ_ENABLE_REMOUNT, | ||
187 | }; | ||
188 | |||
189 | #ifdef CONFIG_LOCKDEP | ||
190 | static void sysrq_handle_showlocks(int key) | ||
191 | { | ||
192 | debug_show_all_locks(); | ||
193 | } | ||
194 | |||
195 | static struct sysrq_key_op sysrq_showlocks_op = { | ||
196 | .handler = sysrq_handle_showlocks, | ||
197 | .help_msg = "show-all-locks(D)", | ||
198 | .action_msg = "Show Locks Held", | ||
199 | }; | ||
200 | #else | ||
201 | #define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL) | ||
202 | #endif | ||
203 | |||
204 | #ifdef CONFIG_SMP | ||
205 | static DEFINE_SPINLOCK(show_lock); | ||
206 | |||
207 | static void showacpu(void *dummy) | ||
208 | { | ||
209 | unsigned long flags; | ||
210 | |||
211 | /* Idle CPUs have no interesting backtrace. */ | ||
212 | if (idle_cpu(smp_processor_id())) | ||
213 | return; | ||
214 | |||
215 | spin_lock_irqsave(&show_lock, flags); | ||
216 | printk(KERN_INFO "CPU%d:\n", smp_processor_id()); | ||
217 | show_stack(NULL, NULL); | ||
218 | spin_unlock_irqrestore(&show_lock, flags); | ||
219 | } | ||
220 | |||
221 | static void sysrq_showregs_othercpus(struct work_struct *dummy) | ||
222 | { | ||
223 | smp_call_function(showacpu, NULL, 0); | ||
224 | } | ||
225 | |||
226 | static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus); | ||
227 | |||
228 | static void sysrq_handle_showallcpus(int key) | ||
229 | { | ||
230 | /* | ||
231 | * Fall back to the workqueue based printing if the | ||
232 | * backtrace printing did not succeed or the | ||
233 | * architecture has no support for it: | ||
234 | */ | ||
235 | if (!trigger_all_cpu_backtrace()) { | ||
236 | struct pt_regs *regs = get_irq_regs(); | ||
237 | |||
238 | if (regs) { | ||
239 | printk(KERN_INFO "CPU%d:\n", smp_processor_id()); | ||
240 | show_regs(regs); | ||
241 | } | ||
242 | schedule_work(&sysrq_showallcpus); | ||
243 | } | ||
244 | } | ||
245 | |||
246 | static struct sysrq_key_op sysrq_showallcpus_op = { | ||
247 | .handler = sysrq_handle_showallcpus, | ||
248 | .help_msg = "show-backtrace-all-active-cpus(L)", | ||
249 | .action_msg = "Show backtrace of all active CPUs", | ||
250 | .enable_mask = SYSRQ_ENABLE_DUMP, | ||
251 | }; | ||
252 | #endif | ||
253 | |||
254 | static void sysrq_handle_showregs(int key) | ||
255 | { | ||
256 | struct pt_regs *regs = get_irq_regs(); | ||
257 | if (regs) | ||
258 | show_regs(regs); | ||
259 | perf_event_print_debug(); | ||
260 | } | ||
261 | static struct sysrq_key_op sysrq_showregs_op = { | ||
262 | .handler = sysrq_handle_showregs, | ||
263 | .help_msg = "show-registers(P)", | ||
264 | .action_msg = "Show Regs", | ||
265 | .enable_mask = SYSRQ_ENABLE_DUMP, | ||
266 | }; | ||
267 | |||
268 | static void sysrq_handle_showstate(int key) | ||
269 | { | ||
270 | show_state(); | ||
271 | } | ||
272 | static struct sysrq_key_op sysrq_showstate_op = { | ||
273 | .handler = sysrq_handle_showstate, | ||
274 | .help_msg = "show-task-states(T)", | ||
275 | .action_msg = "Show State", | ||
276 | .enable_mask = SYSRQ_ENABLE_DUMP, | ||
277 | }; | ||
278 | |||
279 | static void sysrq_handle_showstate_blocked(int key) | ||
280 | { | ||
281 | show_state_filter(TASK_UNINTERRUPTIBLE); | ||
282 | } | ||
283 | static struct sysrq_key_op sysrq_showstate_blocked_op = { | ||
284 | .handler = sysrq_handle_showstate_blocked, | ||
285 | .help_msg = "show-blocked-tasks(W)", | ||
286 | .action_msg = "Show Blocked State", | ||
287 | .enable_mask = SYSRQ_ENABLE_DUMP, | ||
288 | }; | ||
289 | |||
290 | #ifdef CONFIG_TRACING | ||
291 | #include <linux/ftrace.h> | ||
292 | |||
293 | static void sysrq_ftrace_dump(int key) | ||
294 | { | ||
295 | ftrace_dump(DUMP_ALL); | ||
296 | } | ||
297 | static struct sysrq_key_op sysrq_ftrace_dump_op = { | ||
298 | .handler = sysrq_ftrace_dump, | ||
299 | .help_msg = "dump-ftrace-buffer(Z)", | ||
300 | .action_msg = "Dump ftrace buffer", | ||
301 | .enable_mask = SYSRQ_ENABLE_DUMP, | ||
302 | }; | ||
303 | #else | ||
304 | #define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL) | ||
305 | #endif | ||
306 | |||
307 | static void sysrq_handle_showmem(int key) | ||
308 | { | ||
309 | show_mem(); | ||
310 | } | ||
311 | static struct sysrq_key_op sysrq_showmem_op = { | ||
312 | .handler = sysrq_handle_showmem, | ||
313 | .help_msg = "show-memory-usage(M)", | ||
314 | .action_msg = "Show Memory", | ||
315 | .enable_mask = SYSRQ_ENABLE_DUMP, | ||
316 | }; | ||
317 | |||
318 | /* | ||
319 | * Signal sysrq helper function. Sends a signal to all user processes. | ||
320 | */ | ||
321 | static void send_sig_all(int sig) | ||
322 | { | ||
323 | struct task_struct *p; | ||
324 | |||
325 | for_each_process(p) { | ||
326 | if (p->mm && !is_global_init(p)) | ||
327 | /* Not swapper, init nor kernel thread */ | ||
328 | force_sig(sig, p); | ||
329 | } | ||
330 | } | ||
331 | |||
332 | static void sysrq_handle_term(int key) | ||
333 | { | ||
334 | send_sig_all(SIGTERM); | ||
335 | console_loglevel = 8; | ||
336 | } | ||
337 | static struct sysrq_key_op sysrq_term_op = { | ||
338 | .handler = sysrq_handle_term, | ||
339 | .help_msg = "terminate-all-tasks(E)", | ||
340 | .action_msg = "Terminate All Tasks", | ||
341 | .enable_mask = SYSRQ_ENABLE_SIGNAL, | ||
342 | }; | ||
343 | |||
344 | static void moom_callback(struct work_struct *ignored) | ||
345 | { | ||
346 | out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0, NULL); | ||
347 | } | ||
348 | |||
349 | static DECLARE_WORK(moom_work, moom_callback); | ||
350 | |||
351 | static void sysrq_handle_moom(int key) | ||
352 | { | ||
353 | schedule_work(&moom_work); | ||
354 | } | ||
355 | static struct sysrq_key_op sysrq_moom_op = { | ||
356 | .handler = sysrq_handle_moom, | ||
357 | .help_msg = "memory-full-oom-kill(F)", | ||
358 | .action_msg = "Manual OOM execution", | ||
359 | .enable_mask = SYSRQ_ENABLE_SIGNAL, | ||
360 | }; | ||
361 | |||
362 | #ifdef CONFIG_BLOCK | ||
363 | static void sysrq_handle_thaw(int key) | ||
364 | { | ||
365 | emergency_thaw_all(); | ||
366 | } | ||
367 | static struct sysrq_key_op sysrq_thaw_op = { | ||
368 | .handler = sysrq_handle_thaw, | ||
369 | .help_msg = "thaw-filesystems(J)", | ||
370 | .action_msg = "Emergency Thaw of all frozen filesystems", | ||
371 | .enable_mask = SYSRQ_ENABLE_SIGNAL, | ||
372 | }; | ||
373 | #endif | ||
374 | |||
375 | static void sysrq_handle_kill(int key) | ||
376 | { | ||
377 | send_sig_all(SIGKILL); | ||
378 | console_loglevel = 8; | ||
379 | } | ||
380 | static struct sysrq_key_op sysrq_kill_op = { | ||
381 | .handler = sysrq_handle_kill, | ||
382 | .help_msg = "kill-all-tasks(I)", | ||
383 | .action_msg = "Kill All Tasks", | ||
384 | .enable_mask = SYSRQ_ENABLE_SIGNAL, | ||
385 | }; | ||
386 | |||
387 | static void sysrq_handle_unrt(int key) | ||
388 | { | ||
389 | normalize_rt_tasks(); | ||
390 | } | ||
391 | static struct sysrq_key_op sysrq_unrt_op = { | ||
392 | .handler = sysrq_handle_unrt, | ||
393 | .help_msg = "nice-all-RT-tasks(N)", | ||
394 | .action_msg = "Nice All RT Tasks", | ||
395 | .enable_mask = SYSRQ_ENABLE_RTNICE, | ||
396 | }; | ||
397 | |||
398 | /* Key Operations table and lock */ | ||
399 | static DEFINE_SPINLOCK(sysrq_key_table_lock); | ||
400 | |||
401 | static struct sysrq_key_op *sysrq_key_table[36] = { | ||
402 | &sysrq_loglevel_op, /* 0 */ | ||
403 | &sysrq_loglevel_op, /* 1 */ | ||
404 | &sysrq_loglevel_op, /* 2 */ | ||
405 | &sysrq_loglevel_op, /* 3 */ | ||
406 | &sysrq_loglevel_op, /* 4 */ | ||
407 | &sysrq_loglevel_op, /* 5 */ | ||
408 | &sysrq_loglevel_op, /* 6 */ | ||
409 | &sysrq_loglevel_op, /* 7 */ | ||
410 | &sysrq_loglevel_op, /* 8 */ | ||
411 | &sysrq_loglevel_op, /* 9 */ | ||
412 | |||
413 | /* | ||
414 | * a: Don't use for system provided sysrqs, it is handled specially on | ||
415 | * sparc and will never arrive. | ||
416 | */ | ||
417 | NULL, /* a */ | ||
418 | &sysrq_reboot_op, /* b */ | ||
419 | &sysrq_crash_op, /* c & ibm_emac driver debug */ | ||
420 | &sysrq_showlocks_op, /* d */ | ||
421 | &sysrq_term_op, /* e */ | ||
422 | &sysrq_moom_op, /* f */ | ||
423 | /* g: May be registered for the kernel debugger */ | ||
424 | NULL, /* g */ | ||
425 | NULL, /* h - reserved for help */ | ||
426 | &sysrq_kill_op, /* i */ | ||
427 | #ifdef CONFIG_BLOCK | ||
428 | &sysrq_thaw_op, /* j */ | ||
429 | #else | ||
430 | NULL, /* j */ | ||
431 | #endif | ||
432 | &sysrq_SAK_op, /* k */ | ||
433 | #ifdef CONFIG_SMP | ||
434 | &sysrq_showallcpus_op, /* l */ | ||
435 | #else | ||
436 | NULL, /* l */ | ||
437 | #endif | ||
438 | &sysrq_showmem_op, /* m */ | ||
439 | &sysrq_unrt_op, /* n */ | ||
440 | /* o: This will often be registered as 'Off' at init time */ | ||
441 | NULL, /* o */ | ||
442 | &sysrq_showregs_op, /* p */ | ||
443 | &sysrq_show_timers_op, /* q */ | ||
444 | &sysrq_unraw_op, /* r */ | ||
445 | &sysrq_sync_op, /* s */ | ||
446 | &sysrq_showstate_op, /* t */ | ||
447 | &sysrq_mountro_op, /* u */ | ||
448 | /* v: May be registered for frame buffer console restore */ | ||
449 | NULL, /* v */ | ||
450 | &sysrq_showstate_blocked_op, /* w */ | ||
451 | /* x: May be registered on ppc/powerpc for xmon */ | ||
452 | NULL, /* x */ | ||
453 | /* y: May be registered on sparc64 for global register dump */ | ||
454 | NULL, /* y */ | ||
455 | &sysrq_ftrace_dump_op, /* z */ | ||
456 | }; | ||
457 | |||
458 | /* key2index calculation, -1 on invalid index */ | ||
459 | static int sysrq_key_table_key2index(int key) | ||
460 | { | ||
461 | int retval; | ||
462 | |||
463 | if ((key >= '0') && (key <= '9')) | ||
464 | retval = key - '0'; | ||
465 | else if ((key >= 'a') && (key <= 'z')) | ||
466 | retval = key + 10 - 'a'; | ||
467 | else | ||
468 | retval = -1; | ||
469 | return retval; | ||
470 | } | ||
471 | |||
472 | /* | ||
473 | * get and put functions for the table, exposed to modules. | ||
474 | */ | ||
475 | struct sysrq_key_op *__sysrq_get_key_op(int key) | ||
476 | { | ||
477 | struct sysrq_key_op *op_p = NULL; | ||
478 | int i; | ||
479 | |||
480 | i = sysrq_key_table_key2index(key); | ||
481 | if (i != -1) | ||
482 | op_p = sysrq_key_table[i]; | ||
483 | |||
484 | return op_p; | ||
485 | } | ||
486 | |||
487 | static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p) | ||
488 | { | ||
489 | int i = sysrq_key_table_key2index(key); | ||
490 | |||
491 | if (i != -1) | ||
492 | sysrq_key_table[i] = op_p; | ||
493 | } | ||
494 | |||
495 | void __handle_sysrq(int key, bool check_mask) | ||
496 | { | ||
497 | struct sysrq_key_op *op_p; | ||
498 | int orig_log_level; | ||
499 | int i; | ||
500 | unsigned long flags; | ||
501 | |||
502 | spin_lock_irqsave(&sysrq_key_table_lock, flags); | ||
503 | /* | ||
504 | * Raise the apparent loglevel to maximum so that the sysrq header | ||
505 | * is shown to provide the user with positive feedback. We do not | ||
506 | * simply emit this at KERN_EMERG as that would change message | ||
507 | * routing in the consumers of /proc/kmsg. | ||
508 | */ | ||
509 | orig_log_level = console_loglevel; | ||
510 | console_loglevel = 7; | ||
511 | printk(KERN_INFO "SysRq : "); | ||
512 | |||
513 | op_p = __sysrq_get_key_op(key); | ||
514 | if (op_p) { | ||
515 | /* | ||
516 | * Should we check for enabled operations (/proc/sysrq-trigger | ||
517 | * should not) and is the invoked operation enabled? | ||
518 | */ | ||
519 | if (!check_mask || sysrq_on_mask(op_p->enable_mask)) { | ||
520 | printk("%s\n", op_p->action_msg); | ||
521 | console_loglevel = orig_log_level; | ||
522 | op_p->handler(key); | ||
523 | } else { | ||
524 | printk("This sysrq operation is disabled.\n"); | ||
525 | } | ||
526 | } else { | ||
527 | printk("HELP : "); | ||
528 | /* Only print the help msg once per handler */ | ||
529 | for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) { | ||
530 | if (sysrq_key_table[i]) { | ||
531 | int j; | ||
532 | |||
533 | for (j = 0; sysrq_key_table[i] != | ||
534 | sysrq_key_table[j]; j++) | ||
535 | ; | ||
536 | if (j != i) | ||
537 | continue; | ||
538 | printk("%s ", sysrq_key_table[i]->help_msg); | ||
539 | } | ||
540 | } | ||
541 | printk("\n"); | ||
542 | console_loglevel = orig_log_level; | ||
543 | } | ||
544 | spin_unlock_irqrestore(&sysrq_key_table_lock, flags); | ||
545 | } | ||
546 | |||
547 | void handle_sysrq(int key) | ||
548 | { | ||
549 | if (sysrq_on()) | ||
550 | __handle_sysrq(key, true); | ||
551 | } | ||
552 | EXPORT_SYMBOL(handle_sysrq); | ||
553 | |||
554 | #ifdef CONFIG_INPUT | ||
555 | |||
556 | /* Simple translation table for the SysRq keys */ | ||
557 | static const unsigned char sysrq_xlate[KEY_MAX + 1] = | ||
558 | "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */ | ||
559 | "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */ | ||
560 | "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */ | ||
561 | "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */ | ||
562 | "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */ | ||
563 | "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */ | ||
564 | "\r\000/"; /* 0x60 - 0x6f */ | ||
565 | |||
566 | static bool sysrq_down; | ||
567 | static int sysrq_alt_use; | ||
568 | static int sysrq_alt; | ||
569 | static DEFINE_SPINLOCK(sysrq_event_lock); | ||
570 | |||
571 | static bool sysrq_filter(struct input_handle *handle, unsigned int type, | ||
572 | unsigned int code, int value) | ||
573 | { | ||
574 | bool suppress; | ||
575 | |||
576 | /* We are called with interrupts disabled, just take the lock */ | ||
577 | spin_lock(&sysrq_event_lock); | ||
578 | |||
579 | if (type != EV_KEY) | ||
580 | goto out; | ||
581 | |||
582 | switch (code) { | ||
583 | |||
584 | case KEY_LEFTALT: | ||
585 | case KEY_RIGHTALT: | ||
586 | if (value) | ||
587 | sysrq_alt = code; | ||
588 | else { | ||
589 | if (sysrq_down && code == sysrq_alt_use) | ||
590 | sysrq_down = false; | ||
591 | |||
592 | sysrq_alt = 0; | ||
593 | } | ||
594 | break; | ||
595 | |||
596 | case KEY_SYSRQ: | ||
597 | if (value == 1 && sysrq_alt) { | ||
598 | sysrq_down = true; | ||
599 | sysrq_alt_use = sysrq_alt; | ||
600 | } | ||
601 | break; | ||
602 | |||
603 | default: | ||
604 | if (sysrq_down && value && value != 2) | ||
605 | __handle_sysrq(sysrq_xlate[code], true); | ||
606 | break; | ||
607 | } | ||
608 | |||
609 | out: | ||
610 | suppress = sysrq_down; | ||
611 | spin_unlock(&sysrq_event_lock); | ||
612 | |||
613 | return suppress; | ||
614 | } | ||
615 | |||
616 | static int sysrq_connect(struct input_handler *handler, | ||
617 | struct input_dev *dev, | ||
618 | const struct input_device_id *id) | ||
619 | { | ||
620 | struct input_handle *handle; | ||
621 | int error; | ||
622 | |||
623 | sysrq_down = false; | ||
624 | sysrq_alt = 0; | ||
625 | |||
626 | handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL); | ||
627 | if (!handle) | ||
628 | return -ENOMEM; | ||
629 | |||
630 | handle->dev = dev; | ||
631 | handle->handler = handler; | ||
632 | handle->name = "sysrq"; | ||
633 | |||
634 | error = input_register_handle(handle); | ||
635 | if (error) { | ||
636 | pr_err("Failed to register input sysrq handler, error %d\n", | ||
637 | error); | ||
638 | goto err_free; | ||
639 | } | ||
640 | |||
641 | error = input_open_device(handle); | ||
642 | if (error) { | ||
643 | pr_err("Failed to open input device, error %d\n", error); | ||
644 | goto err_unregister; | ||
645 | } | ||
646 | |||
647 | return 0; | ||
648 | |||
649 | err_unregister: | ||
650 | input_unregister_handle(handle); | ||
651 | err_free: | ||
652 | kfree(handle); | ||
653 | return error; | ||
654 | } | ||
655 | |||
656 | static void sysrq_disconnect(struct input_handle *handle) | ||
657 | { | ||
658 | input_close_device(handle); | ||
659 | input_unregister_handle(handle); | ||
660 | kfree(handle); | ||
661 | } | ||
662 | |||
663 | /* | ||
664 | * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all | ||
665 | * keyboards have SysRq key predefined and so user may add it to keymap | ||
666 | * later, but we expect all such keyboards to have left alt. | ||
667 | */ | ||
668 | static const struct input_device_id sysrq_ids[] = { | ||
669 | { | ||
670 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | | ||
671 | INPUT_DEVICE_ID_MATCH_KEYBIT, | ||
672 | .evbit = { BIT_MASK(EV_KEY) }, | ||
673 | .keybit = { BIT_MASK(KEY_LEFTALT) }, | ||
674 | }, | ||
675 | { }, | ||
676 | }; | ||
677 | |||
678 | static struct input_handler sysrq_handler = { | ||
679 | .filter = sysrq_filter, | ||
680 | .connect = sysrq_connect, | ||
681 | .disconnect = sysrq_disconnect, | ||
682 | .name = "sysrq", | ||
683 | .id_table = sysrq_ids, | ||
684 | }; | ||
685 | |||
686 | static bool sysrq_handler_registered; | ||
687 | |||
688 | static inline void sysrq_register_handler(void) | ||
689 | { | ||
690 | int error; | ||
691 | |||
692 | error = input_register_handler(&sysrq_handler); | ||
693 | if (error) | ||
694 | pr_err("Failed to register input handler, error %d", error); | ||
695 | else | ||
696 | sysrq_handler_registered = true; | ||
697 | } | ||
698 | |||
699 | static inline void sysrq_unregister_handler(void) | ||
700 | { | ||
701 | if (sysrq_handler_registered) { | ||
702 | input_unregister_handler(&sysrq_handler); | ||
703 | sysrq_handler_registered = false; | ||
704 | } | ||
705 | } | ||
706 | |||
707 | #else | ||
708 | |||
709 | static inline void sysrq_register_handler(void) | ||
710 | { | ||
711 | } | ||
712 | |||
713 | static inline void sysrq_unregister_handler(void) | ||
714 | { | ||
715 | } | ||
716 | |||
717 | #endif /* CONFIG_INPUT */ | ||
718 | |||
719 | int sysrq_toggle_support(int enable_mask) | ||
720 | { | ||
721 | bool was_enabled = sysrq_on(); | ||
722 | |||
723 | sysrq_enabled = enable_mask; | ||
724 | |||
725 | if (was_enabled != sysrq_on()) { | ||
726 | if (sysrq_on()) | ||
727 | sysrq_register_handler(); | ||
728 | else | ||
729 | sysrq_unregister_handler(); | ||
730 | } | ||
731 | |||
732 | return 0; | ||
733 | } | ||
734 | |||
735 | static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p, | ||
736 | struct sysrq_key_op *remove_op_p) | ||
737 | { | ||
738 | int retval; | ||
739 | unsigned long flags; | ||
740 | |||
741 | spin_lock_irqsave(&sysrq_key_table_lock, flags); | ||
742 | if (__sysrq_get_key_op(key) == remove_op_p) { | ||
743 | __sysrq_put_key_op(key, insert_op_p); | ||
744 | retval = 0; | ||
745 | } else { | ||
746 | retval = -1; | ||
747 | } | ||
748 | spin_unlock_irqrestore(&sysrq_key_table_lock, flags); | ||
749 | return retval; | ||
750 | } | ||
751 | |||
752 | int register_sysrq_key(int key, struct sysrq_key_op *op_p) | ||
753 | { | ||
754 | return __sysrq_swap_key_ops(key, op_p, NULL); | ||
755 | } | ||
756 | EXPORT_SYMBOL(register_sysrq_key); | ||
757 | |||
758 | int unregister_sysrq_key(int key, struct sysrq_key_op *op_p) | ||
759 | { | ||
760 | return __sysrq_swap_key_ops(key, NULL, op_p); | ||
761 | } | ||
762 | EXPORT_SYMBOL(unregister_sysrq_key); | ||
763 | |||
764 | #ifdef CONFIG_PROC_FS | ||
765 | /* | ||
766 | * writing 'C' to /proc/sysrq-trigger is like sysrq-C | ||
767 | */ | ||
768 | static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, | ||
769 | size_t count, loff_t *ppos) | ||
770 | { | ||
771 | if (count) { | ||
772 | char c; | ||
773 | |||
774 | if (get_user(c, buf)) | ||
775 | return -EFAULT; | ||
776 | __handle_sysrq(c, false); | ||
777 | } | ||
778 | |||
779 | return count; | ||
780 | } | ||
781 | |||
782 | static const struct file_operations proc_sysrq_trigger_operations = { | ||
783 | .write = write_sysrq_trigger, | ||
784 | .llseek = noop_llseek, | ||
785 | }; | ||
786 | |||
787 | static void sysrq_init_procfs(void) | ||
788 | { | ||
789 | if (!proc_create("sysrq-trigger", S_IWUSR, NULL, | ||
790 | &proc_sysrq_trigger_operations)) | ||
791 | pr_err("Failed to register proc interface\n"); | ||
792 | } | ||
793 | |||
794 | #else | ||
795 | |||
796 | static inline void sysrq_init_procfs(void) | ||
797 | { | ||
798 | } | ||
799 | |||
800 | #endif /* CONFIG_PROC_FS */ | ||
801 | |||
802 | static int __init sysrq_init(void) | ||
803 | { | ||
804 | sysrq_init_procfs(); | ||
805 | |||
806 | if (sysrq_on()) | ||
807 | sysrq_register_handler(); | ||
808 | |||
809 | return 0; | ||
810 | } | ||
811 | module_init(sysrq_init); | ||
diff --git a/drivers/tty/tty_audit.c b/drivers/tty/tty_audit.c new file mode 100644 index 000000000000..f64582b0f623 --- /dev/null +++ b/drivers/tty/tty_audit.c | |||
@@ -0,0 +1,358 @@ | |||
1 | /* | ||
2 | * Creating audit events from TTY input. | ||
3 | * | ||
4 | * Copyright (C) 2007 Red Hat, Inc. All rights reserved. This copyrighted | ||
5 | * material is made available to anyone wishing to use, modify, copy, or | ||
6 | * redistribute it subject to the terms and conditions of the GNU General | ||
7 | * Public License v.2. | ||
8 | * | ||
9 | * Authors: Miloslav Trmac <mitr@redhat.com> | ||
10 | */ | ||
11 | |||
12 | #include <linux/audit.h> | ||
13 | #include <linux/slab.h> | ||
14 | #include <linux/tty.h> | ||
15 | |||
16 | struct tty_audit_buf { | ||
17 | atomic_t count; | ||
18 | struct mutex mutex; /* Protects all data below */ | ||
19 | int major, minor; /* The TTY which the data is from */ | ||
20 | unsigned icanon:1; | ||
21 | size_t valid; | ||
22 | unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */ | ||
23 | }; | ||
24 | |||
25 | static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor, | ||
26 | int icanon) | ||
27 | { | ||
28 | struct tty_audit_buf *buf; | ||
29 | |||
30 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | ||
31 | if (!buf) | ||
32 | goto err; | ||
33 | buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL); | ||
34 | if (!buf->data) | ||
35 | goto err_buf; | ||
36 | atomic_set(&buf->count, 1); | ||
37 | mutex_init(&buf->mutex); | ||
38 | buf->major = major; | ||
39 | buf->minor = minor; | ||
40 | buf->icanon = icanon; | ||
41 | buf->valid = 0; | ||
42 | return buf; | ||
43 | |||
44 | err_buf: | ||
45 | kfree(buf); | ||
46 | err: | ||
47 | return NULL; | ||
48 | } | ||
49 | |||
50 | static void tty_audit_buf_free(struct tty_audit_buf *buf) | ||
51 | { | ||
52 | WARN_ON(buf->valid != 0); | ||
53 | kfree(buf->data); | ||
54 | kfree(buf); | ||
55 | } | ||
56 | |||
57 | static void tty_audit_buf_put(struct tty_audit_buf *buf) | ||
58 | { | ||
59 | if (atomic_dec_and_test(&buf->count)) | ||
60 | tty_audit_buf_free(buf); | ||
61 | } | ||
62 | |||
63 | static void tty_audit_log(const char *description, struct task_struct *tsk, | ||
64 | uid_t loginuid, unsigned sessionid, int major, | ||
65 | int minor, unsigned char *data, size_t size) | ||
66 | { | ||
67 | struct audit_buffer *ab; | ||
68 | |||
69 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY); | ||
70 | if (ab) { | ||
71 | char name[sizeof(tsk->comm)]; | ||
72 | uid_t uid = task_uid(tsk); | ||
73 | |||
74 | audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u " | ||
75 | "major=%d minor=%d comm=", description, | ||
76 | tsk->pid, uid, loginuid, sessionid, | ||
77 | major, minor); | ||
78 | get_task_comm(name, tsk); | ||
79 | audit_log_untrustedstring(ab, name); | ||
80 | audit_log_format(ab, " data="); | ||
81 | audit_log_n_hex(ab, data, size); | ||
82 | audit_log_end(ab); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * tty_audit_buf_push - Push buffered data out | ||
88 | * | ||
89 | * Generate an audit message from the contents of @buf, which is owned by | ||
90 | * @tsk with @loginuid. @buf->mutex must be locked. | ||
91 | */ | ||
92 | static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid, | ||
93 | unsigned int sessionid, | ||
94 | struct tty_audit_buf *buf) | ||
95 | { | ||
96 | if (buf->valid == 0) | ||
97 | return; | ||
98 | if (audit_enabled == 0) | ||
99 | return; | ||
100 | tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor, | ||
101 | buf->data, buf->valid); | ||
102 | buf->valid = 0; | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * tty_audit_buf_push_current - Push buffered data out | ||
107 | * | ||
108 | * Generate an audit message from the contents of @buf, which is owned by | ||
109 | * the current task. @buf->mutex must be locked. | ||
110 | */ | ||
111 | static void tty_audit_buf_push_current(struct tty_audit_buf *buf) | ||
112 | { | ||
113 | uid_t auid = audit_get_loginuid(current); | ||
114 | unsigned int sessionid = audit_get_sessionid(current); | ||
115 | tty_audit_buf_push(current, auid, sessionid, buf); | ||
116 | } | ||
117 | |||
118 | /** | ||
119 | * tty_audit_exit - Handle a task exit | ||
120 | * | ||
121 | * Make sure all buffered data is written out and deallocate the buffer. | ||
122 | * Only needs to be called if current->signal->tty_audit_buf != %NULL. | ||
123 | */ | ||
124 | void tty_audit_exit(void) | ||
125 | { | ||
126 | struct tty_audit_buf *buf; | ||
127 | |||
128 | spin_lock_irq(¤t->sighand->siglock); | ||
129 | buf = current->signal->tty_audit_buf; | ||
130 | current->signal->tty_audit_buf = NULL; | ||
131 | spin_unlock_irq(¤t->sighand->siglock); | ||
132 | if (!buf) | ||
133 | return; | ||
134 | |||
135 | mutex_lock(&buf->mutex); | ||
136 | tty_audit_buf_push_current(buf); | ||
137 | mutex_unlock(&buf->mutex); | ||
138 | |||
139 | tty_audit_buf_put(buf); | ||
140 | } | ||
141 | |||
142 | /** | ||
143 | * tty_audit_fork - Copy TTY audit state for a new task | ||
144 | * | ||
145 | * Set up TTY audit state in @sig from current. @sig needs no locking. | ||
146 | */ | ||
147 | void tty_audit_fork(struct signal_struct *sig) | ||
148 | { | ||
149 | spin_lock_irq(¤t->sighand->siglock); | ||
150 | sig->audit_tty = current->signal->audit_tty; | ||
151 | spin_unlock_irq(¤t->sighand->siglock); | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * tty_audit_tiocsti - Log TIOCSTI | ||
156 | */ | ||
157 | void tty_audit_tiocsti(struct tty_struct *tty, char ch) | ||
158 | { | ||
159 | struct tty_audit_buf *buf; | ||
160 | int major, minor, should_audit; | ||
161 | |||
162 | spin_lock_irq(¤t->sighand->siglock); | ||
163 | should_audit = current->signal->audit_tty; | ||
164 | buf = current->signal->tty_audit_buf; | ||
165 | if (buf) | ||
166 | atomic_inc(&buf->count); | ||
167 | spin_unlock_irq(¤t->sighand->siglock); | ||
168 | |||
169 | major = tty->driver->major; | ||
170 | minor = tty->driver->minor_start + tty->index; | ||
171 | if (buf) { | ||
172 | mutex_lock(&buf->mutex); | ||
173 | if (buf->major == major && buf->minor == minor) | ||
174 | tty_audit_buf_push_current(buf); | ||
175 | mutex_unlock(&buf->mutex); | ||
176 | tty_audit_buf_put(buf); | ||
177 | } | ||
178 | |||
179 | if (should_audit && audit_enabled) { | ||
180 | uid_t auid; | ||
181 | unsigned int sessionid; | ||
182 | |||
183 | auid = audit_get_loginuid(current); | ||
184 | sessionid = audit_get_sessionid(current); | ||
185 | tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major, | ||
186 | minor, &ch, 1); | ||
187 | } | ||
188 | } | ||
189 | |||
190 | /** | ||
191 | * tty_audit_push_task - Flush task's pending audit data | ||
192 | * @tsk: task pointer | ||
193 | * @loginuid: sender login uid | ||
194 | * @sessionid: sender session id | ||
195 | * | ||
196 | * Called with a ref on @tsk held. Try to lock sighand and get a | ||
197 | * reference to the tty audit buffer if available. | ||
198 | * Flush the buffer or return an appropriate error code. | ||
199 | */ | ||
200 | int tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid) | ||
201 | { | ||
202 | struct tty_audit_buf *buf = ERR_PTR(-EPERM); | ||
203 | unsigned long flags; | ||
204 | |||
205 | if (!lock_task_sighand(tsk, &flags)) | ||
206 | return -ESRCH; | ||
207 | |||
208 | if (tsk->signal->audit_tty) { | ||
209 | buf = tsk->signal->tty_audit_buf; | ||
210 | if (buf) | ||
211 | atomic_inc(&buf->count); | ||
212 | } | ||
213 | unlock_task_sighand(tsk, &flags); | ||
214 | |||
215 | /* | ||
216 | * Return 0 when signal->audit_tty set | ||
217 | * but tsk->signal->tty_audit_buf == NULL. | ||
218 | */ | ||
219 | if (!buf || IS_ERR(buf)) | ||
220 | return PTR_ERR(buf); | ||
221 | |||
222 | mutex_lock(&buf->mutex); | ||
223 | tty_audit_buf_push(tsk, loginuid, sessionid, buf); | ||
224 | mutex_unlock(&buf->mutex); | ||
225 | |||
226 | tty_audit_buf_put(buf); | ||
227 | return 0; | ||
228 | } | ||
229 | |||
230 | /** | ||
231 | * tty_audit_buf_get - Get an audit buffer. | ||
232 | * | ||
233 | * Get an audit buffer for @tty, allocate it if necessary. Return %NULL | ||
234 | * if TTY auditing is disabled or out of memory. Otherwise, return a new | ||
235 | * reference to the buffer. | ||
236 | */ | ||
237 | static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty) | ||
238 | { | ||
239 | struct tty_audit_buf *buf, *buf2; | ||
240 | |||
241 | buf = NULL; | ||
242 | buf2 = NULL; | ||
243 | spin_lock_irq(¤t->sighand->siglock); | ||
244 | if (likely(!current->signal->audit_tty)) | ||
245 | goto out; | ||
246 | buf = current->signal->tty_audit_buf; | ||
247 | if (buf) { | ||
248 | atomic_inc(&buf->count); | ||
249 | goto out; | ||
250 | } | ||
251 | spin_unlock_irq(¤t->sighand->siglock); | ||
252 | |||
253 | buf2 = tty_audit_buf_alloc(tty->driver->major, | ||
254 | tty->driver->minor_start + tty->index, | ||
255 | tty->icanon); | ||
256 | if (buf2 == NULL) { | ||
257 | audit_log_lost("out of memory in TTY auditing"); | ||
258 | return NULL; | ||
259 | } | ||
260 | |||
261 | spin_lock_irq(¤t->sighand->siglock); | ||
262 | if (!current->signal->audit_tty) | ||
263 | goto out; | ||
264 | buf = current->signal->tty_audit_buf; | ||
265 | if (!buf) { | ||
266 | current->signal->tty_audit_buf = buf2; | ||
267 | buf = buf2; | ||
268 | buf2 = NULL; | ||
269 | } | ||
270 | atomic_inc(&buf->count); | ||
271 | /* Fall through */ | ||
272 | out: | ||
273 | spin_unlock_irq(¤t->sighand->siglock); | ||
274 | if (buf2) | ||
275 | tty_audit_buf_free(buf2); | ||
276 | return buf; | ||
277 | } | ||
278 | |||
279 | /** | ||
280 | * tty_audit_add_data - Add data for TTY auditing. | ||
281 | * | ||
282 | * Audit @data of @size from @tty, if necessary. | ||
283 | */ | ||
284 | void tty_audit_add_data(struct tty_struct *tty, unsigned char *data, | ||
285 | size_t size) | ||
286 | { | ||
287 | struct tty_audit_buf *buf; | ||
288 | int major, minor; | ||
289 | |||
290 | if (unlikely(size == 0)) | ||
291 | return; | ||
292 | |||
293 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY | ||
294 | && tty->driver->subtype == PTY_TYPE_MASTER) | ||
295 | return; | ||
296 | |||
297 | buf = tty_audit_buf_get(tty); | ||
298 | if (!buf) | ||
299 | return; | ||
300 | |||
301 | mutex_lock(&buf->mutex); | ||
302 | major = tty->driver->major; | ||
303 | minor = tty->driver->minor_start + tty->index; | ||
304 | if (buf->major != major || buf->minor != minor | ||
305 | || buf->icanon != tty->icanon) { | ||
306 | tty_audit_buf_push_current(buf); | ||
307 | buf->major = major; | ||
308 | buf->minor = minor; | ||
309 | buf->icanon = tty->icanon; | ||
310 | } | ||
311 | do { | ||
312 | size_t run; | ||
313 | |||
314 | run = N_TTY_BUF_SIZE - buf->valid; | ||
315 | if (run > size) | ||
316 | run = size; | ||
317 | memcpy(buf->data + buf->valid, data, run); | ||
318 | buf->valid += run; | ||
319 | data += run; | ||
320 | size -= run; | ||
321 | if (buf->valid == N_TTY_BUF_SIZE) | ||
322 | tty_audit_buf_push_current(buf); | ||
323 | } while (size != 0); | ||
324 | mutex_unlock(&buf->mutex); | ||
325 | tty_audit_buf_put(buf); | ||
326 | } | ||
327 | |||
328 | /** | ||
329 | * tty_audit_push - Push buffered data out | ||
330 | * | ||
331 | * Make sure no audit data is pending for @tty on the current process. | ||
332 | */ | ||
333 | void tty_audit_push(struct tty_struct *tty) | ||
334 | { | ||
335 | struct tty_audit_buf *buf; | ||
336 | |||
337 | spin_lock_irq(¤t->sighand->siglock); | ||
338 | if (likely(!current->signal->audit_tty)) { | ||
339 | spin_unlock_irq(¤t->sighand->siglock); | ||
340 | return; | ||
341 | } | ||
342 | buf = current->signal->tty_audit_buf; | ||
343 | if (buf) | ||
344 | atomic_inc(&buf->count); | ||
345 | spin_unlock_irq(¤t->sighand->siglock); | ||
346 | |||
347 | if (buf) { | ||
348 | int major, minor; | ||
349 | |||
350 | major = tty->driver->major; | ||
351 | minor = tty->driver->minor_start + tty->index; | ||
352 | mutex_lock(&buf->mutex); | ||
353 | if (buf->major == major && buf->minor == minor) | ||
354 | tty_audit_buf_push_current(buf); | ||
355 | mutex_unlock(&buf->mutex); | ||
356 | tty_audit_buf_put(buf); | ||
357 | } | ||
358 | } | ||
diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c new file mode 100644 index 000000000000..d8210ca00720 --- /dev/null +++ b/drivers/tty/tty_buffer.c | |||
@@ -0,0 +1,534 @@ | |||
1 | /* | ||
2 | * Tty buffer allocation management | ||
3 | */ | ||
4 | |||
5 | #include <linux/types.h> | ||
6 | #include <linux/errno.h> | ||
7 | #include <linux/tty.h> | ||
8 | #include <linux/tty_driver.h> | ||
9 | #include <linux/tty_flip.h> | ||
10 | #include <linux/timer.h> | ||
11 | #include <linux/string.h> | ||
12 | #include <linux/slab.h> | ||
13 | #include <linux/sched.h> | ||
14 | #include <linux/init.h> | ||
15 | #include <linux/wait.h> | ||
16 | #include <linux/bitops.h> | ||
17 | #include <linux/delay.h> | ||
18 | #include <linux/module.h> | ||
19 | |||
20 | /** | ||
21 | * tty_buffer_free_all - free buffers used by a tty | ||
22 | * @tty: tty to free from | ||
23 | * | ||
24 | * Remove all the buffers pending on a tty whether queued with data | ||
25 | * or in the free ring. Must be called when the tty is no longer in use | ||
26 | * | ||
27 | * Locking: none | ||
28 | */ | ||
29 | |||
30 | void tty_buffer_free_all(struct tty_struct *tty) | ||
31 | { | ||
32 | struct tty_buffer *thead; | ||
33 | while ((thead = tty->buf.head) != NULL) { | ||
34 | tty->buf.head = thead->next; | ||
35 | kfree(thead); | ||
36 | } | ||
37 | while ((thead = tty->buf.free) != NULL) { | ||
38 | tty->buf.free = thead->next; | ||
39 | kfree(thead); | ||
40 | } | ||
41 | tty->buf.tail = NULL; | ||
42 | tty->buf.memory_used = 0; | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * tty_buffer_alloc - allocate a tty buffer | ||
47 | * @tty: tty device | ||
48 | * @size: desired size (characters) | ||
49 | * | ||
50 | * Allocate a new tty buffer to hold the desired number of characters. | ||
51 | * Return NULL if out of memory or the allocation would exceed the | ||
52 | * per device queue | ||
53 | * | ||
54 | * Locking: Caller must hold tty->buf.lock | ||
55 | */ | ||
56 | |||
57 | static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size) | ||
58 | { | ||
59 | struct tty_buffer *p; | ||
60 | |||
61 | if (tty->buf.memory_used + size > 65536) | ||
62 | return NULL; | ||
63 | p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC); | ||
64 | if (p == NULL) | ||
65 | return NULL; | ||
66 | p->used = 0; | ||
67 | p->size = size; | ||
68 | p->next = NULL; | ||
69 | p->commit = 0; | ||
70 | p->read = 0; | ||
71 | p->char_buf_ptr = (char *)(p->data); | ||
72 | p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size; | ||
73 | tty->buf.memory_used += size; | ||
74 | return p; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * tty_buffer_free - free a tty buffer | ||
79 | * @tty: tty owning the buffer | ||
80 | * @b: the buffer to free | ||
81 | * | ||
82 | * Free a tty buffer, or add it to the free list according to our | ||
83 | * internal strategy | ||
84 | * | ||
85 | * Locking: Caller must hold tty->buf.lock | ||
86 | */ | ||
87 | |||
88 | static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b) | ||
89 | { | ||
90 | /* Dumb strategy for now - should keep some stats */ | ||
91 | tty->buf.memory_used -= b->size; | ||
92 | WARN_ON(tty->buf.memory_used < 0); | ||
93 | |||
94 | if (b->size >= 512) | ||
95 | kfree(b); | ||
96 | else { | ||
97 | b->next = tty->buf.free; | ||
98 | tty->buf.free = b; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * __tty_buffer_flush - flush full tty buffers | ||
104 | * @tty: tty to flush | ||
105 | * | ||
106 | * flush all the buffers containing receive data. Caller must | ||
107 | * hold the buffer lock and must have ensured no parallel flush to | ||
108 | * ldisc is running. | ||
109 | * | ||
110 | * Locking: Caller must hold tty->buf.lock | ||
111 | */ | ||
112 | |||
113 | static void __tty_buffer_flush(struct tty_struct *tty) | ||
114 | { | ||
115 | struct tty_buffer *thead; | ||
116 | |||
117 | while ((thead = tty->buf.head) != NULL) { | ||
118 | tty->buf.head = thead->next; | ||
119 | tty_buffer_free(tty, thead); | ||
120 | } | ||
121 | tty->buf.tail = NULL; | ||
122 | } | ||
123 | |||
124 | /** | ||
125 | * tty_buffer_flush - flush full tty buffers | ||
126 | * @tty: tty to flush | ||
127 | * | ||
128 | * flush all the buffers containing receive data. If the buffer is | ||
129 | * being processed by flush_to_ldisc then we defer the processing | ||
130 | * to that function | ||
131 | * | ||
132 | * Locking: none | ||
133 | */ | ||
134 | |||
135 | void tty_buffer_flush(struct tty_struct *tty) | ||
136 | { | ||
137 | unsigned long flags; | ||
138 | spin_lock_irqsave(&tty->buf.lock, flags); | ||
139 | |||
140 | /* If the data is being pushed to the tty layer then we can't | ||
141 | process it here. Instead set a flag and the flush_to_ldisc | ||
142 | path will process the flush request before it exits */ | ||
143 | if (test_bit(TTY_FLUSHING, &tty->flags)) { | ||
144 | set_bit(TTY_FLUSHPENDING, &tty->flags); | ||
145 | spin_unlock_irqrestore(&tty->buf.lock, flags); | ||
146 | wait_event(tty->read_wait, | ||
147 | test_bit(TTY_FLUSHPENDING, &tty->flags) == 0); | ||
148 | return; | ||
149 | } else | ||
150 | __tty_buffer_flush(tty); | ||
151 | spin_unlock_irqrestore(&tty->buf.lock, flags); | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * tty_buffer_find - find a free tty buffer | ||
156 | * @tty: tty owning the buffer | ||
157 | * @size: characters wanted | ||
158 | * | ||
159 | * Locate an existing suitable tty buffer or if we are lacking one then | ||
160 | * allocate a new one. We round our buffers off in 256 character chunks | ||
161 | * to get better allocation behaviour. | ||
162 | * | ||
163 | * Locking: Caller must hold tty->buf.lock | ||
164 | */ | ||
165 | |||
166 | static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size) | ||
167 | { | ||
168 | struct tty_buffer **tbh = &tty->buf.free; | ||
169 | while ((*tbh) != NULL) { | ||
170 | struct tty_buffer *t = *tbh; | ||
171 | if (t->size >= size) { | ||
172 | *tbh = t->next; | ||
173 | t->next = NULL; | ||
174 | t->used = 0; | ||
175 | t->commit = 0; | ||
176 | t->read = 0; | ||
177 | tty->buf.memory_used += t->size; | ||
178 | return t; | ||
179 | } | ||
180 | tbh = &((*tbh)->next); | ||
181 | } | ||
182 | /* Round the buffer size out */ | ||
183 | size = (size + 0xFF) & ~0xFF; | ||
184 | return tty_buffer_alloc(tty, size); | ||
185 | /* Should possibly check if this fails for the largest buffer we | ||
186 | have queued and recycle that ? */ | ||
187 | } | ||
188 | |||
189 | /** | ||
190 | * tty_buffer_request_room - grow tty buffer if needed | ||
191 | * @tty: tty structure | ||
192 | * @size: size desired | ||
193 | * | ||
194 | * Make at least size bytes of linear space available for the tty | ||
195 | * buffer. If we fail return the size we managed to find. | ||
196 | * | ||
197 | * Locking: Takes tty->buf.lock | ||
198 | */ | ||
199 | int tty_buffer_request_room(struct tty_struct *tty, size_t size) | ||
200 | { | ||
201 | struct tty_buffer *b, *n; | ||
202 | int left; | ||
203 | unsigned long flags; | ||
204 | |||
205 | spin_lock_irqsave(&tty->buf.lock, flags); | ||
206 | |||
207 | /* OPTIMISATION: We could keep a per tty "zero" sized buffer to | ||
208 | remove this conditional if its worth it. This would be invisible | ||
209 | to the callers */ | ||
210 | if ((b = tty->buf.tail) != NULL) | ||
211 | left = b->size - b->used; | ||
212 | else | ||
213 | left = 0; | ||
214 | |||
215 | if (left < size) { | ||
216 | /* This is the slow path - looking for new buffers to use */ | ||
217 | if ((n = tty_buffer_find(tty, size)) != NULL) { | ||
218 | if (b != NULL) { | ||
219 | b->next = n; | ||
220 | b->commit = b->used; | ||
221 | } else | ||
222 | tty->buf.head = n; | ||
223 | tty->buf.tail = n; | ||
224 | } else | ||
225 | size = left; | ||
226 | } | ||
227 | |||
228 | spin_unlock_irqrestore(&tty->buf.lock, flags); | ||
229 | return size; | ||
230 | } | ||
231 | EXPORT_SYMBOL_GPL(tty_buffer_request_room); | ||
232 | |||
233 | /** | ||
234 | * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer | ||
235 | * @tty: tty structure | ||
236 | * @chars: characters | ||
237 | * @flag: flag value for each character | ||
238 | * @size: size | ||
239 | * | ||
240 | * Queue a series of bytes to the tty buffering. All the characters | ||
241 | * passed are marked with the supplied flag. Returns the number added. | ||
242 | * | ||
243 | * Locking: Called functions may take tty->buf.lock | ||
244 | */ | ||
245 | |||
246 | int tty_insert_flip_string_fixed_flag(struct tty_struct *tty, | ||
247 | const unsigned char *chars, char flag, size_t size) | ||
248 | { | ||
249 | int copied = 0; | ||
250 | do { | ||
251 | int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); | ||
252 | int space = tty_buffer_request_room(tty, goal); | ||
253 | struct tty_buffer *tb = tty->buf.tail; | ||
254 | /* If there is no space then tb may be NULL */ | ||
255 | if (unlikely(space == 0)) | ||
256 | break; | ||
257 | memcpy(tb->char_buf_ptr + tb->used, chars, space); | ||
258 | memset(tb->flag_buf_ptr + tb->used, flag, space); | ||
259 | tb->used += space; | ||
260 | copied += space; | ||
261 | chars += space; | ||
262 | /* There is a small chance that we need to split the data over | ||
263 | several buffers. If this is the case we must loop */ | ||
264 | } while (unlikely(size > copied)); | ||
265 | return copied; | ||
266 | } | ||
267 | EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag); | ||
268 | |||
269 | /** | ||
270 | * tty_insert_flip_string_flags - Add characters to the tty buffer | ||
271 | * @tty: tty structure | ||
272 | * @chars: characters | ||
273 | * @flags: flag bytes | ||
274 | * @size: size | ||
275 | * | ||
276 | * Queue a series of bytes to the tty buffering. For each character | ||
277 | * the flags array indicates the status of the character. Returns the | ||
278 | * number added. | ||
279 | * | ||
280 | * Locking: Called functions may take tty->buf.lock | ||
281 | */ | ||
282 | |||
283 | int tty_insert_flip_string_flags(struct tty_struct *tty, | ||
284 | const unsigned char *chars, const char *flags, size_t size) | ||
285 | { | ||
286 | int copied = 0; | ||
287 | do { | ||
288 | int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); | ||
289 | int space = tty_buffer_request_room(tty, goal); | ||
290 | struct tty_buffer *tb = tty->buf.tail; | ||
291 | /* If there is no space then tb may be NULL */ | ||
292 | if (unlikely(space == 0)) | ||
293 | break; | ||
294 | memcpy(tb->char_buf_ptr + tb->used, chars, space); | ||
295 | memcpy(tb->flag_buf_ptr + tb->used, flags, space); | ||
296 | tb->used += space; | ||
297 | copied += space; | ||
298 | chars += space; | ||
299 | flags += space; | ||
300 | /* There is a small chance that we need to split the data over | ||
301 | several buffers. If this is the case we must loop */ | ||
302 | } while (unlikely(size > copied)); | ||
303 | return copied; | ||
304 | } | ||
305 | EXPORT_SYMBOL(tty_insert_flip_string_flags); | ||
306 | |||
307 | /** | ||
308 | * tty_schedule_flip - push characters to ldisc | ||
309 | * @tty: tty to push from | ||
310 | * | ||
311 | * Takes any pending buffers and transfers their ownership to the | ||
312 | * ldisc side of the queue. It then schedules those characters for | ||
313 | * processing by the line discipline. | ||
314 | * | ||
315 | * Locking: Takes tty->buf.lock | ||
316 | */ | ||
317 | |||
318 | void tty_schedule_flip(struct tty_struct *tty) | ||
319 | { | ||
320 | unsigned long flags; | ||
321 | spin_lock_irqsave(&tty->buf.lock, flags); | ||
322 | if (tty->buf.tail != NULL) | ||
323 | tty->buf.tail->commit = tty->buf.tail->used; | ||
324 | spin_unlock_irqrestore(&tty->buf.lock, flags); | ||
325 | schedule_delayed_work(&tty->buf.work, 1); | ||
326 | } | ||
327 | EXPORT_SYMBOL(tty_schedule_flip); | ||
328 | |||
329 | /** | ||
330 | * tty_prepare_flip_string - make room for characters | ||
331 | * @tty: tty | ||
332 | * @chars: return pointer for character write area | ||
333 | * @size: desired size | ||
334 | * | ||
335 | * Prepare a block of space in the buffer for data. Returns the length | ||
336 | * available and buffer pointer to the space which is now allocated and | ||
337 | * accounted for as ready for normal characters. This is used for drivers | ||
338 | * that need their own block copy routines into the buffer. There is no | ||
339 | * guarantee the buffer is a DMA target! | ||
340 | * | ||
341 | * Locking: May call functions taking tty->buf.lock | ||
342 | */ | ||
343 | |||
344 | int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, | ||
345 | size_t size) | ||
346 | { | ||
347 | int space = tty_buffer_request_room(tty, size); | ||
348 | if (likely(space)) { | ||
349 | struct tty_buffer *tb = tty->buf.tail; | ||
350 | *chars = tb->char_buf_ptr + tb->used; | ||
351 | memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); | ||
352 | tb->used += space; | ||
353 | } | ||
354 | return space; | ||
355 | } | ||
356 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string); | ||
357 | |||
358 | /** | ||
359 | * tty_prepare_flip_string_flags - make room for characters | ||
360 | * @tty: tty | ||
361 | * @chars: return pointer for character write area | ||
362 | * @flags: return pointer for status flag write area | ||
363 | * @size: desired size | ||
364 | * | ||
365 | * Prepare a block of space in the buffer for data. Returns the length | ||
366 | * available and buffer pointer to the space which is now allocated and | ||
367 | * accounted for as ready for characters. This is used for drivers | ||
368 | * that need their own block copy routines into the buffer. There is no | ||
369 | * guarantee the buffer is a DMA target! | ||
370 | * | ||
371 | * Locking: May call functions taking tty->buf.lock | ||
372 | */ | ||
373 | |||
374 | int tty_prepare_flip_string_flags(struct tty_struct *tty, | ||
375 | unsigned char **chars, char **flags, size_t size) | ||
376 | { | ||
377 | int space = tty_buffer_request_room(tty, size); | ||
378 | if (likely(space)) { | ||
379 | struct tty_buffer *tb = tty->buf.tail; | ||
380 | *chars = tb->char_buf_ptr + tb->used; | ||
381 | *flags = tb->flag_buf_ptr + tb->used; | ||
382 | tb->used += space; | ||
383 | } | ||
384 | return space; | ||
385 | } | ||
386 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags); | ||
387 | |||
388 | |||
389 | |||
390 | /** | ||
391 | * flush_to_ldisc | ||
392 | * @work: tty structure passed from work queue. | ||
393 | * | ||
394 | * This routine is called out of the software interrupt to flush data | ||
395 | * from the buffer chain to the line discipline. | ||
396 | * | ||
397 | * Locking: holds tty->buf.lock to guard buffer list. Drops the lock | ||
398 | * while invoking the line discipline receive_buf method. The | ||
399 | * receive_buf method is single threaded for each tty instance. | ||
400 | */ | ||
401 | |||
402 | static void flush_to_ldisc(struct work_struct *work) | ||
403 | { | ||
404 | struct tty_struct *tty = | ||
405 | container_of(work, struct tty_struct, buf.work.work); | ||
406 | unsigned long flags; | ||
407 | struct tty_ldisc *disc; | ||
408 | |||
409 | disc = tty_ldisc_ref(tty); | ||
410 | if (disc == NULL) /* !TTY_LDISC */ | ||
411 | return; | ||
412 | |||
413 | spin_lock_irqsave(&tty->buf.lock, flags); | ||
414 | |||
415 | if (!test_and_set_bit(TTY_FLUSHING, &tty->flags)) { | ||
416 | struct tty_buffer *head, *tail = tty->buf.tail; | ||
417 | int seen_tail = 0; | ||
418 | while ((head = tty->buf.head) != NULL) { | ||
419 | int count; | ||
420 | char *char_buf; | ||
421 | unsigned char *flag_buf; | ||
422 | |||
423 | count = head->commit - head->read; | ||
424 | if (!count) { | ||
425 | if (head->next == NULL) | ||
426 | break; | ||
427 | /* | ||
428 | There's a possibility tty might get new buffer | ||
429 | added during the unlock window below. We could | ||
430 | end up spinning in here forever hogging the CPU | ||
431 | completely. To avoid this let's have a rest each | ||
432 | time we processed the tail buffer. | ||
433 | */ | ||
434 | if (tail == head) | ||
435 | seen_tail = 1; | ||
436 | tty->buf.head = head->next; | ||
437 | tty_buffer_free(tty, head); | ||
438 | continue; | ||
439 | } | ||
440 | /* Ldisc or user is trying to flush the buffers | ||
441 | we are feeding to the ldisc, stop feeding the | ||
442 | line discipline as we want to empty the queue */ | ||
443 | if (test_bit(TTY_FLUSHPENDING, &tty->flags)) | ||
444 | break; | ||
445 | if (!tty->receive_room || seen_tail) { | ||
446 | schedule_delayed_work(&tty->buf.work, 1); | ||
447 | break; | ||
448 | } | ||
449 | if (count > tty->receive_room) | ||
450 | count = tty->receive_room; | ||
451 | char_buf = head->char_buf_ptr + head->read; | ||
452 | flag_buf = head->flag_buf_ptr + head->read; | ||
453 | head->read += count; | ||
454 | spin_unlock_irqrestore(&tty->buf.lock, flags); | ||
455 | disc->ops->receive_buf(tty, char_buf, | ||
456 | flag_buf, count); | ||
457 | spin_lock_irqsave(&tty->buf.lock, flags); | ||
458 | } | ||
459 | clear_bit(TTY_FLUSHING, &tty->flags); | ||
460 | } | ||
461 | |||
462 | /* We may have a deferred request to flush the input buffer, | ||
463 | if so pull the chain under the lock and empty the queue */ | ||
464 | if (test_bit(TTY_FLUSHPENDING, &tty->flags)) { | ||
465 | __tty_buffer_flush(tty); | ||
466 | clear_bit(TTY_FLUSHPENDING, &tty->flags); | ||
467 | wake_up(&tty->read_wait); | ||
468 | } | ||
469 | spin_unlock_irqrestore(&tty->buf.lock, flags); | ||
470 | |||
471 | tty_ldisc_deref(disc); | ||
472 | } | ||
473 | |||
474 | /** | ||
475 | * tty_flush_to_ldisc | ||
476 | * @tty: tty to push | ||
477 | * | ||
478 | * Push the terminal flip buffers to the line discipline. | ||
479 | * | ||
480 | * Must not be called from IRQ context. | ||
481 | */ | ||
482 | void tty_flush_to_ldisc(struct tty_struct *tty) | ||
483 | { | ||
484 | flush_delayed_work(&tty->buf.work); | ||
485 | } | ||
486 | |||
487 | /** | ||
488 | * tty_flip_buffer_push - terminal | ||
489 | * @tty: tty to push | ||
490 | * | ||
491 | * Queue a push of the terminal flip buffers to the line discipline. This | ||
492 | * function must not be called from IRQ context if tty->low_latency is set. | ||
493 | * | ||
494 | * In the event of the queue being busy for flipping the work will be | ||
495 | * held off and retried later. | ||
496 | * | ||
497 | * Locking: tty buffer lock. Driver locks in low latency mode. | ||
498 | */ | ||
499 | |||
500 | void tty_flip_buffer_push(struct tty_struct *tty) | ||
501 | { | ||
502 | unsigned long flags; | ||
503 | spin_lock_irqsave(&tty->buf.lock, flags); | ||
504 | if (tty->buf.tail != NULL) | ||
505 | tty->buf.tail->commit = tty->buf.tail->used; | ||
506 | spin_unlock_irqrestore(&tty->buf.lock, flags); | ||
507 | |||
508 | if (tty->low_latency) | ||
509 | flush_to_ldisc(&tty->buf.work.work); | ||
510 | else | ||
511 | schedule_delayed_work(&tty->buf.work, 1); | ||
512 | } | ||
513 | EXPORT_SYMBOL(tty_flip_buffer_push); | ||
514 | |||
515 | /** | ||
516 | * tty_buffer_init - prepare a tty buffer structure | ||
517 | * @tty: tty to initialise | ||
518 | * | ||
519 | * Set up the initial state of the buffer management for a tty device. | ||
520 | * Must be called before the other tty buffer functions are used. | ||
521 | * | ||
522 | * Locking: none | ||
523 | */ | ||
524 | |||
525 | void tty_buffer_init(struct tty_struct *tty) | ||
526 | { | ||
527 | spin_lock_init(&tty->buf.lock); | ||
528 | tty->buf.head = NULL; | ||
529 | tty->buf.tail = NULL; | ||
530 | tty->buf.free = NULL; | ||
531 | tty->buf.memory_used = 0; | ||
532 | INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc); | ||
533 | } | ||
534 | |||
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c new file mode 100644 index 000000000000..c05c5af5aa04 --- /dev/null +++ b/drivers/tty/tty_io.c | |||
@@ -0,0 +1,3263 @@ | |||
1 | /* | ||
2 | * linux/drivers/char/tty_io.c | ||
3 | * | ||
4 | * Copyright (C) 1991, 1992 Linus Torvalds | ||
5 | */ | ||
6 | |||
7 | /* | ||
8 | * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles | ||
9 | * or rs-channels. It also implements echoing, cooked mode etc. | ||
10 | * | ||
11 | * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0. | ||
12 | * | ||
13 | * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the | ||
14 | * tty_struct and tty_queue structures. Previously there was an array | ||
15 | * of 256 tty_struct's which was statically allocated, and the | ||
16 | * tty_queue structures were allocated at boot time. Both are now | ||
17 | * dynamically allocated only when the tty is open. | ||
18 | * | ||
19 | * Also restructured routines so that there is more of a separation | ||
20 | * between the high-level tty routines (tty_io.c and tty_ioctl.c) and | ||
21 | * the low-level tty routines (serial.c, pty.c, console.c). This | ||
22 | * makes for cleaner and more compact code. -TYT, 9/17/92 | ||
23 | * | ||
24 | * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines | ||
25 | * which can be dynamically activated and de-activated by the line | ||
26 | * discipline handling modules (like SLIP). | ||
27 | * | ||
28 | * NOTE: pay no attention to the line discipline code (yet); its | ||
29 | * interface is still subject to change in this version... | ||
30 | * -- TYT, 1/31/92 | ||
31 | * | ||
32 | * Added functionality to the OPOST tty handling. No delays, but all | ||
33 | * other bits should be there. | ||
34 | * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993. | ||
35 | * | ||
36 | * Rewrote canonical mode and added more termios flags. | ||
37 | * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94 | ||
38 | * | ||
39 | * Reorganized FASYNC support so mouse code can share it. | ||
40 | * -- ctm@ardi.com, 9Sep95 | ||
41 | * | ||
42 | * New TIOCLINUX variants added. | ||
43 | * -- mj@k332.feld.cvut.cz, 19-Nov-95 | ||
44 | * | ||
45 | * Restrict vt switching via ioctl() | ||
46 | * -- grif@cs.ucr.edu, 5-Dec-95 | ||
47 | * | ||
48 | * Move console and virtual terminal code to more appropriate files, | ||
49 | * implement CONFIG_VT and generalize console device interface. | ||
50 | * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97 | ||
51 | * | ||
52 | * Rewrote tty_init_dev and tty_release_dev to eliminate races. | ||
53 | * -- Bill Hawes <whawes@star.net>, June 97 | ||
54 | * | ||
55 | * Added devfs support. | ||
56 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998 | ||
57 | * | ||
58 | * Added support for a Unix98-style ptmx device. | ||
59 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 | ||
60 | * | ||
61 | * Reduced memory usage for older ARM systems | ||
62 | * -- Russell King <rmk@arm.linux.org.uk> | ||
63 | * | ||
64 | * Move do_SAK() into process context. Less stack use in devfs functions. | ||
65 | * alloc_tty_struct() always uses kmalloc() | ||
66 | * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01 | ||
67 | */ | ||
68 | |||
69 | #include <linux/types.h> | ||
70 | #include <linux/major.h> | ||
71 | #include <linux/errno.h> | ||
72 | #include <linux/signal.h> | ||
73 | #include <linux/fcntl.h> | ||
74 | #include <linux/sched.h> | ||
75 | #include <linux/interrupt.h> | ||
76 | #include <linux/tty.h> | ||
77 | #include <linux/tty_driver.h> | ||
78 | #include <linux/tty_flip.h> | ||
79 | #include <linux/devpts_fs.h> | ||
80 | #include <linux/file.h> | ||
81 | #include <linux/fdtable.h> | ||
82 | #include <linux/console.h> | ||
83 | #include <linux/timer.h> | ||
84 | #include <linux/ctype.h> | ||
85 | #include <linux/kd.h> | ||
86 | #include <linux/mm.h> | ||
87 | #include <linux/string.h> | ||
88 | #include <linux/slab.h> | ||
89 | #include <linux/poll.h> | ||
90 | #include <linux/proc_fs.h> | ||
91 | #include <linux/init.h> | ||
92 | #include <linux/module.h> | ||
93 | #include <linux/smp_lock.h> | ||
94 | #include <linux/device.h> | ||
95 | #include <linux/wait.h> | ||
96 | #include <linux/bitops.h> | ||
97 | #include <linux/delay.h> | ||
98 | #include <linux/seq_file.h> | ||
99 | #include <linux/serial.h> | ||
100 | |||
101 | #include <linux/uaccess.h> | ||
102 | #include <asm/system.h> | ||
103 | |||
104 | #include <linux/kbd_kern.h> | ||
105 | #include <linux/vt_kern.h> | ||
106 | #include <linux/selection.h> | ||
107 | |||
108 | #include <linux/kmod.h> | ||
109 | #include <linux/nsproxy.h> | ||
110 | |||
111 | #undef TTY_DEBUG_HANGUP | ||
112 | |||
113 | #define TTY_PARANOIA_CHECK 1 | ||
114 | #define CHECK_TTY_COUNT 1 | ||
115 | |||
116 | struct ktermios tty_std_termios = { /* for the benefit of tty drivers */ | ||
117 | .c_iflag = ICRNL | IXON, | ||
118 | .c_oflag = OPOST | ONLCR, | ||
119 | .c_cflag = B38400 | CS8 | CREAD | HUPCL, | ||
120 | .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | | ||
121 | ECHOCTL | ECHOKE | IEXTEN, | ||
122 | .c_cc = INIT_C_CC, | ||
123 | .c_ispeed = 38400, | ||
124 | .c_ospeed = 38400 | ||
125 | }; | ||
126 | |||
127 | EXPORT_SYMBOL(tty_std_termios); | ||
128 | |||
129 | /* This list gets poked at by procfs and various bits of boot up code. This | ||
130 | could do with some rationalisation such as pulling the tty proc function | ||
131 | into this file */ | ||
132 | |||
133 | LIST_HEAD(tty_drivers); /* linked list of tty drivers */ | ||
134 | |||
135 | /* Mutex to protect creating and releasing a tty. This is shared with | ||
136 | vt.c for deeply disgusting hack reasons */ | ||
137 | DEFINE_MUTEX(tty_mutex); | ||
138 | EXPORT_SYMBOL(tty_mutex); | ||
139 | |||
140 | /* Spinlock to protect the tty->tty_files list */ | ||
141 | DEFINE_SPINLOCK(tty_files_lock); | ||
142 | |||
143 | static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *); | ||
144 | static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *); | ||
145 | ssize_t redirected_tty_write(struct file *, const char __user *, | ||
146 | size_t, loff_t *); | ||
147 | static unsigned int tty_poll(struct file *, poll_table *); | ||
148 | static int tty_open(struct inode *, struct file *); | ||
149 | long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); | ||
150 | #ifdef CONFIG_COMPAT | ||
151 | static long tty_compat_ioctl(struct file *file, unsigned int cmd, | ||
152 | unsigned long arg); | ||
153 | #else | ||
154 | #define tty_compat_ioctl NULL | ||
155 | #endif | ||
156 | static int __tty_fasync(int fd, struct file *filp, int on); | ||
157 | static int tty_fasync(int fd, struct file *filp, int on); | ||
158 | static void release_tty(struct tty_struct *tty, int idx); | ||
159 | static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); | ||
160 | static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); | ||
161 | |||
162 | /** | ||
163 | * alloc_tty_struct - allocate a tty object | ||
164 | * | ||
165 | * Return a new empty tty structure. The data fields have not | ||
166 | * been initialized in any way but has been zeroed | ||
167 | * | ||
168 | * Locking: none | ||
169 | */ | ||
170 | |||
171 | struct tty_struct *alloc_tty_struct(void) | ||
172 | { | ||
173 | return kzalloc(sizeof(struct tty_struct), GFP_KERNEL); | ||
174 | } | ||
175 | |||
176 | /** | ||
177 | * free_tty_struct - free a disused tty | ||
178 | * @tty: tty struct to free | ||
179 | * | ||
180 | * Free the write buffers, tty queue and tty memory itself. | ||
181 | * | ||
182 | * Locking: none. Must be called after tty is definitely unused | ||
183 | */ | ||
184 | |||
185 | void free_tty_struct(struct tty_struct *tty) | ||
186 | { | ||
187 | if (tty->dev) | ||
188 | put_device(tty->dev); | ||
189 | kfree(tty->write_buf); | ||
190 | tty_buffer_free_all(tty); | ||
191 | kfree(tty); | ||
192 | } | ||
193 | |||
194 | static inline struct tty_struct *file_tty(struct file *file) | ||
195 | { | ||
196 | return ((struct tty_file_private *)file->private_data)->tty; | ||
197 | } | ||
198 | |||
199 | /* Associate a new file with the tty structure */ | ||
200 | int tty_add_file(struct tty_struct *tty, struct file *file) | ||
201 | { | ||
202 | struct tty_file_private *priv; | ||
203 | |||
204 | priv = kmalloc(sizeof(*priv), GFP_KERNEL); | ||
205 | if (!priv) | ||
206 | return -ENOMEM; | ||
207 | |||
208 | priv->tty = tty; | ||
209 | priv->file = file; | ||
210 | file->private_data = priv; | ||
211 | |||
212 | spin_lock(&tty_files_lock); | ||
213 | list_add(&priv->list, &tty->tty_files); | ||
214 | spin_unlock(&tty_files_lock); | ||
215 | |||
216 | return 0; | ||
217 | } | ||
218 | |||
219 | /* Delete file from its tty */ | ||
220 | void tty_del_file(struct file *file) | ||
221 | { | ||
222 | struct tty_file_private *priv = file->private_data; | ||
223 | |||
224 | spin_lock(&tty_files_lock); | ||
225 | list_del(&priv->list); | ||
226 | spin_unlock(&tty_files_lock); | ||
227 | file->private_data = NULL; | ||
228 | kfree(priv); | ||
229 | } | ||
230 | |||
231 | |||
232 | #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base) | ||
233 | |||
234 | /** | ||
235 | * tty_name - return tty naming | ||
236 | * @tty: tty structure | ||
237 | * @buf: buffer for output | ||
238 | * | ||
239 | * Convert a tty structure into a name. The name reflects the kernel | ||
240 | * naming policy and if udev is in use may not reflect user space | ||
241 | * | ||
242 | * Locking: none | ||
243 | */ | ||
244 | |||
245 | char *tty_name(struct tty_struct *tty, char *buf) | ||
246 | { | ||
247 | if (!tty) /* Hmm. NULL pointer. That's fun. */ | ||
248 | strcpy(buf, "NULL tty"); | ||
249 | else | ||
250 | strcpy(buf, tty->name); | ||
251 | return buf; | ||
252 | } | ||
253 | |||
254 | EXPORT_SYMBOL(tty_name); | ||
255 | |||
256 | int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, | ||
257 | const char *routine) | ||
258 | { | ||
259 | #ifdef TTY_PARANOIA_CHECK | ||
260 | if (!tty) { | ||
261 | printk(KERN_WARNING | ||
262 | "null TTY for (%d:%d) in %s\n", | ||
263 | imajor(inode), iminor(inode), routine); | ||
264 | return 1; | ||
265 | } | ||
266 | if (tty->magic != TTY_MAGIC) { | ||
267 | printk(KERN_WARNING | ||
268 | "bad magic number for tty struct (%d:%d) in %s\n", | ||
269 | imajor(inode), iminor(inode), routine); | ||
270 | return 1; | ||
271 | } | ||
272 | #endif | ||
273 | return 0; | ||
274 | } | ||
275 | |||
276 | static int check_tty_count(struct tty_struct *tty, const char *routine) | ||
277 | { | ||
278 | #ifdef CHECK_TTY_COUNT | ||
279 | struct list_head *p; | ||
280 | int count = 0; | ||
281 | |||
282 | spin_lock(&tty_files_lock); | ||
283 | list_for_each(p, &tty->tty_files) { | ||
284 | count++; | ||
285 | } | ||
286 | spin_unlock(&tty_files_lock); | ||
287 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | ||
288 | tty->driver->subtype == PTY_TYPE_SLAVE && | ||
289 | tty->link && tty->link->count) | ||
290 | count++; | ||
291 | if (tty->count != count) { | ||
292 | printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) " | ||
293 | "!= #fd's(%d) in %s\n", | ||
294 | tty->name, tty->count, count, routine); | ||
295 | return count; | ||
296 | } | ||
297 | #endif | ||
298 | return 0; | ||
299 | } | ||
300 | |||
301 | /** | ||
302 | * get_tty_driver - find device of a tty | ||
303 | * @dev_t: device identifier | ||
304 | * @index: returns the index of the tty | ||
305 | * | ||
306 | * This routine returns a tty driver structure, given a device number | ||
307 | * and also passes back the index number. | ||
308 | * | ||
309 | * Locking: caller must hold tty_mutex | ||
310 | */ | ||
311 | |||
312 | static struct tty_driver *get_tty_driver(dev_t device, int *index) | ||
313 | { | ||
314 | struct tty_driver *p; | ||
315 | |||
316 | list_for_each_entry(p, &tty_drivers, tty_drivers) { | ||
317 | dev_t base = MKDEV(p->major, p->minor_start); | ||
318 | if (device < base || device >= base + p->num) | ||
319 | continue; | ||
320 | *index = device - base; | ||
321 | return tty_driver_kref_get(p); | ||
322 | } | ||
323 | return NULL; | ||
324 | } | ||
325 | |||
326 | #ifdef CONFIG_CONSOLE_POLL | ||
327 | |||
328 | /** | ||
329 | * tty_find_polling_driver - find device of a polled tty | ||
330 | * @name: name string to match | ||
331 | * @line: pointer to resulting tty line nr | ||
332 | * | ||
333 | * This routine returns a tty driver structure, given a name | ||
334 | * and the condition that the tty driver is capable of polled | ||
335 | * operation. | ||
336 | */ | ||
337 | struct tty_driver *tty_find_polling_driver(char *name, int *line) | ||
338 | { | ||
339 | struct tty_driver *p, *res = NULL; | ||
340 | int tty_line = 0; | ||
341 | int len; | ||
342 | char *str, *stp; | ||
343 | |||
344 | for (str = name; *str; str++) | ||
345 | if ((*str >= '0' && *str <= '9') || *str == ',') | ||
346 | break; | ||
347 | if (!*str) | ||
348 | return NULL; | ||
349 | |||
350 | len = str - name; | ||
351 | tty_line = simple_strtoul(str, &str, 10); | ||
352 | |||
353 | mutex_lock(&tty_mutex); | ||
354 | /* Search through the tty devices to look for a match */ | ||
355 | list_for_each_entry(p, &tty_drivers, tty_drivers) { | ||
356 | if (strncmp(name, p->name, len) != 0) | ||
357 | continue; | ||
358 | stp = str; | ||
359 | if (*stp == ',') | ||
360 | stp++; | ||
361 | if (*stp == '\0') | ||
362 | stp = NULL; | ||
363 | |||
364 | if (tty_line >= 0 && tty_line < p->num && p->ops && | ||
365 | p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { | ||
366 | res = tty_driver_kref_get(p); | ||
367 | *line = tty_line; | ||
368 | break; | ||
369 | } | ||
370 | } | ||
371 | mutex_unlock(&tty_mutex); | ||
372 | |||
373 | return res; | ||
374 | } | ||
375 | EXPORT_SYMBOL_GPL(tty_find_polling_driver); | ||
376 | #endif | ||
377 | |||
378 | /** | ||
379 | * tty_check_change - check for POSIX terminal changes | ||
380 | * @tty: tty to check | ||
381 | * | ||
382 | * If we try to write to, or set the state of, a terminal and we're | ||
383 | * not in the foreground, send a SIGTTOU. If the signal is blocked or | ||
384 | * ignored, go ahead and perform the operation. (POSIX 7.2) | ||
385 | * | ||
386 | * Locking: ctrl_lock | ||
387 | */ | ||
388 | |||
389 | int tty_check_change(struct tty_struct *tty) | ||
390 | { | ||
391 | unsigned long flags; | ||
392 | int ret = 0; | ||
393 | |||
394 | if (current->signal->tty != tty) | ||
395 | return 0; | ||
396 | |||
397 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
398 | |||
399 | if (!tty->pgrp) { | ||
400 | printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n"); | ||
401 | goto out_unlock; | ||
402 | } | ||
403 | if (task_pgrp(current) == tty->pgrp) | ||
404 | goto out_unlock; | ||
405 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
406 | if (is_ignored(SIGTTOU)) | ||
407 | goto out; | ||
408 | if (is_current_pgrp_orphaned()) { | ||
409 | ret = -EIO; | ||
410 | goto out; | ||
411 | } | ||
412 | kill_pgrp(task_pgrp(current), SIGTTOU, 1); | ||
413 | set_thread_flag(TIF_SIGPENDING); | ||
414 | ret = -ERESTARTSYS; | ||
415 | out: | ||
416 | return ret; | ||
417 | out_unlock: | ||
418 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
419 | return ret; | ||
420 | } | ||
421 | |||
422 | EXPORT_SYMBOL(tty_check_change); | ||
423 | |||
424 | static ssize_t hung_up_tty_read(struct file *file, char __user *buf, | ||
425 | size_t count, loff_t *ppos) | ||
426 | { | ||
427 | return 0; | ||
428 | } | ||
429 | |||
430 | static ssize_t hung_up_tty_write(struct file *file, const char __user *buf, | ||
431 | size_t count, loff_t *ppos) | ||
432 | { | ||
433 | return -EIO; | ||
434 | } | ||
435 | |||
436 | /* No kernel lock held - none needed ;) */ | ||
437 | static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait) | ||
438 | { | ||
439 | return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM; | ||
440 | } | ||
441 | |||
442 | static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, | ||
443 | unsigned long arg) | ||
444 | { | ||
445 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; | ||
446 | } | ||
447 | |||
448 | static long hung_up_tty_compat_ioctl(struct file *file, | ||
449 | unsigned int cmd, unsigned long arg) | ||
450 | { | ||
451 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; | ||
452 | } | ||
453 | |||
454 | static const struct file_operations tty_fops = { | ||
455 | .llseek = no_llseek, | ||
456 | .read = tty_read, | ||
457 | .write = tty_write, | ||
458 | .poll = tty_poll, | ||
459 | .unlocked_ioctl = tty_ioctl, | ||
460 | .compat_ioctl = tty_compat_ioctl, | ||
461 | .open = tty_open, | ||
462 | .release = tty_release, | ||
463 | .fasync = tty_fasync, | ||
464 | }; | ||
465 | |||
466 | static const struct file_operations console_fops = { | ||
467 | .llseek = no_llseek, | ||
468 | .read = tty_read, | ||
469 | .write = redirected_tty_write, | ||
470 | .poll = tty_poll, | ||
471 | .unlocked_ioctl = tty_ioctl, | ||
472 | .compat_ioctl = tty_compat_ioctl, | ||
473 | .open = tty_open, | ||
474 | .release = tty_release, | ||
475 | .fasync = tty_fasync, | ||
476 | }; | ||
477 | |||
478 | static const struct file_operations hung_up_tty_fops = { | ||
479 | .llseek = no_llseek, | ||
480 | .read = hung_up_tty_read, | ||
481 | .write = hung_up_tty_write, | ||
482 | .poll = hung_up_tty_poll, | ||
483 | .unlocked_ioctl = hung_up_tty_ioctl, | ||
484 | .compat_ioctl = hung_up_tty_compat_ioctl, | ||
485 | .release = tty_release, | ||
486 | }; | ||
487 | |||
488 | static DEFINE_SPINLOCK(redirect_lock); | ||
489 | static struct file *redirect; | ||
490 | |||
491 | /** | ||
492 | * tty_wakeup - request more data | ||
493 | * @tty: terminal | ||
494 | * | ||
495 | * Internal and external helper for wakeups of tty. This function | ||
496 | * informs the line discipline if present that the driver is ready | ||
497 | * to receive more output data. | ||
498 | */ | ||
499 | |||
500 | void tty_wakeup(struct tty_struct *tty) | ||
501 | { | ||
502 | struct tty_ldisc *ld; | ||
503 | |||
504 | if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { | ||
505 | ld = tty_ldisc_ref(tty); | ||
506 | if (ld) { | ||
507 | if (ld->ops->write_wakeup) | ||
508 | ld->ops->write_wakeup(tty); | ||
509 | tty_ldisc_deref(ld); | ||
510 | } | ||
511 | } | ||
512 | wake_up_interruptible_poll(&tty->write_wait, POLLOUT); | ||
513 | } | ||
514 | |||
515 | EXPORT_SYMBOL_GPL(tty_wakeup); | ||
516 | |||
517 | /** | ||
518 | * __tty_hangup - actual handler for hangup events | ||
519 | * @work: tty device | ||
520 | * | ||
521 | * This can be called by the "eventd" kernel thread. That is process | ||
522 | * synchronous but doesn't hold any locks, so we need to make sure we | ||
523 | * have the appropriate locks for what we're doing. | ||
524 | * | ||
525 | * The hangup event clears any pending redirections onto the hung up | ||
526 | * device. It ensures future writes will error and it does the needed | ||
527 | * line discipline hangup and signal delivery. The tty object itself | ||
528 | * remains intact. | ||
529 | * | ||
530 | * Locking: | ||
531 | * BTM | ||
532 | * redirect lock for undoing redirection | ||
533 | * file list lock for manipulating list of ttys | ||
534 | * tty_ldisc_lock from called functions | ||
535 | * termios_mutex resetting termios data | ||
536 | * tasklist_lock to walk task list for hangup event | ||
537 | * ->siglock to protect ->signal/->sighand | ||
538 | */ | ||
539 | void __tty_hangup(struct tty_struct *tty) | ||
540 | { | ||
541 | struct file *cons_filp = NULL; | ||
542 | struct file *filp, *f = NULL; | ||
543 | struct task_struct *p; | ||
544 | struct tty_file_private *priv; | ||
545 | int closecount = 0, n; | ||
546 | unsigned long flags; | ||
547 | int refs = 0; | ||
548 | |||
549 | if (!tty) | ||
550 | return; | ||
551 | |||
552 | |||
553 | spin_lock(&redirect_lock); | ||
554 | if (redirect && file_tty(redirect) == tty) { | ||
555 | f = redirect; | ||
556 | redirect = NULL; | ||
557 | } | ||
558 | spin_unlock(&redirect_lock); | ||
559 | |||
560 | tty_lock(); | ||
561 | |||
562 | /* inuse_filps is protected by the single tty lock, | ||
563 | this really needs to change if we want to flush the | ||
564 | workqueue with the lock held */ | ||
565 | check_tty_count(tty, "tty_hangup"); | ||
566 | |||
567 | spin_lock(&tty_files_lock); | ||
568 | /* This breaks for file handles being sent over AF_UNIX sockets ? */ | ||
569 | list_for_each_entry(priv, &tty->tty_files, list) { | ||
570 | filp = priv->file; | ||
571 | if (filp->f_op->write == redirected_tty_write) | ||
572 | cons_filp = filp; | ||
573 | if (filp->f_op->write != tty_write) | ||
574 | continue; | ||
575 | closecount++; | ||
576 | __tty_fasync(-1, filp, 0); /* can't block */ | ||
577 | filp->f_op = &hung_up_tty_fops; | ||
578 | } | ||
579 | spin_unlock(&tty_files_lock); | ||
580 | |||
581 | tty_ldisc_hangup(tty); | ||
582 | |||
583 | read_lock(&tasklist_lock); | ||
584 | if (tty->session) { | ||
585 | do_each_pid_task(tty->session, PIDTYPE_SID, p) { | ||
586 | spin_lock_irq(&p->sighand->siglock); | ||
587 | if (p->signal->tty == tty) { | ||
588 | p->signal->tty = NULL; | ||
589 | /* We defer the dereferences outside fo | ||
590 | the tasklist lock */ | ||
591 | refs++; | ||
592 | } | ||
593 | if (!p->signal->leader) { | ||
594 | spin_unlock_irq(&p->sighand->siglock); | ||
595 | continue; | ||
596 | } | ||
597 | __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p); | ||
598 | __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p); | ||
599 | put_pid(p->signal->tty_old_pgrp); /* A noop */ | ||
600 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
601 | if (tty->pgrp) | ||
602 | p->signal->tty_old_pgrp = get_pid(tty->pgrp); | ||
603 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
604 | spin_unlock_irq(&p->sighand->siglock); | ||
605 | } while_each_pid_task(tty->session, PIDTYPE_SID, p); | ||
606 | } | ||
607 | read_unlock(&tasklist_lock); | ||
608 | |||
609 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
610 | clear_bit(TTY_THROTTLED, &tty->flags); | ||
611 | clear_bit(TTY_PUSH, &tty->flags); | ||
612 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | ||
613 | put_pid(tty->session); | ||
614 | put_pid(tty->pgrp); | ||
615 | tty->session = NULL; | ||
616 | tty->pgrp = NULL; | ||
617 | tty->ctrl_status = 0; | ||
618 | set_bit(TTY_HUPPED, &tty->flags); | ||
619 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
620 | |||
621 | /* Account for the p->signal references we killed */ | ||
622 | while (refs--) | ||
623 | tty_kref_put(tty); | ||
624 | |||
625 | /* | ||
626 | * If one of the devices matches a console pointer, we | ||
627 | * cannot just call hangup() because that will cause | ||
628 | * tty->count and state->count to go out of sync. | ||
629 | * So we just call close() the right number of times. | ||
630 | */ | ||
631 | if (cons_filp) { | ||
632 | if (tty->ops->close) | ||
633 | for (n = 0; n < closecount; n++) | ||
634 | tty->ops->close(tty, cons_filp); | ||
635 | } else if (tty->ops->hangup) | ||
636 | (tty->ops->hangup)(tty); | ||
637 | /* | ||
638 | * We don't want to have driver/ldisc interactions beyond | ||
639 | * the ones we did here. The driver layer expects no | ||
640 | * calls after ->hangup() from the ldisc side. However we | ||
641 | * can't yet guarantee all that. | ||
642 | */ | ||
643 | set_bit(TTY_HUPPED, &tty->flags); | ||
644 | tty_ldisc_enable(tty); | ||
645 | |||
646 | tty_unlock(); | ||
647 | |||
648 | if (f) | ||
649 | fput(f); | ||
650 | } | ||
651 | |||
652 | static void do_tty_hangup(struct work_struct *work) | ||
653 | { | ||
654 | struct tty_struct *tty = | ||
655 | container_of(work, struct tty_struct, hangup_work); | ||
656 | |||
657 | __tty_hangup(tty); | ||
658 | } | ||
659 | |||
660 | /** | ||
661 | * tty_hangup - trigger a hangup event | ||
662 | * @tty: tty to hangup | ||
663 | * | ||
664 | * A carrier loss (virtual or otherwise) has occurred on this like | ||
665 | * schedule a hangup sequence to run after this event. | ||
666 | */ | ||
667 | |||
668 | void tty_hangup(struct tty_struct *tty) | ||
669 | { | ||
670 | #ifdef TTY_DEBUG_HANGUP | ||
671 | char buf[64]; | ||
672 | printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf)); | ||
673 | #endif | ||
674 | schedule_work(&tty->hangup_work); | ||
675 | } | ||
676 | |||
677 | EXPORT_SYMBOL(tty_hangup); | ||
678 | |||
679 | /** | ||
680 | * tty_vhangup - process vhangup | ||
681 | * @tty: tty to hangup | ||
682 | * | ||
683 | * The user has asked via system call for the terminal to be hung up. | ||
684 | * We do this synchronously so that when the syscall returns the process | ||
685 | * is complete. That guarantee is necessary for security reasons. | ||
686 | */ | ||
687 | |||
688 | void tty_vhangup(struct tty_struct *tty) | ||
689 | { | ||
690 | #ifdef TTY_DEBUG_HANGUP | ||
691 | char buf[64]; | ||
692 | |||
693 | printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); | ||
694 | #endif | ||
695 | __tty_hangup(tty); | ||
696 | } | ||
697 | |||
698 | EXPORT_SYMBOL(tty_vhangup); | ||
699 | |||
700 | |||
701 | /** | ||
702 | * tty_vhangup_self - process vhangup for own ctty | ||
703 | * | ||
704 | * Perform a vhangup on the current controlling tty | ||
705 | */ | ||
706 | |||
707 | void tty_vhangup_self(void) | ||
708 | { | ||
709 | struct tty_struct *tty; | ||
710 | |||
711 | tty = get_current_tty(); | ||
712 | if (tty) { | ||
713 | tty_vhangup(tty); | ||
714 | tty_kref_put(tty); | ||
715 | } | ||
716 | } | ||
717 | |||
718 | /** | ||
719 | * tty_hung_up_p - was tty hung up | ||
720 | * @filp: file pointer of tty | ||
721 | * | ||
722 | * Return true if the tty has been subject to a vhangup or a carrier | ||
723 | * loss | ||
724 | */ | ||
725 | |||
726 | int tty_hung_up_p(struct file *filp) | ||
727 | { | ||
728 | return (filp->f_op == &hung_up_tty_fops); | ||
729 | } | ||
730 | |||
731 | EXPORT_SYMBOL(tty_hung_up_p); | ||
732 | |||
733 | static void session_clear_tty(struct pid *session) | ||
734 | { | ||
735 | struct task_struct *p; | ||
736 | do_each_pid_task(session, PIDTYPE_SID, p) { | ||
737 | proc_clear_tty(p); | ||
738 | } while_each_pid_task(session, PIDTYPE_SID, p); | ||
739 | } | ||
740 | |||
741 | /** | ||
742 | * disassociate_ctty - disconnect controlling tty | ||
743 | * @on_exit: true if exiting so need to "hang up" the session | ||
744 | * | ||
745 | * This function is typically called only by the session leader, when | ||
746 | * it wants to disassociate itself from its controlling tty. | ||
747 | * | ||
748 | * It performs the following functions: | ||
749 | * (1) Sends a SIGHUP and SIGCONT to the foreground process group | ||
750 | * (2) Clears the tty from being controlling the session | ||
751 | * (3) Clears the controlling tty for all processes in the | ||
752 | * session group. | ||
753 | * | ||
754 | * The argument on_exit is set to 1 if called when a process is | ||
755 | * exiting; it is 0 if called by the ioctl TIOCNOTTY. | ||
756 | * | ||
757 | * Locking: | ||
758 | * BTM is taken for hysterical raisins, and held when | ||
759 | * called from no_tty(). | ||
760 | * tty_mutex is taken to protect tty | ||
761 | * ->siglock is taken to protect ->signal/->sighand | ||
762 | * tasklist_lock is taken to walk process list for sessions | ||
763 | * ->siglock is taken to protect ->signal/->sighand | ||
764 | */ | ||
765 | |||
766 | void disassociate_ctty(int on_exit) | ||
767 | { | ||
768 | struct tty_struct *tty; | ||
769 | struct pid *tty_pgrp = NULL; | ||
770 | |||
771 | if (!current->signal->leader) | ||
772 | return; | ||
773 | |||
774 | tty = get_current_tty(); | ||
775 | if (tty) { | ||
776 | tty_pgrp = get_pid(tty->pgrp); | ||
777 | if (on_exit) { | ||
778 | if (tty->driver->type != TTY_DRIVER_TYPE_PTY) | ||
779 | tty_vhangup(tty); | ||
780 | } | ||
781 | tty_kref_put(tty); | ||
782 | } else if (on_exit) { | ||
783 | struct pid *old_pgrp; | ||
784 | spin_lock_irq(¤t->sighand->siglock); | ||
785 | old_pgrp = current->signal->tty_old_pgrp; | ||
786 | current->signal->tty_old_pgrp = NULL; | ||
787 | spin_unlock_irq(¤t->sighand->siglock); | ||
788 | if (old_pgrp) { | ||
789 | kill_pgrp(old_pgrp, SIGHUP, on_exit); | ||
790 | kill_pgrp(old_pgrp, SIGCONT, on_exit); | ||
791 | put_pid(old_pgrp); | ||
792 | } | ||
793 | return; | ||
794 | } | ||
795 | if (tty_pgrp) { | ||
796 | kill_pgrp(tty_pgrp, SIGHUP, on_exit); | ||
797 | if (!on_exit) | ||
798 | kill_pgrp(tty_pgrp, SIGCONT, on_exit); | ||
799 | put_pid(tty_pgrp); | ||
800 | } | ||
801 | |||
802 | spin_lock_irq(¤t->sighand->siglock); | ||
803 | put_pid(current->signal->tty_old_pgrp); | ||
804 | current->signal->tty_old_pgrp = NULL; | ||
805 | spin_unlock_irq(¤t->sighand->siglock); | ||
806 | |||
807 | tty = get_current_tty(); | ||
808 | if (tty) { | ||
809 | unsigned long flags; | ||
810 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
811 | put_pid(tty->session); | ||
812 | put_pid(tty->pgrp); | ||
813 | tty->session = NULL; | ||
814 | tty->pgrp = NULL; | ||
815 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
816 | tty_kref_put(tty); | ||
817 | } else { | ||
818 | #ifdef TTY_DEBUG_HANGUP | ||
819 | printk(KERN_DEBUG "error attempted to write to tty [0x%p]" | ||
820 | " = NULL", tty); | ||
821 | #endif | ||
822 | } | ||
823 | |||
824 | /* Now clear signal->tty under the lock */ | ||
825 | read_lock(&tasklist_lock); | ||
826 | session_clear_tty(task_session(current)); | ||
827 | read_unlock(&tasklist_lock); | ||
828 | } | ||
829 | |||
830 | /** | ||
831 | * | ||
832 | * no_tty - Ensure the current process does not have a controlling tty | ||
833 | */ | ||
834 | void no_tty(void) | ||
835 | { | ||
836 | struct task_struct *tsk = current; | ||
837 | tty_lock(); | ||
838 | disassociate_ctty(0); | ||
839 | tty_unlock(); | ||
840 | proc_clear_tty(tsk); | ||
841 | } | ||
842 | |||
843 | |||
844 | /** | ||
845 | * stop_tty - propagate flow control | ||
846 | * @tty: tty to stop | ||
847 | * | ||
848 | * Perform flow control to the driver. For PTY/TTY pairs we | ||
849 | * must also propagate the TIOCKPKT status. May be called | ||
850 | * on an already stopped device and will not re-call the driver | ||
851 | * method. | ||
852 | * | ||
853 | * This functionality is used by both the line disciplines for | ||
854 | * halting incoming flow and by the driver. It may therefore be | ||
855 | * called from any context, may be under the tty atomic_write_lock | ||
856 | * but not always. | ||
857 | * | ||
858 | * Locking: | ||
859 | * Uses the tty control lock internally | ||
860 | */ | ||
861 | |||
862 | void stop_tty(struct tty_struct *tty) | ||
863 | { | ||
864 | unsigned long flags; | ||
865 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
866 | if (tty->stopped) { | ||
867 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
868 | return; | ||
869 | } | ||
870 | tty->stopped = 1; | ||
871 | if (tty->link && tty->link->packet) { | ||
872 | tty->ctrl_status &= ~TIOCPKT_START; | ||
873 | tty->ctrl_status |= TIOCPKT_STOP; | ||
874 | wake_up_interruptible_poll(&tty->link->read_wait, POLLIN); | ||
875 | } | ||
876 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
877 | if (tty->ops->stop) | ||
878 | (tty->ops->stop)(tty); | ||
879 | } | ||
880 | |||
881 | EXPORT_SYMBOL(stop_tty); | ||
882 | |||
883 | /** | ||
884 | * start_tty - propagate flow control | ||
885 | * @tty: tty to start | ||
886 | * | ||
887 | * Start a tty that has been stopped if at all possible. Perform | ||
888 | * any necessary wakeups and propagate the TIOCPKT status. If this | ||
889 | * is the tty was previous stopped and is being started then the | ||
890 | * driver start method is invoked and the line discipline woken. | ||
891 | * | ||
892 | * Locking: | ||
893 | * ctrl_lock | ||
894 | */ | ||
895 | |||
896 | void start_tty(struct tty_struct *tty) | ||
897 | { | ||
898 | unsigned long flags; | ||
899 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
900 | if (!tty->stopped || tty->flow_stopped) { | ||
901 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
902 | return; | ||
903 | } | ||
904 | tty->stopped = 0; | ||
905 | if (tty->link && tty->link->packet) { | ||
906 | tty->ctrl_status &= ~TIOCPKT_STOP; | ||
907 | tty->ctrl_status |= TIOCPKT_START; | ||
908 | wake_up_interruptible_poll(&tty->link->read_wait, POLLIN); | ||
909 | } | ||
910 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
911 | if (tty->ops->start) | ||
912 | (tty->ops->start)(tty); | ||
913 | /* If we have a running line discipline it may need kicking */ | ||
914 | tty_wakeup(tty); | ||
915 | } | ||
916 | |||
917 | EXPORT_SYMBOL(start_tty); | ||
918 | |||
919 | /** | ||
920 | * tty_read - read method for tty device files | ||
921 | * @file: pointer to tty file | ||
922 | * @buf: user buffer | ||
923 | * @count: size of user buffer | ||
924 | * @ppos: unused | ||
925 | * | ||
926 | * Perform the read system call function on this terminal device. Checks | ||
927 | * for hung up devices before calling the line discipline method. | ||
928 | * | ||
929 | * Locking: | ||
930 | * Locks the line discipline internally while needed. Multiple | ||
931 | * read calls may be outstanding in parallel. | ||
932 | */ | ||
933 | |||
934 | static ssize_t tty_read(struct file *file, char __user *buf, size_t count, | ||
935 | loff_t *ppos) | ||
936 | { | ||
937 | int i; | ||
938 | struct inode *inode = file->f_path.dentry->d_inode; | ||
939 | struct tty_struct *tty = file_tty(file); | ||
940 | struct tty_ldisc *ld; | ||
941 | |||
942 | if (tty_paranoia_check(tty, inode, "tty_read")) | ||
943 | return -EIO; | ||
944 | if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) | ||
945 | return -EIO; | ||
946 | |||
947 | /* We want to wait for the line discipline to sort out in this | ||
948 | situation */ | ||
949 | ld = tty_ldisc_ref_wait(tty); | ||
950 | if (ld->ops->read) | ||
951 | i = (ld->ops->read)(tty, file, buf, count); | ||
952 | else | ||
953 | i = -EIO; | ||
954 | tty_ldisc_deref(ld); | ||
955 | if (i > 0) | ||
956 | inode->i_atime = current_fs_time(inode->i_sb); | ||
957 | return i; | ||
958 | } | ||
959 | |||
960 | void tty_write_unlock(struct tty_struct *tty) | ||
961 | { | ||
962 | mutex_unlock(&tty->atomic_write_lock); | ||
963 | wake_up_interruptible_poll(&tty->write_wait, POLLOUT); | ||
964 | } | ||
965 | |||
966 | int tty_write_lock(struct tty_struct *tty, int ndelay) | ||
967 | { | ||
968 | if (!mutex_trylock(&tty->atomic_write_lock)) { | ||
969 | if (ndelay) | ||
970 | return -EAGAIN; | ||
971 | if (mutex_lock_interruptible(&tty->atomic_write_lock)) | ||
972 | return -ERESTARTSYS; | ||
973 | } | ||
974 | return 0; | ||
975 | } | ||
976 | |||
977 | /* | ||
978 | * Split writes up in sane blocksizes to avoid | ||
979 | * denial-of-service type attacks | ||
980 | */ | ||
981 | static inline ssize_t do_tty_write( | ||
982 | ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), | ||
983 | struct tty_struct *tty, | ||
984 | struct file *file, | ||
985 | const char __user *buf, | ||
986 | size_t count) | ||
987 | { | ||
988 | ssize_t ret, written = 0; | ||
989 | unsigned int chunk; | ||
990 | |||
991 | ret = tty_write_lock(tty, file->f_flags & O_NDELAY); | ||
992 | if (ret < 0) | ||
993 | return ret; | ||
994 | |||
995 | /* | ||
996 | * We chunk up writes into a temporary buffer. This | ||
997 | * simplifies low-level drivers immensely, since they | ||
998 | * don't have locking issues and user mode accesses. | ||
999 | * | ||
1000 | * But if TTY_NO_WRITE_SPLIT is set, we should use a | ||
1001 | * big chunk-size.. | ||
1002 | * | ||
1003 | * The default chunk-size is 2kB, because the NTTY | ||
1004 | * layer has problems with bigger chunks. It will | ||
1005 | * claim to be able to handle more characters than | ||
1006 | * it actually does. | ||
1007 | * | ||
1008 | * FIXME: This can probably go away now except that 64K chunks | ||
1009 | * are too likely to fail unless switched to vmalloc... | ||
1010 | */ | ||
1011 | chunk = 2048; | ||
1012 | if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) | ||
1013 | chunk = 65536; | ||
1014 | if (count < chunk) | ||
1015 | chunk = count; | ||
1016 | |||
1017 | /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ | ||
1018 | if (tty->write_cnt < chunk) { | ||
1019 | unsigned char *buf_chunk; | ||
1020 | |||
1021 | if (chunk < 1024) | ||
1022 | chunk = 1024; | ||
1023 | |||
1024 | buf_chunk = kmalloc(chunk, GFP_KERNEL); | ||
1025 | if (!buf_chunk) { | ||
1026 | ret = -ENOMEM; | ||
1027 | goto out; | ||
1028 | } | ||
1029 | kfree(tty->write_buf); | ||
1030 | tty->write_cnt = chunk; | ||
1031 | tty->write_buf = buf_chunk; | ||
1032 | } | ||
1033 | |||
1034 | /* Do the write .. */ | ||
1035 | for (;;) { | ||
1036 | size_t size = count; | ||
1037 | if (size > chunk) | ||
1038 | size = chunk; | ||
1039 | ret = -EFAULT; | ||
1040 | if (copy_from_user(tty->write_buf, buf, size)) | ||
1041 | break; | ||
1042 | ret = write(tty, file, tty->write_buf, size); | ||
1043 | if (ret <= 0) | ||
1044 | break; | ||
1045 | written += ret; | ||
1046 | buf += ret; | ||
1047 | count -= ret; | ||
1048 | if (!count) | ||
1049 | break; | ||
1050 | ret = -ERESTARTSYS; | ||
1051 | if (signal_pending(current)) | ||
1052 | break; | ||
1053 | cond_resched(); | ||
1054 | } | ||
1055 | if (written) { | ||
1056 | struct inode *inode = file->f_path.dentry->d_inode; | ||
1057 | inode->i_mtime = current_fs_time(inode->i_sb); | ||
1058 | ret = written; | ||
1059 | } | ||
1060 | out: | ||
1061 | tty_write_unlock(tty); | ||
1062 | return ret; | ||
1063 | } | ||
1064 | |||
1065 | /** | ||
1066 | * tty_write_message - write a message to a certain tty, not just the console. | ||
1067 | * @tty: the destination tty_struct | ||
1068 | * @msg: the message to write | ||
1069 | * | ||
1070 | * This is used for messages that need to be redirected to a specific tty. | ||
1071 | * We don't put it into the syslog queue right now maybe in the future if | ||
1072 | * really needed. | ||
1073 | * | ||
1074 | * We must still hold the BTM and test the CLOSING flag for the moment. | ||
1075 | */ | ||
1076 | |||
1077 | void tty_write_message(struct tty_struct *tty, char *msg) | ||
1078 | { | ||
1079 | if (tty) { | ||
1080 | mutex_lock(&tty->atomic_write_lock); | ||
1081 | tty_lock(); | ||
1082 | if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { | ||
1083 | tty_unlock(); | ||
1084 | tty->ops->write(tty, msg, strlen(msg)); | ||
1085 | } else | ||
1086 | tty_unlock(); | ||
1087 | tty_write_unlock(tty); | ||
1088 | } | ||
1089 | return; | ||
1090 | } | ||
1091 | |||
1092 | |||
1093 | /** | ||
1094 | * tty_write - write method for tty device file | ||
1095 | * @file: tty file pointer | ||
1096 | * @buf: user data to write | ||
1097 | * @count: bytes to write | ||
1098 | * @ppos: unused | ||
1099 | * | ||
1100 | * Write data to a tty device via the line discipline. | ||
1101 | * | ||
1102 | * Locking: | ||
1103 | * Locks the line discipline as required | ||
1104 | * Writes to the tty driver are serialized by the atomic_write_lock | ||
1105 | * and are then processed in chunks to the device. The line discipline | ||
1106 | * write method will not be invoked in parallel for each device. | ||
1107 | */ | ||
1108 | |||
1109 | static ssize_t tty_write(struct file *file, const char __user *buf, | ||
1110 | size_t count, loff_t *ppos) | ||
1111 | { | ||
1112 | struct inode *inode = file->f_path.dentry->d_inode; | ||
1113 | struct tty_struct *tty = file_tty(file); | ||
1114 | struct tty_ldisc *ld; | ||
1115 | ssize_t ret; | ||
1116 | |||
1117 | if (tty_paranoia_check(tty, inode, "tty_write")) | ||
1118 | return -EIO; | ||
1119 | if (!tty || !tty->ops->write || | ||
1120 | (test_bit(TTY_IO_ERROR, &tty->flags))) | ||
1121 | return -EIO; | ||
1122 | /* Short term debug to catch buggy drivers */ | ||
1123 | if (tty->ops->write_room == NULL) | ||
1124 | printk(KERN_ERR "tty driver %s lacks a write_room method.\n", | ||
1125 | tty->driver->name); | ||
1126 | ld = tty_ldisc_ref_wait(tty); | ||
1127 | if (!ld->ops->write) | ||
1128 | ret = -EIO; | ||
1129 | else | ||
1130 | ret = do_tty_write(ld->ops->write, tty, file, buf, count); | ||
1131 | tty_ldisc_deref(ld); | ||
1132 | return ret; | ||
1133 | } | ||
1134 | |||
1135 | ssize_t redirected_tty_write(struct file *file, const char __user *buf, | ||
1136 | size_t count, loff_t *ppos) | ||
1137 | { | ||
1138 | struct file *p = NULL; | ||
1139 | |||
1140 | spin_lock(&redirect_lock); | ||
1141 | if (redirect) { | ||
1142 | get_file(redirect); | ||
1143 | p = redirect; | ||
1144 | } | ||
1145 | spin_unlock(&redirect_lock); | ||
1146 | |||
1147 | if (p) { | ||
1148 | ssize_t res; | ||
1149 | res = vfs_write(p, buf, count, &p->f_pos); | ||
1150 | fput(p); | ||
1151 | return res; | ||
1152 | } | ||
1153 | return tty_write(file, buf, count, ppos); | ||
1154 | } | ||
1155 | |||
1156 | static char ptychar[] = "pqrstuvwxyzabcde"; | ||
1157 | |||
1158 | /** | ||
1159 | * pty_line_name - generate name for a pty | ||
1160 | * @driver: the tty driver in use | ||
1161 | * @index: the minor number | ||
1162 | * @p: output buffer of at least 6 bytes | ||
1163 | * | ||
1164 | * Generate a name from a driver reference and write it to the output | ||
1165 | * buffer. | ||
1166 | * | ||
1167 | * Locking: None | ||
1168 | */ | ||
1169 | static void pty_line_name(struct tty_driver *driver, int index, char *p) | ||
1170 | { | ||
1171 | int i = index + driver->name_base; | ||
1172 | /* ->name is initialized to "ttyp", but "tty" is expected */ | ||
1173 | sprintf(p, "%s%c%x", | ||
1174 | driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, | ||
1175 | ptychar[i >> 4 & 0xf], i & 0xf); | ||
1176 | } | ||
1177 | |||
1178 | /** | ||
1179 | * tty_line_name - generate name for a tty | ||
1180 | * @driver: the tty driver in use | ||
1181 | * @index: the minor number | ||
1182 | * @p: output buffer of at least 7 bytes | ||
1183 | * | ||
1184 | * Generate a name from a driver reference and write it to the output | ||
1185 | * buffer. | ||
1186 | * | ||
1187 | * Locking: None | ||
1188 | */ | ||
1189 | static void tty_line_name(struct tty_driver *driver, int index, char *p) | ||
1190 | { | ||
1191 | sprintf(p, "%s%d", driver->name, index + driver->name_base); | ||
1192 | } | ||
1193 | |||
1194 | /** | ||
1195 | * tty_driver_lookup_tty() - find an existing tty, if any | ||
1196 | * @driver: the driver for the tty | ||
1197 | * @idx: the minor number | ||
1198 | * | ||
1199 | * Return the tty, if found or ERR_PTR() otherwise. | ||
1200 | * | ||
1201 | * Locking: tty_mutex must be held. If tty is found, the mutex must | ||
1202 | * be held until the 'fast-open' is also done. Will change once we | ||
1203 | * have refcounting in the driver and per driver locking | ||
1204 | */ | ||
1205 | static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, | ||
1206 | struct inode *inode, int idx) | ||
1207 | { | ||
1208 | struct tty_struct *tty; | ||
1209 | |||
1210 | if (driver->ops->lookup) | ||
1211 | return driver->ops->lookup(driver, inode, idx); | ||
1212 | |||
1213 | tty = driver->ttys[idx]; | ||
1214 | return tty; | ||
1215 | } | ||
1216 | |||
1217 | /** | ||
1218 | * tty_init_termios - helper for termios setup | ||
1219 | * @tty: the tty to set up | ||
1220 | * | ||
1221 | * Initialise the termios structures for this tty. Thus runs under | ||
1222 | * the tty_mutex currently so we can be relaxed about ordering. | ||
1223 | */ | ||
1224 | |||
1225 | int tty_init_termios(struct tty_struct *tty) | ||
1226 | { | ||
1227 | struct ktermios *tp; | ||
1228 | int idx = tty->index; | ||
1229 | |||
1230 | tp = tty->driver->termios[idx]; | ||
1231 | if (tp == NULL) { | ||
1232 | tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); | ||
1233 | if (tp == NULL) | ||
1234 | return -ENOMEM; | ||
1235 | memcpy(tp, &tty->driver->init_termios, | ||
1236 | sizeof(struct ktermios)); | ||
1237 | tty->driver->termios[idx] = tp; | ||
1238 | } | ||
1239 | tty->termios = tp; | ||
1240 | tty->termios_locked = tp + 1; | ||
1241 | |||
1242 | /* Compatibility until drivers always set this */ | ||
1243 | tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios); | ||
1244 | tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios); | ||
1245 | return 0; | ||
1246 | } | ||
1247 | EXPORT_SYMBOL_GPL(tty_init_termios); | ||
1248 | |||
1249 | /** | ||
1250 | * tty_driver_install_tty() - install a tty entry in the driver | ||
1251 | * @driver: the driver for the tty | ||
1252 | * @tty: the tty | ||
1253 | * | ||
1254 | * Install a tty object into the driver tables. The tty->index field | ||
1255 | * will be set by the time this is called. This method is responsible | ||
1256 | * for ensuring any need additional structures are allocated and | ||
1257 | * configured. | ||
1258 | * | ||
1259 | * Locking: tty_mutex for now | ||
1260 | */ | ||
1261 | static int tty_driver_install_tty(struct tty_driver *driver, | ||
1262 | struct tty_struct *tty) | ||
1263 | { | ||
1264 | int idx = tty->index; | ||
1265 | int ret; | ||
1266 | |||
1267 | if (driver->ops->install) { | ||
1268 | ret = driver->ops->install(driver, tty); | ||
1269 | return ret; | ||
1270 | } | ||
1271 | |||
1272 | if (tty_init_termios(tty) == 0) { | ||
1273 | tty_driver_kref_get(driver); | ||
1274 | tty->count++; | ||
1275 | driver->ttys[idx] = tty; | ||
1276 | return 0; | ||
1277 | } | ||
1278 | return -ENOMEM; | ||
1279 | } | ||
1280 | |||
1281 | /** | ||
1282 | * tty_driver_remove_tty() - remove a tty from the driver tables | ||
1283 | * @driver: the driver for the tty | ||
1284 | * @idx: the minor number | ||
1285 | * | ||
1286 | * Remvoe a tty object from the driver tables. The tty->index field | ||
1287 | * will be set by the time this is called. | ||
1288 | * | ||
1289 | * Locking: tty_mutex for now | ||
1290 | */ | ||
1291 | static void tty_driver_remove_tty(struct tty_driver *driver, | ||
1292 | struct tty_struct *tty) | ||
1293 | { | ||
1294 | if (driver->ops->remove) | ||
1295 | driver->ops->remove(driver, tty); | ||
1296 | else | ||
1297 | driver->ttys[tty->index] = NULL; | ||
1298 | } | ||
1299 | |||
1300 | /* | ||
1301 | * tty_reopen() - fast re-open of an open tty | ||
1302 | * @tty - the tty to open | ||
1303 | * | ||
1304 | * Return 0 on success, -errno on error. | ||
1305 | * | ||
1306 | * Locking: tty_mutex must be held from the time the tty was found | ||
1307 | * till this open completes. | ||
1308 | */ | ||
1309 | static int tty_reopen(struct tty_struct *tty) | ||
1310 | { | ||
1311 | struct tty_driver *driver = tty->driver; | ||
1312 | |||
1313 | if (test_bit(TTY_CLOSING, &tty->flags)) | ||
1314 | return -EIO; | ||
1315 | |||
1316 | if (driver->type == TTY_DRIVER_TYPE_PTY && | ||
1317 | driver->subtype == PTY_TYPE_MASTER) { | ||
1318 | /* | ||
1319 | * special case for PTY masters: only one open permitted, | ||
1320 | * and the slave side open count is incremented as well. | ||
1321 | */ | ||
1322 | if (tty->count) | ||
1323 | return -EIO; | ||
1324 | |||
1325 | tty->link->count++; | ||
1326 | } | ||
1327 | tty->count++; | ||
1328 | tty->driver = driver; /* N.B. why do this every time?? */ | ||
1329 | |||
1330 | mutex_lock(&tty->ldisc_mutex); | ||
1331 | WARN_ON(!test_bit(TTY_LDISC, &tty->flags)); | ||
1332 | mutex_unlock(&tty->ldisc_mutex); | ||
1333 | |||
1334 | return 0; | ||
1335 | } | ||
1336 | |||
1337 | /** | ||
1338 | * tty_init_dev - initialise a tty device | ||
1339 | * @driver: tty driver we are opening a device on | ||
1340 | * @idx: device index | ||
1341 | * @ret_tty: returned tty structure | ||
1342 | * @first_ok: ok to open a new device (used by ptmx) | ||
1343 | * | ||
1344 | * Prepare a tty device. This may not be a "new" clean device but | ||
1345 | * could also be an active device. The pty drivers require special | ||
1346 | * handling because of this. | ||
1347 | * | ||
1348 | * Locking: | ||
1349 | * The function is called under the tty_mutex, which | ||
1350 | * protects us from the tty struct or driver itself going away. | ||
1351 | * | ||
1352 | * On exit the tty device has the line discipline attached and | ||
1353 | * a reference count of 1. If a pair was created for pty/tty use | ||
1354 | * and the other was a pty master then it too has a reference count of 1. | ||
1355 | * | ||
1356 | * WSH 06/09/97: Rewritten to remove races and properly clean up after a | ||
1357 | * failed open. The new code protects the open with a mutex, so it's | ||
1358 | * really quite straightforward. The mutex locking can probably be | ||
1359 | * relaxed for the (most common) case of reopening a tty. | ||
1360 | */ | ||
1361 | |||
1362 | struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx, | ||
1363 | int first_ok) | ||
1364 | { | ||
1365 | struct tty_struct *tty; | ||
1366 | int retval; | ||
1367 | |||
1368 | /* Check if pty master is being opened multiple times */ | ||
1369 | if (driver->subtype == PTY_TYPE_MASTER && | ||
1370 | (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) { | ||
1371 | return ERR_PTR(-EIO); | ||
1372 | } | ||
1373 | |||
1374 | /* | ||
1375 | * First time open is complex, especially for PTY devices. | ||
1376 | * This code guarantees that either everything succeeds and the | ||
1377 | * TTY is ready for operation, or else the table slots are vacated | ||
1378 | * and the allocated memory released. (Except that the termios | ||
1379 | * and locked termios may be retained.) | ||
1380 | */ | ||
1381 | |||
1382 | if (!try_module_get(driver->owner)) | ||
1383 | return ERR_PTR(-ENODEV); | ||
1384 | |||
1385 | tty = alloc_tty_struct(); | ||
1386 | if (!tty) | ||
1387 | goto fail_no_mem; | ||
1388 | initialize_tty_struct(tty, driver, idx); | ||
1389 | |||
1390 | retval = tty_driver_install_tty(driver, tty); | ||
1391 | if (retval < 0) { | ||
1392 | free_tty_struct(tty); | ||
1393 | module_put(driver->owner); | ||
1394 | return ERR_PTR(retval); | ||
1395 | } | ||
1396 | |||
1397 | /* | ||
1398 | * Structures all installed ... call the ldisc open routines. | ||
1399 | * If we fail here just call release_tty to clean up. No need | ||
1400 | * to decrement the use counts, as release_tty doesn't care. | ||
1401 | */ | ||
1402 | retval = tty_ldisc_setup(tty, tty->link); | ||
1403 | if (retval) | ||
1404 | goto release_mem_out; | ||
1405 | return tty; | ||
1406 | |||
1407 | fail_no_mem: | ||
1408 | module_put(driver->owner); | ||
1409 | return ERR_PTR(-ENOMEM); | ||
1410 | |||
1411 | /* call the tty release_tty routine to clean out this slot */ | ||
1412 | release_mem_out: | ||
1413 | if (printk_ratelimit()) | ||
1414 | printk(KERN_INFO "tty_init_dev: ldisc open failed, " | ||
1415 | "clearing slot %d\n", idx); | ||
1416 | release_tty(tty, idx); | ||
1417 | return ERR_PTR(retval); | ||
1418 | } | ||
1419 | |||
1420 | void tty_free_termios(struct tty_struct *tty) | ||
1421 | { | ||
1422 | struct ktermios *tp; | ||
1423 | int idx = tty->index; | ||
1424 | /* Kill this flag and push into drivers for locking etc */ | ||
1425 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) { | ||
1426 | /* FIXME: Locking on ->termios array */ | ||
1427 | tp = tty->termios; | ||
1428 | tty->driver->termios[idx] = NULL; | ||
1429 | kfree(tp); | ||
1430 | } | ||
1431 | } | ||
1432 | EXPORT_SYMBOL(tty_free_termios); | ||
1433 | |||
1434 | void tty_shutdown(struct tty_struct *tty) | ||
1435 | { | ||
1436 | tty_driver_remove_tty(tty->driver, tty); | ||
1437 | tty_free_termios(tty); | ||
1438 | } | ||
1439 | EXPORT_SYMBOL(tty_shutdown); | ||
1440 | |||
1441 | /** | ||
1442 | * release_one_tty - release tty structure memory | ||
1443 | * @kref: kref of tty we are obliterating | ||
1444 | * | ||
1445 | * Releases memory associated with a tty structure, and clears out the | ||
1446 | * driver table slots. This function is called when a device is no longer | ||
1447 | * in use. It also gets called when setup of a device fails. | ||
1448 | * | ||
1449 | * Locking: | ||
1450 | * tty_mutex - sometimes only | ||
1451 | * takes the file list lock internally when working on the list | ||
1452 | * of ttys that the driver keeps. | ||
1453 | * | ||
1454 | * This method gets called from a work queue so that the driver private | ||
1455 | * cleanup ops can sleep (needed for USB at least) | ||
1456 | */ | ||
1457 | static void release_one_tty(struct work_struct *work) | ||
1458 | { | ||
1459 | struct tty_struct *tty = | ||
1460 | container_of(work, struct tty_struct, hangup_work); | ||
1461 | struct tty_driver *driver = tty->driver; | ||
1462 | |||
1463 | if (tty->ops->cleanup) | ||
1464 | tty->ops->cleanup(tty); | ||
1465 | |||
1466 | tty->magic = 0; | ||
1467 | tty_driver_kref_put(driver); | ||
1468 | module_put(driver->owner); | ||
1469 | |||
1470 | spin_lock(&tty_files_lock); | ||
1471 | list_del_init(&tty->tty_files); | ||
1472 | spin_unlock(&tty_files_lock); | ||
1473 | |||
1474 | put_pid(tty->pgrp); | ||
1475 | put_pid(tty->session); | ||
1476 | free_tty_struct(tty); | ||
1477 | } | ||
1478 | |||
1479 | static void queue_release_one_tty(struct kref *kref) | ||
1480 | { | ||
1481 | struct tty_struct *tty = container_of(kref, struct tty_struct, kref); | ||
1482 | |||
1483 | if (tty->ops->shutdown) | ||
1484 | tty->ops->shutdown(tty); | ||
1485 | else | ||
1486 | tty_shutdown(tty); | ||
1487 | |||
1488 | /* The hangup queue is now free so we can reuse it rather than | ||
1489 | waste a chunk of memory for each port */ | ||
1490 | INIT_WORK(&tty->hangup_work, release_one_tty); | ||
1491 | schedule_work(&tty->hangup_work); | ||
1492 | } | ||
1493 | |||
1494 | /** | ||
1495 | * tty_kref_put - release a tty kref | ||
1496 | * @tty: tty device | ||
1497 | * | ||
1498 | * Release a reference to a tty device and if need be let the kref | ||
1499 | * layer destruct the object for us | ||
1500 | */ | ||
1501 | |||
1502 | void tty_kref_put(struct tty_struct *tty) | ||
1503 | { | ||
1504 | if (tty) | ||
1505 | kref_put(&tty->kref, queue_release_one_tty); | ||
1506 | } | ||
1507 | EXPORT_SYMBOL(tty_kref_put); | ||
1508 | |||
1509 | /** | ||
1510 | * release_tty - release tty structure memory | ||
1511 | * | ||
1512 | * Release both @tty and a possible linked partner (think pty pair), | ||
1513 | * and decrement the refcount of the backing module. | ||
1514 | * | ||
1515 | * Locking: | ||
1516 | * tty_mutex - sometimes only | ||
1517 | * takes the file list lock internally when working on the list | ||
1518 | * of ttys that the driver keeps. | ||
1519 | * FIXME: should we require tty_mutex is held here ?? | ||
1520 | * | ||
1521 | */ | ||
1522 | static void release_tty(struct tty_struct *tty, int idx) | ||
1523 | { | ||
1524 | /* This should always be true but check for the moment */ | ||
1525 | WARN_ON(tty->index != idx); | ||
1526 | |||
1527 | if (tty->link) | ||
1528 | tty_kref_put(tty->link); | ||
1529 | tty_kref_put(tty); | ||
1530 | } | ||
1531 | |||
1532 | /** | ||
1533 | * tty_release - vfs callback for close | ||
1534 | * @inode: inode of tty | ||
1535 | * @filp: file pointer for handle to tty | ||
1536 | * | ||
1537 | * Called the last time each file handle is closed that references | ||
1538 | * this tty. There may however be several such references. | ||
1539 | * | ||
1540 | * Locking: | ||
1541 | * Takes bkl. See tty_release_dev | ||
1542 | * | ||
1543 | * Even releasing the tty structures is a tricky business.. We have | ||
1544 | * to be very careful that the structures are all released at the | ||
1545 | * same time, as interrupts might otherwise get the wrong pointers. | ||
1546 | * | ||
1547 | * WSH 09/09/97: rewritten to avoid some nasty race conditions that could | ||
1548 | * lead to double frees or releasing memory still in use. | ||
1549 | */ | ||
1550 | |||
1551 | int tty_release(struct inode *inode, struct file *filp) | ||
1552 | { | ||
1553 | struct tty_struct *tty = file_tty(filp); | ||
1554 | struct tty_struct *o_tty; | ||
1555 | int pty_master, tty_closing, o_tty_closing, do_sleep; | ||
1556 | int devpts; | ||
1557 | int idx; | ||
1558 | char buf[64]; | ||
1559 | |||
1560 | if (tty_paranoia_check(tty, inode, "tty_release_dev")) | ||
1561 | return 0; | ||
1562 | |||
1563 | tty_lock(); | ||
1564 | check_tty_count(tty, "tty_release_dev"); | ||
1565 | |||
1566 | __tty_fasync(-1, filp, 0); | ||
1567 | |||
1568 | idx = tty->index; | ||
1569 | pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && | ||
1570 | tty->driver->subtype == PTY_TYPE_MASTER); | ||
1571 | devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0; | ||
1572 | o_tty = tty->link; | ||
1573 | |||
1574 | #ifdef TTY_PARANOIA_CHECK | ||
1575 | if (idx < 0 || idx >= tty->driver->num) { | ||
1576 | printk(KERN_DEBUG "tty_release_dev: bad idx when trying to " | ||
1577 | "free (%s)\n", tty->name); | ||
1578 | tty_unlock(); | ||
1579 | return 0; | ||
1580 | } | ||
1581 | if (!devpts) { | ||
1582 | if (tty != tty->driver->ttys[idx]) { | ||
1583 | tty_unlock(); | ||
1584 | printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty " | ||
1585 | "for (%s)\n", idx, tty->name); | ||
1586 | return 0; | ||
1587 | } | ||
1588 | if (tty->termios != tty->driver->termios[idx]) { | ||
1589 | tty_unlock(); | ||
1590 | printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios " | ||
1591 | "for (%s)\n", | ||
1592 | idx, tty->name); | ||
1593 | return 0; | ||
1594 | } | ||
1595 | } | ||
1596 | #endif | ||
1597 | |||
1598 | #ifdef TTY_DEBUG_HANGUP | ||
1599 | printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...", | ||
1600 | tty_name(tty, buf), tty->count); | ||
1601 | #endif | ||
1602 | |||
1603 | #ifdef TTY_PARANOIA_CHECK | ||
1604 | if (tty->driver->other && | ||
1605 | !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { | ||
1606 | if (o_tty != tty->driver->other->ttys[idx]) { | ||
1607 | tty_unlock(); | ||
1608 | printk(KERN_DEBUG "tty_release_dev: other->table[%d] " | ||
1609 | "not o_tty for (%s)\n", | ||
1610 | idx, tty->name); | ||
1611 | return 0 ; | ||
1612 | } | ||
1613 | if (o_tty->termios != tty->driver->other->termios[idx]) { | ||
1614 | tty_unlock(); | ||
1615 | printk(KERN_DEBUG "tty_release_dev: other->termios[%d] " | ||
1616 | "not o_termios for (%s)\n", | ||
1617 | idx, tty->name); | ||
1618 | return 0; | ||
1619 | } | ||
1620 | if (o_tty->link != tty) { | ||
1621 | tty_unlock(); | ||
1622 | printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n"); | ||
1623 | return 0; | ||
1624 | } | ||
1625 | } | ||
1626 | #endif | ||
1627 | if (tty->ops->close) | ||
1628 | tty->ops->close(tty, filp); | ||
1629 | |||
1630 | tty_unlock(); | ||
1631 | /* | ||
1632 | * Sanity check: if tty->count is going to zero, there shouldn't be | ||
1633 | * any waiters on tty->read_wait or tty->write_wait. We test the | ||
1634 | * wait queues and kick everyone out _before_ actually starting to | ||
1635 | * close. This ensures that we won't block while releasing the tty | ||
1636 | * structure. | ||
1637 | * | ||
1638 | * The test for the o_tty closing is necessary, since the master and | ||
1639 | * slave sides may close in any order. If the slave side closes out | ||
1640 | * first, its count will be one, since the master side holds an open. | ||
1641 | * Thus this test wouldn't be triggered at the time the slave closes, | ||
1642 | * so we do it now. | ||
1643 | * | ||
1644 | * Note that it's possible for the tty to be opened again while we're | ||
1645 | * flushing out waiters. By recalculating the closing flags before | ||
1646 | * each iteration we avoid any problems. | ||
1647 | */ | ||
1648 | while (1) { | ||
1649 | /* Guard against races with tty->count changes elsewhere and | ||
1650 | opens on /dev/tty */ | ||
1651 | |||
1652 | mutex_lock(&tty_mutex); | ||
1653 | tty_lock(); | ||
1654 | tty_closing = tty->count <= 1; | ||
1655 | o_tty_closing = o_tty && | ||
1656 | (o_tty->count <= (pty_master ? 1 : 0)); | ||
1657 | do_sleep = 0; | ||
1658 | |||
1659 | if (tty_closing) { | ||
1660 | if (waitqueue_active(&tty->read_wait)) { | ||
1661 | wake_up_poll(&tty->read_wait, POLLIN); | ||
1662 | do_sleep++; | ||
1663 | } | ||
1664 | if (waitqueue_active(&tty->write_wait)) { | ||
1665 | wake_up_poll(&tty->write_wait, POLLOUT); | ||
1666 | do_sleep++; | ||
1667 | } | ||
1668 | } | ||
1669 | if (o_tty_closing) { | ||
1670 | if (waitqueue_active(&o_tty->read_wait)) { | ||
1671 | wake_up_poll(&o_tty->read_wait, POLLIN); | ||
1672 | do_sleep++; | ||
1673 | } | ||
1674 | if (waitqueue_active(&o_tty->write_wait)) { | ||
1675 | wake_up_poll(&o_tty->write_wait, POLLOUT); | ||
1676 | do_sleep++; | ||
1677 | } | ||
1678 | } | ||
1679 | if (!do_sleep) | ||
1680 | break; | ||
1681 | |||
1682 | printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue " | ||
1683 | "active!\n", tty_name(tty, buf)); | ||
1684 | tty_unlock(); | ||
1685 | mutex_unlock(&tty_mutex); | ||
1686 | schedule(); | ||
1687 | } | ||
1688 | |||
1689 | /* | ||
1690 | * The closing flags are now consistent with the open counts on | ||
1691 | * both sides, and we've completed the last operation that could | ||
1692 | * block, so it's safe to proceed with closing. | ||
1693 | */ | ||
1694 | if (pty_master) { | ||
1695 | if (--o_tty->count < 0) { | ||
1696 | printk(KERN_WARNING "tty_release_dev: bad pty slave count " | ||
1697 | "(%d) for %s\n", | ||
1698 | o_tty->count, tty_name(o_tty, buf)); | ||
1699 | o_tty->count = 0; | ||
1700 | } | ||
1701 | } | ||
1702 | if (--tty->count < 0) { | ||
1703 | printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n", | ||
1704 | tty->count, tty_name(tty, buf)); | ||
1705 | tty->count = 0; | ||
1706 | } | ||
1707 | |||
1708 | /* | ||
1709 | * We've decremented tty->count, so we need to remove this file | ||
1710 | * descriptor off the tty->tty_files list; this serves two | ||
1711 | * purposes: | ||
1712 | * - check_tty_count sees the correct number of file descriptors | ||
1713 | * associated with this tty. | ||
1714 | * - do_tty_hangup no longer sees this file descriptor as | ||
1715 | * something that needs to be handled for hangups. | ||
1716 | */ | ||
1717 | tty_del_file(filp); | ||
1718 | |||
1719 | /* | ||
1720 | * Perform some housekeeping before deciding whether to return. | ||
1721 | * | ||
1722 | * Set the TTY_CLOSING flag if this was the last open. In the | ||
1723 | * case of a pty we may have to wait around for the other side | ||
1724 | * to close, and TTY_CLOSING makes sure we can't be reopened. | ||
1725 | */ | ||
1726 | if (tty_closing) | ||
1727 | set_bit(TTY_CLOSING, &tty->flags); | ||
1728 | if (o_tty_closing) | ||
1729 | set_bit(TTY_CLOSING, &o_tty->flags); | ||
1730 | |||
1731 | /* | ||
1732 | * If _either_ side is closing, make sure there aren't any | ||
1733 | * processes that still think tty or o_tty is their controlling | ||
1734 | * tty. | ||
1735 | */ | ||
1736 | if (tty_closing || o_tty_closing) { | ||
1737 | read_lock(&tasklist_lock); | ||
1738 | session_clear_tty(tty->session); | ||
1739 | if (o_tty) | ||
1740 | session_clear_tty(o_tty->session); | ||
1741 | read_unlock(&tasklist_lock); | ||
1742 | } | ||
1743 | |||
1744 | mutex_unlock(&tty_mutex); | ||
1745 | |||
1746 | /* check whether both sides are closing ... */ | ||
1747 | if (!tty_closing || (o_tty && !o_tty_closing)) { | ||
1748 | tty_unlock(); | ||
1749 | return 0; | ||
1750 | } | ||
1751 | |||
1752 | #ifdef TTY_DEBUG_HANGUP | ||
1753 | printk(KERN_DEBUG "freeing tty structure..."); | ||
1754 | #endif | ||
1755 | /* | ||
1756 | * Ask the line discipline code to release its structures | ||
1757 | */ | ||
1758 | tty_ldisc_release(tty, o_tty); | ||
1759 | /* | ||
1760 | * The release_tty function takes care of the details of clearing | ||
1761 | * the slots and preserving the termios structure. | ||
1762 | */ | ||
1763 | release_tty(tty, idx); | ||
1764 | |||
1765 | /* Make this pty number available for reallocation */ | ||
1766 | if (devpts) | ||
1767 | devpts_kill_index(inode, idx); | ||
1768 | tty_unlock(); | ||
1769 | return 0; | ||
1770 | } | ||
1771 | |||
1772 | /** | ||
1773 | * tty_open - open a tty device | ||
1774 | * @inode: inode of device file | ||
1775 | * @filp: file pointer to tty | ||
1776 | * | ||
1777 | * tty_open and tty_release keep up the tty count that contains the | ||
1778 | * number of opens done on a tty. We cannot use the inode-count, as | ||
1779 | * different inodes might point to the same tty. | ||
1780 | * | ||
1781 | * Open-counting is needed for pty masters, as well as for keeping | ||
1782 | * track of serial lines: DTR is dropped when the last close happens. | ||
1783 | * (This is not done solely through tty->count, now. - Ted 1/27/92) | ||
1784 | * | ||
1785 | * The termios state of a pty is reset on first open so that | ||
1786 | * settings don't persist across reuse. | ||
1787 | * | ||
1788 | * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work. | ||
1789 | * tty->count should protect the rest. | ||
1790 | * ->siglock protects ->signal/->sighand | ||
1791 | */ | ||
1792 | |||
1793 | static int tty_open(struct inode *inode, struct file *filp) | ||
1794 | { | ||
1795 | struct tty_struct *tty = NULL; | ||
1796 | int noctty, retval; | ||
1797 | struct tty_driver *driver; | ||
1798 | int index; | ||
1799 | dev_t device = inode->i_rdev; | ||
1800 | unsigned saved_flags = filp->f_flags; | ||
1801 | |||
1802 | nonseekable_open(inode, filp); | ||
1803 | |||
1804 | retry_open: | ||
1805 | noctty = filp->f_flags & O_NOCTTY; | ||
1806 | index = -1; | ||
1807 | retval = 0; | ||
1808 | |||
1809 | mutex_lock(&tty_mutex); | ||
1810 | tty_lock(); | ||
1811 | |||
1812 | if (device == MKDEV(TTYAUX_MAJOR, 0)) { | ||
1813 | tty = get_current_tty(); | ||
1814 | if (!tty) { | ||
1815 | tty_unlock(); | ||
1816 | mutex_unlock(&tty_mutex); | ||
1817 | return -ENXIO; | ||
1818 | } | ||
1819 | driver = tty_driver_kref_get(tty->driver); | ||
1820 | index = tty->index; | ||
1821 | filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ | ||
1822 | /* noctty = 1; */ | ||
1823 | /* FIXME: Should we take a driver reference ? */ | ||
1824 | tty_kref_put(tty); | ||
1825 | goto got_driver; | ||
1826 | } | ||
1827 | #ifdef CONFIG_VT | ||
1828 | if (device == MKDEV(TTY_MAJOR, 0)) { | ||
1829 | extern struct tty_driver *console_driver; | ||
1830 | driver = tty_driver_kref_get(console_driver); | ||
1831 | index = fg_console; | ||
1832 | noctty = 1; | ||
1833 | goto got_driver; | ||
1834 | } | ||
1835 | #endif | ||
1836 | if (device == MKDEV(TTYAUX_MAJOR, 1)) { | ||
1837 | struct tty_driver *console_driver = console_device(&index); | ||
1838 | if (console_driver) { | ||
1839 | driver = tty_driver_kref_get(console_driver); | ||
1840 | if (driver) { | ||
1841 | /* Don't let /dev/console block */ | ||
1842 | filp->f_flags |= O_NONBLOCK; | ||
1843 | noctty = 1; | ||
1844 | goto got_driver; | ||
1845 | } | ||
1846 | } | ||
1847 | tty_unlock(); | ||
1848 | mutex_unlock(&tty_mutex); | ||
1849 | return -ENODEV; | ||
1850 | } | ||
1851 | |||
1852 | driver = get_tty_driver(device, &index); | ||
1853 | if (!driver) { | ||
1854 | tty_unlock(); | ||
1855 | mutex_unlock(&tty_mutex); | ||
1856 | return -ENODEV; | ||
1857 | } | ||
1858 | got_driver: | ||
1859 | if (!tty) { | ||
1860 | /* check whether we're reopening an existing tty */ | ||
1861 | tty = tty_driver_lookup_tty(driver, inode, index); | ||
1862 | |||
1863 | if (IS_ERR(tty)) { | ||
1864 | tty_unlock(); | ||
1865 | mutex_unlock(&tty_mutex); | ||
1866 | return PTR_ERR(tty); | ||
1867 | } | ||
1868 | } | ||
1869 | |||
1870 | if (tty) { | ||
1871 | retval = tty_reopen(tty); | ||
1872 | if (retval) | ||
1873 | tty = ERR_PTR(retval); | ||
1874 | } else | ||
1875 | tty = tty_init_dev(driver, index, 0); | ||
1876 | |||
1877 | mutex_unlock(&tty_mutex); | ||
1878 | tty_driver_kref_put(driver); | ||
1879 | if (IS_ERR(tty)) { | ||
1880 | tty_unlock(); | ||
1881 | return PTR_ERR(tty); | ||
1882 | } | ||
1883 | |||
1884 | retval = tty_add_file(tty, filp); | ||
1885 | if (retval) { | ||
1886 | tty_unlock(); | ||
1887 | return retval; | ||
1888 | } | ||
1889 | |||
1890 | check_tty_count(tty, "tty_open"); | ||
1891 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | ||
1892 | tty->driver->subtype == PTY_TYPE_MASTER) | ||
1893 | noctty = 1; | ||
1894 | #ifdef TTY_DEBUG_HANGUP | ||
1895 | printk(KERN_DEBUG "opening %s...", tty->name); | ||
1896 | #endif | ||
1897 | if (!retval) { | ||
1898 | if (tty->ops->open) | ||
1899 | retval = tty->ops->open(tty, filp); | ||
1900 | else | ||
1901 | retval = -ENODEV; | ||
1902 | } | ||
1903 | filp->f_flags = saved_flags; | ||
1904 | |||
1905 | if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && | ||
1906 | !capable(CAP_SYS_ADMIN)) | ||
1907 | retval = -EBUSY; | ||
1908 | |||
1909 | if (retval) { | ||
1910 | #ifdef TTY_DEBUG_HANGUP | ||
1911 | printk(KERN_DEBUG "error %d in opening %s...", retval, | ||
1912 | tty->name); | ||
1913 | #endif | ||
1914 | tty_unlock(); /* need to call tty_release without BTM */ | ||
1915 | tty_release(inode, filp); | ||
1916 | if (retval != -ERESTARTSYS) | ||
1917 | return retval; | ||
1918 | |||
1919 | if (signal_pending(current)) | ||
1920 | return retval; | ||
1921 | |||
1922 | schedule(); | ||
1923 | /* | ||
1924 | * Need to reset f_op in case a hangup happened. | ||
1925 | */ | ||
1926 | tty_lock(); | ||
1927 | if (filp->f_op == &hung_up_tty_fops) | ||
1928 | filp->f_op = &tty_fops; | ||
1929 | tty_unlock(); | ||
1930 | goto retry_open; | ||
1931 | } | ||
1932 | tty_unlock(); | ||
1933 | |||
1934 | |||
1935 | mutex_lock(&tty_mutex); | ||
1936 | tty_lock(); | ||
1937 | spin_lock_irq(¤t->sighand->siglock); | ||
1938 | if (!noctty && | ||
1939 | current->signal->leader && | ||
1940 | !current->signal->tty && | ||
1941 | tty->session == NULL) | ||
1942 | __proc_set_tty(current, tty); | ||
1943 | spin_unlock_irq(¤t->sighand->siglock); | ||
1944 | tty_unlock(); | ||
1945 | mutex_unlock(&tty_mutex); | ||
1946 | return 0; | ||
1947 | } | ||
1948 | |||
1949 | |||
1950 | |||
1951 | /** | ||
1952 | * tty_poll - check tty status | ||
1953 | * @filp: file being polled | ||
1954 | * @wait: poll wait structures to update | ||
1955 | * | ||
1956 | * Call the line discipline polling method to obtain the poll | ||
1957 | * status of the device. | ||
1958 | * | ||
1959 | * Locking: locks called line discipline but ldisc poll method | ||
1960 | * may be re-entered freely by other callers. | ||
1961 | */ | ||
1962 | |||
1963 | static unsigned int tty_poll(struct file *filp, poll_table *wait) | ||
1964 | { | ||
1965 | struct tty_struct *tty = file_tty(filp); | ||
1966 | struct tty_ldisc *ld; | ||
1967 | int ret = 0; | ||
1968 | |||
1969 | if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll")) | ||
1970 | return 0; | ||
1971 | |||
1972 | ld = tty_ldisc_ref_wait(tty); | ||
1973 | if (ld->ops->poll) | ||
1974 | ret = (ld->ops->poll)(tty, filp, wait); | ||
1975 | tty_ldisc_deref(ld); | ||
1976 | return ret; | ||
1977 | } | ||
1978 | |||
1979 | static int __tty_fasync(int fd, struct file *filp, int on) | ||
1980 | { | ||
1981 | struct tty_struct *tty = file_tty(filp); | ||
1982 | unsigned long flags; | ||
1983 | int retval = 0; | ||
1984 | |||
1985 | if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync")) | ||
1986 | goto out; | ||
1987 | |||
1988 | retval = fasync_helper(fd, filp, on, &tty->fasync); | ||
1989 | if (retval <= 0) | ||
1990 | goto out; | ||
1991 | |||
1992 | if (on) { | ||
1993 | enum pid_type type; | ||
1994 | struct pid *pid; | ||
1995 | if (!waitqueue_active(&tty->read_wait)) | ||
1996 | tty->minimum_to_wake = 1; | ||
1997 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
1998 | if (tty->pgrp) { | ||
1999 | pid = tty->pgrp; | ||
2000 | type = PIDTYPE_PGID; | ||
2001 | } else { | ||
2002 | pid = task_pid(current); | ||
2003 | type = PIDTYPE_PID; | ||
2004 | } | ||
2005 | get_pid(pid); | ||
2006 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
2007 | retval = __f_setown(filp, pid, type, 0); | ||
2008 | put_pid(pid); | ||
2009 | if (retval) | ||
2010 | goto out; | ||
2011 | } else { | ||
2012 | if (!tty->fasync && !waitqueue_active(&tty->read_wait)) | ||
2013 | tty->minimum_to_wake = N_TTY_BUF_SIZE; | ||
2014 | } | ||
2015 | retval = 0; | ||
2016 | out: | ||
2017 | return retval; | ||
2018 | } | ||
2019 | |||
2020 | static int tty_fasync(int fd, struct file *filp, int on) | ||
2021 | { | ||
2022 | int retval; | ||
2023 | tty_lock(); | ||
2024 | retval = __tty_fasync(fd, filp, on); | ||
2025 | tty_unlock(); | ||
2026 | return retval; | ||
2027 | } | ||
2028 | |||
2029 | /** | ||
2030 | * tiocsti - fake input character | ||
2031 | * @tty: tty to fake input into | ||
2032 | * @p: pointer to character | ||
2033 | * | ||
2034 | * Fake input to a tty device. Does the necessary locking and | ||
2035 | * input management. | ||
2036 | * | ||
2037 | * FIXME: does not honour flow control ?? | ||
2038 | * | ||
2039 | * Locking: | ||
2040 | * Called functions take tty_ldisc_lock | ||
2041 | * current->signal->tty check is safe without locks | ||
2042 | * | ||
2043 | * FIXME: may race normal receive processing | ||
2044 | */ | ||
2045 | |||
2046 | static int tiocsti(struct tty_struct *tty, char __user *p) | ||
2047 | { | ||
2048 | char ch, mbz = 0; | ||
2049 | struct tty_ldisc *ld; | ||
2050 | |||
2051 | if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) | ||
2052 | return -EPERM; | ||
2053 | if (get_user(ch, p)) | ||
2054 | return -EFAULT; | ||
2055 | tty_audit_tiocsti(tty, ch); | ||
2056 | ld = tty_ldisc_ref_wait(tty); | ||
2057 | ld->ops->receive_buf(tty, &ch, &mbz, 1); | ||
2058 | tty_ldisc_deref(ld); | ||
2059 | return 0; | ||
2060 | } | ||
2061 | |||
2062 | /** | ||
2063 | * tiocgwinsz - implement window query ioctl | ||
2064 | * @tty; tty | ||
2065 | * @arg: user buffer for result | ||
2066 | * | ||
2067 | * Copies the kernel idea of the window size into the user buffer. | ||
2068 | * | ||
2069 | * Locking: tty->termios_mutex is taken to ensure the winsize data | ||
2070 | * is consistent. | ||
2071 | */ | ||
2072 | |||
2073 | static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) | ||
2074 | { | ||
2075 | int err; | ||
2076 | |||
2077 | mutex_lock(&tty->termios_mutex); | ||
2078 | err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); | ||
2079 | mutex_unlock(&tty->termios_mutex); | ||
2080 | |||
2081 | return err ? -EFAULT: 0; | ||
2082 | } | ||
2083 | |||
2084 | /** | ||
2085 | * tty_do_resize - resize event | ||
2086 | * @tty: tty being resized | ||
2087 | * @rows: rows (character) | ||
2088 | * @cols: cols (character) | ||
2089 | * | ||
2090 | * Update the termios variables and send the necessary signals to | ||
2091 | * peform a terminal resize correctly | ||
2092 | */ | ||
2093 | |||
2094 | int tty_do_resize(struct tty_struct *tty, struct winsize *ws) | ||
2095 | { | ||
2096 | struct pid *pgrp; | ||
2097 | unsigned long flags; | ||
2098 | |||
2099 | /* Lock the tty */ | ||
2100 | mutex_lock(&tty->termios_mutex); | ||
2101 | if (!memcmp(ws, &tty->winsize, sizeof(*ws))) | ||
2102 | goto done; | ||
2103 | /* Get the PID values and reference them so we can | ||
2104 | avoid holding the tty ctrl lock while sending signals */ | ||
2105 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
2106 | pgrp = get_pid(tty->pgrp); | ||
2107 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
2108 | |||
2109 | if (pgrp) | ||
2110 | kill_pgrp(pgrp, SIGWINCH, 1); | ||
2111 | put_pid(pgrp); | ||
2112 | |||
2113 | tty->winsize = *ws; | ||
2114 | done: | ||
2115 | mutex_unlock(&tty->termios_mutex); | ||
2116 | return 0; | ||
2117 | } | ||
2118 | |||
2119 | /** | ||
2120 | * tiocswinsz - implement window size set ioctl | ||
2121 | * @tty; tty side of tty | ||
2122 | * @arg: user buffer for result | ||
2123 | * | ||
2124 | * Copies the user idea of the window size to the kernel. Traditionally | ||
2125 | * this is just advisory information but for the Linux console it | ||
2126 | * actually has driver level meaning and triggers a VC resize. | ||
2127 | * | ||
2128 | * Locking: | ||
2129 | * Driver dependant. The default do_resize method takes the | ||
2130 | * tty termios mutex and ctrl_lock. The console takes its own lock | ||
2131 | * then calls into the default method. | ||
2132 | */ | ||
2133 | |||
2134 | static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) | ||
2135 | { | ||
2136 | struct winsize tmp_ws; | ||
2137 | if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) | ||
2138 | return -EFAULT; | ||
2139 | |||
2140 | if (tty->ops->resize) | ||
2141 | return tty->ops->resize(tty, &tmp_ws); | ||
2142 | else | ||
2143 | return tty_do_resize(tty, &tmp_ws); | ||
2144 | } | ||
2145 | |||
2146 | /** | ||
2147 | * tioccons - allow admin to move logical console | ||
2148 | * @file: the file to become console | ||
2149 | * | ||
2150 | * Allow the adminstrator to move the redirected console device | ||
2151 | * | ||
2152 | * Locking: uses redirect_lock to guard the redirect information | ||
2153 | */ | ||
2154 | |||
2155 | static int tioccons(struct file *file) | ||
2156 | { | ||
2157 | if (!capable(CAP_SYS_ADMIN)) | ||
2158 | return -EPERM; | ||
2159 | if (file->f_op->write == redirected_tty_write) { | ||
2160 | struct file *f; | ||
2161 | spin_lock(&redirect_lock); | ||
2162 | f = redirect; | ||
2163 | redirect = NULL; | ||
2164 | spin_unlock(&redirect_lock); | ||
2165 | if (f) | ||
2166 | fput(f); | ||
2167 | return 0; | ||
2168 | } | ||
2169 | spin_lock(&redirect_lock); | ||
2170 | if (redirect) { | ||
2171 | spin_unlock(&redirect_lock); | ||
2172 | return -EBUSY; | ||
2173 | } | ||
2174 | get_file(file); | ||
2175 | redirect = file; | ||
2176 | spin_unlock(&redirect_lock); | ||
2177 | return 0; | ||
2178 | } | ||
2179 | |||
2180 | /** | ||
2181 | * fionbio - non blocking ioctl | ||
2182 | * @file: file to set blocking value | ||
2183 | * @p: user parameter | ||
2184 | * | ||
2185 | * Historical tty interfaces had a blocking control ioctl before | ||
2186 | * the generic functionality existed. This piece of history is preserved | ||
2187 | * in the expected tty API of posix OS's. | ||
2188 | * | ||
2189 | * Locking: none, the open file handle ensures it won't go away. | ||
2190 | */ | ||
2191 | |||
2192 | static int fionbio(struct file *file, int __user *p) | ||
2193 | { | ||
2194 | int nonblock; | ||
2195 | |||
2196 | if (get_user(nonblock, p)) | ||
2197 | return -EFAULT; | ||
2198 | |||
2199 | spin_lock(&file->f_lock); | ||
2200 | if (nonblock) | ||
2201 | file->f_flags |= O_NONBLOCK; | ||
2202 | else | ||
2203 | file->f_flags &= ~O_NONBLOCK; | ||
2204 | spin_unlock(&file->f_lock); | ||
2205 | return 0; | ||
2206 | } | ||
2207 | |||
2208 | /** | ||
2209 | * tiocsctty - set controlling tty | ||
2210 | * @tty: tty structure | ||
2211 | * @arg: user argument | ||
2212 | * | ||
2213 | * This ioctl is used to manage job control. It permits a session | ||
2214 | * leader to set this tty as the controlling tty for the session. | ||
2215 | * | ||
2216 | * Locking: | ||
2217 | * Takes tty_mutex() to protect tty instance | ||
2218 | * Takes tasklist_lock internally to walk sessions | ||
2219 | * Takes ->siglock() when updating signal->tty | ||
2220 | */ | ||
2221 | |||
2222 | static int tiocsctty(struct tty_struct *tty, int arg) | ||
2223 | { | ||
2224 | int ret = 0; | ||
2225 | if (current->signal->leader && (task_session(current) == tty->session)) | ||
2226 | return ret; | ||
2227 | |||
2228 | mutex_lock(&tty_mutex); | ||
2229 | /* | ||
2230 | * The process must be a session leader and | ||
2231 | * not have a controlling tty already. | ||
2232 | */ | ||
2233 | if (!current->signal->leader || current->signal->tty) { | ||
2234 | ret = -EPERM; | ||
2235 | goto unlock; | ||
2236 | } | ||
2237 | |||
2238 | if (tty->session) { | ||
2239 | /* | ||
2240 | * This tty is already the controlling | ||
2241 | * tty for another session group! | ||
2242 | */ | ||
2243 | if (arg == 1 && capable(CAP_SYS_ADMIN)) { | ||
2244 | /* | ||
2245 | * Steal it away | ||
2246 | */ | ||
2247 | read_lock(&tasklist_lock); | ||
2248 | session_clear_tty(tty->session); | ||
2249 | read_unlock(&tasklist_lock); | ||
2250 | } else { | ||
2251 | ret = -EPERM; | ||
2252 | goto unlock; | ||
2253 | } | ||
2254 | } | ||
2255 | proc_set_tty(current, tty); | ||
2256 | unlock: | ||
2257 | mutex_unlock(&tty_mutex); | ||
2258 | return ret; | ||
2259 | } | ||
2260 | |||
2261 | /** | ||
2262 | * tty_get_pgrp - return a ref counted pgrp pid | ||
2263 | * @tty: tty to read | ||
2264 | * | ||
2265 | * Returns a refcounted instance of the pid struct for the process | ||
2266 | * group controlling the tty. | ||
2267 | */ | ||
2268 | |||
2269 | struct pid *tty_get_pgrp(struct tty_struct *tty) | ||
2270 | { | ||
2271 | unsigned long flags; | ||
2272 | struct pid *pgrp; | ||
2273 | |||
2274 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
2275 | pgrp = get_pid(tty->pgrp); | ||
2276 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
2277 | |||
2278 | return pgrp; | ||
2279 | } | ||
2280 | EXPORT_SYMBOL_GPL(tty_get_pgrp); | ||
2281 | |||
2282 | /** | ||
2283 | * tiocgpgrp - get process group | ||
2284 | * @tty: tty passed by user | ||
2285 | * @real_tty: tty side of the tty pased by the user if a pty else the tty | ||
2286 | * @p: returned pid | ||
2287 | * | ||
2288 | * Obtain the process group of the tty. If there is no process group | ||
2289 | * return an error. | ||
2290 | * | ||
2291 | * Locking: none. Reference to current->signal->tty is safe. | ||
2292 | */ | ||
2293 | |||
2294 | static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) | ||
2295 | { | ||
2296 | struct pid *pid; | ||
2297 | int ret; | ||
2298 | /* | ||
2299 | * (tty == real_tty) is a cheap way of | ||
2300 | * testing if the tty is NOT a master pty. | ||
2301 | */ | ||
2302 | if (tty == real_tty && current->signal->tty != real_tty) | ||
2303 | return -ENOTTY; | ||
2304 | pid = tty_get_pgrp(real_tty); | ||
2305 | ret = put_user(pid_vnr(pid), p); | ||
2306 | put_pid(pid); | ||
2307 | return ret; | ||
2308 | } | ||
2309 | |||
2310 | /** | ||
2311 | * tiocspgrp - attempt to set process group | ||
2312 | * @tty: tty passed by user | ||
2313 | * @real_tty: tty side device matching tty passed by user | ||
2314 | * @p: pid pointer | ||
2315 | * | ||
2316 | * Set the process group of the tty to the session passed. Only | ||
2317 | * permitted where the tty session is our session. | ||
2318 | * | ||
2319 | * Locking: RCU, ctrl lock | ||
2320 | */ | ||
2321 | |||
2322 | static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) | ||
2323 | { | ||
2324 | struct pid *pgrp; | ||
2325 | pid_t pgrp_nr; | ||
2326 | int retval = tty_check_change(real_tty); | ||
2327 | unsigned long flags; | ||
2328 | |||
2329 | if (retval == -EIO) | ||
2330 | return -ENOTTY; | ||
2331 | if (retval) | ||
2332 | return retval; | ||
2333 | if (!current->signal->tty || | ||
2334 | (current->signal->tty != real_tty) || | ||
2335 | (real_tty->session != task_session(current))) | ||
2336 | return -ENOTTY; | ||
2337 | if (get_user(pgrp_nr, p)) | ||
2338 | return -EFAULT; | ||
2339 | if (pgrp_nr < 0) | ||
2340 | return -EINVAL; | ||
2341 | rcu_read_lock(); | ||
2342 | pgrp = find_vpid(pgrp_nr); | ||
2343 | retval = -ESRCH; | ||
2344 | if (!pgrp) | ||
2345 | goto out_unlock; | ||
2346 | retval = -EPERM; | ||
2347 | if (session_of_pgrp(pgrp) != task_session(current)) | ||
2348 | goto out_unlock; | ||
2349 | retval = 0; | ||
2350 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
2351 | put_pid(real_tty->pgrp); | ||
2352 | real_tty->pgrp = get_pid(pgrp); | ||
2353 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
2354 | out_unlock: | ||
2355 | rcu_read_unlock(); | ||
2356 | return retval; | ||
2357 | } | ||
2358 | |||
2359 | /** | ||
2360 | * tiocgsid - get session id | ||
2361 | * @tty: tty passed by user | ||
2362 | * @real_tty: tty side of the tty pased by the user if a pty else the tty | ||
2363 | * @p: pointer to returned session id | ||
2364 | * | ||
2365 | * Obtain the session id of the tty. If there is no session | ||
2366 | * return an error. | ||
2367 | * | ||
2368 | * Locking: none. Reference to current->signal->tty is safe. | ||
2369 | */ | ||
2370 | |||
2371 | static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) | ||
2372 | { | ||
2373 | /* | ||
2374 | * (tty == real_tty) is a cheap way of | ||
2375 | * testing if the tty is NOT a master pty. | ||
2376 | */ | ||
2377 | if (tty == real_tty && current->signal->tty != real_tty) | ||
2378 | return -ENOTTY; | ||
2379 | if (!real_tty->session) | ||
2380 | return -ENOTTY; | ||
2381 | return put_user(pid_vnr(real_tty->session), p); | ||
2382 | } | ||
2383 | |||
2384 | /** | ||
2385 | * tiocsetd - set line discipline | ||
2386 | * @tty: tty device | ||
2387 | * @p: pointer to user data | ||
2388 | * | ||
2389 | * Set the line discipline according to user request. | ||
2390 | * | ||
2391 | * Locking: see tty_set_ldisc, this function is just a helper | ||
2392 | */ | ||
2393 | |||
2394 | static int tiocsetd(struct tty_struct *tty, int __user *p) | ||
2395 | { | ||
2396 | int ldisc; | ||
2397 | int ret; | ||
2398 | |||
2399 | if (get_user(ldisc, p)) | ||
2400 | return -EFAULT; | ||
2401 | |||
2402 | ret = tty_set_ldisc(tty, ldisc); | ||
2403 | |||
2404 | return ret; | ||
2405 | } | ||
2406 | |||
2407 | /** | ||
2408 | * send_break - performed time break | ||
2409 | * @tty: device to break on | ||
2410 | * @duration: timeout in mS | ||
2411 | * | ||
2412 | * Perform a timed break on hardware that lacks its own driver level | ||
2413 | * timed break functionality. | ||
2414 | * | ||
2415 | * Locking: | ||
2416 | * atomic_write_lock serializes | ||
2417 | * | ||
2418 | */ | ||
2419 | |||
2420 | static int send_break(struct tty_struct *tty, unsigned int duration) | ||
2421 | { | ||
2422 | int retval; | ||
2423 | |||
2424 | if (tty->ops->break_ctl == NULL) | ||
2425 | return 0; | ||
2426 | |||
2427 | if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) | ||
2428 | retval = tty->ops->break_ctl(tty, duration); | ||
2429 | else { | ||
2430 | /* Do the work ourselves */ | ||
2431 | if (tty_write_lock(tty, 0) < 0) | ||
2432 | return -EINTR; | ||
2433 | retval = tty->ops->break_ctl(tty, -1); | ||
2434 | if (retval) | ||
2435 | goto out; | ||
2436 | if (!signal_pending(current)) | ||
2437 | msleep_interruptible(duration); | ||
2438 | retval = tty->ops->break_ctl(tty, 0); | ||
2439 | out: | ||
2440 | tty_write_unlock(tty); | ||
2441 | if (signal_pending(current)) | ||
2442 | retval = -EINTR; | ||
2443 | } | ||
2444 | return retval; | ||
2445 | } | ||
2446 | |||
2447 | /** | ||
2448 | * tty_tiocmget - get modem status | ||
2449 | * @tty: tty device | ||
2450 | * @file: user file pointer | ||
2451 | * @p: pointer to result | ||
2452 | * | ||
2453 | * Obtain the modem status bits from the tty driver if the feature | ||
2454 | * is supported. Return -EINVAL if it is not available. | ||
2455 | * | ||
2456 | * Locking: none (up to the driver) | ||
2457 | */ | ||
2458 | |||
2459 | static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p) | ||
2460 | { | ||
2461 | int retval = -EINVAL; | ||
2462 | |||
2463 | if (tty->ops->tiocmget) { | ||
2464 | retval = tty->ops->tiocmget(tty, file); | ||
2465 | |||
2466 | if (retval >= 0) | ||
2467 | retval = put_user(retval, p); | ||
2468 | } | ||
2469 | return retval; | ||
2470 | } | ||
2471 | |||
2472 | /** | ||
2473 | * tty_tiocmset - set modem status | ||
2474 | * @tty: tty device | ||
2475 | * @file: user file pointer | ||
2476 | * @cmd: command - clear bits, set bits or set all | ||
2477 | * @p: pointer to desired bits | ||
2478 | * | ||
2479 | * Set the modem status bits from the tty driver if the feature | ||
2480 | * is supported. Return -EINVAL if it is not available. | ||
2481 | * | ||
2482 | * Locking: none (up to the driver) | ||
2483 | */ | ||
2484 | |||
2485 | static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd, | ||
2486 | unsigned __user *p) | ||
2487 | { | ||
2488 | int retval; | ||
2489 | unsigned int set, clear, val; | ||
2490 | |||
2491 | if (tty->ops->tiocmset == NULL) | ||
2492 | return -EINVAL; | ||
2493 | |||
2494 | retval = get_user(val, p); | ||
2495 | if (retval) | ||
2496 | return retval; | ||
2497 | set = clear = 0; | ||
2498 | switch (cmd) { | ||
2499 | case TIOCMBIS: | ||
2500 | set = val; | ||
2501 | break; | ||
2502 | case TIOCMBIC: | ||
2503 | clear = val; | ||
2504 | break; | ||
2505 | case TIOCMSET: | ||
2506 | set = val; | ||
2507 | clear = ~val; | ||
2508 | break; | ||
2509 | } | ||
2510 | set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; | ||
2511 | clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; | ||
2512 | return tty->ops->tiocmset(tty, file, set, clear); | ||
2513 | } | ||
2514 | |||
2515 | static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) | ||
2516 | { | ||
2517 | int retval = -EINVAL; | ||
2518 | struct serial_icounter_struct icount; | ||
2519 | memset(&icount, 0, sizeof(icount)); | ||
2520 | if (tty->ops->get_icount) | ||
2521 | retval = tty->ops->get_icount(tty, &icount); | ||
2522 | if (retval != 0) | ||
2523 | return retval; | ||
2524 | if (copy_to_user(arg, &icount, sizeof(icount))) | ||
2525 | return -EFAULT; | ||
2526 | return 0; | ||
2527 | } | ||
2528 | |||
2529 | struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) | ||
2530 | { | ||
2531 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | ||
2532 | tty->driver->subtype == PTY_TYPE_MASTER) | ||
2533 | tty = tty->link; | ||
2534 | return tty; | ||
2535 | } | ||
2536 | EXPORT_SYMBOL(tty_pair_get_tty); | ||
2537 | |||
2538 | struct tty_struct *tty_pair_get_pty(struct tty_struct *tty) | ||
2539 | { | ||
2540 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | ||
2541 | tty->driver->subtype == PTY_TYPE_MASTER) | ||
2542 | return tty; | ||
2543 | return tty->link; | ||
2544 | } | ||
2545 | EXPORT_SYMBOL(tty_pair_get_pty); | ||
2546 | |||
2547 | /* | ||
2548 | * Split this up, as gcc can choke on it otherwise.. | ||
2549 | */ | ||
2550 | long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | ||
2551 | { | ||
2552 | struct tty_struct *tty = file_tty(file); | ||
2553 | struct tty_struct *real_tty; | ||
2554 | void __user *p = (void __user *)arg; | ||
2555 | int retval; | ||
2556 | struct tty_ldisc *ld; | ||
2557 | struct inode *inode = file->f_dentry->d_inode; | ||
2558 | |||
2559 | if (tty_paranoia_check(tty, inode, "tty_ioctl")) | ||
2560 | return -EINVAL; | ||
2561 | |||
2562 | real_tty = tty_pair_get_tty(tty); | ||
2563 | |||
2564 | /* | ||
2565 | * Factor out some common prep work | ||
2566 | */ | ||
2567 | switch (cmd) { | ||
2568 | case TIOCSETD: | ||
2569 | case TIOCSBRK: | ||
2570 | case TIOCCBRK: | ||
2571 | case TCSBRK: | ||
2572 | case TCSBRKP: | ||
2573 | retval = tty_check_change(tty); | ||
2574 | if (retval) | ||
2575 | return retval; | ||
2576 | if (cmd != TIOCCBRK) { | ||
2577 | tty_wait_until_sent(tty, 0); | ||
2578 | if (signal_pending(current)) | ||
2579 | return -EINTR; | ||
2580 | } | ||
2581 | break; | ||
2582 | } | ||
2583 | |||
2584 | /* | ||
2585 | * Now do the stuff. | ||
2586 | */ | ||
2587 | switch (cmd) { | ||
2588 | case TIOCSTI: | ||
2589 | return tiocsti(tty, p); | ||
2590 | case TIOCGWINSZ: | ||
2591 | return tiocgwinsz(real_tty, p); | ||
2592 | case TIOCSWINSZ: | ||
2593 | return tiocswinsz(real_tty, p); | ||
2594 | case TIOCCONS: | ||
2595 | return real_tty != tty ? -EINVAL : tioccons(file); | ||
2596 | case FIONBIO: | ||
2597 | return fionbio(file, p); | ||
2598 | case TIOCEXCL: | ||
2599 | set_bit(TTY_EXCLUSIVE, &tty->flags); | ||
2600 | return 0; | ||
2601 | case TIOCNXCL: | ||
2602 | clear_bit(TTY_EXCLUSIVE, &tty->flags); | ||
2603 | return 0; | ||
2604 | case TIOCNOTTY: | ||
2605 | if (current->signal->tty != tty) | ||
2606 | return -ENOTTY; | ||
2607 | no_tty(); | ||
2608 | return 0; | ||
2609 | case TIOCSCTTY: | ||
2610 | return tiocsctty(tty, arg); | ||
2611 | case TIOCGPGRP: | ||
2612 | return tiocgpgrp(tty, real_tty, p); | ||
2613 | case TIOCSPGRP: | ||
2614 | return tiocspgrp(tty, real_tty, p); | ||
2615 | case TIOCGSID: | ||
2616 | return tiocgsid(tty, real_tty, p); | ||
2617 | case TIOCGETD: | ||
2618 | return put_user(tty->ldisc->ops->num, (int __user *)p); | ||
2619 | case TIOCSETD: | ||
2620 | return tiocsetd(tty, p); | ||
2621 | /* | ||
2622 | * Break handling | ||
2623 | */ | ||
2624 | case TIOCSBRK: /* Turn break on, unconditionally */ | ||
2625 | if (tty->ops->break_ctl) | ||
2626 | return tty->ops->break_ctl(tty, -1); | ||
2627 | return 0; | ||
2628 | case TIOCCBRK: /* Turn break off, unconditionally */ | ||
2629 | if (tty->ops->break_ctl) | ||
2630 | return tty->ops->break_ctl(tty, 0); | ||
2631 | return 0; | ||
2632 | case TCSBRK: /* SVID version: non-zero arg --> no break */ | ||
2633 | /* non-zero arg means wait for all output data | ||
2634 | * to be sent (performed above) but don't send break. | ||
2635 | * This is used by the tcdrain() termios function. | ||
2636 | */ | ||
2637 | if (!arg) | ||
2638 | return send_break(tty, 250); | ||
2639 | return 0; | ||
2640 | case TCSBRKP: /* support for POSIX tcsendbreak() */ | ||
2641 | return send_break(tty, arg ? arg*100 : 250); | ||
2642 | |||
2643 | case TIOCMGET: | ||
2644 | return tty_tiocmget(tty, file, p); | ||
2645 | case TIOCMSET: | ||
2646 | case TIOCMBIC: | ||
2647 | case TIOCMBIS: | ||
2648 | return tty_tiocmset(tty, file, cmd, p); | ||
2649 | case TIOCGICOUNT: | ||
2650 | retval = tty_tiocgicount(tty, p); | ||
2651 | /* For the moment allow fall through to the old method */ | ||
2652 | if (retval != -EINVAL) | ||
2653 | return retval; | ||
2654 | break; | ||
2655 | case TCFLSH: | ||
2656 | switch (arg) { | ||
2657 | case TCIFLUSH: | ||
2658 | case TCIOFLUSH: | ||
2659 | /* flush tty buffer and allow ldisc to process ioctl */ | ||
2660 | tty_buffer_flush(tty); | ||
2661 | break; | ||
2662 | } | ||
2663 | break; | ||
2664 | } | ||
2665 | if (tty->ops->ioctl) { | ||
2666 | retval = (tty->ops->ioctl)(tty, file, cmd, arg); | ||
2667 | if (retval != -ENOIOCTLCMD) | ||
2668 | return retval; | ||
2669 | } | ||
2670 | ld = tty_ldisc_ref_wait(tty); | ||
2671 | retval = -EINVAL; | ||
2672 | if (ld->ops->ioctl) { | ||
2673 | retval = ld->ops->ioctl(tty, file, cmd, arg); | ||
2674 | if (retval == -ENOIOCTLCMD) | ||
2675 | retval = -EINVAL; | ||
2676 | } | ||
2677 | tty_ldisc_deref(ld); | ||
2678 | return retval; | ||
2679 | } | ||
2680 | |||
2681 | #ifdef CONFIG_COMPAT | ||
2682 | static long tty_compat_ioctl(struct file *file, unsigned int cmd, | ||
2683 | unsigned long arg) | ||
2684 | { | ||
2685 | struct inode *inode = file->f_dentry->d_inode; | ||
2686 | struct tty_struct *tty = file_tty(file); | ||
2687 | struct tty_ldisc *ld; | ||
2688 | int retval = -ENOIOCTLCMD; | ||
2689 | |||
2690 | if (tty_paranoia_check(tty, inode, "tty_ioctl")) | ||
2691 | return -EINVAL; | ||
2692 | |||
2693 | if (tty->ops->compat_ioctl) { | ||
2694 | retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg); | ||
2695 | if (retval != -ENOIOCTLCMD) | ||
2696 | return retval; | ||
2697 | } | ||
2698 | |||
2699 | ld = tty_ldisc_ref_wait(tty); | ||
2700 | if (ld->ops->compat_ioctl) | ||
2701 | retval = ld->ops->compat_ioctl(tty, file, cmd, arg); | ||
2702 | tty_ldisc_deref(ld); | ||
2703 | |||
2704 | return retval; | ||
2705 | } | ||
2706 | #endif | ||
2707 | |||
2708 | /* | ||
2709 | * This implements the "Secure Attention Key" --- the idea is to | ||
2710 | * prevent trojan horses by killing all processes associated with this | ||
2711 | * tty when the user hits the "Secure Attention Key". Required for | ||
2712 | * super-paranoid applications --- see the Orange Book for more details. | ||
2713 | * | ||
2714 | * This code could be nicer; ideally it should send a HUP, wait a few | ||
2715 | * seconds, then send a INT, and then a KILL signal. But you then | ||
2716 | * have to coordinate with the init process, since all processes associated | ||
2717 | * with the current tty must be dead before the new getty is allowed | ||
2718 | * to spawn. | ||
2719 | * | ||
2720 | * Now, if it would be correct ;-/ The current code has a nasty hole - | ||
2721 | * it doesn't catch files in flight. We may send the descriptor to ourselves | ||
2722 | * via AF_UNIX socket, close it and later fetch from socket. FIXME. | ||
2723 | * | ||
2724 | * Nasty bug: do_SAK is being called in interrupt context. This can | ||
2725 | * deadlock. We punt it up to process context. AKPM - 16Mar2001 | ||
2726 | */ | ||
2727 | void __do_SAK(struct tty_struct *tty) | ||
2728 | { | ||
2729 | #ifdef TTY_SOFT_SAK | ||
2730 | tty_hangup(tty); | ||
2731 | #else | ||
2732 | struct task_struct *g, *p; | ||
2733 | struct pid *session; | ||
2734 | int i; | ||
2735 | struct file *filp; | ||
2736 | struct fdtable *fdt; | ||
2737 | |||
2738 | if (!tty) | ||
2739 | return; | ||
2740 | session = tty->session; | ||
2741 | |||
2742 | tty_ldisc_flush(tty); | ||
2743 | |||
2744 | tty_driver_flush_buffer(tty); | ||
2745 | |||
2746 | read_lock(&tasklist_lock); | ||
2747 | /* Kill the entire session */ | ||
2748 | do_each_pid_task(session, PIDTYPE_SID, p) { | ||
2749 | printk(KERN_NOTICE "SAK: killed process %d" | ||
2750 | " (%s): task_session(p)==tty->session\n", | ||
2751 | task_pid_nr(p), p->comm); | ||
2752 | send_sig(SIGKILL, p, 1); | ||
2753 | } while_each_pid_task(session, PIDTYPE_SID, p); | ||
2754 | /* Now kill any processes that happen to have the | ||
2755 | * tty open. | ||
2756 | */ | ||
2757 | do_each_thread(g, p) { | ||
2758 | if (p->signal->tty == tty) { | ||
2759 | printk(KERN_NOTICE "SAK: killed process %d" | ||
2760 | " (%s): task_session(p)==tty->session\n", | ||
2761 | task_pid_nr(p), p->comm); | ||
2762 | send_sig(SIGKILL, p, 1); | ||
2763 | continue; | ||
2764 | } | ||
2765 | task_lock(p); | ||
2766 | if (p->files) { | ||
2767 | /* | ||
2768 | * We don't take a ref to the file, so we must | ||
2769 | * hold ->file_lock instead. | ||
2770 | */ | ||
2771 | spin_lock(&p->files->file_lock); | ||
2772 | fdt = files_fdtable(p->files); | ||
2773 | for (i = 0; i < fdt->max_fds; i++) { | ||
2774 | filp = fcheck_files(p->files, i); | ||
2775 | if (!filp) | ||
2776 | continue; | ||
2777 | if (filp->f_op->read == tty_read && | ||
2778 | file_tty(filp) == tty) { | ||
2779 | printk(KERN_NOTICE "SAK: killed process %d" | ||
2780 | " (%s): fd#%d opened to the tty\n", | ||
2781 | task_pid_nr(p), p->comm, i); | ||
2782 | force_sig(SIGKILL, p); | ||
2783 | break; | ||
2784 | } | ||
2785 | } | ||
2786 | spin_unlock(&p->files->file_lock); | ||
2787 | } | ||
2788 | task_unlock(p); | ||
2789 | } while_each_thread(g, p); | ||
2790 | read_unlock(&tasklist_lock); | ||
2791 | #endif | ||
2792 | } | ||
2793 | |||
2794 | static void do_SAK_work(struct work_struct *work) | ||
2795 | { | ||
2796 | struct tty_struct *tty = | ||
2797 | container_of(work, struct tty_struct, SAK_work); | ||
2798 | __do_SAK(tty); | ||
2799 | } | ||
2800 | |||
2801 | /* | ||
2802 | * The tq handling here is a little racy - tty->SAK_work may already be queued. | ||
2803 | * Fortunately we don't need to worry, because if ->SAK_work is already queued, | ||
2804 | * the values which we write to it will be identical to the values which it | ||
2805 | * already has. --akpm | ||
2806 | */ | ||
2807 | void do_SAK(struct tty_struct *tty) | ||
2808 | { | ||
2809 | if (!tty) | ||
2810 | return; | ||
2811 | schedule_work(&tty->SAK_work); | ||
2812 | } | ||
2813 | |||
2814 | EXPORT_SYMBOL(do_SAK); | ||
2815 | |||
2816 | static int dev_match_devt(struct device *dev, void *data) | ||
2817 | { | ||
2818 | dev_t *devt = data; | ||
2819 | return dev->devt == *devt; | ||
2820 | } | ||
2821 | |||
2822 | /* Must put_device() after it's unused! */ | ||
2823 | static struct device *tty_get_device(struct tty_struct *tty) | ||
2824 | { | ||
2825 | dev_t devt = tty_devnum(tty); | ||
2826 | return class_find_device(tty_class, NULL, &devt, dev_match_devt); | ||
2827 | } | ||
2828 | |||
2829 | |||
2830 | /** | ||
2831 | * initialize_tty_struct | ||
2832 | * @tty: tty to initialize | ||
2833 | * | ||
2834 | * This subroutine initializes a tty structure that has been newly | ||
2835 | * allocated. | ||
2836 | * | ||
2837 | * Locking: none - tty in question must not be exposed at this point | ||
2838 | */ | ||
2839 | |||
2840 | void initialize_tty_struct(struct tty_struct *tty, | ||
2841 | struct tty_driver *driver, int idx) | ||
2842 | { | ||
2843 | memset(tty, 0, sizeof(struct tty_struct)); | ||
2844 | kref_init(&tty->kref); | ||
2845 | tty->magic = TTY_MAGIC; | ||
2846 | tty_ldisc_init(tty); | ||
2847 | tty->session = NULL; | ||
2848 | tty->pgrp = NULL; | ||
2849 | tty->overrun_time = jiffies; | ||
2850 | tty->buf.head = tty->buf.tail = NULL; | ||
2851 | tty_buffer_init(tty); | ||
2852 | mutex_init(&tty->termios_mutex); | ||
2853 | mutex_init(&tty->ldisc_mutex); | ||
2854 | init_waitqueue_head(&tty->write_wait); | ||
2855 | init_waitqueue_head(&tty->read_wait); | ||
2856 | INIT_WORK(&tty->hangup_work, do_tty_hangup); | ||
2857 | mutex_init(&tty->atomic_read_lock); | ||
2858 | mutex_init(&tty->atomic_write_lock); | ||
2859 | mutex_init(&tty->output_lock); | ||
2860 | mutex_init(&tty->echo_lock); | ||
2861 | spin_lock_init(&tty->read_lock); | ||
2862 | spin_lock_init(&tty->ctrl_lock); | ||
2863 | INIT_LIST_HEAD(&tty->tty_files); | ||
2864 | INIT_WORK(&tty->SAK_work, do_SAK_work); | ||
2865 | |||
2866 | tty->driver = driver; | ||
2867 | tty->ops = driver->ops; | ||
2868 | tty->index = idx; | ||
2869 | tty_line_name(driver, idx, tty->name); | ||
2870 | tty->dev = tty_get_device(tty); | ||
2871 | } | ||
2872 | |||
2873 | /** | ||
2874 | * tty_put_char - write one character to a tty | ||
2875 | * @tty: tty | ||
2876 | * @ch: character | ||
2877 | * | ||
2878 | * Write one byte to the tty using the provided put_char method | ||
2879 | * if present. Returns the number of characters successfully output. | ||
2880 | * | ||
2881 | * Note: the specific put_char operation in the driver layer may go | ||
2882 | * away soon. Don't call it directly, use this method | ||
2883 | */ | ||
2884 | |||
2885 | int tty_put_char(struct tty_struct *tty, unsigned char ch) | ||
2886 | { | ||
2887 | if (tty->ops->put_char) | ||
2888 | return tty->ops->put_char(tty, ch); | ||
2889 | return tty->ops->write(tty, &ch, 1); | ||
2890 | } | ||
2891 | EXPORT_SYMBOL_GPL(tty_put_char); | ||
2892 | |||
2893 | struct class *tty_class; | ||
2894 | |||
2895 | /** | ||
2896 | * tty_register_device - register a tty device | ||
2897 | * @driver: the tty driver that describes the tty device | ||
2898 | * @index: the index in the tty driver for this tty device | ||
2899 | * @device: a struct device that is associated with this tty device. | ||
2900 | * This field is optional, if there is no known struct device | ||
2901 | * for this tty device it can be set to NULL safely. | ||
2902 | * | ||
2903 | * Returns a pointer to the struct device for this tty device | ||
2904 | * (or ERR_PTR(-EFOO) on error). | ||
2905 | * | ||
2906 | * This call is required to be made to register an individual tty device | ||
2907 | * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If | ||
2908 | * that bit is not set, this function should not be called by a tty | ||
2909 | * driver. | ||
2910 | * | ||
2911 | * Locking: ?? | ||
2912 | */ | ||
2913 | |||
2914 | struct device *tty_register_device(struct tty_driver *driver, unsigned index, | ||
2915 | struct device *device) | ||
2916 | { | ||
2917 | char name[64]; | ||
2918 | dev_t dev = MKDEV(driver->major, driver->minor_start) + index; | ||
2919 | |||
2920 | if (index >= driver->num) { | ||
2921 | printk(KERN_ERR "Attempt to register invalid tty line number " | ||
2922 | " (%d).\n", index); | ||
2923 | return ERR_PTR(-EINVAL); | ||
2924 | } | ||
2925 | |||
2926 | if (driver->type == TTY_DRIVER_TYPE_PTY) | ||
2927 | pty_line_name(driver, index, name); | ||
2928 | else | ||
2929 | tty_line_name(driver, index, name); | ||
2930 | |||
2931 | return device_create(tty_class, device, dev, NULL, name); | ||
2932 | } | ||
2933 | EXPORT_SYMBOL(tty_register_device); | ||
2934 | |||
2935 | /** | ||
2936 | * tty_unregister_device - unregister a tty device | ||
2937 | * @driver: the tty driver that describes the tty device | ||
2938 | * @index: the index in the tty driver for this tty device | ||
2939 | * | ||
2940 | * If a tty device is registered with a call to tty_register_device() then | ||
2941 | * this function must be called when the tty device is gone. | ||
2942 | * | ||
2943 | * Locking: ?? | ||
2944 | */ | ||
2945 | |||
2946 | void tty_unregister_device(struct tty_driver *driver, unsigned index) | ||
2947 | { | ||
2948 | device_destroy(tty_class, | ||
2949 | MKDEV(driver->major, driver->minor_start) + index); | ||
2950 | } | ||
2951 | EXPORT_SYMBOL(tty_unregister_device); | ||
2952 | |||
2953 | struct tty_driver *alloc_tty_driver(int lines) | ||
2954 | { | ||
2955 | struct tty_driver *driver; | ||
2956 | |||
2957 | driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL); | ||
2958 | if (driver) { | ||
2959 | kref_init(&driver->kref); | ||
2960 | driver->magic = TTY_DRIVER_MAGIC; | ||
2961 | driver->num = lines; | ||
2962 | /* later we'll move allocation of tables here */ | ||
2963 | } | ||
2964 | return driver; | ||
2965 | } | ||
2966 | EXPORT_SYMBOL(alloc_tty_driver); | ||
2967 | |||
2968 | static void destruct_tty_driver(struct kref *kref) | ||
2969 | { | ||
2970 | struct tty_driver *driver = container_of(kref, struct tty_driver, kref); | ||
2971 | int i; | ||
2972 | struct ktermios *tp; | ||
2973 | void *p; | ||
2974 | |||
2975 | if (driver->flags & TTY_DRIVER_INSTALLED) { | ||
2976 | /* | ||
2977 | * Free the termios and termios_locked structures because | ||
2978 | * we don't want to get memory leaks when modular tty | ||
2979 | * drivers are removed from the kernel. | ||
2980 | */ | ||
2981 | for (i = 0; i < driver->num; i++) { | ||
2982 | tp = driver->termios[i]; | ||
2983 | if (tp) { | ||
2984 | driver->termios[i] = NULL; | ||
2985 | kfree(tp); | ||
2986 | } | ||
2987 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) | ||
2988 | tty_unregister_device(driver, i); | ||
2989 | } | ||
2990 | p = driver->ttys; | ||
2991 | proc_tty_unregister_driver(driver); | ||
2992 | driver->ttys = NULL; | ||
2993 | driver->termios = NULL; | ||
2994 | kfree(p); | ||
2995 | cdev_del(&driver->cdev); | ||
2996 | } | ||
2997 | kfree(driver); | ||
2998 | } | ||
2999 | |||
3000 | void tty_driver_kref_put(struct tty_driver *driver) | ||
3001 | { | ||
3002 | kref_put(&driver->kref, destruct_tty_driver); | ||
3003 | } | ||
3004 | EXPORT_SYMBOL(tty_driver_kref_put); | ||
3005 | |||
3006 | void tty_set_operations(struct tty_driver *driver, | ||
3007 | const struct tty_operations *op) | ||
3008 | { | ||
3009 | driver->ops = op; | ||
3010 | }; | ||
3011 | EXPORT_SYMBOL(tty_set_operations); | ||
3012 | |||
3013 | void put_tty_driver(struct tty_driver *d) | ||
3014 | { | ||
3015 | tty_driver_kref_put(d); | ||
3016 | } | ||
3017 | EXPORT_SYMBOL(put_tty_driver); | ||
3018 | |||
3019 | /* | ||
3020 | * Called by a tty driver to register itself. | ||
3021 | */ | ||
3022 | int tty_register_driver(struct tty_driver *driver) | ||
3023 | { | ||
3024 | int error; | ||
3025 | int i; | ||
3026 | dev_t dev; | ||
3027 | void **p = NULL; | ||
3028 | struct device *d; | ||
3029 | |||
3030 | if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) { | ||
3031 | p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL); | ||
3032 | if (!p) | ||
3033 | return -ENOMEM; | ||
3034 | } | ||
3035 | |||
3036 | if (!driver->major) { | ||
3037 | error = alloc_chrdev_region(&dev, driver->minor_start, | ||
3038 | driver->num, driver->name); | ||
3039 | if (!error) { | ||
3040 | driver->major = MAJOR(dev); | ||
3041 | driver->minor_start = MINOR(dev); | ||
3042 | } | ||
3043 | } else { | ||
3044 | dev = MKDEV(driver->major, driver->minor_start); | ||
3045 | error = register_chrdev_region(dev, driver->num, driver->name); | ||
3046 | } | ||
3047 | if (error < 0) { | ||
3048 | kfree(p); | ||
3049 | return error; | ||
3050 | } | ||
3051 | |||
3052 | if (p) { | ||
3053 | driver->ttys = (struct tty_struct **)p; | ||
3054 | driver->termios = (struct ktermios **)(p + driver->num); | ||
3055 | } else { | ||
3056 | driver->ttys = NULL; | ||
3057 | driver->termios = NULL; | ||
3058 | } | ||
3059 | |||
3060 | cdev_init(&driver->cdev, &tty_fops); | ||
3061 | driver->cdev.owner = driver->owner; | ||
3062 | error = cdev_add(&driver->cdev, dev, driver->num); | ||
3063 | if (error) { | ||
3064 | unregister_chrdev_region(dev, driver->num); | ||
3065 | driver->ttys = NULL; | ||
3066 | driver->termios = NULL; | ||
3067 | kfree(p); | ||
3068 | return error; | ||
3069 | } | ||
3070 | |||
3071 | mutex_lock(&tty_mutex); | ||
3072 | list_add(&driver->tty_drivers, &tty_drivers); | ||
3073 | mutex_unlock(&tty_mutex); | ||
3074 | |||
3075 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { | ||
3076 | for (i = 0; i < driver->num; i++) { | ||
3077 | d = tty_register_device(driver, i, NULL); | ||
3078 | if (IS_ERR(d)) { | ||
3079 | error = PTR_ERR(d); | ||
3080 | goto err; | ||
3081 | } | ||
3082 | } | ||
3083 | } | ||
3084 | proc_tty_register_driver(driver); | ||
3085 | driver->flags |= TTY_DRIVER_INSTALLED; | ||
3086 | return 0; | ||
3087 | |||
3088 | err: | ||
3089 | for (i--; i >= 0; i--) | ||
3090 | tty_unregister_device(driver, i); | ||
3091 | |||
3092 | mutex_lock(&tty_mutex); | ||
3093 | list_del(&driver->tty_drivers); | ||
3094 | mutex_unlock(&tty_mutex); | ||
3095 | |||
3096 | unregister_chrdev_region(dev, driver->num); | ||
3097 | driver->ttys = NULL; | ||
3098 | driver->termios = NULL; | ||
3099 | kfree(p); | ||
3100 | return error; | ||
3101 | } | ||
3102 | |||
3103 | EXPORT_SYMBOL(tty_register_driver); | ||
3104 | |||
3105 | /* | ||
3106 | * Called by a tty driver to unregister itself. | ||
3107 | */ | ||
3108 | int tty_unregister_driver(struct tty_driver *driver) | ||
3109 | { | ||
3110 | #if 0 | ||
3111 | /* FIXME */ | ||
3112 | if (driver->refcount) | ||
3113 | return -EBUSY; | ||
3114 | #endif | ||
3115 | unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), | ||
3116 | driver->num); | ||
3117 | mutex_lock(&tty_mutex); | ||
3118 | list_del(&driver->tty_drivers); | ||
3119 | mutex_unlock(&tty_mutex); | ||
3120 | return 0; | ||
3121 | } | ||
3122 | |||
3123 | EXPORT_SYMBOL(tty_unregister_driver); | ||
3124 | |||
3125 | dev_t tty_devnum(struct tty_struct *tty) | ||
3126 | { | ||
3127 | return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; | ||
3128 | } | ||
3129 | EXPORT_SYMBOL(tty_devnum); | ||
3130 | |||
3131 | void proc_clear_tty(struct task_struct *p) | ||
3132 | { | ||
3133 | unsigned long flags; | ||
3134 | struct tty_struct *tty; | ||
3135 | spin_lock_irqsave(&p->sighand->siglock, flags); | ||
3136 | tty = p->signal->tty; | ||
3137 | p->signal->tty = NULL; | ||
3138 | spin_unlock_irqrestore(&p->sighand->siglock, flags); | ||
3139 | tty_kref_put(tty); | ||
3140 | } | ||
3141 | |||
3142 | /* Called under the sighand lock */ | ||
3143 | |||
3144 | static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty) | ||
3145 | { | ||
3146 | if (tty) { | ||
3147 | unsigned long flags; | ||
3148 | /* We should not have a session or pgrp to put here but.... */ | ||
3149 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
3150 | put_pid(tty->session); | ||
3151 | put_pid(tty->pgrp); | ||
3152 | tty->pgrp = get_pid(task_pgrp(tsk)); | ||
3153 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
3154 | tty->session = get_pid(task_session(tsk)); | ||
3155 | if (tsk->signal->tty) { | ||
3156 | printk(KERN_DEBUG "tty not NULL!!\n"); | ||
3157 | tty_kref_put(tsk->signal->tty); | ||
3158 | } | ||
3159 | } | ||
3160 | put_pid(tsk->signal->tty_old_pgrp); | ||
3161 | tsk->signal->tty = tty_kref_get(tty); | ||
3162 | tsk->signal->tty_old_pgrp = NULL; | ||
3163 | } | ||
3164 | |||
3165 | static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty) | ||
3166 | { | ||
3167 | spin_lock_irq(&tsk->sighand->siglock); | ||
3168 | __proc_set_tty(tsk, tty); | ||
3169 | spin_unlock_irq(&tsk->sighand->siglock); | ||
3170 | } | ||
3171 | |||
3172 | struct tty_struct *get_current_tty(void) | ||
3173 | { | ||
3174 | struct tty_struct *tty; | ||
3175 | unsigned long flags; | ||
3176 | |||
3177 | spin_lock_irqsave(¤t->sighand->siglock, flags); | ||
3178 | tty = tty_kref_get(current->signal->tty); | ||
3179 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); | ||
3180 | return tty; | ||
3181 | } | ||
3182 | EXPORT_SYMBOL_GPL(get_current_tty); | ||
3183 | |||
3184 | void tty_default_fops(struct file_operations *fops) | ||
3185 | { | ||
3186 | *fops = tty_fops; | ||
3187 | } | ||
3188 | |||
3189 | /* | ||
3190 | * Initialize the console device. This is called *early*, so | ||
3191 | * we can't necessarily depend on lots of kernel help here. | ||
3192 | * Just do some early initializations, and do the complex setup | ||
3193 | * later. | ||
3194 | */ | ||
3195 | void __init console_init(void) | ||
3196 | { | ||
3197 | initcall_t *call; | ||
3198 | |||
3199 | /* Setup the default TTY line discipline. */ | ||
3200 | tty_ldisc_begin(); | ||
3201 | |||
3202 | /* | ||
3203 | * set up the console device so that later boot sequences can | ||
3204 | * inform about problems etc.. | ||
3205 | */ | ||
3206 | call = __con_initcall_start; | ||
3207 | while (call < __con_initcall_end) { | ||
3208 | (*call)(); | ||
3209 | call++; | ||
3210 | } | ||
3211 | } | ||
3212 | |||
3213 | static char *tty_devnode(struct device *dev, mode_t *mode) | ||
3214 | { | ||
3215 | if (!mode) | ||
3216 | return NULL; | ||
3217 | if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) || | ||
3218 | dev->devt == MKDEV(TTYAUX_MAJOR, 2)) | ||
3219 | *mode = 0666; | ||
3220 | return NULL; | ||
3221 | } | ||
3222 | |||
3223 | static int __init tty_class_init(void) | ||
3224 | { | ||
3225 | tty_class = class_create(THIS_MODULE, "tty"); | ||
3226 | if (IS_ERR(tty_class)) | ||
3227 | return PTR_ERR(tty_class); | ||
3228 | tty_class->devnode = tty_devnode; | ||
3229 | return 0; | ||
3230 | } | ||
3231 | |||
3232 | postcore_initcall(tty_class_init); | ||
3233 | |||
3234 | /* 3/2004 jmc: why do these devices exist? */ | ||
3235 | |||
3236 | static struct cdev tty_cdev, console_cdev; | ||
3237 | |||
3238 | /* | ||
3239 | * Ok, now we can initialize the rest of the tty devices and can count | ||
3240 | * on memory allocations, interrupts etc.. | ||
3241 | */ | ||
3242 | int __init tty_init(void) | ||
3243 | { | ||
3244 | cdev_init(&tty_cdev, &tty_fops); | ||
3245 | if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || | ||
3246 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) | ||
3247 | panic("Couldn't register /dev/tty driver\n"); | ||
3248 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, | ||
3249 | "tty"); | ||
3250 | |||
3251 | cdev_init(&console_cdev, &console_fops); | ||
3252 | if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || | ||
3253 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) | ||
3254 | panic("Couldn't register /dev/console driver\n"); | ||
3255 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, | ||
3256 | "console"); | ||
3257 | |||
3258 | #ifdef CONFIG_VT | ||
3259 | vty_init(&console_fops); | ||
3260 | #endif | ||
3261 | return 0; | ||
3262 | } | ||
3263 | |||
diff --git a/drivers/tty/tty_ioctl.c b/drivers/tty/tty_ioctl.c new file mode 100644 index 000000000000..0c1889971459 --- /dev/null +++ b/drivers/tty/tty_ioctl.c | |||
@@ -0,0 +1,1179 @@ | |||
1 | /* | ||
2 | * linux/drivers/char/tty_ioctl.c | ||
3 | * | ||
4 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | ||
5 | * | ||
6 | * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines | ||
7 | * which can be dynamically activated and de-activated by the line | ||
8 | * discipline handling modules (like SLIP). | ||
9 | */ | ||
10 | |||
11 | #include <linux/types.h> | ||
12 | #include <linux/termios.h> | ||
13 | #include <linux/errno.h> | ||
14 | #include <linux/sched.h> | ||
15 | #include <linux/kernel.h> | ||
16 | #include <linux/major.h> | ||
17 | #include <linux/tty.h> | ||
18 | #include <linux/fcntl.h> | ||
19 | #include <linux/string.h> | ||
20 | #include <linux/mm.h> | ||
21 | #include <linux/module.h> | ||
22 | #include <linux/bitops.h> | ||
23 | #include <linux/mutex.h> | ||
24 | |||
25 | #include <asm/io.h> | ||
26 | #include <asm/uaccess.h> | ||
27 | #include <asm/system.h> | ||
28 | |||
29 | #undef TTY_DEBUG_WAIT_UNTIL_SENT | ||
30 | |||
31 | #undef DEBUG | ||
32 | |||
33 | /* | ||
34 | * Internal flag options for termios setting behavior | ||
35 | */ | ||
36 | #define TERMIOS_FLUSH 1 | ||
37 | #define TERMIOS_WAIT 2 | ||
38 | #define TERMIOS_TERMIO 4 | ||
39 | #define TERMIOS_OLD 8 | ||
40 | |||
41 | |||
42 | /** | ||
43 | * tty_chars_in_buffer - characters pending | ||
44 | * @tty: terminal | ||
45 | * | ||
46 | * Return the number of bytes of data in the device private | ||
47 | * output queue. If no private method is supplied there is assumed | ||
48 | * to be no queue on the device. | ||
49 | */ | ||
50 | |||
51 | int tty_chars_in_buffer(struct tty_struct *tty) | ||
52 | { | ||
53 | if (tty->ops->chars_in_buffer) | ||
54 | return tty->ops->chars_in_buffer(tty); | ||
55 | else | ||
56 | return 0; | ||
57 | } | ||
58 | EXPORT_SYMBOL(tty_chars_in_buffer); | ||
59 | |||
60 | /** | ||
61 | * tty_write_room - write queue space | ||
62 | * @tty: terminal | ||
63 | * | ||
64 | * Return the number of bytes that can be queued to this device | ||
65 | * at the present time. The result should be treated as a guarantee | ||
66 | * and the driver cannot offer a value it later shrinks by more than | ||
67 | * the number of bytes written. If no method is provided 2K is always | ||
68 | * returned and data may be lost as there will be no flow control. | ||
69 | */ | ||
70 | |||
71 | int tty_write_room(struct tty_struct *tty) | ||
72 | { | ||
73 | if (tty->ops->write_room) | ||
74 | return tty->ops->write_room(tty); | ||
75 | return 2048; | ||
76 | } | ||
77 | EXPORT_SYMBOL(tty_write_room); | ||
78 | |||
79 | /** | ||
80 | * tty_driver_flush_buffer - discard internal buffer | ||
81 | * @tty: terminal | ||
82 | * | ||
83 | * Discard the internal output buffer for this device. If no method | ||
84 | * is provided then either the buffer cannot be hardware flushed or | ||
85 | * there is no buffer driver side. | ||
86 | */ | ||
87 | void tty_driver_flush_buffer(struct tty_struct *tty) | ||
88 | { | ||
89 | if (tty->ops->flush_buffer) | ||
90 | tty->ops->flush_buffer(tty); | ||
91 | } | ||
92 | EXPORT_SYMBOL(tty_driver_flush_buffer); | ||
93 | |||
94 | /** | ||
95 | * tty_throttle - flow control | ||
96 | * @tty: terminal | ||
97 | * | ||
98 | * Indicate that a tty should stop transmitting data down the stack. | ||
99 | * Takes the termios mutex to protect against parallel throttle/unthrottle | ||
100 | * and also to ensure the driver can consistently reference its own | ||
101 | * termios data at this point when implementing software flow control. | ||
102 | */ | ||
103 | |||
104 | void tty_throttle(struct tty_struct *tty) | ||
105 | { | ||
106 | mutex_lock(&tty->termios_mutex); | ||
107 | /* check TTY_THROTTLED first so it indicates our state */ | ||
108 | if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) && | ||
109 | tty->ops->throttle) | ||
110 | tty->ops->throttle(tty); | ||
111 | mutex_unlock(&tty->termios_mutex); | ||
112 | } | ||
113 | EXPORT_SYMBOL(tty_throttle); | ||
114 | |||
115 | /** | ||
116 | * tty_unthrottle - flow control | ||
117 | * @tty: terminal | ||
118 | * | ||
119 | * Indicate that a tty may continue transmitting data down the stack. | ||
120 | * Takes the termios mutex to protect against parallel throttle/unthrottle | ||
121 | * and also to ensure the driver can consistently reference its own | ||
122 | * termios data at this point when implementing software flow control. | ||
123 | * | ||
124 | * Drivers should however remember that the stack can issue a throttle, | ||
125 | * then change flow control method, then unthrottle. | ||
126 | */ | ||
127 | |||
128 | void tty_unthrottle(struct tty_struct *tty) | ||
129 | { | ||
130 | mutex_lock(&tty->termios_mutex); | ||
131 | if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) && | ||
132 | tty->ops->unthrottle) | ||
133 | tty->ops->unthrottle(tty); | ||
134 | mutex_unlock(&tty->termios_mutex); | ||
135 | } | ||
136 | EXPORT_SYMBOL(tty_unthrottle); | ||
137 | |||
138 | /** | ||
139 | * tty_wait_until_sent - wait for I/O to finish | ||
140 | * @tty: tty we are waiting for | ||
141 | * @timeout: how long we will wait | ||
142 | * | ||
143 | * Wait for characters pending in a tty driver to hit the wire, or | ||
144 | * for a timeout to occur (eg due to flow control) | ||
145 | * | ||
146 | * Locking: none | ||
147 | */ | ||
148 | |||
149 | void tty_wait_until_sent(struct tty_struct *tty, long timeout) | ||
150 | { | ||
151 | #ifdef TTY_DEBUG_WAIT_UNTIL_SENT | ||
152 | char buf[64]; | ||
153 | |||
154 | printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf)); | ||
155 | #endif | ||
156 | if (!timeout) | ||
157 | timeout = MAX_SCHEDULE_TIMEOUT; | ||
158 | if (wait_event_interruptible_timeout(tty->write_wait, | ||
159 | !tty_chars_in_buffer(tty), timeout) >= 0) { | ||
160 | if (tty->ops->wait_until_sent) | ||
161 | tty->ops->wait_until_sent(tty, timeout); | ||
162 | } | ||
163 | } | ||
164 | EXPORT_SYMBOL(tty_wait_until_sent); | ||
165 | |||
166 | |||
167 | /* | ||
168 | * Termios Helper Methods | ||
169 | */ | ||
170 | |||
171 | static void unset_locked_termios(struct ktermios *termios, | ||
172 | struct ktermios *old, | ||
173 | struct ktermios *locked) | ||
174 | { | ||
175 | int i; | ||
176 | |||
177 | #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z))) | ||
178 | |||
179 | if (!locked) { | ||
180 | printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n"); | ||
181 | return; | ||
182 | } | ||
183 | |||
184 | NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag); | ||
185 | NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag); | ||
186 | NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag); | ||
187 | NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag); | ||
188 | termios->c_line = locked->c_line ? old->c_line : termios->c_line; | ||
189 | for (i = 0; i < NCCS; i++) | ||
190 | termios->c_cc[i] = locked->c_cc[i] ? | ||
191 | old->c_cc[i] : termios->c_cc[i]; | ||
192 | /* FIXME: What should we do for i/ospeed */ | ||
193 | } | ||
194 | |||
195 | /* | ||
196 | * Routine which returns the baud rate of the tty | ||
197 | * | ||
198 | * Note that the baud_table needs to be kept in sync with the | ||
199 | * include/asm/termbits.h file. | ||
200 | */ | ||
201 | static const speed_t baud_table[] = { | ||
202 | 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, | ||
203 | 9600, 19200, 38400, 57600, 115200, 230400, 460800, | ||
204 | #ifdef __sparc__ | ||
205 | 76800, 153600, 307200, 614400, 921600 | ||
206 | #else | ||
207 | 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, | ||
208 | 2500000, 3000000, 3500000, 4000000 | ||
209 | #endif | ||
210 | }; | ||
211 | |||
212 | #ifndef __sparc__ | ||
213 | static const tcflag_t baud_bits[] = { | ||
214 | B0, B50, B75, B110, B134, B150, B200, B300, B600, | ||
215 | B1200, B1800, B2400, B4800, B9600, B19200, B38400, | ||
216 | B57600, B115200, B230400, B460800, B500000, B576000, | ||
217 | B921600, B1000000, B1152000, B1500000, B2000000, B2500000, | ||
218 | B3000000, B3500000, B4000000 | ||
219 | }; | ||
220 | #else | ||
221 | static const tcflag_t baud_bits[] = { | ||
222 | B0, B50, B75, B110, B134, B150, B200, B300, B600, | ||
223 | B1200, B1800, B2400, B4800, B9600, B19200, B38400, | ||
224 | B57600, B115200, B230400, B460800, B76800, B153600, | ||
225 | B307200, B614400, B921600 | ||
226 | }; | ||
227 | #endif | ||
228 | |||
229 | static int n_baud_table = ARRAY_SIZE(baud_table); | ||
230 | |||
231 | /** | ||
232 | * tty_termios_baud_rate | ||
233 | * @termios: termios structure | ||
234 | * | ||
235 | * Convert termios baud rate data into a speed. This should be called | ||
236 | * with the termios lock held if this termios is a terminal termios | ||
237 | * structure. May change the termios data. Device drivers can call this | ||
238 | * function but should use ->c_[io]speed directly as they are updated. | ||
239 | * | ||
240 | * Locking: none | ||
241 | */ | ||
242 | |||
243 | speed_t tty_termios_baud_rate(struct ktermios *termios) | ||
244 | { | ||
245 | unsigned int cbaud; | ||
246 | |||
247 | cbaud = termios->c_cflag & CBAUD; | ||
248 | |||
249 | #ifdef BOTHER | ||
250 | /* Magic token for arbitary speed via c_ispeed/c_ospeed */ | ||
251 | if (cbaud == BOTHER) | ||
252 | return termios->c_ospeed; | ||
253 | #endif | ||
254 | if (cbaud & CBAUDEX) { | ||
255 | cbaud &= ~CBAUDEX; | ||
256 | |||
257 | if (cbaud < 1 || cbaud + 15 > n_baud_table) | ||
258 | termios->c_cflag &= ~CBAUDEX; | ||
259 | else | ||
260 | cbaud += 15; | ||
261 | } | ||
262 | return baud_table[cbaud]; | ||
263 | } | ||
264 | EXPORT_SYMBOL(tty_termios_baud_rate); | ||
265 | |||
266 | /** | ||
267 | * tty_termios_input_baud_rate | ||
268 | * @termios: termios structure | ||
269 | * | ||
270 | * Convert termios baud rate data into a speed. This should be called | ||
271 | * with the termios lock held if this termios is a terminal termios | ||
272 | * structure. May change the termios data. Device drivers can call this | ||
273 | * function but should use ->c_[io]speed directly as they are updated. | ||
274 | * | ||
275 | * Locking: none | ||
276 | */ | ||
277 | |||
278 | speed_t tty_termios_input_baud_rate(struct ktermios *termios) | ||
279 | { | ||
280 | #ifdef IBSHIFT | ||
281 | unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD; | ||
282 | |||
283 | if (cbaud == B0) | ||
284 | return tty_termios_baud_rate(termios); | ||
285 | |||
286 | /* Magic token for arbitary speed via c_ispeed*/ | ||
287 | if (cbaud == BOTHER) | ||
288 | return termios->c_ispeed; | ||
289 | |||
290 | if (cbaud & CBAUDEX) { | ||
291 | cbaud &= ~CBAUDEX; | ||
292 | |||
293 | if (cbaud < 1 || cbaud + 15 > n_baud_table) | ||
294 | termios->c_cflag &= ~(CBAUDEX << IBSHIFT); | ||
295 | else | ||
296 | cbaud += 15; | ||
297 | } | ||
298 | return baud_table[cbaud]; | ||
299 | #else | ||
300 | return tty_termios_baud_rate(termios); | ||
301 | #endif | ||
302 | } | ||
303 | EXPORT_SYMBOL(tty_termios_input_baud_rate); | ||
304 | |||
305 | /** | ||
306 | * tty_termios_encode_baud_rate | ||
307 | * @termios: ktermios structure holding user requested state | ||
308 | * @ispeed: input speed | ||
309 | * @ospeed: output speed | ||
310 | * | ||
311 | * Encode the speeds set into the passed termios structure. This is | ||
312 | * used as a library helper for drivers os that they can report back | ||
313 | * the actual speed selected when it differs from the speed requested | ||
314 | * | ||
315 | * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour | ||
316 | * we need to carefully set the bits when the user does not get the | ||
317 | * desired speed. We allow small margins and preserve as much of possible | ||
318 | * of the input intent to keep compatibility. | ||
319 | * | ||
320 | * Locking: Caller should hold termios lock. This is already held | ||
321 | * when calling this function from the driver termios handler. | ||
322 | * | ||
323 | * The ifdefs deal with platforms whose owners have yet to update them | ||
324 | * and will all go away once this is done. | ||
325 | */ | ||
326 | |||
327 | void tty_termios_encode_baud_rate(struct ktermios *termios, | ||
328 | speed_t ibaud, speed_t obaud) | ||
329 | { | ||
330 | int i = 0; | ||
331 | int ifound = -1, ofound = -1; | ||
332 | int iclose = ibaud/50, oclose = obaud/50; | ||
333 | int ibinput = 0; | ||
334 | |||
335 | if (obaud == 0) /* CD dropped */ | ||
336 | ibaud = 0; /* Clear ibaud to be sure */ | ||
337 | |||
338 | termios->c_ispeed = ibaud; | ||
339 | termios->c_ospeed = obaud; | ||
340 | |||
341 | #ifdef BOTHER | ||
342 | /* If the user asked for a precise weird speed give a precise weird | ||
343 | answer. If they asked for a Bfoo speed they many have problems | ||
344 | digesting non-exact replies so fuzz a bit */ | ||
345 | |||
346 | if ((termios->c_cflag & CBAUD) == BOTHER) | ||
347 | oclose = 0; | ||
348 | if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER) | ||
349 | iclose = 0; | ||
350 | if ((termios->c_cflag >> IBSHIFT) & CBAUD) | ||
351 | ibinput = 1; /* An input speed was specified */ | ||
352 | #endif | ||
353 | termios->c_cflag &= ~CBAUD; | ||
354 | |||
355 | /* | ||
356 | * Our goal is to find a close match to the standard baud rate | ||
357 | * returned. Walk the baud rate table and if we get a very close | ||
358 | * match then report back the speed as a POSIX Bxxxx value by | ||
359 | * preference | ||
360 | */ | ||
361 | |||
362 | do { | ||
363 | if (obaud - oclose <= baud_table[i] && | ||
364 | obaud + oclose >= baud_table[i]) { | ||
365 | termios->c_cflag |= baud_bits[i]; | ||
366 | ofound = i; | ||
367 | } | ||
368 | if (ibaud - iclose <= baud_table[i] && | ||
369 | ibaud + iclose >= baud_table[i]) { | ||
370 | /* For the case input == output don't set IBAUD bits | ||
371 | if the user didn't do so */ | ||
372 | if (ofound == i && !ibinput) | ||
373 | ifound = i; | ||
374 | #ifdef IBSHIFT | ||
375 | else { | ||
376 | ifound = i; | ||
377 | termios->c_cflag |= (baud_bits[i] << IBSHIFT); | ||
378 | } | ||
379 | #endif | ||
380 | } | ||
381 | } while (++i < n_baud_table); | ||
382 | |||
383 | /* | ||
384 | * If we found no match then use BOTHER if provided or warn | ||
385 | * the user their platform maintainer needs to wake up if not. | ||
386 | */ | ||
387 | #ifdef BOTHER | ||
388 | if (ofound == -1) | ||
389 | termios->c_cflag |= BOTHER; | ||
390 | /* Set exact input bits only if the input and output differ or the | ||
391 | user already did */ | ||
392 | if (ifound == -1 && (ibaud != obaud || ibinput)) | ||
393 | termios->c_cflag |= (BOTHER << IBSHIFT); | ||
394 | #else | ||
395 | if (ifound == -1 || ofound == -1) { | ||
396 | printk_once(KERN_WARNING "tty: Unable to return correct " | ||
397 | "speed data as your architecture needs updating.\n"); | ||
398 | } | ||
399 | #endif | ||
400 | } | ||
401 | EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate); | ||
402 | |||
403 | /** | ||
404 | * tty_encode_baud_rate - set baud rate of the tty | ||
405 | * @ibaud: input baud rate | ||
406 | * @obad: output baud rate | ||
407 | * | ||
408 | * Update the current termios data for the tty with the new speed | ||
409 | * settings. The caller must hold the termios_mutex for the tty in | ||
410 | * question. | ||
411 | */ | ||
412 | |||
413 | void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud) | ||
414 | { | ||
415 | tty_termios_encode_baud_rate(tty->termios, ibaud, obaud); | ||
416 | } | ||
417 | EXPORT_SYMBOL_GPL(tty_encode_baud_rate); | ||
418 | |||
419 | /** | ||
420 | * tty_get_baud_rate - get tty bit rates | ||
421 | * @tty: tty to query | ||
422 | * | ||
423 | * Returns the baud rate as an integer for this terminal. The | ||
424 | * termios lock must be held by the caller and the terminal bit | ||
425 | * flags may be updated. | ||
426 | * | ||
427 | * Locking: none | ||
428 | */ | ||
429 | |||
430 | speed_t tty_get_baud_rate(struct tty_struct *tty) | ||
431 | { | ||
432 | speed_t baud = tty_termios_baud_rate(tty->termios); | ||
433 | |||
434 | if (baud == 38400 && tty->alt_speed) { | ||
435 | if (!tty->warned) { | ||
436 | printk(KERN_WARNING "Use of setserial/setrocket to " | ||
437 | "set SPD_* flags is deprecated\n"); | ||
438 | tty->warned = 1; | ||
439 | } | ||
440 | baud = tty->alt_speed; | ||
441 | } | ||
442 | |||
443 | return baud; | ||
444 | } | ||
445 | EXPORT_SYMBOL(tty_get_baud_rate); | ||
446 | |||
447 | /** | ||
448 | * tty_termios_copy_hw - copy hardware settings | ||
449 | * @new: New termios | ||
450 | * @old: Old termios | ||
451 | * | ||
452 | * Propogate the hardware specific terminal setting bits from | ||
453 | * the old termios structure to the new one. This is used in cases | ||
454 | * where the hardware does not support reconfiguration or as a helper | ||
455 | * in some cases where only minimal reconfiguration is supported | ||
456 | */ | ||
457 | |||
458 | void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old) | ||
459 | { | ||
460 | /* The bits a dumb device handles in software. Smart devices need | ||
461 | to always provide a set_termios method */ | ||
462 | new->c_cflag &= HUPCL | CREAD | CLOCAL; | ||
463 | new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL); | ||
464 | new->c_ispeed = old->c_ispeed; | ||
465 | new->c_ospeed = old->c_ospeed; | ||
466 | } | ||
467 | EXPORT_SYMBOL(tty_termios_copy_hw); | ||
468 | |||
469 | /** | ||
470 | * tty_termios_hw_change - check for setting change | ||
471 | * @a: termios | ||
472 | * @b: termios to compare | ||
473 | * | ||
474 | * Check if any of the bits that affect a dumb device have changed | ||
475 | * between the two termios structures, or a speed change is needed. | ||
476 | */ | ||
477 | |||
478 | int tty_termios_hw_change(struct ktermios *a, struct ktermios *b) | ||
479 | { | ||
480 | if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed) | ||
481 | return 1; | ||
482 | if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL)) | ||
483 | return 1; | ||
484 | return 0; | ||
485 | } | ||
486 | EXPORT_SYMBOL(tty_termios_hw_change); | ||
487 | |||
488 | /** | ||
489 | * change_termios - update termios values | ||
490 | * @tty: tty to update | ||
491 | * @new_termios: desired new value | ||
492 | * | ||
493 | * Perform updates to the termios values set on this terminal. There | ||
494 | * is a bit of layering violation here with n_tty in terms of the | ||
495 | * internal knowledge of this function. | ||
496 | * | ||
497 | * Locking: termios_mutex | ||
498 | */ | ||
499 | |||
500 | static void change_termios(struct tty_struct *tty, struct ktermios *new_termios) | ||
501 | { | ||
502 | struct ktermios old_termios; | ||
503 | struct tty_ldisc *ld; | ||
504 | unsigned long flags; | ||
505 | |||
506 | /* | ||
507 | * Perform the actual termios internal changes under lock. | ||
508 | */ | ||
509 | |||
510 | |||
511 | /* FIXME: we need to decide on some locking/ordering semantics | ||
512 | for the set_termios notification eventually */ | ||
513 | mutex_lock(&tty->termios_mutex); | ||
514 | old_termios = *tty->termios; | ||
515 | *tty->termios = *new_termios; | ||
516 | unset_locked_termios(tty->termios, &old_termios, tty->termios_locked); | ||
517 | |||
518 | /* See if packet mode change of state. */ | ||
519 | if (tty->link && tty->link->packet) { | ||
520 | int extproc = (old_termios.c_lflag & EXTPROC) | | ||
521 | (tty->termios->c_lflag & EXTPROC); | ||
522 | int old_flow = ((old_termios.c_iflag & IXON) && | ||
523 | (old_termios.c_cc[VSTOP] == '\023') && | ||
524 | (old_termios.c_cc[VSTART] == '\021')); | ||
525 | int new_flow = (I_IXON(tty) && | ||
526 | STOP_CHAR(tty) == '\023' && | ||
527 | START_CHAR(tty) == '\021'); | ||
528 | if ((old_flow != new_flow) || extproc) { | ||
529 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
530 | if (old_flow != new_flow) { | ||
531 | tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP); | ||
532 | if (new_flow) | ||
533 | tty->ctrl_status |= TIOCPKT_DOSTOP; | ||
534 | else | ||
535 | tty->ctrl_status |= TIOCPKT_NOSTOP; | ||
536 | } | ||
537 | if (extproc) | ||
538 | tty->ctrl_status |= TIOCPKT_IOCTL; | ||
539 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
540 | wake_up_interruptible(&tty->link->read_wait); | ||
541 | } | ||
542 | } | ||
543 | |||
544 | if (tty->ops->set_termios) | ||
545 | (*tty->ops->set_termios)(tty, &old_termios); | ||
546 | else | ||
547 | tty_termios_copy_hw(tty->termios, &old_termios); | ||
548 | |||
549 | ld = tty_ldisc_ref(tty); | ||
550 | if (ld != NULL) { | ||
551 | if (ld->ops->set_termios) | ||
552 | (ld->ops->set_termios)(tty, &old_termios); | ||
553 | tty_ldisc_deref(ld); | ||
554 | } | ||
555 | mutex_unlock(&tty->termios_mutex); | ||
556 | } | ||
557 | |||
558 | /** | ||
559 | * set_termios - set termios values for a tty | ||
560 | * @tty: terminal device | ||
561 | * @arg: user data | ||
562 | * @opt: option information | ||
563 | * | ||
564 | * Helper function to prepare termios data and run necessary other | ||
565 | * functions before using change_termios to do the actual changes. | ||
566 | * | ||
567 | * Locking: | ||
568 | * Called functions take ldisc and termios_mutex locks | ||
569 | */ | ||
570 | |||
571 | static int set_termios(struct tty_struct *tty, void __user *arg, int opt) | ||
572 | { | ||
573 | struct ktermios tmp_termios; | ||
574 | struct tty_ldisc *ld; | ||
575 | int retval = tty_check_change(tty); | ||
576 | |||
577 | if (retval) | ||
578 | return retval; | ||
579 | |||
580 | mutex_lock(&tty->termios_mutex); | ||
581 | memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios)); | ||
582 | mutex_unlock(&tty->termios_mutex); | ||
583 | |||
584 | if (opt & TERMIOS_TERMIO) { | ||
585 | if (user_termio_to_kernel_termios(&tmp_termios, | ||
586 | (struct termio __user *)arg)) | ||
587 | return -EFAULT; | ||
588 | #ifdef TCGETS2 | ||
589 | } else if (opt & TERMIOS_OLD) { | ||
590 | if (user_termios_to_kernel_termios_1(&tmp_termios, | ||
591 | (struct termios __user *)arg)) | ||
592 | return -EFAULT; | ||
593 | } else { | ||
594 | if (user_termios_to_kernel_termios(&tmp_termios, | ||
595 | (struct termios2 __user *)arg)) | ||
596 | return -EFAULT; | ||
597 | } | ||
598 | #else | ||
599 | } else if (user_termios_to_kernel_termios(&tmp_termios, | ||
600 | (struct termios __user *)arg)) | ||
601 | return -EFAULT; | ||
602 | #endif | ||
603 | |||
604 | /* If old style Bfoo values are used then load c_ispeed/c_ospeed | ||
605 | * with the real speed so its unconditionally usable */ | ||
606 | tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios); | ||
607 | tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios); | ||
608 | |||
609 | ld = tty_ldisc_ref(tty); | ||
610 | |||
611 | if (ld != NULL) { | ||
612 | if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer) | ||
613 | ld->ops->flush_buffer(tty); | ||
614 | tty_ldisc_deref(ld); | ||
615 | } | ||
616 | |||
617 | if (opt & TERMIOS_WAIT) { | ||
618 | tty_wait_until_sent(tty, 0); | ||
619 | if (signal_pending(current)) | ||
620 | return -EINTR; | ||
621 | } | ||
622 | |||
623 | change_termios(tty, &tmp_termios); | ||
624 | |||
625 | /* FIXME: Arguably if tmp_termios == tty->termios AND the | ||
626 | actual requested termios was not tmp_termios then we may | ||
627 | want to return an error as no user requested change has | ||
628 | succeeded */ | ||
629 | return 0; | ||
630 | } | ||
631 | |||
632 | static void copy_termios(struct tty_struct *tty, struct ktermios *kterm) | ||
633 | { | ||
634 | mutex_lock(&tty->termios_mutex); | ||
635 | memcpy(kterm, tty->termios, sizeof(struct ktermios)); | ||
636 | mutex_unlock(&tty->termios_mutex); | ||
637 | } | ||
638 | |||
639 | static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm) | ||
640 | { | ||
641 | mutex_lock(&tty->termios_mutex); | ||
642 | memcpy(kterm, tty->termios_locked, sizeof(struct ktermios)); | ||
643 | mutex_unlock(&tty->termios_mutex); | ||
644 | } | ||
645 | |||
646 | static int get_termio(struct tty_struct *tty, struct termio __user *termio) | ||
647 | { | ||
648 | struct ktermios kterm; | ||
649 | copy_termios(tty, &kterm); | ||
650 | if (kernel_termios_to_user_termio(termio, &kterm)) | ||
651 | return -EFAULT; | ||
652 | return 0; | ||
653 | } | ||
654 | |||
655 | |||
656 | #ifdef TCGETX | ||
657 | |||
658 | /** | ||
659 | * set_termiox - set termiox fields if possible | ||
660 | * @tty: terminal | ||
661 | * @arg: termiox structure from user | ||
662 | * @opt: option flags for ioctl type | ||
663 | * | ||
664 | * Implement the device calling points for the SYS5 termiox ioctl | ||
665 | * interface in Linux | ||
666 | */ | ||
667 | |||
668 | static int set_termiox(struct tty_struct *tty, void __user *arg, int opt) | ||
669 | { | ||
670 | struct termiox tnew; | ||
671 | struct tty_ldisc *ld; | ||
672 | |||
673 | if (tty->termiox == NULL) | ||
674 | return -EINVAL; | ||
675 | if (copy_from_user(&tnew, arg, sizeof(struct termiox))) | ||
676 | return -EFAULT; | ||
677 | |||
678 | ld = tty_ldisc_ref(tty); | ||
679 | if (ld != NULL) { | ||
680 | if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer) | ||
681 | ld->ops->flush_buffer(tty); | ||
682 | tty_ldisc_deref(ld); | ||
683 | } | ||
684 | if (opt & TERMIOS_WAIT) { | ||
685 | tty_wait_until_sent(tty, 0); | ||
686 | if (signal_pending(current)) | ||
687 | return -EINTR; | ||
688 | } | ||
689 | |||
690 | mutex_lock(&tty->termios_mutex); | ||
691 | if (tty->ops->set_termiox) | ||
692 | tty->ops->set_termiox(tty, &tnew); | ||
693 | mutex_unlock(&tty->termios_mutex); | ||
694 | return 0; | ||
695 | } | ||
696 | |||
697 | #endif | ||
698 | |||
699 | |||
700 | #ifdef TIOCGETP | ||
701 | /* | ||
702 | * These are deprecated, but there is limited support.. | ||
703 | * | ||
704 | * The "sg_flags" translation is a joke.. | ||
705 | */ | ||
706 | static int get_sgflags(struct tty_struct *tty) | ||
707 | { | ||
708 | int flags = 0; | ||
709 | |||
710 | if (!(tty->termios->c_lflag & ICANON)) { | ||
711 | if (tty->termios->c_lflag & ISIG) | ||
712 | flags |= 0x02; /* cbreak */ | ||
713 | else | ||
714 | flags |= 0x20; /* raw */ | ||
715 | } | ||
716 | if (tty->termios->c_lflag & ECHO) | ||
717 | flags |= 0x08; /* echo */ | ||
718 | if (tty->termios->c_oflag & OPOST) | ||
719 | if (tty->termios->c_oflag & ONLCR) | ||
720 | flags |= 0x10; /* crmod */ | ||
721 | return flags; | ||
722 | } | ||
723 | |||
724 | static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) | ||
725 | { | ||
726 | struct sgttyb tmp; | ||
727 | |||
728 | mutex_lock(&tty->termios_mutex); | ||
729 | tmp.sg_ispeed = tty->termios->c_ispeed; | ||
730 | tmp.sg_ospeed = tty->termios->c_ospeed; | ||
731 | tmp.sg_erase = tty->termios->c_cc[VERASE]; | ||
732 | tmp.sg_kill = tty->termios->c_cc[VKILL]; | ||
733 | tmp.sg_flags = get_sgflags(tty); | ||
734 | mutex_unlock(&tty->termios_mutex); | ||
735 | |||
736 | return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0; | ||
737 | } | ||
738 | |||
739 | static void set_sgflags(struct ktermios *termios, int flags) | ||
740 | { | ||
741 | termios->c_iflag = ICRNL | IXON; | ||
742 | termios->c_oflag = 0; | ||
743 | termios->c_lflag = ISIG | ICANON; | ||
744 | if (flags & 0x02) { /* cbreak */ | ||
745 | termios->c_iflag = 0; | ||
746 | termios->c_lflag &= ~ICANON; | ||
747 | } | ||
748 | if (flags & 0x08) { /* echo */ | ||
749 | termios->c_lflag |= ECHO | ECHOE | ECHOK | | ||
750 | ECHOCTL | ECHOKE | IEXTEN; | ||
751 | } | ||
752 | if (flags & 0x10) { /* crmod */ | ||
753 | termios->c_oflag |= OPOST | ONLCR; | ||
754 | } | ||
755 | if (flags & 0x20) { /* raw */ | ||
756 | termios->c_iflag = 0; | ||
757 | termios->c_lflag &= ~(ISIG | ICANON); | ||
758 | } | ||
759 | if (!(termios->c_lflag & ICANON)) { | ||
760 | termios->c_cc[VMIN] = 1; | ||
761 | termios->c_cc[VTIME] = 0; | ||
762 | } | ||
763 | } | ||
764 | |||
765 | /** | ||
766 | * set_sgttyb - set legacy terminal values | ||
767 | * @tty: tty structure | ||
768 | * @sgttyb: pointer to old style terminal structure | ||
769 | * | ||
770 | * Updates a terminal from the legacy BSD style terminal information | ||
771 | * structure. | ||
772 | * | ||
773 | * Locking: termios_mutex | ||
774 | */ | ||
775 | |||
776 | static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) | ||
777 | { | ||
778 | int retval; | ||
779 | struct sgttyb tmp; | ||
780 | struct ktermios termios; | ||
781 | |||
782 | retval = tty_check_change(tty); | ||
783 | if (retval) | ||
784 | return retval; | ||
785 | |||
786 | if (copy_from_user(&tmp, sgttyb, sizeof(tmp))) | ||
787 | return -EFAULT; | ||
788 | |||
789 | mutex_lock(&tty->termios_mutex); | ||
790 | termios = *tty->termios; | ||
791 | termios.c_cc[VERASE] = tmp.sg_erase; | ||
792 | termios.c_cc[VKILL] = tmp.sg_kill; | ||
793 | set_sgflags(&termios, tmp.sg_flags); | ||
794 | /* Try and encode into Bfoo format */ | ||
795 | #ifdef BOTHER | ||
796 | tty_termios_encode_baud_rate(&termios, termios.c_ispeed, | ||
797 | termios.c_ospeed); | ||
798 | #endif | ||
799 | mutex_unlock(&tty->termios_mutex); | ||
800 | change_termios(tty, &termios); | ||
801 | return 0; | ||
802 | } | ||
803 | #endif | ||
804 | |||
805 | #ifdef TIOCGETC | ||
806 | static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars) | ||
807 | { | ||
808 | struct tchars tmp; | ||
809 | |||
810 | mutex_lock(&tty->termios_mutex); | ||
811 | tmp.t_intrc = tty->termios->c_cc[VINTR]; | ||
812 | tmp.t_quitc = tty->termios->c_cc[VQUIT]; | ||
813 | tmp.t_startc = tty->termios->c_cc[VSTART]; | ||
814 | tmp.t_stopc = tty->termios->c_cc[VSTOP]; | ||
815 | tmp.t_eofc = tty->termios->c_cc[VEOF]; | ||
816 | tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */ | ||
817 | mutex_unlock(&tty->termios_mutex); | ||
818 | return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; | ||
819 | } | ||
820 | |||
821 | static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars) | ||
822 | { | ||
823 | struct tchars tmp; | ||
824 | |||
825 | if (copy_from_user(&tmp, tchars, sizeof(tmp))) | ||
826 | return -EFAULT; | ||
827 | mutex_lock(&tty->termios_mutex); | ||
828 | tty->termios->c_cc[VINTR] = tmp.t_intrc; | ||
829 | tty->termios->c_cc[VQUIT] = tmp.t_quitc; | ||
830 | tty->termios->c_cc[VSTART] = tmp.t_startc; | ||
831 | tty->termios->c_cc[VSTOP] = tmp.t_stopc; | ||
832 | tty->termios->c_cc[VEOF] = tmp.t_eofc; | ||
833 | tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */ | ||
834 | mutex_unlock(&tty->termios_mutex); | ||
835 | return 0; | ||
836 | } | ||
837 | #endif | ||
838 | |||
839 | #ifdef TIOCGLTC | ||
840 | static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars) | ||
841 | { | ||
842 | struct ltchars tmp; | ||
843 | |||
844 | mutex_lock(&tty->termios_mutex); | ||
845 | tmp.t_suspc = tty->termios->c_cc[VSUSP]; | ||
846 | /* what is dsuspc anyway? */ | ||
847 | tmp.t_dsuspc = tty->termios->c_cc[VSUSP]; | ||
848 | tmp.t_rprntc = tty->termios->c_cc[VREPRINT]; | ||
849 | /* what is flushc anyway? */ | ||
850 | tmp.t_flushc = tty->termios->c_cc[VEOL2]; | ||
851 | tmp.t_werasc = tty->termios->c_cc[VWERASE]; | ||
852 | tmp.t_lnextc = tty->termios->c_cc[VLNEXT]; | ||
853 | mutex_unlock(&tty->termios_mutex); | ||
854 | return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; | ||
855 | } | ||
856 | |||
857 | static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars) | ||
858 | { | ||
859 | struct ltchars tmp; | ||
860 | |||
861 | if (copy_from_user(&tmp, ltchars, sizeof(tmp))) | ||
862 | return -EFAULT; | ||
863 | |||
864 | mutex_lock(&tty->termios_mutex); | ||
865 | tty->termios->c_cc[VSUSP] = tmp.t_suspc; | ||
866 | /* what is dsuspc anyway? */ | ||
867 | tty->termios->c_cc[VEOL2] = tmp.t_dsuspc; | ||
868 | tty->termios->c_cc[VREPRINT] = tmp.t_rprntc; | ||
869 | /* what is flushc anyway? */ | ||
870 | tty->termios->c_cc[VEOL2] = tmp.t_flushc; | ||
871 | tty->termios->c_cc[VWERASE] = tmp.t_werasc; | ||
872 | tty->termios->c_cc[VLNEXT] = tmp.t_lnextc; | ||
873 | mutex_unlock(&tty->termios_mutex); | ||
874 | return 0; | ||
875 | } | ||
876 | #endif | ||
877 | |||
878 | /** | ||
879 | * send_prio_char - send priority character | ||
880 | * | ||
881 | * Send a high priority character to the tty even if stopped | ||
882 | * | ||
883 | * Locking: none for xchar method, write ordering for write method. | ||
884 | */ | ||
885 | |||
886 | static int send_prio_char(struct tty_struct *tty, char ch) | ||
887 | { | ||
888 | int was_stopped = tty->stopped; | ||
889 | |||
890 | if (tty->ops->send_xchar) { | ||
891 | tty->ops->send_xchar(tty, ch); | ||
892 | return 0; | ||
893 | } | ||
894 | |||
895 | if (tty_write_lock(tty, 0) < 0) | ||
896 | return -ERESTARTSYS; | ||
897 | |||
898 | if (was_stopped) | ||
899 | start_tty(tty); | ||
900 | tty->ops->write(tty, &ch, 1); | ||
901 | if (was_stopped) | ||
902 | stop_tty(tty); | ||
903 | tty_write_unlock(tty); | ||
904 | return 0; | ||
905 | } | ||
906 | |||
907 | /** | ||
908 | * tty_change_softcar - carrier change ioctl helper | ||
909 | * @tty: tty to update | ||
910 | * @arg: enable/disable CLOCAL | ||
911 | * | ||
912 | * Perform a change to the CLOCAL state and call into the driver | ||
913 | * layer to make it visible. All done with the termios mutex | ||
914 | */ | ||
915 | |||
916 | static int tty_change_softcar(struct tty_struct *tty, int arg) | ||
917 | { | ||
918 | int ret = 0; | ||
919 | int bit = arg ? CLOCAL : 0; | ||
920 | struct ktermios old; | ||
921 | |||
922 | mutex_lock(&tty->termios_mutex); | ||
923 | old = *tty->termios; | ||
924 | tty->termios->c_cflag &= ~CLOCAL; | ||
925 | tty->termios->c_cflag |= bit; | ||
926 | if (tty->ops->set_termios) | ||
927 | tty->ops->set_termios(tty, &old); | ||
928 | if ((tty->termios->c_cflag & CLOCAL) != bit) | ||
929 | ret = -EINVAL; | ||
930 | mutex_unlock(&tty->termios_mutex); | ||
931 | return ret; | ||
932 | } | ||
933 | |||
934 | /** | ||
935 | * tty_mode_ioctl - mode related ioctls | ||
936 | * @tty: tty for the ioctl | ||
937 | * @file: file pointer for the tty | ||
938 | * @cmd: command | ||
939 | * @arg: ioctl argument | ||
940 | * | ||
941 | * Perform non line discipline specific mode control ioctls. This | ||
942 | * is designed to be called by line disciplines to ensure they provide | ||
943 | * consistent mode setting. | ||
944 | */ | ||
945 | |||
946 | int tty_mode_ioctl(struct tty_struct *tty, struct file *file, | ||
947 | unsigned int cmd, unsigned long arg) | ||
948 | { | ||
949 | struct tty_struct *real_tty; | ||
950 | void __user *p = (void __user *)arg; | ||
951 | int ret = 0; | ||
952 | struct ktermios kterm; | ||
953 | |||
954 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | ||
955 | tty->driver->subtype == PTY_TYPE_MASTER) | ||
956 | real_tty = tty->link; | ||
957 | else | ||
958 | real_tty = tty; | ||
959 | |||
960 | switch (cmd) { | ||
961 | #ifdef TIOCGETP | ||
962 | case TIOCGETP: | ||
963 | return get_sgttyb(real_tty, (struct sgttyb __user *) arg); | ||
964 | case TIOCSETP: | ||
965 | case TIOCSETN: | ||
966 | return set_sgttyb(real_tty, (struct sgttyb __user *) arg); | ||
967 | #endif | ||
968 | #ifdef TIOCGETC | ||
969 | case TIOCGETC: | ||
970 | return get_tchars(real_tty, p); | ||
971 | case TIOCSETC: | ||
972 | return set_tchars(real_tty, p); | ||
973 | #endif | ||
974 | #ifdef TIOCGLTC | ||
975 | case TIOCGLTC: | ||
976 | return get_ltchars(real_tty, p); | ||
977 | case TIOCSLTC: | ||
978 | return set_ltchars(real_tty, p); | ||
979 | #endif | ||
980 | case TCSETSF: | ||
981 | return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD); | ||
982 | case TCSETSW: | ||
983 | return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD); | ||
984 | case TCSETS: | ||
985 | return set_termios(real_tty, p, TERMIOS_OLD); | ||
986 | #ifndef TCGETS2 | ||
987 | case TCGETS: | ||
988 | copy_termios(real_tty, &kterm); | ||
989 | if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm)) | ||
990 | ret = -EFAULT; | ||
991 | return ret; | ||
992 | #else | ||
993 | case TCGETS: | ||
994 | copy_termios(real_tty, &kterm); | ||
995 | if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm)) | ||
996 | ret = -EFAULT; | ||
997 | return ret; | ||
998 | case TCGETS2: | ||
999 | copy_termios(real_tty, &kterm); | ||
1000 | if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm)) | ||
1001 | ret = -EFAULT; | ||
1002 | return ret; | ||
1003 | case TCSETSF2: | ||
1004 | return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT); | ||
1005 | case TCSETSW2: | ||
1006 | return set_termios(real_tty, p, TERMIOS_WAIT); | ||
1007 | case TCSETS2: | ||
1008 | return set_termios(real_tty, p, 0); | ||
1009 | #endif | ||
1010 | case TCGETA: | ||
1011 | return get_termio(real_tty, p); | ||
1012 | case TCSETAF: | ||
1013 | return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO); | ||
1014 | case TCSETAW: | ||
1015 | return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO); | ||
1016 | case TCSETA: | ||
1017 | return set_termios(real_tty, p, TERMIOS_TERMIO); | ||
1018 | #ifndef TCGETS2 | ||
1019 | case TIOCGLCKTRMIOS: | ||
1020 | copy_termios_locked(real_tty, &kterm); | ||
1021 | if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm)) | ||
1022 | ret = -EFAULT; | ||
1023 | return ret; | ||
1024 | case TIOCSLCKTRMIOS: | ||
1025 | if (!capable(CAP_SYS_ADMIN)) | ||
1026 | return -EPERM; | ||
1027 | copy_termios_locked(real_tty, &kterm); | ||
1028 | if (user_termios_to_kernel_termios(&kterm, | ||
1029 | (struct termios __user *) arg)) | ||
1030 | return -EFAULT; | ||
1031 | mutex_lock(&real_tty->termios_mutex); | ||
1032 | memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios)); | ||
1033 | mutex_unlock(&real_tty->termios_mutex); | ||
1034 | return 0; | ||
1035 | #else | ||
1036 | case TIOCGLCKTRMIOS: | ||
1037 | copy_termios_locked(real_tty, &kterm); | ||
1038 | if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm)) | ||
1039 | ret = -EFAULT; | ||
1040 | return ret; | ||
1041 | case TIOCSLCKTRMIOS: | ||
1042 | if (!capable(CAP_SYS_ADMIN)) | ||
1043 | return -EPERM; | ||
1044 | copy_termios_locked(real_tty, &kterm); | ||
1045 | if (user_termios_to_kernel_termios_1(&kterm, | ||
1046 | (struct termios __user *) arg)) | ||
1047 | return -EFAULT; | ||
1048 | mutex_lock(&real_tty->termios_mutex); | ||
1049 | memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios)); | ||
1050 | mutex_unlock(&real_tty->termios_mutex); | ||
1051 | return ret; | ||
1052 | #endif | ||
1053 | #ifdef TCGETX | ||
1054 | case TCGETX: { | ||
1055 | struct termiox ktermx; | ||
1056 | if (real_tty->termiox == NULL) | ||
1057 | return -EINVAL; | ||
1058 | mutex_lock(&real_tty->termios_mutex); | ||
1059 | memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox)); | ||
1060 | mutex_unlock(&real_tty->termios_mutex); | ||
1061 | if (copy_to_user(p, &ktermx, sizeof(struct termiox))) | ||
1062 | ret = -EFAULT; | ||
1063 | return ret; | ||
1064 | } | ||
1065 | case TCSETX: | ||
1066 | return set_termiox(real_tty, p, 0); | ||
1067 | case TCSETXW: | ||
1068 | return set_termiox(real_tty, p, TERMIOS_WAIT); | ||
1069 | case TCSETXF: | ||
1070 | return set_termiox(real_tty, p, TERMIOS_FLUSH); | ||
1071 | #endif | ||
1072 | case TIOCGSOFTCAR: | ||
1073 | copy_termios(real_tty, &kterm); | ||
1074 | ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0, | ||
1075 | (int __user *)arg); | ||
1076 | return ret; | ||
1077 | case TIOCSSOFTCAR: | ||
1078 | if (get_user(arg, (unsigned int __user *) arg)) | ||
1079 | return -EFAULT; | ||
1080 | return tty_change_softcar(real_tty, arg); | ||
1081 | default: | ||
1082 | return -ENOIOCTLCMD; | ||
1083 | } | ||
1084 | } | ||
1085 | EXPORT_SYMBOL_GPL(tty_mode_ioctl); | ||
1086 | |||
1087 | int tty_perform_flush(struct tty_struct *tty, unsigned long arg) | ||
1088 | { | ||
1089 | struct tty_ldisc *ld; | ||
1090 | int retval = tty_check_change(tty); | ||
1091 | if (retval) | ||
1092 | return retval; | ||
1093 | |||
1094 | ld = tty_ldisc_ref_wait(tty); | ||
1095 | switch (arg) { | ||
1096 | case TCIFLUSH: | ||
1097 | if (ld && ld->ops->flush_buffer) | ||
1098 | ld->ops->flush_buffer(tty); | ||
1099 | break; | ||
1100 | case TCIOFLUSH: | ||
1101 | if (ld && ld->ops->flush_buffer) | ||
1102 | ld->ops->flush_buffer(tty); | ||
1103 | /* fall through */ | ||
1104 | case TCOFLUSH: | ||
1105 | tty_driver_flush_buffer(tty); | ||
1106 | break; | ||
1107 | default: | ||
1108 | tty_ldisc_deref(ld); | ||
1109 | return -EINVAL; | ||
1110 | } | ||
1111 | tty_ldisc_deref(ld); | ||
1112 | return 0; | ||
1113 | } | ||
1114 | EXPORT_SYMBOL_GPL(tty_perform_flush); | ||
1115 | |||
1116 | int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file, | ||
1117 | unsigned int cmd, unsigned long arg) | ||
1118 | { | ||
1119 | unsigned long flags; | ||
1120 | int retval; | ||
1121 | |||
1122 | switch (cmd) { | ||
1123 | case TCXONC: | ||
1124 | retval = tty_check_change(tty); | ||
1125 | if (retval) | ||
1126 | return retval; | ||
1127 | switch (arg) { | ||
1128 | case TCOOFF: | ||
1129 | if (!tty->flow_stopped) { | ||
1130 | tty->flow_stopped = 1; | ||
1131 | stop_tty(tty); | ||
1132 | } | ||
1133 | break; | ||
1134 | case TCOON: | ||
1135 | if (tty->flow_stopped) { | ||
1136 | tty->flow_stopped = 0; | ||
1137 | start_tty(tty); | ||
1138 | } | ||
1139 | break; | ||
1140 | case TCIOFF: | ||
1141 | if (STOP_CHAR(tty) != __DISABLED_CHAR) | ||
1142 | return send_prio_char(tty, STOP_CHAR(tty)); | ||
1143 | break; | ||
1144 | case TCION: | ||
1145 | if (START_CHAR(tty) != __DISABLED_CHAR) | ||
1146 | return send_prio_char(tty, START_CHAR(tty)); | ||
1147 | break; | ||
1148 | default: | ||
1149 | return -EINVAL; | ||
1150 | } | ||
1151 | return 0; | ||
1152 | case TCFLSH: | ||
1153 | return tty_perform_flush(tty, arg); | ||
1154 | case TIOCPKT: | ||
1155 | { | ||
1156 | int pktmode; | ||
1157 | |||
1158 | if (tty->driver->type != TTY_DRIVER_TYPE_PTY || | ||
1159 | tty->driver->subtype != PTY_TYPE_MASTER) | ||
1160 | return -ENOTTY; | ||
1161 | if (get_user(pktmode, (int __user *) arg)) | ||
1162 | return -EFAULT; | ||
1163 | spin_lock_irqsave(&tty->ctrl_lock, flags); | ||
1164 | if (pktmode) { | ||
1165 | if (!tty->packet) { | ||
1166 | tty->packet = 1; | ||
1167 | tty->link->ctrl_status = 0; | ||
1168 | } | ||
1169 | } else | ||
1170 | tty->packet = 0; | ||
1171 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | ||
1172 | return 0; | ||
1173 | } | ||
1174 | default: | ||
1175 | /* Try the mode commands */ | ||
1176 | return tty_mode_ioctl(tty, file, cmd, arg); | ||
1177 | } | ||
1178 | } | ||
1179 | EXPORT_SYMBOL(n_tty_ioctl_helper); | ||
diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c new file mode 100644 index 000000000000..d8e96b005023 --- /dev/null +++ b/drivers/tty/tty_ldisc.c | |||
@@ -0,0 +1,950 @@ | |||
1 | #include <linux/types.h> | ||
2 | #include <linux/major.h> | ||
3 | #include <linux/errno.h> | ||
4 | #include <linux/signal.h> | ||
5 | #include <linux/fcntl.h> | ||
6 | #include <linux/sched.h> | ||
7 | #include <linux/interrupt.h> | ||
8 | #include <linux/tty.h> | ||
9 | #include <linux/tty_driver.h> | ||
10 | #include <linux/tty_flip.h> | ||
11 | #include <linux/devpts_fs.h> | ||
12 | #include <linux/file.h> | ||
13 | #include <linux/console.h> | ||
14 | #include <linux/timer.h> | ||
15 | #include <linux/ctype.h> | ||
16 | #include <linux/kd.h> | ||
17 | #include <linux/mm.h> | ||
18 | #include <linux/string.h> | ||
19 | #include <linux/slab.h> | ||
20 | #include <linux/poll.h> | ||
21 | #include <linux/proc_fs.h> | ||
22 | #include <linux/init.h> | ||
23 | #include <linux/module.h> | ||
24 | #include <linux/device.h> | ||
25 | #include <linux/wait.h> | ||
26 | #include <linux/bitops.h> | ||
27 | #include <linux/delay.h> | ||
28 | #include <linux/seq_file.h> | ||
29 | |||
30 | #include <linux/uaccess.h> | ||
31 | #include <asm/system.h> | ||
32 | |||
33 | #include <linux/kbd_kern.h> | ||
34 | #include <linux/vt_kern.h> | ||
35 | #include <linux/selection.h> | ||
36 | |||
37 | #include <linux/smp_lock.h> /* For the moment */ | ||
38 | |||
39 | #include <linux/kmod.h> | ||
40 | #include <linux/nsproxy.h> | ||
41 | |||
42 | /* | ||
43 | * This guards the refcounted line discipline lists. The lock | ||
44 | * must be taken with irqs off because there are hangup path | ||
45 | * callers who will do ldisc lookups and cannot sleep. | ||
46 | */ | ||
47 | |||
48 | static DEFINE_SPINLOCK(tty_ldisc_lock); | ||
49 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); | ||
50 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle); | ||
51 | /* Line disc dispatch table */ | ||
52 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; | ||
53 | |||
54 | static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld) | ||
55 | { | ||
56 | if (ld) | ||
57 | atomic_inc(&ld->users); | ||
58 | return ld; | ||
59 | } | ||
60 | |||
61 | static void put_ldisc(struct tty_ldisc *ld) | ||
62 | { | ||
63 | unsigned long flags; | ||
64 | |||
65 | if (WARN_ON_ONCE(!ld)) | ||
66 | return; | ||
67 | |||
68 | /* | ||
69 | * If this is the last user, free the ldisc, and | ||
70 | * release the ldisc ops. | ||
71 | * | ||
72 | * We really want an "atomic_dec_and_lock_irqsave()", | ||
73 | * but we don't have it, so this does it by hand. | ||
74 | */ | ||
75 | local_irq_save(flags); | ||
76 | if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) { | ||
77 | struct tty_ldisc_ops *ldo = ld->ops; | ||
78 | |||
79 | ldo->refcount--; | ||
80 | module_put(ldo->owner); | ||
81 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
82 | |||
83 | kfree(ld); | ||
84 | return; | ||
85 | } | ||
86 | local_irq_restore(flags); | ||
87 | wake_up(&tty_ldisc_idle); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * tty_register_ldisc - install a line discipline | ||
92 | * @disc: ldisc number | ||
93 | * @new_ldisc: pointer to the ldisc object | ||
94 | * | ||
95 | * Installs a new line discipline into the kernel. The discipline | ||
96 | * is set up as unreferenced and then made available to the kernel | ||
97 | * from this point onwards. | ||
98 | * | ||
99 | * Locking: | ||
100 | * takes tty_ldisc_lock to guard against ldisc races | ||
101 | */ | ||
102 | |||
103 | int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) | ||
104 | { | ||
105 | unsigned long flags; | ||
106 | int ret = 0; | ||
107 | |||
108 | if (disc < N_TTY || disc >= NR_LDISCS) | ||
109 | return -EINVAL; | ||
110 | |||
111 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
112 | tty_ldiscs[disc] = new_ldisc; | ||
113 | new_ldisc->num = disc; | ||
114 | new_ldisc->refcount = 0; | ||
115 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
116 | |||
117 | return ret; | ||
118 | } | ||
119 | EXPORT_SYMBOL(tty_register_ldisc); | ||
120 | |||
121 | /** | ||
122 | * tty_unregister_ldisc - unload a line discipline | ||
123 | * @disc: ldisc number | ||
124 | * @new_ldisc: pointer to the ldisc object | ||
125 | * | ||
126 | * Remove a line discipline from the kernel providing it is not | ||
127 | * currently in use. | ||
128 | * | ||
129 | * Locking: | ||
130 | * takes tty_ldisc_lock to guard against ldisc races | ||
131 | */ | ||
132 | |||
133 | int tty_unregister_ldisc(int disc) | ||
134 | { | ||
135 | unsigned long flags; | ||
136 | int ret = 0; | ||
137 | |||
138 | if (disc < N_TTY || disc >= NR_LDISCS) | ||
139 | return -EINVAL; | ||
140 | |||
141 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
142 | if (tty_ldiscs[disc]->refcount) | ||
143 | ret = -EBUSY; | ||
144 | else | ||
145 | tty_ldiscs[disc] = NULL; | ||
146 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
147 | |||
148 | return ret; | ||
149 | } | ||
150 | EXPORT_SYMBOL(tty_unregister_ldisc); | ||
151 | |||
152 | static struct tty_ldisc_ops *get_ldops(int disc) | ||
153 | { | ||
154 | unsigned long flags; | ||
155 | struct tty_ldisc_ops *ldops, *ret; | ||
156 | |||
157 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
158 | ret = ERR_PTR(-EINVAL); | ||
159 | ldops = tty_ldiscs[disc]; | ||
160 | if (ldops) { | ||
161 | ret = ERR_PTR(-EAGAIN); | ||
162 | if (try_module_get(ldops->owner)) { | ||
163 | ldops->refcount++; | ||
164 | ret = ldops; | ||
165 | } | ||
166 | } | ||
167 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
168 | return ret; | ||
169 | } | ||
170 | |||
171 | static void put_ldops(struct tty_ldisc_ops *ldops) | ||
172 | { | ||
173 | unsigned long flags; | ||
174 | |||
175 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
176 | ldops->refcount--; | ||
177 | module_put(ldops->owner); | ||
178 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
179 | } | ||
180 | |||
181 | /** | ||
182 | * tty_ldisc_get - take a reference to an ldisc | ||
183 | * @disc: ldisc number | ||
184 | * | ||
185 | * Takes a reference to a line discipline. Deals with refcounts and | ||
186 | * module locking counts. Returns NULL if the discipline is not available. | ||
187 | * Returns a pointer to the discipline and bumps the ref count if it is | ||
188 | * available | ||
189 | * | ||
190 | * Locking: | ||
191 | * takes tty_ldisc_lock to guard against ldisc races | ||
192 | */ | ||
193 | |||
194 | static struct tty_ldisc *tty_ldisc_get(int disc) | ||
195 | { | ||
196 | struct tty_ldisc *ld; | ||
197 | struct tty_ldisc_ops *ldops; | ||
198 | |||
199 | if (disc < N_TTY || disc >= NR_LDISCS) | ||
200 | return ERR_PTR(-EINVAL); | ||
201 | |||
202 | /* | ||
203 | * Get the ldisc ops - we may need to request them to be loaded | ||
204 | * dynamically and try again. | ||
205 | */ | ||
206 | ldops = get_ldops(disc); | ||
207 | if (IS_ERR(ldops)) { | ||
208 | request_module("tty-ldisc-%d", disc); | ||
209 | ldops = get_ldops(disc); | ||
210 | if (IS_ERR(ldops)) | ||
211 | return ERR_CAST(ldops); | ||
212 | } | ||
213 | |||
214 | ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL); | ||
215 | if (ld == NULL) { | ||
216 | put_ldops(ldops); | ||
217 | return ERR_PTR(-ENOMEM); | ||
218 | } | ||
219 | |||
220 | ld->ops = ldops; | ||
221 | atomic_set(&ld->users, 1); | ||
222 | return ld; | ||
223 | } | ||
224 | |||
225 | static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) | ||
226 | { | ||
227 | return (*pos < NR_LDISCS) ? pos : NULL; | ||
228 | } | ||
229 | |||
230 | static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) | ||
231 | { | ||
232 | (*pos)++; | ||
233 | return (*pos < NR_LDISCS) ? pos : NULL; | ||
234 | } | ||
235 | |||
236 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) | ||
237 | { | ||
238 | } | ||
239 | |||
240 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) | ||
241 | { | ||
242 | int i = *(loff_t *)v; | ||
243 | struct tty_ldisc_ops *ldops; | ||
244 | |||
245 | ldops = get_ldops(i); | ||
246 | if (IS_ERR(ldops)) | ||
247 | return 0; | ||
248 | seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i); | ||
249 | put_ldops(ldops); | ||
250 | return 0; | ||
251 | } | ||
252 | |||
253 | static const struct seq_operations tty_ldiscs_seq_ops = { | ||
254 | .start = tty_ldiscs_seq_start, | ||
255 | .next = tty_ldiscs_seq_next, | ||
256 | .stop = tty_ldiscs_seq_stop, | ||
257 | .show = tty_ldiscs_seq_show, | ||
258 | }; | ||
259 | |||
260 | static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) | ||
261 | { | ||
262 | return seq_open(file, &tty_ldiscs_seq_ops); | ||
263 | } | ||
264 | |||
265 | const struct file_operations tty_ldiscs_proc_fops = { | ||
266 | .owner = THIS_MODULE, | ||
267 | .open = proc_tty_ldiscs_open, | ||
268 | .read = seq_read, | ||
269 | .llseek = seq_lseek, | ||
270 | .release = seq_release, | ||
271 | }; | ||
272 | |||
273 | /** | ||
274 | * tty_ldisc_assign - set ldisc on a tty | ||
275 | * @tty: tty to assign | ||
276 | * @ld: line discipline | ||
277 | * | ||
278 | * Install an instance of a line discipline into a tty structure. The | ||
279 | * ldisc must have a reference count above zero to ensure it remains. | ||
280 | * The tty instance refcount starts at zero. | ||
281 | * | ||
282 | * Locking: | ||
283 | * Caller must hold references | ||
284 | */ | ||
285 | |||
286 | static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) | ||
287 | { | ||
288 | tty->ldisc = ld; | ||
289 | } | ||
290 | |||
291 | /** | ||
292 | * tty_ldisc_try - internal helper | ||
293 | * @tty: the tty | ||
294 | * | ||
295 | * Make a single attempt to grab and bump the refcount on | ||
296 | * the tty ldisc. Return 0 on failure or 1 on success. This is | ||
297 | * used to implement both the waiting and non waiting versions | ||
298 | * of tty_ldisc_ref | ||
299 | * | ||
300 | * Locking: takes tty_ldisc_lock | ||
301 | */ | ||
302 | |||
303 | static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty) | ||
304 | { | ||
305 | unsigned long flags; | ||
306 | struct tty_ldisc *ld; | ||
307 | |||
308 | spin_lock_irqsave(&tty_ldisc_lock, flags); | ||
309 | ld = NULL; | ||
310 | if (test_bit(TTY_LDISC, &tty->flags)) | ||
311 | ld = get_ldisc(tty->ldisc); | ||
312 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | ||
313 | return ld; | ||
314 | } | ||
315 | |||
316 | /** | ||
317 | * tty_ldisc_ref_wait - wait for the tty ldisc | ||
318 | * @tty: tty device | ||
319 | * | ||
320 | * Dereference the line discipline for the terminal and take a | ||
321 | * reference to it. If the line discipline is in flux then | ||
322 | * wait patiently until it changes. | ||
323 | * | ||
324 | * Note: Must not be called from an IRQ/timer context. The caller | ||
325 | * must also be careful not to hold other locks that will deadlock | ||
326 | * against a discipline change, such as an existing ldisc reference | ||
327 | * (which we check for) | ||
328 | * | ||
329 | * Locking: call functions take tty_ldisc_lock | ||
330 | */ | ||
331 | |||
332 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) | ||
333 | { | ||
334 | struct tty_ldisc *ld; | ||
335 | |||
336 | /* wait_event is a macro */ | ||
337 | wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL); | ||
338 | return ld; | ||
339 | } | ||
340 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); | ||
341 | |||
342 | /** | ||
343 | * tty_ldisc_ref - get the tty ldisc | ||
344 | * @tty: tty device | ||
345 | * | ||
346 | * Dereference the line discipline for the terminal and take a | ||
347 | * reference to it. If the line discipline is in flux then | ||
348 | * return NULL. Can be called from IRQ and timer functions. | ||
349 | * | ||
350 | * Locking: called functions take tty_ldisc_lock | ||
351 | */ | ||
352 | |||
353 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) | ||
354 | { | ||
355 | return tty_ldisc_try(tty); | ||
356 | } | ||
357 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); | ||
358 | |||
359 | /** | ||
360 | * tty_ldisc_deref - free a tty ldisc reference | ||
361 | * @ld: reference to free up | ||
362 | * | ||
363 | * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May | ||
364 | * be called in IRQ context. | ||
365 | * | ||
366 | * Locking: takes tty_ldisc_lock | ||
367 | */ | ||
368 | |||
369 | void tty_ldisc_deref(struct tty_ldisc *ld) | ||
370 | { | ||
371 | put_ldisc(ld); | ||
372 | } | ||
373 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); | ||
374 | |||
375 | static inline void tty_ldisc_put(struct tty_ldisc *ld) | ||
376 | { | ||
377 | put_ldisc(ld); | ||
378 | } | ||
379 | |||
380 | /** | ||
381 | * tty_ldisc_enable - allow ldisc use | ||
382 | * @tty: terminal to activate ldisc on | ||
383 | * | ||
384 | * Set the TTY_LDISC flag when the line discipline can be called | ||
385 | * again. Do necessary wakeups for existing sleepers. Clear the LDISC | ||
386 | * changing flag to indicate any ldisc change is now over. | ||
387 | * | ||
388 | * Note: nobody should set the TTY_LDISC bit except via this function. | ||
389 | * Clearing directly is allowed. | ||
390 | */ | ||
391 | |||
392 | void tty_ldisc_enable(struct tty_struct *tty) | ||
393 | { | ||
394 | set_bit(TTY_LDISC, &tty->flags); | ||
395 | clear_bit(TTY_LDISC_CHANGING, &tty->flags); | ||
396 | wake_up(&tty_ldisc_wait); | ||
397 | } | ||
398 | |||
399 | /** | ||
400 | * tty_ldisc_flush - flush line discipline queue | ||
401 | * @tty: tty | ||
402 | * | ||
403 | * Flush the line discipline queue (if any) for this tty. If there | ||
404 | * is no line discipline active this is a no-op. | ||
405 | */ | ||
406 | |||
407 | void tty_ldisc_flush(struct tty_struct *tty) | ||
408 | { | ||
409 | struct tty_ldisc *ld = tty_ldisc_ref(tty); | ||
410 | if (ld) { | ||
411 | if (ld->ops->flush_buffer) | ||
412 | ld->ops->flush_buffer(tty); | ||
413 | tty_ldisc_deref(ld); | ||
414 | } | ||
415 | tty_buffer_flush(tty); | ||
416 | } | ||
417 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); | ||
418 | |||
419 | /** | ||
420 | * tty_set_termios_ldisc - set ldisc field | ||
421 | * @tty: tty structure | ||
422 | * @num: line discipline number | ||
423 | * | ||
424 | * This is probably overkill for real world processors but | ||
425 | * they are not on hot paths so a little discipline won't do | ||
426 | * any harm. | ||
427 | * | ||
428 | * Locking: takes termios_mutex | ||
429 | */ | ||
430 | |||
431 | static void tty_set_termios_ldisc(struct tty_struct *tty, int num) | ||
432 | { | ||
433 | mutex_lock(&tty->termios_mutex); | ||
434 | tty->termios->c_line = num; | ||
435 | mutex_unlock(&tty->termios_mutex); | ||
436 | } | ||
437 | |||
438 | /** | ||
439 | * tty_ldisc_open - open a line discipline | ||
440 | * @tty: tty we are opening the ldisc on | ||
441 | * @ld: discipline to open | ||
442 | * | ||
443 | * A helper opening method. Also a convenient debugging and check | ||
444 | * point. | ||
445 | * | ||
446 | * Locking: always called with BTM already held. | ||
447 | */ | ||
448 | |||
449 | static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) | ||
450 | { | ||
451 | WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); | ||
452 | if (ld->ops->open) { | ||
453 | int ret; | ||
454 | /* BTM here locks versus a hangup event */ | ||
455 | WARN_ON(!tty_locked()); | ||
456 | ret = ld->ops->open(tty); | ||
457 | return ret; | ||
458 | } | ||
459 | return 0; | ||
460 | } | ||
461 | |||
462 | /** | ||
463 | * tty_ldisc_close - close a line discipline | ||
464 | * @tty: tty we are opening the ldisc on | ||
465 | * @ld: discipline to close | ||
466 | * | ||
467 | * A helper close method. Also a convenient debugging and check | ||
468 | * point. | ||
469 | */ | ||
470 | |||
471 | static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) | ||
472 | { | ||
473 | WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); | ||
474 | clear_bit(TTY_LDISC_OPEN, &tty->flags); | ||
475 | if (ld->ops->close) | ||
476 | ld->ops->close(tty); | ||
477 | } | ||
478 | |||
479 | /** | ||
480 | * tty_ldisc_restore - helper for tty ldisc change | ||
481 | * @tty: tty to recover | ||
482 | * @old: previous ldisc | ||
483 | * | ||
484 | * Restore the previous line discipline or N_TTY when a line discipline | ||
485 | * change fails due to an open error | ||
486 | */ | ||
487 | |||
488 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) | ||
489 | { | ||
490 | char buf[64]; | ||
491 | struct tty_ldisc *new_ldisc; | ||
492 | int r; | ||
493 | |||
494 | /* There is an outstanding reference here so this is safe */ | ||
495 | old = tty_ldisc_get(old->ops->num); | ||
496 | WARN_ON(IS_ERR(old)); | ||
497 | tty_ldisc_assign(tty, old); | ||
498 | tty_set_termios_ldisc(tty, old->ops->num); | ||
499 | if (tty_ldisc_open(tty, old) < 0) { | ||
500 | tty_ldisc_put(old); | ||
501 | /* This driver is always present */ | ||
502 | new_ldisc = tty_ldisc_get(N_TTY); | ||
503 | if (IS_ERR(new_ldisc)) | ||
504 | panic("n_tty: get"); | ||
505 | tty_ldisc_assign(tty, new_ldisc); | ||
506 | tty_set_termios_ldisc(tty, N_TTY); | ||
507 | r = tty_ldisc_open(tty, new_ldisc); | ||
508 | if (r < 0) | ||
509 | panic("Couldn't open N_TTY ldisc for " | ||
510 | "%s --- error %d.", | ||
511 | tty_name(tty, buf), r); | ||
512 | } | ||
513 | } | ||
514 | |||
515 | /** | ||
516 | * tty_ldisc_halt - shut down the line discipline | ||
517 | * @tty: tty device | ||
518 | * | ||
519 | * Shut down the line discipline and work queue for this tty device. | ||
520 | * The TTY_LDISC flag being cleared ensures no further references can | ||
521 | * be obtained while the delayed work queue halt ensures that no more | ||
522 | * data is fed to the ldisc. | ||
523 | * | ||
524 | * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex) | ||
525 | * in order to make sure any currently executing ldisc work is also | ||
526 | * flushed. | ||
527 | */ | ||
528 | |||
529 | static int tty_ldisc_halt(struct tty_struct *tty) | ||
530 | { | ||
531 | clear_bit(TTY_LDISC, &tty->flags); | ||
532 | return cancel_delayed_work_sync(&tty->buf.work); | ||
533 | } | ||
534 | |||
535 | /** | ||
536 | * tty_ldisc_wait_idle - wait for the ldisc to become idle | ||
537 | * @tty: tty to wait for | ||
538 | * | ||
539 | * Wait for the line discipline to become idle. The discipline must | ||
540 | * have been halted for this to guarantee it remains idle. | ||
541 | */ | ||
542 | static int tty_ldisc_wait_idle(struct tty_struct *tty) | ||
543 | { | ||
544 | int ret; | ||
545 | ret = wait_event_interruptible_timeout(tty_ldisc_idle, | ||
546 | atomic_read(&tty->ldisc->users) == 1, 5 * HZ); | ||
547 | if (ret < 0) | ||
548 | return ret; | ||
549 | return ret > 0 ? 0 : -EBUSY; | ||
550 | } | ||
551 | |||
552 | /** | ||
553 | * tty_set_ldisc - set line discipline | ||
554 | * @tty: the terminal to set | ||
555 | * @ldisc: the line discipline | ||
556 | * | ||
557 | * Set the discipline of a tty line. Must be called from a process | ||
558 | * context. The ldisc change logic has to protect itself against any | ||
559 | * overlapping ldisc change (including on the other end of pty pairs), | ||
560 | * the close of one side of a tty/pty pair, and eventually hangup. | ||
561 | * | ||
562 | * Locking: takes tty_ldisc_lock, termios_mutex | ||
563 | */ | ||
564 | |||
565 | int tty_set_ldisc(struct tty_struct *tty, int ldisc) | ||
566 | { | ||
567 | int retval; | ||
568 | struct tty_ldisc *o_ldisc, *new_ldisc; | ||
569 | int work, o_work = 0; | ||
570 | struct tty_struct *o_tty; | ||
571 | |||
572 | new_ldisc = tty_ldisc_get(ldisc); | ||
573 | if (IS_ERR(new_ldisc)) | ||
574 | return PTR_ERR(new_ldisc); | ||
575 | |||
576 | tty_lock(); | ||
577 | /* | ||
578 | * We need to look at the tty locking here for pty/tty pairs | ||
579 | * when both sides try to change in parallel. | ||
580 | */ | ||
581 | |||
582 | o_tty = tty->link; /* o_tty is the pty side or NULL */ | ||
583 | |||
584 | |||
585 | /* | ||
586 | * Check the no-op case | ||
587 | */ | ||
588 | |||
589 | if (tty->ldisc->ops->num == ldisc) { | ||
590 | tty_unlock(); | ||
591 | tty_ldisc_put(new_ldisc); | ||
592 | return 0; | ||
593 | } | ||
594 | |||
595 | tty_unlock(); | ||
596 | /* | ||
597 | * Problem: What do we do if this blocks ? | ||
598 | * We could deadlock here | ||
599 | */ | ||
600 | |||
601 | tty_wait_until_sent(tty, 0); | ||
602 | |||
603 | tty_lock(); | ||
604 | mutex_lock(&tty->ldisc_mutex); | ||
605 | |||
606 | /* | ||
607 | * We could be midstream of another ldisc change which has | ||
608 | * dropped the lock during processing. If so we need to wait. | ||
609 | */ | ||
610 | |||
611 | while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { | ||
612 | mutex_unlock(&tty->ldisc_mutex); | ||
613 | tty_unlock(); | ||
614 | wait_event(tty_ldisc_wait, | ||
615 | test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0); | ||
616 | tty_lock(); | ||
617 | mutex_lock(&tty->ldisc_mutex); | ||
618 | } | ||
619 | |||
620 | set_bit(TTY_LDISC_CHANGING, &tty->flags); | ||
621 | |||
622 | /* | ||
623 | * No more input please, we are switching. The new ldisc | ||
624 | * will update this value in the ldisc open function | ||
625 | */ | ||
626 | |||
627 | tty->receive_room = 0; | ||
628 | |||
629 | o_ldisc = tty->ldisc; | ||
630 | |||
631 | tty_unlock(); | ||
632 | /* | ||
633 | * Make sure we don't change while someone holds a | ||
634 | * reference to the line discipline. The TTY_LDISC bit | ||
635 | * prevents anyone taking a reference once it is clear. | ||
636 | * We need the lock to avoid racing reference takers. | ||
637 | * | ||
638 | * We must clear the TTY_LDISC bit here to avoid a livelock | ||
639 | * with a userspace app continually trying to use the tty in | ||
640 | * parallel to the change and re-referencing the tty. | ||
641 | */ | ||
642 | |||
643 | work = tty_ldisc_halt(tty); | ||
644 | if (o_tty) | ||
645 | o_work = tty_ldisc_halt(o_tty); | ||
646 | |||
647 | /* | ||
648 | * Wait for ->hangup_work and ->buf.work handlers to terminate. | ||
649 | * We must drop the mutex here in case a hangup is also in process. | ||
650 | */ | ||
651 | |||
652 | mutex_unlock(&tty->ldisc_mutex); | ||
653 | |||
654 | flush_scheduled_work(); | ||
655 | |||
656 | retval = tty_ldisc_wait_idle(tty); | ||
657 | |||
658 | tty_lock(); | ||
659 | mutex_lock(&tty->ldisc_mutex); | ||
660 | |||
661 | /* handle wait idle failure locked */ | ||
662 | if (retval) { | ||
663 | tty_ldisc_put(new_ldisc); | ||
664 | goto enable; | ||
665 | } | ||
666 | |||
667 | if (test_bit(TTY_HUPPED, &tty->flags)) { | ||
668 | /* We were raced by the hangup method. It will have stomped | ||
669 | the ldisc data and closed the ldisc down */ | ||
670 | clear_bit(TTY_LDISC_CHANGING, &tty->flags); | ||
671 | mutex_unlock(&tty->ldisc_mutex); | ||
672 | tty_ldisc_put(new_ldisc); | ||
673 | tty_unlock(); | ||
674 | return -EIO; | ||
675 | } | ||
676 | |||
677 | /* Shutdown the current discipline. */ | ||
678 | tty_ldisc_close(tty, o_ldisc); | ||
679 | |||
680 | /* Now set up the new line discipline. */ | ||
681 | tty_ldisc_assign(tty, new_ldisc); | ||
682 | tty_set_termios_ldisc(tty, ldisc); | ||
683 | |||
684 | retval = tty_ldisc_open(tty, new_ldisc); | ||
685 | if (retval < 0) { | ||
686 | /* Back to the old one or N_TTY if we can't */ | ||
687 | tty_ldisc_put(new_ldisc); | ||
688 | tty_ldisc_restore(tty, o_ldisc); | ||
689 | } | ||
690 | |||
691 | /* At this point we hold a reference to the new ldisc and a | ||
692 | a reference to the old ldisc. If we ended up flipping back | ||
693 | to the existing ldisc we have two references to it */ | ||
694 | |||
695 | if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc) | ||
696 | tty->ops->set_ldisc(tty); | ||
697 | |||
698 | tty_ldisc_put(o_ldisc); | ||
699 | |||
700 | enable: | ||
701 | /* | ||
702 | * Allow ldisc referencing to occur again | ||
703 | */ | ||
704 | |||
705 | tty_ldisc_enable(tty); | ||
706 | if (o_tty) | ||
707 | tty_ldisc_enable(o_tty); | ||
708 | |||
709 | /* Restart the work queue in case no characters kick it off. Safe if | ||
710 | already running */ | ||
711 | if (work) | ||
712 | schedule_delayed_work(&tty->buf.work, 1); | ||
713 | if (o_work) | ||
714 | schedule_delayed_work(&o_tty->buf.work, 1); | ||
715 | mutex_unlock(&tty->ldisc_mutex); | ||
716 | tty_unlock(); | ||
717 | return retval; | ||
718 | } | ||
719 | |||
720 | /** | ||
721 | * tty_reset_termios - reset terminal state | ||
722 | * @tty: tty to reset | ||
723 | * | ||
724 | * Restore a terminal to the driver default state. | ||
725 | */ | ||
726 | |||
727 | static void tty_reset_termios(struct tty_struct *tty) | ||
728 | { | ||
729 | mutex_lock(&tty->termios_mutex); | ||
730 | *tty->termios = tty->driver->init_termios; | ||
731 | tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios); | ||
732 | tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios); | ||
733 | mutex_unlock(&tty->termios_mutex); | ||
734 | } | ||
735 | |||
736 | |||
737 | /** | ||
738 | * tty_ldisc_reinit - reinitialise the tty ldisc | ||
739 | * @tty: tty to reinit | ||
740 | * @ldisc: line discipline to reinitialize | ||
741 | * | ||
742 | * Switch the tty to a line discipline and leave the ldisc | ||
743 | * state closed | ||
744 | */ | ||
745 | |||
746 | static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc) | ||
747 | { | ||
748 | struct tty_ldisc *ld = tty_ldisc_get(ldisc); | ||
749 | |||
750 | if (IS_ERR(ld)) | ||
751 | return -1; | ||
752 | |||
753 | tty_ldisc_close(tty, tty->ldisc); | ||
754 | tty_ldisc_put(tty->ldisc); | ||
755 | tty->ldisc = NULL; | ||
756 | /* | ||
757 | * Switch the line discipline back | ||
758 | */ | ||
759 | tty_ldisc_assign(tty, ld); | ||
760 | tty_set_termios_ldisc(tty, ldisc); | ||
761 | |||
762 | return 0; | ||
763 | } | ||
764 | |||
765 | /** | ||
766 | * tty_ldisc_hangup - hangup ldisc reset | ||
767 | * @tty: tty being hung up | ||
768 | * | ||
769 | * Some tty devices reset their termios when they receive a hangup | ||
770 | * event. In that situation we must also switch back to N_TTY properly | ||
771 | * before we reset the termios data. | ||
772 | * | ||
773 | * Locking: We can take the ldisc mutex as the rest of the code is | ||
774 | * careful to allow for this. | ||
775 | * | ||
776 | * In the pty pair case this occurs in the close() path of the | ||
777 | * tty itself so we must be careful about locking rules. | ||
778 | */ | ||
779 | |||
780 | void tty_ldisc_hangup(struct tty_struct *tty) | ||
781 | { | ||
782 | struct tty_ldisc *ld; | ||
783 | int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS; | ||
784 | int err = 0; | ||
785 | |||
786 | /* | ||
787 | * FIXME! What are the locking issues here? This may me overdoing | ||
788 | * things... This question is especially important now that we've | ||
789 | * removed the irqlock. | ||
790 | */ | ||
791 | ld = tty_ldisc_ref(tty); | ||
792 | if (ld != NULL) { | ||
793 | /* We may have no line discipline at this point */ | ||
794 | if (ld->ops->flush_buffer) | ||
795 | ld->ops->flush_buffer(tty); | ||
796 | tty_driver_flush_buffer(tty); | ||
797 | if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && | ||
798 | ld->ops->write_wakeup) | ||
799 | ld->ops->write_wakeup(tty); | ||
800 | if (ld->ops->hangup) | ||
801 | ld->ops->hangup(tty); | ||
802 | tty_ldisc_deref(ld); | ||
803 | } | ||
804 | /* | ||
805 | * FIXME: Once we trust the LDISC code better we can wait here for | ||
806 | * ldisc completion and fix the driver call race | ||
807 | */ | ||
808 | wake_up_interruptible_poll(&tty->write_wait, POLLOUT); | ||
809 | wake_up_interruptible_poll(&tty->read_wait, POLLIN); | ||
810 | /* | ||
811 | * Shutdown the current line discipline, and reset it to | ||
812 | * N_TTY if need be. | ||
813 | * | ||
814 | * Avoid racing set_ldisc or tty_ldisc_release | ||
815 | */ | ||
816 | mutex_lock(&tty->ldisc_mutex); | ||
817 | |||
818 | /* | ||
819 | * this is like tty_ldisc_halt, but we need to give up | ||
820 | * the BTM before calling cancel_delayed_work_sync, | ||
821 | * which may need to wait for another function taking the BTM | ||
822 | */ | ||
823 | clear_bit(TTY_LDISC, &tty->flags); | ||
824 | tty_unlock(); | ||
825 | cancel_delayed_work_sync(&tty->buf.work); | ||
826 | mutex_unlock(&tty->ldisc_mutex); | ||
827 | |||
828 | tty_lock(); | ||
829 | mutex_lock(&tty->ldisc_mutex); | ||
830 | |||
831 | /* At this point we have a closed ldisc and we want to | ||
832 | reopen it. We could defer this to the next open but | ||
833 | it means auditing a lot of other paths so this is | ||
834 | a FIXME */ | ||
835 | if (tty->ldisc) { /* Not yet closed */ | ||
836 | if (reset == 0) { | ||
837 | |||
838 | if (!tty_ldisc_reinit(tty, tty->termios->c_line)) | ||
839 | err = tty_ldisc_open(tty, tty->ldisc); | ||
840 | else | ||
841 | err = 1; | ||
842 | } | ||
843 | /* If the re-open fails or we reset then go to N_TTY. The | ||
844 | N_TTY open cannot fail */ | ||
845 | if (reset || err) { | ||
846 | BUG_ON(tty_ldisc_reinit(tty, N_TTY)); | ||
847 | WARN_ON(tty_ldisc_open(tty, tty->ldisc)); | ||
848 | } | ||
849 | tty_ldisc_enable(tty); | ||
850 | } | ||
851 | mutex_unlock(&tty->ldisc_mutex); | ||
852 | if (reset) | ||
853 | tty_reset_termios(tty); | ||
854 | } | ||
855 | |||
856 | /** | ||
857 | * tty_ldisc_setup - open line discipline | ||
858 | * @tty: tty being shut down | ||
859 | * @o_tty: pair tty for pty/tty pairs | ||
860 | * | ||
861 | * Called during the initial open of a tty/pty pair in order to set up the | ||
862 | * line disciplines and bind them to the tty. This has no locking issues | ||
863 | * as the device isn't yet active. | ||
864 | */ | ||
865 | |||
866 | int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) | ||
867 | { | ||
868 | struct tty_ldisc *ld = tty->ldisc; | ||
869 | int retval; | ||
870 | |||
871 | retval = tty_ldisc_open(tty, ld); | ||
872 | if (retval) | ||
873 | return retval; | ||
874 | |||
875 | if (o_tty) { | ||
876 | retval = tty_ldisc_open(o_tty, o_tty->ldisc); | ||
877 | if (retval) { | ||
878 | tty_ldisc_close(tty, ld); | ||
879 | return retval; | ||
880 | } | ||
881 | tty_ldisc_enable(o_tty); | ||
882 | } | ||
883 | tty_ldisc_enable(tty); | ||
884 | return 0; | ||
885 | } | ||
886 | /** | ||
887 | * tty_ldisc_release - release line discipline | ||
888 | * @tty: tty being shut down | ||
889 | * @o_tty: pair tty for pty/tty pairs | ||
890 | * | ||
891 | * Called during the final close of a tty/pty pair in order to shut down | ||
892 | * the line discpline layer. On exit the ldisc assigned is N_TTY and the | ||
893 | * ldisc has not been opened. | ||
894 | */ | ||
895 | |||
896 | void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty) | ||
897 | { | ||
898 | /* | ||
899 | * Prevent flush_to_ldisc() from rescheduling the work for later. Then | ||
900 | * kill any delayed work. As this is the final close it does not | ||
901 | * race with the set_ldisc code path. | ||
902 | */ | ||
903 | |||
904 | tty_unlock(); | ||
905 | tty_ldisc_halt(tty); | ||
906 | flush_scheduled_work(); | ||
907 | tty_lock(); | ||
908 | |||
909 | mutex_lock(&tty->ldisc_mutex); | ||
910 | /* | ||
911 | * Now kill off the ldisc | ||
912 | */ | ||
913 | tty_ldisc_close(tty, tty->ldisc); | ||
914 | tty_ldisc_put(tty->ldisc); | ||
915 | /* Force an oops if we mess this up */ | ||
916 | tty->ldisc = NULL; | ||
917 | |||
918 | /* Ensure the next open requests the N_TTY ldisc */ | ||
919 | tty_set_termios_ldisc(tty, N_TTY); | ||
920 | mutex_unlock(&tty->ldisc_mutex); | ||
921 | |||
922 | /* This will need doing differently if we need to lock */ | ||
923 | if (o_tty) | ||
924 | tty_ldisc_release(o_tty, NULL); | ||
925 | |||
926 | /* And the memory resources remaining (buffers, termios) will be | ||
927 | disposed of when the kref hits zero */ | ||
928 | } | ||
929 | |||
930 | /** | ||
931 | * tty_ldisc_init - ldisc setup for new tty | ||
932 | * @tty: tty being allocated | ||
933 | * | ||
934 | * Set up the line discipline objects for a newly allocated tty. Note that | ||
935 | * the tty structure is not completely set up when this call is made. | ||
936 | */ | ||
937 | |||
938 | void tty_ldisc_init(struct tty_struct *tty) | ||
939 | { | ||
940 | struct tty_ldisc *ld = tty_ldisc_get(N_TTY); | ||
941 | if (IS_ERR(ld)) | ||
942 | panic("n_tty: init_tty"); | ||
943 | tty_ldisc_assign(tty, ld); | ||
944 | } | ||
945 | |||
946 | void tty_ldisc_begin(void) | ||
947 | { | ||
948 | /* Setup the default TTY line discipline. */ | ||
949 | (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); | ||
950 | } | ||
diff --git a/drivers/tty/tty_mutex.c b/drivers/tty/tty_mutex.c new file mode 100644 index 000000000000..133697540c73 --- /dev/null +++ b/drivers/tty/tty_mutex.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* | ||
2 | * drivers/char/tty_lock.c | ||
3 | */ | ||
4 | #include <linux/tty.h> | ||
5 | #include <linux/module.h> | ||
6 | #include <linux/kallsyms.h> | ||
7 | #include <linux/semaphore.h> | ||
8 | #include <linux/sched.h> | ||
9 | |||
10 | /* | ||
11 | * The 'big tty mutex' | ||
12 | * | ||
13 | * This mutex is taken and released by tty_lock() and tty_unlock(), | ||
14 | * replacing the older big kernel lock. | ||
15 | * It can no longer be taken recursively, and does not get | ||
16 | * released implicitly while sleeping. | ||
17 | * | ||
18 | * Don't use in new code. | ||
19 | */ | ||
20 | static DEFINE_MUTEX(big_tty_mutex); | ||
21 | struct task_struct *__big_tty_mutex_owner; | ||
22 | EXPORT_SYMBOL_GPL(__big_tty_mutex_owner); | ||
23 | |||
24 | /* | ||
25 | * Getting the big tty mutex. | ||
26 | */ | ||
27 | void __lockfunc tty_lock(void) | ||
28 | { | ||
29 | struct task_struct *task = current; | ||
30 | |||
31 | WARN_ON(__big_tty_mutex_owner == task); | ||
32 | |||
33 | mutex_lock(&big_tty_mutex); | ||
34 | __big_tty_mutex_owner = task; | ||
35 | } | ||
36 | EXPORT_SYMBOL(tty_lock); | ||
37 | |||
38 | void __lockfunc tty_unlock(void) | ||
39 | { | ||
40 | struct task_struct *task = current; | ||
41 | |||
42 | WARN_ON(__big_tty_mutex_owner != task); | ||
43 | __big_tty_mutex_owner = NULL; | ||
44 | |||
45 | mutex_unlock(&big_tty_mutex); | ||
46 | } | ||
47 | EXPORT_SYMBOL(tty_unlock); | ||
diff --git a/drivers/tty/tty_port.c b/drivers/tty/tty_port.c new file mode 100644 index 000000000000..33d37d230f8f --- /dev/null +++ b/drivers/tty/tty_port.c | |||
@@ -0,0 +1,446 @@ | |||
1 | /* | ||
2 | * Tty port functions | ||
3 | */ | ||
4 | |||
5 | #include <linux/types.h> | ||
6 | #include <linux/errno.h> | ||
7 | #include <linux/tty.h> | ||
8 | #include <linux/tty_driver.h> | ||
9 | #include <linux/tty_flip.h> | ||
10 | #include <linux/serial.h> | ||
11 | #include <linux/timer.h> | ||
12 | #include <linux/string.h> | ||
13 | #include <linux/slab.h> | ||
14 | #include <linux/sched.h> | ||
15 | #include <linux/init.h> | ||
16 | #include <linux/wait.h> | ||
17 | #include <linux/bitops.h> | ||
18 | #include <linux/delay.h> | ||
19 | #include <linux/module.h> | ||
20 | |||
21 | void tty_port_init(struct tty_port *port) | ||
22 | { | ||
23 | memset(port, 0, sizeof(*port)); | ||
24 | init_waitqueue_head(&port->open_wait); | ||
25 | init_waitqueue_head(&port->close_wait); | ||
26 | init_waitqueue_head(&port->delta_msr_wait); | ||
27 | mutex_init(&port->mutex); | ||
28 | mutex_init(&port->buf_mutex); | ||
29 | spin_lock_init(&port->lock); | ||
30 | port->close_delay = (50 * HZ) / 100; | ||
31 | port->closing_wait = (3000 * HZ) / 100; | ||
32 | kref_init(&port->kref); | ||
33 | } | ||
34 | EXPORT_SYMBOL(tty_port_init); | ||
35 | |||
36 | int tty_port_alloc_xmit_buf(struct tty_port *port) | ||
37 | { | ||
38 | /* We may sleep in get_zeroed_page() */ | ||
39 | mutex_lock(&port->buf_mutex); | ||
40 | if (port->xmit_buf == NULL) | ||
41 | port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); | ||
42 | mutex_unlock(&port->buf_mutex); | ||
43 | if (port->xmit_buf == NULL) | ||
44 | return -ENOMEM; | ||
45 | return 0; | ||
46 | } | ||
47 | EXPORT_SYMBOL(tty_port_alloc_xmit_buf); | ||
48 | |||
49 | void tty_port_free_xmit_buf(struct tty_port *port) | ||
50 | { | ||
51 | mutex_lock(&port->buf_mutex); | ||
52 | if (port->xmit_buf != NULL) { | ||
53 | free_page((unsigned long)port->xmit_buf); | ||
54 | port->xmit_buf = NULL; | ||
55 | } | ||
56 | mutex_unlock(&port->buf_mutex); | ||
57 | } | ||
58 | EXPORT_SYMBOL(tty_port_free_xmit_buf); | ||
59 | |||
60 | static void tty_port_destructor(struct kref *kref) | ||
61 | { | ||
62 | struct tty_port *port = container_of(kref, struct tty_port, kref); | ||
63 | if (port->xmit_buf) | ||
64 | free_page((unsigned long)port->xmit_buf); | ||
65 | if (port->ops->destruct) | ||
66 | port->ops->destruct(port); | ||
67 | else | ||
68 | kfree(port); | ||
69 | } | ||
70 | |||
71 | void tty_port_put(struct tty_port *port) | ||
72 | { | ||
73 | if (port) | ||
74 | kref_put(&port->kref, tty_port_destructor); | ||
75 | } | ||
76 | EXPORT_SYMBOL(tty_port_put); | ||
77 | |||
78 | /** | ||
79 | * tty_port_tty_get - get a tty reference | ||
80 | * @port: tty port | ||
81 | * | ||
82 | * Return a refcount protected tty instance or NULL if the port is not | ||
83 | * associated with a tty (eg due to close or hangup) | ||
84 | */ | ||
85 | |||
86 | struct tty_struct *tty_port_tty_get(struct tty_port *port) | ||
87 | { | ||
88 | unsigned long flags; | ||
89 | struct tty_struct *tty; | ||
90 | |||
91 | spin_lock_irqsave(&port->lock, flags); | ||
92 | tty = tty_kref_get(port->tty); | ||
93 | spin_unlock_irqrestore(&port->lock, flags); | ||
94 | return tty; | ||
95 | } | ||
96 | EXPORT_SYMBOL(tty_port_tty_get); | ||
97 | |||
98 | /** | ||
99 | * tty_port_tty_set - set the tty of a port | ||
100 | * @port: tty port | ||
101 | * @tty: the tty | ||
102 | * | ||
103 | * Associate the port and tty pair. Manages any internal refcounts. | ||
104 | * Pass NULL to deassociate a port | ||
105 | */ | ||
106 | |||
107 | void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) | ||
108 | { | ||
109 | unsigned long flags; | ||
110 | |||
111 | spin_lock_irqsave(&port->lock, flags); | ||
112 | if (port->tty) | ||
113 | tty_kref_put(port->tty); | ||
114 | port->tty = tty_kref_get(tty); | ||
115 | spin_unlock_irqrestore(&port->lock, flags); | ||
116 | } | ||
117 | EXPORT_SYMBOL(tty_port_tty_set); | ||
118 | |||
119 | static void tty_port_shutdown(struct tty_port *port) | ||
120 | { | ||
121 | mutex_lock(&port->mutex); | ||
122 | if (port->ops->shutdown && !port->console && | ||
123 | test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) | ||
124 | port->ops->shutdown(port); | ||
125 | mutex_unlock(&port->mutex); | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * tty_port_hangup - hangup helper | ||
130 | * @port: tty port | ||
131 | * | ||
132 | * Perform port level tty hangup flag and count changes. Drop the tty | ||
133 | * reference. | ||
134 | */ | ||
135 | |||
136 | void tty_port_hangup(struct tty_port *port) | ||
137 | { | ||
138 | unsigned long flags; | ||
139 | |||
140 | spin_lock_irqsave(&port->lock, flags); | ||
141 | port->count = 0; | ||
142 | port->flags &= ~ASYNC_NORMAL_ACTIVE; | ||
143 | if (port->tty) { | ||
144 | set_bit(TTY_IO_ERROR, &port->tty->flags); | ||
145 | tty_kref_put(port->tty); | ||
146 | } | ||
147 | port->tty = NULL; | ||
148 | spin_unlock_irqrestore(&port->lock, flags); | ||
149 | wake_up_interruptible(&port->open_wait); | ||
150 | wake_up_interruptible(&port->delta_msr_wait); | ||
151 | tty_port_shutdown(port); | ||
152 | } | ||
153 | EXPORT_SYMBOL(tty_port_hangup); | ||
154 | |||
155 | /** | ||
156 | * tty_port_carrier_raised - carrier raised check | ||
157 | * @port: tty port | ||
158 | * | ||
159 | * Wrapper for the carrier detect logic. For the moment this is used | ||
160 | * to hide some internal details. This will eventually become entirely | ||
161 | * internal to the tty port. | ||
162 | */ | ||
163 | |||
164 | int tty_port_carrier_raised(struct tty_port *port) | ||
165 | { | ||
166 | if (port->ops->carrier_raised == NULL) | ||
167 | return 1; | ||
168 | return port->ops->carrier_raised(port); | ||
169 | } | ||
170 | EXPORT_SYMBOL(tty_port_carrier_raised); | ||
171 | |||
172 | /** | ||
173 | * tty_port_raise_dtr_rts - Raise DTR/RTS | ||
174 | * @port: tty port | ||
175 | * | ||
176 | * Wrapper for the DTR/RTS raise logic. For the moment this is used | ||
177 | * to hide some internal details. This will eventually become entirely | ||
178 | * internal to the tty port. | ||
179 | */ | ||
180 | |||
181 | void tty_port_raise_dtr_rts(struct tty_port *port) | ||
182 | { | ||
183 | if (port->ops->dtr_rts) | ||
184 | port->ops->dtr_rts(port, 1); | ||
185 | } | ||
186 | EXPORT_SYMBOL(tty_port_raise_dtr_rts); | ||
187 | |||
188 | /** | ||
189 | * tty_port_lower_dtr_rts - Lower DTR/RTS | ||
190 | * @port: tty port | ||
191 | * | ||
192 | * Wrapper for the DTR/RTS raise logic. For the moment this is used | ||
193 | * to hide some internal details. This will eventually become entirely | ||
194 | * internal to the tty port. | ||
195 | */ | ||
196 | |||
197 | void tty_port_lower_dtr_rts(struct tty_port *port) | ||
198 | { | ||
199 | if (port->ops->dtr_rts) | ||
200 | port->ops->dtr_rts(port, 0); | ||
201 | } | ||
202 | EXPORT_SYMBOL(tty_port_lower_dtr_rts); | ||
203 | |||
204 | /** | ||
205 | * tty_port_block_til_ready - Waiting logic for tty open | ||
206 | * @port: the tty port being opened | ||
207 | * @tty: the tty device being bound | ||
208 | * @filp: the file pointer of the opener | ||
209 | * | ||
210 | * Implement the core POSIX/SuS tty behaviour when opening a tty device. | ||
211 | * Handles: | ||
212 | * - hangup (both before and during) | ||
213 | * - non blocking open | ||
214 | * - rts/dtr/dcd | ||
215 | * - signals | ||
216 | * - port flags and counts | ||
217 | * | ||
218 | * The passed tty_port must implement the carrier_raised method if it can | ||
219 | * do carrier detect and the dtr_rts method if it supports software | ||
220 | * management of these lines. Note that the dtr/rts raise is done each | ||
221 | * iteration as a hangup may have previously dropped them while we wait. | ||
222 | */ | ||
223 | |||
224 | int tty_port_block_til_ready(struct tty_port *port, | ||
225 | struct tty_struct *tty, struct file *filp) | ||
226 | { | ||
227 | int do_clocal = 0, retval; | ||
228 | unsigned long flags; | ||
229 | DEFINE_WAIT(wait); | ||
230 | int cd; | ||
231 | |||
232 | /* block if port is in the process of being closed */ | ||
233 | if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { | ||
234 | wait_event_interruptible_tty(port->close_wait, | ||
235 | !(port->flags & ASYNC_CLOSING)); | ||
236 | if (port->flags & ASYNC_HUP_NOTIFY) | ||
237 | return -EAGAIN; | ||
238 | else | ||
239 | return -ERESTARTSYS; | ||
240 | } | ||
241 | |||
242 | /* if non-blocking mode is set we can pass directly to open unless | ||
243 | the port has just hung up or is in another error state */ | ||
244 | if (tty->flags & (1 << TTY_IO_ERROR)) { | ||
245 | port->flags |= ASYNC_NORMAL_ACTIVE; | ||
246 | return 0; | ||
247 | } | ||
248 | if (filp->f_flags & O_NONBLOCK) { | ||
249 | /* Indicate we are open */ | ||
250 | if (tty->termios->c_cflag & CBAUD) | ||
251 | tty_port_raise_dtr_rts(port); | ||
252 | port->flags |= ASYNC_NORMAL_ACTIVE; | ||
253 | return 0; | ||
254 | } | ||
255 | |||
256 | if (C_CLOCAL(tty)) | ||
257 | do_clocal = 1; | ||
258 | |||
259 | /* Block waiting until we can proceed. We may need to wait for the | ||
260 | carrier, but we must also wait for any close that is in progress | ||
261 | before the next open may complete */ | ||
262 | |||
263 | retval = 0; | ||
264 | |||
265 | /* The port lock protects the port counts */ | ||
266 | spin_lock_irqsave(&port->lock, flags); | ||
267 | if (!tty_hung_up_p(filp)) | ||
268 | port->count--; | ||
269 | port->blocked_open++; | ||
270 | spin_unlock_irqrestore(&port->lock, flags); | ||
271 | |||
272 | while (1) { | ||
273 | /* Indicate we are open */ | ||
274 | if (tty->termios->c_cflag & CBAUD) | ||
275 | tty_port_raise_dtr_rts(port); | ||
276 | |||
277 | prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); | ||
278 | /* Check for a hangup or uninitialised port. | ||
279 | Return accordingly */ | ||
280 | if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) { | ||
281 | if (port->flags & ASYNC_HUP_NOTIFY) | ||
282 | retval = -EAGAIN; | ||
283 | else | ||
284 | retval = -ERESTARTSYS; | ||
285 | break; | ||
286 | } | ||
287 | /* Probe the carrier. For devices with no carrier detect this | ||
288 | will always return true */ | ||
289 | cd = tty_port_carrier_raised(port); | ||
290 | if (!(port->flags & ASYNC_CLOSING) && | ||
291 | (do_clocal || cd)) | ||
292 | break; | ||
293 | if (signal_pending(current)) { | ||
294 | retval = -ERESTARTSYS; | ||
295 | break; | ||
296 | } | ||
297 | tty_unlock(); | ||
298 | schedule(); | ||
299 | tty_lock(); | ||
300 | } | ||
301 | finish_wait(&port->open_wait, &wait); | ||
302 | |||
303 | /* Update counts. A parallel hangup will have set count to zero and | ||
304 | we must not mess that up further */ | ||
305 | spin_lock_irqsave(&port->lock, flags); | ||
306 | if (!tty_hung_up_p(filp)) | ||
307 | port->count++; | ||
308 | port->blocked_open--; | ||
309 | if (retval == 0) | ||
310 | port->flags |= ASYNC_NORMAL_ACTIVE; | ||
311 | spin_unlock_irqrestore(&port->lock, flags); | ||
312 | return retval; | ||
313 | } | ||
314 | EXPORT_SYMBOL(tty_port_block_til_ready); | ||
315 | |||
316 | int tty_port_close_start(struct tty_port *port, | ||
317 | struct tty_struct *tty, struct file *filp) | ||
318 | { | ||
319 | unsigned long flags; | ||
320 | |||
321 | spin_lock_irqsave(&port->lock, flags); | ||
322 | if (tty_hung_up_p(filp)) { | ||
323 | spin_unlock_irqrestore(&port->lock, flags); | ||
324 | return 0; | ||
325 | } | ||
326 | |||
327 | if (tty->count == 1 && port->count != 1) { | ||
328 | printk(KERN_WARNING | ||
329 | "tty_port_close_start: tty->count = 1 port count = %d.\n", | ||
330 | port->count); | ||
331 | port->count = 1; | ||
332 | } | ||
333 | if (--port->count < 0) { | ||
334 | printk(KERN_WARNING "tty_port_close_start: count = %d\n", | ||
335 | port->count); | ||
336 | port->count = 0; | ||
337 | } | ||
338 | |||
339 | if (port->count) { | ||
340 | spin_unlock_irqrestore(&port->lock, flags); | ||
341 | if (port->ops->drop) | ||
342 | port->ops->drop(port); | ||
343 | return 0; | ||
344 | } | ||
345 | set_bit(ASYNCB_CLOSING, &port->flags); | ||
346 | tty->closing = 1; | ||
347 | spin_unlock_irqrestore(&port->lock, flags); | ||
348 | /* Don't block on a stalled port, just pull the chain */ | ||
349 | if (tty->flow_stopped) | ||
350 | tty_driver_flush_buffer(tty); | ||
351 | if (test_bit(ASYNCB_INITIALIZED, &port->flags) && | ||
352 | port->closing_wait != ASYNC_CLOSING_WAIT_NONE) | ||
353 | tty_wait_until_sent(tty, port->closing_wait); | ||
354 | if (port->drain_delay) { | ||
355 | unsigned int bps = tty_get_baud_rate(tty); | ||
356 | long timeout; | ||
357 | |||
358 | if (bps > 1200) | ||
359 | timeout = max_t(long, | ||
360 | (HZ * 10 * port->drain_delay) / bps, HZ / 10); | ||
361 | else | ||
362 | timeout = 2 * HZ; | ||
363 | schedule_timeout_interruptible(timeout); | ||
364 | } | ||
365 | /* Flush the ldisc buffering */ | ||
366 | tty_ldisc_flush(tty); | ||
367 | |||
368 | /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to | ||
369 | hang up the line */ | ||
370 | if (tty->termios->c_cflag & HUPCL) | ||
371 | tty_port_lower_dtr_rts(port); | ||
372 | |||
373 | /* Don't call port->drop for the last reference. Callers will want | ||
374 | to drop the last active reference in ->shutdown() or the tty | ||
375 | shutdown path */ | ||
376 | return 1; | ||
377 | } | ||
378 | EXPORT_SYMBOL(tty_port_close_start); | ||
379 | |||
380 | void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) | ||
381 | { | ||
382 | unsigned long flags; | ||
383 | |||
384 | spin_lock_irqsave(&port->lock, flags); | ||
385 | tty->closing = 0; | ||
386 | |||
387 | if (port->blocked_open) { | ||
388 | spin_unlock_irqrestore(&port->lock, flags); | ||
389 | if (port->close_delay) { | ||
390 | msleep_interruptible( | ||
391 | jiffies_to_msecs(port->close_delay)); | ||
392 | } | ||
393 | spin_lock_irqsave(&port->lock, flags); | ||
394 | wake_up_interruptible(&port->open_wait); | ||
395 | } | ||
396 | port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING); | ||
397 | wake_up_interruptible(&port->close_wait); | ||
398 | spin_unlock_irqrestore(&port->lock, flags); | ||
399 | } | ||
400 | EXPORT_SYMBOL(tty_port_close_end); | ||
401 | |||
402 | void tty_port_close(struct tty_port *port, struct tty_struct *tty, | ||
403 | struct file *filp) | ||
404 | { | ||
405 | if (tty_port_close_start(port, tty, filp) == 0) | ||
406 | return; | ||
407 | tty_port_shutdown(port); | ||
408 | set_bit(TTY_IO_ERROR, &tty->flags); | ||
409 | tty_port_close_end(port, tty); | ||
410 | tty_port_tty_set(port, NULL); | ||
411 | } | ||
412 | EXPORT_SYMBOL(tty_port_close); | ||
413 | |||
414 | int tty_port_open(struct tty_port *port, struct tty_struct *tty, | ||
415 | struct file *filp) | ||
416 | { | ||
417 | spin_lock_irq(&port->lock); | ||
418 | if (!tty_hung_up_p(filp)) | ||
419 | ++port->count; | ||
420 | spin_unlock_irq(&port->lock); | ||
421 | tty_port_tty_set(port, tty); | ||
422 | |||
423 | /* | ||
424 | * Do the device-specific open only if the hardware isn't | ||
425 | * already initialized. Serialize open and shutdown using the | ||
426 | * port mutex. | ||
427 | */ | ||
428 | |||
429 | mutex_lock(&port->mutex); | ||
430 | |||
431 | if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) { | ||
432 | clear_bit(TTY_IO_ERROR, &tty->flags); | ||
433 | if (port->ops->activate) { | ||
434 | int retval = port->ops->activate(port, tty); | ||
435 | if (retval) { | ||
436 | mutex_unlock(&port->mutex); | ||
437 | return retval; | ||
438 | } | ||
439 | } | ||
440 | set_bit(ASYNCB_INITIALIZED, &port->flags); | ||
441 | } | ||
442 | mutex_unlock(&port->mutex); | ||
443 | return tty_port_block_til_ready(port, tty, filp); | ||
444 | } | ||
445 | |||
446 | EXPORT_SYMBOL(tty_port_open); | ||
diff --git a/drivers/tty/vt/.gitignore b/drivers/tty/vt/.gitignore new file mode 100644 index 000000000000..83683a2d8e6a --- /dev/null +++ b/drivers/tty/vt/.gitignore | |||
@@ -0,0 +1,2 @@ | |||
1 | consolemap_deftbl.c | ||
2 | defkeymap.c | ||
diff --git a/drivers/tty/vt/Makefile b/drivers/tty/vt/Makefile new file mode 100644 index 000000000000..14a51c9960df --- /dev/null +++ b/drivers/tty/vt/Makefile | |||
@@ -0,0 +1,34 @@ | |||
1 | # | ||
2 | # This file contains the font map for the default (hardware) font | ||
3 | # | ||
4 | FONTMAPFILE = cp437.uni | ||
5 | |||
6 | obj-$(CONFIG_VT) += vt_ioctl.o vc_screen.o \ | ||
7 | selection.o keyboard.o | ||
8 | obj-$(CONFIG_CONSOLE_TRANSLATIONS) += consolemap.o consolemap_deftbl.o | ||
9 | obj-$(CONFIG_HW_CONSOLE) += vt.o defkeymap.o | ||
10 | |||
11 | # Files generated that shall be removed upon make clean | ||
12 | clean-files := consolemap_deftbl.c defkeymap.c | ||
13 | |||
14 | quiet_cmd_conmk = CONMK $@ | ||
15 | cmd_conmk = scripts/conmakehash $< > $@ | ||
16 | |||
17 | $(obj)/consolemap_deftbl.c: $(src)/$(FONTMAPFILE) | ||
18 | $(call cmd,conmk) | ||
19 | |||
20 | $(obj)/defkeymap.o: $(obj)/defkeymap.c | ||
21 | |||
22 | # Uncomment if you're changing the keymap and have an appropriate | ||
23 | # loadkeys version for the map. By default, we'll use the shipped | ||
24 | # versions. | ||
25 | # GENERATE_KEYMAP := 1 | ||
26 | |||
27 | ifdef GENERATE_KEYMAP | ||
28 | |||
29 | $(obj)/defkeymap.c: $(obj)/%.c: $(src)/%.map | ||
30 | loadkeys --mktable $< > $@.tmp | ||
31 | sed -e 's/^static *//' $@.tmp > $@ | ||
32 | rm $@.tmp | ||
33 | |||
34 | endif | ||
diff --git a/drivers/tty/vt/consolemap.c b/drivers/tty/vt/consolemap.c new file mode 100644 index 000000000000..45d3e80156d4 --- /dev/null +++ b/drivers/tty/vt/consolemap.c | |||
@@ -0,0 +1,745 @@ | |||
1 | /* | ||
2 | * consolemap.c | ||
3 | * | ||
4 | * Mapping from internal code (such as Latin-1 or Unicode or IBM PC code) | ||
5 | * to font positions. | ||
6 | * | ||
7 | * aeb, 950210 | ||
8 | * | ||
9 | * Support for multiple unimaps by Jakub Jelinek <jj@ultra.linux.cz>, July 1998 | ||
10 | * | ||
11 | * Fix bug in inverse translation. Stanislav Voronyi <stas@cnti.uanet.kharkov.ua>, Dec 1998 | ||
12 | */ | ||
13 | |||
14 | #include <linux/module.h> | ||
15 | #include <linux/kd.h> | ||
16 | #include <linux/errno.h> | ||
17 | #include <linux/mm.h> | ||
18 | #include <linux/slab.h> | ||
19 | #include <linux/init.h> | ||
20 | #include <linux/tty.h> | ||
21 | #include <asm/uaccess.h> | ||
22 | #include <linux/consolemap.h> | ||
23 | #include <linux/vt_kern.h> | ||
24 | |||
25 | static unsigned short translations[][256] = { | ||
26 | /* 8-bit Latin-1 mapped to Unicode -- trivial mapping */ | ||
27 | { | ||
28 | 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, | ||
29 | 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, | ||
30 | 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, | ||
31 | 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f, | ||
32 | 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, | ||
33 | 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f, | ||
34 | 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, | ||
35 | 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, | ||
36 | 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, | ||
37 | 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, | ||
38 | 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, | ||
39 | 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, | ||
40 | 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, | ||
41 | 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f, | ||
42 | 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, | ||
43 | 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f, | ||
44 | 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, | ||
45 | 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f, | ||
46 | 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, | ||
47 | 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f, | ||
48 | 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, | ||
49 | 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, | ||
50 | 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, | ||
51 | 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, | ||
52 | 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, | ||
53 | 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, | ||
54 | 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, | ||
55 | 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, | ||
56 | 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, | ||
57 | 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, | ||
58 | 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, | ||
59 | 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff | ||
60 | }, | ||
61 | /* VT100 graphics mapped to Unicode */ | ||
62 | { | ||
63 | 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, | ||
64 | 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, | ||
65 | 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, | ||
66 | 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f, | ||
67 | 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, | ||
68 | 0x0028, 0x0029, 0x002a, 0x2192, 0x2190, 0x2191, 0x2193, 0x002f, | ||
69 | 0x2588, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, | ||
70 | 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, | ||
71 | 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, | ||
72 | 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, | ||
73 | 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, | ||
74 | 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x00a0, | ||
75 | 0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1, | ||
76 | 0x2591, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, | ||
77 | 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, | ||
78 | 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, 0x007f, | ||
79 | 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, | ||
80 | 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f, | ||
81 | 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, | ||
82 | 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f, | ||
83 | 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, | ||
84 | 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, | ||
85 | 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, | ||
86 | 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, | ||
87 | 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, | ||
88 | 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, | ||
89 | 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, | ||
90 | 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, | ||
91 | 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, | ||
92 | 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, | ||
93 | 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, | ||
94 | 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff | ||
95 | }, | ||
96 | /* IBM Codepage 437 mapped to Unicode */ | ||
97 | { | ||
98 | 0x0000, 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, | ||
99 | 0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c, | ||
100 | 0x25b6, 0x25c0, 0x2195, 0x203c, 0x00b6, 0x00a7, 0x25ac, 0x21a8, | ||
101 | 0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc, | ||
102 | 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, | ||
103 | 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f, | ||
104 | 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, | ||
105 | 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, | ||
106 | 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, | ||
107 | 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, | ||
108 | 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, | ||
109 | 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, | ||
110 | 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, | ||
111 | 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f, | ||
112 | 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, | ||
113 | 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x2302, | ||
114 | 0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5, 0x00e7, | ||
115 | 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5, | ||
116 | 0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9, | ||
117 | 0x00ff, 0x00d6, 0x00dc, 0x00a2, 0x00a3, 0x00a5, 0x20a7, 0x0192, | ||
118 | 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x00aa, 0x00ba, | ||
119 | 0x00bf, 0x2310, 0x00ac, 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb, | ||
120 | 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, | ||
121 | 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510, | ||
122 | 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f, | ||
123 | 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567, | ||
124 | 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b, | ||
125 | 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580, | ||
126 | 0x03b1, 0x00df, 0x0393, 0x03c0, 0x03a3, 0x03c3, 0x00b5, 0x03c4, | ||
127 | 0x03a6, 0x0398, 0x03a9, 0x03b4, 0x221e, 0x03c6, 0x03b5, 0x2229, | ||
128 | 0x2261, 0x00b1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00f7, 0x2248, | ||
129 | 0x00b0, 0x2219, 0x00b7, 0x221a, 0x207f, 0x00b2, 0x25a0, 0x00a0 | ||
130 | }, | ||
131 | /* User mapping -- default to codes for direct font mapping */ | ||
132 | { | ||
133 | 0xf000, 0xf001, 0xf002, 0xf003, 0xf004, 0xf005, 0xf006, 0xf007, | ||
134 | 0xf008, 0xf009, 0xf00a, 0xf00b, 0xf00c, 0xf00d, 0xf00e, 0xf00f, | ||
135 | 0xf010, 0xf011, 0xf012, 0xf013, 0xf014, 0xf015, 0xf016, 0xf017, | ||
136 | 0xf018, 0xf019, 0xf01a, 0xf01b, 0xf01c, 0xf01d, 0xf01e, 0xf01f, | ||
137 | 0xf020, 0xf021, 0xf022, 0xf023, 0xf024, 0xf025, 0xf026, 0xf027, | ||
138 | 0xf028, 0xf029, 0xf02a, 0xf02b, 0xf02c, 0xf02d, 0xf02e, 0xf02f, | ||
139 | 0xf030, 0xf031, 0xf032, 0xf033, 0xf034, 0xf035, 0xf036, 0xf037, | ||
140 | 0xf038, 0xf039, 0xf03a, 0xf03b, 0xf03c, 0xf03d, 0xf03e, 0xf03f, | ||
141 | 0xf040, 0xf041, 0xf042, 0xf043, 0xf044, 0xf045, 0xf046, 0xf047, | ||
142 | 0xf048, 0xf049, 0xf04a, 0xf04b, 0xf04c, 0xf04d, 0xf04e, 0xf04f, | ||
143 | 0xf050, 0xf051, 0xf052, 0xf053, 0xf054, 0xf055, 0xf056, 0xf057, | ||
144 | 0xf058, 0xf059, 0xf05a, 0xf05b, 0xf05c, 0xf05d, 0xf05e, 0xf05f, | ||
145 | 0xf060, 0xf061, 0xf062, 0xf063, 0xf064, 0xf065, 0xf066, 0xf067, | ||
146 | 0xf068, 0xf069, 0xf06a, 0xf06b, 0xf06c, 0xf06d, 0xf06e, 0xf06f, | ||
147 | 0xf070, 0xf071, 0xf072, 0xf073, 0xf074, 0xf075, 0xf076, 0xf077, | ||
148 | 0xf078, 0xf079, 0xf07a, 0xf07b, 0xf07c, 0xf07d, 0xf07e, 0xf07f, | ||
149 | 0xf080, 0xf081, 0xf082, 0xf083, 0xf084, 0xf085, 0xf086, 0xf087, | ||
150 | 0xf088, 0xf089, 0xf08a, 0xf08b, 0xf08c, 0xf08d, 0xf08e, 0xf08f, | ||
151 | 0xf090, 0xf091, 0xf092, 0xf093, 0xf094, 0xf095, 0xf096, 0xf097, | ||
152 | 0xf098, 0xf099, 0xf09a, 0xf09b, 0xf09c, 0xf09d, 0xf09e, 0xf09f, | ||
153 | 0xf0a0, 0xf0a1, 0xf0a2, 0xf0a3, 0xf0a4, 0xf0a5, 0xf0a6, 0xf0a7, | ||
154 | 0xf0a8, 0xf0a9, 0xf0aa, 0xf0ab, 0xf0ac, 0xf0ad, 0xf0ae, 0xf0af, | ||
155 | 0xf0b0, 0xf0b1, 0xf0b2, 0xf0b3, 0xf0b4, 0xf0b5, 0xf0b6, 0xf0b7, | ||
156 | 0xf0b8, 0xf0b9, 0xf0ba, 0xf0bb, 0xf0bc, 0xf0bd, 0xf0be, 0xf0bf, | ||
157 | 0xf0c0, 0xf0c1, 0xf0c2, 0xf0c3, 0xf0c4, 0xf0c5, 0xf0c6, 0xf0c7, | ||
158 | 0xf0c8, 0xf0c9, 0xf0ca, 0xf0cb, 0xf0cc, 0xf0cd, 0xf0ce, 0xf0cf, | ||
159 | 0xf0d0, 0xf0d1, 0xf0d2, 0xf0d3, 0xf0d4, 0xf0d5, 0xf0d6, 0xf0d7, | ||
160 | 0xf0d8, 0xf0d9, 0xf0da, 0xf0db, 0xf0dc, 0xf0dd, 0xf0de, 0xf0df, | ||
161 | 0xf0e0, 0xf0e1, 0xf0e2, 0xf0e3, 0xf0e4, 0xf0e5, 0xf0e6, 0xf0e7, | ||
162 | 0xf0e8, 0xf0e9, 0xf0ea, 0xf0eb, 0xf0ec, 0xf0ed, 0xf0ee, 0xf0ef, | ||
163 | 0xf0f0, 0xf0f1, 0xf0f2, 0xf0f3, 0xf0f4, 0xf0f5, 0xf0f6, 0xf0f7, | ||
164 | 0xf0f8, 0xf0f9, 0xf0fa, 0xf0fb, 0xf0fc, 0xf0fd, 0xf0fe, 0xf0ff | ||
165 | } | ||
166 | }; | ||
167 | |||
168 | /* The standard kernel character-to-font mappings are not invertible | ||
169 | -- this is just a best effort. */ | ||
170 | |||
171 | #define MAX_GLYPH 512 /* Max possible glyph value */ | ||
172 | |||
173 | static int inv_translate[MAX_NR_CONSOLES]; | ||
174 | |||
175 | struct uni_pagedir { | ||
176 | u16 **uni_pgdir[32]; | ||
177 | unsigned long refcount; | ||
178 | unsigned long sum; | ||
179 | unsigned char *inverse_translations[4]; | ||
180 | u16 *inverse_trans_unicode; | ||
181 | int readonly; | ||
182 | }; | ||
183 | |||
184 | static struct uni_pagedir *dflt; | ||
185 | |||
186 | static void set_inverse_transl(struct vc_data *conp, struct uni_pagedir *p, int i) | ||
187 | { | ||
188 | int j, glyph; | ||
189 | unsigned short *t = translations[i]; | ||
190 | unsigned char *q; | ||
191 | |||
192 | if (!p) return; | ||
193 | q = p->inverse_translations[i]; | ||
194 | |||
195 | if (!q) { | ||
196 | q = p->inverse_translations[i] = (unsigned char *) | ||
197 | kmalloc(MAX_GLYPH, GFP_KERNEL); | ||
198 | if (!q) return; | ||
199 | } | ||
200 | memset(q, 0, MAX_GLYPH); | ||
201 | |||
202 | for (j = 0; j < E_TABSZ; j++) { | ||
203 | glyph = conv_uni_to_pc(conp, t[j]); | ||
204 | if (glyph >= 0 && glyph < MAX_GLYPH && q[glyph] < 32) { | ||
205 | /* prefer '-' above SHY etc. */ | ||
206 | q[glyph] = j; | ||
207 | } | ||
208 | } | ||
209 | } | ||
210 | |||
211 | static void set_inverse_trans_unicode(struct vc_data *conp, | ||
212 | struct uni_pagedir *p) | ||
213 | { | ||
214 | int i, j, k, glyph; | ||
215 | u16 **p1, *p2; | ||
216 | u16 *q; | ||
217 | |||
218 | if (!p) return; | ||
219 | q = p->inverse_trans_unicode; | ||
220 | if (!q) { | ||
221 | q = p->inverse_trans_unicode = | ||
222 | kmalloc(MAX_GLYPH * sizeof(u16), GFP_KERNEL); | ||
223 | if (!q) | ||
224 | return; | ||
225 | } | ||
226 | memset(q, 0, MAX_GLYPH * sizeof(u16)); | ||
227 | |||
228 | for (i = 0; i < 32; i++) { | ||
229 | p1 = p->uni_pgdir[i]; | ||
230 | if (!p1) | ||
231 | continue; | ||
232 | for (j = 0; j < 32; j++) { | ||
233 | p2 = p1[j]; | ||
234 | if (!p2) | ||
235 | continue; | ||
236 | for (k = 0; k < 64; k++) { | ||
237 | glyph = p2[k]; | ||
238 | if (glyph >= 0 && glyph < MAX_GLYPH | ||
239 | && q[glyph] < 32) | ||
240 | q[glyph] = (i << 11) + (j << 6) + k; | ||
241 | } | ||
242 | } | ||
243 | } | ||
244 | } | ||
245 | |||
246 | unsigned short *set_translate(int m, struct vc_data *vc) | ||
247 | { | ||
248 | inv_translate[vc->vc_num] = m; | ||
249 | return translations[m]; | ||
250 | } | ||
251 | |||
252 | /* | ||
253 | * Inverse translation is impossible for several reasons: | ||
254 | * 1. The font<->character maps are not 1-1. | ||
255 | * 2. The text may have been written while a different translation map | ||
256 | * was active. | ||
257 | * Still, it is now possible to a certain extent to cut and paste non-ASCII. | ||
258 | */ | ||
259 | u16 inverse_translate(struct vc_data *conp, int glyph, int use_unicode) | ||
260 | { | ||
261 | struct uni_pagedir *p; | ||
262 | int m; | ||
263 | if (glyph < 0 || glyph >= MAX_GLYPH) | ||
264 | return 0; | ||
265 | else if (!(p = (struct uni_pagedir *)*conp->vc_uni_pagedir_loc)) | ||
266 | return glyph; | ||
267 | else if (use_unicode) { | ||
268 | if (!p->inverse_trans_unicode) | ||
269 | return glyph; | ||
270 | else | ||
271 | return p->inverse_trans_unicode[glyph]; | ||
272 | } else { | ||
273 | m = inv_translate[conp->vc_num]; | ||
274 | if (!p->inverse_translations[m]) | ||
275 | return glyph; | ||
276 | else | ||
277 | return p->inverse_translations[m][glyph]; | ||
278 | } | ||
279 | } | ||
280 | EXPORT_SYMBOL_GPL(inverse_translate); | ||
281 | |||
282 | static void update_user_maps(void) | ||
283 | { | ||
284 | int i; | ||
285 | struct uni_pagedir *p, *q = NULL; | ||
286 | |||
287 | for (i = 0; i < MAX_NR_CONSOLES; i++) { | ||
288 | if (!vc_cons_allocated(i)) | ||
289 | continue; | ||
290 | p = (struct uni_pagedir *)*vc_cons[i].d->vc_uni_pagedir_loc; | ||
291 | if (p && p != q) { | ||
292 | set_inverse_transl(vc_cons[i].d, p, USER_MAP); | ||
293 | set_inverse_trans_unicode(vc_cons[i].d, p); | ||
294 | q = p; | ||
295 | } | ||
296 | } | ||
297 | } | ||
298 | |||
299 | /* | ||
300 | * Load customizable translation table | ||
301 | * arg points to a 256 byte translation table. | ||
302 | * | ||
303 | * The "old" variants are for translation directly to font (using the | ||
304 | * 0xf000-0xf0ff "transparent" Unicodes) whereas the "new" variants set | ||
305 | * Unicodes explicitly. | ||
306 | */ | ||
307 | int con_set_trans_old(unsigned char __user * arg) | ||
308 | { | ||
309 | int i; | ||
310 | unsigned short *p = translations[USER_MAP]; | ||
311 | |||
312 | if (!access_ok(VERIFY_READ, arg, E_TABSZ)) | ||
313 | return -EFAULT; | ||
314 | |||
315 | for (i=0; i<E_TABSZ ; i++) { | ||
316 | unsigned char uc; | ||
317 | __get_user(uc, arg+i); | ||
318 | p[i] = UNI_DIRECT_BASE | uc; | ||
319 | } | ||
320 | |||
321 | update_user_maps(); | ||
322 | return 0; | ||
323 | } | ||
324 | |||
325 | int con_get_trans_old(unsigned char __user * arg) | ||
326 | { | ||
327 | int i, ch; | ||
328 | unsigned short *p = translations[USER_MAP]; | ||
329 | |||
330 | if (!access_ok(VERIFY_WRITE, arg, E_TABSZ)) | ||
331 | return -EFAULT; | ||
332 | |||
333 | for (i=0; i<E_TABSZ ; i++) | ||
334 | { | ||
335 | ch = conv_uni_to_pc(vc_cons[fg_console].d, p[i]); | ||
336 | __put_user((ch & ~0xff) ? 0 : ch, arg+i); | ||
337 | } | ||
338 | return 0; | ||
339 | } | ||
340 | |||
341 | int con_set_trans_new(ushort __user * arg) | ||
342 | { | ||
343 | int i; | ||
344 | unsigned short *p = translations[USER_MAP]; | ||
345 | |||
346 | if (!access_ok(VERIFY_READ, arg, E_TABSZ*sizeof(unsigned short))) | ||
347 | return -EFAULT; | ||
348 | |||
349 | for (i=0; i<E_TABSZ ; i++) { | ||
350 | unsigned short us; | ||
351 | __get_user(us, arg+i); | ||
352 | p[i] = us; | ||
353 | } | ||
354 | |||
355 | update_user_maps(); | ||
356 | return 0; | ||
357 | } | ||
358 | |||
359 | int con_get_trans_new(ushort __user * arg) | ||
360 | { | ||
361 | int i; | ||
362 | unsigned short *p = translations[USER_MAP]; | ||
363 | |||
364 | if (!access_ok(VERIFY_WRITE, arg, E_TABSZ*sizeof(unsigned short))) | ||
365 | return -EFAULT; | ||
366 | |||
367 | for (i=0; i<E_TABSZ ; i++) | ||
368 | __put_user(p[i], arg+i); | ||
369 | |||
370 | return 0; | ||
371 | } | ||
372 | |||
373 | /* | ||
374 | * Unicode -> current font conversion | ||
375 | * | ||
376 | * A font has at most 512 chars, usually 256. | ||
377 | * But one font position may represent several Unicode chars. | ||
378 | * A hashtable is somewhat of a pain to deal with, so use a | ||
379 | * "paged table" instead. Simulation has shown the memory cost of | ||
380 | * this 3-level paged table scheme to be comparable to a hash table. | ||
381 | */ | ||
382 | |||
383 | extern u8 dfont_unicount[]; /* Defined in console_defmap.c */ | ||
384 | extern u16 dfont_unitable[]; | ||
385 | |||
386 | static void con_release_unimap(struct uni_pagedir *p) | ||
387 | { | ||
388 | u16 **p1; | ||
389 | int i, j; | ||
390 | |||
391 | if (p == dflt) dflt = NULL; | ||
392 | for (i = 0; i < 32; i++) { | ||
393 | if ((p1 = p->uni_pgdir[i]) != NULL) { | ||
394 | for (j = 0; j < 32; j++) | ||
395 | kfree(p1[j]); | ||
396 | kfree(p1); | ||
397 | } | ||
398 | p->uni_pgdir[i] = NULL; | ||
399 | } | ||
400 | for (i = 0; i < 4; i++) { | ||
401 | kfree(p->inverse_translations[i]); | ||
402 | p->inverse_translations[i] = NULL; | ||
403 | } | ||
404 | if (p->inverse_trans_unicode) { | ||
405 | kfree(p->inverse_trans_unicode); | ||
406 | p->inverse_trans_unicode = NULL; | ||
407 | } | ||
408 | } | ||
409 | |||
410 | void con_free_unimap(struct vc_data *vc) | ||
411 | { | ||
412 | struct uni_pagedir *p; | ||
413 | |||
414 | p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; | ||
415 | if (!p) | ||
416 | return; | ||
417 | *vc->vc_uni_pagedir_loc = 0; | ||
418 | if (--p->refcount) | ||
419 | return; | ||
420 | con_release_unimap(p); | ||
421 | kfree(p); | ||
422 | } | ||
423 | |||
424 | static int con_unify_unimap(struct vc_data *conp, struct uni_pagedir *p) | ||
425 | { | ||
426 | int i, j, k; | ||
427 | struct uni_pagedir *q; | ||
428 | |||
429 | for (i = 0; i < MAX_NR_CONSOLES; i++) { | ||
430 | if (!vc_cons_allocated(i)) | ||
431 | continue; | ||
432 | q = (struct uni_pagedir *)*vc_cons[i].d->vc_uni_pagedir_loc; | ||
433 | if (!q || q == p || q->sum != p->sum) | ||
434 | continue; | ||
435 | for (j = 0; j < 32; j++) { | ||
436 | u16 **p1, **q1; | ||
437 | p1 = p->uni_pgdir[j]; q1 = q->uni_pgdir[j]; | ||
438 | if (!p1 && !q1) | ||
439 | continue; | ||
440 | if (!p1 || !q1) | ||
441 | break; | ||
442 | for (k = 0; k < 32; k++) { | ||
443 | if (!p1[k] && !q1[k]) | ||
444 | continue; | ||
445 | if (!p1[k] || !q1[k]) | ||
446 | break; | ||
447 | if (memcmp(p1[k], q1[k], 64*sizeof(u16))) | ||
448 | break; | ||
449 | } | ||
450 | if (k < 32) | ||
451 | break; | ||
452 | } | ||
453 | if (j == 32) { | ||
454 | q->refcount++; | ||
455 | *conp->vc_uni_pagedir_loc = (unsigned long)q; | ||
456 | con_release_unimap(p); | ||
457 | kfree(p); | ||
458 | return 1; | ||
459 | } | ||
460 | } | ||
461 | return 0; | ||
462 | } | ||
463 | |||
464 | static int | ||
465 | con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos) | ||
466 | { | ||
467 | int i, n; | ||
468 | u16 **p1, *p2; | ||
469 | |||
470 | if (!(p1 = p->uni_pgdir[n = unicode >> 11])) { | ||
471 | p1 = p->uni_pgdir[n] = kmalloc(32*sizeof(u16 *), GFP_KERNEL); | ||
472 | if (!p1) return -ENOMEM; | ||
473 | for (i = 0; i < 32; i++) | ||
474 | p1[i] = NULL; | ||
475 | } | ||
476 | |||
477 | if (!(p2 = p1[n = (unicode >> 6) & 0x1f])) { | ||
478 | p2 = p1[n] = kmalloc(64*sizeof(u16), GFP_KERNEL); | ||
479 | if (!p2) return -ENOMEM; | ||
480 | memset(p2, 0xff, 64*sizeof(u16)); /* No glyphs for the characters (yet) */ | ||
481 | } | ||
482 | |||
483 | p2[unicode & 0x3f] = fontpos; | ||
484 | |||
485 | p->sum += (fontpos << 20) + unicode; | ||
486 | |||
487 | return 0; | ||
488 | } | ||
489 | |||
490 | /* ui is a leftover from using a hashtable, but might be used again */ | ||
491 | int con_clear_unimap(struct vc_data *vc, struct unimapinit *ui) | ||
492 | { | ||
493 | struct uni_pagedir *p, *q; | ||
494 | |||
495 | p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; | ||
496 | if (p && p->readonly) return -EIO; | ||
497 | if (!p || --p->refcount) { | ||
498 | q = kzalloc(sizeof(*p), GFP_KERNEL); | ||
499 | if (!q) { | ||
500 | if (p) p->refcount++; | ||
501 | return -ENOMEM; | ||
502 | } | ||
503 | q->refcount=1; | ||
504 | *vc->vc_uni_pagedir_loc = (unsigned long)q; | ||
505 | } else { | ||
506 | if (p == dflt) dflt = NULL; | ||
507 | p->refcount++; | ||
508 | p->sum = 0; | ||
509 | con_release_unimap(p); | ||
510 | } | ||
511 | return 0; | ||
512 | } | ||
513 | |||
514 | int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list) | ||
515 | { | ||
516 | int err = 0, err1, i; | ||
517 | struct uni_pagedir *p, *q; | ||
518 | |||
519 | p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; | ||
520 | if (p->readonly) return -EIO; | ||
521 | |||
522 | if (!ct) return 0; | ||
523 | |||
524 | if (p->refcount > 1) { | ||
525 | int j, k; | ||
526 | u16 **p1, *p2, l; | ||
527 | |||
528 | err1 = con_clear_unimap(vc, NULL); | ||
529 | if (err1) return err1; | ||
530 | |||
531 | q = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; | ||
532 | for (i = 0, l = 0; i < 32; i++) | ||
533 | if ((p1 = p->uni_pgdir[i])) | ||
534 | for (j = 0; j < 32; j++) | ||
535 | if ((p2 = p1[j])) | ||
536 | for (k = 0; k < 64; k++, l++) | ||
537 | if (p2[k] != 0xffff) { | ||
538 | err1 = con_insert_unipair(q, l, p2[k]); | ||
539 | if (err1) { | ||
540 | p->refcount++; | ||
541 | *vc->vc_uni_pagedir_loc = (unsigned long)p; | ||
542 | con_release_unimap(q); | ||
543 | kfree(q); | ||
544 | return err1; | ||
545 | } | ||
546 | } | ||
547 | p = q; | ||
548 | } else if (p == dflt) | ||
549 | dflt = NULL; | ||
550 | |||
551 | while (ct--) { | ||
552 | unsigned short unicode, fontpos; | ||
553 | __get_user(unicode, &list->unicode); | ||
554 | __get_user(fontpos, &list->fontpos); | ||
555 | if ((err1 = con_insert_unipair(p, unicode,fontpos)) != 0) | ||
556 | err = err1; | ||
557 | list++; | ||
558 | } | ||
559 | |||
560 | if (con_unify_unimap(vc, p)) | ||
561 | return err; | ||
562 | |||
563 | for (i = 0; i <= 3; i++) | ||
564 | set_inverse_transl(vc, p, i); /* Update all inverse translations */ | ||
565 | set_inverse_trans_unicode(vc, p); | ||
566 | |||
567 | return err; | ||
568 | } | ||
569 | |||
570 | /* Loads the unimap for the hardware font, as defined in uni_hash.tbl. | ||
571 | The representation used was the most compact I could come up | ||
572 | with. This routine is executed at sys_setup time, and when the | ||
573 | PIO_FONTRESET ioctl is called. */ | ||
574 | |||
575 | int con_set_default_unimap(struct vc_data *vc) | ||
576 | { | ||
577 | int i, j, err = 0, err1; | ||
578 | u16 *q; | ||
579 | struct uni_pagedir *p; | ||
580 | |||
581 | if (dflt) { | ||
582 | p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; | ||
583 | if (p == dflt) | ||
584 | return 0; | ||
585 | dflt->refcount++; | ||
586 | *vc->vc_uni_pagedir_loc = (unsigned long)dflt; | ||
587 | if (p && --p->refcount) { | ||
588 | con_release_unimap(p); | ||
589 | kfree(p); | ||
590 | } | ||
591 | return 0; | ||
592 | } | ||
593 | |||
594 | /* The default font is always 256 characters */ | ||
595 | |||
596 | err = con_clear_unimap(vc, NULL); | ||
597 | if (err) return err; | ||
598 | |||
599 | p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; | ||
600 | q = dfont_unitable; | ||
601 | |||
602 | for (i = 0; i < 256; i++) | ||
603 | for (j = dfont_unicount[i]; j; j--) { | ||
604 | err1 = con_insert_unipair(p, *(q++), i); | ||
605 | if (err1) | ||
606 | err = err1; | ||
607 | } | ||
608 | |||
609 | if (con_unify_unimap(vc, p)) { | ||
610 | dflt = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; | ||
611 | return err; | ||
612 | } | ||
613 | |||
614 | for (i = 0; i <= 3; i++) | ||
615 | set_inverse_transl(vc, p, i); /* Update all inverse translations */ | ||
616 | set_inverse_trans_unicode(vc, p); | ||
617 | dflt = p; | ||
618 | return err; | ||
619 | } | ||
620 | EXPORT_SYMBOL(con_set_default_unimap); | ||
621 | |||
622 | int con_copy_unimap(struct vc_data *dst_vc, struct vc_data *src_vc) | ||
623 | { | ||
624 | struct uni_pagedir *q; | ||
625 | |||
626 | if (!*src_vc->vc_uni_pagedir_loc) | ||
627 | return -EINVAL; | ||
628 | if (*dst_vc->vc_uni_pagedir_loc == *src_vc->vc_uni_pagedir_loc) | ||
629 | return 0; | ||
630 | con_free_unimap(dst_vc); | ||
631 | q = (struct uni_pagedir *)*src_vc->vc_uni_pagedir_loc; | ||
632 | q->refcount++; | ||
633 | *dst_vc->vc_uni_pagedir_loc = (long)q; | ||
634 | return 0; | ||
635 | } | ||
636 | |||
637 | int con_get_unimap(struct vc_data *vc, ushort ct, ushort __user *uct, struct unipair __user *list) | ||
638 | { | ||
639 | int i, j, k, ect; | ||
640 | u16 **p1, *p2; | ||
641 | struct uni_pagedir *p; | ||
642 | |||
643 | ect = 0; | ||
644 | if (*vc->vc_uni_pagedir_loc) { | ||
645 | p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; | ||
646 | for (i = 0; i < 32; i++) | ||
647 | if ((p1 = p->uni_pgdir[i])) | ||
648 | for (j = 0; j < 32; j++) | ||
649 | if ((p2 = *(p1++))) | ||
650 | for (k = 0; k < 64; k++) { | ||
651 | if (*p2 < MAX_GLYPH && ect++ < ct) { | ||
652 | __put_user((u_short)((i<<11)+(j<<6)+k), | ||
653 | &list->unicode); | ||
654 | __put_user((u_short) *p2, | ||
655 | &list->fontpos); | ||
656 | list++; | ||
657 | } | ||
658 | p2++; | ||
659 | } | ||
660 | } | ||
661 | __put_user(ect, uct); | ||
662 | return ((ect <= ct) ? 0 : -ENOMEM); | ||
663 | } | ||
664 | |||
665 | void con_protect_unimap(struct vc_data *vc, int rdonly) | ||
666 | { | ||
667 | struct uni_pagedir *p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; | ||
668 | |||
669 | if (p) | ||
670 | p->readonly = rdonly; | ||
671 | } | ||
672 | |||
673 | /* | ||
674 | * Always use USER_MAP. These functions are used by the keyboard, | ||
675 | * which shouldn't be affected by G0/G1 switching, etc. | ||
676 | * If the user map still contains default values, i.e. the | ||
677 | * direct-to-font mapping, then assume user is using Latin1. | ||
678 | */ | ||
679 | /* may be called during an interrupt */ | ||
680 | u32 conv_8bit_to_uni(unsigned char c) | ||
681 | { | ||
682 | unsigned short uni = translations[USER_MAP][c]; | ||
683 | return uni == (0xf000 | c) ? c : uni; | ||
684 | } | ||
685 | |||
686 | int conv_uni_to_8bit(u32 uni) | ||
687 | { | ||
688 | int c; | ||
689 | for (c = 0; c < 0x100; c++) | ||
690 | if (translations[USER_MAP][c] == uni || | ||
691 | (translations[USER_MAP][c] == (c | 0xf000) && uni == c)) | ||
692 | return c; | ||
693 | return -1; | ||
694 | } | ||
695 | |||
696 | int | ||
697 | conv_uni_to_pc(struct vc_data *conp, long ucs) | ||
698 | { | ||
699 | int h; | ||
700 | u16 **p1, *p2; | ||
701 | struct uni_pagedir *p; | ||
702 | |||
703 | /* Only 16-bit codes supported at this time */ | ||
704 | if (ucs > 0xffff) | ||
705 | return -4; /* Not found */ | ||
706 | else if (ucs < 0x20) | ||
707 | return -1; /* Not a printable character */ | ||
708 | else if (ucs == 0xfeff || (ucs >= 0x200b && ucs <= 0x200f)) | ||
709 | return -2; /* Zero-width space */ | ||
710 | /* | ||
711 | * UNI_DIRECT_BASE indicates the start of the region in the User Zone | ||
712 | * which always has a 1:1 mapping to the currently loaded font. The | ||
713 | * UNI_DIRECT_MASK indicates the bit span of the region. | ||
714 | */ | ||
715 | else if ((ucs & ~UNI_DIRECT_MASK) == UNI_DIRECT_BASE) | ||
716 | return ucs & UNI_DIRECT_MASK; | ||
717 | |||
718 | if (!*conp->vc_uni_pagedir_loc) | ||
719 | return -3; | ||
720 | |||
721 | p = (struct uni_pagedir *)*conp->vc_uni_pagedir_loc; | ||
722 | if ((p1 = p->uni_pgdir[ucs >> 11]) && | ||
723 | (p2 = p1[(ucs >> 6) & 0x1f]) && | ||
724 | (h = p2[ucs & 0x3f]) < MAX_GLYPH) | ||
725 | return h; | ||
726 | |||
727 | return -4; /* not found */ | ||
728 | } | ||
729 | |||
730 | /* | ||
731 | * This is called at sys_setup time, after memory and the console are | ||
732 | * initialized. It must be possible to call kmalloc(..., GFP_KERNEL) | ||
733 | * from this function, hence the call from sys_setup. | ||
734 | */ | ||
735 | void __init | ||
736 | console_map_init(void) | ||
737 | { | ||
738 | int i; | ||
739 | |||
740 | for (i = 0; i < MAX_NR_CONSOLES; i++) | ||
741 | if (vc_cons_allocated(i) && !*vc_cons[i].d->vc_uni_pagedir_loc) | ||
742 | con_set_default_unimap(vc_cons[i].d); | ||
743 | } | ||
744 | |||
745 | EXPORT_SYMBOL(con_copy_unimap); | ||
diff --git a/drivers/tty/vt/cp437.uni b/drivers/tty/vt/cp437.uni new file mode 100644 index 000000000000..bc6163484f62 --- /dev/null +++ b/drivers/tty/vt/cp437.uni | |||
@@ -0,0 +1,291 @@ | |||
1 | # | ||
2 | # Unicode table for IBM Codepage 437. Note that there are many more | ||
3 | # substitutions that could be conceived (for example, thick-line | ||
4 | # graphs probably should be replaced with double-line ones, accented | ||
5 | # Latin characters should replaced with their nonaccented versions, | ||
6 | # and some upper case Greek characters could be replaced by Latin), however, | ||
7 | # I have limited myself to the Unicodes used by the kernel ISO 8859-1, | ||
8 | # DEC VT, and IBM CP 437 tables. | ||
9 | # | ||
10 | # -------------------------------- | ||
11 | # | ||
12 | # Basic IBM dingbats, some of which will never have a purpose clear | ||
13 | # to mankind | ||
14 | # | ||
15 | 0x00 U+0000 | ||
16 | 0x01 U+263a | ||
17 | 0x02 U+263b | ||
18 | 0x03 U+2665 | ||
19 | 0x04 U+2666 U+25c6 | ||
20 | 0x05 U+2663 | ||
21 | 0x06 U+2660 | ||
22 | 0x07 U+2022 | ||
23 | 0x08 U+25d8 | ||
24 | 0x09 U+25cb | ||
25 | 0x0a U+25d9 | ||
26 | 0x0b U+2642 | ||
27 | 0x0c U+2640 | ||
28 | 0x0d U+266a | ||
29 | 0x0e U+266b | ||
30 | 0x0f U+263c U+00a4 | ||
31 | 0x10 U+25b6 U+25ba | ||
32 | 0x11 U+25c0 U+25c4 | ||
33 | 0x12 U+2195 | ||
34 | 0x13 U+203c | ||
35 | 0x14 U+00b6 | ||
36 | 0x15 U+00a7 | ||
37 | 0x16 U+25ac | ||
38 | 0x17 U+21a8 | ||
39 | 0x18 U+2191 | ||
40 | 0x19 U+2193 | ||
41 | 0x1a U+2192 | ||
42 | 0x1b U+2190 | ||
43 | 0x1c U+221f | ||
44 | 0x1d U+2194 | ||
45 | 0x1e U+25b2 | ||
46 | 0x1f U+25bc | ||
47 | # | ||
48 | # The ASCII range is identity-mapped, but some of the characters also | ||
49 | # have to act as substitutes, especially the upper-case characters. | ||
50 | # | ||
51 | 0x20 U+0020 | ||
52 | 0x21 U+0021 | ||
53 | 0x22 U+0022 U+00a8 | ||
54 | 0x23 U+0023 | ||
55 | 0x24 U+0024 | ||
56 | 0x25 U+0025 | ||
57 | 0x26 U+0026 | ||
58 | 0x27 U+0027 U+00b4 | ||
59 | 0x28 U+0028 | ||
60 | 0x29 U+0029 | ||
61 | 0x2a U+002a | ||
62 | 0x2b U+002b | ||
63 | 0x2c U+002c U+00b8 | ||
64 | 0x2d U+002d U+00ad | ||
65 | 0x2e U+002e | ||
66 | 0x2f U+002f | ||
67 | 0x30 U+0030 | ||
68 | 0x31 U+0031 | ||
69 | 0x32 U+0032 | ||
70 | 0x33 U+0033 | ||
71 | 0x34 U+0034 | ||
72 | 0x35 U+0035 | ||
73 | 0x36 U+0036 | ||
74 | 0x37 U+0037 | ||
75 | 0x38 U+0038 | ||
76 | 0x39 U+0039 | ||
77 | 0x3a U+003a | ||
78 | 0x3b U+003b | ||
79 | 0x3c U+003c | ||
80 | 0x3d U+003d | ||
81 | 0x3e U+003e | ||
82 | 0x3f U+003f | ||
83 | 0x40 U+0040 | ||
84 | 0x41 U+0041 U+00c0 U+00c1 U+00c2 U+00c3 | ||
85 | 0x42 U+0042 | ||
86 | 0x43 U+0043 U+00a9 | ||
87 | 0x44 U+0044 U+00d0 | ||
88 | 0x45 U+0045 U+00c8 U+00ca U+00cb | ||
89 | 0x46 U+0046 | ||
90 | 0x47 U+0047 | ||
91 | 0x48 U+0048 | ||
92 | 0x49 U+0049 U+00cc U+00cd U+00ce U+00cf | ||
93 | 0x4a U+004a | ||
94 | 0x4b U+004b U+212a | ||
95 | 0x4c U+004c | ||
96 | 0x4d U+004d | ||
97 | 0x4e U+004e | ||
98 | 0x4f U+004f U+00d2 U+00d3 U+00d4 U+00d5 | ||
99 | 0x50 U+0050 | ||
100 | 0x51 U+0051 | ||
101 | 0x52 U+0052 U+00ae | ||
102 | 0x53 U+0053 | ||
103 | 0x54 U+0054 | ||
104 | 0x55 U+0055 U+00d9 U+00da U+00db | ||
105 | 0x56 U+0056 | ||
106 | 0x57 U+0057 | ||
107 | 0x58 U+0058 | ||
108 | 0x59 U+0059 U+00dd | ||
109 | 0x5a U+005a | ||
110 | 0x5b U+005b | ||
111 | 0x5c U+005c | ||
112 | 0x5d U+005d | ||
113 | 0x5e U+005e | ||
114 | 0x5f U+005f U+23bd U+f804 | ||
115 | 0x60 U+0060 | ||
116 | 0x61 U+0061 U+00e3 | ||
117 | 0x62 U+0062 | ||
118 | 0x63 U+0063 | ||
119 | 0x64 U+0064 | ||
120 | 0x65 U+0065 | ||
121 | 0x66 U+0066 | ||
122 | 0x67 U+0067 | ||
123 | 0x68 U+0068 | ||
124 | 0x69 U+0069 | ||
125 | 0x6a U+006a | ||
126 | 0x6b U+006b | ||
127 | 0x6c U+006c | ||
128 | 0x6d U+006d | ||
129 | 0x6e U+006e | ||
130 | 0x6f U+006f U+00f5 | ||
131 | 0x70 U+0070 | ||
132 | 0x71 U+0071 | ||
133 | 0x72 U+0072 | ||
134 | 0x73 U+0073 | ||
135 | 0x74 U+0074 | ||
136 | 0x75 U+0075 | ||
137 | 0x76 U+0076 | ||
138 | 0x77 U+0077 | ||
139 | 0x78 U+0078 U+00d7 | ||
140 | 0x79 U+0079 U+00fd | ||
141 | 0x7a U+007a | ||
142 | 0x7b U+007b | ||
143 | 0x7c U+007c U+00a6 | ||
144 | 0x7d U+007d | ||
145 | 0x7e U+007e | ||
146 | # | ||
147 | # Okay, what on Earth is this one supposed to be used for? | ||
148 | # | ||
149 | 0x7f U+2302 | ||
150 | # | ||
151 | # Non-English characters, mostly lower case letters... | ||
152 | # | ||
153 | 0x80 U+00c7 | ||
154 | 0x81 U+00fc | ||
155 | 0x82 U+00e9 | ||
156 | 0x83 U+00e2 | ||
157 | 0x84 U+00e4 | ||
158 | 0x85 U+00e0 | ||
159 | 0x86 U+00e5 | ||
160 | 0x87 U+00e7 | ||
161 | 0x88 U+00ea | ||
162 | 0x89 U+00eb | ||
163 | 0x8a U+00e8 | ||
164 | 0x8b U+00ef | ||
165 | 0x8c U+00ee | ||
166 | 0x8d U+00ec | ||
167 | 0x8e U+00c4 | ||
168 | 0x8f U+00c5 U+212b | ||
169 | 0x90 U+00c9 | ||
170 | 0x91 U+00e6 | ||
171 | 0x92 U+00c6 | ||
172 | 0x93 U+00f4 | ||
173 | 0x94 U+00f6 | ||
174 | 0x95 U+00f2 | ||
175 | 0x96 U+00fb | ||
176 | 0x97 U+00f9 | ||
177 | 0x98 U+00ff | ||
178 | 0x99 U+00d6 | ||
179 | 0x9a U+00dc | ||
180 | 0x9b U+00a2 | ||
181 | 0x9c U+00a3 | ||
182 | 0x9d U+00a5 | ||
183 | 0x9e U+20a7 | ||
184 | 0x9f U+0192 | ||
185 | 0xa0 U+00e1 | ||
186 | 0xa1 U+00ed | ||
187 | 0xa2 U+00f3 | ||
188 | 0xa3 U+00fa | ||
189 | 0xa4 U+00f1 | ||
190 | 0xa5 U+00d1 | ||
191 | 0xa6 U+00aa | ||
192 | 0xa7 U+00ba | ||
193 | 0xa8 U+00bf | ||
194 | 0xa9 U+2310 | ||
195 | 0xaa U+00ac | ||
196 | 0xab U+00bd | ||
197 | 0xac U+00bc | ||
198 | 0xad U+00a1 | ||
199 | 0xae U+00ab | ||
200 | 0xaf U+00bb | ||
201 | # | ||
202 | # Block graphics | ||
203 | # | ||
204 | 0xb0 U+2591 | ||
205 | 0xb1 U+2592 | ||
206 | 0xb2 U+2593 | ||
207 | 0xb3 U+2502 | ||
208 | 0xb4 U+2524 | ||
209 | 0xb5 U+2561 | ||
210 | 0xb6 U+2562 | ||
211 | 0xb7 U+2556 | ||
212 | 0xb8 U+2555 | ||
213 | 0xb9 U+2563 | ||
214 | 0xba U+2551 | ||
215 | 0xbb U+2557 | ||
216 | 0xbc U+255d | ||
217 | 0xbd U+255c | ||
218 | 0xbe U+255b | ||
219 | 0xbf U+2510 | ||
220 | 0xc0 U+2514 | ||
221 | 0xc1 U+2534 | ||
222 | 0xc2 U+252c | ||
223 | 0xc3 U+251c | ||
224 | 0xc4 U+2500 | ||
225 | 0xc5 U+253c | ||
226 | 0xc6 U+255e | ||
227 | 0xc7 U+255f | ||
228 | 0xc8 U+255a | ||
229 | 0xc9 U+2554 | ||
230 | 0xca U+2569 | ||
231 | 0xcb U+2566 | ||
232 | 0xcc U+2560 | ||
233 | 0xcd U+2550 | ||
234 | 0xce U+256c | ||
235 | 0xcf U+2567 | ||
236 | 0xd0 U+2568 | ||
237 | 0xd1 U+2564 | ||
238 | 0xd2 U+2565 | ||
239 | 0xd3 U+2559 | ||
240 | 0xd4 U+2558 | ||
241 | 0xd5 U+2552 | ||
242 | 0xd6 U+2553 | ||
243 | 0xd7 U+256b | ||
244 | 0xd8 U+256a | ||
245 | 0xd9 U+2518 | ||
246 | 0xda U+250c | ||
247 | 0xdb U+2588 | ||
248 | 0xdc U+2584 | ||
249 | 0xdd U+258c | ||
250 | 0xde U+2590 | ||
251 | 0xdf U+2580 | ||
252 | # | ||
253 | # Greek letters and mathematical symbols | ||
254 | # | ||
255 | 0xe0 U+03b1 | ||
256 | 0xe1 U+03b2 U+00df | ||
257 | 0xe2 U+0393 | ||
258 | 0xe3 U+03c0 | ||
259 | 0xe4 U+03a3 | ||
260 | 0xe5 U+03c3 | ||
261 | 0xe6 U+00b5 U+03bc | ||
262 | 0xe7 U+03c4 | ||
263 | 0xe8 U+03a6 U+00d8 | ||
264 | 0xe9 U+0398 | ||
265 | 0xea U+03a9 U+2126 | ||
266 | 0xeb U+03b4 U+00f0 | ||
267 | 0xec U+221e | ||
268 | 0xed U+03c6 U+00f8 | ||
269 | 0xee U+03b5 U+2208 | ||
270 | 0xef U+2229 | ||
271 | 0xf0 U+2261 | ||
272 | 0xf1 U+00b1 | ||
273 | 0xf2 U+2265 | ||
274 | 0xf3 U+2264 | ||
275 | 0xf4 U+2320 | ||
276 | 0xf5 U+2321 | ||
277 | 0xf6 U+00f7 | ||
278 | 0xf7 U+2248 | ||
279 | 0xf8 U+00b0 | ||
280 | 0xf9 U+2219 | ||
281 | 0xfa U+00b7 | ||
282 | 0xfb U+221a | ||
283 | 0xfc U+207f | ||
284 | 0xfd U+00b2 | ||
285 | # | ||
286 | # Square bullet, non-spacing blank | ||
287 | # Mapping U+fffd to the square bullet means it is the substitution | ||
288 | # character | ||
289 | # | ||
290 | 0xfe U+25a0 U+fffd | ||
291 | 0xff U+00a0 | ||
diff --git a/drivers/tty/vt/defkeymap.c_shipped b/drivers/tty/vt/defkeymap.c_shipped new file mode 100644 index 000000000000..d2208dfe3f67 --- /dev/null +++ b/drivers/tty/vt/defkeymap.c_shipped | |||
@@ -0,0 +1,262 @@ | |||
1 | /* Do not edit this file! It was automatically generated by */ | ||
2 | /* loadkeys --mktable defkeymap.map > defkeymap.c */ | ||
3 | |||
4 | #include <linux/types.h> | ||
5 | #include <linux/keyboard.h> | ||
6 | #include <linux/kd.h> | ||
7 | |||
8 | u_short plain_map[NR_KEYS] = { | ||
9 | 0xf200, 0xf01b, 0xf031, 0xf032, 0xf033, 0xf034, 0xf035, 0xf036, | ||
10 | 0xf037, 0xf038, 0xf039, 0xf030, 0xf02d, 0xf03d, 0xf07f, 0xf009, | ||
11 | 0xfb71, 0xfb77, 0xfb65, 0xfb72, 0xfb74, 0xfb79, 0xfb75, 0xfb69, | ||
12 | 0xfb6f, 0xfb70, 0xf05b, 0xf05d, 0xf201, 0xf702, 0xfb61, 0xfb73, | ||
13 | 0xfb64, 0xfb66, 0xfb67, 0xfb68, 0xfb6a, 0xfb6b, 0xfb6c, 0xf03b, | ||
14 | 0xf027, 0xf060, 0xf700, 0xf05c, 0xfb7a, 0xfb78, 0xfb63, 0xfb76, | ||
15 | 0xfb62, 0xfb6e, 0xfb6d, 0xf02c, 0xf02e, 0xf02f, 0xf700, 0xf30c, | ||
16 | 0xf703, 0xf020, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104, | ||
17 | 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf208, 0xf209, 0xf307, | ||
18 | 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301, | ||
19 | 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf03c, 0xf10a, | ||
20 | 0xf10b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
21 | 0xf30e, 0xf702, 0xf30d, 0xf01c, 0xf701, 0xf205, 0xf114, 0xf603, | ||
22 | 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116, | ||
23 | 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d, | ||
24 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
25 | }; | ||
26 | |||
27 | u_short shift_map[NR_KEYS] = { | ||
28 | 0xf200, 0xf01b, 0xf021, 0xf040, 0xf023, 0xf024, 0xf025, 0xf05e, | ||
29 | 0xf026, 0xf02a, 0xf028, 0xf029, 0xf05f, 0xf02b, 0xf07f, 0xf009, | ||
30 | 0xfb51, 0xfb57, 0xfb45, 0xfb52, 0xfb54, 0xfb59, 0xfb55, 0xfb49, | ||
31 | 0xfb4f, 0xfb50, 0xf07b, 0xf07d, 0xf201, 0xf702, 0xfb41, 0xfb53, | ||
32 | 0xfb44, 0xfb46, 0xfb47, 0xfb48, 0xfb4a, 0xfb4b, 0xfb4c, 0xf03a, | ||
33 | 0xf022, 0xf07e, 0xf700, 0xf07c, 0xfb5a, 0xfb58, 0xfb43, 0xfb56, | ||
34 | 0xfb42, 0xfb4e, 0xfb4d, 0xf03c, 0xf03e, 0xf03f, 0xf700, 0xf30c, | ||
35 | 0xf703, 0xf020, 0xf207, 0xf10a, 0xf10b, 0xf10c, 0xf10d, 0xf10e, | ||
36 | 0xf10f, 0xf110, 0xf111, 0xf112, 0xf113, 0xf213, 0xf203, 0xf307, | ||
37 | 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301, | ||
38 | 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf03e, 0xf10a, | ||
39 | 0xf10b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
40 | 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603, | ||
41 | 0xf20b, 0xf601, 0xf602, 0xf117, 0xf600, 0xf20a, 0xf115, 0xf116, | ||
42 | 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d, | ||
43 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
44 | }; | ||
45 | |||
46 | u_short altgr_map[NR_KEYS] = { | ||
47 | 0xf200, 0xf200, 0xf200, 0xf040, 0xf200, 0xf024, 0xf200, 0xf200, | ||
48 | 0xf07b, 0xf05b, 0xf05d, 0xf07d, 0xf05c, 0xf200, 0xf200, 0xf200, | ||
49 | 0xfb71, 0xfb77, 0xf918, 0xfb72, 0xfb74, 0xfb79, 0xfb75, 0xfb69, | ||
50 | 0xfb6f, 0xfb70, 0xf200, 0xf07e, 0xf201, 0xf702, 0xf914, 0xfb73, | ||
51 | 0xf917, 0xf919, 0xfb67, 0xfb68, 0xfb6a, 0xfb6b, 0xfb6c, 0xf200, | ||
52 | 0xf200, 0xf200, 0xf700, 0xf200, 0xfb7a, 0xfb78, 0xf916, 0xfb76, | ||
53 | 0xf915, 0xfb6e, 0xfb6d, 0xf200, 0xf200, 0xf200, 0xf700, 0xf30c, | ||
54 | 0xf703, 0xf200, 0xf207, 0xf50c, 0xf50d, 0xf50e, 0xf50f, 0xf510, | ||
55 | 0xf511, 0xf512, 0xf513, 0xf514, 0xf515, 0xf208, 0xf202, 0xf911, | ||
56 | 0xf912, 0xf913, 0xf30b, 0xf90e, 0xf90f, 0xf910, 0xf30a, 0xf90b, | ||
57 | 0xf90c, 0xf90d, 0xf90a, 0xf310, 0xf206, 0xf200, 0xf07c, 0xf516, | ||
58 | 0xf517, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
59 | 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603, | ||
60 | 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116, | ||
61 | 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d, | ||
62 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
63 | }; | ||
64 | |||
65 | u_short ctrl_map[NR_KEYS] = { | ||
66 | 0xf200, 0xf200, 0xf200, 0xf000, 0xf01b, 0xf01c, 0xf01d, 0xf01e, | ||
67 | 0xf01f, 0xf07f, 0xf200, 0xf200, 0xf01f, 0xf200, 0xf008, 0xf200, | ||
68 | 0xf011, 0xf017, 0xf005, 0xf012, 0xf014, 0xf019, 0xf015, 0xf009, | ||
69 | 0xf00f, 0xf010, 0xf01b, 0xf01d, 0xf201, 0xf702, 0xf001, 0xf013, | ||
70 | 0xf004, 0xf006, 0xf007, 0xf008, 0xf00a, 0xf00b, 0xf00c, 0xf200, | ||
71 | 0xf007, 0xf000, 0xf700, 0xf01c, 0xf01a, 0xf018, 0xf003, 0xf016, | ||
72 | 0xf002, 0xf00e, 0xf00d, 0xf200, 0xf20e, 0xf07f, 0xf700, 0xf30c, | ||
73 | 0xf703, 0xf000, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104, | ||
74 | 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf208, 0xf204, 0xf307, | ||
75 | 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301, | ||
76 | 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf200, 0xf10a, | ||
77 | 0xf10b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
78 | 0xf30e, 0xf702, 0xf30d, 0xf01c, 0xf701, 0xf205, 0xf114, 0xf603, | ||
79 | 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116, | ||
80 | 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d, | ||
81 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
82 | }; | ||
83 | |||
84 | u_short shift_ctrl_map[NR_KEYS] = { | ||
85 | 0xf200, 0xf200, 0xf200, 0xf000, 0xf200, 0xf200, 0xf200, 0xf200, | ||
86 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf01f, 0xf200, 0xf200, 0xf200, | ||
87 | 0xf011, 0xf017, 0xf005, 0xf012, 0xf014, 0xf019, 0xf015, 0xf009, | ||
88 | 0xf00f, 0xf010, 0xf200, 0xf200, 0xf201, 0xf702, 0xf001, 0xf013, | ||
89 | 0xf004, 0xf006, 0xf007, 0xf008, 0xf00a, 0xf00b, 0xf00c, 0xf200, | ||
90 | 0xf200, 0xf200, 0xf700, 0xf200, 0xf01a, 0xf018, 0xf003, 0xf016, | ||
91 | 0xf002, 0xf00e, 0xf00d, 0xf200, 0xf200, 0xf200, 0xf700, 0xf30c, | ||
92 | 0xf703, 0xf200, 0xf207, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
93 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf208, 0xf200, 0xf307, | ||
94 | 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301, | ||
95 | 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf200, 0xf200, | ||
96 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
97 | 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603, | ||
98 | 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116, | ||
99 | 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d, | ||
100 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
101 | }; | ||
102 | |||
103 | u_short alt_map[NR_KEYS] = { | ||
104 | 0xf200, 0xf81b, 0xf831, 0xf832, 0xf833, 0xf834, 0xf835, 0xf836, | ||
105 | 0xf837, 0xf838, 0xf839, 0xf830, 0xf82d, 0xf83d, 0xf87f, 0xf809, | ||
106 | 0xf871, 0xf877, 0xf865, 0xf872, 0xf874, 0xf879, 0xf875, 0xf869, | ||
107 | 0xf86f, 0xf870, 0xf85b, 0xf85d, 0xf80d, 0xf702, 0xf861, 0xf873, | ||
108 | 0xf864, 0xf866, 0xf867, 0xf868, 0xf86a, 0xf86b, 0xf86c, 0xf83b, | ||
109 | 0xf827, 0xf860, 0xf700, 0xf85c, 0xf87a, 0xf878, 0xf863, 0xf876, | ||
110 | 0xf862, 0xf86e, 0xf86d, 0xf82c, 0xf82e, 0xf82f, 0xf700, 0xf30c, | ||
111 | 0xf703, 0xf820, 0xf207, 0xf500, 0xf501, 0xf502, 0xf503, 0xf504, | ||
112 | 0xf505, 0xf506, 0xf507, 0xf508, 0xf509, 0xf208, 0xf209, 0xf907, | ||
113 | 0xf908, 0xf909, 0xf30b, 0xf904, 0xf905, 0xf906, 0xf30a, 0xf901, | ||
114 | 0xf902, 0xf903, 0xf900, 0xf310, 0xf206, 0xf200, 0xf83c, 0xf50a, | ||
115 | 0xf50b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
116 | 0xf30e, 0xf702, 0xf30d, 0xf01c, 0xf701, 0xf205, 0xf114, 0xf603, | ||
117 | 0xf118, 0xf210, 0xf211, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116, | ||
118 | 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d, | ||
119 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
120 | }; | ||
121 | |||
122 | u_short ctrl_alt_map[NR_KEYS] = { | ||
123 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
124 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
125 | 0xf811, 0xf817, 0xf805, 0xf812, 0xf814, 0xf819, 0xf815, 0xf809, | ||
126 | 0xf80f, 0xf810, 0xf200, 0xf200, 0xf201, 0xf702, 0xf801, 0xf813, | ||
127 | 0xf804, 0xf806, 0xf807, 0xf808, 0xf80a, 0xf80b, 0xf80c, 0xf200, | ||
128 | 0xf200, 0xf200, 0xf700, 0xf200, 0xf81a, 0xf818, 0xf803, 0xf816, | ||
129 | 0xf802, 0xf80e, 0xf80d, 0xf200, 0xf200, 0xf200, 0xf700, 0xf30c, | ||
130 | 0xf703, 0xf200, 0xf207, 0xf500, 0xf501, 0xf502, 0xf503, 0xf504, | ||
131 | 0xf505, 0xf506, 0xf507, 0xf508, 0xf509, 0xf208, 0xf200, 0xf307, | ||
132 | 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301, | ||
133 | 0xf302, 0xf303, 0xf300, 0xf20c, 0xf206, 0xf200, 0xf200, 0xf50a, | ||
134 | 0xf50b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
135 | 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603, | ||
136 | 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf20c, | ||
137 | 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d, | ||
138 | 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, | ||
139 | }; | ||
140 | |||
141 | ushort *key_maps[MAX_NR_KEYMAPS] = { | ||
142 | plain_map, shift_map, altgr_map, NULL, | ||
143 | ctrl_map, shift_ctrl_map, NULL, NULL, | ||
144 | alt_map, NULL, NULL, NULL, | ||
145 | ctrl_alt_map, NULL | ||
146 | }; | ||
147 | |||
148 | unsigned int keymap_count = 7; | ||
149 | |||
150 | /* | ||
151 | * Philosophy: most people do not define more strings, but they who do | ||
152 | * often want quite a lot of string space. So, we statically allocate | ||
153 | * the default and allocate dynamically in chunks of 512 bytes. | ||
154 | */ | ||
155 | |||
156 | char func_buf[] = { | ||
157 | '\033', '[', '[', 'A', 0, | ||
158 | '\033', '[', '[', 'B', 0, | ||
159 | '\033', '[', '[', 'C', 0, | ||
160 | '\033', '[', '[', 'D', 0, | ||
161 | '\033', '[', '[', 'E', 0, | ||
162 | '\033', '[', '1', '7', '~', 0, | ||
163 | '\033', '[', '1', '8', '~', 0, | ||
164 | '\033', '[', '1', '9', '~', 0, | ||
165 | '\033', '[', '2', '0', '~', 0, | ||
166 | '\033', '[', '2', '1', '~', 0, | ||
167 | '\033', '[', '2', '3', '~', 0, | ||
168 | '\033', '[', '2', '4', '~', 0, | ||
169 | '\033', '[', '2', '5', '~', 0, | ||
170 | '\033', '[', '2', '6', '~', 0, | ||
171 | '\033', '[', '2', '8', '~', 0, | ||
172 | '\033', '[', '2', '9', '~', 0, | ||
173 | '\033', '[', '3', '1', '~', 0, | ||
174 | '\033', '[', '3', '2', '~', 0, | ||
175 | '\033', '[', '3', '3', '~', 0, | ||
176 | '\033', '[', '3', '4', '~', 0, | ||
177 | '\033', '[', '1', '~', 0, | ||
178 | '\033', '[', '2', '~', 0, | ||
179 | '\033', '[', '3', '~', 0, | ||
180 | '\033', '[', '4', '~', 0, | ||
181 | '\033', '[', '5', '~', 0, | ||
182 | '\033', '[', '6', '~', 0, | ||
183 | '\033', '[', 'M', 0, | ||
184 | '\033', '[', 'P', 0, | ||
185 | }; | ||
186 | |||
187 | char *funcbufptr = func_buf; | ||
188 | int funcbufsize = sizeof(func_buf); | ||
189 | int funcbufleft = 0; /* space left */ | ||
190 | |||
191 | char *func_table[MAX_NR_FUNC] = { | ||
192 | func_buf + 0, | ||
193 | func_buf + 5, | ||
194 | func_buf + 10, | ||
195 | func_buf + 15, | ||
196 | func_buf + 20, | ||
197 | func_buf + 25, | ||
198 | func_buf + 31, | ||
199 | func_buf + 37, | ||
200 | func_buf + 43, | ||
201 | func_buf + 49, | ||
202 | func_buf + 55, | ||
203 | func_buf + 61, | ||
204 | func_buf + 67, | ||
205 | func_buf + 73, | ||
206 | func_buf + 79, | ||
207 | func_buf + 85, | ||
208 | func_buf + 91, | ||
209 | func_buf + 97, | ||
210 | func_buf + 103, | ||
211 | func_buf + 109, | ||
212 | func_buf + 115, | ||
213 | func_buf + 120, | ||
214 | func_buf + 125, | ||
215 | func_buf + 130, | ||
216 | func_buf + 135, | ||
217 | func_buf + 140, | ||
218 | func_buf + 145, | ||
219 | NULL, | ||
220 | NULL, | ||
221 | func_buf + 149, | ||
222 | NULL, | ||
223 | }; | ||
224 | |||
225 | struct kbdiacruc accent_table[MAX_DIACR] = { | ||
226 | {'`', 'A', 0300}, {'`', 'a', 0340}, | ||
227 | {'\'', 'A', 0301}, {'\'', 'a', 0341}, | ||
228 | {'^', 'A', 0302}, {'^', 'a', 0342}, | ||
229 | {'~', 'A', 0303}, {'~', 'a', 0343}, | ||
230 | {'"', 'A', 0304}, {'"', 'a', 0344}, | ||
231 | {'O', 'A', 0305}, {'o', 'a', 0345}, | ||
232 | {'0', 'A', 0305}, {'0', 'a', 0345}, | ||
233 | {'A', 'A', 0305}, {'a', 'a', 0345}, | ||
234 | {'A', 'E', 0306}, {'a', 'e', 0346}, | ||
235 | {',', 'C', 0307}, {',', 'c', 0347}, | ||
236 | {'`', 'E', 0310}, {'`', 'e', 0350}, | ||
237 | {'\'', 'E', 0311}, {'\'', 'e', 0351}, | ||
238 | {'^', 'E', 0312}, {'^', 'e', 0352}, | ||
239 | {'"', 'E', 0313}, {'"', 'e', 0353}, | ||
240 | {'`', 'I', 0314}, {'`', 'i', 0354}, | ||
241 | {'\'', 'I', 0315}, {'\'', 'i', 0355}, | ||
242 | {'^', 'I', 0316}, {'^', 'i', 0356}, | ||
243 | {'"', 'I', 0317}, {'"', 'i', 0357}, | ||
244 | {'-', 'D', 0320}, {'-', 'd', 0360}, | ||
245 | {'~', 'N', 0321}, {'~', 'n', 0361}, | ||
246 | {'`', 'O', 0322}, {'`', 'o', 0362}, | ||
247 | {'\'', 'O', 0323}, {'\'', 'o', 0363}, | ||
248 | {'^', 'O', 0324}, {'^', 'o', 0364}, | ||
249 | {'~', 'O', 0325}, {'~', 'o', 0365}, | ||
250 | {'"', 'O', 0326}, {'"', 'o', 0366}, | ||
251 | {'/', 'O', 0330}, {'/', 'o', 0370}, | ||
252 | {'`', 'U', 0331}, {'`', 'u', 0371}, | ||
253 | {'\'', 'U', 0332}, {'\'', 'u', 0372}, | ||
254 | {'^', 'U', 0333}, {'^', 'u', 0373}, | ||
255 | {'"', 'U', 0334}, {'"', 'u', 0374}, | ||
256 | {'\'', 'Y', 0335}, {'\'', 'y', 0375}, | ||
257 | {'T', 'H', 0336}, {'t', 'h', 0376}, | ||
258 | {'s', 's', 0337}, {'"', 'y', 0377}, | ||
259 | {'s', 'z', 0337}, {'i', 'j', 0377}, | ||
260 | }; | ||
261 | |||
262 | unsigned int accent_table_size = 68; | ||
diff --git a/drivers/tty/vt/defkeymap.map b/drivers/tty/vt/defkeymap.map new file mode 100644 index 000000000000..50b30cace261 --- /dev/null +++ b/drivers/tty/vt/defkeymap.map | |||
@@ -0,0 +1,357 @@ | |||
1 | # Default kernel keymap. This uses 7 modifier combinations. | ||
2 | keymaps 0-2,4-5,8,12 | ||
3 | # Change the above line into | ||
4 | # keymaps 0-2,4-6,8,12 | ||
5 | # in case you want the entries | ||
6 | # altgr control keycode 83 = Boot | ||
7 | # altgr control keycode 111 = Boot | ||
8 | # below. | ||
9 | # | ||
10 | # In fact AltGr is used very little, and one more keymap can | ||
11 | # be saved by mapping AltGr to Alt (and adapting a few entries): | ||
12 | # keycode 100 = Alt | ||
13 | # | ||
14 | keycode 1 = Escape Escape | ||
15 | alt keycode 1 = Meta_Escape | ||
16 | keycode 2 = one exclam | ||
17 | alt keycode 2 = Meta_one | ||
18 | keycode 3 = two at at | ||
19 | control keycode 3 = nul | ||
20 | shift control keycode 3 = nul | ||
21 | alt keycode 3 = Meta_two | ||
22 | keycode 4 = three numbersign | ||
23 | control keycode 4 = Escape | ||
24 | alt keycode 4 = Meta_three | ||
25 | keycode 5 = four dollar dollar | ||
26 | control keycode 5 = Control_backslash | ||
27 | alt keycode 5 = Meta_four | ||
28 | keycode 6 = five percent | ||
29 | control keycode 6 = Control_bracketright | ||
30 | alt keycode 6 = Meta_five | ||
31 | keycode 7 = six asciicircum | ||
32 | control keycode 7 = Control_asciicircum | ||
33 | alt keycode 7 = Meta_six | ||
34 | keycode 8 = seven ampersand braceleft | ||
35 | control keycode 8 = Control_underscore | ||
36 | alt keycode 8 = Meta_seven | ||
37 | keycode 9 = eight asterisk bracketleft | ||
38 | control keycode 9 = Delete | ||
39 | alt keycode 9 = Meta_eight | ||
40 | keycode 10 = nine parenleft bracketright | ||
41 | alt keycode 10 = Meta_nine | ||
42 | keycode 11 = zero parenright braceright | ||
43 | alt keycode 11 = Meta_zero | ||
44 | keycode 12 = minus underscore backslash | ||
45 | control keycode 12 = Control_underscore | ||
46 | shift control keycode 12 = Control_underscore | ||
47 | alt keycode 12 = Meta_minus | ||
48 | keycode 13 = equal plus | ||
49 | alt keycode 13 = Meta_equal | ||
50 | keycode 14 = Delete Delete | ||
51 | control keycode 14 = BackSpace | ||
52 | alt keycode 14 = Meta_Delete | ||
53 | keycode 15 = Tab Tab | ||
54 | alt keycode 15 = Meta_Tab | ||
55 | keycode 16 = q | ||
56 | keycode 17 = w | ||
57 | keycode 18 = e | ||
58 | altgr keycode 18 = Hex_E | ||
59 | keycode 19 = r | ||
60 | keycode 20 = t | ||
61 | keycode 21 = y | ||
62 | keycode 22 = u | ||
63 | keycode 23 = i | ||
64 | keycode 24 = o | ||
65 | keycode 25 = p | ||
66 | keycode 26 = bracketleft braceleft | ||
67 | control keycode 26 = Escape | ||
68 | alt keycode 26 = Meta_bracketleft | ||
69 | keycode 27 = bracketright braceright asciitilde | ||
70 | control keycode 27 = Control_bracketright | ||
71 | alt keycode 27 = Meta_bracketright | ||
72 | keycode 28 = Return | ||
73 | alt keycode 28 = Meta_Control_m | ||
74 | keycode 29 = Control | ||
75 | keycode 30 = a | ||
76 | altgr keycode 30 = Hex_A | ||
77 | keycode 31 = s | ||
78 | keycode 32 = d | ||
79 | altgr keycode 32 = Hex_D | ||
80 | keycode 33 = f | ||
81 | altgr keycode 33 = Hex_F | ||
82 | keycode 34 = g | ||
83 | keycode 35 = h | ||
84 | keycode 36 = j | ||
85 | keycode 37 = k | ||
86 | keycode 38 = l | ||
87 | keycode 39 = semicolon colon | ||
88 | alt keycode 39 = Meta_semicolon | ||
89 | keycode 40 = apostrophe quotedbl | ||
90 | control keycode 40 = Control_g | ||
91 | alt keycode 40 = Meta_apostrophe | ||
92 | keycode 41 = grave asciitilde | ||
93 | control keycode 41 = nul | ||
94 | alt keycode 41 = Meta_grave | ||
95 | keycode 42 = Shift | ||
96 | keycode 43 = backslash bar | ||
97 | control keycode 43 = Control_backslash | ||
98 | alt keycode 43 = Meta_backslash | ||
99 | keycode 44 = z | ||
100 | keycode 45 = x | ||
101 | keycode 46 = c | ||
102 | altgr keycode 46 = Hex_C | ||
103 | keycode 47 = v | ||
104 | keycode 48 = b | ||
105 | altgr keycode 48 = Hex_B | ||
106 | keycode 49 = n | ||
107 | keycode 50 = m | ||
108 | keycode 51 = comma less | ||
109 | alt keycode 51 = Meta_comma | ||
110 | keycode 52 = period greater | ||
111 | control keycode 52 = Compose | ||
112 | alt keycode 52 = Meta_period | ||
113 | keycode 53 = slash question | ||
114 | control keycode 53 = Delete | ||
115 | alt keycode 53 = Meta_slash | ||
116 | keycode 54 = Shift | ||
117 | keycode 55 = KP_Multiply | ||
118 | keycode 56 = Alt | ||
119 | keycode 57 = space space | ||
120 | control keycode 57 = nul | ||
121 | alt keycode 57 = Meta_space | ||
122 | keycode 58 = Caps_Lock | ||
123 | keycode 59 = F1 F11 Console_13 | ||
124 | control keycode 59 = F1 | ||
125 | alt keycode 59 = Console_1 | ||
126 | control alt keycode 59 = Console_1 | ||
127 | keycode 60 = F2 F12 Console_14 | ||
128 | control keycode 60 = F2 | ||
129 | alt keycode 60 = Console_2 | ||
130 | control alt keycode 60 = Console_2 | ||
131 | keycode 61 = F3 F13 Console_15 | ||
132 | control keycode 61 = F3 | ||
133 | alt keycode 61 = Console_3 | ||
134 | control alt keycode 61 = Console_3 | ||
135 | keycode 62 = F4 F14 Console_16 | ||
136 | control keycode 62 = F4 | ||
137 | alt keycode 62 = Console_4 | ||
138 | control alt keycode 62 = Console_4 | ||
139 | keycode 63 = F5 F15 Console_17 | ||
140 | control keycode 63 = F5 | ||
141 | alt keycode 63 = Console_5 | ||
142 | control alt keycode 63 = Console_5 | ||
143 | keycode 64 = F6 F16 Console_18 | ||
144 | control keycode 64 = F6 | ||
145 | alt keycode 64 = Console_6 | ||
146 | control alt keycode 64 = Console_6 | ||
147 | keycode 65 = F7 F17 Console_19 | ||
148 | control keycode 65 = F7 | ||
149 | alt keycode 65 = Console_7 | ||
150 | control alt keycode 65 = Console_7 | ||
151 | keycode 66 = F8 F18 Console_20 | ||
152 | control keycode 66 = F8 | ||
153 | alt keycode 66 = Console_8 | ||
154 | control alt keycode 66 = Console_8 | ||
155 | keycode 67 = F9 F19 Console_21 | ||
156 | control keycode 67 = F9 | ||
157 | alt keycode 67 = Console_9 | ||
158 | control alt keycode 67 = Console_9 | ||
159 | keycode 68 = F10 F20 Console_22 | ||
160 | control keycode 68 = F10 | ||
161 | alt keycode 68 = Console_10 | ||
162 | control alt keycode 68 = Console_10 | ||
163 | keycode 69 = Num_Lock | ||
164 | shift keycode 69 = Bare_Num_Lock | ||
165 | keycode 70 = Scroll_Lock Show_Memory Show_Registers | ||
166 | control keycode 70 = Show_State | ||
167 | alt keycode 70 = Scroll_Lock | ||
168 | keycode 71 = KP_7 | ||
169 | alt keycode 71 = Ascii_7 | ||
170 | altgr keycode 71 = Hex_7 | ||
171 | keycode 72 = KP_8 | ||
172 | alt keycode 72 = Ascii_8 | ||
173 | altgr keycode 72 = Hex_8 | ||
174 | keycode 73 = KP_9 | ||
175 | alt keycode 73 = Ascii_9 | ||
176 | altgr keycode 73 = Hex_9 | ||
177 | keycode 74 = KP_Subtract | ||
178 | keycode 75 = KP_4 | ||
179 | alt keycode 75 = Ascii_4 | ||
180 | altgr keycode 75 = Hex_4 | ||
181 | keycode 76 = KP_5 | ||
182 | alt keycode 76 = Ascii_5 | ||
183 | altgr keycode 76 = Hex_5 | ||
184 | keycode 77 = KP_6 | ||
185 | alt keycode 77 = Ascii_6 | ||
186 | altgr keycode 77 = Hex_6 | ||
187 | keycode 78 = KP_Add | ||
188 | keycode 79 = KP_1 | ||
189 | alt keycode 79 = Ascii_1 | ||
190 | altgr keycode 79 = Hex_1 | ||
191 | keycode 80 = KP_2 | ||
192 | alt keycode 80 = Ascii_2 | ||
193 | altgr keycode 80 = Hex_2 | ||
194 | keycode 81 = KP_3 | ||
195 | alt keycode 81 = Ascii_3 | ||
196 | altgr keycode 81 = Hex_3 | ||
197 | keycode 82 = KP_0 | ||
198 | alt keycode 82 = Ascii_0 | ||
199 | altgr keycode 82 = Hex_0 | ||
200 | keycode 83 = KP_Period | ||
201 | # altgr control keycode 83 = Boot | ||
202 | control alt keycode 83 = Boot | ||
203 | keycode 84 = Last_Console | ||
204 | keycode 85 = | ||
205 | keycode 86 = less greater bar | ||
206 | alt keycode 86 = Meta_less | ||
207 | keycode 87 = F11 F11 Console_23 | ||
208 | control keycode 87 = F11 | ||
209 | alt keycode 87 = Console_11 | ||
210 | control alt keycode 87 = Console_11 | ||
211 | keycode 88 = F12 F12 Console_24 | ||
212 | control keycode 88 = F12 | ||
213 | alt keycode 88 = Console_12 | ||
214 | control alt keycode 88 = Console_12 | ||
215 | keycode 89 = | ||
216 | keycode 90 = | ||
217 | keycode 91 = | ||
218 | keycode 92 = | ||
219 | keycode 93 = | ||
220 | keycode 94 = | ||
221 | keycode 95 = | ||
222 | keycode 96 = KP_Enter | ||
223 | keycode 97 = Control | ||
224 | keycode 98 = KP_Divide | ||
225 | keycode 99 = Control_backslash | ||
226 | control keycode 99 = Control_backslash | ||
227 | alt keycode 99 = Control_backslash | ||
228 | keycode 100 = AltGr | ||
229 | keycode 101 = Break | ||
230 | keycode 102 = Find | ||
231 | keycode 103 = Up | ||
232 | keycode 104 = Prior | ||
233 | shift keycode 104 = Scroll_Backward | ||
234 | keycode 105 = Left | ||
235 | alt keycode 105 = Decr_Console | ||
236 | keycode 106 = Right | ||
237 | alt keycode 106 = Incr_Console | ||
238 | keycode 107 = Select | ||
239 | keycode 108 = Down | ||
240 | keycode 109 = Next | ||
241 | shift keycode 109 = Scroll_Forward | ||
242 | keycode 110 = Insert | ||
243 | keycode 111 = Remove | ||
244 | # altgr control keycode 111 = Boot | ||
245 | control alt keycode 111 = Boot | ||
246 | keycode 112 = Macro | ||
247 | keycode 113 = F13 | ||
248 | keycode 114 = F14 | ||
249 | keycode 115 = Help | ||
250 | keycode 116 = Do | ||
251 | keycode 117 = F17 | ||
252 | keycode 118 = KP_MinPlus | ||
253 | keycode 119 = Pause | ||
254 | keycode 120 = | ||
255 | keycode 121 = | ||
256 | keycode 122 = | ||
257 | keycode 123 = | ||
258 | keycode 124 = | ||
259 | keycode 125 = | ||
260 | keycode 126 = | ||
261 | keycode 127 = | ||
262 | string F1 = "\033[[A" | ||
263 | string F2 = "\033[[B" | ||
264 | string F3 = "\033[[C" | ||
265 | string F4 = "\033[[D" | ||
266 | string F5 = "\033[[E" | ||
267 | string F6 = "\033[17~" | ||
268 | string F7 = "\033[18~" | ||
269 | string F8 = "\033[19~" | ||
270 | string F9 = "\033[20~" | ||
271 | string F10 = "\033[21~" | ||
272 | string F11 = "\033[23~" | ||
273 | string F12 = "\033[24~" | ||
274 | string F13 = "\033[25~" | ||
275 | string F14 = "\033[26~" | ||
276 | string F15 = "\033[28~" | ||
277 | string F16 = "\033[29~" | ||
278 | string F17 = "\033[31~" | ||
279 | string F18 = "\033[32~" | ||
280 | string F19 = "\033[33~" | ||
281 | string F20 = "\033[34~" | ||
282 | string Find = "\033[1~" | ||
283 | string Insert = "\033[2~" | ||
284 | string Remove = "\033[3~" | ||
285 | string Select = "\033[4~" | ||
286 | string Prior = "\033[5~" | ||
287 | string Next = "\033[6~" | ||
288 | string Macro = "\033[M" | ||
289 | string Pause = "\033[P" | ||
290 | compose '`' 'A' to 'À' | ||
291 | compose '`' 'a' to 'à' | ||
292 | compose '\'' 'A' to 'Á' | ||
293 | compose '\'' 'a' to 'á' | ||
294 | compose '^' 'A' to 'Â' | ||
295 | compose '^' 'a' to 'â' | ||
296 | compose '~' 'A' to 'Ã' | ||
297 | compose '~' 'a' to 'ã' | ||
298 | compose '"' 'A' to 'Ä' | ||
299 | compose '"' 'a' to 'ä' | ||
300 | compose 'O' 'A' to 'Å' | ||
301 | compose 'o' 'a' to 'å' | ||
302 | compose '0' 'A' to 'Å' | ||
303 | compose '0' 'a' to 'å' | ||
304 | compose 'A' 'A' to 'Å' | ||
305 | compose 'a' 'a' to 'å' | ||
306 | compose 'A' 'E' to 'Æ' | ||
307 | compose 'a' 'e' to 'æ' | ||
308 | compose ',' 'C' to 'Ç' | ||
309 | compose ',' 'c' to 'ç' | ||
310 | compose '`' 'E' to 'È' | ||
311 | compose '`' 'e' to 'è' | ||
312 | compose '\'' 'E' to 'É' | ||
313 | compose '\'' 'e' to 'é' | ||
314 | compose '^' 'E' to 'Ê' | ||
315 | compose '^' 'e' to 'ê' | ||
316 | compose '"' 'E' to 'Ë' | ||
317 | compose '"' 'e' to 'ë' | ||
318 | compose '`' 'I' to 'Ì' | ||
319 | compose '`' 'i' to 'ì' | ||
320 | compose '\'' 'I' to 'Í' | ||
321 | compose '\'' 'i' to 'í' | ||
322 | compose '^' 'I' to 'Î' | ||
323 | compose '^' 'i' to 'î' | ||
324 | compose '"' 'I' to 'Ï' | ||
325 | compose '"' 'i' to 'ï' | ||
326 | compose '-' 'D' to 'Ð' | ||
327 | compose '-' 'd' to 'ð' | ||
328 | compose '~' 'N' to 'Ñ' | ||
329 | compose '~' 'n' to 'ñ' | ||
330 | compose '`' 'O' to 'Ò' | ||
331 | compose '`' 'o' to 'ò' | ||
332 | compose '\'' 'O' to 'Ó' | ||
333 | compose '\'' 'o' to 'ó' | ||
334 | compose '^' 'O' to 'Ô' | ||
335 | compose '^' 'o' to 'ô' | ||
336 | compose '~' 'O' to 'Õ' | ||
337 | compose '~' 'o' to 'õ' | ||
338 | compose '"' 'O' to 'Ö' | ||
339 | compose '"' 'o' to 'ö' | ||
340 | compose '/' 'O' to 'Ø' | ||
341 | compose '/' 'o' to 'ø' | ||
342 | compose '`' 'U' to 'Ù' | ||
343 | compose '`' 'u' to 'ù' | ||
344 | compose '\'' 'U' to 'Ú' | ||
345 | compose '\'' 'u' to 'ú' | ||
346 | compose '^' 'U' to 'Û' | ||
347 | compose '^' 'u' to 'û' | ||
348 | compose '"' 'U' to 'Ü' | ||
349 | compose '"' 'u' to 'ü' | ||
350 | compose '\'' 'Y' to 'Ý' | ||
351 | compose '\'' 'y' to 'ý' | ||
352 | compose 'T' 'H' to 'Þ' | ||
353 | compose 't' 'h' to 'þ' | ||
354 | compose 's' 's' to 'ß' | ||
355 | compose '"' 'y' to 'ÿ' | ||
356 | compose 's' 'z' to 'ß' | ||
357 | compose 'i' 'j' to 'ÿ' | ||
diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c new file mode 100644 index 000000000000..e95d7876ca6b --- /dev/null +++ b/drivers/tty/vt/keyboard.c | |||
@@ -0,0 +1,1454 @@ | |||
1 | /* | ||
2 | * linux/drivers/char/keyboard.c | ||
3 | * | ||
4 | * Written for linux by Johan Myreen as a translation from | ||
5 | * the assembly version by Linus (with diacriticals added) | ||
6 | * | ||
7 | * Some additional features added by Christoph Niemann (ChN), March 1993 | ||
8 | * | ||
9 | * Loadable keymaps by Risto Kankkunen, May 1993 | ||
10 | * | ||
11 | * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993 | ||
12 | * Added decr/incr_console, dynamic keymaps, Unicode support, | ||
13 | * dynamic function/string keys, led setting, Sept 1994 | ||
14 | * `Sticky' modifier keys, 951006. | ||
15 | * | ||
16 | * 11-11-96: SAK should now work in the raw mode (Martin Mares) | ||
17 | * | ||
18 | * Modified to provide 'generic' keyboard support by Hamish Macdonald | ||
19 | * Merge with the m68k keyboard driver and split-off of the PC low-level | ||
20 | * parts by Geert Uytterhoeven, May 1997 | ||
21 | * | ||
22 | * 27-05-97: Added support for the Magic SysRq Key (Martin Mares) | ||
23 | * 30-07-98: Dead keys redone, aeb@cwi.nl. | ||
24 | * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik) | ||
25 | */ | ||
26 | |||
27 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
28 | |||
29 | #include <linux/consolemap.h> | ||
30 | #include <linux/module.h> | ||
31 | #include <linux/sched.h> | ||
32 | #include <linux/tty.h> | ||
33 | #include <linux/tty_flip.h> | ||
34 | #include <linux/mm.h> | ||
35 | #include <linux/string.h> | ||
36 | #include <linux/init.h> | ||
37 | #include <linux/slab.h> | ||
38 | #include <linux/irq.h> | ||
39 | |||
40 | #include <linux/kbd_kern.h> | ||
41 | #include <linux/kbd_diacr.h> | ||
42 | #include <linux/vt_kern.h> | ||
43 | #include <linux/input.h> | ||
44 | #include <linux/reboot.h> | ||
45 | #include <linux/notifier.h> | ||
46 | #include <linux/jiffies.h> | ||
47 | |||
48 | extern void ctrl_alt_del(void); | ||
49 | |||
50 | /* | ||
51 | * Exported functions/variables | ||
52 | */ | ||
53 | |||
54 | #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META)) | ||
55 | |||
56 | /* | ||
57 | * Some laptops take the 789uiojklm,. keys as number pad when NumLock is on. | ||
58 | * This seems a good reason to start with NumLock off. On HIL keyboards | ||
59 | * of PARISC machines however there is no NumLock key and everyone expects the keypad | ||
60 | * to be used for numbers. | ||
61 | */ | ||
62 | |||
63 | #if defined(CONFIG_PARISC) && (defined(CONFIG_KEYBOARD_HIL) || defined(CONFIG_KEYBOARD_HIL_OLD)) | ||
64 | #define KBD_DEFLEDS (1 << VC_NUMLOCK) | ||
65 | #else | ||
66 | #define KBD_DEFLEDS 0 | ||
67 | #endif | ||
68 | |||
69 | #define KBD_DEFLOCK 0 | ||
70 | |||
71 | void compute_shiftstate(void); | ||
72 | |||
73 | /* | ||
74 | * Handler Tables. | ||
75 | */ | ||
76 | |||
77 | #define K_HANDLERS\ | ||
78 | k_self, k_fn, k_spec, k_pad,\ | ||
79 | k_dead, k_cons, k_cur, k_shift,\ | ||
80 | k_meta, k_ascii, k_lock, k_lowercase,\ | ||
81 | k_slock, k_dead2, k_brl, k_ignore | ||
82 | |||
83 | typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value, | ||
84 | char up_flag); | ||
85 | static k_handler_fn K_HANDLERS; | ||
86 | static k_handler_fn *k_handler[16] = { K_HANDLERS }; | ||
87 | |||
88 | #define FN_HANDLERS\ | ||
89 | fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\ | ||
90 | fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\ | ||
91 | fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\ | ||
92 | fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\ | ||
93 | fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num | ||
94 | |||
95 | typedef void (fn_handler_fn)(struct vc_data *vc); | ||
96 | static fn_handler_fn FN_HANDLERS; | ||
97 | static fn_handler_fn *fn_handler[] = { FN_HANDLERS }; | ||
98 | |||
99 | /* | ||
100 | * Variables exported for vt_ioctl.c | ||
101 | */ | ||
102 | |||
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 = { | ||
117 | .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock), | ||
118 | .pid = NULL, | ||
119 | .sig = 0, | ||
120 | }; | ||
121 | |||
122 | /* | ||
123 | * Variables exported for vt.c | ||
124 | */ | ||
125 | |||
126 | int shift_state = 0; | ||
127 | |||
128 | /* | ||
129 | * Internal Data. | ||
130 | */ | ||
131 | |||
132 | static struct input_handler kbd_handler; | ||
133 | static DEFINE_SPINLOCK(kbd_event_lock); | ||
134 | static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)]; /* keyboard key bitmap */ | ||
135 | static unsigned char shift_down[NR_SHIFT]; /* shift state counters.. */ | ||
136 | static bool dead_key_next; | ||
137 | static int npadch = -1; /* -1 or number assembled on pad */ | ||
138 | static unsigned int diacr; | ||
139 | static char rep; /* flag telling character repeat */ | ||
140 | |||
141 | static unsigned char ledstate = 0xff; /* undefined */ | ||
142 | static unsigned char ledioctl; | ||
143 | |||
144 | static struct ledptr { | ||
145 | unsigned int *addr; | ||
146 | unsigned int mask; | ||
147 | unsigned char valid:1; | ||
148 | } ledptrs[3]; | ||
149 | |||
150 | /* | ||
151 | * Notifier list for console keyboard events | ||
152 | */ | ||
153 | static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list); | ||
154 | |||
155 | int register_keyboard_notifier(struct notifier_block *nb) | ||
156 | { | ||
157 | return atomic_notifier_chain_register(&keyboard_notifier_list, nb); | ||
158 | } | ||
159 | EXPORT_SYMBOL_GPL(register_keyboard_notifier); | ||
160 | |||
161 | int unregister_keyboard_notifier(struct notifier_block *nb) | ||
162 | { | ||
163 | return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb); | ||
164 | } | ||
165 | EXPORT_SYMBOL_GPL(unregister_keyboard_notifier); | ||
166 | |||
167 | /* | ||
168 | * Translation of scancodes to keycodes. We set them on only the first | ||
169 | * keyboard in the list that accepts the scancode and keycode. | ||
170 | * Explanation for not choosing the first attached keyboard anymore: | ||
171 | * USB keyboards for example have two event devices: one for all "normal" | ||
172 | * keys and one for extra function keys (like "volume up", "make coffee", | ||
173 | * etc.). So this means that scancodes for the extra function keys won't | ||
174 | * be valid for the first event device, but will be for the second. | ||
175 | */ | ||
176 | |||
177 | struct getset_keycode_data { | ||
178 | struct input_keymap_entry ke; | ||
179 | int error; | ||
180 | }; | ||
181 | |||
182 | static int getkeycode_helper(struct input_handle *handle, void *data) | ||
183 | { | ||
184 | struct getset_keycode_data *d = data; | ||
185 | |||
186 | d->error = input_get_keycode(handle->dev, &d->ke); | ||
187 | |||
188 | return d->error == 0; /* stop as soon as we successfully get one */ | ||
189 | } | ||
190 | |||
191 | int getkeycode(unsigned int scancode) | ||
192 | { | ||
193 | struct getset_keycode_data d = { | ||
194 | .ke = { | ||
195 | .flags = 0, | ||
196 | .len = sizeof(scancode), | ||
197 | .keycode = 0, | ||
198 | }, | ||
199 | .error = -ENODEV, | ||
200 | }; | ||
201 | |||
202 | memcpy(d.ke.scancode, &scancode, sizeof(scancode)); | ||
203 | |||
204 | input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper); | ||
205 | |||
206 | return d.error ?: d.ke.keycode; | ||
207 | } | ||
208 | |||
209 | static int setkeycode_helper(struct input_handle *handle, void *data) | ||
210 | { | ||
211 | struct getset_keycode_data *d = data; | ||
212 | |||
213 | d->error = input_set_keycode(handle->dev, &d->ke); | ||
214 | |||
215 | return d->error == 0; /* stop as soon as we successfully set one */ | ||
216 | } | ||
217 | |||
218 | int setkeycode(unsigned int scancode, unsigned int keycode) | ||
219 | { | ||
220 | struct getset_keycode_data d = { | ||
221 | .ke = { | ||
222 | .flags = 0, | ||
223 | .len = sizeof(scancode), | ||
224 | .keycode = keycode, | ||
225 | }, | ||
226 | .error = -ENODEV, | ||
227 | }; | ||
228 | |||
229 | memcpy(d.ke.scancode, &scancode, sizeof(scancode)); | ||
230 | |||
231 | input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper); | ||
232 | |||
233 | return d.error; | ||
234 | } | ||
235 | |||
236 | /* | ||
237 | * Making beeps and bells. Note that we prefer beeps to bells, but when | ||
238 | * shutting the sound off we do both. | ||
239 | */ | ||
240 | |||
241 | static int kd_sound_helper(struct input_handle *handle, void *data) | ||
242 | { | ||
243 | unsigned int *hz = data; | ||
244 | struct input_dev *dev = handle->dev; | ||
245 | |||
246 | if (test_bit(EV_SND, dev->evbit)) { | ||
247 | if (test_bit(SND_TONE, dev->sndbit)) { | ||
248 | input_inject_event(handle, EV_SND, SND_TONE, *hz); | ||
249 | if (*hz) | ||
250 | return 0; | ||
251 | } | ||
252 | if (test_bit(SND_BELL, dev->sndbit)) | ||
253 | input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0); | ||
254 | } | ||
255 | |||
256 | return 0; | ||
257 | } | ||
258 | |||
259 | static void kd_nosound(unsigned long ignored) | ||
260 | { | ||
261 | static unsigned int zero; | ||
262 | |||
263 | input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper); | ||
264 | } | ||
265 | |||
266 | static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0); | ||
267 | |||
268 | void kd_mksound(unsigned int hz, unsigned int ticks) | ||
269 | { | ||
270 | del_timer_sync(&kd_mksound_timer); | ||
271 | |||
272 | input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper); | ||
273 | |||
274 | if (hz && ticks) | ||
275 | mod_timer(&kd_mksound_timer, jiffies + ticks); | ||
276 | } | ||
277 | EXPORT_SYMBOL(kd_mksound); | ||
278 | |||
279 | /* | ||
280 | * Setting the keyboard rate. | ||
281 | */ | ||
282 | |||
283 | static int kbd_rate_helper(struct input_handle *handle, void *data) | ||
284 | { | ||
285 | struct input_dev *dev = handle->dev; | ||
286 | struct kbd_repeat *rep = data; | ||
287 | |||
288 | if (test_bit(EV_REP, dev->evbit)) { | ||
289 | |||
290 | if (rep[0].delay > 0) | ||
291 | input_inject_event(handle, | ||
292 | EV_REP, REP_DELAY, rep[0].delay); | ||
293 | if (rep[0].period > 0) | ||
294 | input_inject_event(handle, | ||
295 | EV_REP, REP_PERIOD, rep[0].period); | ||
296 | |||
297 | rep[1].delay = dev->rep[REP_DELAY]; | ||
298 | rep[1].period = dev->rep[REP_PERIOD]; | ||
299 | } | ||
300 | |||
301 | return 0; | ||
302 | } | ||
303 | |||
304 | int kbd_rate(struct kbd_repeat *rep) | ||
305 | { | ||
306 | struct kbd_repeat data[2] = { *rep }; | ||
307 | |||
308 | input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper); | ||
309 | *rep = data[1]; /* Copy currently used settings */ | ||
310 | |||
311 | return 0; | ||
312 | } | ||
313 | |||
314 | /* | ||
315 | * Helper Functions. | ||
316 | */ | ||
317 | static void put_queue(struct vc_data *vc, int ch) | ||
318 | { | ||
319 | struct tty_struct *tty = vc->port.tty; | ||
320 | |||
321 | if (tty) { | ||
322 | tty_insert_flip_char(tty, ch, 0); | ||
323 | con_schedule_flip(tty); | ||
324 | } | ||
325 | } | ||
326 | |||
327 | static void puts_queue(struct vc_data *vc, char *cp) | ||
328 | { | ||
329 | struct tty_struct *tty = vc->port.tty; | ||
330 | |||
331 | if (!tty) | ||
332 | return; | ||
333 | |||
334 | while (*cp) { | ||
335 | tty_insert_flip_char(tty, *cp, 0); | ||
336 | cp++; | ||
337 | } | ||
338 | con_schedule_flip(tty); | ||
339 | } | ||
340 | |||
341 | static void applkey(struct vc_data *vc, int key, char mode) | ||
342 | { | ||
343 | static char buf[] = { 0x1b, 'O', 0x00, 0x00 }; | ||
344 | |||
345 | buf[1] = (mode ? 'O' : '['); | ||
346 | buf[2] = key; | ||
347 | puts_queue(vc, buf); | ||
348 | } | ||
349 | |||
350 | /* | ||
351 | * Many other routines do put_queue, but I think either | ||
352 | * they produce ASCII, or they produce some user-assigned | ||
353 | * string, and in both cases we might assume that it is | ||
354 | * in utf-8 already. | ||
355 | */ | ||
356 | static void to_utf8(struct vc_data *vc, uint c) | ||
357 | { | ||
358 | if (c < 0x80) | ||
359 | /* 0******* */ | ||
360 | put_queue(vc, c); | ||
361 | else if (c < 0x800) { | ||
362 | /* 110***** 10****** */ | ||
363 | put_queue(vc, 0xc0 | (c >> 6)); | ||
364 | put_queue(vc, 0x80 | (c & 0x3f)); | ||
365 | } else if (c < 0x10000) { | ||
366 | if (c >= 0xD800 && c < 0xE000) | ||
367 | return; | ||
368 | if (c == 0xFFFF) | ||
369 | return; | ||
370 | /* 1110**** 10****** 10****** */ | ||
371 | put_queue(vc, 0xe0 | (c >> 12)); | ||
372 | put_queue(vc, 0x80 | ((c >> 6) & 0x3f)); | ||
373 | put_queue(vc, 0x80 | (c & 0x3f)); | ||
374 | } else if (c < 0x110000) { | ||
375 | /* 11110*** 10****** 10****** 10****** */ | ||
376 | put_queue(vc, 0xf0 | (c >> 18)); | ||
377 | put_queue(vc, 0x80 | ((c >> 12) & 0x3f)); | ||
378 | put_queue(vc, 0x80 | ((c >> 6) & 0x3f)); | ||
379 | put_queue(vc, 0x80 | (c & 0x3f)); | ||
380 | } | ||
381 | } | ||
382 | |||
383 | /* | ||
384 | * 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 | ||
386 | * undefined, so that shiftkey release is seen | ||
387 | */ | ||
388 | void compute_shiftstate(void) | ||
389 | { | ||
390 | unsigned int i, j, k, sym, val; | ||
391 | |||
392 | shift_state = 0; | ||
393 | memset(shift_down, 0, sizeof(shift_down)); | ||
394 | |||
395 | for (i = 0; i < ARRAY_SIZE(key_down); i++) { | ||
396 | |||
397 | if (!key_down[i]) | ||
398 | continue; | ||
399 | |||
400 | k = i * BITS_PER_LONG; | ||
401 | |||
402 | for (j = 0; j < BITS_PER_LONG; j++, k++) { | ||
403 | |||
404 | if (!test_bit(k, key_down)) | ||
405 | continue; | ||
406 | |||
407 | sym = U(key_maps[0][k]); | ||
408 | if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK) | ||
409 | continue; | ||
410 | |||
411 | val = KVAL(sym); | ||
412 | if (val == KVAL(K_CAPSSHIFT)) | ||
413 | val = KVAL(K_SHIFT); | ||
414 | |||
415 | shift_down[val]++; | ||
416 | shift_state |= (1 << val); | ||
417 | } | ||
418 | } | ||
419 | } | ||
420 | |||
421 | /* | ||
422 | * We have a combining character DIACR here, followed by the character CH. | ||
423 | * If the combination occurs in the table, return the corresponding value. | ||
424 | * Otherwise, if CH is a space or equals DIACR, return DIACR. | ||
425 | * Otherwise, conclude that DIACR was not combining after all, | ||
426 | * queue it and return CH. | ||
427 | */ | ||
428 | static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch) | ||
429 | { | ||
430 | unsigned int d = diacr; | ||
431 | unsigned int i; | ||
432 | |||
433 | diacr = 0; | ||
434 | |||
435 | if ((d & ~0xff) == BRL_UC_ROW) { | ||
436 | if ((ch & ~0xff) == BRL_UC_ROW) | ||
437 | return d | ch; | ||
438 | } else { | ||
439 | for (i = 0; i < accent_table_size; i++) | ||
440 | if (accent_table[i].diacr == d && accent_table[i].base == ch) | ||
441 | return accent_table[i].result; | ||
442 | } | ||
443 | |||
444 | if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d) | ||
445 | return d; | ||
446 | |||
447 | if (kbd->kbdmode == VC_UNICODE) | ||
448 | to_utf8(vc, d); | ||
449 | else { | ||
450 | int c = conv_uni_to_8bit(d); | ||
451 | if (c != -1) | ||
452 | put_queue(vc, c); | ||
453 | } | ||
454 | |||
455 | return ch; | ||
456 | } | ||
457 | |||
458 | /* | ||
459 | * Special function handlers | ||
460 | */ | ||
461 | static void fn_enter(struct vc_data *vc) | ||
462 | { | ||
463 | if (diacr) { | ||
464 | if (kbd->kbdmode == VC_UNICODE) | ||
465 | to_utf8(vc, diacr); | ||
466 | else { | ||
467 | int c = conv_uni_to_8bit(diacr); | ||
468 | if (c != -1) | ||
469 | put_queue(vc, c); | ||
470 | } | ||
471 | diacr = 0; | ||
472 | } | ||
473 | |||
474 | put_queue(vc, 13); | ||
475 | if (vc_kbd_mode(kbd, VC_CRLF)) | ||
476 | put_queue(vc, 10); | ||
477 | } | ||
478 | |||
479 | static void fn_caps_toggle(struct vc_data *vc) | ||
480 | { | ||
481 | if (rep) | ||
482 | return; | ||
483 | |||
484 | chg_vc_kbd_led(kbd, VC_CAPSLOCK); | ||
485 | } | ||
486 | |||
487 | static void fn_caps_on(struct vc_data *vc) | ||
488 | { | ||
489 | if (rep) | ||
490 | return; | ||
491 | |||
492 | set_vc_kbd_led(kbd, VC_CAPSLOCK); | ||
493 | } | ||
494 | |||
495 | static void fn_show_ptregs(struct vc_data *vc) | ||
496 | { | ||
497 | struct pt_regs *regs = get_irq_regs(); | ||
498 | |||
499 | if (regs) | ||
500 | show_regs(regs); | ||
501 | } | ||
502 | |||
503 | static void fn_hold(struct vc_data *vc) | ||
504 | { | ||
505 | struct tty_struct *tty = vc->port.tty; | ||
506 | |||
507 | if (rep || !tty) | ||
508 | return; | ||
509 | |||
510 | /* | ||
511 | * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty); | ||
512 | * these routines are also activated by ^S/^Q. | ||
513 | * (And SCROLLOCK can also be set by the ioctl KDSKBLED.) | ||
514 | */ | ||
515 | if (tty->stopped) | ||
516 | start_tty(tty); | ||
517 | else | ||
518 | stop_tty(tty); | ||
519 | } | ||
520 | |||
521 | static void fn_num(struct vc_data *vc) | ||
522 | { | ||
523 | if (vc_kbd_mode(kbd, VC_APPLIC)) | ||
524 | applkey(vc, 'P', 1); | ||
525 | else | ||
526 | fn_bare_num(vc); | ||
527 | } | ||
528 | |||
529 | /* | ||
530 | * Bind this to Shift-NumLock if you work in application keypad mode | ||
531 | * but want to be able to change the NumLock flag. | ||
532 | * Bind this to NumLock if you prefer that the NumLock key always | ||
533 | * changes the NumLock flag. | ||
534 | */ | ||
535 | static void fn_bare_num(struct vc_data *vc) | ||
536 | { | ||
537 | if (!rep) | ||
538 | chg_vc_kbd_led(kbd, VC_NUMLOCK); | ||
539 | } | ||
540 | |||
541 | static void fn_lastcons(struct vc_data *vc) | ||
542 | { | ||
543 | /* switch to the last used console, ChN */ | ||
544 | set_console(last_console); | ||
545 | } | ||
546 | |||
547 | static void fn_dec_console(struct vc_data *vc) | ||
548 | { | ||
549 | int i, cur = fg_console; | ||
550 | |||
551 | /* Currently switching? Queue this next switch relative to that. */ | ||
552 | if (want_console != -1) | ||
553 | cur = want_console; | ||
554 | |||
555 | for (i = cur - 1; i != cur; i--) { | ||
556 | if (i == -1) | ||
557 | i = MAX_NR_CONSOLES - 1; | ||
558 | if (vc_cons_allocated(i)) | ||
559 | break; | ||
560 | } | ||
561 | set_console(i); | ||
562 | } | ||
563 | |||
564 | static void fn_inc_console(struct vc_data *vc) | ||
565 | { | ||
566 | int i, cur = fg_console; | ||
567 | |||
568 | /* Currently switching? Queue this next switch relative to that. */ | ||
569 | if (want_console != -1) | ||
570 | cur = want_console; | ||
571 | |||
572 | for (i = cur+1; i != cur; i++) { | ||
573 | if (i == MAX_NR_CONSOLES) | ||
574 | i = 0; | ||
575 | if (vc_cons_allocated(i)) | ||
576 | break; | ||
577 | } | ||
578 | set_console(i); | ||
579 | } | ||
580 | |||
581 | static void fn_send_intr(struct vc_data *vc) | ||
582 | { | ||
583 | struct tty_struct *tty = vc->port.tty; | ||
584 | |||
585 | if (!tty) | ||
586 | return; | ||
587 | tty_insert_flip_char(tty, 0, TTY_BREAK); | ||
588 | con_schedule_flip(tty); | ||
589 | } | ||
590 | |||
591 | static void fn_scroll_forw(struct vc_data *vc) | ||
592 | { | ||
593 | scrollfront(vc, 0); | ||
594 | } | ||
595 | |||
596 | static void fn_scroll_back(struct vc_data *vc) | ||
597 | { | ||
598 | scrollback(vc, 0); | ||
599 | } | ||
600 | |||
601 | static void fn_show_mem(struct vc_data *vc) | ||
602 | { | ||
603 | show_mem(); | ||
604 | } | ||
605 | |||
606 | static void fn_show_state(struct vc_data *vc) | ||
607 | { | ||
608 | show_state(); | ||
609 | } | ||
610 | |||
611 | static void fn_boot_it(struct vc_data *vc) | ||
612 | { | ||
613 | ctrl_alt_del(); | ||
614 | } | ||
615 | |||
616 | static void fn_compose(struct vc_data *vc) | ||
617 | { | ||
618 | dead_key_next = true; | ||
619 | } | ||
620 | |||
621 | static void fn_spawn_con(struct vc_data *vc) | ||
622 | { | ||
623 | spin_lock(&vt_spawn_con.lock); | ||
624 | if (vt_spawn_con.pid) | ||
625 | if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) { | ||
626 | put_pid(vt_spawn_con.pid); | ||
627 | vt_spawn_con.pid = NULL; | ||
628 | } | ||
629 | spin_unlock(&vt_spawn_con.lock); | ||
630 | } | ||
631 | |||
632 | static void fn_SAK(struct vc_data *vc) | ||
633 | { | ||
634 | struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work; | ||
635 | schedule_work(SAK_work); | ||
636 | } | ||
637 | |||
638 | static void fn_null(struct vc_data *vc) | ||
639 | { | ||
640 | compute_shiftstate(); | ||
641 | } | ||
642 | |||
643 | /* | ||
644 | * Special key handlers | ||
645 | */ | ||
646 | static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag) | ||
647 | { | ||
648 | } | ||
649 | |||
650 | static void k_spec(struct vc_data *vc, unsigned char value, char up_flag) | ||
651 | { | ||
652 | if (up_flag) | ||
653 | return; | ||
654 | if (value >= ARRAY_SIZE(fn_handler)) | ||
655 | return; | ||
656 | if ((kbd->kbdmode == VC_RAW || | ||
657 | kbd->kbdmode == VC_MEDIUMRAW) && | ||
658 | value != KVAL(K_SAK)) | ||
659 | return; /* SAK is allowed even in raw mode */ | ||
660 | fn_handler[value](vc); | ||
661 | } | ||
662 | |||
663 | static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag) | ||
664 | { | ||
665 | pr_err("k_lowercase was called - impossible\n"); | ||
666 | } | ||
667 | |||
668 | static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag) | ||
669 | { | ||
670 | if (up_flag) | ||
671 | return; /* no action, if this is a key release */ | ||
672 | |||
673 | if (diacr) | ||
674 | value = handle_diacr(vc, value); | ||
675 | |||
676 | if (dead_key_next) { | ||
677 | dead_key_next = false; | ||
678 | diacr = value; | ||
679 | return; | ||
680 | } | ||
681 | if (kbd->kbdmode == VC_UNICODE) | ||
682 | to_utf8(vc, value); | ||
683 | else { | ||
684 | int c = conv_uni_to_8bit(value); | ||
685 | if (c != -1) | ||
686 | put_queue(vc, c); | ||
687 | } | ||
688 | } | ||
689 | |||
690 | /* | ||
691 | * Handle dead key. Note that we now may have several | ||
692 | * dead keys modifying the same character. Very useful | ||
693 | * for Vietnamese. | ||
694 | */ | ||
695 | static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag) | ||
696 | { | ||
697 | if (up_flag) | ||
698 | return; | ||
699 | |||
700 | diacr = (diacr ? handle_diacr(vc, value) : value); | ||
701 | } | ||
702 | |||
703 | static void k_self(struct vc_data *vc, unsigned char value, char up_flag) | ||
704 | { | ||
705 | k_unicode(vc, conv_8bit_to_uni(value), up_flag); | ||
706 | } | ||
707 | |||
708 | static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag) | ||
709 | { | ||
710 | k_deadunicode(vc, value, up_flag); | ||
711 | } | ||
712 | |||
713 | /* | ||
714 | * Obsolete - for backwards compatibility only | ||
715 | */ | ||
716 | static void k_dead(struct vc_data *vc, unsigned char value, char up_flag) | ||
717 | { | ||
718 | static const unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' }; | ||
719 | |||
720 | k_deadunicode(vc, ret_diacr[value], up_flag); | ||
721 | } | ||
722 | |||
723 | static void k_cons(struct vc_data *vc, unsigned char value, char up_flag) | ||
724 | { | ||
725 | if (up_flag) | ||
726 | return; | ||
727 | |||
728 | set_console(value); | ||
729 | } | ||
730 | |||
731 | static void k_fn(struct vc_data *vc, unsigned char value, char up_flag) | ||
732 | { | ||
733 | if (up_flag) | ||
734 | return; | ||
735 | |||
736 | if ((unsigned)value < ARRAY_SIZE(func_table)) { | ||
737 | if (func_table[value]) | ||
738 | puts_queue(vc, func_table[value]); | ||
739 | } else | ||
740 | pr_err("k_fn called with value=%d\n", value); | ||
741 | } | ||
742 | |||
743 | static void k_cur(struct vc_data *vc, unsigned char value, char up_flag) | ||
744 | { | ||
745 | static const char cur_chars[] = "BDCA"; | ||
746 | |||
747 | if (up_flag) | ||
748 | return; | ||
749 | |||
750 | applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE)); | ||
751 | } | ||
752 | |||
753 | static void k_pad(struct vc_data *vc, unsigned char value, char up_flag) | ||
754 | { | ||
755 | static const char pad_chars[] = "0123456789+-*/\015,.?()#"; | ||
756 | static const char app_map[] = "pqrstuvwxylSRQMnnmPQS"; | ||
757 | |||
758 | if (up_flag) | ||
759 | return; /* no action, if this is a key release */ | ||
760 | |||
761 | /* kludge... shift forces cursor/number keys */ | ||
762 | if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) { | ||
763 | applkey(vc, app_map[value], 1); | ||
764 | return; | ||
765 | } | ||
766 | |||
767 | if (!vc_kbd_led(kbd, VC_NUMLOCK)) { | ||
768 | |||
769 | switch (value) { | ||
770 | case KVAL(K_PCOMMA): | ||
771 | case KVAL(K_PDOT): | ||
772 | k_fn(vc, KVAL(K_REMOVE), 0); | ||
773 | return; | ||
774 | case KVAL(K_P0): | ||
775 | k_fn(vc, KVAL(K_INSERT), 0); | ||
776 | return; | ||
777 | case KVAL(K_P1): | ||
778 | k_fn(vc, KVAL(K_SELECT), 0); | ||
779 | return; | ||
780 | case KVAL(K_P2): | ||
781 | k_cur(vc, KVAL(K_DOWN), 0); | ||
782 | return; | ||
783 | case KVAL(K_P3): | ||
784 | k_fn(vc, KVAL(K_PGDN), 0); | ||
785 | return; | ||
786 | case KVAL(K_P4): | ||
787 | k_cur(vc, KVAL(K_LEFT), 0); | ||
788 | return; | ||
789 | case KVAL(K_P6): | ||
790 | k_cur(vc, KVAL(K_RIGHT), 0); | ||
791 | return; | ||
792 | case KVAL(K_P7): | ||
793 | k_fn(vc, KVAL(K_FIND), 0); | ||
794 | return; | ||
795 | case KVAL(K_P8): | ||
796 | k_cur(vc, KVAL(K_UP), 0); | ||
797 | return; | ||
798 | case KVAL(K_P9): | ||
799 | k_fn(vc, KVAL(K_PGUP), 0); | ||
800 | return; | ||
801 | case KVAL(K_P5): | ||
802 | applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC)); | ||
803 | return; | ||
804 | } | ||
805 | } | ||
806 | |||
807 | put_queue(vc, pad_chars[value]); | ||
808 | if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF)) | ||
809 | put_queue(vc, 10); | ||
810 | } | ||
811 | |||
812 | static void k_shift(struct vc_data *vc, unsigned char value, char up_flag) | ||
813 | { | ||
814 | int old_state = shift_state; | ||
815 | |||
816 | if (rep) | ||
817 | return; | ||
818 | /* | ||
819 | * Mimic typewriter: | ||
820 | * a CapsShift key acts like Shift but undoes CapsLock | ||
821 | */ | ||
822 | if (value == KVAL(K_CAPSSHIFT)) { | ||
823 | value = KVAL(K_SHIFT); | ||
824 | if (!up_flag) | ||
825 | clr_vc_kbd_led(kbd, VC_CAPSLOCK); | ||
826 | } | ||
827 | |||
828 | if (up_flag) { | ||
829 | /* | ||
830 | * handle the case that two shift or control | ||
831 | * keys are depressed simultaneously | ||
832 | */ | ||
833 | if (shift_down[value]) | ||
834 | shift_down[value]--; | ||
835 | } else | ||
836 | shift_down[value]++; | ||
837 | |||
838 | if (shift_down[value]) | ||
839 | shift_state |= (1 << value); | ||
840 | else | ||
841 | shift_state &= ~(1 << value); | ||
842 | |||
843 | /* kludge */ | ||
844 | if (up_flag && shift_state != old_state && npadch != -1) { | ||
845 | if (kbd->kbdmode == VC_UNICODE) | ||
846 | to_utf8(vc, npadch); | ||
847 | else | ||
848 | put_queue(vc, npadch & 0xff); | ||
849 | npadch = -1; | ||
850 | } | ||
851 | } | ||
852 | |||
853 | static void k_meta(struct vc_data *vc, unsigned char value, char up_flag) | ||
854 | { | ||
855 | if (up_flag) | ||
856 | return; | ||
857 | |||
858 | if (vc_kbd_mode(kbd, VC_META)) { | ||
859 | put_queue(vc, '\033'); | ||
860 | put_queue(vc, value); | ||
861 | } else | ||
862 | put_queue(vc, value | 0x80); | ||
863 | } | ||
864 | |||
865 | static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag) | ||
866 | { | ||
867 | int base; | ||
868 | |||
869 | if (up_flag) | ||
870 | return; | ||
871 | |||
872 | if (value < 10) { | ||
873 | /* decimal input of code, while Alt depressed */ | ||
874 | base = 10; | ||
875 | } else { | ||
876 | /* hexadecimal input of code, while AltGr depressed */ | ||
877 | value -= 10; | ||
878 | base = 16; | ||
879 | } | ||
880 | |||
881 | if (npadch == -1) | ||
882 | npadch = value; | ||
883 | else | ||
884 | npadch = npadch * base + value; | ||
885 | } | ||
886 | |||
887 | static void k_lock(struct vc_data *vc, unsigned char value, char up_flag) | ||
888 | { | ||
889 | if (up_flag || rep) | ||
890 | return; | ||
891 | |||
892 | chg_vc_kbd_lock(kbd, value); | ||
893 | } | ||
894 | |||
895 | static void k_slock(struct vc_data *vc, unsigned char value, char up_flag) | ||
896 | { | ||
897 | k_shift(vc, value, up_flag); | ||
898 | if (up_flag || rep) | ||
899 | return; | ||
900 | |||
901 | chg_vc_kbd_slock(kbd, value); | ||
902 | /* try to make Alt, oops, AltGr and such work */ | ||
903 | if (!key_maps[kbd->lockstate ^ kbd->slockstate]) { | ||
904 | kbd->slockstate = 0; | ||
905 | chg_vc_kbd_slock(kbd, value); | ||
906 | } | ||
907 | } | ||
908 | |||
909 | /* by default, 300ms interval for combination release */ | ||
910 | static unsigned brl_timeout = 300; | ||
911 | MODULE_PARM_DESC(brl_timeout, "Braille keys release delay in ms (0 for commit on first key release)"); | ||
912 | module_param(brl_timeout, uint, 0644); | ||
913 | |||
914 | static unsigned brl_nbchords = 1; | ||
915 | MODULE_PARM_DESC(brl_nbchords, "Number of chords that produce a braille pattern (0 for dead chords)"); | ||
916 | module_param(brl_nbchords, uint, 0644); | ||
917 | |||
918 | static void k_brlcommit(struct vc_data *vc, unsigned int pattern, char up_flag) | ||
919 | { | ||
920 | static unsigned long chords; | ||
921 | static unsigned committed; | ||
922 | |||
923 | if (!brl_nbchords) | ||
924 | k_deadunicode(vc, BRL_UC_ROW | pattern, up_flag); | ||
925 | else { | ||
926 | committed |= pattern; | ||
927 | chords++; | ||
928 | if (chords == brl_nbchords) { | ||
929 | k_unicode(vc, BRL_UC_ROW | committed, up_flag); | ||
930 | chords = 0; | ||
931 | committed = 0; | ||
932 | } | ||
933 | } | ||
934 | } | ||
935 | |||
936 | static void k_brl(struct vc_data *vc, unsigned char value, char up_flag) | ||
937 | { | ||
938 | static unsigned pressed, committing; | ||
939 | static unsigned long releasestart; | ||
940 | |||
941 | if (kbd->kbdmode != VC_UNICODE) { | ||
942 | if (!up_flag) | ||
943 | pr_warning("keyboard mode must be unicode for braille patterns\n"); | ||
944 | return; | ||
945 | } | ||
946 | |||
947 | if (!value) { | ||
948 | k_unicode(vc, BRL_UC_ROW, up_flag); | ||
949 | return; | ||
950 | } | ||
951 | |||
952 | if (value > 8) | ||
953 | return; | ||
954 | |||
955 | if (!up_flag) { | ||
956 | pressed |= 1 << (value - 1); | ||
957 | if (!brl_timeout) | ||
958 | committing = pressed; | ||
959 | } else if (brl_timeout) { | ||
960 | if (!committing || | ||
961 | time_after(jiffies, | ||
962 | releasestart + msecs_to_jiffies(brl_timeout))) { | ||
963 | committing = pressed; | ||
964 | releasestart = jiffies; | ||
965 | } | ||
966 | pressed &= ~(1 << (value - 1)); | ||
967 | if (!pressed && committing) { | ||
968 | k_brlcommit(vc, committing, 0); | ||
969 | committing = 0; | ||
970 | } | ||
971 | } else { | ||
972 | if (committing) { | ||
973 | k_brlcommit(vc, committing, 0); | ||
974 | committing = 0; | ||
975 | } | ||
976 | pressed &= ~(1 << (value - 1)); | ||
977 | } | ||
978 | } | ||
979 | |||
980 | /* | ||
981 | * The leds display either (i) the status of NumLock, CapsLock, ScrollLock, | ||
982 | * or (ii) whatever pattern of lights people want to show using KDSETLED, | ||
983 | * or (iii) specified bits of specified words in kernel memory. | ||
984 | */ | ||
985 | unsigned char getledstate(void) | ||
986 | { | ||
987 | return ledstate; | ||
988 | } | ||
989 | |||
990 | void setledstate(struct kbd_struct *kbd, unsigned int led) | ||
991 | { | ||
992 | if (!(led & ~7)) { | ||
993 | ledioctl = led; | ||
994 | kbd->ledmode = LED_SHOW_IOCTL; | ||
995 | } else | ||
996 | kbd->ledmode = LED_SHOW_FLAGS; | ||
997 | |||
998 | set_leds(); | ||
999 | } | ||
1000 | |||
1001 | static inline unsigned char getleds(void) | ||
1002 | { | ||
1003 | struct kbd_struct *kbd = kbd_table + fg_console; | ||
1004 | unsigned char leds; | ||
1005 | int i; | ||
1006 | |||
1007 | if (kbd->ledmode == LED_SHOW_IOCTL) | ||
1008 | return ledioctl; | ||
1009 | |||
1010 | leds = kbd->ledflagstate; | ||
1011 | |||
1012 | if (kbd->ledmode == LED_SHOW_MEM) { | ||
1013 | for (i = 0; i < 3; i++) | ||
1014 | if (ledptrs[i].valid) { | ||
1015 | if (*ledptrs[i].addr & ledptrs[i].mask) | ||
1016 | leds |= (1 << i); | ||
1017 | else | ||
1018 | leds &= ~(1 << i); | ||
1019 | } | ||
1020 | } | ||
1021 | return leds; | ||
1022 | } | ||
1023 | |||
1024 | static int kbd_update_leds_helper(struct input_handle *handle, void *data) | ||
1025 | { | ||
1026 | unsigned char leds = *(unsigned char *)data; | ||
1027 | |||
1028 | if (test_bit(EV_LED, handle->dev->evbit)) { | ||
1029 | input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01)); | ||
1030 | input_inject_event(handle, EV_LED, LED_NUML, !!(leds & 0x02)); | ||
1031 | input_inject_event(handle, EV_LED, LED_CAPSL, !!(leds & 0x04)); | ||
1032 | input_inject_event(handle, EV_SYN, SYN_REPORT, 0); | ||
1033 | } | ||
1034 | |||
1035 | return 0; | ||
1036 | } | ||
1037 | |||
1038 | /* | ||
1039 | * This is the tasklet that updates LED state on all keyboards | ||
1040 | * attached to the box. The reason we use tasklet is that we | ||
1041 | * need to handle the scenario when keyboard handler is not | ||
1042 | * registered yet but we already getting updates form VT to | ||
1043 | * update led state. | ||
1044 | */ | ||
1045 | static void kbd_bh(unsigned long dummy) | ||
1046 | { | ||
1047 | unsigned char leds = getleds(); | ||
1048 | |||
1049 | if (leds != ledstate) { | ||
1050 | input_handler_for_each_handle(&kbd_handler, &leds, | ||
1051 | kbd_update_leds_helper); | ||
1052 | ledstate = leds; | ||
1053 | } | ||
1054 | } | ||
1055 | |||
1056 | DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0); | ||
1057 | |||
1058 | #if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\ | ||
1059 | defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\ | ||
1060 | defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\ | ||
1061 | (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\ | ||
1062 | defined(CONFIG_AVR32) | ||
1063 | |||
1064 | #define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\ | ||
1065 | ((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001)) | ||
1066 | |||
1067 | static const unsigned short x86_keycodes[256] = | ||
1068 | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | ||
1069 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, | ||
1070 | 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, | ||
1071 | 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, | ||
1072 | 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, | ||
1073 | 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92, | ||
1074 | 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339, | ||
1075 | 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349, | ||
1076 | 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355, | ||
1077 | 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361, | ||
1078 | 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114, | ||
1079 | 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116, | ||
1080 | 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307, | ||
1081 | 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330, | ||
1082 | 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 }; | ||
1083 | |||
1084 | #ifdef CONFIG_SPARC | ||
1085 | static int sparc_l1_a_state; | ||
1086 | extern void sun_do_break(void); | ||
1087 | #endif | ||
1088 | |||
1089 | static int emulate_raw(struct vc_data *vc, unsigned int keycode, | ||
1090 | unsigned char up_flag) | ||
1091 | { | ||
1092 | int code; | ||
1093 | |||
1094 | switch (keycode) { | ||
1095 | |||
1096 | case KEY_PAUSE: | ||
1097 | put_queue(vc, 0xe1); | ||
1098 | put_queue(vc, 0x1d | up_flag); | ||
1099 | put_queue(vc, 0x45 | up_flag); | ||
1100 | break; | ||
1101 | |||
1102 | case KEY_HANGEUL: | ||
1103 | if (!up_flag) | ||
1104 | put_queue(vc, 0xf2); | ||
1105 | break; | ||
1106 | |||
1107 | case KEY_HANJA: | ||
1108 | if (!up_flag) | ||
1109 | put_queue(vc, 0xf1); | ||
1110 | break; | ||
1111 | |||
1112 | case KEY_SYSRQ: | ||
1113 | /* | ||
1114 | * Real AT keyboards (that's what we're trying | ||
1115 | * to emulate here emit 0xe0 0x2a 0xe0 0x37 when | ||
1116 | * pressing PrtSc/SysRq alone, but simply 0x54 | ||
1117 | * when pressing Alt+PrtSc/SysRq. | ||
1118 | */ | ||
1119 | if (test_bit(KEY_LEFTALT, key_down) || | ||
1120 | test_bit(KEY_RIGHTALT, key_down)) { | ||
1121 | put_queue(vc, 0x54 | up_flag); | ||
1122 | } else { | ||
1123 | put_queue(vc, 0xe0); | ||
1124 | put_queue(vc, 0x2a | up_flag); | ||
1125 | put_queue(vc, 0xe0); | ||
1126 | put_queue(vc, 0x37 | up_flag); | ||
1127 | } | ||
1128 | break; | ||
1129 | |||
1130 | default: | ||
1131 | if (keycode > 255) | ||
1132 | return -1; | ||
1133 | |||
1134 | code = x86_keycodes[keycode]; | ||
1135 | if (!code) | ||
1136 | return -1; | ||
1137 | |||
1138 | if (code & 0x100) | ||
1139 | put_queue(vc, 0xe0); | ||
1140 | put_queue(vc, (code & 0x7f) | up_flag); | ||
1141 | |||
1142 | break; | ||
1143 | } | ||
1144 | |||
1145 | return 0; | ||
1146 | } | ||
1147 | |||
1148 | #else | ||
1149 | |||
1150 | #define HW_RAW(dev) 0 | ||
1151 | |||
1152 | static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag) | ||
1153 | { | ||
1154 | if (keycode > 127) | ||
1155 | return -1; | ||
1156 | |||
1157 | put_queue(vc, keycode | up_flag); | ||
1158 | return 0; | ||
1159 | } | ||
1160 | #endif | ||
1161 | |||
1162 | static void kbd_rawcode(unsigned char data) | ||
1163 | { | ||
1164 | struct vc_data *vc = vc_cons[fg_console].d; | ||
1165 | |||
1166 | kbd = kbd_table + vc->vc_num; | ||
1167 | if (kbd->kbdmode == VC_RAW) | ||
1168 | put_queue(vc, data); | ||
1169 | } | ||
1170 | |||
1171 | static void kbd_keycode(unsigned int keycode, int down, int hw_raw) | ||
1172 | { | ||
1173 | struct vc_data *vc = vc_cons[fg_console].d; | ||
1174 | unsigned short keysym, *key_map; | ||
1175 | unsigned char type; | ||
1176 | bool raw_mode; | ||
1177 | struct tty_struct *tty; | ||
1178 | int shift_final; | ||
1179 | struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down }; | ||
1180 | int rc; | ||
1181 | |||
1182 | tty = vc->port.tty; | ||
1183 | |||
1184 | if (tty && (!tty->driver_data)) { | ||
1185 | /* No driver data? Strange. Okay we fix it then. */ | ||
1186 | tty->driver_data = vc; | ||
1187 | } | ||
1188 | |||
1189 | kbd = kbd_table + vc->vc_num; | ||
1190 | |||
1191 | #ifdef CONFIG_SPARC | ||
1192 | if (keycode == KEY_STOP) | ||
1193 | sparc_l1_a_state = down; | ||
1194 | #endif | ||
1195 | |||
1196 | rep = (down == 2); | ||
1197 | |||
1198 | raw_mode = (kbd->kbdmode == VC_RAW); | ||
1199 | if (raw_mode && !hw_raw) | ||
1200 | if (emulate_raw(vc, keycode, !down << 7)) | ||
1201 | if (keycode < BTN_MISC && printk_ratelimit()) | ||
1202 | pr_warning("can't emulate rawmode for keycode %d\n", | ||
1203 | keycode); | ||
1204 | |||
1205 | #ifdef CONFIG_SPARC | ||
1206 | if (keycode == KEY_A && sparc_l1_a_state) { | ||
1207 | sparc_l1_a_state = false; | ||
1208 | sun_do_break(); | ||
1209 | } | ||
1210 | #endif | ||
1211 | |||
1212 | if (kbd->kbdmode == VC_MEDIUMRAW) { | ||
1213 | /* | ||
1214 | * This is extended medium raw mode, with keys above 127 | ||
1215 | * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing | ||
1216 | * the 'up' flag if needed. 0 is reserved, so this shouldn't | ||
1217 | * interfere with anything else. The two bytes after 0 will | ||
1218 | * always have the up flag set not to interfere with older | ||
1219 | * applications. This allows for 16384 different keycodes, | ||
1220 | * which should be enough. | ||
1221 | */ | ||
1222 | if (keycode < 128) { | ||
1223 | put_queue(vc, keycode | (!down << 7)); | ||
1224 | } else { | ||
1225 | put_queue(vc, !down << 7); | ||
1226 | put_queue(vc, (keycode >> 7) | 0x80); | ||
1227 | put_queue(vc, keycode | 0x80); | ||
1228 | } | ||
1229 | raw_mode = true; | ||
1230 | } | ||
1231 | |||
1232 | if (down) | ||
1233 | set_bit(keycode, key_down); | ||
1234 | else | ||
1235 | clear_bit(keycode, key_down); | ||
1236 | |||
1237 | if (rep && | ||
1238 | (!vc_kbd_mode(kbd, VC_REPEAT) || | ||
1239 | (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) { | ||
1240 | /* | ||
1241 | * Don't repeat a key if the input buffers are not empty and the | ||
1242 | * characters get aren't echoed locally. This makes key repeat | ||
1243 | * usable with slow applications and under heavy loads. | ||
1244 | */ | ||
1245 | return; | ||
1246 | } | ||
1247 | |||
1248 | param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate; | ||
1249 | param.ledstate = kbd->ledflagstate; | ||
1250 | key_map = key_maps[shift_final]; | ||
1251 | |||
1252 | rc = atomic_notifier_call_chain(&keyboard_notifier_list, | ||
1253 | KBD_KEYCODE, ¶m); | ||
1254 | if (rc == NOTIFY_STOP || !key_map) { | ||
1255 | atomic_notifier_call_chain(&keyboard_notifier_list, | ||
1256 | KBD_UNBOUND_KEYCODE, ¶m); | ||
1257 | compute_shiftstate(); | ||
1258 | kbd->slockstate = 0; | ||
1259 | return; | ||
1260 | } | ||
1261 | |||
1262 | if (keycode < NR_KEYS) | ||
1263 | keysym = key_map[keycode]; | ||
1264 | else if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8) | ||
1265 | keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1)); | ||
1266 | else | ||
1267 | return; | ||
1268 | |||
1269 | type = KTYP(keysym); | ||
1270 | |||
1271 | if (type < 0xf0) { | ||
1272 | param.value = keysym; | ||
1273 | rc = atomic_notifier_call_chain(&keyboard_notifier_list, | ||
1274 | KBD_UNICODE, ¶m); | ||
1275 | if (rc != NOTIFY_STOP) | ||
1276 | if (down && !raw_mode) | ||
1277 | to_utf8(vc, keysym); | ||
1278 | return; | ||
1279 | } | ||
1280 | |||
1281 | type -= 0xf0; | ||
1282 | |||
1283 | if (type == KT_LETTER) { | ||
1284 | type = KT_LATIN; | ||
1285 | if (vc_kbd_led(kbd, VC_CAPSLOCK)) { | ||
1286 | key_map = key_maps[shift_final ^ (1 << KG_SHIFT)]; | ||
1287 | if (key_map) | ||
1288 | keysym = key_map[keycode]; | ||
1289 | } | ||
1290 | } | ||
1291 | |||
1292 | param.value = keysym; | ||
1293 | rc = atomic_notifier_call_chain(&keyboard_notifier_list, | ||
1294 | KBD_KEYSYM, ¶m); | ||
1295 | if (rc == NOTIFY_STOP) | ||
1296 | return; | ||
1297 | |||
1298 | if (raw_mode && type != KT_SPEC && type != KT_SHIFT) | ||
1299 | return; | ||
1300 | |||
1301 | (*k_handler[type])(vc, keysym & 0xff, !down); | ||
1302 | |||
1303 | param.ledstate = kbd->ledflagstate; | ||
1304 | atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, ¶m); | ||
1305 | |||
1306 | if (type != KT_SLOCK) | ||
1307 | kbd->slockstate = 0; | ||
1308 | } | ||
1309 | |||
1310 | static void kbd_event(struct input_handle *handle, unsigned int event_type, | ||
1311 | unsigned int event_code, int value) | ||
1312 | { | ||
1313 | /* We are called with interrupts disabled, just take the lock */ | ||
1314 | spin_lock(&kbd_event_lock); | ||
1315 | |||
1316 | if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev)) | ||
1317 | kbd_rawcode(value); | ||
1318 | if (event_type == EV_KEY) | ||
1319 | kbd_keycode(event_code, value, HW_RAW(handle->dev)); | ||
1320 | |||
1321 | spin_unlock(&kbd_event_lock); | ||
1322 | |||
1323 | tasklet_schedule(&keyboard_tasklet); | ||
1324 | do_poke_blanked_console = 1; | ||
1325 | schedule_console_callback(); | ||
1326 | } | ||
1327 | |||
1328 | static bool kbd_match(struct input_handler *handler, struct input_dev *dev) | ||
1329 | { | ||
1330 | int i; | ||
1331 | |||
1332 | if (test_bit(EV_SND, dev->evbit)) | ||
1333 | return true; | ||
1334 | |||
1335 | if (test_bit(EV_KEY, dev->evbit)) { | ||
1336 | for (i = KEY_RESERVED; i < BTN_MISC; i++) | ||
1337 | if (test_bit(i, dev->keybit)) | ||
1338 | return true; | ||
1339 | for (i = KEY_BRL_DOT1; i <= KEY_BRL_DOT10; i++) | ||
1340 | if (test_bit(i, dev->keybit)) | ||
1341 | return true; | ||
1342 | } | ||
1343 | |||
1344 | return false; | ||
1345 | } | ||
1346 | |||
1347 | /* | ||
1348 | * When a keyboard (or other input device) is found, the kbd_connect | ||
1349 | * function is called. The function then looks at the device, and if it | ||
1350 | * likes it, it can open it and get events from it. In this (kbd_connect) | ||
1351 | * function, we should decide which VT to bind that keyboard to initially. | ||
1352 | */ | ||
1353 | static int kbd_connect(struct input_handler *handler, struct input_dev *dev, | ||
1354 | const struct input_device_id *id) | ||
1355 | { | ||
1356 | struct input_handle *handle; | ||
1357 | int error; | ||
1358 | |||
1359 | handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL); | ||
1360 | if (!handle) | ||
1361 | return -ENOMEM; | ||
1362 | |||
1363 | handle->dev = dev; | ||
1364 | handle->handler = handler; | ||
1365 | handle->name = "kbd"; | ||
1366 | |||
1367 | error = input_register_handle(handle); | ||
1368 | if (error) | ||
1369 | goto err_free_handle; | ||
1370 | |||
1371 | error = input_open_device(handle); | ||
1372 | if (error) | ||
1373 | goto err_unregister_handle; | ||
1374 | |||
1375 | return 0; | ||
1376 | |||
1377 | err_unregister_handle: | ||
1378 | input_unregister_handle(handle); | ||
1379 | err_free_handle: | ||
1380 | kfree(handle); | ||
1381 | return error; | ||
1382 | } | ||
1383 | |||
1384 | static void kbd_disconnect(struct input_handle *handle) | ||
1385 | { | ||
1386 | input_close_device(handle); | ||
1387 | input_unregister_handle(handle); | ||
1388 | kfree(handle); | ||
1389 | } | ||
1390 | |||
1391 | /* | ||
1392 | * Start keyboard handler on the new keyboard by refreshing LED state to | ||
1393 | * match the rest of the system. | ||
1394 | */ | ||
1395 | static void kbd_start(struct input_handle *handle) | ||
1396 | { | ||
1397 | tasklet_disable(&keyboard_tasklet); | ||
1398 | |||
1399 | if (ledstate != 0xff) | ||
1400 | kbd_update_leds_helper(handle, &ledstate); | ||
1401 | |||
1402 | tasklet_enable(&keyboard_tasklet); | ||
1403 | } | ||
1404 | |||
1405 | static const struct input_device_id kbd_ids[] = { | ||
1406 | { | ||
1407 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT, | ||
1408 | .evbit = { BIT_MASK(EV_KEY) }, | ||
1409 | }, | ||
1410 | |||
1411 | { | ||
1412 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT, | ||
1413 | .evbit = { BIT_MASK(EV_SND) }, | ||
1414 | }, | ||
1415 | |||
1416 | { }, /* Terminating entry */ | ||
1417 | }; | ||
1418 | |||
1419 | MODULE_DEVICE_TABLE(input, kbd_ids); | ||
1420 | |||
1421 | static struct input_handler kbd_handler = { | ||
1422 | .event = kbd_event, | ||
1423 | .match = kbd_match, | ||
1424 | .connect = kbd_connect, | ||
1425 | .disconnect = kbd_disconnect, | ||
1426 | .start = kbd_start, | ||
1427 | .name = "kbd", | ||
1428 | .id_table = kbd_ids, | ||
1429 | }; | ||
1430 | |||
1431 | int __init kbd_init(void) | ||
1432 | { | ||
1433 | int i; | ||
1434 | int error; | ||
1435 | |||
1436 | for (i = 0; i < MAX_NR_CONSOLES; i++) { | ||
1437 | kbd_table[i].ledflagstate = KBD_DEFLEDS; | ||
1438 | kbd_table[i].default_ledflagstate = KBD_DEFLEDS; | ||
1439 | kbd_table[i].ledmode = LED_SHOW_FLAGS; | ||
1440 | kbd_table[i].lockstate = KBD_DEFLOCK; | ||
1441 | kbd_table[i].slockstate = 0; | ||
1442 | kbd_table[i].modeflags = KBD_DEFMODE; | ||
1443 | kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; | ||
1444 | } | ||
1445 | |||
1446 | error = input_register_handler(&kbd_handler); | ||
1447 | if (error) | ||
1448 | return error; | ||
1449 | |||
1450 | tasklet_enable(&keyboard_tasklet); | ||
1451 | tasklet_schedule(&keyboard_tasklet); | ||
1452 | |||
1453 | return 0; | ||
1454 | } | ||
diff --git a/drivers/tty/vt/selection.c b/drivers/tty/vt/selection.c new file mode 100644 index 000000000000..ebae344ce910 --- /dev/null +++ b/drivers/tty/vt/selection.c | |||
@@ -0,0 +1,348 @@ | |||
1 | /* | ||
2 | * linux/drivers/char/selection.c | ||
3 | * | ||
4 | * This module exports the functions: | ||
5 | * | ||
6 | * 'int set_selection(struct tiocl_selection __user *, struct tty_struct *)' | ||
7 | * 'void clear_selection(void)' | ||
8 | * 'int paste_selection(struct tty_struct *)' | ||
9 | * 'int sel_loadlut(char __user *)' | ||
10 | * | ||
11 | * Now that /dev/vcs exists, most of this can disappear again. | ||
12 | */ | ||
13 | |||
14 | #include <linux/module.h> | ||
15 | #include <linux/tty.h> | ||
16 | #include <linux/sched.h> | ||
17 | #include <linux/mm.h> | ||
18 | #include <linux/slab.h> | ||
19 | #include <linux/types.h> | ||
20 | |||
21 | #include <asm/uaccess.h> | ||
22 | |||
23 | #include <linux/kbd_kern.h> | ||
24 | #include <linux/vt_kern.h> | ||
25 | #include <linux/consolemap.h> | ||
26 | #include <linux/selection.h> | ||
27 | #include <linux/tiocl.h> | ||
28 | #include <linux/console.h> | ||
29 | #include <linux/smp_lock.h> | ||
30 | |||
31 | /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */ | ||
32 | #define isspace(c) ((c) == ' ') | ||
33 | |||
34 | extern void poke_blanked_console(void); | ||
35 | |||
36 | /* Variables for selection control. */ | ||
37 | /* Use a dynamic buffer, instead of static (Dec 1994) */ | ||
38 | struct vc_data *sel_cons; /* must not be deallocated */ | ||
39 | static int use_unicode; | ||
40 | static volatile int sel_start = -1; /* cleared by clear_selection */ | ||
41 | static int sel_end; | ||
42 | static int sel_buffer_lth; | ||
43 | static char *sel_buffer; | ||
44 | |||
45 | /* clear_selection, highlight and highlight_pointer can be called | ||
46 | from interrupt (via scrollback/front) */ | ||
47 | |||
48 | /* set reverse video on characters s-e of console with selection. */ | ||
49 | static inline void highlight(const int s, const int e) | ||
50 | { | ||
51 | invert_screen(sel_cons, s, e-s+2, 1); | ||
52 | } | ||
53 | |||
54 | /* use complementary color to show the pointer */ | ||
55 | static inline void highlight_pointer(const int where) | ||
56 | { | ||
57 | complement_pos(sel_cons, where); | ||
58 | } | ||
59 | |||
60 | static u16 | ||
61 | sel_pos(int n) | ||
62 | { | ||
63 | return inverse_translate(sel_cons, screen_glyph(sel_cons, n), | ||
64 | use_unicode); | ||
65 | } | ||
66 | |||
67 | /* remove the current selection highlight, if any, | ||
68 | from the console holding the selection. */ | ||
69 | void | ||
70 | clear_selection(void) { | ||
71 | highlight_pointer(-1); /* hide the pointer */ | ||
72 | if (sel_start != -1) { | ||
73 | highlight(sel_start, sel_end); | ||
74 | sel_start = -1; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | /* | ||
79 | * User settable table: what characters are to be considered alphabetic? | ||
80 | * 256 bits | ||
81 | */ | ||
82 | static u32 inwordLut[8]={ | ||
83 | 0x00000000, /* control chars */ | ||
84 | 0x03FF0000, /* digits */ | ||
85 | 0x87FFFFFE, /* uppercase and '_' */ | ||
86 | 0x07FFFFFE, /* lowercase */ | ||
87 | 0x00000000, | ||
88 | 0x00000000, | ||
89 | 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */ | ||
90 | 0xFF7FFFFF /* latin-1 accented letters, not division sign */ | ||
91 | }; | ||
92 | |||
93 | static inline int inword(const u16 c) { | ||
94 | return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1); | ||
95 | } | ||
96 | |||
97 | /* set inwordLut contents. Invoked by ioctl(). */ | ||
98 | int sel_loadlut(char __user *p) | ||
99 | { | ||
100 | return copy_from_user(inwordLut, (u32 __user *)(p+4), 32) ? -EFAULT : 0; | ||
101 | } | ||
102 | |||
103 | /* does screen address p correspond to character at LH/RH edge of screen? */ | ||
104 | static inline int atedge(const int p, int size_row) | ||
105 | { | ||
106 | return (!(p % size_row) || !((p + 2) % size_row)); | ||
107 | } | ||
108 | |||
109 | /* constrain v such that v <= u */ | ||
110 | static inline unsigned short limit(const unsigned short v, const unsigned short u) | ||
111 | { | ||
112 | return (v > u) ? u : v; | ||
113 | } | ||
114 | |||
115 | /* stores the char in UTF8 and returns the number of bytes used (1-3) */ | ||
116 | static int store_utf8(u16 c, char *p) | ||
117 | { | ||
118 | if (c < 0x80) { | ||
119 | /* 0******* */ | ||
120 | p[0] = c; | ||
121 | return 1; | ||
122 | } else if (c < 0x800) { | ||
123 | /* 110***** 10****** */ | ||
124 | p[0] = 0xc0 | (c >> 6); | ||
125 | p[1] = 0x80 | (c & 0x3f); | ||
126 | return 2; | ||
127 | } else { | ||
128 | /* 1110**** 10****** 10****** */ | ||
129 | p[0] = 0xe0 | (c >> 12); | ||
130 | p[1] = 0x80 | ((c >> 6) & 0x3f); | ||
131 | p[2] = 0x80 | (c & 0x3f); | ||
132 | return 3; | ||
133 | } | ||
134 | } | ||
135 | |||
136 | /* set the current selection. Invoked by ioctl() or by kernel code. */ | ||
137 | int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty) | ||
138 | { | ||
139 | struct vc_data *vc = vc_cons[fg_console].d; | ||
140 | int sel_mode, new_sel_start, new_sel_end, spc; | ||
141 | char *bp, *obp; | ||
142 | int i, ps, pe, multiplier; | ||
143 | u16 c; | ||
144 | struct kbd_struct *kbd = kbd_table + fg_console; | ||
145 | |||
146 | poke_blanked_console(); | ||
147 | |||
148 | { unsigned short xs, ys, xe, ye; | ||
149 | |||
150 | if (!access_ok(VERIFY_READ, sel, sizeof(*sel))) | ||
151 | return -EFAULT; | ||
152 | __get_user(xs, &sel->xs); | ||
153 | __get_user(ys, &sel->ys); | ||
154 | __get_user(xe, &sel->xe); | ||
155 | __get_user(ye, &sel->ye); | ||
156 | __get_user(sel_mode, &sel->sel_mode); | ||
157 | xs--; ys--; xe--; ye--; | ||
158 | xs = limit(xs, vc->vc_cols - 1); | ||
159 | ys = limit(ys, vc->vc_rows - 1); | ||
160 | xe = limit(xe, vc->vc_cols - 1); | ||
161 | ye = limit(ye, vc->vc_rows - 1); | ||
162 | ps = ys * vc->vc_size_row + (xs << 1); | ||
163 | pe = ye * vc->vc_size_row + (xe << 1); | ||
164 | |||
165 | if (sel_mode == TIOCL_SELCLEAR) { | ||
166 | /* useful for screendump without selection highlights */ | ||
167 | clear_selection(); | ||
168 | return 0; | ||
169 | } | ||
170 | |||
171 | if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) { | ||
172 | mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys); | ||
173 | return 0; | ||
174 | } | ||
175 | } | ||
176 | |||
177 | if (ps > pe) /* make sel_start <= sel_end */ | ||
178 | { | ||
179 | int tmp = ps; | ||
180 | ps = pe; | ||
181 | pe = tmp; | ||
182 | } | ||
183 | |||
184 | if (sel_cons != vc_cons[fg_console].d) { | ||
185 | clear_selection(); | ||
186 | sel_cons = vc_cons[fg_console].d; | ||
187 | } | ||
188 | use_unicode = kbd && kbd->kbdmode == VC_UNICODE; | ||
189 | |||
190 | switch (sel_mode) | ||
191 | { | ||
192 | case TIOCL_SELCHAR: /* character-by-character selection */ | ||
193 | new_sel_start = ps; | ||
194 | new_sel_end = pe; | ||
195 | break; | ||
196 | case TIOCL_SELWORD: /* word-by-word selection */ | ||
197 | spc = isspace(sel_pos(ps)); | ||
198 | for (new_sel_start = ps; ; ps -= 2) | ||
199 | { | ||
200 | if ((spc && !isspace(sel_pos(ps))) || | ||
201 | (!spc && !inword(sel_pos(ps)))) | ||
202 | break; | ||
203 | new_sel_start = ps; | ||
204 | if (!(ps % vc->vc_size_row)) | ||
205 | break; | ||
206 | } | ||
207 | spc = isspace(sel_pos(pe)); | ||
208 | for (new_sel_end = pe; ; pe += 2) | ||
209 | { | ||
210 | if ((spc && !isspace(sel_pos(pe))) || | ||
211 | (!spc && !inword(sel_pos(pe)))) | ||
212 | break; | ||
213 | new_sel_end = pe; | ||
214 | if (!((pe + 2) % vc->vc_size_row)) | ||
215 | break; | ||
216 | } | ||
217 | break; | ||
218 | case TIOCL_SELLINE: /* line-by-line selection */ | ||
219 | new_sel_start = ps - ps % vc->vc_size_row; | ||
220 | new_sel_end = pe + vc->vc_size_row | ||
221 | - pe % vc->vc_size_row - 2; | ||
222 | break; | ||
223 | case TIOCL_SELPOINTER: | ||
224 | highlight_pointer(pe); | ||
225 | return 0; | ||
226 | default: | ||
227 | return -EINVAL; | ||
228 | } | ||
229 | |||
230 | /* remove the pointer */ | ||
231 | highlight_pointer(-1); | ||
232 | |||
233 | /* select to end of line if on trailing space */ | ||
234 | if (new_sel_end > new_sel_start && | ||
235 | !atedge(new_sel_end, vc->vc_size_row) && | ||
236 | isspace(sel_pos(new_sel_end))) { | ||
237 | for (pe = new_sel_end + 2; ; pe += 2) | ||
238 | if (!isspace(sel_pos(pe)) || | ||
239 | atedge(pe, vc->vc_size_row)) | ||
240 | break; | ||
241 | if (isspace(sel_pos(pe))) | ||
242 | new_sel_end = pe; | ||
243 | } | ||
244 | if (sel_start == -1) /* no current selection */ | ||
245 | highlight(new_sel_start, new_sel_end); | ||
246 | else if (new_sel_start == sel_start) | ||
247 | { | ||
248 | if (new_sel_end == sel_end) /* no action required */ | ||
249 | return 0; | ||
250 | else if (new_sel_end > sel_end) /* extend to right */ | ||
251 | highlight(sel_end + 2, new_sel_end); | ||
252 | else /* contract from right */ | ||
253 | highlight(new_sel_end + 2, sel_end); | ||
254 | } | ||
255 | else if (new_sel_end == sel_end) | ||
256 | { | ||
257 | if (new_sel_start < sel_start) /* extend to left */ | ||
258 | highlight(new_sel_start, sel_start - 2); | ||
259 | else /* contract from left */ | ||
260 | highlight(sel_start, new_sel_start - 2); | ||
261 | } | ||
262 | else /* some other case; start selection from scratch */ | ||
263 | { | ||
264 | clear_selection(); | ||
265 | highlight(new_sel_start, new_sel_end); | ||
266 | } | ||
267 | sel_start = new_sel_start; | ||
268 | sel_end = new_sel_end; | ||
269 | |||
270 | /* Allocate a new buffer before freeing the old one ... */ | ||
271 | multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */ | ||
272 | bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL); | ||
273 | if (!bp) { | ||
274 | printk(KERN_WARNING "selection: kmalloc() failed\n"); | ||
275 | clear_selection(); | ||
276 | return -ENOMEM; | ||
277 | } | ||
278 | kfree(sel_buffer); | ||
279 | sel_buffer = bp; | ||
280 | |||
281 | obp = bp; | ||
282 | for (i = sel_start; i <= sel_end; i += 2) { | ||
283 | c = sel_pos(i); | ||
284 | if (use_unicode) | ||
285 | bp += store_utf8(c, bp); | ||
286 | else | ||
287 | *bp++ = c; | ||
288 | if (!isspace(c)) | ||
289 | obp = bp; | ||
290 | if (! ((i + 2) % vc->vc_size_row)) { | ||
291 | /* strip trailing blanks from line and add newline, | ||
292 | unless non-space at end of line. */ | ||
293 | if (obp != bp) { | ||
294 | bp = obp; | ||
295 | *bp++ = '\r'; | ||
296 | } | ||
297 | obp = bp; | ||
298 | } | ||
299 | } | ||
300 | sel_buffer_lth = bp - sel_buffer; | ||
301 | return 0; | ||
302 | } | ||
303 | |||
304 | /* Insert the contents of the selection buffer into the | ||
305 | * queue of the tty associated with the current console. | ||
306 | * Invoked by ioctl(). | ||
307 | */ | ||
308 | int paste_selection(struct tty_struct *tty) | ||
309 | { | ||
310 | struct vc_data *vc = tty->driver_data; | ||
311 | int pasted = 0; | ||
312 | unsigned int count; | ||
313 | struct tty_ldisc *ld; | ||
314 | DECLARE_WAITQUEUE(wait, current); | ||
315 | |||
316 | /* always called with BTM from vt_ioctl */ | ||
317 | WARN_ON(!tty_locked()); | ||
318 | |||
319 | acquire_console_sem(); | ||
320 | poke_blanked_console(); | ||
321 | release_console_sem(); | ||
322 | |||
323 | ld = tty_ldisc_ref(tty); | ||
324 | if (!ld) { | ||
325 | tty_unlock(); | ||
326 | ld = tty_ldisc_ref_wait(tty); | ||
327 | tty_lock(); | ||
328 | } | ||
329 | |||
330 | add_wait_queue(&vc->paste_wait, &wait); | ||
331 | while (sel_buffer && sel_buffer_lth > pasted) { | ||
332 | set_current_state(TASK_INTERRUPTIBLE); | ||
333 | if (test_bit(TTY_THROTTLED, &tty->flags)) { | ||
334 | schedule(); | ||
335 | continue; | ||
336 | } | ||
337 | count = sel_buffer_lth - pasted; | ||
338 | count = min(count, tty->receive_room); | ||
339 | tty->ldisc->ops->receive_buf(tty, sel_buffer + pasted, | ||
340 | NULL, count); | ||
341 | pasted += count; | ||
342 | } | ||
343 | remove_wait_queue(&vc->paste_wait, &wait); | ||
344 | __set_current_state(TASK_RUNNING); | ||
345 | |||
346 | tty_ldisc_deref(ld); | ||
347 | return 0; | ||
348 | } | ||
diff --git a/drivers/tty/vt/vc_screen.c b/drivers/tty/vt/vc_screen.c new file mode 100644 index 000000000000..eab3a1ff99e4 --- /dev/null +++ b/drivers/tty/vt/vc_screen.c | |||
@@ -0,0 +1,644 @@ | |||
1 | /* | ||
2 | * linux/drivers/char/vc_screen.c | ||
3 | * | ||
4 | * Provide access to virtual console memory. | ||
5 | * /dev/vcs0: the screen as it is being viewed right now (possibly scrolled) | ||
6 | * /dev/vcsN: the screen of /dev/ttyN (1 <= N <= 63) | ||
7 | * [minor: N] | ||
8 | * | ||
9 | * /dev/vcsaN: idem, but including attributes, and prefixed with | ||
10 | * the 4 bytes lines,columns,x,y (as screendump used to give). | ||
11 | * Attribute/character pair is in native endianity. | ||
12 | * [minor: N+128] | ||
13 | * | ||
14 | * This replaces screendump and part of selection, so that the system | ||
15 | * administrator can control access using file system permissions. | ||
16 | * | ||
17 | * aeb@cwi.nl - efter Friedas begravelse - 950211 | ||
18 | * | ||
19 | * machek@k332.feld.cvut.cz - modified not to send characters to wrong console | ||
20 | * - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...) | ||
21 | * - making it shorter - scr_readw are macros which expand in PRETTY long code | ||
22 | */ | ||
23 | |||
24 | #include <linux/kernel.h> | ||
25 | #include <linux/major.h> | ||
26 | #include <linux/errno.h> | ||
27 | #include <linux/tty.h> | ||
28 | #include <linux/interrupt.h> | ||
29 | #include <linux/mm.h> | ||
30 | #include <linux/init.h> | ||
31 | #include <linux/mutex.h> | ||
32 | #include <linux/vt_kern.h> | ||
33 | #include <linux/selection.h> | ||
34 | #include <linux/kbd_kern.h> | ||
35 | #include <linux/console.h> | ||
36 | #include <linux/device.h> | ||
37 | #include <linux/smp_lock.h> | ||
38 | #include <linux/sched.h> | ||
39 | #include <linux/fs.h> | ||
40 | #include <linux/poll.h> | ||
41 | #include <linux/signal.h> | ||
42 | #include <linux/slab.h> | ||
43 | #include <linux/notifier.h> | ||
44 | |||
45 | #include <asm/uaccess.h> | ||
46 | #include <asm/byteorder.h> | ||
47 | #include <asm/unaligned.h> | ||
48 | |||
49 | #undef attr | ||
50 | #undef org | ||
51 | #undef addr | ||
52 | #define HEADER_SIZE 4 | ||
53 | |||
54 | struct vcs_poll_data { | ||
55 | struct notifier_block notifier; | ||
56 | unsigned int cons_num; | ||
57 | bool seen_last_update; | ||
58 | wait_queue_head_t waitq; | ||
59 | struct fasync_struct *fasync; | ||
60 | }; | ||
61 | |||
62 | static int | ||
63 | vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param) | ||
64 | { | ||
65 | struct vt_notifier_param *param = _param; | ||
66 | struct vc_data *vc = param->vc; | ||
67 | struct vcs_poll_data *poll = | ||
68 | container_of(nb, struct vcs_poll_data, notifier); | ||
69 | int currcons = poll->cons_num; | ||
70 | |||
71 | if (code != VT_UPDATE) | ||
72 | return NOTIFY_DONE; | ||
73 | |||
74 | if (currcons == 0) | ||
75 | currcons = fg_console; | ||
76 | else | ||
77 | currcons--; | ||
78 | if (currcons != vc->vc_num) | ||
79 | return NOTIFY_DONE; | ||
80 | |||
81 | poll->seen_last_update = false; | ||
82 | wake_up_interruptible(&poll->waitq); | ||
83 | kill_fasync(&poll->fasync, SIGIO, POLL_IN); | ||
84 | return NOTIFY_OK; | ||
85 | } | ||
86 | |||
87 | static void | ||
88 | vcs_poll_data_free(struct vcs_poll_data *poll) | ||
89 | { | ||
90 | unregister_vt_notifier(&poll->notifier); | ||
91 | kfree(poll); | ||
92 | } | ||
93 | |||
94 | static struct vcs_poll_data * | ||
95 | vcs_poll_data_get(struct file *file) | ||
96 | { | ||
97 | struct vcs_poll_data *poll = file->private_data; | ||
98 | |||
99 | if (poll) | ||
100 | return poll; | ||
101 | |||
102 | poll = kzalloc(sizeof(*poll), GFP_KERNEL); | ||
103 | if (!poll) | ||
104 | return NULL; | ||
105 | poll->cons_num = iminor(file->f_path.dentry->d_inode) & 127; | ||
106 | init_waitqueue_head(&poll->waitq); | ||
107 | poll->notifier.notifier_call = vcs_notifier; | ||
108 | if (register_vt_notifier(&poll->notifier) != 0) { | ||
109 | kfree(poll); | ||
110 | return NULL; | ||
111 | } | ||
112 | |||
113 | /* | ||
114 | * This code may be called either through ->poll() or ->fasync(). | ||
115 | * If we have two threads using the same file descriptor, they could | ||
116 | * both enter this function, both notice that the structure hasn't | ||
117 | * been allocated yet and go ahead allocating it in parallel, but | ||
118 | * only one of them must survive and be shared otherwise we'd leak | ||
119 | * memory with a dangling notifier callback. | ||
120 | */ | ||
121 | spin_lock(&file->f_lock); | ||
122 | if (!file->private_data) { | ||
123 | file->private_data = poll; | ||
124 | } else { | ||
125 | /* someone else raced ahead of us */ | ||
126 | vcs_poll_data_free(poll); | ||
127 | poll = file->private_data; | ||
128 | } | ||
129 | spin_unlock(&file->f_lock); | ||
130 | |||
131 | return poll; | ||
132 | } | ||
133 | |||
134 | static int | ||
135 | vcs_size(struct inode *inode) | ||
136 | { | ||
137 | int size; | ||
138 | int minor = iminor(inode); | ||
139 | int currcons = minor & 127; | ||
140 | struct vc_data *vc; | ||
141 | |||
142 | if (currcons == 0) | ||
143 | currcons = fg_console; | ||
144 | else | ||
145 | currcons--; | ||
146 | if (!vc_cons_allocated(currcons)) | ||
147 | return -ENXIO; | ||
148 | vc = vc_cons[currcons].d; | ||
149 | |||
150 | size = vc->vc_rows * vc->vc_cols; | ||
151 | |||
152 | if (minor & 128) | ||
153 | size = 2*size + HEADER_SIZE; | ||
154 | return size; | ||
155 | } | ||
156 | |||
157 | static loff_t vcs_lseek(struct file *file, loff_t offset, int orig) | ||
158 | { | ||
159 | int size; | ||
160 | |||
161 | mutex_lock(&con_buf_mtx); | ||
162 | size = vcs_size(file->f_path.dentry->d_inode); | ||
163 | switch (orig) { | ||
164 | default: | ||
165 | mutex_unlock(&con_buf_mtx); | ||
166 | return -EINVAL; | ||
167 | case 2: | ||
168 | offset += size; | ||
169 | break; | ||
170 | case 1: | ||
171 | offset += file->f_pos; | ||
172 | case 0: | ||
173 | break; | ||
174 | } | ||
175 | if (offset < 0 || offset > size) { | ||
176 | mutex_unlock(&con_buf_mtx); | ||
177 | return -EINVAL; | ||
178 | } | ||
179 | file->f_pos = offset; | ||
180 | mutex_unlock(&con_buf_mtx); | ||
181 | return file->f_pos; | ||
182 | } | ||
183 | |||
184 | |||
185 | static ssize_t | ||
186 | vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | ||
187 | { | ||
188 | struct inode *inode = file->f_path.dentry->d_inode; | ||
189 | unsigned int currcons = iminor(inode); | ||
190 | struct vc_data *vc; | ||
191 | struct vcs_poll_data *poll; | ||
192 | long pos; | ||
193 | long viewed, attr, read; | ||
194 | int col, maxcol; | ||
195 | unsigned short *org = NULL; | ||
196 | ssize_t ret; | ||
197 | |||
198 | mutex_lock(&con_buf_mtx); | ||
199 | |||
200 | pos = *ppos; | ||
201 | |||
202 | /* Select the proper current console and verify | ||
203 | * sanity of the situation under the console lock. | ||
204 | */ | ||
205 | acquire_console_sem(); | ||
206 | |||
207 | attr = (currcons & 128); | ||
208 | currcons = (currcons & 127); | ||
209 | if (currcons == 0) { | ||
210 | currcons = fg_console; | ||
211 | viewed = 1; | ||
212 | } else { | ||
213 | currcons--; | ||
214 | viewed = 0; | ||
215 | } | ||
216 | ret = -ENXIO; | ||
217 | if (!vc_cons_allocated(currcons)) | ||
218 | goto unlock_out; | ||
219 | vc = vc_cons[currcons].d; | ||
220 | |||
221 | ret = -EINVAL; | ||
222 | if (pos < 0) | ||
223 | goto unlock_out; | ||
224 | poll = file->private_data; | ||
225 | if (count && poll) | ||
226 | poll->seen_last_update = true; | ||
227 | read = 0; | ||
228 | ret = 0; | ||
229 | while (count) { | ||
230 | char *con_buf0, *con_buf_start; | ||
231 | long this_round, size; | ||
232 | ssize_t orig_count; | ||
233 | long p = pos; | ||
234 | |||
235 | /* Check whether we are above size each round, | ||
236 | * as copy_to_user at the end of this loop | ||
237 | * could sleep. | ||
238 | */ | ||
239 | size = vcs_size(inode); | ||
240 | if (pos >= size) | ||
241 | break; | ||
242 | if (count > size - pos) | ||
243 | count = size - pos; | ||
244 | |||
245 | this_round = count; | ||
246 | if (this_round > CON_BUF_SIZE) | ||
247 | this_round = CON_BUF_SIZE; | ||
248 | |||
249 | /* Perform the whole read into the local con_buf. | ||
250 | * Then we can drop the console spinlock and safely | ||
251 | * attempt to move it to userspace. | ||
252 | */ | ||
253 | |||
254 | con_buf_start = con_buf0 = con_buf; | ||
255 | orig_count = this_round; | ||
256 | maxcol = vc->vc_cols; | ||
257 | if (!attr) { | ||
258 | org = screen_pos(vc, p, viewed); | ||
259 | col = p % maxcol; | ||
260 | p += maxcol - col; | ||
261 | while (this_round-- > 0) { | ||
262 | *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff); | ||
263 | if (++col == maxcol) { | ||
264 | org = screen_pos(vc, p, viewed); | ||
265 | col = 0; | ||
266 | p += maxcol; | ||
267 | } | ||
268 | } | ||
269 | } else { | ||
270 | if (p < HEADER_SIZE) { | ||
271 | size_t tmp_count; | ||
272 | |||
273 | con_buf0[0] = (char)vc->vc_rows; | ||
274 | con_buf0[1] = (char)vc->vc_cols; | ||
275 | getconsxy(vc, con_buf0 + 2); | ||
276 | |||
277 | con_buf_start += p; | ||
278 | this_round += p; | ||
279 | if (this_round > CON_BUF_SIZE) { | ||
280 | this_round = CON_BUF_SIZE; | ||
281 | orig_count = this_round - p; | ||
282 | } | ||
283 | |||
284 | tmp_count = HEADER_SIZE; | ||
285 | if (tmp_count > this_round) | ||
286 | tmp_count = this_round; | ||
287 | |||
288 | /* Advance state pointers and move on. */ | ||
289 | this_round -= tmp_count; | ||
290 | p = HEADER_SIZE; | ||
291 | con_buf0 = con_buf + HEADER_SIZE; | ||
292 | /* If this_round >= 0, then p is even... */ | ||
293 | } else if (p & 1) { | ||
294 | /* Skip first byte for output if start address is odd | ||
295 | * Update region sizes up/down depending on free | ||
296 | * space in buffer. | ||
297 | */ | ||
298 | con_buf_start++; | ||
299 | if (this_round < CON_BUF_SIZE) | ||
300 | this_round++; | ||
301 | else | ||
302 | orig_count--; | ||
303 | } | ||
304 | if (this_round > 0) { | ||
305 | unsigned short *tmp_buf = (unsigned short *)con_buf0; | ||
306 | |||
307 | p -= HEADER_SIZE; | ||
308 | p /= 2; | ||
309 | col = p % maxcol; | ||
310 | |||
311 | org = screen_pos(vc, p, viewed); | ||
312 | p += maxcol - col; | ||
313 | |||
314 | /* Buffer has even length, so we can always copy | ||
315 | * character + attribute. We do not copy last byte | ||
316 | * to userspace if this_round is odd. | ||
317 | */ | ||
318 | this_round = (this_round + 1) >> 1; | ||
319 | |||
320 | while (this_round) { | ||
321 | *tmp_buf++ = vcs_scr_readw(vc, org++); | ||
322 | this_round --; | ||
323 | if (++col == maxcol) { | ||
324 | org = screen_pos(vc, p, viewed); | ||
325 | col = 0; | ||
326 | p += maxcol; | ||
327 | } | ||
328 | } | ||
329 | } | ||
330 | } | ||
331 | |||
332 | /* Finally, release the console semaphore while we push | ||
333 | * all the data to userspace from our temporary buffer. | ||
334 | * | ||
335 | * AKPM: Even though it's a semaphore, we should drop it because | ||
336 | * the pagefault handling code may want to call printk(). | ||
337 | */ | ||
338 | |||
339 | release_console_sem(); | ||
340 | ret = copy_to_user(buf, con_buf_start, orig_count); | ||
341 | acquire_console_sem(); | ||
342 | |||
343 | if (ret) { | ||
344 | read += (orig_count - ret); | ||
345 | ret = -EFAULT; | ||
346 | break; | ||
347 | } | ||
348 | buf += orig_count; | ||
349 | pos += orig_count; | ||
350 | read += orig_count; | ||
351 | count -= orig_count; | ||
352 | } | ||
353 | *ppos += read; | ||
354 | if (read) | ||
355 | ret = read; | ||
356 | unlock_out: | ||
357 | release_console_sem(); | ||
358 | mutex_unlock(&con_buf_mtx); | ||
359 | return ret; | ||
360 | } | ||
361 | |||
362 | static ssize_t | ||
363 | vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) | ||
364 | { | ||
365 | struct inode *inode = file->f_path.dentry->d_inode; | ||
366 | unsigned int currcons = iminor(inode); | ||
367 | struct vc_data *vc; | ||
368 | long pos; | ||
369 | long viewed, attr, size, written; | ||
370 | char *con_buf0; | ||
371 | int col, maxcol; | ||
372 | u16 *org0 = NULL, *org = NULL; | ||
373 | size_t ret; | ||
374 | |||
375 | mutex_lock(&con_buf_mtx); | ||
376 | |||
377 | pos = *ppos; | ||
378 | |||
379 | /* Select the proper current console and verify | ||
380 | * sanity of the situation under the console lock. | ||
381 | */ | ||
382 | acquire_console_sem(); | ||
383 | |||
384 | attr = (currcons & 128); | ||
385 | currcons = (currcons & 127); | ||
386 | |||
387 | if (currcons == 0) { | ||
388 | currcons = fg_console; | ||
389 | viewed = 1; | ||
390 | } else { | ||
391 | currcons--; | ||
392 | viewed = 0; | ||
393 | } | ||
394 | ret = -ENXIO; | ||
395 | if (!vc_cons_allocated(currcons)) | ||
396 | goto unlock_out; | ||
397 | vc = vc_cons[currcons].d; | ||
398 | |||
399 | size = vcs_size(inode); | ||
400 | ret = -EINVAL; | ||
401 | if (pos < 0 || pos > size) | ||
402 | goto unlock_out; | ||
403 | if (count > size - pos) | ||
404 | count = size - pos; | ||
405 | written = 0; | ||
406 | while (count) { | ||
407 | long this_round = count; | ||
408 | size_t orig_count; | ||
409 | long p; | ||
410 | |||
411 | if (this_round > CON_BUF_SIZE) | ||
412 | this_round = CON_BUF_SIZE; | ||
413 | |||
414 | /* Temporarily drop the console lock so that we can read | ||
415 | * in the write data from userspace safely. | ||
416 | */ | ||
417 | release_console_sem(); | ||
418 | ret = copy_from_user(con_buf, buf, this_round); | ||
419 | acquire_console_sem(); | ||
420 | |||
421 | if (ret) { | ||
422 | this_round -= ret; | ||
423 | if (!this_round) { | ||
424 | /* Abort loop if no data were copied. Otherwise | ||
425 | * fail with -EFAULT. | ||
426 | */ | ||
427 | if (written) | ||
428 | break; | ||
429 | ret = -EFAULT; | ||
430 | goto unlock_out; | ||
431 | } | ||
432 | } | ||
433 | |||
434 | /* The vcs_size might have changed while we slept to grab | ||
435 | * the user buffer, so recheck. | ||
436 | * Return data written up to now on failure. | ||
437 | */ | ||
438 | size = vcs_size(inode); | ||
439 | if (pos >= size) | ||
440 | break; | ||
441 | if (this_round > size - pos) | ||
442 | this_round = size - pos; | ||
443 | |||
444 | /* OK, now actually push the write to the console | ||
445 | * under the lock using the local kernel buffer. | ||
446 | */ | ||
447 | |||
448 | con_buf0 = con_buf; | ||
449 | orig_count = this_round; | ||
450 | maxcol = vc->vc_cols; | ||
451 | p = pos; | ||
452 | if (!attr) { | ||
453 | org0 = org = screen_pos(vc, p, viewed); | ||
454 | col = p % maxcol; | ||
455 | p += maxcol - col; | ||
456 | |||
457 | while (this_round > 0) { | ||
458 | unsigned char c = *con_buf0++; | ||
459 | |||
460 | this_round--; | ||
461 | vcs_scr_writew(vc, | ||
462 | (vcs_scr_readw(vc, org) & 0xff00) | c, org); | ||
463 | org++; | ||
464 | if (++col == maxcol) { | ||
465 | org = screen_pos(vc, p, viewed); | ||
466 | col = 0; | ||
467 | p += maxcol; | ||
468 | } | ||
469 | } | ||
470 | } else { | ||
471 | if (p < HEADER_SIZE) { | ||
472 | char header[HEADER_SIZE]; | ||
473 | |||
474 | getconsxy(vc, header + 2); | ||
475 | while (p < HEADER_SIZE && this_round > 0) { | ||
476 | this_round--; | ||
477 | header[p++] = *con_buf0++; | ||
478 | } | ||
479 | if (!viewed) | ||
480 | putconsxy(vc, header + 2); | ||
481 | } | ||
482 | p -= HEADER_SIZE; | ||
483 | col = (p/2) % maxcol; | ||
484 | if (this_round > 0) { | ||
485 | org0 = org = screen_pos(vc, p/2, viewed); | ||
486 | if ((p & 1) && this_round > 0) { | ||
487 | char c; | ||
488 | |||
489 | this_round--; | ||
490 | c = *con_buf0++; | ||
491 | #ifdef __BIG_ENDIAN | ||
492 | vcs_scr_writew(vc, c | | ||
493 | (vcs_scr_readw(vc, org) & 0xff00), org); | ||
494 | #else | ||
495 | vcs_scr_writew(vc, (c << 8) | | ||
496 | (vcs_scr_readw(vc, org) & 0xff), org); | ||
497 | #endif | ||
498 | org++; | ||
499 | p++; | ||
500 | if (++col == maxcol) { | ||
501 | org = screen_pos(vc, p/2, viewed); | ||
502 | col = 0; | ||
503 | } | ||
504 | } | ||
505 | p /= 2; | ||
506 | p += maxcol - col; | ||
507 | } | ||
508 | while (this_round > 1) { | ||
509 | unsigned short w; | ||
510 | |||
511 | w = get_unaligned(((unsigned short *)con_buf0)); | ||
512 | vcs_scr_writew(vc, w, org++); | ||
513 | con_buf0 += 2; | ||
514 | this_round -= 2; | ||
515 | if (++col == maxcol) { | ||
516 | org = screen_pos(vc, p, viewed); | ||
517 | col = 0; | ||
518 | p += maxcol; | ||
519 | } | ||
520 | } | ||
521 | if (this_round > 0) { | ||
522 | unsigned char c; | ||
523 | |||
524 | c = *con_buf0++; | ||
525 | #ifdef __BIG_ENDIAN | ||
526 | vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org); | ||
527 | #else | ||
528 | vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org); | ||
529 | #endif | ||
530 | } | ||
531 | } | ||
532 | count -= orig_count; | ||
533 | written += orig_count; | ||
534 | buf += orig_count; | ||
535 | pos += orig_count; | ||
536 | if (org0) | ||
537 | update_region(vc, (unsigned long)(org0), org - org0); | ||
538 | } | ||
539 | *ppos += written; | ||
540 | ret = written; | ||
541 | if (written) | ||
542 | vcs_scr_updated(vc); | ||
543 | |||
544 | unlock_out: | ||
545 | release_console_sem(); | ||
546 | |||
547 | mutex_unlock(&con_buf_mtx); | ||
548 | |||
549 | return ret; | ||
550 | } | ||
551 | |||
552 | static unsigned int | ||
553 | vcs_poll(struct file *file, poll_table *wait) | ||
554 | { | ||
555 | struct vcs_poll_data *poll = vcs_poll_data_get(file); | ||
556 | int ret = DEFAULT_POLLMASK|POLLERR|POLLPRI; | ||
557 | |||
558 | if (poll) { | ||
559 | poll_wait(file, &poll->waitq, wait); | ||
560 | if (poll->seen_last_update) | ||
561 | ret = DEFAULT_POLLMASK; | ||
562 | } | ||
563 | return ret; | ||
564 | } | ||
565 | |||
566 | static int | ||
567 | vcs_fasync(int fd, struct file *file, int on) | ||
568 | { | ||
569 | struct vcs_poll_data *poll = file->private_data; | ||
570 | |||
571 | if (!poll) { | ||
572 | /* don't allocate anything if all we want is disable fasync */ | ||
573 | if (!on) | ||
574 | return 0; | ||
575 | poll = vcs_poll_data_get(file); | ||
576 | if (!poll) | ||
577 | return -ENOMEM; | ||
578 | } | ||
579 | |||
580 | return fasync_helper(fd, file, on, &poll->fasync); | ||
581 | } | ||
582 | |||
583 | static int | ||
584 | vcs_open(struct inode *inode, struct file *filp) | ||
585 | { | ||
586 | unsigned int currcons = iminor(inode) & 127; | ||
587 | int ret = 0; | ||
588 | |||
589 | tty_lock(); | ||
590 | if(currcons && !vc_cons_allocated(currcons-1)) | ||
591 | ret = -ENXIO; | ||
592 | tty_unlock(); | ||
593 | return ret; | ||
594 | } | ||
595 | |||
596 | static int vcs_release(struct inode *inode, struct file *file) | ||
597 | { | ||
598 | struct vcs_poll_data *poll = file->private_data; | ||
599 | |||
600 | if (poll) | ||
601 | vcs_poll_data_free(poll); | ||
602 | return 0; | ||
603 | } | ||
604 | |||
605 | static const struct file_operations vcs_fops = { | ||
606 | .llseek = vcs_lseek, | ||
607 | .read = vcs_read, | ||
608 | .write = vcs_write, | ||
609 | .poll = vcs_poll, | ||
610 | .fasync = vcs_fasync, | ||
611 | .open = vcs_open, | ||
612 | .release = vcs_release, | ||
613 | }; | ||
614 | |||
615 | static struct class *vc_class; | ||
616 | |||
617 | void vcs_make_sysfs(int index) | ||
618 | { | ||
619 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL, | ||
620 | "vcs%u", index + 1); | ||
621 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL, | ||
622 | "vcsa%u", index + 1); | ||
623 | } | ||
624 | |||
625 | void vcs_remove_sysfs(int index) | ||
626 | { | ||
627 | device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1)); | ||
628 | device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129)); | ||
629 | } | ||
630 | |||
631 | int __init vcs_init(void) | ||
632 | { | ||
633 | unsigned int i; | ||
634 | |||
635 | if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops)) | ||
636 | panic("unable to get major %d for vcs device", VCS_MAJOR); | ||
637 | vc_class = class_create(THIS_MODULE, "vc"); | ||
638 | |||
639 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs"); | ||
640 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa"); | ||
641 | for (i = 0; i < MIN_NR_CONSOLES; i++) | ||
642 | vcs_make_sysfs(i); | ||
643 | return 0; | ||
644 | } | ||
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c new file mode 100644 index 000000000000..a8ec48ed14d9 --- /dev/null +++ b/drivers/tty/vt/vt.c | |||
@@ -0,0 +1,4209 @@ | |||
1 | /* | ||
2 | * linux/drivers/char/vt.c | ||
3 | * | ||
4 | * Copyright (C) 1991, 1992 Linus Torvalds | ||
5 | */ | ||
6 | |||
7 | /* | ||
8 | * Hopefully this will be a rather complete VT102 implementation. | ||
9 | * | ||
10 | * Beeping thanks to John T Kohl. | ||
11 | * | ||
12 | * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics | ||
13 | * Chars, and VT100 enhancements by Peter MacDonald. | ||
14 | * | ||
15 | * Copy and paste function by Andrew Haylett, | ||
16 | * some enhancements by Alessandro Rubini. | ||
17 | * | ||
18 | * Code to check for different video-cards mostly by Galen Hunt, | ||
19 | * <g-hunt@ee.utah.edu> | ||
20 | * | ||
21 | * Rudimentary ISO 10646/Unicode/UTF-8 character set support by | ||
22 | * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>. | ||
23 | * | ||
24 | * Dynamic allocation of consoles, aeb@cwi.nl, May 1994 | ||
25 | * Resizing of consoles, aeb, 940926 | ||
26 | * | ||
27 | * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94 | ||
28 | * <poe@daimi.aau.dk> | ||
29 | * | ||
30 | * User-defined bell sound, new setterm control sequences and printk | ||
31 | * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95 | ||
32 | * | ||
33 | * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp> | ||
34 | * | ||
35 | * Merge with the abstract console driver by Geert Uytterhoeven | ||
36 | * <geert@linux-m68k.org>, Jan 1997. | ||
37 | * | ||
38 | * Original m68k console driver modifications by | ||
39 | * | ||
40 | * - Arno Griffioen <arno@usn.nl> | ||
41 | * - David Carter <carter@cs.bris.ac.uk> | ||
42 | * | ||
43 | * The abstract console driver provides a generic interface for a text | ||
44 | * console. It supports VGA text mode, frame buffer based graphical consoles | ||
45 | * and special graphics processors that are only accessible through some | ||
46 | * registers (e.g. a TMS340x0 GSP). | ||
47 | * | ||
48 | * The interface to the hardware is specified using a special structure | ||
49 | * (struct consw) which contains function pointers to console operations | ||
50 | * (see <linux/console.h> for more information). | ||
51 | * | ||
52 | * Support for changeable cursor shape | ||
53 | * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997 | ||
54 | * | ||
55 | * Ported to i386 and con_scrolldelta fixed | ||
56 | * by Emmanuel Marty <core@ggi-project.org>, April 1998 | ||
57 | * | ||
58 | * Resurrected character buffers in videoram plus lots of other trickery | ||
59 | * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998 | ||
60 | * | ||
61 | * Removed old-style timers, introduced console_timer, made timer | ||
62 | * deletion SMP-safe. 17Jun00, Andrew Morton | ||
63 | * | ||
64 | * Removed console_lock, enabled interrupts across all console operations | ||
65 | * 13 March 2001, Andrew Morton | ||
66 | * | ||
67 | * Fixed UTF-8 mode so alternate charset modes always work according | ||
68 | * to control sequences interpreted in do_con_trol function | ||
69 | * preserving backward VT100 semigraphics compatibility, | ||
70 | * malformed UTF sequences represented as sequences of replacement glyphs, | ||
71 | * original codes or '?' as a last resort if replacement glyph is undefined | ||
72 | * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006 | ||
73 | */ | ||
74 | |||
75 | #include <linux/module.h> | ||
76 | #include <linux/types.h> | ||
77 | #include <linux/sched.h> | ||
78 | #include <linux/tty.h> | ||
79 | #include <linux/tty_flip.h> | ||
80 | #include <linux/kernel.h> | ||
81 | #include <linux/string.h> | ||
82 | #include <linux/errno.h> | ||
83 | #include <linux/kd.h> | ||
84 | #include <linux/slab.h> | ||
85 | #include <linux/major.h> | ||
86 | #include <linux/mm.h> | ||
87 | #include <linux/console.h> | ||
88 | #include <linux/init.h> | ||
89 | #include <linux/mutex.h> | ||
90 | #include <linux/vt_kern.h> | ||
91 | #include <linux/selection.h> | ||
92 | #include <linux/smp_lock.h> | ||
93 | #include <linux/tiocl.h> | ||
94 | #include <linux/kbd_kern.h> | ||
95 | #include <linux/consolemap.h> | ||
96 | #include <linux/timer.h> | ||
97 | #include <linux/interrupt.h> | ||
98 | #include <linux/workqueue.h> | ||
99 | #include <linux/pm.h> | ||
100 | #include <linux/font.h> | ||
101 | #include <linux/bitops.h> | ||
102 | #include <linux/notifier.h> | ||
103 | #include <linux/device.h> | ||
104 | #include <linux/io.h> | ||
105 | #include <asm/system.h> | ||
106 | #include <linux/uaccess.h> | ||
107 | #include <linux/kdb.h> | ||
108 | #include <linux/ctype.h> | ||
109 | |||
110 | #define MAX_NR_CON_DRIVER 16 | ||
111 | |||
112 | #define CON_DRIVER_FLAG_MODULE 1 | ||
113 | #define CON_DRIVER_FLAG_INIT 2 | ||
114 | #define CON_DRIVER_FLAG_ATTR 4 | ||
115 | |||
116 | struct con_driver { | ||
117 | const struct consw *con; | ||
118 | const char *desc; | ||
119 | struct device *dev; | ||
120 | int node; | ||
121 | int first; | ||
122 | int last; | ||
123 | int flag; | ||
124 | }; | ||
125 | |||
126 | static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER]; | ||
127 | const struct consw *conswitchp; | ||
128 | |||
129 | /* A bitmap for codes <32. A bit of 1 indicates that the code | ||
130 | * corresponding to that bit number invokes some special action | ||
131 | * (such as cursor movement) and should not be displayed as a | ||
132 | * glyph unless the disp_ctrl mode is explicitly enabled. | ||
133 | */ | ||
134 | #define CTRL_ACTION 0x0d00ff81 | ||
135 | #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */ | ||
136 | |||
137 | /* | ||
138 | * Here is the default bell parameters: 750HZ, 1/8th of a second | ||
139 | */ | ||
140 | #define DEFAULT_BELL_PITCH 750 | ||
141 | #define DEFAULT_BELL_DURATION (HZ/8) | ||
142 | |||
143 | struct vc vc_cons [MAX_NR_CONSOLES]; | ||
144 | |||
145 | #ifndef VT_SINGLE_DRIVER | ||
146 | static const struct consw *con_driver_map[MAX_NR_CONSOLES]; | ||
147 | #endif | ||
148 | |||
149 | static int con_open(struct tty_struct *, struct file *); | ||
150 | static void vc_init(struct vc_data *vc, unsigned int rows, | ||
151 | unsigned int cols, int do_clear); | ||
152 | static void gotoxy(struct vc_data *vc, int new_x, int new_y); | ||
153 | static void save_cur(struct vc_data *vc); | ||
154 | static void reset_terminal(struct vc_data *vc, int do_clear); | ||
155 | static void con_flush_chars(struct tty_struct *tty); | ||
156 | static int set_vesa_blanking(char __user *p); | ||
157 | static void set_cursor(struct vc_data *vc); | ||
158 | static void hide_cursor(struct vc_data *vc); | ||
159 | static void console_callback(struct work_struct *ignored); | ||
160 | static void blank_screen_t(unsigned long dummy); | ||
161 | static void set_palette(struct vc_data *vc); | ||
162 | |||
163 | static int printable; /* Is console ready for printing? */ | ||
164 | int default_utf8 = true; | ||
165 | module_param(default_utf8, int, S_IRUGO | S_IWUSR); | ||
166 | int global_cursor_default = -1; | ||
167 | module_param(global_cursor_default, int, S_IRUGO | S_IWUSR); | ||
168 | |||
169 | static int cur_default = CUR_DEFAULT; | ||
170 | module_param(cur_default, int, S_IRUGO | S_IWUSR); | ||
171 | |||
172 | /* | ||
173 | * ignore_poke: don't unblank the screen when things are typed. This is | ||
174 | * mainly for the privacy of braille terminal users. | ||
175 | */ | ||
176 | static int ignore_poke; | ||
177 | |||
178 | int do_poke_blanked_console; | ||
179 | int console_blanked; | ||
180 | |||
181 | static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */ | ||
182 | static int vesa_off_interval; | ||
183 | static int blankinterval = 10*60; | ||
184 | core_param(consoleblank, blankinterval, int, 0444); | ||
185 | |||
186 | static DECLARE_WORK(console_work, console_callback); | ||
187 | |||
188 | /* | ||
189 | * fg_console is the current virtual console, | ||
190 | * last_console is the last used one, | ||
191 | * want_console is the console we want to switch to, | ||
192 | * saved_* variants are for save/restore around kernel debugger enter/leave | ||
193 | */ | ||
194 | int fg_console; | ||
195 | int last_console; | ||
196 | int want_console = -1; | ||
197 | static int saved_fg_console; | ||
198 | static int saved_last_console; | ||
199 | static int saved_want_console; | ||
200 | static int saved_vc_mode; | ||
201 | static int saved_console_blanked; | ||
202 | |||
203 | /* | ||
204 | * For each existing display, we have a pointer to console currently visible | ||
205 | * on that display, allowing consoles other than fg_console to be refreshed | ||
206 | * appropriately. Unless the low-level driver supplies its own display_fg | ||
207 | * variable, we use this one for the "master display". | ||
208 | */ | ||
209 | static struct vc_data *master_display_fg; | ||
210 | |||
211 | /* | ||
212 | * Unfortunately, we need to delay tty echo when we're currently writing to the | ||
213 | * console since the code is (and always was) not re-entrant, so we schedule | ||
214 | * all flip requests to process context with schedule-task() and run it from | ||
215 | * console_callback(). | ||
216 | */ | ||
217 | |||
218 | /* | ||
219 | * For the same reason, we defer scrollback to the console callback. | ||
220 | */ | ||
221 | static int scrollback_delta; | ||
222 | |||
223 | /* | ||
224 | * Hook so that the power management routines can (un)blank | ||
225 | * the console on our behalf. | ||
226 | */ | ||
227 | int (*console_blank_hook)(int); | ||
228 | |||
229 | static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0); | ||
230 | static int blank_state; | ||
231 | static int blank_timer_expired; | ||
232 | enum { | ||
233 | blank_off = 0, | ||
234 | blank_normal_wait, | ||
235 | blank_vesa_wait, | ||
236 | }; | ||
237 | |||
238 | /* | ||
239 | * Notifier list for console events. | ||
240 | */ | ||
241 | static ATOMIC_NOTIFIER_HEAD(vt_notifier_list); | ||
242 | |||
243 | int register_vt_notifier(struct notifier_block *nb) | ||
244 | { | ||
245 | return atomic_notifier_chain_register(&vt_notifier_list, nb); | ||
246 | } | ||
247 | EXPORT_SYMBOL_GPL(register_vt_notifier); | ||
248 | |||
249 | int unregister_vt_notifier(struct notifier_block *nb) | ||
250 | { | ||
251 | return atomic_notifier_chain_unregister(&vt_notifier_list, nb); | ||
252 | } | ||
253 | EXPORT_SYMBOL_GPL(unregister_vt_notifier); | ||
254 | |||
255 | static void notify_write(struct vc_data *vc, unsigned int unicode) | ||
256 | { | ||
257 | struct vt_notifier_param param = { .vc = vc, unicode = unicode }; | ||
258 | atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, ¶m); | ||
259 | } | ||
260 | |||
261 | static void notify_update(struct vc_data *vc) | ||
262 | { | ||
263 | struct vt_notifier_param param = { .vc = vc }; | ||
264 | atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, ¶m); | ||
265 | } | ||
266 | /* | ||
267 | * Low-Level Functions | ||
268 | */ | ||
269 | |||
270 | #define IS_FG(vc) ((vc)->vc_num == fg_console) | ||
271 | |||
272 | #ifdef VT_BUF_VRAM_ONLY | ||
273 | #define DO_UPDATE(vc) 0 | ||
274 | #else | ||
275 | #define DO_UPDATE(vc) (CON_IS_VISIBLE(vc) && !console_blanked) | ||
276 | #endif | ||
277 | |||
278 | static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed) | ||
279 | { | ||
280 | unsigned short *p; | ||
281 | |||
282 | if (!viewed) | ||
283 | p = (unsigned short *)(vc->vc_origin + offset); | ||
284 | else if (!vc->vc_sw->con_screen_pos) | ||
285 | p = (unsigned short *)(vc->vc_visible_origin + offset); | ||
286 | else | ||
287 | p = vc->vc_sw->con_screen_pos(vc, offset); | ||
288 | return p; | ||
289 | } | ||
290 | |||
291 | /* Called from the keyboard irq path.. */ | ||
292 | static inline void scrolldelta(int lines) | ||
293 | { | ||
294 | /* FIXME */ | ||
295 | /* scrolldelta needs some kind of consistency lock, but the BKL was | ||
296 | and still is not protecting versus the scheduled back end */ | ||
297 | scrollback_delta += lines; | ||
298 | schedule_console_callback(); | ||
299 | } | ||
300 | |||
301 | void schedule_console_callback(void) | ||
302 | { | ||
303 | schedule_work(&console_work); | ||
304 | } | ||
305 | |||
306 | static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr) | ||
307 | { | ||
308 | unsigned short *d, *s; | ||
309 | |||
310 | if (t+nr >= b) | ||
311 | nr = b - t - 1; | ||
312 | if (b > vc->vc_rows || t >= b || nr < 1) | ||
313 | return; | ||
314 | if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr)) | ||
315 | return; | ||
316 | d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t); | ||
317 | s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr)); | ||
318 | scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row); | ||
319 | scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char, | ||
320 | vc->vc_size_row * nr); | ||
321 | } | ||
322 | |||
323 | static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr) | ||
324 | { | ||
325 | unsigned short *s; | ||
326 | unsigned int step; | ||
327 | |||
328 | if (t+nr >= b) | ||
329 | nr = b - t - 1; | ||
330 | if (b > vc->vc_rows || t >= b || nr < 1) | ||
331 | return; | ||
332 | if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr)) | ||
333 | return; | ||
334 | s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t); | ||
335 | step = vc->vc_cols * nr; | ||
336 | scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row); | ||
337 | scr_memsetw(s, vc->vc_video_erase_char, 2 * step); | ||
338 | } | ||
339 | |||
340 | static void do_update_region(struct vc_data *vc, unsigned long start, int count) | ||
341 | { | ||
342 | #ifndef VT_BUF_VRAM_ONLY | ||
343 | unsigned int xx, yy, offset; | ||
344 | u16 *p; | ||
345 | |||
346 | p = (u16 *) start; | ||
347 | if (!vc->vc_sw->con_getxy) { | ||
348 | offset = (start - vc->vc_origin) / 2; | ||
349 | xx = offset % vc->vc_cols; | ||
350 | yy = offset / vc->vc_cols; | ||
351 | } else { | ||
352 | int nxx, nyy; | ||
353 | start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy); | ||
354 | xx = nxx; yy = nyy; | ||
355 | } | ||
356 | for(;;) { | ||
357 | u16 attrib = scr_readw(p) & 0xff00; | ||
358 | int startx = xx; | ||
359 | u16 *q = p; | ||
360 | while (xx < vc->vc_cols && count) { | ||
361 | if (attrib != (scr_readw(p) & 0xff00)) { | ||
362 | if (p > q) | ||
363 | vc->vc_sw->con_putcs(vc, q, p-q, yy, startx); | ||
364 | startx = xx; | ||
365 | q = p; | ||
366 | attrib = scr_readw(p) & 0xff00; | ||
367 | } | ||
368 | p++; | ||
369 | xx++; | ||
370 | count--; | ||
371 | } | ||
372 | if (p > q) | ||
373 | vc->vc_sw->con_putcs(vc, q, p-q, yy, startx); | ||
374 | if (!count) | ||
375 | break; | ||
376 | xx = 0; | ||
377 | yy++; | ||
378 | if (vc->vc_sw->con_getxy) { | ||
379 | p = (u16 *)start; | ||
380 | start = vc->vc_sw->con_getxy(vc, start, NULL, NULL); | ||
381 | } | ||
382 | } | ||
383 | #endif | ||
384 | } | ||
385 | |||
386 | void update_region(struct vc_data *vc, unsigned long start, int count) | ||
387 | { | ||
388 | WARN_CONSOLE_UNLOCKED(); | ||
389 | |||
390 | if (DO_UPDATE(vc)) { | ||
391 | hide_cursor(vc); | ||
392 | do_update_region(vc, start, count); | ||
393 | set_cursor(vc); | ||
394 | } | ||
395 | } | ||
396 | |||
397 | /* Structure of attributes is hardware-dependent */ | ||
398 | |||
399 | static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink, | ||
400 | u8 _underline, u8 _reverse, u8 _italic) | ||
401 | { | ||
402 | if (vc->vc_sw->con_build_attr) | ||
403 | return vc->vc_sw->con_build_attr(vc, _color, _intensity, | ||
404 | _blink, _underline, _reverse, _italic); | ||
405 | |||
406 | #ifndef VT_BUF_VRAM_ONLY | ||
407 | /* | ||
408 | * ++roman: I completely changed the attribute format for monochrome | ||
409 | * mode (!can_do_color). The formerly used MDA (monochrome display | ||
410 | * adapter) format didn't allow the combination of certain effects. | ||
411 | * Now the attribute is just a bit vector: | ||
412 | * Bit 0..1: intensity (0..2) | ||
413 | * Bit 2 : underline | ||
414 | * Bit 3 : reverse | ||
415 | * Bit 7 : blink | ||
416 | */ | ||
417 | { | ||
418 | u8 a = _color; | ||
419 | if (!vc->vc_can_do_color) | ||
420 | return _intensity | | ||
421 | (_italic ? 2 : 0) | | ||
422 | (_underline ? 4 : 0) | | ||
423 | (_reverse ? 8 : 0) | | ||
424 | (_blink ? 0x80 : 0); | ||
425 | if (_italic) | ||
426 | a = (a & 0xF0) | vc->vc_itcolor; | ||
427 | else if (_underline) | ||
428 | a = (a & 0xf0) | vc->vc_ulcolor; | ||
429 | else if (_intensity == 0) | ||
430 | a = (a & 0xf0) | vc->vc_ulcolor; | ||
431 | if (_reverse) | ||
432 | a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77); | ||
433 | if (_blink) | ||
434 | a ^= 0x80; | ||
435 | if (_intensity == 2) | ||
436 | a ^= 0x08; | ||
437 | if (vc->vc_hi_font_mask == 0x100) | ||
438 | a <<= 1; | ||
439 | return a; | ||
440 | } | ||
441 | #else | ||
442 | return 0; | ||
443 | #endif | ||
444 | } | ||
445 | |||
446 | static void update_attr(struct vc_data *vc) | ||
447 | { | ||
448 | vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity, | ||
449 | vc->vc_blink, vc->vc_underline, | ||
450 | vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic); | ||
451 | vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' '; | ||
452 | } | ||
453 | |||
454 | /* Note: inverting the screen twice should revert to the original state */ | ||
455 | void invert_screen(struct vc_data *vc, int offset, int count, int viewed) | ||
456 | { | ||
457 | unsigned short *p; | ||
458 | |||
459 | WARN_CONSOLE_UNLOCKED(); | ||
460 | |||
461 | count /= 2; | ||
462 | p = screenpos(vc, offset, viewed); | ||
463 | if (vc->vc_sw->con_invert_region) | ||
464 | vc->vc_sw->con_invert_region(vc, p, count); | ||
465 | #ifndef VT_BUF_VRAM_ONLY | ||
466 | else { | ||
467 | u16 *q = p; | ||
468 | int cnt = count; | ||
469 | u16 a; | ||
470 | |||
471 | if (!vc->vc_can_do_color) { | ||
472 | while (cnt--) { | ||
473 | a = scr_readw(q); | ||
474 | a ^= 0x0800; | ||
475 | scr_writew(a, q); | ||
476 | q++; | ||
477 | } | ||
478 | } else if (vc->vc_hi_font_mask == 0x100) { | ||
479 | while (cnt--) { | ||
480 | a = scr_readw(q); | ||
481 | a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4); | ||
482 | scr_writew(a, q); | ||
483 | q++; | ||
484 | } | ||
485 | } else { | ||
486 | while (cnt--) { | ||
487 | a = scr_readw(q); | ||
488 | a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4); | ||
489 | scr_writew(a, q); | ||
490 | q++; | ||
491 | } | ||
492 | } | ||
493 | } | ||
494 | #endif | ||
495 | if (DO_UPDATE(vc)) | ||
496 | do_update_region(vc, (unsigned long) p, count); | ||
497 | } | ||
498 | |||
499 | /* used by selection: complement pointer position */ | ||
500 | void complement_pos(struct vc_data *vc, int offset) | ||
501 | { | ||
502 | static int old_offset = -1; | ||
503 | static unsigned short old; | ||
504 | static unsigned short oldx, oldy; | ||
505 | |||
506 | WARN_CONSOLE_UNLOCKED(); | ||
507 | |||
508 | if (old_offset != -1 && old_offset >= 0 && | ||
509 | old_offset < vc->vc_screenbuf_size) { | ||
510 | scr_writew(old, screenpos(vc, old_offset, 1)); | ||
511 | if (DO_UPDATE(vc)) | ||
512 | vc->vc_sw->con_putc(vc, old, oldy, oldx); | ||
513 | } | ||
514 | |||
515 | old_offset = offset; | ||
516 | |||
517 | if (offset != -1 && offset >= 0 && | ||
518 | offset < vc->vc_screenbuf_size) { | ||
519 | unsigned short new; | ||
520 | unsigned short *p; | ||
521 | p = screenpos(vc, offset, 1); | ||
522 | old = scr_readw(p); | ||
523 | new = old ^ vc->vc_complement_mask; | ||
524 | scr_writew(new, p); | ||
525 | if (DO_UPDATE(vc)) { | ||
526 | oldx = (offset >> 1) % vc->vc_cols; | ||
527 | oldy = (offset >> 1) / vc->vc_cols; | ||
528 | vc->vc_sw->con_putc(vc, new, oldy, oldx); | ||
529 | } | ||
530 | } | ||
531 | |||
532 | } | ||
533 | |||
534 | static void insert_char(struct vc_data *vc, unsigned int nr) | ||
535 | { | ||
536 | unsigned short *p, *q = (unsigned short *)vc->vc_pos; | ||
537 | |||
538 | p = q + vc->vc_cols - nr - vc->vc_x; | ||
539 | while (--p >= q) | ||
540 | scr_writew(scr_readw(p), p + nr); | ||
541 | scr_memsetw(q, vc->vc_video_erase_char, nr * 2); | ||
542 | vc->vc_need_wrap = 0; | ||
543 | if (DO_UPDATE(vc)) { | ||
544 | unsigned short oldattr = vc->vc_attr; | ||
545 | vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1, | ||
546 | vc->vc_cols - vc->vc_x - nr); | ||
547 | vc->vc_attr = vc->vc_video_erase_char >> 8; | ||
548 | while (nr--) | ||
549 | vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr); | ||
550 | vc->vc_attr = oldattr; | ||
551 | } | ||
552 | } | ||
553 | |||
554 | static void delete_char(struct vc_data *vc, unsigned int nr) | ||
555 | { | ||
556 | unsigned int i = vc->vc_x; | ||
557 | unsigned short *p = (unsigned short *)vc->vc_pos; | ||
558 | |||
559 | while (++i <= vc->vc_cols - nr) { | ||
560 | scr_writew(scr_readw(p+nr), p); | ||
561 | p++; | ||
562 | } | ||
563 | scr_memsetw(p, vc->vc_video_erase_char, nr * 2); | ||
564 | vc->vc_need_wrap = 0; | ||
565 | if (DO_UPDATE(vc)) { | ||
566 | unsigned short oldattr = vc->vc_attr; | ||
567 | vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1, | ||
568 | vc->vc_cols - vc->vc_x - nr); | ||
569 | vc->vc_attr = vc->vc_video_erase_char >> 8; | ||
570 | while (nr--) | ||
571 | vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, | ||
572 | vc->vc_cols - 1 - nr); | ||
573 | vc->vc_attr = oldattr; | ||
574 | } | ||
575 | } | ||
576 | |||
577 | static int softcursor_original; | ||
578 | |||
579 | static void add_softcursor(struct vc_data *vc) | ||
580 | { | ||
581 | int i = scr_readw((u16 *) vc->vc_pos); | ||
582 | u32 type = vc->vc_cursor_type; | ||
583 | |||
584 | if (! (type & 0x10)) return; | ||
585 | if (softcursor_original != -1) return; | ||
586 | softcursor_original = i; | ||
587 | i |= ((type >> 8) & 0xff00 ); | ||
588 | i ^= ((type) & 0xff00 ); | ||
589 | if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000; | ||
590 | if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700; | ||
591 | scr_writew(i, (u16 *) vc->vc_pos); | ||
592 | if (DO_UPDATE(vc)) | ||
593 | vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x); | ||
594 | } | ||
595 | |||
596 | static void hide_softcursor(struct vc_data *vc) | ||
597 | { | ||
598 | if (softcursor_original != -1) { | ||
599 | scr_writew(softcursor_original, (u16 *)vc->vc_pos); | ||
600 | if (DO_UPDATE(vc)) | ||
601 | vc->vc_sw->con_putc(vc, softcursor_original, | ||
602 | vc->vc_y, vc->vc_x); | ||
603 | softcursor_original = -1; | ||
604 | } | ||
605 | } | ||
606 | |||
607 | static void hide_cursor(struct vc_data *vc) | ||
608 | { | ||
609 | if (vc == sel_cons) | ||
610 | clear_selection(); | ||
611 | vc->vc_sw->con_cursor(vc, CM_ERASE); | ||
612 | hide_softcursor(vc); | ||
613 | } | ||
614 | |||
615 | static void set_cursor(struct vc_data *vc) | ||
616 | { | ||
617 | if (!IS_FG(vc) || console_blanked || | ||
618 | vc->vc_mode == KD_GRAPHICS) | ||
619 | return; | ||
620 | if (vc->vc_deccm) { | ||
621 | if (vc == sel_cons) | ||
622 | clear_selection(); | ||
623 | add_softcursor(vc); | ||
624 | if ((vc->vc_cursor_type & 0x0f) != 1) | ||
625 | vc->vc_sw->con_cursor(vc, CM_DRAW); | ||
626 | } else | ||
627 | hide_cursor(vc); | ||
628 | } | ||
629 | |||
630 | static void set_origin(struct vc_data *vc) | ||
631 | { | ||
632 | WARN_CONSOLE_UNLOCKED(); | ||
633 | |||
634 | if (!CON_IS_VISIBLE(vc) || | ||
635 | !vc->vc_sw->con_set_origin || | ||
636 | !vc->vc_sw->con_set_origin(vc)) | ||
637 | vc->vc_origin = (unsigned long)vc->vc_screenbuf; | ||
638 | vc->vc_visible_origin = vc->vc_origin; | ||
639 | vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size; | ||
640 | vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x; | ||
641 | } | ||
642 | |||
643 | static inline void save_screen(struct vc_data *vc) | ||
644 | { | ||
645 | WARN_CONSOLE_UNLOCKED(); | ||
646 | |||
647 | if (vc->vc_sw->con_save_screen) | ||
648 | vc->vc_sw->con_save_screen(vc); | ||
649 | } | ||
650 | |||
651 | /* | ||
652 | * Redrawing of screen | ||
653 | */ | ||
654 | |||
655 | static void clear_buffer_attributes(struct vc_data *vc) | ||
656 | { | ||
657 | unsigned short *p = (unsigned short *)vc->vc_origin; | ||
658 | int count = vc->vc_screenbuf_size / 2; | ||
659 | int mask = vc->vc_hi_font_mask | 0xff; | ||
660 | |||
661 | for (; count > 0; count--, p++) { | ||
662 | scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p); | ||
663 | } | ||
664 | } | ||
665 | |||
666 | void redraw_screen(struct vc_data *vc, int is_switch) | ||
667 | { | ||
668 | int redraw = 0; | ||
669 | |||
670 | WARN_CONSOLE_UNLOCKED(); | ||
671 | |||
672 | if (!vc) { | ||
673 | /* strange ... */ | ||
674 | /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */ | ||
675 | return; | ||
676 | } | ||
677 | |||
678 | if (is_switch) { | ||
679 | struct vc_data *old_vc = vc_cons[fg_console].d; | ||
680 | if (old_vc == vc) | ||
681 | return; | ||
682 | if (!CON_IS_VISIBLE(vc)) | ||
683 | redraw = 1; | ||
684 | *vc->vc_display_fg = vc; | ||
685 | fg_console = vc->vc_num; | ||
686 | hide_cursor(old_vc); | ||
687 | if (!CON_IS_VISIBLE(old_vc)) { | ||
688 | save_screen(old_vc); | ||
689 | set_origin(old_vc); | ||
690 | } | ||
691 | } else { | ||
692 | hide_cursor(vc); | ||
693 | redraw = 1; | ||
694 | } | ||
695 | |||
696 | if (redraw) { | ||
697 | int update; | ||
698 | int old_was_color = vc->vc_can_do_color; | ||
699 | |||
700 | set_origin(vc); | ||
701 | update = vc->vc_sw->con_switch(vc); | ||
702 | set_palette(vc); | ||
703 | /* | ||
704 | * If console changed from mono<->color, the best we can do | ||
705 | * is to clear the buffer attributes. As it currently stands, | ||
706 | * rebuilding new attributes from the old buffer is not doable | ||
707 | * without overly complex code. | ||
708 | */ | ||
709 | if (old_was_color != vc->vc_can_do_color) { | ||
710 | update_attr(vc); | ||
711 | clear_buffer_attributes(vc); | ||
712 | } | ||
713 | |||
714 | /* Forcibly update if we're panicing */ | ||
715 | if ((update && vc->vc_mode != KD_GRAPHICS) || | ||
716 | vt_force_oops_output(vc)) | ||
717 | do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); | ||
718 | } | ||
719 | set_cursor(vc); | ||
720 | if (is_switch) { | ||
721 | set_leds(); | ||
722 | compute_shiftstate(); | ||
723 | notify_update(vc); | ||
724 | } | ||
725 | } | ||
726 | |||
727 | /* | ||
728 | * Allocation, freeing and resizing of VTs. | ||
729 | */ | ||
730 | |||
731 | int vc_cons_allocated(unsigned int i) | ||
732 | { | ||
733 | return (i < MAX_NR_CONSOLES && vc_cons[i].d); | ||
734 | } | ||
735 | |||
736 | static void visual_init(struct vc_data *vc, int num, int init) | ||
737 | { | ||
738 | /* ++Geert: vc->vc_sw->con_init determines console size */ | ||
739 | if (vc->vc_sw) | ||
740 | module_put(vc->vc_sw->owner); | ||
741 | vc->vc_sw = conswitchp; | ||
742 | #ifndef VT_SINGLE_DRIVER | ||
743 | if (con_driver_map[num]) | ||
744 | vc->vc_sw = con_driver_map[num]; | ||
745 | #endif | ||
746 | __module_get(vc->vc_sw->owner); | ||
747 | vc->vc_num = num; | ||
748 | vc->vc_display_fg = &master_display_fg; | ||
749 | vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir; | ||
750 | vc->vc_uni_pagedir = 0; | ||
751 | vc->vc_hi_font_mask = 0; | ||
752 | vc->vc_complement_mask = 0; | ||
753 | vc->vc_can_do_color = 0; | ||
754 | vc->vc_panic_force_write = false; | ||
755 | vc->vc_sw->con_init(vc, init); | ||
756 | if (!vc->vc_complement_mask) | ||
757 | vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800; | ||
758 | vc->vc_s_complement_mask = vc->vc_complement_mask; | ||
759 | vc->vc_size_row = vc->vc_cols << 1; | ||
760 | vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row; | ||
761 | } | ||
762 | |||
763 | int vc_allocate(unsigned int currcons) /* return 0 on success */ | ||
764 | { | ||
765 | WARN_CONSOLE_UNLOCKED(); | ||
766 | |||
767 | if (currcons >= MAX_NR_CONSOLES) | ||
768 | return -ENXIO; | ||
769 | if (!vc_cons[currcons].d) { | ||
770 | struct vc_data *vc; | ||
771 | struct vt_notifier_param param; | ||
772 | |||
773 | /* prevent users from taking too much memory */ | ||
774 | if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE)) | ||
775 | return -EPERM; | ||
776 | |||
777 | /* due to the granularity of kmalloc, we waste some memory here */ | ||
778 | /* the alloc is done in two steps, to optimize the common situation | ||
779 | of a 25x80 console (structsize=216, screenbuf_size=4000) */ | ||
780 | /* although the numbers above are not valid since long ago, the | ||
781 | point is still up-to-date and the comment still has its value | ||
782 | even if only as a historical artifact. --mj, July 1998 */ | ||
783 | param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL); | ||
784 | if (!vc) | ||
785 | return -ENOMEM; | ||
786 | vc_cons[currcons].d = vc; | ||
787 | tty_port_init(&vc->port); | ||
788 | INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); | ||
789 | visual_init(vc, currcons, 1); | ||
790 | if (!*vc->vc_uni_pagedir_loc) | ||
791 | con_set_default_unimap(vc); | ||
792 | vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL); | ||
793 | if (!vc->vc_screenbuf) { | ||
794 | kfree(vc); | ||
795 | vc_cons[currcons].d = NULL; | ||
796 | return -ENOMEM; | ||
797 | } | ||
798 | |||
799 | /* If no drivers have overridden us and the user didn't pass a | ||
800 | boot option, default to displaying the cursor */ | ||
801 | if (global_cursor_default == -1) | ||
802 | global_cursor_default = 1; | ||
803 | |||
804 | vc_init(vc, vc->vc_rows, vc->vc_cols, 1); | ||
805 | vcs_make_sysfs(currcons); | ||
806 | atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, ¶m); | ||
807 | } | ||
808 | return 0; | ||
809 | } | ||
810 | |||
811 | static inline int resize_screen(struct vc_data *vc, int width, int height, | ||
812 | int user) | ||
813 | { | ||
814 | /* Resizes the resolution of the display adapater */ | ||
815 | int err = 0; | ||
816 | |||
817 | if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize) | ||
818 | err = vc->vc_sw->con_resize(vc, width, height, user); | ||
819 | |||
820 | return err; | ||
821 | } | ||
822 | |||
823 | /* | ||
824 | * Change # of rows and columns (0 means unchanged/the size of fg_console) | ||
825 | * [this is to be used together with some user program | ||
826 | * like resize that changes the hardware videomode] | ||
827 | */ | ||
828 | #define VC_RESIZE_MAXCOL (32767) | ||
829 | #define VC_RESIZE_MAXROW (32767) | ||
830 | |||
831 | /** | ||
832 | * vc_do_resize - resizing method for the tty | ||
833 | * @tty: tty being resized | ||
834 | * @real_tty: real tty (different to tty if a pty/tty pair) | ||
835 | * @vc: virtual console private data | ||
836 | * @cols: columns | ||
837 | * @lines: lines | ||
838 | * | ||
839 | * Resize a virtual console, clipping according to the actual constraints. | ||
840 | * If the caller passes a tty structure then update the termios winsize | ||
841 | * information and perform any necessary signal handling. | ||
842 | * | ||
843 | * Caller must hold the console semaphore. Takes the termios mutex and | ||
844 | * ctrl_lock of the tty IFF a tty is passed. | ||
845 | */ | ||
846 | |||
847 | static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc, | ||
848 | unsigned int cols, unsigned int lines) | ||
849 | { | ||
850 | unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0; | ||
851 | unsigned long end; | ||
852 | unsigned int old_cols, old_rows, old_row_size, old_screen_size; | ||
853 | unsigned int new_cols, new_rows, new_row_size, new_screen_size; | ||
854 | unsigned int user; | ||
855 | unsigned short *newscreen; | ||
856 | |||
857 | WARN_CONSOLE_UNLOCKED(); | ||
858 | |||
859 | if (!vc) | ||
860 | return -ENXIO; | ||
861 | |||
862 | user = vc->vc_resize_user; | ||
863 | vc->vc_resize_user = 0; | ||
864 | |||
865 | if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW) | ||
866 | return -EINVAL; | ||
867 | |||
868 | new_cols = (cols ? cols : vc->vc_cols); | ||
869 | new_rows = (lines ? lines : vc->vc_rows); | ||
870 | new_row_size = new_cols << 1; | ||
871 | new_screen_size = new_row_size * new_rows; | ||
872 | |||
873 | if (new_cols == vc->vc_cols && new_rows == vc->vc_rows) | ||
874 | return 0; | ||
875 | |||
876 | newscreen = kmalloc(new_screen_size, GFP_USER); | ||
877 | if (!newscreen) | ||
878 | return -ENOMEM; | ||
879 | |||
880 | old_rows = vc->vc_rows; | ||
881 | old_cols = vc->vc_cols; | ||
882 | old_row_size = vc->vc_size_row; | ||
883 | old_screen_size = vc->vc_screenbuf_size; | ||
884 | |||
885 | err = resize_screen(vc, new_cols, new_rows, user); | ||
886 | if (err) { | ||
887 | kfree(newscreen); | ||
888 | return err; | ||
889 | } | ||
890 | |||
891 | vc->vc_rows = new_rows; | ||
892 | vc->vc_cols = new_cols; | ||
893 | vc->vc_size_row = new_row_size; | ||
894 | vc->vc_screenbuf_size = new_screen_size; | ||
895 | |||
896 | rlth = min(old_row_size, new_row_size); | ||
897 | rrem = new_row_size - rlth; | ||
898 | old_origin = vc->vc_origin; | ||
899 | new_origin = (long) newscreen; | ||
900 | new_scr_end = new_origin + new_screen_size; | ||
901 | |||
902 | if (vc->vc_y > new_rows) { | ||
903 | if (old_rows - vc->vc_y < new_rows) { | ||
904 | /* | ||
905 | * Cursor near the bottom, copy contents from the | ||
906 | * bottom of buffer | ||
907 | */ | ||
908 | old_origin += (old_rows - new_rows) * old_row_size; | ||
909 | } else { | ||
910 | /* | ||
911 | * Cursor is in no man's land, copy 1/2 screenful | ||
912 | * from the top and bottom of cursor position | ||
913 | */ | ||
914 | old_origin += (vc->vc_y - new_rows/2) * old_row_size; | ||
915 | } | ||
916 | } | ||
917 | |||
918 | end = old_origin + old_row_size * min(old_rows, new_rows); | ||
919 | |||
920 | update_attr(vc); | ||
921 | |||
922 | while (old_origin < end) { | ||
923 | scr_memcpyw((unsigned short *) new_origin, | ||
924 | (unsigned short *) old_origin, rlth); | ||
925 | if (rrem) | ||
926 | scr_memsetw((void *)(new_origin + rlth), | ||
927 | vc->vc_video_erase_char, rrem); | ||
928 | old_origin += old_row_size; | ||
929 | new_origin += new_row_size; | ||
930 | } | ||
931 | if (new_scr_end > new_origin) | ||
932 | scr_memsetw((void *)new_origin, vc->vc_video_erase_char, | ||
933 | new_scr_end - new_origin); | ||
934 | kfree(vc->vc_screenbuf); | ||
935 | vc->vc_screenbuf = newscreen; | ||
936 | vc->vc_screenbuf_size = new_screen_size; | ||
937 | set_origin(vc); | ||
938 | |||
939 | /* do part of a reset_terminal() */ | ||
940 | vc->vc_top = 0; | ||
941 | vc->vc_bottom = vc->vc_rows; | ||
942 | gotoxy(vc, vc->vc_x, vc->vc_y); | ||
943 | save_cur(vc); | ||
944 | |||
945 | if (tty) { | ||
946 | /* Rewrite the requested winsize data with the actual | ||
947 | resulting sizes */ | ||
948 | struct winsize ws; | ||
949 | memset(&ws, 0, sizeof(ws)); | ||
950 | ws.ws_row = vc->vc_rows; | ||
951 | ws.ws_col = vc->vc_cols; | ||
952 | ws.ws_ypixel = vc->vc_scan_lines; | ||
953 | tty_do_resize(tty, &ws); | ||
954 | } | ||
955 | |||
956 | if (CON_IS_VISIBLE(vc)) | ||
957 | update_screen(vc); | ||
958 | vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num); | ||
959 | return err; | ||
960 | } | ||
961 | |||
962 | /** | ||
963 | * vc_resize - resize a VT | ||
964 | * @vc: virtual console | ||
965 | * @cols: columns | ||
966 | * @rows: rows | ||
967 | * | ||
968 | * Resize a virtual console as seen from the console end of things. We | ||
969 | * use the common vc_do_resize methods to update the structures. The | ||
970 | * caller must hold the console sem to protect console internals and | ||
971 | * vc->port.tty | ||
972 | */ | ||
973 | |||
974 | int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows) | ||
975 | { | ||
976 | return vc_do_resize(vc->port.tty, vc, cols, rows); | ||
977 | } | ||
978 | |||
979 | /** | ||
980 | * vt_resize - resize a VT | ||
981 | * @tty: tty to resize | ||
982 | * @ws: winsize attributes | ||
983 | * | ||
984 | * Resize a virtual terminal. This is called by the tty layer as we | ||
985 | * register our own handler for resizing. The mutual helper does all | ||
986 | * the actual work. | ||
987 | * | ||
988 | * Takes the console sem and the called methods then take the tty | ||
989 | * termios_mutex and the tty ctrl_lock in that order. | ||
990 | */ | ||
991 | static int vt_resize(struct tty_struct *tty, struct winsize *ws) | ||
992 | { | ||
993 | struct vc_data *vc = tty->driver_data; | ||
994 | int ret; | ||
995 | |||
996 | acquire_console_sem(); | ||
997 | ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row); | ||
998 | release_console_sem(); | ||
999 | return ret; | ||
1000 | } | ||
1001 | |||
1002 | void vc_deallocate(unsigned int currcons) | ||
1003 | { | ||
1004 | WARN_CONSOLE_UNLOCKED(); | ||
1005 | |||
1006 | if (vc_cons_allocated(currcons)) { | ||
1007 | struct vc_data *vc = vc_cons[currcons].d; | ||
1008 | struct vt_notifier_param param = { .vc = vc }; | ||
1009 | |||
1010 | atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, ¶m); | ||
1011 | vcs_remove_sysfs(currcons); | ||
1012 | vc->vc_sw->con_deinit(vc); | ||
1013 | put_pid(vc->vt_pid); | ||
1014 | module_put(vc->vc_sw->owner); | ||
1015 | kfree(vc->vc_screenbuf); | ||
1016 | if (currcons >= MIN_NR_CONSOLES) | ||
1017 | kfree(vc); | ||
1018 | vc_cons[currcons].d = NULL; | ||
1019 | } | ||
1020 | } | ||
1021 | |||
1022 | /* | ||
1023 | * VT102 emulator | ||
1024 | */ | ||
1025 | |||
1026 | #define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x)) | ||
1027 | #define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x)) | ||
1028 | #define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x)) | ||
1029 | |||
1030 | #define decarm VC_REPEAT | ||
1031 | #define decckm VC_CKMODE | ||
1032 | #define kbdapplic VC_APPLIC | ||
1033 | #define lnm VC_CRLF | ||
1034 | |||
1035 | /* | ||
1036 | * this is what the terminal answers to a ESC-Z or csi0c query. | ||
1037 | */ | ||
1038 | #define VT100ID "\033[?1;2c" | ||
1039 | #define VT102ID "\033[?6c" | ||
1040 | |||
1041 | unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7, | ||
1042 | 8,12,10,14, 9,13,11,15 }; | ||
1043 | |||
1044 | /* the default colour table, for VGA+ colour systems */ | ||
1045 | int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa, | ||
1046 | 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff}; | ||
1047 | int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa, | ||
1048 | 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff}; | ||
1049 | int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa, | ||
1050 | 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff}; | ||
1051 | |||
1052 | module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR); | ||
1053 | module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR); | ||
1054 | module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR); | ||
1055 | |||
1056 | /* | ||
1057 | * gotoxy() must verify all boundaries, because the arguments | ||
1058 | * might also be negative. If the given position is out of | ||
1059 | * bounds, the cursor is placed at the nearest margin. | ||
1060 | */ | ||
1061 | static void gotoxy(struct vc_data *vc, int new_x, int new_y) | ||
1062 | { | ||
1063 | int min_y, max_y; | ||
1064 | |||
1065 | if (new_x < 0) | ||
1066 | vc->vc_x = 0; | ||
1067 | else { | ||
1068 | if (new_x >= vc->vc_cols) | ||
1069 | vc->vc_x = vc->vc_cols - 1; | ||
1070 | else | ||
1071 | vc->vc_x = new_x; | ||
1072 | } | ||
1073 | |||
1074 | if (vc->vc_decom) { | ||
1075 | min_y = vc->vc_top; | ||
1076 | max_y = vc->vc_bottom; | ||
1077 | } else { | ||
1078 | min_y = 0; | ||
1079 | max_y = vc->vc_rows; | ||
1080 | } | ||
1081 | if (new_y < min_y) | ||
1082 | vc->vc_y = min_y; | ||
1083 | else if (new_y >= max_y) | ||
1084 | vc->vc_y = max_y - 1; | ||
1085 | else | ||
1086 | vc->vc_y = new_y; | ||
1087 | vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1); | ||
1088 | vc->vc_need_wrap = 0; | ||
1089 | } | ||
1090 | |||
1091 | /* for absolute user moves, when decom is set */ | ||
1092 | static void gotoxay(struct vc_data *vc, int new_x, int new_y) | ||
1093 | { | ||
1094 | gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y); | ||
1095 | } | ||
1096 | |||
1097 | void scrollback(struct vc_data *vc, int lines) | ||
1098 | { | ||
1099 | if (!lines) | ||
1100 | lines = vc->vc_rows / 2; | ||
1101 | scrolldelta(-lines); | ||
1102 | } | ||
1103 | |||
1104 | void scrollfront(struct vc_data *vc, int lines) | ||
1105 | { | ||
1106 | if (!lines) | ||
1107 | lines = vc->vc_rows / 2; | ||
1108 | scrolldelta(lines); | ||
1109 | } | ||
1110 | |||
1111 | static void lf(struct vc_data *vc) | ||
1112 | { | ||
1113 | /* don't scroll if above bottom of scrolling region, or | ||
1114 | * if below scrolling region | ||
1115 | */ | ||
1116 | if (vc->vc_y + 1 == vc->vc_bottom) | ||
1117 | scrup(vc, vc->vc_top, vc->vc_bottom, 1); | ||
1118 | else if (vc->vc_y < vc->vc_rows - 1) { | ||
1119 | vc->vc_y++; | ||
1120 | vc->vc_pos += vc->vc_size_row; | ||
1121 | } | ||
1122 | vc->vc_need_wrap = 0; | ||
1123 | notify_write(vc, '\n'); | ||
1124 | } | ||
1125 | |||
1126 | static void ri(struct vc_data *vc) | ||
1127 | { | ||
1128 | /* don't scroll if below top of scrolling region, or | ||
1129 | * if above scrolling region | ||
1130 | */ | ||
1131 | if (vc->vc_y == vc->vc_top) | ||
1132 | scrdown(vc, vc->vc_top, vc->vc_bottom, 1); | ||
1133 | else if (vc->vc_y > 0) { | ||
1134 | vc->vc_y--; | ||
1135 | vc->vc_pos -= vc->vc_size_row; | ||
1136 | } | ||
1137 | vc->vc_need_wrap = 0; | ||
1138 | } | ||
1139 | |||
1140 | static inline void cr(struct vc_data *vc) | ||
1141 | { | ||
1142 | vc->vc_pos -= vc->vc_x << 1; | ||
1143 | vc->vc_need_wrap = vc->vc_x = 0; | ||
1144 | notify_write(vc, '\r'); | ||
1145 | } | ||
1146 | |||
1147 | static inline void bs(struct vc_data *vc) | ||
1148 | { | ||
1149 | if (vc->vc_x) { | ||
1150 | vc->vc_pos -= 2; | ||
1151 | vc->vc_x--; | ||
1152 | vc->vc_need_wrap = 0; | ||
1153 | notify_write(vc, '\b'); | ||
1154 | } | ||
1155 | } | ||
1156 | |||
1157 | static inline void del(struct vc_data *vc) | ||
1158 | { | ||
1159 | /* ignored */ | ||
1160 | } | ||
1161 | |||
1162 | static void csi_J(struct vc_data *vc, int vpar) | ||
1163 | { | ||
1164 | unsigned int count; | ||
1165 | unsigned short * start; | ||
1166 | |||
1167 | switch (vpar) { | ||
1168 | case 0: /* erase from cursor to end of display */ | ||
1169 | count = (vc->vc_scr_end - vc->vc_pos) >> 1; | ||
1170 | start = (unsigned short *)vc->vc_pos; | ||
1171 | if (DO_UPDATE(vc)) { | ||
1172 | /* do in two stages */ | ||
1173 | vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, | ||
1174 | vc->vc_cols - vc->vc_x); | ||
1175 | vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0, | ||
1176 | vc->vc_rows - vc->vc_y - 1, | ||
1177 | vc->vc_cols); | ||
1178 | } | ||
1179 | break; | ||
1180 | case 1: /* erase from start to cursor */ | ||
1181 | count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1; | ||
1182 | start = (unsigned short *)vc->vc_origin; | ||
1183 | if (DO_UPDATE(vc)) { | ||
1184 | /* do in two stages */ | ||
1185 | vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y, | ||
1186 | vc->vc_cols); | ||
1187 | vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1, | ||
1188 | vc->vc_x + 1); | ||
1189 | } | ||
1190 | break; | ||
1191 | case 2: /* erase whole display */ | ||
1192 | count = vc->vc_cols * vc->vc_rows; | ||
1193 | start = (unsigned short *)vc->vc_origin; | ||
1194 | if (DO_UPDATE(vc)) | ||
1195 | vc->vc_sw->con_clear(vc, 0, 0, | ||
1196 | vc->vc_rows, | ||
1197 | vc->vc_cols); | ||
1198 | break; | ||
1199 | default: | ||
1200 | return; | ||
1201 | } | ||
1202 | scr_memsetw(start, vc->vc_video_erase_char, 2 * count); | ||
1203 | vc->vc_need_wrap = 0; | ||
1204 | } | ||
1205 | |||
1206 | static void csi_K(struct vc_data *vc, int vpar) | ||
1207 | { | ||
1208 | unsigned int count; | ||
1209 | unsigned short * start; | ||
1210 | |||
1211 | switch (vpar) { | ||
1212 | case 0: /* erase from cursor to end of line */ | ||
1213 | count = vc->vc_cols - vc->vc_x; | ||
1214 | start = (unsigned short *)vc->vc_pos; | ||
1215 | if (DO_UPDATE(vc)) | ||
1216 | vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, | ||
1217 | vc->vc_cols - vc->vc_x); | ||
1218 | break; | ||
1219 | case 1: /* erase from start of line to cursor */ | ||
1220 | start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1)); | ||
1221 | count = vc->vc_x + 1; | ||
1222 | if (DO_UPDATE(vc)) | ||
1223 | vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1, | ||
1224 | vc->vc_x + 1); | ||
1225 | break; | ||
1226 | case 2: /* erase whole line */ | ||
1227 | start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1)); | ||
1228 | count = vc->vc_cols; | ||
1229 | if (DO_UPDATE(vc)) | ||
1230 | vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1, | ||
1231 | vc->vc_cols); | ||
1232 | break; | ||
1233 | default: | ||
1234 | return; | ||
1235 | } | ||
1236 | scr_memsetw(start, vc->vc_video_erase_char, 2 * count); | ||
1237 | vc->vc_need_wrap = 0; | ||
1238 | } | ||
1239 | |||
1240 | static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */ | ||
1241 | { /* not vt100? */ | ||
1242 | int count; | ||
1243 | |||
1244 | if (!vpar) | ||
1245 | vpar++; | ||
1246 | count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar; | ||
1247 | |||
1248 | scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count); | ||
1249 | if (DO_UPDATE(vc)) | ||
1250 | vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count); | ||
1251 | vc->vc_need_wrap = 0; | ||
1252 | } | ||
1253 | |||
1254 | static void default_attr(struct vc_data *vc) | ||
1255 | { | ||
1256 | vc->vc_intensity = 1; | ||
1257 | vc->vc_italic = 0; | ||
1258 | vc->vc_underline = 0; | ||
1259 | vc->vc_reverse = 0; | ||
1260 | vc->vc_blink = 0; | ||
1261 | vc->vc_color = vc->vc_def_color; | ||
1262 | } | ||
1263 | |||
1264 | /* console_sem is held */ | ||
1265 | static void csi_m(struct vc_data *vc) | ||
1266 | { | ||
1267 | int i; | ||
1268 | |||
1269 | for (i = 0; i <= vc->vc_npar; i++) | ||
1270 | switch (vc->vc_par[i]) { | ||
1271 | case 0: /* all attributes off */ | ||
1272 | default_attr(vc); | ||
1273 | break; | ||
1274 | case 1: | ||
1275 | vc->vc_intensity = 2; | ||
1276 | break; | ||
1277 | case 2: | ||
1278 | vc->vc_intensity = 0; | ||
1279 | break; | ||
1280 | case 3: | ||
1281 | vc->vc_italic = 1; | ||
1282 | break; | ||
1283 | case 4: | ||
1284 | vc->vc_underline = 1; | ||
1285 | break; | ||
1286 | case 5: | ||
1287 | vc->vc_blink = 1; | ||
1288 | break; | ||
1289 | case 7: | ||
1290 | vc->vc_reverse = 1; | ||
1291 | break; | ||
1292 | case 10: /* ANSI X3.64-1979 (SCO-ish?) | ||
1293 | * Select primary font, don't display | ||
1294 | * control chars if defined, don't set | ||
1295 | * bit 8 on output. | ||
1296 | */ | ||
1297 | vc->vc_translate = set_translate(vc->vc_charset == 0 | ||
1298 | ? vc->vc_G0_charset | ||
1299 | : vc->vc_G1_charset, vc); | ||
1300 | vc->vc_disp_ctrl = 0; | ||
1301 | vc->vc_toggle_meta = 0; | ||
1302 | break; | ||
1303 | case 11: /* ANSI X3.64-1979 (SCO-ish?) | ||
1304 | * Select first alternate font, lets | ||
1305 | * chars < 32 be displayed as ROM chars. | ||
1306 | */ | ||
1307 | vc->vc_translate = set_translate(IBMPC_MAP, vc); | ||
1308 | vc->vc_disp_ctrl = 1; | ||
1309 | vc->vc_toggle_meta = 0; | ||
1310 | break; | ||
1311 | case 12: /* ANSI X3.64-1979 (SCO-ish?) | ||
1312 | * Select second alternate font, toggle | ||
1313 | * high bit before displaying as ROM char. | ||
1314 | */ | ||
1315 | vc->vc_translate = set_translate(IBMPC_MAP, vc); | ||
1316 | vc->vc_disp_ctrl = 1; | ||
1317 | vc->vc_toggle_meta = 1; | ||
1318 | break; | ||
1319 | case 21: | ||
1320 | case 22: | ||
1321 | vc->vc_intensity = 1; | ||
1322 | break; | ||
1323 | case 23: | ||
1324 | vc->vc_italic = 0; | ||
1325 | break; | ||
1326 | case 24: | ||
1327 | vc->vc_underline = 0; | ||
1328 | break; | ||
1329 | case 25: | ||
1330 | vc->vc_blink = 0; | ||
1331 | break; | ||
1332 | case 27: | ||
1333 | vc->vc_reverse = 0; | ||
1334 | break; | ||
1335 | case 38: /* ANSI X3.64-1979 (SCO-ish?) | ||
1336 | * Enables underscore, white foreground | ||
1337 | * with white underscore (Linux - use | ||
1338 | * default foreground). | ||
1339 | */ | ||
1340 | vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0); | ||
1341 | vc->vc_underline = 1; | ||
1342 | break; | ||
1343 | case 39: /* ANSI X3.64-1979 (SCO-ish?) | ||
1344 | * Disable underline option. | ||
1345 | * Reset colour to default? It did this | ||
1346 | * before... | ||
1347 | */ | ||
1348 | vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0); | ||
1349 | vc->vc_underline = 0; | ||
1350 | break; | ||
1351 | case 49: | ||
1352 | vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f); | ||
1353 | break; | ||
1354 | default: | ||
1355 | if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37) | ||
1356 | vc->vc_color = color_table[vc->vc_par[i] - 30] | ||
1357 | | (vc->vc_color & 0xf0); | ||
1358 | else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47) | ||
1359 | vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4) | ||
1360 | | (vc->vc_color & 0x0f); | ||
1361 | break; | ||
1362 | } | ||
1363 | update_attr(vc); | ||
1364 | } | ||
1365 | |||
1366 | static void respond_string(const char *p, struct tty_struct *tty) | ||
1367 | { | ||
1368 | while (*p) { | ||
1369 | tty_insert_flip_char(tty, *p, 0); | ||
1370 | p++; | ||
1371 | } | ||
1372 | con_schedule_flip(tty); | ||
1373 | } | ||
1374 | |||
1375 | static void cursor_report(struct vc_data *vc, struct tty_struct *tty) | ||
1376 | { | ||
1377 | char buf[40]; | ||
1378 | |||
1379 | sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1); | ||
1380 | respond_string(buf, tty); | ||
1381 | } | ||
1382 | |||
1383 | static inline void status_report(struct tty_struct *tty) | ||
1384 | { | ||
1385 | respond_string("\033[0n", tty); /* Terminal ok */ | ||
1386 | } | ||
1387 | |||
1388 | static inline void respond_ID(struct tty_struct * tty) | ||
1389 | { | ||
1390 | respond_string(VT102ID, tty); | ||
1391 | } | ||
1392 | |||
1393 | void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry) | ||
1394 | { | ||
1395 | char buf[8]; | ||
1396 | |||
1397 | sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx), | ||
1398 | (char)('!' + mry)); | ||
1399 | respond_string(buf, tty); | ||
1400 | } | ||
1401 | |||
1402 | /* invoked via ioctl(TIOCLINUX) and through set_selection */ | ||
1403 | int mouse_reporting(void) | ||
1404 | { | ||
1405 | return vc_cons[fg_console].d->vc_report_mouse; | ||
1406 | } | ||
1407 | |||
1408 | /* console_sem is held */ | ||
1409 | static void set_mode(struct vc_data *vc, int on_off) | ||
1410 | { | ||
1411 | int i; | ||
1412 | |||
1413 | for (i = 0; i <= vc->vc_npar; i++) | ||
1414 | if (vc->vc_ques) { | ||
1415 | switch(vc->vc_par[i]) { /* DEC private modes set/reset */ | ||
1416 | case 1: /* Cursor keys send ^[Ox/^[[x */ | ||
1417 | if (on_off) | ||
1418 | set_kbd(vc, decckm); | ||
1419 | else | ||
1420 | clr_kbd(vc, decckm); | ||
1421 | break; | ||
1422 | case 3: /* 80/132 mode switch unimplemented */ | ||
1423 | vc->vc_deccolm = on_off; | ||
1424 | #if 0 | ||
1425 | vc_resize(deccolm ? 132 : 80, vc->vc_rows); | ||
1426 | /* this alone does not suffice; some user mode | ||
1427 | utility has to change the hardware regs */ | ||
1428 | #endif | ||
1429 | break; | ||
1430 | case 5: /* Inverted screen on/off */ | ||
1431 | if (vc->vc_decscnm != on_off) { | ||
1432 | vc->vc_decscnm = on_off; | ||
1433 | invert_screen(vc, 0, vc->vc_screenbuf_size, 0); | ||
1434 | update_attr(vc); | ||
1435 | } | ||
1436 | break; | ||
1437 | case 6: /* Origin relative/absolute */ | ||
1438 | vc->vc_decom = on_off; | ||
1439 | gotoxay(vc, 0, 0); | ||
1440 | break; | ||
1441 | case 7: /* Autowrap on/off */ | ||
1442 | vc->vc_decawm = on_off; | ||
1443 | break; | ||
1444 | case 8: /* Autorepeat on/off */ | ||
1445 | if (on_off) | ||
1446 | set_kbd(vc, decarm); | ||
1447 | else | ||
1448 | clr_kbd(vc, decarm); | ||
1449 | break; | ||
1450 | case 9: | ||
1451 | vc->vc_report_mouse = on_off ? 1 : 0; | ||
1452 | break; | ||
1453 | case 25: /* Cursor on/off */ | ||
1454 | vc->vc_deccm = on_off; | ||
1455 | break; | ||
1456 | case 1000: | ||
1457 | vc->vc_report_mouse = on_off ? 2 : 0; | ||
1458 | break; | ||
1459 | } | ||
1460 | } else { | ||
1461 | switch(vc->vc_par[i]) { /* ANSI modes set/reset */ | ||
1462 | case 3: /* Monitor (display ctrls) */ | ||
1463 | vc->vc_disp_ctrl = on_off; | ||
1464 | break; | ||
1465 | case 4: /* Insert Mode on/off */ | ||
1466 | vc->vc_decim = on_off; | ||
1467 | break; | ||
1468 | case 20: /* Lf, Enter == CrLf/Lf */ | ||
1469 | if (on_off) | ||
1470 | set_kbd(vc, lnm); | ||
1471 | else | ||
1472 | clr_kbd(vc, lnm); | ||
1473 | break; | ||
1474 | } | ||
1475 | } | ||
1476 | } | ||
1477 | |||
1478 | /* console_sem is held */ | ||
1479 | static void setterm_command(struct vc_data *vc) | ||
1480 | { | ||
1481 | switch(vc->vc_par[0]) { | ||
1482 | case 1: /* set color for underline mode */ | ||
1483 | if (vc->vc_can_do_color && | ||
1484 | vc->vc_par[1] < 16) { | ||
1485 | vc->vc_ulcolor = color_table[vc->vc_par[1]]; | ||
1486 | if (vc->vc_underline) | ||
1487 | update_attr(vc); | ||
1488 | } | ||
1489 | break; | ||
1490 | case 2: /* set color for half intensity mode */ | ||
1491 | if (vc->vc_can_do_color && | ||
1492 | vc->vc_par[1] < 16) { | ||
1493 | vc->vc_halfcolor = color_table[vc->vc_par[1]]; | ||
1494 | if (vc->vc_intensity == 0) | ||
1495 | update_attr(vc); | ||
1496 | } | ||
1497 | break; | ||
1498 | case 8: /* store colors as defaults */ | ||
1499 | vc->vc_def_color = vc->vc_attr; | ||
1500 | if (vc->vc_hi_font_mask == 0x100) | ||
1501 | vc->vc_def_color >>= 1; | ||
1502 | default_attr(vc); | ||
1503 | update_attr(vc); | ||
1504 | break; | ||
1505 | case 9: /* set blanking interval */ | ||
1506 | blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60; | ||
1507 | poke_blanked_console(); | ||
1508 | break; | ||
1509 | case 10: /* set bell frequency in Hz */ | ||
1510 | if (vc->vc_npar >= 1) | ||
1511 | vc->vc_bell_pitch = vc->vc_par[1]; | ||
1512 | else | ||
1513 | vc->vc_bell_pitch = DEFAULT_BELL_PITCH; | ||
1514 | break; | ||
1515 | case 11: /* set bell duration in msec */ | ||
1516 | if (vc->vc_npar >= 1) | ||
1517 | vc->vc_bell_duration = (vc->vc_par[1] < 2000) ? | ||
1518 | vc->vc_par[1] * HZ / 1000 : 0; | ||
1519 | else | ||
1520 | vc->vc_bell_duration = DEFAULT_BELL_DURATION; | ||
1521 | break; | ||
1522 | case 12: /* bring specified console to the front */ | ||
1523 | if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1)) | ||
1524 | set_console(vc->vc_par[1] - 1); | ||
1525 | break; | ||
1526 | case 13: /* unblank the screen */ | ||
1527 | poke_blanked_console(); | ||
1528 | break; | ||
1529 | case 14: /* set vesa powerdown interval */ | ||
1530 | vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ; | ||
1531 | break; | ||
1532 | case 15: /* activate the previous console */ | ||
1533 | set_console(last_console); | ||
1534 | break; | ||
1535 | } | ||
1536 | } | ||
1537 | |||
1538 | /* console_sem is held */ | ||
1539 | static void csi_at(struct vc_data *vc, unsigned int nr) | ||
1540 | { | ||
1541 | if (nr > vc->vc_cols - vc->vc_x) | ||
1542 | nr = vc->vc_cols - vc->vc_x; | ||
1543 | else if (!nr) | ||
1544 | nr = 1; | ||
1545 | insert_char(vc, nr); | ||
1546 | } | ||
1547 | |||
1548 | /* console_sem is held */ | ||
1549 | static void csi_L(struct vc_data *vc, unsigned int nr) | ||
1550 | { | ||
1551 | if (nr > vc->vc_rows - vc->vc_y) | ||
1552 | nr = vc->vc_rows - vc->vc_y; | ||
1553 | else if (!nr) | ||
1554 | nr = 1; | ||
1555 | scrdown(vc, vc->vc_y, vc->vc_bottom, nr); | ||
1556 | vc->vc_need_wrap = 0; | ||
1557 | } | ||
1558 | |||
1559 | /* console_sem is held */ | ||
1560 | static void csi_P(struct vc_data *vc, unsigned int nr) | ||
1561 | { | ||
1562 | if (nr > vc->vc_cols - vc->vc_x) | ||
1563 | nr = vc->vc_cols - vc->vc_x; | ||
1564 | else if (!nr) | ||
1565 | nr = 1; | ||
1566 | delete_char(vc, nr); | ||
1567 | } | ||
1568 | |||
1569 | /* console_sem is held */ | ||
1570 | static void csi_M(struct vc_data *vc, unsigned int nr) | ||
1571 | { | ||
1572 | if (nr > vc->vc_rows - vc->vc_y) | ||
1573 | nr = vc->vc_rows - vc->vc_y; | ||
1574 | else if (!nr) | ||
1575 | nr=1; | ||
1576 | scrup(vc, vc->vc_y, vc->vc_bottom, nr); | ||
1577 | vc->vc_need_wrap = 0; | ||
1578 | } | ||
1579 | |||
1580 | /* console_sem is held (except via vc_init->reset_terminal */ | ||
1581 | static void save_cur(struct vc_data *vc) | ||
1582 | { | ||
1583 | vc->vc_saved_x = vc->vc_x; | ||
1584 | vc->vc_saved_y = vc->vc_y; | ||
1585 | vc->vc_s_intensity = vc->vc_intensity; | ||
1586 | vc->vc_s_italic = vc->vc_italic; | ||
1587 | vc->vc_s_underline = vc->vc_underline; | ||
1588 | vc->vc_s_blink = vc->vc_blink; | ||
1589 | vc->vc_s_reverse = vc->vc_reverse; | ||
1590 | vc->vc_s_charset = vc->vc_charset; | ||
1591 | vc->vc_s_color = vc->vc_color; | ||
1592 | vc->vc_saved_G0 = vc->vc_G0_charset; | ||
1593 | vc->vc_saved_G1 = vc->vc_G1_charset; | ||
1594 | } | ||
1595 | |||
1596 | /* console_sem is held */ | ||
1597 | static void restore_cur(struct vc_data *vc) | ||
1598 | { | ||
1599 | gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y); | ||
1600 | vc->vc_intensity = vc->vc_s_intensity; | ||
1601 | vc->vc_italic = vc->vc_s_italic; | ||
1602 | vc->vc_underline = vc->vc_s_underline; | ||
1603 | vc->vc_blink = vc->vc_s_blink; | ||
1604 | vc->vc_reverse = vc->vc_s_reverse; | ||
1605 | vc->vc_charset = vc->vc_s_charset; | ||
1606 | vc->vc_color = vc->vc_s_color; | ||
1607 | vc->vc_G0_charset = vc->vc_saved_G0; | ||
1608 | vc->vc_G1_charset = vc->vc_saved_G1; | ||
1609 | vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc); | ||
1610 | update_attr(vc); | ||
1611 | vc->vc_need_wrap = 0; | ||
1612 | } | ||
1613 | |||
1614 | enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey, | ||
1615 | EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd, | ||
1616 | ESpalette }; | ||
1617 | |||
1618 | /* console_sem is held (except via vc_init()) */ | ||
1619 | static void reset_terminal(struct vc_data *vc, int do_clear) | ||
1620 | { | ||
1621 | vc->vc_top = 0; | ||
1622 | vc->vc_bottom = vc->vc_rows; | ||
1623 | vc->vc_state = ESnormal; | ||
1624 | vc->vc_ques = 0; | ||
1625 | vc->vc_translate = set_translate(LAT1_MAP, vc); | ||
1626 | vc->vc_G0_charset = LAT1_MAP; | ||
1627 | vc->vc_G1_charset = GRAF_MAP; | ||
1628 | vc->vc_charset = 0; | ||
1629 | vc->vc_need_wrap = 0; | ||
1630 | vc->vc_report_mouse = 0; | ||
1631 | vc->vc_utf = default_utf8; | ||
1632 | vc->vc_utf_count = 0; | ||
1633 | |||
1634 | vc->vc_disp_ctrl = 0; | ||
1635 | vc->vc_toggle_meta = 0; | ||
1636 | |||
1637 | vc->vc_decscnm = 0; | ||
1638 | vc->vc_decom = 0; | ||
1639 | vc->vc_decawm = 1; | ||
1640 | vc->vc_deccm = global_cursor_default; | ||
1641 | vc->vc_decim = 0; | ||
1642 | |||
1643 | set_kbd(vc, decarm); | ||
1644 | clr_kbd(vc, decckm); | ||
1645 | clr_kbd(vc, kbdapplic); | ||
1646 | clr_kbd(vc, lnm); | ||
1647 | kbd_table[vc->vc_num].lockstate = 0; | ||
1648 | kbd_table[vc->vc_num].slockstate = 0; | ||
1649 | kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS; | ||
1650 | kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate; | ||
1651 | /* do not do set_leds here because this causes an endless tasklet loop | ||
1652 | when the keyboard hasn't been initialized yet */ | ||
1653 | |||
1654 | vc->vc_cursor_type = cur_default; | ||
1655 | vc->vc_complement_mask = vc->vc_s_complement_mask; | ||
1656 | |||
1657 | default_attr(vc); | ||
1658 | update_attr(vc); | ||
1659 | |||
1660 | vc->vc_tab_stop[0] = 0x01010100; | ||
1661 | vc->vc_tab_stop[1] = | ||
1662 | vc->vc_tab_stop[2] = | ||
1663 | vc->vc_tab_stop[3] = | ||
1664 | vc->vc_tab_stop[4] = | ||
1665 | vc->vc_tab_stop[5] = | ||
1666 | vc->vc_tab_stop[6] = | ||
1667 | vc->vc_tab_stop[7] = 0x01010101; | ||
1668 | |||
1669 | vc->vc_bell_pitch = DEFAULT_BELL_PITCH; | ||
1670 | vc->vc_bell_duration = DEFAULT_BELL_DURATION; | ||
1671 | |||
1672 | gotoxy(vc, 0, 0); | ||
1673 | save_cur(vc); | ||
1674 | if (do_clear) | ||
1675 | csi_J(vc, 2); | ||
1676 | } | ||
1677 | |||
1678 | /* console_sem is held */ | ||
1679 | static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c) | ||
1680 | { | ||
1681 | /* | ||
1682 | * Control characters can be used in the _middle_ | ||
1683 | * of an escape sequence. | ||
1684 | */ | ||
1685 | switch (c) { | ||
1686 | case 0: | ||
1687 | return; | ||
1688 | case 7: | ||
1689 | if (vc->vc_bell_duration) | ||
1690 | kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration); | ||
1691 | return; | ||
1692 | case 8: | ||
1693 | bs(vc); | ||
1694 | return; | ||
1695 | case 9: | ||
1696 | vc->vc_pos -= (vc->vc_x << 1); | ||
1697 | while (vc->vc_x < vc->vc_cols - 1) { | ||
1698 | vc->vc_x++; | ||
1699 | if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31))) | ||
1700 | break; | ||
1701 | } | ||
1702 | vc->vc_pos += (vc->vc_x << 1); | ||
1703 | notify_write(vc, '\t'); | ||
1704 | return; | ||
1705 | case 10: case 11: case 12: | ||
1706 | lf(vc); | ||
1707 | if (!is_kbd(vc, lnm)) | ||
1708 | return; | ||
1709 | case 13: | ||
1710 | cr(vc); | ||
1711 | return; | ||
1712 | case 14: | ||
1713 | vc->vc_charset = 1; | ||
1714 | vc->vc_translate = set_translate(vc->vc_G1_charset, vc); | ||
1715 | vc->vc_disp_ctrl = 1; | ||
1716 | return; | ||
1717 | case 15: | ||
1718 | vc->vc_charset = 0; | ||
1719 | vc->vc_translate = set_translate(vc->vc_G0_charset, vc); | ||
1720 | vc->vc_disp_ctrl = 0; | ||
1721 | return; | ||
1722 | case 24: case 26: | ||
1723 | vc->vc_state = ESnormal; | ||
1724 | return; | ||
1725 | case 27: | ||
1726 | vc->vc_state = ESesc; | ||
1727 | return; | ||
1728 | case 127: | ||
1729 | del(vc); | ||
1730 | return; | ||
1731 | case 128+27: | ||
1732 | vc->vc_state = ESsquare; | ||
1733 | return; | ||
1734 | } | ||
1735 | switch(vc->vc_state) { | ||
1736 | case ESesc: | ||
1737 | vc->vc_state = ESnormal; | ||
1738 | switch (c) { | ||
1739 | case '[': | ||
1740 | vc->vc_state = ESsquare; | ||
1741 | return; | ||
1742 | case ']': | ||
1743 | vc->vc_state = ESnonstd; | ||
1744 | return; | ||
1745 | case '%': | ||
1746 | vc->vc_state = ESpercent; | ||
1747 | return; | ||
1748 | case 'E': | ||
1749 | cr(vc); | ||
1750 | lf(vc); | ||
1751 | return; | ||
1752 | case 'M': | ||
1753 | ri(vc); | ||
1754 | return; | ||
1755 | case 'D': | ||
1756 | lf(vc); | ||
1757 | return; | ||
1758 | case 'H': | ||
1759 | vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31)); | ||
1760 | return; | ||
1761 | case 'Z': | ||
1762 | respond_ID(tty); | ||
1763 | return; | ||
1764 | case '7': | ||
1765 | save_cur(vc); | ||
1766 | return; | ||
1767 | case '8': | ||
1768 | restore_cur(vc); | ||
1769 | return; | ||
1770 | case '(': | ||
1771 | vc->vc_state = ESsetG0; | ||
1772 | return; | ||
1773 | case ')': | ||
1774 | vc->vc_state = ESsetG1; | ||
1775 | return; | ||
1776 | case '#': | ||
1777 | vc->vc_state = EShash; | ||
1778 | return; | ||
1779 | case 'c': | ||
1780 | reset_terminal(vc, 1); | ||
1781 | return; | ||
1782 | case '>': /* Numeric keypad */ | ||
1783 | clr_kbd(vc, kbdapplic); | ||
1784 | return; | ||
1785 | case '=': /* Appl. keypad */ | ||
1786 | set_kbd(vc, kbdapplic); | ||
1787 | return; | ||
1788 | } | ||
1789 | return; | ||
1790 | case ESnonstd: | ||
1791 | if (c=='P') { /* palette escape sequence */ | ||
1792 | for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++) | ||
1793 | vc->vc_par[vc->vc_npar] = 0; | ||
1794 | vc->vc_npar = 0; | ||
1795 | vc->vc_state = ESpalette; | ||
1796 | return; | ||
1797 | } else if (c=='R') { /* reset palette */ | ||
1798 | reset_palette(vc); | ||
1799 | vc->vc_state = ESnormal; | ||
1800 | } else | ||
1801 | vc->vc_state = ESnormal; | ||
1802 | return; | ||
1803 | case ESpalette: | ||
1804 | if (isxdigit(c)) { | ||
1805 | vc->vc_par[vc->vc_npar++] = hex_to_bin(c); | ||
1806 | if (vc->vc_npar == 7) { | ||
1807 | int i = vc->vc_par[0] * 3, j = 1; | ||
1808 | vc->vc_palette[i] = 16 * vc->vc_par[j++]; | ||
1809 | vc->vc_palette[i++] += vc->vc_par[j++]; | ||
1810 | vc->vc_palette[i] = 16 * vc->vc_par[j++]; | ||
1811 | vc->vc_palette[i++] += vc->vc_par[j++]; | ||
1812 | vc->vc_palette[i] = 16 * vc->vc_par[j++]; | ||
1813 | vc->vc_palette[i] += vc->vc_par[j]; | ||
1814 | set_palette(vc); | ||
1815 | vc->vc_state = ESnormal; | ||
1816 | } | ||
1817 | } else | ||
1818 | vc->vc_state = ESnormal; | ||
1819 | return; | ||
1820 | case ESsquare: | ||
1821 | for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++) | ||
1822 | vc->vc_par[vc->vc_npar] = 0; | ||
1823 | vc->vc_npar = 0; | ||
1824 | vc->vc_state = ESgetpars; | ||
1825 | if (c == '[') { /* Function key */ | ||
1826 | vc->vc_state=ESfunckey; | ||
1827 | return; | ||
1828 | } | ||
1829 | vc->vc_ques = (c == '?'); | ||
1830 | if (vc->vc_ques) | ||
1831 | return; | ||
1832 | case ESgetpars: | ||
1833 | if (c == ';' && vc->vc_npar < NPAR - 1) { | ||
1834 | vc->vc_npar++; | ||
1835 | return; | ||
1836 | } else if (c>='0' && c<='9') { | ||
1837 | vc->vc_par[vc->vc_npar] *= 10; | ||
1838 | vc->vc_par[vc->vc_npar] += c - '0'; | ||
1839 | return; | ||
1840 | } else | ||
1841 | vc->vc_state = ESgotpars; | ||
1842 | case ESgotpars: | ||
1843 | vc->vc_state = ESnormal; | ||
1844 | switch(c) { | ||
1845 | case 'h': | ||
1846 | set_mode(vc, 1); | ||
1847 | return; | ||
1848 | case 'l': | ||
1849 | set_mode(vc, 0); | ||
1850 | return; | ||
1851 | case 'c': | ||
1852 | if (vc->vc_ques) { | ||
1853 | if (vc->vc_par[0]) | ||
1854 | vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16); | ||
1855 | else | ||
1856 | vc->vc_cursor_type = cur_default; | ||
1857 | return; | ||
1858 | } | ||
1859 | break; | ||
1860 | case 'm': | ||
1861 | if (vc->vc_ques) { | ||
1862 | clear_selection(); | ||
1863 | if (vc->vc_par[0]) | ||
1864 | vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1]; | ||
1865 | else | ||
1866 | vc->vc_complement_mask = vc->vc_s_complement_mask; | ||
1867 | return; | ||
1868 | } | ||
1869 | break; | ||
1870 | case 'n': | ||
1871 | if (!vc->vc_ques) { | ||
1872 | if (vc->vc_par[0] == 5) | ||
1873 | status_report(tty); | ||
1874 | else if (vc->vc_par[0] == 6) | ||
1875 | cursor_report(vc, tty); | ||
1876 | } | ||
1877 | return; | ||
1878 | } | ||
1879 | if (vc->vc_ques) { | ||
1880 | vc->vc_ques = 0; | ||
1881 | return; | ||
1882 | } | ||
1883 | switch(c) { | ||
1884 | case 'G': case '`': | ||
1885 | if (vc->vc_par[0]) | ||
1886 | vc->vc_par[0]--; | ||
1887 | gotoxy(vc, vc->vc_par[0], vc->vc_y); | ||
1888 | return; | ||
1889 | case 'A': | ||
1890 | if (!vc->vc_par[0]) | ||
1891 | vc->vc_par[0]++; | ||
1892 | gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]); | ||
1893 | return; | ||
1894 | case 'B': case 'e': | ||
1895 | if (!vc->vc_par[0]) | ||
1896 | vc->vc_par[0]++; | ||
1897 | gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]); | ||
1898 | return; | ||
1899 | case 'C': case 'a': | ||
1900 | if (!vc->vc_par[0]) | ||
1901 | vc->vc_par[0]++; | ||
1902 | gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y); | ||
1903 | return; | ||
1904 | case 'D': | ||
1905 | if (!vc->vc_par[0]) | ||
1906 | vc->vc_par[0]++; | ||
1907 | gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y); | ||
1908 | return; | ||
1909 | case 'E': | ||
1910 | if (!vc->vc_par[0]) | ||
1911 | vc->vc_par[0]++; | ||
1912 | gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]); | ||
1913 | return; | ||
1914 | case 'F': | ||
1915 | if (!vc->vc_par[0]) | ||
1916 | vc->vc_par[0]++; | ||
1917 | gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]); | ||
1918 | return; | ||
1919 | case 'd': | ||
1920 | if (vc->vc_par[0]) | ||
1921 | vc->vc_par[0]--; | ||
1922 | gotoxay(vc, vc->vc_x ,vc->vc_par[0]); | ||
1923 | return; | ||
1924 | case 'H': case 'f': | ||
1925 | if (vc->vc_par[0]) | ||
1926 | vc->vc_par[0]--; | ||
1927 | if (vc->vc_par[1]) | ||
1928 | vc->vc_par[1]--; | ||
1929 | gotoxay(vc, vc->vc_par[1], vc->vc_par[0]); | ||
1930 | return; | ||
1931 | case 'J': | ||
1932 | csi_J(vc, vc->vc_par[0]); | ||
1933 | return; | ||
1934 | case 'K': | ||
1935 | csi_K(vc, vc->vc_par[0]); | ||
1936 | return; | ||
1937 | case 'L': | ||
1938 | csi_L(vc, vc->vc_par[0]); | ||
1939 | return; | ||
1940 | case 'M': | ||
1941 | csi_M(vc, vc->vc_par[0]); | ||
1942 | return; | ||
1943 | case 'P': | ||
1944 | csi_P(vc, vc->vc_par[0]); | ||
1945 | return; | ||
1946 | case 'c': | ||
1947 | if (!vc->vc_par[0]) | ||
1948 | respond_ID(tty); | ||
1949 | return; | ||
1950 | case 'g': | ||
1951 | if (!vc->vc_par[0]) | ||
1952 | vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31)); | ||
1953 | else if (vc->vc_par[0] == 3) { | ||
1954 | vc->vc_tab_stop[0] = | ||
1955 | vc->vc_tab_stop[1] = | ||
1956 | vc->vc_tab_stop[2] = | ||
1957 | vc->vc_tab_stop[3] = | ||
1958 | vc->vc_tab_stop[4] = | ||
1959 | vc->vc_tab_stop[5] = | ||
1960 | vc->vc_tab_stop[6] = | ||
1961 | vc->vc_tab_stop[7] = 0; | ||
1962 | } | ||
1963 | return; | ||
1964 | case 'm': | ||
1965 | csi_m(vc); | ||
1966 | return; | ||
1967 | case 'q': /* DECLL - but only 3 leds */ | ||
1968 | /* map 0,1,2,3 to 0,1,2,4 */ | ||
1969 | if (vc->vc_par[0] < 4) | ||
1970 | setledstate(kbd_table + vc->vc_num, | ||
1971 | (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4); | ||
1972 | return; | ||
1973 | case 'r': | ||
1974 | if (!vc->vc_par[0]) | ||
1975 | vc->vc_par[0]++; | ||
1976 | if (!vc->vc_par[1]) | ||
1977 | vc->vc_par[1] = vc->vc_rows; | ||
1978 | /* Minimum allowed region is 2 lines */ | ||
1979 | if (vc->vc_par[0] < vc->vc_par[1] && | ||
1980 | vc->vc_par[1] <= vc->vc_rows) { | ||
1981 | vc->vc_top = vc->vc_par[0] - 1; | ||
1982 | vc->vc_bottom = vc->vc_par[1]; | ||
1983 | gotoxay(vc, 0, 0); | ||
1984 | } | ||
1985 | return; | ||
1986 | case 's': | ||
1987 | save_cur(vc); | ||
1988 | return; | ||
1989 | case 'u': | ||
1990 | restore_cur(vc); | ||
1991 | return; | ||
1992 | case 'X': | ||
1993 | csi_X(vc, vc->vc_par[0]); | ||
1994 | return; | ||
1995 | case '@': | ||
1996 | csi_at(vc, vc->vc_par[0]); | ||
1997 | return; | ||
1998 | case ']': /* setterm functions */ | ||
1999 | setterm_command(vc); | ||
2000 | return; | ||
2001 | } | ||
2002 | return; | ||
2003 | case ESpercent: | ||
2004 | vc->vc_state = ESnormal; | ||
2005 | switch (c) { | ||
2006 | case '@': /* defined in ISO 2022 */ | ||
2007 | vc->vc_utf = 0; | ||
2008 | return; | ||
2009 | case 'G': /* prelim official escape code */ | ||
2010 | case '8': /* retained for compatibility */ | ||
2011 | vc->vc_utf = 1; | ||
2012 | return; | ||
2013 | } | ||
2014 | return; | ||
2015 | case ESfunckey: | ||
2016 | vc->vc_state = ESnormal; | ||
2017 | return; | ||
2018 | case EShash: | ||
2019 | vc->vc_state = ESnormal; | ||
2020 | if (c == '8') { | ||
2021 | /* DEC screen alignment test. kludge :-) */ | ||
2022 | vc->vc_video_erase_char = | ||
2023 | (vc->vc_video_erase_char & 0xff00) | 'E'; | ||
2024 | csi_J(vc, 2); | ||
2025 | vc->vc_video_erase_char = | ||
2026 | (vc->vc_video_erase_char & 0xff00) | ' '; | ||
2027 | do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); | ||
2028 | } | ||
2029 | return; | ||
2030 | case ESsetG0: | ||
2031 | if (c == '0') | ||
2032 | vc->vc_G0_charset = GRAF_MAP; | ||
2033 | else if (c == 'B') | ||
2034 | vc->vc_G0_charset = LAT1_MAP; | ||
2035 | else if (c == 'U') | ||
2036 | vc->vc_G0_charset = IBMPC_MAP; | ||
2037 | else if (c == 'K') | ||
2038 | vc->vc_G0_charset = USER_MAP; | ||
2039 | if (vc->vc_charset == 0) | ||
2040 | vc->vc_translate = set_translate(vc->vc_G0_charset, vc); | ||
2041 | vc->vc_state = ESnormal; | ||
2042 | return; | ||
2043 | case ESsetG1: | ||
2044 | if (c == '0') | ||
2045 | vc->vc_G1_charset = GRAF_MAP; | ||
2046 | else if (c == 'B') | ||
2047 | vc->vc_G1_charset = LAT1_MAP; | ||
2048 | else if (c == 'U') | ||
2049 | vc->vc_G1_charset = IBMPC_MAP; | ||
2050 | else if (c == 'K') | ||
2051 | vc->vc_G1_charset = USER_MAP; | ||
2052 | if (vc->vc_charset == 1) | ||
2053 | vc->vc_translate = set_translate(vc->vc_G1_charset, vc); | ||
2054 | vc->vc_state = ESnormal; | ||
2055 | return; | ||
2056 | default: | ||
2057 | vc->vc_state = ESnormal; | ||
2058 | } | ||
2059 | } | ||
2060 | |||
2061 | /* This is a temporary buffer used to prepare a tty console write | ||
2062 | * so that we can easily avoid touching user space while holding the | ||
2063 | * console spinlock. It is allocated in con_init and is shared by | ||
2064 | * this code and the vc_screen read/write tty calls. | ||
2065 | * | ||
2066 | * We have to allocate this statically in the kernel data section | ||
2067 | * since console_init (and thus con_init) are called before any | ||
2068 | * kernel memory allocation is available. | ||
2069 | */ | ||
2070 | char con_buf[CON_BUF_SIZE]; | ||
2071 | DEFINE_MUTEX(con_buf_mtx); | ||
2072 | |||
2073 | /* is_double_width() is based on the wcwidth() implementation by | ||
2074 | * Markus Kuhn -- 2007-05-26 (Unicode 5.0) | ||
2075 | * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c | ||
2076 | */ | ||
2077 | struct interval { | ||
2078 | uint32_t first; | ||
2079 | uint32_t last; | ||
2080 | }; | ||
2081 | |||
2082 | static int bisearch(uint32_t ucs, const struct interval *table, int max) | ||
2083 | { | ||
2084 | int min = 0; | ||
2085 | int mid; | ||
2086 | |||
2087 | if (ucs < table[0].first || ucs > table[max].last) | ||
2088 | return 0; | ||
2089 | while (max >= min) { | ||
2090 | mid = (min + max) / 2; | ||
2091 | if (ucs > table[mid].last) | ||
2092 | min = mid + 1; | ||
2093 | else if (ucs < table[mid].first) | ||
2094 | max = mid - 1; | ||
2095 | else | ||
2096 | return 1; | ||
2097 | } | ||
2098 | return 0; | ||
2099 | } | ||
2100 | |||
2101 | static int is_double_width(uint32_t ucs) | ||
2102 | { | ||
2103 | static const struct interval double_width[] = { | ||
2104 | { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E }, | ||
2105 | { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF }, | ||
2106 | { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 }, | ||
2107 | { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD } | ||
2108 | }; | ||
2109 | return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1); | ||
2110 | } | ||
2111 | |||
2112 | /* acquires console_sem */ | ||
2113 | static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count) | ||
2114 | { | ||
2115 | #ifdef VT_BUF_VRAM_ONLY | ||
2116 | #define FLUSH do { } while(0); | ||
2117 | #else | ||
2118 | #define FLUSH if (draw_x >= 0) { \ | ||
2119 | vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \ | ||
2120 | draw_x = -1; \ | ||
2121 | } | ||
2122 | #endif | ||
2123 | |||
2124 | int c, tc, ok, n = 0, draw_x = -1; | ||
2125 | unsigned int currcons; | ||
2126 | unsigned long draw_from = 0, draw_to = 0; | ||
2127 | struct vc_data *vc; | ||
2128 | unsigned char vc_attr; | ||
2129 | struct vt_notifier_param param; | ||
2130 | uint8_t rescan; | ||
2131 | uint8_t inverse; | ||
2132 | uint8_t width; | ||
2133 | u16 himask, charmask; | ||
2134 | |||
2135 | if (in_interrupt()) | ||
2136 | return count; | ||
2137 | |||
2138 | might_sleep(); | ||
2139 | |||
2140 | acquire_console_sem(); | ||
2141 | vc = tty->driver_data; | ||
2142 | if (vc == NULL) { | ||
2143 | printk(KERN_ERR "vt: argh, driver_data is NULL !\n"); | ||
2144 | release_console_sem(); | ||
2145 | return 0; | ||
2146 | } | ||
2147 | |||
2148 | currcons = vc->vc_num; | ||
2149 | if (!vc_cons_allocated(currcons)) { | ||
2150 | /* could this happen? */ | ||
2151 | printk_once("con_write: tty %d not allocated\n", currcons+1); | ||
2152 | release_console_sem(); | ||
2153 | return 0; | ||
2154 | } | ||
2155 | |||
2156 | himask = vc->vc_hi_font_mask; | ||
2157 | charmask = himask ? 0x1ff : 0xff; | ||
2158 | |||
2159 | /* undraw cursor first */ | ||
2160 | if (IS_FG(vc)) | ||
2161 | hide_cursor(vc); | ||
2162 | |||
2163 | param.vc = vc; | ||
2164 | |||
2165 | while (!tty->stopped && count) { | ||
2166 | int orig = *buf; | ||
2167 | c = orig; | ||
2168 | buf++; | ||
2169 | n++; | ||
2170 | count--; | ||
2171 | rescan = 0; | ||
2172 | inverse = 0; | ||
2173 | width = 1; | ||
2174 | |||
2175 | /* Do no translation at all in control states */ | ||
2176 | if (vc->vc_state != ESnormal) { | ||
2177 | tc = c; | ||
2178 | } else if (vc->vc_utf && !vc->vc_disp_ctrl) { | ||
2179 | /* Combine UTF-8 into Unicode in vc_utf_char. | ||
2180 | * vc_utf_count is the number of continuation bytes still | ||
2181 | * expected to arrive. | ||
2182 | * vc_npar is the number of continuation bytes arrived so | ||
2183 | * far | ||
2184 | */ | ||
2185 | rescan_last_byte: | ||
2186 | if ((c & 0xc0) == 0x80) { | ||
2187 | /* Continuation byte received */ | ||
2188 | static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff }; | ||
2189 | if (vc->vc_utf_count) { | ||
2190 | vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f); | ||
2191 | vc->vc_npar++; | ||
2192 | if (--vc->vc_utf_count) { | ||
2193 | /* Still need some bytes */ | ||
2194 | continue; | ||
2195 | } | ||
2196 | /* Got a whole character */ | ||
2197 | c = vc->vc_utf_char; | ||
2198 | /* Reject overlong sequences */ | ||
2199 | if (c <= utf8_length_changes[vc->vc_npar - 1] || | ||
2200 | c > utf8_length_changes[vc->vc_npar]) | ||
2201 | c = 0xfffd; | ||
2202 | } else { | ||
2203 | /* Unexpected continuation byte */ | ||
2204 | vc->vc_utf_count = 0; | ||
2205 | c = 0xfffd; | ||
2206 | } | ||
2207 | } else { | ||
2208 | /* Single ASCII byte or first byte of a sequence received */ | ||
2209 | if (vc->vc_utf_count) { | ||
2210 | /* Continuation byte expected */ | ||
2211 | rescan = 1; | ||
2212 | vc->vc_utf_count = 0; | ||
2213 | c = 0xfffd; | ||
2214 | } else if (c > 0x7f) { | ||
2215 | /* First byte of a multibyte sequence received */ | ||
2216 | vc->vc_npar = 0; | ||
2217 | if ((c & 0xe0) == 0xc0) { | ||
2218 | vc->vc_utf_count = 1; | ||
2219 | vc->vc_utf_char = (c & 0x1f); | ||
2220 | } else if ((c & 0xf0) == 0xe0) { | ||
2221 | vc->vc_utf_count = 2; | ||
2222 | vc->vc_utf_char = (c & 0x0f); | ||
2223 | } else if ((c & 0xf8) == 0xf0) { | ||
2224 | vc->vc_utf_count = 3; | ||
2225 | vc->vc_utf_char = (c & 0x07); | ||
2226 | } else if ((c & 0xfc) == 0xf8) { | ||
2227 | vc->vc_utf_count = 4; | ||
2228 | vc->vc_utf_char = (c & 0x03); | ||
2229 | } else if ((c & 0xfe) == 0xfc) { | ||
2230 | vc->vc_utf_count = 5; | ||
2231 | vc->vc_utf_char = (c & 0x01); | ||
2232 | } else { | ||
2233 | /* 254 and 255 are invalid */ | ||
2234 | c = 0xfffd; | ||
2235 | } | ||
2236 | if (vc->vc_utf_count) { | ||
2237 | /* Still need some bytes */ | ||
2238 | continue; | ||
2239 | } | ||
2240 | } | ||
2241 | /* Nothing to do if an ASCII byte was received */ | ||
2242 | } | ||
2243 | /* End of UTF-8 decoding. */ | ||
2244 | /* c is the received character, or U+FFFD for invalid sequences. */ | ||
2245 | /* Replace invalid Unicode code points with U+FFFD too */ | ||
2246 | if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff) | ||
2247 | c = 0xfffd; | ||
2248 | tc = c; | ||
2249 | } else { /* no utf or alternate charset mode */ | ||
2250 | tc = vc_translate(vc, c); | ||
2251 | } | ||
2252 | |||
2253 | param.c = tc; | ||
2254 | if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE, | ||
2255 | ¶m) == NOTIFY_STOP) | ||
2256 | continue; | ||
2257 | |||
2258 | /* If the original code was a control character we | ||
2259 | * only allow a glyph to be displayed if the code is | ||
2260 | * not normally used (such as for cursor movement) or | ||
2261 | * if the disp_ctrl mode has been explicitly enabled. | ||
2262 | * Certain characters (as given by the CTRL_ALWAYS | ||
2263 | * bitmap) are always displayed as control characters, | ||
2264 | * as the console would be pretty useless without | ||
2265 | * them; to display an arbitrary font position use the | ||
2266 | * direct-to-font zone in UTF-8 mode. | ||
2267 | */ | ||
2268 | ok = tc && (c >= 32 || | ||
2269 | !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 : | ||
2270 | vc->vc_utf || ((CTRL_ACTION >> c) & 1))) | ||
2271 | && (c != 127 || vc->vc_disp_ctrl) | ||
2272 | && (c != 128+27); | ||
2273 | |||
2274 | if (vc->vc_state == ESnormal && ok) { | ||
2275 | if (vc->vc_utf && !vc->vc_disp_ctrl) { | ||
2276 | if (is_double_width(c)) | ||
2277 | width = 2; | ||
2278 | } | ||
2279 | /* Now try to find out how to display it */ | ||
2280 | tc = conv_uni_to_pc(vc, tc); | ||
2281 | if (tc & ~charmask) { | ||
2282 | if (tc == -1 || tc == -2) { | ||
2283 | continue; /* nothing to display */ | ||
2284 | } | ||
2285 | /* Glyph not found */ | ||
2286 | if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) { | ||
2287 | /* In legacy mode use the glyph we get by a 1:1 mapping. | ||
2288 | This would make absolutely no sense with Unicode in mind, | ||
2289 | but do this for ASCII characters since a font may lack | ||
2290 | Unicode mapping info and we don't want to end up with | ||
2291 | having question marks only. */ | ||
2292 | tc = c; | ||
2293 | } else { | ||
2294 | /* Display U+FFFD. If it's not found, display an inverse question mark. */ | ||
2295 | tc = conv_uni_to_pc(vc, 0xfffd); | ||
2296 | if (tc < 0) { | ||
2297 | inverse = 1; | ||
2298 | tc = conv_uni_to_pc(vc, '?'); | ||
2299 | if (tc < 0) tc = '?'; | ||
2300 | } | ||
2301 | } | ||
2302 | } | ||
2303 | |||
2304 | if (!inverse) { | ||
2305 | vc_attr = vc->vc_attr; | ||
2306 | } else { | ||
2307 | /* invert vc_attr */ | ||
2308 | if (!vc->vc_can_do_color) { | ||
2309 | vc_attr = (vc->vc_attr) ^ 0x08; | ||
2310 | } else if (vc->vc_hi_font_mask == 0x100) { | ||
2311 | vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4); | ||
2312 | } else { | ||
2313 | vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4); | ||
2314 | } | ||
2315 | FLUSH | ||
2316 | } | ||
2317 | |||
2318 | while (1) { | ||
2319 | if (vc->vc_need_wrap || vc->vc_decim) | ||
2320 | FLUSH | ||
2321 | if (vc->vc_need_wrap) { | ||
2322 | cr(vc); | ||
2323 | lf(vc); | ||
2324 | } | ||
2325 | if (vc->vc_decim) | ||
2326 | insert_char(vc, 1); | ||
2327 | scr_writew(himask ? | ||
2328 | ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) : | ||
2329 | (vc_attr << 8) + tc, | ||
2330 | (u16 *) vc->vc_pos); | ||
2331 | if (DO_UPDATE(vc) && draw_x < 0) { | ||
2332 | draw_x = vc->vc_x; | ||
2333 | draw_from = vc->vc_pos; | ||
2334 | } | ||
2335 | if (vc->vc_x == vc->vc_cols - 1) { | ||
2336 | vc->vc_need_wrap = vc->vc_decawm; | ||
2337 | draw_to = vc->vc_pos + 2; | ||
2338 | } else { | ||
2339 | vc->vc_x++; | ||
2340 | draw_to = (vc->vc_pos += 2); | ||
2341 | } | ||
2342 | |||
2343 | if (!--width) break; | ||
2344 | |||
2345 | tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */ | ||
2346 | if (tc < 0) tc = ' '; | ||
2347 | } | ||
2348 | notify_write(vc, c); | ||
2349 | |||
2350 | if (inverse) { | ||
2351 | FLUSH | ||
2352 | } | ||
2353 | |||
2354 | if (rescan) { | ||
2355 | rescan = 0; | ||
2356 | inverse = 0; | ||
2357 | width = 1; | ||
2358 | c = orig; | ||
2359 | goto rescan_last_byte; | ||
2360 | } | ||
2361 | continue; | ||
2362 | } | ||
2363 | FLUSH | ||
2364 | do_con_trol(tty, vc, orig); | ||
2365 | } | ||
2366 | FLUSH | ||
2367 | console_conditional_schedule(); | ||
2368 | release_console_sem(); | ||
2369 | notify_update(vc); | ||
2370 | return n; | ||
2371 | #undef FLUSH | ||
2372 | } | ||
2373 | |||
2374 | /* | ||
2375 | * This is the console switching callback. | ||
2376 | * | ||
2377 | * Doing console switching in a process context allows | ||
2378 | * us to do the switches asynchronously (needed when we want | ||
2379 | * to switch due to a keyboard interrupt). Synchronization | ||
2380 | * with other console code and prevention of re-entrancy is | ||
2381 | * ensured with console_sem. | ||
2382 | */ | ||
2383 | static void console_callback(struct work_struct *ignored) | ||
2384 | { | ||
2385 | acquire_console_sem(); | ||
2386 | |||
2387 | if (want_console >= 0) { | ||
2388 | if (want_console != fg_console && | ||
2389 | vc_cons_allocated(want_console)) { | ||
2390 | hide_cursor(vc_cons[fg_console].d); | ||
2391 | change_console(vc_cons[want_console].d); | ||
2392 | /* we only changed when the console had already | ||
2393 | been allocated - a new console is not created | ||
2394 | in an interrupt routine */ | ||
2395 | } | ||
2396 | want_console = -1; | ||
2397 | } | ||
2398 | if (do_poke_blanked_console) { /* do not unblank for a LED change */ | ||
2399 | do_poke_blanked_console = 0; | ||
2400 | poke_blanked_console(); | ||
2401 | } | ||
2402 | if (scrollback_delta) { | ||
2403 | struct vc_data *vc = vc_cons[fg_console].d; | ||
2404 | clear_selection(); | ||
2405 | if (vc->vc_mode == KD_TEXT) | ||
2406 | vc->vc_sw->con_scrolldelta(vc, scrollback_delta); | ||
2407 | scrollback_delta = 0; | ||
2408 | } | ||
2409 | if (blank_timer_expired) { | ||
2410 | do_blank_screen(0); | ||
2411 | blank_timer_expired = 0; | ||
2412 | } | ||
2413 | notify_update(vc_cons[fg_console].d); | ||
2414 | |||
2415 | release_console_sem(); | ||
2416 | } | ||
2417 | |||
2418 | int set_console(int nr) | ||
2419 | { | ||
2420 | struct vc_data *vc = vc_cons[fg_console].d; | ||
2421 | |||
2422 | if (!vc_cons_allocated(nr) || vt_dont_switch || | ||
2423 | (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) { | ||
2424 | |||
2425 | /* | ||
2426 | * Console switch will fail in console_callback() or | ||
2427 | * change_console() so there is no point scheduling | ||
2428 | * the callback | ||
2429 | * | ||
2430 | * Existing set_console() users don't check the return | ||
2431 | * value so this shouldn't break anything | ||
2432 | */ | ||
2433 | return -EINVAL; | ||
2434 | } | ||
2435 | |||
2436 | want_console = nr; | ||
2437 | schedule_console_callback(); | ||
2438 | |||
2439 | return 0; | ||
2440 | } | ||
2441 | |||
2442 | struct tty_driver *console_driver; | ||
2443 | |||
2444 | #ifdef CONFIG_VT_CONSOLE | ||
2445 | |||
2446 | /** | ||
2447 | * vt_kmsg_redirect() - Sets/gets the kernel message console | ||
2448 | * @new: The new virtual terminal number or -1 if the console should stay | ||
2449 | * unchanged | ||
2450 | * | ||
2451 | * By default, the kernel messages are always printed on the current virtual | ||
2452 | * console. However, the user may modify that default with the | ||
2453 | * TIOCL_SETKMSGREDIRECT ioctl call. | ||
2454 | * | ||
2455 | * This function sets the kernel message console to be @new. It returns the old | ||
2456 | * virtual console number. The virtual terminal number 0 (both as parameter and | ||
2457 | * return value) means no redirection (i.e. always printed on the currently | ||
2458 | * active console). | ||
2459 | * | ||
2460 | * The parameter -1 means that only the current console is returned, but the | ||
2461 | * value is not modified. You may use the macro vt_get_kmsg_redirect() in that | ||
2462 | * case to make the code more understandable. | ||
2463 | * | ||
2464 | * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores | ||
2465 | * the parameter and always returns 0. | ||
2466 | */ | ||
2467 | int vt_kmsg_redirect(int new) | ||
2468 | { | ||
2469 | static int kmsg_con; | ||
2470 | |||
2471 | if (new != -1) | ||
2472 | return xchg(&kmsg_con, new); | ||
2473 | else | ||
2474 | return kmsg_con; | ||
2475 | } | ||
2476 | |||
2477 | /* | ||
2478 | * Console on virtual terminal | ||
2479 | * | ||
2480 | * The console must be locked when we get here. | ||
2481 | */ | ||
2482 | |||
2483 | static void vt_console_print(struct console *co, const char *b, unsigned count) | ||
2484 | { | ||
2485 | struct vc_data *vc = vc_cons[fg_console].d; | ||
2486 | unsigned char c; | ||
2487 | static DEFINE_SPINLOCK(printing_lock); | ||
2488 | const ushort *start; | ||
2489 | ushort cnt = 0; | ||
2490 | ushort myx; | ||
2491 | int kmsg_console; | ||
2492 | |||
2493 | /* console busy or not yet initialized */ | ||
2494 | if (!printable) | ||
2495 | return; | ||
2496 | if (!spin_trylock(&printing_lock)) | ||
2497 | return; | ||
2498 | |||
2499 | kmsg_console = vt_get_kmsg_redirect(); | ||
2500 | if (kmsg_console && vc_cons_allocated(kmsg_console - 1)) | ||
2501 | vc = vc_cons[kmsg_console - 1].d; | ||
2502 | |||
2503 | /* read `x' only after setting currcons properly (otherwise | ||
2504 | the `x' macro will read the x of the foreground console). */ | ||
2505 | myx = vc->vc_x; | ||
2506 | |||
2507 | if (!vc_cons_allocated(fg_console)) { | ||
2508 | /* impossible */ | ||
2509 | /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */ | ||
2510 | goto quit; | ||
2511 | } | ||
2512 | |||
2513 | if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc)) | ||
2514 | goto quit; | ||
2515 | |||
2516 | /* undraw cursor first */ | ||
2517 | if (IS_FG(vc)) | ||
2518 | hide_cursor(vc); | ||
2519 | |||
2520 | start = (ushort *)vc->vc_pos; | ||
2521 | |||
2522 | /* Contrived structure to try to emulate original need_wrap behaviour | ||
2523 | * Problems caused when we have need_wrap set on '\n' character */ | ||
2524 | while (count--) { | ||
2525 | c = *b++; | ||
2526 | if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) { | ||
2527 | if (cnt > 0) { | ||
2528 | if (CON_IS_VISIBLE(vc)) | ||
2529 | vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x); | ||
2530 | vc->vc_x += cnt; | ||
2531 | if (vc->vc_need_wrap) | ||
2532 | vc->vc_x--; | ||
2533 | cnt = 0; | ||
2534 | } | ||
2535 | if (c == 8) { /* backspace */ | ||
2536 | bs(vc); | ||
2537 | start = (ushort *)vc->vc_pos; | ||
2538 | myx = vc->vc_x; | ||
2539 | continue; | ||
2540 | } | ||
2541 | if (c != 13) | ||
2542 | lf(vc); | ||
2543 | cr(vc); | ||
2544 | start = (ushort *)vc->vc_pos; | ||
2545 | myx = vc->vc_x; | ||
2546 | if (c == 10 || c == 13) | ||
2547 | continue; | ||
2548 | } | ||
2549 | scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos); | ||
2550 | notify_write(vc, c); | ||
2551 | cnt++; | ||
2552 | if (myx == vc->vc_cols - 1) { | ||
2553 | vc->vc_need_wrap = 1; | ||
2554 | continue; | ||
2555 | } | ||
2556 | vc->vc_pos += 2; | ||
2557 | myx++; | ||
2558 | } | ||
2559 | if (cnt > 0) { | ||
2560 | if (CON_IS_VISIBLE(vc)) | ||
2561 | vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x); | ||
2562 | vc->vc_x += cnt; | ||
2563 | if (vc->vc_x == vc->vc_cols) { | ||
2564 | vc->vc_x--; | ||
2565 | vc->vc_need_wrap = 1; | ||
2566 | } | ||
2567 | } | ||
2568 | set_cursor(vc); | ||
2569 | notify_update(vc); | ||
2570 | |||
2571 | quit: | ||
2572 | spin_unlock(&printing_lock); | ||
2573 | } | ||
2574 | |||
2575 | static struct tty_driver *vt_console_device(struct console *c, int *index) | ||
2576 | { | ||
2577 | *index = c->index ? c->index-1 : fg_console; | ||
2578 | return console_driver; | ||
2579 | } | ||
2580 | |||
2581 | static struct console vt_console_driver = { | ||
2582 | .name = "tty", | ||
2583 | .write = vt_console_print, | ||
2584 | .device = vt_console_device, | ||
2585 | .unblank = unblank_screen, | ||
2586 | .flags = CON_PRINTBUFFER, | ||
2587 | .index = -1, | ||
2588 | }; | ||
2589 | #endif | ||
2590 | |||
2591 | /* | ||
2592 | * Handling of Linux-specific VC ioctls | ||
2593 | */ | ||
2594 | |||
2595 | /* | ||
2596 | * Generally a bit racy with respect to console_sem(). | ||
2597 | * | ||
2598 | * There are some functions which don't need it. | ||
2599 | * | ||
2600 | * There are some functions which can sleep for arbitrary periods | ||
2601 | * (paste_selection) but we don't need the lock there anyway. | ||
2602 | * | ||
2603 | * set_selection has locking, and definitely needs it | ||
2604 | */ | ||
2605 | |||
2606 | int tioclinux(struct tty_struct *tty, unsigned long arg) | ||
2607 | { | ||
2608 | char type, data; | ||
2609 | char __user *p = (char __user *)arg; | ||
2610 | int lines; | ||
2611 | int ret; | ||
2612 | |||
2613 | if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN)) | ||
2614 | return -EPERM; | ||
2615 | if (get_user(type, p)) | ||
2616 | return -EFAULT; | ||
2617 | ret = 0; | ||
2618 | |||
2619 | switch (type) | ||
2620 | { | ||
2621 | case TIOCL_SETSEL: | ||
2622 | acquire_console_sem(); | ||
2623 | ret = set_selection((struct tiocl_selection __user *)(p+1), tty); | ||
2624 | release_console_sem(); | ||
2625 | break; | ||
2626 | case TIOCL_PASTESEL: | ||
2627 | ret = paste_selection(tty); | ||
2628 | break; | ||
2629 | case TIOCL_UNBLANKSCREEN: | ||
2630 | acquire_console_sem(); | ||
2631 | unblank_screen(); | ||
2632 | release_console_sem(); | ||
2633 | break; | ||
2634 | case TIOCL_SELLOADLUT: | ||
2635 | ret = sel_loadlut(p); | ||
2636 | break; | ||
2637 | case TIOCL_GETSHIFTSTATE: | ||
2638 | |||
2639 | /* | ||
2640 | * Make it possible to react to Shift+Mousebutton. | ||
2641 | * Note that 'shift_state' is an undocumented | ||
2642 | * kernel-internal variable; programs not closely | ||
2643 | * related to the kernel should not use this. | ||
2644 | */ | ||
2645 | data = shift_state; | ||
2646 | ret = __put_user(data, p); | ||
2647 | break; | ||
2648 | case TIOCL_GETMOUSEREPORTING: | ||
2649 | data = mouse_reporting(); | ||
2650 | ret = __put_user(data, p); | ||
2651 | break; | ||
2652 | case TIOCL_SETVESABLANK: | ||
2653 | ret = set_vesa_blanking(p); | ||
2654 | break; | ||
2655 | case TIOCL_GETKMSGREDIRECT: | ||
2656 | data = vt_get_kmsg_redirect(); | ||
2657 | ret = __put_user(data, p); | ||
2658 | break; | ||
2659 | case TIOCL_SETKMSGREDIRECT: | ||
2660 | if (!capable(CAP_SYS_ADMIN)) { | ||
2661 | ret = -EPERM; | ||
2662 | } else { | ||
2663 | if (get_user(data, p+1)) | ||
2664 | ret = -EFAULT; | ||
2665 | else | ||
2666 | vt_kmsg_redirect(data); | ||
2667 | } | ||
2668 | break; | ||
2669 | case TIOCL_GETFGCONSOLE: | ||
2670 | ret = fg_console; | ||
2671 | break; | ||
2672 | case TIOCL_SCROLLCONSOLE: | ||
2673 | if (get_user(lines, (s32 __user *)(p+4))) { | ||
2674 | ret = -EFAULT; | ||
2675 | } else { | ||
2676 | scrollfront(vc_cons[fg_console].d, lines); | ||
2677 | ret = 0; | ||
2678 | } | ||
2679 | break; | ||
2680 | case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */ | ||
2681 | acquire_console_sem(); | ||
2682 | ignore_poke = 1; | ||
2683 | do_blank_screen(0); | ||
2684 | release_console_sem(); | ||
2685 | break; | ||
2686 | case TIOCL_BLANKEDSCREEN: | ||
2687 | ret = console_blanked; | ||
2688 | break; | ||
2689 | default: | ||
2690 | ret = -EINVAL; | ||
2691 | break; | ||
2692 | } | ||
2693 | return ret; | ||
2694 | } | ||
2695 | |||
2696 | /* | ||
2697 | * /dev/ttyN handling | ||
2698 | */ | ||
2699 | |||
2700 | static int con_write(struct tty_struct *tty, const unsigned char *buf, int count) | ||
2701 | { | ||
2702 | int retval; | ||
2703 | |||
2704 | retval = do_con_write(tty, buf, count); | ||
2705 | con_flush_chars(tty); | ||
2706 | |||
2707 | return retval; | ||
2708 | } | ||
2709 | |||
2710 | static int con_put_char(struct tty_struct *tty, unsigned char ch) | ||
2711 | { | ||
2712 | if (in_interrupt()) | ||
2713 | return 0; /* n_r3964 calls put_char() from interrupt context */ | ||
2714 | return do_con_write(tty, &ch, 1); | ||
2715 | } | ||
2716 | |||
2717 | static int con_write_room(struct tty_struct *tty) | ||
2718 | { | ||
2719 | if (tty->stopped) | ||
2720 | return 0; | ||
2721 | return 32768; /* No limit, really; we're not buffering */ | ||
2722 | } | ||
2723 | |||
2724 | static int con_chars_in_buffer(struct tty_struct *tty) | ||
2725 | { | ||
2726 | return 0; /* we're not buffering */ | ||
2727 | } | ||
2728 | |||
2729 | /* | ||
2730 | * con_throttle and con_unthrottle are only used for | ||
2731 | * paste_selection(), which has to stuff in a large number of | ||
2732 | * characters... | ||
2733 | */ | ||
2734 | static void con_throttle(struct tty_struct *tty) | ||
2735 | { | ||
2736 | } | ||
2737 | |||
2738 | static void con_unthrottle(struct tty_struct *tty) | ||
2739 | { | ||
2740 | struct vc_data *vc = tty->driver_data; | ||
2741 | |||
2742 | wake_up_interruptible(&vc->paste_wait); | ||
2743 | } | ||
2744 | |||
2745 | /* | ||
2746 | * Turn the Scroll-Lock LED on when the tty is stopped | ||
2747 | */ | ||
2748 | static void con_stop(struct tty_struct *tty) | ||
2749 | { | ||
2750 | int console_num; | ||
2751 | if (!tty) | ||
2752 | return; | ||
2753 | console_num = tty->index; | ||
2754 | if (!vc_cons_allocated(console_num)) | ||
2755 | return; | ||
2756 | set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK); | ||
2757 | set_leds(); | ||
2758 | } | ||
2759 | |||
2760 | /* | ||
2761 | * Turn the Scroll-Lock LED off when the console is started | ||
2762 | */ | ||
2763 | static void con_start(struct tty_struct *tty) | ||
2764 | { | ||
2765 | int console_num; | ||
2766 | if (!tty) | ||
2767 | return; | ||
2768 | console_num = tty->index; | ||
2769 | if (!vc_cons_allocated(console_num)) | ||
2770 | return; | ||
2771 | clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK); | ||
2772 | set_leds(); | ||
2773 | } | ||
2774 | |||
2775 | static void con_flush_chars(struct tty_struct *tty) | ||
2776 | { | ||
2777 | struct vc_data *vc; | ||
2778 | |||
2779 | if (in_interrupt()) /* from flush_to_ldisc */ | ||
2780 | return; | ||
2781 | |||
2782 | /* if we race with con_close(), vt may be null */ | ||
2783 | acquire_console_sem(); | ||
2784 | vc = tty->driver_data; | ||
2785 | if (vc) | ||
2786 | set_cursor(vc); | ||
2787 | release_console_sem(); | ||
2788 | } | ||
2789 | |||
2790 | /* | ||
2791 | * Allocate the console screen memory. | ||
2792 | */ | ||
2793 | static int con_open(struct tty_struct *tty, struct file *filp) | ||
2794 | { | ||
2795 | unsigned int currcons = tty->index; | ||
2796 | int ret = 0; | ||
2797 | |||
2798 | acquire_console_sem(); | ||
2799 | if (tty->driver_data == NULL) { | ||
2800 | ret = vc_allocate(currcons); | ||
2801 | if (ret == 0) { | ||
2802 | struct vc_data *vc = vc_cons[currcons].d; | ||
2803 | |||
2804 | /* Still being freed */ | ||
2805 | if (vc->port.tty) { | ||
2806 | release_console_sem(); | ||
2807 | return -ERESTARTSYS; | ||
2808 | } | ||
2809 | tty->driver_data = vc; | ||
2810 | vc->port.tty = tty; | ||
2811 | |||
2812 | if (!tty->winsize.ws_row && !tty->winsize.ws_col) { | ||
2813 | tty->winsize.ws_row = vc_cons[currcons].d->vc_rows; | ||
2814 | tty->winsize.ws_col = vc_cons[currcons].d->vc_cols; | ||
2815 | } | ||
2816 | if (vc->vc_utf) | ||
2817 | tty->termios->c_iflag |= IUTF8; | ||
2818 | else | ||
2819 | tty->termios->c_iflag &= ~IUTF8; | ||
2820 | release_console_sem(); | ||
2821 | return ret; | ||
2822 | } | ||
2823 | } | ||
2824 | release_console_sem(); | ||
2825 | return ret; | ||
2826 | } | ||
2827 | |||
2828 | static void con_close(struct tty_struct *tty, struct file *filp) | ||
2829 | { | ||
2830 | /* Nothing to do - we defer to shutdown */ | ||
2831 | } | ||
2832 | |||
2833 | static void con_shutdown(struct tty_struct *tty) | ||
2834 | { | ||
2835 | struct vc_data *vc = tty->driver_data; | ||
2836 | BUG_ON(vc == NULL); | ||
2837 | acquire_console_sem(); | ||
2838 | vc->port.tty = NULL; | ||
2839 | release_console_sem(); | ||
2840 | tty_shutdown(tty); | ||
2841 | } | ||
2842 | |||
2843 | static int default_italic_color = 2; // green (ASCII) | ||
2844 | static int default_underline_color = 3; // cyan (ASCII) | ||
2845 | module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR); | ||
2846 | module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR); | ||
2847 | |||
2848 | static void vc_init(struct vc_data *vc, unsigned int rows, | ||
2849 | unsigned int cols, int do_clear) | ||
2850 | { | ||
2851 | int j, k ; | ||
2852 | |||
2853 | vc->vc_cols = cols; | ||
2854 | vc->vc_rows = rows; | ||
2855 | vc->vc_size_row = cols << 1; | ||
2856 | vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row; | ||
2857 | |||
2858 | set_origin(vc); | ||
2859 | vc->vc_pos = vc->vc_origin; | ||
2860 | reset_vc(vc); | ||
2861 | for (j=k=0; j<16; j++) { | ||
2862 | vc->vc_palette[k++] = default_red[j] ; | ||
2863 | vc->vc_palette[k++] = default_grn[j] ; | ||
2864 | vc->vc_palette[k++] = default_blu[j] ; | ||
2865 | } | ||
2866 | vc->vc_def_color = 0x07; /* white */ | ||
2867 | vc->vc_ulcolor = default_underline_color; | ||
2868 | vc->vc_itcolor = default_italic_color; | ||
2869 | vc->vc_halfcolor = 0x08; /* grey */ | ||
2870 | init_waitqueue_head(&vc->paste_wait); | ||
2871 | reset_terminal(vc, do_clear); | ||
2872 | } | ||
2873 | |||
2874 | /* | ||
2875 | * This routine initializes console interrupts, and does nothing | ||
2876 | * else. If you want the screen to clear, call tty_write with | ||
2877 | * the appropriate escape-sequence. | ||
2878 | */ | ||
2879 | |||
2880 | static int __init con_init(void) | ||
2881 | { | ||
2882 | const char *display_desc = NULL; | ||
2883 | struct vc_data *vc; | ||
2884 | unsigned int currcons = 0, i; | ||
2885 | |||
2886 | acquire_console_sem(); | ||
2887 | |||
2888 | if (conswitchp) | ||
2889 | display_desc = conswitchp->con_startup(); | ||
2890 | if (!display_desc) { | ||
2891 | fg_console = 0; | ||
2892 | release_console_sem(); | ||
2893 | return 0; | ||
2894 | } | ||
2895 | |||
2896 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { | ||
2897 | struct con_driver *con_driver = ®istered_con_driver[i]; | ||
2898 | |||
2899 | if (con_driver->con == NULL) { | ||
2900 | con_driver->con = conswitchp; | ||
2901 | con_driver->desc = display_desc; | ||
2902 | con_driver->flag = CON_DRIVER_FLAG_INIT; | ||
2903 | con_driver->first = 0; | ||
2904 | con_driver->last = MAX_NR_CONSOLES - 1; | ||
2905 | break; | ||
2906 | } | ||
2907 | } | ||
2908 | |||
2909 | for (i = 0; i < MAX_NR_CONSOLES; i++) | ||
2910 | con_driver_map[i] = conswitchp; | ||
2911 | |||
2912 | if (blankinterval) { | ||
2913 | blank_state = blank_normal_wait; | ||
2914 | mod_timer(&console_timer, jiffies + (blankinterval * HZ)); | ||
2915 | } | ||
2916 | |||
2917 | for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) { | ||
2918 | vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT); | ||
2919 | INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); | ||
2920 | tty_port_init(&vc->port); | ||
2921 | visual_init(vc, currcons, 1); | ||
2922 | vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT); | ||
2923 | vc_init(vc, vc->vc_rows, vc->vc_cols, | ||
2924 | currcons || !vc->vc_sw->con_save_screen); | ||
2925 | } | ||
2926 | currcons = fg_console = 0; | ||
2927 | master_display_fg = vc = vc_cons[currcons].d; | ||
2928 | set_origin(vc); | ||
2929 | save_screen(vc); | ||
2930 | gotoxy(vc, vc->vc_x, vc->vc_y); | ||
2931 | csi_J(vc, 0); | ||
2932 | update_screen(vc); | ||
2933 | printk("Console: %s %s %dx%d", | ||
2934 | vc->vc_can_do_color ? "colour" : "mono", | ||
2935 | display_desc, vc->vc_cols, vc->vc_rows); | ||
2936 | printable = 1; | ||
2937 | printk("\n"); | ||
2938 | |||
2939 | release_console_sem(); | ||
2940 | |||
2941 | #ifdef CONFIG_VT_CONSOLE | ||
2942 | register_console(&vt_console_driver); | ||
2943 | #endif | ||
2944 | return 0; | ||
2945 | } | ||
2946 | console_initcall(con_init); | ||
2947 | |||
2948 | static const struct tty_operations con_ops = { | ||
2949 | .open = con_open, | ||
2950 | .close = con_close, | ||
2951 | .write = con_write, | ||
2952 | .write_room = con_write_room, | ||
2953 | .put_char = con_put_char, | ||
2954 | .flush_chars = con_flush_chars, | ||
2955 | .chars_in_buffer = con_chars_in_buffer, | ||
2956 | .ioctl = vt_ioctl, | ||
2957 | #ifdef CONFIG_COMPAT | ||
2958 | .compat_ioctl = vt_compat_ioctl, | ||
2959 | #endif | ||
2960 | .stop = con_stop, | ||
2961 | .start = con_start, | ||
2962 | .throttle = con_throttle, | ||
2963 | .unthrottle = con_unthrottle, | ||
2964 | .resize = vt_resize, | ||
2965 | .shutdown = con_shutdown | ||
2966 | }; | ||
2967 | |||
2968 | static struct cdev vc0_cdev; | ||
2969 | |||
2970 | int __init vty_init(const struct file_operations *console_fops) | ||
2971 | { | ||
2972 | cdev_init(&vc0_cdev, console_fops); | ||
2973 | if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || | ||
2974 | register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) | ||
2975 | panic("Couldn't register /dev/tty0 driver\n"); | ||
2976 | device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0"); | ||
2977 | |||
2978 | vcs_init(); | ||
2979 | |||
2980 | console_driver = alloc_tty_driver(MAX_NR_CONSOLES); | ||
2981 | if (!console_driver) | ||
2982 | panic("Couldn't allocate console driver\n"); | ||
2983 | console_driver->owner = THIS_MODULE; | ||
2984 | console_driver->name = "tty"; | ||
2985 | console_driver->name_base = 1; | ||
2986 | console_driver->major = TTY_MAJOR; | ||
2987 | console_driver->minor_start = 1; | ||
2988 | console_driver->type = TTY_DRIVER_TYPE_CONSOLE; | ||
2989 | console_driver->init_termios = tty_std_termios; | ||
2990 | if (default_utf8) | ||
2991 | console_driver->init_termios.c_iflag |= IUTF8; | ||
2992 | console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS; | ||
2993 | tty_set_operations(console_driver, &con_ops); | ||
2994 | if (tty_register_driver(console_driver)) | ||
2995 | panic("Couldn't register console driver\n"); | ||
2996 | kbd_init(); | ||
2997 | console_map_init(); | ||
2998 | #ifdef CONFIG_MDA_CONSOLE | ||
2999 | mda_console_init(); | ||
3000 | #endif | ||
3001 | return 0; | ||
3002 | } | ||
3003 | |||
3004 | #ifndef VT_SINGLE_DRIVER | ||
3005 | |||
3006 | static struct class *vtconsole_class; | ||
3007 | |||
3008 | static int bind_con_driver(const struct consw *csw, int first, int last, | ||
3009 | int deflt) | ||
3010 | { | ||
3011 | struct module *owner = csw->owner; | ||
3012 | const char *desc = NULL; | ||
3013 | struct con_driver *con_driver; | ||
3014 | int i, j = -1, k = -1, retval = -ENODEV; | ||
3015 | |||
3016 | if (!try_module_get(owner)) | ||
3017 | return -ENODEV; | ||
3018 | |||
3019 | acquire_console_sem(); | ||
3020 | |||
3021 | /* check if driver is registered */ | ||
3022 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { | ||
3023 | con_driver = ®istered_con_driver[i]; | ||
3024 | |||
3025 | if (con_driver->con == csw) { | ||
3026 | desc = con_driver->desc; | ||
3027 | retval = 0; | ||
3028 | break; | ||
3029 | } | ||
3030 | } | ||
3031 | |||
3032 | if (retval) | ||
3033 | goto err; | ||
3034 | |||
3035 | if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) { | ||
3036 | csw->con_startup(); | ||
3037 | con_driver->flag |= CON_DRIVER_FLAG_INIT; | ||
3038 | } | ||
3039 | |||
3040 | if (deflt) { | ||
3041 | if (conswitchp) | ||
3042 | module_put(conswitchp->owner); | ||
3043 | |||
3044 | __module_get(owner); | ||
3045 | conswitchp = csw; | ||
3046 | } | ||
3047 | |||
3048 | first = max(first, con_driver->first); | ||
3049 | last = min(last, con_driver->last); | ||
3050 | |||
3051 | for (i = first; i <= last; i++) { | ||
3052 | int old_was_color; | ||
3053 | struct vc_data *vc = vc_cons[i].d; | ||
3054 | |||
3055 | if (con_driver_map[i]) | ||
3056 | module_put(con_driver_map[i]->owner); | ||
3057 | __module_get(owner); | ||
3058 | con_driver_map[i] = csw; | ||
3059 | |||
3060 | if (!vc || !vc->vc_sw) | ||
3061 | continue; | ||
3062 | |||
3063 | j = i; | ||
3064 | |||
3065 | if (CON_IS_VISIBLE(vc)) { | ||
3066 | k = i; | ||
3067 | save_screen(vc); | ||
3068 | } | ||
3069 | |||
3070 | old_was_color = vc->vc_can_do_color; | ||
3071 | vc->vc_sw->con_deinit(vc); | ||
3072 | vc->vc_origin = (unsigned long)vc->vc_screenbuf; | ||
3073 | visual_init(vc, i, 0); | ||
3074 | set_origin(vc); | ||
3075 | update_attr(vc); | ||
3076 | |||
3077 | /* If the console changed between mono <-> color, then | ||
3078 | * the attributes in the screenbuf will be wrong. The | ||
3079 | * following resets all attributes to something sane. | ||
3080 | */ | ||
3081 | if (old_was_color != vc->vc_can_do_color) | ||
3082 | clear_buffer_attributes(vc); | ||
3083 | } | ||
3084 | |||
3085 | printk("Console: switching "); | ||
3086 | if (!deflt) | ||
3087 | printk("consoles %d-%d ", first+1, last+1); | ||
3088 | if (j >= 0) { | ||
3089 | struct vc_data *vc = vc_cons[j].d; | ||
3090 | |||
3091 | printk("to %s %s %dx%d\n", | ||
3092 | vc->vc_can_do_color ? "colour" : "mono", | ||
3093 | desc, vc->vc_cols, vc->vc_rows); | ||
3094 | |||
3095 | if (k >= 0) { | ||
3096 | vc = vc_cons[k].d; | ||
3097 | update_screen(vc); | ||
3098 | } | ||
3099 | } else | ||
3100 | printk("to %s\n", desc); | ||
3101 | |||
3102 | retval = 0; | ||
3103 | err: | ||
3104 | release_console_sem(); | ||
3105 | module_put(owner); | ||
3106 | return retval; | ||
3107 | }; | ||
3108 | |||
3109 | #ifdef CONFIG_VT_HW_CONSOLE_BINDING | ||
3110 | static int con_is_graphics(const struct consw *csw, int first, int last) | ||
3111 | { | ||
3112 | int i, retval = 0; | ||
3113 | |||
3114 | for (i = first; i <= last; i++) { | ||
3115 | struct vc_data *vc = vc_cons[i].d; | ||
3116 | |||
3117 | if (vc && vc->vc_mode == KD_GRAPHICS) { | ||
3118 | retval = 1; | ||
3119 | break; | ||
3120 | } | ||
3121 | } | ||
3122 | |||
3123 | return retval; | ||
3124 | } | ||
3125 | |||
3126 | /** | ||
3127 | * unbind_con_driver - unbind a console driver | ||
3128 | * @csw: pointer to console driver to unregister | ||
3129 | * @first: first in range of consoles that @csw should be unbound from | ||
3130 | * @last: last in range of consoles that @csw should be unbound from | ||
3131 | * @deflt: should next bound console driver be default after @csw is unbound? | ||
3132 | * | ||
3133 | * To unbind a driver from all possible consoles, pass 0 as @first and | ||
3134 | * %MAX_NR_CONSOLES as @last. | ||
3135 | * | ||
3136 | * @deflt controls whether the console that ends up replacing @csw should be | ||
3137 | * the default console. | ||
3138 | * | ||
3139 | * RETURNS: | ||
3140 | * -ENODEV if @csw isn't a registered console driver or can't be unregistered | ||
3141 | * or 0 on success. | ||
3142 | */ | ||
3143 | int unbind_con_driver(const struct consw *csw, int first, int last, int deflt) | ||
3144 | { | ||
3145 | struct module *owner = csw->owner; | ||
3146 | const struct consw *defcsw = NULL; | ||
3147 | struct con_driver *con_driver = NULL, *con_back = NULL; | ||
3148 | int i, retval = -ENODEV; | ||
3149 | |||
3150 | if (!try_module_get(owner)) | ||
3151 | return -ENODEV; | ||
3152 | |||
3153 | acquire_console_sem(); | ||
3154 | |||
3155 | /* check if driver is registered and if it is unbindable */ | ||
3156 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { | ||
3157 | con_driver = ®istered_con_driver[i]; | ||
3158 | |||
3159 | if (con_driver->con == csw && | ||
3160 | con_driver->flag & CON_DRIVER_FLAG_MODULE) { | ||
3161 | retval = 0; | ||
3162 | break; | ||
3163 | } | ||
3164 | } | ||
3165 | |||
3166 | if (retval) { | ||
3167 | release_console_sem(); | ||
3168 | goto err; | ||
3169 | } | ||
3170 | |||
3171 | retval = -ENODEV; | ||
3172 | |||
3173 | /* check if backup driver exists */ | ||
3174 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { | ||
3175 | con_back = ®istered_con_driver[i]; | ||
3176 | |||
3177 | if (con_back->con && | ||
3178 | !(con_back->flag & CON_DRIVER_FLAG_MODULE)) { | ||
3179 | defcsw = con_back->con; | ||
3180 | retval = 0; | ||
3181 | break; | ||
3182 | } | ||
3183 | } | ||
3184 | |||
3185 | if (retval) { | ||
3186 | release_console_sem(); | ||
3187 | goto err; | ||
3188 | } | ||
3189 | |||
3190 | if (!con_is_bound(csw)) { | ||
3191 | release_console_sem(); | ||
3192 | goto err; | ||
3193 | } | ||
3194 | |||
3195 | first = max(first, con_driver->first); | ||
3196 | last = min(last, con_driver->last); | ||
3197 | |||
3198 | for (i = first; i <= last; i++) { | ||
3199 | if (con_driver_map[i] == csw) { | ||
3200 | module_put(csw->owner); | ||
3201 | con_driver_map[i] = NULL; | ||
3202 | } | ||
3203 | } | ||
3204 | |||
3205 | if (!con_is_bound(defcsw)) { | ||
3206 | const struct consw *defconsw = conswitchp; | ||
3207 | |||
3208 | defcsw->con_startup(); | ||
3209 | con_back->flag |= CON_DRIVER_FLAG_INIT; | ||
3210 | /* | ||
3211 | * vgacon may change the default driver to point | ||
3212 | * to dummycon, we restore it here... | ||
3213 | */ | ||
3214 | conswitchp = defconsw; | ||
3215 | } | ||
3216 | |||
3217 | if (!con_is_bound(csw)) | ||
3218 | con_driver->flag &= ~CON_DRIVER_FLAG_INIT; | ||
3219 | |||
3220 | release_console_sem(); | ||
3221 | /* ignore return value, binding should not fail */ | ||
3222 | bind_con_driver(defcsw, first, last, deflt); | ||
3223 | err: | ||
3224 | module_put(owner); | ||
3225 | return retval; | ||
3226 | |||
3227 | } | ||
3228 | EXPORT_SYMBOL(unbind_con_driver); | ||
3229 | |||
3230 | static int vt_bind(struct con_driver *con) | ||
3231 | { | ||
3232 | const struct consw *defcsw = NULL, *csw = NULL; | ||
3233 | int i, more = 1, first = -1, last = -1, deflt = 0; | ||
3234 | |||
3235 | if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) || | ||
3236 | con_is_graphics(con->con, con->first, con->last)) | ||
3237 | goto err; | ||
3238 | |||
3239 | csw = con->con; | ||
3240 | |||
3241 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { | ||
3242 | struct con_driver *con = ®istered_con_driver[i]; | ||
3243 | |||
3244 | if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) { | ||
3245 | defcsw = con->con; | ||
3246 | break; | ||
3247 | } | ||
3248 | } | ||
3249 | |||
3250 | if (!defcsw) | ||
3251 | goto err; | ||
3252 | |||
3253 | while (more) { | ||
3254 | more = 0; | ||
3255 | |||
3256 | for (i = con->first; i <= con->last; i++) { | ||
3257 | if (con_driver_map[i] == defcsw) { | ||
3258 | if (first == -1) | ||
3259 | first = i; | ||
3260 | last = i; | ||
3261 | more = 1; | ||
3262 | } else if (first != -1) | ||
3263 | break; | ||
3264 | } | ||
3265 | |||
3266 | if (first == 0 && last == MAX_NR_CONSOLES -1) | ||
3267 | deflt = 1; | ||
3268 | |||
3269 | if (first != -1) | ||
3270 | bind_con_driver(csw, first, last, deflt); | ||
3271 | |||
3272 | first = -1; | ||
3273 | last = -1; | ||
3274 | deflt = 0; | ||
3275 | } | ||
3276 | |||
3277 | err: | ||
3278 | return 0; | ||
3279 | } | ||
3280 | |||
3281 | static int vt_unbind(struct con_driver *con) | ||
3282 | { | ||
3283 | const struct consw *csw = NULL; | ||
3284 | int i, more = 1, first = -1, last = -1, deflt = 0; | ||
3285 | |||
3286 | if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) || | ||
3287 | con_is_graphics(con->con, con->first, con->last)) | ||
3288 | goto err; | ||
3289 | |||
3290 | csw = con->con; | ||
3291 | |||
3292 | while (more) { | ||
3293 | more = 0; | ||
3294 | |||
3295 | for (i = con->first; i <= con->last; i++) { | ||
3296 | if (con_driver_map[i] == csw) { | ||
3297 | if (first == -1) | ||
3298 | first = i; | ||
3299 | last = i; | ||
3300 | more = 1; | ||
3301 | } else if (first != -1) | ||
3302 | break; | ||
3303 | } | ||
3304 | |||
3305 | if (first == 0 && last == MAX_NR_CONSOLES -1) | ||
3306 | deflt = 1; | ||
3307 | |||
3308 | if (first != -1) | ||
3309 | unbind_con_driver(csw, first, last, deflt); | ||
3310 | |||
3311 | first = -1; | ||
3312 | last = -1; | ||
3313 | deflt = 0; | ||
3314 | } | ||
3315 | |||
3316 | err: | ||
3317 | return 0; | ||
3318 | } | ||
3319 | #else | ||
3320 | static inline int vt_bind(struct con_driver *con) | ||
3321 | { | ||
3322 | return 0; | ||
3323 | } | ||
3324 | static inline int vt_unbind(struct con_driver *con) | ||
3325 | { | ||
3326 | return 0; | ||
3327 | } | ||
3328 | #endif /* CONFIG_VT_HW_CONSOLE_BINDING */ | ||
3329 | |||
3330 | static ssize_t store_bind(struct device *dev, struct device_attribute *attr, | ||
3331 | const char *buf, size_t count) | ||
3332 | { | ||
3333 | struct con_driver *con = dev_get_drvdata(dev); | ||
3334 | int bind = simple_strtoul(buf, NULL, 0); | ||
3335 | |||
3336 | if (bind) | ||
3337 | vt_bind(con); | ||
3338 | else | ||
3339 | vt_unbind(con); | ||
3340 | |||
3341 | return count; | ||
3342 | } | ||
3343 | |||
3344 | static ssize_t show_bind(struct device *dev, struct device_attribute *attr, | ||
3345 | char *buf) | ||
3346 | { | ||
3347 | struct con_driver *con = dev_get_drvdata(dev); | ||
3348 | int bind = con_is_bound(con->con); | ||
3349 | |||
3350 | return snprintf(buf, PAGE_SIZE, "%i\n", bind); | ||
3351 | } | ||
3352 | |||
3353 | static ssize_t show_name(struct device *dev, struct device_attribute *attr, | ||
3354 | char *buf) | ||
3355 | { | ||
3356 | struct con_driver *con = dev_get_drvdata(dev); | ||
3357 | |||
3358 | return snprintf(buf, PAGE_SIZE, "%s %s\n", | ||
3359 | (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)", | ||
3360 | con->desc); | ||
3361 | |||
3362 | } | ||
3363 | |||
3364 | static struct device_attribute device_attrs[] = { | ||
3365 | __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind), | ||
3366 | __ATTR(name, S_IRUGO, show_name, NULL), | ||
3367 | }; | ||
3368 | |||
3369 | static int vtconsole_init_device(struct con_driver *con) | ||
3370 | { | ||
3371 | int i; | ||
3372 | int error = 0; | ||
3373 | |||
3374 | con->flag |= CON_DRIVER_FLAG_ATTR; | ||
3375 | dev_set_drvdata(con->dev, con); | ||
3376 | for (i = 0; i < ARRAY_SIZE(device_attrs); i++) { | ||
3377 | error = device_create_file(con->dev, &device_attrs[i]); | ||
3378 | if (error) | ||
3379 | break; | ||
3380 | } | ||
3381 | |||
3382 | if (error) { | ||
3383 | while (--i >= 0) | ||
3384 | device_remove_file(con->dev, &device_attrs[i]); | ||
3385 | con->flag &= ~CON_DRIVER_FLAG_ATTR; | ||
3386 | } | ||
3387 | |||
3388 | return error; | ||
3389 | } | ||
3390 | |||
3391 | static void vtconsole_deinit_device(struct con_driver *con) | ||
3392 | { | ||
3393 | int i; | ||
3394 | |||
3395 | if (con->flag & CON_DRIVER_FLAG_ATTR) { | ||
3396 | for (i = 0; i < ARRAY_SIZE(device_attrs); i++) | ||
3397 | device_remove_file(con->dev, &device_attrs[i]); | ||
3398 | con->flag &= ~CON_DRIVER_FLAG_ATTR; | ||
3399 | } | ||
3400 | } | ||
3401 | |||
3402 | /** | ||
3403 | * con_is_bound - checks if driver is bound to the console | ||
3404 | * @csw: console driver | ||
3405 | * | ||
3406 | * RETURNS: zero if unbound, nonzero if bound | ||
3407 | * | ||
3408 | * Drivers can call this and if zero, they should release | ||
3409 | * all resources allocated on con_startup() | ||
3410 | */ | ||
3411 | int con_is_bound(const struct consw *csw) | ||
3412 | { | ||
3413 | int i, bound = 0; | ||
3414 | |||
3415 | for (i = 0; i < MAX_NR_CONSOLES; i++) { | ||
3416 | if (con_driver_map[i] == csw) { | ||
3417 | bound = 1; | ||
3418 | break; | ||
3419 | } | ||
3420 | } | ||
3421 | |||
3422 | return bound; | ||
3423 | } | ||
3424 | EXPORT_SYMBOL(con_is_bound); | ||
3425 | |||
3426 | /** | ||
3427 | * con_debug_enter - prepare the console for the kernel debugger | ||
3428 | * @sw: console driver | ||
3429 | * | ||
3430 | * Called when the console is taken over by the kernel debugger, this | ||
3431 | * function needs to save the current console state, then put the console | ||
3432 | * into a state suitable for the kernel debugger. | ||
3433 | * | ||
3434 | * RETURNS: | ||
3435 | * Zero on success, nonzero if a failure occurred when trying to prepare | ||
3436 | * the console for the debugger. | ||
3437 | */ | ||
3438 | int con_debug_enter(struct vc_data *vc) | ||
3439 | { | ||
3440 | int ret = 0; | ||
3441 | |||
3442 | saved_fg_console = fg_console; | ||
3443 | saved_last_console = last_console; | ||
3444 | saved_want_console = want_console; | ||
3445 | saved_vc_mode = vc->vc_mode; | ||
3446 | saved_console_blanked = console_blanked; | ||
3447 | vc->vc_mode = KD_TEXT; | ||
3448 | console_blanked = 0; | ||
3449 | if (vc->vc_sw->con_debug_enter) | ||
3450 | ret = vc->vc_sw->con_debug_enter(vc); | ||
3451 | #ifdef CONFIG_KGDB_KDB | ||
3452 | /* Set the initial LINES variable if it is not already set */ | ||
3453 | if (vc->vc_rows < 999) { | ||
3454 | int linecount; | ||
3455 | char lns[4]; | ||
3456 | const char *setargs[3] = { | ||
3457 | "set", | ||
3458 | "LINES", | ||
3459 | lns, | ||
3460 | }; | ||
3461 | if (kdbgetintenv(setargs[0], &linecount)) { | ||
3462 | snprintf(lns, 4, "%i", vc->vc_rows); | ||
3463 | kdb_set(2, setargs); | ||
3464 | } | ||
3465 | } | ||
3466 | #endif /* CONFIG_KGDB_KDB */ | ||
3467 | return ret; | ||
3468 | } | ||
3469 | EXPORT_SYMBOL_GPL(con_debug_enter); | ||
3470 | |||
3471 | /** | ||
3472 | * con_debug_leave - restore console state | ||
3473 | * @sw: console driver | ||
3474 | * | ||
3475 | * Restore the console state to what it was before the kernel debugger | ||
3476 | * was invoked. | ||
3477 | * | ||
3478 | * RETURNS: | ||
3479 | * Zero on success, nonzero if a failure occurred when trying to restore | ||
3480 | * the console. | ||
3481 | */ | ||
3482 | int con_debug_leave(void) | ||
3483 | { | ||
3484 | struct vc_data *vc; | ||
3485 | int ret = 0; | ||
3486 | |||
3487 | fg_console = saved_fg_console; | ||
3488 | last_console = saved_last_console; | ||
3489 | want_console = saved_want_console; | ||
3490 | console_blanked = saved_console_blanked; | ||
3491 | vc_cons[fg_console].d->vc_mode = saved_vc_mode; | ||
3492 | |||
3493 | vc = vc_cons[fg_console].d; | ||
3494 | if (vc->vc_sw->con_debug_leave) | ||
3495 | ret = vc->vc_sw->con_debug_leave(vc); | ||
3496 | return ret; | ||
3497 | } | ||
3498 | EXPORT_SYMBOL_GPL(con_debug_leave); | ||
3499 | |||
3500 | /** | ||
3501 | * register_con_driver - register console driver to console layer | ||
3502 | * @csw: console driver | ||
3503 | * @first: the first console to take over, minimum value is 0 | ||
3504 | * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1 | ||
3505 | * | ||
3506 | * DESCRIPTION: This function registers a console driver which can later | ||
3507 | * bind to a range of consoles specified by @first and @last. It will | ||
3508 | * also initialize the console driver by calling con_startup(). | ||
3509 | */ | ||
3510 | int register_con_driver(const struct consw *csw, int first, int last) | ||
3511 | { | ||
3512 | struct module *owner = csw->owner; | ||
3513 | struct con_driver *con_driver; | ||
3514 | const char *desc; | ||
3515 | int i, retval = 0; | ||
3516 | |||
3517 | if (!try_module_get(owner)) | ||
3518 | return -ENODEV; | ||
3519 | |||
3520 | acquire_console_sem(); | ||
3521 | |||
3522 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { | ||
3523 | con_driver = ®istered_con_driver[i]; | ||
3524 | |||
3525 | /* already registered */ | ||
3526 | if (con_driver->con == csw) | ||
3527 | retval = -EINVAL; | ||
3528 | } | ||
3529 | |||
3530 | if (retval) | ||
3531 | goto err; | ||
3532 | |||
3533 | desc = csw->con_startup(); | ||
3534 | |||
3535 | if (!desc) | ||
3536 | goto err; | ||
3537 | |||
3538 | retval = -EINVAL; | ||
3539 | |||
3540 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { | ||
3541 | con_driver = ®istered_con_driver[i]; | ||
3542 | |||
3543 | if (con_driver->con == NULL) { | ||
3544 | con_driver->con = csw; | ||
3545 | con_driver->desc = desc; | ||
3546 | con_driver->node = i; | ||
3547 | con_driver->flag = CON_DRIVER_FLAG_MODULE | | ||
3548 | CON_DRIVER_FLAG_INIT; | ||
3549 | con_driver->first = first; | ||
3550 | con_driver->last = last; | ||
3551 | retval = 0; | ||
3552 | break; | ||
3553 | } | ||
3554 | } | ||
3555 | |||
3556 | if (retval) | ||
3557 | goto err; | ||
3558 | |||
3559 | con_driver->dev = device_create(vtconsole_class, NULL, | ||
3560 | MKDEV(0, con_driver->node), | ||
3561 | NULL, "vtcon%i", | ||
3562 | con_driver->node); | ||
3563 | |||
3564 | if (IS_ERR(con_driver->dev)) { | ||
3565 | printk(KERN_WARNING "Unable to create device for %s; " | ||
3566 | "errno = %ld\n", con_driver->desc, | ||
3567 | PTR_ERR(con_driver->dev)); | ||
3568 | con_driver->dev = NULL; | ||
3569 | } else { | ||
3570 | vtconsole_init_device(con_driver); | ||
3571 | } | ||
3572 | |||
3573 | err: | ||
3574 | release_console_sem(); | ||
3575 | module_put(owner); | ||
3576 | return retval; | ||
3577 | } | ||
3578 | EXPORT_SYMBOL(register_con_driver); | ||
3579 | |||
3580 | /** | ||
3581 | * unregister_con_driver - unregister console driver from console layer | ||
3582 | * @csw: console driver | ||
3583 | * | ||
3584 | * DESCRIPTION: All drivers that registers to the console layer must | ||
3585 | * call this function upon exit, or if the console driver is in a state | ||
3586 | * where it won't be able to handle console services, such as the | ||
3587 | * framebuffer console without loaded framebuffer drivers. | ||
3588 | * | ||
3589 | * The driver must unbind first prior to unregistration. | ||
3590 | */ | ||
3591 | int unregister_con_driver(const struct consw *csw) | ||
3592 | { | ||
3593 | int i, retval = -ENODEV; | ||
3594 | |||
3595 | acquire_console_sem(); | ||
3596 | |||
3597 | /* cannot unregister a bound driver */ | ||
3598 | if (con_is_bound(csw)) | ||
3599 | goto err; | ||
3600 | |||
3601 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { | ||
3602 | struct con_driver *con_driver = ®istered_con_driver[i]; | ||
3603 | |||
3604 | if (con_driver->con == csw && | ||
3605 | con_driver->flag & CON_DRIVER_FLAG_MODULE) { | ||
3606 | vtconsole_deinit_device(con_driver); | ||
3607 | device_destroy(vtconsole_class, | ||
3608 | MKDEV(0, con_driver->node)); | ||
3609 | con_driver->con = NULL; | ||
3610 | con_driver->desc = NULL; | ||
3611 | con_driver->dev = NULL; | ||
3612 | con_driver->node = 0; | ||
3613 | con_driver->flag = 0; | ||
3614 | con_driver->first = 0; | ||
3615 | con_driver->last = 0; | ||
3616 | retval = 0; | ||
3617 | break; | ||
3618 | } | ||
3619 | } | ||
3620 | err: | ||
3621 | release_console_sem(); | ||
3622 | return retval; | ||
3623 | } | ||
3624 | EXPORT_SYMBOL(unregister_con_driver); | ||
3625 | |||
3626 | /* | ||
3627 | * If we support more console drivers, this function is used | ||
3628 | * when a driver wants to take over some existing consoles | ||
3629 | * and become default driver for newly opened ones. | ||
3630 | * | ||
3631 | * take_over_console is basically a register followed by unbind | ||
3632 | */ | ||
3633 | int take_over_console(const struct consw *csw, int first, int last, int deflt) | ||
3634 | { | ||
3635 | int err; | ||
3636 | |||
3637 | err = register_con_driver(csw, first, last); | ||
3638 | |||
3639 | if (!err) | ||
3640 | bind_con_driver(csw, first, last, deflt); | ||
3641 | |||
3642 | return err; | ||
3643 | } | ||
3644 | |||
3645 | /* | ||
3646 | * give_up_console is a wrapper to unregister_con_driver. It will only | ||
3647 | * work if driver is fully unbound. | ||
3648 | */ | ||
3649 | void give_up_console(const struct consw *csw) | ||
3650 | { | ||
3651 | unregister_con_driver(csw); | ||
3652 | } | ||
3653 | |||
3654 | static int __init vtconsole_class_init(void) | ||
3655 | { | ||
3656 | int i; | ||
3657 | |||
3658 | vtconsole_class = class_create(THIS_MODULE, "vtconsole"); | ||
3659 | if (IS_ERR(vtconsole_class)) { | ||
3660 | printk(KERN_WARNING "Unable to create vt console class; " | ||
3661 | "errno = %ld\n", PTR_ERR(vtconsole_class)); | ||
3662 | vtconsole_class = NULL; | ||
3663 | } | ||
3664 | |||
3665 | /* Add system drivers to sysfs */ | ||
3666 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { | ||
3667 | struct con_driver *con = ®istered_con_driver[i]; | ||
3668 | |||
3669 | if (con->con && !con->dev) { | ||
3670 | con->dev = device_create(vtconsole_class, NULL, | ||
3671 | MKDEV(0, con->node), | ||
3672 | NULL, "vtcon%i", | ||
3673 | con->node); | ||
3674 | |||
3675 | if (IS_ERR(con->dev)) { | ||
3676 | printk(KERN_WARNING "Unable to create " | ||
3677 | "device for %s; errno = %ld\n", | ||
3678 | con->desc, PTR_ERR(con->dev)); | ||
3679 | con->dev = NULL; | ||
3680 | } else { | ||
3681 | vtconsole_init_device(con); | ||
3682 | } | ||
3683 | } | ||
3684 | } | ||
3685 | |||
3686 | return 0; | ||
3687 | } | ||
3688 | postcore_initcall(vtconsole_class_init); | ||
3689 | |||
3690 | #endif | ||
3691 | |||
3692 | /* | ||
3693 | * Screen blanking | ||
3694 | */ | ||
3695 | |||
3696 | static int set_vesa_blanking(char __user *p) | ||
3697 | { | ||
3698 | unsigned int mode; | ||
3699 | |||
3700 | if (get_user(mode, p + 1)) | ||
3701 | return -EFAULT; | ||
3702 | |||
3703 | vesa_blank_mode = (mode < 4) ? mode : 0; | ||
3704 | return 0; | ||
3705 | } | ||
3706 | |||
3707 | void do_blank_screen(int entering_gfx) | ||
3708 | { | ||
3709 | struct vc_data *vc = vc_cons[fg_console].d; | ||
3710 | int i; | ||
3711 | |||
3712 | WARN_CONSOLE_UNLOCKED(); | ||
3713 | |||
3714 | if (console_blanked) { | ||
3715 | if (blank_state == blank_vesa_wait) { | ||
3716 | blank_state = blank_off; | ||
3717 | vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0); | ||
3718 | } | ||
3719 | return; | ||
3720 | } | ||
3721 | |||
3722 | /* entering graphics mode? */ | ||
3723 | if (entering_gfx) { | ||
3724 | hide_cursor(vc); | ||
3725 | save_screen(vc); | ||
3726 | vc->vc_sw->con_blank(vc, -1, 1); | ||
3727 | console_blanked = fg_console + 1; | ||
3728 | blank_state = blank_off; | ||
3729 | set_origin(vc); | ||
3730 | return; | ||
3731 | } | ||
3732 | |||
3733 | if (blank_state != blank_normal_wait) | ||
3734 | return; | ||
3735 | blank_state = blank_off; | ||
3736 | |||
3737 | /* don't blank graphics */ | ||
3738 | if (vc->vc_mode != KD_TEXT) { | ||
3739 | console_blanked = fg_console + 1; | ||
3740 | return; | ||
3741 | } | ||
3742 | |||
3743 | hide_cursor(vc); | ||
3744 | del_timer_sync(&console_timer); | ||
3745 | blank_timer_expired = 0; | ||
3746 | |||
3747 | save_screen(vc); | ||
3748 | /* In case we need to reset origin, blanking hook returns 1 */ | ||
3749 | i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0); | ||
3750 | console_blanked = fg_console + 1; | ||
3751 | if (i) | ||
3752 | set_origin(vc); | ||
3753 | |||
3754 | if (console_blank_hook && console_blank_hook(1)) | ||
3755 | return; | ||
3756 | |||
3757 | if (vesa_off_interval && vesa_blank_mode) { | ||
3758 | blank_state = blank_vesa_wait; | ||
3759 | mod_timer(&console_timer, jiffies + vesa_off_interval); | ||
3760 | } | ||
3761 | vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num); | ||
3762 | } | ||
3763 | EXPORT_SYMBOL(do_blank_screen); | ||
3764 | |||
3765 | /* | ||
3766 | * Called by timer as well as from vt_console_driver | ||
3767 | */ | ||
3768 | void do_unblank_screen(int leaving_gfx) | ||
3769 | { | ||
3770 | struct vc_data *vc; | ||
3771 | |||
3772 | /* This should now always be called from a "sane" (read: can schedule) | ||
3773 | * context for the sake of the low level drivers, except in the special | ||
3774 | * case of oops_in_progress | ||
3775 | */ | ||
3776 | if (!oops_in_progress) | ||
3777 | might_sleep(); | ||
3778 | |||
3779 | WARN_CONSOLE_UNLOCKED(); | ||
3780 | |||
3781 | ignore_poke = 0; | ||
3782 | if (!console_blanked) | ||
3783 | return; | ||
3784 | if (!vc_cons_allocated(fg_console)) { | ||
3785 | /* impossible */ | ||
3786 | printk("unblank_screen: tty %d not allocated ??\n", fg_console+1); | ||
3787 | return; | ||
3788 | } | ||
3789 | vc = vc_cons[fg_console].d; | ||
3790 | /* Try to unblank in oops case too */ | ||
3791 | if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc)) | ||
3792 | return; /* but leave console_blanked != 0 */ | ||
3793 | |||
3794 | if (blankinterval) { | ||
3795 | mod_timer(&console_timer, jiffies + (blankinterval * HZ)); | ||
3796 | blank_state = blank_normal_wait; | ||
3797 | } | ||
3798 | |||
3799 | console_blanked = 0; | ||
3800 | if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc)) | ||
3801 | /* Low-level driver cannot restore -> do it ourselves */ | ||
3802 | update_screen(vc); | ||
3803 | if (console_blank_hook) | ||
3804 | console_blank_hook(0); | ||
3805 | set_palette(vc); | ||
3806 | set_cursor(vc); | ||
3807 | vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num); | ||
3808 | } | ||
3809 | EXPORT_SYMBOL(do_unblank_screen); | ||
3810 | |||
3811 | /* | ||
3812 | * This is called by the outside world to cause a forced unblank, mostly for | ||
3813 | * oopses. Currently, I just call do_unblank_screen(0), but we could eventually | ||
3814 | * call it with 1 as an argument and so force a mode restore... that may kill | ||
3815 | * X or at least garbage the screen but would also make the Oops visible... | ||
3816 | */ | ||
3817 | void unblank_screen(void) | ||
3818 | { | ||
3819 | do_unblank_screen(0); | ||
3820 | } | ||
3821 | |||
3822 | /* | ||
3823 | * We defer the timer blanking to work queue so it can take the console mutex | ||
3824 | * (console operations can still happen at irq time, but only from printk which | ||
3825 | * has the console mutex. Not perfect yet, but better than no locking | ||
3826 | */ | ||
3827 | static void blank_screen_t(unsigned long dummy) | ||
3828 | { | ||
3829 | if (unlikely(!keventd_up())) { | ||
3830 | mod_timer(&console_timer, jiffies + (blankinterval * HZ)); | ||
3831 | return; | ||
3832 | } | ||
3833 | blank_timer_expired = 1; | ||
3834 | schedule_work(&console_work); | ||
3835 | } | ||
3836 | |||
3837 | void poke_blanked_console(void) | ||
3838 | { | ||
3839 | WARN_CONSOLE_UNLOCKED(); | ||
3840 | |||
3841 | /* Add this so we quickly catch whoever might call us in a non | ||
3842 | * safe context. Nowadays, unblank_screen() isn't to be called in | ||
3843 | * atomic contexts and is allowed to schedule (with the special case | ||
3844 | * of oops_in_progress, but that isn't of any concern for this | ||
3845 | * function. --BenH. | ||
3846 | */ | ||
3847 | might_sleep(); | ||
3848 | |||
3849 | /* This isn't perfectly race free, but a race here would be mostly harmless, | ||
3850 | * at worse, we'll do a spurrious blank and it's unlikely | ||
3851 | */ | ||
3852 | del_timer(&console_timer); | ||
3853 | blank_timer_expired = 0; | ||
3854 | |||
3855 | if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS) | ||
3856 | return; | ||
3857 | if (console_blanked) | ||
3858 | unblank_screen(); | ||
3859 | else if (blankinterval) { | ||
3860 | mod_timer(&console_timer, jiffies + (blankinterval * HZ)); | ||
3861 | blank_state = blank_normal_wait; | ||
3862 | } | ||
3863 | } | ||
3864 | |||
3865 | /* | ||
3866 | * Palettes | ||
3867 | */ | ||
3868 | |||
3869 | static void set_palette(struct vc_data *vc) | ||
3870 | { | ||
3871 | WARN_CONSOLE_UNLOCKED(); | ||
3872 | |||
3873 | if (vc->vc_mode != KD_GRAPHICS) | ||
3874 | vc->vc_sw->con_set_palette(vc, color_table); | ||
3875 | } | ||
3876 | |||
3877 | static int set_get_cmap(unsigned char __user *arg, int set) | ||
3878 | { | ||
3879 | int i, j, k; | ||
3880 | |||
3881 | WARN_CONSOLE_UNLOCKED(); | ||
3882 | |||
3883 | for (i = 0; i < 16; i++) | ||
3884 | if (set) { | ||
3885 | get_user(default_red[i], arg++); | ||
3886 | get_user(default_grn[i], arg++); | ||
3887 | get_user(default_blu[i], arg++); | ||
3888 | } else { | ||
3889 | put_user(default_red[i], arg++); | ||
3890 | put_user(default_grn[i], arg++); | ||
3891 | put_user(default_blu[i], arg++); | ||
3892 | } | ||
3893 | if (set) { | ||
3894 | for (i = 0; i < MAX_NR_CONSOLES; i++) | ||
3895 | if (vc_cons_allocated(i)) { | ||
3896 | for (j = k = 0; j < 16; j++) { | ||
3897 | vc_cons[i].d->vc_palette[k++] = default_red[j]; | ||
3898 | vc_cons[i].d->vc_palette[k++] = default_grn[j]; | ||
3899 | vc_cons[i].d->vc_palette[k++] = default_blu[j]; | ||
3900 | } | ||
3901 | set_palette(vc_cons[i].d); | ||
3902 | } | ||
3903 | } | ||
3904 | return 0; | ||
3905 | } | ||
3906 | |||
3907 | /* | ||
3908 | * Load palette into the DAC registers. arg points to a colour | ||
3909 | * map, 3 bytes per colour, 16 colours, range from 0 to 255. | ||
3910 | */ | ||
3911 | |||
3912 | int con_set_cmap(unsigned char __user *arg) | ||
3913 | { | ||
3914 | int rc; | ||
3915 | |||
3916 | acquire_console_sem(); | ||
3917 | rc = set_get_cmap (arg,1); | ||
3918 | release_console_sem(); | ||
3919 | |||
3920 | return rc; | ||
3921 | } | ||
3922 | |||
3923 | int con_get_cmap(unsigned char __user *arg) | ||
3924 | { | ||
3925 | int rc; | ||
3926 | |||
3927 | acquire_console_sem(); | ||
3928 | rc = set_get_cmap (arg,0); | ||
3929 | release_console_sem(); | ||
3930 | |||
3931 | return rc; | ||
3932 | } | ||
3933 | |||
3934 | void reset_palette(struct vc_data *vc) | ||
3935 | { | ||
3936 | int j, k; | ||
3937 | for (j=k=0; j<16; j++) { | ||
3938 | vc->vc_palette[k++] = default_red[j]; | ||
3939 | vc->vc_palette[k++] = default_grn[j]; | ||
3940 | vc->vc_palette[k++] = default_blu[j]; | ||
3941 | } | ||
3942 | set_palette(vc); | ||
3943 | } | ||
3944 | |||
3945 | /* | ||
3946 | * Font switching | ||
3947 | * | ||
3948 | * Currently we only support fonts up to 32 pixels wide, at a maximum height | ||
3949 | * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints, | ||
3950 | * depending on width) reserved for each character which is kinda wasty, but | ||
3951 | * this is done in order to maintain compatibility with the EGA/VGA fonts. It | ||
3952 | * is upto the actual low-level console-driver convert data into its favorite | ||
3953 | * format (maybe we should add a `fontoffset' field to the `display' | ||
3954 | * structure so we won't have to convert the fontdata all the time. | ||
3955 | * /Jes | ||
3956 | */ | ||
3957 | |||
3958 | #define max_font_size 65536 | ||
3959 | |||
3960 | static int con_font_get(struct vc_data *vc, struct console_font_op *op) | ||
3961 | { | ||
3962 | struct console_font font; | ||
3963 | int rc = -EINVAL; | ||
3964 | int c; | ||
3965 | |||
3966 | if (vc->vc_mode != KD_TEXT) | ||
3967 | return -EINVAL; | ||
3968 | |||
3969 | if (op->data) { | ||
3970 | font.data = kmalloc(max_font_size, GFP_KERNEL); | ||
3971 | if (!font.data) | ||
3972 | return -ENOMEM; | ||
3973 | } else | ||
3974 | font.data = NULL; | ||
3975 | |||
3976 | acquire_console_sem(); | ||
3977 | if (vc->vc_sw->con_font_get) | ||
3978 | rc = vc->vc_sw->con_font_get(vc, &font); | ||
3979 | else | ||
3980 | rc = -ENOSYS; | ||
3981 | release_console_sem(); | ||
3982 | |||
3983 | if (rc) | ||
3984 | goto out; | ||
3985 | |||
3986 | c = (font.width+7)/8 * 32 * font.charcount; | ||
3987 | |||
3988 | if (op->data && font.charcount > op->charcount) | ||
3989 | rc = -ENOSPC; | ||
3990 | if (!(op->flags & KD_FONT_FLAG_OLD)) { | ||
3991 | if (font.width > op->width || font.height > op->height) | ||
3992 | rc = -ENOSPC; | ||
3993 | } else { | ||
3994 | if (font.width != 8) | ||
3995 | rc = -EIO; | ||
3996 | else if ((op->height && font.height > op->height) || | ||
3997 | font.height > 32) | ||
3998 | rc = -ENOSPC; | ||
3999 | } | ||
4000 | if (rc) | ||
4001 | goto out; | ||
4002 | |||
4003 | op->height = font.height; | ||
4004 | op->width = font.width; | ||
4005 | op->charcount = font.charcount; | ||
4006 | |||
4007 | if (op->data && copy_to_user(op->data, font.data, c)) | ||
4008 | rc = -EFAULT; | ||
4009 | |||
4010 | out: | ||
4011 | kfree(font.data); | ||
4012 | return rc; | ||
4013 | } | ||
4014 | |||
4015 | static int con_font_set(struct vc_data *vc, struct console_font_op *op) | ||
4016 | { | ||
4017 | struct console_font font; | ||
4018 | int rc = -EINVAL; | ||
4019 | int size; | ||
4020 | |||
4021 | if (vc->vc_mode != KD_TEXT) | ||
4022 | return -EINVAL; | ||
4023 | if (!op->data) | ||
4024 | return -EINVAL; | ||
4025 | if (op->charcount > 512) | ||
4026 | return -EINVAL; | ||
4027 | if (!op->height) { /* Need to guess font height [compat] */ | ||
4028 | int h, i; | ||
4029 | u8 __user *charmap = op->data; | ||
4030 | u8 tmp; | ||
4031 | |||
4032 | /* If from KDFONTOP ioctl, don't allow things which can be done in userland, | ||
4033 | so that we can get rid of this soon */ | ||
4034 | if (!(op->flags & KD_FONT_FLAG_OLD)) | ||
4035 | return -EINVAL; | ||
4036 | for (h = 32; h > 0; h--) | ||
4037 | for (i = 0; i < op->charcount; i++) { | ||
4038 | if (get_user(tmp, &charmap[32*i+h-1])) | ||
4039 | return -EFAULT; | ||
4040 | if (tmp) | ||
4041 | goto nonzero; | ||
4042 | } | ||
4043 | return -EINVAL; | ||
4044 | nonzero: | ||
4045 | op->height = h; | ||
4046 | } | ||
4047 | if (op->width <= 0 || op->width > 32 || op->height > 32) | ||
4048 | return -EINVAL; | ||
4049 | size = (op->width+7)/8 * 32 * op->charcount; | ||
4050 | if (size > max_font_size) | ||
4051 | return -ENOSPC; | ||
4052 | font.charcount = op->charcount; | ||
4053 | font.height = op->height; | ||
4054 | font.width = op->width; | ||
4055 | font.data = memdup_user(op->data, size); | ||
4056 | if (IS_ERR(font.data)) | ||
4057 | return PTR_ERR(font.data); | ||
4058 | acquire_console_sem(); | ||
4059 | if (vc->vc_sw->con_font_set) | ||
4060 | rc = vc->vc_sw->con_font_set(vc, &font, op->flags); | ||
4061 | else | ||
4062 | rc = -ENOSYS; | ||
4063 | release_console_sem(); | ||
4064 | kfree(font.data); | ||
4065 | return rc; | ||
4066 | } | ||
4067 | |||
4068 | static int con_font_default(struct vc_data *vc, struct console_font_op *op) | ||
4069 | { | ||
4070 | struct console_font font = {.width = op->width, .height = op->height}; | ||
4071 | char name[MAX_FONT_NAME]; | ||
4072 | char *s = name; | ||
4073 | int rc; | ||
4074 | |||
4075 | if (vc->vc_mode != KD_TEXT) | ||
4076 | return -EINVAL; | ||
4077 | |||
4078 | if (!op->data) | ||
4079 | s = NULL; | ||
4080 | else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0) | ||
4081 | return -EFAULT; | ||
4082 | else | ||
4083 | name[MAX_FONT_NAME - 1] = 0; | ||
4084 | |||
4085 | acquire_console_sem(); | ||
4086 | if (vc->vc_sw->con_font_default) | ||
4087 | rc = vc->vc_sw->con_font_default(vc, &font, s); | ||
4088 | else | ||
4089 | rc = -ENOSYS; | ||
4090 | release_console_sem(); | ||
4091 | if (!rc) { | ||
4092 | op->width = font.width; | ||
4093 | op->height = font.height; | ||
4094 | } | ||
4095 | return rc; | ||
4096 | } | ||
4097 | |||
4098 | static int con_font_copy(struct vc_data *vc, struct console_font_op *op) | ||
4099 | { | ||
4100 | int con = op->height; | ||
4101 | int rc; | ||
4102 | |||
4103 | if (vc->vc_mode != KD_TEXT) | ||
4104 | return -EINVAL; | ||
4105 | |||
4106 | acquire_console_sem(); | ||
4107 | if (!vc->vc_sw->con_font_copy) | ||
4108 | rc = -ENOSYS; | ||
4109 | else if (con < 0 || !vc_cons_allocated(con)) | ||
4110 | rc = -ENOTTY; | ||
4111 | else if (con == vc->vc_num) /* nothing to do */ | ||
4112 | rc = 0; | ||
4113 | else | ||
4114 | rc = vc->vc_sw->con_font_copy(vc, con); | ||
4115 | release_console_sem(); | ||
4116 | return rc; | ||
4117 | } | ||
4118 | |||
4119 | int con_font_op(struct vc_data *vc, struct console_font_op *op) | ||
4120 | { | ||
4121 | switch (op->op) { | ||
4122 | case KD_FONT_OP_SET: | ||
4123 | return con_font_set(vc, op); | ||
4124 | case KD_FONT_OP_GET: | ||
4125 | return con_font_get(vc, op); | ||
4126 | case KD_FONT_OP_SET_DEFAULT: | ||
4127 | return con_font_default(vc, op); | ||
4128 | case KD_FONT_OP_COPY: | ||
4129 | return con_font_copy(vc, op); | ||
4130 | } | ||
4131 | return -ENOSYS; | ||
4132 | } | ||
4133 | |||
4134 | /* | ||
4135 | * Interface exported to selection and vcs. | ||
4136 | */ | ||
4137 | |||
4138 | /* used by selection */ | ||
4139 | u16 screen_glyph(struct vc_data *vc, int offset) | ||
4140 | { | ||
4141 | u16 w = scr_readw(screenpos(vc, offset, 1)); | ||
4142 | u16 c = w & 0xff; | ||
4143 | |||
4144 | if (w & vc->vc_hi_font_mask) | ||
4145 | c |= 0x100; | ||
4146 | return c; | ||
4147 | } | ||
4148 | EXPORT_SYMBOL_GPL(screen_glyph); | ||
4149 | |||
4150 | /* used by vcs - note the word offset */ | ||
4151 | unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed) | ||
4152 | { | ||
4153 | return screenpos(vc, 2 * w_offset, viewed); | ||
4154 | } | ||
4155 | |||
4156 | void getconsxy(struct vc_data *vc, unsigned char *p) | ||
4157 | { | ||
4158 | p[0] = vc->vc_x; | ||
4159 | p[1] = vc->vc_y; | ||
4160 | } | ||
4161 | |||
4162 | void putconsxy(struct vc_data *vc, unsigned char *p) | ||
4163 | { | ||
4164 | hide_cursor(vc); | ||
4165 | gotoxy(vc, p[0], p[1]); | ||
4166 | set_cursor(vc); | ||
4167 | } | ||
4168 | |||
4169 | u16 vcs_scr_readw(struct vc_data *vc, const u16 *org) | ||
4170 | { | ||
4171 | if ((unsigned long)org == vc->vc_pos && softcursor_original != -1) | ||
4172 | return softcursor_original; | ||
4173 | return scr_readw(org); | ||
4174 | } | ||
4175 | |||
4176 | void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org) | ||
4177 | { | ||
4178 | scr_writew(val, org); | ||
4179 | if ((unsigned long)org == vc->vc_pos) { | ||
4180 | softcursor_original = -1; | ||
4181 | add_softcursor(vc); | ||
4182 | } | ||
4183 | } | ||
4184 | |||
4185 | void vcs_scr_updated(struct vc_data *vc) | ||
4186 | { | ||
4187 | notify_update(vc); | ||
4188 | } | ||
4189 | |||
4190 | /* | ||
4191 | * Visible symbols for modules | ||
4192 | */ | ||
4193 | |||
4194 | EXPORT_SYMBOL(color_table); | ||
4195 | EXPORT_SYMBOL(default_red); | ||
4196 | EXPORT_SYMBOL(default_grn); | ||
4197 | EXPORT_SYMBOL(default_blu); | ||
4198 | EXPORT_SYMBOL(update_region); | ||
4199 | EXPORT_SYMBOL(redraw_screen); | ||
4200 | EXPORT_SYMBOL(vc_resize); | ||
4201 | EXPORT_SYMBOL(fg_console); | ||
4202 | EXPORT_SYMBOL(console_blank_hook); | ||
4203 | EXPORT_SYMBOL(console_blanked); | ||
4204 | EXPORT_SYMBOL(vc_cons); | ||
4205 | EXPORT_SYMBOL(global_cursor_default); | ||
4206 | #ifndef VT_SINGLE_DRIVER | ||
4207 | EXPORT_SYMBOL(take_over_console); | ||
4208 | EXPORT_SYMBOL(give_up_console); | ||
4209 | #endif | ||
diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c new file mode 100644 index 000000000000..6b68a0fb4611 --- /dev/null +++ b/drivers/tty/vt/vt_ioctl.c | |||
@@ -0,0 +1,1788 @@ | |||
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 | |||
13 | #include <linux/types.h> | ||
14 | #include <linux/errno.h> | ||
15 | #include <linux/sched.h> | ||
16 | #include <linux/tty.h> | ||
17 | #include <linux/timer.h> | ||
18 | #include <linux/kernel.h> | ||
19 | #include <linux/compat.h> | ||
20 | #include <linux/module.h> | ||
21 | #include <linux/kd.h> | ||
22 | #include <linux/vt.h> | ||
23 | #include <linux/string.h> | ||
24 | #include <linux/slab.h> | ||
25 | #include <linux/major.h> | ||
26 | #include <linux/fs.h> | ||
27 | #include <linux/console.h> | ||
28 | #include <linux/consolemap.h> | ||
29 | #include <linux/signal.h> | ||
30 | #include <linux/smp_lock.h> | ||
31 | #include <linux/timex.h> | ||
32 | |||
33 | #include <asm/io.h> | ||
34 | #include <asm/uaccess.h> | ||
35 | |||
36 | #include <linux/kbd_kern.h> | ||
37 | #include <linux/vt_kern.h> | ||
38 | #include <linux/kbd_diacr.h> | ||
39 | #include <linux/selection.h> | ||
40 | |||
41 | char vt_dont_switch; | ||
42 | extern struct tty_driver *console_driver; | ||
43 | |||
44 | #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count) | ||
45 | #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons) | ||
46 | |||
47 | /* | ||
48 | * Console (vt and kd) routines, as defined by USL SVR4 manual, and by | ||
49 | * experimentation and study of X386 SYSV handling. | ||
50 | * | ||
51 | * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and | ||
52 | * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console, | ||
53 | * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will | ||
54 | * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to | ||
55 | * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using | ||
56 | * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing | ||
57 | * to the current console is done by the main ioctl code. | ||
58 | */ | ||
59 | |||
60 | #ifdef CONFIG_X86 | ||
61 | #include <linux/syscalls.h> | ||
62 | #endif | ||
63 | |||
64 | static void complete_change_console(struct vc_data *vc); | ||
65 | |||
66 | /* | ||
67 | * User space VT_EVENT handlers | ||
68 | */ | ||
69 | |||
70 | struct vt_event_wait { | ||
71 | struct list_head list; | ||
72 | struct vt_event event; | ||
73 | int done; | ||
74 | }; | ||
75 | |||
76 | static LIST_HEAD(vt_events); | ||
77 | static DEFINE_SPINLOCK(vt_event_lock); | ||
78 | static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue); | ||
79 | |||
80 | /** | ||
81 | * vt_event_post | ||
82 | * @event: the event that occurred | ||
83 | * @old: old console | ||
84 | * @new: new console | ||
85 | * | ||
86 | * Post an VT event to interested VT handlers | ||
87 | */ | ||
88 | |||
89 | void vt_event_post(unsigned int event, unsigned int old, unsigned int new) | ||
90 | { | ||
91 | struct list_head *pos, *head; | ||
92 | unsigned long flags; | ||
93 | int wake = 0; | ||
94 | |||
95 | spin_lock_irqsave(&vt_event_lock, flags); | ||
96 | head = &vt_events; | ||
97 | |||
98 | list_for_each(pos, head) { | ||
99 | struct vt_event_wait *ve = list_entry(pos, | ||
100 | struct vt_event_wait, list); | ||
101 | if (!(ve->event.event & event)) | ||
102 | continue; | ||
103 | ve->event.event = event; | ||
104 | /* kernel view is consoles 0..n-1, user space view is | ||
105 | console 1..n with 0 meaning current, so we must bias */ | ||
106 | ve->event.oldev = old + 1; | ||
107 | ve->event.newev = new + 1; | ||
108 | wake = 1; | ||
109 | ve->done = 1; | ||
110 | } | ||
111 | spin_unlock_irqrestore(&vt_event_lock, flags); | ||
112 | if (wake) | ||
113 | wake_up_interruptible(&vt_event_waitqueue); | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * vt_event_wait - wait for an event | ||
118 | * @vw: our event | ||
119 | * | ||
120 | * Waits for an event to occur which completes our vt_event_wait | ||
121 | * structure. On return the structure has wv->done set to 1 for success | ||
122 | * or 0 if some event such as a signal ended the wait. | ||
123 | */ | ||
124 | |||
125 | static void vt_event_wait(struct vt_event_wait *vw) | ||
126 | { | ||
127 | unsigned long flags; | ||
128 | /* Prepare the event */ | ||
129 | INIT_LIST_HEAD(&vw->list); | ||
130 | vw->done = 0; | ||
131 | /* Queue our event */ | ||
132 | spin_lock_irqsave(&vt_event_lock, flags); | ||
133 | list_add(&vw->list, &vt_events); | ||
134 | spin_unlock_irqrestore(&vt_event_lock, flags); | ||
135 | /* Wait for it to pass */ | ||
136 | wait_event_interruptible_tty(vt_event_waitqueue, vw->done); | ||
137 | /* Dequeue it */ | ||
138 | spin_lock_irqsave(&vt_event_lock, flags); | ||
139 | list_del(&vw->list); | ||
140 | spin_unlock_irqrestore(&vt_event_lock, flags); | ||
141 | } | ||
142 | |||
143 | /** | ||
144 | * vt_event_wait_ioctl - event ioctl handler | ||
145 | * @arg: argument to ioctl | ||
146 | * | ||
147 | * Implement the VT_WAITEVENT ioctl using the VT event interface | ||
148 | */ | ||
149 | |||
150 | static int vt_event_wait_ioctl(struct vt_event __user *event) | ||
151 | { | ||
152 | struct vt_event_wait vw; | ||
153 | |||
154 | if (copy_from_user(&vw.event, event, sizeof(struct vt_event))) | ||
155 | return -EFAULT; | ||
156 | /* Highest supported event for now */ | ||
157 | if (vw.event.event & ~VT_MAX_EVENT) | ||
158 | return -EINVAL; | ||
159 | |||
160 | vt_event_wait(&vw); | ||
161 | /* If it occurred report it */ | ||
162 | if (vw.done) { | ||
163 | if (copy_to_user(event, &vw.event, sizeof(struct vt_event))) | ||
164 | return -EFAULT; | ||
165 | return 0; | ||
166 | } | ||
167 | return -EINTR; | ||
168 | } | ||
169 | |||
170 | /** | ||
171 | * vt_waitactive - active console wait | ||
172 | * @event: event code | ||
173 | * @n: new console | ||
174 | * | ||
175 | * Helper for event waits. Used to implement the legacy | ||
176 | * event waiting ioctls in terms of events | ||
177 | */ | ||
178 | |||
179 | int vt_waitactive(int n) | ||
180 | { | ||
181 | struct vt_event_wait vw; | ||
182 | do { | ||
183 | if (n == fg_console + 1) | ||
184 | break; | ||
185 | vw.event.event = VT_EVENT_SWITCH; | ||
186 | vt_event_wait(&vw); | ||
187 | if (vw.done == 0) | ||
188 | return -EINTR; | ||
189 | } while (vw.event.newev != n); | ||
190 | return 0; | ||
191 | } | ||
192 | |||
193 | /* | ||
194 | * these are the valid i/o ports we're allowed to change. they map all the | ||
195 | * video ports | ||
196 | */ | ||
197 | #define GPFIRST 0x3b4 | ||
198 | #define GPLAST 0x3df | ||
199 | #define GPNUM (GPLAST - GPFIRST + 1) | ||
200 | |||
201 | #define i (tmp.kb_index) | ||
202 | #define s (tmp.kb_table) | ||
203 | #define v (tmp.kb_value) | ||
204 | static inline int | ||
205 | do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd) | ||
206 | { | ||
207 | struct kbentry tmp; | ||
208 | ushort *key_map, val, ov; | ||
209 | |||
210 | if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry))) | ||
211 | return -EFAULT; | ||
212 | |||
213 | if (!capable(CAP_SYS_TTY_CONFIG)) | ||
214 | perm = 0; | ||
215 | |||
216 | switch (cmd) { | ||
217 | case KDGKBENT: | ||
218 | key_map = key_maps[s]; | ||
219 | if (key_map) { | ||
220 | val = U(key_map[i]); | ||
221 | if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES) | ||
222 | val = K_HOLE; | ||
223 | } else | ||
224 | val = (i ? K_HOLE : K_NOSUCHMAP); | ||
225 | return put_user(val, &user_kbe->kb_value); | ||
226 | case KDSKBENT: | ||
227 | if (!perm) | ||
228 | return -EPERM; | ||
229 | if (!i && v == K_NOSUCHMAP) { | ||
230 | /* deallocate map */ | ||
231 | key_map = key_maps[s]; | ||
232 | if (s && key_map) { | ||
233 | key_maps[s] = NULL; | ||
234 | if (key_map[0] == U(K_ALLOCATED)) { | ||
235 | kfree(key_map); | ||
236 | keymap_count--; | ||
237 | } | ||
238 | } | ||
239 | break; | ||
240 | } | ||
241 | |||
242 | if (KTYP(v) < NR_TYPES) { | ||
243 | if (KVAL(v) > max_vals[KTYP(v)]) | ||
244 | return -EINVAL; | ||
245 | } else | ||
246 | if (kbd->kbdmode != VC_UNICODE) | ||
247 | return -EINVAL; | ||
248 | |||
249 | /* ++Geert: non-PC keyboards may generate keycode zero */ | ||
250 | #if !defined(__mc68000__) && !defined(__powerpc__) | ||
251 | /* assignment to entry 0 only tests validity of args */ | ||
252 | if (!i) | ||
253 | break; | ||
254 | #endif | ||
255 | |||
256 | if (!(key_map = key_maps[s])) { | ||
257 | int j; | ||
258 | |||
259 | if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && | ||
260 | !capable(CAP_SYS_RESOURCE)) | ||
261 | return -EPERM; | ||
262 | |||
263 | key_map = kmalloc(sizeof(plain_map), | ||
264 | GFP_KERNEL); | ||
265 | if (!key_map) | ||
266 | return -ENOMEM; | ||
267 | key_maps[s] = key_map; | ||
268 | key_map[0] = U(K_ALLOCATED); | ||
269 | for (j = 1; j < NR_KEYS; j++) | ||
270 | key_map[j] = U(K_HOLE); | ||
271 | keymap_count++; | ||
272 | } | ||
273 | ov = U(key_map[i]); | ||
274 | if (v == ov) | ||
275 | break; /* nothing to do */ | ||
276 | /* | ||
277 | * Attention Key. | ||
278 | */ | ||
279 | if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN)) | ||
280 | return -EPERM; | ||
281 | key_map[i] = U(v); | ||
282 | if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT)) | ||
283 | compute_shiftstate(); | ||
284 | break; | ||
285 | } | ||
286 | return 0; | ||
287 | } | ||
288 | #undef i | ||
289 | #undef s | ||
290 | #undef v | ||
291 | |||
292 | static inline int | ||
293 | do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm) | ||
294 | { | ||
295 | struct kbkeycode tmp; | ||
296 | int kc = 0; | ||
297 | |||
298 | if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode))) | ||
299 | return -EFAULT; | ||
300 | switch (cmd) { | ||
301 | case KDGETKEYCODE: | ||
302 | kc = getkeycode(tmp.scancode); | ||
303 | if (kc >= 0) | ||
304 | kc = put_user(kc, &user_kbkc->keycode); | ||
305 | break; | ||
306 | case KDSETKEYCODE: | ||
307 | if (!perm) | ||
308 | return -EPERM; | ||
309 | kc = setkeycode(tmp.scancode, tmp.keycode); | ||
310 | break; | ||
311 | } | ||
312 | return kc; | ||
313 | } | ||
314 | |||
315 | static inline int | ||
316 | do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) | ||
317 | { | ||
318 | struct kbsentry *kbs; | ||
319 | char *p; | ||
320 | u_char *q; | ||
321 | u_char __user *up; | ||
322 | int sz; | ||
323 | int delta; | ||
324 | char *first_free, *fj, *fnw; | ||
325 | int i, j, k; | ||
326 | int ret; | ||
327 | |||
328 | if (!capable(CAP_SYS_TTY_CONFIG)) | ||
329 | perm = 0; | ||
330 | |||
331 | kbs = kmalloc(sizeof(*kbs), GFP_KERNEL); | ||
332 | if (!kbs) { | ||
333 | ret = -ENOMEM; | ||
334 | goto reterr; | ||
335 | } | ||
336 | |||
337 | /* we mostly copy too much here (512bytes), but who cares ;) */ | ||
338 | if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) { | ||
339 | ret = -EFAULT; | ||
340 | goto reterr; | ||
341 | } | ||
342 | kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0'; | ||
343 | i = kbs->kb_func; | ||
344 | |||
345 | switch (cmd) { | ||
346 | case KDGKBSENT: | ||
347 | sz = sizeof(kbs->kb_string) - 1; /* sz should have been | ||
348 | a struct member */ | ||
349 | up = user_kdgkb->kb_string; | ||
350 | p = func_table[i]; | ||
351 | if(p) | ||
352 | for ( ; *p && sz; p++, sz--) | ||
353 | if (put_user(*p, up++)) { | ||
354 | ret = -EFAULT; | ||
355 | goto reterr; | ||
356 | } | ||
357 | if (put_user('\0', up)) { | ||
358 | ret = -EFAULT; | ||
359 | goto reterr; | ||
360 | } | ||
361 | kfree(kbs); | ||
362 | return ((p && *p) ? -EOVERFLOW : 0); | ||
363 | case KDSKBSENT: | ||
364 | if (!perm) { | ||
365 | ret = -EPERM; | ||
366 | goto reterr; | ||
367 | } | ||
368 | |||
369 | q = func_table[i]; | ||
370 | first_free = funcbufptr + (funcbufsize - funcbufleft); | ||
371 | for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++) | ||
372 | ; | ||
373 | if (j < MAX_NR_FUNC) | ||
374 | fj = func_table[j]; | ||
375 | else | ||
376 | fj = first_free; | ||
377 | |||
378 | delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string); | ||
379 | if (delta <= funcbufleft) { /* it fits in current buf */ | ||
380 | if (j < MAX_NR_FUNC) { | ||
381 | memmove(fj + delta, fj, first_free - fj); | ||
382 | for (k = j; k < MAX_NR_FUNC; k++) | ||
383 | if (func_table[k]) | ||
384 | func_table[k] += delta; | ||
385 | } | ||
386 | if (!q) | ||
387 | func_table[i] = fj; | ||
388 | funcbufleft -= delta; | ||
389 | } else { /* allocate a larger buffer */ | ||
390 | sz = 256; | ||
391 | while (sz < funcbufsize - funcbufleft + delta) | ||
392 | sz <<= 1; | ||
393 | fnw = kmalloc(sz, GFP_KERNEL); | ||
394 | if(!fnw) { | ||
395 | ret = -ENOMEM; | ||
396 | goto reterr; | ||
397 | } | ||
398 | |||
399 | if (!q) | ||
400 | func_table[i] = fj; | ||
401 | if (fj > funcbufptr) | ||
402 | memmove(fnw, funcbufptr, fj - funcbufptr); | ||
403 | for (k = 0; k < j; k++) | ||
404 | if (func_table[k]) | ||
405 | func_table[k] = fnw + (func_table[k] - funcbufptr); | ||
406 | |||
407 | if (first_free > fj) { | ||
408 | memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj); | ||
409 | for (k = j; k < MAX_NR_FUNC; k++) | ||
410 | if (func_table[k]) | ||
411 | func_table[k] = fnw + (func_table[k] - funcbufptr) + delta; | ||
412 | } | ||
413 | if (funcbufptr != func_buf) | ||
414 | kfree(funcbufptr); | ||
415 | funcbufptr = fnw; | ||
416 | funcbufleft = funcbufleft - delta + sz - funcbufsize; | ||
417 | funcbufsize = sz; | ||
418 | } | ||
419 | strcpy(func_table[i], kbs->kb_string); | ||
420 | break; | ||
421 | } | ||
422 | ret = 0; | ||
423 | reterr: | ||
424 | kfree(kbs); | ||
425 | return ret; | ||
426 | } | ||
427 | |||
428 | static inline int | ||
429 | do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op) | ||
430 | { | ||
431 | struct consolefontdesc cfdarg; | ||
432 | int i; | ||
433 | |||
434 | if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc))) | ||
435 | return -EFAULT; | ||
436 | |||
437 | switch (cmd) { | ||
438 | case PIO_FONTX: | ||
439 | if (!perm) | ||
440 | return -EPERM; | ||
441 | op->op = KD_FONT_OP_SET; | ||
442 | op->flags = KD_FONT_FLAG_OLD; | ||
443 | op->width = 8; | ||
444 | op->height = cfdarg.charheight; | ||
445 | op->charcount = cfdarg.charcount; | ||
446 | op->data = cfdarg.chardata; | ||
447 | return con_font_op(vc_cons[fg_console].d, op); | ||
448 | case GIO_FONTX: { | ||
449 | op->op = KD_FONT_OP_GET; | ||
450 | op->flags = KD_FONT_FLAG_OLD; | ||
451 | op->width = 8; | ||
452 | op->height = cfdarg.charheight; | ||
453 | op->charcount = cfdarg.charcount; | ||
454 | op->data = cfdarg.chardata; | ||
455 | i = con_font_op(vc_cons[fg_console].d, op); | ||
456 | if (i) | ||
457 | return i; | ||
458 | cfdarg.charheight = op->height; | ||
459 | cfdarg.charcount = op->charcount; | ||
460 | if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc))) | ||
461 | return -EFAULT; | ||
462 | return 0; | ||
463 | } | ||
464 | } | ||
465 | return -EINVAL; | ||
466 | } | ||
467 | |||
468 | static inline int | ||
469 | do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc) | ||
470 | { | ||
471 | struct unimapdesc tmp; | ||
472 | |||
473 | if (copy_from_user(&tmp, user_ud, sizeof tmp)) | ||
474 | return -EFAULT; | ||
475 | if (tmp.entries) | ||
476 | if (!access_ok(VERIFY_WRITE, tmp.entries, | ||
477 | tmp.entry_ct*sizeof(struct unipair))) | ||
478 | return -EFAULT; | ||
479 | switch (cmd) { | ||
480 | case PIO_UNIMAP: | ||
481 | if (!perm) | ||
482 | return -EPERM; | ||
483 | return con_set_unimap(vc, tmp.entry_ct, tmp.entries); | ||
484 | case GIO_UNIMAP: | ||
485 | if (!perm && fg_console != vc->vc_num) | ||
486 | return -EPERM; | ||
487 | return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries); | ||
488 | } | ||
489 | return 0; | ||
490 | } | ||
491 | |||
492 | |||
493 | |||
494 | /* | ||
495 | * We handle the console-specific ioctl's here. We allow the | ||
496 | * capability to modify any console, not just the fg_console. | ||
497 | */ | ||
498 | int vt_ioctl(struct tty_struct *tty, struct file * file, | ||
499 | unsigned int cmd, unsigned long arg) | ||
500 | { | ||
501 | struct vc_data *vc = tty->driver_data; | ||
502 | struct console_font_op op; /* used in multiple places here */ | ||
503 | struct kbd_struct * kbd; | ||
504 | unsigned int console; | ||
505 | unsigned char ucval; | ||
506 | unsigned int uival; | ||
507 | void __user *up = (void __user *)arg; | ||
508 | int i, perm; | ||
509 | int ret = 0; | ||
510 | |||
511 | console = vc->vc_num; | ||
512 | |||
513 | tty_lock(); | ||
514 | |||
515 | if (!vc_cons_allocated(console)) { /* impossible? */ | ||
516 | ret = -ENOIOCTLCMD; | ||
517 | goto out; | ||
518 | } | ||
519 | |||
520 | |||
521 | /* | ||
522 | * To have permissions to do most of the vt ioctls, we either have | ||
523 | * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG. | ||
524 | */ | ||
525 | perm = 0; | ||
526 | if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG)) | ||
527 | perm = 1; | ||
528 | |||
529 | kbd = kbd_table + console; | ||
530 | switch (cmd) { | ||
531 | case TIOCLINUX: | ||
532 | ret = tioclinux(tty, arg); | ||
533 | break; | ||
534 | case KIOCSOUND: | ||
535 | if (!perm) | ||
536 | goto eperm; | ||
537 | /* | ||
538 | * The use of PIT_TICK_RATE is historic, it used to be | ||
539 | * the platform-dependent CLOCK_TICK_RATE between 2.6.12 | ||
540 | * and 2.6.36, which was a minor but unfortunate ABI | ||
541 | * change. | ||
542 | */ | ||
543 | if (arg) | ||
544 | arg = PIT_TICK_RATE / arg; | ||
545 | kd_mksound(arg, 0); | ||
546 | break; | ||
547 | |||
548 | case KDMKTONE: | ||
549 | if (!perm) | ||
550 | goto eperm; | ||
551 | { | ||
552 | unsigned int ticks, count; | ||
553 | |||
554 | /* | ||
555 | * Generate the tone for the appropriate number of ticks. | ||
556 | * If the time is zero, turn off sound ourselves. | ||
557 | */ | ||
558 | ticks = HZ * ((arg >> 16) & 0xffff) / 1000; | ||
559 | count = ticks ? (arg & 0xffff) : 0; | ||
560 | if (count) | ||
561 | count = PIT_TICK_RATE / count; | ||
562 | kd_mksound(count, ticks); | ||
563 | break; | ||
564 | } | ||
565 | |||
566 | case KDGKBTYPE: | ||
567 | /* | ||
568 | * this is naive. | ||
569 | */ | ||
570 | ucval = KB_101; | ||
571 | goto setchar; | ||
572 | |||
573 | /* | ||
574 | * These cannot be implemented on any machine that implements | ||
575 | * ioperm() in user level (such as Alpha PCs) or not at all. | ||
576 | * | ||
577 | * XXX: you should never use these, just call ioperm directly.. | ||
578 | */ | ||
579 | #ifdef CONFIG_X86 | ||
580 | case KDADDIO: | ||
581 | case KDDELIO: | ||
582 | /* | ||
583 | * KDADDIO and KDDELIO may be able to add ports beyond what | ||
584 | * we reject here, but to be safe... | ||
585 | */ | ||
586 | if (arg < GPFIRST || arg > GPLAST) { | ||
587 | ret = -EINVAL; | ||
588 | break; | ||
589 | } | ||
590 | ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0; | ||
591 | break; | ||
592 | |||
593 | case KDENABIO: | ||
594 | case KDDISABIO: | ||
595 | ret = sys_ioperm(GPFIRST, GPNUM, | ||
596 | (cmd == KDENABIO)) ? -ENXIO : 0; | ||
597 | break; | ||
598 | #endif | ||
599 | |||
600 | /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */ | ||
601 | |||
602 | case KDKBDREP: | ||
603 | { | ||
604 | struct kbd_repeat kbrep; | ||
605 | |||
606 | if (!capable(CAP_SYS_TTY_CONFIG)) | ||
607 | goto eperm; | ||
608 | |||
609 | if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) { | ||
610 | ret = -EFAULT; | ||
611 | break; | ||
612 | } | ||
613 | ret = kbd_rate(&kbrep); | ||
614 | if (ret) | ||
615 | break; | ||
616 | if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat))) | ||
617 | ret = -EFAULT; | ||
618 | break; | ||
619 | } | ||
620 | |||
621 | case KDSETMODE: | ||
622 | /* | ||
623 | * currently, setting the mode from KD_TEXT to KD_GRAPHICS | ||
624 | * doesn't do a whole lot. i'm not sure if it should do any | ||
625 | * restoration of modes or what... | ||
626 | * | ||
627 | * XXX It should at least call into the driver, fbdev's definitely | ||
628 | * need to restore their engine state. --BenH | ||
629 | */ | ||
630 | if (!perm) | ||
631 | goto eperm; | ||
632 | switch (arg) { | ||
633 | case KD_GRAPHICS: | ||
634 | break; | ||
635 | case KD_TEXT0: | ||
636 | case KD_TEXT1: | ||
637 | arg = KD_TEXT; | ||
638 | case KD_TEXT: | ||
639 | break; | ||
640 | default: | ||
641 | ret = -EINVAL; | ||
642 | goto out; | ||
643 | } | ||
644 | if (vc->vc_mode == (unsigned char) arg) | ||
645 | break; | ||
646 | vc->vc_mode = (unsigned char) arg; | ||
647 | if (console != fg_console) | ||
648 | break; | ||
649 | /* | ||
650 | * explicitly blank/unblank the screen if switching modes | ||
651 | */ | ||
652 | acquire_console_sem(); | ||
653 | if (arg == KD_TEXT) | ||
654 | do_unblank_screen(1); | ||
655 | else | ||
656 | do_blank_screen(1); | ||
657 | release_console_sem(); | ||
658 | break; | ||
659 | |||
660 | case KDGETMODE: | ||
661 | uival = vc->vc_mode; | ||
662 | goto setint; | ||
663 | |||
664 | case KDMAPDISP: | ||
665 | case KDUNMAPDISP: | ||
666 | /* | ||
667 | * these work like a combination of mmap and KDENABIO. | ||
668 | * this could be easily finished. | ||
669 | */ | ||
670 | ret = -EINVAL; | ||
671 | break; | ||
672 | |||
673 | case KDSKBMODE: | ||
674 | if (!perm) | ||
675 | goto eperm; | ||
676 | switch(arg) { | ||
677 | case K_RAW: | ||
678 | kbd->kbdmode = VC_RAW; | ||
679 | break; | ||
680 | case K_MEDIUMRAW: | ||
681 | kbd->kbdmode = VC_MEDIUMRAW; | ||
682 | break; | ||
683 | case K_XLATE: | ||
684 | kbd->kbdmode = VC_XLATE; | ||
685 | compute_shiftstate(); | ||
686 | break; | ||
687 | case K_UNICODE: | ||
688 | kbd->kbdmode = VC_UNICODE; | ||
689 | compute_shiftstate(); | ||
690 | break; | ||
691 | default: | ||
692 | ret = -EINVAL; | ||
693 | goto out; | ||
694 | } | ||
695 | tty_ldisc_flush(tty); | ||
696 | break; | ||
697 | |||
698 | case KDGKBMODE: | ||
699 | uival = ((kbd->kbdmode == VC_RAW) ? K_RAW : | ||
700 | (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW : | ||
701 | (kbd->kbdmode == VC_UNICODE) ? K_UNICODE : | ||
702 | K_XLATE); | ||
703 | goto setint; | ||
704 | |||
705 | /* this could be folded into KDSKBMODE, but for compatibility | ||
706 | reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */ | ||
707 | case KDSKBMETA: | ||
708 | switch(arg) { | ||
709 | case K_METABIT: | ||
710 | clr_vc_kbd_mode(kbd, VC_META); | ||
711 | break; | ||
712 | case K_ESCPREFIX: | ||
713 | set_vc_kbd_mode(kbd, VC_META); | ||
714 | break; | ||
715 | default: | ||
716 | ret = -EINVAL; | ||
717 | } | ||
718 | break; | ||
719 | |||
720 | case KDGKBMETA: | ||
721 | uival = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT); | ||
722 | setint: | ||
723 | ret = put_user(uival, (int __user *)arg); | ||
724 | break; | ||
725 | |||
726 | case KDGETKEYCODE: | ||
727 | case KDSETKEYCODE: | ||
728 | if(!capable(CAP_SYS_TTY_CONFIG)) | ||
729 | perm = 0; | ||
730 | ret = do_kbkeycode_ioctl(cmd, up, perm); | ||
731 | break; | ||
732 | |||
733 | case KDGKBENT: | ||
734 | case KDSKBENT: | ||
735 | ret = do_kdsk_ioctl(cmd, up, perm, kbd); | ||
736 | break; | ||
737 | |||
738 | case KDGKBSENT: | ||
739 | case KDSKBSENT: | ||
740 | ret = do_kdgkb_ioctl(cmd, up, perm); | ||
741 | break; | ||
742 | |||
743 | case KDGKBDIACR: | ||
744 | { | ||
745 | struct kbdiacrs __user *a = up; | ||
746 | struct kbdiacr diacr; | ||
747 | int i; | ||
748 | |||
749 | if (put_user(accent_table_size, &a->kb_cnt)) { | ||
750 | ret = -EFAULT; | ||
751 | break; | ||
752 | } | ||
753 | for (i = 0; i < accent_table_size; i++) { | ||
754 | diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr); | ||
755 | diacr.base = conv_uni_to_8bit(accent_table[i].base); | ||
756 | diacr.result = conv_uni_to_8bit(accent_table[i].result); | ||
757 | if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) { | ||
758 | ret = -EFAULT; | ||
759 | break; | ||
760 | } | ||
761 | } | ||
762 | break; | ||
763 | } | ||
764 | case KDGKBDIACRUC: | ||
765 | { | ||
766 | struct kbdiacrsuc __user *a = up; | ||
767 | |||
768 | if (put_user(accent_table_size, &a->kb_cnt)) | ||
769 | ret = -EFAULT; | ||
770 | else if (copy_to_user(a->kbdiacruc, accent_table, | ||
771 | accent_table_size*sizeof(struct kbdiacruc))) | ||
772 | ret = -EFAULT; | ||
773 | break; | ||
774 | } | ||
775 | |||
776 | case KDSKBDIACR: | ||
777 | { | ||
778 | struct kbdiacrs __user *a = up; | ||
779 | struct kbdiacr diacr; | ||
780 | unsigned int ct; | ||
781 | int i; | ||
782 | |||
783 | if (!perm) | ||
784 | goto eperm; | ||
785 | if (get_user(ct,&a->kb_cnt)) { | ||
786 | ret = -EFAULT; | ||
787 | break; | ||
788 | } | ||
789 | if (ct >= MAX_DIACR) { | ||
790 | ret = -EINVAL; | ||
791 | break; | ||
792 | } | ||
793 | accent_table_size = ct; | ||
794 | for (i = 0; i < ct; i++) { | ||
795 | if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) { | ||
796 | ret = -EFAULT; | ||
797 | break; | ||
798 | } | ||
799 | accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr); | ||
800 | accent_table[i].base = conv_8bit_to_uni(diacr.base); | ||
801 | accent_table[i].result = conv_8bit_to_uni(diacr.result); | ||
802 | } | ||
803 | break; | ||
804 | } | ||
805 | |||
806 | case KDSKBDIACRUC: | ||
807 | { | ||
808 | struct kbdiacrsuc __user *a = up; | ||
809 | unsigned int ct; | ||
810 | |||
811 | if (!perm) | ||
812 | goto eperm; | ||
813 | if (get_user(ct,&a->kb_cnt)) { | ||
814 | ret = -EFAULT; | ||
815 | break; | ||
816 | } | ||
817 | if (ct >= MAX_DIACR) { | ||
818 | ret = -EINVAL; | ||
819 | break; | ||
820 | } | ||
821 | accent_table_size = ct; | ||
822 | if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc))) | ||
823 | ret = -EFAULT; | ||
824 | break; | ||
825 | } | ||
826 | |||
827 | /* the ioctls below read/set the flags usually shown in the leds */ | ||
828 | /* don't use them - they will go away without warning */ | ||
829 | case KDGKBLED: | ||
830 | ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4); | ||
831 | goto setchar; | ||
832 | |||
833 | case KDSKBLED: | ||
834 | if (!perm) | ||
835 | goto eperm; | ||
836 | if (arg & ~0x77) { | ||
837 | ret = -EINVAL; | ||
838 | break; | ||
839 | } | ||
840 | kbd->ledflagstate = (arg & 7); | ||
841 | kbd->default_ledflagstate = ((arg >> 4) & 7); | ||
842 | set_leds(); | ||
843 | break; | ||
844 | |||
845 | /* the ioctls below only set the lights, not the functions */ | ||
846 | /* for those, see KDGKBLED and KDSKBLED above */ | ||
847 | case KDGETLED: | ||
848 | ucval = getledstate(); | ||
849 | setchar: | ||
850 | ret = put_user(ucval, (char __user *)arg); | ||
851 | break; | ||
852 | |||
853 | case KDSETLED: | ||
854 | if (!perm) | ||
855 | goto eperm; | ||
856 | setledstate(kbd, arg); | ||
857 | break; | ||
858 | |||
859 | /* | ||
860 | * A process can indicate its willingness to accept signals | ||
861 | * generated by pressing an appropriate key combination. | ||
862 | * Thus, one can have a daemon that e.g. spawns a new console | ||
863 | * upon a keypress and then changes to it. | ||
864 | * See also the kbrequest field of inittab(5). | ||
865 | */ | ||
866 | case KDSIGACCEPT: | ||
867 | { | ||
868 | if (!perm || !capable(CAP_KILL)) | ||
869 | goto eperm; | ||
870 | if (!valid_signal(arg) || arg < 1 || arg == SIGKILL) | ||
871 | ret = -EINVAL; | ||
872 | else { | ||
873 | spin_lock_irq(&vt_spawn_con.lock); | ||
874 | put_pid(vt_spawn_con.pid); | ||
875 | vt_spawn_con.pid = get_pid(task_pid(current)); | ||
876 | vt_spawn_con.sig = arg; | ||
877 | spin_unlock_irq(&vt_spawn_con.lock); | ||
878 | } | ||
879 | break; | ||
880 | } | ||
881 | |||
882 | case VT_SETMODE: | ||
883 | { | ||
884 | struct vt_mode tmp; | ||
885 | |||
886 | if (!perm) | ||
887 | goto eperm; | ||
888 | if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) { | ||
889 | ret = -EFAULT; | ||
890 | goto out; | ||
891 | } | ||
892 | if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) { | ||
893 | ret = -EINVAL; | ||
894 | goto out; | ||
895 | } | ||
896 | acquire_console_sem(); | ||
897 | vc->vt_mode = tmp; | ||
898 | /* the frsig is ignored, so we set it to 0 */ | ||
899 | vc->vt_mode.frsig = 0; | ||
900 | put_pid(vc->vt_pid); | ||
901 | vc->vt_pid = get_pid(task_pid(current)); | ||
902 | /* no switch is required -- saw@shade.msu.ru */ | ||
903 | vc->vt_newvt = -1; | ||
904 | release_console_sem(); | ||
905 | break; | ||
906 | } | ||
907 | |||
908 | case VT_GETMODE: | ||
909 | { | ||
910 | struct vt_mode tmp; | ||
911 | int rc; | ||
912 | |||
913 | acquire_console_sem(); | ||
914 | memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode)); | ||
915 | release_console_sem(); | ||
916 | |||
917 | rc = copy_to_user(up, &tmp, sizeof(struct vt_mode)); | ||
918 | if (rc) | ||
919 | ret = -EFAULT; | ||
920 | break; | ||
921 | } | ||
922 | |||
923 | /* | ||
924 | * Returns global vt state. Note that VT 0 is always open, since | ||
925 | * it's an alias for the current VT, and people can't use it here. | ||
926 | * We cannot return state for more than 16 VTs, since v_state is short. | ||
927 | */ | ||
928 | case VT_GETSTATE: | ||
929 | { | ||
930 | struct vt_stat __user *vtstat = up; | ||
931 | unsigned short state, mask; | ||
932 | |||
933 | if (put_user(fg_console + 1, &vtstat->v_active)) | ||
934 | ret = -EFAULT; | ||
935 | else { | ||
936 | state = 1; /* /dev/tty0 is always open */ | ||
937 | for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; | ||
938 | ++i, mask <<= 1) | ||
939 | if (VT_IS_IN_USE(i)) | ||
940 | state |= mask; | ||
941 | ret = put_user(state, &vtstat->v_state); | ||
942 | } | ||
943 | break; | ||
944 | } | ||
945 | |||
946 | /* | ||
947 | * Returns the first available (non-opened) console. | ||
948 | */ | ||
949 | case VT_OPENQRY: | ||
950 | for (i = 0; i < MAX_NR_CONSOLES; ++i) | ||
951 | if (! VT_IS_IN_USE(i)) | ||
952 | break; | ||
953 | uival = i < MAX_NR_CONSOLES ? (i+1) : -1; | ||
954 | goto setint; | ||
955 | |||
956 | /* | ||
957 | * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num, | ||
958 | * with num >= 1 (switches to vt 0, our console, are not allowed, just | ||
959 | * to preserve sanity). | ||
960 | */ | ||
961 | case VT_ACTIVATE: | ||
962 | if (!perm) | ||
963 | goto eperm; | ||
964 | if (arg == 0 || arg > MAX_NR_CONSOLES) | ||
965 | ret = -ENXIO; | ||
966 | else { | ||
967 | arg--; | ||
968 | acquire_console_sem(); | ||
969 | ret = vc_allocate(arg); | ||
970 | release_console_sem(); | ||
971 | if (ret) | ||
972 | break; | ||
973 | set_console(arg); | ||
974 | } | ||
975 | break; | ||
976 | |||
977 | case VT_SETACTIVATE: | ||
978 | { | ||
979 | struct vt_setactivate vsa; | ||
980 | |||
981 | if (!perm) | ||
982 | goto eperm; | ||
983 | |||
984 | if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg, | ||
985 | sizeof(struct vt_setactivate))) { | ||
986 | ret = -EFAULT; | ||
987 | goto out; | ||
988 | } | ||
989 | if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES) | ||
990 | ret = -ENXIO; | ||
991 | else { | ||
992 | vsa.console--; | ||
993 | acquire_console_sem(); | ||
994 | ret = vc_allocate(vsa.console); | ||
995 | if (ret == 0) { | ||
996 | struct vc_data *nvc; | ||
997 | /* This is safe providing we don't drop the | ||
998 | console sem between vc_allocate and | ||
999 | finishing referencing nvc */ | ||
1000 | nvc = vc_cons[vsa.console].d; | ||
1001 | nvc->vt_mode = vsa.mode; | ||
1002 | nvc->vt_mode.frsig = 0; | ||
1003 | put_pid(nvc->vt_pid); | ||
1004 | nvc->vt_pid = get_pid(task_pid(current)); | ||
1005 | } | ||
1006 | release_console_sem(); | ||
1007 | if (ret) | ||
1008 | break; | ||
1009 | /* Commence switch and lock */ | ||
1010 | set_console(arg); | ||
1011 | } | ||
1012 | } | ||
1013 | |||
1014 | /* | ||
1015 | * wait until the specified VT has been activated | ||
1016 | */ | ||
1017 | case VT_WAITACTIVE: | ||
1018 | if (!perm) | ||
1019 | goto eperm; | ||
1020 | if (arg == 0 || arg > MAX_NR_CONSOLES) | ||
1021 | ret = -ENXIO; | ||
1022 | else | ||
1023 | ret = vt_waitactive(arg); | ||
1024 | break; | ||
1025 | |||
1026 | /* | ||
1027 | * If a vt is under process control, the kernel will not switch to it | ||
1028 | * immediately, but postpone the operation until the process calls this | ||
1029 | * ioctl, allowing the switch to complete. | ||
1030 | * | ||
1031 | * According to the X sources this is the behavior: | ||
1032 | * 0: pending switch-from not OK | ||
1033 | * 1: pending switch-from OK | ||
1034 | * 2: completed switch-to OK | ||
1035 | */ | ||
1036 | case VT_RELDISP: | ||
1037 | if (!perm) | ||
1038 | goto eperm; | ||
1039 | |||
1040 | if (vc->vt_mode.mode != VT_PROCESS) { | ||
1041 | ret = -EINVAL; | ||
1042 | break; | ||
1043 | } | ||
1044 | /* | ||
1045 | * Switching-from response | ||
1046 | */ | ||
1047 | acquire_console_sem(); | ||
1048 | if (vc->vt_newvt >= 0) { | ||
1049 | if (arg == 0) | ||
1050 | /* | ||
1051 | * Switch disallowed, so forget we were trying | ||
1052 | * to do it. | ||
1053 | */ | ||
1054 | vc->vt_newvt = -1; | ||
1055 | |||
1056 | else { | ||
1057 | /* | ||
1058 | * The current vt has been released, so | ||
1059 | * complete the switch. | ||
1060 | */ | ||
1061 | int newvt; | ||
1062 | newvt = vc->vt_newvt; | ||
1063 | vc->vt_newvt = -1; | ||
1064 | ret = vc_allocate(newvt); | ||
1065 | if (ret) { | ||
1066 | release_console_sem(); | ||
1067 | break; | ||
1068 | } | ||
1069 | /* | ||
1070 | * When we actually do the console switch, | ||
1071 | * make sure we are atomic with respect to | ||
1072 | * other console switches.. | ||
1073 | */ | ||
1074 | complete_change_console(vc_cons[newvt].d); | ||
1075 | } | ||
1076 | } else { | ||
1077 | /* | ||
1078 | * Switched-to response | ||
1079 | */ | ||
1080 | /* | ||
1081 | * If it's just an ACK, ignore it | ||
1082 | */ | ||
1083 | if (arg != VT_ACKACQ) | ||
1084 | ret = -EINVAL; | ||
1085 | } | ||
1086 | release_console_sem(); | ||
1087 | break; | ||
1088 | |||
1089 | /* | ||
1090 | * Disallocate memory associated to VT (but leave VT1) | ||
1091 | */ | ||
1092 | case VT_DISALLOCATE: | ||
1093 | if (arg > MAX_NR_CONSOLES) { | ||
1094 | ret = -ENXIO; | ||
1095 | break; | ||
1096 | } | ||
1097 | if (arg == 0) { | ||
1098 | /* deallocate all unused consoles, but leave 0 */ | ||
1099 | acquire_console_sem(); | ||
1100 | for (i=1; i<MAX_NR_CONSOLES; i++) | ||
1101 | if (! VT_BUSY(i)) | ||
1102 | vc_deallocate(i); | ||
1103 | release_console_sem(); | ||
1104 | } else { | ||
1105 | /* deallocate a single console, if possible */ | ||
1106 | arg--; | ||
1107 | if (VT_BUSY(arg)) | ||
1108 | ret = -EBUSY; | ||
1109 | else if (arg) { /* leave 0 */ | ||
1110 | acquire_console_sem(); | ||
1111 | vc_deallocate(arg); | ||
1112 | release_console_sem(); | ||
1113 | } | ||
1114 | } | ||
1115 | break; | ||
1116 | |||
1117 | case VT_RESIZE: | ||
1118 | { | ||
1119 | struct vt_sizes __user *vtsizes = up; | ||
1120 | struct vc_data *vc; | ||
1121 | |||
1122 | ushort ll,cc; | ||
1123 | if (!perm) | ||
1124 | goto eperm; | ||
1125 | if (get_user(ll, &vtsizes->v_rows) || | ||
1126 | get_user(cc, &vtsizes->v_cols)) | ||
1127 | ret = -EFAULT; | ||
1128 | else { | ||
1129 | acquire_console_sem(); | ||
1130 | for (i = 0; i < MAX_NR_CONSOLES; i++) { | ||
1131 | vc = vc_cons[i].d; | ||
1132 | |||
1133 | if (vc) { | ||
1134 | vc->vc_resize_user = 1; | ||
1135 | vc_resize(vc_cons[i].d, cc, ll); | ||
1136 | } | ||
1137 | } | ||
1138 | release_console_sem(); | ||
1139 | } | ||
1140 | break; | ||
1141 | } | ||
1142 | |||
1143 | case VT_RESIZEX: | ||
1144 | { | ||
1145 | struct vt_consize __user *vtconsize = up; | ||
1146 | ushort ll,cc,vlin,clin,vcol,ccol; | ||
1147 | if (!perm) | ||
1148 | goto eperm; | ||
1149 | if (!access_ok(VERIFY_READ, vtconsize, | ||
1150 | sizeof(struct vt_consize))) { | ||
1151 | ret = -EFAULT; | ||
1152 | break; | ||
1153 | } | ||
1154 | /* FIXME: Should check the copies properly */ | ||
1155 | __get_user(ll, &vtconsize->v_rows); | ||
1156 | __get_user(cc, &vtconsize->v_cols); | ||
1157 | __get_user(vlin, &vtconsize->v_vlin); | ||
1158 | __get_user(clin, &vtconsize->v_clin); | ||
1159 | __get_user(vcol, &vtconsize->v_vcol); | ||
1160 | __get_user(ccol, &vtconsize->v_ccol); | ||
1161 | vlin = vlin ? vlin : vc->vc_scan_lines; | ||
1162 | if (clin) { | ||
1163 | if (ll) { | ||
1164 | if (ll != vlin/clin) { | ||
1165 | /* Parameters don't add up */ | ||
1166 | ret = -EINVAL; | ||
1167 | break; | ||
1168 | } | ||
1169 | } else | ||
1170 | ll = vlin/clin; | ||
1171 | } | ||
1172 | if (vcol && ccol) { | ||
1173 | if (cc) { | ||
1174 | if (cc != vcol/ccol) { | ||
1175 | ret = -EINVAL; | ||
1176 | break; | ||
1177 | } | ||
1178 | } else | ||
1179 | cc = vcol/ccol; | ||
1180 | } | ||
1181 | |||
1182 | if (clin > 32) { | ||
1183 | ret = -EINVAL; | ||
1184 | break; | ||
1185 | } | ||
1186 | |||
1187 | for (i = 0; i < MAX_NR_CONSOLES; i++) { | ||
1188 | if (!vc_cons[i].d) | ||
1189 | continue; | ||
1190 | acquire_console_sem(); | ||
1191 | if (vlin) | ||
1192 | vc_cons[i].d->vc_scan_lines = vlin; | ||
1193 | if (clin) | ||
1194 | vc_cons[i].d->vc_font.height = clin; | ||
1195 | vc_cons[i].d->vc_resize_user = 1; | ||
1196 | vc_resize(vc_cons[i].d, cc, ll); | ||
1197 | release_console_sem(); | ||
1198 | } | ||
1199 | break; | ||
1200 | } | ||
1201 | |||
1202 | case PIO_FONT: { | ||
1203 | if (!perm) | ||
1204 | goto eperm; | ||
1205 | op.op = KD_FONT_OP_SET; | ||
1206 | op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */ | ||
1207 | op.width = 8; | ||
1208 | op.height = 0; | ||
1209 | op.charcount = 256; | ||
1210 | op.data = up; | ||
1211 | ret = con_font_op(vc_cons[fg_console].d, &op); | ||
1212 | break; | ||
1213 | } | ||
1214 | |||
1215 | case GIO_FONT: { | ||
1216 | op.op = KD_FONT_OP_GET; | ||
1217 | op.flags = KD_FONT_FLAG_OLD; | ||
1218 | op.width = 8; | ||
1219 | op.height = 32; | ||
1220 | op.charcount = 256; | ||
1221 | op.data = up; | ||
1222 | ret = con_font_op(vc_cons[fg_console].d, &op); | ||
1223 | break; | ||
1224 | } | ||
1225 | |||
1226 | case PIO_CMAP: | ||
1227 | if (!perm) | ||
1228 | ret = -EPERM; | ||
1229 | else | ||
1230 | ret = con_set_cmap(up); | ||
1231 | break; | ||
1232 | |||
1233 | case GIO_CMAP: | ||
1234 | ret = con_get_cmap(up); | ||
1235 | break; | ||
1236 | |||
1237 | case PIO_FONTX: | ||
1238 | case GIO_FONTX: | ||
1239 | ret = do_fontx_ioctl(cmd, up, perm, &op); | ||
1240 | break; | ||
1241 | |||
1242 | case PIO_FONTRESET: | ||
1243 | { | ||
1244 | if (!perm) | ||
1245 | goto eperm; | ||
1246 | |||
1247 | #ifdef BROKEN_GRAPHICS_PROGRAMS | ||
1248 | /* With BROKEN_GRAPHICS_PROGRAMS defined, the default | ||
1249 | font is not saved. */ | ||
1250 | ret = -ENOSYS; | ||
1251 | break; | ||
1252 | #else | ||
1253 | { | ||
1254 | op.op = KD_FONT_OP_SET_DEFAULT; | ||
1255 | op.data = NULL; | ||
1256 | ret = con_font_op(vc_cons[fg_console].d, &op); | ||
1257 | if (ret) | ||
1258 | break; | ||
1259 | con_set_default_unimap(vc_cons[fg_console].d); | ||
1260 | break; | ||
1261 | } | ||
1262 | #endif | ||
1263 | } | ||
1264 | |||
1265 | case KDFONTOP: { | ||
1266 | if (copy_from_user(&op, up, sizeof(op))) { | ||
1267 | ret = -EFAULT; | ||
1268 | break; | ||
1269 | } | ||
1270 | if (!perm && op.op != KD_FONT_OP_GET) | ||
1271 | goto eperm; | ||
1272 | ret = con_font_op(vc, &op); | ||
1273 | if (ret) | ||
1274 | break; | ||
1275 | if (copy_to_user(up, &op, sizeof(op))) | ||
1276 | ret = -EFAULT; | ||
1277 | break; | ||
1278 | } | ||
1279 | |||
1280 | case PIO_SCRNMAP: | ||
1281 | if (!perm) | ||
1282 | ret = -EPERM; | ||
1283 | else | ||
1284 | ret = con_set_trans_old(up); | ||
1285 | break; | ||
1286 | |||
1287 | case GIO_SCRNMAP: | ||
1288 | ret = con_get_trans_old(up); | ||
1289 | break; | ||
1290 | |||
1291 | case PIO_UNISCRNMAP: | ||
1292 | if (!perm) | ||
1293 | ret = -EPERM; | ||
1294 | else | ||
1295 | ret = con_set_trans_new(up); | ||
1296 | break; | ||
1297 | |||
1298 | case GIO_UNISCRNMAP: | ||
1299 | ret = con_get_trans_new(up); | ||
1300 | break; | ||
1301 | |||
1302 | case PIO_UNIMAPCLR: | ||
1303 | { struct unimapinit ui; | ||
1304 | if (!perm) | ||
1305 | goto eperm; | ||
1306 | ret = copy_from_user(&ui, up, sizeof(struct unimapinit)); | ||
1307 | if (ret) | ||
1308 | ret = -EFAULT; | ||
1309 | else | ||
1310 | con_clear_unimap(vc, &ui); | ||
1311 | break; | ||
1312 | } | ||
1313 | |||
1314 | case PIO_UNIMAP: | ||
1315 | case GIO_UNIMAP: | ||
1316 | ret = do_unimap_ioctl(cmd, up, perm, vc); | ||
1317 | break; | ||
1318 | |||
1319 | case VT_LOCKSWITCH: | ||
1320 | if (!capable(CAP_SYS_TTY_CONFIG)) | ||
1321 | goto eperm; | ||
1322 | vt_dont_switch = 1; | ||
1323 | break; | ||
1324 | case VT_UNLOCKSWITCH: | ||
1325 | if (!capable(CAP_SYS_TTY_CONFIG)) | ||
1326 | goto eperm; | ||
1327 | vt_dont_switch = 0; | ||
1328 | break; | ||
1329 | case VT_GETHIFONTMASK: | ||
1330 | ret = put_user(vc->vc_hi_font_mask, | ||
1331 | (unsigned short __user *)arg); | ||
1332 | break; | ||
1333 | case VT_WAITEVENT: | ||
1334 | ret = vt_event_wait_ioctl((struct vt_event __user *)arg); | ||
1335 | break; | ||
1336 | default: | ||
1337 | ret = -ENOIOCTLCMD; | ||
1338 | } | ||
1339 | out: | ||
1340 | tty_unlock(); | ||
1341 | return ret; | ||
1342 | eperm: | ||
1343 | ret = -EPERM; | ||
1344 | goto out; | ||
1345 | } | ||
1346 | |||
1347 | void reset_vc(struct vc_data *vc) | ||
1348 | { | ||
1349 | vc->vc_mode = KD_TEXT; | ||
1350 | kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; | ||
1351 | vc->vt_mode.mode = VT_AUTO; | ||
1352 | vc->vt_mode.waitv = 0; | ||
1353 | vc->vt_mode.relsig = 0; | ||
1354 | vc->vt_mode.acqsig = 0; | ||
1355 | vc->vt_mode.frsig = 0; | ||
1356 | put_pid(vc->vt_pid); | ||
1357 | vc->vt_pid = NULL; | ||
1358 | vc->vt_newvt = -1; | ||
1359 | if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */ | ||
1360 | reset_palette(vc); | ||
1361 | } | ||
1362 | |||
1363 | void vc_SAK(struct work_struct *work) | ||
1364 | { | ||
1365 | struct vc *vc_con = | ||
1366 | container_of(work, struct vc, SAK_work); | ||
1367 | struct vc_data *vc; | ||
1368 | struct tty_struct *tty; | ||
1369 | |||
1370 | acquire_console_sem(); | ||
1371 | vc = vc_con->d; | ||
1372 | if (vc) { | ||
1373 | tty = vc->port.tty; | ||
1374 | /* | ||
1375 | * SAK should also work in all raw modes and reset | ||
1376 | * them properly. | ||
1377 | */ | ||
1378 | if (tty) | ||
1379 | __do_SAK(tty); | ||
1380 | reset_vc(vc); | ||
1381 | } | ||
1382 | release_console_sem(); | ||
1383 | } | ||
1384 | |||
1385 | #ifdef CONFIG_COMPAT | ||
1386 | |||
1387 | struct compat_consolefontdesc { | ||
1388 | unsigned short charcount; /* characters in font (256 or 512) */ | ||
1389 | unsigned short charheight; /* scan lines per character (1-32) */ | ||
1390 | compat_caddr_t chardata; /* font data in expanded form */ | ||
1391 | }; | ||
1392 | |||
1393 | static inline int | ||
1394 | compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd, | ||
1395 | int perm, struct console_font_op *op) | ||
1396 | { | ||
1397 | struct compat_consolefontdesc cfdarg; | ||
1398 | int i; | ||
1399 | |||
1400 | if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc))) | ||
1401 | return -EFAULT; | ||
1402 | |||
1403 | switch (cmd) { | ||
1404 | case PIO_FONTX: | ||
1405 | if (!perm) | ||
1406 | return -EPERM; | ||
1407 | op->op = KD_FONT_OP_SET; | ||
1408 | op->flags = KD_FONT_FLAG_OLD; | ||
1409 | op->width = 8; | ||
1410 | op->height = cfdarg.charheight; | ||
1411 | op->charcount = cfdarg.charcount; | ||
1412 | op->data = compat_ptr(cfdarg.chardata); | ||
1413 | return con_font_op(vc_cons[fg_console].d, op); | ||
1414 | case GIO_FONTX: | ||
1415 | op->op = KD_FONT_OP_GET; | ||
1416 | op->flags = KD_FONT_FLAG_OLD; | ||
1417 | op->width = 8; | ||
1418 | op->height = cfdarg.charheight; | ||
1419 | op->charcount = cfdarg.charcount; | ||
1420 | op->data = compat_ptr(cfdarg.chardata); | ||
1421 | i = con_font_op(vc_cons[fg_console].d, op); | ||
1422 | if (i) | ||
1423 | return i; | ||
1424 | cfdarg.charheight = op->height; | ||
1425 | cfdarg.charcount = op->charcount; | ||
1426 | if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc))) | ||
1427 | return -EFAULT; | ||
1428 | return 0; | ||
1429 | } | ||
1430 | return -EINVAL; | ||
1431 | } | ||
1432 | |||
1433 | struct compat_console_font_op { | ||
1434 | compat_uint_t op; /* operation code KD_FONT_OP_* */ | ||
1435 | compat_uint_t flags; /* KD_FONT_FLAG_* */ | ||
1436 | compat_uint_t width, height; /* font size */ | ||
1437 | compat_uint_t charcount; | ||
1438 | compat_caddr_t data; /* font data with height fixed to 32 */ | ||
1439 | }; | ||
1440 | |||
1441 | static inline int | ||
1442 | compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop, | ||
1443 | int perm, struct console_font_op *op, struct vc_data *vc) | ||
1444 | { | ||
1445 | int i; | ||
1446 | |||
1447 | if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op))) | ||
1448 | return -EFAULT; | ||
1449 | if (!perm && op->op != KD_FONT_OP_GET) | ||
1450 | return -EPERM; | ||
1451 | op->data = compat_ptr(((struct compat_console_font_op *)op)->data); | ||
1452 | op->flags |= KD_FONT_FLAG_OLD; | ||
1453 | i = con_font_op(vc, op); | ||
1454 | if (i) | ||
1455 | return i; | ||
1456 | ((struct compat_console_font_op *)op)->data = (unsigned long)op->data; | ||
1457 | if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op))) | ||
1458 | return -EFAULT; | ||
1459 | return 0; | ||
1460 | } | ||
1461 | |||
1462 | struct compat_unimapdesc { | ||
1463 | unsigned short entry_ct; | ||
1464 | compat_caddr_t entries; | ||
1465 | }; | ||
1466 | |||
1467 | static inline int | ||
1468 | compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud, | ||
1469 | int perm, struct vc_data *vc) | ||
1470 | { | ||
1471 | struct compat_unimapdesc tmp; | ||
1472 | struct unipair __user *tmp_entries; | ||
1473 | |||
1474 | if (copy_from_user(&tmp, user_ud, sizeof tmp)) | ||
1475 | return -EFAULT; | ||
1476 | tmp_entries = compat_ptr(tmp.entries); | ||
1477 | if (tmp_entries) | ||
1478 | if (!access_ok(VERIFY_WRITE, tmp_entries, | ||
1479 | tmp.entry_ct*sizeof(struct unipair))) | ||
1480 | return -EFAULT; | ||
1481 | switch (cmd) { | ||
1482 | case PIO_UNIMAP: | ||
1483 | if (!perm) | ||
1484 | return -EPERM; | ||
1485 | return con_set_unimap(vc, tmp.entry_ct, tmp_entries); | ||
1486 | case GIO_UNIMAP: | ||
1487 | if (!perm && fg_console != vc->vc_num) | ||
1488 | return -EPERM; | ||
1489 | return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries); | ||
1490 | } | ||
1491 | return 0; | ||
1492 | } | ||
1493 | |||
1494 | long vt_compat_ioctl(struct tty_struct *tty, struct file * file, | ||
1495 | unsigned int cmd, unsigned long arg) | ||
1496 | { | ||
1497 | struct vc_data *vc = tty->driver_data; | ||
1498 | struct console_font_op op; /* used in multiple places here */ | ||
1499 | struct kbd_struct *kbd; | ||
1500 | unsigned int console; | ||
1501 | void __user *up = (void __user *)arg; | ||
1502 | int perm; | ||
1503 | int ret = 0; | ||
1504 | |||
1505 | console = vc->vc_num; | ||
1506 | |||
1507 | tty_lock(); | ||
1508 | |||
1509 | if (!vc_cons_allocated(console)) { /* impossible? */ | ||
1510 | ret = -ENOIOCTLCMD; | ||
1511 | goto out; | ||
1512 | } | ||
1513 | |||
1514 | /* | ||
1515 | * To have permissions to do most of the vt ioctls, we either have | ||
1516 | * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG. | ||
1517 | */ | ||
1518 | perm = 0; | ||
1519 | if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG)) | ||
1520 | perm = 1; | ||
1521 | |||
1522 | kbd = kbd_table + console; | ||
1523 | switch (cmd) { | ||
1524 | /* | ||
1525 | * these need special handlers for incompatible data structures | ||
1526 | */ | ||
1527 | case PIO_FONTX: | ||
1528 | case GIO_FONTX: | ||
1529 | ret = compat_fontx_ioctl(cmd, up, perm, &op); | ||
1530 | break; | ||
1531 | |||
1532 | case KDFONTOP: | ||
1533 | ret = compat_kdfontop_ioctl(up, perm, &op, vc); | ||
1534 | break; | ||
1535 | |||
1536 | case PIO_UNIMAP: | ||
1537 | case GIO_UNIMAP: | ||
1538 | ret = compat_unimap_ioctl(cmd, up, perm, vc); | ||
1539 | break; | ||
1540 | |||
1541 | /* | ||
1542 | * all these treat 'arg' as an integer | ||
1543 | */ | ||
1544 | case KIOCSOUND: | ||
1545 | case KDMKTONE: | ||
1546 | #ifdef CONFIG_X86 | ||
1547 | case KDADDIO: | ||
1548 | case KDDELIO: | ||
1549 | #endif | ||
1550 | case KDSETMODE: | ||
1551 | case KDMAPDISP: | ||
1552 | case KDUNMAPDISP: | ||
1553 | case KDSKBMODE: | ||
1554 | case KDSKBMETA: | ||
1555 | case KDSKBLED: | ||
1556 | case KDSETLED: | ||
1557 | case KDSIGACCEPT: | ||
1558 | case VT_ACTIVATE: | ||
1559 | case VT_WAITACTIVE: | ||
1560 | case VT_RELDISP: | ||
1561 | case VT_DISALLOCATE: | ||
1562 | case VT_RESIZE: | ||
1563 | case VT_RESIZEX: | ||
1564 | goto fallback; | ||
1565 | |||
1566 | /* | ||
1567 | * the rest has a compatible data structure behind arg, | ||
1568 | * but we have to convert it to a proper 64 bit pointer. | ||
1569 | */ | ||
1570 | default: | ||
1571 | arg = (unsigned long)compat_ptr(arg); | ||
1572 | goto fallback; | ||
1573 | } | ||
1574 | out: | ||
1575 | tty_unlock(); | ||
1576 | return ret; | ||
1577 | |||
1578 | fallback: | ||
1579 | tty_unlock(); | ||
1580 | return vt_ioctl(tty, file, cmd, arg); | ||
1581 | } | ||
1582 | |||
1583 | |||
1584 | #endif /* CONFIG_COMPAT */ | ||
1585 | |||
1586 | |||
1587 | /* | ||
1588 | * Performs the back end of a vt switch. Called under the console | ||
1589 | * semaphore. | ||
1590 | */ | ||
1591 | static void complete_change_console(struct vc_data *vc) | ||
1592 | { | ||
1593 | unsigned char old_vc_mode; | ||
1594 | int old = fg_console; | ||
1595 | |||
1596 | last_console = fg_console; | ||
1597 | |||
1598 | /* | ||
1599 | * If we're switching, we could be going from KD_GRAPHICS to | ||
1600 | * KD_TEXT mode or vice versa, which means we need to blank or | ||
1601 | * unblank the screen later. | ||
1602 | */ | ||
1603 | old_vc_mode = vc_cons[fg_console].d->vc_mode; | ||
1604 | switch_screen(vc); | ||
1605 | |||
1606 | /* | ||
1607 | * This can't appear below a successful kill_pid(). If it did, | ||
1608 | * then the *blank_screen operation could occur while X, having | ||
1609 | * received acqsig, is waking up on another processor. This | ||
1610 | * condition can lead to overlapping accesses to the VGA range | ||
1611 | * and the framebuffer (causing system lockups). | ||
1612 | * | ||
1613 | * To account for this we duplicate this code below only if the | ||
1614 | * controlling process is gone and we've called reset_vc. | ||
1615 | */ | ||
1616 | if (old_vc_mode != vc->vc_mode) { | ||
1617 | if (vc->vc_mode == KD_TEXT) | ||
1618 | do_unblank_screen(1); | ||
1619 | else | ||
1620 | do_blank_screen(1); | ||
1621 | } | ||
1622 | |||
1623 | /* | ||
1624 | * If this new console is under process control, send it a signal | ||
1625 | * telling it that it has acquired. Also check if it has died and | ||
1626 | * clean up (similar to logic employed in change_console()) | ||
1627 | */ | ||
1628 | if (vc->vt_mode.mode == VT_PROCESS) { | ||
1629 | /* | ||
1630 | * Send the signal as privileged - kill_pid() will | ||
1631 | * tell us if the process has gone or something else | ||
1632 | * is awry | ||
1633 | */ | ||
1634 | if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) { | ||
1635 | /* | ||
1636 | * The controlling process has died, so we revert back to | ||
1637 | * normal operation. In this case, we'll also change back | ||
1638 | * to KD_TEXT mode. I'm not sure if this is strictly correct | ||
1639 | * but it saves the agony when the X server dies and the screen | ||
1640 | * remains blanked due to KD_GRAPHICS! It would be nice to do | ||
1641 | * this outside of VT_PROCESS but there is no single process | ||
1642 | * to account for and tracking tty count may be undesirable. | ||
1643 | */ | ||
1644 | reset_vc(vc); | ||
1645 | |||
1646 | if (old_vc_mode != vc->vc_mode) { | ||
1647 | if (vc->vc_mode == KD_TEXT) | ||
1648 | do_unblank_screen(1); | ||
1649 | else | ||
1650 | do_blank_screen(1); | ||
1651 | } | ||
1652 | } | ||
1653 | } | ||
1654 | |||
1655 | /* | ||
1656 | * Wake anyone waiting for their VT to activate | ||
1657 | */ | ||
1658 | vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num); | ||
1659 | return; | ||
1660 | } | ||
1661 | |||
1662 | /* | ||
1663 | * Performs the front-end of a vt switch | ||
1664 | */ | ||
1665 | void change_console(struct vc_data *new_vc) | ||
1666 | { | ||
1667 | struct vc_data *vc; | ||
1668 | |||
1669 | if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch) | ||
1670 | return; | ||
1671 | |||
1672 | /* | ||
1673 | * If this vt is in process mode, then we need to handshake with | ||
1674 | * that process before switching. Essentially, we store where that | ||
1675 | * vt wants to switch to and wait for it to tell us when it's done | ||
1676 | * (via VT_RELDISP ioctl). | ||
1677 | * | ||
1678 | * We also check to see if the controlling process still exists. | ||
1679 | * If it doesn't, we reset this vt to auto mode and continue. | ||
1680 | * This is a cheap way to track process control. The worst thing | ||
1681 | * that can happen is: we send a signal to a process, it dies, and | ||
1682 | * the switch gets "lost" waiting for a response; hopefully, the | ||
1683 | * user will try again, we'll detect the process is gone (unless | ||
1684 | * the user waits just the right amount of time :-) and revert the | ||
1685 | * vt to auto control. | ||
1686 | */ | ||
1687 | vc = vc_cons[fg_console].d; | ||
1688 | if (vc->vt_mode.mode == VT_PROCESS) { | ||
1689 | /* | ||
1690 | * Send the signal as privileged - kill_pid() will | ||
1691 | * tell us if the process has gone or something else | ||
1692 | * is awry. | ||
1693 | * | ||
1694 | * We need to set vt_newvt *before* sending the signal or we | ||
1695 | * have a race. | ||
1696 | */ | ||
1697 | vc->vt_newvt = new_vc->vc_num; | ||
1698 | if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) { | ||
1699 | /* | ||
1700 | * It worked. Mark the vt to switch to and | ||
1701 | * return. The process needs to send us a | ||
1702 | * VT_RELDISP ioctl to complete the switch. | ||
1703 | */ | ||
1704 | return; | ||
1705 | } | ||
1706 | |||
1707 | /* | ||
1708 | * The controlling process has died, so we revert back to | ||
1709 | * normal operation. In this case, we'll also change back | ||
1710 | * to KD_TEXT mode. I'm not sure if this is strictly correct | ||
1711 | * but it saves the agony when the X server dies and the screen | ||
1712 | * remains blanked due to KD_GRAPHICS! It would be nice to do | ||
1713 | * this outside of VT_PROCESS but there is no single process | ||
1714 | * to account for and tracking tty count may be undesirable. | ||
1715 | */ | ||
1716 | reset_vc(vc); | ||
1717 | |||
1718 | /* | ||
1719 | * Fall through to normal (VT_AUTO) handling of the switch... | ||
1720 | */ | ||
1721 | } | ||
1722 | |||
1723 | /* | ||
1724 | * Ignore all switches in KD_GRAPHICS+VT_AUTO mode | ||
1725 | */ | ||
1726 | if (vc->vc_mode == KD_GRAPHICS) | ||
1727 | return; | ||
1728 | |||
1729 | complete_change_console(new_vc); | ||
1730 | } | ||
1731 | |||
1732 | /* Perform a kernel triggered VT switch for suspend/resume */ | ||
1733 | |||
1734 | static int disable_vt_switch; | ||
1735 | |||
1736 | int vt_move_to_console(unsigned int vt, int alloc) | ||
1737 | { | ||
1738 | int prev; | ||
1739 | |||
1740 | acquire_console_sem(); | ||
1741 | /* Graphics mode - up to X */ | ||
1742 | if (disable_vt_switch) { | ||
1743 | release_console_sem(); | ||
1744 | return 0; | ||
1745 | } | ||
1746 | prev = fg_console; | ||
1747 | |||
1748 | if (alloc && vc_allocate(vt)) { | ||
1749 | /* we can't have a free VC for now. Too bad, | ||
1750 | * we don't want to mess the screen for now. */ | ||
1751 | release_console_sem(); | ||
1752 | return -ENOSPC; | ||
1753 | } | ||
1754 | |||
1755 | if (set_console(vt)) { | ||
1756 | /* | ||
1757 | * We're unable to switch to the SUSPEND_CONSOLE. | ||
1758 | * Let the calling function know so it can decide | ||
1759 | * what to do. | ||
1760 | */ | ||
1761 | release_console_sem(); | ||
1762 | return -EIO; | ||
1763 | } | ||
1764 | release_console_sem(); | ||
1765 | tty_lock(); | ||
1766 | if (vt_waitactive(vt + 1)) { | ||
1767 | pr_debug("Suspend: Can't switch VCs."); | ||
1768 | tty_unlock(); | ||
1769 | return -EINTR; | ||
1770 | } | ||
1771 | tty_unlock(); | ||
1772 | return prev; | ||
1773 | } | ||
1774 | |||
1775 | /* | ||
1776 | * Normally during a suspend, we allocate a new console and switch to it. | ||
1777 | * When we resume, we switch back to the original console. This switch | ||
1778 | * can be slow, so on systems where the framebuffer can handle restoration | ||
1779 | * of video registers anyways, there's little point in doing the console | ||
1780 | * switch. This function allows you to disable it by passing it '0'. | ||
1781 | */ | ||
1782 | void pm_set_vt_switch(int do_switch) | ||
1783 | { | ||
1784 | acquire_console_sem(); | ||
1785 | disable_vt_switch = !do_switch; | ||
1786 | release_console_sem(); | ||
1787 | } | ||
1788 | EXPORT_SYMBOL(pm_set_vt_switch); | ||