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