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