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 | /* XXX: could well be moved to somewhere else, if needed. */ |
| 23 | static int my_printf(const char * fmt, ...) |
| 24 | __attribute__ ((format (printf, 1, 2))); |
Jeff Dike | 84ddaa8 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 25 | |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 26 | static int my_printf(const char * fmt, ...) |
| 27 | { |
| 28 | /* Yes, can be called on atomic context.*/ |
| 29 | char *buf = kmalloc(4096, GFP_ATOMIC); |
| 30 | va_list args; |
| 31 | int r; |
| 32 | |
| 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; |
| 42 | } |
| 43 | |
| 44 | if (r) |
| 45 | r = os_write_file(1, fmt, r); |
| 46 | return r; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | #ifdef CONFIG_NOCONFIG_CHAN |
| 51 | /* Despite its name, there's no added trailing newline. */ |
| 52 | static int my_puts(const char * buf) |
| 53 | { |
| 54 | return os_write_file(1, buf, strlen(buf)); |
| 55 | } |
Jeff Dike | 84ddaa8 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static void *not_configged_init(char *str, int device, struct chan_opts *opts) |
| 58 | { |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 59 | my_puts("Using a channel type which is configured out of " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | "UML\n"); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 61 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int not_configged_open(int input, int output, int primary, void *data, |
| 65 | char **dev_out) |
| 66 | { |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 67 | my_puts("Using a channel type which is configured out of " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | "UML\n"); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 69 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void not_configged_close(int fd, void *data) |
| 73 | { |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 74 | my_puts("Using a channel type which is configured out of " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | "UML\n"); |
| 76 | } |
| 77 | |
| 78 | static int not_configged_read(int fd, char *c_out, void *data) |
| 79 | { |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 80 | my_puts("Using a channel type which is configured out of " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | "UML\n"); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 82 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static int not_configged_write(int fd, const char *buf, int len, void *data) |
| 86 | { |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 87 | my_puts("Using a channel type which is configured out of " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | "UML\n"); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 89 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Paolo 'Blaisorblade' Giarrusso | 55c033c | 2005-11-13 16:07:11 -0800 | [diff] [blame] | 92 | 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] | 93 | { |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 94 | my_puts("Using a channel type which is configured out of " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | "UML\n"); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 96 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static int not_configged_window_size(int fd, void *data, unsigned short *rows, |
| 100 | unsigned short *cols) |
| 101 | { |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 102 | my_puts("Using a channel type which is configured out of " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | "UML\n"); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 104 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void not_configged_free(void *data) |
| 108 | { |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 109 | my_puts("Using a channel type which is configured out of " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | "UML\n"); |
| 111 | } |
| 112 | |
| 113 | static 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, |
| 123 | }; |
| 124 | #endif /* CONFIG_NOCONFIG_CHAN */ |
| 125 | |
| 126 | void generic_close(int fd, void *unused) |
| 127 | { |
| 128 | os_close_file(fd); |
| 129 | } |
| 130 | |
| 131 | int generic_read(int fd, char *c_out, void *unused) |
| 132 | { |
| 133 | int n; |
| 134 | |
| 135 | n = os_read_file(fd, c_out, sizeof(*c_out)); |
| 136 | |
| 137 | if(n == -EAGAIN) |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 138 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | else if(n == 0) |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 140 | return -EIO; |
| 141 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* XXX Trivial wrapper around os_write_file */ |
| 145 | |
| 146 | int generic_write(int fd, const char *buf, int n, void *unused) |
| 147 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 148 | return os_write_file(fd, buf, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int generic_window_size(int fd, void *unused, unsigned short *rows_out, |
| 152 | unsigned short *cols_out) |
| 153 | { |
| 154 | int rows, cols; |
| 155 | int ret; |
| 156 | |
| 157 | ret = os_window_size(fd, &rows, &cols); |
| 158 | if(ret < 0) |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 159 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | ret = ((*rows_out != rows) || (*cols_out != cols)); |
| 162 | |
| 163 | *rows_out = rows; |
| 164 | *cols_out = cols; |
| 165 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 166 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void generic_free(void *data) |
| 170 | { |
| 171 | kfree(data); |
| 172 | } |
| 173 | |
| 174 | static void tty_receive_char(struct tty_struct *tty, char ch) |
| 175 | { |
| 176 | if(tty == NULL) return; |
| 177 | |
| 178 | if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) { |
| 179 | if(ch == STOP_CHAR(tty)){ |
| 180 | stop_tty(tty); |
| 181 | return; |
| 182 | } |
| 183 | else if(ch == START_CHAR(tty)){ |
| 184 | start_tty(tty); |
| 185 | return; |
| 186 | } |
| 187 | } |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | tty_insert_flip_char(tty, ch, TTY_NORMAL); |
| 190 | } |
| 191 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 192 | static int open_one_chan(struct chan *chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
| 194 | int fd; |
| 195 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 196 | if(chan->opened) |
| 197 | return 0; |
| 198 | |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | chan->fd = fd; |
| 206 | |
| 207 | chan->opened = 1; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 208 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | int open_chan(struct list_head *chans) |
| 212 | { |
| 213 | struct list_head *ele; |
| 214 | struct chan *chan; |
| 215 | int ret, err = 0; |
| 216 | |
| 217 | list_for_each(ele, chans){ |
| 218 | chan = list_entry(ele, struct chan, list); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 219 | ret = open_one_chan(chan); |
| 220 | if(chan->primary) |
| 221 | err = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 223 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void chan_enable_winch(struct list_head *chans, struct tty_struct *tty) |
| 227 | { |
| 228 | struct list_head *ele; |
| 229 | struct chan *chan; |
| 230 | |
| 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; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 240 | void enable_chan(struct line *line) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | struct list_head *ele; |
| 243 | struct chan *chan; |
| 244 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 245 | list_for_each(ele, &line->chan_list){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | chan = list_entry(ele, struct chan, list); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 247 | if(open_one_chan(chan)) |
| 248 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 250 | if(chan->enabled) |
| 251 | continue; |
| 252 | line_setup_irq(chan->fd, chan->input, chan->output, line, |
| 253 | chan); |
| 254 | chan->enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 258 | static LIST_HEAD(irqs_to_free); |
| 259 | |
| 260 | void free_irqs(void) |
| 261 | { |
| 262 | struct chan *chan; |
| 263 | |
| 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); |
| 267 | |
| 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; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static void close_one_chan(struct chan *chan, int delay_free_irq) |
| 277 | { |
| 278 | if(!chan->opened) |
| 279 | return; |
| 280 | |
| 281 | if(delay_free_irq){ |
| 282 | list_add(&chan->free_list, &irqs_to_free); |
| 283 | } |
| 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; |
| 290 | } |
| 291 | if(chan->ops->close != NULL) |
| 292 | (*chan->ops->close)(chan->fd, chan->data); |
| 293 | |
| 294 | chan->opened = 0; |
| 295 | chan->fd = -1; |
| 296 | } |
| 297 | |
| 298 | void close_chan(struct list_head *chans, int delay_free_irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
| 300 | struct chan *chan; |
| 301 | |
| 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. |
| 306 | */ |
| 307 | list_for_each_entry_reverse(chan, chans, list) { |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 308 | close_one_chan(chan, delay_free_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 312 | void deactivate_chan(struct list_head *chans, int irq) |
| 313 | { |
| 314 | struct list_head *ele; |
| 315 | |
| 316 | struct chan *chan; |
| 317 | list_for_each(ele, chans) { |
| 318 | chan = list_entry(ele, struct chan, list); |
| 319 | |
| 320 | if(chan->enabled && chan->input) |
| 321 | deactivate_fd(chan->fd, irq); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void reactivate_chan(struct list_head *chans, int irq) |
| 326 | { |
| 327 | struct list_head *ele; |
| 328 | struct chan *chan; |
| 329 | |
| 330 | list_for_each(ele, chans) { |
| 331 | chan = list_entry(ele, struct chan, list); |
| 332 | |
| 333 | if(chan->enabled && chan->input) |
| 334 | reactivate_fd(chan->fd, irq); |
| 335 | } |
| 336 | } |
| 337 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 338 | int write_chan(struct list_head *chans, const char *buf, int len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | int write_irq) |
| 340 | { |
| 341 | struct list_head *ele; |
| 342 | struct chan *chan = NULL; |
| 343 | int n, ret = 0; |
| 344 | |
| 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); |
| 354 | } |
| 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 | |
| 359 | int console_write_chan(struct list_head *chans, const char *buf, int len) |
| 360 | { |
| 361 | struct list_head *ele; |
| 362 | struct chan *chan; |
| 363 | int n, ret = 0; |
| 364 | |
| 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; |
Paolo 'Blaisorblade' Giarrusso | 55c033c | 2005-11-13 16:07:11 -0800 | [diff] [blame] | 369 | n = chan->ops->console_write(chan->fd, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if(chan->primary) ret = n; |
| 371 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 372 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 375 | int console_open_chan(struct line *line, struct console *co, |
| 376 | struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 378 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 380 | err = open_chan(&line->chan_list); |
| 381 | if(err) |
| 382 | return err; |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | printk("Console initialized on /dev/%s%d\n",co->name,co->index); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | int chan_window_size(struct list_head *chans, unsigned short *rows_out, |
| 389 | unsigned short *cols_out) |
| 390 | { |
| 391 | struct list_head *ele; |
| 392 | struct chan *chan; |
| 393 | |
| 394 | list_for_each(ele, chans){ |
| 395 | chan = list_entry(ele, struct chan, list); |
| 396 | if(chan->primary){ |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 403 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Paolo 'Blaisorblade' Giarrusso | 42947cb | 2006-02-01 03:06:29 -0800 | [diff] [blame] | 406 | static void free_one_chan(struct chan *chan, int delay_free_irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
| 408 | list_del(&chan->list); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 409 | |
| 410 | close_one_chan(chan, delay_free_irq); |
| 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if(chan->ops->free != NULL) |
| 413 | (*chan->ops->free)(chan->data); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if(chan->primary && chan->output) ignore_sigio_fd(chan->fd); |
| 416 | kfree(chan); |
| 417 | } |
| 418 | |
Paolo 'Blaisorblade' Giarrusso | 42947cb | 2006-02-01 03:06:29 -0800 | [diff] [blame] | 419 | static void free_chan(struct list_head *chans, int delay_free_irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
| 421 | struct list_head *ele, *next; |
| 422 | struct chan *chan; |
| 423 | |
| 424 | list_for_each_safe(ele, next, chans){ |
| 425 | chan = list_entry(ele, struct chan, list); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 426 | free_one_chan(chan, delay_free_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | static int one_chan_config_string(struct chan *chan, char *str, int size, |
| 431 | char **error_out) |
| 432 | { |
| 433 | int n = 0; |
| 434 | |
| 435 | if(chan == NULL){ |
| 436 | CONFIG_CHUNK(str, size, n, "none", 1); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 437 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | CONFIG_CHUNK(str, size, n, chan->ops->type, 0); |
| 441 | |
| 442 | if(chan->dev == NULL){ |
| 443 | CONFIG_CHUNK(str, size, n, "", 1); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 444 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | CONFIG_CHUNK(str, size, n, ":", 0); |
| 448 | CONFIG_CHUNK(str, size, n, chan->dev, 0); |
| 449 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 450 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 453 | static int chan_pair_config_string(struct chan *in, struct chan *out, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | char *str, int size, char **error_out) |
| 455 | { |
| 456 | int n; |
| 457 | |
| 458 | n = one_chan_config_string(in, str, size, error_out); |
| 459 | str += n; |
| 460 | size -= n; |
| 461 | |
| 462 | if(in == out){ |
| 463 | CONFIG_CHUNK(str, size, n, "", 1); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 464 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 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); |
| 472 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 473 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 476 | int chan_config_string(struct list_head *chans, char *str, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | char **error_out) |
| 478 | { |
| 479 | struct list_head *ele; |
| 480 | struct chan *chan, *in = NULL, *out = NULL; |
| 481 | |
| 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; |
| 490 | } |
| 491 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 492 | return chan_pair_config_string(in, out, str, size, error_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | struct chan_type { |
| 496 | char *key; |
| 497 | struct chan_ops *ops; |
| 498 | }; |
| 499 | |
Paolo 'Blaisorblade' Giarrusso | 42947cb | 2006-02-01 03:06:29 -0800 | [diff] [blame] | 500 | static struct chan_type chan_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { "fd", &fd_ops }, |
| 502 | |
| 503 | #ifdef CONFIG_NULL_CHAN |
| 504 | { "null", &null_ops }, |
| 505 | #else |
| 506 | { "null", ¬_configged_ops }, |
| 507 | #endif |
| 508 | |
| 509 | #ifdef CONFIG_PORT_CHAN |
| 510 | { "port", &port_ops }, |
| 511 | #else |
| 512 | { "port", ¬_configged_ops }, |
| 513 | #endif |
| 514 | |
| 515 | #ifdef CONFIG_PTY_CHAN |
| 516 | { "pty", &pty_ops }, |
| 517 | { "pts", &pts_ops }, |
| 518 | #else |
| 519 | { "pty", ¬_configged_ops }, |
| 520 | { "pts", ¬_configged_ops }, |
| 521 | #endif |
| 522 | |
| 523 | #ifdef CONFIG_TTY_CHAN |
| 524 | { "tty", &tty_ops }, |
| 525 | #else |
| 526 | { "tty", ¬_configged_ops }, |
| 527 | #endif |
| 528 | |
| 529 | #ifdef CONFIG_XTERM_CHAN |
| 530 | { "xterm", &xterm_ops }, |
| 531 | #else |
| 532 | { "xterm", ¬_configged_ops }, |
| 533 | #endif |
| 534 | }; |
| 535 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 536 | static struct chan *parse_chan(struct line *line, char *str, int device, |
| 537 | struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
| 539 | struct chan_type *entry; |
| 540 | struct chan_ops *ops; |
| 541 | struct chan *chan; |
| 542 | void *data; |
| 543 | int i; |
| 544 | |
| 545 | ops = NULL; |
| 546 | data = NULL; |
| 547 | for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); 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; |
| 553 | } |
| 554 | } |
| 555 | if(ops == NULL){ |
Paolo 'Blaisorblade' Giarrusso | fac97ae | 2005-09-22 21:44:22 -0700 | [diff] [blame] | 556 | my_printf("parse_chan couldn't parse \"%s\"\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | str); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 558 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 560 | if(ops->init == NULL) |
| 561 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | data = (*ops->init)(str, device, opts); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 563 | if(data == NULL) |
| 564 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Paolo 'Blaisorblade' Giarrusso | 79ae2cb | 2005-09-22 21:44:21 -0700 | [diff] [blame] | 566 | chan = kmalloc(sizeof(*chan), GFP_ATOMIC); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 567 | if(chan == NULL) |
| 568 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list), |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 570 | .free_list = |
| 571 | LIST_HEAD_INIT(chan->free_list), |
| 572 | .line = line, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | .primary = 1, |
| 574 | .input = 0, |
| 575 | .output = 0, |
| 576 | .opened = 0, |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 577 | .enabled = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | .fd = -1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | .ops = ops, |
| 580 | .data = data }); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 581 | return chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 584 | int parse_chan_pair(char *str, struct line *line, int device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | struct chan_opts *opts) |
| 586 | { |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 587 | struct list_head *chans = &line->chan_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | struct chan *new, *chan; |
| 589 | char *in, *out; |
| 590 | |
| 591 | if(!list_empty(chans)){ |
| 592 | chan = list_entry(chans->next, struct chan, list); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 593 | free_chan(chans, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | INIT_LIST_HEAD(chans); |
| 595 | } |
| 596 | |
| 597 | out = strchr(str, ','); |
| 598 | if(out != NULL){ |
| 599 | in = str; |
| 600 | *out = '\0'; |
| 601 | out++; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 602 | new = parse_chan(line, in, device, opts); |
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 | new->input = 1; |
| 607 | list_add(&new->list, chans); |
| 608 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 609 | new = parse_chan(line, out, device, opts); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 610 | if(new == NULL) |
| 611 | return -1; |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | list_add(&new->list, chans); |
| 614 | new->output = 1; |
| 615 | } |
| 616 | else { |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 617 | new = parse_chan(line, str, device, opts); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 618 | if(new == NULL) |
| 619 | return -1; |
| 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | list_add(&new->list, chans); |
| 622 | new->input = 1; |
| 623 | new->output = 1; |
| 624 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 625 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | int chan_out_fd(struct list_head *chans) |
| 629 | { |
| 630 | struct list_head *ele; |
| 631 | struct chan *chan; |
| 632 | |
| 633 | list_for_each(ele, chans){ |
| 634 | chan = list_entry(ele, struct chan, list); |
| 635 | if(chan->primary && chan->output) |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 636 | return chan->fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 638 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | void chan_interrupt(struct list_head *chans, struct work_struct *task, |
| 642 | struct tty_struct *tty, int irq) |
| 643 | { |
| 644 | struct list_head *ele, *next; |
| 645 | struct chan *chan; |
| 646 | int err; |
| 647 | char c; |
| 648 | |
| 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 { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 653 | if (tty && !tty_buffer_request_room(tty, 1)) { |
Jeff Dike | 9159c9d | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 654 | schedule_delayed_work(task, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | goto out; |
| 656 | } |
| 657 | err = chan->ops->read(chan->fd, &c, chan->data); |
| 658 | if(err > 0) |
| 659 | tty_receive_char(tty, c); |
| 660 | } while(err > 0); |
| 661 | |
| 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); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 667 | close_chan(chans, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return; |
| 669 | } |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 670 | else close_one_chan(chan, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | out: |
| 674 | if(tty) tty_flip_buffer_push(tty); |
| 675 | } |