2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include "linux/irqreturn.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
;
22 chan_interrupt(&line
->chan_list
, &line
->task
, tty
, irq
);
26 static void line_timer_cb(struct work_struct
*work
)
28 struct line
*line
= container_of(work
, struct line
, task
.work
);
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
)
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
;
51 n
= LINE_BUFSIZE
+ n
; /* The other case */
55 int line_write_room(struct tty_struct
*tty
)
57 struct line
*line
= tty
->driver_data
;
64 spin_lock_irqsave(&line
->lock
, flags
);
65 room
= write_room(line
);
66 spin_unlock_irqrestore(&line
->lock
, flags
);
68 /*XXX: Warning to remove */
70 printk(KERN_DEBUG
"%s: %s: no room left in buffer\n",
71 __FUNCTION__
,tty
->name
);
75 int line_chars_in_buffer(struct tty_struct
*tty
)
77 struct line
*line
= tty
->driver_data
;
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
);
91 * This copies the content of buf into the circular buffer associated with
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
)
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 "
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
;
120 memcpy(line
->tail
, buf
, len
);
124 /* The circular buffer is wrapping */
125 memcpy(line
->tail
, buf
, end
);
127 memcpy(line
->buffer
, buf
, len
- end
);
128 line
->tail
= line
->buffer
+ len
- end
;
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
)
147 if ((line
->buffer
== NULL
) || (line
->head
== line
->tail
))
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
);
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
;
170 count
= line
->tail
- line
->head
;
171 n
= write_chan(&line
->chan_list
, line
->head
, count
,
172 line
->driver
->write_irq
);
178 return line
->head
== line
->tail
;
181 void line_flush_buffer(struct tty_struct
*tty
)
183 struct line
*line
= tty
->driver_data
;
187 /*XXX: copied from line_write, verify if it is correct!*/
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
;
219 spin_lock_irqsave(&line
->lock
, flags
);
220 if (line
->head
!= line
->tail
)
221 ret
= buffer_data(line
, buf
, len
);
223 n
= write_chan(&line
->chan_list
, buf
, len
,
224 line
->driver
->write_irq
);
233 ret
+= buffer_data(line
, buf
+ n
, len
);
236 spin_unlock_irqrestore(&line
->lock
, flags
);
240 void line_set_termios(struct tty_struct
*tty
, struct ktermios
* old
)
245 static const struct {
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
)
316 for (i
= 0; i
< ARRAY_SIZE(tty_ioctls
); i
++)
317 if (cmd
== tty_ioctls
[i
].cmd
)
319 if (i
== ARRAY_SIZE(tty_ioctls
)) {
320 printk(KERN_ERR
"%s: %s: unknown ioctl: 0x%x\n",
321 __FUNCTION__
, tty
->name
, cmd
);
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
);
337 void line_unthrottle(struct tty_struct
*tty
)
339 struct line
*line
= tty
->driver_data
;
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
;
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
);
370 } else if (err
< 0) {
371 line
->head
= line
->buffer
;
372 line
->tail
= line
->buffer
;
374 spin_unlock(&line
->lock
);
379 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
) &&
380 (tty
->ldisc
.write_wakeup
!= NULL
))
381 (tty
->ldisc
.write_wakeup
)(tty
);
385 * In blocking mode, everything sleeps on tty->write_wait.
386 * Sleeping in the console driver would break non-blocking
390 if (waitqueue_active(&tty
->write_wait
))
391 wake_up_interruptible(&tty
->write_wait
);
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
;
401 err
= um_request_irq(driver
->read_irq
, fd
, IRQ_READ
,
402 line_interrupt
, flags
,
403 driver
->read_irq_name
, data
);
407 err
= um_request_irq(driver
->write_irq
, fd
, IRQ_WRITE
,
408 line_write_interrupt
, flags
,
409 driver
->write_irq_name
, data
);
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
];
436 spin_lock(&line
->count_lock
);
444 spin_unlock(&line
->count_lock
);
446 tty
->driver_data
= line
;
449 err
= enable_chan(line
);
453 INIT_DELAYED_WORK(&line
->task
, line_timer_cb
);
456 chan_enable_winch(&line
->chan_list
, tty
);
460 chan_window_size(&line
->chan_list
, &tty
->winsize
.ws_row
,
461 &tty
->winsize
.ws_col
);
466 spin_unlock(&line
->count_lock
);
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.
483 /* We ignore the error anyway! */
486 spin_lock(&line
->count_lock
);
493 spin_unlock(&line
->count_lock
);
496 tty
->driver_data
= NULL
;
499 unregister_winch(tty
);
506 spin_unlock(&line
->count_lock
);
509 void close_lines(struct line
*lines
, int nlines
)
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
,
520 struct line
*line
= &lines
[n
];
523 spin_lock(&line
->count_lock
);
525 if (line
->tty
!= NULL
) {
526 *error_out
= "Device is already open";
530 if (line
->init_pri
<= init_prio
) {
531 line
->init_pri
= init_prio
;
532 if (!strcmp(init
, "none"))
535 line
->init_str
= init
;
541 spin_unlock(&line
->count_lock
);
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
,
560 * We said con=/ssl= instead of con#=, so we are configuring all
566 n
= simple_strtoul(init
, &end
, 0);
568 *error_out
= "Couldn't parse device number";
575 if (n
>= (signed int) num
) {
576 *error_out
= "Device number out of range";
580 err
= setup_one_line(lines
, n
, init
, INIT_ONE
, error_out
);
585 for(i
= 0; i
< num
; i
++) {
586 err
= setup_one_line(lines
, i
, init
, INIT_ALL
,
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
)
603 *error_out
= "Can't configure all devices from mconsole";
607 new = kstrdup(str
, GFP_KERNEL
);
609 *error_out
= "Failed to allocate memory";
612 n
= line_setup(lines
, num
, new, error_out
);
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
)
627 dev
= simple_strtoul(name
, &end
, 0);
628 if ((*end
!= '\0') || (end
== name
)) {
629 *error_out
= "line_get_config failed to parse device number";
633 if ((dev
< 0) || (dev
>= num
)) {
634 *error_out
= "device number out of range";
640 spin_lock(&line
->count_lock
);
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
);
651 int line_id(char **str
, int *start_out
, int *end_out
)
656 n
= simple_strtoul(*str
, &end
, 0);
657 if ((*end
!= '\0') || (end
== *str
))
666 int line_remove(struct line
*lines
, unsigned int num
, int n
, char **error_out
)
669 char config
[sizeof("conxxxx=none\0")];
671 sprintf(config
, "%d=none", n
);
672 err
= line_setup(lines
, num
, config
, error_out
);
678 struct tty_driver
*register_lines(struct line_driver
*line_driver
,
679 const struct tty_operations
*ops
,
680 struct line
*lines
, int nlines
)
683 struct tty_driver
*driver
= alloc_tty_driver(nlines
);
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",
701 put_tty_driver(driver
);
705 for(i
= 0; i
< nlines
; i
++) {
707 tty_unregister_device(driver
, i
);
710 mconsole_register_dev(&line_driver
->mc
);
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
)
723 for(i
= 0; i
< nlines
; i
++) {
725 INIT_LIST_HEAD(&line
->chan_list
);
727 if (line
->init_str
== NULL
)
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
);
743 struct list_head list
;
747 struct tty_struct
*tty
;
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);
758 os_close_file(winch
->fd
);
759 if (winch
->stack
!= 0)
760 free_stack(winch
->stack
, 0);
762 free_irq(WINCH_IRQ
, winch
);
766 static irqreturn_t
winch_interrupt(int irq
, void *data
)
768 struct winch
*winch
= data
;
769 struct tty_struct
*tty
;
774 if (winch
->fd
!= -1) {
775 err
= generic_read(winch
->fd
, &c
, NULL
);
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);
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);
797 reactivate_fd(winch
->fd
, WINCH_IRQ
);
801 void register_winch_irq(int fd
, int tty_fd
, int pid
, struct tty_struct
*tty
,
806 winch
= kmalloc(sizeof(*winch
), GFP_KERNEL
);
808 printk(KERN_ERR
"register_winch_irq - kmalloc failed\n");
812 *winch
= ((struct winch
) { .list
= LIST_HEAD_INIT(winch
->list
),
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 "
827 spin_lock(&winch_handler_lock
);
828 list_add(&winch
->list
, &winch_handlers
);
829 spin_unlock(&winch_handler_lock
);
836 os_kill_process(pid
, 1);
839 free_stack(stack
, 0);
842 static void unregister_winch(struct tty_struct
*tty
)
844 struct list_head
*ele
;
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);
856 spin_unlock(&winch_handler_lock
);
859 static void winch_cleanup(void)
861 struct list_head
*ele
, *next
;
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
)
884 len
= strlen(base
) + strlen(" ()") + strlen(umid
) + 1;
885 title
= kmalloc(len
, GFP_KERNEL
);
887 printk(KERN_ERR
"Failed to allocate buffer for xterm title\n");
891 snprintf(title
, len
, "%s (%s)", base
, umid
);