blob: b4538dfb48205cb85c31d52bbe3e9783f221761b [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
Jeff Dikee16f5352007-06-08 13:46:54 -07006#include "linux/kernel.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "linux/sched.h"
8#include "linux/slab.h"
9#include "linux/list.h"
10#include "linux/kd.h"
11#include "linux/interrupt.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "asm/uaccess.h"
13#include "chan_kern.h"
14#include "irq_user.h"
15#include "line.h"
16#include "kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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;
Jeff Dikec59dbca2007-10-16 01:26:42 -0700219 int n, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
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);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700225 if (line->head != line->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ret = buffer_data(line, buf, len);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700227 else {
Jeff Diked50084a2006-01-06 00:18:50 -0800228 n = write_chan(&line->chan_list, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700230 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ret = n;
232 goto out_up;
233 }
234
235 len -= n;
236 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700237 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ret += buffer_data(line, buf + n, len);
239 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700240out_up:
241 spin_unlock_irqrestore(&line->lock, flags);
242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Alan Cox606d0992006-12-08 02:38:45 -0800245void line_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 /* nothing */
248}
249
Jeff Dike5e7672e2006-09-27 01:50:33 -0700250static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int cmd;
252 char *level;
253 char *name;
254} tty_ioctls[] = {
255 /* don't print these, they flood the log ... */
256 { TCGETS, NULL, "TCGETS" },
257 { TCSETS, NULL, "TCSETS" },
258 { TCSETSW, NULL, "TCSETSW" },
259 { TCFLSH, NULL, "TCFLSH" },
260 { TCSBRK, NULL, "TCSBRK" },
261
262 /* general tty stuff */
263 { TCSETSF, KERN_DEBUG, "TCSETSF" },
264 { TCGETA, KERN_DEBUG, "TCGETA" },
265 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
266 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
267 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
268
269 /* linux-specific ones */
270 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
271 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
272 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
273 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
274};
275
276int line_ioctl(struct tty_struct *tty, struct file * file,
277 unsigned int cmd, unsigned long arg)
278{
279 int ret;
280 int i;
281
282 ret = 0;
283 switch(cmd) {
284#ifdef TIOCGETP
285 case TIOCGETP:
286 case TIOCSETP:
287 case TIOCSETN:
288#endif
289#ifdef TIOCGETC
290 case TIOCGETC:
291 case TIOCSETC:
292#endif
293#ifdef TIOCGLTC
294 case TIOCGLTC:
295 case TIOCSLTC:
296#endif
297 case TCGETS:
298 case TCSETSF:
299 case TCSETSW:
300 case TCSETS:
301 case TCGETA:
302 case TCSETAF:
303 case TCSETAW:
304 case TCSETA:
305 case TCXONC:
306 case TCFLSH:
307 case TIOCOUTQ:
308 case TIOCINQ:
309 case TIOCGLCKTRMIOS:
310 case TIOCSLCKTRMIOS:
311 case TIOCPKT:
312 case TIOCGSOFTCAR:
313 case TIOCSSOFTCAR:
314 return -ENOIOCTLCMD;
315#if 0
316 case TCwhatever:
317 /* do something */
318 break;
319#endif
320 default:
321 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
322 if (cmd == tty_ioctls[i].cmd)
323 break;
324 if (i < ARRAY_SIZE(tty_ioctls)) {
325 if (NULL != tty_ioctls[i].level)
326 printk("%s%s: %s: ioctl %s called\n",
327 tty_ioctls[i].level, __FUNCTION__,
328 tty->name, tty_ioctls[i].name);
329 } else {
330 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
331 __FUNCTION__, tty->name, cmd);
332 }
333 ret = -ENOIOCTLCMD;
334 break;
335 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Jeff Dikee4dcee82006-01-06 00:18:58 -0800339void line_throttle(struct tty_struct *tty)
340{
341 struct line *line = tty->driver_data;
342
343 deactivate_chan(&line->chan_list, line->driver->read_irq);
344 line->throttled = 1;
345}
346
347void line_unthrottle(struct tty_struct *tty)
348{
349 struct line *line = tty->driver_data;
350
351 line->throttled = 0;
352 chan_interrupt(&line->chan_list, &line->task, tty,
353 line->driver->read_irq);
354
355 /* Maybe there is enough stuff pending that calling the interrupt
356 * throttles us again. In this case, line->throttled will be 1
357 * again and we shouldn't turn the interrupt back on.
358 */
359 if(!line->throttled)
360 reactivate_chan(&line->chan_list, line->driver->read_irq);
361}
362
Al Viro7bea96f2006-10-08 22:49:34 +0100363static irqreturn_t line_write_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Jeff Dike165dc592006-01-06 00:18:57 -0800365 struct chan *chan = data;
366 struct line *line = chan->line;
367 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int err;
369
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800370 /* Interrupts are disabled here because we registered the interrupt with
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700371 * IRQF_DISABLED (see line_setup_irq).*/
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700372
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800373 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700375 if (err == 0) {
376 return IRQ_NONE;
377 } else if(err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 line->head = line->buffer;
379 line->tail = line->buffer;
380 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800381 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if(tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700384 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700386 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 (tty->ldisc.write_wakeup != NULL))
388 (tty->ldisc.write_wakeup)(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* BLOCKING mode
391 * In blocking mode, everything sleeps on tty->write_wait.
392 * Sleeping in the console driver would break non-blocking
393 * writes.
394 */
395
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700396 if (waitqueue_active(&tty->write_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 wake_up_interruptible(&tty->write_wait);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700398 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Jeff Dike165dc592006-01-06 00:18:57 -0800401int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700403 const struct line_driver *driver = line->driver;
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700404 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700406 if (input)
407 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800408 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800409 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700410 if (err)
411 return err;
412 if (output)
413 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800414 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800415 driver->write_irq_name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Jeff Diked79a5802007-02-10 01:43:52 -0800420/* Normally, a driver like this can rely mostly on the tty layer
421 * locking, particularly when it comes to the driver structure.
422 * However, in this case, mconsole requests can come in "from the
423 * side", and race with opens and closes.
424 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800425 * mconsole config requests will want to be sure the device isn't in
426 * use, and get_config, open, and close will want a stable
427 * configuration. The checking and modification of the configuration
428 * is done under a spinlock. Checking whether the device is in use is
429 * line->tty->count > 1, also under the spinlock.
Jeff Diked79a5802007-02-10 01:43:52 -0800430 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800431 * tty->count serves to decide whether the device should be enabled or
432 * disabled on the host. If it's equal to 1, then we are doing the
433 * first open or last close. Otherwise, open and close just return.
Jeff Diked79a5802007-02-10 01:43:52 -0800434 */
435
Jeff Dike1f801712006-01-06 00:18:55 -0800436int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jeff Diked79a5802007-02-10 01:43:52 -0800438 struct line *line = &lines[tty->index];
Jeff Dike165dc592006-01-06 00:18:57 -0800439 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Jeff Diked79a5802007-02-10 01:43:52 -0800441 spin_lock(&line->count_lock);
442 if(!line->valid)
443 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jeff Diked79a5802007-02-10 01:43:52 -0800445 err = 0;
446 if(tty->count > 1)
447 goto out_unlock;
448
Jeff Diked79a5802007-02-10 01:43:52 -0800449 spin_unlock(&line->count_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800450
451 tty->driver_data = line;
452 line->tty = tty;
Jeff Dike165dc592006-01-06 00:18:57 -0800453
Jeff Diked14ad812007-07-15 23:38:54 -0700454 err = enable_chan(line);
455 if (err)
456 return err;
457
Jeff Diked79a5802007-02-10 01:43:52 -0800458 INIT_DELAYED_WORK(&line->task, line_timer_cb);
Jeff Dike165dc592006-01-06 00:18:57 -0800459
Jeff Diked79a5802007-02-10 01:43:52 -0800460 if(!line->sigio){
461 chan_enable_winch(&line->chan_list, tty);
462 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
Jeff Diked79a5802007-02-10 01:43:52 -0800465 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
466 &tty->winsize.ws_col);
467
Jeff Diked79a5802007-02-10 01:43:52 -0800468 return err;
469
470out_unlock:
471 spin_unlock(&line->count_lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700475static void unregister_winch(struct tty_struct *tty);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477void line_close(struct tty_struct *tty, struct file * filp)
478{
479 struct line *line = tty->driver_data;
480
Jeff Diked79a5802007-02-10 01:43:52 -0800481 /* If line_open fails (and tty->driver_data is never set),
482 * tty_open will call line_close. So just return in this case.
483 */
484 if(line == NULL)
485 return;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700486
487 /* We ignore the error anyway! */
488 flush_buffer(line);
489
Jeff Diked79a5802007-02-10 01:43:52 -0800490 spin_lock(&line->count_lock);
491 if(!line->valid)
492 goto out_unlock;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700493
Jeff Diked79a5802007-02-10 01:43:52 -0800494 if(tty->count > 1)
495 goto out_unlock;
496
Jeff Diked79a5802007-02-10 01:43:52 -0800497 spin_unlock(&line->count_lock);
498
499 line->tty = NULL;
500 tty->driver_data = NULL;
501
502 if(line->sigio){
503 unregister_winch(tty);
504 line->sigio = 0;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700505 }
506
Jeff Diked79a5802007-02-10 01:43:52 -0800507 return;
508
509out_unlock:
510 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513void close_lines(struct line *lines, int nlines)
514{
515 int i;
516
517 for(i = 0; i < nlines; i++)
Jeff Dike165dc592006-01-06 00:18:57 -0800518 close_chan(&lines[i].chan_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Jeff Dikef28169d2007-02-10 01:43:53 -0800521static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
522 char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800523{
524 struct line *line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800525 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800526
527 spin_lock(&line->count_lock);
528
529 if(line->tty != NULL){
Jeff Dikef28169d2007-02-10 01:43:53 -0800530 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800531 goto out;
532 }
533
534 if (line->init_pri <= init_prio){
535 line->init_pri = init_prio;
536 if (!strcmp(init, "none"))
537 line->valid = 0;
538 else {
539 line->init_str = init;
540 line->valid = 1;
541 }
542 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800543 err = 0;
Jeff Diked79a5802007-02-10 01:43:52 -0800544out:
545 spin_unlock(&line->count_lock);
Jeff Dikef28169d2007-02-10 01:43:53 -0800546 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800547}
548
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700549/* Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200550 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700551 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800552 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800553 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700554
Jeff Dikef28169d2007-02-10 01:43:53 -0800555int line_setup(struct line *lines, unsigned int num, char *init,
556 char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Jeff Dikef28169d2007-02-10 01:43:53 -0800558 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 char *end;
560
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700561 if(*init == '=') {
562 /* We said con=/ssl= instead of con#=, so we are configuring all
563 * consoles at once.*/
564 n = -1;
Jeff Diked50084a2006-01-06 00:18:50 -0800565 }
566 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 n = simple_strtoul(init, &end, 0);
568 if(*end != '='){
Jeff Dikef28169d2007-02-10 01:43:53 -0800569 *error_out = "Couldn't parse device number";
570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 init = end;
573 }
574 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700575
576 if (n >= (signed int) num) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800577 *error_out = "Device number out of range";
578 return -EINVAL;
Jeff Diked50084a2006-01-06 00:18:50 -0800579 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800580 else if (n >= 0){
581 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
582 if(err)
583 return err;
584 }
Jeff Diked50084a2006-01-06 00:18:50 -0800585 else {
Jeff Dikef28169d2007-02-10 01:43:53 -0800586 for(i = 0; i < num; i++){
587 err = setup_one_line(lines, i, init, INIT_ALL,
588 error_out);
589 if(err)
590 return err;
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800593 return n == -1 ? num : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Jeff Dike1f801712006-01-06 00:18:55 -0800596int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800597 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Jeff Dike1f801712006-01-06 00:18:55 -0800599 struct line *line;
Jeff Dike970d6e32006-01-06 00:18:48 -0800600 char *new;
Jeff Dike418e55d2006-01-06 00:18:54 -0800601 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Jeff Diked571cd12006-01-06 00:18:53 -0800603 if(*str == '='){
Jeff Dikef28169d2007-02-10 01:43:53 -0800604 *error_out = "Can't configure all devices from mconsole";
605 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800606 }
607
Jeff Dike970d6e32006-01-06 00:18:48 -0800608 new = kstrdup(str, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if(new == NULL){
Jeff Dikef28169d2007-02-10 01:43:53 -0800610 *error_out = "Failed to allocate memory";
611 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800613 n = line_setup(lines, num, new, error_out);
Jeff Dike1f801712006-01-06 00:18:55 -0800614 if(n < 0)
Jeff Dikef28169d2007-02-10 01:43:53 -0800615 return n;
Jeff Dike1f801712006-01-06 00:18:55 -0800616
617 line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800618 return parse_chan_pair(line->init_str, line, n, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700621int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int size, char **error_out)
623{
624 struct line *line;
625 char *end;
626 int dev, n = 0;
627
628 dev = simple_strtoul(name, &end, 0);
629 if((*end != '\0') || (end == name)){
630 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 if((dev < 0) || (dev >= num)){
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700635 *error_out = "device number out of range";
636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
639 line = &lines[dev];
640
Jeff Diked79a5802007-02-10 01:43:52 -0800641 spin_lock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if(!line->valid)
643 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike165dc592006-01-06 00:18:57 -0800644 else if(line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 CONFIG_CHUNK(str, size, n, line->init_str, 1);
646 else n = chan_config_string(&line->chan_list, str, size, error_out);
Jeff Diked79a5802007-02-10 01:43:52 -0800647 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700649 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Jeff Dike29d56cf2005-06-25 14:55:25 -0700652int line_id(char **str, int *start_out, int *end_out)
653{
654 char *end;
655 int n;
656
657 n = simple_strtoul(*str, &end, 0);
658 if((*end != '\0') || (end == *str))
659 return -1;
660
661 *str = end;
662 *start_out = n;
663 *end_out = n;
664 return n;
665}
666
Jeff Dikef28169d2007-02-10 01:43:53 -0800667int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Jeff Dike418e55d2006-01-06 00:18:54 -0800669 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 char config[sizeof("conxxxx=none\0")];
671
Jeff Dike29d56cf2005-06-25 14:55:25 -0700672 sprintf(config, "%d=none", n);
Jeff Dikef28169d2007-02-10 01:43:53 -0800673 err = line_setup(lines, num, config, error_out);
Jeff Dike418e55d2006-01-06 00:18:54 -0800674 if(err >= 0)
675 err = 0;
676 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Jeff Diked5c9ffc2007-02-10 01:44:08 -0800679struct tty_driver *register_lines(struct line_driver *line_driver,
680 const struct tty_operations *ops,
681 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 int i;
684 struct tty_driver *driver = alloc_tty_driver(nlines);
685
686 if (!driver)
687 return NULL;
688
689 driver->driver_name = line_driver->name;
690 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 driver->major = line_driver->major;
692 driver->minor_start = line_driver->minor_start;
693 driver->type = line_driver->type;
694 driver->subtype = line_driver->subtype;
695 driver->flags = TTY_DRIVER_REAL_RAW;
696 driver->init_termios = tty_std_termios;
697 tty_set_operations(driver, ops);
698
699 if (tty_register_driver(driver)) {
700 printk("%s: can't register %s driver\n",
701 __FUNCTION__,line_driver->name);
702 put_tty_driver(driver);
703 return NULL;
704 }
705
706 for(i = 0; i < nlines; i++){
Jeff Diked50084a2006-01-06 00:18:50 -0800707 if(!lines[i].valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 tty_unregister_device(driver, i);
709 }
710
711 mconsole_register_dev(&line_driver->mc);
712 return driver;
713}
714
Jeff Dike90107722006-01-06 00:18:54 -0800715static DEFINE_SPINLOCK(winch_handler_lock);
716static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700717
Jeff Dike1f801712006-01-06 00:18:55 -0800718void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct line *line;
Jeff Dikef28169d2007-02-10 01:43:53 -0800721 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int i;
723
724 for(i = 0; i < nlines; i++){
725 line = &lines[i];
726 INIT_LIST_HEAD(&line->chan_list);
Jeff Dike90107722006-01-06 00:18:54 -0800727
Jeff Diked50084a2006-01-06 00:18:50 -0800728 if(line->init_str == NULL)
729 continue;
730
731 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
732 if(line->init_str == NULL)
733 printk("lines_init - kstrdup returned NULL\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800734
Jeff Dikef28169d2007-02-10 01:43:53 -0800735 if(parse_chan_pair(line->init_str, line, i, opts, &error)){
736 printk("parse_chan_pair failed for device %d : %s\n",
737 i, error);
Jeff Dike1f801712006-01-06 00:18:55 -0800738 line->valid = 0;
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741}
742
743struct winch {
744 struct list_head list;
745 int fd;
746 int tty_fd;
747 int pid;
748 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700749 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750};
751
Jeff Dike42a359e2007-07-15 23:38:55 -0700752static void free_winch(struct winch *winch, int free_irq_ok)
753{
754 list_del(&winch->list);
755
756 if (winch->pid != -1)
757 os_kill_process(winch->pid, 1);
758 if (winch->fd != -1)
759 os_close_file(winch->fd);
760 if (winch->stack != 0)
761 free_stack(winch->stack, 0);
762 if (free_irq_ok)
763 free_irq(WINCH_IRQ, winch);
764 kfree(winch);
765}
766
Al Viro7bea96f2006-10-08 22:49:34 +0100767static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct winch *winch = data;
770 struct tty_struct *tty;
771 struct line *line;
772 int err;
773 char c;
774
775 if(winch->fd != -1){
776 err = generic_read(winch->fd, &c, NULL);
777 if(err < 0){
778 if(err != -EAGAIN){
779 printk("winch_interrupt : read failed, "
780 "errno = %d\n", -err);
781 printk("fd %d is losing SIGWINCH support\n",
782 winch->tty_fd);
Jeff Dike42a359e2007-07-15 23:38:55 -0700783 free_winch(winch, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700784 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 goto out;
787 }
788 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700789 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (tty != NULL) {
791 line = tty->driver_data;
Jeff Diked50084a2006-01-06 00:18:50 -0800792 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 &tty->winsize.ws_col);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800794 kill_pgrp(tty->pgrp, SIGWINCH, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 out:
797 if(winch->fd != -1)
798 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700799 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Jeff Dike42a359e2007-07-15 23:38:55 -0700802void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
803 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct winch *winch;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
808 if (winch == NULL) {
809 printk("register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700810 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
814 .fd = fd,
815 .tty_fd = tty_fd,
816 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700817 .tty = tty,
818 .stack = stack });
819
820 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
821 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
822 "winch", winch) < 0) {
823 printk("register_winch_irq - failed to register IRQ\n");
824 goto out_free;
825 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700826
827 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700829 spin_unlock(&winch_handler_lock);
830
Jeff Dike42a359e2007-07-15 23:38:55 -0700831 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Jeff Dike42a359e2007-07-15 23:38:55 -0700833 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800834 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700835 cleanup:
836 os_kill_process(pid, 1);
837 os_close_file(fd);
838 if (stack != 0)
839 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700840}
841
Jeff Dikee464bf22006-01-06 00:19:01 -0800842static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct list_head *ele;
845 struct winch *winch;
846
Jeff Dikee464bf22006-01-06 00:19:01 -0800847 spin_lock(&winch_handler_lock);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 list_for_each(ele, &winch_handlers){
850 winch = list_entry(ele, struct winch, list);
Jeff Dikee464bf22006-01-06 00:19:01 -0800851 if(winch->tty == tty){
Jeff Dike42a359e2007-07-15 23:38:55 -0700852 free_winch(winch, 1);
Jeff Dikee464bf22006-01-06 00:19:01 -0800853 break;
854 }
855 }
856 spin_unlock(&winch_handler_lock);
857}
858
859static void winch_cleanup(void)
860{
861 struct list_head *ele, *next;
862 struct winch *winch;
863
864 spin_lock(&winch_handler_lock);
865
866 list_for_each_safe(ele, next, &winch_handlers){
867 winch = list_entry(ele, struct winch, list);
Jeff Dike42a359e2007-07-15 23:38:55 -0700868 free_winch(winch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800870
871 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873__uml_exitcall(winch_cleanup);
874
875char *add_xterm_umid(char *base)
876{
877 char *umid, *title;
878 int len;
879
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800880 umid = get_umid();
881 if(*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700882 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
885 title = kmalloc(len, GFP_KERNEL);
886 if(title == NULL){
887 printk("Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700888 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700892 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}