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