OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [pdc_console.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *  linux/drivers/char/pdc_console.c
3
 *
4
 *  2001, Christoph Plattner
5
 *
6
 *  Driver template was linux's serial.c
7
 *
8
 */
9
 
10
static char *pdc_drv_version = "0.3";
11
static char *pdc_drv_revdate = "2001-11-17";
12
#define AUTHOR "christoph.plattner@gmx.at"
13
#include <linux/config.h>
14
#include <linux/version.h>
15
 
16
#undef PDC_DRV_DEBUG
17
 
18
#undef SERIAL_PARANOIA_CHECK
19
#define CONFIG_SERIAL_NOPAUSE_IO
20
#define SERIAL_DO_RESTART
21
 
22
#define PDC_POLL_DELAY (30 * HZ / 1000)
23
 
24
/*
25
 * End of serial driver configuration section.
26
 */
27
 
28
#include <linux/module.h>
29
 
30
#include <linux/types.h>
31
#include <linux/serial.h>
32
#include <linux/serialP.h>
33
#include <linux/serial_reg.h>
34
#include <asm/serial.h>
35
#define LOCAL_VERSTRING ""
36
 
37
#include <linux/errno.h>
38
#include <linux/signal.h>
39
#include <linux/sched.h>
40
#include <linux/timer.h>
41
#include <linux/interrupt.h>
42
#include <linux/tty.h>
43
#include <linux/tty_flip.h>
44
#include <linux/major.h>
45
#include <linux/string.h>
46
#include <linux/fcntl.h>
47
#include <linux/ptrace.h>
48
#include <linux/ioport.h>
49
#include <linux/mm.h>
50
#include <linux/slab.h>
51
#include <linux/init.h>
52
#include <asm/uaccess.h>
53
#include <linux/delay.h>
54
 
55
#include <asm/system.h>
56
#include <asm/io.h>
57
#include <asm/irq.h>
58
#include <asm/bitops.h>
59
 
60
#ifdef CONFIG_GSC
61
#include <asm/gsc.h>
62
#endif
63
 
64
extern int pdc_console_poll_key(void *);
65
extern void pdc_outc(unsigned char);
66
 
67
static char *pdc_drv_name = "PDC Software Console";
68
 
69
static struct tty_driver pdc_drv_driver;
70
static int pdc_drv_refcount = 0;
71
static struct async_struct *pdc_drv_info;
72
 
73
static struct timer_list pdc_drv_timer;
74
 
75
/* serial subtype definitions */
76
#ifndef SERIAL_TYPE_NORMAL
77
#define SERIAL_TYPE_NORMAL      1
78
#define SERIAL_TYPE_CALLOUT     2
79
#endif
80
 
81
#define NR_PORTS 1
82
#define PDC_DUMMY_BUF 2048
83
 
84
static struct tty_struct *pdc_drv_table[NR_PORTS];
85
static struct termios *pdc_drv_termios[NR_PORTS];
86
static struct termios *pdc_drv_termios_locked[NR_PORTS];
87
 
88
/*
89
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
90
 * lock it in case the copy_from_user blocks while swapping in a page,
91
 * and some other program tries to do a serial write at the same time.
92
 * Since the lock will only come under contention when the system is
93
 * swapping and available memory is low, it makes sense to share one
94
 * buffer across all the serial ports, since it significantly saves
95
 * memory if large numbers of serial ports are open.
96
 */
97
static unsigned char *tmp_buf;
98
#ifdef DECLARE_MUTEX
99
static DECLARE_MUTEX(tmp_buf_sem);
100
#else
101
static struct semaphore tmp_buf_sem = MUTEX;
102
#endif
103
 
104
/*
105
 * ------------------------------------------------------------
106
 * pdc_stop() and pdc_start()
107
 *
108
 * This routines are called before setting or resetting tty->stopped.
109
 * They enable or disable transmitter interrupts, as necessary.
110
 * ------------------------------------------------------------
111
 */
112
static void
113
pdc_stop(struct tty_struct *tty)
114
{
115
}
116
 
117
static void
118
pdc_start(struct tty_struct *tty)
119
{
120
}
121
 
122
/*
123
 * ----------------------------------------------------------------------
124
 *
125
 * Here starts the interrupt handling routines.  All of the following
126
 * subroutines are declared as inline and are folded into
127
 * rs_interrupt().  They were separated out for readability's sake.
128
 *
129
 * Note: rs_interrupt() is a "fast" interrupt, which means that it
130
 * runs with interrupts turned off.  People who may want to modify
131
 * rs_interrupt() should try to keep the interrupt handler as fast as
132
 * possible.  After you are done making modifications, it is not a bad
133
 * idea to do:
134
 *
135
 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
136
 *
137
 * and look at the resulting assemble code in serial.s.
138
 *
139
 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
140
 * -----------------------------------------------------------------------
141
 */
142
 
143
static void
144
receive_chars(struct async_struct *info, int *status, struct pt_regs *regs)
145
{
146
        struct tty_struct *tty = info->tty;
147
        unsigned char ch;
148
        int __ch;
149
 
150
        while (1) {
151
                __ch = pdc_console_poll_key(NULL);
152
 
153
                if (__ch == -1) /* no character available */
154
                        break;
155
 
156
                ch = (unsigned char) ((unsigned) __ch & 0x000000ffu);
157
 
158
                if (tty->flip.count >= TTY_FLIPBUF_SIZE)
159
                        continue;
160
 
161
                *tty->flip.char_buf_ptr = ch;
162
                *tty->flip.flag_buf_ptr = 0;
163
 
164
                tty->flip.flag_buf_ptr++;
165
                tty->flip.char_buf_ptr++;
166
                tty->flip.count++;
167
        }
168
 
169
        tty_flip_buffer_push(tty);
170
}
171
 
172
static void
173
pdc_drv_poll(unsigned long dummy)
174
{
175
        struct async_struct *info;
176
        int status = 0;
177
 
178
        info = pdc_drv_info;
179
 
180
        if (!info || !info->tty || (pdc_drv_refcount == 0)) {
181
                /* do nothing */
182
        } else {
183
                receive_chars(info, &status, NULL);
184
                info->last_active = jiffies;
185
        }
186
 
187
        mod_timer(&pdc_drv_timer, jiffies + PDC_POLL_DELAY);
188
}
189
 
190
static void
191
pdc_put_char(struct tty_struct *tty, unsigned char ch)
192
{
193
#ifdef PDC_DRV_DEBUG
194
        printk(KERN_NOTICE "[%s] %c return\n", __FUNCTION__, ch);
195
#endif
196
        pdc_outc(ch);
197
}
198
 
199
static void
200
pdc_flush_chars(struct tty_struct *tty)
201
{
202
        /* PCD console always flushed all characters */
203
 
204
#ifdef PDC_DRV_DEBUG
205
        printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
206
#endif
207
 
208
        /* nothing to do */
209
}
210
 
211
static int
212
pdc_write(struct tty_struct *tty, int from_user,
213
          const unsigned char *buf, int count)
214
{
215
        char pdc_tmp_buf[PDC_DUMMY_BUF];
216
        char *pdc_tmp_buf_ptr;
217
        int len;
218
        int ret = 0;
219
 
220
#ifdef PDC_DRV_DEBUG
221
        printk(KERN_NOTICE "[%s] entry\n", __FUNCTION__);
222
#endif
223
        while (count) {
224
                if (count < PDC_DUMMY_BUF)
225
                        len = count;
226
                else
227
                        len = PDC_DUMMY_BUF;
228
 
229
                if (from_user) {
230
                        copy_from_user(pdc_tmp_buf, buf, len);
231
                        pdc_tmp_buf_ptr = pdc_tmp_buf;
232
                } else
233
                        pdc_tmp_buf_ptr = (char *) buf;
234
 
235
                while (len) {
236
                        pdc_outc(*pdc_tmp_buf_ptr);
237
                        buf++;
238
                        pdc_tmp_buf_ptr++;
239
                        ret++;
240
                        count--;
241
                        len--;
242
                }
243
        }
244
#ifdef PDC_DRV_DEBUG
245
        printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
246
#endif
247
        return ret;
248
}
249
 
250
static int
251
pdc_write_room(struct tty_struct *tty)
252
{
253
#ifdef PDC_DRV_DEBUG
254
        printk(KERN_NOTICE "[%s] entry\n", __FUNCTION__);
255
#endif
256
        return PDC_DUMMY_BUF;
257
}
258
 
259
static int
260
pdc_chars_in_buffer(struct tty_struct *tty)
261
{
262
#ifdef PDC_DRV_DEBUG
263
        printk(KERN_NOTICE "[%s] entry\n", __FUNCTION__);
264
#endif
265
        return 0;                /* no characters in buffer, always flushed ! */
266
}
267
 
268
static void
269
pdc_flush_buffer(struct tty_struct *tty)
270
{
271
#ifdef PDC_DRV_DEBUG
272
        printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
273
#endif
274
}
275
 
276
/*
277
 * This function is used to send a high-priority XON/XOFF character to
278
 * the device
279
 */
280
static void
281
pdc_send_xchar(struct tty_struct *tty, char ch)
282
{
283
}
284
 
285
/*
286
 * ------------------------------------------------------------
287
 * pdc_throttle()
288
 *
289
 * This routine is called by the upper-layer tty layer to signal that
290
 * incoming characters should be throttled.
291
 * ------------------------------------------------------------
292
 */
293
static void
294
pdc_throttle(struct tty_struct *tty)
295
{
296
}
297
 
298
static void
299
pdc_unthrottle(struct tty_struct *tty)
300
{
301
}
302
 
303
/*
304
 * ------------------------------------------------------------
305
 * pdc_ioctl() and friends
306
 * ------------------------------------------------------------
307
 */
308
 
309
static void
310
pdc_break(struct tty_struct *tty, int break_state)
311
{
312
}
313
 
314
static int
315
get_serial_info(struct async_struct * info,
316
                           struct serial_struct * retinfo)
317
{
318
        struct serial_struct tmp;
319
 
320
        if (!retinfo)
321
                return -EFAULT;
322
        memset(&tmp, 0, sizeof(tmp));
323
        tmp.line = info->line;
324
        tmp.port = info->line;
325
        tmp.flags = info->flags;
326
        tmp.close_delay = info->close_delay;
327
        return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
328
}
329
 
330
static int get_modem_info(struct async_struct * info, unsigned int *value)
331
{
332
        unsigned int result = TIOCM_DTR|TIOCM_CAR|TIOCM_CTS|TIOCM_RTS;
333
 
334
        return copy_to_user(value, &result, sizeof(int)) ? -EFAULT : 0;
335
}
336
 
337
static int get_lsr_info(struct async_struct * info, unsigned int *value)
338
{
339
        unsigned int result = TIOCSER_TEMT;
340
 
341
        return copy_to_user(value, &result, sizeof(int)) ? -EFAULT : 0;
342
}
343
 
344
static int
345
pdc_ioctl(struct tty_struct *tty, struct file *file,
346
          unsigned int cmd, unsigned long arg)
347
{
348
        struct async_struct *info = (struct async_struct *) tty->driver_data;
349
 
350
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
351
            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
352
            (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
353
                if (tty->flags & (1 << TTY_IO_ERROR))
354
                        return -EIO;
355
        }
356
 
357
        switch (cmd) {
358
        case TIOCMGET:
359
                return get_modem_info(info, (unsigned int *) arg);
360
        case TIOCMBIS:
361
        case TIOCMBIC:
362
        case TIOCMSET:
363
                return 0;
364
        case TIOCGSERIAL:
365
                return get_serial_info(info, (struct serial_struct *) arg);
366
        case TIOCSSERIAL:
367
                return 0;
368
        case TIOCSERCONFIG:
369
                return 0;
370
 
371
        case TIOCSERGETLSR:     /* Get line status register */
372
                return get_lsr_info(info, (unsigned int *) arg);
373
 
374
        case TIOCSERGSTRUCT:
375
                if (copy_to_user((struct async_struct *) arg,
376
                                 info, sizeof (struct async_struct)))
377
                        return -EFAULT;
378
                return 0;
379
 
380
        case TIOCMIWAIT:
381
                return 0;
382
 
383
        case TIOCGICOUNT:
384
                return 0;
385
        case TIOCSERGWILD:
386
        case TIOCSERSWILD:
387
                /* "setserial -W" is called in Debian boot */
388
                printk("TIOCSER?WILD ioctl obsolete, ignored.\n");
389
                return 0;
390
 
391
        default:
392
                return -ENOIOCTLCMD;
393
        }
394
        return 0;
395
}
396
 
397
static void
398
pdc_set_termios(struct tty_struct *tty, struct termios *old_termios)
399
{
400
 
401
#if 0                           /* XXX CP, has to be checked, if there is stuff to do */
402
        struct async_struct *info = (struct async_struct *) tty->driver_data;
403
        unsigned long flags;
404
        unsigned int cflag = tty->termios->c_cflag;
405
 
406
        if ((cflag == old_termios->c_cflag)
407
            && (RELEVANT_IFLAG(tty->termios->c_iflag)
408
                == RELEVANT_IFLAG(old_termios->c_iflag)))
409
                return;
410
#if 0
411
        change_speed(info, old_termios);
412
#endif
413
        /* Handle turning off CRTSCTS */
414
        if ((old_termios->c_cflag & CRTSCTS) &&
415
            !(tty->termios->c_cflag & CRTSCTS)) {
416
                tty->hw_stopped = 0;
417
                pdc_start(tty);
418
        }
419
#endif
420
}
421
 
422
/*
423
 * ------------------------------------------------------------
424
 * pdc_close()
425
 *
426
 * This routine is called when the serial port gets closed.  First, we
427
 * wait for the last remaining data to be sent.  Then, we unlink its
428
 * async structure from the interrupt chain if necessary, and we free
429
 * that IRQ if nothing is left in the chain.
430
 * ------------------------------------------------------------
431
 */
432
static void
433
pdc_close(struct tty_struct *tty, struct file *filp)
434
{
435
        struct async_struct *info = (struct async_struct *) tty->driver_data;
436
 
437
#ifdef PDC_DEBUG_OPEN
438
        printk("pdc_close ttyB%d, count = %d\n", info->line, state->count);
439
#endif
440
        pdc_drv_refcount--;
441
        if (pdc_drv_refcount > 0)
442
                return;
443
 
444
        info->flags |= ASYNC_CLOSING;
445
 
446
        /*
447
         * Save the termios structure, since this port may have
448
         * separate termios for callout and dialin.
449
         */
450
        if (info->flags & ASYNC_NORMAL_ACTIVE)
451
                info->state->normal_termios = *tty->termios;
452
        if (info->flags & ASYNC_CALLOUT_ACTIVE)
453
                info->state->callout_termios = *tty->termios;
454
 
455
        /*
456
         * At this point we stop accepting input.  To do this, we
457
         * disable the receive line status interrupts, and tell the
458
         * interrupt driver to stop checking the data ready bit in the
459
         * line status register.
460
         */
461
 
462
        /* XXX CP: make mask for receive !!! */
463
 
464
        if (tty->driver.flush_buffer)
465
                tty->driver.flush_buffer(tty);
466
        if (tty->ldisc.flush_buffer)
467
                tty->ldisc.flush_buffer(tty);
468
        tty->closing = 0;
469
        info->event = 0;
470
        info->tty = 0;
471
        pdc_drv_info = NULL;
472
        if (info->blocked_open) {
473
                if (info->close_delay) {
474
                        set_current_state(TASK_INTERRUPTIBLE);
475
                        schedule_timeout(info->close_delay);
476
                }
477
                wake_up_interruptible(&info->open_wait);
478
        }
479
        info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE |
480
                         ASYNC_CLOSING);
481
        wake_up_interruptible(&info->close_wait);
482
        MOD_DEC_USE_COUNT;
483
}
484
 
485
/*
486
 * pdc_wait_until_sent() --- wait until the transmitter is empty
487
 */
488
static void
489
pdc_wait_until_sent(struct tty_struct *tty, int timeout)
490
{
491
        /* we always send immideate */
492
}
493
 
494
/*
495
 * pdc_hangup() --- called by tty_hangup() when a hangup is signaled.
496
 */
497
static void
498
pdc_hangup(struct tty_struct *tty)
499
{
500
}
501
 
502
/*
503
 * ------------------------------------------------------------
504
 * pdc_open() and friends
505
 * ------------------------------------------------------------
506
 */
507
 
508
static int
509
get_async_struct(int line, struct async_struct **ret_info)
510
{
511
        struct async_struct *info;
512
 
513
        info = kmalloc(sizeof (struct async_struct), GFP_KERNEL);
514
        if (!info) {
515
                return -ENOMEM;
516
        }
517
        memset(info, 0, sizeof (struct async_struct));
518
        init_waitqueue_head(&info->open_wait);
519
        init_waitqueue_head(&info->close_wait);
520
        init_waitqueue_head(&info->delta_msr_wait);
521
        info->magic = SERIAL_MAGIC;
522
        info->port = 0;
523
        info->flags = 0;
524
        info->io_type = 0;
525
        info->iomem_base = 0;
526
        info->iomem_reg_shift = 0;
527
        info->xmit_fifo_size = PDC_DUMMY_BUF;
528
        info->line = line;
529
        info->tqueue.routine = NULL;
530
        info->tqueue.data = info;
531
        info->state = NULL;
532
        *ret_info = info;
533
        return 0;
534
}
535
 
536
/*
537
 * This routine is called whenever a serial port is opened.  It
538
 * enables interrupts for a serial port, linking in its async structure into
539
 * the IRQ chain.   It also performs the serial-specific
540
 * initialization for the tty structure.
541
 */
542
static int
543
pdc_open(struct tty_struct *tty, struct file *filp)
544
{
545
        struct async_struct *info;
546
        int retval, line;
547
        unsigned long page;
548
 
549
        MOD_INC_USE_COUNT;
550
        line = MINOR(tty->device) - tty->driver.minor_start;
551
        if ((line < 0) || (line >= NR_PORTS)) {
552
                MOD_DEC_USE_COUNT;
553
                return -ENODEV;
554
        }
555
        retval = get_async_struct(line, &info);
556
        if (retval) {
557
                MOD_DEC_USE_COUNT;
558
                return retval;
559
        }
560
        tty->driver_data = info;
561
        info->tty = tty;
562
        pdc_drv_info = info;
563
 
564
#ifdef PDC_DEBUG_OPEN
565
        printk("pdc_open %s%d, count = %d\n", tty->driver.name, info->line,
566
               info->state->count);
567
#endif
568
        info->tty->low_latency = 0;
569
        if (!tmp_buf) {
570
                page = get_zeroed_page(GFP_KERNEL);
571
                if (!page) {
572
                        MOD_DEC_USE_COUNT;
573
                        return -ENOMEM;
574
                }
575
                if (tmp_buf)
576
                        free_page(page);
577
                else
578
                        tmp_buf = (unsigned char *) page;
579
        }
580
 
581
        info->session = current->session;
582
        info->pgrp = current->pgrp;
583
 
584
#ifdef PDC_DEBUG_OPEN
585
        printk("pdc_open ttyB%d successful...", info->line);
586
#endif
587
        pdc_drv_refcount++;
588
        return 0;
589
}
590
 
591
/*
592
 * ---------------------------------------------------------------------
593
 * pdc_init() and friends
594
 *
595
 * pdc_init() is called at boot-time to initialize the pdc driver.
596
 * ---------------------------------------------------------------------
597
 */
598
 
599
static void
600
show_pdc_drv_version(void)
601
{
602
        printk(KERN_INFO "%s version %s%s (%s), %s\n", pdc_drv_name,
603
               pdc_drv_version, LOCAL_VERSTRING, pdc_drv_revdate, AUTHOR);
604
}
605
 
606
/*
607
 * The serial driver boot-time initialization code!
608
 */
609
static int __init
610
pdc_drv_init(void)
611
{
612
        init_timer(&pdc_drv_timer);
613
        pdc_drv_timer.function = pdc_drv_poll;
614
        mod_timer(&pdc_drv_timer, jiffies + PDC_POLL_DELAY);
615
 
616
        show_pdc_drv_version();
617
 
618
        /* Initialize the tty_driver structure */
619
 
620
        memset(&pdc_drv_driver, 0, sizeof (struct tty_driver));
621
        pdc_drv_driver.magic = TTY_DRIVER_MAGIC;
622
        pdc_drv_driver.driver_name = "pdc_console";
623
#ifdef CONFIG_DEVFS_FS
624
        pdc_drv_driver.name = "ttb/%d";
625
#else
626
        pdc_drv_driver.name = "ttyB";
627
#endif
628
        pdc_drv_driver.major = MUX_MAJOR;
629
        pdc_drv_driver.minor_start = 0;
630
        pdc_drv_driver.num = NR_PORTS;
631
        pdc_drv_driver.type = TTY_DRIVER_TYPE_SERIAL;
632
        pdc_drv_driver.subtype = SERIAL_TYPE_NORMAL;
633
        pdc_drv_driver.init_termios = tty_std_termios;
634
        pdc_drv_driver.init_termios.c_cflag =
635
            B9600 | CS8 | CREAD | HUPCL | CLOCAL;
636
        pdc_drv_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
637
        pdc_drv_driver.refcount = &pdc_drv_refcount;
638
        pdc_drv_driver.table = pdc_drv_table;
639
        pdc_drv_driver.termios = pdc_drv_termios;
640
        pdc_drv_driver.termios_locked = pdc_drv_termios_locked;
641
 
642
        pdc_drv_driver.open = pdc_open;
643
        pdc_drv_driver.close = pdc_close;
644
        pdc_drv_driver.write = pdc_write;
645
        pdc_drv_driver.put_char = pdc_put_char;
646
        pdc_drv_driver.flush_chars = pdc_flush_chars;
647
        pdc_drv_driver.write_room = pdc_write_room;
648
        pdc_drv_driver.chars_in_buffer = pdc_chars_in_buffer;
649
        pdc_drv_driver.flush_buffer = pdc_flush_buffer;
650
        pdc_drv_driver.ioctl = pdc_ioctl;
651
        pdc_drv_driver.throttle = pdc_throttle;
652
        pdc_drv_driver.unthrottle = pdc_unthrottle;
653
        pdc_drv_driver.set_termios = pdc_set_termios;
654
        pdc_drv_driver.stop = pdc_stop;
655
        pdc_drv_driver.start = pdc_start;
656
        pdc_drv_driver.hangup = pdc_hangup;
657
        pdc_drv_driver.break_ctl = pdc_break;
658
        pdc_drv_driver.send_xchar = pdc_send_xchar;
659
        pdc_drv_driver.wait_until_sent = pdc_wait_until_sent;
660
        pdc_drv_driver.read_proc = NULL;
661
 
662
        if (tty_register_driver(&pdc_drv_driver))
663
                panic("Couldn't register pdc_console driver\n");
664
 
665
        return 0;
666
}
667
 
668
static void __exit
669
pdc_fini(void)
670
{
671
        int e1;
672
 
673
        if ((e1 = tty_unregister_driver(&pdc_drv_driver)))
674
                printk("pdc_console: failed to unregister pdc_drv driver (%d)\n",
675
                       e1);
676
}
677
 
678
module_init(pdc_drv_init);
679
module_exit(pdc_fini);
680
MODULE_DESCRIPTION("PDC Software Console");
681
MODULE_AUTHOR(AUTHOR);

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.