blob: b8e3e800ee41056c2bbd96956f531d240c11f3b0 [file] [log] [blame]
Jeff Dike165dc592006-01-06 00:18:57 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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{
Jeff Dike165dc592006-01-06 00:18:57 -080026 struct chan *chan = data;
27 struct line *line = chan->line;
28 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 if (line)
31 chan_interrupt(&line->chan_list, &line->task, tty, irq);
32 return IRQ_HANDLED;
33}
34
35static void line_timer_cb(void *arg)
36{
Jeff Dike165dc592006-01-06 00:18:57 -080037 struct line *line = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Jeff Dikee4dcee82006-01-06 00:18:58 -080039 if(!line->throttled)
40 chan_interrupt(&line->chan_list, &line->task, line->tty,
41 line->driver->read_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070044/* Returns the free space inside the ring buffer of this line.
45 *
46 * Should be called while holding line->lock (this does not modify datas).
47 */
48static int write_room(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 int n;
51
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070052 if (line->buffer == NULL)
53 return LINE_BUFSIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070055 /* This is for the case where the buffer is wrapped! */
56 n = line->head - line->tail;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (n <= 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070059 n = LINE_BUFSIZE + n; /* The other case */
60 return n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070063int line_write_room(struct tty_struct *tty)
64{
65 struct line *line = tty->driver_data;
66 unsigned long flags;
67 int room;
68
69 if (tty->stopped)
70 return 0;
71
72 spin_lock_irqsave(&line->lock, flags);
73 room = write_room(line);
74 spin_unlock_irqrestore(&line->lock, flags);
75
76 /*XXX: Warning to remove */
77 if (0 == room)
78 printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
79 __FUNCTION__,tty->name);
80 return room;
81}
82
83int line_chars_in_buffer(struct tty_struct *tty)
84{
85 struct line *line = tty->driver_data;
86 unsigned long flags;
87 int ret;
88
89 spin_lock_irqsave(&line->lock, flags);
90
91 /*write_room subtracts 1 for the needed NULL, so we readd it.*/
92 ret = LINE_BUFSIZE - (write_room(line) + 1);
93 spin_unlock_irqrestore(&line->lock, flags);
94
95 return ret;
96}
97
98/*
99 * This copies the content of buf into the circular buffer associated with
100 * this line.
101 * The return value is the number of characters actually copied, i.e. the ones
102 * for which there was space: this function is not supposed to ever flush out
103 * the circular buffer.
104 *
105 * Must be called while holding line->lock!
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int buffer_data(struct line *line, const char *buf, int len)
108{
109 int end, room;
110
111 if(line->buffer == NULL){
112 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
113 if (line->buffer == NULL) {
114 printk("buffer_data - atomic allocation failed\n");
115 return(0);
116 }
117 line->head = line->buffer;
118 line->tail = line->buffer;
119 }
120
121 room = write_room(line);
122 len = (len > room) ? room : len;
123
124 end = line->buffer + LINE_BUFSIZE - line->tail;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700125
126 if (len < end){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 memcpy(line->tail, buf, len);
128 line->tail += len;
Jeff Diked50084a2006-01-06 00:18:50 -0800129 }
130 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700131 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 memcpy(line->tail, buf, end);
133 buf += end;
134 memcpy(line->buffer, buf, len - end);
135 line->tail = line->buffer + len - end;
136 }
137
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700138 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700141/*
142 * Flushes the ring buffer to the output channels. That is, write_chan is
143 * called, passing it line->head as buffer, and an appropriate count.
144 *
145 * On exit, returns 1 when the buffer is empty,
146 * 0 when the buffer is not empty on exit,
147 * and -errno when an error occurred.
148 *
149 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int flush_buffer(struct line *line)
151{
152 int n, count;
153
154 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700155 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700158 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 n = write_chan(&line->chan_list, line->head, count,
162 line->driver->write_irq);
163 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700164 return n;
165 if (n == count) {
166 /* We have flushed from ->head to buffer end, now we
167 * must flush only from the beginning to ->tail.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700169 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 }
174
175 count = line->tail - line->head;
Jeff Diked50084a2006-01-06 00:18:50 -0800176 n = write_chan(&line->chan_list, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700178
179 if(n < 0)
180 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700183 return line->head == line->tail;
184}
185
186void line_flush_buffer(struct tty_struct *tty)
187{
188 struct line *line = tty->driver_data;
189 unsigned long flags;
190 int err;
191
192 /*XXX: copied from line_write, verify if it is correct!*/
193 if(tty->stopped)
194 return;
195 //return 0;
196
197 spin_lock_irqsave(&line->lock, flags);
198 err = flush_buffer(line);
199 /*if (err == 1)
200 err = 0;*/
201 spin_unlock_irqrestore(&line->lock, flags);
202 //return err;
203}
204
205/* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
206 * and ->write. Hope it's not that bad.*/
207void line_flush_chars(struct tty_struct *tty)
208{
209 line_flush_buffer(tty);
210}
211
212void line_put_char(struct tty_struct *tty, unsigned char ch)
213{
214 line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
218{
219 struct line *line = tty->driver_data;
220 unsigned long flags;
221 int n, err, ret = 0;
222
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700223 if(tty->stopped)
224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700226 spin_lock_irqsave(&line->lock, flags);
227 if (line->head != line->tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 ret = buffer_data(line, buf, len);
229 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700230 if (err <= 0 && (err != -EAGAIN || !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ret = err;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700232 } else {
Jeff Diked50084a2006-01-06 00:18:50 -0800233 n = write_chan(&line->chan_list, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700235 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 ret = n;
237 goto out_up;
238 }
239
240 len -= n;
241 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700242 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 ret += buffer_data(line, buf + n, len);
244 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700245out_up:
246 spin_unlock_irqrestore(&line->lock, flags);
247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250void line_set_termios(struct tty_struct *tty, struct termios * old)
251{
252 /* nothing */
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static struct {
256 int cmd;
257 char *level;
258 char *name;
259} tty_ioctls[] = {
260 /* don't print these, they flood the log ... */
261 { TCGETS, NULL, "TCGETS" },
262 { TCSETS, NULL, "TCSETS" },
263 { TCSETSW, NULL, "TCSETSW" },
264 { TCFLSH, NULL, "TCFLSH" },
265 { TCSBRK, NULL, "TCSBRK" },
266
267 /* general tty stuff */
268 { TCSETSF, KERN_DEBUG, "TCSETSF" },
269 { TCGETA, KERN_DEBUG, "TCGETA" },
270 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
271 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
272 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
273
274 /* linux-specific ones */
275 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
276 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
277 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
278 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
279};
280
281int line_ioctl(struct tty_struct *tty, struct file * file,
282 unsigned int cmd, unsigned long arg)
283{
284 int ret;
285 int i;
286
287 ret = 0;
288 switch(cmd) {
289#ifdef TIOCGETP
290 case TIOCGETP:
291 case TIOCSETP:
292 case TIOCSETN:
293#endif
294#ifdef TIOCGETC
295 case TIOCGETC:
296 case TIOCSETC:
297#endif
298#ifdef TIOCGLTC
299 case TIOCGLTC:
300 case TIOCSLTC:
301#endif
302 case TCGETS:
303 case TCSETSF:
304 case TCSETSW:
305 case TCSETS:
306 case TCGETA:
307 case TCSETAF:
308 case TCSETAW:
309 case TCSETA:
310 case TCXONC:
311 case TCFLSH:
312 case TIOCOUTQ:
313 case TIOCINQ:
314 case TIOCGLCKTRMIOS:
315 case TIOCSLCKTRMIOS:
316 case TIOCPKT:
317 case TIOCGSOFTCAR:
318 case TIOCSSOFTCAR:
319 return -ENOIOCTLCMD;
320#if 0
321 case TCwhatever:
322 /* do something */
323 break;
324#endif
325 default:
326 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
327 if (cmd == tty_ioctls[i].cmd)
328 break;
329 if (i < ARRAY_SIZE(tty_ioctls)) {
330 if (NULL != tty_ioctls[i].level)
331 printk("%s%s: %s: ioctl %s called\n",
332 tty_ioctls[i].level, __FUNCTION__,
333 tty->name, tty_ioctls[i].name);
334 } else {
335 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
336 __FUNCTION__, tty->name, cmd);
337 }
338 ret = -ENOIOCTLCMD;
339 break;
340 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Jeff Dikee4dcee82006-01-06 00:18:58 -0800344void line_throttle(struct tty_struct *tty)
345{
346 struct line *line = tty->driver_data;
347
348 deactivate_chan(&line->chan_list, line->driver->read_irq);
349 line->throttled = 1;
350}
351
352void line_unthrottle(struct tty_struct *tty)
353{
354 struct line *line = tty->driver_data;
355
356 line->throttled = 0;
357 chan_interrupt(&line->chan_list, &line->task, tty,
358 line->driver->read_irq);
359
360 /* Maybe there is enough stuff pending that calling the interrupt
361 * throttles us again. In this case, line->throttled will be 1
362 * again and we shouldn't turn the interrupt back on.
363 */
364 if(!line->throttled)
365 reactivate_chan(&line->chan_list, line->driver->read_irq);
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static irqreturn_t line_write_interrupt(int irq, void *data,
369 struct pt_regs *unused)
370{
Jeff Dike165dc592006-01-06 00:18:57 -0800371 struct chan *chan = data;
372 struct line *line = chan->line;
373 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int err;
375
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700376 /* Interrupts are enabled here because we registered the interrupt with
377 * SA_INTERRUPT (see line_setup_irq).*/
378
379 spin_lock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700381 if (err == 0) {
382 return IRQ_NONE;
383 } else if(err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 line->head = line->buffer;
385 line->tail = line->buffer;
386 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700387 spin_unlock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if(tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700390 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700392 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 (tty->ldisc.write_wakeup != NULL))
394 (tty->ldisc.write_wakeup)(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* BLOCKING mode
397 * In blocking mode, everything sleeps on tty->write_wait.
398 * Sleeping in the console driver would break non-blocking
399 * writes.
400 */
401
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700402 if (waitqueue_active(&tty->write_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 wake_up_interruptible(&tty->write_wait);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700404 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Jeff Dike165dc592006-01-06 00:18:57 -0800407int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct line_driver *driver = line->driver;
410 int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
411
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700412 if (input)
413 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800414 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800415 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700416 if (err)
417 return err;
418 if (output)
419 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800420 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800421 driver->write_irq_name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Jeff Dike1f801712006-01-06 00:18:55 -0800426int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 struct line *line;
Jeff Dike165dc592006-01-06 00:18:57 -0800429 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 line = &lines[tty->index];
432 tty->driver_data = line;
433
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700434 /* The IRQ which takes this lock is not yet enabled and won't be run
435 * before the end, so we don't need to use spin_lock_irq.*/
436 spin_lock(&line->lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800437
438 tty->driver_data = line;
439 line->tty = tty;
440 if(!line->valid)
441 goto out;
442
443 if(tty->count == 1){
444 /* Here the device is opened, if necessary, and interrupt
445 * is registered.
446 */
447 enable_chan(line);
448 INIT_WORK(&line->task, line_timer_cb, line);
449
450 if(!line->sigio){
451 chan_enable_winch(&line->chan_list, tty);
452 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Jeff Dike1f801712006-01-06 00:18:55 -0800454
Jeff Dike165dc592006-01-06 00:18:57 -0800455 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
456 &tty->winsize.ws_col);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
Jeff Dike165dc592006-01-06 00:18:57 -0800459 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460out:
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
Jeff Dike165dc592006-01-06 00:18:57 -0800479 if(tty->count == 1){
480 line->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 tty->driver_data = NULL;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700482
Jeff Dike165dc592006-01-06 00:18:57 -0800483 if(line->sigio){
484 unregister_winch(tty);
485 line->sigio = 0;
486 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700487 }
488
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700489 spin_unlock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492void close_lines(struct line *lines, int nlines)
493{
494 int i;
495
496 for(i = 0; i < nlines; i++)
Jeff Dike165dc592006-01-06 00:18:57 -0800497 close_chan(&lines[i].chan_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700500/* Common setup code for both startup command line and mconsole initialization.
501 * @lines contains the the array (of size @num) to modify;
502 * @init is the setup string;
Jeff Diked571cd12006-01-06 00:18:53 -0800503 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700504
Jeff Diked571cd12006-01-06 00:18:53 -0800505int line_setup(struct line *lines, unsigned int num, char *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 int i, n;
508 char *end;
509
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700510 if(*init == '=') {
511 /* We said con=/ssl= instead of con#=, so we are configuring all
512 * consoles at once.*/
513 n = -1;
Jeff Diked50084a2006-01-06 00:18:50 -0800514 }
515 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 n = simple_strtoul(init, &end, 0);
517 if(*end != '='){
Jeff Diked50084a2006-01-06 00:18:50 -0800518 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 init);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 init = end;
523 }
524 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700525
526 if (n >= (signed int) num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
528 n, num - 1);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700529 return 0;
Jeff Diked50084a2006-01-06 00:18:50 -0800530 }
531 else if (n >= 0){
Jeff Dike165dc592006-01-06 00:18:57 -0800532 if (lines[n].tty != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 printk("line_setup - device %d is open\n", n);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536 if (lines[n].init_pri <= INIT_ONE){
537 lines[n].init_pri = INIT_ONE;
538 if (!strcmp(init, "none"))
539 lines[n].valid = 0;
540 else {
541 lines[n].init_str = init;
542 lines[n].valid = 1;
Jeff Dike165dc592006-01-06 00:18:57 -0800543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Jeff Diked50084a2006-01-06 00:18:50 -0800545 }
Jeff Diked50084a2006-01-06 00:18:50 -0800546 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 for(i = 0; i < num; i++){
548 if(lines[i].init_pri <= INIT_ALL){
549 lines[i].init_pri = INIT_ALL;
550 if(!strcmp(init, "none")) lines[i].valid = 0;
551 else {
552 lines[i].init_str = init;
553 lines[i].valid = 1;
554 }
555 }
556 }
557 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800558 return n == -1 ? num : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Jeff Dike1f801712006-01-06 00:18:55 -0800561int line_config(struct line *lines, unsigned int num, char *str,
562 struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Jeff Dike1f801712006-01-06 00:18:55 -0800564 struct line *line;
Jeff Dike970d6e32006-01-06 00:18:48 -0800565 char *new;
Jeff Dike418e55d2006-01-06 00:18:54 -0800566 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jeff Diked571cd12006-01-06 00:18:53 -0800568 if(*str == '='){
569 printk("line_config - can't configure all devices from "
570 "mconsole\n");
571 return 1;
572 }
573
Jeff Dike970d6e32006-01-06 00:18:48 -0800574 new = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if(new == NULL){
Jeff Dike970d6e32006-01-06 00:18:48 -0800576 printk("line_config - kstrdup failed\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800577 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800579 n = line_setup(lines, num, new);
Jeff Dike1f801712006-01-06 00:18:55 -0800580 if(n < 0)
581 return 1;
582
583 line = &lines[n];
Jeff Dike165dc592006-01-06 00:18:57 -0800584 return parse_chan_pair(line->init_str, line, n, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700587int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int size, char **error_out)
589{
590 struct line *line;
591 char *end;
592 int dev, n = 0;
593
594 dev = simple_strtoul(name, &end, 0);
595 if((*end != '\0') || (end == name)){
596 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
600 if((dev < 0) || (dev >= num)){
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700601 *error_out = "device number out of range";
602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
605 line = &lines[dev];
606
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700607 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if(!line->valid)
609 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike165dc592006-01-06 00:18:57 -0800610 else if(line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 CONFIG_CHUNK(str, size, n, line->init_str, 1);
612 else n = chan_config_string(&line->chan_list, str, size, error_out);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700613 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700615 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Jeff Dike29d56cf2005-06-25 14:55:25 -0700618int line_id(char **str, int *start_out, int *end_out)
619{
620 char *end;
621 int n;
622
623 n = simple_strtoul(*str, &end, 0);
624 if((*end != '\0') || (end == *str))
625 return -1;
626
627 *str = end;
628 *start_out = n;
629 *end_out = n;
630 return n;
631}
632
633int line_remove(struct line *lines, unsigned int num, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Jeff Dike418e55d2006-01-06 00:18:54 -0800635 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 char config[sizeof("conxxxx=none\0")];
637
Jeff Dike29d56cf2005-06-25 14:55:25 -0700638 sprintf(config, "%d=none", n);
Jeff Dike418e55d2006-01-06 00:18:54 -0800639 err = line_setup(lines, num, config);
640 if(err >= 0)
641 err = 0;
642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645struct tty_driver *line_register_devfs(struct lines *set,
Jeff Diked50084a2006-01-06 00:18:50 -0800646 struct line_driver *line_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct tty_operations *ops, struct line *lines,
648 int nlines)
649{
650 int i;
651 struct tty_driver *driver = alloc_tty_driver(nlines);
652
653 if (!driver)
654 return NULL;
655
656 driver->driver_name = line_driver->name;
657 driver->name = line_driver->device_name;
658 driver->devfs_name = line_driver->devfs_name;
659 driver->major = line_driver->major;
660 driver->minor_start = line_driver->minor_start;
661 driver->type = line_driver->type;
662 driver->subtype = line_driver->subtype;
663 driver->flags = TTY_DRIVER_REAL_RAW;
664 driver->init_termios = tty_std_termios;
665 tty_set_operations(driver, ops);
666
667 if (tty_register_driver(driver)) {
668 printk("%s: can't register %s driver\n",
669 __FUNCTION__,line_driver->name);
670 put_tty_driver(driver);
671 return NULL;
672 }
673
674 for(i = 0; i < nlines; i++){
Jeff Diked50084a2006-01-06 00:18:50 -0800675 if(!lines[i].valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 tty_unregister_device(driver, i);
677 }
678
679 mconsole_register_dev(&line_driver->mc);
680 return driver;
681}
682
Jeff Dike90107722006-01-06 00:18:54 -0800683static DEFINE_SPINLOCK(winch_handler_lock);
684static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700685
Jeff Dike1f801712006-01-06 00:18:55 -0800686void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct line *line;
689 int i;
690
691 for(i = 0; i < nlines; i++){
692 line = &lines[i];
693 INIT_LIST_HEAD(&line->chan_list);
Jeff Dike90107722006-01-06 00:18:54 -0800694
Jeff Diked50084a2006-01-06 00:18:50 -0800695 if(line->init_str == NULL)
696 continue;
697
698 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
699 if(line->init_str == NULL)
700 printk("lines_init - kstrdup returned NULL\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800701
Jeff Dike165dc592006-01-06 00:18:57 -0800702 if(parse_chan_pair(line->init_str, line, i, opts)){
Jeff Dike1f801712006-01-06 00:18:55 -0800703 printk("parse_chan_pair failed for device %d\n", i);
704 line->valid = 0;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707}
708
709struct winch {
710 struct list_head list;
711 int fd;
712 int tty_fd;
713 int pid;
714 struct tty_struct *tty;
715};
716
717irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
718{
719 struct winch *winch = data;
720 struct tty_struct *tty;
721 struct line *line;
722 int err;
723 char c;
724
725 if(winch->fd != -1){
726 err = generic_read(winch->fd, &c, NULL);
727 if(err < 0){
728 if(err != -EAGAIN){
729 printk("winch_interrupt : read failed, "
730 "errno = %d\n", -err);
731 printk("fd %d is losing SIGWINCH support\n",
732 winch->tty_fd);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700733 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 goto out;
736 }
737 }
738 tty = winch->tty;
739 if (tty != NULL) {
740 line = tty->driver_data;
Jeff Diked50084a2006-01-06 00:18:50 -0800741 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 &tty->winsize.ws_col);
743 kill_pg(tty->pgrp, SIGWINCH, 1);
744 }
745 out:
746 if(winch->fd != -1)
747 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700748 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
752{
753 struct winch *winch;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
756 if (winch == NULL) {
757 printk("register_winch_irq - kmalloc failed\n");
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700758 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
762 .fd = fd,
763 .tty_fd = tty_fd,
764 .pid = pid,
765 .tty = tty });
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700766
767 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700769 spin_unlock(&winch_handler_lock);
770
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700771 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Jeff Diked50084a2006-01-06 00:18:50 -0800772 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 "winch", winch) < 0)
774 printk("register_winch_irq - failed to register IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700777static void unregister_winch(struct tty_struct *tty)
778{
779 struct list_head *ele;
780 struct winch *winch, *found = NULL;
781
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700782 spin_lock(&winch_handler_lock);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700783 list_for_each(ele, &winch_handlers){
784 winch = list_entry(ele, struct winch, list);
785 if(winch->tty == tty){
786 found = winch;
787 break;
788 }
789 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700790 if(found == NULL)
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700791 goto err;
792
793 list_del(&winch->list);
794 spin_unlock(&winch_handler_lock);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700795
796 if(winch->pid != -1)
797 os_kill_process(winch->pid, 1);
798
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700799 free_irq(WINCH_IRQ, winch);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700800 kfree(winch);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700801
802 return;
803err:
804 spin_unlock(&winch_handler_lock);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700805}
806
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700807/* XXX: No lock as it's an exitcall... is this valid? Depending on cleanup
808 * order... are we sure that nothing else is done on the list? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809static void winch_cleanup(void)
810{
811 struct list_head *ele;
812 struct winch *winch;
813
814 list_for_each(ele, &winch_handlers){
815 winch = list_entry(ele, struct winch, list);
816 if(winch->fd != -1){
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700817 /* Why is this different from the above free_irq(),
818 * which deactivates SIGIO? This searches the FD
819 * somewhere else and removes it from the list... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 deactivate_fd(winch->fd, WINCH_IRQ);
821 os_close_file(winch->fd);
822 }
Jeff Diked50084a2006-01-06 00:18:50 -0800823 if(winch->pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 os_kill_process(winch->pid, 1);
825 }
826}
827__uml_exitcall(winch_cleanup);
828
829char *add_xterm_umid(char *base)
830{
831 char *umid, *title;
832 int len;
833
834 umid = get_umid(1);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700835 if(umid == NULL)
836 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
839 title = kmalloc(len, GFP_KERNEL);
840 if(title == NULL){
841 printk("Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700842 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
845 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700846 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}