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