blob: 95d5e7803bc735cfb1b74ef8b56111af39f24168 [file] [log] [blame]
Jeff Dike165dc592006-01-06 00:18:57 -08001/*
Jeff Dike2f8a2dc2007-10-16 01:26:43 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dike2f8a2dc2007-10-16 01:26:43 -07006#include "linux/irqreturn.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "linux/kd.h"
Alexey Dobriyand43c36d2009-10-07 17:09:06 +04008#include "linux/sched.h"
Jan Kiszka7f3c1fa2010-04-19 17:46:23 +09009#include "linux/slab.h"
Al Viro510c72a32011-08-18 20:08:29 +010010#include "chan.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "irq_kern.h"
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070012#include "irq_user.h"
Jeff Dikeedea1382008-02-04 22:30:46 -080013#include "kern_util.h"
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070014#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define LINE_BUFSIZE 4096
17
Al Viro7bea96f2006-10-08 22:49:34 +010018static irqreturn_t line_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
Jeff Dike165dc592006-01-06 00:18:57 -080020 struct chan *chan = data;
21 struct line *line = chan->line;
Jiri Slaby6fc58842012-06-04 13:35:27 +020022 struct tty_struct *tty = tty_port_tty_get(&line->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24 if (line)
Jiri Slaby6fc58842012-06-04 13:35:27 +020025 chan_interrupt(line, tty, irq);
26 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 return IRQ_HANDLED;
28}
29
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070030/*
31 * Returns the free space inside the ring buffer of this line.
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070032 *
Simon Arlottb60745b2007-10-20 01:23:03 +020033 * Should be called while holding line->lock (this does not modify data).
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070034 */
35static int write_room(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 int n;
38
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070039 if (line->buffer == NULL)
40 return LINE_BUFSIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070042 /* This is for the case where the buffer is wrapped! */
43 n = line->head - line->tail;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (n <= 0)
Jeff Dikeacb2cf32008-02-04 22:30:42 -080046 n += LINE_BUFSIZE; /* The other case */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070047 return n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070050int line_write_room(struct tty_struct *tty)
51{
52 struct line *line = tty->driver_data;
53 unsigned long flags;
54 int room;
55
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070056 spin_lock_irqsave(&line->lock, flags);
57 room = write_room(line);
58 spin_unlock_irqrestore(&line->lock, flags);
59
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070060 return room;
61}
62
63int line_chars_in_buffer(struct tty_struct *tty)
64{
65 struct line *line = tty->driver_data;
66 unsigned long flags;
67 int ret;
68
69 spin_lock_irqsave(&line->lock, flags);
Jeff Dikeacb2cf32008-02-04 22:30:42 -080070 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070071 ret = LINE_BUFSIZE - (write_room(line) + 1);
72 spin_unlock_irqrestore(&line->lock, flags);
73
74 return ret;
75}
76
77/*
78 * This copies the content of buf into the circular buffer associated with
79 * this line.
80 * The return value is the number of characters actually copied, i.e. the ones
81 * for which there was space: this function is not supposed to ever flush out
82 * the circular buffer.
83 *
84 * Must be called while holding line->lock!
85 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int buffer_data(struct line *line, const char *buf, int len)
87{
88 int end, room;
89
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070090 if (line->buffer == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
92 if (line->buffer == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070093 printk(KERN_ERR "buffer_data - atomic allocation "
94 "failed\n");
95 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97 line->head = line->buffer;
98 line->tail = line->buffer;
99 }
100
101 room = write_room(line);
102 len = (len > room) ? room : len;
103
104 end = line->buffer + LINE_BUFSIZE - line->tail;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700105
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700106 if (len < end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 memcpy(line->tail, buf, len);
108 line->tail += len;
Jeff Diked50084a2006-01-06 00:18:50 -0800109 }
110 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700111 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 memcpy(line->tail, buf, end);
113 buf += end;
114 memcpy(line->buffer, buf, len - end);
115 line->tail = line->buffer + len - end;
116 }
117
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700118 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700121/*
122 * Flushes the ring buffer to the output channels. That is, write_chan is
123 * called, passing it line->head as buffer, and an appropriate count.
124 *
125 * On exit, returns 1 when the buffer is empty,
126 * 0 when the buffer is not empty on exit,
127 * and -errno when an error occurred.
128 *
129 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static int flush_buffer(struct line *line)
131{
132 int n, count;
133
134 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700135 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700138 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700140
Al Virobed5e392011-09-08 10:49:34 -0400141 n = write_chan(line->chan_out, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 line->driver->write_irq);
143 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700144 return n;
145 if (n == count) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700146 /*
147 * We have flushed from ->head to buffer end, now we
148 * must flush only from the beginning to ->tail.
149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700151 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
156
157 count = line->tail - line->head;
Al Virobed5e392011-09-08 10:49:34 -0400158 n = write_chan(line->chan_out, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700160
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700161 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700162 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700165 return line->head == line->tail;
166}
167
168void line_flush_buffer(struct tty_struct *tty)
169{
170 struct line *line = tty->driver_data;
171 unsigned long flags;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700172
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700173 spin_lock_irqsave(&line->lock, flags);
Richard Weinbergerf1c93e42011-07-25 17:12:55 -0700174 flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700175 spin_unlock_irqrestore(&line->lock, flags);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700176}
177
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700178/*
179 * We map both ->flush_chars and ->put_char (which go in pair) onto
180 * ->flush_buffer and ->write. Hope it's not that bad.
181 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700182void line_flush_chars(struct tty_struct *tty)
183{
184 line_flush_buffer(tty);
185}
186
WANG Cong3168cb92008-05-06 20:42:33 -0700187int line_put_char(struct tty_struct *tty, unsigned char ch)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700188{
WANG Cong3168cb92008-05-06 20:42:33 -0700189 return line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
193{
194 struct line *line = tty->driver_data;
195 unsigned long flags;
Jeff Dikec59dbca2007-10-16 01:26:42 -0700196 int n, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700198 spin_lock_irqsave(&line->lock, flags);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700199 if (line->head != line->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 ret = buffer_data(line, buf, len);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700201 else {
Al Virobed5e392011-09-08 10:49:34 -0400202 n = write_chan(line->chan_out, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700204 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ret = n;
206 goto out_up;
207 }
208
209 len -= n;
210 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700211 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ret += buffer_data(line, buf + n, len);
213 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700214out_up:
215 spin_unlock_irqrestore(&line->lock, flags);
216 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Alan Cox606d0992006-12-08 02:38:45 -0800219void line_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 /* nothing */
222}
223
Jeff Dikee4dcee82006-01-06 00:18:58 -0800224void line_throttle(struct tty_struct *tty)
225{
226 struct line *line = tty->driver_data;
227
Al Virobed5e392011-09-08 10:49:34 -0400228 deactivate_chan(line->chan_in, line->driver->read_irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800229 line->throttled = 1;
230}
231
232void line_unthrottle(struct tty_struct *tty)
233{
234 struct line *line = tty->driver_data;
235
236 line->throttled = 0;
Al Viro0fcd7192011-09-10 08:17:04 -0400237 chan_interrupt(line, tty, line->driver->read_irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800238
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700239 /*
240 * Maybe there is enough stuff pending that calling the interrupt
Jeff Dikee4dcee82006-01-06 00:18:58 -0800241 * throttles us again. In this case, line->throttled will be 1
242 * again and we shouldn't turn the interrupt back on.
243 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700244 if (!line->throttled)
Al Virobed5e392011-09-08 10:49:34 -0400245 reactivate_chan(line->chan_in, line->driver->read_irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800246}
247
Al Viro7bea96f2006-10-08 22:49:34 +0100248static irqreturn_t line_write_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Jeff Dike165dc592006-01-06 00:18:57 -0800250 struct chan *chan = data;
251 struct line *line = chan->line;
Jiri Slaby6fc58842012-06-04 13:35:27 +0200252 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int err;
254
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700255 /*
Yong Zhangc0b79a92011-09-22 16:58:46 +0800256 * Interrupts are disabled here because genirq keep irqs disabled when
257 * calling the action handler.
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700258 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700259
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800260 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700262 if (err == 0) {
Al Viro199eebb2012-02-11 03:05:32 -0500263 spin_unlock(&line->lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700264 return IRQ_NONE;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700265 } else if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 line->head = line->buffer;
267 line->tail = line->buffer;
268 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800269 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jiri Slaby6fc58842012-06-04 13:35:27 +0200271 tty = tty_port_tty_get(&line->port);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700272 if (tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700273 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
WANG Cong06ac6672008-07-29 22:33:34 -0700275 tty_wakeup(tty);
Jiri Slaby6fc58842012-06-04 13:35:27 +0200276 tty_kref_put(tty);
277
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700278 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Jeff Dike165dc592006-01-06 00:18:57 -0800281int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700283 const struct line_driver *driver = line->driver;
Yong Zhangc0b79a92011-09-22 16:58:46 +0800284 int err = 0, flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700286 if (input)
287 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800288 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800289 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700290 if (err)
291 return err;
292 if (output)
293 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800294 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800295 driver->write_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700296 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Richard Weinberger79e02732012-06-04 21:57:24 +0200299static int line_activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Richard Weinberger79e02732012-06-04 21:57:24 +0200301 int ret;
302 struct line *line = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Richard Weinberger79e02732012-06-04 21:57:24 +0200304 ret = enable_chan(line);
305 if (ret)
306 return ret;
Jeff Diked14ad812007-07-15 23:38:54 -0700307
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700308 if (!line->sigio) {
Al Virobed5e392011-09-08 10:49:34 -0400309 chan_enable_winch(line->chan_out, tty);
Jeff Diked79a5802007-02-10 01:43:52 -0800310 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
Al Virobed5e392011-09-08 10:49:34 -0400313 chan_window_size(line, &tty->winsize.ws_row,
Richard Weinberger79e02732012-06-04 21:57:24 +0200314 &tty->winsize.ws_col);
315
316 return 0;
317}
318
319static const struct tty_port_operations line_port_ops = {
320 .activate = line_activate,
321};
322
323int line_open(struct tty_struct *tty, struct file *filp)
324{
325 struct line *line = tty->driver_data;
326
327 return tty_port_open(&line->port, tty, filp);
328}
329
330int line_install(struct tty_driver *driver, struct tty_struct *tty,
331 struct line *line)
332{
333 int ret;
334
335 ret = tty_standard_install(driver, tty);
336 if (ret)
337 return ret;
338
339 tty->driver_data = line;
340
341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700344static void unregister_winch(struct tty_struct *tty);
345
Richard Weinberger79e02732012-06-04 21:57:24 +0200346void line_cleanup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct line *line = tty->driver_data;
349
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700350 if (line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800351 unregister_winch(tty);
352 line->sigio = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700353 }
Richard Weinberger79e02732012-06-04 21:57:24 +0200354}
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700355
Richard Weinberger79e02732012-06-04 21:57:24 +0200356void line_close(struct tty_struct *tty, struct file * filp)
357{
358 struct line *line = tty->driver_data;
359
360 tty_port_close(&line->port, tty, filp);
361}
362
363void line_hangup(struct tty_struct *tty)
364{
365 struct line *line = tty->driver_data;
366
367 tty_port_hangup(&line->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370void close_lines(struct line *lines, int nlines)
371{
372 int i;
373
374 for(i = 0; i < nlines; i++)
Al Viro10c890c2011-09-10 08:39:18 -0400375 close_chan(&lines[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Al Viro04292b22011-09-09 20:07:05 -0400378int setup_one_line(struct line *lines, int n, char *init,
379 const struct chan_opts *opts, char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800380{
381 struct line *line = &lines[n];
Al Virocfe6b7c2011-09-09 19:45:42 -0400382 struct tty_driver *driver = line->driver->driver;
Jeff Dikef28169d2007-02-10 01:43:53 -0800383 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800384
Al Virod8c215a2011-09-09 17:36:37 -0400385 mutex_lock(&line->count_lock);
Jeff Diked79a5802007-02-10 01:43:52 -0800386
Jiri Slaby060ed312012-06-04 13:35:26 +0200387 if (line->port.count) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800388 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800389 goto out;
390 }
391
Al Viro31efceb2011-09-09 19:14:02 -0400392 if (!strcmp(init, "none")) {
393 if (line->valid) {
394 line->valid = 0;
395 kfree(line->init_str);
Al Virocfe6b7c2011-09-09 19:45:42 -0400396 tty_unregister_device(driver, n);
Al Viro31efceb2011-09-09 19:14:02 -0400397 parse_chan_pair(NULL, line, n, opts, error_out);
398 err = 0;
399 }
400 } else {
401 char *new = kstrdup(init, GFP_KERNEL);
402 if (!new) {
403 *error_out = "Failed to allocate memory";
404 return -ENOMEM;
405 }
Al Viroc8e28762011-09-09 20:08:48 -0400406 if (line->valid) {
Al Virocfe6b7c2011-09-09 19:45:42 -0400407 tty_unregister_device(driver, n);
Al Viroc8e28762011-09-09 20:08:48 -0400408 kfree(line->init_str);
409 }
Al Viro31efceb2011-09-09 19:14:02 -0400410 line->init_str = new;
Al Viro43574c12011-09-09 17:25:00 -0400411 line->valid = 1;
Al Viro31efceb2011-09-09 19:14:02 -0400412 err = parse_chan_pair(new, line, n, opts, error_out);
Al Virocfe6b7c2011-09-09 19:45:42 -0400413 if (!err) {
414 struct device *d = tty_register_device(driver, n, NULL);
415 if (IS_ERR(d)) {
416 *error_out = "Failed to register device";
417 err = PTR_ERR(d);
418 parse_chan_pair(NULL, line, n, opts, error_out);
419 }
420 }
Al Viro31efceb2011-09-09 19:14:02 -0400421 if (err) {
422 line->init_str = NULL;
423 line->valid = 0;
424 kfree(new);
425 }
Jeff Diked79a5802007-02-10 01:43:52 -0800426 }
427out:
Al Virod8c215a2011-09-09 17:36:37 -0400428 mutex_unlock(&line->count_lock);
Jeff Dikef28169d2007-02-10 01:43:53 -0800429 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800430}
431
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700432/*
433 * Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200434 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700435 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800436 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800437 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700438
Al Viro43574c12011-09-09 17:25:00 -0400439int line_setup(char **conf, unsigned int num, char **def,
440 char *init, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Al Viro43574c12011-09-09 17:25:00 -0400442 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700444 if (*init == '=') {
445 /*
446 * We said con=/ssl= instead of con#=, so we are configuring all
447 * consoles at once.
448 */
Al Viro43574c12011-09-09 17:25:00 -0400449 *def = init + 1;
450 } else {
451 char *end;
452 unsigned n = simple_strtoul(init, &end, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700453
Al Viro43574c12011-09-09 17:25:00 -0400454 if (*end != '=') {
455 error = "Couldn't parse device number";
456 goto out;
Jeff Dikef28169d2007-02-10 01:43:53 -0800457 }
Al Viro43574c12011-09-09 17:25:00 -0400458 if (n >= num) {
459 error = "Device number out of range";
460 goto out;
461 }
462 conf[n] = end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Al Viro43574c12011-09-09 17:25:00 -0400464 return 0;
465
466out:
467 printk(KERN_ERR "Failed to set up %s with "
468 "configuration string \"%s\" : %s\n", name, init, error);
469 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Jeff Dike1f801712006-01-06 00:18:55 -0800472int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800473 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Al Virofe9a6b02011-09-08 20:44:06 -0400475 char *end;
Al Viro31efceb2011-09-09 19:14:02 -0400476 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700478 if (*str == '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800479 *error_out = "Can't configure all devices from mconsole";
480 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800481 }
482
Al Virofe9a6b02011-09-08 20:44:06 -0400483 n = simple_strtoul(str, &end, 0);
484 if (*end++ != '=') {
485 *error_out = "Couldn't parse device number";
486 return -EINVAL;
487 }
488 if (n >= num) {
489 *error_out = "Device number out of range";
490 return -EINVAL;
491 }
492
Al Viro31efceb2011-09-09 19:14:02 -0400493 return setup_one_line(lines, n, end, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700496int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int size, char **error_out)
498{
499 struct line *line;
500 char *end;
501 int dev, n = 0;
502
503 dev = simple_strtoul(name, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700504 if ((*end != '\0') || (end == name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700509 if ((dev < 0) || (dev >= num)) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700510 *error_out = "device number out of range";
511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
514 line = &lines[dev];
515
Al Virod8c215a2011-09-09 17:36:37 -0400516 mutex_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700517 if (!line->valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 CONFIG_CHUNK(str, size, n, "none", 1);
Jiri Slaby6fc58842012-06-04 13:35:27 +0200519 else {
520 struct tty_struct *tty = tty_port_tty_get(&line->port);
521 if (tty == NULL) {
522 CONFIG_CHUNK(str, size, n, line->init_str, 1);
523 } else {
524 n = chan_config_string(line, str, size, error_out);
525 tty_kref_put(tty);
526 }
527 }
Al Virod8c215a2011-09-09 17:36:37 -0400528 mutex_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700530 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Jeff Dike29d56cf2005-06-25 14:55:25 -0700533int line_id(char **str, int *start_out, int *end_out)
534{
535 char *end;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700536 int n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700537
538 n = simple_strtoul(*str, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700539 if ((*end != '\0') || (end == *str))
540 return -1;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700541
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700542 *str = end;
543 *start_out = n;
544 *end_out = n;
545 return n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700546}
547
Jeff Dikef28169d2007-02-10 01:43:53 -0800548int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Al Viroda645f32011-09-08 20:34:52 -0400550 if (n >= num) {
551 *error_out = "Device number out of range";
552 return -EINVAL;
553 }
Al Viro31efceb2011-09-09 19:14:02 -0400554 return setup_one_line(lines, n, "none", NULL, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Al Virocfe6b7c2011-09-09 19:45:42 -0400557int register_lines(struct line_driver *line_driver,
558 const struct tty_operations *ops,
559 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct tty_driver *driver = alloc_tty_driver(nlines);
Al Virocfe6b7c2011-09-09 19:45:42 -0400562 int err;
Al Viro04292b22011-09-09 20:07:05 -0400563 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (!driver)
Al Virocfe6b7c2011-09-09 19:45:42 -0400566 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 driver->driver_name = line_driver->name;
569 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 driver->major = line_driver->major;
571 driver->minor_start = line_driver->minor_start;
572 driver->type = line_driver->type;
573 driver->subtype = line_driver->subtype;
Al Virocfe6b7c2011-09-09 19:45:42 -0400574 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 driver->init_termios = tty_std_termios;
Al Viro04292b22011-09-09 20:07:05 -0400576
577 for (i = 0; i < nlines; i++) {
Jiri Slaby060ed312012-06-04 13:35:26 +0200578 tty_port_init(&lines[i].port);
Richard Weinberger79e02732012-06-04 21:57:24 +0200579 lines[i].port.ops = &line_port_ops;
Al Viro04292b22011-09-09 20:07:05 -0400580 spin_lock_init(&lines[i].lock);
581 mutex_init(&lines[i].count_lock);
582 lines[i].driver = line_driver;
583 INIT_LIST_HEAD(&lines[i].chan_list);
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 tty_set_operations(driver, ops);
586
Al Virocfe6b7c2011-09-09 19:45:42 -0400587 err = tty_register_driver(driver);
588 if (err) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700589 printk(KERN_ERR "register_lines : can't register %s driver\n",
590 line_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 put_tty_driver(driver);
Al Virocfe6b7c2011-09-09 19:45:42 -0400592 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Al Virocfe6b7c2011-09-09 19:45:42 -0400595 line_driver->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 mconsole_register_dev(&line_driver->mc);
Al Virocfe6b7c2011-09-09 19:45:42 -0400597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Jeff Dike90107722006-01-06 00:18:54 -0800600static DEFINE_SPINLOCK(winch_handler_lock);
601static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603struct winch {
604 struct list_head list;
605 int fd;
606 int tty_fd;
607 int pid;
608 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700609 unsigned long stack;
Al Viro7cf3cf22011-09-14 16:21:31 -0700610 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611};
612
Al Viro7cf3cf22011-09-14 16:21:31 -0700613static void __free_winch(struct work_struct *work)
Jeff Dike42a359e2007-07-15 23:38:55 -0700614{
Al Viro7cf3cf22011-09-14 16:21:31 -0700615 struct winch *winch = container_of(work, struct winch, work);
Richard Weinbergerfa7a0442012-04-17 22:37:13 +0200616 um_free_irq(WINCH_IRQ, winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700617
618 if (winch->pid != -1)
619 os_kill_process(winch->pid, 1);
Jeff Dike42a359e2007-07-15 23:38:55 -0700620 if (winch->stack != 0)
621 free_stack(winch->stack, 0);
Jeff Dike42a359e2007-07-15 23:38:55 -0700622 kfree(winch);
623}
624
Al Viro7cf3cf22011-09-14 16:21:31 -0700625static void free_winch(struct winch *winch)
626{
627 int fd = winch->fd;
628 winch->fd = -1;
629 if (fd != -1)
630 os_close_file(fd);
631 list_del(&winch->list);
632 __free_winch(&winch->work);
633}
634
Al Viro7bea96f2006-10-08 22:49:34 +0100635static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct winch *winch = data;
638 struct tty_struct *tty;
639 struct line *line;
Al Viro7cf3cf22011-09-14 16:21:31 -0700640 int fd = winch->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int err;
642 char c;
643
Al Viro7cf3cf22011-09-14 16:21:31 -0700644 if (fd != -1) {
645 err = generic_read(fd, &c, NULL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700646 if (err < 0) {
647 if (err != -EAGAIN) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700648 winch->fd = -1;
649 list_del(&winch->list);
650 os_close_file(fd);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700651 printk(KERN_ERR "winch_interrupt : "
652 "read failed, errno = %d\n", -err);
653 printk(KERN_ERR "fd %d is losing SIGWINCH "
654 "support\n", winch->tty_fd);
Al Viro7cf3cf22011-09-14 16:21:31 -0700655 INIT_WORK(&winch->work, __free_winch);
656 schedule_work(&winch->work);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700657 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 goto out;
660 }
661 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700662 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (tty != NULL) {
664 line = tty->driver_data;
Jeff Dike438ee672008-02-04 22:31:19 -0800665 if (line != NULL) {
Al Virobed5e392011-09-08 10:49:34 -0400666 chan_window_size(line, &tty->winsize.ws_row,
Jeff Dike438ee672008-02-04 22:31:19 -0800667 &tty->winsize.ws_col);
668 kill_pgrp(tty->pgrp, SIGWINCH, 1);
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 out:
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700672 if (winch->fd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700674 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Jeff Dike42a359e2007-07-15 23:38:55 -0700677void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
678 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct winch *winch;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
683 if (winch == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700684 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700685 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
689 .fd = fd,
690 .tty_fd = tty_fd,
691 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700692 .tty = tty,
693 .stack = stack });
694
695 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Yong Zhangc0b79a92011-09-22 16:58:46 +0800696 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Jeff Dike42a359e2007-07-15 23:38:55 -0700697 "winch", winch) < 0) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700698 printk(KERN_ERR "register_winch_irq - failed to register "
699 "IRQ\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700700 goto out_free;
701 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700702
703 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700705 spin_unlock(&winch_handler_lock);
706
Jeff Dike42a359e2007-07-15 23:38:55 -0700707 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Jeff Dike42a359e2007-07-15 23:38:55 -0700709 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800710 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700711 cleanup:
712 os_kill_process(pid, 1);
713 os_close_file(fd);
714 if (stack != 0)
715 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700716}
717
Jeff Dikee464bf22006-01-06 00:19:01 -0800718static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Will Newton48a0b742011-01-12 16:59:26 -0800720 struct list_head *ele, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct winch *winch;
722
Jeff Dikee464bf22006-01-06 00:19:01 -0800723 spin_lock(&winch_handler_lock);
724
Will Newton48a0b742011-01-12 16:59:26 -0800725 list_for_each_safe(ele, next, &winch_handlers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 winch = list_entry(ele, struct winch, list);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700727 if (winch->tty == tty) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700728 free_winch(winch);
Jeff Dikee464bf22006-01-06 00:19:01 -0800729 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700730 }
731 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800732 spin_unlock(&winch_handler_lock);
733}
734
735static void winch_cleanup(void)
736{
737 struct list_head *ele, *next;
738 struct winch *winch;
739
740 spin_lock(&winch_handler_lock);
741
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700742 list_for_each_safe(ele, next, &winch_handlers) {
Jeff Dikee464bf22006-01-06 00:19:01 -0800743 winch = list_entry(ele, struct winch, list);
Al Viro7cf3cf22011-09-14 16:21:31 -0700744 free_winch(winch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800746
747 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749__uml_exitcall(winch_cleanup);
750
751char *add_xterm_umid(char *base)
752{
753 char *umid, *title;
754 int len;
755
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800756 umid = get_umid();
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700757 if (*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700758 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
761 title = kmalloc(len, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700762 if (title == NULL) {
763 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700764 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
767 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700768 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}