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