USB-SERIAL:cp2101 Add new device ID
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / um / drivers / chan_kern.c
blob3576b3cc505e65432a3068dbce33f6aecc1f666b
1 /*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
6 #include <linux/stddef.h>
7 #include <linux/kernel.h>
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/tty.h>
11 #include <linux/string.h>
12 #include <linux/tty_flip.h>
13 #include <asm/irq.h>
14 #include "chan_kern.h"
15 #include "user_util.h"
16 #include "kern.h"
17 #include "irq_user.h"
18 #include "sigio.h"
19 #include "line.h"
20 #include "os.h"
22 /* XXX: could well be moved to somewhere else, if needed. */
23 static int my_printf(const char * fmt, ...)
24 __attribute__ ((format (printf, 1, 2)));
26 static int my_printf(const char * fmt, ...)
28 /* Yes, can be called on atomic context.*/
29 char *buf = kmalloc(4096, GFP_ATOMIC);
30 va_list args;
31 int r;
33 if (!buf) {
34 /* We print directly fmt.
35 * Yes, yes, yes, feel free to complain. */
36 r = strlen(fmt);
37 } else {
38 va_start(args, fmt);
39 r = vsprintf(buf, fmt, args);
40 va_end(args);
41 fmt = buf;
44 if (r)
45 r = os_write_file(1, fmt, r);
46 return r;
50 #ifdef CONFIG_NOCONFIG_CHAN
51 /* Despite its name, there's no added trailing newline. */
52 static int my_puts(const char * buf)
54 return os_write_file(1, buf, strlen(buf));
57 static void *not_configged_init(char *str, int device, struct chan_opts *opts)
59 my_puts("Using a channel type which is configured out of "
60 "UML\n");
61 return NULL;
64 static int not_configged_open(int input, int output, int primary, void *data,
65 char **dev_out)
67 my_puts("Using a channel type which is configured out of "
68 "UML\n");
69 return -ENODEV;
72 static void not_configged_close(int fd, void *data)
74 my_puts("Using a channel type which is configured out of "
75 "UML\n");
78 static int not_configged_read(int fd, char *c_out, void *data)
80 my_puts("Using a channel type which is configured out of "
81 "UML\n");
82 return -EIO;
85 static int not_configged_write(int fd, const char *buf, int len, void *data)
87 my_puts("Using a channel type which is configured out of "
88 "UML\n");
89 return -EIO;
92 static int not_configged_console_write(int fd, const char *buf, int len)
94 my_puts("Using a channel type which is configured out of "
95 "UML\n");
96 return -EIO;
99 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
100 unsigned short *cols)
102 my_puts("Using a channel type which is configured out of "
103 "UML\n");
104 return -ENODEV;
107 static void not_configged_free(void *data)
109 my_puts("Using a channel type which is configured out of "
110 "UML\n");
113 static const struct chan_ops not_configged_ops = {
114 .init = not_configged_init,
115 .open = not_configged_open,
116 .close = not_configged_close,
117 .read = not_configged_read,
118 .write = not_configged_write,
119 .console_write = not_configged_console_write,
120 .window_size = not_configged_window_size,
121 .free = not_configged_free,
122 .winch = 0,
124 #endif /* CONFIG_NOCONFIG_CHAN */
126 void generic_close(int fd, void *unused)
128 os_close_file(fd);
131 int generic_read(int fd, char *c_out, void *unused)
133 int n;
135 n = os_read_file(fd, c_out, sizeof(*c_out));
137 if(n == -EAGAIN)
138 return 0;
139 else if(n == 0)
140 return -EIO;
141 return n;
144 /* XXX Trivial wrapper around os_write_file */
146 int generic_write(int fd, const char *buf, int n, void *unused)
148 return os_write_file(fd, buf, n);
151 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
152 unsigned short *cols_out)
154 int rows, cols;
155 int ret;
157 ret = os_window_size(fd, &rows, &cols);
158 if(ret < 0)
159 return ret;
161 ret = ((*rows_out != rows) || (*cols_out != cols));
163 *rows_out = rows;
164 *cols_out = cols;
166 return ret;
169 void generic_free(void *data)
171 kfree(data);
174 static void tty_receive_char(struct tty_struct *tty, char ch)
176 if(tty == NULL) return;
178 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
179 if(ch == STOP_CHAR(tty)){
180 stop_tty(tty);
181 return;
183 else if(ch == START_CHAR(tty)){
184 start_tty(tty);
185 return;
189 tty_insert_flip_char(tty, ch, TTY_NORMAL);
192 static int open_one_chan(struct chan *chan)
194 int fd;
196 if(chan->opened)
197 return 0;
199 if(chan->ops->open == NULL)
200 fd = 0;
201 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
202 chan->data, &chan->dev);
203 if(fd < 0)
204 return fd;
205 chan->fd = fd;
207 chan->opened = 1;
208 return 0;
211 int open_chan(struct list_head *chans)
213 struct list_head *ele;
214 struct chan *chan;
215 int ret, err = 0;
217 list_for_each(ele, chans){
218 chan = list_entry(ele, struct chan, list);
219 ret = open_one_chan(chan);
220 if(chan->primary)
221 err = ret;
223 return err;
226 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
228 struct list_head *ele;
229 struct chan *chan;
231 list_for_each(ele, chans){
232 chan = list_entry(ele, struct chan, list);
233 if(chan->primary && chan->output && chan->ops->winch){
234 register_winch(chan->fd, tty);
235 return;
240 void enable_chan(struct line *line)
242 struct list_head *ele;
243 struct chan *chan;
245 list_for_each(ele, &line->chan_list){
246 chan = list_entry(ele, struct chan, list);
247 if(open_one_chan(chan))
248 continue;
250 if(chan->enabled)
251 continue;
252 line_setup_irq(chan->fd, chan->input, chan->output, line,
253 chan);
254 chan->enabled = 1;
258 static LIST_HEAD(irqs_to_free);
260 void free_irqs(void)
262 struct chan *chan;
264 while(!list_empty(&irqs_to_free)){
265 chan = list_entry(irqs_to_free.next, struct chan, free_list);
266 list_del(&chan->free_list);
268 if(chan->input)
269 free_irq(chan->line->driver->read_irq, chan);
270 if(chan->output)
271 free_irq(chan->line->driver->write_irq, chan);
272 chan->enabled = 0;
276 static void close_one_chan(struct chan *chan, int delay_free_irq)
278 if(!chan->opened)
279 return;
281 if(delay_free_irq){
282 list_add(&chan->free_list, &irqs_to_free);
284 else {
285 if(chan->input)
286 free_irq(chan->line->driver->read_irq, chan);
287 if(chan->output)
288 free_irq(chan->line->driver->write_irq, chan);
289 chan->enabled = 0;
291 if(chan->ops->close != NULL)
292 (*chan->ops->close)(chan->fd, chan->data);
294 chan->opened = 0;
295 chan->fd = -1;
298 void close_chan(struct list_head *chans, int delay_free_irq)
300 struct chan *chan;
302 /* Close in reverse order as open in case more than one of them
303 * refers to the same device and they save and restore that device's
304 * state. Then, the first one opened will have the original state,
305 * so it must be the last closed.
307 list_for_each_entry_reverse(chan, chans, list) {
308 close_one_chan(chan, delay_free_irq);
312 void deactivate_chan(struct list_head *chans, int irq)
314 struct list_head *ele;
316 struct chan *chan;
317 list_for_each(ele, chans) {
318 chan = list_entry(ele, struct chan, list);
320 if(chan->enabled && chan->input)
321 deactivate_fd(chan->fd, irq);
325 void reactivate_chan(struct list_head *chans, int irq)
327 struct list_head *ele;
328 struct chan *chan;
330 list_for_each(ele, chans) {
331 chan = list_entry(ele, struct chan, list);
333 if(chan->enabled && chan->input)
334 reactivate_fd(chan->fd, irq);
338 int write_chan(struct list_head *chans, const char *buf, int len,
339 int write_irq)
341 struct list_head *ele;
342 struct chan *chan = NULL;
343 int n, ret = 0;
345 list_for_each(ele, chans) {
346 chan = list_entry(ele, struct chan, list);
347 if (!chan->output || (chan->ops->write == NULL))
348 continue;
349 n = chan->ops->write(chan->fd, buf, len, chan->data);
350 if (chan->primary) {
351 ret = n;
352 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
353 reactivate_fd(chan->fd, write_irq);
356 return ret;
359 int console_write_chan(struct list_head *chans, const char *buf, int len)
361 struct list_head *ele;
362 struct chan *chan;
363 int n, ret = 0;
365 list_for_each(ele, chans){
366 chan = list_entry(ele, struct chan, list);
367 if(!chan->output || (chan->ops->console_write == NULL))
368 continue;
369 n = chan->ops->console_write(chan->fd, buf, len);
370 if(chan->primary) ret = n;
372 return ret;
375 int console_open_chan(struct line *line, struct console *co,
376 const struct chan_opts *opts)
378 int err;
380 err = open_chan(&line->chan_list);
381 if(err)
382 return err;
384 printk("Console initialized on /dev/%s%d\n",co->name,co->index);
385 return 0;
388 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
389 unsigned short *cols_out)
391 struct list_head *ele;
392 struct chan *chan;
394 list_for_each(ele, chans){
395 chan = list_entry(ele, struct chan, list);
396 if(chan->primary){
397 if(chan->ops->window_size == NULL)
398 return 0;
399 return chan->ops->window_size(chan->fd, chan->data,
400 rows_out, cols_out);
403 return 0;
406 static void free_one_chan(struct chan *chan, int delay_free_irq)
408 list_del(&chan->list);
410 close_one_chan(chan, delay_free_irq);
412 if(chan->ops->free != NULL)
413 (*chan->ops->free)(chan->data);
415 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
416 kfree(chan);
419 static void free_chan(struct list_head *chans, int delay_free_irq)
421 struct list_head *ele, *next;
422 struct chan *chan;
424 list_for_each_safe(ele, next, chans){
425 chan = list_entry(ele, struct chan, list);
426 free_one_chan(chan, delay_free_irq);
430 static int one_chan_config_string(struct chan *chan, char *str, int size,
431 char **error_out)
433 int n = 0;
435 if(chan == NULL){
436 CONFIG_CHUNK(str, size, n, "none", 1);
437 return n;
440 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
442 if(chan->dev == NULL){
443 CONFIG_CHUNK(str, size, n, "", 1);
444 return n;
447 CONFIG_CHUNK(str, size, n, ":", 0);
448 CONFIG_CHUNK(str, size, n, chan->dev, 0);
450 return n;
453 static int chan_pair_config_string(struct chan *in, struct chan *out,
454 char *str, int size, char **error_out)
456 int n;
458 n = one_chan_config_string(in, str, size, error_out);
459 str += n;
460 size -= n;
462 if(in == out){
463 CONFIG_CHUNK(str, size, n, "", 1);
464 return n;
467 CONFIG_CHUNK(str, size, n, ",", 1);
468 n = one_chan_config_string(out, str, size, error_out);
469 str += n;
470 size -= n;
471 CONFIG_CHUNK(str, size, n, "", 1);
473 return n;
476 int chan_config_string(struct list_head *chans, char *str, int size,
477 char **error_out)
479 struct list_head *ele;
480 struct chan *chan, *in = NULL, *out = NULL;
482 list_for_each(ele, chans){
483 chan = list_entry(ele, struct chan, list);
484 if(!chan->primary)
485 continue;
486 if(chan->input)
487 in = chan;
488 if(chan->output)
489 out = chan;
492 return chan_pair_config_string(in, out, str, size, error_out);
495 struct chan_type {
496 char *key;
497 const struct chan_ops *ops;
500 static const struct chan_type chan_table[] = {
501 { "fd", &fd_ops },
503 #ifdef CONFIG_NULL_CHAN
504 { "null", &null_ops },
505 #else
506 { "null", &not_configged_ops },
507 #endif
509 #ifdef CONFIG_PORT_CHAN
510 { "port", &port_ops },
511 #else
512 { "port", &not_configged_ops },
513 #endif
515 #ifdef CONFIG_PTY_CHAN
516 { "pty", &pty_ops },
517 { "pts", &pts_ops },
518 #else
519 { "pty", &not_configged_ops },
520 { "pts", &not_configged_ops },
521 #endif
523 #ifdef CONFIG_TTY_CHAN
524 { "tty", &tty_ops },
525 #else
526 { "tty", &not_configged_ops },
527 #endif
529 #ifdef CONFIG_XTERM_CHAN
530 { "xterm", &xterm_ops },
531 #else
532 { "xterm", &not_configged_ops },
533 #endif
536 static struct chan *parse_chan(struct line *line, char *str, int device,
537 const struct chan_opts *opts)
539 const struct chan_type *entry;
540 const struct chan_ops *ops;
541 struct chan *chan;
542 void *data;
543 int i;
545 ops = NULL;
546 data = NULL;
547 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
548 entry = &chan_table[i];
549 if(!strncmp(str, entry->key, strlen(entry->key))){
550 ops = entry->ops;
551 str += strlen(entry->key);
552 break;
555 if(ops == NULL){
556 my_printf("parse_chan couldn't parse \"%s\"\n",
557 str);
558 return NULL;
560 if(ops->init == NULL)
561 return NULL;
562 data = (*ops->init)(str, device, opts);
563 if(data == NULL)
564 return NULL;
566 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
567 if(chan == NULL)
568 return NULL;
569 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
570 .free_list =
571 LIST_HEAD_INIT(chan->free_list),
572 .line = line,
573 .primary = 1,
574 .input = 0,
575 .output = 0,
576 .opened = 0,
577 .enabled = 0,
578 .fd = -1,
579 .ops = ops,
580 .data = data });
581 return chan;
584 int parse_chan_pair(char *str, struct line *line, int device,
585 const struct chan_opts *opts)
587 struct list_head *chans = &line->chan_list;
588 struct chan *new, *chan;
589 char *in, *out;
591 if(!list_empty(chans)){
592 chan = list_entry(chans->next, struct chan, list);
593 free_chan(chans, 0);
594 INIT_LIST_HEAD(chans);
597 out = strchr(str, ',');
598 if(out != NULL){
599 in = str;
600 *out = '\0';
601 out++;
602 new = parse_chan(line, in, device, opts);
603 if(new == NULL)
604 return -1;
606 new->input = 1;
607 list_add(&new->list, chans);
609 new = parse_chan(line, out, device, opts);
610 if(new == NULL)
611 return -1;
613 list_add(&new->list, chans);
614 new->output = 1;
616 else {
617 new = parse_chan(line, str, device, opts);
618 if(new == NULL)
619 return -1;
621 list_add(&new->list, chans);
622 new->input = 1;
623 new->output = 1;
625 return 0;
628 int chan_out_fd(struct list_head *chans)
630 struct list_head *ele;
631 struct chan *chan;
633 list_for_each(ele, chans){
634 chan = list_entry(ele, struct chan, list);
635 if(chan->primary && chan->output)
636 return chan->fd;
638 return -1;
641 void chan_interrupt(struct list_head *chans, struct work_struct *task,
642 struct tty_struct *tty, int irq)
644 struct list_head *ele, *next;
645 struct chan *chan;
646 int err;
647 char c;
649 list_for_each_safe(ele, next, chans){
650 chan = list_entry(ele, struct chan, list);
651 if(!chan->input || (chan->ops->read == NULL)) continue;
652 do {
653 if (tty && !tty_buffer_request_room(tty, 1)) {
654 schedule_delayed_work(task, 1);
655 goto out;
657 err = chan->ops->read(chan->fd, &c, chan->data);
658 if(err > 0)
659 tty_receive_char(tty, c);
660 } while(err > 0);
662 if(err == 0) reactivate_fd(chan->fd, irq);
663 if(err == -EIO){
664 if(chan->primary){
665 if(tty != NULL)
666 tty_hangup(tty);
667 close_chan(chans, 1);
668 return;
670 else close_one_chan(chan, 1);
673 out:
674 if(tty) tty_flip_buffer_push(tty);