blob: e620ed46ed3f507cf429cf9596c2b781c3ce391c [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
Al Viro7bea96f2006-10-08 22:49:34 +010023static irqreturn_t line_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
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
Andrew Mortona2ce7742006-12-06 20:31:36 -080034static void line_timer_cb(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Andrew Mortona2ce7742006-12-06 20:31:36 -080036 struct line *line = container_of(work, struct line, task.work);
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;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700194
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
Alan Cox606d0992006-12-08 02:38:45 -0800248void line_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 /* nothing */
251}
252
Jeff Dike5e7672e2006-09-27 01:50:33 -0700253static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 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
Jeff Dikee4dcee82006-01-06 00:18:58 -0800342void line_throttle(struct tty_struct *tty)
343{
344 struct line *line = tty->driver_data;
345
346 deactivate_chan(&line->chan_list, line->driver->read_irq);
347 line->throttled = 1;
348}
349
350void line_unthrottle(struct tty_struct *tty)
351{
352 struct line *line = tty->driver_data;
353
354 line->throttled = 0;
355 chan_interrupt(&line->chan_list, &line->task, tty,
356 line->driver->read_irq);
357
358 /* Maybe there is enough stuff pending that calling the interrupt
359 * throttles us again. In this case, line->throttled will be 1
360 * again and we shouldn't turn the interrupt back on.
361 */
362 if(!line->throttled)
363 reactivate_chan(&line->chan_list, line->driver->read_irq);
364}
365
Al Viro7bea96f2006-10-08 22:49:34 +0100366static irqreturn_t line_write_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jeff Dike165dc592006-01-06 00:18:57 -0800368 struct chan *chan = data;
369 struct line *line = chan->line;
370 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int err;
372
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700373 /* Interrupts are enabled here because we registered the interrupt with
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700374 * IRQF_DISABLED (see line_setup_irq).*/
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700375
376 spin_lock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700378 if (err == 0) {
379 return IRQ_NONE;
380 } else if(err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 line->head = line->buffer;
382 line->tail = line->buffer;
383 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700384 spin_unlock_irq(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 if(tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700387 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700389 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 (tty->ldisc.write_wakeup != NULL))
391 (tty->ldisc.write_wakeup)(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* BLOCKING mode
394 * In blocking mode, everything sleeps on tty->write_wait.
395 * Sleeping in the console driver would break non-blocking
396 * writes.
397 */
398
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700399 if (waitqueue_active(&tty->write_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 wake_up_interruptible(&tty->write_wait);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700401 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Jeff Dike165dc592006-01-06 00:18:57 -0800404int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700406 const struct line_driver *driver = line->driver;
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700407 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700409 if (input)
410 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800411 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800412 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700413 if (err)
414 return err;
415 if (output)
416 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800417 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800418 driver->write_irq_name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Jeff Diked79a5802007-02-10 01:43:52 -0800423/* Normally, a driver like this can rely mostly on the tty layer
424 * locking, particularly when it comes to the driver structure.
425 * However, in this case, mconsole requests can come in "from the
426 * side", and race with opens and closes.
427 *
428 * The problem comes from line_setup not wanting to sleep if
429 * the device is open or being opened. This can happen because the
430 * first opener of a device is responsible for setting it up on the
431 * host, and that can sleep. The open of a port device will sleep
432 * until someone telnets to it.
433 *
434 * The obvious solution of putting everything under a mutex fails
435 * because then trying (and failing) to change the configuration of an
436 * open(ing) device will block until the open finishes. The right
437 * thing to happen is for it to fail immediately.
438 *
439 * We can put the opening (and closing) of the host device under a
440 * separate lock, but that has to be taken before the count lock is
441 * released. Otherwise, you open a window in which another open can
442 * come through and assume that the host side is opened and working.
443 *
444 * So, if the tty count is one, open will take the open mutex
445 * inside the count lock. Otherwise, it just returns. This will sleep
446 * if the last close is pending, and will block a setup or get_config,
447 * but that should not last long.
448 *
449 * So, what we end up with is that open and close take the count lock.
450 * If the first open or last close are happening, then the open mutex
451 * is taken inside the count lock and the host opening or closing is done.
452 *
453 * setup and get_config only take the count lock. setup modifies the
454 * device configuration only if the open count is zero. Arbitrarily
455 * long blocking of setup doesn't happen because something would have to be
456 * waiting for an open to happen. However, a second open with
457 * tty->count == 1 can't happen, and a close can't happen until the open
458 * had finished.
459 *
460 * We can't maintain our own count here because the tty layer doesn't
461 * match opens and closes. It will call close if an open failed, and
462 * a tty hangup will result in excess closes. So, we rely on
463 * tty->count instead. It is one on both the first open and last close.
464 */
465
Jeff Dike1f801712006-01-06 00:18:55 -0800466int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Jeff Diked79a5802007-02-10 01:43:52 -0800468 struct line *line = &lines[tty->index];
Jeff Dike165dc592006-01-06 00:18:57 -0800469 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jeff Diked79a5802007-02-10 01:43:52 -0800471 spin_lock(&line->count_lock);
472 if(!line->valid)
473 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Jeff Diked79a5802007-02-10 01:43:52 -0800475 err = 0;
476 if(tty->count > 1)
477 goto out_unlock;
478
479 mutex_lock(&line->open_mutex);
480 spin_unlock(&line->count_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800481
482 tty->driver_data = line;
483 line->tty = tty;
Jeff Dike165dc592006-01-06 00:18:57 -0800484
Jeff Diked79a5802007-02-10 01:43:52 -0800485 enable_chan(line);
486 INIT_DELAYED_WORK(&line->task, line_timer_cb);
Jeff Dike165dc592006-01-06 00:18:57 -0800487
Jeff Diked79a5802007-02-10 01:43:52 -0800488 if(!line->sigio){
489 chan_enable_winch(&line->chan_list, tty);
490 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Jeff Diked79a5802007-02-10 01:43:52 -0800493 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
494 &tty->winsize.ws_col);
495
496 mutex_unlock(&line->open_mutex);
497 return err;
498
499out_unlock:
500 spin_unlock(&line->count_lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700504static void unregister_winch(struct tty_struct *tty);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506void line_close(struct tty_struct *tty, struct file * filp)
507{
508 struct line *line = tty->driver_data;
509
Jeff Diked79a5802007-02-10 01:43:52 -0800510 /* If line_open fails (and tty->driver_data is never set),
511 * tty_open will call line_close. So just return in this case.
512 */
513 if(line == NULL)
514 return;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700515
516 /* We ignore the error anyway! */
517 flush_buffer(line);
518
Jeff Diked79a5802007-02-10 01:43:52 -0800519 spin_lock(&line->count_lock);
520 if(!line->valid)
521 goto out_unlock;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700522
Jeff Diked79a5802007-02-10 01:43:52 -0800523 if(tty->count > 1)
524 goto out_unlock;
525
526 mutex_lock(&line->open_mutex);
527 spin_unlock(&line->count_lock);
528
529 line->tty = NULL;
530 tty->driver_data = NULL;
531
532 if(line->sigio){
533 unregister_winch(tty);
534 line->sigio = 0;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700535 }
536
Jeff Diked79a5802007-02-10 01:43:52 -0800537 mutex_unlock(&line->open_mutex);
538 return;
539
540out_unlock:
541 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544void close_lines(struct line *lines, int nlines)
545{
546 int i;
547
548 for(i = 0; i < nlines; i++)
Jeff Dike165dc592006-01-06 00:18:57 -0800549 close_chan(&lines[i].chan_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Jeff Dikef28169d2007-02-10 01:43:53 -0800552static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
553 char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800554{
555 struct line *line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800556 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800557
558 spin_lock(&line->count_lock);
559
560 if(line->tty != NULL){
Jeff Dikef28169d2007-02-10 01:43:53 -0800561 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800562 goto out;
563 }
564
565 if (line->init_pri <= init_prio){
566 line->init_pri = init_prio;
567 if (!strcmp(init, "none"))
568 line->valid = 0;
569 else {
570 line->init_str = init;
571 line->valid = 1;
572 }
573 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800574 err = 0;
Jeff Diked79a5802007-02-10 01:43:52 -0800575out:
576 spin_unlock(&line->count_lock);
Jeff Dikef28169d2007-02-10 01:43:53 -0800577 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800578}
579
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700580/* Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200581 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700582 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800583 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800584 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700585
Jeff Dikef28169d2007-02-10 01:43:53 -0800586int line_setup(struct line *lines, unsigned int num, char *init,
587 char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Jeff Dikef28169d2007-02-10 01:43:53 -0800589 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 char *end;
591
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700592 if(*init == '=') {
593 /* We said con=/ssl= instead of con#=, so we are configuring all
594 * consoles at once.*/
595 n = -1;
Jeff Diked50084a2006-01-06 00:18:50 -0800596 }
597 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 n = simple_strtoul(init, &end, 0);
599 if(*end != '='){
Jeff Dikef28169d2007-02-10 01:43:53 -0800600 *error_out = "Couldn't parse device number";
601 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 init = end;
604 }
605 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700606
607 if (n >= (signed int) num) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800608 *error_out = "Device number out of range";
609 return -EINVAL;
Jeff Diked50084a2006-01-06 00:18:50 -0800610 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800611 else if (n >= 0){
612 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
613 if(err)
614 return err;
615 }
Jeff Diked50084a2006-01-06 00:18:50 -0800616 else {
Jeff Dikef28169d2007-02-10 01:43:53 -0800617 for(i = 0; i < num; i++){
618 err = setup_one_line(lines, i, init, INIT_ALL,
619 error_out);
620 if(err)
621 return err;
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800624 return n == -1 ? num : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Jeff Dike1f801712006-01-06 00:18:55 -0800627int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800628 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Jeff Dike1f801712006-01-06 00:18:55 -0800630 struct line *line;
Jeff Dike970d6e32006-01-06 00:18:48 -0800631 char *new;
Jeff Dike418e55d2006-01-06 00:18:54 -0800632 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Jeff Diked571cd12006-01-06 00:18:53 -0800634 if(*str == '='){
Jeff Dikef28169d2007-02-10 01:43:53 -0800635 *error_out = "Can't configure all devices from mconsole";
636 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800637 }
638
Jeff Dike970d6e32006-01-06 00:18:48 -0800639 new = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if(new == NULL){
Jeff Dikef28169d2007-02-10 01:43:53 -0800641 *error_out = "Failed to allocate memory";
642 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800644 n = line_setup(lines, num, new, error_out);
Jeff Dike1f801712006-01-06 00:18:55 -0800645 if(n < 0)
Jeff Dikef28169d2007-02-10 01:43:53 -0800646 return n;
Jeff Dike1f801712006-01-06 00:18:55 -0800647
648 line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800649 return parse_chan_pair(line->init_str, line, n, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700652int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int size, char **error_out)
654{
655 struct line *line;
656 char *end;
657 int dev, n = 0;
658
659 dev = simple_strtoul(name, &end, 0);
660 if((*end != '\0') || (end == name)){
661 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 if((dev < 0) || (dev >= num)){
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700666 *error_out = "device number out of range";
667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
670 line = &lines[dev];
671
Jeff Diked79a5802007-02-10 01:43:52 -0800672 spin_lock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if(!line->valid)
674 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike165dc592006-01-06 00:18:57 -0800675 else if(line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 CONFIG_CHUNK(str, size, n, line->init_str, 1);
677 else n = chan_config_string(&line->chan_list, str, size, error_out);
Jeff Diked79a5802007-02-10 01:43:52 -0800678 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700680 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Jeff Dike29d56cf2005-06-25 14:55:25 -0700683int line_id(char **str, int *start_out, int *end_out)
684{
685 char *end;
686 int n;
687
688 n = simple_strtoul(*str, &end, 0);
689 if((*end != '\0') || (end == *str))
690 return -1;
691
692 *str = end;
693 *start_out = n;
694 *end_out = n;
695 return n;
696}
697
Jeff Dikef28169d2007-02-10 01:43:53 -0800698int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Jeff Dike418e55d2006-01-06 00:18:54 -0800700 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 char config[sizeof("conxxxx=none\0")];
702
Jeff Dike29d56cf2005-06-25 14:55:25 -0700703 sprintf(config, "%d=none", n);
Jeff Dikef28169d2007-02-10 01:43:53 -0800704 err = line_setup(lines, num, config, error_out);
Jeff Dike418e55d2006-01-06 00:18:54 -0800705 if(err >= 0)
706 err = 0;
707 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710struct tty_driver *line_register_devfs(struct lines *set,
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700711 struct line_driver *line_driver,
712 const struct tty_operations *ops,
713 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 int i;
716 struct tty_driver *driver = alloc_tty_driver(nlines);
717
718 if (!driver)
719 return NULL;
720
721 driver->driver_name = line_driver->name;
722 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 driver->major = line_driver->major;
724 driver->minor_start = line_driver->minor_start;
725 driver->type = line_driver->type;
726 driver->subtype = line_driver->subtype;
727 driver->flags = TTY_DRIVER_REAL_RAW;
728 driver->init_termios = tty_std_termios;
729 tty_set_operations(driver, ops);
730
731 if (tty_register_driver(driver)) {
732 printk("%s: can't register %s driver\n",
733 __FUNCTION__,line_driver->name);
734 put_tty_driver(driver);
735 return NULL;
736 }
737
738 for(i = 0; i < nlines; i++){
Jeff Diked50084a2006-01-06 00:18:50 -0800739 if(!lines[i].valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 tty_unregister_device(driver, i);
741 }
742
743 mconsole_register_dev(&line_driver->mc);
744 return driver;
745}
746
Jeff Dike90107722006-01-06 00:18:54 -0800747static DEFINE_SPINLOCK(winch_handler_lock);
748static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700749
Jeff Dike1f801712006-01-06 00:18:55 -0800750void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct line *line;
Jeff Dikef28169d2007-02-10 01:43:53 -0800753 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int i;
755
756 for(i = 0; i < nlines; i++){
757 line = &lines[i];
758 INIT_LIST_HEAD(&line->chan_list);
Jeff Diked79a5802007-02-10 01:43:52 -0800759 mutex_init(&line->open_mutex);
Jeff Dike90107722006-01-06 00:18:54 -0800760
Jeff Diked50084a2006-01-06 00:18:50 -0800761 if(line->init_str == NULL)
762 continue;
763
764 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
765 if(line->init_str == NULL)
766 printk("lines_init - kstrdup returned NULL\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800767
Jeff Dikef28169d2007-02-10 01:43:53 -0800768 if(parse_chan_pair(line->init_str, line, i, opts, &error)){
769 printk("parse_chan_pair failed for device %d : %s\n",
770 i, error);
Jeff Dike1f801712006-01-06 00:18:55 -0800771 line->valid = 0;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774}
775
776struct winch {
777 struct list_head list;
778 int fd;
779 int tty_fd;
780 int pid;
781 struct tty_struct *tty;
782};
783
Al Viro7bea96f2006-10-08 22:49:34 +0100784static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct winch *winch = data;
787 struct tty_struct *tty;
788 struct line *line;
789 int err;
790 char c;
791
792 if(winch->fd != -1){
793 err = generic_read(winch->fd, &c, NULL);
794 if(err < 0){
795 if(err != -EAGAIN){
796 printk("winch_interrupt : read failed, "
797 "errno = %d\n", -err);
798 printk("fd %d is losing SIGWINCH support\n",
799 winch->tty_fd);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700800 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 goto out;
803 }
804 }
805 tty = winch->tty;
806 if (tty != NULL) {
807 line = tty->driver_data;
Jeff Diked50084a2006-01-06 00:18:50 -0800808 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 &tty->winsize.ws_col);
810 kill_pg(tty->pgrp, SIGWINCH, 1);
811 }
812 out:
813 if(winch->fd != -1)
814 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700815 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
819{
820 struct winch *winch;
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
823 if (winch == NULL) {
824 printk("register_winch_irq - kmalloc failed\n");
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700825 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
829 .fd = fd,
830 .tty_fd = tty_fd,
831 .pid = pid,
832 .tty = tty });
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700833
834 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700836 spin_unlock(&winch_handler_lock);
837
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700838 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700839 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 "winch", winch) < 0)
841 printk("register_winch_irq - failed to register IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Jeff Dikee464bf22006-01-06 00:19:01 -0800844static void free_winch(struct winch *winch)
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700845{
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700846 list_del(&winch->list);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700847
Jeff Dikee464bf22006-01-06 00:19:01 -0800848 if(winch->pid != -1)
849 os_kill_process(winch->pid, 1);
850 if(winch->fd != -1)
851 os_close_file(winch->fd);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700852
Jeff Dikee464bf22006-01-06 00:19:01 -0800853 free_irq(WINCH_IRQ, winch);
854 kfree(winch);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700855}
856
Jeff Dikee464bf22006-01-06 00:19:01 -0800857static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 struct list_head *ele;
860 struct winch *winch;
861
Jeff Dikee464bf22006-01-06 00:19:01 -0800862 spin_lock(&winch_handler_lock);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 list_for_each(ele, &winch_handlers){
865 winch = list_entry(ele, struct winch, list);
Jeff Dikee464bf22006-01-06 00:19:01 -0800866 if(winch->tty == tty){
867 free_winch(winch);
868 break;
869 }
870 }
871 spin_unlock(&winch_handler_lock);
872}
873
874static void winch_cleanup(void)
875{
876 struct list_head *ele, *next;
877 struct winch *winch;
878
879 spin_lock(&winch_handler_lock);
880
881 list_for_each_safe(ele, next, &winch_handlers){
882 winch = list_entry(ele, struct winch, list);
883 free_winch(winch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800885
886 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888__uml_exitcall(winch_cleanup);
889
890char *add_xterm_umid(char *base)
891{
892 char *umid, *title;
893 int len;
894
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800895 umid = get_umid();
896 if(*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700897 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
900 title = kmalloc(len, GFP_KERNEL);
901 if(title == NULL){
902 printk("Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700903 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700907 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}