uml: Fix build breakage after slab.h changes
[linux-2.6/libata-dev.git] / arch / um / drivers / line.c
blob7a656bd8bd3c185f9fe324f6bd5a8a9bc2f533d2
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;
22 struct tty_struct *tty;
24 if (line)
25 chan_interrupt(&line->chan_list, &line->task, line->tty, irq);
26 return IRQ_HANDLED;
29 static void line_timer_cb(struct work_struct *work)
31 struct line *line = container_of(work, struct line, task.work);
33 if (!line->throttled)
34 chan_interrupt(&line->chan_list, &line->task, line->tty,
35 line->driver->read_irq);
39 * Returns the free space inside the ring buffer of this line.
41 * Should be called while holding line->lock (this does not modify data).
43 static int write_room(struct line *line)
45 int n;
47 if (line->buffer == NULL)
48 return LINE_BUFSIZE - 1;
50 /* This is for the case where the buffer is wrapped! */
51 n = line->head - line->tail;
53 if (n <= 0)
54 n += LINE_BUFSIZE; /* The other case */
55 return n - 1;
58 int line_write_room(struct tty_struct *tty)
60 struct line *line = tty->driver_data;
61 unsigned long flags;
62 int room;
64 spin_lock_irqsave(&line->lock, flags);
65 room = write_room(line);
66 spin_unlock_irqrestore(&line->lock, flags);
68 return room;
71 int line_chars_in_buffer(struct tty_struct *tty)
73 struct line *line = tty->driver_data;
74 unsigned long flags;
75 int ret;
77 spin_lock_irqsave(&line->lock, flags);
78 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
79 ret = LINE_BUFSIZE - (write_room(line) + 1);
80 spin_unlock_irqrestore(&line->lock, flags);
82 return ret;
86 * This copies the content of buf into the circular buffer associated with
87 * this line.
88 * The return value is the number of characters actually copied, i.e. the ones
89 * for which there was space: this function is not supposed to ever flush out
90 * the circular buffer.
92 * Must be called while holding line->lock!
94 static int buffer_data(struct line *line, const char *buf, int len)
96 int end, room;
98 if (line->buffer == NULL) {
99 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
100 if (line->buffer == NULL) {
101 printk(KERN_ERR "buffer_data - atomic allocation "
102 "failed\n");
103 return 0;
105 line->head = line->buffer;
106 line->tail = line->buffer;
109 room = write_room(line);
110 len = (len > room) ? room : len;
112 end = line->buffer + LINE_BUFSIZE - line->tail;
114 if (len < end) {
115 memcpy(line->tail, buf, len);
116 line->tail += len;
118 else {
119 /* The circular buffer is wrapping */
120 memcpy(line->tail, buf, end);
121 buf += end;
122 memcpy(line->buffer, buf, len - end);
123 line->tail = line->buffer + len - end;
126 return len;
130 * Flushes the ring buffer to the output channels. That is, write_chan is
131 * called, passing it line->head as buffer, and an appropriate count.
133 * On exit, returns 1 when the buffer is empty,
134 * 0 when the buffer is not empty on exit,
135 * and -errno when an error occurred.
137 * Must be called while holding line->lock!*/
138 static int flush_buffer(struct line *line)
140 int n, count;
142 if ((line->buffer == NULL) || (line->head == line->tail))
143 return 1;
145 if (line->tail < line->head) {
146 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
147 count = line->buffer + LINE_BUFSIZE - line->head;
149 n = write_chan(&line->chan_list, line->head, count,
150 line->driver->write_irq);
151 if (n < 0)
152 return n;
153 if (n == count) {
155 * We have flushed from ->head to buffer end, now we
156 * must flush only from the beginning to ->tail.
158 line->head = line->buffer;
159 } else {
160 line->head += n;
161 return 0;
165 count = line->tail - line->head;
166 n = write_chan(&line->chan_list, line->head, count,
167 line->driver->write_irq);
169 if (n < 0)
170 return n;
172 line->head += n;
173 return line->head == line->tail;
176 void line_flush_buffer(struct tty_struct *tty)
178 struct line *line = tty->driver_data;
179 unsigned long flags;
180 int err;
182 spin_lock_irqsave(&line->lock, flags);
183 err = flush_buffer(line);
184 spin_unlock_irqrestore(&line->lock, flags);
188 * We map both ->flush_chars and ->put_char (which go in pair) onto
189 * ->flush_buffer and ->write. Hope it's not that bad.
191 void line_flush_chars(struct tty_struct *tty)
193 line_flush_buffer(tty);
196 int line_put_char(struct tty_struct *tty, unsigned char ch)
198 return line_write(tty, &ch, sizeof(ch));
201 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
203 struct line *line = tty->driver_data;
204 unsigned long flags;
205 int n, ret = 0;
207 spin_lock_irqsave(&line->lock, flags);
208 if (line->head != line->tail)
209 ret = buffer_data(line, buf, len);
210 else {
211 n = write_chan(&line->chan_list, buf, len,
212 line->driver->write_irq);
213 if (n < 0) {
214 ret = n;
215 goto out_up;
218 len -= n;
219 ret += n;
220 if (len > 0)
221 ret += buffer_data(line, buf + n, len);
223 out_up:
224 spin_unlock_irqrestore(&line->lock, flags);
225 return ret;
228 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
230 /* nothing */
233 static const struct {
234 int cmd;
235 char *level;
236 char *name;
237 } tty_ioctls[] = {
238 /* don't print these, they flood the log ... */
239 { TCGETS, NULL, "TCGETS" },
240 { TCSETS, NULL, "TCSETS" },
241 { TCSETSW, NULL, "TCSETSW" },
242 { TCFLSH, NULL, "TCFLSH" },
243 { TCSBRK, NULL, "TCSBRK" },
245 /* general tty stuff */
246 { TCSETSF, KERN_DEBUG, "TCSETSF" },
247 { TCGETA, KERN_DEBUG, "TCGETA" },
248 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
249 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
250 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
252 /* linux-specific ones */
253 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
254 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
255 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
256 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
259 int line_ioctl(struct tty_struct *tty, struct file * file,
260 unsigned int cmd, unsigned long arg)
262 int ret;
263 int i;
265 ret = 0;
266 switch(cmd) {
267 #ifdef TIOCGETP
268 case TIOCGETP:
269 case TIOCSETP:
270 case TIOCSETN:
271 #endif
272 #ifdef TIOCGETC
273 case TIOCGETC:
274 case TIOCSETC:
275 #endif
276 #ifdef TIOCGLTC
277 case TIOCGLTC:
278 case TIOCSLTC:
279 #endif
280 /* Note: these are out of date as we now have TCGETS2 etc but this
281 whole lot should probably go away */
282 case TCGETS:
283 case TCSETSF:
284 case TCSETSW:
285 case TCSETS:
286 case TCGETA:
287 case TCSETAF:
288 case TCSETAW:
289 case TCSETA:
290 case TCXONC:
291 case TCFLSH:
292 case TIOCOUTQ:
293 case TIOCINQ:
294 case TIOCGLCKTRMIOS:
295 case TIOCSLCKTRMIOS:
296 case TIOCPKT:
297 case TIOCGSOFTCAR:
298 case TIOCSSOFTCAR:
299 return -ENOIOCTLCMD;
300 #if 0
301 case TCwhatever:
302 /* do something */
303 break;
304 #endif
305 default:
306 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
307 if (cmd == tty_ioctls[i].cmd)
308 break;
309 if (i == ARRAY_SIZE(tty_ioctls)) {
310 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
311 __func__, tty->name, cmd);
313 ret = -ENOIOCTLCMD;
314 break;
316 return ret;
319 void line_throttle(struct tty_struct *tty)
321 struct line *line = tty->driver_data;
323 deactivate_chan(&line->chan_list, line->driver->read_irq);
324 line->throttled = 1;
327 void line_unthrottle(struct tty_struct *tty)
329 struct line *line = tty->driver_data;
331 line->throttled = 0;
332 chan_interrupt(&line->chan_list, &line->task, tty,
333 line->driver->read_irq);
336 * Maybe there is enough stuff pending that calling the interrupt
337 * throttles us again. In this case, line->throttled will be 1
338 * again and we shouldn't turn the interrupt back on.
340 if (!line->throttled)
341 reactivate_chan(&line->chan_list, line->driver->read_irq);
344 static irqreturn_t line_write_interrupt(int irq, void *data)
346 struct chan *chan = data;
347 struct line *line = chan->line;
348 struct tty_struct *tty = line->tty;
349 int err;
352 * Interrupts are disabled here because we registered the interrupt with
353 * IRQF_DISABLED (see line_setup_irq).
356 spin_lock(&line->lock);
357 err = flush_buffer(line);
358 if (err == 0) {
359 return IRQ_NONE;
360 } else if (err < 0) {
361 line->head = line->buffer;
362 line->tail = line->buffer;
364 spin_unlock(&line->lock);
366 if (tty == NULL)
367 return IRQ_NONE;
369 tty_wakeup(tty);
370 return IRQ_HANDLED;
373 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
375 const struct line_driver *driver = line->driver;
376 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
378 if (input)
379 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
380 line_interrupt, flags,
381 driver->read_irq_name, data);
382 if (err)
383 return err;
384 if (output)
385 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
386 line_write_interrupt, flags,
387 driver->write_irq_name, data);
388 line->have_irq = 1;
389 return err;
393 * Normally, a driver like this can rely mostly on the tty layer
394 * locking, particularly when it comes to the driver structure.
395 * However, in this case, mconsole requests can come in "from the
396 * side", and race with opens and closes.
398 * mconsole config requests will want to be sure the device isn't in
399 * use, and get_config, open, and close will want a stable
400 * configuration. The checking and modification of the configuration
401 * is done under a spinlock. Checking whether the device is in use is
402 * line->tty->count > 1, also under the spinlock.
404 * tty->count serves to decide whether the device should be enabled or
405 * disabled on the host. If it's equal to 1, then we are doing the
406 * first open or last close. Otherwise, open and close just return.
409 int line_open(struct line *lines, struct tty_struct *tty)
411 struct line *line = &lines[tty->index];
412 int err = -ENODEV;
414 spin_lock(&line->count_lock);
415 if (!line->valid)
416 goto out_unlock;
418 err = 0;
419 if (tty->count > 1)
420 goto out_unlock;
422 spin_unlock(&line->count_lock);
424 tty->driver_data = line;
425 line->tty = tty;
427 err = enable_chan(line);
428 if (err)
429 return err;
431 INIT_DELAYED_WORK(&line->task, line_timer_cb);
433 if (!line->sigio) {
434 chan_enable_winch(&line->chan_list, tty);
435 line->sigio = 1;
438 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
439 &tty->winsize.ws_col);
441 return err;
443 out_unlock:
444 spin_unlock(&line->count_lock);
445 return err;
448 static void unregister_winch(struct tty_struct *tty);
450 void line_close(struct tty_struct *tty, struct file * filp)
452 struct line *line = tty->driver_data;
455 * If line_open fails (and tty->driver_data is never set),
456 * tty_open will call line_close. So just return in this case.
458 if (line == NULL)
459 return;
461 /* We ignore the error anyway! */
462 flush_buffer(line);
464 spin_lock(&line->count_lock);
465 if (!line->valid)
466 goto out_unlock;
468 if (tty->count > 1)
469 goto out_unlock;
471 spin_unlock(&line->count_lock);
473 line->tty = NULL;
474 tty->driver_data = NULL;
476 if (line->sigio) {
477 unregister_winch(tty);
478 line->sigio = 0;
481 return;
483 out_unlock:
484 spin_unlock(&line->count_lock);
487 void close_lines(struct line *lines, int nlines)
489 int i;
491 for(i = 0; i < nlines; i++)
492 close_chan(&lines[i].chan_list, 0);
495 static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
496 char **error_out)
498 struct line *line = &lines[n];
499 int err = -EINVAL;
501 spin_lock(&line->count_lock);
503 if (line->tty != NULL) {
504 *error_out = "Device is already open";
505 goto out;
508 if (line->init_pri <= init_prio) {
509 line->init_pri = init_prio;
510 if (!strcmp(init, "none"))
511 line->valid = 0;
512 else {
513 line->init_str = init;
514 line->valid = 1;
517 err = 0;
518 out:
519 spin_unlock(&line->count_lock);
520 return err;
524 * Common setup code for both startup command line and mconsole initialization.
525 * @lines contains the array (of size @num) to modify;
526 * @init is the setup string;
527 * @error_out is an error string in the case of failure;
530 int line_setup(struct line *lines, unsigned int num, char *init,
531 char **error_out)
533 int i, n, err;
534 char *end;
536 if (*init == '=') {
538 * We said con=/ssl= instead of con#=, so we are configuring all
539 * consoles at once.
541 n = -1;
543 else {
544 n = simple_strtoul(init, &end, 0);
545 if (*end != '=') {
546 *error_out = "Couldn't parse device number";
547 return -EINVAL;
549 init = end;
551 init++;
553 if (n >= (signed int) num) {
554 *error_out = "Device number out of range";
555 return -EINVAL;
557 else if (n >= 0) {
558 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
559 if (err)
560 return err;
562 else {
563 for(i = 0; i < num; i++) {
564 err = setup_one_line(lines, i, init, INIT_ALL,
565 error_out);
566 if (err)
567 return err;
570 return n == -1 ? num : n;
573 int line_config(struct line *lines, unsigned int num, char *str,
574 const struct chan_opts *opts, char **error_out)
576 struct line *line;
577 char *new;
578 int n;
580 if (*str == '=') {
581 *error_out = "Can't configure all devices from mconsole";
582 return -EINVAL;
585 new = kstrdup(str, GFP_KERNEL);
586 if (new == NULL) {
587 *error_out = "Failed to allocate memory";
588 return -ENOMEM;
590 n = line_setup(lines, num, new, error_out);
591 if (n < 0)
592 return n;
594 line = &lines[n];
595 return parse_chan_pair(line->init_str, line, n, opts, error_out);
598 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
599 int size, char **error_out)
601 struct line *line;
602 char *end;
603 int dev, n = 0;
605 dev = simple_strtoul(name, &end, 0);
606 if ((*end != '\0') || (end == name)) {
607 *error_out = "line_get_config failed to parse device number";
608 return 0;
611 if ((dev < 0) || (dev >= num)) {
612 *error_out = "device number out of range";
613 return 0;
616 line = &lines[dev];
618 spin_lock(&line->count_lock);
619 if (!line->valid)
620 CONFIG_CHUNK(str, size, n, "none", 1);
621 else if (line->tty == NULL)
622 CONFIG_CHUNK(str, size, n, line->init_str, 1);
623 else n = chan_config_string(&line->chan_list, str, size, error_out);
624 spin_unlock(&line->count_lock);
626 return n;
629 int line_id(char **str, int *start_out, int *end_out)
631 char *end;
632 int n;
634 n = simple_strtoul(*str, &end, 0);
635 if ((*end != '\0') || (end == *str))
636 return -1;
638 *str = end;
639 *start_out = n;
640 *end_out = n;
641 return n;
644 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
646 int err;
647 char config[sizeof("conxxxx=none\0")];
649 sprintf(config, "%d=none", n);
650 err = line_setup(lines, num, config, error_out);
651 if (err >= 0)
652 err = 0;
653 return err;
656 struct tty_driver *register_lines(struct line_driver *line_driver,
657 const struct tty_operations *ops,
658 struct line *lines, int nlines)
660 int i;
661 struct tty_driver *driver = alloc_tty_driver(nlines);
663 if (!driver)
664 return NULL;
666 driver->driver_name = line_driver->name;
667 driver->name = line_driver->device_name;
668 driver->major = line_driver->major;
669 driver->minor_start = line_driver->minor_start;
670 driver->type = line_driver->type;
671 driver->subtype = line_driver->subtype;
672 driver->flags = TTY_DRIVER_REAL_RAW;
673 driver->init_termios = tty_std_termios;
674 tty_set_operations(driver, ops);
676 if (tty_register_driver(driver)) {
677 printk(KERN_ERR "register_lines : can't register %s driver\n",
678 line_driver->name);
679 put_tty_driver(driver);
680 return NULL;
683 for(i = 0; i < nlines; i++) {
684 if (!lines[i].valid)
685 tty_unregister_device(driver, i);
688 mconsole_register_dev(&line_driver->mc);
689 return driver;
692 static DEFINE_SPINLOCK(winch_handler_lock);
693 static LIST_HEAD(winch_handlers);
695 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
697 struct line *line;
698 char *error;
699 int i;
701 for(i = 0; i < nlines; i++) {
702 line = &lines[i];
703 INIT_LIST_HEAD(&line->chan_list);
705 if (line->init_str == NULL)
706 continue;
708 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
709 if (line->init_str == NULL)
710 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
712 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
713 printk(KERN_ERR "parse_chan_pair failed for "
714 "device %d : %s\n", i, error);
715 line->valid = 0;
720 struct winch {
721 struct list_head list;
722 int fd;
723 int tty_fd;
724 int pid;
725 struct tty_struct *tty;
726 unsigned long stack;
729 static void free_winch(struct winch *winch, int free_irq_ok)
731 list_del(&winch->list);
733 if (winch->pid != -1)
734 os_kill_process(winch->pid, 1);
735 if (winch->fd != -1)
736 os_close_file(winch->fd);
737 if (winch->stack != 0)
738 free_stack(winch->stack, 0);
739 if (free_irq_ok)
740 free_irq(WINCH_IRQ, winch);
741 kfree(winch);
744 static irqreturn_t winch_interrupt(int irq, void *data)
746 struct winch *winch = data;
747 struct tty_struct *tty;
748 struct line *line;
749 int err;
750 char c;
752 if (winch->fd != -1) {
753 err = generic_read(winch->fd, &c, NULL);
754 if (err < 0) {
755 if (err != -EAGAIN) {
756 printk(KERN_ERR "winch_interrupt : "
757 "read failed, errno = %d\n", -err);
758 printk(KERN_ERR "fd %d is losing SIGWINCH "
759 "support\n", winch->tty_fd);
760 free_winch(winch, 0);
761 return IRQ_HANDLED;
763 goto out;
766 tty = winch->tty;
767 if (tty != NULL) {
768 line = tty->driver_data;
769 if (line != NULL) {
770 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
771 &tty->winsize.ws_col);
772 kill_pgrp(tty->pgrp, SIGWINCH, 1);
775 out:
776 if (winch->fd != -1)
777 reactivate_fd(winch->fd, WINCH_IRQ);
778 return IRQ_HANDLED;
781 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
782 unsigned long stack)
784 struct winch *winch;
786 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
787 if (winch == NULL) {
788 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
789 goto cleanup;
792 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
793 .fd = fd,
794 .tty_fd = tty_fd,
795 .pid = pid,
796 .tty = tty,
797 .stack = stack });
799 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
800 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
801 "winch", winch) < 0) {
802 printk(KERN_ERR "register_winch_irq - failed to register "
803 "IRQ\n");
804 goto out_free;
807 spin_lock(&winch_handler_lock);
808 list_add(&winch->list, &winch_handlers);
809 spin_unlock(&winch_handler_lock);
811 return;
813 out_free:
814 kfree(winch);
815 cleanup:
816 os_kill_process(pid, 1);
817 os_close_file(fd);
818 if (stack != 0)
819 free_stack(stack, 0);
822 static void unregister_winch(struct tty_struct *tty)
824 struct list_head *ele;
825 struct winch *winch;
827 spin_lock(&winch_handler_lock);
829 list_for_each(ele, &winch_handlers) {
830 winch = list_entry(ele, struct winch, list);
831 if (winch->tty == tty) {
832 free_winch(winch, 1);
833 break;
836 spin_unlock(&winch_handler_lock);
839 static void winch_cleanup(void)
841 struct list_head *ele, *next;
842 struct winch *winch;
844 spin_lock(&winch_handler_lock);
846 list_for_each_safe(ele, next, &winch_handlers) {
847 winch = list_entry(ele, struct winch, list);
848 free_winch(winch, 1);
851 spin_unlock(&winch_handler_lock);
853 __uml_exitcall(winch_cleanup);
855 char *add_xterm_umid(char *base)
857 char *umid, *title;
858 int len;
860 umid = get_umid();
861 if (*umid == '\0')
862 return base;
864 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
865 title = kmalloc(len, GFP_KERNEL);
866 if (title == NULL) {
867 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
868 return base;
871 snprintf(title, len, "%s (%s)", base, umid);
872 return title;