File: /usr/src/linux/drivers/char/ser_a2232.c

1     /* drivers/char/ser_a2232.c */
2     
3     /* $Id: ser_a2232.c,v 0.4 2000/01/25 12:00:00 ehaase Exp $ */
4     
5     /* Linux serial driver for the Amiga A2232 board */
6     
7     /* This driver is MAINTAINED. Before applying any changes, please contact
8      * the author.
9      */
10     
11     /* Copyright (c) 2000-2001 Enver Haase    <ehaase@inf.fu-berlin.de>
12      *                   alias The A2232 driver project <A2232@gmx.net>
13      * All rights reserved.
14      *
15      *   This program is free software; you can redistribute it and/or modify
16      *   it under the terms of the GNU General Public License as published by
17      *   the Free Software Foundation; either version 2 of the License, or
18      *   (at your option) any later version.
19      *
20      *   This program is distributed in the hope that it will be useful,
21      *   but WITHOUT ANY WARRANTY; without even the implied warranty of
22      *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23      *   GNU General Public License for more details.
24      *
25      *   You should have received a copy of the GNU General Public License
26      *   along with this program; if not, write to the Free Software
27      *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28      *
29      */
30     /***************************** Documentation ************************/
31     /*
32      * This driver is in EXPERIMENTAL state. That means I could not find
33      * someone with five A2232 boards with 35 ports running at 19200 bps
34      * at the same time and test the machine's behaviour.
35      * However, I know that you can performance-tweak this driver (see
36      * the source code).
37      * One thing to consider is the time this driver consumes during the
38      * Amiga's vertical blank interrupt. Everything that is to be done
39      * _IS DONE_ when entering the vertical blank interrupt handler of
40      * this driver.
41      * However, it would be more sane to only do the job for only ONE card
42      * instead of ALL cards at a time; or, more generally, to handle only
43      * SOME ports instead of ALL ports at a time.
44      * However, as long as no-one runs into problems I guess I shouldn't
45      * change the driver as it runs fine for me :) .
46      *
47      * Version history of this file:
48      * 0.4	Resolved licensing issues.
49      * 0.3	Inclusion in the Linux/m68k tree, small fixes.
50      * 0.2	Added documentation, minor typo fixes.
51      * 0.1	Initial release.
52      *
53      * TO DO:
54      * -	Handle incoming BREAK events. I guess "Stevens: Advanced
55      *	Programming in the UNIX(R) Environment" is a good reference
56      *	on what is to be done.
57      * -	When installing as a module, don't simply 'printk' text, but
58      *	send it to the TTY used by the user.
59      *
60      * THANKS TO:
61      * -	Jukka Marin (65EC02 code).
62      * -	The other NetBSD developers on whose A2232 driver I had a
63      *	pretty close look. However, I didn't copy any code so it
64      *	is okay to put my code under the GPL and include it into
65      *	Linux.
66      */
67     /***************************** End of Documentation *****************/
68     
69     /***************************** Defines ******************************/
70     /*
71      * Enables experimental 115200 (normal) 230400 (turbo) baud rate.
72      * The A2232 specification states it can only operate at speeds up to
73      * 19200 bits per second, and I was not able to send a file via
74      * "sz"/"rz" and a null-modem cable from one A2232 port to another
75      * at 115200 bits per second.
76      * However, this might work for you.
77      */
78     #undef A2232_SPEEDHACK
79     /*
80      * Default is not to use RTS/CTS so you could be talked to death.
81      */
82     #define A2232_SUPPRESS_RTSCTS_WARNING
83     /************************* End of Defines ***************************/
84     
85     /***************************** Includes *****************************/
86     #include <linux/module.h>
87     
88     #include <linux/types.h>
89     #include <linux/sched.h>
90     #include <linux/interrupt.h>
91     #include <linux/kernel.h>
92     #include <linux/errno.h>
93     
94     #include <asm/setup.h>
95     #include <asm/amigaints.h>
96     #include <asm/amigahw.h>
97     #include <linux/zorro.h>
98     #include <asm/irq.h>
99     #include <asm/semaphore.h>
100     
101     #include <linux/delay.h>
102     
103     #include <linux/serial.h>
104     #include <linux/generic_serial.h>
105     
106     #include "ser_a2232.h"
107     #include "ser_a2232fw.h"
108     /************************* End of Includes **************************/
109     
110     /***************************** Prototypes ***************************/
111     /* Helper functions */
112     static __inline__ volatile struct a2232status *a2232stat(unsigned int board,
113     						unsigned int portonboard);
114     static __inline__ volatile struct a2232memory *a2232mem (unsigned int board); 
115     static __inline__ void a2232_receive_char(	struct a2232_port *port,
116     						int ch, int err );
117     /* The interrupt service routine */
118     static void a2232_vbl_inter(int irq, void *data, struct pt_regs *fp);
119     /* Initialize the port structures */
120     static void a2232_init_portstructs(void);
121     /* Initialize and register TTY drivers. */
122     /* returns 0 IFF successful */
123     static int a2232_init_drivers(void); 
124     /* Initialize all A2232 boards; main entry point. */
125     int a2232board_init(void);
126     
127     /* BEGIN GENERIC_SERIAL PROTOTYPES */
128     static void a2232_disable_tx_interrupts(void *ptr);
129     static void a2232_enable_tx_interrupts(void *ptr);
130     static void a2232_disable_rx_interrupts(void *ptr);
131     static void a2232_enable_rx_interrupts(void *ptr);
132     static int  a2232_get_CD(void *ptr);
133     static void a2232_shutdown_port(void *ptr);
134     static int  a2232_set_real_termios(void *ptr);
135     static int  a2232_chars_in_buffer(void *ptr);
136     static void a2232_close(void *ptr);
137     static void a2232_hungup(void *ptr);
138     /* static void a2232_getserial (void *ptr, struct serial_struct *sp); */
139     /* END GENERIC_SERIAL PROTOTYPES */
140     
141     /* Functions that the TTY driver struct expects */
142     static int  a2232_ioctl(struct tty_struct *tty, struct file *file,
143     										unsigned int cmd, unsigned long arg);
144     static void a2232_throttle(struct tty_struct *tty);
145     static void a2232_unthrottle(struct tty_struct *tty);
146     static int  a2232_open(struct tty_struct * tty, struct file * filp);
147     /************************* End of Prototypes ************************/
148     
149     /***************************** Global variables *********************/
150     /*---------------------------------------------------------------------------
151      * Interface from generic_serial.c back here
152      *--------------------------------------------------------------------------*/
153     static struct real_driver a2232_real_driver = {
154             a2232_disable_tx_interrupts,
155             a2232_enable_tx_interrupts,
156             a2232_disable_rx_interrupts,
157             a2232_enable_rx_interrupts,
158             a2232_get_CD,
159             a2232_shutdown_port,
160             a2232_set_real_termios,
161             a2232_chars_in_buffer,
162             a2232_close,
163             a2232_hungup,
164     	NULL	/* a2232_getserial */
165     };
166     
167     static void *a2232_driver_ID = &a2232_driver_ID; // Some memory address WE own.
168     
169     /* Ports structs */
170     static struct a2232_port a2232_ports[MAX_A2232_BOARDS*NUMLINES];
171     
172     /* TTY driver structs */
173     static struct tty_driver a2232_driver;
174     static struct tty_driver a2232_callout_driver;
175     
176     /* Variables used by the TTY driver */
177     static int a2232_refcount;
178     static struct tty_struct *a2232_table[MAX_A2232_BOARDS*NUMLINES] = { NULL, };
179     static struct termios *a2232_termios[MAX_A2232_BOARDS*NUMLINES];
180     static struct termios *a2232_termios_locked[MAX_A2232_BOARDS*NUMLINES];
181     
182     /* nr of cards completely (all ports) and correctly configured */
183     static int nr_a2232; 
184     
185     /* zorro_dev structs for the A2232's */
186     static struct zorro_dev *zd_a2232[MAX_A2232_BOARDS]; 
187     /***************************** End of Global variables **************/
188     
189     /***************************** Functions ****************************/
190     /*** BEGIN OF REAL_DRIVER FUNCTIONS ***/
191     
192     static void a2232_disable_tx_interrupts(void *ptr)
193     {
194     	struct a2232_port *port;
195     	volatile struct a2232status *stat;
196     	unsigned long flags;
197       
198     	port = ptr;
199     	stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
200     	stat->OutDisable = -1;
201     
202     	/* Does this here really have to be? */
203     	save_flags(flags);
204     	cli(); 
205     	port->gs.flags &= ~GS_TX_INTEN;
206     	restore_flags(flags);
207     }
208     
209     static void a2232_enable_tx_interrupts(void *ptr)
210     {
211     	struct a2232_port *port;
212     	volatile struct a2232status *stat;
213     	unsigned long flags;
214     
215     	port = ptr;
216     	stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
217     	stat->OutDisable = 0;
218     
219     	/* Does this here really have to be? */
220     	save_flags(flags);
221     	cli();
222     	port->gs.flags |= GS_TX_INTEN;
223     	restore_flags(flags);
224     }
225     
226     static void a2232_disable_rx_interrupts(void *ptr)
227     {
228     	struct a2232_port *port;
229     	port = ptr;
230     	port->disable_rx = -1;
231     }
232     
233     static void a2232_enable_rx_interrupts(void *ptr)
234     {
235     	struct a2232_port *port;
236     	port = ptr;
237     	port->disable_rx = 0;
238     }
239     
240     static int  a2232_get_CD(void *ptr)
241     {
242     	return ((struct a2232_port *) ptr)->cd_status;
243     }
244     
245     static void a2232_shutdown_port(void *ptr)
246     {
247     	struct a2232_port *port;
248     	volatile struct a2232status *stat;
249     	unsigned long flags;
250     
251     	port = ptr;
252     	stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
253     
254     	save_flags(flags);
255     	cli();
256     
257     	port->gs.flags &= ~GS_ACTIVE;
258     	
259     	if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL) {
260     		/* Set DTR and RTS to Low, flush output.
261     		   The NetBSD driver "msc.c" does it this way. */
262     		stat->Command = (	(stat->Command & ~A2232CMD_CMask) | 
263     					A2232CMD_Close );
264     		stat->OutFlush = -1;
265     		stat->Setup = -1;
266     	}
267     
268     	restore_flags(flags);
269     	
270     	/* After analyzing control flow, I think a2232_shutdown_port
271     		is actually the last call from the system when at application
272     		level someone issues a "echo Hello >>/dev/ttyY0".
273     		Therefore I think the MOD_DEC_USE_COUNT should be here and
274     		not in "a2232_close()". See the comment in "sx.c", too.
275     		If you run into problems, compile this driver into the
276     		kernel instead of compiling it as a module. */
277     	MOD_DEC_USE_COUNT;
278     }
279     
280     static int  a2232_set_real_termios(void *ptr)
281     {
282     	unsigned int cflag, baud, chsize, stopb, parity, softflow;
283     	int rate;
284     	int a2232_param, a2232_cmd;
285     	unsigned long flags;
286     	unsigned int i;
287     	struct a2232_port *port = ptr;
288     	volatile struct a2232status *status;
289     	volatile struct a2232memory *mem;
290     
291     	if (!port->gs.tty || !port->gs.tty->termios) return 0;
292     
293     	status = a2232stat(port->which_a2232, port->which_port_on_a2232);
294     	mem = a2232mem(port->which_a2232);
295     	
296     	a2232_param = a2232_cmd = 0;
297     
298     	// get baud rate
299     	baud = port->gs.baud;
300     	if (baud == 0) {
301     		/* speed == 0 -> drop DTR, do nothing else */
302     		save_flags(flags);
303     		cli();
304     		// Clear DTR (and RTS... mhhh).
305     		status->Command = (	(status->Command & ~A2232CMD_CMask) |
306     					A2232CMD_Close );
307     		status->OutFlush = -1;
308     		status->Setup = -1;
309     		
310     		restore_flags(flags);
311     		return 0;
312     	}
313     	
314     	rate = A2232_BAUD_TABLE_NOAVAIL;
315     	for (i=0; i < A2232_BAUD_TABLE_NUM_RATES * 3; i += 3){
316     		if (a2232_baud_table[i] == baud){
317     			if (mem->Common.Crystal == A2232_TURBO) rate = a2232_baud_table[i+2];
318     			else                                    rate = a2232_baud_table[i+1];
319     		}
320     	}
321     	if (rate == A2232_BAUD_TABLE_NOAVAIL){
322     		printk("a2232: Board %d Port %d unsupported baud rate: %d baud. Using another.\n",port->which_a2232,port->which_port_on_a2232,baud);
323     		// This is useful for both (turbo or normal) Crystal versions.
324     		rate = A2232PARAM_B9600;
325     	}
326     	a2232_param |= rate;
327     
328     	cflag  = port->gs.tty->termios->c_cflag;
329     
330     	// get character size
331     	chsize = cflag & CSIZE;
332     	switch (chsize){
333     		case CS8: 	a2232_param |= A2232PARAM_8Bit; break;
334     		case CS7: 	a2232_param |= A2232PARAM_7Bit; break;
335     		case CS6: 	a2232_param |= A2232PARAM_6Bit; break;
336     		case CS5: 	a2232_param |= A2232PARAM_5Bit; break;
337     		default:	printk("a2232: Board %d Port %d unsupported character size: %d. Using 8 data bits.\n",
338     					port->which_a2232,port->which_port_on_a2232,chsize);
339     				a2232_param |= A2232PARAM_8Bit; break;
340     	}
341     
342     	// get number of stop bits
343     	stopb  = cflag & CSTOPB;
344     	if (stopb){ // two stop bits instead of one
345     		printk("a2232: Board %d Port %d 2 stop bits unsupported. Using 1 stop bit.\n",
346     			port->which_a2232,port->which_port_on_a2232);
347     	}
348     
349     	// Warn if RTS/CTS not wanted
350     	if (!(cflag & CRTSCTS)){
351     #ifndef A2232_SUPPRESS_RTSCTS_WARNING
352     		printk("a2232: Board %d Port %d cannot switch off firmware-implemented RTS/CTS hardware flow control.\n",
353     			port->which_a2232,port->which_port_on_a2232);
354     #endif
355     	}
356     
357     	/*	I think this is correct.
358     		However, IXOFF means _input_ flow control and I wonder
359     		if one should care about IXON _output_ flow control,
360     		too. If this makes problems, one should turn the A2232
361     		firmware XON/XOFF "SoftFlow" flow control off and use
362     		the conventional way of inserting START/STOP characters
363     		by hand in throttle()/unthrottle().
364     	*/
365     	softflow = !!( port->gs.tty->termios->c_iflag & IXOFF );
366     
367     	// get Parity (Enabled/Disabled? If Enabled, Odd or Even?)
368     	parity = cflag & (PARENB | PARODD);
369     	if (parity & PARENB){
370     		if (parity & PARODD){
371     			a2232_cmd |= A2232CMD_OddParity;
372     		}
373     		else{
374     			a2232_cmd |= A2232CMD_EvenParity;
375     		}
376     	}
377     	else a2232_cmd |= A2232CMD_NoParity;
378     
379     
380     	/*	Hmm. Maybe an own a2232_port structure
381     		member would be cleaner?	*/
382     	if (cflag & CLOCAL)
383     		port->gs.flags &= ~ASYNC_CHECK_CD;
384     	else
385     		port->gs.flags |= ASYNC_CHECK_CD;
386     
387     
388     	/* Now we have all parameters and can go to set them: */
389     	save_flags(flags);
390     	cli();
391     
392     	status->Param = a2232_param | A2232PARAM_RcvBaud;
393     	status->Command = a2232_cmd | A2232CMD_Open |  A2232CMD_Enable;
394     	status->SoftFlow = softflow;
395     	status->OutDisable = 0;
396     	status->Setup = -1;
397     
398     	restore_flags(flags);
399     	return 0;
400     }
401     
402     static int  a2232_chars_in_buffer(void *ptr)
403     {
404     	struct a2232_port *port;
405     	volatile struct a2232status *status; 
406     	unsigned char ret; /* we need modulo-256 arithmetics */
407     	port = ptr;
408     	status = a2232stat(port->which_a2232, port->which_port_on_a2232);
409     #if A2232_IOBUFLEN != 256
410     #error "Re-Implement a2232_chars_in_buffer()!"
411     #endif
412     	ret = (status->OutHead - status->OutTail);
413     	return ret;
414     }
415     
416     static void a2232_close(void *ptr)
417     {
418     	a2232_disable_tx_interrupts(ptr);
419     	a2232_disable_rx_interrupts(ptr);
420     	/* see the comment in a2232_shutdown_port above. */
421     	/* MOD_DEC_USE_COUNT; */
422     }
423     
424     static void a2232_hungup(void *ptr)
425     {
426     	a2232_close(ptr);
427     }
428     /*** END   OF REAL_DRIVER FUNCTIONS ***/
429     
430     /*** BEGIN  FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
431     static int a2232_ioctl(	struct tty_struct *tty, struct file *file,
432     			unsigned int cmd, unsigned long arg)
433     {
434     	return -ENOIOCTLCMD;
435     }
436     
437     static void a2232_throttle(struct tty_struct *tty)
438     {
439     /* Throttle: System cannot take another chars: Drop RTS or
440                  send the STOP char or whatever.
441        The A2232 firmware does RTS/CTS anyway, and XON/XOFF
442        if switched on. So the only thing we can do at this
443        layer here is not taking any characters out of the
444        A2232 buffer any more. */
445     	struct a2232_port *port = (struct a2232_port *) tty->driver_data;
446     	port->throttle_input = -1;
447     }
448     
449     static void a2232_unthrottle(struct tty_struct *tty)
450     {
451     /* Unthrottle: dual to "throttle()" above. */
452     	struct a2232_port *port = (struct a2232_port *) tty->driver_data;
453     	port->throttle_input = 0;
454     }
455     
456     static int  a2232_open(struct tty_struct * tty, struct file * filp)
457     {
458     /* More or less stolen from other drivers. */
459     	int line;
460     	int retval;
461     	struct a2232_port *port;
462     
463     	line = MINOR(tty->device);
464     	port = &a2232_ports[line];
465     	
466     	tty->driver_data = port;
467     	port->gs.tty = tty;
468     	port->gs.count++;
469     	retval = gs_init_port(&port->gs);
470     	if (retval) {
471     		port->gs.count--;
472     		return retval;
473     	}
474     	port->gs.flags |= GS_ACTIVE;
475     	if (port->gs.count == 1) {
476     		MOD_INC_USE_COUNT;
477     	}
478     	retval = gs_block_til_ready(port, filp);
479     
480     	if (retval) {
481     		MOD_DEC_USE_COUNT;
482     		port->gs.count--;
483     		return retval;
484     	}
485     
486     	if ((port->gs.count == 1) && (port->gs.flags & ASYNC_SPLIT_TERMIOS)){
487     		if (tty->driver.subtype == A2232_TTY_SUBTYPE_NORMAL)
488     			*tty->termios = port->gs.normal_termios;
489     		else 
490     			*tty->termios = port->gs.callout_termios;
491     		a2232_set_real_termios (port);
492     	}
493     
494     	port->gs.session = current->session;
495     	port->gs.pgrp = current->pgrp;
496     
497     	a2232_enable_rx_interrupts(port);
498     	
499     	return 0;
500     }
501     /*** END OF FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
502     
503     static __inline__ volatile struct a2232status *a2232stat(unsigned int board, unsigned int portonboard)
504     {
505     	volatile struct a2232memory *mem = a2232mem(board);
506     	return &(mem->Status[portonboard]);
507     }
508     
509     static __inline__ volatile struct a2232memory *a2232mem (unsigned int board)
510     {
511     	return (volatile struct a2232memory *) ZTWO_VADDR( zd_a2232[board]->resource.start );
512     }
513     
514     static __inline__ void a2232_receive_char(	struct a2232_port *port,
515     						int ch, int err )
516     {
517     /* 	Mostly stolen from other drivers.
518     	Maybe one could implement a more efficient version by not only
519     	transferring one character at a time.
520     */
521     	struct tty_struct *tty = port->gs.tty;
522     	
523     	if (tty->flip.count >= TTY_FLIPBUF_SIZE)
524     		return;
525     
526     	tty->flip.count++;
527     
528     #if 0
529     	switch(err) {
530     	case TTY_BREAK:
531     		break;
532     	case TTY_PARITY:
533     		break;
534     	case TTY_OVERRUN:
535     		break;
536     	case TTY_FRAME:
537     		break;
538     	}
539     #endif
540     
541     	*tty->flip.flag_buf_ptr++ = err;
542     	*tty->flip.char_buf_ptr++ = ch;
543     	tty_flip_buffer_push(tty);
544     }
545     
546     static void a2232_vbl_inter(int irq, void *data, struct pt_regs *fp)
547     {
548     #if A2232_IOBUFLEN != 256
549     #error "Re-Implement a2232_vbl_inter()!"
550     #endif
551     
552     struct a2232_port *port;
553     volatile struct a2232memory *mem;
554     volatile struct a2232status *status;
555     unsigned char newhead;
556     unsigned char bufpos; /* Must be unsigned char. We need the modulo-256 arithmetics */
557     unsigned char ncd, ocd, ccd; /* names consistent with the NetBSD driver */
558     volatile u_char *ibuf, *cbuf, *obuf;
559     int ch, err, n, p;
560     	for (n = 0; n < nr_a2232; n++){		/* for every completely initialized A2232 board */
561     		mem = a2232mem(n);
562     		for (p = 0; p < NUMLINES; p++){	/* for every port on this board */
563     			err = 0;
564     			port = &a2232_ports[n*NUMLINES+p];
565     			if ( port->gs.flags & GS_ACTIVE ){ /* if the port is used */
566     
567     				status = a2232stat(n,p);
568     
569     				if (!port->disable_rx && !port->throttle_input){ /* If input is not disabled */
570     					newhead = status->InHead;               /* 65EC02 write pointer */
571     					bufpos = status->InTail;
572     
573     					/* check for input for this port */
574     					if (newhead != bufpos) {
575     						/* buffer for input chars/events */
576     						ibuf = mem->InBuf[p];
577      
578     						/* data types of bytes in ibuf */
579     						cbuf = mem->InCtl[p];
580      
581     						/* do for all chars */
582     						while (bufpos != newhead) {
583     							/* which type of input data? */
584     							switch (cbuf[bufpos]) {
585     								/* switch on input event (CD, BREAK, etc.) */
586     							case A2232INCTL_EVENT:
587     								switch (ibuf[bufpos++]) {
588     								case A2232EVENT_Break:
589     									/* TODO: Handle BREAK signal */
590     									break;
591     									/*	A2232EVENT_CarrierOn and A2232EVENT_CarrierOff are
592     										handled in a separate queue and should not occur here. */
593     								case A2232EVENT_Sync:
594     									printk("A2232: 65EC02 software sent SYNC event, don't know what to do. Ignoring.");
595     									break;
596     								default:
597     									printk("A2232: 65EC02 software broken, unknown event type %d occured.\n",ibuf[bufpos-1]);
598     								} /* event type switch */
599     								break;
600      							case A2232INCTL_CHAR:
601     								/* Receive incoming char */
602     								a2232_receive_char(port, ibuf[bufpos], err);
603     								bufpos++;
604     								break;
605      							default:
606     								printk("A2232: 65EC02 software broken, unknown data type %d occured.\n",cbuf[bufpos]);
607     								bufpos++;
608     							} /* switch on input data type */
609     						} /* while there's something in the buffer */
610     
611     						status->InTail = bufpos;            /* tell 65EC02 what we've read */
612     						
613     					} /* if there was something in the buffer */                          
614     				} /* If input is not disabled */
615     
616     				/* Now check if there's something to output */
617     				obuf = mem->OutBuf[p];
618     				bufpos = status->OutHead;
619     				while ( (port->gs.xmit_cnt > 0)		&&
620     					(!port->gs.tty->stopped)	&&
621     					(!port->gs.tty->hw_stopped) ){	/* While there are chars to transmit */
622     					if (((bufpos+1) & A2232_IOBUFLENMASK) != status->OutTail) { /* If the A2232 buffer is not full */
623     						ch = port->gs.xmit_buf[port->gs.xmit_tail];					/* get the next char to transmit */
624     						port->gs.xmit_tail = (port->gs.xmit_tail+1) & (SERIAL_XMIT_SIZE-1); /* modulo-addition for the gs.xmit_buf ring-buffer */
625     						obuf[bufpos++] = ch;																/* put it into the A2232 buffer */
626     						port->gs.xmit_cnt--;
627     					}
628     					else{																									/* If A2232 the buffer is full */
629     						break;																							/* simply stop filling it. */
630     					}													
631     				}					
632     				status->OutHead = bufpos;
633     					
634     				/* WakeUp if output buffer runs low */
635     				if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.tty) {
636     					if ((port->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && port->gs.tty->ldisc.write_wakeup){
637     						(port->gs.tty->ldisc.write_wakeup)(port->gs.tty);
638     					}
639     					wake_up_interruptible(&port->gs.tty->write_wait);
640     				}
641     			} // if the port is used
642     		} // for every port on the board
643     			
644     		/* Now check the CD message queue */
645     		newhead = mem->Common.CDHead;
646     		bufpos = mem->Common.CDTail;
647     		if (newhead != bufpos){				/* There are CD events in queue */
648     			ocd = mem->Common.CDStatus; 		/* get old status bits */
649     			while (newhead != bufpos){		/* read all events */
650     				ncd = mem->CDBuf[bufpos++]; 	/* get one event */
651     				ccd = ncd ^ ocd; 		/* mask of changed lines */
652     				ocd = ncd; 			/* save new status bits */
653     				for(p=0; p < NUMLINES; p++){	/* for all ports */
654     					if (ccd & 1){		/* this one changed */
655     
656     						struct a2232_port *port = &a2232_ports[n*7+p];
657     						port->cd_status = !(ncd & 1); /* ncd&1 <=> CD is now off */
658     
659     						if (!(port->gs.flags & ASYNC_CHECK_CD))
660     							;	/* Don't report DCD changes */
661     						else if (port->cd_status) { // if DCD on: DCD went UP!
662     							if (~(port->gs.flags & ASYNC_NORMAL_ACTIVE) ||
663     							    ~(port->gs.flags & ASYNC_CALLOUT_ACTIVE)) {
664     								/* Are we blocking in open?*/
665     								wake_up_interruptible(&port->gs.open_wait);
666     							}
667     						}
668     						else { // if DCD off: DCD went DOWN!
669     							if (!((port->gs.flags & ASYNC_CALLOUT_ACTIVE) &&
670     							      (port->gs.flags & ASYNC_CALLOUT_NOHUP))) {
671     								if (port->gs.tty)
672     									tty_hangup (port->gs.tty);
673     							}
674     						}
675     						
676     					} // if CD changed for this port
677     					ccd >>= 1;
678     					ncd >>= 1;									/* Shift bits for next line */
679     				} // for every port
680     			} // while CD events in queue
681     			mem->Common.CDStatus = ocd; /* save new status */
682     			mem->Common.CDTail = bufpos; /* remove events */
683     		} // if events in CD queue
684     		
685     	} // for every completely initialized A2232 board
686     }
687     
688     static void a2232_init_portstructs(void)
689     {
690     	struct a2232_port *port;
691     	int i;
692     
693     	for (i = 0; i < MAX_A2232_BOARDS*NUMLINES; i++) {
694     		port = a2232_ports + i;
695     		port->which_a2232 = i/NUMLINES;
696     		port->which_port_on_a2232 = i%NUMLINES;
697     		port->disable_rx = port->throttle_input = port->cd_status = 0;
698     		port->gs.callout_termios = tty_std_termios;
699     		port->gs.normal_termios = tty_std_termios;
700     		port->gs.magic = A2232_MAGIC;
701     		port->gs.close_delay = HZ/2;
702     		port->gs.closing_wait = 30 * HZ;
703     		port->gs.rd = &a2232_real_driver;
704     #ifdef NEW_WRITE_LOCKING
705     		init_MUTEX(&(port->gs.port_write_sem));
706     #endif
707     		init_waitqueue_head(&port->gs.open_wait);
708     		init_waitqueue_head(&port->gs.close_wait);
709     	}
710     }
711     
712     static int a2232_init_drivers(void)
713     {
714     	int error;
715     
716     	memset(&a2232_driver, 0, sizeof(a2232_driver));
717     	a2232_driver.magic = TTY_DRIVER_MAGIC;
718     	a2232_driver.driver_name = "commodore_a2232";
719     	a2232_driver.name = "ttyY";
720     	a2232_driver.major = A2232_NORMAL_MAJOR;
721     	a2232_driver.num = NUMLINES * nr_a2232;
722     	a2232_driver.type = TTY_DRIVER_TYPE_SERIAL;
723     	a2232_driver.subtype = A2232_TTY_SUBTYPE_NORMAL;
724     	a2232_driver.init_termios = tty_std_termios;
725     	a2232_driver.init_termios.c_cflag =
726     		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
727     	a2232_driver.flags = TTY_DRIVER_REAL_RAW;
728     	a2232_driver.refcount = &a2232_refcount;
729     	a2232_driver.table = a2232_table;
730     	a2232_driver.termios = a2232_termios;
731     	a2232_driver.termios_locked = a2232_termios_locked;
732     
733     	a2232_driver.open = a2232_open;
734     	a2232_driver.close = gs_close;
735     	a2232_driver.write = gs_write;
736     	a2232_driver.put_char = gs_put_char;
737     	a2232_driver.flush_chars = gs_flush_chars;
738     	a2232_driver.write_room = gs_write_room;
739     	a2232_driver.chars_in_buffer = gs_chars_in_buffer;
740     	a2232_driver.flush_buffer = gs_flush_buffer;
741     	a2232_driver.ioctl = a2232_ioctl;
742     	a2232_driver.throttle = a2232_throttle;
743     	a2232_driver.unthrottle = a2232_unthrottle;
744     	a2232_driver.set_termios = gs_set_termios;
745     	a2232_driver.stop = gs_stop;
746     	a2232_driver.start = gs_start;
747     	a2232_driver.hangup = gs_hangup;
748     
749     	a2232_callout_driver = a2232_driver;
750     	a2232_callout_driver.name = "cuy";
751     	a2232_callout_driver.major = A2232_CALLOUT_MAJOR;
752     	a2232_callout_driver.subtype = A2232_TTY_SUBTYPE_CALLOUT;
753     
754     	if ((error = tty_register_driver(&a2232_driver))) {
755     		printk(KERN_ERR "A2232: Couldn't register A2232 driver, error = %d\n",
756     		       error);
757     		return 1;
758     	}
759     	if ((error = tty_register_driver(&a2232_callout_driver))) {
760     		tty_unregister_driver(&a2232_driver);
761     		printk(KERN_ERR "A2232: Couldn't register A2232 callout driver, error = %d\n",
762     		       error);
763     		return 1;
764     	}
765     	return 0;
766     }
767     
768     int a2232board_init(void)
769     {
770     	struct zorro_dev *z;
771     
772     	unsigned int boardaddr;
773     	int bcount;
774     	short start;
775     	u_char *from;
776     	volatile u_char *to;
777     	volatile struct a2232memory *mem;
778     
779     #ifdef __SMP__
780     	return -ENODEV;	/* This driver is not SMP aware. Is there an SMP ZorroII-bus-machine? */
781     #endif
782     
783     	if (!MACH_IS_AMIGA){
784     		return -ENODEV;
785     	}
786     
787     	printk("Commodore A2232 driver initializing.\n"); /* Say that we're alive. */
788     
789     	z = NULL;
790     	nr_a2232 = 0;
791     	while ( (z = zorro_find_device(ZORRO_WILDCARD, z)) ){
792     		if (	(z->id != ZORRO_PROD_CBM_A2232_PROTOTYPE) && 
793     			(z->id != ZORRO_PROD_CBM_A2232)	){
794     			continue;	// The board found was no A2232
795     		}
796     		if (!zorro_request_device(z,"A2232 driver"))
797     			continue;
798     
799     		printk("Commodore A2232 found (#%d).\n",nr_a2232);
800     
801     		zd_a2232[nr_a2232] = z;
802     
803     		boardaddr = ZTWO_VADDR( z->resource.start );
804     		printk("Board is located at address 0x%x, size is 0x%x.\n", boardaddr, (unsigned int) ((z->resource.end+1) - (z->resource.start)));
805     
806     		mem = (volatile struct a2232memory *) boardaddr;
807     
808     		(void) mem->Enable6502Reset;   /* copy the code across to the board */
809     		to = (u_char *)mem;  from = a2232_65EC02code; bcount = sizeof(a2232_65EC02code) - 2;
810     		start = *(short *)from;
811     		from += sizeof(start);
812     		to += start;
813     		while(bcount--) *to++ = *from++;
814     		printk("65EC02 software uploaded to the A2232 memory.\n");
815       
816     		mem->Common.Crystal = A2232_UNKNOWN;  /* use automatic speed check */
817       
818     		/* start 6502 running */
819     		(void) mem->ResetBoard;
820     		printk("A2232's 65EC02 CPU up and running.\n");
821       
822     		/* wait until speed detector has finished */
823     		for (bcount = 0; bcount < 2000; bcount++) {
824     			udelay(1000);
825     			if (mem->Common.Crystal)
826     				break;
827     		}
828     		printk((mem->Common.Crystal?"A2232 oscillator crystal detected by 65EC02 software: ":"65EC02 software could not determine A2232 oscillator crystal: "));
829     		switch (mem->Common.Crystal){
830     		case A2232_UNKNOWN:
831     			printk("Unknown crystal.\n");
832     			break;
833      		case A2232_NORMAL:
834     			printk ("Normal crystal.\n");
835     			break;
836     		case A2232_TURBO:
837     			printk ("Turbo crystal.\n");
838     			break;
839     		default:
840     			printk ("0x%x. Huh?\n",mem->Common.Crystal);
841     		}
842     
843     		nr_a2232++;
844     
845     	}	
846     
847     	printk("Total: %d A2232 boards initialized.\n.", nr_a2232); /* Some status report if no card was found */
848     
849     	a2232_init_portstructs();
850     
851     	/*
852     		a2232_init_drivers also registers the drivers. Must be here because all boards
853     		have to be detected first.
854     	*/
855     	if (a2232_init_drivers()) return -ENODEV; // maybe we should use a different -Exxx?
856     
857     	request_irq(IRQ_AMIGA_VERTB, a2232_vbl_inter, 0, "A2232 serial VBL", a2232_driver_ID);
858     	return 0;
859     }
860     
861     #ifdef MODULE
862     int init_module(void)
863     {
864     	return a2232board_init();
865     }
866     
867     void cleanup_module(void)
868     {
869     	int i;
870     
871     	for (i = 0; i < nr_a2232; i++) {
872     		zorro_release_device(zd_a2232[i]);
873     	}
874     
875     	tty_unregister_driver(&a2232_driver);
876     	tty_unregister_driver(&a2232_callout_driver);
877     	free_irq(IRQ_AMIGA_VERTB, a2232_driver_ID);
878     }
879     #endif
880     /***************************** End of Functions *********************/
881     
882     MODULE_AUTHOR("Enver Haase");
883     MODULE_DESCRIPTION("Amiga A2232 multi-serial board driver");
884     MODULE_LICENSE("GPL");
885