This commit was generated by cvs2svn to compensate for changes in r632,
[linux-2.6.git] / arch / um / drivers / line.c
1 /* 
2  * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/sched.h"
7 #include "linux/slab.h"
8 #include "linux/list.h"
9 #include "linux/interrupt.h"
10 #include "linux/devfs_fs_kernel.h"
11 #include "asm/uaccess.h"
12 #include "chan_kern.h"
13 #include "irq_user.h"
14 #include "line.h"
15 #include "kern.h"
16 #include "user_util.h"
17 #include "kern_util.h"
18 #include "os.h"
19 #include "irq_kern.h"
20
21 #define LINE_BUFSIZE 4096
22
23 static irqreturn_t line_interrupt(int irq, void *data, struct pt_regs *unused)
24 {
25         struct line *dev = data;
26
27         if(dev->count > 0) 
28                 chan_interrupt(&dev->chan_list, &dev->task, dev->tty, irq, 
29                                dev);
30         return IRQ_HANDLED;
31 }
32
33 static void line_timer_cb(void *arg)
34 {
35         struct line *dev = arg;
36
37         line_interrupt(dev->driver->read_irq, dev, NULL);
38 }
39
40 static int write_room(struct line *dev)
41 {
42         int n;
43
44         if(dev->buffer == NULL) return(LINE_BUFSIZE - 1);
45
46         n = dev->head - dev->tail;
47         if(n <= 0) n = LINE_BUFSIZE + n;
48         return(n - 1);
49 }
50
51 static int buffer_data(struct line *line, const char *buf, int len)
52 {
53         int end, room;
54
55         if(line->buffer == NULL){
56                 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
57                 if(line->buffer == NULL){
58                         printk("buffer_data - atomic allocation failed\n");
59                         return(0);
60                 }
61                 line->head = line->buffer;
62                 line->tail = line->buffer;
63         }
64
65         room = write_room(line);
66         len = (len > room) ? room : len;
67
68         end = line->buffer + LINE_BUFSIZE - line->tail;
69         if(len < end){
70                 memcpy(line->tail, buf, len);
71                 line->tail += len;
72         }
73         else {
74                 memcpy(line->tail, buf, end);
75                 buf += end;
76                 memcpy(line->buffer, buf, len - end);
77                 line->tail = line->buffer + len - end;
78         }
79
80         return(len);
81 }
82
83 static int flush_buffer(struct line *line)
84 {
85         int n, count;
86
87         if((line->buffer == NULL) || (line->head == line->tail)) return(1);
88
89         if(line->tail < line->head){
90                 count = line->buffer + LINE_BUFSIZE - line->head;
91                 n = write_chan(&line->chan_list, line->head, count,
92                                line->driver->write_irq);
93                 if(n < 0) return(n);
94                 if(n == count) line->head = line->buffer;
95                 else {
96                         line->head += n;
97                         return(0);
98                 }
99         }
100
101         count = line->tail - line->head;
102         n = write_chan(&line->chan_list, line->head, count, 
103                        line->driver->write_irq);
104         if(n < 0) return(n);
105
106         line->head += n;
107         return(line->head == line->tail);
108 }
109
110 int line_write(struct line *lines, struct tty_struct *tty, const char *buf, int len)
111 {
112         struct line *line;
113         unsigned long flags;
114         int n, err, i, ret = 0;
115
116         if(tty->stopped) return 0;
117
118         i = tty->index;
119         line = &lines[i];
120
121         down(&line->sem);
122         if(line->head != line->tail){
123                 local_irq_save(flags);
124                 ret += buffer_data(line, buf, len);
125                 err = flush_buffer(line);
126                 local_irq_restore(flags);
127                 if(err <= 0)
128                         goto out_up;
129         }
130         else {
131                 n = write_chan(&line->chan_list, buf, len, 
132                                line->driver->write_irq);
133                 if(n < 0){
134                         ret = n;
135                         goto out_up;
136                 }
137
138                 len -= n;
139                 ret += n;
140                 if(len > 0)
141                         ret += buffer_data(line, buf + n, len);
142         }
143  out_up:
144         up(&line->sem);
145         return(ret);
146 }
147
148 static irqreturn_t line_write_interrupt(int irq, void *data,
149                                         struct pt_regs *unused)
150 {
151         struct line *dev = data;
152         struct tty_struct *tty = dev->tty;
153         int err;
154
155         err = flush_buffer(dev);
156         if(err == 0)
157                 return(IRQ_NONE);
158         else if(err < 0){
159                 dev->head = dev->buffer;
160                 dev->tail = dev->buffer;
161         }
162
163         if(tty == NULL)
164                 return(IRQ_NONE);
165
166         if(test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
167            (tty->ldisc.write_wakeup != NULL))
168                 (tty->ldisc.write_wakeup)(tty);
169         
170         /* BLOCKING mode
171          * In blocking mode, everything sleeps on tty->write_wait.
172          * Sleeping in the console driver would break non-blocking
173          * writes.
174          */
175
176         if(waitqueue_active(&tty->write_wait))
177                 wake_up_interruptible(&tty->write_wait);
178         return(IRQ_HANDLED);
179 }
180
181 int line_setup_irq(int fd, int input, int output, void *data)
182 {
183         struct line *line = data;
184         struct line_driver *driver = line->driver;
185         int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
186
187         if(input) err = um_request_irq(driver->read_irq, fd, IRQ_READ, 
188                                        line_interrupt, flags, 
189                                        driver->read_irq_name, line);
190         if(err) return(err);
191         if(output) err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, 
192                                         line_write_interrupt, flags, 
193                                         driver->write_irq_name, line);
194         line->have_irq = 1;
195         return(err);
196 }
197
198 void line_disable(struct line *line, int current_irq)
199 {
200         if(!line->have_irq) return;
201
202         if(line->driver->read_irq == current_irq)
203                 free_irq_later(line->driver->read_irq, line);
204         else {
205                 free_irq_by_irq_and_dev(line->driver->read_irq, line);
206                 free_irq(line->driver->read_irq, line);
207         }
208
209         if(line->driver->write_irq == current_irq)
210                 free_irq_later(line->driver->write_irq, line);
211         else {
212                 free_irq_by_irq_and_dev(line->driver->write_irq, line);
213                 free_irq(line->driver->write_irq, line);
214         }
215
216         line->have_irq = 0;
217 }
218
219 int line_open(struct line *lines, struct tty_struct *tty,
220               struct chan_opts *opts)
221 {
222         struct line *line;
223         int n, err = 0;
224
225         if(tty == NULL) n = 0;
226         else n = tty->index;
227         line = &lines[n];
228
229         down(&line->sem);
230         if(line->count == 0){
231                 if(!line->valid){
232                         err = -ENODEV;
233                         goto out;
234                 }
235                 if(list_empty(&line->chan_list)){
236                         err = parse_chan_pair(line->init_str, &line->chan_list,
237                                               line->init_pri, n, opts);
238                         if(err) goto out;
239                         err = open_chan(&line->chan_list);
240                         if(err) goto out;
241                 }
242                 enable_chan(&line->chan_list, line);
243                 INIT_WORK(&line->task, line_timer_cb, line);
244         }
245
246         if(!line->sigio){
247                 chan_enable_winch(&line->chan_list, line);
248                 line->sigio = 1;
249         }
250
251         /* This is outside the if because the initial console is opened
252          * with tty == NULL
253          */
254         line->tty = tty;
255
256         if(tty != NULL){
257                 tty->driver_data = line;
258                 chan_window_size(&line->chan_list, &tty->winsize.ws_row, 
259                                  &tty->winsize.ws_col);
260         }
261
262         line->count++;
263  out:
264         up(&line->sem);
265         return(err);
266 }
267
268 void line_close(struct line *lines, struct tty_struct *tty)
269 {
270         struct line *line;
271         int n;
272
273         if(tty == NULL) n = 0;
274         else n = tty->index;
275         line = &lines[n];
276
277         down(&line->sem);
278         line->count--;
279
280         /* I don't like this, but I can't think of anything better.  What's
281          * going on is that the tty is in the process of being closed for
282          * the last time.  Its count hasn't been dropped yet, so it's still
283          * at 1.  This may happen when line->count != 0 because of the initial
284          * console open (without a tty) bumping it up to 1.
285          */
286         if((line->tty != NULL) && (line->tty->count == 1))
287                 line->tty = NULL;
288         if(line->count == 0)
289                 line_disable(line, -1);
290         up(&line->sem);
291 }
292
293 void close_lines(struct line *lines, int nlines)
294 {
295         int i;
296
297         for(i = 0; i < nlines; i++)
298                 close_chan(&lines[i].chan_list);
299 }
300
301 int line_setup(struct line *lines, int num, char *init, int all_allowed)
302 {
303         int i, n;
304         char *end;
305
306         if(*init == '=') n = -1;
307         else {
308                 n = simple_strtoul(init, &end, 0);
309                 if(*end != '='){
310                         printk(KERN_ERR "line_setup failed to parse \"%s\"\n", 
311                                init);
312                         return(0);
313                 }
314                 init = end;
315         }
316         init++;
317         if((n >= 0) && (n >= num)){
318                 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
319                        n, num - 1);
320                 return(0);
321         }
322         else if(n >= 0){
323                 if(lines[n].count > 0){
324                         printk("line_setup - device %d is open\n", n);
325                         return(0);
326                 }
327                 if(lines[n].init_pri <= INIT_ONE){
328                         lines[n].init_pri = INIT_ONE;
329                         if(!strcmp(init, "none")) lines[n].valid = 0;
330                         else {
331                                 lines[n].init_str = init;
332                                 lines[n].valid = 1;
333                         }       
334                 }
335         }
336         else if(!all_allowed){
337                 printk("line_setup - can't configure all devices from "
338                        "mconsole\n");
339                 return(0);
340         }
341         else {
342                 for(i = 0; i < num; i++){
343                         if(lines[i].init_pri <= INIT_ALL){
344                                 lines[i].init_pri = INIT_ALL;
345                                 if(!strcmp(init, "none")) lines[i].valid = 0;
346                                 else {
347                                         lines[i].init_str = init;
348                                         lines[i].valid = 1;
349                                 }
350                         }
351                 }
352         }
353         return(1);
354 }
355
356 int line_config(struct line *lines, int num, char *str)
357 {
358         char *new = uml_strdup(str);
359
360         if(new == NULL){
361                 printk("line_config - uml_strdup failed\n");
362                 return(-ENOMEM);
363         }
364         return(!line_setup(lines, num, new, 0));
365 }
366
367 int line_get_config(char *name, struct line *lines, int num, char *str, 
368                     int size, char **error_out)
369 {
370         struct line *line;
371         char *end;
372         int dev, n = 0;
373
374         dev = simple_strtoul(name, &end, 0);
375         if((*end != '\0') || (end == name)){
376                 *error_out = "line_get_config failed to parse device number";
377                 return(0);
378         }
379
380         if((dev < 0) || (dev >= num)){
381                 *error_out = "device number of of range";
382                 return(0);
383         }
384
385         line = &lines[dev];
386
387         down(&line->sem);
388         if(!line->valid)
389                 CONFIG_CHUNK(str, size, n, "none", 1);
390         else if(line->count == 0)
391                 CONFIG_CHUNK(str, size, n, line->init_str, 1);
392         else n = chan_config_string(&line->chan_list, str, size, error_out);
393         up(&line->sem);
394
395         return(n);
396 }
397
398 int line_remove(struct line *lines, int num, char *str)
399 {
400         char config[sizeof("conxxxx=none\0")];
401
402         sprintf(config, "%s=none", str);
403         return(!line_setup(lines, num, config, 0));
404 }
405
406 int line_write_room(struct tty_struct *tty)
407 {
408         struct line *dev = tty->driver_data;
409
410         return(write_room(dev));
411 }
412
413 struct tty_driver *line_register_devfs(struct lines *set,
414                          struct line_driver *line_driver, 
415                          struct tty_operations *ops, struct line *lines,
416                          int nlines)
417 {
418         int err, i;
419         char *from, *to;
420         struct tty_driver *driver = alloc_tty_driver(nlines);
421
422         if (!driver)
423                 return NULL;
424
425         driver->driver_name = line_driver->name;
426         driver->name = line_driver->device_name;
427         driver->devfs_name = line_driver->devfs_name;
428         driver->major = line_driver->major;
429         driver->minor_start = line_driver->minor_start;
430         driver->type = line_driver->type;
431         driver->subtype = line_driver->subtype;
432         driver->flags = TTY_DRIVER_REAL_RAW;
433         driver->init_termios = tty_std_termios;
434         tty_set_operations(driver, ops);
435
436         if (tty_register_driver(driver))
437                 panic("line_register_devfs : Couldn't register driver\n");
438
439         from = line_driver->symlink_from;
440         to = line_driver->symlink_to;
441         err = devfs_mk_symlink(from, to);
442         if(err) printk("Symlink creation from /dev/%s to /dev/%s "
443                        "returned %d\n", from, to, err);
444
445         for(i = 0; i < nlines; i++){
446                 if(!lines[i].valid) 
447                         tty_unregister_device(driver, i);
448         }
449
450         mconsole_register_dev(&line_driver->mc);
451         return driver;
452 }
453
454 void lines_init(struct line *lines, int nlines)
455 {
456         struct line *line;
457         int i;
458
459         for(i = 0; i < nlines; i++){
460                 line = &lines[i];
461                 INIT_LIST_HEAD(&line->chan_list);
462                 sema_init(&line->sem, 1);
463                 if(line->init_str != NULL){
464                         line->init_str = uml_strdup(line->init_str);
465                         if(line->init_str == NULL)
466                                 printk("lines_init - uml_strdup returned "
467                                        "NULL\n");
468                 }
469         }
470 }
471
472 struct winch {
473         struct list_head list;
474         int fd;
475         int tty_fd;
476         int pid;
477         struct line *line;
478 };
479
480 irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
481 {
482         struct winch *winch = data;
483         struct tty_struct *tty;
484         int err;
485         char c;
486
487         if(winch->fd != -1){
488                 err = generic_read(winch->fd, &c, NULL);
489                 if(err < 0){
490                         if(err != -EAGAIN){
491                                 printk("winch_interrupt : read failed, "
492                                        "errno = %d\n", -err);
493                                 printk("fd %d is losing SIGWINCH support\n",
494                                        winch->tty_fd);
495                                 return(IRQ_HANDLED);
496                         }
497                         goto out;
498                 }
499         }
500         tty = winch->line->tty;
501         if(tty != NULL){
502                 chan_window_size(&winch->line->chan_list, 
503                                  &tty->winsize.ws_row, 
504                                  &tty->winsize.ws_col);
505                 kill_pg(tty->pgrp, SIGWINCH, 1);
506         }
507  out:
508         if(winch->fd != -1)
509                 reactivate_fd(winch->fd, WINCH_IRQ);
510         return(IRQ_HANDLED);
511 }
512
513 DECLARE_MUTEX(winch_handler_sem);
514 LIST_HEAD(winch_handlers);
515
516 void register_winch_irq(int fd, int tty_fd, int pid, void *line)
517 {
518         struct winch *winch;
519
520         down(&winch_handler_sem);
521         winch = kmalloc(sizeof(*winch), GFP_KERNEL);
522         if(winch == NULL){
523                 printk("register_winch_irq - kmalloc failed\n");
524                 goto out;
525         }
526         *winch = ((struct winch) { .list        = LIST_HEAD_INIT(winch->list),
527                                    .fd          = fd,
528                                    .tty_fd      = tty_fd,
529                                    .pid         = pid,
530                                    .line        = line });
531         list_add(&winch->list, &winch_handlers);
532         if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, 
533                           SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM, 
534                           "winch", winch) < 0)
535                 printk("register_winch_irq - failed to register IRQ\n");
536  out:
537         up(&winch_handler_sem);
538 }
539
540 static void winch_cleanup(void)
541 {
542         struct list_head *ele;
543         struct winch *winch;
544
545         list_for_each(ele, &winch_handlers){
546                 winch = list_entry(ele, struct winch, list);
547                 if(winch->fd != -1){
548                         deactivate_fd(winch->fd, WINCH_IRQ);
549                         os_close_file(winch->fd);
550                 }
551                 if(winch->pid != -1) 
552                         os_kill_process(winch->pid, 1);
553         }
554 }
555
556 __uml_exitcall(winch_cleanup);
557
558 char *add_xterm_umid(char *base)
559 {
560         char *umid, *title;
561         int len;
562
563         umid = get_umid(1);
564         if(umid == NULL) return(base);
565         
566         len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
567         title = kmalloc(len, GFP_KERNEL);
568         if(title == NULL){
569                 printk("Failed to allocate buffer for xterm title\n");
570                 return(base);
571         }
572
573         snprintf(title, len, "%s (%s)", base, umid);
574         return(title);
575 }
576
577 /*
578  * Overrides for Emacs so that we follow Linus's tabbing style.
579  * Emacs will notice this stuff at the end of the file and automatically
580  * adjust the settings for this buffer only.  This must remain at the end
581  * of the file.
582  * ---------------------------------------------------------------------------
583  * Local variables:
584  * c-file-style: "linux"
585  * End:
586  */