GNU Linux-libre 4.19.245-gnu1
[releases.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_port *port)
126 {
127         if (chan && chan->primary && chan->ops->winch)
128                 register_winch(chan->fd, port);
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
135         if (!line->throttled)
136                 chan_interrupt(line, line->driver->read_irq);
137 }
138
139 int enable_chan(struct line *line)
140 {
141         struct list_head *ele;
142         struct chan *chan;
143         int err;
144
145         INIT_DELAYED_WORK(&line->task, line_timer_cb);
146
147         list_for_each(ele, &line->chan_list) {
148                 chan = list_entry(ele, struct chan, list);
149                 err = open_one_chan(chan);
150                 if (err) {
151                         if (chan->primary)
152                                 goto out_close;
153
154                         continue;
155                 }
156
157                 if (chan->enabled)
158                         continue;
159                 err = line_setup_irq(chan->fd, chan->input, chan->output, line,
160                                      chan);
161                 if (err)
162                         goto out_close;
163
164                 chan->enabled = 1;
165         }
166
167         return 0;
168
169  out_close:
170         close_chan(line);
171         return err;
172 }
173
174 /* Items are added in IRQ context, when free_irq can't be called, and
175  * removed in process context, when it can.
176  * This handles interrupt sources which disappear, and which need to
177  * be permanently disabled.  This is discovered in IRQ context, but
178  * the freeing of the IRQ must be done later.
179  */
180 static DEFINE_SPINLOCK(irqs_to_free_lock);
181 static LIST_HEAD(irqs_to_free);
182
183 void free_irqs(void)
184 {
185         struct chan *chan;
186         LIST_HEAD(list);
187         struct list_head *ele;
188         unsigned long flags;
189
190         spin_lock_irqsave(&irqs_to_free_lock, flags);
191         list_splice_init(&irqs_to_free, &list);
192         spin_unlock_irqrestore(&irqs_to_free_lock, flags);
193
194         list_for_each(ele, &list) {
195                 chan = list_entry(ele, struct chan, free_list);
196
197                 if (chan->input && chan->enabled)
198                         um_free_irq(chan->line->driver->read_irq, chan);
199                 if (chan->output && chan->enabled)
200                         um_free_irq(chan->line->driver->write_irq, chan);
201                 chan->enabled = 0;
202         }
203 }
204
205 static void close_one_chan(struct chan *chan, int delay_free_irq)
206 {
207         unsigned long flags;
208
209         if (!chan->opened)
210                 return;
211
212         if (delay_free_irq) {
213                 spin_lock_irqsave(&irqs_to_free_lock, flags);
214                 list_add(&chan->free_list, &irqs_to_free);
215                 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
216         } else {
217                 if (chan->input && chan->enabled)
218                         um_free_irq(chan->line->driver->read_irq, chan);
219                 if (chan->output && chan->enabled)
220                         um_free_irq(chan->line->driver->write_irq, chan);
221                 chan->enabled = 0;
222         }
223         if (chan->ops->close != NULL)
224                 (*chan->ops->close)(chan->fd, chan->data);
225
226         chan->opened = 0;
227         chan->fd = -1;
228 }
229
230 void close_chan(struct line *line)
231 {
232         struct chan *chan;
233
234         /* Close in reverse order as open in case more than one of them
235          * refers to the same device and they save and restore that device's
236          * state.  Then, the first one opened will have the original state,
237          * so it must be the last closed.
238          */
239         list_for_each_entry_reverse(chan, &line->chan_list, list) {
240                 close_one_chan(chan, 0);
241         }
242 }
243
244 void deactivate_chan(struct chan *chan, int irq)
245 {
246         if (chan && chan->enabled)
247                 deactivate_fd(chan->fd, irq);
248 }
249
250 void reactivate_chan(struct chan *chan, int irq)
251 {
252         if (chan && chan->enabled)
253                 reactivate_fd(chan->fd, irq);
254 }
255
256 int write_chan(struct chan *chan, const char *buf, int len,
257                int write_irq)
258 {
259         int n, ret = 0;
260
261         if (len == 0 || !chan || !chan->ops->write)
262                 return 0;
263
264         n = chan->ops->write(chan->fd, buf, len, chan->data);
265         if (chan->primary) {
266                 ret = n;
267                 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
268                         reactivate_fd(chan->fd, write_irq);
269         }
270         return ret;
271 }
272
273 int console_write_chan(struct chan *chan, const char *buf, int len)
274 {
275         int n, ret = 0;
276
277         if (!chan || !chan->ops->console_write)
278                 return 0;
279
280         n = chan->ops->console_write(chan->fd, buf, len);
281         if (chan->primary)
282                 ret = n;
283         return ret;
284 }
285
286 int console_open_chan(struct line *line, struct console *co)
287 {
288         int err;
289
290         err = open_chan(&line->chan_list);
291         if (err)
292                 return err;
293
294         printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
295                co->index);
296         return 0;
297 }
298
299 int chan_window_size(struct line *line, unsigned short *rows_out,
300                       unsigned short *cols_out)
301 {
302         struct chan *chan;
303
304         chan = line->chan_in;
305         if (chan && chan->primary) {
306                 if (chan->ops->window_size == NULL)
307                         return 0;
308                 return chan->ops->window_size(chan->fd, chan->data,
309                                               rows_out, cols_out);
310         }
311         chan = line->chan_out;
312         if (chan && chan->primary) {
313                 if (chan->ops->window_size == NULL)
314                         return 0;
315                 return chan->ops->window_size(chan->fd, chan->data,
316                                               rows_out, cols_out);
317         }
318         return 0;
319 }
320
321 static void free_one_chan(struct chan *chan)
322 {
323         list_del(&chan->list);
324
325         close_one_chan(chan, 0);
326
327         if (chan->ops->free != NULL)
328                 (*chan->ops->free)(chan->data);
329
330         if (chan->primary && chan->output)
331                 ignore_sigio_fd(chan->fd);
332         kfree(chan);
333 }
334
335 static void free_chan(struct list_head *chans)
336 {
337         struct list_head *ele, *next;
338         struct chan *chan;
339
340         list_for_each_safe(ele, next, chans) {
341                 chan = list_entry(ele, struct chan, list);
342                 free_one_chan(chan);
343         }
344 }
345
346 static int one_chan_config_string(struct chan *chan, char *str, int size,
347                                   char **error_out)
348 {
349         int n = 0;
350
351         if (chan == NULL) {
352                 CONFIG_CHUNK(str, size, n, "none", 1);
353                 return n;
354         }
355
356         CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
357
358         if (chan->dev == NULL) {
359                 CONFIG_CHUNK(str, size, n, "", 1);
360                 return n;
361         }
362
363         CONFIG_CHUNK(str, size, n, ":", 0);
364         CONFIG_CHUNK(str, size, n, chan->dev, 0);
365
366         return n;
367 }
368
369 static int chan_pair_config_string(struct chan *in, struct chan *out,
370                                    char *str, int size, char **error_out)
371 {
372         int n;
373
374         n = one_chan_config_string(in, str, size, error_out);
375         str += n;
376         size -= n;
377
378         if (in == out) {
379                 CONFIG_CHUNK(str, size, n, "", 1);
380                 return n;
381         }
382
383         CONFIG_CHUNK(str, size, n, ",", 1);
384         n = one_chan_config_string(out, str, size, error_out);
385         str += n;
386         size -= n;
387         CONFIG_CHUNK(str, size, n, "", 1);
388
389         return n;
390 }
391
392 int chan_config_string(struct line *line, char *str, int size,
393                        char **error_out)
394 {
395         struct chan *in = line->chan_in, *out = line->chan_out;
396
397         if (in && !in->primary)
398                 in = NULL;
399         if (out && !out->primary)
400                 out = NULL;
401
402         return chan_pair_config_string(in, out, str, size, error_out);
403 }
404
405 struct chan_type {
406         char *key;
407         const struct chan_ops *ops;
408 };
409
410 static const struct chan_type chan_table[] = {
411         { "fd", &fd_ops },
412
413 #ifdef CONFIG_NULL_CHAN
414         { "null", &null_ops },
415 #else
416         { "null", &not_configged_ops },
417 #endif
418
419 #ifdef CONFIG_PORT_CHAN
420         { "port", &port_ops },
421 #else
422         { "port", &not_configged_ops },
423 #endif
424
425 #ifdef CONFIG_PTY_CHAN
426         { "pty", &pty_ops },
427         { "pts", &pts_ops },
428 #else
429         { "pty", &not_configged_ops },
430         { "pts", &not_configged_ops },
431 #endif
432
433 #ifdef CONFIG_TTY_CHAN
434         { "tty", &tty_ops },
435 #else
436         { "tty", &not_configged_ops },
437 #endif
438
439 #ifdef CONFIG_XTERM_CHAN
440         { "xterm", &xterm_ops },
441 #else
442         { "xterm", &not_configged_ops },
443 #endif
444 };
445
446 static struct chan *parse_chan(struct line *line, char *str, int device,
447                                const struct chan_opts *opts, char **error_out)
448 {
449         const struct chan_type *entry;
450         const struct chan_ops *ops;
451         struct chan *chan;
452         void *data;
453         int i;
454
455         ops = NULL;
456         data = NULL;
457         for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
458                 entry = &chan_table[i];
459                 if (!strncmp(str, entry->key, strlen(entry->key))) {
460                         ops = entry->ops;
461                         str += strlen(entry->key);
462                         break;
463                 }
464         }
465         if (ops == NULL) {
466                 *error_out = "No match for configured backends";
467                 return NULL;
468         }
469
470         data = (*ops->init)(str, device, opts);
471         if (data == NULL) {
472                 *error_out = "Configuration failed";
473                 return NULL;
474         }
475
476         chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
477         if (chan == NULL) {
478                 *error_out = "Memory allocation failed";
479                 return NULL;
480         }
481         *chan = ((struct chan) { .list          = LIST_HEAD_INIT(chan->list),
482                                  .free_list     =
483                                         LIST_HEAD_INIT(chan->free_list),
484                                  .line          = line,
485                                  .primary       = 1,
486                                  .input         = 0,
487                                  .output        = 0,
488                                  .opened        = 0,
489                                  .enabled       = 0,
490                                  .fd            = -1,
491                                  .ops           = ops,
492                                  .data          = data });
493         return chan;
494 }
495
496 int parse_chan_pair(char *str, struct line *line, int device,
497                     const struct chan_opts *opts, char **error_out)
498 {
499         struct list_head *chans = &line->chan_list;
500         struct chan *new;
501         char *in, *out;
502
503         if (!list_empty(chans)) {
504                 line->chan_in = line->chan_out = NULL;
505                 free_chan(chans);
506                 INIT_LIST_HEAD(chans);
507         }
508
509         if (!str)
510                 return 0;
511
512         out = strchr(str, ',');
513         if (out != NULL) {
514                 in = str;
515                 *out = '\0';
516                 out++;
517                 new = parse_chan(line, in, device, opts, error_out);
518                 if (new == NULL)
519                         return -1;
520
521                 new->input = 1;
522                 list_add(&new->list, chans);
523                 line->chan_in = new;
524
525                 new = parse_chan(line, out, device, opts, error_out);
526                 if (new == NULL)
527                         return -1;
528
529                 list_add(&new->list, chans);
530                 new->output = 1;
531                 line->chan_out = new;
532         }
533         else {
534                 new = parse_chan(line, str, device, opts, error_out);
535                 if (new == NULL)
536                         return -1;
537
538                 list_add(&new->list, chans);
539                 new->input = 1;
540                 new->output = 1;
541                 line->chan_in = line->chan_out = new;
542         }
543         return 0;
544 }
545
546 void chan_interrupt(struct line *line, int irq)
547 {
548         struct tty_port *port = &line->port;
549         struct chan *chan = line->chan_in;
550         int err;
551         char c;
552
553         if (!chan || !chan->ops->read)
554                 goto out;
555
556         do {
557                 if (!tty_buffer_request_room(port, 1)) {
558                         schedule_delayed_work(&line->task, 1);
559                         goto out;
560                 }
561                 err = chan->ops->read(chan->fd, &c, chan->data);
562                 if (err > 0)
563                         tty_insert_flip_char(port, c, TTY_NORMAL);
564         } while (err > 0);
565
566         if (err == 0)
567                 reactivate_fd(chan->fd, irq);
568         if (err == -EIO) {
569                 if (chan->primary) {
570                         tty_port_tty_hangup(&line->port, false);
571                         if (line->chan_out != chan)
572                                 close_one_chan(line->chan_out, 1);
573                 }
574                 close_one_chan(chan, 1);
575                 if (chan->primary)
576                         return;
577         }
578  out:
579         tty_flip_buffer_push(port);
580 }