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 "asm/uaccess.h"
12 #include "chan_kern.h"
16 #include "kern_util.h"
20 #define LINE_BUFSIZE 4096
22 static irqreturn_t
line_interrupt(int irq
, void *data
)
24 struct chan
*chan
= data
;
25 struct line
*line
= chan
->line
;
26 struct tty_struct
*tty
= line
->tty
;
29 chan_interrupt(&line
->chan_list
, &line
->task
, tty
, irq
);
33 static void line_timer_cb(struct work_struct
*work
)
35 struct line
*line
= container_of(work
, struct line
, task
.work
);
38 chan_interrupt(&line
->chan_list
, &line
->task
, line
->tty
,
39 line
->driver
->read_irq
);
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!*/
194 spin_lock_irqsave(&line
->lock
, flags
);
195 err
= flush_buffer(line
);
198 spin_unlock_irqrestore(&line
->lock
, flags
);
202 /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
203 * and ->write. Hope it's not that bad.*/
204 void line_flush_chars(struct tty_struct
*tty
)
206 line_flush_buffer(tty
);
209 void line_put_char(struct tty_struct
*tty
, unsigned char ch
)
211 line_write(tty
, &ch
, sizeof(ch
));
214 int line_write(struct tty_struct
*tty
, const unsigned char *buf
, int len
)
216 struct line
*line
= tty
->driver_data
;
223 spin_lock_irqsave(&line
->lock
, flags
);
224 if (line
->head
!= line
->tail
) {
225 ret
= buffer_data(line
, buf
, len
);
226 err
= flush_buffer(line
);
227 if (err
<= 0 && (err
!= -EAGAIN
|| !ret
))
230 n
= write_chan(&line
->chan_list
, buf
, len
,
231 line
->driver
->write_irq
);
240 ret
+= buffer_data(line
, buf
+ n
, len
);
243 spin_unlock_irqrestore(&line
->lock
, flags
);
247 void line_set_termios(struct tty_struct
*tty
, struct ktermios
* old
)
252 static const struct {
257 /* don't print these, they flood the log ... */
258 { TCGETS
, NULL
, "TCGETS" },
259 { TCSETS
, NULL
, "TCSETS" },
260 { TCSETSW
, NULL
, "TCSETSW" },
261 { TCFLSH
, NULL
, "TCFLSH" },
262 { TCSBRK
, NULL
, "TCSBRK" },
264 /* general tty stuff */
265 { TCSETSF
, KERN_DEBUG
, "TCSETSF" },
266 { TCGETA
, KERN_DEBUG
, "TCGETA" },
267 { TIOCMGET
, KERN_DEBUG
, "TIOCMGET" },
268 { TCSBRKP
, KERN_DEBUG
, "TCSBRKP" },
269 { TIOCMSET
, KERN_DEBUG
, "TIOCMSET" },
271 /* linux-specific ones */
272 { TIOCLINUX
, KERN_INFO
, "TIOCLINUX" },
273 { KDGKBMODE
, KERN_INFO
, "KDGKBMODE" },
274 { KDGKBTYPE
, KERN_INFO
, "KDGKBTYPE" },
275 { KDSIGACCEPT
, KERN_INFO
, "KDSIGACCEPT" },
278 int line_ioctl(struct tty_struct
*tty
, struct file
* file
,
279 unsigned int cmd
, unsigned long arg
)
323 for (i
= 0; i
< ARRAY_SIZE(tty_ioctls
); i
++)
324 if (cmd
== tty_ioctls
[i
].cmd
)
326 if (i
< ARRAY_SIZE(tty_ioctls
)) {
327 if (NULL
!= tty_ioctls
[i
].level
)
328 printk("%s%s: %s: ioctl %s called\n",
329 tty_ioctls
[i
].level
, __FUNCTION__
,
330 tty
->name
, tty_ioctls
[i
].name
);
332 printk(KERN_ERR
"%s: %s: unknown ioctl: 0x%x\n",
333 __FUNCTION__
, tty
->name
, cmd
);
341 void line_throttle(struct tty_struct
*tty
)
343 struct line
*line
= tty
->driver_data
;
345 deactivate_chan(&line
->chan_list
, line
->driver
->read_irq
);
349 void line_unthrottle(struct tty_struct
*tty
)
351 struct line
*line
= tty
->driver_data
;
354 chan_interrupt(&line
->chan_list
, &line
->task
, tty
,
355 line
->driver
->read_irq
);
357 /* Maybe there is enough stuff pending that calling the interrupt
358 * throttles us again. In this case, line->throttled will be 1
359 * again and we shouldn't turn the interrupt back on.
362 reactivate_chan(&line
->chan_list
, line
->driver
->read_irq
);
365 static irqreturn_t
line_write_interrupt(int irq
, void *data
)
367 struct chan
*chan
= data
;
368 struct line
*line
= chan
->line
;
369 struct tty_struct
*tty
= line
->tty
;
372 /* Interrupts are disabled here because we registered the interrupt with
373 * IRQF_DISABLED (see line_setup_irq).*/
375 spin_lock(&line
->lock
);
376 err
= flush_buffer(line
);
380 line
->head
= line
->buffer
;
381 line
->tail
= line
->buffer
;
383 spin_unlock(&line
->lock
);
388 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
) &&
389 (tty
->ldisc
.write_wakeup
!= NULL
))
390 (tty
->ldisc
.write_wakeup
)(tty
);
393 * In blocking mode, everything sleeps on tty->write_wait.
394 * Sleeping in the console driver would break non-blocking
398 if (waitqueue_active(&tty
->write_wait
))
399 wake_up_interruptible(&tty
->write_wait
);
403 int line_setup_irq(int fd
, int input
, int output
, struct line
*line
, void *data
)
405 const struct line_driver
*driver
= line
->driver
;
406 int err
= 0, flags
= IRQF_DISABLED
| IRQF_SHARED
| IRQF_SAMPLE_RANDOM
;
409 err
= um_request_irq(driver
->read_irq
, fd
, IRQ_READ
,
410 line_interrupt
, flags
,
411 driver
->read_irq_name
, data
);
415 err
= um_request_irq(driver
->write_irq
, fd
, IRQ_WRITE
,
416 line_write_interrupt
, flags
,
417 driver
->write_irq_name
, data
);
422 /* Normally, a driver like this can rely mostly on the tty layer
423 * locking, particularly when it comes to the driver structure.
424 * However, in this case, mconsole requests can come in "from the
425 * side", and race with opens and closes.
427 * mconsole config requests will want to be sure the device isn't in
428 * use, and get_config, open, and close will want a stable
429 * configuration. The checking and modification of the configuration
430 * is done under a spinlock. Checking whether the device is in use is
431 * line->tty->count > 1, also under the spinlock.
433 * tty->count serves to decide whether the device should be enabled or
434 * disabled on the host. If it's equal to 1, then we are doing the
435 * first open or last close. Otherwise, open and close just return.
438 int line_open(struct line
*lines
, struct tty_struct
*tty
)
440 struct line
*line
= &lines
[tty
->index
];
443 spin_lock(&line
->count_lock
);
451 spin_unlock(&line
->count_lock
);
453 tty
->driver_data
= line
;
457 INIT_DELAYED_WORK(&line
->task
, line_timer_cb
);
460 chan_enable_winch(&line
->chan_list
, tty
);
464 chan_window_size(&line
->chan_list
, &tty
->winsize
.ws_row
,
465 &tty
->winsize
.ws_col
);
470 spin_unlock(&line
->count_lock
);
474 static void unregister_winch(struct tty_struct
*tty
);
476 void line_close(struct tty_struct
*tty
, struct file
* filp
)
478 struct line
*line
= tty
->driver_data
;
480 /* If line_open fails (and tty->driver_data is never set),
481 * tty_open will call line_close. So just return in this case.
486 /* We ignore the error anyway! */
489 spin_lock(&line
->count_lock
);
496 spin_unlock(&line
->count_lock
);
499 tty
->driver_data
= NULL
;
502 unregister_winch(tty
);
509 spin_unlock(&line
->count_lock
);
512 void close_lines(struct line
*lines
, int nlines
)
516 for(i
= 0; i
< nlines
; i
++)
517 close_chan(&lines
[i
].chan_list
, 0);
520 static int setup_one_line(struct line
*lines
, int n
, char *init
, int init_prio
,
523 struct line
*line
= &lines
[n
];
526 spin_lock(&line
->count_lock
);
528 if(line
->tty
!= NULL
){
529 *error_out
= "Device is already open";
533 if (line
->init_pri
<= init_prio
){
534 line
->init_pri
= init_prio
;
535 if (!strcmp(init
, "none"))
538 line
->init_str
= init
;
544 spin_unlock(&line
->count_lock
);
548 /* Common setup code for both startup command line and mconsole initialization.
549 * @lines contains the array (of size @num) to modify;
550 * @init is the setup string;
551 * @error_out is an error string in the case of failure;
554 int line_setup(struct line
*lines
, unsigned int num
, char *init
,
561 /* We said con=/ssl= instead of con#=, so we are configuring all
562 * consoles at once.*/
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("%s: can't register %s driver\n",
700 __FUNCTION__
,line_driver
->name
);
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("lines_init - kstrdup returned NULL\n");
734 if(parse_chan_pair(line
->init_str
, line
, i
, opts
, &error
)){
735 printk("parse_chan_pair failed for device %d : %s\n",
743 struct list_head list
;
747 struct tty_struct
*tty
;
750 static irqreturn_t
winch_interrupt(int irq
, void *data
)
752 struct winch
*winch
= data
;
753 struct tty_struct
*tty
;
759 err
= generic_read(winch
->fd
, &c
, NULL
);
762 printk("winch_interrupt : read failed, "
763 "errno = %d\n", -err
);
764 printk("fd %d is losing SIGWINCH support\n",
773 line
= tty
->driver_data
;
774 chan_window_size(&line
->chan_list
, &tty
->winsize
.ws_row
,
775 &tty
->winsize
.ws_col
);
776 kill_pgrp(tty
->pgrp
, SIGWINCH
, 1);
780 reactivate_fd(winch
->fd
, WINCH_IRQ
);
784 void register_winch_irq(int fd
, int tty_fd
, int pid
, struct tty_struct
*tty
)
788 winch
= kmalloc(sizeof(*winch
), GFP_KERNEL
);
790 printk("register_winch_irq - kmalloc failed\n");
794 *winch
= ((struct winch
) { .list
= LIST_HEAD_INIT(winch
->list
),
800 spin_lock(&winch_handler_lock
);
801 list_add(&winch
->list
, &winch_handlers
);
802 spin_unlock(&winch_handler_lock
);
804 if(um_request_irq(WINCH_IRQ
, fd
, IRQ_READ
, winch_interrupt
,
805 IRQF_DISABLED
| IRQF_SHARED
| IRQF_SAMPLE_RANDOM
,
807 printk("register_winch_irq - failed to register IRQ\n");
810 static void free_winch(struct winch
*winch
)
812 list_del(&winch
->list
);
815 os_kill_process(winch
->pid
, 1);
817 os_close_file(winch
->fd
);
819 free_irq(WINCH_IRQ
, winch
);
823 static void unregister_winch(struct tty_struct
*tty
)
825 struct list_head
*ele
;
828 spin_lock(&winch_handler_lock
);
830 list_for_each(ele
, &winch_handlers
){
831 winch
= list_entry(ele
, struct winch
, list
);
832 if(winch
->tty
== tty
){
837 spin_unlock(&winch_handler_lock
);
840 static void winch_cleanup(void)
842 struct list_head
*ele
, *next
;
845 spin_lock(&winch_handler_lock
);
847 list_for_each_safe(ele
, next
, &winch_handlers
){
848 winch
= list_entry(ele
, struct winch
, list
);
852 spin_unlock(&winch_handler_lock
);
854 __uml_exitcall(winch_cleanup
);
856 char *add_xterm_umid(char *base
)
865 len
= strlen(base
) + strlen(" ()") + strlen(umid
) + 1;
866 title
= kmalloc(len
, GFP_KERNEL
);
868 printk("Failed to allocate buffer for xterm title\n");
872 snprintf(title
, len
, "%s (%s)", base
, umid
);