m68knommu: remove local gettimeofday code
[wrt350n-kernel.git] / arch / um / drivers / line.c
blob83bf15a3dda88472dc5aea5ade3353e3f772e0e2
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 "chan_kern.h"
9 #include "irq_kern.h"
10 #include "irq_user.h"
11 #include "os.h"
13 #define LINE_BUFSIZE 4096
15 static irqreturn_t line_interrupt(int irq, void *data)
17 struct chan *chan = data;
18 struct line *line = chan->line;
19 struct tty_struct *tty = line->tty;
21 if (line)
22 chan_interrupt(&line->chan_list, &line->task, tty, irq);
23 return IRQ_HANDLED;
26 static void line_timer_cb(struct work_struct *work)
28 struct line *line = container_of(work, struct line, task.work);
30 if (!line->throttled)
31 chan_interrupt(&line->chan_list, &line->task, line->tty,
32 line->driver->read_irq);
36 * Returns the free space inside the ring buffer of this line.
38 * Should be called while holding line->lock (this does not modify data).
40 static int write_room(struct line *line)
42 int n;
44 if (line->buffer == NULL)
45 return LINE_BUFSIZE - 1;
47 /* This is for the case where the buffer is wrapped! */
48 n = line->head - line->tail;
50 if (n <= 0)
51 n = LINE_BUFSIZE + n; /* The other case */
52 return n - 1;
55 int line_write_room(struct tty_struct *tty)
57 struct line *line = tty->driver_data;
58 unsigned long flags;
59 int room;
61 if (tty->stopped)
62 return 0;
64 spin_lock_irqsave(&line->lock, flags);
65 room = write_room(line);
66 spin_unlock_irqrestore(&line->lock, flags);
68 /*XXX: Warning to remove */
69 if (0 == room)
70 printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
71 __FUNCTION__,tty->name);
72 return room;
75 int line_chars_in_buffer(struct tty_struct *tty)
77 struct line *line = tty->driver_data;
78 unsigned long flags;
79 int ret;
81 spin_lock_irqsave(&line->lock, flags);
83 /*write_room subtracts 1 for the needed NULL, so we readd it.*/
84 ret = LINE_BUFSIZE - (write_room(line) + 1);
85 spin_unlock_irqrestore(&line->lock, flags);
87 return ret;
91 * This copies the content of buf into the circular buffer associated with
92 * this line.
93 * The return value is the number of characters actually copied, i.e. the ones
94 * for which there was space: this function is not supposed to ever flush out
95 * the circular buffer.
97 * Must be called while holding line->lock!
99 static int buffer_data(struct line *line, const char *buf, int len)
101 int end, room;
103 if (line->buffer == NULL) {
104 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
105 if (line->buffer == NULL) {
106 printk(KERN_ERR "buffer_data - atomic allocation "
107 "failed\n");
108 return 0;
110 line->head = line->buffer;
111 line->tail = line->buffer;
114 room = write_room(line);
115 len = (len > room) ? room : len;
117 end = line->buffer + LINE_BUFSIZE - line->tail;
119 if (len < end) {
120 memcpy(line->tail, buf, len);
121 line->tail += len;
123 else {
124 /* The circular buffer is wrapping */
125 memcpy(line->tail, buf, end);
126 buf += end;
127 memcpy(line->buffer, buf, len - end);
128 line->tail = line->buffer + len - end;
131 return len;
135 * Flushes the ring buffer to the output channels. That is, write_chan is
136 * called, passing it line->head as buffer, and an appropriate count.
138 * On exit, returns 1 when the buffer is empty,
139 * 0 when the buffer is not empty on exit,
140 * and -errno when an error occurred.
142 * Must be called while holding line->lock!*/
143 static int flush_buffer(struct line *line)
145 int n, count;
147 if ((line->buffer == NULL) || (line->head == line->tail))
148 return 1;
150 if (line->tail < line->head) {
151 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
152 count = line->buffer + LINE_BUFSIZE - line->head;
154 n = write_chan(&line->chan_list, line->head, count,
155 line->driver->write_irq);
156 if (n < 0)
157 return n;
158 if (n == count) {
160 * We have flushed from ->head to buffer end, now we
161 * must flush only from the beginning to ->tail.
163 line->head = line->buffer;
164 } else {
165 line->head += n;
166 return 0;
170 count = line->tail - line->head;
171 n = write_chan(&line->chan_list, line->head, count,
172 line->driver->write_irq);
174 if (n < 0)
175 return n;
177 line->head += n;
178 return line->head == line->tail;
181 void line_flush_buffer(struct tty_struct *tty)
183 struct line *line = tty->driver_data;
184 unsigned long flags;
185 int err;
187 /*XXX: copied from line_write, verify if it is correct!*/
188 if (tty->stopped)
189 return;
191 spin_lock_irqsave(&line->lock, flags);
192 err = flush_buffer(line);
193 spin_unlock_irqrestore(&line->lock, flags);
197 * We map both ->flush_chars and ->put_char (which go in pair) onto
198 * ->flush_buffer and ->write. Hope it's not that bad.
200 void line_flush_chars(struct tty_struct *tty)
202 line_flush_buffer(tty);
205 void line_put_char(struct tty_struct *tty, unsigned char ch)
207 line_write(tty, &ch, sizeof(ch));
210 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
212 struct line *line = tty->driver_data;
213 unsigned long flags;
214 int n, ret = 0;
216 if (tty->stopped)
217 return 0;
219 spin_lock_irqsave(&line->lock, flags);
220 if (line->head != line->tail)
221 ret = buffer_data(line, buf, len);
222 else {
223 n = write_chan(&line->chan_list, buf, len,
224 line->driver->write_irq);
225 if (n < 0) {
226 ret = n;
227 goto out_up;
230 len -= n;
231 ret += n;
232 if (len > 0)
233 ret += buffer_data(line, buf + n, len);
235 out_up:
236 spin_unlock_irqrestore(&line->lock, flags);
237 return ret;
240 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
242 /* nothing */
245 static const struct {
246 int cmd;
247 char *level;
248 char *name;
249 } tty_ioctls[] = {
250 /* don't print these, they flood the log ... */
251 { TCGETS, NULL, "TCGETS" },
252 { TCSETS, NULL, "TCSETS" },
253 { TCSETSW, NULL, "TCSETSW" },
254 { TCFLSH, NULL, "TCFLSH" },
255 { TCSBRK, NULL, "TCSBRK" },
257 /* general tty stuff */
258 { TCSETSF, KERN_DEBUG, "TCSETSF" },
259 { TCGETA, KERN_DEBUG, "TCGETA" },
260 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
261 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
262 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
264 /* linux-specific ones */
265 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
266 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
267 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
268 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
271 int line_ioctl(struct tty_struct *tty, struct file * file,
272 unsigned int cmd, unsigned long arg)
274 int ret;
275 int i;
277 ret = 0;
278 switch(cmd) {
279 #ifdef TIOCGETP
280 case TIOCGETP:
281 case TIOCSETP:
282 case TIOCSETN:
283 #endif
284 #ifdef TIOCGETC
285 case TIOCGETC:
286 case TIOCSETC:
287 #endif
288 #ifdef TIOCGLTC
289 case TIOCGLTC:
290 case TIOCSLTC:
291 #endif
292 case TCGETS:
293 case TCSETSF:
294 case TCSETSW:
295 case TCSETS:
296 case TCGETA:
297 case TCSETAF:
298 case TCSETAW:
299 case TCSETA:
300 case TCXONC:
301 case TCFLSH:
302 case TIOCOUTQ:
303 case TIOCINQ:
304 case TIOCGLCKTRMIOS:
305 case TIOCSLCKTRMIOS:
306 case TIOCPKT:
307 case TIOCGSOFTCAR:
308 case TIOCSSOFTCAR:
309 return -ENOIOCTLCMD;
310 #if 0
311 case TCwhatever:
312 /* do something */
313 break;
314 #endif
315 default:
316 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
317 if (cmd == tty_ioctls[i].cmd)
318 break;
319 if (i == ARRAY_SIZE(tty_ioctls)) {
320 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
321 __FUNCTION__, tty->name, cmd);
323 ret = -ENOIOCTLCMD;
324 break;
326 return ret;
329 void line_throttle(struct tty_struct *tty)
331 struct line *line = tty->driver_data;
333 deactivate_chan(&line->chan_list, line->driver->read_irq);
334 line->throttled = 1;
337 void line_unthrottle(struct tty_struct *tty)
339 struct line *line = tty->driver_data;
341 line->throttled = 0;
342 chan_interrupt(&line->chan_list, &line->task, tty,
343 line->driver->read_irq);
346 * Maybe there is enough stuff pending that calling the interrupt
347 * throttles us again. In this case, line->throttled will be 1
348 * again and we shouldn't turn the interrupt back on.
350 if (!line->throttled)
351 reactivate_chan(&line->chan_list, line->driver->read_irq);
354 static irqreturn_t line_write_interrupt(int irq, void *data)
356 struct chan *chan = data;
357 struct line *line = chan->line;
358 struct tty_struct *tty = line->tty;
359 int err;
362 * Interrupts are disabled here because we registered the interrupt with
363 * IRQF_DISABLED (see line_setup_irq).
366 spin_lock(&line->lock);
367 err = flush_buffer(line);
368 if (err == 0) {
369 return IRQ_NONE;
370 } else if (err < 0) {
371 line->head = line->buffer;
372 line->tail = line->buffer;
374 spin_unlock(&line->lock);
376 if (tty == NULL)
377 return IRQ_NONE;
379 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
380 (tty->ldisc.write_wakeup != NULL))
381 (tty->ldisc.write_wakeup)(tty);
384 * BLOCKING mode
385 * In blocking mode, everything sleeps on tty->write_wait.
386 * Sleeping in the console driver would break non-blocking
387 * writes.
390 if (waitqueue_active(&tty->write_wait))
391 wake_up_interruptible(&tty->write_wait);
392 return IRQ_HANDLED;
395 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
397 const struct line_driver *driver = line->driver;
398 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
400 if (input)
401 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
402 line_interrupt, flags,
403 driver->read_irq_name, data);
404 if (err)
405 return err;
406 if (output)
407 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
408 line_write_interrupt, flags,
409 driver->write_irq_name, data);
410 line->have_irq = 1;
411 return err;
415 * Normally, a driver like this can rely mostly on the tty layer
416 * locking, particularly when it comes to the driver structure.
417 * However, in this case, mconsole requests can come in "from the
418 * side", and race with opens and closes.
420 * mconsole config requests will want to be sure the device isn't in
421 * use, and get_config, open, and close will want a stable
422 * configuration. The checking and modification of the configuration
423 * is done under a spinlock. Checking whether the device is in use is
424 * line->tty->count > 1, also under the spinlock.
426 * tty->count serves to decide whether the device should be enabled or
427 * disabled on the host. If it's equal to 1, then we are doing the
428 * first open or last close. Otherwise, open and close just return.
431 int line_open(struct line *lines, struct tty_struct *tty)
433 struct line *line = &lines[tty->index];
434 int err = -ENODEV;
436 spin_lock(&line->count_lock);
437 if (!line->valid)
438 goto out_unlock;
440 err = 0;
441 if (tty->count > 1)
442 goto out_unlock;
444 spin_unlock(&line->count_lock);
446 tty->driver_data = line;
447 line->tty = tty;
449 err = enable_chan(line);
450 if (err)
451 return err;
453 INIT_DELAYED_WORK(&line->task, line_timer_cb);
455 if (!line->sigio) {
456 chan_enable_winch(&line->chan_list, tty);
457 line->sigio = 1;
460 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
461 &tty->winsize.ws_col);
463 return err;
465 out_unlock:
466 spin_unlock(&line->count_lock);
467 return err;
470 static void unregister_winch(struct tty_struct *tty);
472 void line_close(struct tty_struct *tty, struct file * filp)
474 struct line *line = tty->driver_data;
477 * If line_open fails (and tty->driver_data is never set),
478 * tty_open will call line_close. So just return in this case.
480 if (line == NULL)
481 return;
483 /* We ignore the error anyway! */
484 flush_buffer(line);
486 spin_lock(&line->count_lock);
487 if (!line->valid)
488 goto out_unlock;
490 if (tty->count > 1)
491 goto out_unlock;
493 spin_unlock(&line->count_lock);
495 line->tty = NULL;
496 tty->driver_data = NULL;
498 if (line->sigio) {
499 unregister_winch(tty);
500 line->sigio = 0;
503 return;
505 out_unlock:
506 spin_unlock(&line->count_lock);
509 void close_lines(struct line *lines, int nlines)
511 int i;
513 for(i = 0; i < nlines; i++)
514 close_chan(&lines[i].chan_list, 0);
517 static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
518 char **error_out)
520 struct line *line = &lines[n];
521 int err = -EINVAL;
523 spin_lock(&line->count_lock);
525 if (line->tty != NULL) {
526 *error_out = "Device is already open";
527 goto out;
530 if (line->init_pri <= init_prio) {
531 line->init_pri = init_prio;
532 if (!strcmp(init, "none"))
533 line->valid = 0;
534 else {
535 line->init_str = init;
536 line->valid = 1;
539 err = 0;
540 out:
541 spin_unlock(&line->count_lock);
542 return err;
546 * Common setup code for both startup command line and mconsole initialization.
547 * @lines contains the array (of size @num) to modify;
548 * @init is the setup string;
549 * @error_out is an error string in the case of failure;
552 int line_setup(struct line *lines, unsigned int num, char *init,
553 char **error_out)
555 int i, n, err;
556 char *end;
558 if (*init == '=') {
560 * We said con=/ssl= instead of con#=, so we are configuring all
561 * consoles at once.
563 n = -1;
565 else {
566 n = simple_strtoul(init, &end, 0);
567 if (*end != '=') {
568 *error_out = "Couldn't parse device number";
569 return -EINVAL;
571 init = end;
573 init++;
575 if (n >= (signed int) num) {
576 *error_out = "Device number out of range";
577 return -EINVAL;
579 else if (n >= 0) {
580 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
581 if (err)
582 return err;
584 else {
585 for(i = 0; i < num; i++) {
586 err = setup_one_line(lines, i, init, INIT_ALL,
587 error_out);
588 if (err)
589 return err;
592 return n == -1 ? num : n;
595 int line_config(struct line *lines, unsigned int num, char *str,
596 const struct chan_opts *opts, char **error_out)
598 struct line *line;
599 char *new;
600 int n;
602 if (*str == '=') {
603 *error_out = "Can't configure all devices from mconsole";
604 return -EINVAL;
607 new = kstrdup(str, GFP_KERNEL);
608 if (new == NULL) {
609 *error_out = "Failed to allocate memory";
610 return -ENOMEM;
612 n = line_setup(lines, num, new, error_out);
613 if (n < 0)
614 return n;
616 line = &lines[n];
617 return parse_chan_pair(line->init_str, line, n, opts, error_out);
620 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
621 int size, char **error_out)
623 struct line *line;
624 char *end;
625 int dev, n = 0;
627 dev = simple_strtoul(name, &end, 0);
628 if ((*end != '\0') || (end == name)) {
629 *error_out = "line_get_config failed to parse device number";
630 return 0;
633 if ((dev < 0) || (dev >= num)) {
634 *error_out = "device number out of range";
635 return 0;
638 line = &lines[dev];
640 spin_lock(&line->count_lock);
641 if (!line->valid)
642 CONFIG_CHUNK(str, size, n, "none", 1);
643 else if (line->tty == NULL)
644 CONFIG_CHUNK(str, size, n, line->init_str, 1);
645 else n = chan_config_string(&line->chan_list, str, size, error_out);
646 spin_unlock(&line->count_lock);
648 return n;
651 int line_id(char **str, int *start_out, int *end_out)
653 char *end;
654 int n;
656 n = simple_strtoul(*str, &end, 0);
657 if ((*end != '\0') || (end == *str))
658 return -1;
660 *str = end;
661 *start_out = n;
662 *end_out = n;
663 return n;
666 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
668 int err;
669 char config[sizeof("conxxxx=none\0")];
671 sprintf(config, "%d=none", n);
672 err = line_setup(lines, num, config, error_out);
673 if (err >= 0)
674 err = 0;
675 return err;
678 struct tty_driver *register_lines(struct line_driver *line_driver,
679 const struct tty_operations *ops,
680 struct line *lines, int nlines)
682 int i;
683 struct tty_driver *driver = alloc_tty_driver(nlines);
685 if (!driver)
686 return NULL;
688 driver->driver_name = line_driver->name;
689 driver->name = line_driver->device_name;
690 driver->major = line_driver->major;
691 driver->minor_start = line_driver->minor_start;
692 driver->type = line_driver->type;
693 driver->subtype = line_driver->subtype;
694 driver->flags = TTY_DRIVER_REAL_RAW;
695 driver->init_termios = tty_std_termios;
696 tty_set_operations(driver, ops);
698 if (tty_register_driver(driver)) {
699 printk(KERN_ERR "register_lines : can't register %s driver\n",
700 line_driver->name);
701 put_tty_driver(driver);
702 return NULL;
705 for(i = 0; i < nlines; i++) {
706 if (!lines[i].valid)
707 tty_unregister_device(driver, i);
710 mconsole_register_dev(&line_driver->mc);
711 return driver;
714 static DEFINE_SPINLOCK(winch_handler_lock);
715 static LIST_HEAD(winch_handlers);
717 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
719 struct line *line;
720 char *error;
721 int i;
723 for(i = 0; i < nlines; i++) {
724 line = &lines[i];
725 INIT_LIST_HEAD(&line->chan_list);
727 if (line->init_str == NULL)
728 continue;
730 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
731 if (line->init_str == NULL)
732 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
734 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
735 printk(KERN_ERR "parse_chan_pair failed for "
736 "device %d : %s\n", i, error);
737 line->valid = 0;
742 struct winch {
743 struct list_head list;
744 int fd;
745 int tty_fd;
746 int pid;
747 struct tty_struct *tty;
748 unsigned long stack;
751 static void free_winch(struct winch *winch, int free_irq_ok)
753 list_del(&winch->list);
755 if (winch->pid != -1)
756 os_kill_process(winch->pid, 1);
757 if (winch->fd != -1)
758 os_close_file(winch->fd);
759 if (winch->stack != 0)
760 free_stack(winch->stack, 0);
761 if (free_irq_ok)
762 free_irq(WINCH_IRQ, winch);
763 kfree(winch);
766 static irqreturn_t winch_interrupt(int irq, void *data)
768 struct winch *winch = data;
769 struct tty_struct *tty;
770 struct line *line;
771 int err;
772 char c;
774 if (winch->fd != -1) {
775 err = generic_read(winch->fd, &c, NULL);
776 if (err < 0) {
777 if (err != -EAGAIN) {
778 printk(KERN_ERR "winch_interrupt : "
779 "read failed, errno = %d\n", -err);
780 printk(KERN_ERR "fd %d is losing SIGWINCH "
781 "support\n", winch->tty_fd);
782 free_winch(winch, 0);
783 return IRQ_HANDLED;
785 goto out;
788 tty = winch->tty;
789 if (tty != NULL) {
790 line = tty->driver_data;
791 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
792 &tty->winsize.ws_col);
793 kill_pgrp(tty->pgrp, SIGWINCH, 1);
795 out:
796 if (winch->fd != -1)
797 reactivate_fd(winch->fd, WINCH_IRQ);
798 return IRQ_HANDLED;
801 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
802 unsigned long stack)
804 struct winch *winch;
806 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
807 if (winch == NULL) {
808 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
809 goto cleanup;
812 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
813 .fd = fd,
814 .tty_fd = tty_fd,
815 .pid = pid,
816 .tty = tty,
817 .stack = stack });
819 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
820 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
821 "winch", winch) < 0) {
822 printk(KERN_ERR "register_winch_irq - failed to register "
823 "IRQ\n");
824 goto out_free;
827 spin_lock(&winch_handler_lock);
828 list_add(&winch->list, &winch_handlers);
829 spin_unlock(&winch_handler_lock);
831 return;
833 out_free:
834 kfree(winch);
835 cleanup:
836 os_kill_process(pid, 1);
837 os_close_file(fd);
838 if (stack != 0)
839 free_stack(stack, 0);
842 static void unregister_winch(struct tty_struct *tty)
844 struct list_head *ele;
845 struct winch *winch;
847 spin_lock(&winch_handler_lock);
849 list_for_each(ele, &winch_handlers) {
850 winch = list_entry(ele, struct winch, list);
851 if (winch->tty == tty) {
852 free_winch(winch, 1);
853 break;
856 spin_unlock(&winch_handler_lock);
859 static void winch_cleanup(void)
861 struct list_head *ele, *next;
862 struct winch *winch;
864 spin_lock(&winch_handler_lock);
866 list_for_each_safe(ele, next, &winch_handlers) {
867 winch = list_entry(ele, struct winch, list);
868 free_winch(winch, 1);
871 spin_unlock(&winch_handler_lock);
873 __uml_exitcall(winch_cleanup);
875 char *add_xterm_umid(char *base)
877 char *umid, *title;
878 int len;
880 umid = get_umid();
881 if (*umid == '\0')
882 return base;
884 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
885 title = kmalloc(len, GFP_KERNEL);
886 if (title == NULL) {
887 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
888 return base;
891 snprintf(title, len, "%s (%s)", base, umid);
892 return title;