blob: 232243aec70c1ded138780fd9d67241df0bb6dec [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
Al Viro37185b32012-10-08 03:27:32 +01006#include <linux/irqreturn.h>
7#include <linux/kd.h>
8#include <linux/sched.h>
9#include <linux/slab.h>
Al Viro510c72a32011-08-18 20:08:29 +010010#include "chan.h"
Al Viro37185b32012-10-08 03:27:32 +010011#include <irq_kern.h>
12#include <irq_user.h>
13#include <kern_util.h>
14#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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23 if (line)
Jiri Slaby2e124b42013-01-03 15:53:06 +010024 chan_interrupt(line, irq);
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 return IRQ_HANDLED;
27}
28
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070029/*
30 * Returns the free space inside the ring buffer of this line.
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070031 *
Simon Arlottb60745b2007-10-20 01:23:03 +020032 * Should be called while holding line->lock (this does not modify data).
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070033 */
34static int write_room(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 int n;
37
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070038 if (line->buffer == NULL)
39 return LINE_BUFSIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070041 /* This is for the case where the buffer is wrapped! */
42 n = line->head - line->tail;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (n <= 0)
Jeff Dikeacb2cf32008-02-04 22:30:42 -080045 n += LINE_BUFSIZE; /* The other case */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070046 return n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070049int line_write_room(struct tty_struct *tty)
50{
51 struct line *line = tty->driver_data;
52 unsigned long flags;
53 int room;
54
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070055 spin_lock_irqsave(&line->lock, flags);
56 room = write_room(line);
57 spin_unlock_irqrestore(&line->lock, flags);
58
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070059 return room;
60}
61
62int line_chars_in_buffer(struct tty_struct *tty)
63{
64 struct line *line = tty->driver_data;
65 unsigned long flags;
66 int ret;
67
68 spin_lock_irqsave(&line->lock, flags);
Jeff Dikeacb2cf32008-02-04 22:30:42 -080069 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070070 ret = LINE_BUFSIZE - (write_room(line) + 1);
71 spin_unlock_irqrestore(&line->lock, flags);
72
73 return ret;
74}
75
76/*
77 * This copies the content of buf into the circular buffer associated with
78 * this line.
79 * The return value is the number of characters actually copied, i.e. the ones
80 * for which there was space: this function is not supposed to ever flush out
81 * the circular buffer.
82 *
83 * Must be called while holding line->lock!
84 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int buffer_data(struct line *line, const char *buf, int len)
86{
87 int end, room;
88
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070089 if (line->buffer == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
91 if (line->buffer == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070092 printk(KERN_ERR "buffer_data - atomic allocation "
93 "failed\n");
94 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96 line->head = line->buffer;
97 line->tail = line->buffer;
98 }
99
100 room = write_room(line);
101 len = (len > room) ? room : len;
102
103 end = line->buffer + LINE_BUFSIZE - line->tail;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700104
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700105 if (len < end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 memcpy(line->tail, buf, len);
107 line->tail += len;
Jeff Diked50084a2006-01-06 00:18:50 -0800108 }
109 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700110 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 memcpy(line->tail, buf, end);
112 buf += end;
113 memcpy(line->buffer, buf, len - end);
114 line->tail = line->buffer + len - end;
115 }
116
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700117 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700120/*
121 * Flushes the ring buffer to the output channels. That is, write_chan is
122 * called, passing it line->head as buffer, and an appropriate count.
123 *
124 * On exit, returns 1 when the buffer is empty,
125 * 0 when the buffer is not empty on exit,
126 * and -errno when an error occurred.
127 *
128 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int flush_buffer(struct line *line)
130{
131 int n, count;
132
133 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700137 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700139
Al Virobed5e392011-09-08 10:49:34 -0400140 n = write_chan(line->chan_out, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 line->driver->write_irq);
142 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700143 return n;
144 if (n == count) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700145 /*
146 * We have flushed from ->head to buffer end, now we
147 * must flush only from the beginning to ->tail.
148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700150 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154 }
155
156 count = line->tail - line->head;
Al Virobed5e392011-09-08 10:49:34 -0400157 n = write_chan(line->chan_out, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700159
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700160 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700161 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700164 return line->head == line->tail;
165}
166
167void line_flush_buffer(struct tty_struct *tty)
168{
169 struct line *line = tty->driver_data;
170 unsigned long flags;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700171
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700172 spin_lock_irqsave(&line->lock, flags);
Richard Weinbergerf1c93e42011-07-25 17:12:55 -0700173 flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700174 spin_unlock_irqrestore(&line->lock, flags);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700175}
176
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700177/*
178 * We map both ->flush_chars and ->put_char (which go in pair) onto
179 * ->flush_buffer and ->write. Hope it's not that bad.
180 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700181void line_flush_chars(struct tty_struct *tty)
182{
183 line_flush_buffer(tty);
184}
185
WANG Cong3168cb92008-05-06 20:42:33 -0700186int line_put_char(struct tty_struct *tty, unsigned char ch)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700187{
WANG Cong3168cb92008-05-06 20:42:33 -0700188 return line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
192{
193 struct line *line = tty->driver_data;
194 unsigned long flags;
Jeff Dikec59dbca2007-10-16 01:26:42 -0700195 int n, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700197 spin_lock_irqsave(&line->lock, flags);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700198 if (line->head != line->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 ret = buffer_data(line, buf, len);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700200 else {
Al Virobed5e392011-09-08 10:49:34 -0400201 n = write_chan(line->chan_out, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700203 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 ret = n;
205 goto out_up;
206 }
207
208 len -= n;
209 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700210 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ret += buffer_data(line, buf + n, len);
212 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700213out_up:
214 spin_unlock_irqrestore(&line->lock, flags);
215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Alan Cox606d0992006-12-08 02:38:45 -0800218void line_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 /* nothing */
221}
222
Jeff Dikee4dcee82006-01-06 00:18:58 -0800223void line_throttle(struct tty_struct *tty)
224{
225 struct line *line = tty->driver_data;
226
Al Virobed5e392011-09-08 10:49:34 -0400227 deactivate_chan(line->chan_in, line->driver->read_irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800228 line->throttled = 1;
229}
230
231void line_unthrottle(struct tty_struct *tty)
232{
233 struct line *line = tty->driver_data;
234
235 line->throttled = 0;
Jiri Slaby2e124b42013-01-03 15:53:06 +0100236 chan_interrupt(line, line->driver->read_irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800237
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700238 /*
239 * Maybe there is enough stuff pending that calling the interrupt
Jeff Dikee4dcee82006-01-06 00:18:58 -0800240 * throttles us again. In this case, line->throttled will be 1
241 * again and we shouldn't turn the interrupt back on.
242 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700243 if (!line->throttled)
Al Virobed5e392011-09-08 10:49:34 -0400244 reactivate_chan(line->chan_in, line->driver->read_irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800245}
246
Al Viro7bea96f2006-10-08 22:49:34 +0100247static irqreturn_t line_write_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Jeff Dike165dc592006-01-06 00:18:57 -0800249 struct chan *chan = data;
250 struct line *line = chan->line;
Jiri Slaby6fc58842012-06-04 13:35:27 +0200251 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int err;
253
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700254 /*
Yong Zhangc0b79a92011-09-22 16:58:46 +0800255 * Interrupts are disabled here because genirq keep irqs disabled when
256 * calling the action handler.
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700257 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700258
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800259 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700261 if (err == 0) {
Al Viro199eebb2012-02-11 03:05:32 -0500262 spin_unlock(&line->lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700263 return IRQ_NONE;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700264 } else if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 line->head = line->buffer;
266 line->tail = line->buffer;
267 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800268 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jiri Slaby6fc58842012-06-04 13:35:27 +0200270 tty = tty_port_tty_get(&line->port);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700271 if (tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700272 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
WANG Cong06ac6672008-07-29 22:33:34 -0700274 tty_wakeup(tty);
Jiri Slaby6fc58842012-06-04 13:35:27 +0200275 tty_kref_put(tty);
276
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700277 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Jeff Dike165dc592006-01-06 00:18:57 -0800280int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700282 const struct line_driver *driver = line->driver;
Theodore Ts'oaab94462012-07-17 14:18:23 -0400283 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700285 if (input)
286 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Theodore Ts'oaab94462012-07-17 14:18:23 -0400287 line_interrupt, IRQF_SHARED,
288 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700289 if (err)
290 return err;
291 if (output)
292 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Theodore Ts'oaab94462012-07-17 14:18:23 -0400293 line_write_interrupt, IRQF_SHARED,
294 driver->write_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700295 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Richard Weinberger79e02732012-06-04 21:57:24 +0200298static int line_activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Richard Weinberger79e02732012-06-04 21:57:24 +0200300 int ret;
301 struct line *line = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Richard Weinberger79e02732012-06-04 21:57:24 +0200303 ret = enable_chan(line);
304 if (ret)
305 return ret;
Jeff Diked14ad812007-07-15 23:38:54 -0700306
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700307 if (!line->sigio) {
Al Virobed5e392011-09-08 10:49:34 -0400308 chan_enable_winch(line->chan_out, tty);
Jeff Diked79a5802007-02-10 01:43:52 -0800309 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Al Virobed5e392011-09-08 10:49:34 -0400312 chan_window_size(line, &tty->winsize.ws_row,
Richard Weinberger79e02732012-06-04 21:57:24 +0200313 &tty->winsize.ws_col);
314
315 return 0;
316}
317
Richard Weinbergercc4f0242013-03-11 10:03:42 +0100318static void unregister_winch(struct tty_struct *tty);
319
320static void line_destruct(struct tty_port *port)
321{
322 struct tty_struct *tty = tty_port_tty_get(port);
323 struct line *line = tty->driver_data;
324
325 if (line->sigio) {
326 unregister_winch(tty);
327 line->sigio = 0;
328 }
329}
330
Richard Weinberger79e02732012-06-04 21:57:24 +0200331static const struct tty_port_operations line_port_ops = {
332 .activate = line_activate,
Richard Weinbergercc4f0242013-03-11 10:03:42 +0100333 .destruct = line_destruct,
Richard Weinberger79e02732012-06-04 21:57:24 +0200334};
335
336int line_open(struct tty_struct *tty, struct file *filp)
337{
338 struct line *line = tty->driver_data;
339
340 return tty_port_open(&line->port, tty, filp);
341}
342
343int line_install(struct tty_driver *driver, struct tty_struct *tty,
344 struct line *line)
345{
346 int ret;
347
348 ret = tty_standard_install(driver, tty);
349 if (ret)
350 return ret;
351
352 tty->driver_data = line;
353
354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Richard Weinberger79e02732012-06-04 21:57:24 +0200357void line_close(struct tty_struct *tty, struct file * filp)
358{
359 struct line *line = tty->driver_data;
360
361 tty_port_close(&line->port, tty, filp);
362}
363
364void line_hangup(struct tty_struct *tty)
365{
366 struct line *line = tty->driver_data;
367
368 tty_port_hangup(&line->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371void close_lines(struct line *lines, int nlines)
372{
373 int i;
374
375 for(i = 0; i < nlines; i++)
Al Viro10c890c02011-09-10 08:39:18 -0400376 close_chan(&lines[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Al Viro04292b22011-09-09 20:07:05 -0400379int setup_one_line(struct line *lines, int n, char *init,
380 const struct chan_opts *opts, char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800381{
382 struct line *line = &lines[n];
Al Virocfe6b7c2011-09-09 19:45:42 -0400383 struct tty_driver *driver = line->driver->driver;
Jeff Dikef28169d2007-02-10 01:43:53 -0800384 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800385
Jiri Slaby060ed312012-06-04 13:35:26 +0200386 if (line->port.count) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800387 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800388 goto out;
389 }
390
Al Viro31efceb2011-09-09 19:14:02 -0400391 if (!strcmp(init, "none")) {
392 if (line->valid) {
393 line->valid = 0;
394 kfree(line->init_str);
Al Virocfe6b7c2011-09-09 19:45:42 -0400395 tty_unregister_device(driver, n);
Al Viro31efceb2011-09-09 19:14:02 -0400396 parse_chan_pair(NULL, line, n, opts, error_out);
397 err = 0;
398 }
399 } else {
400 char *new = kstrdup(init, GFP_KERNEL);
401 if (!new) {
402 *error_out = "Failed to allocate memory";
403 return -ENOMEM;
404 }
Al Viroc8e28762011-09-09 20:08:48 -0400405 if (line->valid) {
Al Virocfe6b7c2011-09-09 19:45:42 -0400406 tty_unregister_device(driver, n);
Al Viroc8e28762011-09-09 20:08:48 -0400407 kfree(line->init_str);
408 }
Al Viro31efceb2011-09-09 19:14:02 -0400409 line->init_str = new;
Al Viro43574c12011-09-09 17:25:00 -0400410 line->valid = 1;
Al Viro31efceb2011-09-09 19:14:02 -0400411 err = parse_chan_pair(new, line, n, opts, error_out);
Al Virocfe6b7c2011-09-09 19:45:42 -0400412 if (!err) {
Jiri Slaby734cc172012-08-07 21:47:47 +0200413 struct device *d = tty_port_register_device(&line->port,
414 driver, n, NULL);
Al Virocfe6b7c2011-09-09 19:45:42 -0400415 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:
Jeff Dikef28169d2007-02-10 01:43:53 -0800428 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800429}
430
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700431/*
432 * Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200433 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700434 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800435 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800436 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700437
Al Viro43574c12011-09-09 17:25:00 -0400438int line_setup(char **conf, unsigned int num, char **def,
439 char *init, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Al Viro43574c12011-09-09 17:25:00 -0400441 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700443 if (*init == '=') {
444 /*
445 * We said con=/ssl= instead of con#=, so we are configuring all
446 * consoles at once.
447 */
Al Viro43574c12011-09-09 17:25:00 -0400448 *def = init + 1;
449 } else {
450 char *end;
451 unsigned n = simple_strtoul(init, &end, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700452
Al Viro43574c12011-09-09 17:25:00 -0400453 if (*end != '=') {
454 error = "Couldn't parse device number";
455 goto out;
Jeff Dikef28169d2007-02-10 01:43:53 -0800456 }
Al Viro43574c12011-09-09 17:25:00 -0400457 if (n >= num) {
458 error = "Device number out of range";
459 goto out;
460 }
461 conf[n] = end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Al Viro43574c12011-09-09 17:25:00 -0400463 return 0;
464
465out:
466 printk(KERN_ERR "Failed to set up %s with "
467 "configuration string \"%s\" : %s\n", name, init, error);
468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Jeff Dike1f801712006-01-06 00:18:55 -0800471int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800472 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Al Virofe9a6b02011-09-08 20:44:06 -0400474 char *end;
Al Viro31efceb2011-09-09 19:14:02 -0400475 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700477 if (*str == '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800478 *error_out = "Can't configure all devices from mconsole";
479 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800480 }
481
Al Virofe9a6b02011-09-08 20:44:06 -0400482 n = simple_strtoul(str, &end, 0);
483 if (*end++ != '=') {
484 *error_out = "Couldn't parse device number";
485 return -EINVAL;
486 }
487 if (n >= num) {
488 *error_out = "Device number out of range";
489 return -EINVAL;
490 }
491
Al Viro31efceb2011-09-09 19:14:02 -0400492 return setup_one_line(lines, n, end, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700495int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int size, char **error_out)
497{
498 struct line *line;
499 char *end;
500 int dev, n = 0;
501
502 dev = simple_strtoul(name, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700503 if ((*end != '\0') || (end == name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700508 if ((dev < 0) || (dev >= num)) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700509 *error_out = "device number out of range";
510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
513 line = &lines[dev];
514
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700515 if (!line->valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 CONFIG_CHUNK(str, size, n, "none", 1);
Jiri Slaby6fc58842012-06-04 13:35:27 +0200517 else {
518 struct tty_struct *tty = tty_port_tty_get(&line->port);
519 if (tty == NULL) {
520 CONFIG_CHUNK(str, size, n, line->init_str, 1);
521 } else {
522 n = chan_config_string(line, str, size, error_out);
523 tty_kref_put(tty);
524 }
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700527 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Jeff Dike29d56cf2005-06-25 14:55:25 -0700530int line_id(char **str, int *start_out, int *end_out)
531{
532 char *end;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700533 int n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700534
535 n = simple_strtoul(*str, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700536 if ((*end != '\0') || (end == *str))
537 return -1;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700538
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700539 *str = end;
540 *start_out = n;
541 *end_out = n;
542 return n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700543}
544
Jeff Dikef28169d2007-02-10 01:43:53 -0800545int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Al Viroda645f32011-09-08 20:34:52 -0400547 if (n >= num) {
548 *error_out = "Device number out of range";
549 return -EINVAL;
550 }
Al Viro31efceb2011-09-09 19:14:02 -0400551 return setup_one_line(lines, n, "none", NULL, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Al Virocfe6b7c2011-09-09 19:45:42 -0400554int register_lines(struct line_driver *line_driver,
555 const struct tty_operations *ops,
556 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct tty_driver *driver = alloc_tty_driver(nlines);
Al Virocfe6b7c2011-09-09 19:45:42 -0400559 int err;
Al Viro04292b22011-09-09 20:07:05 -0400560 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (!driver)
Al Virocfe6b7c2011-09-09 19:45:42 -0400563 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 driver->driver_name = line_driver->name;
566 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 driver->major = line_driver->major;
568 driver->minor_start = line_driver->minor_start;
569 driver->type = line_driver->type;
570 driver->subtype = line_driver->subtype;
Al Virocfe6b7c2011-09-09 19:45:42 -0400571 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 driver->init_termios = tty_std_termios;
Al Viro04292b22011-09-09 20:07:05 -0400573
574 for (i = 0; i < nlines; i++) {
Jiri Slaby060ed312012-06-04 13:35:26 +0200575 tty_port_init(&lines[i].port);
Richard Weinberger79e02732012-06-04 21:57:24 +0200576 lines[i].port.ops = &line_port_ops;
Al Viro04292b22011-09-09 20:07:05 -0400577 spin_lock_init(&lines[i].lock);
Al Viro04292b22011-09-09 20:07:05 -0400578 lines[i].driver = line_driver;
579 INIT_LIST_HEAD(&lines[i].chan_list);
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 tty_set_operations(driver, ops);
582
Al Virocfe6b7c2011-09-09 19:45:42 -0400583 err = tty_register_driver(driver);
584 if (err) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700585 printk(KERN_ERR "register_lines : can't register %s driver\n",
586 line_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 put_tty_driver(driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100588 for (i = 0; i < nlines; i++)
589 tty_port_destroy(&lines[i].port);
Al Virocfe6b7c2011-09-09 19:45:42 -0400590 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
Al Virocfe6b7c2011-09-09 19:45:42 -0400593 line_driver->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 mconsole_register_dev(&line_driver->mc);
Al Virocfe6b7c2011-09-09 19:45:42 -0400595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Jeff Dike90107722006-01-06 00:18:54 -0800598static DEFINE_SPINLOCK(winch_handler_lock);
599static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601struct winch {
602 struct list_head list;
603 int fd;
604 int tty_fd;
605 int pid;
606 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700607 unsigned long stack;
Al Viro7cf3cf22011-09-14 16:21:31 -0700608 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609};
610
Al Viro7cf3cf22011-09-14 16:21:31 -0700611static void __free_winch(struct work_struct *work)
Jeff Dike42a359e2007-07-15 23:38:55 -0700612{
Al Viro7cf3cf22011-09-14 16:21:31 -0700613 struct winch *winch = container_of(work, struct winch, work);
Richard Weinbergerfa7a0442012-04-17 22:37:13 +0200614 um_free_irq(WINCH_IRQ, winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700615
616 if (winch->pid != -1)
617 os_kill_process(winch->pid, 1);
Jeff Dike42a359e2007-07-15 23:38:55 -0700618 if (winch->stack != 0)
619 free_stack(winch->stack, 0);
Jeff Dike42a359e2007-07-15 23:38:55 -0700620 kfree(winch);
621}
622
Al Viro7cf3cf22011-09-14 16:21:31 -0700623static void free_winch(struct winch *winch)
624{
625 int fd = winch->fd;
626 winch->fd = -1;
627 if (fd != -1)
628 os_close_file(fd);
629 list_del(&winch->list);
630 __free_winch(&winch->work);
631}
632
Al Viro7bea96f2006-10-08 22:49:34 +0100633static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct winch *winch = data;
636 struct tty_struct *tty;
637 struct line *line;
Al Viro7cf3cf22011-09-14 16:21:31 -0700638 int fd = winch->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int err;
640 char c;
641
Al Viro7cf3cf22011-09-14 16:21:31 -0700642 if (fd != -1) {
643 err = generic_read(fd, &c, NULL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700644 if (err < 0) {
645 if (err != -EAGAIN) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700646 winch->fd = -1;
647 list_del(&winch->list);
648 os_close_file(fd);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700649 printk(KERN_ERR "winch_interrupt : "
650 "read failed, errno = %d\n", -err);
651 printk(KERN_ERR "fd %d is losing SIGWINCH "
652 "support\n", winch->tty_fd);
Al Viro7cf3cf22011-09-14 16:21:31 -0700653 INIT_WORK(&winch->work, __free_winch);
654 schedule_work(&winch->work);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700655 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 goto out;
658 }
659 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700660 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (tty != NULL) {
662 line = tty->driver_data;
Jeff Dike438ee672008-02-04 22:31:19 -0800663 if (line != NULL) {
Al Virobed5e392011-09-08 10:49:34 -0400664 chan_window_size(line, &tty->winsize.ws_row,
Jeff Dike438ee672008-02-04 22:31:19 -0800665 &tty->winsize.ws_col);
666 kill_pgrp(tty->pgrp, SIGWINCH, 1);
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 out:
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700670 if (winch->fd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700672 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Jeff Dike42a359e2007-07-15 23:38:55 -0700675void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
676 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct winch *winch;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
681 if (winch == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700682 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700683 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
687 .fd = fd,
688 .tty_fd = tty_fd,
689 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700690 .tty = tty,
691 .stack = stack });
692
693 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Theodore Ts'oaab94462012-07-17 14:18:23 -0400694 IRQF_SHARED, "winch", winch) < 0) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700695 printk(KERN_ERR "register_winch_irq - failed to register "
696 "IRQ\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700697 goto out_free;
698 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700699
700 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700702 spin_unlock(&winch_handler_lock);
703
Jeff Dike42a359e2007-07-15 23:38:55 -0700704 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jeff Dike42a359e2007-07-15 23:38:55 -0700706 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800707 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700708 cleanup:
709 os_kill_process(pid, 1);
710 os_close_file(fd);
711 if (stack != 0)
712 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700713}
714
Jeff Dikee464bf22006-01-06 00:19:01 -0800715static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Will Newton48a0b742011-01-12 16:59:26 -0800717 struct list_head *ele, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct winch *winch;
719
Jeff Dikee464bf22006-01-06 00:19:01 -0800720 spin_lock(&winch_handler_lock);
721
Will Newton48a0b742011-01-12 16:59:26 -0800722 list_for_each_safe(ele, next, &winch_handlers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 winch = list_entry(ele, struct winch, list);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700724 if (winch->tty == tty) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700725 free_winch(winch);
Jeff Dikee464bf22006-01-06 00:19:01 -0800726 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700727 }
728 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800729 spin_unlock(&winch_handler_lock);
730}
731
732static void winch_cleanup(void)
733{
734 struct list_head *ele, *next;
735 struct winch *winch;
736
737 spin_lock(&winch_handler_lock);
738
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700739 list_for_each_safe(ele, next, &winch_handlers) {
Jeff Dikee464bf22006-01-06 00:19:01 -0800740 winch = list_entry(ele, struct winch, list);
Al Viro7cf3cf22011-09-14 16:21:31 -0700741 free_winch(winch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800743
744 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746__uml_exitcall(winch_cleanup);
747
748char *add_xterm_umid(char *base)
749{
750 char *umid, *title;
751 int len;
752
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800753 umid = get_umid();
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700754 if (*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700755 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
758 title = kmalloc(len, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700759 if (title == NULL) {
760 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700761 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763
764 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700765 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}