ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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/ioport.h>
52
53 #include <asm/uaccess.h>
54 #include <asm/io.h>
55 #include <asm/system.h>
56
57 #include <linux/pci.h>
58
59 #include <linux/isicom.h>
60
61 static struct pci_device_id isicom_pci_tbl[] = {
62         { VENDOR_ID, 0x2028, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
63         { VENDOR_ID, 0x2051, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
64         { VENDOR_ID, 0x2052, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
65         { VENDOR_ID, 0x2053, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
66         { VENDOR_ID, 0x2054, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
67         { VENDOR_ID, 0x2055, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
68         { VENDOR_ID, 0x2056, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
69         { VENDOR_ID, 0x2057, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
70         { VENDOR_ID, 0x2058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
71         { 0 }
72 };
73 MODULE_DEVICE_TABLE(pci, isicom_pci_tbl);
74
75 static int prev_card = 3;       /*      start servicing isi_card[0]     */
76 static struct isi_board * irq_to_board[16];
77 static struct tty_driver *isicom_normal;
78
79 static struct isi_board isi_card[BOARD_COUNT];
80 static struct isi_port  isi_ports[PORT_COUNT];
81
82 static struct timer_list tx;
83 static char re_schedule = 1;
84 #ifdef ISICOM_DEBUG
85 static unsigned long tx_count = 0;
86 #endif
87
88 static int ISILoad_ioctl(struct inode *inode, struct file *filp, unsigned  int cmd, unsigned long arg);
89
90 static void isicom_tx(unsigned long _data);
91 static void isicom_start(struct tty_struct * tty);
92
93 static unsigned char * tmp_buf = 0;
94 static DECLARE_MUTEX(tmp_buf_sem);
95
96 /*   baud index mappings from linux defns to isi */
97
98 static signed char linuxb_to_isib[] = {
99         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17,     
100         18, 19
101 };
102
103 /* 
104  *  Firmware loader driver specific routines
105  *
106  */
107
108 static struct file_operations ISILoad_fops = {
109         .owner          = THIS_MODULE,
110         .ioctl          = ISILoad_ioctl,
111 };
112
113 struct miscdevice isiloader_device = {
114         ISILOAD_MISC_MINOR, "isictl", &ISILoad_fops
115 };
116
117  
118 static inline int WaitTillCardIsFree(unsigned short base)
119 {
120         unsigned long count=0;
121         while( (!(inw(base+0xe) & 0x1)) && (count++ < 6000000));
122         if (inw(base+0xe)&0x1)  
123                 return 0;
124         else
125                 return 1;
126 }
127
128 static int ISILoad_ioctl(struct inode *inode, struct file *filp,
129                          unsigned int cmd, unsigned long arg)
130 {
131         unsigned int card, i, j, signature, status, portcount = 0;
132         unsigned long t;
133         unsigned short word_count, base;
134         bin_frame frame;
135         /* exec_record exec_rec; */
136         
137         if(get_user(card, (int *)arg))
138                 return -EFAULT;
139                 
140         if(card < 0 || card >= BOARD_COUNT)
141                 return -ENXIO;
142                 
143         base=isi_card[card].base;
144         
145         if(base==0)
146                 return -ENXIO;  /* disabled or not used */
147         
148         switch(cmd) {
149                 case MIOCTL_RESET_CARD:
150                         if (!capable(CAP_SYS_ADMIN))
151                                 return -EPERM;
152                         printk(KERN_DEBUG "ISILoad:Resetting Card%d at 0x%x ",card+1,base);
153                                                                 
154                         inw(base+0x8);
155                         
156                         for(t=jiffies+HZ/100;time_before(jiffies, t););
157                                 
158                         outw(0,base+0x8); /* Reset */
159                         
160                         for(j=1;j<=3;j++) {
161                                 for(t=jiffies+HZ;time_before(jiffies, t););
162                                 printk(".");
163                         }       
164                         signature=(inw(base+0x4)) & 0xff;       
165                         if (isi_card[card].isa) {
166                                         
167                                 if (!(inw(base+0xe) & 0x1) || (inw(base+0x2))) {
168 #ifdef ISICOM_DEBUG                             
169                                         printk("\nbase+0x2=0x%x , base+0xe=0x%x",inw(base+0x2),inw(base+0xe));
170 #endif                          
171                                         printk("\nISILoad:ISA Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
172                                         return -EIO;                                    
173                                 }
174                         }       
175                         else {
176                                 portcount = inw(base+0x2);
177                                 if (!(inw(base+0xe) & 0x1) || ((portcount!=0) && (portcount!=4) && (portcount!=8))) {   
178 #ifdef ISICOM_DEBUG
179                                         printk("\nbase+0x2=0x%x , base+0xe=0x%x",inw(base+0x2),inw(base+0xe));
180 #endif
181                                         printk("\nISILoad:PCI Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
182                                         return -EIO;
183                                 }
184                         }       
185                         switch(signature) {
186                         case    0xa5:
187                         case    0xbb:
188                         case    0xdd:   
189                                         if (isi_card[card].isa) 
190                                                 isi_card[card].port_count = 8;
191                                         else {
192                                                 if (portcount == 4)
193                                                         isi_card[card].port_count = 4;
194                                                 else
195                                                         isi_card[card].port_count = 8;
196                                         }       
197                                         isi_card[card].shift_count = 12;
198                                         break;
199                                         
200                         case    0xcc:   isi_card[card].port_count = 16;
201                                         isi_card[card].shift_count = 11;
202                                         break;                          
203                                         
204                         default: printk("ISILoad:Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
205 #ifdef ISICOM_DEBUG                     
206                                  printk("Sig=0x%x\n",signature);
207 #endif                           
208                                  return -EIO;
209                         }
210                         printk("-Done\n");
211                         return put_user(signature,(unsigned int*)arg);
212                                                 
213         case    MIOCTL_LOAD_FIRMWARE:
214                         if (!capable(CAP_SYS_ADMIN))
215                                 return -EPERM;
216                                 
217                         if(copy_from_user(&frame, (void *) arg, sizeof(bin_frame)))
218                                 return -EFAULT;
219                         
220                         if (WaitTillCardIsFree(base))
221                                 return -EIO;
222                         
223                         outw(0xf0,base);        /* start upload sequence */ 
224                         outw(0x00,base);
225                         outw((frame.addr), base);/*      lsb of adderess    */
226                         
227                         word_count=(frame.count >> 1) + frame.count % 2;
228                         outw(word_count, base);
229                         InterruptTheCard(base);
230                         
231                         for(i=0;i<=0x2f;i++);   /* a wee bit of delay */
232                         
233                         if (WaitTillCardIsFree(base)) 
234                                 return -EIO;
235                                 
236                         if ((status=inw(base+0x4))!=0) {
237                                 printk(KERN_WARNING "ISILoad:Card%d rejected load header:\nAddress:0x%x \nCount:0x%x \nStatus:0x%x \n", 
238                                 card+1, frame.addr, frame.count, status);
239                                 return -EIO;
240                         }
241                         outsw(base, (void *) frame.bin_data, word_count);
242                         
243                         InterruptTheCard(base);
244                         
245                         for(i=0;i<=0x0f;i++);   /* another wee bit of delay */ 
246                         
247                         if (WaitTillCardIsFree(base)) 
248                                 return -EIO;
249                                 
250                         if ((status=inw(base+0x4))!=0) {
251                                 printk(KERN_ERR "ISILoad:Card%d got out of sync.Card Status:0x%x\n",card+1, status);
252                                 return -EIO;
253                         }       
254                         return 0;
255                                                 
256         case    MIOCTL_READ_FIRMWARE:
257                         if (!capable(CAP_SYS_ADMIN))
258                                 return -EPERM;
259                                 
260                         if(copy_from_user(&frame, (void *) arg, sizeof(bin_header)))
261                                 return -EFAULT;
262                         
263                         if (WaitTillCardIsFree(base))
264                                 return -EIO;
265                         
266                         outw(0xf1,base);        /* start download sequence */ 
267                         outw(0x00,base);
268                         outw((frame.addr), base);/*      lsb of adderess    */
269                         
270                         word_count=(frame.count >> 1) + frame.count % 2;
271                         outw(word_count+1, base);
272                         InterruptTheCard(base);
273                         
274                         for(i=0;i<=0xf;i++);    /* a wee bit of delay */
275                         
276                         if (WaitTillCardIsFree(base)) 
277                                 return -EIO;
278                                 
279                         if ((status=inw(base+0x4))!=0) {
280                                 printk(KERN_WARNING "ISILoad:Card%d rejected verify header:\nAddress:0x%x \nCount:0x%x \nStatus:0x%x \n", 
281                                 card+1, frame.addr, frame.count, status);
282                                 return -EIO;
283                         }
284                         
285                         inw(base);
286                         insw(base, frame.bin_data, word_count);
287                         InterruptTheCard(base);
288                         
289                         for(i=0;i<=0x0f;i++);   /* another wee bit of delay */ 
290                         
291                         if (WaitTillCardIsFree(base)) 
292                                 return -EIO;
293                                 
294                         if ((status=inw(base+0x4))!=0) {
295                                 printk(KERN_ERR "ISILoad:Card%d verify got out of sync.Card Status:0x%x\n",card+1, status);
296                                 return -EIO;
297                         }       
298                         
299                         if(copy_to_user((void *) arg, &frame, sizeof(bin_frame)))
300                                 return -EFAULT;
301                         return 0;
302         
303         case    MIOCTL_XFER_CTRL:
304                         if (!capable(CAP_SYS_ADMIN))
305                                 return -EPERM;
306                         if (WaitTillCardIsFree(base)) 
307                                 return -EIO;
308                                         
309                         outw(0xf2, base);
310                         outw(0x800, base);
311                         outw(0x0, base);
312                         outw(0x0, base);
313                         InterruptTheCard(base);
314                         outw(0x0, base+0x4);    /* for ISI4608 cards */
315                                                         
316                         isi_card[card].status |= FIRMWARE_LOADED;
317                         return 0;       
318                         
319         default:
320 #ifdef ISICOM_DEBUG     
321                 printk(KERN_DEBUG "ISILoad: Received Ioctl cmd 0x%x.\n", cmd); 
322 #endif
323                 return -ENOIOCTLCMD;
324         
325         }
326         
327 }
328                                 
329
330 /*
331  *      ISICOM Driver specific routines ...
332  *
333  */
334  
335 static inline int isicom_paranoia_check(struct isi_port const * port, char *name, 
336                                         const char * routine)
337 {
338 #ifdef ISICOM_DEBUG 
339         static const char * badmagic = 
340                         KERN_WARNING "ISICOM: Warning: bad isicom magic for dev %s in %s.\n";
341         static const char * badport = 
342                         KERN_WARNING "ISICOM: Warning: NULL isicom port for dev %s in %s.\n";           
343         if (!port) {
344                 printk(badport, name, routine);
345                 return 1;
346         }
347         if (port->magic != ISICOM_MAGIC) {
348                 printk(badmagic, name, routine);
349                 return 1;
350         }       
351 #endif  
352         return 0;
353 }
354                         
355 /*      Transmitter     */
356
357 static void isicom_tx(unsigned long _data)
358 {
359         short count = (BOARD_COUNT-1), card, base;
360         short txcount, wait, wrd, residue, word_count, cnt;
361         struct isi_port * port;
362         struct tty_struct * tty;
363         unsigned long flags;
364         
365 #ifdef ISICOM_DEBUG
366         ++tx_count;
367 #endif  
368         
369         /*      find next active board  */
370         card = (prev_card + 1) & 0x0003;
371         while(count-- > 0) {
372                 if (isi_card[card].status & BOARD_ACTIVE) 
373                         break;
374                 card = (card + 1) & 0x0003;     
375         }
376         if (!(isi_card[card].status & BOARD_ACTIVE))
377                 goto sched_again;
378                 
379         prev_card = card;
380         
381         count = isi_card[card].port_count;
382         port = isi_card[card].ports;
383         base = isi_card[card].base;
384         for (;count > 0;count--, port++) {
385                 /* port not active or tx disabled to force flow control */
386                 if (!(port->status & ISI_TXOK))
387                         continue;
388                 
389                 tty = port->tty;
390                 save_flags(flags); cli();
391                 txcount = MIN(TX_SIZE, port->xmit_cnt);
392                 if ((txcount <= 0) || tty->stopped || tty->hw_stopped) {
393                         restore_flags(flags);
394                         continue;
395                 }
396                 wait = 200;     
397                 while(((inw(base+0x0e) & 0x01) == 0) && (wait-- > 0));
398                 if (wait <= 0) {
399                         restore_flags(flags);
400 #ifdef ISICOM_DEBUG
401                         printk(KERN_DEBUG "ISICOM: isicom_tx:Card(0x%x) found busy.\n",
402                                 card);
403 #endif
404                         continue;
405                 }
406                 if (!(inw(base + 0x02) & (1 << port->channel))) {
407                         restore_flags(flags);
408 #ifdef ISICOM_DEBUG                                     
409                         printk(KERN_DEBUG "ISICOM: isicom_tx: cannot tx to 0x%x:%d.\n",
410                                         base, port->channel + 1);
411 #endif                                  
412                         continue;               
413                 }
414 #ifdef ISICOM_DEBUG
415                 printk(KERN_DEBUG "ISICOM: txing %d bytes, port%d.\n", 
416                                 txcount, port->channel+1); 
417 #endif  
418                 outw((port->channel << isi_card[card].shift_count) | txcount
419                                         , base);
420                 residue = NO;
421                 wrd = 0;                        
422                 while (1) {
423                         cnt = MIN(txcount, (SERIAL_XMIT_SIZE - port->xmit_tail));
424                         if (residue == YES) {
425                                 residue = NO;
426                                 if (cnt > 0) {
427                                         wrd |= (port->xmit_buf[port->xmit_tail] << 8);
428                                         port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
429                                         port->xmit_cnt--;
430                                         txcount--;
431                                         cnt--;
432                                         outw(wrd, base);                        
433                                 }
434                                 else {
435                                         outw(wrd, base);
436                                         break;
437                                 }
438                         }               
439                         if (cnt <= 0) break;
440                         word_count = cnt >> 1;
441                         outsw(base, port->xmit_buf+port->xmit_tail, word_count);
442                         port->xmit_tail = (port->xmit_tail + (word_count << 1)) &
443                                                 (SERIAL_XMIT_SIZE - 1);
444                         txcount -= (word_count << 1);
445                         port->xmit_cnt -= (word_count << 1);
446                         if (cnt & 0x0001) {
447                                 residue = YES;
448                                 wrd = port->xmit_buf[port->xmit_tail];
449                                 port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
450                                 port->xmit_cnt--;
451                                 txcount--;
452                         }
453                 }
454
455                 InterruptTheCard(base);
456                 if (port->xmit_cnt <= 0)
457                         port->status &= ~ISI_TXOK;
458                 if (port->xmit_cnt <= WAKEUP_CHARS)
459                         schedule_work(&port->bh_tqueue);
460                 restore_flags(flags);
461         }       
462
463                 /*      schedule another tx for hopefully in about 10ms */      
464 sched_again:    
465         if (!re_schedule)       
466                 return;
467         init_timer(&tx);
468         tx.expires = jiffies + HZ/100;
469         tx.data = 0;
470         tx.function = isicom_tx;
471         add_timer(&tx);
472         
473         return; 
474 }               
475  
476 /*      Interrupt handlers      */
477
478  
479 static void isicom_bottomhalf(void * data)
480 {
481         struct isi_port * port = (struct isi_port *) data;
482         struct tty_struct * tty = port->tty;
483         
484         if (!tty)
485                 return;
486         
487         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
488             tty->ldisc.write_wakeup)
489                 (tty->ldisc.write_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(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         if (tty->ldisc.flush_buffer)
1122                 tty->ldisc.flush_buffer(tty);
1123         tty->closing = 0;
1124         port->tty = 0;
1125         if (port->blocked_open) {
1126                 if (port->close_delay) {
1127                         set_current_state(TASK_INTERRUPTIBLE);
1128 #ifdef ISICOM_DEBUG                     
1129                         printk(KERN_DEBUG "ISICOM: scheduling until time out.\n");
1130 #endif                  
1131                         schedule_timeout(port->close_delay);
1132                 }
1133                 wake_up_interruptible(&port->open_wait);
1134         }       
1135         port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1136         wake_up_interruptible(&port->close_wait);
1137         restore_flags(flags);
1138 #ifdef ISICOM_DEBUG     
1139         printk(KERN_DEBUG "ISICOM: Close end!!!.\n");
1140 #endif  
1141 }
1142
1143 /* write et all */
1144 static int isicom_write(struct tty_struct * tty, int from_user,
1145                         const unsigned char * buf, int count)
1146 {
1147         struct isi_port * port = (struct isi_port *) tty->driver_data;
1148         unsigned long flags;
1149         int cnt, total = 0;
1150 #ifdef ISICOM_DEBUG
1151         printk(KERN_DEBUG "ISICOM: isicom_write for port%d: %d bytes.\n",
1152                         port->channel+1, count);
1153 #endif          
1154         if (isicom_paranoia_check(port, tty->name, "isicom_write"))
1155                 return 0;
1156         
1157         if (!tty || !port->xmit_buf || !tmp_buf)
1158                 return 0;
1159         if (from_user)
1160                 down(&tmp_buf_sem); /* acquire xclusive access to tmp_buf */
1161                 
1162         save_flags(flags);
1163         while(1) {      
1164                 cli();
1165                 cnt = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1166                         SERIAL_XMIT_SIZE - port->xmit_head));
1167                 if (cnt <= 0) 
1168                         break;
1169                 
1170                 if (from_user) {
1171                         /* the following may block for paging... hence 
1172                            enabling interrupts but tx routine may have 
1173                            created more space in xmit_buf when the ctrl 
1174                            gets back here  */
1175                         sti(); 
1176                         if (copy_from_user(tmp_buf, buf, cnt)) {
1177                                 up(&tmp_buf_sem);
1178                                 restore_flags(flags);
1179                                 return -EFAULT;
1180                         }
1181                         cli();
1182                         cnt = MIN(cnt, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1183                         SERIAL_XMIT_SIZE - port->xmit_head));
1184                         memcpy(port->xmit_buf + port->xmit_head, tmp_buf, cnt);
1185                 }       
1186                 else
1187                         memcpy(port->xmit_buf + port->xmit_head, buf, cnt);
1188                 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE - 1);
1189                 port->xmit_cnt += cnt;
1190                 restore_flags(flags);
1191                 buf += cnt;
1192                 count -= cnt;
1193                 total += cnt;
1194         }               
1195         if (from_user)
1196                 up(&tmp_buf_sem);
1197         if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1198                 port->status |= ISI_TXOK;
1199         restore_flags(flags);
1200 #ifdef ISICOM_DEBUG
1201         printk(KERN_DEBUG "ISICOM: isicom_write %d bytes written.\n", total);
1202 #endif          
1203         return total;   
1204 }
1205
1206 /* put_char et all */
1207 static void isicom_put_char(struct tty_struct * tty, unsigned char ch)
1208 {
1209         struct isi_port * port = (struct isi_port *) tty->driver_data;
1210         unsigned long flags;
1211         
1212         if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
1213                 return;
1214         
1215         if (!tty || !port->xmit_buf)
1216                 return;
1217 #ifdef ISICOM_DEBUG
1218         printk(KERN_DEBUG "ISICOM: put_char, port %d, char %c.\n", port->channel+1, ch);
1219 #endif                  
1220                 
1221         save_flags(flags); cli();
1222         
1223         if (port->xmit_cnt >= (SERIAL_XMIT_SIZE - 1)) {
1224                 restore_flags(flags);
1225                 return;
1226         }
1227         
1228         port->xmit_buf[port->xmit_head++] = ch;
1229         port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
1230         port->xmit_cnt++;
1231         restore_flags(flags);
1232 }
1233
1234 /* flush_chars et all */
1235 static void isicom_flush_chars(struct tty_struct * tty)
1236 {
1237         struct isi_port * port = (struct isi_port *) tty->driver_data;
1238         
1239         if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
1240                 return;
1241         
1242         if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1243             !port->xmit_buf)
1244                 return;
1245                 
1246         /* this tells the transmitter to consider this port for
1247            data output to the card ... that's the best we can do. */
1248         port->status |= ISI_TXOK;       
1249 }
1250
1251 /* write_room et all */
1252 static int isicom_write_room(struct tty_struct * tty)
1253 {
1254         struct isi_port * port = (struct isi_port *) tty->driver_data;
1255         int free;
1256         if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
1257                 return 0;
1258         
1259         free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1260         if (free < 0)
1261                 free = 0;
1262         return free;
1263 }
1264
1265 /* chars_in_buffer et all */
1266 static int isicom_chars_in_buffer(struct tty_struct * tty)
1267 {
1268         struct isi_port * port = (struct isi_port *) tty->driver_data;
1269         if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
1270                 return 0;
1271         return port->xmit_cnt;
1272 }
1273
1274 /* ioctl et all */
1275 static inline void isicom_send_break(struct isi_port * port, unsigned long length)
1276 {
1277         struct isi_board * card = port->card;
1278         short wait = 10;
1279         unsigned short base = card->base;       
1280         unsigned long flags;
1281         
1282         save_flags(flags); cli();
1283         while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));     
1284         if (!wait) {
1285                 printk(KERN_DEBUG "ISICOM: Card found busy in isicom_send_break.\n");
1286                 goto out;
1287         }       
1288         outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1289         outw((length & 0xff) << 8 | 0x00, base);
1290         outw((length & 0xff00), base);
1291         InterruptTheCard(base);
1292 out:    restore_flags(flags);
1293 }
1294
1295 static int isicom_tiocmget(struct tty_struct *tty, struct file *file)
1296 {
1297         struct isi_port * port = (struct isi_port *) tty->driver_data;
1298         /* just send the port status */
1299         unsigned short status = port->status;
1300
1301         if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1302                 return -ENODEV;
1303         
1304         return  ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1305                 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1306                 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1307                 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1308                 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1309                 ((status & ISI_RI ) ? TIOCM_RI  : 0);
1310 }
1311
1312 static int isicom_tiocmset(struct tty_struct *tty, struct file *file,
1313                            unsigned int set, unsigned int clear)
1314 {
1315         struct isi_port * port = (struct isi_port *) tty->driver_data;
1316         unsigned long flags;
1317         
1318         if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1319                 return -ENODEV;
1320         
1321         save_flags(flags); cli();
1322         if (set & TIOCM_RTS)
1323                 raise_rts(port);
1324         if (set & TIOCM_DTR)
1325                 raise_dtr(port);
1326
1327         if (clear & TIOCM_RTS)
1328                 drop_rts(port);
1329         if (clear & TIOCM_DTR)
1330                 drop_dtr(port);
1331
1332         restore_flags(flags);
1333         return 0;
1334 }                       
1335
1336 static int isicom_set_serial_info(struct isi_port * port,
1337                                         struct serial_struct * info)
1338 {
1339         struct serial_struct newinfo;
1340         unsigned long flags;
1341         int reconfig_port;
1342
1343         if(copy_from_user(&newinfo, info, sizeof(newinfo)))
1344                 return -EFAULT;
1345                 
1346         reconfig_port = ((port->flags & ASYNC_SPD_MASK) != 
1347                          (newinfo.flags & ASYNC_SPD_MASK));
1348         
1349         if (!capable(CAP_SYS_ADMIN)) {
1350                 if ((newinfo.close_delay != port->close_delay) ||
1351                     (newinfo.closing_wait != port->closing_wait) ||
1352                     ((newinfo.flags & ~ASYNC_USR_MASK) != 
1353                      (port->flags & ~ASYNC_USR_MASK)))
1354                         return -EPERM;
1355                 port->flags = ((port->flags & ~ ASYNC_USR_MASK) |
1356                                 (newinfo.flags & ASYNC_USR_MASK));
1357         }       
1358         else {
1359                 port->close_delay = newinfo.close_delay;
1360                 port->closing_wait = newinfo.closing_wait; 
1361                 port->flags = ((port->flags & ~ASYNC_FLAGS) | 
1362                                 (newinfo.flags & ASYNC_FLAGS));
1363         }
1364         if (reconfig_port) {
1365                 save_flags(flags); cli();
1366                 isicom_config_port(port);
1367                 restore_flags(flags);
1368         }
1369         return 0;                
1370 }               
1371
1372 static int isicom_get_serial_info(struct isi_port * port, 
1373                                         struct serial_struct * info)
1374 {
1375         struct serial_struct out_info;
1376         
1377         memset(&out_info, 0, sizeof(out_info));
1378 /*      out_info.type = ? */
1379         out_info.line = port - isi_ports;
1380         out_info.port = port->card->base;
1381         out_info.irq = port->card->irq;
1382         out_info.flags = port->flags;
1383 /*      out_info.baud_base = ? */
1384         out_info.close_delay = port->close_delay;
1385         out_info.closing_wait = port->closing_wait;
1386         if(copy_to_user(info, &out_info, sizeof(out_info)))
1387                 return -EFAULT;
1388         return 0;
1389 }                                       
1390
1391 static int isicom_ioctl(struct tty_struct * tty, struct file * filp,
1392                         unsigned int cmd, unsigned long arg) 
1393 {
1394         struct isi_port * port = (struct isi_port *) tty->driver_data;
1395         int retval;
1396
1397         if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1398                 return -ENODEV;
1399
1400         switch(cmd) {
1401                 case TCSBRK:
1402                         retval = tty_check_change(tty);
1403                         if (retval)
1404                                 return retval;
1405                         tty_wait_until_sent(tty, 0);
1406                         if (!arg)
1407                                 isicom_send_break(port, HZ/4);
1408                         return 0;
1409                         
1410                 case TCSBRKP:   
1411                         retval = tty_check_change(tty);
1412                         if (retval)
1413                                 return retval;
1414                         tty_wait_until_sent(tty, 0);
1415                         isicom_send_break(port, arg ? arg * (HZ/10) : HZ/4);
1416                         return 0;
1417                         
1418                 case TIOCGSOFTCAR:
1419                         return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1420                         
1421                 case TIOCSSOFTCAR:
1422                         if(get_user(arg, (unsigned long *) arg))
1423                                 return -EFAULT;
1424                         tty->termios->c_cflag =
1425                                 ((tty->termios->c_cflag & ~CLOCAL) |
1426                                 (arg ? CLOCAL : 0));
1427                         return 0;       
1428                         
1429                 case TIOCGSERIAL:
1430                         return isicom_get_serial_info(port, 
1431                                         (struct serial_struct *) arg);
1432                 
1433                 case TIOCSSERIAL:
1434                         return isicom_set_serial_info(port,
1435                                         (struct serial_struct *) arg);
1436                                         
1437                 default:
1438                         return -ENOIOCTLCMD;                                            
1439         }
1440         return 0;
1441 }
1442
1443 /* set_termios et all */
1444 static void isicom_set_termios(struct tty_struct * tty, struct termios * old_termios)
1445 {
1446         struct isi_port * port = (struct isi_port *) tty->driver_data;
1447         unsigned long flags;
1448         
1449         if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
1450                 return;
1451         
1452         if (tty->termios->c_cflag == old_termios->c_cflag &&
1453             tty->termios->c_iflag == old_termios->c_iflag)
1454                 return;
1455                 
1456         save_flags(flags); cli();
1457         isicom_config_port(port);
1458         restore_flags(flags);
1459         
1460         if ((old_termios->c_cflag & CRTSCTS) &&
1461             !(tty->termios->c_cflag & CRTSCTS)) {       
1462                 tty->hw_stopped = 0;
1463                 isicom_start(tty);   
1464         }    
1465 }
1466
1467 /* throttle et all */
1468 static void isicom_throttle(struct tty_struct * tty)
1469 {
1470         struct isi_port * port = (struct isi_port *) tty->driver_data;
1471         struct isi_board * card = port->card;
1472         unsigned long flags;
1473         
1474         if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
1475                 return;
1476         
1477         /* tell the card that this port cannot handle any more data for now */
1478         save_flags(flags); cli();
1479         card->port_status &= ~(1 << port->channel);
1480         outw(card->port_status, card->base + 0x02);
1481         restore_flags(flags);
1482 }
1483
1484 /* unthrottle et all */
1485 static void isicom_unthrottle(struct tty_struct * tty)
1486 {
1487         struct isi_port * port = (struct isi_port *) tty->driver_data;
1488         struct isi_board * card = port->card;
1489         unsigned long flags;
1490         
1491         if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
1492                 return;
1493         
1494         /* tell the card that this port is ready to accept more data */
1495         save_flags(flags); cli();
1496         card->port_status |= (1 << port->channel);
1497         outw(card->port_status, card->base + 0x02);
1498         restore_flags(flags);
1499 }
1500
1501 /* stop et all */
1502 static void isicom_stop(struct tty_struct * tty)
1503 {
1504         struct isi_port * port = (struct isi_port *) tty->driver_data;
1505
1506         if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
1507                 return;
1508         
1509         /* this tells the transmitter not to consider this port for
1510            data output to the card. */
1511         port->status &= ~ISI_TXOK;
1512 }
1513
1514 /* start et all */
1515 static void isicom_start(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_start"))
1520                 return;
1521         
1522         /* this tells the transmitter to consider this port for
1523            data output to the card. */
1524         port->status |= ISI_TXOK;
1525 }
1526
1527 /* hangup et all */
1528 static void do_isicom_hangup(void * data)
1529 {
1530         struct isi_port * port = (struct isi_port *) data;
1531         struct tty_struct * tty;
1532         
1533         tty = port->tty;
1534         if (tty)
1535                 tty_hangup(tty);        /* FIXME: module removal race here - AKPM */
1536 }
1537
1538 static void isicom_hangup(struct tty_struct * tty)
1539 {
1540         struct isi_port * port = (struct isi_port *) tty->driver_data;
1541         
1542         if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
1543                 return;
1544         
1545         isicom_shutdown_port(port);
1546         port->count = 0;
1547         port->flags &= ~ASYNC_NORMAL_ACTIVE;
1548         port->tty = 0;
1549         wake_up_interruptible(&port->open_wait);
1550 }
1551
1552 /* flush_buffer et all */
1553 static void isicom_flush_buffer(struct tty_struct * tty)
1554 {
1555         struct isi_port * port = (struct isi_port *) tty->driver_data;
1556         unsigned long flags;
1557         
1558         if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
1559                 return;
1560         
1561         save_flags(flags); cli();
1562         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1563         restore_flags(flags);
1564         
1565         wake_up_interruptible(&tty->write_wait);
1566         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1567             tty->ldisc.write_wakeup)
1568                 (tty->ldisc.write_wakeup)(tty);
1569 }
1570
1571
1572 static int register_ioregion(void)
1573 {
1574         int count, done=0;
1575         for (count=0; count < BOARD_COUNT; count++ ) {
1576                 if (isi_card[count].base)
1577                         if (!request_region(isi_card[count].base,16,ISICOM_NAME)) {
1578                                 printk(KERN_DEBUG "ISICOM: I/O Region 0x%x-0x%x is busy. Card%d will be disabled.\n",
1579                                         isi_card[count].base,isi_card[count].base+15,count+1);
1580                                 isi_card[count].base=0;
1581                                 done++;
1582                         }
1583         }
1584         return done;
1585 }
1586
1587 static void unregister_ioregion(void)
1588 {
1589         int count;
1590         for (count=0; count < BOARD_COUNT; count++ ) 
1591                 if (isi_card[count].base) {
1592                         release_region(isi_card[count].base,16);
1593 #ifdef ISICOM_DEBUG                     
1594                         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);
1595 #endif                  
1596                 }
1597 }
1598
1599 static struct tty_operations isicom_ops = {
1600         .open   = isicom_open,
1601         .close  = isicom_close,
1602         .write  = isicom_write,
1603         .put_char       = isicom_put_char,
1604         .flush_chars    = isicom_flush_chars,
1605         .write_room     = isicom_write_room,
1606         .chars_in_buffer        = isicom_chars_in_buffer,
1607         .ioctl  = isicom_ioctl,
1608         .set_termios    = isicom_set_termios,
1609         .throttle       = isicom_throttle,
1610         .unthrottle     = isicom_unthrottle,
1611         .stop   = isicom_stop,
1612         .start  = isicom_start,
1613         .hangup = isicom_hangup,
1614         .flush_buffer   = isicom_flush_buffer,
1615         .tiocmget       = isicom_tiocmget,
1616         .tiocmset       = isicom_tiocmset,
1617 };
1618
1619 static int register_drivers(void)
1620 {
1621         int error;
1622
1623         /* tty driver structure initialization */
1624         isicom_normal = alloc_tty_driver(PORT_COUNT);
1625         if (!isicom_normal)
1626                 return -ENOMEM;
1627
1628         isicom_normal->owner    = THIS_MODULE;
1629         isicom_normal->name     = "ttyM";
1630         isicom_normal->devfs_name = "isicom/";
1631         isicom_normal->major    = ISICOM_NMAJOR;
1632         isicom_normal->minor_start      = 0;
1633         isicom_normal->type     = TTY_DRIVER_TYPE_SERIAL;
1634         isicom_normal->subtype  = SERIAL_TYPE_NORMAL;
1635         isicom_normal->init_termios     = tty_std_termios;
1636         isicom_normal->init_termios.c_cflag     = 
1637                                 B9600 | CS8 | CREAD | HUPCL |CLOCAL;
1638         isicom_normal->flags    = TTY_DRIVER_REAL_RAW;
1639         tty_set_operations(isicom_normal, &isicom_ops);
1640         
1641         if ((error=tty_register_driver(isicom_normal))!=0) {
1642                 printk(KERN_DEBUG "ISICOM: Couldn't register the dialin driver, error=%d\n",
1643                         error);
1644                 put_tty_driver(isicom_normal);
1645                 return error;
1646         }
1647         return 0;
1648 }
1649
1650 static void unregister_drivers(void)
1651 {
1652         int error = tty_unregister_driver(isicom_normal);
1653         if (error)
1654                 printk(KERN_DEBUG "ISICOM: couldn't unregister normal driver error=%d.\n",error);
1655         put_tty_driver(isicom_normal);
1656 }
1657
1658 static int register_isr(void)
1659 {
1660         int count, done=0, card;
1661         int flag;
1662         unsigned char request;
1663         for (count=0; count < BOARD_COUNT; count++ ) {
1664                 if (isi_card[count].base) {
1665                 /*
1666                  * verify if the required irq has already been requested for
1667                  * another ISI Card, if so we already have it, else request it
1668                  */
1669                         request = YES;
1670                         for(card = 0; card < count; card++)
1671                         if ((isi_card[card].base) && (isi_card[card].irq == isi_card[count].irq)) {
1672                                 request = NO;
1673                                 if ((isi_card[count].isa == NO) && (isi_card[card].isa == NO))
1674                                         break;
1675                                 /*
1676                                  * ISA cards cannot share interrupts with other
1677                                  * PCI or ISA devices hence disable this card.
1678                                  */
1679                                 release_region(isi_card[count].base,16);
1680                                 isi_card[count].base = 0;
1681                                 break;
1682                         }
1683                         flag=0;
1684                         if(isi_card[count].isa == NO)
1685                                 flag |= SA_SHIRQ;
1686                                 
1687                         if (request == YES) { 
1688                                 if (request_irq(isi_card[count].irq, isicom_interrupt, SA_INTERRUPT|flag, ISICOM_NAME, NULL)) {
1689                                         printk(KERN_WARNING "ISICOM: Could not install handler at Irq %d. Card%d will be disabled.\n",
1690                                                 isi_card[count].irq, count+1);
1691                                         release_region(isi_card[count].base,16);
1692                                         isi_card[count].base=0;
1693                                 }
1694                                 else {
1695                                         printk(KERN_INFO "ISICOM: Card%d at 0x%x using irq %d.\n", 
1696                                         count+1, isi_card[count].base, isi_card[count].irq); 
1697                                         
1698                                         irq_to_board[isi_card[count].irq]=&isi_card[count];
1699                                         done++;
1700                                 }
1701                         }
1702                 }       
1703         }
1704         return done;
1705 }
1706
1707 static void unregister_isr(void)
1708 {
1709         int count, card;
1710         unsigned char freeirq;
1711         for (count=0; count < BOARD_COUNT; count++ ) {
1712                 if (isi_card[count].base) {
1713                         freeirq = YES;
1714                         for(card = 0; card < count; card++)
1715                                 if ((isi_card[card].base) && (isi_card[card].irq == isi_card[count].irq)) {
1716                                         freeirq = NO;
1717                                         break;
1718                                 }
1719                         if (freeirq == YES) {
1720                                 free_irq(isi_card[count].irq, NULL);
1721 #ifdef ISICOM_DEBUG                     
1722                                 printk(KERN_DEBUG "ISICOM: Irq %d released for Card%d.\n",isi_card[count].irq, count+1);
1723 #endif  
1724                         }               
1725                 }
1726         }
1727 }
1728
1729 static int isicom_init(void)
1730 {
1731         int card, channel, base;
1732         struct isi_port * port;
1733         unsigned long page;
1734         
1735         if (!tmp_buf) { 
1736                 page = get_zeroed_page(GFP_KERNEL);
1737                 if (!page) {
1738 #ifdef ISICOM_DEBUG             
1739                         printk(KERN_DEBUG "ISICOM: Couldn't allocate page for tmp_buf.\n");
1740 #else
1741                         printk(KERN_ERR "ISICOM: Not enough memory...\n");
1742 #endif        
1743                         return 0;
1744                 }       
1745                 tmp_buf = (unsigned char *) page;
1746         }
1747         
1748         if (!register_ioregion()) 
1749         {
1750                 printk(KERN_ERR "ISICOM: All required I/O space found busy.\n");
1751                 free_page((unsigned long)tmp_buf);
1752                 return 0;
1753         }
1754         if (register_drivers()) 
1755         {
1756                 unregister_ioregion();
1757                 free_page((unsigned long)tmp_buf);
1758                 return 0;
1759         }
1760         if (!register_isr()) 
1761         {
1762                 unregister_drivers();
1763                 /*  ioports already uregistered in register_isr */
1764                 free_page((unsigned long)tmp_buf);
1765                 return 0;               
1766         }
1767         
1768         memset(isi_ports, 0, sizeof(isi_ports));
1769         for (card = 0; card < BOARD_COUNT; card++) {
1770                 port = &isi_ports[card * 16];
1771                 isi_card[card].ports = port;
1772                 base = isi_card[card].base;
1773                 for (channel = 0; channel < 16; channel++, port++) {
1774                         port->magic = ISICOM_MAGIC;
1775                         port->card = &isi_card[card];
1776                         port->channel = channel;                
1777                         port->close_delay = 50 * HZ/100;
1778                         port->closing_wait = 3000 * HZ/100;
1779                         INIT_WORK(&port->hangup_tq, do_isicom_hangup, port);
1780                         INIT_WORK(&port->bh_tqueue, isicom_bottomhalf, port);
1781                         port->status = 0;
1782                         init_waitqueue_head(&port->open_wait);                                  
1783                         init_waitqueue_head(&port->close_wait);
1784                         /*  . . .  */
1785                 }
1786         } 
1787         
1788         return 1;       
1789 }
1790
1791 /*
1792  *      Insmod can set static symbols so keep these static
1793  */
1794  
1795 static int io[4];
1796 static int irq[4];
1797
1798 MODULE_AUTHOR("MultiTech");
1799 MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
1800 MODULE_LICENSE("GPL");
1801 MODULE_PARM(io, "1-4i");
1802 MODULE_PARM_DESC(io, "I/O ports for the cards");
1803 MODULE_PARM(irq, "1-4i");
1804 MODULE_PARM_DESC(irq, "Interrupts for the cards");
1805
1806 int init_module(void)
1807 {
1808         struct pci_dev *dev = NULL;
1809         int retval, card, idx, count;
1810         unsigned char pciirq;
1811         unsigned int ioaddr;
1812                         
1813         card = 0;
1814         for(idx=0; idx < BOARD_COUNT; idx++) {  
1815                 if (io[idx]) {
1816                         isi_card[idx].base=io[idx];
1817                         isi_card[idx].irq=irq[idx];
1818                         isi_card[idx].isa=YES;
1819                         card++;
1820                 }
1821                 else {
1822                         isi_card[idx].base = 0;
1823                         isi_card[idx].irq = 0;
1824                 }
1825         }
1826         
1827         for (idx=0 ;idx < card; idx++) {
1828                 if (!((isi_card[idx].irq==2)||(isi_card[idx].irq==3)||
1829                     (isi_card[idx].irq==4)||(isi_card[idx].irq==5)||
1830                     (isi_card[idx].irq==7)||(isi_card[idx].irq==10)||
1831                     (isi_card[idx].irq==11)||(isi_card[idx].irq==12)||
1832                     (isi_card[idx].irq==15))) {
1833                         
1834                         if (isi_card[idx].base) {
1835                                 printk(KERN_ERR "ISICOM: Irq %d unsupported. Disabling Card%d...\n",
1836                                         isi_card[idx].irq, idx+1);
1837                                 isi_card[idx].base=0;
1838                                 card--;
1839                         }       
1840                 }
1841         }       
1842         
1843         if (card < BOARD_COUNT) {
1844                 for (idx=0; idx < DEVID_COUNT; idx++) {
1845                         dev = NULL;
1846                         for (;;){
1847                                 if (!(dev = pci_find_device(VENDOR_ID, isicom_pci_tbl[idx].device, dev)))
1848                                         break;
1849                                 if (card >= BOARD_COUNT)
1850                                         break;
1851                                         
1852                                 if (pci_enable_device(dev))
1853                                         break;
1854
1855                                 /* found a PCI ISI card! */
1856                                 ioaddr = pci_resource_start (dev, 3); /* i.e at offset 0x1c in the
1857                                                                        * PCI configuration register
1858                                                                        * space.
1859                                                                        */
1860                                 pciirq = dev->irq;
1861                                 printk(KERN_INFO "ISI PCI Card(Device ID 0x%x)\n", isicom_pci_tbl[idx].device);
1862                                 /*
1863                                  * allot the first empty slot in the array
1864                                  */                             
1865                                 for (count=0; count < BOARD_COUNT; count++) {                           
1866                                         if (isi_card[count].base == 0) {
1867                                                 isi_card[count].base = ioaddr;
1868                                                 isi_card[count].irq = pciirq;
1869                                                 isi_card[count].isa = NO;
1870                                                 card++;
1871                                                 break;
1872                                         }
1873                                 }
1874                         }                               
1875                         if (card >= BOARD_COUNT) break;
1876                 }
1877         }
1878         
1879         if (!(isi_card[0].base || isi_card[1].base || isi_card[2].base || isi_card[3].base)) {
1880                 printk(KERN_ERR "ISICOM: No valid card configuration. Driver cannot be initialized...\n"); 
1881                 return -EIO;
1882         }       
1883
1884         retval = misc_register(&isiloader_device);
1885         if (retval < 0) {
1886                 printk(KERN_ERR "ISICOM: Unable to register firmware loader driver.\n");
1887                 return retval;
1888         }
1889         
1890         if (!isicom_init()) {
1891                 if (misc_deregister(&isiloader_device)) 
1892                         printk(KERN_ERR "ISICOM: Unable to unregister Firmware Loader driver\n");
1893                 return -EIO;
1894         }
1895         
1896         init_timer(&tx);
1897         tx.expires = jiffies + 1;
1898         tx.data = 0;
1899         tx.function = isicom_tx;
1900         re_schedule = 1;
1901         add_timer(&tx);
1902         
1903         return 0;
1904 }
1905
1906 void cleanup_module(void)
1907 {
1908         re_schedule = 0;
1909         set_current_state(TASK_INTERRUPTIBLE);
1910         schedule_timeout(HZ);
1911
1912 #ifdef ISICOM_DEBUG     
1913         printk("ISICOM: isicom_tx tx_count = %ld.\n", tx_count);
1914 #endif  
1915
1916 #ifdef ISICOM_DEBUG
1917         printk("ISICOM: uregistering isr ...\n");
1918 #endif  
1919         unregister_isr();
1920
1921 #ifdef ISICOM_DEBUG     
1922         printk("ISICOM: unregistering drivers ...\n");
1923 #endif
1924         unregister_drivers();
1925         
1926 #ifdef ISICOM_DEBUG     
1927         printk("ISICOM: unregistering ioregion ...\n");
1928 #endif  
1929         unregister_ioregion();  
1930         
1931 #ifdef ISICOM_DEBUG     
1932         printk("ISICOM: freeing tmp_buf ...\n");
1933 #endif  
1934         free_page((unsigned long)tmp_buf);
1935         
1936 #ifdef ISICOM_DEBUG             
1937         printk("ISICOM: unregistering firmware loader ...\n");  
1938 #endif
1939         if (misc_deregister(&isiloader_device))
1940                 printk(KERN_ERR "ISICOM: Unable to unregister Firmware Loader driver\n");
1941 }