Commit | Line | Data |
---|---|---|
165dc591 | 1 | /* |
2f8a2dc2 | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4 LT |
3 | * Licensed under the GPL |
4 | */ | |
5 | ||
2f8a2dc2 | 6 | #include "linux/irqreturn.h" |
1da177e4 | 7 | #include "linux/kd.h" |
d43c36dc | 8 | #include "linux/sched.h" |
7f3c1fa4 | 9 | #include "linux/slab.h" |
510c72a3 | 10 | #include "chan.h" |
2f8a2dc2 | 11 | #include "irq_kern.h" |
1da177e4 | 12 | #include "irq_user.h" |
edea1385 | 13 | #include "kern_util.h" |
1da177e4 | 14 | #include "os.h" |
1da177e4 LT |
15 | |
16 | #define LINE_BUFSIZE 4096 | |
17 | ||
7bea96fd | 18 | static irqreturn_t line_interrupt(int irq, void *data) |
1da177e4 | 19 | { |
165dc591 JD |
20 | struct chan *chan = data; |
21 | struct line *line = chan->line; | |
1da177e4 LT |
22 | |
23 | if (line) | |
0fcd7199 | 24 | chan_interrupt(line, line->tty, irq); |
1da177e4 LT |
25 | return IRQ_HANDLED; |
26 | } | |
27 | ||
2f8a2dc2 JD |
28 | /* |
29 | * Returns the free space inside the ring buffer of this line. | |
b97b77cc | 30 | * |
b60745b9 | 31 | * Should be called while holding line->lock (this does not modify data). |
b97b77cc PBG |
32 | */ |
33 | static int write_room(struct line *line) | |
1da177e4 LT |
34 | { |
35 | int n; | |
36 | ||
b97b77cc PBG |
37 | if (line->buffer == NULL) |
38 | return LINE_BUFSIZE - 1; | |
39 | ||
40 | /* This is for the case where the buffer is wrapped! */ | |
41 | n = line->head - line->tail; | |
1da177e4 | 42 | |
1da177e4 | 43 | if (n <= 0) |
acb2cf34 | 44 | n += LINE_BUFSIZE; /* The other case */ |
b97b77cc PBG |
45 | return n - 1; |
46 | } | |
47 | ||
48 | int line_write_room(struct tty_struct *tty) | |
49 | { | |
50 | struct line *line = tty->driver_data; | |
51 | unsigned long flags; | |
52 | int room; | |
53 | ||
b97b77cc PBG |
54 | spin_lock_irqsave(&line->lock, flags); |
55 | room = write_room(line); | |
56 | spin_unlock_irqrestore(&line->lock, flags); | |
57 | ||
b97b77cc | 58 | return room; |
1da177e4 LT |
59 | } |
60 | ||
b97b77cc PBG |
61 | int line_chars_in_buffer(struct tty_struct *tty) |
62 | { | |
63 | struct line *line = tty->driver_data; | |
64 | unsigned long flags; | |
65 | int ret; | |
66 | ||
67 | spin_lock_irqsave(&line->lock, flags); | |
acb2cf34 | 68 | /* write_room subtracts 1 for the needed NULL, so we readd it.*/ |
b97b77cc PBG |
69 | ret = LINE_BUFSIZE - (write_room(line) + 1); |
70 | spin_unlock_irqrestore(&line->lock, flags); | |
71 | ||
72 | return ret; | |
73 | } | |
74 | ||
75 | /* | |
76 | * This copies the content of buf into the circular buffer associated with | |
77 | * this line. | |
78 | * The return value is the number of characters actually copied, i.e. the ones | |
79 | * for which there was space: this function is not supposed to ever flush out | |
80 | * the circular buffer. | |
81 | * | |
82 | * Must be called while holding line->lock! | |
83 | */ | |
1da177e4 LT |
84 | static int buffer_data(struct line *line, const char *buf, int len) |
85 | { | |
86 | int end, room; | |
87 | ||
2f8a2dc2 | 88 | if (line->buffer == NULL) { |
1da177e4 LT |
89 | line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC); |
90 | if (line->buffer == NULL) { | |
2f8a2dc2 JD |
91 | printk(KERN_ERR "buffer_data - atomic allocation " |
92 | "failed\n"); | |
93 | return 0; | |
1da177e4 LT |
94 | } |
95 | line->head = line->buffer; | |
96 | line->tail = line->buffer; | |
97 | } | |
98 | ||
99 | room = write_room(line); | |
100 | len = (len > room) ? room : len; | |
101 | ||
102 | end = line->buffer + LINE_BUFSIZE - line->tail; | |
b97b77cc | 103 | |
2f8a2dc2 | 104 | if (len < end) { |
1da177e4 LT |
105 | memcpy(line->tail, buf, len); |
106 | line->tail += len; | |
d50084a2 JD |
107 | } |
108 | else { | |
b97b77cc | 109 | /* The circular buffer is wrapping */ |
1da177e4 LT |
110 | memcpy(line->tail, buf, end); |
111 | buf += end; | |
112 | memcpy(line->buffer, buf, len - end); | |
113 | line->tail = line->buffer + len - end; | |
114 | } | |
115 | ||
b97b77cc | 116 | return len; |
1da177e4 LT |
117 | } |
118 | ||
b97b77cc PBG |
119 | /* |
120 | * Flushes the ring buffer to the output channels. That is, write_chan is | |
121 | * called, passing it line->head as buffer, and an appropriate count. | |
122 | * | |
123 | * On exit, returns 1 when the buffer is empty, | |
124 | * 0 when the buffer is not empty on exit, | |
125 | * and -errno when an error occurred. | |
126 | * | |
127 | * Must be called while holding line->lock!*/ | |
1da177e4 LT |
128 | static int flush_buffer(struct line *line) |
129 | { | |
130 | int n, count; | |
131 | ||
132 | if ((line->buffer == NULL) || (line->head == line->tail)) | |
b97b77cc | 133 | return 1; |
1da177e4 LT |
134 | |
135 | if (line->tail < line->head) { | |
b97b77cc | 136 | /* line->buffer + LINE_BUFSIZE is the end of the buffer! */ |
1da177e4 | 137 | count = line->buffer + LINE_BUFSIZE - line->head; |
b97b77cc | 138 | |
bed5e39c | 139 | n = write_chan(line->chan_out, line->head, count, |
1da177e4 LT |
140 | line->driver->write_irq); |
141 | if (n < 0) | |
b97b77cc PBG |
142 | return n; |
143 | if (n == count) { | |
2f8a2dc2 JD |
144 | /* |
145 | * We have flushed from ->head to buffer end, now we | |
146 | * must flush only from the beginning to ->tail. | |
147 | */ | |
1da177e4 | 148 | line->head = line->buffer; |
b97b77cc | 149 | } else { |
1da177e4 | 150 | line->head += n; |
b97b77cc | 151 | return 0; |
1da177e4 LT |
152 | } |
153 | } | |
154 | ||
155 | count = line->tail - line->head; | |
bed5e39c | 156 | n = write_chan(line->chan_out, line->head, count, |
1da177e4 | 157 | line->driver->write_irq); |
b97b77cc | 158 | |
2f8a2dc2 | 159 | if (n < 0) |
b97b77cc | 160 | return n; |
1da177e4 LT |
161 | |
162 | line->head += n; | |
b97b77cc PBG |
163 | return line->head == line->tail; |
164 | } | |
165 | ||
166 | void line_flush_buffer(struct tty_struct *tty) | |
167 | { | |
168 | struct line *line = tty->driver_data; | |
169 | unsigned long flags; | |
b97b77cc | 170 | |
b97b77cc | 171 | spin_lock_irqsave(&line->lock, flags); |
f1c93e49 | 172 | flush_buffer(line); |
b97b77cc | 173 | spin_unlock_irqrestore(&line->lock, flags); |
b97b77cc PBG |
174 | } |
175 | ||
2f8a2dc2 JD |
176 | /* |
177 | * We map both ->flush_chars and ->put_char (which go in pair) onto | |
178 | * ->flush_buffer and ->write. Hope it's not that bad. | |
179 | */ | |
b97b77cc PBG |
180 | void line_flush_chars(struct tty_struct *tty) |
181 | { | |
182 | line_flush_buffer(tty); | |
183 | } | |
184 | ||
3168cb98 | 185 | int line_put_char(struct tty_struct *tty, unsigned char ch) |
b97b77cc | 186 | { |
3168cb98 | 187 | return line_write(tty, &ch, sizeof(ch)); |
1da177e4 LT |
188 | } |
189 | ||
190 | int line_write(struct tty_struct *tty, const unsigned char *buf, int len) | |
191 | { | |
192 | struct line *line = tty->driver_data; | |
193 | unsigned long flags; | |
c59dbcad | 194 | int n, ret = 0; |
1da177e4 | 195 | |
b97b77cc | 196 | spin_lock_irqsave(&line->lock, flags); |
c59dbcad | 197 | if (line->head != line->tail) |
1da177e4 | 198 | ret = buffer_data(line, buf, len); |
c59dbcad | 199 | else { |
bed5e39c | 200 | n = write_chan(line->chan_out, buf, len, |
1da177e4 | 201 | line->driver->write_irq); |
b97b77cc | 202 | if (n < 0) { |
1da177e4 LT |
203 | ret = n; |
204 | goto out_up; | |
205 | } | |
206 | ||
207 | len -= n; | |
208 | ret += n; | |
b97b77cc | 209 | if (len > 0) |
1da177e4 LT |
210 | ret += buffer_data(line, buf + n, len); |
211 | } | |
b97b77cc PBG |
212 | out_up: |
213 | spin_unlock_irqrestore(&line->lock, flags); | |
214 | return ret; | |
1da177e4 LT |
215 | } |
216 | ||
606d099c | 217 | void line_set_termios(struct tty_struct *tty, struct ktermios * old) |
1da177e4 LT |
218 | { |
219 | /* nothing */ | |
220 | } | |
221 | ||
5e7672ec | 222 | static const struct { |
1da177e4 LT |
223 | int cmd; |
224 | char *level; | |
225 | char *name; | |
226 | } tty_ioctls[] = { | |
227 | /* don't print these, they flood the log ... */ | |
228 | { TCGETS, NULL, "TCGETS" }, | |
2f8a2dc2 JD |
229 | { TCSETS, NULL, "TCSETS" }, |
230 | { TCSETSW, NULL, "TCSETSW" }, | |
231 | { TCFLSH, NULL, "TCFLSH" }, | |
232 | { TCSBRK, NULL, "TCSBRK" }, | |
1da177e4 LT |
233 | |
234 | /* general tty stuff */ | |
2f8a2dc2 JD |
235 | { TCSETSF, KERN_DEBUG, "TCSETSF" }, |
236 | { TCGETA, KERN_DEBUG, "TCGETA" }, | |
237 | { TIOCMGET, KERN_DEBUG, "TIOCMGET" }, | |
238 | { TCSBRKP, KERN_DEBUG, "TCSBRKP" }, | |
239 | { TIOCMSET, KERN_DEBUG, "TIOCMSET" }, | |
1da177e4 LT |
240 | |
241 | /* linux-specific ones */ | |
242 | { TIOCLINUX, KERN_INFO, "TIOCLINUX" }, | |
243 | { KDGKBMODE, KERN_INFO, "KDGKBMODE" }, | |
244 | { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" }, | |
245 | { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" }, | |
246 | }; | |
247 | ||
8a06dc4d RW |
248 | int line_ioctl(struct tty_struct *tty, unsigned int cmd, |
249 | unsigned long arg) | |
1da177e4 LT |
250 | { |
251 | int ret; | |
252 | int i; | |
253 | ||
254 | ret = 0; | |
255 | switch(cmd) { | |
256 | #ifdef TIOCGETP | |
257 | case TIOCGETP: | |
258 | case TIOCSETP: | |
259 | case TIOCSETN: | |
260 | #endif | |
261 | #ifdef TIOCGETC | |
262 | case TIOCGETC: | |
263 | case TIOCSETC: | |
264 | #endif | |
265 | #ifdef TIOCGLTC | |
266 | case TIOCGLTC: | |
267 | case TIOCSLTC: | |
268 | #endif | |
c564b6fd AC |
269 | /* Note: these are out of date as we now have TCGETS2 etc but this |
270 | whole lot should probably go away */ | |
1da177e4 LT |
271 | case TCGETS: |
272 | case TCSETSF: | |
273 | case TCSETSW: | |
274 | case TCSETS: | |
275 | case TCGETA: | |
276 | case TCSETAF: | |
277 | case TCSETAW: | |
278 | case TCSETA: | |
279 | case TCXONC: | |
280 | case TCFLSH: | |
281 | case TIOCOUTQ: | |
282 | case TIOCINQ: | |
283 | case TIOCGLCKTRMIOS: | |
284 | case TIOCSLCKTRMIOS: | |
285 | case TIOCPKT: | |
286 | case TIOCGSOFTCAR: | |
287 | case TIOCSSOFTCAR: | |
288 | return -ENOIOCTLCMD; | |
289 | #if 0 | |
290 | case TCwhatever: | |
291 | /* do something */ | |
292 | break; | |
293 | #endif | |
294 | default: | |
295 | for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++) | |
296 | if (cmd == tty_ioctls[i].cmd) | |
297 | break; | |
2f8a2dc2 | 298 | if (i == ARRAY_SIZE(tty_ioctls)) { |
1da177e4 | 299 | printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n", |
3595726a | 300 | __func__, tty->name, cmd); |
1da177e4 LT |
301 | } |
302 | ret = -ENOIOCTLCMD; | |
303 | break; | |
304 | } | |
b97b77cc | 305 | return ret; |
1da177e4 LT |
306 | } |
307 | ||
e4dcee80 JD |
308 | void line_throttle(struct tty_struct *tty) |
309 | { | |
310 | struct line *line = tty->driver_data; | |
311 | ||
bed5e39c | 312 | deactivate_chan(line->chan_in, line->driver->read_irq); |
e4dcee80 JD |
313 | line->throttled = 1; |
314 | } | |
315 | ||
316 | void line_unthrottle(struct tty_struct *tty) | |
317 | { | |
318 | struct line *line = tty->driver_data; | |
319 | ||
320 | line->throttled = 0; | |
0fcd7199 | 321 | chan_interrupt(line, tty, line->driver->read_irq); |
e4dcee80 | 322 | |
2f8a2dc2 JD |
323 | /* |
324 | * Maybe there is enough stuff pending that calling the interrupt | |
e4dcee80 JD |
325 | * throttles us again. In this case, line->throttled will be 1 |
326 | * again and we shouldn't turn the interrupt back on. | |
327 | */ | |
2f8a2dc2 | 328 | if (!line->throttled) |
bed5e39c | 329 | reactivate_chan(line->chan_in, line->driver->read_irq); |
e4dcee80 JD |
330 | } |
331 | ||
7bea96fd | 332 | static irqreturn_t line_write_interrupt(int irq, void *data) |
1da177e4 | 333 | { |
165dc591 JD |
334 | struct chan *chan = data; |
335 | struct line *line = chan->line; | |
336 | struct tty_struct *tty = line->tty; | |
1da177e4 LT |
337 | int err; |
338 | ||
2f8a2dc2 | 339 | /* |
c0b79a90 YZ |
340 | * Interrupts are disabled here because genirq keep irqs disabled when |
341 | * calling the action handler. | |
2f8a2dc2 | 342 | */ |
b97b77cc | 343 | |
ec0ac8ad | 344 | spin_lock(&line->lock); |
1da177e4 | 345 | err = flush_buffer(line); |
b97b77cc PBG |
346 | if (err == 0) { |
347 | return IRQ_NONE; | |
2f8a2dc2 | 348 | } else if (err < 0) { |
1da177e4 LT |
349 | line->head = line->buffer; |
350 | line->tail = line->buffer; | |
351 | } | |
ec0ac8ad | 352 | spin_unlock(&line->lock); |
1da177e4 | 353 | |
2f8a2dc2 | 354 | if (tty == NULL) |
b97b77cc | 355 | return IRQ_NONE; |
1da177e4 | 356 | |
06ac6679 | 357 | tty_wakeup(tty); |
b97b77cc | 358 | return IRQ_HANDLED; |
1da177e4 LT |
359 | } |
360 | ||
165dc591 | 361 | int line_setup_irq(int fd, int input, int output, struct line *line, void *data) |
1da177e4 | 362 | { |
5e7672ec | 363 | const struct line_driver *driver = line->driver; |
c0b79a90 | 364 | int err = 0, flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM; |
1da177e4 | 365 | |
b97b77cc PBG |
366 | if (input) |
367 | err = um_request_irq(driver->read_irq, fd, IRQ_READ, | |
d50084a2 | 368 | line_interrupt, flags, |
165dc591 | 369 | driver->read_irq_name, data); |
b97b77cc PBG |
370 | if (err) |
371 | return err; | |
372 | if (output) | |
373 | err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, | |
d50084a2 | 374 | line_write_interrupt, flags, |
165dc591 | 375 | driver->write_irq_name, data); |
b97b77cc | 376 | return err; |
1da177e4 LT |
377 | } |
378 | ||
2f8a2dc2 JD |
379 | /* |
380 | * Normally, a driver like this can rely mostly on the tty layer | |
d79a5809 JD |
381 | * locking, particularly when it comes to the driver structure. |
382 | * However, in this case, mconsole requests can come in "from the | |
383 | * side", and race with opens and closes. | |
384 | * | |
c6256c68 JD |
385 | * mconsole config requests will want to be sure the device isn't in |
386 | * use, and get_config, open, and close will want a stable | |
387 | * configuration. The checking and modification of the configuration | |
388 | * is done under a spinlock. Checking whether the device is in use is | |
389 | * line->tty->count > 1, also under the spinlock. | |
d79a5809 | 390 | * |
f71f9484 AV |
391 | * line->count serves to decide whether the device should be enabled or |
392 | * disabled on the host. If it's equal to 0, then we are doing the | |
c6256c68 | 393 | * first open or last close. Otherwise, open and close just return. |
d79a5809 JD |
394 | */ |
395 | ||
1f80171e | 396 | int line_open(struct line *lines, struct tty_struct *tty) |
1da177e4 | 397 | { |
d79a5809 | 398 | struct line *line = &lines[tty->index]; |
165dc591 | 399 | int err = -ENODEV; |
1da177e4 | 400 | |
d8c215ad | 401 | mutex_lock(&line->count_lock); |
2f8a2dc2 | 402 | if (!line->valid) |
d79a5809 | 403 | goto out_unlock; |
1da177e4 | 404 | |
d79a5809 | 405 | err = 0; |
f71f9484 | 406 | if (line->count++) |
d79a5809 JD |
407 | goto out_unlock; |
408 | ||
f71f9484 | 409 | BUG_ON(tty->driver_data); |
165dc591 JD |
410 | tty->driver_data = line; |
411 | line->tty = tty; | |
165dc591 | 412 | |
d14ad81f | 413 | err = enable_chan(line); |
f71f9484 | 414 | if (err) /* line_close() will be called by our caller */ |
d8c215ad | 415 | goto out_unlock; |
d14ad81f | 416 | |
2f8a2dc2 | 417 | if (!line->sigio) { |
bed5e39c | 418 | chan_enable_winch(line->chan_out, tty); |
d79a5809 | 419 | line->sigio = 1; |
1da177e4 | 420 | } |
1da177e4 | 421 | |
bed5e39c | 422 | chan_window_size(line, &tty->winsize.ws_row, |
d79a5809 | 423 | &tty->winsize.ws_col); |
d79a5809 | 424 | out_unlock: |
d8c215ad | 425 | mutex_unlock(&line->count_lock); |
b97b77cc | 426 | return err; |
1da177e4 LT |
427 | } |
428 | ||
cd2ee4a3 JD |
429 | static void unregister_winch(struct tty_struct *tty); |
430 | ||
1da177e4 LT |
431 | void line_close(struct tty_struct *tty, struct file * filp) |
432 | { | |
433 | struct line *line = tty->driver_data; | |
434 | ||
2f8a2dc2 JD |
435 | /* |
436 | * If line_open fails (and tty->driver_data is never set), | |
d79a5809 JD |
437 | * tty_open will call line_close. So just return in this case. |
438 | */ | |
2f8a2dc2 | 439 | if (line == NULL) |
d79a5809 | 440 | return; |
b97b77cc PBG |
441 | |
442 | /* We ignore the error anyway! */ | |
443 | flush_buffer(line); | |
444 | ||
d8c215ad | 445 | mutex_lock(&line->count_lock); |
f71f9484 | 446 | BUG_ON(!line->valid); |
cd2ee4a3 | 447 | |
f71f9484 | 448 | if (--line->count) |
d79a5809 JD |
449 | goto out_unlock; |
450 | ||
d79a5809 JD |
451 | line->tty = NULL; |
452 | tty->driver_data = NULL; | |
453 | ||
2f8a2dc2 | 454 | if (line->sigio) { |
d79a5809 JD |
455 | unregister_winch(tty); |
456 | line->sigio = 0; | |
2f8a2dc2 | 457 | } |
cd2ee4a3 | 458 | |
d79a5809 | 459 | out_unlock: |
d8c215ad | 460 | mutex_unlock(&line->count_lock); |
1da177e4 LT |
461 | } |
462 | ||
463 | void close_lines(struct line *lines, int nlines) | |
464 | { | |
465 | int i; | |
466 | ||
467 | for(i = 0; i < nlines; i++) | |
10c890c0 | 468 | close_chan(&lines[i]); |
1da177e4 LT |
469 | } |
470 | ||
04292b2c AV |
471 | int setup_one_line(struct line *lines, int n, char *init, |
472 | const struct chan_opts *opts, char **error_out) | |
d79a5809 JD |
473 | { |
474 | struct line *line = &lines[n]; | |
cfe6b7c7 | 475 | struct tty_driver *driver = line->driver->driver; |
f28169d2 | 476 | int err = -EINVAL; |
d79a5809 | 477 | |
d8c215ad | 478 | mutex_lock(&line->count_lock); |
d79a5809 | 479 | |
f71f9484 | 480 | if (line->count) { |
f28169d2 | 481 | *error_out = "Device is already open"; |
d79a5809 JD |
482 | goto out; |
483 | } | |
484 | ||
31efcebb AV |
485 | if (!strcmp(init, "none")) { |
486 | if (line->valid) { | |
487 | line->valid = 0; | |
488 | kfree(line->init_str); | |
cfe6b7c7 | 489 | tty_unregister_device(driver, n); |
31efcebb AV |
490 | parse_chan_pair(NULL, line, n, opts, error_out); |
491 | err = 0; | |
492 | } | |
493 | } else { | |
494 | char *new = kstrdup(init, GFP_KERNEL); | |
495 | if (!new) { | |
496 | *error_out = "Failed to allocate memory"; | |
497 | return -ENOMEM; | |
498 | } | |
c8e2876f | 499 | if (line->valid) { |
cfe6b7c7 | 500 | tty_unregister_device(driver, n); |
c8e2876f AV |
501 | kfree(line->init_str); |
502 | } | |
31efcebb | 503 | line->init_str = new; |
43574c1a | 504 | line->valid = 1; |
31efcebb | 505 | err = parse_chan_pair(new, line, n, opts, error_out); |
cfe6b7c7 AV |
506 | if (!err) { |
507 | struct device *d = tty_register_device(driver, n, NULL); | |
508 | if (IS_ERR(d)) { | |
509 | *error_out = "Failed to register device"; | |
510 | err = PTR_ERR(d); | |
511 | parse_chan_pair(NULL, line, n, opts, error_out); | |
512 | } | |
513 | } | |
31efcebb AV |
514 | if (err) { |
515 | line->init_str = NULL; | |
516 | line->valid = 0; | |
517 | kfree(new); | |
518 | } | |
d79a5809 JD |
519 | } |
520 | out: | |
d8c215ad | 521 | mutex_unlock(&line->count_lock); |
f28169d2 | 522 | return err; |
d79a5809 JD |
523 | } |
524 | ||
2f8a2dc2 JD |
525 | /* |
526 | * Common setup code for both startup command line and mconsole initialization. | |
4b3f686d | 527 | * @lines contains the array (of size @num) to modify; |
b97b77cc | 528 | * @init is the setup string; |
f28169d2 | 529 | * @error_out is an error string in the case of failure; |
d571cd18 | 530 | */ |
b97b77cc | 531 | |
43574c1a AV |
532 | int line_setup(char **conf, unsigned int num, char **def, |
533 | char *init, char *name) | |
1da177e4 | 534 | { |
43574c1a | 535 | char *error; |
1da177e4 | 536 | |
2f8a2dc2 JD |
537 | if (*init == '=') { |
538 | /* | |
539 | * We said con=/ssl= instead of con#=, so we are configuring all | |
540 | * consoles at once. | |
541 | */ | |
43574c1a AV |
542 | *def = init + 1; |
543 | } else { | |
544 | char *end; | |
545 | unsigned n = simple_strtoul(init, &end, 0); | |
546 | ||
2f8a2dc2 | 547 | if (*end != '=') { |
43574c1a AV |
548 | error = "Couldn't parse device number"; |
549 | goto out; | |
1da177e4 | 550 | } |
43574c1a AV |
551 | if (n >= num) { |
552 | error = "Device number out of range"; | |
553 | goto out; | |
f28169d2 | 554 | } |
43574c1a | 555 | conf[n] = end + 1; |
1da177e4 | 556 | } |
43574c1a AV |
557 | return 0; |
558 | ||
559 | out: | |
560 | printk(KERN_ERR "Failed to set up %s with " | |
561 | "configuration string \"%s\" : %s\n", name, init, error); | |
562 | return -EINVAL; | |
1da177e4 LT |
563 | } |
564 | ||
1f80171e | 565 | int line_config(struct line *lines, unsigned int num, char *str, |
f28169d2 | 566 | const struct chan_opts *opts, char **error_out) |
1da177e4 | 567 | { |
fe9a6b00 | 568 | char *end; |
31efcebb | 569 | int n; |
1da177e4 | 570 | |
2f8a2dc2 | 571 | if (*str == '=') { |
f28169d2 JD |
572 | *error_out = "Can't configure all devices from mconsole"; |
573 | return -EINVAL; | |
d571cd18 JD |
574 | } |
575 | ||
fe9a6b00 AV |
576 | n = simple_strtoul(str, &end, 0); |
577 | if (*end++ != '=') { | |
578 | *error_out = "Couldn't parse device number"; | |
579 | return -EINVAL; | |
580 | } | |
581 | if (n >= num) { | |
582 | *error_out = "Device number out of range"; | |
583 | return -EINVAL; | |
584 | } | |
585 | ||
31efcebb | 586 | return setup_one_line(lines, n, end, opts, error_out); |
1da177e4 LT |
587 | } |
588 | ||
b97b77cc | 589 | int line_get_config(char *name, struct line *lines, unsigned int num, char *str, |
1da177e4 LT |
590 | int size, char **error_out) |
591 | { | |
592 | struct line *line; | |
593 | char *end; | |
594 | int dev, n = 0; | |
595 | ||
596 | dev = simple_strtoul(name, &end, 0); | |
2f8a2dc2 | 597 | if ((*end != '\0') || (end == name)) { |
1da177e4 | 598 | *error_out = "line_get_config failed to parse device number"; |
b97b77cc | 599 | return 0; |
1da177e4 LT |
600 | } |
601 | ||
2f8a2dc2 | 602 | if ((dev < 0) || (dev >= num)) { |
b97b77cc PBG |
603 | *error_out = "device number out of range"; |
604 | return 0; | |
1da177e4 LT |
605 | } |
606 | ||
607 | line = &lines[dev]; | |
608 | ||
d8c215ad | 609 | mutex_lock(&line->count_lock); |
2f8a2dc2 | 610 | if (!line->valid) |
1da177e4 | 611 | CONFIG_CHUNK(str, size, n, "none", 1); |
2f8a2dc2 | 612 | else if (line->tty == NULL) |
1da177e4 | 613 | CONFIG_CHUNK(str, size, n, line->init_str, 1); |
bed5e39c | 614 | else n = chan_config_string(line, str, size, error_out); |
d8c215ad | 615 | mutex_unlock(&line->count_lock); |
1da177e4 | 616 | |
b97b77cc | 617 | return n; |
1da177e4 LT |
618 | } |
619 | ||
29d56cfe JD |
620 | int line_id(char **str, int *start_out, int *end_out) |
621 | { | |
622 | char *end; | |
2f8a2dc2 | 623 | int n; |
29d56cfe JD |
624 | |
625 | n = simple_strtoul(*str, &end, 0); | |
2f8a2dc2 JD |
626 | if ((*end != '\0') || (end == *str)) |
627 | return -1; | |
29d56cfe | 628 | |
2f8a2dc2 JD |
629 | *str = end; |
630 | *start_out = n; | |
631 | *end_out = n; | |
632 | return n; | |
29d56cfe JD |
633 | } |
634 | ||
f28169d2 | 635 | int line_remove(struct line *lines, unsigned int num, int n, char **error_out) |
1da177e4 | 636 | { |
da645f3b AV |
637 | if (n >= num) { |
638 | *error_out = "Device number out of range"; | |
639 | return -EINVAL; | |
640 | } | |
31efcebb | 641 | return setup_one_line(lines, n, "none", NULL, error_out); |
1da177e4 LT |
642 | } |
643 | ||
cfe6b7c7 AV |
644 | int register_lines(struct line_driver *line_driver, |
645 | const struct tty_operations *ops, | |
646 | struct line *lines, int nlines) | |
1da177e4 | 647 | { |
1da177e4 | 648 | struct tty_driver *driver = alloc_tty_driver(nlines); |
cfe6b7c7 | 649 | int err; |
04292b2c | 650 | int i; |
1da177e4 LT |
651 | |
652 | if (!driver) | |
cfe6b7c7 | 653 | return -ENOMEM; |
1da177e4 LT |
654 | |
655 | driver->driver_name = line_driver->name; | |
656 | driver->name = line_driver->device_name; | |
1da177e4 LT |
657 | driver->major = line_driver->major; |
658 | driver->minor_start = line_driver->minor_start; | |
659 | driver->type = line_driver->type; | |
660 | driver->subtype = line_driver->subtype; | |
cfe6b7c7 | 661 | driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
1da177e4 | 662 | driver->init_termios = tty_std_termios; |
04292b2c AV |
663 | |
664 | for (i = 0; i < nlines; i++) { | |
665 | spin_lock_init(&lines[i].lock); | |
666 | mutex_init(&lines[i].count_lock); | |
667 | lines[i].driver = line_driver; | |
668 | INIT_LIST_HEAD(&lines[i].chan_list); | |
669 | } | |
1da177e4 LT |
670 | tty_set_operations(driver, ops); |
671 | ||
cfe6b7c7 AV |
672 | err = tty_register_driver(driver); |
673 | if (err) { | |
2f8a2dc2 JD |
674 | printk(KERN_ERR "register_lines : can't register %s driver\n", |
675 | line_driver->name); | |
1da177e4 | 676 | put_tty_driver(driver); |
cfe6b7c7 | 677 | return err; |
1da177e4 LT |
678 | } |
679 | ||
cfe6b7c7 | 680 | line_driver->driver = driver; |
1da177e4 | 681 | mconsole_register_dev(&line_driver->mc); |
cfe6b7c7 | 682 | return 0; |
1da177e4 LT |
683 | } |
684 | ||
9010772c JD |
685 | static DEFINE_SPINLOCK(winch_handler_lock); |
686 | static LIST_HEAD(winch_handlers); | |
605a69ac | 687 | |
1da177e4 LT |
688 | struct winch { |
689 | struct list_head list; | |
690 | int fd; | |
691 | int tty_fd; | |
692 | int pid; | |
693 | struct tty_struct *tty; | |
42a359e3 | 694 | unsigned long stack; |
7cf3cf21 | 695 | struct work_struct work; |
1da177e4 LT |
696 | }; |
697 | ||
7cf3cf21 | 698 | static void __free_winch(struct work_struct *work) |
42a359e3 | 699 | { |
7cf3cf21 AV |
700 | struct winch *winch = container_of(work, struct winch, work); |
701 | free_irq(WINCH_IRQ, winch); | |
42a359e3 JD |
702 | |
703 | if (winch->pid != -1) | |
704 | os_kill_process(winch->pid, 1); | |
42a359e3 JD |
705 | if (winch->stack != 0) |
706 | free_stack(winch->stack, 0); | |
42a359e3 JD |
707 | kfree(winch); |
708 | } | |
709 | ||
7cf3cf21 AV |
710 | static void free_winch(struct winch *winch) |
711 | { | |
712 | int fd = winch->fd; | |
713 | winch->fd = -1; | |
714 | if (fd != -1) | |
715 | os_close_file(fd); | |
716 | list_del(&winch->list); | |
717 | __free_winch(&winch->work); | |
718 | } | |
719 | ||
7bea96fd | 720 | static irqreturn_t winch_interrupt(int irq, void *data) |
1da177e4 LT |
721 | { |
722 | struct winch *winch = data; | |
723 | struct tty_struct *tty; | |
724 | struct line *line; | |
7cf3cf21 | 725 | int fd = winch->fd; |
1da177e4 LT |
726 | int err; |
727 | char c; | |
728 | ||
7cf3cf21 AV |
729 | if (fd != -1) { |
730 | err = generic_read(fd, &c, NULL); | |
2f8a2dc2 JD |
731 | if (err < 0) { |
732 | if (err != -EAGAIN) { | |
7cf3cf21 AV |
733 | winch->fd = -1; |
734 | list_del(&winch->list); | |
735 | os_close_file(fd); | |
2f8a2dc2 JD |
736 | printk(KERN_ERR "winch_interrupt : " |
737 | "read failed, errno = %d\n", -err); | |
738 | printk(KERN_ERR "fd %d is losing SIGWINCH " | |
739 | "support\n", winch->tty_fd); | |
7cf3cf21 AV |
740 | INIT_WORK(&winch->work, __free_winch); |
741 | schedule_work(&winch->work); | |
b97b77cc | 742 | return IRQ_HANDLED; |
1da177e4 LT |
743 | } |
744 | goto out; | |
745 | } | |
746 | } | |
42a359e3 | 747 | tty = winch->tty; |
1da177e4 LT |
748 | if (tty != NULL) { |
749 | line = tty->driver_data; | |
438ee679 | 750 | if (line != NULL) { |
bed5e39c | 751 | chan_window_size(line, &tty->winsize.ws_row, |
438ee679 JD |
752 | &tty->winsize.ws_col); |
753 | kill_pgrp(tty->pgrp, SIGWINCH, 1); | |
754 | } | |
1da177e4 LT |
755 | } |
756 | out: | |
2f8a2dc2 | 757 | if (winch->fd != -1) |
1da177e4 | 758 | reactivate_fd(winch->fd, WINCH_IRQ); |
b97b77cc | 759 | return IRQ_HANDLED; |
1da177e4 LT |
760 | } |
761 | ||
42a359e3 JD |
762 | void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty, |
763 | unsigned long stack) | |
1da177e4 LT |
764 | { |
765 | struct winch *winch; | |
766 | ||
1da177e4 LT |
767 | winch = kmalloc(sizeof(*winch), GFP_KERNEL); |
768 | if (winch == NULL) { | |
2f8a2dc2 | 769 | printk(KERN_ERR "register_winch_irq - kmalloc failed\n"); |
42a359e3 | 770 | goto cleanup; |
1da177e4 | 771 | } |
605a69ac | 772 | |
1da177e4 LT |
773 | *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list), |
774 | .fd = fd, | |
775 | .tty_fd = tty_fd, | |
776 | .pid = pid, | |
42a359e3 JD |
777 | .tty = tty, |
778 | .stack = stack }); | |
779 | ||
780 | if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, | |
c0b79a90 | 781 | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
42a359e3 | 782 | "winch", winch) < 0) { |
2f8a2dc2 JD |
783 | printk(KERN_ERR "register_winch_irq - failed to register " |
784 | "IRQ\n"); | |
42a359e3 JD |
785 | goto out_free; |
786 | } | |
605a69ac PBG |
787 | |
788 | spin_lock(&winch_handler_lock); | |
1da177e4 | 789 | list_add(&winch->list, &winch_handlers); |
605a69ac PBG |
790 | spin_unlock(&winch_handler_lock); |
791 | ||
42a359e3 | 792 | return; |
e464bf2b | 793 | |
42a359e3 | 794 | out_free: |
e464bf2b | 795 | kfree(winch); |
42a359e3 JD |
796 | cleanup: |
797 | os_kill_process(pid, 1); | |
798 | os_close_file(fd); | |
799 | if (stack != 0) | |
800 | free_stack(stack, 0); | |
e464bf2b JD |
801 | } |
802 | ||
cd2ee4a3 JD |
803 | static void unregister_winch(struct tty_struct *tty) |
804 | { | |
48a0b740 | 805 | struct list_head *ele, *next; |
e464bf2b | 806 | struct winch *winch; |
cd2ee4a3 | 807 | |
605a69ac | 808 | spin_lock(&winch_handler_lock); |
e464bf2b | 809 | |
48a0b740 | 810 | list_for_each_safe(ele, next, &winch_handlers) { |
cd2ee4a3 | 811 | winch = list_entry(ele, struct winch, list); |
2f8a2dc2 | 812 | if (winch->tty == tty) { |
7cf3cf21 | 813 | free_winch(winch); |
e464bf2b | 814 | break; |
2f8a2dc2 JD |
815 | } |
816 | } | |
605a69ac | 817 | spin_unlock(&winch_handler_lock); |
cd2ee4a3 JD |
818 | } |
819 | ||
1da177e4 LT |
820 | static void winch_cleanup(void) |
821 | { | |
e464bf2b | 822 | struct list_head *ele, *next; |
1da177e4 LT |
823 | struct winch *winch; |
824 | ||
e464bf2b JD |
825 | spin_lock(&winch_handler_lock); |
826 | ||
2f8a2dc2 | 827 | list_for_each_safe(ele, next, &winch_handlers) { |
1da177e4 | 828 | winch = list_entry(ele, struct winch, list); |
7cf3cf21 | 829 | free_winch(winch); |
1da177e4 | 830 | } |
e464bf2b JD |
831 | |
832 | spin_unlock(&winch_handler_lock); | |
1da177e4 LT |
833 | } |
834 | __uml_exitcall(winch_cleanup); | |
835 | ||
836 | char *add_xterm_umid(char *base) | |
837 | { | |
838 | char *umid, *title; | |
839 | int len; | |
840 | ||
7eebe8a9 | 841 | umid = get_umid(); |
2f8a2dc2 | 842 | if (*umid == '\0') |
b97b77cc | 843 | return base; |
165dc591 | 844 | |
1da177e4 LT |
845 | len = strlen(base) + strlen(" ()") + strlen(umid) + 1; |
846 | title = kmalloc(len, GFP_KERNEL); | |
2f8a2dc2 JD |
847 | if (title == NULL) { |
848 | printk(KERN_ERR "Failed to allocate buffer for xterm title\n"); | |
b97b77cc | 849 | return base; |
1da177e4 LT |
850 | } |
851 | ||
852 | snprintf(title, len, "%s (%s)", base, umid); | |
b97b77cc | 853 | return title; |
1da177e4 | 854 | } |