blob: da81d22ec04aaef3f4c8a7a23145ef8333f37c88 [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;
Jeff Diked50084a2006-01-06 00:18:50 -0800127 }
128 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700129 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 memcpy(line->tail, buf, end);
131 buf += end;
132 memcpy(line->buffer, buf, len - end);
133 line->tail = line->buffer + len - end;
134 }
135
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700136 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700139/*
140 * Flushes the ring buffer to the output channels. That is, write_chan is
141 * called, passing it line->head as buffer, and an appropriate count.
142 *
143 * On exit, returns 1 when the buffer is empty,
144 * 0 when the buffer is not empty on exit,
145 * and -errno when an error occurred.
146 *
147 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int flush_buffer(struct line *line)
149{
150 int n, count;
151
152 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700153 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700156 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 n = write_chan(&line->chan_list, line->head, count,
160 line->driver->write_irq);
161 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700162 return n;
163 if (n == count) {
164 /* We have flushed from ->head to buffer end, now we
165 * must flush only from the beginning to ->tail.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700167 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 }
172
173 count = line->tail - line->head;
Jeff Diked50084a2006-01-06 00:18:50 -0800174 n = write_chan(&line->chan_list, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700176
177 if(n < 0)
178 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700181 return line->head == line->tail;
182}
183
184void line_flush_buffer(struct tty_struct *tty)
185{
186 struct line *line = tty->driver_data;
187 unsigned long flags;
188 int err;
189
190 /*XXX: copied from line_write, verify if it is correct!*/
191 if(tty->stopped)
192 return;
193 //return 0;
194
195 spin_lock_irqsave(&line->lock, flags);
196 err = flush_buffer(line);
197 /*if (err == 1)
198 err = 0;*/
199 spin_unlock_irqrestore(&line->lock, flags);
200 //return err;
201}
202
203/* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
204 * and ->write. Hope it's not that bad.*/
205void line_flush_chars(struct tty_struct *tty)
206{
207 line_flush_buffer(tty);
208}
209
210void line_put_char(struct tty_struct *tty, unsigned char ch)
211{
212 line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
216{
217 struct line *line = tty->driver_data;
218 unsigned long flags;
219 int n, err, ret = 0;
220
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700221 if(tty->stopped)
222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700224 spin_lock_irqsave(&line->lock, flags);
225 if (line->head != line->tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ret = buffer_data(line, buf, len);
227 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700228 if (err <= 0 && (err != -EAGAIN || !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 ret = err;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700230 } else {
Jeff Diked50084a2006-01-06 00:18:50 -0800231 n = write_chan(&line->chan_list, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700233 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 ret = n;
235 goto out_up;
236 }
237
238 len -= n;
239 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700240 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 ret += buffer_data(line, buf + n, len);
242 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700243out_up:
244 spin_unlock_irqrestore(&line->lock, flags);
245 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248void line_set_termios(struct tty_struct *tty, struct termios * old)
249{
250 /* nothing */
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static struct {
254 int cmd;
255 char *level;
256 char *name;
257} tty_ioctls[] = {
258 /* don't print these, they flood the log ... */
259 { TCGETS, NULL, "TCGETS" },
260 { TCSETS, NULL, "TCSETS" },
261 { TCSETSW, NULL, "TCSETSW" },
262 { TCFLSH, NULL, "TCFLSH" },
263 { TCSBRK, NULL, "TCSBRK" },
264
265 /* general tty stuff */
266 { TCSETSF, KERN_DEBUG, "TCSETSF" },
267 { TCGETA, KERN_DEBUG, "TCGETA" },
268 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
269 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
270 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
271
272 /* linux-specific ones */
273 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
274 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
275 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
276 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
277};
278
279int line_ioctl(struct tty_struct *tty, struct file * file,
280 unsigned int cmd, unsigned long arg)
281{
282 int ret;
283 int i;
284
285 ret = 0;
286 switch(cmd) {
287#ifdef TIOCGETP
288 case TIOCGETP:
289 case TIOCSETP:
290 case TIOCSETN:
291#endif
292#ifdef TIOCGETC
293 case TIOCGETC:
294 case TIOCSETC:
295#endif
296#ifdef TIOCGLTC
297 case TIOCGLTC:
298 case TIOCSLTC:
299#endif
300 case TCGETS:
301 case TCSETSF:
302 case TCSETSW:
303 case TCSETS:
304 case TCGETA:
305 case TCSETAF:
306 case TCSETAW:
307 case TCSETA:
308 case TCXONC:
309 case TCFLSH:
310 case TIOCOUTQ:
311 case TIOCINQ:
312 case TIOCGLCKTRMIOS:
313 case TIOCSLCKTRMIOS:
314 case TIOCPKT:
315 case TIOCGSOFTCAR:
316 case TIOCSSOFTCAR:
317 return -ENOIOCTLCMD;
318#if 0
319 case TCwhatever:
320 /* do something */
321 break;
322#endif
323 default:
324 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
325 if (cmd == tty_ioctls[i].cmd)
326 break;
327 if (i < ARRAY_SIZE(tty_ioctls)) {
328 if (NULL != tty_ioctls[i].level)
329 printk("%s%s: %s: ioctl %s called\n",
330 tty_ioctls[i].level, __FUNCTION__,
331 tty->name, tty_ioctls[i].name);
332 } else {
333 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
334 __FUNCTION__, tty->name, cmd);
335 }
336 ret = -ENOIOCTLCMD;
337 break;
338 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342static irqreturn_t line_write_interrupt(int irq, void *data,
343 struct pt_regs *unused)
344{
345 struct tty_struct *tty = data;
346 struct line *line = tty->driver_data;
347 int err;
348
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700349 /* Interrupts are enabled here because we registered the interrupt with
350 * SA_INTERRUPT (see line_setup_irq).*/
351
352 spin_lock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700354 if (err == 0) {
355 return IRQ_NONE;
356 } else if(err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 line->head = line->buffer;
358 line->tail = line->buffer;
359 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700360 spin_unlock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if(tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700363 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700365 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 (tty->ldisc.write_wakeup != NULL))
367 (tty->ldisc.write_wakeup)(tty);
368
369 /* BLOCKING mode
370 * In blocking mode, everything sleeps on tty->write_wait.
371 * Sleeping in the console driver would break non-blocking
372 * writes.
373 */
374
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700375 if (waitqueue_active(&tty->write_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 wake_up_interruptible(&tty->write_wait);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700377 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380int line_setup_irq(int fd, int input, int output, struct tty_struct *tty)
381{
382 struct line *line = tty->driver_data;
383 struct line_driver *driver = line->driver;
384 int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
385
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700386 if (input)
387 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800388 line_interrupt, flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 driver->read_irq_name, tty);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700390 if (err)
391 return err;
392 if (output)
393 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800394 line_write_interrupt, flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 driver->write_irq_name, tty);
396 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700397 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400void line_disable(struct tty_struct *tty, int current_irq)
401{
402 struct line *line = tty->driver_data;
403
404 if(!line->have_irq)
405 return;
406
407 if(line->driver->read_irq == current_irq)
408 free_irq_later(line->driver->read_irq, tty);
409 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 free_irq(line->driver->write_irq, tty);
417 }
418
419 line->have_irq = 0;
420}
421
Jeff Dike1f801712006-01-06 00:18:55 -0800422int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct line *line;
425 int err = 0;
426
427 line = &lines[tty->index];
428 tty->driver_data = line;
429
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700430 /* The IRQ which takes this lock is not yet enabled and won't be run
431 * before the end, so we don't need to use spin_lock_irq.*/
432 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (tty->count == 1) {
434 if (!line->valid) {
435 err = -ENODEV;
436 goto out;
437 }
Jeff Dike1f801712006-01-06 00:18:55 -0800438
439 err = open_chan(&line->chan_list);
440 if(err)
441 goto out;
442
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700443 /* Here the interrupt is registered.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 enable_chan(&line->chan_list, tty);
445 INIT_WORK(&line->task, line_timer_cb, tty);
446 }
447
448 if(!line->sigio){
449 chan_enable_winch(&line->chan_list, tty);
450 line->sigio = 1;
451 }
452 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
453 &tty->winsize.ws_col);
454 line->count++;
455
456out:
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700457 spin_unlock(&line->lock);
458 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700461static void unregister_winch(struct tty_struct *tty);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463void line_close(struct tty_struct *tty, struct file * filp)
464{
465 struct line *line = tty->driver_data;
466
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700467 /* XXX: I assume this should be called in process context, not with
468 * interrupts disabled!
469 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700470 spin_lock_irq(&line->lock);
471
472 /* We ignore the error anyway! */
473 flush_buffer(line);
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 line->count--;
476 if (tty->count == 1) {
477 line_disable(tty, -1);
478 tty->driver_data = NULL;
479 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700480
481 if((line->count == 0) && line->sigio){
482 unregister_winch(tty);
483 line->sigio = 0;
484 }
485
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700486 spin_unlock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489void close_lines(struct line *lines, int nlines)
490{
491 int i;
492
493 for(i = 0; i < nlines; i++)
494 close_chan(&lines[i].chan_list);
495}
496
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700497/* Common setup code for both startup command line and mconsole initialization.
498 * @lines contains the the array (of size @num) to modify;
499 * @init is the setup string;
Jeff Diked571cd12006-01-06 00:18:53 -0800500 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700501
Jeff Diked571cd12006-01-06 00:18:53 -0800502int line_setup(struct line *lines, unsigned int num, char *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 int i, n;
505 char *end;
506
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700507 if(*init == '=') {
508 /* We said con=/ssl= instead of con#=, so we are configuring all
509 * consoles at once.*/
510 n = -1;
Jeff Diked50084a2006-01-06 00:18:50 -0800511 }
512 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 n = simple_strtoul(init, &end, 0);
514 if(*end != '='){
Jeff Diked50084a2006-01-06 00:18:50 -0800515 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 init);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 init = end;
520 }
521 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700522
523 if (n >= (signed int) num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
525 n, num - 1);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700526 return 0;
Jeff Diked50084a2006-01-06 00:18:50 -0800527 }
528 else if (n >= 0){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (lines[n].count > 0) {
530 printk("line_setup - device %d is open\n", n);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 if (lines[n].init_pri <= INIT_ONE){
534 lines[n].init_pri = INIT_ONE;
535 if (!strcmp(init, "none"))
536 lines[n].valid = 0;
537 else {
538 lines[n].init_str = init;
539 lines[n].valid = 1;
540 }
541 }
Jeff Diked50084a2006-01-06 00:18:50 -0800542 }
Jeff Diked50084a2006-01-06 00:18:50 -0800543 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 for(i = 0; i < num; i++){
545 if(lines[i].init_pri <= INIT_ALL){
546 lines[i].init_pri = INIT_ALL;
547 if(!strcmp(init, "none")) lines[i].valid = 0;
548 else {
549 lines[i].init_str = init;
550 lines[i].valid = 1;
551 }
552 }
553 }
554 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800555 return n == -1 ? num : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Jeff Dike1f801712006-01-06 00:18:55 -0800558int line_config(struct line *lines, unsigned int num, char *str,
559 struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Jeff Dike1f801712006-01-06 00:18:55 -0800561 struct line *line;
Jeff Dike970d6e32006-01-06 00:18:48 -0800562 char *new;
Jeff Dike418e55d2006-01-06 00:18:54 -0800563 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Jeff Diked571cd12006-01-06 00:18:53 -0800565 if(*str == '='){
566 printk("line_config - can't configure all devices from "
567 "mconsole\n");
568 return 1;
569 }
570
Jeff Dike970d6e32006-01-06 00:18:48 -0800571 new = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if(new == NULL){
Jeff Dike970d6e32006-01-06 00:18:48 -0800573 printk("line_config - kstrdup failed\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800574 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800576 n = line_setup(lines, num, new);
Jeff Dike1f801712006-01-06 00:18:55 -0800577 if(n < 0)
578 return 1;
579
580 line = &lines[n];
581 return parse_chan_pair(line->init_str, &line->chan_list, n, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700584int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int size, char **error_out)
586{
587 struct line *line;
588 char *end;
589 int dev, n = 0;
590
591 dev = simple_strtoul(name, &end, 0);
592 if((*end != '\0') || (end == name)){
593 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 if((dev < 0) || (dev >= num)){
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700598 *error_out = "device number out of range";
599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 line = &lines[dev];
603
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700604 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if(!line->valid)
606 CONFIG_CHUNK(str, size, n, "none", 1);
607 else if(line->count == 0)
608 CONFIG_CHUNK(str, size, n, line->init_str, 1);
609 else n = chan_config_string(&line->chan_list, str, size, error_out);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700610 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700612 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Jeff Dike29d56cf2005-06-25 14:55:25 -0700615int line_id(char **str, int *start_out, int *end_out)
616{
617 char *end;
618 int n;
619
620 n = simple_strtoul(*str, &end, 0);
621 if((*end != '\0') || (end == *str))
622 return -1;
623
624 *str = end;
625 *start_out = n;
626 *end_out = n;
627 return n;
628}
629
630int line_remove(struct line *lines, unsigned int num, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Jeff Dike418e55d2006-01-06 00:18:54 -0800632 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 char config[sizeof("conxxxx=none\0")];
634
Jeff Dike29d56cf2005-06-25 14:55:25 -0700635 sprintf(config, "%d=none", n);
Jeff Dike418e55d2006-01-06 00:18:54 -0800636 err = line_setup(lines, num, config);
637 if(err >= 0)
638 err = 0;
639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642struct tty_driver *line_register_devfs(struct lines *set,
Jeff Diked50084a2006-01-06 00:18:50 -0800643 struct line_driver *line_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct tty_operations *ops, struct line *lines,
645 int nlines)
646{
647 int i;
648 struct tty_driver *driver = alloc_tty_driver(nlines);
649
650 if (!driver)
651 return NULL;
652
653 driver->driver_name = line_driver->name;
654 driver->name = line_driver->device_name;
655 driver->devfs_name = line_driver->devfs_name;
656 driver->major = line_driver->major;
657 driver->minor_start = line_driver->minor_start;
658 driver->type = line_driver->type;
659 driver->subtype = line_driver->subtype;
660 driver->flags = TTY_DRIVER_REAL_RAW;
661 driver->init_termios = tty_std_termios;
662 tty_set_operations(driver, ops);
663
664 if (tty_register_driver(driver)) {
665 printk("%s: can't register %s driver\n",
666 __FUNCTION__,line_driver->name);
667 put_tty_driver(driver);
668 return NULL;
669 }
670
671 for(i = 0; i < nlines; i++){
Jeff Diked50084a2006-01-06 00:18:50 -0800672 if(!lines[i].valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 tty_unregister_device(driver, i);
674 }
675
676 mconsole_register_dev(&line_driver->mc);
677 return driver;
678}
679
Jeff Dike90107722006-01-06 00:18:54 -0800680static DEFINE_SPINLOCK(winch_handler_lock);
681static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700682
Jeff Dike1f801712006-01-06 00:18:55 -0800683void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct line *line;
686 int i;
687
688 for(i = 0; i < nlines; i++){
689 line = &lines[i];
690 INIT_LIST_HEAD(&line->chan_list);
Jeff Dike90107722006-01-06 00:18:54 -0800691
Jeff Diked50084a2006-01-06 00:18:50 -0800692 if(line->init_str == NULL)
693 continue;
694
695 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
696 if(line->init_str == NULL)
697 printk("lines_init - kstrdup returned NULL\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800698
699 if(parse_chan_pair(line->init_str, &line->chan_list, i, opts)){
700 printk("parse_chan_pair failed for device %d\n", i);
701 line->valid = 0;
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704}
705
706struct winch {
707 struct list_head list;
708 int fd;
709 int tty_fd;
710 int pid;
711 struct tty_struct *tty;
712};
713
714irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
715{
716 struct winch *winch = data;
717 struct tty_struct *tty;
718 struct line *line;
719 int err;
720 char c;
721
722 if(winch->fd != -1){
723 err = generic_read(winch->fd, &c, NULL);
724 if(err < 0){
725 if(err != -EAGAIN){
726 printk("winch_interrupt : read failed, "
727 "errno = %d\n", -err);
728 printk("fd %d is losing SIGWINCH support\n",
729 winch->tty_fd);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700730 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732 goto out;
733 }
734 }
735 tty = winch->tty;
736 if (tty != NULL) {
737 line = tty->driver_data;
Jeff Diked50084a2006-01-06 00:18:50 -0800738 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 &tty->winsize.ws_col);
740 kill_pg(tty->pgrp, SIGWINCH, 1);
741 }
742 out:
743 if(winch->fd != -1)
744 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700745 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
749{
750 struct winch *winch;
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
753 if (winch == NULL) {
754 printk("register_winch_irq - kmalloc failed\n");
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700755 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
759 .fd = fd,
760 .tty_fd = tty_fd,
761 .pid = pid,
762 .tty = tty });
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700763
764 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700766 spin_unlock(&winch_handler_lock);
767
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700768 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Jeff Diked50084a2006-01-06 00:18:50 -0800769 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 "winch", winch) < 0)
771 printk("register_winch_irq - failed to register IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700774static void unregister_winch(struct tty_struct *tty)
775{
776 struct list_head *ele;
777 struct winch *winch, *found = NULL;
778
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700779 spin_lock(&winch_handler_lock);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700780 list_for_each(ele, &winch_handlers){
781 winch = list_entry(ele, struct winch, list);
782 if(winch->tty == tty){
783 found = winch;
784 break;
785 }
786 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700787 if(found == NULL)
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700788 goto err;
789
790 list_del(&winch->list);
791 spin_unlock(&winch_handler_lock);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700792
793 if(winch->pid != -1)
794 os_kill_process(winch->pid, 1);
795
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700796 free_irq(WINCH_IRQ, winch);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700797 kfree(winch);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700798
799 return;
800err:
801 spin_unlock(&winch_handler_lock);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700802}
803
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700804/* XXX: No lock as it's an exitcall... is this valid? Depending on cleanup
805 * order... are we sure that nothing else is done on the list? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806static void winch_cleanup(void)
807{
808 struct list_head *ele;
809 struct winch *winch;
810
811 list_for_each(ele, &winch_handlers){
812 winch = list_entry(ele, struct winch, list);
813 if(winch->fd != -1){
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700814 /* Why is this different from the above free_irq(),
815 * which deactivates SIGIO? This searches the FD
816 * somewhere else and removes it from the list... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 deactivate_fd(winch->fd, WINCH_IRQ);
818 os_close_file(winch->fd);
819 }
Jeff Diked50084a2006-01-06 00:18:50 -0800820 if(winch->pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 os_kill_process(winch->pid, 1);
822 }
823}
824__uml_exitcall(winch_cleanup);
825
826char *add_xterm_umid(char *base)
827{
828 char *umid, *title;
829 int len;
830
831 umid = get_umid(1);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700832 if(umid == NULL)
833 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
836 title = kmalloc(len, GFP_KERNEL);
837 if(title == NULL){
838 printk("Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700839 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700843 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}