This commit was generated by cvs2svn to compensate for changes in r517,
[linux-2.6.git] / drivers / char / isicom.c
1 /*
2  *      This program is free software; you can redistribute it and/or
3  *      modify it under the terms of the GNU General Public License
4  *      as published by the Free Software Foundation; either version
5  *      2 of the License, or (at your option) any later version.
6  *
7  *      Original driver code supplied by Multi-Tech
8  *
9  *      Changes
10  *      1/9/98  alan@redhat.com         Merge to 2.0.x kernel tree
11  *                                      Obtain and use official major/minors
12  *                                      Loader switched to a misc device
13  *                                      (fixed range check bug as a side effect)
14  *                                      Printk clean up
15  *      9/12/98 alan@redhat.com         Rough port to 2.1.x
16  *
17  *      10/6/99 sameer                  Merged the ISA and PCI drivers to
18  *                                      a new unified driver.
19  *      09/06/01 acme@conectiva.com.br  use capable, not suser, do
20  *                                      restore_flags on failure in
21  *                                      isicom_send_break, verify put_user
22  *                                      result
23  *      ***********************************************************
24  *
25  *      To use this driver you also need the support package. You 
26  *      can find this in RPM format on
27  *              ftp://ftp.linux.org.uk/pub/linux/alan
28  *      
29  *      You can find the original tools for this direct from Multitech
30  *              ftp://ftp.multitech.com/ISI-Cards/
31  *
32  *      Having installed the cards the module options (/etc/modprobe.conf)
33  *
34  *      options isicom   io=card1,card2,card3,card4 irq=card1,card2,card3,card4
35  *
36  *      Omit those entries for boards you don't have installed.
37  *
38  */
39
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/tty.h>
43 #include <linux/termios.h>
44 #include <linux/fs.h>
45 #include <linux/sched.h>
46 #include <linux/serial.h>
47 #include <linux/mm.h>
48 #include <linux/miscdevice.h>
49 #include <linux/interrupt.h>
50 #include <linux/timer.h>
51 #include <linux/delay.h>
52 #include <linux/ioport.h>
53
54 #include <asm/uaccess.h>
55 #include <asm/io.h>
56 #include <asm/system.h>
57
58 #include <linux/pci.h>
59
60 #include <linux/isicom.h>
61
62 static struct pci_device_id isicom_pci_tbl[] = {
63         { VENDOR_ID, 0x2028, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
64         { VENDOR_ID, 0x2051, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
65         { VENDOR_ID, 0x2052, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
66         { VENDOR_ID, 0x2053, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
67         { VENDOR_ID, 0x2054, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
68         { VENDOR_ID, 0x2055, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
69         { VENDOR_ID, 0x2056, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
70         { VENDOR_ID, 0x2057, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
71         { VENDOR_ID, 0x2058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
72         { 0 }
73 };
74 MODULE_DEVICE_TABLE(pci, isicom_pci_tbl);
75
76 static int prev_card = 3;       /*      start servicing isi_card[0]     */
77 static struct isi_board * irq_to_board[16];
78 static struct tty_driver *isicom_normal;
79
80 static struct isi_board isi_card[BOARD_COUNT];
81 static struct isi_port  isi_ports[PORT_COUNT];
82
83 static struct timer_list tx;
84 static char re_schedule = 1;
85 #ifdef ISICOM_DEBUG
86 static unsigned long tx_count = 0;
87 #endif
88
89 static int ISILoad_ioctl(struct inode *inode, struct file *filp, unsigned  int cmd, unsigned long arg);
90
91 static void isicom_tx(unsigned long _data);
92 static void isicom_start(struct tty_struct * tty);
93
94 static unsigned char * tmp_buf;
95 static DECLARE_MUTEX(tmp_buf_sem);
96
97 /*   baud index mappings from linux defns to isi */
98
99 static signed char linuxb_to_isib[] = {
100         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17,     
101         18, 19
102 };
103
104 /* 
105  *  Firmware loader driver specific routines
106  *
107  */
108
109 static struct file_operations ISILoad_fops = {
110         .owner          = THIS_MODULE,
111         .ioctl          = ISILoad_ioctl,
112 };
113
114 struct miscdevice isiloader_device = {
115         ISILOAD_MISC_MINOR, "isictl", &ISILoad_fops
116 };
117
118  
119 static inline int WaitTillCardIsFree(unsigned short base)
120 {
121         unsigned long count=0;
122         while( (!(inw(base+0xe) & 0x1)) && (count++ < 6000000));
123         if (inw(base+0xe)&0x1)  
124                 return 0;
125         else
126                 return 1;
127 }
128
129 static int ISILoad_ioctl(struct inode *inode, struct file *filp,
130                          unsigned int cmd, unsigned long arg)
131 {
132         unsigned int card, i, j, signature, status, portcount = 0;
133         unsigned long t;
134         unsigned short word_count, base;
135         bin_frame frame;
136         void __user *argp = (void __user *)arg;
137         /* exec_record exec_rec; */
138         
139         if(get_user(card, (int __user *)argp))
140                 return -EFAULT;
141                 
142         if(card < 0 || card >= BOARD_COUNT)
143                 return -ENXIO;
144                 
145         base=isi_card[card].base;
146         
147         if(base==0)
148                 return -ENXIO;  /* disabled or not used */
149         
150         switch(cmd) {
151                 case MIOCTL_RESET_CARD:
152                         if (!capable(CAP_SYS_ADMIN))
153                                 return -EPERM;
154                         printk(KERN_DEBUG "ISILoad:Resetting Card%d at 0x%x ",card+1,base);
155                                                                 
156                         inw(base+0x8);
157                         
158                         for(t=jiffies+HZ/100;time_before(jiffies, t););
159                                 
160                         outw(0,base+0x8); /* Reset */
161                         
162                         for(j=1;j<=3;j++) {
163                                 for(t=jiffies+HZ;time_before(jiffies, t););
164                                 printk(".");
165                         }       
166                         signature=(inw(base+0x4)) & 0xff;       
167                         if (isi_card[card].isa) {
168                                         
169                                 if (!(inw(base+0xe) & 0x1) || (inw(base+0x2))) {
170 #ifdef ISICOM_DEBUG                             
171                                         printk("\nbase+0x2=0x%x , base+0xe=0x%x",inw(base+0x2),inw(base+0xe));
172 #endif                          
173                                         printk("\nISILoad:ISA Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
174                                         return -EIO;                                    
175                                 }
176                         }       
177                         else {
178                                 portcount = inw(base+0x2);
179                                 if (!(inw(base+0xe) & 0x1) || ((portcount!=0) && (portcount!=4) && (portcount!=8))) {   
180 #ifdef ISICOM_DEBUG
181                                         printk("\nbase+0x2=0x%x , base+0xe=0x%x",inw(base+0x2),inw(base+0xe));
182 #endif
183                                         printk("\nISILoad:PCI Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
184                                         return -EIO;
185                                 }
186                         }       
187                         switch(signature) {
188                         case    0xa5:
189                         case    0xbb:
190                         case    0xdd:   
191                                         if (isi_card[card].isa) 
192                                                 isi_card[card].port_count = 8;
193                                         else {
194                                                 if (portcount == 4)
195                                                         isi_card[card].port_count = 4;
196                                                 else
197                                                         isi_card[card].port_count = 8;
198                                         }       
199                                         isi_card[card].shift_count = 12;
200                                         break;
201                                         
202                         case    0xcc:   isi_card[card].port_count = 16;
203                                         isi_card[card].shift_count = 11;
204                                         break;                          
205                                         
206                         default: printk("ISILoad:Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
207 #ifdef ISICOM_DEBUG                     
208                                  printk("Sig=0x%x\n",signature);
209 #endif                           
210                                  return -EIO;
211                         }
212                         printk("-Done\n");
213                         return put_user(signature,(unsigned __user *)argp);
214                                                 
215         case    MIOCTL_LOAD_FIRMWARE:
216                         if (!capable(CAP_SYS_ADMIN))
217                                 return -EPERM;
218                                 
219                         if(copy_from_user(&frame, argp, sizeof(bin_frame)))
220                                 return -EFAULT;
221                         
222                         if (WaitTillCardIsFree(base))
223                                 return -EIO;
224                         
225                         outw(0xf0,base);        /* start upload sequence */ 
226                         outw(0x00,base);
227                         outw((frame.addr), base);/*      lsb of adderess    */
228                         
229                         word_count=(frame.count >> 1) + frame.count % 2;
230                         outw(word_count, base);
231                         InterruptTheCard(base);
232                         
233                         for(i=0;i<=0x2f;i++);   /* a wee bit of delay */
234                         
235                         if (WaitTillCardIsFree(base)) 
236                                 return -EIO;
237                                 
238                         if ((status=inw(base+0x4))!=0) {
239                                 printk(KERN_WARNING "ISILoad:Card%d rejected load header:\nAddress:0x%x \nCount:0x%x \nStatus:0x%x \n", 
240                                 card+1, frame.addr, frame.count, status);
241                                 return -EIO;
242                         }
243                         outsw(base, (void *) frame.bin_data, word_count);
244                         
245                         InterruptTheCard(base);
246                         
247                         for(i=0;i<=0x0f;i++);   /* another wee bit of delay */ 
248                         
249                         if (WaitTillCardIsFree(base)) 
250                                 return -EIO;
251                                 
252                         if ((status=inw(base+0x4))!=0) {
253                                 printk(KERN_ERR "ISILoad:Card%d got out of sync.Card Status:0x%x\n",card+1, status);
254                                 return -EIO;
255                         }       
256                         return 0;
257                                                 
258         case    MIOCTL_READ_FIRMWARE:
259                         if (!capable(CAP_SYS_ADMIN))
260                                 return -EPERM;
261                                 
262                         if(copy_from_user(&frame, argp, sizeof(bin_header)))
263                                 return -EFAULT;
264                         
265                         if (WaitTillCardIsFree(base))
266                                 return -EIO;
267                         
268                         outw(0xf1,base);        /* start download sequence */ 
269                         outw(0x00,base);
270                         outw((frame.addr), base);/*      lsb of adderess    */
271                         
272                         word_count=(frame.count >> 1) + frame.count % 2;
273                         outw(word_count+1, base);
274                         InterruptTheCard(base);
275                         
276                         for(i=0;i<=0xf;i++);    /* a wee bit of delay */
277                         
278                         if (WaitTillCardIsFree(base)) 
279                                 return -EIO;
280                                 
281                         if ((status=inw(base+0x4))!=0) {
282                                 printk(KERN_WARNING "ISILoad:Card%d rejected verify header:\nAddress:0x%x \nCount:0x%x \nStatus:0x%x \n", 
283                                 card+1, frame.addr, frame.count, status);
284                                 return -EIO;
285                         }
286                         
287                         inw(base);
288                         insw(base, frame.bin_data, word_count);
289                         InterruptTheCard(base);
290                         
291                         for(i=0;i<=0x0f;i++);   /* another wee bit of delay */ 
292                         
293                         if (WaitTillCardIsFree(base)) 
294                                 return -EIO;
295                                 
296                         if ((status=inw(base+0x4))!=0) {
297                                 printk(KERN_ERR "ISILoad:Card%d verify got out of sync.Card Status:0x%x\n",card+1, status);
298                                 return -EIO;
299                         }       
300                         
301                         if(copy_to_user(argp, &frame, sizeof(bin_frame)))
302                                 return -EFAULT;
303                         return 0;
304         
305         case    MIOCTL_XFER_CTRL:
306                         if (!capable(CAP_SYS_ADMIN))
307                                 return -EPERM;
308                         if (WaitTillCardIsFree(base)) 
309                                 return -EIO;
310                                         
311                         outw(0xf2, base);
312                         outw(0x800, base);
313                         outw(0x0, base);
314                         outw(0x0, base);
315                         InterruptTheCard(base);
316                         outw(0x0, base+0x4);    /* for ISI4608 cards */
317                                                         
318                         isi_card[card].status |= FIRMWARE_LOADED;
319                         return 0;       
320                         
321         default:
322 #ifdef ISICOM_DEBUG     
323                 printk(KERN_DEBUG "ISILoad: Received Ioctl cmd 0x%x.\n", cmd); 
324 #endif
325                 return -ENOIOCTLCMD;
326         
327         }
328         
329 }
330                                 
331
332 /*
333  *      ISICOM Driver specific routines ...
334  *
335  */
336  
337 static inline int isicom_paranoia_check(struct isi_port const * port, char *name, 
338                                         const char * routine)
339 {
340 #ifdef ISICOM_DEBUG 
341         static const char * badmagic = 
342                         KERN_WARNING "ISICOM: Warning: bad isicom magic for dev %s in %s.\n";
343         static const char * badport = 
344                         KERN_WARNING "ISICOM: Warning: NULL isicom port for dev %s in %s.\n";           
345         if (!port) {
346                 printk(badport, name, routine);
347                 return 1;
348         }
349         if (port->magic != ISICOM_MAGIC) {
350                 printk(badmagic, name, routine);
351                 return 1;
352         }       
353 #endif  
354         return 0;
355 }
356                         
357 /*      Transmitter     */
358
359 static void isicom_tx(unsigned long _data)
360 {
361         short count = (BOARD_COUNT-1), card, base;
362         short txcount, wait, wrd, residue, word_count, cnt;
363         struct isi_port * port;
364         struct tty_struct * tty;
365         unsigned long flags;
366         
367 #ifdef ISICOM_DEBUG
368         ++tx_count;
369 #endif  
370         
371         /*      find next active board  */
372         card = (prev_card + 1) & 0x0003;
373         while(count-- > 0) {
374                 if (isi_card[card].status & BOARD_ACTIVE) 
375                         break;
376                 card = (card + 1) & 0x0003;     
377         }
378         if (!(isi_card[card].status & BOARD_ACTIVE))
379                 goto sched_again;
380                 
381         prev_card = card;
382         
383         count = isi_card[card].port_count;
384         port = isi_card[card].ports;
385         base = isi_card[card].base;
386         for (;count > 0;count--, port++) {
387                 /* port not active or tx disabled to force flow control */
388                 if (!(port->status & ISI_TXOK))
389                         continue;
390                 
391                 tty = port->tty;
392                 save_flags(flags); cli();
393                 txcount = min_t(short, TX_SIZE, port->xmit_cnt);
394                 if ((txcount <= 0) || tty->stopped || tty->hw_stopped) {
395                         restore_flags(flags);
396                         continue;
397                 }
398                 wait = 200;     
399                 while(((inw(base+0x0e) & 0x01) == 0) && (wait-- > 0));
400                 if (wait <= 0) {
401                         restore_flags(flags);
402 #ifdef ISICOM_DEBUG
403                         printk(KERN_DEBUG "ISICOM: isicom_tx:Card(0x%x) found busy.\n",
404                                 card);
405 #endif
406                         continue;
407                 }
408                 if (!(inw(base + 0x02) & (1 << port->channel))) {
409                         restore_flags(flags);
410 #ifdef ISICOM_DEBUG                                     
411                         printk(KERN_DEBUG "ISICOM: isicom_tx: cannot tx to 0x%x:%d.\n",
412                                         base, port->channel + 1);
413 #endif                                  
414                         continue;               
415                 }
416 #ifdef ISICOM_DEBUG
417                 printk(KERN_DEBUG "ISICOM: txing %d bytes, port%d.\n", 
418                                 txcount, port->channel+1); 
419 #endif  
420                 outw((port->channel << isi_card[card].shift_count) | txcount
421                                         , base);
422                 residue = NO;
423                 wrd = 0;                        
424                 while (1) {
425                         cnt = min_t(int, txcount, (SERIAL_XMIT_SIZE - port->xmit_tail));
426                         if (residue == YES) {
427                                 residue = NO;
428                                 if (cnt > 0) {
429                                         wrd |= (port->xmit_buf[port->xmit_tail] << 8);
430                                         port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
431                                         port->xmit_cnt--;
432                                         txcount--;
433                                         cnt--;
434                                         outw(wrd, base);                        
435                                 }
436                                 else {
437                                         outw(wrd, base);
438                                         break;
439                                 }
440                         }               
441                         if (cnt <= 0) break;
442                         word_count = cnt >> 1;
443                         outsw(base, port->xmit_buf+port->xmit_tail, word_count);
444                         port->xmit_tail = (port->xmit_tail + (word_count << 1)) &
445                                                 (SERIAL_XMIT_SIZE - 1);
446                         txcount -= (word_count << 1);
447                         port->xmit_cnt -= (word_count << 1);
448                         if (cnt & 0x0001) {
449                                 residue = YES;
450                                 wrd = port->xmit_buf[port->xmit_tail];
451                                 port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
452                                 port->xmit_cnt--;
453                                 txcount--;
454                         }
455                 }
456
457                 InterruptTheCard(base);
458                 if (port->xmit_cnt <= 0)
459                         port->status &= ~ISI_TXOK;
460                 if (port->xmit_cnt <= WAKEUP_CHARS)
461                         schedule_work(&port->bh_tqueue);
462                 restore_flags(flags);
463         }       
464
465                 /*      schedule another tx for hopefully in about 10ms */      
466 sched_again:    
467         if (!re_schedule)       
468                 return;
469         init_timer(&tx);
470         tx.expires = jiffies + HZ/100;
471         tx.data = 0;
472         tx.function = isicom_tx;
473         add_timer(&tx);
474         
475         return; 
476 }               
477  
478 /*      Interrupt handlers      */
479
480  
481 static void isicom_bottomhalf(void * data)
482 {
483         struct isi_port * port = (struct isi_port *) data;
484         struct tty_struct * tty = port->tty;
485         
486         if (!tty)
487                 return;
488
489         tty_wakeup(tty);        
490         wake_up_interruptible(&tty->write_wait);
491 }               
492                 
493 /* main interrupt handler routine */            
494 static irqreturn_t isicom_interrupt(int irq, void *dev_id,
495                                         struct pt_regs *regs)
496 {
497         struct isi_board * card;
498         struct isi_port * port;
499         struct tty_struct * tty;
500         unsigned short base, header, word_count, count;
501         unsigned char channel;
502         short byte_count;
503         
504         /*
505          *      find the source of interrupt
506          */
507          
508         for(count = 0; count < BOARD_COUNT; count++) { 
509                 card = &isi_card[count];
510                 if (card->base != 0) {
511                         if (((card->isa == YES) && (card->irq == irq)) || 
512                                 ((card->isa == NO) && (card->irq == irq) && (inw(card->base+0x0e) & 0x02)))
513                                 break;
514                 }
515                 card = NULL;
516         }
517
518         if (!card || !(card->status & FIRMWARE_LOADED)) {
519 /*              printk(KERN_DEBUG "ISICOM: interrupt: not handling irq%d!.\n", irq);*/
520                 return IRQ_NONE;
521         }
522         
523         base = card->base;
524         if (card->isa == NO) {
525         /*
526          *      disable any interrupts from the PCI card and lower the
527          *      interrupt line
528          */
529                 outw(0x8000, base+0x04);
530                 ClearInterrupt(base);
531         }
532         
533         inw(base);              /* get the dummy word out */
534         header = inw(base);
535         channel = (header & 0x7800) >> card->shift_count;
536         byte_count = header & 0xff;
537 #ifdef ISICOM_DEBUG     
538         printk(KERN_DEBUG "ISICOM:Intr:(0x%x:%d).\n", base, channel+1);
539 #endif  
540         if ((channel+1) > card->port_count) {
541                 printk(KERN_WARNING "ISICOM: isicom_interrupt(0x%x): %d(channel) > port_count.\n",
542                                 base, channel+1);
543                 if (card->isa)
544                         ClearInterrupt(base);
545                 else
546                         outw(0x0000, base+0x04); /* enable interrupts */                
547                 return IRQ_HANDLED;                     
548         }
549         port = card->ports + channel;
550         if (!(port->flags & ASYNC_INITIALIZED)) {
551                 if (card->isa)
552                         ClearInterrupt(base);
553                 else
554                         outw(0x0000, base+0x04); /* enable interrupts */
555                 return IRQ_HANDLED;
556         }       
557                 
558         tty = port->tty;
559         
560         if (header & 0x8000) {          /* Status Packet */
561                 header = inw(base);
562                 switch(header & 0xff) {
563                         case 0: /* Change in EIA signals */
564                                 
565                                 if (port->flags & ASYNC_CHECK_CD) {
566                                         if (port->status & ISI_DCD) {
567                                                 if (!(header & ISI_DCD)) {
568                                                 /* Carrier has been lost  */
569 #ifdef ISICOM_DEBUG                                             
570                                                         printk(KERN_DEBUG "ISICOM: interrupt: DCD->low.\n");
571 #endif                                                  
572                                                         port->status &= ~ISI_DCD;
573                                                         schedule_work(&port->hangup_tq);
574                                                 }
575                                         }
576                                         else {
577                                                 if (header & ISI_DCD) {
578                                                 /* Carrier has been detected */
579 #ifdef ISICOM_DEBUG
580                                                         printk(KERN_DEBUG "ISICOM: interrupt: DCD->high.\n");
581 #endif                                                  
582                                                         port->status |= ISI_DCD;
583                                                         wake_up_interruptible(&port->open_wait);
584                                                 }
585                                         }
586                                 }
587                                 else {
588                                         if (header & ISI_DCD) 
589                                                 port->status |= ISI_DCD;
590                                         else
591                                                 port->status &= ~ISI_DCD;
592                                 }       
593                                 
594                                 if (port->flags & ASYNC_CTS_FLOW) {
595                                         if (port->tty->hw_stopped) {
596                                                 if (header & ISI_CTS) {
597                                                         port->tty->hw_stopped = 0;
598                                                         /* start tx ing */
599                                                         port->status |= (ISI_TXOK | ISI_CTS);
600                                                         schedule_work(&port->bh_tqueue);
601                                                 }
602                                         }
603                                         else {
604                                                 if (!(header & ISI_CTS)) {
605                                                         port->tty->hw_stopped = 1;
606                                                         /* stop tx ing */
607                                                         port->status &= ~(ISI_TXOK | ISI_CTS);
608                                                 }
609                                         }
610                                 }
611                                 else {
612                                         if (header & ISI_CTS) 
613                                                 port->status |= ISI_CTS;
614                                         else
615                                                 port->status &= ~ISI_CTS;
616                                 }
617                                 
618                                 if (header & ISI_DSR) 
619                                         port->status |= ISI_DSR;
620                                 else
621                                         port->status &= ~ISI_DSR;
622                                 
623                                 if (header & ISI_RI) 
624                                         port->status |= ISI_RI;
625                                 else
626                                         port->status &= ~ISI_RI;                                                
627                                 
628                                 break;
629                                 
630                         case 1: /* Received Break !!!    */
631                                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
632                                         break;
633                                 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
634                                 /* dunno if this is right */    
635                                 *tty->flip.char_buf_ptr++ = 0;
636                                 tty->flip.count++;
637                                 if (port->flags & ASYNC_SAK)
638                                         do_SAK(tty);
639                                 schedule_delayed_work(&tty->flip.work, 1);
640                                 break;
641                                 
642                         case 2: /* Statistics            */
643                                 printk(KERN_DEBUG "ISICOM: isicom_interrupt: stats!!!.\n");                     
644                                 break;
645                                 
646                         default:
647                                 printk(KERN_WARNING "ISICOM: Intr: Unknown code in status packet.\n");
648                                 break;
649                 }        
650         }
651         else {                          /* Data   Packet */
652                 count = min_t(unsigned short, byte_count, (TTY_FLIPBUF_SIZE - tty->flip.count));
653 #ifdef ISICOM_DEBUG
654                 printk(KERN_DEBUG "ISICOM: Intr: Can rx %d of %d bytes.\n", 
655                                         count, byte_count);
656 #endif                  
657                 word_count = count >> 1;
658                 insw(base, tty->flip.char_buf_ptr, word_count);
659                 tty->flip.char_buf_ptr += (word_count << 1);            
660                 byte_count -= (word_count << 1);
661                 if (count & 0x0001) {
662                         *tty->flip.char_buf_ptr++ = (char)(inw(base) & 0xff);
663                         byte_count -= 2;
664                 }       
665                 memset(tty->flip.flag_buf_ptr, 0, count);
666                 tty->flip.flag_buf_ptr += count;
667                 tty->flip.count += count;
668                 
669                 if (byte_count > 0) {
670                         printk(KERN_DEBUG "ISICOM: Intr(0x%x:%d): Flip buffer overflow! dropping bytes...\n",
671                                         base, channel+1);
672                         while(byte_count > 0) { /* drain out unread xtra data */
673                                 inw(base);
674                                 byte_count -= 2;
675                         }
676                 }
677                 schedule_delayed_work(&tty->flip.work, 1);
678         }
679         if (card->isa == YES)
680                 ClearInterrupt(base);
681         else
682                 outw(0x0000, base+0x04); /* enable interrupts */        
683         return IRQ_HANDLED;
684
685
686  /* called with interrupts disabled */ 
687 static void isicom_config_port(struct isi_port * port)
688 {
689         struct isi_board * card = port->card;
690         struct tty_struct * tty;
691         unsigned long baud;
692         unsigned short channel_setup, wait, base = card->base;
693         unsigned short channel = port->channel, shift_count = card->shift_count;
694         unsigned char flow_ctrl;
695         
696         if (!(tty = port->tty) || !tty->termios)
697                 return;
698         baud = C_BAUD(tty);
699         if (baud & CBAUDEX) {
700                 baud &= ~CBAUDEX;
701                 
702                 /*  if CBAUDEX bit is on and the baud is set to either 50 or 75
703                  *  then the card is programmed for 57.6Kbps or 115Kbps
704                  *  respectively.
705                  */   
706                  
707                 if (baud < 1 || baud > 2)
708                         port->tty->termios->c_cflag &= ~CBAUDEX;
709                 else
710                         baud += 15;
711         }       
712         if (baud == 15) {
713         
714                 /*  the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set 
715                  *  by the set_serial_info ioctl ... this is done by
716                  *  the 'setserial' utility.
717                  */  
718                         
719                 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
720                         baud++;     /*  57.6 Kbps */
721                 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
722                         baud +=2;   /*  115  Kbps */     
723         }
724         if (linuxb_to_isib[baud] == -1) {
725                 /* hang up */
726                 drop_dtr(port);
727                 return;
728         }       
729         else  
730                 raise_dtr(port);
731                 
732         wait = 100;     
733         while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));     
734         if (!wait) {
735                 printk(KERN_WARNING "ISICOM: Card found busy in isicom_config_port at channel setup.\n");
736                 return;
737         }                        
738         outw(0x8000 | (channel << shift_count) |0x03, base);
739         outw(linuxb_to_isib[baud] << 8 | 0x03, base);
740         channel_setup = 0;
741         switch(C_CSIZE(tty)) {
742                 case CS5:
743                         channel_setup |= ISICOM_CS5;
744                         break;
745                 case CS6:
746                         channel_setup |= ISICOM_CS6;
747                         break;
748                 case CS7:
749                         channel_setup |= ISICOM_CS7;
750                         break;
751                 case CS8:
752                         channel_setup |= ISICOM_CS8;
753                         break;
754         }
755                 
756         if (C_CSTOPB(tty))
757                 channel_setup |= ISICOM_2SB;
758         
759         if (C_PARENB(tty))
760                 channel_setup |= ISICOM_EVPAR;
761         if (C_PARODD(tty))
762                 channel_setup |= ISICOM_ODPAR;  
763         outw(channel_setup, base);      
764         InterruptTheCard(base);
765         
766         if (C_CLOCAL(tty))
767                 port->flags &= ~ASYNC_CHECK_CD;
768         else
769                 port->flags |= ASYNC_CHECK_CD;  
770         
771         /* flow control settings ...*/
772         flow_ctrl = 0;
773         port->flags &= ~ASYNC_CTS_FLOW;
774         if (C_CRTSCTS(tty)) {
775                 port->flags |= ASYNC_CTS_FLOW;
776                 flow_ctrl |= ISICOM_CTSRTS;
777         }       
778         if (I_IXON(tty))        
779                 flow_ctrl |= ISICOM_RESPOND_XONXOFF;
780         if (I_IXOFF(tty))
781                 flow_ctrl |= ISICOM_INITIATE_XONXOFF;   
782                 
783         wait = 100;     
784         while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));     
785         if (!wait) {
786                 printk(KERN_WARNING "ISICOM: Card found busy in isicom_config_port at flow setup.\n");
787                 return;
788         }                        
789         outw(0x8000 | (channel << shift_count) |0x04, base);
790         outw(flow_ctrl << 8 | 0x05, base);
791         outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
792         InterruptTheCard(base);
793         
794         /*      rx enabled -> enable port for rx on the card    */
795         if (C_CREAD(tty)) {
796                 card->port_status |= (1 << channel);
797                 outw(card->port_status, base + 0x02);
798         }
799                 
800 }
801  
802 /* open et all */ 
803
804 static inline void isicom_setup_board(struct isi_board * bp)
805 {
806         int channel;
807         struct isi_port * port;
808         unsigned long flags;
809         
810         if (bp->status & BOARD_ACTIVE) 
811                 return;
812         port = bp->ports;
813 #ifdef ISICOM_DEBUG     
814         printk(KERN_DEBUG "ISICOM: setup_board: drop_dtr_rts start, port_count %d...\n", bp->port_count);
815 #endif
816         for(channel = 0; channel < bp->port_count; channel++, port++) {
817                 save_flags(flags); cli();
818                 drop_dtr_rts(port);
819                 restore_flags(flags);
820         }
821 #ifdef ISICOM_DEBUG             
822         printk(KERN_DEBUG "ISICOM: setup_board: drop_dtr_rts stop...\n");       
823 #endif  
824         
825         bp->status |= BOARD_ACTIVE;
826         return;
827 }
828  
829 static int isicom_setup_port(struct isi_port * port)
830 {
831         struct isi_board * card = port->card;
832         unsigned long flags;
833         
834         if (port->flags & ASYNC_INITIALIZED)
835                 return 0;
836         if (!port->xmit_buf) {
837                 unsigned long page;
838                 
839                 if (!(page = get_zeroed_page(GFP_KERNEL)))
840                         return -ENOMEM;
841                 
842                 if (port->xmit_buf) {
843                         free_page(page);
844                         return -ERESTARTSYS;
845                 }
846                 port->xmit_buf = (unsigned char *) page;        
847         }       
848         save_flags(flags); cli();
849         if (port->tty)
850                 clear_bit(TTY_IO_ERROR, &port->tty->flags);
851         if (port->count == 1)
852                 card->count++;
853                 
854         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
855         
856         /*      discard any residual data       */
857         kill_queue(port, ISICOM_KILLTX | ISICOM_KILLRX);
858         
859         isicom_config_port(port);
860         port->flags |= ASYNC_INITIALIZED;
861         
862         restore_flags(flags);
863         
864         return 0;               
865
866  
867 static int block_til_ready(struct tty_struct * tty, struct file * filp, struct isi_port * port) 
868 {
869         int do_clocal = 0, retval;
870         DECLARE_WAITQUEUE(wait, current);
871
872         /* block if port is in the process of being closed */
873
874         if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
875 #ifdef ISICOM_DEBUG     
876                 printk(KERN_DEBUG "ISICOM: block_til_ready: close in progress.\n");
877 #endif          
878                 interruptible_sleep_on(&port->close_wait);
879                 if (port->flags & ASYNC_HUP_NOTIFY)
880                         return -EAGAIN;
881                 else
882                         return -ERESTARTSYS;
883         }
884         
885         /* if non-blocking mode is set ... */
886         
887         if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
888 #ifdef ISICOM_DEBUG     
889                 printk(KERN_DEBUG "ISICOM: block_til_ready: non-block mode.\n");
890 #endif          
891                 port->flags |= ASYNC_NORMAL_ACTIVE;
892                 return 0;       
893         }       
894         
895         if (C_CLOCAL(tty))
896                 do_clocal = 1;
897 #ifdef ISICOM_DEBUG     
898         if (do_clocal)
899                 printk(KERN_DEBUG "ISICOM: block_til_ready: CLOCAL set.\n");
900 #endif          
901         
902         /* block waiting for DCD to be asserted, and while 
903                                                 callout dev is busy */
904         retval = 0;
905         add_wait_queue(&port->open_wait, &wait);
906         cli();
907                 if (!tty_hung_up_p(filp))
908                         port->count--;
909         sti();
910         port->blocked_open++;
911 #ifdef ISICOM_DEBUG     
912         printk(KERN_DEBUG "ISICOM: block_til_ready: waiting for DCD...\n");
913 #endif  
914         while (1) {
915                 cli();
916                 raise_dtr_rts(port);
917                 sti();
918                 set_current_state(TASK_INTERRUPTIBLE);
919                 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {        
920                         if (port->flags & ASYNC_HUP_NOTIFY)
921                                 retval = -EAGAIN;
922                         else
923                                 retval = -ERESTARTSYS;
924 #ifdef ISICOM_DEBUG                             
925                         printk(KERN_DEBUG "ISICOM: block_til_ready: tty_hung_up_p || not init.\n"); 
926 #endif                  
927                         break;
928                 }       
929                 if (!(port->flags & ASYNC_CLOSING) &&
930                     (do_clocal || (port->status & ISI_DCD))) {
931 #ifdef ISICOM_DEBUG                 
932                         printk(KERN_DEBUG "ISICOM: block_til_ready: do_clocal || DCD.\n");   
933 #endif                  
934                         break;
935                 }       
936                 if (signal_pending(current)) {
937 #ifdef ISICOM_DEBUG             
938                         printk(KERN_DEBUG "ISICOM: block_til_ready: sig blocked.\n");
939 #endif                  
940                         retval = -ERESTARTSYS;
941                         break;
942                 }
943                 schedule();             
944         }
945         set_current_state(TASK_RUNNING);
946         remove_wait_queue(&port->open_wait, &wait);
947         if (!tty_hung_up_p(filp))
948                 port->count++;
949         port->blocked_open--;
950         if (retval)
951                 return retval;
952         port->flags |= ASYNC_NORMAL_ACTIVE;
953         return 0;
954 }
955  
956 static int isicom_open(struct tty_struct * tty, struct file * filp)
957 {
958         struct isi_port * port;
959         struct isi_board * card;
960         unsigned int line, board;
961         int error;
962
963 #ifdef ISICOM_DEBUG     
964         printk(KERN_DEBUG "ISICOM: open start!!!.\n");
965 #endif  
966         line = tty->index;
967         
968 #ifdef ISICOM_DEBUG     
969         printk(KERN_DEBUG "line = %d.\n", line);
970 #endif  
971         
972         if ((line < 0) || (line > (PORT_COUNT-1)))
973                 return -ENODEV;
974         board = BOARD(line);
975         
976 #ifdef ISICOM_DEBUG     
977         printk(KERN_DEBUG "board = %d.\n", board);
978 #endif  
979         
980         card = &isi_card[board];
981         if (!(card->status & FIRMWARE_LOADED)) {
982 #ifdef ISICOM_DEBUG     
983                 printk(KERN_DEBUG"ISICOM: Firmware not loaded to card%d.\n", board);
984 #endif          
985                 return -ENODEV;
986         }
987         
988         /*  open on a port greater than the port count for the card !!! */
989         if (line > ((board * 16) + card->port_count - 1)) {
990                 printk(KERN_ERR "ISICOM: Open on a port which exceeds the port_count of the card!\n");
991                 return -ENODEV;
992         }       
993         port = &isi_ports[line];        
994         if (isicom_paranoia_check(port, tty->name, "isicom_open"))
995                 return -ENODEV;
996                 
997 #ifdef ISICOM_DEBUG             
998         printk(KERN_DEBUG "ISICOM: isicom_setup_board ...\n");          
999 #endif  
1000         isicom_setup_board(card);               
1001         
1002         port->count++;
1003         tty->driver_data = port;
1004         port->tty = tty;
1005 #ifdef ISICOM_DEBUG     
1006         printk(KERN_DEBUG "ISICOM: isicom_setup_port ...\n");
1007 #endif  
1008         if ((error = isicom_setup_port(port))!=0)
1009                 return error;
1010 #ifdef ISICOM_DEBUG             
1011         printk(KERN_DEBUG "ISICOM: block_til_ready ...\n");     
1012 #endif  
1013         if ((error = block_til_ready(tty, filp, port))!=0)
1014                 return error;
1015
1016 #ifdef ISICOM_DEBUG     
1017         printk(KERN_DEBUG "ISICOM: open end!!!.\n");
1018 #endif  
1019         return 0;               
1020 }
1021  
1022 /* close et all */
1023
1024 static inline void isicom_shutdown_board(struct isi_board * bp)
1025 {
1026         int channel;
1027         struct isi_port * port;
1028         
1029         if (!(bp->status & BOARD_ACTIVE))
1030                 return;
1031         bp->status &= ~BOARD_ACTIVE;
1032         port = bp->ports;
1033         for(channel = 0; channel < bp->port_count; channel++, port++) {
1034                 drop_dtr_rts(port);
1035         }       
1036 }
1037
1038 static void isicom_shutdown_port(struct isi_port * port)
1039 {
1040         struct isi_board * card = port->card;
1041         struct tty_struct * tty;        
1042         
1043         if (!(port->flags & ASYNC_INITIALIZED))
1044                 return;
1045         if (port->xmit_buf) {
1046                 free_page((unsigned long) port->xmit_buf);
1047                 port->xmit_buf = NULL;
1048         }       
1049         if (!(tty = port->tty) || C_HUPCL(tty)) 
1050                 /* drop dtr on this port */
1051                 drop_dtr(port);
1052                 
1053         /* any other port uninits  */ 
1054         
1055         if (tty)
1056                 set_bit(TTY_IO_ERROR, &tty->flags);
1057         port->flags &= ~ASYNC_INITIALIZED;
1058         
1059         if (--card->count < 0) {
1060                 printk(KERN_DEBUG "ISICOM: isicom_shutdown_port: bad board(0x%x) count %d.\n",
1061                         card->base, card->count);
1062                 card->count = 0;        
1063         }
1064         
1065         /* last port was closed , shutdown that boad too */
1066         if (!card->count)
1067                 isicom_shutdown_board(card);
1068 }
1069
1070 static void isicom_close(struct tty_struct * tty, struct file * filp)
1071 {
1072         struct isi_port * port = (struct isi_port *) tty->driver_data;
1073         struct isi_board * card = port->card;
1074         unsigned long flags;
1075         
1076         if (!port)
1077                 return;
1078         if (isicom_paranoia_check(port, tty->name, "isicom_close"))
1079                 return;
1080         
1081 #ifdef ISICOM_DEBUG             
1082         printk(KERN_DEBUG "ISICOM: Close start!!!.\n");
1083 #endif  
1084         
1085         save_flags(flags); cli();
1086         if (tty_hung_up_p(filp)) {
1087                 restore_flags(flags);
1088                 return;
1089         }
1090         
1091         if ((tty->count == 1) && (port->count != 1)) {
1092                 printk(KERN_WARNING "ISICOM:(0x%x) isicom_close: bad port count"
1093                         "tty->count = 1 port count = %d.\n",
1094                         card->base, port->count);
1095                 port->count = 1;
1096         }
1097         if (--port->count < 0) {
1098                 printk(KERN_WARNING "ISICOM:(0x%x) isicom_close: bad port count for"
1099                         "channel%d = %d", card->base, port->channel, 
1100                         port->count);
1101                 port->count = 0;        
1102         }
1103         
1104         if (port->count) {
1105                 restore_flags(flags);
1106                 return;
1107         }       
1108         port->flags |= ASYNC_CLOSING;
1109         tty->closing = 1;
1110         if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1111                 tty_wait_until_sent(tty, port->closing_wait);
1112         /* indicate to the card that no more data can be received 
1113            on this port */
1114         if (port->flags & ASYNC_INITIALIZED) {   
1115                 card->port_status &= ~(1 << port->channel);
1116                 outw(card->port_status, card->base + 0x02);
1117         }       
1118         isicom_shutdown_port(port);
1119         if (tty->driver->flush_buffer)
1120                 tty->driver->flush_buffer(tty);
1121                 
1122         tty_ldisc_flush(tty);
1123         tty->closing = 0;
1124         port->tty = NULL;
1125         if (port->blocked_open) {
1126                 if (port->close_delay) {
1127 #ifdef ISICOM_DEBUG                     
1128                         printk(KERN_DEBUG "ISICOM: scheduling until time out.\n");
1129 #endif                  
1130                         msleep_interruptible(jiffies_to_msecs(port->close_delay));
1131                 }
1132                 wake_up_interruptible(&port->open_wait);
1133         }       
1134         port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1135         wake_up_interruptible(&port->close_wait);
1136         restore_flags(flags);
1137 #ifdef ISICOM_DEBUG     
1138         printk(KERN_DEBUG "ISICOM: Close end!!!.\n");
1139 #endif  
1140 }
1141
1142 /* write et all */
1143 static int isicom_write(struct tty_struct * tty,
1144                         const unsigned char * buf, int count)
1145 {
1146         struct isi_port * port = (struct isi_port *) tty->driver_data;
1147         unsigned long flags;
1148         int cnt, total = 0;
1149 #ifdef ISICOM_DEBUG
1150         printk(KERN_DEBUG "ISICOM: isicom_write for port%d: %d bytes.\n",
1151                         port->channel+1, count);
1152 #endif          
1153         if (isicom_paranoia_check(port, tty->name, "isicom_write"))
1154                 return 0;
1155         
1156         if (!tty || !port->xmit_buf || !tmp_buf)
1157                 return 0;
1158                 
1159         save_flags(flags);
1160         while(1) {      
1161                 cli();
1162                 cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1163                                             SERIAL_XMIT_SIZE - port->xmit_head));
1164                 if (cnt <= 0) 
1165                         break;
1166                 
1167                 memcpy(port->xmit_buf + port->xmit_head, buf, cnt);
1168                 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE - 1);
1169                 port->xmit_cnt += cnt;
1170                 restore_flags(flags);
1171                 buf += cnt;
1172                 count -= cnt;
1173                 total += cnt;
1174         }               
1175         if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1176                 port->status |= ISI_TXOK;
1177         restore_flags(flags);
1178 #ifdef ISICOM_DEBUG
1179         printk(KERN_DEBUG "ISICOM: isicom_write %d bytes written.\n", total);
1180 #endif          
1181         return total;   
1182 }
1183
1184 /* put_char et all */
1185 static void isicom_put_char(struct tty_struct * tty, unsigned char ch)
1186 {
1187         struct isi_port * port = (struct isi_port *) tty->driver_data;
1188         unsigned long flags;
1189         
1190         if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
1191                 return;
1192         
1193         if (!tty || !port->xmit_buf)
1194                 return;
1195 #ifdef ISICOM_DEBUG
1196         printk(KERN_DEBUG "ISICOM: put_char, port %d, char %c.\n", port->channel+1, ch);
1197 #endif                  
1198                 
1199         save_flags(flags); cli();
1200         
1201         if (port->xmit_cnt >= (SERIAL_XMIT_SIZE - 1)) {
1202                 restore_flags(flags);
1203                 return;
1204         }
1205         
1206         port->xmit_buf[port->xmit_head++] = ch;
1207         port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
1208         port->xmit_cnt++;
1209         restore_flags(flags);
1210 }
1211
1212 /* flush_chars et all */
1213 static void isicom_flush_chars(struct tty_struct * tty)
1214 {
1215         struct isi_port * port = (struct isi_port *) tty->driver_data;
1216         
1217         if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
1218                 return;
1219         
1220         if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1221             !port->xmit_buf)
1222                 return;
1223                 
1224         /* this tells the transmitter to consider this port for
1225            data output to the card ... that's the best we can do. */
1226         port->status |= ISI_TXOK;       
1227 }
1228
1229 /* write_room et all */
1230 static int isicom_write_room(struct tty_struct * tty)
1231 {
1232         struct isi_port * port = (struct isi_port *) tty->driver_data;
1233         int free;
1234         if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
1235                 return 0;
1236         
1237         free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1238         if (free < 0)
1239                 free = 0;
1240         return free;
1241 }
1242
1243 /* chars_in_buffer et all */
1244 static int isicom_chars_in_buffer(struct tty_struct * tty)
1245 {
1246         struct isi_port * port = (struct isi_port *) tty->driver_data;
1247         if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
1248                 return 0;
1249         return port->xmit_cnt;
1250 }
1251
1252 /* ioctl et all */
1253 static inline void isicom_send_break(struct isi_port * port, unsigned long length)
1254 {
1255         struct isi_board * card = port->card;
1256         short wait = 10;
1257         unsigned short base = card->base;       
1258         unsigned long flags;
1259         
1260         save_flags(flags); cli();
1261         while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));     
1262         if (!wait) {
1263                 printk(KERN_DEBUG "ISICOM: Card found busy in isicom_send_break.\n");
1264                 goto out;
1265         }       
1266         outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1267         outw((length & 0xff) << 8 | 0x00, base);
1268         outw((length & 0xff00), base);
1269         InterruptTheCard(base);
1270 out:    restore_flags(flags);
1271 }
1272
1273 static int isicom_tiocmget(struct tty_struct *tty, struct file *file)
1274 {
1275         struct isi_port * port = (struct isi_port *) tty->driver_data;
1276         /* just send the port status */
1277         unsigned short status = port->status;
1278
1279         if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1280                 return -ENODEV;
1281         
1282         return  ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1283                 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1284                 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1285                 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1286                 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1287                 ((status & ISI_RI ) ? TIOCM_RI  : 0);
1288 }
1289
1290 static int isicom_tiocmset(struct tty_struct *tty, struct file *file,
1291                            unsigned int set, unsigned int clear)
1292 {
1293         struct isi_port * port = (struct isi_port *) tty->driver_data;
1294         unsigned long flags;
1295         
1296         if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1297                 return -ENODEV;
1298         
1299         save_flags(flags); cli();
1300         if (set & TIOCM_RTS)
1301                 raise_rts(port);
1302         if (set & TIOCM_DTR)
1303                 raise_dtr(port);
1304
1305         if (clear & TIOCM_RTS)
1306                 drop_rts(port);
1307         if (clear & TIOCM_DTR)
1308                 drop_dtr(port);
1309
1310         restore_flags(flags);
1311         return 0;
1312 }                       
1313
1314 static int isicom_set_serial_info(struct isi_port * port,
1315                                         struct serial_struct __user *info)
1316 {
1317         struct serial_struct newinfo;
1318         unsigned long flags;
1319         int reconfig_port;
1320
1321         if(copy_from_user(&newinfo, info, sizeof(newinfo)))
1322                 return -EFAULT;
1323                 
1324         reconfig_port = ((port->flags & ASYNC_SPD_MASK) != 
1325                          (newinfo.flags & ASYNC_SPD_MASK));
1326         
1327         if (!capable(CAP_SYS_ADMIN)) {
1328                 if ((newinfo.close_delay != port->close_delay) ||
1329                     (newinfo.closing_wait != port->closing_wait) ||
1330                     ((newinfo.flags & ~ASYNC_USR_MASK) != 
1331                      (port->flags & ~ASYNC_USR_MASK)))
1332                         return -EPERM;
1333                 port->flags = ((port->flags & ~ ASYNC_USR_MASK) |
1334                                 (newinfo.flags & ASYNC_USR_MASK));
1335         }       
1336         else {
1337                 port->close_delay = newinfo.close_delay;
1338                 port->closing_wait = newinfo.closing_wait; 
1339                 port->flags = ((port->flags & ~ASYNC_FLAGS) | 
1340                                 (newinfo.flags & ASYNC_FLAGS));
1341         }
1342         if (reconfig_port) {
1343                 save_flags(flags); cli();
1344                 isicom_config_port(port);
1345                 restore_flags(flags);
1346         }
1347         return 0;                
1348 }               
1349
1350 static int isicom_get_serial_info(struct isi_port * port, 
1351                                         struct serial_struct __user *info)
1352 {
1353         struct serial_struct out_info;
1354         
1355         memset(&out_info, 0, sizeof(out_info));
1356 /*      out_info.type = ? */
1357         out_info.line = port - isi_ports;
1358         out_info.port = port->card->base;
1359         out_info.irq = port->card->irq;
1360         out_info.flags = port->flags;
1361 /*      out_info.baud_base = ? */
1362         out_info.close_delay = port->close_delay;
1363         out_info.closing_wait = port->closing_wait;
1364         if(copy_to_user(info, &out_info, sizeof(out_info)))
1365                 return -EFAULT;
1366         return 0;
1367 }                                       
1368
1369 static int isicom_ioctl(struct tty_struct * tty, struct file * filp,
1370                         unsigned int cmd, unsigned long arg) 
1371 {
1372         struct isi_port * port = (struct isi_port *) tty->driver_data;
1373         void __user *argp = (void __user *)arg;
1374         int retval;
1375
1376         if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1377                 return -ENODEV;
1378
1379         switch(cmd) {
1380                 case TCSBRK:
1381                         retval = tty_check_change(tty);
1382                         if (retval)
1383                                 return retval;
1384                         tty_wait_until_sent(tty, 0);
1385                         if (!arg)
1386                                 isicom_send_break(port, HZ/4);
1387                         return 0;
1388                         
1389                 case TCSBRKP:   
1390                         retval = tty_check_change(tty);
1391                         if (retval)
1392                                 return retval;
1393                         tty_wait_until_sent(tty, 0);
1394                         isicom_send_break(port, arg ? arg * (HZ/10) : HZ/4);
1395                         return 0;
1396                         
1397                 case TIOCGSOFTCAR:
1398                         return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1399                         
1400                 case TIOCSSOFTCAR:
1401                         if(get_user(arg, (unsigned long __user *) argp))
1402                                 return -EFAULT;
1403                         tty->termios->c_cflag =
1404                                 ((tty->termios->c_cflag & ~CLOCAL) |
1405                                 (arg ? CLOCAL : 0));
1406                         return 0;       
1407                         
1408                 case TIOCGSERIAL:
1409                         return isicom_get_serial_info(port, argp);
1410                 
1411                 case TIOCSSERIAL:
1412                         return isicom_set_serial_info(port, argp);
1413                                         
1414                 default:
1415                         return -ENOIOCTLCMD;                                            
1416         }
1417         return 0;
1418 }
1419
1420 /* set_termios et all */
1421 static void isicom_set_termios(struct tty_struct * tty, struct termios * old_termios)
1422 {
1423         struct isi_port * port = (struct isi_port *) tty->driver_data;
1424         unsigned long flags;
1425         
1426         if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
1427                 return;
1428         
1429         if (tty->termios->c_cflag == old_termios->c_cflag &&
1430             tty->termios->c_iflag == old_termios->c_iflag)
1431                 return;
1432                 
1433         save_flags(flags); cli();
1434         isicom_config_port(port);
1435         restore_flags(flags);
1436         
1437         if ((old_termios->c_cflag & CRTSCTS) &&
1438             !(tty->termios->c_cflag & CRTSCTS)) {       
1439                 tty->hw_stopped = 0;
1440                 isicom_start(tty);   
1441         }    
1442 }
1443
1444 /* throttle et all */
1445 static void isicom_throttle(struct tty_struct * tty)
1446 {
1447         struct isi_port * port = (struct isi_port *) tty->driver_data;
1448         struct isi_board * card = port->card;
1449         unsigned long flags;
1450         
1451         if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
1452                 return;
1453         
1454         /* tell the card that this port cannot handle any more data for now */
1455         save_flags(flags); cli();
1456         card->port_status &= ~(1 << port->channel);
1457         outw(card->port_status, card->base + 0x02);
1458         restore_flags(flags);
1459 }
1460
1461 /* unthrottle et all */
1462 static void isicom_unthrottle(struct tty_struct * tty)
1463 {
1464         struct isi_port * port = (struct isi_port *) tty->driver_data;
1465         struct isi_board * card = port->card;
1466         unsigned long flags;
1467         
1468         if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
1469                 return;
1470         
1471         /* tell the card that this port is ready to accept more data */
1472         save_flags(flags); cli();
1473         card->port_status |= (1 << port->channel);
1474         outw(card->port_status, card->base + 0x02);
1475         restore_flags(flags);
1476 }
1477
1478 /* stop et all */
1479 static void isicom_stop(struct tty_struct * tty)
1480 {
1481         struct isi_port * port = (struct isi_port *) tty->driver_data;
1482
1483         if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
1484                 return;
1485         
1486         /* this tells the transmitter not to consider this port for
1487            data output to the card. */
1488         port->status &= ~ISI_TXOK;
1489 }
1490
1491 /* start et all */
1492 static void isicom_start(struct tty_struct * tty)
1493 {
1494         struct isi_port * port = (struct isi_port *) tty->driver_data;
1495         
1496         if (isicom_paranoia_check(port, tty->name, "isicom_start"))
1497                 return;
1498         
1499         /* this tells the transmitter to consider this port for
1500            data output to the card. */
1501         port->status |= ISI_TXOK;
1502 }
1503
1504 /* hangup et all */
1505 static void do_isicom_hangup(void * data)
1506 {
1507         struct isi_port * port = (struct isi_port *) data;
1508         struct tty_struct * tty;
1509         
1510         tty = port->tty;
1511         if (tty)
1512                 tty_hangup(tty);        /* FIXME: module removal race here - AKPM */
1513 }
1514
1515 static void isicom_hangup(struct tty_struct * tty)
1516 {
1517         struct isi_port * port = (struct isi_port *) tty->driver_data;
1518         
1519         if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
1520                 return;
1521         
1522         isicom_shutdown_port(port);
1523         port->count = 0;
1524         port->flags &= ~ASYNC_NORMAL_ACTIVE;
1525         port->tty = NULL;
1526         wake_up_interruptible(&port->open_wait);
1527 }
1528
1529 /* flush_buffer et all */
1530 static void isicom_flush_buffer(struct tty_struct * tty)
1531 {
1532         struct isi_port * port = (struct isi_port *) tty->driver_data;
1533         unsigned long flags;
1534         
1535         if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
1536                 return;
1537         
1538         save_flags(flags); cli();
1539         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1540         restore_flags(flags);
1541         
1542         wake_up_interruptible(&tty->write_wait);
1543         tty_wakeup(tty);
1544 }
1545
1546
1547 static int register_ioregion(void)
1548 {
1549         int count, done=0;
1550         for (count=0; count < BOARD_COUNT; count++ ) {
1551                 if (isi_card[count].base)
1552                         if (!request_region(isi_card[count].base,16,ISICOM_NAME)) {
1553                                 printk(KERN_DEBUG "ISICOM: I/O Region 0x%x-0x%x is busy. Card%d will be disabled.\n",
1554                                         isi_card[count].base,isi_card[count].base+15,count+1);
1555                                 isi_card[count].base=0;
1556                                 done++;
1557                         }
1558         }
1559         return done;
1560 }
1561
1562 static void unregister_ioregion(void)
1563 {
1564         int count;
1565         for (count=0; count < BOARD_COUNT; count++ ) 
1566                 if (isi_card[count].base) {
1567                         release_region(isi_card[count].base,16);
1568 #ifdef ISICOM_DEBUG                     
1569                         printk(KERN_DEBUG "ISICOM: I/O Region 0x%x-0x%x released for Card%d.\n",isi_card[count].base,isi_card[count].base+15,count+1);
1570 #endif                  
1571                 }
1572 }
1573
1574 static struct tty_operations isicom_ops = {
1575         .open   = isicom_open,
1576         .close  = isicom_close,
1577         .write  = isicom_write,
1578         .put_char       = isicom_put_char,
1579         .flush_chars    = isicom_flush_chars,
1580         .write_room     = isicom_write_room,
1581         .chars_in_buffer        = isicom_chars_in_buffer,
1582         .ioctl  = isicom_ioctl,
1583         .set_termios    = isicom_set_termios,
1584         .throttle       = isicom_throttle,
1585         .unthrottle     = isicom_unthrottle,
1586         .stop   = isicom_stop,
1587         .start  = isicom_start,
1588         .hangup = isicom_hangup,
1589         .flush_buffer   = isicom_flush_buffer,
1590         .tiocmget       = isicom_tiocmget,
1591         .tiocmset       = isicom_tiocmset,
1592 };
1593
1594 static int register_drivers(void)
1595 {
1596         int error;
1597
1598         /* tty driver structure initialization */
1599         isicom_normal = alloc_tty_driver(PORT_COUNT);
1600         if (!isicom_normal)
1601                 return -ENOMEM;
1602
1603         isicom_normal->owner    = THIS_MODULE;
1604         isicom_normal->name     = "ttyM";
1605         isicom_normal->devfs_name = "isicom/";
1606         isicom_normal->major    = ISICOM_NMAJOR;
1607         isicom_normal->minor_start      = 0;
1608         isicom_normal->type     = TTY_DRIVER_TYPE_SERIAL;
1609         isicom_normal->subtype  = SERIAL_TYPE_NORMAL;
1610         isicom_normal->init_termios     = tty_std_termios;
1611         isicom_normal->init_termios.c_cflag     = 
1612                                 B9600 | CS8 | CREAD | HUPCL |CLOCAL;
1613         isicom_normal->flags    = TTY_DRIVER_REAL_RAW;
1614         tty_set_operations(isicom_normal, &isicom_ops);
1615         
1616         if ((error=tty_register_driver(isicom_normal))!=0) {
1617                 printk(KERN_DEBUG "ISICOM: Couldn't register the dialin driver, error=%d\n",
1618                         error);
1619                 put_tty_driver(isicom_normal);
1620                 return error;
1621         }
1622         return 0;
1623 }
1624
1625 static void unregister_drivers(void)
1626 {
1627         int error = tty_unregister_driver(isicom_normal);
1628         if (error)
1629                 printk(KERN_DEBUG "ISICOM: couldn't unregister normal driver error=%d.\n",error);
1630         put_tty_driver(isicom_normal);
1631 }
1632
1633 static int register_isr(void)
1634 {
1635         int count, done=0, card;
1636         int flag;
1637         unsigned char request;
1638         for (count=0; count < BOARD_COUNT; count++ ) {
1639                 if (isi_card[count].base) {
1640                 /*
1641                  * verify if the required irq has already been requested for
1642                  * another ISI Card, if so we already have it, else request it
1643                  */
1644                         request = YES;
1645                         for(card = 0; card < count; card++)
1646                         if ((isi_card[card].base) && (isi_card[card].irq == isi_card[count].irq)) {
1647                                 request = NO;
1648                                 if ((isi_card[count].isa == NO) && (isi_card[card].isa == NO))
1649                                         break;
1650                                 /*
1651                                  * ISA cards cannot share interrupts with other
1652                                  * PCI or ISA devices hence disable this card.
1653                                  */
1654                                 release_region(isi_card[count].base,16);
1655                                 isi_card[count].base = 0;
1656                                 break;
1657                         }
1658                         flag=0;
1659                         if(isi_card[count].isa == NO)
1660                                 flag |= SA_SHIRQ;
1661                                 
1662                         if (request == YES) { 
1663                                 if (request_irq(isi_card[count].irq, isicom_interrupt, SA_INTERRUPT|flag, ISICOM_NAME, NULL)) {
1664                                         printk(KERN_WARNING "ISICOM: Could not install handler at Irq %d. Card%d will be disabled.\n",
1665                                                 isi_card[count].irq, count+1);
1666                                         release_region(isi_card[count].base,16);
1667                                         isi_card[count].base=0;
1668                                 }
1669                                 else {
1670                                         printk(KERN_INFO "ISICOM: Card%d at 0x%x using irq %d.\n", 
1671                                         count+1, isi_card[count].base, isi_card[count].irq); 
1672                                         
1673                                         irq_to_board[isi_card[count].irq]=&isi_card[count];
1674                                         done++;
1675                                 }
1676                         }
1677                 }       
1678         }
1679         return done;
1680 }
1681
1682 static void unregister_isr(void)
1683 {
1684         int count, card;
1685         unsigned char freeirq;
1686         for (count=0; count < BOARD_COUNT; count++ ) {
1687                 if (isi_card[count].base) {
1688                         freeirq = YES;
1689                         for(card = 0; card < count; card++)
1690                                 if ((isi_card[card].base) && (isi_card[card].irq == isi_card[count].irq)) {
1691                                         freeirq = NO;
1692                                         break;
1693                                 }
1694                         if (freeirq == YES) {
1695                                 free_irq(isi_card[count].irq, NULL);
1696 #ifdef ISICOM_DEBUG                     
1697                                 printk(KERN_DEBUG "ISICOM: Irq %d released for Card%d.\n",isi_card[count].irq, count+1);
1698 #endif  
1699                         }               
1700                 }
1701         }
1702 }
1703
1704 static int isicom_init(void)
1705 {
1706         int card, channel, base;
1707         struct isi_port * port;
1708         unsigned long page;
1709         
1710         if (!tmp_buf) { 
1711                 page = get_zeroed_page(GFP_KERNEL);
1712                 if (!page) {
1713 #ifdef ISICOM_DEBUG             
1714                         printk(KERN_DEBUG "ISICOM: Couldn't allocate page for tmp_buf.\n");
1715 #else
1716                         printk(KERN_ERR "ISICOM: Not enough memory...\n");
1717 #endif        
1718                         return 0;
1719                 }       
1720                 tmp_buf = (unsigned char *) page;
1721         }
1722         
1723         if (!register_ioregion()) 
1724         {
1725                 printk(KERN_ERR "ISICOM: All required I/O space found busy.\n");
1726                 free_page((unsigned long)tmp_buf);
1727                 return 0;
1728         }
1729         if (register_drivers()) 
1730         {
1731                 unregister_ioregion();
1732                 free_page((unsigned long)tmp_buf);
1733                 return 0;
1734         }
1735         if (!register_isr()) 
1736         {
1737                 unregister_drivers();
1738                 /*  ioports already uregistered in register_isr */
1739                 free_page((unsigned long)tmp_buf);
1740                 return 0;               
1741         }
1742         
1743         memset(isi_ports, 0, sizeof(isi_ports));
1744         for (card = 0; card < BOARD_COUNT; card++) {
1745                 port = &isi_ports[card * 16];
1746                 isi_card[card].ports = port;
1747                 base = isi_card[card].base;
1748                 for (channel = 0; channel < 16; channel++, port++) {
1749                         port->magic = ISICOM_MAGIC;
1750                         port->card = &isi_card[card];
1751                         port->channel = channel;                
1752                         port->close_delay = 50 * HZ/100;
1753                         port->closing_wait = 3000 * HZ/100;
1754                         INIT_WORK(&port->hangup_tq, do_isicom_hangup, port);
1755                         INIT_WORK(&port->bh_tqueue, isicom_bottomhalf, port);
1756                         port->status = 0;
1757                         init_waitqueue_head(&port->open_wait);                                  
1758                         init_waitqueue_head(&port->close_wait);
1759                         /*  . . .  */
1760                 }
1761         } 
1762         
1763         return 1;       
1764 }
1765
1766 /*
1767  *      Insmod can set static symbols so keep these static
1768  */
1769  
1770 static int io[4];
1771 static int irq[4];
1772
1773 MODULE_AUTHOR("MultiTech");
1774 MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
1775 MODULE_LICENSE("GPL");
1776 module_param_array(io, int, NULL, 0);
1777 MODULE_PARM_DESC(io, "I/O ports for the cards");
1778 module_param_array(irq, int, NULL, 0);
1779 MODULE_PARM_DESC(irq, "Interrupts for the cards");
1780
1781 int init_module(void)
1782 {
1783         struct pci_dev *dev = NULL;
1784         int retval, card, idx, count;
1785         unsigned char pciirq;
1786         unsigned int ioaddr;
1787                         
1788         card = 0;
1789         for(idx=0; idx < BOARD_COUNT; idx++) {  
1790                 if (io[idx]) {
1791                         isi_card[idx].base=io[idx];
1792                         isi_card[idx].irq=irq[idx];
1793                         isi_card[idx].isa=YES;
1794                         card++;
1795                 }
1796                 else {
1797                         isi_card[idx].base = 0;
1798                         isi_card[idx].irq = 0;
1799                 }
1800         }
1801         
1802         for (idx=0 ;idx < card; idx++) {
1803                 if (!((isi_card[idx].irq==2)||(isi_card[idx].irq==3)||
1804                     (isi_card[idx].irq==4)||(isi_card[idx].irq==5)||
1805                     (isi_card[idx].irq==7)||(isi_card[idx].irq==10)||
1806                     (isi_card[idx].irq==11)||(isi_card[idx].irq==12)||
1807                     (isi_card[idx].irq==15))) {
1808                         
1809                         if (isi_card[idx].base) {
1810                                 printk(KERN_ERR "ISICOM: Irq %d unsupported. Disabling Card%d...\n",
1811                                         isi_card[idx].irq, idx+1);
1812                                 isi_card[idx].base=0;
1813                                 card--;
1814                         }       
1815                 }
1816         }       
1817         
1818         if (card < BOARD_COUNT) {
1819                 for (idx=0; idx < DEVID_COUNT; idx++) {
1820                         dev = NULL;
1821                         for (;;){
1822                                 if (!(dev = pci_find_device(VENDOR_ID, isicom_pci_tbl[idx].device, dev)))
1823                                         break;
1824                                 if (card >= BOARD_COUNT)
1825                                         break;
1826                                         
1827                                 if (pci_enable_device(dev))
1828                                         break;
1829
1830                                 /* found a PCI ISI card! */
1831                                 ioaddr = pci_resource_start (dev, 3); /* i.e at offset 0x1c in the
1832                                                                        * PCI configuration register
1833                                                                        * space.
1834                                                                        */
1835                                 pciirq = dev->irq;
1836                                 printk(KERN_INFO "ISI PCI Card(Device ID 0x%x)\n", isicom_pci_tbl[idx].device);
1837                                 /*
1838                                  * allot the first empty slot in the array
1839                                  */                             
1840                                 for (count=0; count < BOARD_COUNT; count++) {                           
1841                                         if (isi_card[count].base == 0) {
1842                                                 isi_card[count].base = ioaddr;
1843                                                 isi_card[count].irq = pciirq;
1844                                                 isi_card[count].isa = NO;
1845                                                 card++;
1846                                                 break;
1847                                         }
1848                                 }
1849                         }                               
1850                         if (card >= BOARD_COUNT) break;
1851                 }
1852         }
1853         
1854         if (!(isi_card[0].base || isi_card[1].base || isi_card[2].base || isi_card[3].base)) {
1855                 printk(KERN_ERR "ISICOM: No valid card configuration. Driver cannot be initialized...\n"); 
1856                 return -EIO;
1857         }       
1858
1859         retval = misc_register(&isiloader_device);
1860         if (retval < 0) {
1861                 printk(KERN_ERR "ISICOM: Unable to register firmware loader driver.\n");
1862                 return retval;
1863         }
1864         
1865         if (!isicom_init()) {
1866                 if (misc_deregister(&isiloader_device)) 
1867                         printk(KERN_ERR "ISICOM: Unable to unregister Firmware Loader driver\n");
1868                 return -EIO;
1869         }
1870         
1871         init_timer(&tx);
1872         tx.expires = jiffies + 1;
1873         tx.data = 0;
1874         tx.function = isicom_tx;
1875         re_schedule = 1;
1876         add_timer(&tx);
1877         
1878         return 0;
1879 }
1880
1881 void cleanup_module(void)
1882 {
1883         re_schedule = 0;
1884         msleep(1000);
1885
1886 #ifdef ISICOM_DEBUG     
1887         printk("ISICOM: isicom_tx tx_count = %ld.\n", tx_count);
1888 #endif  
1889
1890 #ifdef ISICOM_DEBUG
1891         printk("ISICOM: uregistering isr ...\n");
1892 #endif  
1893         unregister_isr();
1894
1895 #ifdef ISICOM_DEBUG     
1896         printk("ISICOM: unregistering drivers ...\n");
1897 #endif
1898         unregister_drivers();
1899         
1900 #ifdef ISICOM_DEBUG     
1901         printk("ISICOM: unregistering ioregion ...\n");
1902 #endif  
1903         unregister_ioregion();  
1904         
1905 #ifdef ISICOM_DEBUG     
1906         printk("ISICOM: freeing tmp_buf ...\n");
1907 #endif  
1908         free_page((unsigned long)tmp_buf);
1909         
1910 #ifdef ISICOM_DEBUG             
1911         printk("ISICOM: unregistering firmware loader ...\n");  
1912 #endif
1913         if (misc_deregister(&isiloader_device))
1914                 printk(KERN_ERR "ISICOM: Unable to unregister Firmware Loader driver\n");
1915 }