1/* drivers/char/ser_a2232.c */
2
3/* $Id: ser_a2232.c,v 1.1.1.1 2007/08/03 18:52:28 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/interrupt.h>
90#include <linux/kernel.h>
91#include <linux/errno.h>
92#include <linux/tty.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 <linux/mutex.h>
100
101#include <linux/delay.h>
102
103#include <linux/serial.h>
104#include <linux/generic_serial.h>
105#include <linux/tty_flip.h>
106
107#include "ser_a2232.h"
108#include "ser_a2232fw.h"
109/************************* End of Includes **************************/
110
111/***************************** Prototypes ***************************/
112/* The interrupt service routine */
113static irqreturn_t a2232_vbl_inter(int irq, void *data);
114/* Initialize the port structures */
115static void a2232_init_portstructs(void);
116/* Initialize and register TTY drivers. */
117/* returns 0 IFF successful */
118static int a2232_init_drivers(void);
119
120/* BEGIN GENERIC_SERIAL PROTOTYPES */
121static void a2232_disable_tx_interrupts(void *ptr);
122static void a2232_enable_tx_interrupts(void *ptr);
123static void a2232_disable_rx_interrupts(void *ptr);
124static void a2232_enable_rx_interrupts(void *ptr);
125static int  a2232_get_CD(void *ptr);
126static void a2232_shutdown_port(void *ptr);
127static int  a2232_set_real_termios(void *ptr);
128static int  a2232_chars_in_buffer(void *ptr);
129static void a2232_close(void *ptr);
130static void a2232_hungup(void *ptr);
131/* static void a2232_getserial (void *ptr, struct serial_struct *sp); */
132/* END GENERIC_SERIAL PROTOTYPES */
133
134/* Functions that the TTY driver struct expects */
135static int  a2232_ioctl(struct tty_struct *tty, struct file *file,
136										unsigned int cmd, unsigned long arg);
137static void a2232_throttle(struct tty_struct *tty);
138static void a2232_unthrottle(struct tty_struct *tty);
139static int  a2232_open(struct tty_struct * tty, struct file * filp);
140/************************* End of Prototypes ************************/
141
142/***************************** Global variables *********************/
143/*---------------------------------------------------------------------------
144 * Interface from generic_serial.c back here
145 *--------------------------------------------------------------------------*/
146static struct real_driver a2232_real_driver = {
147        a2232_disable_tx_interrupts,
148        a2232_enable_tx_interrupts,
149        a2232_disable_rx_interrupts,
150        a2232_enable_rx_interrupts,
151        a2232_get_CD,
152        a2232_shutdown_port,
153        a2232_set_real_termios,
154        a2232_chars_in_buffer,
155        a2232_close,
156        a2232_hungup,
157	NULL	/* a2232_getserial */
158};
159
160static void *a2232_driver_ID = &a2232_driver_ID; // Some memory address WE own.
161
162/* Ports structs */
163static struct a2232_port a2232_ports[MAX_A2232_BOARDS*NUMLINES];
164
165/* TTY driver structs */
166static struct tty_driver *a2232_driver;
167
168/* nr of cards completely (all ports) and correctly configured */
169static int nr_a2232;
170
171/* zorro_dev structs for the A2232's */
172static struct zorro_dev *zd_a2232[MAX_A2232_BOARDS];
173/***************************** End of Global variables **************/
174
175/* Helper functions */
176
177static inline volatile struct a2232memory *a2232mem(unsigned int board)
178{
179	return (volatile struct a2232memory *)ZTWO_VADDR(zd_a2232[board]->resource.start);
180}
181
182static inline volatile struct a2232status *a2232stat(unsigned int board,
183						     unsigned int portonboard)
184{
185	volatile struct a2232memory *mem = a2232mem(board);
186	return &(mem->Status[portonboard]);
187}
188
189static inline void a2232_receive_char(struct a2232_port *port, int ch, int err)
190{
191/* 	Mostly stolen from other drivers.
192	Maybe one could implement a more efficient version by not only
193	transferring one character at a time.
194*/
195	struct tty_struct *tty = port->gs.tty;
196
197
198	tty_insert_flip_char(tty, ch, err);
199	tty_flip_buffer_push(tty);
200}
201
202/***************************** Functions ****************************/
203/*** BEGIN OF REAL_DRIVER FUNCTIONS ***/
204
205static void a2232_disable_tx_interrupts(void *ptr)
206{
207	struct a2232_port *port;
208	volatile struct a2232status *stat;
209	unsigned long flags;
210
211	port = ptr;
212	stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
213	stat->OutDisable = -1;
214
215	/* Does this here really have to be? */
216	local_irq_save(flags);
217	port->gs.flags &= ~GS_TX_INTEN;
218	local_irq_restore(flags);
219}
220
221static void a2232_enable_tx_interrupts(void *ptr)
222{
223	struct a2232_port *port;
224	volatile struct a2232status *stat;
225	unsigned long flags;
226
227	port = ptr;
228	stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
229	stat->OutDisable = 0;
230
231	/* Does this here really have to be? */
232	local_irq_save(flags);
233	port->gs.flags |= GS_TX_INTEN;
234	local_irq_restore(flags);
235}
236
237static void a2232_disable_rx_interrupts(void *ptr)
238{
239	struct a2232_port *port;
240	port = ptr;
241	port->disable_rx = -1;
242}
243
244static void a2232_enable_rx_interrupts(void *ptr)
245{
246	struct a2232_port *port;
247	port = ptr;
248	port->disable_rx = 0;
249}
250
251static int  a2232_get_CD(void *ptr)
252{
253	return ((struct a2232_port *) ptr)->cd_status;
254}
255
256static void a2232_shutdown_port(void *ptr)
257{
258	struct a2232_port *port;
259	volatile struct a2232status *stat;
260	unsigned long flags;
261
262	port = ptr;
263	stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
264
265	local_irq_save(flags);
266
267	port->gs.flags &= ~GS_ACTIVE;
268
269	if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL) {
270		/* Set DTR and RTS to Low, flush output.
271		   The NetBSD driver "msc.c" does it this way. */
272		stat->Command = (	(stat->Command & ~A2232CMD_CMask) |
273					A2232CMD_Close );
274		stat->OutFlush = -1;
275		stat->Setup = -1;
276	}
277
278	local_irq_restore(flags);
279
280	/* After analyzing control flow, I think a2232_shutdown_port
281		is actually the last call from the system when at application
282		level someone issues a "echo Hello >>/dev/ttyY0".
283		Therefore I think the MOD_DEC_USE_COUNT should be here and
284		not in "a2232_close()". See the comment in "sx.c", too.
285		If you run into problems, compile this driver into the
286		kernel instead of compiling it as a module. */
287}
288
289static int  a2232_set_real_termios(void *ptr)
290{
291	unsigned int cflag, baud, chsize, stopb, parity, softflow;
292	int rate;
293	int a2232_param, a2232_cmd;
294	unsigned long flags;
295	unsigned int i;
296	struct a2232_port *port = ptr;
297	volatile struct a2232status *status;
298	volatile struct a2232memory *mem;
299
300	if (!port->gs.tty || !port->gs.tty->termios) return 0;
301
302	status = a2232stat(port->which_a2232, port->which_port_on_a2232);
303	mem = a2232mem(port->which_a2232);
304
305	a2232_param = a2232_cmd = 0;
306
307	// get baud rate
308	baud = port->gs.baud;
309	if (baud == 0) {
310		/* speed == 0 -> drop DTR, do nothing else */
311		local_irq_save(flags);
312		// Clear DTR (and RTS... mhhh).
313		status->Command = (	(status->Command & ~A2232CMD_CMask) |
314					A2232CMD_Close );
315		status->OutFlush = -1;
316		status->Setup = -1;
317
318		local_irq_restore(flags);
319		return 0;
320	}
321
322	rate = A2232_BAUD_TABLE_NOAVAIL;
323	for (i=0; i < A2232_BAUD_TABLE_NUM_RATES * 3; i += 3){
324		if (a2232_baud_table[i] == baud){
325			if (mem->Common.Crystal == A2232_TURBO) rate = a2232_baud_table[i+2];
326			else                                    rate = a2232_baud_table[i+1];
327		}
328	}
329	if (rate == A2232_BAUD_TABLE_NOAVAIL){
330		printk("a2232: Board %d Port %d unsupported baud rate: %d baud. Using another.\n",port->which_a2232,port->which_port_on_a2232,baud);
331		// This is useful for both (turbo or normal) Crystal versions.
332		rate = A2232PARAM_B9600;
333	}
334	a2232_param |= rate;
335
336	cflag  = port->gs.tty->termios->c_cflag;
337
338	// get character size
339	chsize = cflag & CSIZE;
340	switch (chsize){
341		case CS8: 	a2232_param |= A2232PARAM_8Bit; break;
342		case CS7: 	a2232_param |= A2232PARAM_7Bit; break;
343		case CS6: 	a2232_param |= A2232PARAM_6Bit; break;
344		case CS5: 	a2232_param |= A2232PARAM_5Bit; break;
345		default:	printk("a2232: Board %d Port %d unsupported character size: %d. Using 8 data bits.\n",
346					port->which_a2232,port->which_port_on_a2232,chsize);
347				a2232_param |= A2232PARAM_8Bit; break;
348	}
349
350	// get number of stop bits
351	stopb  = cflag & CSTOPB;
352	if (stopb){ // two stop bits instead of one
353		printk("a2232: Board %d Port %d 2 stop bits unsupported. Using 1 stop bit.\n",
354			port->which_a2232,port->which_port_on_a2232);
355	}
356
357	// Warn if RTS/CTS not wanted
358	if (!(cflag & CRTSCTS)){
359#ifndef A2232_SUPPRESS_RTSCTS_WARNING
360		printk("a2232: Board %d Port %d cannot switch off firmware-implemented RTS/CTS hardware flow control.\n",
361			port->which_a2232,port->which_port_on_a2232);
362#endif
363	}
364
365	/*	I think this is correct.
366		However, IXOFF means _input_ flow control and I wonder
367		if one should care about IXON _output_ flow control,
368		too. If this makes problems, one should turn the A2232
369		firmware XON/XOFF "SoftFlow" flow control off and use
370		the conventional way of inserting START/STOP characters
371		by hand in throttle()/unthrottle().
372	*/
373	softflow = !!( port->gs.tty->termios->c_iflag & IXOFF );
374
375	// get Parity (Enabled/Disabled? If Enabled, Odd or Even?)
376	parity = cflag & (PARENB | PARODD);
377	if (parity & PARENB){
378		if (parity & PARODD){
379			a2232_cmd |= A2232CMD_OddParity;
380		}
381		else{
382			a2232_cmd |= A2232CMD_EvenParity;
383		}
384	}
385	else a2232_cmd |= A2232CMD_NoParity;
386
387
388	/*	Hmm. Maybe an own a2232_port structure
389		member would be cleaner?	*/
390	if (cflag & CLOCAL)
391		port->gs.flags &= ~ASYNC_CHECK_CD;
392	else
393		port->gs.flags |= ASYNC_CHECK_CD;
394
395
396	/* Now we have all parameters and can go to set them: */
397	local_irq_save(flags);
398
399	status->Param = a2232_param | A2232PARAM_RcvBaud;
400	status->Command = a2232_cmd | A2232CMD_Open |  A2232CMD_Enable;
401	status->SoftFlow = softflow;
402	status->OutDisable = 0;
403	status->Setup = -1;
404
405	local_irq_restore(flags);
406	return 0;
407}
408
409static int  a2232_chars_in_buffer(void *ptr)
410{
411	struct a2232_port *port;
412	volatile struct a2232status *status;
413	unsigned char ret; /* we need modulo-256 arithmetics */
414	port = ptr;
415	status = a2232stat(port->which_a2232, port->which_port_on_a2232);
416#if A2232_IOBUFLEN != 256
417#error "Re-Implement a2232_chars_in_buffer()!"
418#endif
419	ret = (status->OutHead - status->OutTail);
420	return ret;
421}
422
423static void a2232_close(void *ptr)
424{
425	a2232_disable_tx_interrupts(ptr);
426	a2232_disable_rx_interrupts(ptr);
427	/* see the comment in a2232_shutdown_port above. */
428}
429
430static void a2232_hungup(void *ptr)
431{
432	a2232_close(ptr);
433}
434/*** END   OF REAL_DRIVER FUNCTIONS ***/
435
436/*** BEGIN  FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
437static int a2232_ioctl(	struct tty_struct *tty, struct file *file,
438			unsigned int cmd, unsigned long arg)
439{
440	return -ENOIOCTLCMD;
441}
442
443static void a2232_throttle(struct tty_struct *tty)
444{
445/* Throttle: System cannot take another chars: Drop RTS or
446             send the STOP char or whatever.
447   The A2232 firmware does RTS/CTS anyway, and XON/XOFF
448   if switched on. So the only thing we can do at this
449   layer here is not taking any characters out of the
450   A2232 buffer any more. */
451	struct a2232_port *port = (struct a2232_port *) tty->driver_data;
452	port->throttle_input = -1;
453}
454
455static void a2232_unthrottle(struct tty_struct *tty)
456{
457/* Unthrottle: dual to "throttle()" above. */
458	struct a2232_port *port = (struct a2232_port *) tty->driver_data;
459	port->throttle_input = 0;
460}
461
462static int  a2232_open(struct tty_struct * tty, struct file * filp)
463{
464/* More or less stolen from other drivers. */
465	int line;
466	int retval;
467	struct a2232_port *port;
468
469	line = tty->index;
470	port = &a2232_ports[line];
471
472	tty->driver_data = port;
473	port->gs.tty = tty;
474	port->gs.count++;
475	retval = gs_init_port(&port->gs);
476	if (retval) {
477		port->gs.count--;
478		return retval;
479	}
480	port->gs.flags |= GS_ACTIVE;
481	retval = gs_block_til_ready(port, filp);
482
483	if (retval) {
484		port->gs.count--;
485		return retval;
486	}
487
488	a2232_enable_rx_interrupts(port);
489
490	return 0;
491}
492/*** END OF FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
493
494static irqreturn_t a2232_vbl_inter(int irq, void *data)
495{
496#if A2232_IOBUFLEN != 256
497#error "Re-Implement a2232_vbl_inter()!"
498#endif
499
500struct a2232_port *port;
501volatile struct a2232memory *mem;
502volatile struct a2232status *status;
503unsigned char newhead;
504unsigned char bufpos; /* Must be unsigned char. We need the modulo-256 arithmetics */
505unsigned char ncd, ocd, ccd; /* names consistent with the NetBSD driver */
506volatile u_char *ibuf, *cbuf, *obuf;
507int ch, err, n, p;
508	for (n = 0; n < nr_a2232; n++){		/* for every completely initialized A2232 board */
509		mem = a2232mem(n);
510		for (p = 0; p < NUMLINES; p++){	/* for every port on this board */
511			err = 0;
512			port = &a2232_ports[n*NUMLINES+p];
513			if ( port->gs.flags & GS_ACTIVE ){ /* if the port is used */
514
515				status = a2232stat(n,p);
516
517				if (!port->disable_rx && !port->throttle_input){ /* If input is not disabled */
518					newhead = status->InHead;               /* 65EC02 write pointer */
519					bufpos = status->InTail;
520
521					/* check for input for this port */
522					if (newhead != bufpos) {
523						/* buffer for input chars/events */
524						ibuf = mem->InBuf[p];
525
526						/* data types of bytes in ibuf */
527						cbuf = mem->InCtl[p];
528
529						/* do for all chars */
530						while (bufpos != newhead) {
531							/* which type of input data? */
532							switch (cbuf[bufpos]) {
533								/* switch on input event (CD, BREAK, etc.) */
534							case A2232INCTL_EVENT:
535								switch (ibuf[bufpos++]) {
536								case A2232EVENT_Break:
537									/* TODO: Handle BREAK signal */
538									break;
539									/*	A2232EVENT_CarrierOn and A2232EVENT_CarrierOff are
540										handled in a separate queue and should not occur here. */
541								case A2232EVENT_Sync:
542									printk("A2232: 65EC02 software sent SYNC event, don't know what to do. Ignoring.");
543									break;
544								default:
545									printk("A2232: 65EC02 software broken, unknown event type %d occurred.\n",ibuf[bufpos-1]);
546								} /* event type switch */
547								break;
548 							case A2232INCTL_CHAR:
549								/* Receive incoming char */
550								a2232_receive_char(port, ibuf[bufpos], err);
551								bufpos++;
552								break;
553 							default:
554								printk("A2232: 65EC02 software broken, unknown data type %d occurred.\n",cbuf[bufpos]);
555								bufpos++;
556							} /* switch on input data type */
557						} /* while there's something in the buffer */
558
559						status->InTail = bufpos;            /* tell 65EC02 what we've read */
560
561					} /* if there was something in the buffer */
562				} /* If input is not disabled */
563
564				/* Now check if there's something to output */
565				obuf = mem->OutBuf[p];
566				bufpos = status->OutHead;
567				while ( (port->gs.xmit_cnt > 0)		&&
568					(!port->gs.tty->stopped)	&&
569					(!port->gs.tty->hw_stopped) ){	/* While there are chars to transmit */
570					if (((bufpos+1) & A2232_IOBUFLENMASK) != status->OutTail) { /* If the A2232 buffer is not full */
571						ch = port->gs.xmit_buf[port->gs.xmit_tail];					/* get the next char to transmit */
572						port->gs.xmit_tail = (port->gs.xmit_tail+1) & (SERIAL_XMIT_SIZE-1); /* modulo-addition for the gs.xmit_buf ring-buffer */
573						obuf[bufpos++] = ch;																/* put it into the A2232 buffer */
574						port->gs.xmit_cnt--;
575					}
576					else{																									/* If A2232 the buffer is full */
577						break;																							/* simply stop filling it. */
578					}
579				}
580				status->OutHead = bufpos;
581
582				/* WakeUp if output buffer runs low */
583				if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.tty) {
584					tty_wakeup(port->gs.tty);
585				}
586			} // if the port is used
587		} // for every port on the board
588
589		/* Now check the CD message queue */
590		newhead = mem->Common.CDHead;
591		bufpos = mem->Common.CDTail;
592		if (newhead != bufpos){				/* There are CD events in queue */
593			ocd = mem->Common.CDStatus; 		/* get old status bits */
594			while (newhead != bufpos){		/* read all events */
595				ncd = mem->CDBuf[bufpos++]; 	/* get one event */
596				ccd = ncd ^ ocd; 		/* mask of changed lines */
597				ocd = ncd; 			/* save new status bits */
598				for(p=0; p < NUMLINES; p++){	/* for all ports */
599					if (ccd & 1){		/* this one changed */
600
601						struct a2232_port *port = &a2232_ports[n*7+p];
602						port->cd_status = !(ncd & 1); /* ncd&1 <=> CD is now off */
603
604						if (!(port->gs.flags & ASYNC_CHECK_CD))
605							;	/* Don't report DCD changes */
606						else if (port->cd_status) { // if DCD on: DCD went UP!
607
608							/* Are we blocking in open?*/
609							wake_up_interruptible(&port->gs.open_wait);
610						}
611						else { // if DCD off: DCD went DOWN!
612							if (port->gs.tty)
613								tty_hangup (port->gs.tty);
614						}
615
616					} // if CD changed for this port
617					ccd >>= 1;
618					ncd >>= 1;									/* Shift bits for next line */
619				} // for every port
620			} // while CD events in queue
621			mem->Common.CDStatus = ocd; /* save new status */
622			mem->Common.CDTail = bufpos; /* remove events */
623		} // if events in CD queue
624
625	} // for every completely initialized A2232 board
626	return IRQ_HANDLED;
627}
628
629static void a2232_init_portstructs(void)
630{
631	struct a2232_port *port;
632	int i;
633
634	for (i = 0; i < MAX_A2232_BOARDS*NUMLINES; i++) {
635		port = a2232_ports + i;
636		port->which_a2232 = i/NUMLINES;
637		port->which_port_on_a2232 = i%NUMLINES;
638		port->disable_rx = port->throttle_input = port->cd_status = 0;
639		port->gs.magic = A2232_MAGIC;
640		port->gs.close_delay = HZ/2;
641		port->gs.closing_wait = 30 * HZ;
642		port->gs.rd = &a2232_real_driver;
643#ifdef NEW_WRITE_LOCKING
644		init_MUTEX(&(port->gs.port_write_mutex));
645#endif
646		init_waitqueue_head(&port->gs.open_wait);
647		init_waitqueue_head(&port->gs.close_wait);
648	}
649}
650
651static const struct tty_operations a2232_ops = {
652	.open = a2232_open,
653	.close = gs_close,
654	.write = gs_write,
655	.put_char = gs_put_char,
656	.flush_chars = gs_flush_chars,
657	.write_room = gs_write_room,
658	.chars_in_buffer = gs_chars_in_buffer,
659	.flush_buffer = gs_flush_buffer,
660	.ioctl = a2232_ioctl,
661	.throttle = a2232_throttle,
662	.unthrottle = a2232_unthrottle,
663	.set_termios = gs_set_termios,
664	.stop = gs_stop,
665	.start = gs_start,
666	.hangup = gs_hangup,
667};
668
669static int a2232_init_drivers(void)
670{
671	int error;
672
673	a2232_driver = alloc_tty_driver(NUMLINES * nr_a2232);
674	if (!a2232_driver)
675		return -ENOMEM;
676	a2232_driver->owner = THIS_MODULE;
677	a2232_driver->driver_name = "commodore_a2232";
678	a2232_driver->name = "ttyY";
679	a2232_driver->major = A2232_NORMAL_MAJOR;
680	a2232_driver->type = TTY_DRIVER_TYPE_SERIAL;
681	a2232_driver->subtype = SERIAL_TYPE_NORMAL;
682	a2232_driver->init_termios = tty_std_termios;
683	a2232_driver->init_termios.c_cflag =
684		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
685	a2232_driver->init_termios.c_ispeed = 9600;
686	a2232_driver->init_termios.c_ospeed = 9600;
687	a2232_driver->flags = TTY_DRIVER_REAL_RAW;
688	tty_set_operations(a2232_driver, &a2232_ops);
689	if ((error = tty_register_driver(a2232_driver))) {
690		printk(KERN_ERR "A2232: Couldn't register A2232 driver, error = %d\n",
691		       error);
692		put_tty_driver(a2232_driver);
693		return 1;
694	}
695	return 0;
696}
697
698static int __init a2232board_init(void)
699{
700	struct zorro_dev *z;
701
702	unsigned int boardaddr;
703	int bcount;
704	short start;
705	u_char *from;
706	volatile u_char *to;
707	volatile struct a2232memory *mem;
708
709#ifdef CONFIG_SMP
710	return -ENODEV;	/* This driver is not SMP aware. Is there an SMP ZorroII-bus-machine? */
711#endif
712
713	if (!MACH_IS_AMIGA){
714		return -ENODEV;
715	}
716
717	printk("Commodore A2232 driver initializing.\n"); /* Say that we're alive. */
718
719	z = NULL;
720	nr_a2232 = 0;
721	while ( (z = zorro_find_device(ZORRO_WILDCARD, z)) ){
722		if (	(z->id != ZORRO_PROD_CBM_A2232_PROTOTYPE) &&
723			(z->id != ZORRO_PROD_CBM_A2232)	){
724			continue;	// The board found was no A2232
725		}
726		if (!zorro_request_device(z,"A2232 driver"))
727			continue;
728
729		printk("Commodore A2232 found (#%d).\n",nr_a2232);
730
731		zd_a2232[nr_a2232] = z;
732
733		boardaddr = ZTWO_VADDR( z->resource.start );
734		printk("Board is located at address 0x%x, size is 0x%x.\n", boardaddr, (unsigned int) ((z->resource.end+1) - (z->resource.start)));
735
736		mem = (volatile struct a2232memory *) boardaddr;
737
738		(void) mem->Enable6502Reset;   /* copy the code across to the board */
739		to = (u_char *)mem;  from = a2232_65EC02code; bcount = sizeof(a2232_65EC02code) - 2;
740		start = *(short *)from;
741		from += sizeof(start);
742		to += start;
743		while(bcount--) *to++ = *from++;
744		printk("65EC02 software uploaded to the A2232 memory.\n");
745
746		mem->Common.Crystal = A2232_UNKNOWN;  /* use automatic speed check */
747
748		/* start 6502 running */
749		(void) mem->ResetBoard;
750		printk("A2232's 65EC02 CPU up and running.\n");
751
752		/* wait until speed detector has finished */
753		for (bcount = 0; bcount < 2000; bcount++) {
754			udelay(1000);
755			if (mem->Common.Crystal)
756				break;
757		}
758		printk((mem->Common.Crystal?"A2232 oscillator crystal detected by 65EC02 software: ":"65EC02 software could not determine A2232 oscillator crystal: "));
759		switch (mem->Common.Crystal){
760		case A2232_UNKNOWN:
761			printk("Unknown crystal.\n");
762			break;
763 		case A2232_NORMAL:
764			printk ("Normal crystal.\n");
765			break;
766		case A2232_TURBO:
767			printk ("Turbo crystal.\n");
768			break;
769		default:
770			printk ("0x%x. Huh?\n",mem->Common.Crystal);
771		}
772
773		nr_a2232++;
774
775	}
776
777	printk("Total: %d A2232 boards initialized.\n", nr_a2232); /* Some status report if no card was found */
778
779	a2232_init_portstructs();
780
781	/*
782		a2232_init_drivers also registers the drivers. Must be here because all boards
783		have to be detected first.
784	*/
785	if (a2232_init_drivers()) return -ENODEV; // maybe we should use a different -Exxx?
786
787	request_irq(IRQ_AMIGA_VERTB, a2232_vbl_inter, 0, "A2232 serial VBL", a2232_driver_ID);
788	return 0;
789}
790
791static void __exit a2232board_exit(void)
792{
793	int i;
794
795	for (i = 0; i < nr_a2232; i++) {
796		zorro_release_device(zd_a2232[i]);
797	}
798
799	tty_unregister_driver(a2232_driver);
800	put_tty_driver(a2232_driver);
801	free_irq(IRQ_AMIGA_VERTB, a2232_driver_ID);
802}
803
804module_init(a2232board_init);
805module_exit(a2232board_exit);
806
807MODULE_AUTHOR("Enver Haase");
808MODULE_DESCRIPTION("Amiga A2232 multi-serial board driver");
809MODULE_LICENSE("GPL");
810