GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / um / drivers / line.c
blob345e8ea7f92a74bf07aba5c7d8288eb41b9ab542
1 /*
2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
6 #include "linux/irqreturn.h"
7 #include "linux/kd.h"
8 #include "linux/sched.h"
9 #include "linux/slab.h"
10 #include "chan_kern.h"
11 #include "irq_kern.h"
12 #include "irq_user.h"
13 #include "kern_util.h"
14 #include "os.h"
16 #define LINE_BUFSIZE 4096
18 static irqreturn_t line_interrupt(int irq, void *data)
20 struct chan *chan = data;
21 struct line *line = chan->line;
23 if (line)
24 chan_interrupt(&line->chan_list, &line->task, line->tty, irq);
25 return IRQ_HANDLED;
28 static void line_timer_cb(struct work_struct *work)
30 struct line *line = container_of(work, struct line, task.work);
32 if (!line->throttled)
33 chan_interrupt(&line->chan_list, &line->task, line->tty,
34 line->driver->read_irq);
38 * Returns the free space inside the ring buffer of this line.
40 * Should be called while holding line->lock (this does not modify data).
42 static int write_room(struct line *line)
44 int n;
46 if (line->buffer == NULL)
47 return LINE_BUFSIZE - 1;
49 /* This is for the case where the buffer is wrapped! */
50 n = line->head - line->tail;
52 if (n <= 0)
53 n += LINE_BUFSIZE; /* The other case */
54 return n - 1;
57 int line_write_room(struct tty_struct *tty)
59 struct line *line = tty->driver_data;
60 unsigned long flags;
61 int room;
63 spin_lock_irqsave(&line->lock, flags);
64 room = write_room(line);
65 spin_unlock_irqrestore(&line->lock, flags);
67 return room;
70 int line_chars_in_buffer(struct tty_struct *tty)
72 struct line *line = tty->driver_data;
73 unsigned long flags;
74 int ret;
76 spin_lock_irqsave(&line->lock, flags);
77 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
78 ret = LINE_BUFSIZE - (write_room(line) + 1);
79 spin_unlock_irqrestore(&line->lock, flags);
81 return ret;
85 * This copies the content of buf into the circular buffer associated with
86 * this line.
87 * The return value is the number of characters actually copied, i.e. the ones
88 * for which there was space: this function is not supposed to ever flush out
89 * the circular buffer.
91 * Must be called while holding line->lock!
93 static int buffer_data(struct line *line, const char *buf, int len)
95 int end, room;
97 if (line->buffer == NULL) {
98 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
99 if (line->buffer == NULL) {
100 printk(KERN_ERR "buffer_data - atomic allocation "
101 "failed\n");
102 return 0;
104 line->head = line->buffer;
105 line->tail = line->buffer;
108 room = write_room(line);
109 len = (len > room) ? room : len;
111 end = line->buffer + LINE_BUFSIZE - line->tail;
113 if (len < end) {
114 memcpy(line->tail, buf, len);
115 line->tail += len;
117 else {
118 /* The circular buffer is wrapping */
119 memcpy(line->tail, buf, end);
120 buf += end;
121 memcpy(line->buffer, buf, len - end);
122 line->tail = line->buffer + len - end;
125 return len;
129 * Flushes the ring buffer to the output channels. That is, write_chan is
130 * called, passing it line->head as buffer, and an appropriate count.
132 * On exit, returns 1 when the buffer is empty,
133 * 0 when the buffer is not empty on exit,
134 * and -errno when an error occurred.
136 * Must be called while holding line->lock!*/
137 static int flush_buffer(struct line *line)
139 int n, count;
141 if ((line->buffer == NULL) || (line->head == line->tail))
142 return 1;
144 if (line->tail < line->head) {
145 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
146 count = line->buffer + LINE_BUFSIZE - line->head;
148 n = write_chan(&line->chan_list, line->head, count,
149 line->driver->write_irq);
150 if (n < 0)
151 return n;
152 if (n == count) {
154 * We have flushed from ->head to buffer end, now we
155 * must flush only from the beginning to ->tail.
157 line->head = line->buffer;
158 } else {
159 line->head += n;
160 return 0;
164 count = line->tail - line->head;
165 n = write_chan(&line->chan_list, line->head, count,
166 line->driver->write_irq);
168 if (n < 0)
169 return n;
171 line->head += n;
172 return line->head == line->tail;
175 void line_flush_buffer(struct tty_struct *tty)
177 struct line *line = tty->driver_data;
178 unsigned long flags;
179 int err;
181 spin_lock_irqsave(&line->lock, flags);
182 err = flush_buffer(line);
183 spin_unlock_irqrestore(&line->lock, flags);
187 * We map both ->flush_chars and ->put_char (which go in pair) onto
188 * ->flush_buffer and ->write. Hope it's not that bad.
190 void line_flush_chars(struct tty_struct *tty)
192 line_flush_buffer(tty);
195 int line_put_char(struct tty_struct *tty, unsigned char ch)
197 return line_write(tty, &ch, sizeof(ch));
200 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
202 struct line *line = tty->driver_data;
203 unsigned long flags;
204 int n, ret = 0;
206 spin_lock_irqsave(&line->lock, flags);
207 if (line->head != line->tail)
208 ret = buffer_data(line, buf, len);
209 else {
210 n = write_chan(&line->chan_list, buf, len,
211 line->driver->write_irq);
212 if (n < 0) {
213 ret = n;
214 goto out_up;
217 len -= n;
218 ret += n;
219 if (len > 0)
220 ret += buffer_data(line, buf + n, len);
222 out_up:
223 spin_unlock_irqrestore(&line->lock, flags);
224 return ret;
227 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
229 /* nothing */
232 static const struct {
233 int cmd;
234 char *level;
235 char *name;
236 } tty_ioctls[] = {
237 /* don't print these, they flood the log ... */
238 { TCGETS, NULL, "TCGETS" },
239 { TCSETS, NULL, "TCSETS" },
240 { TCSETSW, NULL, "TCSETSW" },
241 { TCFLSH, NULL, "TCFLSH" },
242 { TCSBRK, NULL, "TCSBRK" },
244 /* general tty stuff */
245 { TCSETSF, KERN_DEBUG, "TCSETSF" },
246 { TCGETA, KERN_DEBUG, "TCGETA" },
247 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
248 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
249 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
251 /* linux-specific ones */
252 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
253 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
254 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
255 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
258 int line_ioctl(struct tty_struct *tty, struct file * file,
259 unsigned int cmd, unsigned long arg)
261 int ret;
262 int i;
264 ret = 0;
265 switch(cmd) {
266 #ifdef TIOCGETP
267 case TIOCGETP:
268 case TIOCSETP:
269 case TIOCSETN:
270 #endif
271 #ifdef TIOCGETC
272 case TIOCGETC:
273 case TIOCSETC:
274 #endif
275 #ifdef TIOCGLTC
276 case TIOCGLTC:
277 case TIOCSLTC:
278 #endif
279 /* Note: these are out of date as we now have TCGETS2 etc but this
280 whole lot should probably go away */
281 case TCGETS:
282 case TCSETSF:
283 case TCSETSW:
284 case TCSETS:
285 case TCGETA:
286 case TCSETAF:
287 case TCSETAW:
288 case TCSETA:
289 case TCXONC:
290 case TCFLSH:
291 case TIOCOUTQ:
292 case TIOCINQ:
293 case TIOCGLCKTRMIOS:
294 case TIOCSLCKTRMIOS:
295 case TIOCPKT:
296 case TIOCGSOFTCAR:
297 case TIOCSSOFTCAR:
298 return -ENOIOCTLCMD;
299 default:
300 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
301 if (cmd == tty_ioctls[i].cmd)
302 break;
303 if (i == ARRAY_SIZE(tty_ioctls)) {
304 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
305 __func__, tty->name, cmd);
307 ret = -ENOIOCTLCMD;
308 break;
310 return ret;
313 void line_throttle(struct tty_struct *tty)
315 struct line *line = tty->driver_data;
317 deactivate_chan(&line->chan_list, line->driver->read_irq);
318 line->throttled = 1;
321 void line_unthrottle(struct tty_struct *tty)
323 struct line *line = tty->driver_data;
325 line->throttled = 0;
326 chan_interrupt(&line->chan_list, &line->task, tty,
327 line->driver->read_irq);
330 * Maybe there is enough stuff pending that calling the interrupt
331 * throttles us again. In this case, line->throttled will be 1
332 * again and we shouldn't turn the interrupt back on.
334 if (!line->throttled)
335 reactivate_chan(&line->chan_list, line->driver->read_irq);
338 static irqreturn_t line_write_interrupt(int irq, void *data)
340 struct chan *chan = data;
341 struct line *line = chan->line;
342 struct tty_struct *tty = line->tty;
343 int err;
346 * Interrupts are disabled here because we registered the interrupt with
347 * IRQF_DISABLED (see line_setup_irq).
350 spin_lock(&line->lock);
351 err = flush_buffer(line);
352 if (err == 0) {
353 return IRQ_NONE;
354 } else if (err < 0) {
355 line->head = line->buffer;
356 line->tail = line->buffer;
358 spin_unlock(&line->lock);
360 if (tty == NULL)
361 return IRQ_NONE;
363 tty_wakeup(tty);
364 return IRQ_HANDLED;
367 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
369 const struct line_driver *driver = line->driver;
370 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
372 if (input)
373 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
374 line_interrupt, flags,
375 driver->read_irq_name, data);
376 if (err)
377 return err;
378 if (output)
379 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
380 line_write_interrupt, flags,
381 driver->write_irq_name, data);
382 line->have_irq = 1;
383 return err;
387 * Normally, a driver like this can rely mostly on the tty layer
388 * locking, particularly when it comes to the driver structure.
389 * However, in this case, mconsole requests can come in "from the
390 * side", and race with opens and closes.
392 * mconsole config requests will want to be sure the device isn't in
393 * use, and get_config, open, and close will want a stable
394 * configuration. The checking and modification of the configuration
395 * is done under a spinlock. Checking whether the device is in use is
396 * line->tty->count > 1, also under the spinlock.
398 * tty->count serves to decide whether the device should be enabled or
399 * disabled on the host. If it's equal to 1, then we are doing the
400 * first open or last close. Otherwise, open and close just return.
403 int line_open(struct line *lines, struct tty_struct *tty)
405 struct line *line = &lines[tty->index];
406 int err = -ENODEV;
408 spin_lock(&line->count_lock);
409 if (!line->valid)
410 goto out_unlock;
412 err = 0;
413 if (tty->count > 1)
414 goto out_unlock;
416 spin_unlock(&line->count_lock);
418 tty->driver_data = line;
419 line->tty = tty;
421 err = enable_chan(line);
422 if (err)
423 return err;
425 INIT_DELAYED_WORK(&line->task, line_timer_cb);
427 if (!line->sigio) {
428 chan_enable_winch(&line->chan_list, tty);
429 line->sigio = 1;
432 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
433 &tty->winsize.ws_col);
435 return err;
437 out_unlock:
438 spin_unlock(&line->count_lock);
439 return err;
442 static void unregister_winch(struct tty_struct *tty);
444 void line_close(struct tty_struct *tty, struct file * filp)
446 struct line *line = tty->driver_data;
449 * If line_open fails (and tty->driver_data is never set),
450 * tty_open will call line_close. So just return in this case.
452 if (line == NULL)
453 return;
455 /* We ignore the error anyway! */
456 flush_buffer(line);
458 spin_lock(&line->count_lock);
459 if (!line->valid)
460 goto out_unlock;
462 if (tty->count > 1)
463 goto out_unlock;
465 spin_unlock(&line->count_lock);
467 line->tty = NULL;
468 tty->driver_data = NULL;
470 if (line->sigio) {
471 unregister_winch(tty);
472 line->sigio = 0;
475 return;
477 out_unlock:
478 spin_unlock(&line->count_lock);
481 void close_lines(struct line *lines, int nlines)
483 int i;
485 for(i = 0; i < nlines; i++)
486 close_chan(&lines[i].chan_list, 0);
489 static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
490 char **error_out)
492 struct line *line = &lines[n];
493 int err = -EINVAL;
495 spin_lock(&line->count_lock);
497 if (line->tty != NULL) {
498 *error_out = "Device is already open";
499 goto out;
502 if (line->init_pri <= init_prio) {
503 line->init_pri = init_prio;
504 if (!strcmp(init, "none"))
505 line->valid = 0;
506 else {
507 line->init_str = init;
508 line->valid = 1;
511 err = 0;
512 out:
513 spin_unlock(&line->count_lock);
514 return err;
518 * Common setup code for both startup command line and mconsole initialization.
519 * @lines contains the array (of size @num) to modify;
520 * @init is the setup string;
521 * @error_out is an error string in the case of failure;
524 int line_setup(struct line *lines, unsigned int num, char *init,
525 char **error_out)
527 int i, n, err;
528 char *end;
530 if (*init == '=') {
532 * We said con=/ssl= instead of con#=, so we are configuring all
533 * consoles at once.
535 n = -1;
537 else {
538 n = simple_strtoul(init, &end, 0);
539 if (*end != '=') {
540 *error_out = "Couldn't parse device number";
541 return -EINVAL;
543 init = end;
545 init++;
547 if (n >= (signed int) num) {
548 *error_out = "Device number out of range";
549 return -EINVAL;
551 else if (n >= 0) {
552 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
553 if (err)
554 return err;
556 else {
557 for(i = 0; i < num; i++) {
558 err = setup_one_line(lines, i, init, INIT_ALL,
559 error_out);
560 if (err)
561 return err;
564 return n == -1 ? num : n;
567 int line_config(struct line *lines, unsigned int num, char *str,
568 const struct chan_opts *opts, char **error_out)
570 struct line *line;
571 char *new;
572 int n;
574 if (*str == '=') {
575 *error_out = "Can't configure all devices from mconsole";
576 return -EINVAL;
579 new = kstrdup(str, GFP_KERNEL);
580 if (new == NULL) {
581 *error_out = "Failed to allocate memory";
582 return -ENOMEM;
584 n = line_setup(lines, num, new, error_out);
585 if (n < 0)
586 return n;
588 line = &lines[n];
589 return parse_chan_pair(line->init_str, line, n, opts, error_out);
592 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
593 int size, char **error_out)
595 struct line *line;
596 char *end;
597 int dev, n = 0;
599 dev = simple_strtoul(name, &end, 0);
600 if ((*end != '\0') || (end == name)) {
601 *error_out = "line_get_config failed to parse device number";
602 return 0;
605 if ((dev < 0) || (dev >= num)) {
606 *error_out = "device number out of range";
607 return 0;
610 line = &lines[dev];
612 spin_lock(&line->count_lock);
613 if (!line->valid)
614 CONFIG_CHUNK(str, size, n, "none", 1);
615 else if (line->tty == NULL)
616 CONFIG_CHUNK(str, size, n, line->init_str, 1);
617 else n = chan_config_string(&line->chan_list, str, size, error_out);
618 spin_unlock(&line->count_lock);
620 return n;
623 int line_id(char **str, int *start_out, int *end_out)
625 char *end;
626 int n;
628 n = simple_strtoul(*str, &end, 0);
629 if ((*end != '\0') || (end == *str))
630 return -1;
632 *str = end;
633 *start_out = n;
634 *end_out = n;
635 return n;
638 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
640 int err;
641 char config[sizeof("conxxxx=none\0")];
643 sprintf(config, "%d=none", n);
644 err = line_setup(lines, num, config, error_out);
645 if (err >= 0)
646 err = 0;
647 return err;
650 struct tty_driver *register_lines(struct line_driver *line_driver,
651 const struct tty_operations *ops,
652 struct line *lines, int nlines)
654 int i;
655 struct tty_driver *driver = alloc_tty_driver(nlines);
657 if (!driver)
658 return NULL;
660 driver->driver_name = line_driver->name;
661 driver->name = line_driver->device_name;
662 driver->major = line_driver->major;
663 driver->minor_start = line_driver->minor_start;
664 driver->type = line_driver->type;
665 driver->subtype = line_driver->subtype;
666 driver->flags = TTY_DRIVER_REAL_RAW;
667 driver->init_termios = tty_std_termios;
668 tty_set_operations(driver, ops);
670 if (tty_register_driver(driver)) {
671 printk(KERN_ERR "register_lines : can't register %s driver\n",
672 line_driver->name);
673 put_tty_driver(driver);
674 return NULL;
677 for(i = 0; i < nlines; i++) {
678 if (!lines[i].valid)
679 tty_unregister_device(driver, i);
682 mconsole_register_dev(&line_driver->mc);
683 return driver;
686 static DEFINE_SPINLOCK(winch_handler_lock);
687 static LIST_HEAD(winch_handlers);
689 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
691 struct line *line;
692 char *error;
693 int i;
695 for(i = 0; i < nlines; i++) {
696 line = &lines[i];
697 INIT_LIST_HEAD(&line->chan_list);
699 if (line->init_str == NULL)
700 continue;
702 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
703 if (line->init_str == NULL)
704 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
706 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
707 printk(KERN_ERR "parse_chan_pair failed for "
708 "device %d : %s\n", i, error);
709 line->valid = 0;
714 struct winch {
715 struct list_head list;
716 int fd;
717 int tty_fd;
718 int pid;
719 struct tty_struct *tty;
720 unsigned long stack;
723 static void free_winch(struct winch *winch, int free_irq_ok)
725 if (free_irq_ok)
726 free_irq(WINCH_IRQ, winch);
728 list_del(&winch->list);
730 if (winch->pid != -1)
731 os_kill_process(winch->pid, 1);
732 if (winch->fd != -1)
733 os_close_file(winch->fd);
734 if (winch->stack != 0)
735 free_stack(winch->stack, 0);
736 kfree(winch);
739 static irqreturn_t winch_interrupt(int irq, void *data)
741 struct winch *winch = data;
742 struct tty_struct *tty;
743 struct line *line;
744 int err;
745 char c;
747 if (winch->fd != -1) {
748 err = generic_read(winch->fd, &c, NULL);
749 if (err < 0) {
750 if (err != -EAGAIN) {
751 printk(KERN_ERR "winch_interrupt : "
752 "read failed, errno = %d\n", -err);
753 printk(KERN_ERR "fd %d is losing SIGWINCH "
754 "support\n", winch->tty_fd);
755 free_winch(winch, 0);
756 return IRQ_HANDLED;
758 goto out;
761 tty = winch->tty;
762 if (tty != NULL) {
763 line = tty->driver_data;
764 if (line != NULL) {
765 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
766 &tty->winsize.ws_col);
767 kill_pgrp(tty->pgrp, SIGWINCH, 1);
770 out:
771 if (winch->fd != -1)
772 reactivate_fd(winch->fd, WINCH_IRQ);
773 return IRQ_HANDLED;
776 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
777 unsigned long stack)
779 struct winch *winch;
781 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
782 if (winch == NULL) {
783 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
784 goto cleanup;
787 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
788 .fd = fd,
789 .tty_fd = tty_fd,
790 .pid = pid,
791 .tty = tty,
792 .stack = stack });
794 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
795 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
796 "winch", winch) < 0) {
797 printk(KERN_ERR "register_winch_irq - failed to register "
798 "IRQ\n");
799 goto out_free;
802 spin_lock(&winch_handler_lock);
803 list_add(&winch->list, &winch_handlers);
804 spin_unlock(&winch_handler_lock);
806 return;
808 out_free:
809 kfree(winch);
810 cleanup:
811 os_kill_process(pid, 1);
812 os_close_file(fd);
813 if (stack != 0)
814 free_stack(stack, 0);
817 static void unregister_winch(struct tty_struct *tty)
819 struct list_head *ele;
820 struct winch *winch;
822 spin_lock(&winch_handler_lock);
824 list_for_each(ele, &winch_handlers) {
825 winch = list_entry(ele, struct winch, list);
826 if (winch->tty == tty) {
827 free_winch(winch, 1);
828 break;
831 spin_unlock(&winch_handler_lock);
834 static void winch_cleanup(void)
836 struct list_head *ele, *next;
837 struct winch *winch;
839 spin_lock(&winch_handler_lock);
841 list_for_each_safe(ele, next, &winch_handlers) {
842 winch = list_entry(ele, struct winch, list);
843 free_winch(winch, 1);
846 spin_unlock(&winch_handler_lock);
848 __uml_exitcall(winch_cleanup);
850 char *add_xterm_umid(char *base)
852 char *umid, *title;
853 int len;
855 umid = get_umid();
856 if (*umid == '\0')
857 return base;
859 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
860 title = kmalloc(len, GFP_KERNEL);
861 if (title == NULL) {
862 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
863 return base;
866 snprintf(title, len, "%s (%s)", base, umid);
867 return title;