blob: 025d3be8aca4ae522e40b6aaa2a8623834805243 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include "linux/sched.h"
7#include "linux/slab.h"
8#include "linux/list.h"
9#include "linux/kd.h"
10#include "linux/interrupt.h"
11#include "linux/devfs_fs_kernel.h"
12#include "asm/uaccess.h"
13#include "chan_kern.h"
14#include "irq_user.h"
15#include "line.h"
16#include "kern.h"
17#include "user_util.h"
18#include "kern_util.h"
19#include "os.h"
20#include "irq_kern.h"
21
22#define LINE_BUFSIZE 4096
23
24static irqreturn_t line_interrupt(int irq, void *data, struct pt_regs *unused)
25{
26 struct tty_struct *tty = data;
27 struct line *line = tty->driver_data;
28
29 if (line)
30 chan_interrupt(&line->chan_list, &line->task, tty, irq);
31 return IRQ_HANDLED;
32}
33
34static void line_timer_cb(void *arg)
35{
36 struct tty_struct *tty = arg;
37 struct line *line = tty->driver_data;
38
39 line_interrupt(line->driver->read_irq, arg, NULL);
40}
41
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070042/* Returns the free space inside the ring buffer of this line.
43 *
44 * Should be called while holding line->lock (this does not modify datas).
45 */
46static int write_room(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 int n;
49
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070050 if (line->buffer == NULL)
51 return LINE_BUFSIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070053 /* This is for the case where the buffer is wrapped! */
54 n = line->head - line->tail;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (n <= 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070057 n = LINE_BUFSIZE + n; /* The other case */
58 return n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070061int line_write_room(struct tty_struct *tty)
62{
63 struct line *line = tty->driver_data;
64 unsigned long flags;
65 int room;
66
67 if (tty->stopped)
68 return 0;
69
70 spin_lock_irqsave(&line->lock, flags);
71 room = write_room(line);
72 spin_unlock_irqrestore(&line->lock, flags);
73
74 /*XXX: Warning to remove */
75 if (0 == room)
76 printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
77 __FUNCTION__,tty->name);
78 return room;
79}
80
81int line_chars_in_buffer(struct tty_struct *tty)
82{
83 struct line *line = tty->driver_data;
84 unsigned long flags;
85 int ret;
86
87 spin_lock_irqsave(&line->lock, flags);
88
89 /*write_room subtracts 1 for the needed NULL, so we readd it.*/
90 ret = LINE_BUFSIZE - (write_room(line) + 1);
91 spin_unlock_irqrestore(&line->lock, flags);
92
93 return ret;
94}
95
96/*
97 * This copies the content of buf into the circular buffer associated with
98 * this line.
99 * The return value is the number of characters actually copied, i.e. the ones
100 * for which there was space: this function is not supposed to ever flush out
101 * the circular buffer.
102 *
103 * Must be called while holding line->lock!
104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int buffer_data(struct line *line, const char *buf, int len)
106{
107 int end, room;
108
109 if(line->buffer == NULL){
110 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
111 if (line->buffer == NULL) {
112 printk("buffer_data - atomic allocation failed\n");
113 return(0);
114 }
115 line->head = line->buffer;
116 line->tail = line->buffer;
117 }
118
119 room = write_room(line);
120 len = (len > room) ? room : len;
121
122 end = line->buffer + LINE_BUFSIZE - line->tail;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700123
124 if (len < end){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 memcpy(line->tail, buf, len);
126 line->tail += len;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700127 } else {
128 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 memcpy(line->tail, buf, end);
130 buf += end;
131 memcpy(line->buffer, buf, len - end);
132 line->tail = line->buffer + len - end;
133 }
134
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700135 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700138/*
139 * Flushes the ring buffer to the output channels. That is, write_chan is
140 * called, passing it line->head as buffer, and an appropriate count.
141 *
142 * On exit, returns 1 when the buffer is empty,
143 * 0 when the buffer is not empty on exit,
144 * and -errno when an error occurred.
145 *
146 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int flush_buffer(struct line *line)
148{
149 int n, count;
150
151 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700152 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700155 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 n = write_chan(&line->chan_list, line->head, count,
159 line->driver->write_irq);
160 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700161 return n;
162 if (n == count) {
163 /* We have flushed from ->head to buffer end, now we
164 * must flush only from the beginning to ->tail.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700166 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 }
171
172 count = line->tail - line->head;
173 n = write_chan(&line->chan_list, line->head, count,
174 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700175
176 if(n < 0)
177 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700180 return line->head == line->tail;
181}
182
183void line_flush_buffer(struct tty_struct *tty)
184{
185 struct line *line = tty->driver_data;
186 unsigned long flags;
187 int err;
188
189 /*XXX: copied from line_write, verify if it is correct!*/
190 if(tty->stopped)
191 return;
192 //return 0;
193
194 spin_lock_irqsave(&line->lock, flags);
195 err = flush_buffer(line);
196 /*if (err == 1)
197 err = 0;*/
198 spin_unlock_irqrestore(&line->lock, flags);
199 //return err;
200}
201
202/* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
203 * and ->write. Hope it's not that bad.*/
204void line_flush_chars(struct tty_struct *tty)
205{
206 line_flush_buffer(tty);
207}
208
209void line_put_char(struct tty_struct *tty, unsigned char ch)
210{
211 line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
215{
216 struct line *line = tty->driver_data;
217 unsigned long flags;
218 int n, err, ret = 0;
219
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700220 if(tty->stopped)
221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700223 spin_lock_irqsave(&line->lock, flags);
224 if (line->head != line->tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 ret = buffer_data(line, buf, len);
226 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700227 if (err <= 0 && (err != -EAGAIN || !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 ret = err;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700229 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 n = write_chan(&line->chan_list, buf, len,
231 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700232 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ret = n;
234 goto out_up;
235 }
236
237 len -= n;
238 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700239 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ret += buffer_data(line, buf + n, len);
241 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700242out_up:
243 spin_unlock_irqrestore(&line->lock, flags);
244 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247void line_set_termios(struct tty_struct *tty, struct termios * old)
248{
249 /* nothing */
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static struct {
253 int cmd;
254 char *level;
255 char *name;
256} tty_ioctls[] = {
257 /* don't print these, they flood the log ... */
258 { TCGETS, NULL, "TCGETS" },
259 { TCSETS, NULL, "TCSETS" },
260 { TCSETSW, NULL, "TCSETSW" },
261 { TCFLSH, NULL, "TCFLSH" },
262 { TCSBRK, NULL, "TCSBRK" },
263
264 /* general tty stuff */
265 { TCSETSF, KERN_DEBUG, "TCSETSF" },
266 { TCGETA, KERN_DEBUG, "TCGETA" },
267 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
268 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
269 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
270
271 /* linux-specific ones */
272 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
273 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
274 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
275 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
276};
277
278int line_ioctl(struct tty_struct *tty, struct file * file,
279 unsigned int cmd, unsigned long arg)
280{
281 int ret;
282 int i;
283
284 ret = 0;
285 switch(cmd) {
286#ifdef TIOCGETP
287 case TIOCGETP:
288 case TIOCSETP:
289 case TIOCSETN:
290#endif
291#ifdef TIOCGETC
292 case TIOCGETC:
293 case TIOCSETC:
294#endif
295#ifdef TIOCGLTC
296 case TIOCGLTC:
297 case TIOCSLTC:
298#endif
299 case TCGETS:
300 case TCSETSF:
301 case TCSETSW:
302 case TCSETS:
303 case TCGETA:
304 case TCSETAF:
305 case TCSETAW:
306 case TCSETA:
307 case TCXONC:
308 case TCFLSH:
309 case TIOCOUTQ:
310 case TIOCINQ:
311 case TIOCGLCKTRMIOS:
312 case TIOCSLCKTRMIOS:
313 case TIOCPKT:
314 case TIOCGSOFTCAR:
315 case TIOCSSOFTCAR:
316 return -ENOIOCTLCMD;
317#if 0
318 case TCwhatever:
319 /* do something */
320 break;
321#endif
322 default:
323 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
324 if (cmd == tty_ioctls[i].cmd)
325 break;
326 if (i < ARRAY_SIZE(tty_ioctls)) {
327 if (NULL != tty_ioctls[i].level)
328 printk("%s%s: %s: ioctl %s called\n",
329 tty_ioctls[i].level, __FUNCTION__,
330 tty->name, tty_ioctls[i].name);
331 } else {
332 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
333 __FUNCTION__, tty->name, cmd);
334 }
335 ret = -ENOIOCTLCMD;
336 break;
337 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341static irqreturn_t line_write_interrupt(int irq, void *data,
342 struct pt_regs *unused)
343{
344 struct tty_struct *tty = data;
345 struct line *line = tty->driver_data;
346 int err;
347
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700348 /* Interrupts are enabled here because we registered the interrupt with
349 * SA_INTERRUPT (see line_setup_irq).*/
350
351 spin_lock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700353 if (err == 0) {
354 return IRQ_NONE;
355 } else if(err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 line->head = line->buffer;
357 line->tail = line->buffer;
358 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700359 spin_unlock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if(tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700362 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700364 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 (tty->ldisc.write_wakeup != NULL))
366 (tty->ldisc.write_wakeup)(tty);
367
368 /* BLOCKING mode
369 * In blocking mode, everything sleeps on tty->write_wait.
370 * Sleeping in the console driver would break non-blocking
371 * writes.
372 */
373
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700374 if (waitqueue_active(&tty->write_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 wake_up_interruptible(&tty->write_wait);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700376 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379int line_setup_irq(int fd, int input, int output, struct tty_struct *tty)
380{
381 struct line *line = tty->driver_data;
382 struct line_driver *driver = line->driver;
383 int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
384
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700385 if (input)
386 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 line_interrupt, flags,
388 driver->read_irq_name, tty);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700389 if (err)
390 return err;
391 if (output)
392 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 line_write_interrupt, flags,
394 driver->write_irq_name, tty);
395 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399void line_disable(struct tty_struct *tty, int current_irq)
400{
401 struct line *line = tty->driver_data;
402
403 if(!line->have_irq)
404 return;
405
406 if(line->driver->read_irq == current_irq)
407 free_irq_later(line->driver->read_irq, tty);
408 else {
409 free_irq_by_irq_and_dev(line->driver->read_irq, tty);
410 free_irq(line->driver->read_irq, tty);
411 }
412
413 if(line->driver->write_irq == current_irq)
414 free_irq_later(line->driver->write_irq, tty);
415 else {
416 free_irq_by_irq_and_dev(line->driver->write_irq, tty);
417 free_irq(line->driver->write_irq, tty);
418 }
419
420 line->have_irq = 0;
421}
422
423int line_open(struct line *lines, struct tty_struct *tty,
424 struct chan_opts *opts)
425{
426 struct line *line;
427 int err = 0;
428
429 line = &lines[tty->index];
430 tty->driver_data = line;
431
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700432 /* The IRQ which takes this lock is not yet enabled and won't be run
433 * before the end, so we don't need to use spin_lock_irq.*/
434 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (tty->count == 1) {
436 if (!line->valid) {
437 err = -ENODEV;
438 goto out;
439 }
440 if (list_empty(&line->chan_list)) {
441 err = parse_chan_pair(line->init_str, &line->chan_list,
442 line->init_pri, tty->index, opts);
443 if(err) goto out;
444 err = open_chan(&line->chan_list);
445 if(err) goto out;
446 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700447 /* Here the interrupt is registered.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 enable_chan(&line->chan_list, tty);
449 INIT_WORK(&line->task, line_timer_cb, tty);
450 }
451
452 if(!line->sigio){
453 chan_enable_winch(&line->chan_list, tty);
454 line->sigio = 1;
455 }
456 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
457 &tty->winsize.ws_col);
458 line->count++;
459
460out:
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700461 spin_unlock(&line->lock);
462 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700465static void unregister_winch(struct tty_struct *tty);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467void line_close(struct tty_struct *tty, struct file * filp)
468{
469 struct line *line = tty->driver_data;
470
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700471 /* XXX: I assume this should be called in process context, not with
472 * interrupts disabled!
473 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700474 spin_lock_irq(&line->lock);
475
476 /* We ignore the error anyway! */
477 flush_buffer(line);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 line->count--;
480 if (tty->count == 1) {
481 line_disable(tty, -1);
482 tty->driver_data = NULL;
483 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700484
485 if((line->count == 0) && line->sigio){
486 unregister_winch(tty);
487 line->sigio = 0;
488 }
489
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700490 spin_unlock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493void close_lines(struct line *lines, int nlines)
494{
495 int i;
496
497 for(i = 0; i < nlines; i++)
498 close_chan(&lines[i].chan_list);
499}
500
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700501/* Common setup code for both startup command line and mconsole initialization.
502 * @lines contains the the array (of size @num) to modify;
503 * @init is the setup string;
504 * @all_allowed is a boolean saying if we can setup the whole @lines
505 * at once. For instance, it will be usually true for startup init. (where we
506 * can use con=xterm) and false for mconsole.*/
507
508int line_setup(struct line *lines, unsigned int num, char *init, int all_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 int i, n;
511 char *end;
512
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700513 if(*init == '=') {
514 /* We said con=/ssl= instead of con#=, so we are configuring all
515 * consoles at once.*/
516 n = -1;
517 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 n = simple_strtoul(init, &end, 0);
519 if(*end != '='){
520 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
521 init);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524 init = end;
525 }
526 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700527
528 if (n >= (signed int) num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
530 n, num - 1);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700531 return 0;
532 } else if (n >= 0){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (lines[n].count > 0) {
534 printk("line_setup - device %d is open\n", n);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 if (lines[n].init_pri <= INIT_ONE){
538 lines[n].init_pri = INIT_ONE;
539 if (!strcmp(init, "none"))
540 lines[n].valid = 0;
541 else {
542 lines[n].init_str = init;
543 lines[n].valid = 1;
544 }
545 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700546 } else if(!all_allowed){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 printk("line_setup - can't configure all devices from "
548 "mconsole\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700549 return 0;
550 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 for(i = 0; i < num; i++){
552 if(lines[i].init_pri <= INIT_ALL){
553 lines[i].init_pri = INIT_ALL;
554 if(!strcmp(init, "none")) lines[i].valid = 0;
555 else {
556 lines[i].init_str = init;
557 lines[i].valid = 1;
558 }
559 }
560 }
561 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700562 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700565int line_config(struct line *lines, unsigned int num, char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 char *new = uml_strdup(str);
568
569 if(new == NULL){
570 printk("line_config - uml_strdup failed\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700571 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700573 return !line_setup(lines, num, new, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700576int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int size, char **error_out)
578{
579 struct line *line;
580 char *end;
581 int dev, n = 0;
582
583 dev = simple_strtoul(name, &end, 0);
584 if((*end != '\0') || (end == name)){
585 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
589 if((dev < 0) || (dev >= num)){
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700590 *error_out = "device number out of range";
591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 line = &lines[dev];
595
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700596 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if(!line->valid)
598 CONFIG_CHUNK(str, size, n, "none", 1);
599 else if(line->count == 0)
600 CONFIG_CHUNK(str, size, n, line->init_str, 1);
601 else n = chan_config_string(&line->chan_list, str, size, error_out);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700602 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700604 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700607int line_remove(struct line *lines, unsigned int num, char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 char config[sizeof("conxxxx=none\0")];
610
611 sprintf(config, "%s=none", str);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700612 return !line_setup(lines, num, config, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615struct tty_driver *line_register_devfs(struct lines *set,
616 struct line_driver *line_driver,
617 struct tty_operations *ops, struct line *lines,
618 int nlines)
619{
620 int i;
621 struct tty_driver *driver = alloc_tty_driver(nlines);
622
623 if (!driver)
624 return NULL;
625
626 driver->driver_name = line_driver->name;
627 driver->name = line_driver->device_name;
628 driver->devfs_name = line_driver->devfs_name;
629 driver->major = line_driver->major;
630 driver->minor_start = line_driver->minor_start;
631 driver->type = line_driver->type;
632 driver->subtype = line_driver->subtype;
633 driver->flags = TTY_DRIVER_REAL_RAW;
634 driver->init_termios = tty_std_termios;
635 tty_set_operations(driver, ops);
636
637 if (tty_register_driver(driver)) {
638 printk("%s: can't register %s driver\n",
639 __FUNCTION__,line_driver->name);
640 put_tty_driver(driver);
641 return NULL;
642 }
643
644 for(i = 0; i < nlines; i++){
645 if(!lines[i].valid)
646 tty_unregister_device(driver, i);
647 }
648
649 mconsole_register_dev(&line_driver->mc);
650 return driver;
651}
652
653void lines_init(struct line *lines, int nlines)
654{
655 struct line *line;
656 int i;
657
658 for(i = 0; i < nlines; i++){
659 line = &lines[i];
660 INIT_LIST_HEAD(&line->chan_list);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700661 spin_lock_init(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if(line->init_str != NULL){
663 line->init_str = uml_strdup(line->init_str);
664 if(line->init_str == NULL)
665 printk("lines_init - uml_strdup returned "
666 "NULL\n");
667 }
668 }
669}
670
671struct winch {
672 struct list_head list;
673 int fd;
674 int tty_fd;
675 int pid;
676 struct tty_struct *tty;
677};
678
679irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
680{
681 struct winch *winch = data;
682 struct tty_struct *tty;
683 struct line *line;
684 int err;
685 char c;
686
687 if(winch->fd != -1){
688 err = generic_read(winch->fd, &c, NULL);
689 if(err < 0){
690 if(err != -EAGAIN){
691 printk("winch_interrupt : read failed, "
692 "errno = %d\n", -err);
693 printk("fd %d is losing SIGWINCH support\n",
694 winch->tty_fd);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700695 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 goto out;
698 }
699 }
700 tty = winch->tty;
701 if (tty != NULL) {
702 line = tty->driver_data;
703 chan_window_size(&line->chan_list,
704 &tty->winsize.ws_row,
705 &tty->winsize.ws_col);
706 kill_pg(tty->pgrp, SIGWINCH, 1);
707 }
708 out:
709 if(winch->fd != -1)
710 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700711 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714DECLARE_MUTEX(winch_handler_sem);
715LIST_HEAD(winch_handlers);
716
717void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
718{
719 struct winch *winch;
720
721 down(&winch_handler_sem);
722 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
723 if (winch == NULL) {
724 printk("register_winch_irq - kmalloc failed\n");
725 goto out;
726 }
727 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
728 .fd = fd,
729 .tty_fd = tty_fd,
730 .pid = pid,
731 .tty = tty });
732 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700733 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
735 "winch", winch) < 0)
736 printk("register_winch_irq - failed to register IRQ\n");
737 out:
738 up(&winch_handler_sem);
739}
740
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700741static void unregister_winch(struct tty_struct *tty)
742{
743 struct list_head *ele;
744 struct winch *winch, *found = NULL;
745
746 down(&winch_handler_sem);
747 list_for_each(ele, &winch_handlers){
748 winch = list_entry(ele, struct winch, list);
749 if(winch->tty == tty){
750 found = winch;
751 break;
752 }
753 }
754
755 if(found == NULL)
756 goto out;
757
758 if(winch->pid != -1)
759 os_kill_process(winch->pid, 1);
760
761 free_irq_by_irq_and_dev(WINCH_IRQ, winch);
762 free_irq(WINCH_IRQ, winch);
763 list_del(&winch->list);
764 kfree(winch);
765 out:
766 up(&winch_handler_sem);
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static void winch_cleanup(void)
770{
771 struct list_head *ele;
772 struct winch *winch;
773
774 list_for_each(ele, &winch_handlers){
775 winch = list_entry(ele, struct winch, list);
776 if(winch->fd != -1){
777 deactivate_fd(winch->fd, WINCH_IRQ);
778 os_close_file(winch->fd);
779 }
780 if(winch->pid != -1)
781 os_kill_process(winch->pid, 1);
782 }
783}
784__uml_exitcall(winch_cleanup);
785
786char *add_xterm_umid(char *base)
787{
788 char *umid, *title;
789 int len;
790
791 umid = get_umid(1);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700792 if(umid == NULL)
793 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
796 title = kmalloc(len, GFP_KERNEL);
797 if(title == NULL){
798 printk("Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700799 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
802 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700803 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}