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