]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/um/drivers/line.c
TTY: add tty_port_tty_wakeup helper
[karo-tx-linux.git] / arch / um / drivers / line.c
1 /*
2  * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5
6 #include <linux/irqreturn.h>
7 #include <linux/kd.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include "chan.h"
11 #include <irq_kern.h>
12 #include <irq_user.h>
13 #include <kern_util.h>
14 #include <os.h>
15
16 #define LINE_BUFSIZE 4096
17
18 static irqreturn_t line_interrupt(int irq, void *data)
19 {
20         struct chan *chan = data;
21         struct line *line = chan->line;
22
23         if (line)
24                 chan_interrupt(line, irq);
25
26         return IRQ_HANDLED;
27 }
28
29 /*
30  * Returns the free space inside the ring buffer of this line.
31  *
32  * Should be called while holding line->lock (this does not modify data).
33  */
34 static int write_room(struct line *line)
35 {
36         int n;
37
38         if (line->buffer == NULL)
39                 return LINE_BUFSIZE - 1;
40
41         /* This is for the case where the buffer is wrapped! */
42         n = line->head - line->tail;
43
44         if (n <= 0)
45                 n += LINE_BUFSIZE; /* The other case */
46         return n - 1;
47 }
48
49 int line_write_room(struct tty_struct *tty)
50 {
51         struct line *line = tty->driver_data;
52         unsigned long flags;
53         int room;
54
55         spin_lock_irqsave(&line->lock, flags);
56         room = write_room(line);
57         spin_unlock_irqrestore(&line->lock, flags);
58
59         return room;
60 }
61
62 int line_chars_in_buffer(struct tty_struct *tty)
63 {
64         struct line *line = tty->driver_data;
65         unsigned long flags;
66         int ret;
67
68         spin_lock_irqsave(&line->lock, flags);
69         /* write_room subtracts 1 for the needed NULL, so we readd it.*/
70         ret = LINE_BUFSIZE - (write_room(line) + 1);
71         spin_unlock_irqrestore(&line->lock, flags);
72
73         return ret;
74 }
75
76 /*
77  * This copies the content of buf into the circular buffer associated with
78  * this line.
79  * The return value is the number of characters actually copied, i.e. the ones
80  * for which there was space: this function is not supposed to ever flush out
81  * the circular buffer.
82  *
83  * Must be called while holding line->lock!
84  */
85 static int buffer_data(struct line *line, const char *buf, int len)
86 {
87         int end, room;
88
89         if (line->buffer == NULL) {
90                 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
91                 if (line->buffer == NULL) {
92                         printk(KERN_ERR "buffer_data - atomic allocation "
93                                "failed\n");
94                         return 0;
95                 }
96                 line->head = line->buffer;
97                 line->tail = line->buffer;
98         }
99
100         room = write_room(line);
101         len = (len > room) ? room : len;
102
103         end = line->buffer + LINE_BUFSIZE - line->tail;
104
105         if (len < end) {
106                 memcpy(line->tail, buf, len);
107                 line->tail += len;
108         }
109         else {
110                 /* The circular buffer is wrapping */
111                 memcpy(line->tail, buf, end);
112                 buf += end;
113                 memcpy(line->buffer, buf, len - end);
114                 line->tail = line->buffer + len - end;
115         }
116
117         return len;
118 }
119
120 /*
121  * Flushes the ring buffer to the output channels. That is, write_chan is
122  * called, passing it line->head as buffer, and an appropriate count.
123  *
124  * On exit, returns 1 when the buffer is empty,
125  * 0 when the buffer is not empty on exit,
126  * and -errno when an error occurred.
127  *
128  * Must be called while holding line->lock!*/
129 static int flush_buffer(struct line *line)
130 {
131         int n, count;
132
133         if ((line->buffer == NULL) || (line->head == line->tail))
134                 return 1;
135
136         if (line->tail < line->head) {
137                 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
138                 count = line->buffer + LINE_BUFSIZE - line->head;
139
140                 n = write_chan(line->chan_out, line->head, count,
141                                line->driver->write_irq);
142                 if (n < 0)
143                         return n;
144                 if (n == count) {
145                         /*
146                          * We have flushed from ->head to buffer end, now we
147                          * must flush only from the beginning to ->tail.
148                          */
149                         line->head = line->buffer;
150                 } else {
151                         line->head += n;
152                         return 0;
153                 }
154         }
155
156         count = line->tail - line->head;
157         n = write_chan(line->chan_out, line->head, count,
158                        line->driver->write_irq);
159
160         if (n < 0)
161                 return n;
162
163         line->head += n;
164         return line->head == line->tail;
165 }
166
167 void line_flush_buffer(struct tty_struct *tty)
168 {
169         struct line *line = tty->driver_data;
170         unsigned long flags;
171
172         spin_lock_irqsave(&line->lock, flags);
173         flush_buffer(line);
174         spin_unlock_irqrestore(&line->lock, flags);
175 }
176
177 /*
178  * We map both ->flush_chars and ->put_char (which go in pair) onto
179  * ->flush_buffer and ->write. Hope it's not that bad.
180  */
181 void line_flush_chars(struct tty_struct *tty)
182 {
183         line_flush_buffer(tty);
184 }
185
186 int line_put_char(struct tty_struct *tty, unsigned char ch)
187 {
188         return line_write(tty, &ch, sizeof(ch));
189 }
190
191 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
192 {
193         struct line *line = tty->driver_data;
194         unsigned long flags;
195         int n, ret = 0;
196
197         spin_lock_irqsave(&line->lock, flags);
198         if (line->head != line->tail)
199                 ret = buffer_data(line, buf, len);
200         else {
201                 n = write_chan(line->chan_out, buf, len,
202                                line->driver->write_irq);
203                 if (n < 0) {
204                         ret = n;
205                         goto out_up;
206                 }
207
208                 len -= n;
209                 ret += n;
210                 if (len > 0)
211                         ret += buffer_data(line, buf + n, len);
212         }
213 out_up:
214         spin_unlock_irqrestore(&line->lock, flags);
215         return ret;
216 }
217
218 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
219 {
220         /* nothing */
221 }
222
223 void line_throttle(struct tty_struct *tty)
224 {
225         struct line *line = tty->driver_data;
226
227         deactivate_chan(line->chan_in, line->driver->read_irq);
228         line->throttled = 1;
229 }
230
231 void line_unthrottle(struct tty_struct *tty)
232 {
233         struct line *line = tty->driver_data;
234
235         line->throttled = 0;
236         chan_interrupt(line, line->driver->read_irq);
237
238         /*
239          * Maybe there is enough stuff pending that calling the interrupt
240          * throttles us again.  In this case, line->throttled will be 1
241          * again and we shouldn't turn the interrupt back on.
242          */
243         if (!line->throttled)
244                 reactivate_chan(line->chan_in, line->driver->read_irq);
245 }
246
247 static irqreturn_t line_write_interrupt(int irq, void *data)
248 {
249         struct chan *chan = data;
250         struct line *line = chan->line;
251         int err;
252
253         /*
254          * Interrupts are disabled here because genirq keep irqs disabled when
255          * calling the action handler.
256          */
257
258         spin_lock(&line->lock);
259         err = flush_buffer(line);
260         if (err == 0) {
261                 spin_unlock(&line->lock);
262                 return IRQ_NONE;
263         } else if (err < 0) {
264                 line->head = line->buffer;
265                 line->tail = line->buffer;
266         }
267         spin_unlock(&line->lock);
268
269         tty_port_tty_wakeup(&line->port);
270
271         return IRQ_HANDLED;
272 }
273
274 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
275 {
276         const struct line_driver *driver = line->driver;
277         int err = 0;
278
279         if (input)
280                 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
281                                      line_interrupt, IRQF_SHARED,
282                                      driver->read_irq_name, data);
283         if (err)
284                 return err;
285         if (output)
286                 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
287                                      line_write_interrupt, IRQF_SHARED,
288                                      driver->write_irq_name, data);
289         return err;
290 }
291
292 static int line_activate(struct tty_port *port, struct tty_struct *tty)
293 {
294         int ret;
295         struct line *line = tty->driver_data;
296
297         ret = enable_chan(line);
298         if (ret)
299                 return ret;
300
301         if (!line->sigio) {
302                 chan_enable_winch(line->chan_out, tty);
303                 line->sigio = 1;
304         }
305
306         chan_window_size(line, &tty->winsize.ws_row,
307                 &tty->winsize.ws_col);
308
309         return 0;
310 }
311
312 static const struct tty_port_operations line_port_ops = {
313         .activate = line_activate,
314 };
315
316 int line_open(struct tty_struct *tty, struct file *filp)
317 {
318         struct line *line = tty->driver_data;
319
320         return tty_port_open(&line->port, tty, filp);
321 }
322
323 int line_install(struct tty_driver *driver, struct tty_struct *tty,
324                  struct line *line)
325 {
326         int ret;
327
328         ret = tty_standard_install(driver, tty);
329         if (ret)
330                 return ret;
331
332         tty->driver_data = line;
333
334         return 0;
335 }
336
337 static void unregister_winch(struct tty_struct *tty);
338
339 void line_cleanup(struct tty_struct *tty)
340 {
341         struct line *line = tty->driver_data;
342
343         if (line->sigio) {
344                 unregister_winch(tty);
345                 line->sigio = 0;
346         }
347 }
348
349 void line_close(struct tty_struct *tty, struct file * filp)
350 {
351         struct line *line = tty->driver_data;
352
353         tty_port_close(&line->port, tty, filp);
354 }
355
356 void line_hangup(struct tty_struct *tty)
357 {
358         struct line *line = tty->driver_data;
359
360         tty_port_hangup(&line->port);
361 }
362
363 void close_lines(struct line *lines, int nlines)
364 {
365         int i;
366
367         for(i = 0; i < nlines; i++)
368                 close_chan(&lines[i]);
369 }
370
371 int setup_one_line(struct line *lines, int n, char *init,
372                    const struct chan_opts *opts, char **error_out)
373 {
374         struct line *line = &lines[n];
375         struct tty_driver *driver = line->driver->driver;
376         int err = -EINVAL;
377
378         if (line->port.count) {
379                 *error_out = "Device is already open";
380                 goto out;
381         }
382
383         if (!strcmp(init, "none")) {
384                 if (line->valid) {
385                         line->valid = 0;
386                         kfree(line->init_str);
387                         tty_unregister_device(driver, n);
388                         parse_chan_pair(NULL, line, n, opts, error_out);
389                         err = 0;
390                 }
391         } else {
392                 char *new = kstrdup(init, GFP_KERNEL);
393                 if (!new) {
394                         *error_out = "Failed to allocate memory";
395                         return -ENOMEM;
396                 }
397                 if (line->valid) {
398                         tty_unregister_device(driver, n);
399                         kfree(line->init_str);
400                 }
401                 line->init_str = new;
402                 line->valid = 1;
403                 err = parse_chan_pair(new, line, n, opts, error_out);
404                 if (!err) {
405                         struct device *d = tty_port_register_device(&line->port,
406                                         driver, n, NULL);
407                         if (IS_ERR(d)) {
408                                 *error_out = "Failed to register device";
409                                 err = PTR_ERR(d);
410                                 parse_chan_pair(NULL, line, n, opts, error_out);
411                         }
412                 }
413                 if (err) {
414                         line->init_str = NULL;
415                         line->valid = 0;
416                         kfree(new);
417                 }
418         }
419 out:
420         return err;
421 }
422
423 /*
424  * Common setup code for both startup command line and mconsole initialization.
425  * @lines contains the array (of size @num) to modify;
426  * @init is the setup string;
427  * @error_out is an error string in the case of failure;
428  */
429
430 int line_setup(char **conf, unsigned int num, char **def,
431                char *init, char *name)
432 {
433         char *error;
434
435         if (*init == '=') {
436                 /*
437                  * We said con=/ssl= instead of con#=, so we are configuring all
438                  * consoles at once.
439                  */
440                 *def = init + 1;
441         } else {
442                 char *end;
443                 unsigned n = simple_strtoul(init, &end, 0);
444
445                 if (*end != '=') {
446                         error = "Couldn't parse device number";
447                         goto out;
448                 }
449                 if (n >= num) {
450                         error = "Device number out of range";
451                         goto out;
452                 }
453                 conf[n] = end + 1;
454         }
455         return 0;
456
457 out:
458         printk(KERN_ERR "Failed to set up %s with "
459                "configuration string \"%s\" : %s\n", name, init, error);
460         return -EINVAL;
461 }
462
463 int line_config(struct line *lines, unsigned int num, char *str,
464                 const struct chan_opts *opts, char **error_out)
465 {
466         char *end;
467         int n;
468
469         if (*str == '=') {
470                 *error_out = "Can't configure all devices from mconsole";
471                 return -EINVAL;
472         }
473
474         n = simple_strtoul(str, &end, 0);
475         if (*end++ != '=') {
476                 *error_out = "Couldn't parse device number";
477                 return -EINVAL;
478         }
479         if (n >= num) {
480                 *error_out = "Device number out of range";
481                 return -EINVAL;
482         }
483
484         return setup_one_line(lines, n, end, opts, error_out);
485 }
486
487 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
488                     int size, char **error_out)
489 {
490         struct line *line;
491         char *end;
492         int dev, n = 0;
493
494         dev = simple_strtoul(name, &end, 0);
495         if ((*end != '\0') || (end == name)) {
496                 *error_out = "line_get_config failed to parse device number";
497                 return 0;
498         }
499
500         if ((dev < 0) || (dev >= num)) {
501                 *error_out = "device number out of range";
502                 return 0;
503         }
504
505         line = &lines[dev];
506
507         if (!line->valid)
508                 CONFIG_CHUNK(str, size, n, "none", 1);
509         else {
510                 struct tty_struct *tty = tty_port_tty_get(&line->port);
511                 if (tty == NULL) {
512                         CONFIG_CHUNK(str, size, n, line->init_str, 1);
513                 } else {
514                         n = chan_config_string(line, str, size, error_out);
515                         tty_kref_put(tty);
516                 }
517         }
518
519         return n;
520 }
521
522 int line_id(char **str, int *start_out, int *end_out)
523 {
524         char *end;
525         int n;
526
527         n = simple_strtoul(*str, &end, 0);
528         if ((*end != '\0') || (end == *str))
529                 return -1;
530
531         *str = end;
532         *start_out = n;
533         *end_out = n;
534         return n;
535 }
536
537 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
538 {
539         if (n >= num) {
540                 *error_out = "Device number out of range";
541                 return -EINVAL;
542         }
543         return setup_one_line(lines, n, "none", NULL, error_out);
544 }
545
546 int register_lines(struct line_driver *line_driver,
547                    const struct tty_operations *ops,
548                    struct line *lines, int nlines)
549 {
550         struct tty_driver *driver = alloc_tty_driver(nlines);
551         int err;
552         int i;
553
554         if (!driver)
555                 return -ENOMEM;
556
557         driver->driver_name = line_driver->name;
558         driver->name = line_driver->device_name;
559         driver->major = line_driver->major;
560         driver->minor_start = line_driver->minor_start;
561         driver->type = line_driver->type;
562         driver->subtype = line_driver->subtype;
563         driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
564         driver->init_termios = tty_std_termios;
565         
566         for (i = 0; i < nlines; i++) {
567                 tty_port_init(&lines[i].port);
568                 lines[i].port.ops = &line_port_ops;
569                 spin_lock_init(&lines[i].lock);
570                 lines[i].driver = line_driver;
571                 INIT_LIST_HEAD(&lines[i].chan_list);
572         }
573         tty_set_operations(driver, ops);
574
575         err = tty_register_driver(driver);
576         if (err) {
577                 printk(KERN_ERR "register_lines : can't register %s driver\n",
578                        line_driver->name);
579                 put_tty_driver(driver);
580                 for (i = 0; i < nlines; i++)
581                         tty_port_destroy(&lines[i].port);
582                 return err;
583         }
584
585         line_driver->driver = driver;
586         mconsole_register_dev(&line_driver->mc);
587         return 0;
588 }
589
590 static DEFINE_SPINLOCK(winch_handler_lock);
591 static LIST_HEAD(winch_handlers);
592
593 struct winch {
594         struct list_head list;
595         int fd;
596         int tty_fd;
597         int pid;
598         struct tty_struct *tty;
599         unsigned long stack;
600         struct work_struct work;
601 };
602
603 static void __free_winch(struct work_struct *work)
604 {
605         struct winch *winch = container_of(work, struct winch, work);
606         um_free_irq(WINCH_IRQ, winch);
607
608         if (winch->pid != -1)
609                 os_kill_process(winch->pid, 1);
610         if (winch->stack != 0)
611                 free_stack(winch->stack, 0);
612         kfree(winch);
613 }
614
615 static void free_winch(struct winch *winch)
616 {
617         int fd = winch->fd;
618         winch->fd = -1;
619         if (fd != -1)
620                 os_close_file(fd);
621         list_del(&winch->list);
622         __free_winch(&winch->work);
623 }
624
625 static irqreturn_t winch_interrupt(int irq, void *data)
626 {
627         struct winch *winch = data;
628         struct tty_struct *tty;
629         struct line *line;
630         int fd = winch->fd;
631         int err;
632         char c;
633
634         if (fd != -1) {
635                 err = generic_read(fd, &c, NULL);
636                 if (err < 0) {
637                         if (err != -EAGAIN) {
638                                 winch->fd = -1;
639                                 list_del(&winch->list);
640                                 os_close_file(fd);
641                                 printk(KERN_ERR "winch_interrupt : "
642                                        "read failed, errno = %d\n", -err);
643                                 printk(KERN_ERR "fd %d is losing SIGWINCH "
644                                        "support\n", winch->tty_fd);
645                                 INIT_WORK(&winch->work, __free_winch);
646                                 schedule_work(&winch->work);
647                                 return IRQ_HANDLED;
648                         }
649                         goto out;
650                 }
651         }
652         tty = winch->tty;
653         if (tty != NULL) {
654                 line = tty->driver_data;
655                 if (line != NULL) {
656                         chan_window_size(line, &tty->winsize.ws_row,
657                                          &tty->winsize.ws_col);
658                         kill_pgrp(tty->pgrp, SIGWINCH, 1);
659                 }
660         }
661  out:
662         if (winch->fd != -1)
663                 reactivate_fd(winch->fd, WINCH_IRQ);
664         return IRQ_HANDLED;
665 }
666
667 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
668                         unsigned long stack)
669 {
670         struct winch *winch;
671
672         winch = kmalloc(sizeof(*winch), GFP_KERNEL);
673         if (winch == NULL) {
674                 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
675                 goto cleanup;
676         }
677
678         *winch = ((struct winch) { .list        = LIST_HEAD_INIT(winch->list),
679                                    .fd          = fd,
680                                    .tty_fd      = tty_fd,
681                                    .pid         = pid,
682                                    .tty         = tty,
683                                    .stack       = stack });
684
685         if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
686                            IRQF_SHARED, "winch", winch) < 0) {
687                 printk(KERN_ERR "register_winch_irq - failed to register "
688                        "IRQ\n");
689                 goto out_free;
690         }
691
692         spin_lock(&winch_handler_lock);
693         list_add(&winch->list, &winch_handlers);
694         spin_unlock(&winch_handler_lock);
695
696         return;
697
698  out_free:
699         kfree(winch);
700  cleanup:
701         os_kill_process(pid, 1);
702         os_close_file(fd);
703         if (stack != 0)
704                 free_stack(stack, 0);
705 }
706
707 static void unregister_winch(struct tty_struct *tty)
708 {
709         struct list_head *ele, *next;
710         struct winch *winch;
711
712         spin_lock(&winch_handler_lock);
713
714         list_for_each_safe(ele, next, &winch_handlers) {
715                 winch = list_entry(ele, struct winch, list);
716                 if (winch->tty == tty) {
717                         free_winch(winch);
718                         break;
719                 }
720         }
721         spin_unlock(&winch_handler_lock);
722 }
723
724 static void winch_cleanup(void)
725 {
726         struct list_head *ele, *next;
727         struct winch *winch;
728
729         spin_lock(&winch_handler_lock);
730
731         list_for_each_safe(ele, next, &winch_handlers) {
732                 winch = list_entry(ele, struct winch, list);
733                 free_winch(winch);
734         }
735
736         spin_unlock(&winch_handler_lock);
737 }
738 __uml_exitcall(winch_cleanup);
739
740 char *add_xterm_umid(char *base)
741 {
742         char *umid, *title;
743         int len;
744
745         umid = get_umid();
746         if (*umid == '\0')
747                 return base;
748
749         len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
750         title = kmalloc(len, GFP_KERNEL);
751         if (title == NULL) {
752                 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
753                 return base;
754         }
755
756         snprintf(title, len, "%s (%s)", base, umid);
757         return title;
758 }