]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/um/drivers/chan_kern.c
TTY: switch tty_insert_flip_char
[karo-tx-linux.git] / arch / um / drivers / chan_kern.c
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
3  * Licensed under the GPL
4  */
5
6 #include <linux/slab.h>
7 #include <linux/tty.h>
8 #include <linux/tty_flip.h>
9 #include "chan.h"
10 #include <os.h>
11 #include <irq_kern.h>
12
13 #ifdef CONFIG_NOCONFIG_CHAN
14 static void *not_configged_init(char *str, int device,
15                                 const struct chan_opts *opts)
16 {
17         printk(KERN_ERR "Using a channel type which is configured out of "
18                "UML\n");
19         return NULL;
20 }
21
22 static int not_configged_open(int input, int output, int primary, void *data,
23                               char **dev_out)
24 {
25         printk(KERN_ERR "Using a channel type which is configured out of "
26                "UML\n");
27         return -ENODEV;
28 }
29
30 static void not_configged_close(int fd, void *data)
31 {
32         printk(KERN_ERR "Using a channel type which is configured out of "
33                "UML\n");
34 }
35
36 static int not_configged_read(int fd, char *c_out, void *data)
37 {
38         printk(KERN_ERR "Using a channel type which is configured out of "
39                "UML\n");
40         return -EIO;
41 }
42
43 static int not_configged_write(int fd, const char *buf, int len, void *data)
44 {
45         printk(KERN_ERR "Using a channel type which is configured out of "
46                "UML\n");
47         return -EIO;
48 }
49
50 static int not_configged_console_write(int fd, const char *buf, int len)
51 {
52         printk(KERN_ERR "Using a channel type which is configured out of "
53                "UML\n");
54         return -EIO;
55 }
56
57 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
58                                      unsigned short *cols)
59 {
60         printk(KERN_ERR "Using a channel type which is configured out of "
61                "UML\n");
62         return -ENODEV;
63 }
64
65 static void not_configged_free(void *data)
66 {
67         printk(KERN_ERR "Using a channel type which is configured out of "
68                "UML\n");
69 }
70
71 static const struct chan_ops not_configged_ops = {
72         .init           = not_configged_init,
73         .open           = not_configged_open,
74         .close          = not_configged_close,
75         .read           = not_configged_read,
76         .write          = not_configged_write,
77         .console_write  = not_configged_console_write,
78         .window_size    = not_configged_window_size,
79         .free           = not_configged_free,
80         .winch          = 0,
81 };
82 #endif /* CONFIG_NOCONFIG_CHAN */
83
84 static int open_one_chan(struct chan *chan)
85 {
86         int fd, err;
87
88         if (chan->opened)
89                 return 0;
90
91         if (chan->ops->open == NULL)
92                 fd = 0;
93         else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
94                                      chan->data, &chan->dev);
95         if (fd < 0)
96                 return fd;
97
98         err = os_set_fd_block(fd, 0);
99         if (err) {
100                 (*chan->ops->close)(fd, chan->data);
101                 return err;
102         }
103
104         chan->fd = fd;
105
106         chan->opened = 1;
107         return 0;
108 }
109
110 static int open_chan(struct list_head *chans)
111 {
112         struct list_head *ele;
113         struct chan *chan;
114         int ret, err = 0;
115
116         list_for_each(ele, chans) {
117                 chan = list_entry(ele, struct chan, list);
118                 ret = open_one_chan(chan);
119                 if (chan->primary)
120                         err = ret;
121         }
122         return err;
123 }
124
125 void chan_enable_winch(struct chan *chan, struct tty_struct *tty)
126 {
127         if (chan && chan->primary && chan->ops->winch)
128                 register_winch(chan->fd, tty);
129 }
130
131 static void line_timer_cb(struct work_struct *work)
132 {
133         struct line *line = container_of(work, struct line, task.work);
134         struct tty_struct *tty = tty_port_tty_get(&line->port);
135
136         if (!line->throttled)
137                 chan_interrupt(line, tty, line->driver->read_irq);
138         tty_kref_put(tty);
139 }
140
141 int enable_chan(struct line *line)
142 {
143         struct list_head *ele;
144         struct chan *chan;
145         int err;
146
147         INIT_DELAYED_WORK(&line->task, line_timer_cb);
148
149         list_for_each(ele, &line->chan_list) {
150                 chan = list_entry(ele, struct chan, list);
151                 err = open_one_chan(chan);
152                 if (err) {
153                         if (chan->primary)
154                                 goto out_close;
155
156                         continue;
157                 }
158
159                 if (chan->enabled)
160                         continue;
161                 err = line_setup_irq(chan->fd, chan->input, chan->output, line,
162                                      chan);
163                 if (err)
164                         goto out_close;
165
166                 chan->enabled = 1;
167         }
168
169         return 0;
170
171  out_close:
172         close_chan(line);
173         return err;
174 }
175
176 /* Items are added in IRQ context, when free_irq can't be called, and
177  * removed in process context, when it can.
178  * This handles interrupt sources which disappear, and which need to
179  * be permanently disabled.  This is discovered in IRQ context, but
180  * the freeing of the IRQ must be done later.
181  */
182 static DEFINE_SPINLOCK(irqs_to_free_lock);
183 static LIST_HEAD(irqs_to_free);
184
185 void free_irqs(void)
186 {
187         struct chan *chan;
188         LIST_HEAD(list);
189         struct list_head *ele;
190         unsigned long flags;
191
192         spin_lock_irqsave(&irqs_to_free_lock, flags);
193         list_splice_init(&irqs_to_free, &list);
194         spin_unlock_irqrestore(&irqs_to_free_lock, flags);
195
196         list_for_each(ele, &list) {
197                 chan = list_entry(ele, struct chan, free_list);
198
199                 if (chan->input && chan->enabled)
200                         um_free_irq(chan->line->driver->read_irq, chan);
201                 if (chan->output && chan->enabled)
202                         um_free_irq(chan->line->driver->write_irq, chan);
203                 chan->enabled = 0;
204         }
205 }
206
207 static void close_one_chan(struct chan *chan, int delay_free_irq)
208 {
209         unsigned long flags;
210
211         if (!chan->opened)
212                 return;
213
214         if (delay_free_irq) {
215                 spin_lock_irqsave(&irqs_to_free_lock, flags);
216                 list_add(&chan->free_list, &irqs_to_free);
217                 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
218         }
219         else {
220                 if (chan->input && chan->enabled)
221                         um_free_irq(chan->line->driver->read_irq, chan);
222                 if (chan->output && chan->enabled)
223                         um_free_irq(chan->line->driver->write_irq, chan);
224                 chan->enabled = 0;
225         }
226         if (chan->ops->close != NULL)
227                 (*chan->ops->close)(chan->fd, chan->data);
228
229         chan->opened = 0;
230         chan->fd = -1;
231 }
232
233 void close_chan(struct line *line)
234 {
235         struct chan *chan;
236
237         /* Close in reverse order as open in case more than one of them
238          * refers to the same device and they save and restore that device's
239          * state.  Then, the first one opened will have the original state,
240          * so it must be the last closed.
241          */
242         list_for_each_entry_reverse(chan, &line->chan_list, list) {
243                 close_one_chan(chan, 0);
244         }
245 }
246
247 void deactivate_chan(struct chan *chan, int irq)
248 {
249         if (chan && chan->enabled)
250                 deactivate_fd(chan->fd, irq);
251 }
252
253 void reactivate_chan(struct chan *chan, int irq)
254 {
255         if (chan && chan->enabled)
256                 reactivate_fd(chan->fd, irq);
257 }
258
259 int write_chan(struct chan *chan, const char *buf, int len,
260                int write_irq)
261 {
262         int n, ret = 0;
263
264         if (len == 0 || !chan || !chan->ops->write)
265                 return 0;
266
267         n = chan->ops->write(chan->fd, buf, len, chan->data);
268         if (chan->primary) {
269                 ret = n;
270                 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
271                         reactivate_fd(chan->fd, write_irq);
272         }
273         return ret;
274 }
275
276 int console_write_chan(struct chan *chan, const char *buf, int len)
277 {
278         int n, ret = 0;
279
280         if (!chan || !chan->ops->console_write)
281                 return 0;
282
283         n = chan->ops->console_write(chan->fd, buf, len);
284         if (chan->primary)
285                 ret = n;
286         return ret;
287 }
288
289 int console_open_chan(struct line *line, struct console *co)
290 {
291         int err;
292
293         err = open_chan(&line->chan_list);
294         if (err)
295                 return err;
296
297         printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
298                co->index);
299         return 0;
300 }
301
302 int chan_window_size(struct line *line, unsigned short *rows_out,
303                       unsigned short *cols_out)
304 {
305         struct chan *chan;
306
307         chan = line->chan_in;
308         if (chan && chan->primary) {
309                 if (chan->ops->window_size == NULL)
310                         return 0;
311                 return chan->ops->window_size(chan->fd, chan->data,
312                                               rows_out, cols_out);
313         }
314         chan = line->chan_out;
315         if (chan && chan->primary) {
316                 if (chan->ops->window_size == NULL)
317                         return 0;
318                 return chan->ops->window_size(chan->fd, chan->data,
319                                               rows_out, cols_out);
320         }
321         return 0;
322 }
323
324 static void free_one_chan(struct chan *chan)
325 {
326         list_del(&chan->list);
327
328         close_one_chan(chan, 0);
329
330         if (chan->ops->free != NULL)
331                 (*chan->ops->free)(chan->data);
332
333         if (chan->primary && chan->output)
334                 ignore_sigio_fd(chan->fd);
335         kfree(chan);
336 }
337
338 static void free_chan(struct list_head *chans)
339 {
340         struct list_head *ele, *next;
341         struct chan *chan;
342
343         list_for_each_safe(ele, next, chans) {
344                 chan = list_entry(ele, struct chan, list);
345                 free_one_chan(chan);
346         }
347 }
348
349 static int one_chan_config_string(struct chan *chan, char *str, int size,
350                                   char **error_out)
351 {
352         int n = 0;
353
354         if (chan == NULL) {
355                 CONFIG_CHUNK(str, size, n, "none", 1);
356                 return n;
357         }
358
359         CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
360
361         if (chan->dev == NULL) {
362                 CONFIG_CHUNK(str, size, n, "", 1);
363                 return n;
364         }
365
366         CONFIG_CHUNK(str, size, n, ":", 0);
367         CONFIG_CHUNK(str, size, n, chan->dev, 0);
368
369         return n;
370 }
371
372 static int chan_pair_config_string(struct chan *in, struct chan *out,
373                                    char *str, int size, char **error_out)
374 {
375         int n;
376
377         n = one_chan_config_string(in, str, size, error_out);
378         str += n;
379         size -= n;
380
381         if (in == out) {
382                 CONFIG_CHUNK(str, size, n, "", 1);
383                 return n;
384         }
385
386         CONFIG_CHUNK(str, size, n, ",", 1);
387         n = one_chan_config_string(out, str, size, error_out);
388         str += n;
389         size -= n;
390         CONFIG_CHUNK(str, size, n, "", 1);
391
392         return n;
393 }
394
395 int chan_config_string(struct line *line, char *str, int size,
396                        char **error_out)
397 {
398         struct chan *in = line->chan_in, *out = line->chan_out;
399
400         if (in && !in->primary)
401                 in = NULL;
402         if (out && !out->primary)
403                 out = NULL;
404
405         return chan_pair_config_string(in, out, str, size, error_out);
406 }
407
408 struct chan_type {
409         char *key;
410         const struct chan_ops *ops;
411 };
412
413 static const struct chan_type chan_table[] = {
414         { "fd", &fd_ops },
415
416 #ifdef CONFIG_NULL_CHAN
417         { "null", &null_ops },
418 #else
419         { "null", &not_configged_ops },
420 #endif
421
422 #ifdef CONFIG_PORT_CHAN
423         { "port", &port_ops },
424 #else
425         { "port", &not_configged_ops },
426 #endif
427
428 #ifdef CONFIG_PTY_CHAN
429         { "pty", &pty_ops },
430         { "pts", &pts_ops },
431 #else
432         { "pty", &not_configged_ops },
433         { "pts", &not_configged_ops },
434 #endif
435
436 #ifdef CONFIG_TTY_CHAN
437         { "tty", &tty_ops },
438 #else
439         { "tty", &not_configged_ops },
440 #endif
441
442 #ifdef CONFIG_XTERM_CHAN
443         { "xterm", &xterm_ops },
444 #else
445         { "xterm", &not_configged_ops },
446 #endif
447 };
448
449 static struct chan *parse_chan(struct line *line, char *str, int device,
450                                const struct chan_opts *opts, char **error_out)
451 {
452         const struct chan_type *entry;
453         const struct chan_ops *ops;
454         struct chan *chan;
455         void *data;
456         int i;
457
458         ops = NULL;
459         data = NULL;
460         for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
461                 entry = &chan_table[i];
462                 if (!strncmp(str, entry->key, strlen(entry->key))) {
463                         ops = entry->ops;
464                         str += strlen(entry->key);
465                         break;
466                 }
467         }
468         if (ops == NULL) {
469                 *error_out = "No match for configured backends";
470                 return NULL;
471         }
472
473         data = (*ops->init)(str, device, opts);
474         if (data == NULL) {
475                 *error_out = "Configuration failed";
476                 return NULL;
477         }
478
479         chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
480         if (chan == NULL) {
481                 *error_out = "Memory allocation failed";
482                 return NULL;
483         }
484         *chan = ((struct chan) { .list          = LIST_HEAD_INIT(chan->list),
485                                  .free_list     =
486                                         LIST_HEAD_INIT(chan->free_list),
487                                  .line          = line,
488                                  .primary       = 1,
489                                  .input         = 0,
490                                  .output        = 0,
491                                  .opened        = 0,
492                                  .enabled       = 0,
493                                  .fd            = -1,
494                                  .ops           = ops,
495                                  .data          = data });
496         return chan;
497 }
498
499 int parse_chan_pair(char *str, struct line *line, int device,
500                     const struct chan_opts *opts, char **error_out)
501 {
502         struct list_head *chans = &line->chan_list;
503         struct chan *new;
504         char *in, *out;
505
506         if (!list_empty(chans)) {
507                 line->chan_in = line->chan_out = NULL;
508                 free_chan(chans);
509                 INIT_LIST_HEAD(chans);
510         }
511
512         if (!str)
513                 return 0;
514
515         out = strchr(str, ',');
516         if (out != NULL) {
517                 in = str;
518                 *out = '\0';
519                 out++;
520                 new = parse_chan(line, in, device, opts, error_out);
521                 if (new == NULL)
522                         return -1;
523
524                 new->input = 1;
525                 list_add(&new->list, chans);
526                 line->chan_in = new;
527
528                 new = parse_chan(line, out, device, opts, error_out);
529                 if (new == NULL)
530                         return -1;
531
532                 list_add(&new->list, chans);
533                 new->output = 1;
534                 line->chan_out = new;
535         }
536         else {
537                 new = parse_chan(line, str, device, opts, error_out);
538                 if (new == NULL)
539                         return -1;
540
541                 list_add(&new->list, chans);
542                 new->input = 1;
543                 new->output = 1;
544                 line->chan_in = line->chan_out = new;
545         }
546         return 0;
547 }
548
549 void chan_interrupt(struct line *line, struct tty_struct *tty, int irq)
550 {
551         struct tty_port *port = &line->port;
552         struct chan *chan = line->chan_in;
553         int err;
554         char c;
555
556         if (!chan || !chan->ops->read)
557                 goto out;
558
559         do {
560                 if (!tty_buffer_request_room(port, 1)) {
561                         schedule_delayed_work(&line->task, 1);
562                         goto out;
563                 }
564                 err = chan->ops->read(chan->fd, &c, chan->data);
565                 if (err > 0)
566                         tty_insert_flip_char(port, c, TTY_NORMAL);
567         } while (err > 0);
568
569         if (err == 0)
570                 reactivate_fd(chan->fd, irq);
571         if (err == -EIO) {
572                 if (chan->primary) {
573                         if (tty != NULL)
574                                 tty_hangup(tty);
575                         if (line->chan_out != chan)
576                                 close_one_chan(line->chan_out, 1);
577                 }
578                 close_one_chan(chan, 1);
579                 if (chan->primary)
580                         return;
581         }
582  out:
583         if (tty)
584                 tty_flip_buffer_push(tty);
585 }