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