blob: cc206eda245c9912705c98e23c6f60eab56e01eb [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int err;
252
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700253 /*
Yong Zhangc0b79a92011-09-22 16:58:46 +0800254 * Interrupts are disabled here because genirq keep irqs disabled when
255 * calling the action handler.
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700256 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700257
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800258 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700260 if (err == 0) {
Al Viro199eebb2012-02-11 03:05:32 -0500261 spin_unlock(&line->lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700262 return IRQ_NONE;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700263 } else if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 line->head = line->buffer;
265 line->tail = line->buffer;
266 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800267 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100269 tty_port_tty_wakeup(&line->port);
Jiri Slaby6fc58842012-06-04 13:35:27 +0200270
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700271 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Jeff Dike165dc592006-01-06 00:18:57 -0800274int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700276 const struct line_driver *driver = line->driver;
Theodore Ts'oaab94462012-07-17 14:18:23 -0400277 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700279 if (input)
280 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Theodore Ts'oaab94462012-07-17 14:18:23 -0400281 line_interrupt, IRQF_SHARED,
282 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700283 if (err)
284 return err;
285 if (output)
286 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Theodore Ts'oaab94462012-07-17 14:18:23 -0400287 line_write_interrupt, IRQF_SHARED,
288 driver->write_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700289 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Richard Weinberger79e02732012-06-04 21:57:24 +0200292static int line_activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Richard Weinberger79e02732012-06-04 21:57:24 +0200294 int ret;
295 struct line *line = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Richard Weinberger79e02732012-06-04 21:57:24 +0200297 ret = enable_chan(line);
298 if (ret)
299 return ret;
Jeff Diked14ad812007-07-15 23:38:54 -0700300
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700301 if (!line->sigio) {
Al Virobed5e392011-09-08 10:49:34 -0400302 chan_enable_winch(line->chan_out, tty);
Jeff Diked79a5802007-02-10 01:43:52 -0800303 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Al Virobed5e392011-09-08 10:49:34 -0400306 chan_window_size(line, &tty->winsize.ws_row,
Richard Weinberger79e02732012-06-04 21:57:24 +0200307 &tty->winsize.ws_col);
308
309 return 0;
310}
311
312static const struct tty_port_operations line_port_ops = {
313 .activate = line_activate,
314};
315
316int line_open(struct tty_struct *tty, struct file *filp)
317{
318 struct line *line = tty->driver_data;
319
320 return tty_port_open(&line->port, tty, filp);
321}
322
323int line_install(struct tty_driver *driver, struct tty_struct *tty,
324 struct line *line)
325{
326 int ret;
327
328 ret = tty_standard_install(driver, tty);
329 if (ret)
330 return ret;
331
332 tty->driver_data = line;
333
334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700337static void unregister_winch(struct tty_struct *tty);
338
Richard Weinberger79e02732012-06-04 21:57:24 +0200339void line_cleanup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct line *line = tty->driver_data;
342
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700343 if (line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800344 unregister_winch(tty);
345 line->sigio = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700346 }
Richard Weinberger79e02732012-06-04 21:57:24 +0200347}
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700348
Richard Weinberger79e02732012-06-04 21:57:24 +0200349void line_close(struct tty_struct *tty, struct file * filp)
350{
351 struct line *line = tty->driver_data;
352
353 tty_port_close(&line->port, tty, filp);
354}
355
356void line_hangup(struct tty_struct *tty)
357{
358 struct line *line = tty->driver_data;
359
360 tty_port_hangup(&line->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363void close_lines(struct line *lines, int nlines)
364{
365 int i;
366
367 for(i = 0; i < nlines; i++)
Al Viro10c890c02011-09-10 08:39:18 -0400368 close_chan(&lines[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Al Viro04292b22011-09-09 20:07:05 -0400371int setup_one_line(struct line *lines, int n, char *init,
372 const struct chan_opts *opts, char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800373{
374 struct line *line = &lines[n];
Al Virocfe6b7c2011-09-09 19:45:42 -0400375 struct tty_driver *driver = line->driver->driver;
Jeff Dikef28169d2007-02-10 01:43:53 -0800376 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800377
Jiri Slaby060ed312012-06-04 13:35:26 +0200378 if (line->port.count) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800379 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800380 goto out;
381 }
382
Al Viro31efceb2011-09-09 19:14:02 -0400383 if (!strcmp(init, "none")) {
384 if (line->valid) {
385 line->valid = 0;
386 kfree(line->init_str);
Al Virocfe6b7c2011-09-09 19:45:42 -0400387 tty_unregister_device(driver, n);
Al Viro31efceb2011-09-09 19:14:02 -0400388 parse_chan_pair(NULL, line, n, opts, error_out);
389 err = 0;
390 }
391 } else {
392 char *new = kstrdup(init, GFP_KERNEL);
393 if (!new) {
394 *error_out = "Failed to allocate memory";
395 return -ENOMEM;
396 }
Al Viroc8e28762011-09-09 20:08:48 -0400397 if (line->valid) {
Al Virocfe6b7c2011-09-09 19:45:42 -0400398 tty_unregister_device(driver, n);
Al Viroc8e28762011-09-09 20:08:48 -0400399 kfree(line->init_str);
400 }
Al Viro31efceb2011-09-09 19:14:02 -0400401 line->init_str = new;
Al Viro43574c12011-09-09 17:25:00 -0400402 line->valid = 1;
Al Viro31efceb2011-09-09 19:14:02 -0400403 err = parse_chan_pair(new, line, n, opts, error_out);
Al Virocfe6b7c2011-09-09 19:45:42 -0400404 if (!err) {
Jiri Slaby734cc172012-08-07 21:47:47 +0200405 struct device *d = tty_port_register_device(&line->port,
406 driver, n, NULL);
Al Virocfe6b7c2011-09-09 19:45:42 -0400407 if (IS_ERR(d)) {
408 *error_out = "Failed to register device";
409 err = PTR_ERR(d);
410 parse_chan_pair(NULL, line, n, opts, error_out);
411 }
412 }
Al Viro31efceb2011-09-09 19:14:02 -0400413 if (err) {
414 line->init_str = NULL;
415 line->valid = 0;
416 kfree(new);
417 }
Jeff Diked79a5802007-02-10 01:43:52 -0800418 }
419out:
Jeff Dikef28169d2007-02-10 01:43:53 -0800420 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800421}
422
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700423/*
424 * Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200425 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700426 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800427 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800428 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700429
Al Viro43574c12011-09-09 17:25:00 -0400430int line_setup(char **conf, unsigned int num, char **def,
431 char *init, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Al Viro43574c12011-09-09 17:25:00 -0400433 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700435 if (*init == '=') {
436 /*
437 * We said con=/ssl= instead of con#=, so we are configuring all
438 * consoles at once.
439 */
Al Viro43574c12011-09-09 17:25:00 -0400440 *def = init + 1;
441 } else {
442 char *end;
443 unsigned n = simple_strtoul(init, &end, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700444
Al Viro43574c12011-09-09 17:25:00 -0400445 if (*end != '=') {
446 error = "Couldn't parse device number";
447 goto out;
Jeff Dikef28169d2007-02-10 01:43:53 -0800448 }
Al Viro43574c12011-09-09 17:25:00 -0400449 if (n >= num) {
450 error = "Device number out of range";
451 goto out;
452 }
453 conf[n] = end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Al Viro43574c12011-09-09 17:25:00 -0400455 return 0;
456
457out:
458 printk(KERN_ERR "Failed to set up %s with "
459 "configuration string \"%s\" : %s\n", name, init, error);
460 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Jeff Dike1f801712006-01-06 00:18:55 -0800463int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800464 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Al Virofe9a6b02011-09-08 20:44:06 -0400466 char *end;
Al Viro31efceb2011-09-09 19:14:02 -0400467 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700469 if (*str == '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800470 *error_out = "Can't configure all devices from mconsole";
471 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800472 }
473
Al Virofe9a6b02011-09-08 20:44:06 -0400474 n = simple_strtoul(str, &end, 0);
475 if (*end++ != '=') {
476 *error_out = "Couldn't parse device number";
477 return -EINVAL;
478 }
479 if (n >= num) {
480 *error_out = "Device number out of range";
481 return -EINVAL;
482 }
483
Al Viro31efceb2011-09-09 19:14:02 -0400484 return setup_one_line(lines, n, end, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700487int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int size, char **error_out)
489{
490 struct line *line;
491 char *end;
492 int dev, n = 0;
493
494 dev = simple_strtoul(name, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700495 if ((*end != '\0') || (end == name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700500 if ((dev < 0) || (dev >= num)) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700501 *error_out = "device number out of range";
502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
505 line = &lines[dev];
506
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700507 if (!line->valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 CONFIG_CHUNK(str, size, n, "none", 1);
Jiri Slaby6fc58842012-06-04 13:35:27 +0200509 else {
510 struct tty_struct *tty = tty_port_tty_get(&line->port);
511 if (tty == NULL) {
512 CONFIG_CHUNK(str, size, n, line->init_str, 1);
513 } else {
514 n = chan_config_string(line, str, size, error_out);
515 tty_kref_put(tty);
516 }
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700519 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Jeff Dike29d56cf2005-06-25 14:55:25 -0700522int line_id(char **str, int *start_out, int *end_out)
523{
524 char *end;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700525 int n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700526
527 n = simple_strtoul(*str, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700528 if ((*end != '\0') || (end == *str))
529 return -1;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700530
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700531 *str = end;
532 *start_out = n;
533 *end_out = n;
534 return n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700535}
536
Jeff Dikef28169d2007-02-10 01:43:53 -0800537int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Al Viroda645f32011-09-08 20:34:52 -0400539 if (n >= num) {
540 *error_out = "Device number out of range";
541 return -EINVAL;
542 }
Al Viro31efceb2011-09-09 19:14:02 -0400543 return setup_one_line(lines, n, "none", NULL, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Al Virocfe6b7c2011-09-09 19:45:42 -0400546int register_lines(struct line_driver *line_driver,
547 const struct tty_operations *ops,
548 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct tty_driver *driver = alloc_tty_driver(nlines);
Al Virocfe6b7c2011-09-09 19:45:42 -0400551 int err;
Al Viro04292b22011-09-09 20:07:05 -0400552 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 if (!driver)
Al Virocfe6b7c2011-09-09 19:45:42 -0400555 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 driver->driver_name = line_driver->name;
558 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 driver->major = line_driver->major;
560 driver->minor_start = line_driver->minor_start;
561 driver->type = line_driver->type;
562 driver->subtype = line_driver->subtype;
Al Virocfe6b7c2011-09-09 19:45:42 -0400563 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 driver->init_termios = tty_std_termios;
Al Viro04292b22011-09-09 20:07:05 -0400565
566 for (i = 0; i < nlines; i++) {
Jiri Slaby060ed312012-06-04 13:35:26 +0200567 tty_port_init(&lines[i].port);
Richard Weinberger79e02732012-06-04 21:57:24 +0200568 lines[i].port.ops = &line_port_ops;
Al Viro04292b22011-09-09 20:07:05 -0400569 spin_lock_init(&lines[i].lock);
Al Viro04292b22011-09-09 20:07:05 -0400570 lines[i].driver = line_driver;
571 INIT_LIST_HEAD(&lines[i].chan_list);
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 tty_set_operations(driver, ops);
574
Al Virocfe6b7c2011-09-09 19:45:42 -0400575 err = tty_register_driver(driver);
576 if (err) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700577 printk(KERN_ERR "register_lines : can't register %s driver\n",
578 line_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 put_tty_driver(driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100580 for (i = 0; i < nlines; i++)
581 tty_port_destroy(&lines[i].port);
Al Virocfe6b7c2011-09-09 19:45:42 -0400582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
Al Virocfe6b7c2011-09-09 19:45:42 -0400585 line_driver->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 mconsole_register_dev(&line_driver->mc);
Al Virocfe6b7c2011-09-09 19:45:42 -0400587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Jeff Dike90107722006-01-06 00:18:54 -0800590static DEFINE_SPINLOCK(winch_handler_lock);
591static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593struct winch {
594 struct list_head list;
595 int fd;
596 int tty_fd;
597 int pid;
598 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700599 unsigned long stack;
Al Viro7cf3cf22011-09-14 16:21:31 -0700600 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601};
602
Al Viro7cf3cf22011-09-14 16:21:31 -0700603static void __free_winch(struct work_struct *work)
Jeff Dike42a359e2007-07-15 23:38:55 -0700604{
Al Viro7cf3cf22011-09-14 16:21:31 -0700605 struct winch *winch = container_of(work, struct winch, work);
Richard Weinbergerfa7a0442012-04-17 22:37:13 +0200606 um_free_irq(WINCH_IRQ, winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700607
608 if (winch->pid != -1)
609 os_kill_process(winch->pid, 1);
Jeff Dike42a359e2007-07-15 23:38:55 -0700610 if (winch->stack != 0)
611 free_stack(winch->stack, 0);
Jeff Dike42a359e2007-07-15 23:38:55 -0700612 kfree(winch);
613}
614
Al Viro7cf3cf22011-09-14 16:21:31 -0700615static void free_winch(struct winch *winch)
616{
617 int fd = winch->fd;
618 winch->fd = -1;
619 if (fd != -1)
620 os_close_file(fd);
621 list_del(&winch->list);
622 __free_winch(&winch->work);
623}
624
Al Viro7bea96f2006-10-08 22:49:34 +0100625static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct winch *winch = data;
628 struct tty_struct *tty;
629 struct line *line;
Al Viro7cf3cf22011-09-14 16:21:31 -0700630 int fd = winch->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int err;
632 char c;
633
Al Viro7cf3cf22011-09-14 16:21:31 -0700634 if (fd != -1) {
635 err = generic_read(fd, &c, NULL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700636 if (err < 0) {
637 if (err != -EAGAIN) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700638 winch->fd = -1;
639 list_del(&winch->list);
640 os_close_file(fd);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700641 printk(KERN_ERR "winch_interrupt : "
642 "read failed, errno = %d\n", -err);
643 printk(KERN_ERR "fd %d is losing SIGWINCH "
644 "support\n", winch->tty_fd);
Al Viro7cf3cf22011-09-14 16:21:31 -0700645 INIT_WORK(&winch->work, __free_winch);
646 schedule_work(&winch->work);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700647 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 goto out;
650 }
651 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700652 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (tty != NULL) {
654 line = tty->driver_data;
Jeff Dike438ee672008-02-04 22:31:19 -0800655 if (line != NULL) {
Al Virobed5e392011-09-08 10:49:34 -0400656 chan_window_size(line, &tty->winsize.ws_row,
Jeff Dike438ee672008-02-04 22:31:19 -0800657 &tty->winsize.ws_col);
658 kill_pgrp(tty->pgrp, SIGWINCH, 1);
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661 out:
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700662 if (winch->fd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700664 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Jeff Dike42a359e2007-07-15 23:38:55 -0700667void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
668 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct winch *winch;
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
673 if (winch == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700674 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700675 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
679 .fd = fd,
680 .tty_fd = tty_fd,
681 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700682 .tty = tty,
683 .stack = stack });
684
685 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Theodore Ts'oaab94462012-07-17 14:18:23 -0400686 IRQF_SHARED, "winch", winch) < 0) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700687 printk(KERN_ERR "register_winch_irq - failed to register "
688 "IRQ\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700689 goto out_free;
690 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700691
692 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700694 spin_unlock(&winch_handler_lock);
695
Jeff Dike42a359e2007-07-15 23:38:55 -0700696 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Jeff Dike42a359e2007-07-15 23:38:55 -0700698 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800699 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700700 cleanup:
701 os_kill_process(pid, 1);
702 os_close_file(fd);
703 if (stack != 0)
704 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700705}
706
Jeff Dikee464bf22006-01-06 00:19:01 -0800707static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Will Newton48a0b742011-01-12 16:59:26 -0800709 struct list_head *ele, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct winch *winch;
711
Jeff Dikee464bf22006-01-06 00:19:01 -0800712 spin_lock(&winch_handler_lock);
713
Will Newton48a0b742011-01-12 16:59:26 -0800714 list_for_each_safe(ele, next, &winch_handlers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 winch = list_entry(ele, struct winch, list);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700716 if (winch->tty == tty) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700717 free_winch(winch);
Jeff Dikee464bf22006-01-06 00:19:01 -0800718 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700719 }
720 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800721 spin_unlock(&winch_handler_lock);
722}
723
724static void winch_cleanup(void)
725{
726 struct list_head *ele, *next;
727 struct winch *winch;
728
729 spin_lock(&winch_handler_lock);
730
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700731 list_for_each_safe(ele, next, &winch_handlers) {
Jeff Dikee464bf22006-01-06 00:19:01 -0800732 winch = list_entry(ele, struct winch, list);
Al Viro7cf3cf22011-09-14 16:21:31 -0700733 free_winch(winch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800735
736 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738__uml_exitcall(winch_cleanup);
739
740char *add_xterm_umid(char *base)
741{
742 char *umid, *title;
743 int len;
744
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800745 umid = get_umid();
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700746 if (*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700747 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
750 title = kmalloc(len, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700751 if (title == NULL) {
752 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700753 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
756 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700757 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}