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 chan
*chan
= data
;
27 struct line
*line
= chan
->line
;
28 struct tty_struct
*tty
= line
->tty
;
31 chan_interrupt(&line
->chan_list
, &line
->task
, tty
, irq
);
35 static void line_timer_cb(void *arg
)
37 struct line
*line
= arg
;
40 chan_interrupt(&line
->chan_list
, &line
->task
, line
->tty
,
41 line
->driver
->read_irq
);
44 /* Returns the free space inside the ring buffer of this line.
46 * Should be called while holding line->lock (this does not modify datas).
48 static int write_room(struct line
*line
)
52 if (line
->buffer
== NULL
)
53 return LINE_BUFSIZE
- 1;
55 /* This is for the case where the buffer is wrapped! */
56 n
= line
->head
- line
->tail
;
59 n
= LINE_BUFSIZE
+ n
; /* The other case */
63 int line_write_room(struct tty_struct
*tty
)
65 struct line
*line
= tty
->driver_data
;
72 spin_lock_irqsave(&line
->lock
, flags
);
73 room
= write_room(line
);
74 spin_unlock_irqrestore(&line
->lock
, flags
);
76 /*XXX: Warning to remove */
78 printk(KERN_DEBUG
"%s: %s: no room left in buffer\n",
79 __FUNCTION__
,tty
->name
);
83 int line_chars_in_buffer(struct tty_struct
*tty
)
85 struct line
*line
= tty
->driver_data
;
89 spin_lock_irqsave(&line
->lock
, flags
);
91 /*write_room subtracts 1 for the needed NULL, so we readd it.*/
92 ret
= LINE_BUFSIZE
- (write_room(line
) + 1);
93 spin_unlock_irqrestore(&line
->lock
, flags
);
99 * This copies the content of buf into the circular buffer associated with
101 * The return value is the number of characters actually copied, i.e. the ones
102 * for which there was space: this function is not supposed to ever flush out
103 * the circular buffer.
105 * Must be called while holding line->lock!
107 static int buffer_data(struct line
*line
, const char *buf
, int len
)
111 if(line
->buffer
== NULL
){
112 line
->buffer
= kmalloc(LINE_BUFSIZE
, GFP_ATOMIC
);
113 if (line
->buffer
== NULL
) {
114 printk("buffer_data - atomic allocation failed\n");
117 line
->head
= line
->buffer
;
118 line
->tail
= line
->buffer
;
121 room
= write_room(line
);
122 len
= (len
> room
) ? room
: len
;
124 end
= line
->buffer
+ LINE_BUFSIZE
- line
->tail
;
127 memcpy(line
->tail
, buf
, len
);
131 /* The circular buffer is wrapping */
132 memcpy(line
->tail
, buf
, end
);
134 memcpy(line
->buffer
, buf
, len
- end
);
135 line
->tail
= line
->buffer
+ len
- end
;
142 * Flushes the ring buffer to the output channels. That is, write_chan is
143 * called, passing it line->head as buffer, and an appropriate count.
145 * On exit, returns 1 when the buffer is empty,
146 * 0 when the buffer is not empty on exit,
147 * and -errno when an error occurred.
149 * Must be called while holding line->lock!*/
150 static int flush_buffer(struct line
*line
)
154 if ((line
->buffer
== NULL
) || (line
->head
== line
->tail
))
157 if (line
->tail
< line
->head
) {
158 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
159 count
= line
->buffer
+ LINE_BUFSIZE
- line
->head
;
161 n
= write_chan(&line
->chan_list
, line
->head
, count
,
162 line
->driver
->write_irq
);
166 /* We have flushed from ->head to buffer end, now we
167 * must flush only from the beginning to ->tail.*/
168 line
->head
= line
->buffer
;
175 count
= line
->tail
- line
->head
;
176 n
= write_chan(&line
->chan_list
, line
->head
, count
,
177 line
->driver
->write_irq
);
183 return line
->head
== line
->tail
;
186 void line_flush_buffer(struct tty_struct
*tty
)
188 struct line
*line
= tty
->driver_data
;
192 /*XXX: copied from line_write, verify if it is correct!*/
197 spin_lock_irqsave(&line
->lock
, flags
);
198 err
= flush_buffer(line
);
201 spin_unlock_irqrestore(&line
->lock
, flags
);
205 /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
206 * and ->write. Hope it's not that bad.*/
207 void line_flush_chars(struct tty_struct
*tty
)
209 line_flush_buffer(tty
);
212 void line_put_char(struct tty_struct
*tty
, unsigned char ch
)
214 line_write(tty
, &ch
, sizeof(ch
));
217 int line_write(struct tty_struct
*tty
, const unsigned char *buf
, int len
)
219 struct line
*line
= tty
->driver_data
;
226 spin_lock_irqsave(&line
->lock
, flags
);
227 if (line
->head
!= line
->tail
) {
228 ret
= buffer_data(line
, buf
, len
);
229 err
= flush_buffer(line
);
230 if (err
<= 0 && (err
!= -EAGAIN
|| !ret
))
233 n
= write_chan(&line
->chan_list
, buf
, len
,
234 line
->driver
->write_irq
);
243 ret
+= buffer_data(line
, buf
+ n
, len
);
246 spin_unlock_irqrestore(&line
->lock
, flags
);
250 void line_set_termios(struct tty_struct
*tty
, struct termios
* old
)
260 /* don't print these, they flood the log ... */
261 { TCGETS
, NULL
, "TCGETS" },
262 { TCSETS
, NULL
, "TCSETS" },
263 { TCSETSW
, NULL
, "TCSETSW" },
264 { TCFLSH
, NULL
, "TCFLSH" },
265 { TCSBRK
, NULL
, "TCSBRK" },
267 /* general tty stuff */
268 { TCSETSF
, KERN_DEBUG
, "TCSETSF" },
269 { TCGETA
, KERN_DEBUG
, "TCGETA" },
270 { TIOCMGET
, KERN_DEBUG
, "TIOCMGET" },
271 { TCSBRKP
, KERN_DEBUG
, "TCSBRKP" },
272 { TIOCMSET
, KERN_DEBUG
, "TIOCMSET" },
274 /* linux-specific ones */
275 { TIOCLINUX
, KERN_INFO
, "TIOCLINUX" },
276 { KDGKBMODE
, KERN_INFO
, "KDGKBMODE" },
277 { KDGKBTYPE
, KERN_INFO
, "KDGKBTYPE" },
278 { KDSIGACCEPT
, KERN_INFO
, "KDSIGACCEPT" },
281 int line_ioctl(struct tty_struct
*tty
, struct file
* file
,
282 unsigned int cmd
, unsigned long arg
)
326 for (i
= 0; i
< ARRAY_SIZE(tty_ioctls
); i
++)
327 if (cmd
== tty_ioctls
[i
].cmd
)
329 if (i
< ARRAY_SIZE(tty_ioctls
)) {
330 if (NULL
!= tty_ioctls
[i
].level
)
331 printk("%s%s: %s: ioctl %s called\n",
332 tty_ioctls
[i
].level
, __FUNCTION__
,
333 tty
->name
, tty_ioctls
[i
].name
);
335 printk(KERN_ERR
"%s: %s: unknown ioctl: 0x%x\n",
336 __FUNCTION__
, tty
->name
, cmd
);
344 void line_throttle(struct tty_struct
*tty
)
346 struct line
*line
= tty
->driver_data
;
348 deactivate_chan(&line
->chan_list
, line
->driver
->read_irq
);
352 void line_unthrottle(struct tty_struct
*tty
)
354 struct line
*line
= tty
->driver_data
;
357 chan_interrupt(&line
->chan_list
, &line
->task
, tty
,
358 line
->driver
->read_irq
);
360 /* Maybe there is enough stuff pending that calling the interrupt
361 * throttles us again. In this case, line->throttled will be 1
362 * again and we shouldn't turn the interrupt back on.
365 reactivate_chan(&line
->chan_list
, line
->driver
->read_irq
);
368 static irqreturn_t
line_write_interrupt(int irq
, void *data
,
369 struct pt_regs
*unused
)
371 struct chan
*chan
= data
;
372 struct line
*line
= chan
->line
;
373 struct tty_struct
*tty
= line
->tty
;
376 /* Interrupts are enabled here because we registered the interrupt with
377 * SA_INTERRUPT (see line_setup_irq).*/
379 spin_lock_irq(&line
->lock
);
380 err
= flush_buffer(line
);
384 line
->head
= line
->buffer
;
385 line
->tail
= line
->buffer
;
387 spin_unlock_irq(&line
->lock
);
392 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
) &&
393 (tty
->ldisc
.write_wakeup
!= NULL
))
394 (tty
->ldisc
.write_wakeup
)(tty
);
397 * In blocking mode, everything sleeps on tty->write_wait.
398 * Sleeping in the console driver would break non-blocking
402 if (waitqueue_active(&tty
->write_wait
))
403 wake_up_interruptible(&tty
->write_wait
);
407 int line_setup_irq(int fd
, int input
, int output
, struct line
*line
, void *data
)
409 struct line_driver
*driver
= line
->driver
;
410 int err
= 0, flags
= SA_INTERRUPT
| SA_SHIRQ
| SA_SAMPLE_RANDOM
;
413 err
= um_request_irq(driver
->read_irq
, fd
, IRQ_READ
,
414 line_interrupt
, flags
,
415 driver
->read_irq_name
, data
);
419 err
= um_request_irq(driver
->write_irq
, fd
, IRQ_WRITE
,
420 line_write_interrupt
, flags
,
421 driver
->write_irq_name
, data
);
426 int line_open(struct line
*lines
, struct tty_struct
*tty
)
431 line
= &lines
[tty
->index
];
432 tty
->driver_data
= line
;
434 /* The IRQ which takes this lock is not yet enabled and won't be run
435 * before the end, so we don't need to use spin_lock_irq.*/
436 spin_lock(&line
->lock
);
438 tty
->driver_data
= line
;
444 /* Here the device is opened, if necessary, and interrupt
448 INIT_WORK(&line
->task
, line_timer_cb
, line
);
451 chan_enable_winch(&line
->chan_list
, tty
);
455 chan_window_size(&line
->chan_list
, &tty
->winsize
.ws_row
,
456 &tty
->winsize
.ws_col
);
461 spin_unlock(&line
->lock
);
465 static void unregister_winch(struct tty_struct
*tty
);
467 void line_close(struct tty_struct
*tty
, struct file
* filp
)
469 struct line
*line
= tty
->driver_data
;
471 /* XXX: I assume this should be called in process context, not with
472 * interrupts disabled!
474 spin_lock_irq(&line
->lock
);
476 /* We ignore the error anyway! */
481 tty
->driver_data
= NULL
;
484 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
, 0);
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
].tty
!= NULL
) {
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
,
562 struct chan_opts
*opts
)
569 printk("line_config - can't configure all devices from "
574 new = kstrdup(str
, GFP_KERNEL
);
576 printk("line_config - kstrdup failed\n");
579 n
= line_setup(lines
, num
, new);
584 return parse_chan_pair(line
->init_str
, line
, n
, opts
);
587 int line_get_config(char *name
, struct line
*lines
, unsigned int num
, char *str
,
588 int size
, char **error_out
)
594 dev
= simple_strtoul(name
, &end
, 0);
595 if((*end
!= '\0') || (end
== name
)){
596 *error_out
= "line_get_config failed to parse device number";
600 if((dev
< 0) || (dev
>= num
)){
601 *error_out
= "device number out of range";
607 spin_lock(&line
->lock
);
609 CONFIG_CHUNK(str
, size
, n
, "none", 1);
610 else if(line
->tty
== NULL
)
611 CONFIG_CHUNK(str
, size
, n
, line
->init_str
, 1);
612 else n
= chan_config_string(&line
->chan_list
, str
, size
, error_out
);
613 spin_unlock(&line
->lock
);
618 int line_id(char **str
, int *start_out
, int *end_out
)
623 n
= simple_strtoul(*str
, &end
, 0);
624 if((*end
!= '\0') || (end
== *str
))
633 int line_remove(struct line
*lines
, unsigned int num
, int n
)
636 char config
[sizeof("conxxxx=none\0")];
638 sprintf(config
, "%d=none", n
);
639 err
= line_setup(lines
, num
, config
);
645 struct tty_driver
*line_register_devfs(struct lines
*set
,
646 struct line_driver
*line_driver
,
647 struct tty_operations
*ops
, struct line
*lines
,
651 struct tty_driver
*driver
= alloc_tty_driver(nlines
);
656 driver
->driver_name
= line_driver
->name
;
657 driver
->name
= line_driver
->device_name
;
658 driver
->devfs_name
= line_driver
->devfs_name
;
659 driver
->major
= line_driver
->major
;
660 driver
->minor_start
= line_driver
->minor_start
;
661 driver
->type
= line_driver
->type
;
662 driver
->subtype
= line_driver
->subtype
;
663 driver
->flags
= TTY_DRIVER_REAL_RAW
;
664 driver
->init_termios
= tty_std_termios
;
665 tty_set_operations(driver
, ops
);
667 if (tty_register_driver(driver
)) {
668 printk("%s: can't register %s driver\n",
669 __FUNCTION__
,line_driver
->name
);
670 put_tty_driver(driver
);
674 for(i
= 0; i
< nlines
; i
++){
676 tty_unregister_device(driver
, i
);
679 mconsole_register_dev(&line_driver
->mc
);
683 static DEFINE_SPINLOCK(winch_handler_lock
);
684 static LIST_HEAD(winch_handlers
);
686 void lines_init(struct line
*lines
, int nlines
, struct chan_opts
*opts
)
691 for(i
= 0; i
< nlines
; i
++){
693 INIT_LIST_HEAD(&line
->chan_list
);
695 if(line
->init_str
== NULL
)
698 line
->init_str
= kstrdup(line
->init_str
, GFP_KERNEL
);
699 if(line
->init_str
== NULL
)
700 printk("lines_init - kstrdup returned NULL\n");
702 if(parse_chan_pair(line
->init_str
, line
, i
, opts
)){
703 printk("parse_chan_pair failed for device %d\n", i
);
710 struct list_head list
;
714 struct tty_struct
*tty
;
717 static irqreturn_t
winch_interrupt(int irq
, void *data
, struct pt_regs
*unused
)
719 struct winch
*winch
= data
;
720 struct tty_struct
*tty
;
726 err
= generic_read(winch
->fd
, &c
, NULL
);
729 printk("winch_interrupt : read failed, "
730 "errno = %d\n", -err
);
731 printk("fd %d is losing SIGWINCH support\n",
740 line
= tty
->driver_data
;
741 chan_window_size(&line
->chan_list
, &tty
->winsize
.ws_row
,
742 &tty
->winsize
.ws_col
);
743 kill_pg(tty
->pgrp
, SIGWINCH
, 1);
747 reactivate_fd(winch
->fd
, WINCH_IRQ
);
751 void register_winch_irq(int fd
, int tty_fd
, int pid
, struct tty_struct
*tty
)
755 winch
= kmalloc(sizeof(*winch
), GFP_KERNEL
);
757 printk("register_winch_irq - kmalloc failed\n");
761 *winch
= ((struct winch
) { .list
= LIST_HEAD_INIT(winch
->list
),
767 spin_lock(&winch_handler_lock
);
768 list_add(&winch
->list
, &winch_handlers
);
769 spin_unlock(&winch_handler_lock
);
771 if(um_request_irq(WINCH_IRQ
, fd
, IRQ_READ
, winch_interrupt
,
772 SA_INTERRUPT
| SA_SHIRQ
| SA_SAMPLE_RANDOM
,
774 printk("register_winch_irq - failed to register IRQ\n");
777 static void free_winch(struct winch
*winch
)
779 list_del(&winch
->list
);
782 os_kill_process(winch
->pid
, 1);
784 os_close_file(winch
->fd
);
786 free_irq(WINCH_IRQ
, winch
);
790 static void unregister_winch(struct tty_struct
*tty
)
792 struct list_head
*ele
;
795 spin_lock(&winch_handler_lock
);
797 list_for_each(ele
, &winch_handlers
){
798 winch
= list_entry(ele
, struct winch
, list
);
799 if(winch
->tty
== tty
){
804 spin_unlock(&winch_handler_lock
);
807 static void winch_cleanup(void)
809 struct list_head
*ele
, *next
;
812 spin_lock(&winch_handler_lock
);
814 list_for_each_safe(ele
, next
, &winch_handlers
){
815 winch
= list_entry(ele
, struct winch
, list
);
819 spin_unlock(&winch_handler_lock
);
821 __uml_exitcall(winch_cleanup
);
823 char *add_xterm_umid(char *base
)
832 len
= strlen(base
) + strlen(" ()") + strlen(umid
) + 1;
833 title
= kmalloc(len
, GFP_KERNEL
);
835 printk("Failed to allocate buffer for xterm title\n");
839 snprintf(title
, len
, "%s (%s)", base
, umid
);