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