File: pdc_console.c

package info (click to toggle)
kernel-image-2.4.17-hppa 32.4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 156,356 kB
  • ctags: 442,585
  • sloc: ansic: 2,542,442; asm: 144,771; makefile: 8,468; sh: 3,097; perl: 2,578; yacc: 1,177; tcl: 577; lex: 352; awk: 251; lisp: 218; sed: 72
file content (651 lines) | stat: -rw-r--r-- 15,662 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
 *  linux/drivers/char/pdc_console.c
 *
 *  2001, Christoph Plattner
 * 
 *  Driver template was linux's serial.c
 *
 */

static char *pdc_drv_version = "0.3";
static char *pdc_drv_revdate = "2001-11-17";
#define AUTHOR "christoph.plattner@gmx.at"
#include <linux/config.h>
#include <linux/version.h>

#undef PDC_DRV_DEBUG

#undef SERIAL_PARANOIA_CHECK
#define CONFIG_SERIAL_NOPAUSE_IO
#define SERIAL_DO_RESTART

#define PDC_POLL_DELAY (30 * HZ / 1000)

/*
 * End of serial driver configuration section.
 */

#include <linux/module.h>

#include <linux/types.h>
#include <linux/serial.h>
#include <linux/serialP.h>
#include <linux/serial_reg.h>
#include <asm/serial.h>
#define LOCAL_VERSTRING ""

#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <linux/delay.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/bitops.h>

#ifdef CONFIG_GSC
#include <asm/gsc.h>
#endif

extern int pdc_console_poll_key(void *);
extern void pdc_outc(unsigned char);

static char *pdc_drv_name = "PDC Software Console";

static struct tty_driver pdc_drv_driver;
static int pdc_drv_refcount = 0;
static struct async_struct *pdc_drv_info;

static struct timer_list pdc_drv_timer;

/* serial subtype definitions */
#ifndef SERIAL_TYPE_NORMAL
#define SERIAL_TYPE_NORMAL	1
#define SERIAL_TYPE_CALLOUT	2
#endif

#define NR_PORTS 1
#define PDC_DUMMY_BUF 2048

static struct tty_struct *pdc_drv_table[NR_PORTS];
static struct termios *pdc_drv_termios[NR_PORTS];
static struct termios *pdc_drv_termios_locked[NR_PORTS];

/*
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
 * lock it in case the copy_from_user blocks while swapping in a page,
 * and some other program tries to do a serial write at the same time.
 * Since the lock will only come under contention when the system is
 * swapping and available memory is low, it makes sense to share one
 * buffer across all the serial ports, since it significantly saves
 * memory if large numbers of serial ports are open.
 */
static unsigned char *tmp_buf;
#ifdef DECLARE_MUTEX
static DECLARE_MUTEX(tmp_buf_sem);
#else
static struct semaphore tmp_buf_sem = MUTEX;
#endif

/*
 * ------------------------------------------------------------
 * pdc_stop() and pdc_start()
 *
 * This routines are called before setting or resetting tty->stopped.
 * They enable or disable transmitter interrupts, as necessary.
 * ------------------------------------------------------------
 */
static void
pdc_stop(struct tty_struct *tty)
{
}

static void
pdc_start(struct tty_struct *tty)
{
}

/*
 * ----------------------------------------------------------------------
 *
 * Here starts the interrupt handling routines.  All of the following
 * subroutines are declared as inline and are folded into
 * rs_interrupt().  They were separated out for readability's sake.
 *
 * Note: rs_interrupt() is a "fast" interrupt, which means that it
 * runs with interrupts turned off.  People who may want to modify
 * rs_interrupt() should try to keep the interrupt handler as fast as
 * possible.  After you are done making modifications, it is not a bad
 * idea to do:
 * 
 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
 *
 * and look at the resulting assemble code in serial.s.
 *
 * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
 * -----------------------------------------------------------------------
 */

static void
receive_chars(struct async_struct *info, int *status, struct pt_regs *regs)
{
	struct tty_struct *tty = info->tty;
	unsigned char ch;
	int __ch;

	while (1) {
		__ch = pdc_console_poll_key(NULL);

		if (__ch == -1)	/* no character available */
			break;

		ch = (unsigned char) ((unsigned) __ch & 0x000000ffu);

		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
			continue;

		*tty->flip.char_buf_ptr = ch;
		*tty->flip.flag_buf_ptr = 0;

		tty->flip.flag_buf_ptr++;
		tty->flip.char_buf_ptr++;
		tty->flip.count++;
	}

	tty_flip_buffer_push(tty);
}

static void
pdc_drv_poll(unsigned long dummy)
{
	struct async_struct *info;
	int status = 0;

	info = pdc_drv_info;

	if (!info || !info->tty || (pdc_drv_refcount == 0)) {
		/* do nothing */
	} else {
		receive_chars(info, &status, NULL);
		info->last_active = jiffies;
	}

	mod_timer(&pdc_drv_timer, jiffies + PDC_POLL_DELAY);
}

static void
pdc_put_char(struct tty_struct *tty, unsigned char ch)
{
#ifdef PDC_DRV_DEBUG
	printk(KERN_NOTICE "[%s] %c return\n", __FUNCTION__, ch);
#endif
	pdc_outc(ch);
}

static void
pdc_flush_chars(struct tty_struct *tty)
{
	/* PCD console always flushed all characters */

#ifdef PDC_DRV_DEBUG
	printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
#endif

	/* nothing to do */
}

static int
pdc_write(struct tty_struct *tty, int from_user,
	  const unsigned char *buf, int count)
{
	char pdc_tmp_buf[PDC_DUMMY_BUF];
	char *pdc_tmp_buf_ptr;
	int len;
	int ret = 0;

#ifdef PDC_DRV_DEBUG
	printk(KERN_NOTICE "[%s] entry\n", __FUNCTION__);
#endif
	while (count) {
		if (count < PDC_DUMMY_BUF)
			len = count;
		else
			len = PDC_DUMMY_BUF;

		if (from_user) {
			copy_from_user(pdc_tmp_buf, buf, len);
			pdc_tmp_buf_ptr = pdc_tmp_buf;
		} else
			pdc_tmp_buf_ptr = (char *) buf;

		while (len) {
			pdc_outc(*pdc_tmp_buf_ptr);
			buf++;
			pdc_tmp_buf_ptr++;
			ret++;
			count--;
			len--;
		}
	}
#ifdef PDC_DRV_DEBUG
	printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
#endif
	return ret;
}

static int
pdc_write_room(struct tty_struct *tty)
{
#ifdef PDC_DRV_DEBUG
	printk(KERN_NOTICE "[%s] entry\n", __FUNCTION__);
#endif
	return PDC_DUMMY_BUF;
}

static int
pdc_chars_in_buffer(struct tty_struct *tty)
{
#ifdef PDC_DRV_DEBUG
	printk(KERN_NOTICE "[%s] entry\n", __FUNCTION__);
#endif
	return 0;		/* no characters in buffer, always flushed ! */
}

static void
pdc_flush_buffer(struct tty_struct *tty)
{
#ifdef PDC_DRV_DEBUG
	printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
#endif
}

/*
 * This function is used to send a high-priority XON/XOFF character to
 * the device
 */
static void
pdc_send_xchar(struct tty_struct *tty, char ch)
{
}

/*
 * ------------------------------------------------------------
 * pdc_throttle()
 * 
 * This routine is called by the upper-layer tty layer to signal that
 * incoming characters should be throttled.
 * ------------------------------------------------------------
 */
static void
pdc_throttle(struct tty_struct *tty)
{
}

static void
pdc_unthrottle(struct tty_struct *tty)
{
}

/*
 * ------------------------------------------------------------
 * pdc_ioctl() and friends
 * ------------------------------------------------------------
 */

static void
pdc_break(struct tty_struct *tty, int break_state)
{
}

static int
pdc_ioctl(struct tty_struct *tty, struct file *file,
	  unsigned int cmd, unsigned long arg)
{
	struct async_struct *info = (struct async_struct *) tty->driver_data;

	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
	    (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
		if (tty->flags & (1 << TTY_IO_ERROR))
			return -EIO;
	}

	switch (cmd) {
	case TIOCMGET:
		return 0;
	case TIOCMBIS:
	case TIOCMBIC:
	case TIOCMSET:
		return 0;
	case TIOCGSERIAL:
		return 0;
	case TIOCSSERIAL:
		return 0;
	case TIOCSERCONFIG:
		return 0;

	case TIOCSERGETLSR:	/* Get line status register */
		return 0;

	case TIOCSERGSTRUCT:
		if (copy_to_user((struct async_struct *) arg,
				 info, sizeof (struct async_struct)))
			return -EFAULT;
		return 0;

	case TIOCMIWAIT:
		return 0;

	case TIOCGICOUNT:
		return 0;
	case TIOCSERGWILD:
	case TIOCSERSWILD:
		/* "setserial -W" is called in Debian boot */
		printk("TIOCSER?WILD ioctl obsolete, ignored.\n");
		return 0;

	default:
		return -ENOIOCTLCMD;
	}
	return 0;
}

static void
pdc_set_termios(struct tty_struct *tty, struct termios *old_termios)
{

#if 0				/* XXX CP, has to be checked, if there is stuff to do */
	struct async_struct *info = (struct async_struct *) tty->driver_data;
	unsigned long flags;
	unsigned int cflag = tty->termios->c_cflag;

	if ((cflag == old_termios->c_cflag)
	    && (RELEVANT_IFLAG(tty->termios->c_iflag)
		== RELEVANT_IFLAG(old_termios->c_iflag)))
		return;
#if 0
	change_speed(info, old_termios);
#endif
	/* Handle turning off CRTSCTS */
	if ((old_termios->c_cflag & CRTSCTS) &&
	    !(tty->termios->c_cflag & CRTSCTS)) {
		tty->hw_stopped = 0;
		pdc_start(tty);
	}
#endif
}

/*
 * ------------------------------------------------------------
 * pdc_close()
 * 
 * This routine is called when the serial port gets closed.  First, we
 * wait for the last remaining data to be sent.  Then, we unlink its
 * async structure from the interrupt chain if necessary, and we free
 * that IRQ if nothing is left in the chain.
 * ------------------------------------------------------------
 */
static void
pdc_close(struct tty_struct *tty, struct file *filp)
{
	struct async_struct *info = (struct async_struct *) tty->driver_data;

#ifdef PDC_DEBUG_OPEN
	printk("pdc_close ttyB%d, count = %d\n", info->line, state->count);
#endif
	pdc_drv_refcount--;
	if (pdc_drv_refcount > 0)
		return;

	info->flags |= ASYNC_CLOSING;

	/*
	 * Save the termios structure, since this port may have
	 * separate termios for callout and dialin.
	 */
	if (info->flags & ASYNC_NORMAL_ACTIVE)
		info->state->normal_termios = *tty->termios;
	if (info->flags & ASYNC_CALLOUT_ACTIVE)
		info->state->callout_termios = *tty->termios;

	/*
	 * At this point we stop accepting input.  To do this, we
	 * disable the receive line status interrupts, and tell the
	 * interrupt driver to stop checking the data ready bit in the
	 * line status register.
	 */

	/* XXX CP: make mask for receive !!! */

	if (tty->driver.flush_buffer)
		tty->driver.flush_buffer(tty);
	if (tty->ldisc.flush_buffer)
		tty->ldisc.flush_buffer(tty);
	tty->closing = 0;
	info->event = 0;
	info->tty = 0;
	pdc_drv_info = NULL;
	if (info->blocked_open) {
		if (info->close_delay) {
			set_current_state(TASK_INTERRUPTIBLE);
			schedule_timeout(info->close_delay);
		}
		wake_up_interruptible(&info->open_wait);
	}
	info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE |
			 ASYNC_CLOSING);
	wake_up_interruptible(&info->close_wait);
	MOD_DEC_USE_COUNT;
}

/*
 * pdc_wait_until_sent() --- wait until the transmitter is empty
 */
static void
pdc_wait_until_sent(struct tty_struct *tty, int timeout)
{
	/* we always send immideate */
}

/*
 * pdc_hangup() --- called by tty_hangup() when a hangup is signaled.
 */
static void
pdc_hangup(struct tty_struct *tty)
{
}

/*
 * ------------------------------------------------------------
 * pdc_open() and friends
 * ------------------------------------------------------------
 */

static int
get_async_struct(int line, struct async_struct **ret_info)
{
	struct async_struct *info;

	info = kmalloc(sizeof (struct async_struct), GFP_KERNEL);
	if (!info) {
		return -ENOMEM;
	}
	memset(info, 0, sizeof (struct async_struct));
	init_waitqueue_head(&info->open_wait);
	init_waitqueue_head(&info->close_wait);
	init_waitqueue_head(&info->delta_msr_wait);
	info->magic = SERIAL_MAGIC;
	info->port = 0;
	info->flags = 0;
	info->io_type = 0;
	info->iomem_base = 0;
	info->iomem_reg_shift = 0;
	info->xmit_fifo_size = PDC_DUMMY_BUF;
	info->line = line;
	info->tqueue.routine = NULL;
	info->tqueue.data = info;
	info->state = NULL;
	*ret_info = info;
	return 0;
}

/*
 * This routine is called whenever a serial port is opened.  It
 * enables interrupts for a serial port, linking in its async structure into
 * the IRQ chain.   It also performs the serial-specific
 * initialization for the tty structure.
 */
static int
pdc_open(struct tty_struct *tty, struct file *filp)
{
	struct async_struct *info;
	int retval, line;
	unsigned long page;

	MOD_INC_USE_COUNT;
	line = MINOR(tty->device) - tty->driver.minor_start;
	if ((line < 0) || (line >= NR_PORTS)) {
		MOD_DEC_USE_COUNT;
		return -ENODEV;
	}
	retval = get_async_struct(line, &info);
	if (retval) {
		MOD_DEC_USE_COUNT;
		return retval;
	}
	tty->driver_data = info;
	info->tty = tty;
	pdc_drv_info = info;

#ifdef PDC_DEBUG_OPEN
	printk("pdc_open %s%d, count = %d\n", tty->driver.name, info->line,
	       info->state->count);
#endif
	info->tty->low_latency = 0;
	if (!tmp_buf) {
		page = get_zeroed_page(GFP_KERNEL);
		if (!page) {
			MOD_DEC_USE_COUNT;
			return -ENOMEM;
		}
		if (tmp_buf)
			free_page(page);
		else
			tmp_buf = (unsigned char *) page;
	}

	info->session = current->session;
	info->pgrp = current->pgrp;

#ifdef PDC_DEBUG_OPEN
	printk("pdc_open ttyB%d successful...", info->line);
#endif
	pdc_drv_refcount++;
	return 0;
}

/*
 * ---------------------------------------------------------------------
 * pdc_init() and friends
 *
 * pdc_init() is called at boot-time to initialize the pdc driver.
 * ---------------------------------------------------------------------
 */

static void
show_pdc_drv_version(void)
{
	printk(KERN_INFO "%s version %s%s (%s), %s\n", pdc_drv_name,
	       pdc_drv_version, LOCAL_VERSTRING, pdc_drv_revdate, AUTHOR);
}

/*
 * The serial driver boot-time initialization code!
 */
static int __init
pdc_drv_init(void)
{
	init_timer(&pdc_drv_timer);
	pdc_drv_timer.function = pdc_drv_poll;
	mod_timer(&pdc_drv_timer, jiffies + PDC_POLL_DELAY);

	show_pdc_drv_version();

	/* Initialize the tty_driver structure */

	memset(&pdc_drv_driver, 0, sizeof (struct tty_driver));
	pdc_drv_driver.magic = TTY_DRIVER_MAGIC;
	pdc_drv_driver.driver_name = "pdc_console";
#ifdef CONFIG_DEVFS_FS
	pdc_drv_driver.name = "ttb/%d";
#else
	pdc_drv_driver.name = "ttyB";
#endif
	pdc_drv_driver.major = PDCCONS_MAJOR;
	pdc_drv_driver.minor_start = 0;
	pdc_drv_driver.num = NR_PORTS;
	pdc_drv_driver.type = TTY_DRIVER_TYPE_SERIAL;
	pdc_drv_driver.subtype = SERIAL_TYPE_NORMAL;
	pdc_drv_driver.init_termios = tty_std_termios;
	pdc_drv_driver.init_termios.c_cflag =
	    B9600 | CS8 | CREAD | HUPCL | CLOCAL;
	pdc_drv_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
	pdc_drv_driver.refcount = &pdc_drv_refcount;
	pdc_drv_driver.table = pdc_drv_table;
	pdc_drv_driver.termios = pdc_drv_termios;
	pdc_drv_driver.termios_locked = pdc_drv_termios_locked;

	pdc_drv_driver.open = pdc_open;
	pdc_drv_driver.close = pdc_close;
	pdc_drv_driver.write = pdc_write;
	pdc_drv_driver.put_char = pdc_put_char;
	pdc_drv_driver.flush_chars = pdc_flush_chars;
	pdc_drv_driver.write_room = pdc_write_room;
	pdc_drv_driver.chars_in_buffer = pdc_chars_in_buffer;
	pdc_drv_driver.flush_buffer = pdc_flush_buffer;
	pdc_drv_driver.ioctl = pdc_ioctl;
	pdc_drv_driver.throttle = pdc_throttle;
	pdc_drv_driver.unthrottle = pdc_unthrottle;
	pdc_drv_driver.set_termios = pdc_set_termios;
	pdc_drv_driver.stop = pdc_stop;
	pdc_drv_driver.start = pdc_start;
	pdc_drv_driver.hangup = pdc_hangup;
	pdc_drv_driver.break_ctl = pdc_break;
	pdc_drv_driver.send_xchar = pdc_send_xchar;
	pdc_drv_driver.wait_until_sent = pdc_wait_until_sent;
	pdc_drv_driver.read_proc = NULL;

	if (tty_register_driver(&pdc_drv_driver))
		panic("Couldn't register pdc_console driver\n");

	return 0;
}

static void __exit
pdc_fini(void)
{
	int e1;

	if ((e1 = tty_unregister_driver(&pdc_drv_driver)))
		printk("pdc_console: failed to unregister pdc_drv driver (%d)\n",
		       e1);
}

module_init(pdc_drv_init);
module_exit(pdc_fini);
MODULE_DESCRIPTION("PDC Software Console");
MODULE_AUTHOR(AUTHOR);