blob: ebebaabb78ad75150ff818cd092f7c918b3fe7dd [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "asm/uaccess.h"
12#include "chan_kern.h"
13#include "irq_user.h"
14#include "line.h"
15#include "kern.h"
16#include "user_util.h"
17#include "kern_util.h"
18#include "os.h"
19#include "irq_kern.h"
20
21#define LINE_BUFSIZE 4096
22
23static irqreturn_t line_interrupt(int irq, void *data, struct pt_regs *unused)
24{
Jeff Dike165dc592006-01-06 00:18:57 -080025 struct chan *chan = data;
26 struct line *line = chan->line;
27 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
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{
Jeff Dike165dc592006-01-06 00:18:57 -080036 struct line *line = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Jeff Dikee4dcee82006-01-06 00:18:58 -080038 if(!line->throttled)
39 chan_interrupt(&line->chan_list, &line->task, line->tty,
40 line->driver->read_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070043/* Returns the free space inside the ring buffer of this line.
44 *
45 * Should be called while holding line->lock (this does not modify datas).
46 */
47static int write_room(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 int n;
50
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070051 if (line->buffer == NULL)
52 return LINE_BUFSIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070054 /* This is for the case where the buffer is wrapped! */
55 n = line->head - line->tail;
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (n <= 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070058 n = LINE_BUFSIZE + n; /* The other case */
59 return n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070062int line_write_room(struct tty_struct *tty)
63{
64 struct line *line = tty->driver_data;
65 unsigned long flags;
66 int room;
67
68 if (tty->stopped)
69 return 0;
70
71 spin_lock_irqsave(&line->lock, flags);
72 room = write_room(line);
73 spin_unlock_irqrestore(&line->lock, flags);
74
75 /*XXX: Warning to remove */
76 if (0 == room)
77 printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
78 __FUNCTION__,tty->name);
79 return room;
80}
81
82int line_chars_in_buffer(struct tty_struct *tty)
83{
84 struct line *line = tty->driver_data;
85 unsigned long flags;
86 int ret;
87
88 spin_lock_irqsave(&line->lock, flags);
89
90 /*write_room subtracts 1 for the needed NULL, so we readd it.*/
91 ret = LINE_BUFSIZE - (write_room(line) + 1);
92 spin_unlock_irqrestore(&line->lock, flags);
93
94 return ret;
95}
96
97/*
98 * This copies the content of buf into the circular buffer associated with
99 * this line.
100 * The return value is the number of characters actually copied, i.e. the ones
101 * for which there was space: this function is not supposed to ever flush out
102 * the circular buffer.
103 *
104 * Must be called while holding line->lock!
105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int buffer_data(struct line *line, const char *buf, int len)
107{
108 int end, room;
109
110 if(line->buffer == NULL){
111 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
112 if (line->buffer == NULL) {
113 printk("buffer_data - atomic allocation failed\n");
114 return(0);
115 }
116 line->head = line->buffer;
117 line->tail = line->buffer;
118 }
119
120 room = write_room(line);
121 len = (len > room) ? room : len;
122
123 end = line->buffer + LINE_BUFSIZE - line->tail;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700124
125 if (len < end){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 memcpy(line->tail, buf, len);
127 line->tail += len;
Jeff Diked50084a2006-01-06 00:18:50 -0800128 }
129 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700130 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 memcpy(line->tail, buf, end);
132 buf += end;
133 memcpy(line->buffer, buf, len - end);
134 line->tail = line->buffer + len - end;
135 }
136
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700137 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700140/*
141 * Flushes the ring buffer to the output channels. That is, write_chan is
142 * called, passing it line->head as buffer, and an appropriate count.
143 *
144 * On exit, returns 1 when the buffer is empty,
145 * 0 when the buffer is not empty on exit,
146 * and -errno when an error occurred.
147 *
148 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static int flush_buffer(struct line *line)
150{
151 int n, count;
152
153 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700154 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700157 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 n = write_chan(&line->chan_list, line->head, count,
161 line->driver->write_irq);
162 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700163 return n;
164 if (n == count) {
165 /* We have flushed from ->head to buffer end, now we
166 * must flush only from the beginning to ->tail.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700168 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 }
173
174 count = line->tail - line->head;
Jeff Diked50084a2006-01-06 00:18:50 -0800175 n = write_chan(&line->chan_list, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700177
178 if(n < 0)
179 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700182 return line->head == line->tail;
183}
184
185void line_flush_buffer(struct tty_struct *tty)
186{
187 struct line *line = tty->driver_data;
188 unsigned long flags;
189 int err;
190
191 /*XXX: copied from line_write, verify if it is correct!*/
192 if(tty->stopped)
193 return;
194 //return 0;
195
196 spin_lock_irqsave(&line->lock, flags);
197 err = flush_buffer(line);
198 /*if (err == 1)
199 err = 0;*/
200 spin_unlock_irqrestore(&line->lock, flags);
201 //return err;
202}
203
204/* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
205 * and ->write. Hope it's not that bad.*/
206void line_flush_chars(struct tty_struct *tty)
207{
208 line_flush_buffer(tty);
209}
210
211void line_put_char(struct tty_struct *tty, unsigned char ch)
212{
213 line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
217{
218 struct line *line = tty->driver_data;
219 unsigned long flags;
220 int n, err, ret = 0;
221
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700222 if(tty->stopped)
223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700225 spin_lock_irqsave(&line->lock, flags);
226 if (line->head != line->tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ret = buffer_data(line, buf, len);
228 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700229 if (err <= 0 && (err != -EAGAIN || !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 ret = err;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700231 } else {
Jeff Diked50084a2006-01-06 00:18:50 -0800232 n = write_chan(&line->chan_list, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700234 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ret = n;
236 goto out_up;
237 }
238
239 len -= n;
240 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700241 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ret += buffer_data(line, buf + n, len);
243 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700244out_up:
245 spin_unlock_irqrestore(&line->lock, flags);
246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249void line_set_termios(struct tty_struct *tty, struct termios * old)
250{
251 /* nothing */
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static struct {
255 int cmd;
256 char *level;
257 char *name;
258} tty_ioctls[] = {
259 /* don't print these, they flood the log ... */
260 { TCGETS, NULL, "TCGETS" },
261 { TCSETS, NULL, "TCSETS" },
262 { TCSETSW, NULL, "TCSETSW" },
263 { TCFLSH, NULL, "TCFLSH" },
264 { TCSBRK, NULL, "TCSBRK" },
265
266 /* general tty stuff */
267 { TCSETSF, KERN_DEBUG, "TCSETSF" },
268 { TCGETA, KERN_DEBUG, "TCGETA" },
269 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
270 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
271 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
272
273 /* linux-specific ones */
274 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
275 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
276 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
277 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
278};
279
280int line_ioctl(struct tty_struct *tty, struct file * file,
281 unsigned int cmd, unsigned long arg)
282{
283 int ret;
284 int i;
285
286 ret = 0;
287 switch(cmd) {
288#ifdef TIOCGETP
289 case TIOCGETP:
290 case TIOCSETP:
291 case TIOCSETN:
292#endif
293#ifdef TIOCGETC
294 case TIOCGETC:
295 case TIOCSETC:
296#endif
297#ifdef TIOCGLTC
298 case TIOCGLTC:
299 case TIOCSLTC:
300#endif
301 case TCGETS:
302 case TCSETSF:
303 case TCSETSW:
304 case TCSETS:
305 case TCGETA:
306 case TCSETAF:
307 case TCSETAW:
308 case TCSETA:
309 case TCXONC:
310 case TCFLSH:
311 case TIOCOUTQ:
312 case TIOCINQ:
313 case TIOCGLCKTRMIOS:
314 case TIOCSLCKTRMIOS:
315 case TIOCPKT:
316 case TIOCGSOFTCAR:
317 case TIOCSSOFTCAR:
318 return -ENOIOCTLCMD;
319#if 0
320 case TCwhatever:
321 /* do something */
322 break;
323#endif
324 default:
325 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
326 if (cmd == tty_ioctls[i].cmd)
327 break;
328 if (i < ARRAY_SIZE(tty_ioctls)) {
329 if (NULL != tty_ioctls[i].level)
330 printk("%s%s: %s: ioctl %s called\n",
331 tty_ioctls[i].level, __FUNCTION__,
332 tty->name, tty_ioctls[i].name);
333 } else {
334 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
335 __FUNCTION__, tty->name, cmd);
336 }
337 ret = -ENOIOCTLCMD;
338 break;
339 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700340 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Jeff Dikee4dcee82006-01-06 00:18:58 -0800343void line_throttle(struct tty_struct *tty)
344{
345 struct line *line = tty->driver_data;
346
347 deactivate_chan(&line->chan_list, line->driver->read_irq);
348 line->throttled = 1;
349}
350
351void line_unthrottle(struct tty_struct *tty)
352{
353 struct line *line = tty->driver_data;
354
355 line->throttled = 0;
356 chan_interrupt(&line->chan_list, &line->task, tty,
357 line->driver->read_irq);
358
359 /* Maybe there is enough stuff pending that calling the interrupt
360 * throttles us again. In this case, line->throttled will be 1
361 * again and we shouldn't turn the interrupt back on.
362 */
363 if(!line->throttled)
364 reactivate_chan(&line->chan_list, line->driver->read_irq);
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static irqreturn_t line_write_interrupt(int irq, void *data,
368 struct pt_regs *unused)
369{
Jeff Dike165dc592006-01-06 00:18:57 -0800370 struct chan *chan = data;
371 struct line *line = chan->line;
372 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int err;
374
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700375 /* Interrupts are enabled here because we registered the interrupt with
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700376 * IRQF_DISABLED (see line_setup_irq).*/
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700377
378 spin_lock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700380 if (err == 0) {
381 return IRQ_NONE;
382 } else if(err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 line->head = line->buffer;
384 line->tail = line->buffer;
385 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700386 spin_unlock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if(tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700389 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700391 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 (tty->ldisc.write_wakeup != NULL))
393 (tty->ldisc.write_wakeup)(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* BLOCKING mode
396 * In blocking mode, everything sleeps on tty->write_wait.
397 * Sleeping in the console driver would break non-blocking
398 * writes.
399 */
400
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700401 if (waitqueue_active(&tty->write_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 wake_up_interruptible(&tty->write_wait);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700403 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Jeff Dike165dc592006-01-06 00:18:57 -0800406int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct line_driver *driver = line->driver;
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700409 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700411 if (input)
412 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800413 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800414 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700415 if (err)
416 return err;
417 if (output)
418 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800419 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800420 driver->write_irq_name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700422 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Jeff Dike1f801712006-01-06 00:18:55 -0800425int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct line *line;
Jeff Dike165dc592006-01-06 00:18:57 -0800428 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 line = &lines[tty->index];
431 tty->driver_data = line;
432
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700433 /* The IRQ which takes this lock is not yet enabled and won't be run
434 * before the end, so we don't need to use spin_lock_irq.*/
435 spin_lock(&line->lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800436
437 tty->driver_data = line;
438 line->tty = tty;
439 if(!line->valid)
440 goto out;
441
442 if(tty->count == 1){
443 /* Here the device is opened, if necessary, and interrupt
444 * is registered.
445 */
446 enable_chan(line);
447 INIT_WORK(&line->task, line_timer_cb, line);
448
449 if(!line->sigio){
450 chan_enable_winch(&line->chan_list, tty);
451 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Jeff Dike1f801712006-01-06 00:18:55 -0800453
Jeff Dike165dc592006-01-06 00:18:57 -0800454 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
455 &tty->winsize.ws_col);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Jeff Dike165dc592006-01-06 00:18:57 -0800458 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459out:
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700460 spin_unlock(&line->lock);
461 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700464static void unregister_winch(struct tty_struct *tty);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466void line_close(struct tty_struct *tty, struct file * filp)
467{
468 struct line *line = tty->driver_data;
469
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700470 /* XXX: I assume this should be called in process context, not with
471 * interrupts disabled!
472 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700473 spin_lock_irq(&line->lock);
474
475 /* We ignore the error anyway! */
476 flush_buffer(line);
477
Jeff Dike165dc592006-01-06 00:18:57 -0800478 if(tty->count == 1){
479 line->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 tty->driver_data = NULL;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700481
Jeff Dike165dc592006-01-06 00:18:57 -0800482 if(line->sigio){
483 unregister_winch(tty);
484 line->sigio = 0;
485 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700486 }
487
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700488 spin_unlock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491void close_lines(struct line *lines, int nlines)
492{
493 int i;
494
495 for(i = 0; i < nlines; i++)
Jeff Dike165dc592006-01-06 00:18:57 -0800496 close_chan(&lines[i].chan_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700499/* Common setup code for both startup command line and mconsole initialization.
500 * @lines contains the the array (of size @num) to modify;
501 * @init is the setup string;
Jeff Diked571cd12006-01-06 00:18:53 -0800502 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700503
Jeff Diked571cd12006-01-06 00:18:53 -0800504int line_setup(struct line *lines, unsigned int num, char *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 int i, n;
507 char *end;
508
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700509 if(*init == '=') {
510 /* We said con=/ssl= instead of con#=, so we are configuring all
511 * consoles at once.*/
512 n = -1;
Jeff Diked50084a2006-01-06 00:18:50 -0800513 }
514 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 n = simple_strtoul(init, &end, 0);
516 if(*end != '='){
Jeff Diked50084a2006-01-06 00:18:50 -0800517 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 init);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 init = end;
522 }
523 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700524
525 if (n >= (signed int) num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
527 n, num - 1);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700528 return 0;
Jeff Diked50084a2006-01-06 00:18:50 -0800529 }
530 else if (n >= 0){
Jeff Dike165dc592006-01-06 00:18:57 -0800531 if (lines[n].tty != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 printk("line_setup - device %d is open\n", n);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 if (lines[n].init_pri <= INIT_ONE){
536 lines[n].init_pri = INIT_ONE;
537 if (!strcmp(init, "none"))
538 lines[n].valid = 0;
539 else {
540 lines[n].init_str = init;
541 lines[n].valid = 1;
Jeff Dike165dc592006-01-06 00:18:57 -0800542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Jeff Diked50084a2006-01-06 00:18:50 -0800544 }
Jeff Diked50084a2006-01-06 00:18:50 -0800545 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 for(i = 0; i < num; i++){
547 if(lines[i].init_pri <= INIT_ALL){
548 lines[i].init_pri = INIT_ALL;
549 if(!strcmp(init, "none")) lines[i].valid = 0;
550 else {
551 lines[i].init_str = init;
552 lines[i].valid = 1;
553 }
554 }
555 }
556 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800557 return n == -1 ? num : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Jeff Dike1f801712006-01-06 00:18:55 -0800560int line_config(struct line *lines, unsigned int num, char *str,
561 struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Jeff Dike1f801712006-01-06 00:18:55 -0800563 struct line *line;
Jeff Dike970d6e32006-01-06 00:18:48 -0800564 char *new;
Jeff Dike418e55d2006-01-06 00:18:54 -0800565 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Jeff Diked571cd12006-01-06 00:18:53 -0800567 if(*str == '='){
568 printk("line_config - can't configure all devices from "
569 "mconsole\n");
570 return 1;
571 }
572
Jeff Dike970d6e32006-01-06 00:18:48 -0800573 new = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if(new == NULL){
Jeff Dike970d6e32006-01-06 00:18:48 -0800575 printk("line_config - kstrdup failed\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800576 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800578 n = line_setup(lines, num, new);
Jeff Dike1f801712006-01-06 00:18:55 -0800579 if(n < 0)
580 return 1;
581
582 line = &lines[n];
Jeff Dike165dc592006-01-06 00:18:57 -0800583 return parse_chan_pair(line->init_str, line, n, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700586int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int size, char **error_out)
588{
589 struct line *line;
590 char *end;
591 int dev, n = 0;
592
593 dev = simple_strtoul(name, &end, 0);
594 if((*end != '\0') || (end == name)){
595 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
599 if((dev < 0) || (dev >= num)){
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700600 *error_out = "device number out of range";
601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
604 line = &lines[dev];
605
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700606 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if(!line->valid)
608 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike165dc592006-01-06 00:18:57 -0800609 else if(line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 CONFIG_CHUNK(str, size, n, line->init_str, 1);
611 else n = chan_config_string(&line->chan_list, str, size, error_out);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700612 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700614 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Jeff Dike29d56cf2005-06-25 14:55:25 -0700617int line_id(char **str, int *start_out, int *end_out)
618{
619 char *end;
620 int n;
621
622 n = simple_strtoul(*str, &end, 0);
623 if((*end != '\0') || (end == *str))
624 return -1;
625
626 *str = end;
627 *start_out = n;
628 *end_out = n;
629 return n;
630}
631
632int line_remove(struct line *lines, unsigned int num, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jeff Dike418e55d2006-01-06 00:18:54 -0800634 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 char config[sizeof("conxxxx=none\0")];
636
Jeff Dike29d56cf2005-06-25 14:55:25 -0700637 sprintf(config, "%d=none", n);
Jeff Dike418e55d2006-01-06 00:18:54 -0800638 err = line_setup(lines, num, config);
639 if(err >= 0)
640 err = 0;
641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644struct tty_driver *line_register_devfs(struct lines *set,
Jeff Diked50084a2006-01-06 00:18:50 -0800645 struct line_driver *line_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct tty_operations *ops, struct line *lines,
647 int nlines)
648{
649 int i;
650 struct tty_driver *driver = alloc_tty_driver(nlines);
651
652 if (!driver)
653 return NULL;
654
655 driver->driver_name = line_driver->name;
656 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 driver->major = line_driver->major;
658 driver->minor_start = line_driver->minor_start;
659 driver->type = line_driver->type;
660 driver->subtype = line_driver->subtype;
661 driver->flags = TTY_DRIVER_REAL_RAW;
662 driver->init_termios = tty_std_termios;
663 tty_set_operations(driver, ops);
664
665 if (tty_register_driver(driver)) {
666 printk("%s: can't register %s driver\n",
667 __FUNCTION__,line_driver->name);
668 put_tty_driver(driver);
669 return NULL;
670 }
671
672 for(i = 0; i < nlines; i++){
Jeff Diked50084a2006-01-06 00:18:50 -0800673 if(!lines[i].valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 tty_unregister_device(driver, i);
675 }
676
677 mconsole_register_dev(&line_driver->mc);
678 return driver;
679}
680
Jeff Dike90107722006-01-06 00:18:54 -0800681static DEFINE_SPINLOCK(winch_handler_lock);
682static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700683
Jeff Dike1f801712006-01-06 00:18:55 -0800684void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 struct line *line;
687 int i;
688
689 for(i = 0; i < nlines; i++){
690 line = &lines[i];
691 INIT_LIST_HEAD(&line->chan_list);
Jeff Dike90107722006-01-06 00:18:54 -0800692
Jeff Diked50084a2006-01-06 00:18:50 -0800693 if(line->init_str == NULL)
694 continue;
695
696 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
697 if(line->init_str == NULL)
698 printk("lines_init - kstrdup returned NULL\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800699
Jeff Dike165dc592006-01-06 00:18:57 -0800700 if(parse_chan_pair(line->init_str, line, i, opts)){
Jeff Dike1f801712006-01-06 00:18:55 -0800701 printk("parse_chan_pair failed for device %d\n", i);
702 line->valid = 0;
703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705}
706
707struct winch {
708 struct list_head list;
709 int fd;
710 int tty_fd;
711 int pid;
712 struct tty_struct *tty;
713};
714
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800715static irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct winch *winch = data;
718 struct tty_struct *tty;
719 struct line *line;
720 int err;
721 char c;
722
723 if(winch->fd != -1){
724 err = generic_read(winch->fd, &c, NULL);
725 if(err < 0){
726 if(err != -EAGAIN){
727 printk("winch_interrupt : read failed, "
728 "errno = %d\n", -err);
729 printk("fd %d is losing SIGWINCH support\n",
730 winch->tty_fd);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700731 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 goto out;
734 }
735 }
736 tty = winch->tty;
737 if (tty != NULL) {
738 line = tty->driver_data;
Jeff Diked50084a2006-01-06 00:18:50 -0800739 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 &tty->winsize.ws_col);
741 kill_pg(tty->pgrp, SIGWINCH, 1);
742 }
743 out:
744 if(winch->fd != -1)
745 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700746 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
750{
751 struct winch *winch;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
754 if (winch == NULL) {
755 printk("register_winch_irq - kmalloc failed\n");
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700756 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
760 .fd = fd,
761 .tty_fd = tty_fd,
762 .pid = pid,
763 .tty = tty });
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700764
765 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700767 spin_unlock(&winch_handler_lock);
768
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700769 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700770 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 "winch", winch) < 0)
772 printk("register_winch_irq - failed to register IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Jeff Dikee464bf22006-01-06 00:19:01 -0800775static void free_winch(struct winch *winch)
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700776{
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700777 list_del(&winch->list);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700778
Jeff Dikee464bf22006-01-06 00:19:01 -0800779 if(winch->pid != -1)
780 os_kill_process(winch->pid, 1);
781 if(winch->fd != -1)
782 os_close_file(winch->fd);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700783
Jeff Dikee464bf22006-01-06 00:19:01 -0800784 free_irq(WINCH_IRQ, winch);
785 kfree(winch);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700786}
787
Jeff Dikee464bf22006-01-06 00:19:01 -0800788static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct list_head *ele;
791 struct winch *winch;
792
Jeff Dikee464bf22006-01-06 00:19:01 -0800793 spin_lock(&winch_handler_lock);
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 list_for_each(ele, &winch_handlers){
796 winch = list_entry(ele, struct winch, list);
Jeff Dikee464bf22006-01-06 00:19:01 -0800797 if(winch->tty == tty){
798 free_winch(winch);
799 break;
800 }
801 }
802 spin_unlock(&winch_handler_lock);
803}
804
805static void winch_cleanup(void)
806{
807 struct list_head *ele, *next;
808 struct winch *winch;
809
810 spin_lock(&winch_handler_lock);
811
812 list_for_each_safe(ele, next, &winch_handlers){
813 winch = list_entry(ele, struct winch, list);
814 free_winch(winch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800816
817 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819__uml_exitcall(winch_cleanup);
820
821char *add_xterm_umid(char *base)
822{
823 char *umid, *title;
824 int len;
825
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800826 umid = get_umid();
827 if(*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700828 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
831 title = kmalloc(len, GFP_KERNEL);
832 if(title == NULL){
833 printk("Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700834 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700838 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}