VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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;
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         void __user *argp = (void __user *)arg;
136         /* exec_record exec_rec; */
137         
138         if(get_user(card, (int __user *)argp))
139                 return -EFAULT;
140                 
141         if(card < 0 || card >= BOARD_COUNT)
142                 return -ENXIO;
143                 
144         base=isi_card[card].base;
145         
146         if(base==0)
147                 return -ENXIO;  /* disabled or not used */
148         
149         switch(cmd) {
150                 case MIOCTL_RESET_CARD:
151                         if (!capable(CAP_SYS_ADMIN))
152                                 return -EPERM;
153                         printk(KERN_DEBUG "ISILoad:Resetting Card%d at 0x%x ",card+1,base);
154                                                                 
155                         inw(base+0x8);
156                         
157                         for(t=jiffies+HZ/100;time_before(jiffies, t););
158                                 
159                         outw(0,base+0x8); /* Reset */
160                         
161                         for(j=1;j<=3;j++) {
162                                 for(t=jiffies+HZ;time_before(jiffies, t););
163                                 printk(".");
164                         }       
165                         signature=(inw(base+0x4)) & 0xff;       
166                         if (isi_card[card].isa) {
167                                         
168                                 if (!(inw(base+0xe) & 0x1) || (inw(base+0x2))) {
169 #ifdef ISICOM_DEBUG                             
170                                         printk("\nbase+0x2=0x%x , base+0xe=0x%x",inw(base+0x2),inw(base+0xe));
171 #endif                          
172                                         printk("\nISILoad:ISA Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
173                                         return -EIO;                                    
174                                 }
175                         }       
176                         else {
177                                 portcount = inw(base+0x2);
178                                 if (!(inw(base+0xe) & 0x1) || ((portcount!=0) && (portcount!=4) && (portcount!=8))) {   
179 #ifdef ISICOM_DEBUG
180                                         printk("\nbase+0x2=0x%x , base+0xe=0x%x",inw(base+0x2),inw(base+0xe));
181 #endif
182                                         printk("\nISILoad:PCI Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
183                                         return -EIO;
184                                 }
185                         }       
186                         switch(signature) {
187                         case    0xa5:
188                         case    0xbb:
189                         case    0xdd:   
190                                         if (isi_card[card].isa) 
191                                                 isi_card[card].port_count = 8;
192                                         else {
193                                                 if (portcount == 4)
194                                                         isi_card[card].port_count = 4;
195                                                 else
196                                                         isi_card[card].port_count = 8;
197                                         }       
198                                         isi_card[card].shift_count = 12;
199                                         break;
200                                         
201                         case    0xcc:   isi_card[card].port_count = 16;
202                                         isi_card[card].shift_count = 11;
203                                         break;                          
204                                         
205                         default: printk("ISILoad:Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
206 #ifdef ISICOM_DEBUG                     
207                                  printk("Sig=0x%x\n",signature);
208 #endif                           
209                                  return -EIO;
210                         }
211                         printk("-Done\n");
212                         return put_user(signature,(unsigned __user *)argp);
213                                                 
214         case    MIOCTL_LOAD_FIRMWARE:
215                         if (!capable(CAP_SYS_ADMIN))
216                                 return -EPERM;
217                                 
218                         if(copy_from_user(&frame, argp, sizeof(bin_frame)))
219                                 return -EFAULT;
220                         
221                         if (WaitTillCardIsFree(base))
222                                 return -EIO;
223                         
224                         outw(0xf0,base);        /* start upload sequence */ 
225                         outw(0x00,base);
226                         outw((frame.addr), base);/*      lsb of adderess    */
227                         
228                         word_count=(frame.count >> 1) + frame.count % 2;
229                         outw(word_count, base);
230                         InterruptTheCard(base);
231                         
232                         for(i=0;i<=0x2f;i++);   /* a wee bit of delay */
233                         
234                         if (WaitTillCardIsFree(base)) 
235                                 return -EIO;
236                                 
237                         if ((status=inw(base+0x4))!=0) {
238                                 printk(KERN_WARNING "ISILoad:Card%d rejected load header:\nAddress:0x%x \nCount:0x%x \nStatus:0x%x \n", 
239                                 card+1, frame.addr, frame.count, status);
240                                 return -EIO;
241                         }
242                         outsw(base, (void *) frame.bin_data, word_count);
243                         
244                         InterruptTheCard(base);
245                         
246                         for(i=0;i<=0x0f;i++);   /* another wee bit of delay */ 
247                         
248                         if (WaitTillCardIsFree(base)) 
249                                 return -EIO;
250                                 
251                         if ((status=inw(base+0x4))!=0) {
252                                 printk(KERN_ERR "ISILoad:Card%d got out of sync.Card Status:0x%x\n",card+1, status);
253                                 return -EIO;
254                         }       
255                         return 0;
256                                                 
257         case    MIOCTL_READ_FIRMWARE:
258                         if (!capable(CAP_SYS_ADMIN))
259                                 return -EPERM;
260                                 
261                         if(copy_from_user(&frame, argp, sizeof(bin_header)))
262                                 return -EFAULT;
263                         
264                         if (WaitTillCardIsFree(base))
265                                 return -EIO;
266                         
267                         outw(0xf1,base);        /* start download sequence */ 
268                         outw(0x00,base);
269                         outw((frame.addr), base);/*      lsb of adderess    */
270                         
271                         word_count=(frame.count >> 1) + frame.count % 2;
272                         outw(word_count+1, base);
273                         InterruptTheCard(base);
274                         
275                         for(i=0;i<=0xf;i++);    /* a wee bit of delay */
276                         
277                         if (WaitTillCardIsFree(base)) 
278                                 return -EIO;
279                                 
280                         if ((status=inw(base+0x4))!=0) {
281                                 printk(KERN_WARNING "ISILoad:Card%d rejected verify header:\nAddress:0x%x \nCount:0x%x \nStatus:0x%x \n", 
282                                 card+1, frame.addr, frame.count, status);
283                                 return -EIO;
284                         }
285                         
286                         inw(base);
287                         insw(base, frame.bin_data, word_count);
288                         InterruptTheCard(base);
289                         
290                         for(i=0;i<=0x0f;i++);   /* another wee bit of delay */ 
291                         
292                         if (WaitTillCardIsFree(base)) 
293                                 return -EIO;
294                                 
295                         if ((status=inw(base+0x4))!=0) {
296                                 printk(KERN_ERR "ISILoad:Card%d verify got out of sync.Card Status:0x%x\n",card+1, status);
297                                 return -EIO;
298                         }       
299                         
300                         if(copy_to_user(argp, &frame, sizeof(bin_frame)))
301                                 return -EFAULT;
302                         return 0;
303         
304         case    MIOCTL_XFER_CTRL:
305                         if (!capable(CAP_SYS_ADMIN))
306                                 return -EPERM;
307                         if (WaitTillCardIsFree(base)) 
308                                 return -EIO;
309                                         
310                         outw(0xf2, base);
311                         outw(0x800, base);
312                         outw(0x0, base);
313                         outw(0x0, base);
314                         InterruptTheCard(base);
315                         outw(0x0, base+0x4);    /* for ISI4608 cards */
316                                                         
317                         isi_card[card].status |= FIRMWARE_LOADED;
318                         return 0;       
319                         
320         default:
321 #ifdef ISICOM_DEBUG     
322                 printk(KERN_DEBUG "ISILoad: Received Ioctl cmd 0x%x.\n", cmd); 
323 #endif
324                 return -ENOIOCTLCMD;
325         
326         }
327         
328 }
329                                 
330
331 /*
332  *      ISICOM Driver specific routines ...
333  *
334  */
335  
336 static inline int isicom_paranoia_check(struct isi_port const * port, char *name, 
337                                         const char * routine)
338 {
339 #ifdef ISICOM_DEBUG 
340         static const char * badmagic = 
341                         KERN_WARNING "ISICOM: Warning: bad isicom magic for dev %s in %s.\n";
342         static const char * badport = 
343                         KERN_WARNING "ISICOM: Warning: NULL isicom port for dev %s in %s.\n";           
344         if (!port) {
345                 printk(badport, name, routine);
346                 return 1;
347         }
348         if (port->magic != ISICOM_MAGIC) {
349                 printk(badmagic, name, routine);
350                 return 1;
351         }       
352 #endif  
353         return 0;
354 }
355                         
356 /*      Transmitter     */
357
358 static void isicom_tx(unsigned long _data)
359 {
360         short count = (BOARD_COUNT-1), card, base;
361         short txcount, wait, wrd, residue, word_count, cnt;
362         struct isi_port * port;
363         struct tty_struct * tty;
364         unsigned long flags;
365         
366 #ifdef ISICOM_DEBUG
367         ++tx_count;
368 #endif  
369         
370         /*      find next active board  */
371         card = (prev_card + 1) & 0x0003;
372         while(count-- > 0) {
373                 if (isi_card[card].status & BOARD_ACTIVE) 
374                         break;
375                 card = (card + 1) & 0x0003;     
376         }
377         if (!(isi_card[card].status & BOARD_ACTIVE))
378                 goto sched_again;
379                 
380         prev_card = card;
381         
382         count = isi_card[card].port_count;
383         port = isi_card[card].ports;
384         base = isi_card[card].base;
385         for (;count > 0;count--, port++) {
386                 /* port not active or tx disabled to force flow control */
387                 if (!(port->status & ISI_TXOK))
388                         continue;
389                 
390                 tty = port->tty;
391                 save_flags(flags); cli();
392                 txcount = MIN(TX_SIZE, port->xmit_cnt);
393                 if ((txcount <= 0) || tty->stopped || tty->hw_stopped) {
394                         restore_flags(flags);
395                         continue;
396                 }
397                 wait = 200;     
398                 while(((inw(base+0x0e) & 0x01) == 0) && (wait-- > 0));
399                 if (wait <= 0) {
400                         restore_flags(flags);
401 #ifdef ISICOM_DEBUG
402                         printk(KERN_DEBUG "ISICOM: isicom_tx:Card(0x%x) found busy.\n",
403                                 card);
404 #endif
405                         continue;
406                 }
407                 if (!(inw(base + 0x02) & (1 << port->channel))) {
408                         restore_flags(flags);
409 #ifdef ISICOM_DEBUG                                     
410                         printk(KERN_DEBUG "ISICOM: isicom_tx: cannot tx to 0x%x:%d.\n",
411                                         base, port->channel + 1);
412 #endif                                  
413                         continue;               
414                 }
415 #ifdef ISICOM_DEBUG
416                 printk(KERN_DEBUG "ISICOM: txing %d bytes, port%d.\n", 
417                                 txcount, port->channel+1); 
418 #endif  
419                 outw((port->channel << isi_card[card].shift_count) | txcount
420                                         , base);
421                 residue = NO;
422                 wrd = 0;                        
423                 while (1) {
424                         cnt = MIN(txcount, (SERIAL_XMIT_SIZE - port->xmit_tail));
425                         if (residue == YES) {
426                                 residue = NO;
427                                 if (cnt > 0) {
428                                         wrd |= (port->xmit_buf[port->xmit_tail] << 8);
429                                         port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
430                                         port->xmit_cnt--;
431                                         txcount--;
432                                         cnt--;
433                                         outw(wrd, base);                        
434                                 }
435                                 else {
436                                         outw(wrd, base);
437                                         break;
438                                 }
439                         }               
440                         if (cnt <= 0) break;
441                         word_count = cnt >> 1;
442                         outsw(base, port->xmit_buf+port->xmit_tail, word_count);
443                         port->xmit_tail = (port->xmit_tail + (word_count << 1)) &
444                                                 (SERIAL_XMIT_SIZE - 1);
445                         txcount -= (word_count << 1);
446                         port->xmit_cnt -= (word_count << 1);
447                         if (cnt & 0x0001) {
448                                 residue = YES;
449                                 wrd = port->xmit_buf[port->xmit_tail];
450                                 port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
451                                 port->xmit_cnt--;
452                                 txcount--;
453                         }
454                 }
455
456                 InterruptTheCard(base);
457                 if (port->xmit_cnt <= 0)
458                         port->status &= ~ISI_TXOK;
459                 if (port->xmit_cnt <= WAKEUP_CHARS)
460                         schedule_work(&port->bh_tqueue);
461                 restore_flags(flags);
462         }       
463
464                 /*      schedule another tx for hopefully in about 10ms */      
465 sched_again:    
466         if (!re_schedule)       
467                 return;
468         init_timer(&tx);
469         tx.expires = jiffies + HZ/100;
470         tx.data = 0;
471         tx.function = isicom_tx;
472         add_timer(&tx);
473         
474         return; 
475 }               
476  
477 /*      Interrupt handlers      */
478
479  
480 static void isicom_bottomhalf(void * data)
481 {
482         struct isi_port * port = (struct isi_port *) data;
483         struct tty_struct * tty = port->tty;
484         
485         if (!tty)
486                 return;
487         
488         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
489             tty->ldisc.write_wakeup)
490                 (tty->ldisc.write_wakeup)(tty);
491         wake_up_interruptible(&tty->write_wait);
492 }               
493                 
494 /* main interrupt handler routine */            
495 static irqreturn_t isicom_interrupt(int irq, void *dev_id,
496                                         struct pt_regs *regs)
497 {
498         struct isi_board * card;
499         struct isi_port * port;
500         struct tty_struct * tty;
501         unsigned short base, header, word_count, count;
502         unsigned char channel;
503         short byte_count;
504         
505         /*
506          *      find the source of interrupt
507          */
508          
509         for(count = 0; count < BOARD_COUNT; count++) { 
510                 card = &isi_card[count];
511                 if (card->base != 0) {
512                         if (((card->isa == YES) && (card->irq == irq)) || 
513                                 ((card->isa == NO) && (card->irq == irq) && (inw(card->base+0x0e) & 0x02)))
514                                 break;
515                 }
516                 card = NULL;
517         }
518
519         if (!card || !(card->status & FIRMWARE_LOADED)) {
520 /*              printk(KERN_DEBUG "ISICOM: interrupt: not handling irq%d!.\n", irq);*/
521                 return IRQ_NONE;
522         }
523         
524         base = card->base;
525         if (card->isa == NO) {
526         /*
527          *      disable any interrupts from the PCI card and lower the
528          *      interrupt line
529          */
530                 outw(0x8000, base+0x04);
531                 ClearInterrupt(base);
532         }
533         
534         inw(base);              /* get the dummy word out */
535         header = inw(base);
536         channel = (header & 0x7800) >> card->shift_count;
537         byte_count = header & 0xff;
538 #ifdef ISICOM_DEBUG     
539         printk(KERN_DEBUG "ISICOM:Intr:(0x%x:%d).\n", base, channel+1);
540 #endif  
541         if ((channel+1) > card->port_count) {
542                 printk(KERN_WARNING "ISICOM: isicom_interrupt(0x%x): %d(channel) > port_count.\n",
543                                 base, channel+1);
544                 if (card->isa)
545                         ClearInterrupt(base);
546                 else
547                         outw(0x0000, base+0x04); /* enable interrupts */                
548                 return IRQ_HANDLED;                     
549         }
550         port = card->ports + channel;
551         if (!(port->flags & ASYNC_INITIALIZED)) {
552                 if (card->isa)
553                         ClearInterrupt(base);
554                 else
555                         outw(0x0000, base+0x04); /* enable interrupts */
556                 return IRQ_HANDLED;
557         }       
558                 
559         tty = port->tty;
560         
561         if (header & 0x8000) {          /* Status Packet */
562                 header = inw(base);
563                 switch(header & 0xff) {
564                         case 0: /* Change in EIA signals */
565                                 
566                                 if (port->flags & ASYNC_CHECK_CD) {
567                                         if (port->status & ISI_DCD) {
568                                                 if (!(header & ISI_DCD)) {
569                                                 /* Carrier has been lost  */
570 #ifdef ISICOM_DEBUG                                             
571                                                         printk(KERN_DEBUG "ISICOM: interrupt: DCD->low.\n");
572 #endif                                                  
573                                                         port->status &= ~ISI_DCD;
574                                                         schedule_work(&port->hangup_tq);
575                                                 }
576                                         }
577                                         else {
578                                                 if (header & ISI_DCD) {
579                                                 /* Carrier has been detected */
580 #ifdef ISICOM_DEBUG
581                                                         printk(KERN_DEBUG "ISICOM: interrupt: DCD->high.\n");
582 #endif                                                  
583                                                         port->status |= ISI_DCD;
584                                                         wake_up_interruptible(&port->open_wait);
585                                                 }
586                                         }
587                                 }
588                                 else {
589                                         if (header & ISI_DCD) 
590                                                 port->status |= ISI_DCD;
591                                         else
592                                                 port->status &= ~ISI_DCD;
593                                 }       
594                                 
595                                 if (port->flags & ASYNC_CTS_FLOW) {
596                                         if (port->tty->hw_stopped) {
597                                                 if (header & ISI_CTS) {
598                                                         port->tty->hw_stopped = 0;
599                                                         /* start tx ing */
600                                                         port->status |= (ISI_TXOK | ISI_CTS);
601                                                         schedule_work(&port->bh_tqueue);
602                                                 }
603                                         }
604                                         else {
605                                                 if (!(header & ISI_CTS)) {
606                                                         port->tty->hw_stopped = 1;
607                                                         /* stop tx ing */
608                                                         port->status &= ~(ISI_TXOK | ISI_CTS);
609                                                 }
610                                         }
611                                 }
612                                 else {
613                                         if (header & ISI_CTS) 
614                                                 port->status |= ISI_CTS;
615                                         else
616                                                 port->status &= ~ISI_CTS;
617                                 }
618                                 
619                                 if (header & ISI_DSR) 
620                                         port->status |= ISI_DSR;
621                                 else
622                                         port->status &= ~ISI_DSR;
623                                 
624                                 if (header & ISI_RI) 
625                                         port->status |= ISI_RI;
626                                 else
627                                         port->status &= ~ISI_RI;                                                
628                                 
629                                 break;
630                                 
631                         case 1: /* Received Break !!!    */
632                                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
633                                         break;
634                                 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
635                                 /* dunno if this is right */    
636                                 *tty->flip.char_buf_ptr++ = 0;
637                                 tty->flip.count++;
638                                 if (port->flags & ASYNC_SAK)
639                                         do_SAK(tty);
640                                 schedule_delayed_work(&tty->flip.work, 1);
641                                 break;
642                                 
643                         case 2: /* Statistics            */
644                                 printk(KERN_DEBUG "ISICOM: isicom_interrupt: stats!!!.\n");                     
645                                 break;
646                                 
647                         default:
648                                 printk(KERN_WARNING "ISICOM: Intr: Unknown code in status packet.\n");
649                                 break;
650                 }        
651         }
652         else {                          /* Data   Packet */
653                 count = MIN(byte_count, (TTY_FLIPBUF_SIZE - tty->flip.count));
654 #ifdef ISICOM_DEBUG
655                 printk(KERN_DEBUG "ISICOM: Intr: Can rx %d of %d bytes.\n", 
656                                         count, byte_count);
657 #endif                  
658                 word_count = count >> 1;
659                 insw(base, tty->flip.char_buf_ptr, word_count);
660                 tty->flip.char_buf_ptr += (word_count << 1);            
661                 byte_count -= (word_count << 1);
662                 if (count & 0x0001) {
663                         *tty->flip.char_buf_ptr++ = (char)(inw(base) & 0xff);
664                         byte_count -= 2;
665                 }       
666                 memset(tty->flip.flag_buf_ptr, 0, count);
667                 tty->flip.flag_buf_ptr += count;
668                 tty->flip.count += count;
669                 
670                 if (byte_count > 0) {
671                         printk(KERN_DEBUG "ISICOM: Intr(0x%x:%d): Flip buffer overflow! dropping bytes...\n",
672                                         base, channel+1);
673                         while(byte_count > 0) { /* drain out unread xtra data */
674                                 inw(base);
675                                 byte_count -= 2;
676                         }
677                 }
678                 schedule_delayed_work(&tty->flip.work, 1);
679         }
680         if (card->isa == YES)
681                 ClearInterrupt(base);
682         else
683                 outw(0x0000, base+0x04); /* enable interrupts */        
684         return IRQ_HANDLED;
685
686
687  /* called with interrupts disabled */ 
688 static void isicom_config_port(struct isi_port * port)
689 {
690         struct isi_board * card = port->card;
691         struct tty_struct * tty;
692         unsigned long baud;
693         unsigned short channel_setup, wait, base = card->base;
694         unsigned short channel = port->channel, shift_count = card->shift_count;
695         unsigned char flow_ctrl;
696         
697         if (!(tty = port->tty) || !tty->termios)
698                 return;
699         baud = C_BAUD(tty);
700         if (baud & CBAUDEX) {
701                 baud &= ~CBAUDEX;
702                 
703                 /*  if CBAUDEX bit is on and the baud is set to either 50 or 75
704                  *  then the card is programmed for 57.6Kbps or 115Kbps
705                  *  respectively.
706                  */   
707                  
708                 if (baud < 1 || baud > 2)
709                         port->tty->termios->c_cflag &= ~CBAUDEX;
710                 else
711                         baud += 15;
712         }       
713         if (baud == 15) {
714         
715                 /*  the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set 
716                  *  by the set_serial_info ioctl ... this is done by
717                  *  the 'setserial' utility.
718                  */  
719                         
720                 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
721                         baud++;     /*  57.6 Kbps */
722                 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
723                         baud +=2;   /*  115  Kbps */     
724         }
725         if (linuxb_to_isib[baud] == -1) {
726                 /* hang up */
727                 drop_dtr(port);
728                 return;
729         }       
730         else  
731                 raise_dtr(port);
732                 
733         wait = 100;     
734         while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));     
735         if (!wait) {
736                 printk(KERN_WARNING "ISICOM: Card found busy in isicom_config_port at channel setup.\n");
737                 return;
738         }                        
739         outw(0x8000 | (channel << shift_count) |0x03, base);
740         outw(linuxb_to_isib[baud] << 8 | 0x03, base);
741         channel_setup = 0;
742         switch(C_CSIZE(tty)) {
743                 case CS5:
744                         channel_setup |= ISICOM_CS5;
745                         break;
746                 case CS6:
747                         channel_setup |= ISICOM_CS6;
748                         break;
749                 case CS7:
750                         channel_setup |= ISICOM_CS7;
751                         break;
752                 case CS8:
753                         channel_setup |= ISICOM_CS8;
754                         break;
755         }
756                 
757         if (C_CSTOPB(tty))
758                 channel_setup |= ISICOM_2SB;
759         
760         if (C_PARENB(tty))
761                 channel_setup |= ISICOM_EVPAR;
762         if (C_PARODD(tty))
763                 channel_setup |= ISICOM_ODPAR;  
764         outw(channel_setup, base);      
765         InterruptTheCard(base);
766         
767         if (C_CLOCAL(tty))
768                 port->flags &= ~ASYNC_CHECK_CD;
769         else
770                 port->flags |= ASYNC_CHECK_CD;  
771         
772         /* flow control settings ...*/
773         flow_ctrl = 0;
774         port->flags &= ~ASYNC_CTS_FLOW;
775         if (C_CRTSCTS(tty)) {
776                 port->flags |= ASYNC_CTS_FLOW;
777                 flow_ctrl |= ISICOM_CTSRTS;
778         }       
779         if (I_IXON(tty))        
780                 flow_ctrl |= ISICOM_RESPOND_XONXOFF;
781         if (I_IXOFF(tty))
782                 flow_ctrl |= ISICOM_INITIATE_XONXOFF;   
783                 
784         wait = 100;     
785         while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));     
786         if (!wait) {
787                 printk(KERN_WARNING "ISICOM: Card found busy in isicom_config_port at flow setup.\n");
788                 return;
789         }                        
790         outw(0x8000 | (channel << shift_count) |0x04, base);
791         outw(flow_ctrl << 8 | 0x05, base);
792         outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
793         InterruptTheCard(base);
794         
795         /*      rx enabled -> enable port for rx on the card    */
796         if (C_CREAD(tty)) {
797                 card->port_status |= (1 << channel);
798                 outw(card->port_status, base + 0x02);
799         }
800                 
801 }
802  
803 /* open et all */ 
804
805 static inline void isicom_setup_board(struct isi_board * bp)
806 {
807         int channel;
808         struct isi_port * port;
809         unsigned long flags;
810         
811         if (bp->status & BOARD_ACTIVE) 
812                 return;
813         port = bp->ports;
814 #ifdef ISICOM_DEBUG     
815         printk(KERN_DEBUG "ISICOM: setup_board: drop_dtr_rts start, port_count %d...\n", bp->port_count);
816 #endif
817         for(channel = 0; channel < bp->port_count; channel++, port++) {
818                 save_flags(flags); cli();
819                 drop_dtr_rts(port);
820                 restore_flags(flags);
821         }
822 #ifdef ISICOM_DEBUG             
823         printk(KERN_DEBUG "ISICOM: setup_board: drop_dtr_rts stop...\n");       
824 #endif  
825         
826         bp->status |= BOARD_ACTIVE;
827         return;
828 }
829  
830 static int isicom_setup_port(struct isi_port * port)
831 {
832         struct isi_board * card = port->card;
833         unsigned long flags;
834         
835         if (port->flags & ASYNC_INITIALIZED)
836                 return 0;
837         if (!port->xmit_buf) {
838                 unsigned long page;
839                 
840                 if (!(page = get_zeroed_page(GFP_KERNEL)))
841                         return -ENOMEM;
842                 
843                 if (port->xmit_buf) {
844                         free_page(page);
845                         return -ERESTARTSYS;
846                 }
847                 port->xmit_buf = (unsigned char *) page;        
848         }       
849         save_flags(flags); cli();
850         if (port->tty)
851                 clear_bit(TTY_IO_ERROR, &port->tty->flags);
852         if (port->count == 1)
853                 card->count++;
854                 
855         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
856         
857         /*      discard any residual data       */
858         kill_queue(port, ISICOM_KILLTX | ISICOM_KILLRX);
859         
860         isicom_config_port(port);
861         port->flags |= ASYNC_INITIALIZED;
862         
863         restore_flags(flags);
864         
865         return 0;               
866
867  
868 static int block_til_ready(struct tty_struct * tty, struct file * filp, struct isi_port * port) 
869 {
870         int do_clocal = 0, retval;
871         DECLARE_WAITQUEUE(wait, current);
872
873         /* block if port is in the process of being closed */
874
875         if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
876 #ifdef ISICOM_DEBUG     
877                 printk(KERN_DEBUG "ISICOM: block_til_ready: close in progress.\n");
878 #endif          
879                 interruptible_sleep_on(&port->close_wait);
880                 if (port->flags & ASYNC_HUP_NOTIFY)
881                         return -EAGAIN;
882                 else
883                         return -ERESTARTSYS;
884         }
885         
886         /* if non-blocking mode is set ... */
887         
888         if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
889 #ifdef ISICOM_DEBUG     
890                 printk(KERN_DEBUG "ISICOM: block_til_ready: non-block mode.\n");
891 #endif          
892                 port->flags |= ASYNC_NORMAL_ACTIVE;
893                 return 0;       
894         }       
895         
896         if (C_CLOCAL(tty))
897                 do_clocal = 1;
898 #ifdef ISICOM_DEBUG     
899         if (do_clocal)
900                 printk(KERN_DEBUG "ISICOM: block_til_ready: CLOCAL set.\n");
901 #endif          
902         
903         /* block waiting for DCD to be asserted, and while 
904                                                 callout dev is busy */
905         retval = 0;
906         add_wait_queue(&port->open_wait, &wait);
907         cli();
908                 if (!tty_hung_up_p(filp))
909                         port->count--;
910         sti();
911         port->blocked_open++;
912 #ifdef ISICOM_DEBUG     
913         printk(KERN_DEBUG "ISICOM: block_til_ready: waiting for DCD...\n");
914 #endif  
915         while (1) {
916                 cli();
917                 raise_dtr_rts(port);
918                 sti();
919                 set_current_state(TASK_INTERRUPTIBLE);
920                 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {        
921                         if (port->flags & ASYNC_HUP_NOTIFY)
922                                 retval = -EAGAIN;
923                         else
924                                 retval = -ERESTARTSYS;
925 #ifdef ISICOM_DEBUG                             
926                         printk(KERN_DEBUG "ISICOM: block_til_ready: tty_hung_up_p || not init.\n"); 
927 #endif                  
928                         break;
929                 }       
930                 if (!(port->flags & ASYNC_CLOSING) &&
931                     (do_clocal || (port->status & ISI_DCD))) {
932 #ifdef ISICOM_DEBUG                 
933                         printk(KERN_DEBUG "ISICOM: block_til_ready: do_clocal || DCD.\n");   
934 #endif                  
935                         break;
936                 }       
937                 if (signal_pending(current)) {
938 #ifdef ISICOM_DEBUG             
939                         printk(KERN_DEBUG "ISICOM: block_til_ready: sig blocked.\n");
940 #endif                  
941                         retval = -ERESTARTSYS;
942                         break;
943                 }
944                 schedule();             
945         }
946         set_current_state(TASK_RUNNING);
947         remove_wait_queue(&port->open_wait, &wait);
948         if (!tty_hung_up_p(filp))
949                 port->count++;
950         port->blocked_open--;
951         if (retval)
952                 return retval;
953         port->flags |= ASYNC_NORMAL_ACTIVE;
954         return 0;
955 }
956  
957 static int isicom_open(struct tty_struct * tty, struct file * filp)
958 {
959         struct isi_port * port;
960         struct isi_board * card;
961         unsigned int line, board;
962         int error;
963
964 #ifdef ISICOM_DEBUG     
965         printk(KERN_DEBUG "ISICOM: open start!!!.\n");
966 #endif  
967         line = tty->index;
968         
969 #ifdef ISICOM_DEBUG     
970         printk(KERN_DEBUG "line = %d.\n", line);
971 #endif  
972         
973         if ((line < 0) || (line > (PORT_COUNT-1)))
974                 return -ENODEV;
975         board = BOARD(line);
976         
977 #ifdef ISICOM_DEBUG     
978         printk(KERN_DEBUG "board = %d.\n", board);
979 #endif  
980         
981         card = &isi_card[board];
982         if (!(card->status & FIRMWARE_LOADED)) {
983 #ifdef ISICOM_DEBUG     
984                 printk(KERN_DEBUG"ISICOM: Firmware not loaded to card%d.\n", board);
985 #endif          
986                 return -ENODEV;
987         }
988         
989         /*  open on a port greater than the port count for the card !!! */
990         if (line > ((board * 16) + card->port_count - 1)) {
991                 printk(KERN_ERR "ISICOM: Open on a port which exceeds the port_count of the card!\n");
992                 return -ENODEV;
993         }       
994         port = &isi_ports[line];        
995         if (isicom_paranoia_check(port, tty->name, "isicom_open"))
996                 return -ENODEV;
997                 
998 #ifdef ISICOM_DEBUG             
999         printk(KERN_DEBUG "ISICOM: isicom_setup_board ...\n");          
1000 #endif  
1001         isicom_setup_board(card);               
1002         
1003         port->count++;
1004         tty->driver_data = port;
1005         port->tty = tty;
1006 #ifdef ISICOM_DEBUG     
1007         printk(KERN_DEBUG "ISICOM: isicom_setup_port ...\n");
1008 #endif  
1009         if ((error = isicom_setup_port(port))!=0)
1010                 return error;
1011 #ifdef ISICOM_DEBUG             
1012         printk(KERN_DEBUG "ISICOM: block_til_ready ...\n");     
1013 #endif  
1014         if ((error = block_til_ready(tty, filp, port))!=0)
1015                 return error;
1016
1017 #ifdef ISICOM_DEBUG     
1018         printk(KERN_DEBUG "ISICOM: open end!!!.\n");
1019 #endif  
1020         return 0;               
1021 }
1022  
1023 /* close et all */
1024
1025 static inline void isicom_shutdown_board(struct isi_board * bp)
1026 {
1027         int channel;
1028         struct isi_port * port;
1029         
1030         if (!(bp->status & BOARD_ACTIVE))
1031                 return;
1032         bp->status &= ~BOARD_ACTIVE;
1033         port = bp->ports;
1034         for(channel = 0; channel < bp->port_count; channel++, port++) {
1035                 drop_dtr_rts(port);
1036         }       
1037 }
1038
1039 static void isicom_shutdown_port(struct isi_port * port)
1040 {
1041         struct isi_board * card = port->card;
1042         struct tty_struct * tty;        
1043         
1044         if (!(port->flags & ASYNC_INITIALIZED))
1045                 return;
1046         if (port->xmit_buf) {
1047                 free_page((unsigned long) port->xmit_buf);
1048                 port->xmit_buf = NULL;
1049         }       
1050         if (!(tty = port->tty) || C_HUPCL(tty)) 
1051                 /* drop dtr on this port */
1052                 drop_dtr(port);
1053                 
1054         /* any other port uninits  */ 
1055         
1056         if (tty)
1057                 set_bit(TTY_IO_ERROR, &tty->flags);
1058         port->flags &= ~ASYNC_INITIALIZED;
1059         
1060         if (--card->count < 0) {
1061                 printk(KERN_DEBUG "ISICOM: isicom_shutdown_port: bad board(0x%x) count %d.\n",
1062                         card->base, card->count);
1063                 card->count = 0;        
1064         }
1065         
1066         /* last port was closed , shutdown that boad too */
1067         if (!card->count)
1068                 isicom_shutdown_board(card);
1069 }
1070
1071 static void isicom_close(struct tty_struct * tty, struct file * filp)
1072 {
1073         struct isi_port * port = (struct isi_port *) tty->driver_data;
1074         struct isi_board * card = port->card;
1075         unsigned long flags;
1076         
1077         if (!port)
1078                 return;
1079         if (isicom_paranoia_check(port, tty->name, "isicom_close"))
1080                 return;
1081         
1082 #ifdef ISICOM_DEBUG             
1083         printk(KERN_DEBUG "ISICOM: Close start!!!.\n");
1084 #endif  
1085         
1086         save_flags(flags); cli();
1087         if (tty_hung_up_p(filp)) {
1088                 restore_flags(flags);
1089                 return;
1090         }
1091         
1092         if ((tty->count == 1) && (port->count != 1)) {
1093                 printk(KERN_WARNING "ISICOM:(0x%x) isicom_close: bad port count"
1094                         "tty->count = 1 port count = %d.\n",
1095                         card->base, port->count);
1096                 port->count = 1;
1097         }
1098         if (--port->count < 0) {
1099                 printk(KERN_WARNING "ISICOM:(0x%x) isicom_close: bad port count for"
1100                         "channel%d = %d", card->base, port->channel, 
1101                         port->count);
1102                 port->count = 0;        
1103         }
1104         
1105         if (port->count) {
1106                 restore_flags(flags);
1107                 return;
1108         }       
1109         port->flags |= ASYNC_CLOSING;
1110         tty->closing = 1;
1111         if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1112                 tty_wait_until_sent(tty, port->closing_wait);
1113         /* indicate to the card that no more data can be received 
1114            on this port */
1115         if (port->flags & ASYNC_INITIALIZED) {   
1116                 card->port_status &= ~(1 << port->channel);
1117                 outw(card->port_status, card->base + 0x02);
1118         }       
1119         isicom_shutdown_port(port);
1120         if (tty->driver->flush_buffer)
1121                 tty->driver->flush_buffer(tty);
1122         if (tty->ldisc.flush_buffer)
1123                 tty->ldisc.flush_buffer(tty);
1124         tty->closing = 0;
1125         port->tty = NULL;
1126         if (port->blocked_open) {
1127                 if (port->close_delay) {
1128                         set_current_state(TASK_INTERRUPTIBLE);
1129 #ifdef ISICOM_DEBUG                     
1130                         printk(KERN_DEBUG "ISICOM: scheduling until time out.\n");
1131 #endif                  
1132                         schedule_timeout(port->close_delay);
1133                 }
1134                 wake_up_interruptible(&port->open_wait);
1135         }       
1136         port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1137         wake_up_interruptible(&port->close_wait);
1138         restore_flags(flags);
1139 #ifdef ISICOM_DEBUG     
1140         printk(KERN_DEBUG "ISICOM: Close end!!!.\n");
1141 #endif  
1142 }
1143
1144 /* write et all */
1145 static int isicom_write(struct tty_struct * tty, int from_user,
1146                         const unsigned char * buf, int count)
1147 {
1148         struct isi_port * port = (struct isi_port *) tty->driver_data;
1149         unsigned long flags;
1150         int cnt, total = 0;
1151 #ifdef ISICOM_DEBUG
1152         printk(KERN_DEBUG "ISICOM: isicom_write for port%d: %d bytes.\n",
1153                         port->channel+1, count);
1154 #endif          
1155         if (isicom_paranoia_check(port, tty->name, "isicom_write"))
1156                 return 0;
1157         
1158         if (!tty || !port->xmit_buf || !tmp_buf)
1159                 return 0;
1160         if (from_user)
1161                 down(&tmp_buf_sem); /* acquire xclusive access to tmp_buf */
1162                 
1163         save_flags(flags);
1164         while(1) {      
1165                 cli();
1166                 cnt = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1167                         SERIAL_XMIT_SIZE - port->xmit_head));
1168                 if (cnt <= 0) 
1169                         break;
1170                 
1171                 if (from_user) {
1172                         /* the following may block for paging... hence 
1173                            enabling interrupts but tx routine may have 
1174                            created more space in xmit_buf when the ctrl 
1175                            gets back here  */
1176                         sti(); 
1177                         if (copy_from_user(tmp_buf, buf, cnt)) {
1178                                 up(&tmp_buf_sem);
1179                                 restore_flags(flags);
1180                                 return -EFAULT;
1181                         }
1182                         cli();
1183                         cnt = MIN(cnt, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1184                         SERIAL_XMIT_SIZE - port->xmit_head));
1185                         memcpy(port->xmit_buf + port->xmit_head, tmp_buf, cnt);
1186                 }       
1187                 else
1188                         memcpy(port->xmit_buf + port->xmit_head, buf, cnt);
1189                 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE - 1);
1190                 port->xmit_cnt += cnt;
1191                 restore_flags(flags);
1192                 buf += cnt;
1193                 count -= cnt;
1194                 total += cnt;
1195         }               
1196         if (from_user)
1197                 up(&tmp_buf_sem);
1198         if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1199                 port->status |= ISI_TXOK;
1200         restore_flags(flags);
1201 #ifdef ISICOM_DEBUG
1202         printk(KERN_DEBUG "ISICOM: isicom_write %d bytes written.\n", total);
1203 #endif          
1204         return total;   
1205 }
1206
1207 /* put_char et all */
1208 static void isicom_put_char(struct tty_struct * tty, unsigned char ch)
1209 {
1210         struct isi_port * port = (struct isi_port *) tty->driver_data;
1211         unsigned long flags;
1212         
1213         if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
1214                 return;
1215         
1216         if (!tty || !port->xmit_buf)
1217                 return;
1218 #ifdef ISICOM_DEBUG
1219         printk(KERN_DEBUG "ISICOM: put_char, port %d, char %c.\n", port->channel+1, ch);
1220 #endif                  
1221                 
1222         save_flags(flags); cli();
1223         
1224         if (port->xmit_cnt >= (SERIAL_XMIT_SIZE - 1)) {
1225                 restore_flags(flags);
1226                 return;
1227         }
1228         
1229         port->xmit_buf[port->xmit_head++] = ch;
1230         port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
1231         port->xmit_cnt++;
1232         restore_flags(flags);
1233 }
1234
1235 /* flush_chars et all */
1236 static void isicom_flush_chars(struct tty_struct * tty)
1237 {
1238         struct isi_port * port = (struct isi_port *) tty->driver_data;
1239         
1240         if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
1241                 return;
1242         
1243         if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1244             !port->xmit_buf)
1245                 return;
1246                 
1247         /* this tells the transmitter to consider this port for
1248            data output to the card ... that's the best we can do. */
1249         port->status |= ISI_TXOK;       
1250 }
1251
1252 /* write_room et all */
1253 static int isicom_write_room(struct tty_struct * tty)
1254 {
1255         struct isi_port * port = (struct isi_port *) tty->driver_data;
1256         int free;
1257         if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
1258                 return 0;
1259         
1260         free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1261         if (free < 0)
1262                 free = 0;
1263         return free;
1264 }
1265
1266 /* chars_in_buffer et all */
1267 static int isicom_chars_in_buffer(struct tty_struct * tty)
1268 {
1269         struct isi_port * port = (struct isi_port *) tty->driver_data;
1270         if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
1271                 return 0;
1272         return port->xmit_cnt;
1273 }
1274
1275 /* ioctl et all */
1276 static inline void isicom_send_break(struct isi_port * port, unsigned long length)
1277 {
1278         struct isi_board * card = port->card;
1279         short wait = 10;
1280         unsigned short base = card->base;       
1281         unsigned long flags;
1282         
1283         save_flags(flags); cli();
1284         while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));     
1285         if (!wait) {
1286                 printk(KERN_DEBUG "ISICOM: Card found busy in isicom_send_break.\n");
1287                 goto out;
1288         }       
1289         outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1290         outw((length & 0xff) << 8 | 0x00, base);
1291         outw((length & 0xff00), base);
1292         InterruptTheCard(base);
1293 out:    restore_flags(flags);
1294 }
1295
1296 static int isicom_tiocmget(struct tty_struct *tty, struct file *file)
1297 {
1298         struct isi_port * port = (struct isi_port *) tty->driver_data;
1299         /* just send the port status */
1300         unsigned short status = port->status;
1301
1302         if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1303                 return -ENODEV;
1304         
1305         return  ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1306                 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1307                 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1308                 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1309                 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1310                 ((status & ISI_RI ) ? TIOCM_RI  : 0);
1311 }
1312
1313 static int isicom_tiocmset(struct tty_struct *tty, struct file *file,
1314                            unsigned int set, unsigned int clear)
1315 {
1316         struct isi_port * port = (struct isi_port *) tty->driver_data;
1317         unsigned long flags;
1318         
1319         if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1320                 return -ENODEV;
1321         
1322         save_flags(flags); cli();
1323         if (set & TIOCM_RTS)
1324                 raise_rts(port);
1325         if (set & TIOCM_DTR)
1326                 raise_dtr(port);
1327
1328         if (clear & TIOCM_RTS)
1329                 drop_rts(port);
1330         if (clear & TIOCM_DTR)
1331                 drop_dtr(port);
1332
1333         restore_flags(flags);
1334         return 0;
1335 }                       
1336
1337 static int isicom_set_serial_info(struct isi_port * port,
1338                                         struct serial_struct __user *info)
1339 {
1340         struct serial_struct newinfo;
1341         unsigned long flags;
1342         int reconfig_port;
1343
1344         if(copy_from_user(&newinfo, info, sizeof(newinfo)))
1345                 return -EFAULT;
1346                 
1347         reconfig_port = ((port->flags & ASYNC_SPD_MASK) != 
1348                          (newinfo.flags & ASYNC_SPD_MASK));
1349         
1350         if (!capable(CAP_SYS_ADMIN)) {
1351                 if ((newinfo.close_delay != port->close_delay) ||
1352                     (newinfo.closing_wait != port->closing_wait) ||
1353                     ((newinfo.flags & ~ASYNC_USR_MASK) != 
1354                      (port->flags & ~ASYNC_USR_MASK)))
1355                         return -EPERM;
1356                 port->flags = ((port->flags & ~ ASYNC_USR_MASK) |
1357                                 (newinfo.flags & ASYNC_USR_MASK));
1358         }       
1359         else {
1360                 port->close_delay = newinfo.close_delay;
1361                 port->closing_wait = newinfo.closing_wait; 
1362                 port->flags = ((port->flags & ~ASYNC_FLAGS) | 
1363                                 (newinfo.flags & ASYNC_FLAGS));
1364         }
1365         if (reconfig_port) {
1366                 save_flags(flags); cli();
1367                 isicom_config_port(port);
1368                 restore_flags(flags);
1369         }
1370         return 0;                
1371 }               
1372
1373 static int isicom_get_serial_info(struct isi_port * port, 
1374                                         struct serial_struct __user *info)
1375 {
1376         struct serial_struct out_info;
1377         
1378         memset(&out_info, 0, sizeof(out_info));
1379 /*      out_info.type = ? */
1380         out_info.line = port - isi_ports;
1381         out_info.port = port->card->base;
1382         out_info.irq = port->card->irq;
1383         out_info.flags = port->flags;
1384 /*      out_info.baud_base = ? */
1385         out_info.close_delay = port->close_delay;
1386         out_info.closing_wait = port->closing_wait;
1387         if(copy_to_user(info, &out_info, sizeof(out_info)))
1388                 return -EFAULT;
1389         return 0;
1390 }                                       
1391
1392 static int isicom_ioctl(struct tty_struct * tty, struct file * filp,
1393                         unsigned int cmd, unsigned long arg) 
1394 {
1395         struct isi_port * port = (struct isi_port *) tty->driver_data;
1396         void __user *argp = (void __user *)arg;
1397         int retval;
1398
1399         if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1400                 return -ENODEV;
1401
1402         switch(cmd) {
1403                 case TCSBRK:
1404                         retval = tty_check_change(tty);
1405                         if (retval)
1406                                 return retval;
1407                         tty_wait_until_sent(tty, 0);
1408                         if (!arg)
1409                                 isicom_send_break(port, HZ/4);
1410                         return 0;
1411                         
1412                 case TCSBRKP:   
1413                         retval = tty_check_change(tty);
1414                         if (retval)
1415                                 return retval;
1416                         tty_wait_until_sent(tty, 0);
1417                         isicom_send_break(port, arg ? arg * (HZ/10) : HZ/4);
1418                         return 0;
1419                         
1420                 case TIOCGSOFTCAR:
1421                         return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1422                         
1423                 case TIOCSSOFTCAR:
1424                         if(get_user(arg, (unsigned long __user *) argp))
1425                                 return -EFAULT;
1426                         tty->termios->c_cflag =
1427                                 ((tty->termios->c_cflag & ~CLOCAL) |
1428                                 (arg ? CLOCAL : 0));
1429                         return 0;       
1430                         
1431                 case TIOCGSERIAL:
1432                         return isicom_get_serial_info(port, argp);
1433                 
1434                 case TIOCSSERIAL:
1435                         return isicom_set_serial_info(port, argp);
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 = NULL;
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 }