1
2
3
4
5
6
7
8
9
10static char *pdc_drv_version = "0.3";
11static 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
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
64extern int pdc_console_poll_key(void *);
65extern void pdc_outc(unsigned char);
66
67static char *pdc_drv_name = "PDC Software Console";
68
69static struct tty_driver pdc_drv_driver;
70static int pdc_drv_refcount = 0;
71static struct async_struct *pdc_drv_info;
72
73static struct timer_list pdc_drv_timer;
74
75
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
84static struct tty_struct *pdc_drv_table[NR_PORTS];
85static struct termios *pdc_drv_termios[NR_PORTS];
86static struct termios *pdc_drv_termios_locked[NR_PORTS];
87
88
89
90
91
92
93
94
95
96
97static unsigned char *tmp_buf;
98#ifdef DECLARE_MUTEX
99static DECLARE_MUTEX(tmp_buf_sem);
100#else
101static struct semaphore tmp_buf_sem = MUTEX;
102#endif
103
104
105
106
107
108
109
110
111
112static void
113pdc_stop(struct tty_struct *tty)
114{
115}
116
117static void
118pdc_start(struct tty_struct *tty)
119{
120}
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143static void
144receive_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)
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
172static void
173pdc_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
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
190static void
191pdc_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
199static void
200pdc_flush_chars(struct tty_struct *tty)
201{
202
203
204#ifdef PDC_DRV_DEBUG
205 printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
206#endif
207
208
209}
210
211static int
212pdc_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
250static int
251pdc_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
259static int
260pdc_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;
266}
267
268static void
269pdc_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
278
279
280static void
281pdc_send_xchar(struct tty_struct *tty, char ch)
282{
283}
284
285
286
287
288
289
290
291
292
293static void
294pdc_throttle(struct tty_struct *tty)
295{
296}
297
298static void
299pdc_unthrottle(struct tty_struct *tty)
300{
301}
302
303
304
305
306
307
308
309static void
310pdc_break(struct tty_struct *tty, int break_state)
311{
312}
313
314static int
315get_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
330static 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
337static 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
344static int
345pdc_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:
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
388 printk("TIOCSER?WILD ioctl obsolete, ignored.\n");
389 return 0;
390
391 default:
392 return -ENOIOCTLCMD;
393 }
394 return 0;
395}
396
397static void
398pdc_set_termios(struct tty_struct *tty, struct termios *old_termios)
399{
400
401#if 0
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
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
425
426
427
428
429
430
431
432static void
433pdc_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
448
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
457
458
459
460
461
462
463
464 if (tty->driver.flush_buffer)
465 tty->driver.flush_buffer(tty);
466 tty_ldisc_flush(tty);
467 tty->closing = 0;
468 info->event = 0;
469 info->tty = 0;
470 pdc_drv_info = NULL;
471 if (info->blocked_open) {
472 if (info->close_delay) {
473 set_current_state(TASK_INTERRUPTIBLE);
474 schedule_timeout(info->close_delay);
475 }
476 wake_up_interruptible(&info->open_wait);
477 }
478 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE |
479 ASYNC_CLOSING);
480 wake_up_interruptible(&info->close_wait);
481 MOD_DEC_USE_COUNT;
482}
483
484
485
486
487static void
488pdc_wait_until_sent(struct tty_struct *tty, int timeout)
489{
490
491}
492
493
494
495
496static void
497pdc_hangup(struct tty_struct *tty)
498{
499}
500
501
502
503
504
505
506
507static int
508get_async_struct(int line, struct async_struct **ret_info)
509{
510 struct async_struct *info;
511
512 info = kmalloc(sizeof (struct async_struct), GFP_KERNEL);
513 if (!info) {
514 return -ENOMEM;
515 }
516 memset(info, 0, sizeof (struct async_struct));
517 init_waitqueue_head(&info->open_wait);
518 init_waitqueue_head(&info->close_wait);
519 init_waitqueue_head(&info->delta_msr_wait);
520 info->magic = SERIAL_MAGIC;
521 info->port = 0;
522 info->flags = 0;
523 info->io_type = 0;
524 info->iomem_base = 0;
525 info->iomem_reg_shift = 0;
526 info->xmit_fifo_size = PDC_DUMMY_BUF;
527 info->line = line;
528 info->tqueue.routine = NULL;
529 info->tqueue.data = info;
530 info->state = NULL;
531 *ret_info = info;
532 return 0;
533}
534
535
536
537
538
539
540
541static int
542pdc_open(struct tty_struct *tty, struct file *filp)
543{
544 struct async_struct *info;
545 int retval, line;
546 unsigned long page;
547
548 MOD_INC_USE_COUNT;
549 line = MINOR(tty->device) - tty->driver.minor_start;
550 if ((line < 0) || (line >= NR_PORTS)) {
551 MOD_DEC_USE_COUNT;
552 return -ENODEV;
553 }
554 retval = get_async_struct(line, &info);
555 if (retval) {
556 MOD_DEC_USE_COUNT;
557 return retval;
558 }
559 tty->driver_data = info;
560 info->tty = tty;
561 pdc_drv_info = info;
562
563#ifdef PDC_DEBUG_OPEN
564 printk("pdc_open %s%d, count = %d\n", tty->driver.name, info->line,
565 info->state->count);
566#endif
567 info->tty->low_latency = 0;
568 if (!tmp_buf) {
569 page = get_zeroed_page(GFP_KERNEL);
570 if (!page) {
571 MOD_DEC_USE_COUNT;
572 return -ENOMEM;
573 }
574 if (tmp_buf)
575 free_page(page);
576 else
577 tmp_buf = (unsigned char *) page;
578 }
579
580 info->session = current->session;
581 info->pgrp = current->pgrp;
582
583#ifdef PDC_DEBUG_OPEN
584 printk("pdc_open ttyB%d successful...", info->line);
585#endif
586 pdc_drv_refcount++;
587 return 0;
588}
589
590
591
592
593
594
595
596
597
598static void
599show_pdc_drv_version(void)
600{
601 printk(KERN_INFO "%s version %s%s (%s), %s\n", pdc_drv_name,
602 pdc_drv_version, LOCAL_VERSTRING, pdc_drv_revdate, AUTHOR);
603}
604
605
606
607
608static int __init
609pdc_drv_init(void)
610{
611 init_timer(&pdc_drv_timer);
612 pdc_drv_timer.function = pdc_drv_poll;
613 mod_timer(&pdc_drv_timer, jiffies + PDC_POLL_DELAY);
614
615 show_pdc_drv_version();
616
617
618
619 memset(&pdc_drv_driver, 0, sizeof (struct tty_driver));
620 pdc_drv_driver.magic = TTY_DRIVER_MAGIC;
621 pdc_drv_driver.driver_name = "pdc_console";
622#ifdef CONFIG_DEVFS_FS
623 pdc_drv_driver.name = "ttb/%d";
624#else
625 pdc_drv_driver.name = "ttyB";
626#endif
627 pdc_drv_driver.major = MUX_MAJOR;
628 pdc_drv_driver.minor_start = 0;
629 pdc_drv_driver.num = NR_PORTS;
630 pdc_drv_driver.type = TTY_DRIVER_TYPE_SERIAL;
631 pdc_drv_driver.subtype = SERIAL_TYPE_NORMAL;
632 pdc_drv_driver.init_termios = tty_std_termios;
633 pdc_drv_driver.init_termios.c_cflag =
634 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
635 pdc_drv_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
636 pdc_drv_driver.refcount = &pdc_drv_refcount;
637 pdc_drv_driver.table = pdc_drv_table;
638 pdc_drv_driver.termios = pdc_drv_termios;
639 pdc_drv_driver.termios_locked = pdc_drv_termios_locked;
640
641 pdc_drv_driver.open = pdc_open;
642 pdc_drv_driver.close = pdc_close;
643 pdc_drv_driver.write = pdc_write;
644 pdc_drv_driver.put_char = pdc_put_char;
645 pdc_drv_driver.flush_chars = pdc_flush_chars;
646 pdc_drv_driver.write_room = pdc_write_room;
647 pdc_drv_driver.chars_in_buffer = pdc_chars_in_buffer;
648 pdc_drv_driver.flush_buffer = pdc_flush_buffer;
649 pdc_drv_driver.ioctl = pdc_ioctl;
650 pdc_drv_driver.throttle = pdc_throttle;
651 pdc_drv_driver.unthrottle = pdc_unthrottle;
652 pdc_drv_driver.set_termios = pdc_set_termios;
653 pdc_drv_driver.stop = pdc_stop;
654 pdc_drv_driver.start = pdc_start;
655 pdc_drv_driver.hangup = pdc_hangup;
656 pdc_drv_driver.break_ctl = pdc_break;
657 pdc_drv_driver.send_xchar = pdc_send_xchar;
658 pdc_drv_driver.wait_until_sent = pdc_wait_until_sent;
659 pdc_drv_driver.read_proc = NULL;
660
661 if (tty_register_driver(&pdc_drv_driver))
662 panic("Couldn't register pdc_console driver\n");
663
664 return 0;
665}
666
667static void __exit
668pdc_fini(void)
669{
670 int e1;
671
672 if ((e1 = tty_unregister_driver(&pdc_drv_driver)))
673 printk("pdc_console: failed to unregister pdc_drv driver (%d)\n",
674 e1);
675}
676
677module_init(pdc_drv_init);
678module_exit(pdc_fini);
679MODULE_DESCRIPTION("PDC Software Console");
680MODULE_AUTHOR(AUTHOR);
681