]>
Commit | Line | Data |
---|---|---|
165dc591 | 1 | /* |
2f8a2dc2 | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4 LT |
3 | * Licensed under the GPL |
4 | */ | |
5 | ||
2f8a2dc2 | 6 | #include "linux/irqreturn.h" |
1da177e4 | 7 | #include "linux/kd.h" |
d43c36dc | 8 | #include "linux/sched.h" |
7f3c1fa4 | 9 | #include "linux/slab.h" |
510c72a3 | 10 | #include "chan.h" |
2f8a2dc2 | 11 | #include "irq_kern.h" |
1da177e4 | 12 | #include "irq_user.h" |
edea1385 | 13 | #include "kern_util.h" |
1da177e4 | 14 | #include "os.h" |
1da177e4 LT |
15 | |
16 | #define LINE_BUFSIZE 4096 | |
17 | ||
7bea96fd | 18 | static irqreturn_t line_interrupt(int irq, void *data) |
1da177e4 | 19 | { |
165dc591 JD |
20 | struct chan *chan = data; |
21 | struct line *line = chan->line; | |
1da177e4 LT |
22 | |
23 | if (line) | |
03315b59 | 24 | chan_interrupt(&line->chan_list, &line->task, line->tty, irq); |
1da177e4 LT |
25 | return IRQ_HANDLED; |
26 | } | |
27 | ||
a2ce7740 | 28 | static void line_timer_cb(struct work_struct *work) |
1da177e4 | 29 | { |
a2ce7740 | 30 | struct line *line = container_of(work, struct line, task.work); |
1da177e4 | 31 | |
2f8a2dc2 | 32 | if (!line->throttled) |
e4dcee80 JD |
33 | chan_interrupt(&line->chan_list, &line->task, line->tty, |
34 | line->driver->read_irq); | |
1da177e4 LT |
35 | } |
36 | ||
2f8a2dc2 JD |
37 | /* |
38 | * Returns the free space inside the ring buffer of this line. | |
b97b77cc | 39 | * |
b60745b9 | 40 | * Should be called while holding line->lock (this does not modify data). |
b97b77cc PBG |
41 | */ |
42 | static int write_room(struct line *line) | |
1da177e4 LT |
43 | { |
44 | int n; | |
45 | ||
b97b77cc PBG |
46 | if (line->buffer == NULL) |
47 | return LINE_BUFSIZE - 1; | |
48 | ||
49 | /* This is for the case where the buffer is wrapped! */ | |
50 | n = line->head - line->tail; | |
1da177e4 | 51 | |
1da177e4 | 52 | if (n <= 0) |
acb2cf34 | 53 | n += LINE_BUFSIZE; /* The other case */ |
b97b77cc PBG |
54 | return n - 1; |
55 | } | |
56 | ||
57 | int line_write_room(struct tty_struct *tty) | |
58 | { | |
59 | struct line *line = tty->driver_data; | |
60 | unsigned long flags; | |
61 | int room; | |
62 | ||
b97b77cc PBG |
63 | spin_lock_irqsave(&line->lock, flags); |
64 | room = write_room(line); | |
65 | spin_unlock_irqrestore(&line->lock, flags); | |
66 | ||
b97b77cc | 67 | return room; |
1da177e4 LT |
68 | } |
69 | ||
b97b77cc PBG |
70 | int line_chars_in_buffer(struct tty_struct *tty) |
71 | { | |
72 | struct line *line = tty->driver_data; | |
73 | unsigned long flags; | |
74 | int ret; | |
75 | ||
76 | spin_lock_irqsave(&line->lock, flags); | |
acb2cf34 | 77 | /* write_room subtracts 1 for the needed NULL, so we readd it.*/ |
b97b77cc PBG |
78 | ret = LINE_BUFSIZE - (write_room(line) + 1); |
79 | spin_unlock_irqrestore(&line->lock, flags); | |
80 | ||
81 | return ret; | |
82 | } | |
83 | ||
84 | /* | |
85 | * This copies the content of buf into the circular buffer associated with | |
86 | * this line. | |
87 | * The return value is the number of characters actually copied, i.e. the ones | |
88 | * for which there was space: this function is not supposed to ever flush out | |
89 | * the circular buffer. | |
90 | * | |
91 | * Must be called while holding line->lock! | |
92 | */ | |
1da177e4 LT |
93 | static int buffer_data(struct line *line, const char *buf, int len) |
94 | { | |
95 | int end, room; | |
96 | ||
2f8a2dc2 | 97 | if (line->buffer == NULL) { |
1da177e4 LT |
98 | line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC); |
99 | if (line->buffer == NULL) { | |
2f8a2dc2 JD |
100 | printk(KERN_ERR "buffer_data - atomic allocation " |
101 | "failed\n"); | |
102 | return 0; | |
1da177e4 LT |
103 | } |
104 | line->head = line->buffer; | |
105 | line->tail = line->buffer; | |
106 | } | |
107 | ||
108 | room = write_room(line); | |
109 | len = (len > room) ? room : len; | |
110 | ||
111 | end = line->buffer + LINE_BUFSIZE - line->tail; | |
b97b77cc | 112 | |
2f8a2dc2 | 113 | if (len < end) { |
1da177e4 LT |
114 | memcpy(line->tail, buf, len); |
115 | line->tail += len; | |
d50084a2 JD |
116 | } |
117 | else { | |
b97b77cc | 118 | /* The circular buffer is wrapping */ |
1da177e4 LT |
119 | memcpy(line->tail, buf, end); |
120 | buf += end; | |
121 | memcpy(line->buffer, buf, len - end); | |
122 | line->tail = line->buffer + len - end; | |
123 | } | |
124 | ||
b97b77cc | 125 | return len; |
1da177e4 LT |
126 | } |
127 | ||
b97b77cc PBG |
128 | /* |
129 | * Flushes the ring buffer to the output channels. That is, write_chan is | |
130 | * called, passing it line->head as buffer, and an appropriate count. | |
131 | * | |
132 | * On exit, returns 1 when the buffer is empty, | |
133 | * 0 when the buffer is not empty on exit, | |
134 | * and -errno when an error occurred. | |
135 | * | |
136 | * Must be called while holding line->lock!*/ | |
1da177e4 LT |
137 | static int flush_buffer(struct line *line) |
138 | { | |
139 | int n, count; | |
140 | ||
141 | if ((line->buffer == NULL) || (line->head == line->tail)) | |
b97b77cc | 142 | return 1; |
1da177e4 LT |
143 | |
144 | if (line->tail < line->head) { | |
b97b77cc | 145 | /* line->buffer + LINE_BUFSIZE is the end of the buffer! */ |
1da177e4 | 146 | count = line->buffer + LINE_BUFSIZE - line->head; |
b97b77cc | 147 | |
1da177e4 LT |
148 | n = write_chan(&line->chan_list, line->head, count, |
149 | line->driver->write_irq); | |
150 | if (n < 0) | |
b97b77cc PBG |
151 | return n; |
152 | if (n == count) { | |
2f8a2dc2 JD |
153 | /* |
154 | * We have flushed from ->head to buffer end, now we | |
155 | * must flush only from the beginning to ->tail. | |
156 | */ | |
1da177e4 | 157 | line->head = line->buffer; |
b97b77cc | 158 | } else { |
1da177e4 | 159 | line->head += n; |
b97b77cc | 160 | return 0; |
1da177e4 LT |
161 | } |
162 | } | |
163 | ||
164 | count = line->tail - line->head; | |
d50084a2 | 165 | n = write_chan(&line->chan_list, line->head, count, |
1da177e4 | 166 | line->driver->write_irq); |
b97b77cc | 167 | |
2f8a2dc2 | 168 | if (n < 0) |
b97b77cc | 169 | return n; |
1da177e4 LT |
170 | |
171 | line->head += n; | |
b97b77cc PBG |
172 | return line->head == line->tail; |
173 | } | |
174 | ||
175 | void line_flush_buffer(struct tty_struct *tty) | |
176 | { | |
177 | struct line *line = tty->driver_data; | |
178 | unsigned long flags; | |
b97b77cc | 179 | |
b97b77cc | 180 | spin_lock_irqsave(&line->lock, flags); |
f1c93e49 | 181 | flush_buffer(line); |
b97b77cc | 182 | spin_unlock_irqrestore(&line->lock, flags); |
b97b77cc PBG |
183 | } |
184 | ||
2f8a2dc2 JD |
185 | /* |
186 | * We map both ->flush_chars and ->put_char (which go in pair) onto | |
187 | * ->flush_buffer and ->write. Hope it's not that bad. | |
188 | */ | |
b97b77cc PBG |
189 | void line_flush_chars(struct tty_struct *tty) |
190 | { | |
191 | line_flush_buffer(tty); | |
192 | } | |
193 | ||
3168cb98 | 194 | int line_put_char(struct tty_struct *tty, unsigned char ch) |
b97b77cc | 195 | { |
3168cb98 | 196 | return line_write(tty, &ch, sizeof(ch)); |
1da177e4 LT |
197 | } |
198 | ||
199 | int line_write(struct tty_struct *tty, const unsigned char *buf, int len) | |
200 | { | |
201 | struct line *line = tty->driver_data; | |
202 | unsigned long flags; | |
c59dbcad | 203 | int n, ret = 0; |
1da177e4 | 204 | |
b97b77cc | 205 | spin_lock_irqsave(&line->lock, flags); |
c59dbcad | 206 | if (line->head != line->tail) |
1da177e4 | 207 | ret = buffer_data(line, buf, len); |
c59dbcad | 208 | else { |
d50084a2 | 209 | n = write_chan(&line->chan_list, buf, len, |
1da177e4 | 210 | line->driver->write_irq); |
b97b77cc | 211 | if (n < 0) { |
1da177e4 LT |
212 | ret = n; |
213 | goto out_up; | |
214 | } | |
215 | ||
216 | len -= n; | |
217 | ret += n; | |
b97b77cc | 218 | if (len > 0) |
1da177e4 LT |
219 | ret += buffer_data(line, buf + n, len); |
220 | } | |
b97b77cc PBG |
221 | out_up: |
222 | spin_unlock_irqrestore(&line->lock, flags); | |
223 | return ret; | |
1da177e4 LT |
224 | } |
225 | ||
606d099c | 226 | void line_set_termios(struct tty_struct *tty, struct ktermios * old) |
1da177e4 LT |
227 | { |
228 | /* nothing */ | |
229 | } | |
230 | ||
5e7672ec | 231 | static const struct { |
1da177e4 LT |
232 | int cmd; |
233 | char *level; | |
234 | char *name; | |
235 | } tty_ioctls[] = { | |
236 | /* don't print these, they flood the log ... */ | |
237 | { TCGETS, NULL, "TCGETS" }, | |
2f8a2dc2 JD |
238 | { TCSETS, NULL, "TCSETS" }, |
239 | { TCSETSW, NULL, "TCSETSW" }, | |
240 | { TCFLSH, NULL, "TCFLSH" }, | |
241 | { TCSBRK, NULL, "TCSBRK" }, | |
1da177e4 LT |
242 | |
243 | /* general tty stuff */ | |
2f8a2dc2 JD |
244 | { TCSETSF, KERN_DEBUG, "TCSETSF" }, |
245 | { TCGETA, KERN_DEBUG, "TCGETA" }, | |
246 | { TIOCMGET, KERN_DEBUG, "TIOCMGET" }, | |
247 | { TCSBRKP, KERN_DEBUG, "TCSBRKP" }, | |
248 | { TIOCMSET, KERN_DEBUG, "TIOCMSET" }, | |
1da177e4 LT |
249 | |
250 | /* linux-specific ones */ | |
251 | { TIOCLINUX, KERN_INFO, "TIOCLINUX" }, | |
252 | { KDGKBMODE, KERN_INFO, "KDGKBMODE" }, | |
253 | { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" }, | |
254 | { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" }, | |
255 | }; | |
256 | ||
8a06dc4d RW |
257 | int line_ioctl(struct tty_struct *tty, unsigned int cmd, |
258 | unsigned long arg) | |
1da177e4 LT |
259 | { |
260 | int ret; | |
261 | int i; | |
262 | ||
263 | ret = 0; | |
264 | switch(cmd) { | |
265 | #ifdef TIOCGETP | |
266 | case TIOCGETP: | |
267 | case TIOCSETP: | |
268 | case TIOCSETN: | |
269 | #endif | |
270 | #ifdef TIOCGETC | |
271 | case TIOCGETC: | |
272 | case TIOCSETC: | |
273 | #endif | |
274 | #ifdef TIOCGLTC | |
275 | case TIOCGLTC: | |
276 | case TIOCSLTC: | |
277 | #endif | |
c564b6fd AC |
278 | /* Note: these are out of date as we now have TCGETS2 etc but this |
279 | whole lot should probably go away */ | |
1da177e4 LT |
280 | case TCGETS: |
281 | case TCSETSF: | |
282 | case TCSETSW: | |
283 | case TCSETS: | |
284 | case TCGETA: | |
285 | case TCSETAF: | |
286 | case TCSETAW: | |
287 | case TCSETA: | |
288 | case TCXONC: | |
289 | case TCFLSH: | |
290 | case TIOCOUTQ: | |
291 | case TIOCINQ: | |
292 | case TIOCGLCKTRMIOS: | |
293 | case TIOCSLCKTRMIOS: | |
294 | case TIOCPKT: | |
295 | case TIOCGSOFTCAR: | |
296 | case TIOCSSOFTCAR: | |
297 | return -ENOIOCTLCMD; | |
298 | #if 0 | |
299 | case TCwhatever: | |
300 | /* do something */ | |
301 | break; | |
302 | #endif | |
303 | default: | |
304 | for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++) | |
305 | if (cmd == tty_ioctls[i].cmd) | |
306 | break; | |
2f8a2dc2 | 307 | if (i == ARRAY_SIZE(tty_ioctls)) { |
1da177e4 | 308 | printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n", |
3595726a | 309 | __func__, tty->name, cmd); |
1da177e4 LT |
310 | } |
311 | ret = -ENOIOCTLCMD; | |
312 | break; | |
313 | } | |
b97b77cc | 314 | return ret; |
1da177e4 LT |
315 | } |
316 | ||
e4dcee80 JD |
317 | void line_throttle(struct tty_struct *tty) |
318 | { | |
319 | struct line *line = tty->driver_data; | |
320 | ||
321 | deactivate_chan(&line->chan_list, line->driver->read_irq); | |
322 | line->throttled = 1; | |
323 | } | |
324 | ||
325 | void line_unthrottle(struct tty_struct *tty) | |
326 | { | |
327 | struct line *line = tty->driver_data; | |
328 | ||
329 | line->throttled = 0; | |
330 | chan_interrupt(&line->chan_list, &line->task, tty, | |
331 | line->driver->read_irq); | |
332 | ||
2f8a2dc2 JD |
333 | /* |
334 | * Maybe there is enough stuff pending that calling the interrupt | |
e4dcee80 JD |
335 | * throttles us again. In this case, line->throttled will be 1 |
336 | * again and we shouldn't turn the interrupt back on. | |
337 | */ | |
2f8a2dc2 | 338 | if (!line->throttled) |
e4dcee80 JD |
339 | reactivate_chan(&line->chan_list, line->driver->read_irq); |
340 | } | |
341 | ||
7bea96fd | 342 | static irqreturn_t line_write_interrupt(int irq, void *data) |
1da177e4 | 343 | { |
165dc591 JD |
344 | struct chan *chan = data; |
345 | struct line *line = chan->line; | |
346 | struct tty_struct *tty = line->tty; | |
1da177e4 LT |
347 | int err; |
348 | ||
2f8a2dc2 | 349 | /* |
c0b79a90 YZ |
350 | * Interrupts are disabled here because genirq keep irqs disabled when |
351 | * calling the action handler. | |
2f8a2dc2 | 352 | */ |
b97b77cc | 353 | |
ec0ac8ad | 354 | spin_lock(&line->lock); |
1da177e4 | 355 | err = flush_buffer(line); |
b97b77cc PBG |
356 | if (err == 0) { |
357 | return IRQ_NONE; | |
2f8a2dc2 | 358 | } else if (err < 0) { |
1da177e4 LT |
359 | line->head = line->buffer; |
360 | line->tail = line->buffer; | |
361 | } | |
ec0ac8ad | 362 | spin_unlock(&line->lock); |
1da177e4 | 363 | |
2f8a2dc2 | 364 | if (tty == NULL) |
b97b77cc | 365 | return IRQ_NONE; |
1da177e4 | 366 | |
06ac6679 | 367 | tty_wakeup(tty); |
b97b77cc | 368 | return IRQ_HANDLED; |
1da177e4 LT |
369 | } |
370 | ||
165dc591 | 371 | int line_setup_irq(int fd, int input, int output, struct line *line, void *data) |
1da177e4 | 372 | { |
5e7672ec | 373 | const struct line_driver *driver = line->driver; |
c0b79a90 | 374 | int err = 0, flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM; |
1da177e4 | 375 | |
b97b77cc PBG |
376 | if (input) |
377 | err = um_request_irq(driver->read_irq, fd, IRQ_READ, | |
d50084a2 | 378 | line_interrupt, flags, |
165dc591 | 379 | driver->read_irq_name, data); |
b97b77cc PBG |
380 | if (err) |
381 | return err; | |
382 | if (output) | |
383 | err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, | |
d50084a2 | 384 | line_write_interrupt, flags, |
165dc591 | 385 | driver->write_irq_name, data); |
1da177e4 | 386 | line->have_irq = 1; |
b97b77cc | 387 | return err; |
1da177e4 LT |
388 | } |
389 | ||
2f8a2dc2 JD |
390 | /* |
391 | * Normally, a driver like this can rely mostly on the tty layer | |
d79a5809 JD |
392 | * locking, particularly when it comes to the driver structure. |
393 | * However, in this case, mconsole requests can come in "from the | |
394 | * side", and race with opens and closes. | |
395 | * | |
c6256c68 JD |
396 | * mconsole config requests will want to be sure the device isn't in |
397 | * use, and get_config, open, and close will want a stable | |
398 | * configuration. The checking and modification of the configuration | |
399 | * is done under a spinlock. Checking whether the device is in use is | |
400 | * line->tty->count > 1, also under the spinlock. | |
d79a5809 | 401 | * |
f71f9484 AV |
402 | * line->count serves to decide whether the device should be enabled or |
403 | * disabled on the host. If it's equal to 0, then we are doing the | |
c6256c68 | 404 | * first open or last close. Otherwise, open and close just return. |
d79a5809 JD |
405 | */ |
406 | ||
1f80171e | 407 | int line_open(struct line *lines, struct tty_struct *tty) |
1da177e4 | 408 | { |
d79a5809 | 409 | struct line *line = &lines[tty->index]; |
165dc591 | 410 | int err = -ENODEV; |
1da177e4 | 411 | |
d79a5809 | 412 | spin_lock(&line->count_lock); |
2f8a2dc2 | 413 | if (!line->valid) |
d79a5809 | 414 | goto out_unlock; |
1da177e4 | 415 | |
d79a5809 | 416 | err = 0; |
f71f9484 | 417 | if (line->count++) |
d79a5809 JD |
418 | goto out_unlock; |
419 | ||
f71f9484 | 420 | BUG_ON(tty->driver_data); |
165dc591 JD |
421 | tty->driver_data = line; |
422 | line->tty = tty; | |
165dc591 | 423 | |
f71f9484 | 424 | spin_unlock(&line->count_lock); |
d14ad81f | 425 | err = enable_chan(line); |
f71f9484 | 426 | if (err) /* line_close() will be called by our caller */ |
d14ad81f JD |
427 | return err; |
428 | ||
d79a5809 | 429 | INIT_DELAYED_WORK(&line->task, line_timer_cb); |
165dc591 | 430 | |
2f8a2dc2 | 431 | if (!line->sigio) { |
d79a5809 JD |
432 | chan_enable_winch(&line->chan_list, tty); |
433 | line->sigio = 1; | |
1da177e4 | 434 | } |
1da177e4 | 435 | |
d79a5809 JD |
436 | chan_window_size(&line->chan_list, &tty->winsize.ws_row, |
437 | &tty->winsize.ws_col); | |
438 | ||
f71f9484 | 439 | return 0; |
d79a5809 JD |
440 | |
441 | out_unlock: | |
442 | spin_unlock(&line->count_lock); | |
b97b77cc | 443 | return err; |
1da177e4 LT |
444 | } |
445 | ||
cd2ee4a3 JD |
446 | static void unregister_winch(struct tty_struct *tty); |
447 | ||
1da177e4 LT |
448 | void line_close(struct tty_struct *tty, struct file * filp) |
449 | { | |
450 | struct line *line = tty->driver_data; | |
451 | ||
2f8a2dc2 JD |
452 | /* |
453 | * If line_open fails (and tty->driver_data is never set), | |
d79a5809 JD |
454 | * tty_open will call line_close. So just return in this case. |
455 | */ | |
2f8a2dc2 | 456 | if (line == NULL) |
d79a5809 | 457 | return; |
b97b77cc PBG |
458 | |
459 | /* We ignore the error anyway! */ | |
460 | flush_buffer(line); | |
461 | ||
d79a5809 | 462 | spin_lock(&line->count_lock); |
f71f9484 | 463 | BUG_ON(!line->valid); |
cd2ee4a3 | 464 | |
f71f9484 | 465 | if (--line->count) |
d79a5809 JD |
466 | goto out_unlock; |
467 | ||
d79a5809 JD |
468 | line->tty = NULL; |
469 | tty->driver_data = NULL; | |
470 | ||
f71f9484 AV |
471 | spin_unlock(&line->count_lock); |
472 | ||
2f8a2dc2 | 473 | if (line->sigio) { |
d79a5809 JD |
474 | unregister_winch(tty); |
475 | line->sigio = 0; | |
2f8a2dc2 | 476 | } |
cd2ee4a3 | 477 | |
d79a5809 JD |
478 | return; |
479 | ||
480 | out_unlock: | |
481 | spin_unlock(&line->count_lock); | |
1da177e4 LT |
482 | } |
483 | ||
484 | void close_lines(struct line *lines, int nlines) | |
485 | { | |
486 | int i; | |
487 | ||
488 | for(i = 0; i < nlines; i++) | |
165dc591 | 489 | close_chan(&lines[i].chan_list, 0); |
1da177e4 LT |
490 | } |
491 | ||
f28169d2 JD |
492 | static int setup_one_line(struct line *lines, int n, char *init, int init_prio, |
493 | char **error_out) | |
d79a5809 JD |
494 | { |
495 | struct line *line = &lines[n]; | |
f28169d2 | 496 | int err = -EINVAL; |
d79a5809 JD |
497 | |
498 | spin_lock(&line->count_lock); | |
499 | ||
f71f9484 | 500 | if (line->count) { |
f28169d2 | 501 | *error_out = "Device is already open"; |
d79a5809 JD |
502 | goto out; |
503 | } | |
504 | ||
2f8a2dc2 | 505 | if (line->init_pri <= init_prio) { |
d79a5809 JD |
506 | line->init_pri = init_prio; |
507 | if (!strcmp(init, "none")) | |
508 | line->valid = 0; | |
509 | else { | |
510 | line->init_str = init; | |
511 | line->valid = 1; | |
512 | } | |
513 | } | |
f28169d2 | 514 | err = 0; |
d79a5809 JD |
515 | out: |
516 | spin_unlock(&line->count_lock); | |
f28169d2 | 517 | return err; |
d79a5809 JD |
518 | } |
519 | ||
2f8a2dc2 JD |
520 | /* |
521 | * Common setup code for both startup command line and mconsole initialization. | |
4b3f686d | 522 | * @lines contains the array (of size @num) to modify; |
b97b77cc | 523 | * @init is the setup string; |
f28169d2 | 524 | * @error_out is an error string in the case of failure; |
d571cd18 | 525 | */ |
b97b77cc | 526 | |
f28169d2 JD |
527 | int line_setup(struct line *lines, unsigned int num, char *init, |
528 | char **error_out) | |
1da177e4 | 529 | { |
f28169d2 | 530 | int i, n, err; |
1da177e4 LT |
531 | char *end; |
532 | ||
2f8a2dc2 JD |
533 | if (*init == '=') { |
534 | /* | |
535 | * We said con=/ssl= instead of con#=, so we are configuring all | |
536 | * consoles at once. | |
537 | */ | |
b97b77cc | 538 | n = -1; |
d50084a2 JD |
539 | } |
540 | else { | |
1da177e4 | 541 | n = simple_strtoul(init, &end, 0); |
2f8a2dc2 | 542 | if (*end != '=') { |
f28169d2 JD |
543 | *error_out = "Couldn't parse device number"; |
544 | return -EINVAL; | |
1da177e4 LT |
545 | } |
546 | init = end; | |
547 | } | |
548 | init++; | |
b97b77cc PBG |
549 | |
550 | if (n >= (signed int) num) { | |
f28169d2 JD |
551 | *error_out = "Device number out of range"; |
552 | return -EINVAL; | |
553 | } | |
2f8a2dc2 | 554 | else if (n >= 0) { |
f28169d2 | 555 | err = setup_one_line(lines, n, init, INIT_ONE, error_out); |
2f8a2dc2 | 556 | if (err) |
f28169d2 | 557 | return err; |
d50084a2 | 558 | } |
d50084a2 | 559 | else { |
2f8a2dc2 | 560 | for(i = 0; i < num; i++) { |
f28169d2 JD |
561 | err = setup_one_line(lines, i, init, INIT_ALL, |
562 | error_out); | |
2f8a2dc2 | 563 | if (err) |
f28169d2 JD |
564 | return err; |
565 | } | |
1da177e4 | 566 | } |
418e55d4 | 567 | return n == -1 ? num : n; |
1da177e4 LT |
568 | } |
569 | ||
1f80171e | 570 | int line_config(struct line *lines, unsigned int num, char *str, |
f28169d2 | 571 | const struct chan_opts *opts, char **error_out) |
1da177e4 | 572 | { |
1f80171e | 573 | struct line *line; |
970d6e3a | 574 | char *new; |
418e55d4 | 575 | int n; |
1da177e4 | 576 | |
2f8a2dc2 | 577 | if (*str == '=') { |
f28169d2 JD |
578 | *error_out = "Can't configure all devices from mconsole"; |
579 | return -EINVAL; | |
d571cd18 JD |
580 | } |
581 | ||
970d6e3a | 582 | new = kstrdup(str, GFP_KERNEL); |
2f8a2dc2 | 583 | if (new == NULL) { |
f28169d2 JD |
584 | *error_out = "Failed to allocate memory"; |
585 | return -ENOMEM; | |
1da177e4 | 586 | } |
f28169d2 | 587 | n = line_setup(lines, num, new, error_out); |
2f8a2dc2 | 588 | if (n < 0) |
f28169d2 | 589 | return n; |
1f80171e JD |
590 | |
591 | line = &lines[n]; | |
f28169d2 | 592 | return parse_chan_pair(line->init_str, line, n, opts, error_out); |
1da177e4 LT |
593 | } |
594 | ||
b97b77cc | 595 | int line_get_config(char *name, struct line *lines, unsigned int num, char *str, |
1da177e4 LT |
596 | int size, char **error_out) |
597 | { | |
598 | struct line *line; | |
599 | char *end; | |
600 | int dev, n = 0; | |
601 | ||
602 | dev = simple_strtoul(name, &end, 0); | |
2f8a2dc2 | 603 | if ((*end != '\0') || (end == name)) { |
1da177e4 | 604 | *error_out = "line_get_config failed to parse device number"; |
b97b77cc | 605 | return 0; |
1da177e4 LT |
606 | } |
607 | ||
2f8a2dc2 | 608 | if ((dev < 0) || (dev >= num)) { |
b97b77cc PBG |
609 | *error_out = "device number out of range"; |
610 | return 0; | |
1da177e4 LT |
611 | } |
612 | ||
613 | line = &lines[dev]; | |
614 | ||
d79a5809 | 615 | spin_lock(&line->count_lock); |
2f8a2dc2 | 616 | if (!line->valid) |
1da177e4 | 617 | CONFIG_CHUNK(str, size, n, "none", 1); |
2f8a2dc2 | 618 | else if (line->tty == NULL) |
1da177e4 LT |
619 | CONFIG_CHUNK(str, size, n, line->init_str, 1); |
620 | else n = chan_config_string(&line->chan_list, str, size, error_out); | |
d79a5809 | 621 | spin_unlock(&line->count_lock); |
1da177e4 | 622 | |
b97b77cc | 623 | return n; |
1da177e4 LT |
624 | } |
625 | ||
29d56cfe JD |
626 | int line_id(char **str, int *start_out, int *end_out) |
627 | { | |
628 | char *end; | |
2f8a2dc2 | 629 | int n; |
29d56cfe JD |
630 | |
631 | n = simple_strtoul(*str, &end, 0); | |
2f8a2dc2 JD |
632 | if ((*end != '\0') || (end == *str)) |
633 | return -1; | |
29d56cfe | 634 | |
2f8a2dc2 JD |
635 | *str = end; |
636 | *start_out = n; | |
637 | *end_out = n; | |
638 | return n; | |
29d56cfe JD |
639 | } |
640 | ||
f28169d2 | 641 | int line_remove(struct line *lines, unsigned int num, int n, char **error_out) |
1da177e4 | 642 | { |
da645f3b AV |
643 | if (n >= num) { |
644 | *error_out = "Device number out of range"; | |
645 | return -EINVAL; | |
646 | } | |
647 | return setup_one_line(lines, n, "none", INIT_ONE, error_out); | |
1da177e4 LT |
648 | } |
649 | ||
d5c9ffc6 JD |
650 | struct tty_driver *register_lines(struct line_driver *line_driver, |
651 | const struct tty_operations *ops, | |
652 | struct line *lines, int nlines) | |
1da177e4 LT |
653 | { |
654 | int i; | |
655 | struct tty_driver *driver = alloc_tty_driver(nlines); | |
656 | ||
657 | if (!driver) | |
658 | return NULL; | |
659 | ||
660 | driver->driver_name = line_driver->name; | |
661 | driver->name = line_driver->device_name; | |
1da177e4 LT |
662 | driver->major = line_driver->major; |
663 | driver->minor_start = line_driver->minor_start; | |
664 | driver->type = line_driver->type; | |
665 | driver->subtype = line_driver->subtype; | |
666 | driver->flags = TTY_DRIVER_REAL_RAW; | |
667 | driver->init_termios = tty_std_termios; | |
668 | tty_set_operations(driver, ops); | |
669 | ||
670 | if (tty_register_driver(driver)) { | |
2f8a2dc2 JD |
671 | printk(KERN_ERR "register_lines : can't register %s driver\n", |
672 | line_driver->name); | |
1da177e4 LT |
673 | put_tty_driver(driver); |
674 | return NULL; | |
675 | } | |
676 | ||
2f8a2dc2 JD |
677 | for(i = 0; i < nlines; i++) { |
678 | if (!lines[i].valid) | |
1da177e4 LT |
679 | tty_unregister_device(driver, i); |
680 | } | |
681 | ||
682 | mconsole_register_dev(&line_driver->mc); | |
683 | return driver; | |
684 | } | |
685 | ||
9010772c JD |
686 | static DEFINE_SPINLOCK(winch_handler_lock); |
687 | static LIST_HEAD(winch_handlers); | |
605a69ac | 688 | |
1f80171e | 689 | void lines_init(struct line *lines, int nlines, struct chan_opts *opts) |
1da177e4 LT |
690 | { |
691 | struct line *line; | |
f28169d2 | 692 | char *error; |
1da177e4 LT |
693 | int i; |
694 | ||
2f8a2dc2 | 695 | for(i = 0; i < nlines; i++) { |
1da177e4 LT |
696 | line = &lines[i]; |
697 | INIT_LIST_HEAD(&line->chan_list); | |
9010772c | 698 | |
2f8a2dc2 | 699 | if (line->init_str == NULL) |
d50084a2 JD |
700 | continue; |
701 | ||
702 | line->init_str = kstrdup(line->init_str, GFP_KERNEL); | |
2f8a2dc2 JD |
703 | if (line->init_str == NULL) |
704 | printk(KERN_ERR "lines_init - kstrdup returned NULL\n"); | |
1f80171e | 705 | |
2f8a2dc2 JD |
706 | if (parse_chan_pair(line->init_str, line, i, opts, &error)) { |
707 | printk(KERN_ERR "parse_chan_pair failed for " | |
708 | "device %d : %s\n", i, error); | |
1f80171e JD |
709 | line->valid = 0; |
710 | } | |
1da177e4 LT |
711 | } |
712 | } | |
713 | ||
714 | struct winch { | |
715 | struct list_head list; | |
716 | int fd; | |
717 | int tty_fd; | |
718 | int pid; | |
719 | struct tty_struct *tty; | |
42a359e3 | 720 | unsigned long stack; |
7cf3cf21 | 721 | struct work_struct work; |
1da177e4 LT |
722 | }; |
723 | ||
7cf3cf21 | 724 | static void __free_winch(struct work_struct *work) |
42a359e3 | 725 | { |
7cf3cf21 AV |
726 | struct winch *winch = container_of(work, struct winch, work); |
727 | free_irq(WINCH_IRQ, winch); | |
42a359e3 JD |
728 | |
729 | if (winch->pid != -1) | |
730 | os_kill_process(winch->pid, 1); | |
42a359e3 JD |
731 | if (winch->stack != 0) |
732 | free_stack(winch->stack, 0); | |
42a359e3 JD |
733 | kfree(winch); |
734 | } | |
735 | ||
7cf3cf21 AV |
736 | static void free_winch(struct winch *winch) |
737 | { | |
738 | int fd = winch->fd; | |
739 | winch->fd = -1; | |
740 | if (fd != -1) | |
741 | os_close_file(fd); | |
742 | list_del(&winch->list); | |
743 | __free_winch(&winch->work); | |
744 | } | |
745 | ||
7bea96fd | 746 | static irqreturn_t winch_interrupt(int irq, void *data) |
1da177e4 LT |
747 | { |
748 | struct winch *winch = data; | |
749 | struct tty_struct *tty; | |
750 | struct line *line; | |
7cf3cf21 | 751 | int fd = winch->fd; |
1da177e4 LT |
752 | int err; |
753 | char c; | |
754 | ||
7cf3cf21 AV |
755 | if (fd != -1) { |
756 | err = generic_read(fd, &c, NULL); | |
2f8a2dc2 JD |
757 | if (err < 0) { |
758 | if (err != -EAGAIN) { | |
7cf3cf21 AV |
759 | winch->fd = -1; |
760 | list_del(&winch->list); | |
761 | os_close_file(fd); | |
2f8a2dc2 JD |
762 | printk(KERN_ERR "winch_interrupt : " |
763 | "read failed, errno = %d\n", -err); | |
764 | printk(KERN_ERR "fd %d is losing SIGWINCH " | |
765 | "support\n", winch->tty_fd); | |
7cf3cf21 AV |
766 | INIT_WORK(&winch->work, __free_winch); |
767 | schedule_work(&winch->work); | |
b97b77cc | 768 | return IRQ_HANDLED; |
1da177e4 LT |
769 | } |
770 | goto out; | |
771 | } | |
772 | } | |
42a359e3 | 773 | tty = winch->tty; |
1da177e4 LT |
774 | if (tty != NULL) { |
775 | line = tty->driver_data; | |
438ee679 JD |
776 | if (line != NULL) { |
777 | chan_window_size(&line->chan_list, &tty->winsize.ws_row, | |
778 | &tty->winsize.ws_col); | |
779 | kill_pgrp(tty->pgrp, SIGWINCH, 1); | |
780 | } | |
1da177e4 LT |
781 | } |
782 | out: | |
2f8a2dc2 | 783 | if (winch->fd != -1) |
1da177e4 | 784 | reactivate_fd(winch->fd, WINCH_IRQ); |
b97b77cc | 785 | return IRQ_HANDLED; |
1da177e4 LT |
786 | } |
787 | ||
42a359e3 JD |
788 | void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty, |
789 | unsigned long stack) | |
1da177e4 LT |
790 | { |
791 | struct winch *winch; | |
792 | ||
1da177e4 LT |
793 | winch = kmalloc(sizeof(*winch), GFP_KERNEL); |
794 | if (winch == NULL) { | |
2f8a2dc2 | 795 | printk(KERN_ERR "register_winch_irq - kmalloc failed\n"); |
42a359e3 | 796 | goto cleanup; |
1da177e4 | 797 | } |
605a69ac | 798 | |
1da177e4 LT |
799 | *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list), |
800 | .fd = fd, | |
801 | .tty_fd = tty_fd, | |
802 | .pid = pid, | |
42a359e3 JD |
803 | .tty = tty, |
804 | .stack = stack }); | |
805 | ||
806 | if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, | |
c0b79a90 | 807 | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
42a359e3 | 808 | "winch", winch) < 0) { |
2f8a2dc2 JD |
809 | printk(KERN_ERR "register_winch_irq - failed to register " |
810 | "IRQ\n"); | |
42a359e3 JD |
811 | goto out_free; |
812 | } | |
605a69ac PBG |
813 | |
814 | spin_lock(&winch_handler_lock); | |
1da177e4 | 815 | list_add(&winch->list, &winch_handlers); |
605a69ac PBG |
816 | spin_unlock(&winch_handler_lock); |
817 | ||
42a359e3 | 818 | return; |
e464bf2b | 819 | |
42a359e3 | 820 | out_free: |
e464bf2b | 821 | kfree(winch); |
42a359e3 JD |
822 | cleanup: |
823 | os_kill_process(pid, 1); | |
824 | os_close_file(fd); | |
825 | if (stack != 0) | |
826 | free_stack(stack, 0); | |
e464bf2b JD |
827 | } |
828 | ||
cd2ee4a3 JD |
829 | static void unregister_winch(struct tty_struct *tty) |
830 | { | |
48a0b740 | 831 | struct list_head *ele, *next; |
e464bf2b | 832 | struct winch *winch; |
cd2ee4a3 | 833 | |
605a69ac | 834 | spin_lock(&winch_handler_lock); |
e464bf2b | 835 | |
48a0b740 | 836 | list_for_each_safe(ele, next, &winch_handlers) { |
cd2ee4a3 | 837 | winch = list_entry(ele, struct winch, list); |
2f8a2dc2 | 838 | if (winch->tty == tty) { |
7cf3cf21 | 839 | free_winch(winch); |
e464bf2b | 840 | break; |
2f8a2dc2 JD |
841 | } |
842 | } | |
605a69ac | 843 | spin_unlock(&winch_handler_lock); |
cd2ee4a3 JD |
844 | } |
845 | ||
1da177e4 LT |
846 | static void winch_cleanup(void) |
847 | { | |
e464bf2b | 848 | struct list_head *ele, *next; |
1da177e4 LT |
849 | struct winch *winch; |
850 | ||
e464bf2b JD |
851 | spin_lock(&winch_handler_lock); |
852 | ||
2f8a2dc2 | 853 | list_for_each_safe(ele, next, &winch_handlers) { |
1da177e4 | 854 | winch = list_entry(ele, struct winch, list); |
7cf3cf21 | 855 | free_winch(winch); |
1da177e4 | 856 | } |
e464bf2b JD |
857 | |
858 | spin_unlock(&winch_handler_lock); | |
1da177e4 LT |
859 | } |
860 | __uml_exitcall(winch_cleanup); | |
861 | ||
862 | char *add_xterm_umid(char *base) | |
863 | { | |
864 | char *umid, *title; | |
865 | int len; | |
866 | ||
7eebe8a9 | 867 | umid = get_umid(); |
2f8a2dc2 | 868 | if (*umid == '\0') |
b97b77cc | 869 | return base; |
165dc591 | 870 | |
1da177e4 LT |
871 | len = strlen(base) + strlen(" ()") + strlen(umid) + 1; |
872 | title = kmalloc(len, GFP_KERNEL); | |
2f8a2dc2 JD |
873 | if (title == NULL) { |
874 | printk(KERN_ERR "Failed to allocate buffer for xterm title\n"); | |
b97b77cc | 875 | return base; |
1da177e4 LT |
876 | } |
877 | ||
878 | snprintf(title, len, "%s (%s)", base, umid); | |
b97b77cc | 879 | return title; |
1da177e4 | 880 | } |