2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/sched.h"
7 #include "linux/slab.h"
8 #include "linux/list.h"
10 #include "linux/interrupt.h"
11 #include "linux/devfs_fs_kernel.h"
12 #include "asm/uaccess.h"
13 #include "chan_kern.h"
17 #include "user_util.h"
18 #include "kern_util.h"
22 #define LINE_BUFSIZE 4096
24 static irqreturn_t
line_interrupt(int irq
, void *data
, struct pt_regs
*unused
)
26 struct tty_struct
*tty
= data
;
27 struct line
*line
= tty
->driver_data
;
30 chan_interrupt(&line
->chan_list
, &line
->task
, tty
, irq
);
34 static void line_timer_cb(void *arg
)
36 struct tty_struct
*tty
= arg
;
37 struct line
*line
= tty
->driver_data
;
39 line_interrupt(line
->driver
->read_irq
, arg
, NULL
);
42 /* Returns the free space inside the ring buffer of this line.
44 * Should be called while holding line->lock (this does not modify datas).
46 static int write_room(struct line
*line
)
50 if (line
->buffer
== NULL
)
51 return LINE_BUFSIZE
- 1;
53 /* This is for the case where the buffer is wrapped! */
54 n
= line
->head
- line
->tail
;
57 n
= LINE_BUFSIZE
+ n
; /* The other case */
61 int line_write_room(struct tty_struct
*tty
)
63 struct line
*line
= tty
->driver_data
;
70 spin_lock_irqsave(&line
->lock
, flags
);
71 room
= write_room(line
);
72 spin_unlock_irqrestore(&line
->lock
, flags
);
74 /*XXX: Warning to remove */
76 printk(KERN_DEBUG
"%s: %s: no room left in buffer\n",
77 __FUNCTION__
,tty
->name
);
81 int line_chars_in_buffer(struct tty_struct
*tty
)
83 struct line
*line
= tty
->driver_data
;
87 spin_lock_irqsave(&line
->lock
, flags
);
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
);
97 * This copies the content of buf into the circular buffer associated with
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.
103 * Must be called while holding line->lock!
105 static int buffer_data(struct line
*line
, const char *buf
, int len
)
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");
115 line
->head
= line
->buffer
;
116 line
->tail
= line
->buffer
;
119 room
= write_room(line
);
120 len
= (len
> room
) ? room
: len
;
122 end
= line
->buffer
+ LINE_BUFSIZE
- line
->tail
;
125 memcpy(line
->tail
, buf
, len
);
129 /* The circular buffer is wrapping */
130 memcpy(line
->tail
, buf
, end
);
132 memcpy(line
->buffer
, buf
, len
- end
);
133 line
->tail
= line
->buffer
+ len
- end
;
140 * Flushes the ring buffer to the output channels. That is, write_chan is
141 * called, passing it line->head as buffer, and an appropriate count.
143 * On exit, returns 1 when the buffer is empty,
144 * 0 when the buffer is not empty on exit,
145 * and -errno when an error occurred.
147 * Must be called while holding line->lock!*/
148 static int flush_buffer(struct line
*line
)
152 if ((line
->buffer
== NULL
) || (line
->head
== line
->tail
))
155 if (line
->tail
< line
->head
) {
156 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
157 count
= line
->buffer
+ LINE_BUFSIZE
- line
->head
;
159 n
= write_chan(&line
->chan_list
, line
->head
, count
,
160 line
->driver
->write_irq
);
164 /* We have flushed from ->head to buffer end, now we
165 * must flush only from the beginning to ->tail.*/
166 line
->head
= line
->buffer
;
173 count
= line
->tail
- line
->head
;
174 n
= write_chan(&line
->chan_list
, line
->head
, count
,
175 line
->driver
->write_irq
);
181 return line
->head
== line
->tail
;
184 void line_flush_buffer(struct tty_struct
*tty
)
186 struct line
*line
= tty
->driver_data
;
190 /*XXX: copied from line_write, verify if it is correct!*/
195 spin_lock_irqsave(&line
->lock
, flags
);
196 err
= flush_buffer(line
);
199 spin_unlock_irqrestore(&line
->lock
, flags
);
203 /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
204 * and ->write. Hope it's not that bad.*/
205 void line_flush_chars(struct tty_struct
*tty
)
207 line_flush_buffer(tty
);
210 void line_put_char(struct tty_struct
*tty
, unsigned char ch
)
212 line_write(tty
, &ch
, sizeof(ch
));
215 int line_write(struct tty_struct
*tty
, const unsigned char *buf
, int len
)
217 struct line
*line
= tty
->driver_data
;
224 spin_lock_irqsave(&line
->lock
, flags
);
225 if (line
->head
!= line
->tail
) {
226 ret
= buffer_data(line
, buf
, len
);
227 err
= flush_buffer(line
);
228 if (err
<= 0 && (err
!= -EAGAIN
|| !ret
))
231 n
= write_chan(&line
->chan_list
, buf
, len
,
232 line
->driver
->write_irq
);
241 ret
+= buffer_data(line
, buf
+ n
, len
);
244 spin_unlock_irqrestore(&line
->lock
, flags
);
248 void line_set_termios(struct tty_struct
*tty
, struct termios
* old
)
258 /* don't print these, they flood the log ... */
259 { TCGETS
, NULL
, "TCGETS" },
260 { TCSETS
, NULL
, "TCSETS" },
261 { TCSETSW
, NULL
, "TCSETSW" },
262 { TCFLSH
, NULL
, "TCFLSH" },
263 { TCSBRK
, NULL
, "TCSBRK" },
265 /* general tty stuff */
266 { TCSETSF
, KERN_DEBUG
, "TCSETSF" },
267 { TCGETA
, KERN_DEBUG
, "TCGETA" },
268 { TIOCMGET
, KERN_DEBUG
, "TIOCMGET" },
269 { TCSBRKP
, KERN_DEBUG
, "TCSBRKP" },
270 { TIOCMSET
, KERN_DEBUG
, "TIOCMSET" },
272 /* linux-specific ones */
273 { TIOCLINUX
, KERN_INFO
, "TIOCLINUX" },
274 { KDGKBMODE
, KERN_INFO
, "KDGKBMODE" },
275 { KDGKBTYPE
, KERN_INFO
, "KDGKBTYPE" },
276 { KDSIGACCEPT
, KERN_INFO
, "KDSIGACCEPT" },
279 int line_ioctl(struct tty_struct
*tty
, struct file
* file
,
280 unsigned int cmd
, unsigned long arg
)
324 for (i
= 0; i
< ARRAY_SIZE(tty_ioctls
); i
++)
325 if (cmd
== tty_ioctls
[i
].cmd
)
327 if (i
< ARRAY_SIZE(tty_ioctls
)) {
328 if (NULL
!= tty_ioctls
[i
].level
)
329 printk("%s%s: %s: ioctl %s called\n",
330 tty_ioctls
[i
].level
, __FUNCTION__
,
331 tty
->name
, tty_ioctls
[i
].name
);
333 printk(KERN_ERR
"%s: %s: unknown ioctl: 0x%x\n",
334 __FUNCTION__
, tty
->name
, cmd
);
342 static irqreturn_t
line_write_interrupt(int irq
, void *data
,
343 struct pt_regs
*unused
)
345 struct tty_struct
*tty
= data
;
346 struct line
*line
= tty
->driver_data
;
349 /* Interrupts are enabled here because we registered the interrupt with
350 * SA_INTERRUPT (see line_setup_irq).*/
352 spin_lock_irq(&line
->lock
);
353 err
= flush_buffer(line
);
357 line
->head
= line
->buffer
;
358 line
->tail
= line
->buffer
;
360 spin_unlock_irq(&line
->lock
);
365 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
) &&
366 (tty
->ldisc
.write_wakeup
!= NULL
))
367 (tty
->ldisc
.write_wakeup
)(tty
);
370 * In blocking mode, everything sleeps on tty->write_wait.
371 * Sleeping in the console driver would break non-blocking
375 if (waitqueue_active(&tty
->write_wait
))
376 wake_up_interruptible(&tty
->write_wait
);
380 int line_setup_irq(int fd
, int input
, int output
, struct tty_struct
*tty
)
382 struct line
*line
= tty
->driver_data
;
383 struct line_driver
*driver
= line
->driver
;
384 int err
= 0, flags
= SA_INTERRUPT
| SA_SHIRQ
| SA_SAMPLE_RANDOM
;
387 err
= um_request_irq(driver
->read_irq
, fd
, IRQ_READ
,
388 line_interrupt
, flags
,
389 driver
->read_irq_name
, tty
);
393 err
= um_request_irq(driver
->write_irq
, fd
, IRQ_WRITE
,
394 line_write_interrupt
, flags
,
395 driver
->write_irq_name
, tty
);
400 void line_disable(struct tty_struct
*tty
, int current_irq
)
402 struct line
*line
= tty
->driver_data
;
407 if(line
->driver
->read_irq
== current_irq
)
408 free_irq_later(line
->driver
->read_irq
, tty
);
410 free_irq(line
->driver
->read_irq
, tty
);
413 if(line
->driver
->write_irq
== current_irq
)
414 free_irq_later(line
->driver
->write_irq
, tty
);
416 free_irq(line
->driver
->write_irq
, tty
);
422 int line_open(struct line
*lines
, struct tty_struct
*tty
,
423 struct chan_opts
*opts
)
428 line
= &lines
[tty
->index
];
429 tty
->driver_data
= line
;
431 /* The IRQ which takes this lock is not yet enabled and won't be run
432 * before the end, so we don't need to use spin_lock_irq.*/
433 spin_lock(&line
->lock
);
434 if (tty
->count
== 1) {
439 if (list_empty(&line
->chan_list
)) {
440 err
= parse_chan_pair(line
->init_str
, &line
->chan_list
,
443 err
= open_chan(&line
->chan_list
);
446 /* Here the interrupt is registered.*/
447 enable_chan(&line
->chan_list
, tty
);
448 INIT_WORK(&line
->task
, line_timer_cb
, tty
);
452 chan_enable_winch(&line
->chan_list
, tty
);
455 chan_window_size(&line
->chan_list
, &tty
->winsize
.ws_row
,
456 &tty
->winsize
.ws_col
);
460 spin_unlock(&line
->lock
);
464 static void unregister_winch(struct tty_struct
*tty
);
466 void line_close(struct tty_struct
*tty
, struct file
* filp
)
468 struct line
*line
= tty
->driver_data
;
470 /* XXX: I assume this should be called in process context, not with
471 * interrupts disabled!
473 spin_lock_irq(&line
->lock
);
475 /* We ignore the error anyway! */
479 if (tty
->count
== 1) {
480 line_disable(tty
, -1);
481 tty
->driver_data
= NULL
;
484 if((line
->count
== 0) && line
->sigio
){
485 unregister_winch(tty
);
489 spin_unlock_irq(&line
->lock
);
492 void close_lines(struct line
*lines
, int nlines
)
496 for(i
= 0; i
< nlines
; i
++)
497 close_chan(&lines
[i
].chan_list
);
500 /* Common setup code for both startup command line and mconsole initialization.
501 * @lines contains the the array (of size @num) to modify;
502 * @init is the setup string;
505 int line_setup(struct line
*lines
, unsigned int num
, char *init
)
511 /* We said con=/ssl= instead of con#=, so we are configuring all
512 * consoles at once.*/
516 n
= simple_strtoul(init
, &end
, 0);
518 printk(KERN_ERR
"line_setup failed to parse \"%s\"\n",
526 if (n
>= (signed int) num
) {
527 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
532 if (lines
[n
].count
> 0) {
533 printk("line_setup - device %d is open\n", n
);
536 if (lines
[n
].init_pri
<= INIT_ONE
){
537 lines
[n
].init_pri
= INIT_ONE
;
538 if (!strcmp(init
, "none"))
541 lines
[n
].init_str
= init
;
547 for(i
= 0; i
< num
; i
++){
548 if(lines
[i
].init_pri
<= INIT_ALL
){
549 lines
[i
].init_pri
= INIT_ALL
;
550 if(!strcmp(init
, "none")) lines
[i
].valid
= 0;
552 lines
[i
].init_str
= init
;
558 return n
== -1 ? num
: n
;
561 int line_config(struct line
*lines
, unsigned int num
, char *str
)
567 printk("line_config - can't configure all devices from "
572 new = kstrdup(str
, GFP_KERNEL
);
574 printk("line_config - kstrdup failed\n");
577 n
= line_setup(lines
, num
, new);
578 return n
< 0 ? n
: 0;
581 int line_get_config(char *name
, struct line
*lines
, unsigned int num
, char *str
,
582 int size
, char **error_out
)
588 dev
= simple_strtoul(name
, &end
, 0);
589 if((*end
!= '\0') || (end
== name
)){
590 *error_out
= "line_get_config failed to parse device number";
594 if((dev
< 0) || (dev
>= num
)){
595 *error_out
= "device number out of range";
601 spin_lock(&line
->lock
);
603 CONFIG_CHUNK(str
, size
, n
, "none", 1);
604 else if(line
->count
== 0)
605 CONFIG_CHUNK(str
, size
, n
, line
->init_str
, 1);
606 else n
= chan_config_string(&line
->chan_list
, str
, size
, error_out
);
607 spin_unlock(&line
->lock
);
612 int line_id(char **str
, int *start_out
, int *end_out
)
617 n
= simple_strtoul(*str
, &end
, 0);
618 if((*end
!= '\0') || (end
== *str
))
627 int line_remove(struct line
*lines
, unsigned int num
, int n
)
630 char config
[sizeof("conxxxx=none\0")];
632 sprintf(config
, "%d=none", n
);
633 err
= line_setup(lines
, num
, config
);
639 struct tty_driver
*line_register_devfs(struct lines
*set
,
640 struct line_driver
*line_driver
,
641 struct tty_operations
*ops
, struct line
*lines
,
645 struct tty_driver
*driver
= alloc_tty_driver(nlines
);
650 driver
->driver_name
= line_driver
->name
;
651 driver
->name
= line_driver
->device_name
;
652 driver
->devfs_name
= line_driver
->devfs_name
;
653 driver
->major
= line_driver
->major
;
654 driver
->minor_start
= line_driver
->minor_start
;
655 driver
->type
= line_driver
->type
;
656 driver
->subtype
= line_driver
->subtype
;
657 driver
->flags
= TTY_DRIVER_REAL_RAW
;
658 driver
->init_termios
= tty_std_termios
;
659 tty_set_operations(driver
, ops
);
661 if (tty_register_driver(driver
)) {
662 printk("%s: can't register %s driver\n",
663 __FUNCTION__
,line_driver
->name
);
664 put_tty_driver(driver
);
668 for(i
= 0; i
< nlines
; i
++){
670 tty_unregister_device(driver
, i
);
673 mconsole_register_dev(&line_driver
->mc
);
677 static DEFINE_SPINLOCK(winch_handler_lock
);
678 static LIST_HEAD(winch_handlers
);
680 void lines_init(struct line
*lines
, int nlines
)
685 for(i
= 0; i
< nlines
; i
++){
687 INIT_LIST_HEAD(&line
->chan_list
);
689 if(line
->init_str
== NULL
)
692 line
->init_str
= kstrdup(line
->init_str
, GFP_KERNEL
);
693 if(line
->init_str
== NULL
)
694 printk("lines_init - kstrdup returned NULL\n");
699 struct list_head list
;
703 struct tty_struct
*tty
;
706 irqreturn_t
winch_interrupt(int irq
, void *data
, struct pt_regs
*unused
)
708 struct winch
*winch
= data
;
709 struct tty_struct
*tty
;
715 err
= generic_read(winch
->fd
, &c
, NULL
);
718 printk("winch_interrupt : read failed, "
719 "errno = %d\n", -err
);
720 printk("fd %d is losing SIGWINCH support\n",
729 line
= tty
->driver_data
;
730 chan_window_size(&line
->chan_list
, &tty
->winsize
.ws_row
,
731 &tty
->winsize
.ws_col
);
732 kill_pg(tty
->pgrp
, SIGWINCH
, 1);
736 reactivate_fd(winch
->fd
, WINCH_IRQ
);
740 void register_winch_irq(int fd
, int tty_fd
, int pid
, struct tty_struct
*tty
)
744 winch
= kmalloc(sizeof(*winch
), GFP_KERNEL
);
746 printk("register_winch_irq - kmalloc failed\n");
750 *winch
= ((struct winch
) { .list
= LIST_HEAD_INIT(winch
->list
),
756 spin_lock(&winch_handler_lock
);
757 list_add(&winch
->list
, &winch_handlers
);
758 spin_unlock(&winch_handler_lock
);
760 if(um_request_irq(WINCH_IRQ
, fd
, IRQ_READ
, winch_interrupt
,
761 SA_INTERRUPT
| SA_SHIRQ
| SA_SAMPLE_RANDOM
,
763 printk("register_winch_irq - failed to register IRQ\n");
766 static void unregister_winch(struct tty_struct
*tty
)
768 struct list_head
*ele
;
769 struct winch
*winch
, *found
= NULL
;
771 spin_lock(&winch_handler_lock
);
772 list_for_each(ele
, &winch_handlers
){
773 winch
= list_entry(ele
, struct winch
, list
);
774 if(winch
->tty
== tty
){
782 list_del(&winch
->list
);
783 spin_unlock(&winch_handler_lock
);
786 os_kill_process(winch
->pid
, 1);
788 free_irq(WINCH_IRQ
, winch
);
793 spin_unlock(&winch_handler_lock
);
796 /* XXX: No lock as it's an exitcall... is this valid? Depending on cleanup
797 * order... are we sure that nothing else is done on the list? */
798 static void winch_cleanup(void)
800 struct list_head
*ele
;
803 list_for_each(ele
, &winch_handlers
){
804 winch
= list_entry(ele
, struct winch
, list
);
806 /* Why is this different from the above free_irq(),
807 * which deactivates SIGIO? This searches the FD
808 * somewhere else and removes it from the list... */
809 deactivate_fd(winch
->fd
, WINCH_IRQ
);
810 os_close_file(winch
->fd
);
813 os_kill_process(winch
->pid
, 1);
816 __uml_exitcall(winch_cleanup
);
818 char *add_xterm_umid(char *base
)
827 len
= strlen(base
) + strlen(" ()") + strlen(umid
) + 1;
828 title
= kmalloc(len
, GFP_KERNEL
);
830 printk("Failed to allocate buffer for xterm title\n");
834 snprintf(title
, len
, "%s (%s)", base
, umid
);