- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / drivers / char / isicom.c
blob5f5a94fbc5ecd9466ecf2f8c18bfef284dcb932e
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.
7 * Original driver code supplied by Multi-Tech
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
17 * 10/6/99 sameer Merged the ISA and PCI drivers to
18 * a new unified driver.
19 * ***********************************************************
21 * To use this driver you also need the support package. You
22 * can find this in RPM format on
23 * ftp://ftp.linux.org.uk/pub/linux/alan
25 * You can find the original tools for this direct from Multitech
26 * ftp://ftp.multitech.com/ISI-Cards/
28 * Having installed the cards the module options (/etc/modules.conf)
30 * options isicom io=card1,card2,card3,card4 irq=card1,card2,card3,card4
32 * Omit those entries for boards you don't have installed.
36 #include <linux/module.h>
37 #include <linux/version.h>
38 #include <linux/kernel.h>
39 #include <linux/tty.h>
40 #include <linux/termios.h>
41 #include <linux/fs.h>
42 #include <linux/sched.h>
43 #include <linux/serial.h>
44 #include <linux/mm.h>
45 #include <linux/miscdevice.h>
46 #include <linux/interrupt.h>
47 #include <linux/timer.h>
48 #include <linux/ioport.h>
50 #include <asm/segment.h>
51 #include <asm/uaccess.h>
52 #include <asm/io.h>
53 #include <asm/system.h>
55 #include <linux/pci.h>
57 #include <linux/isicom.h>
59 static int device_id[] = { 0x2028,
60 0x2051,
61 0x2052,
62 0x2053,
63 0x2054,
64 0x2055,
65 0x2056,
66 0x2057,
67 0x2058
70 static int isicom_refcount = 0;
71 static int prev_card = 3; /* start servicing isi_card[0] */
72 static struct isi_board * irq_to_board[16] = { NULL, };
73 static struct tty_driver isicom_normal, isicom_callout;
74 static struct tty_struct * isicom_table[PORT_COUNT] = { NULL, };
75 static struct termios * isicom_termios[PORT_COUNT] = { NULL, };
76 static struct termios * isicom_termios_locked[PORT_COUNT] = { NULL, };
78 static struct isi_board isi_card[BOARD_COUNT];
79 static struct isi_port isi_ports[PORT_COUNT];
81 DECLARE_TASK_QUEUE(tq_isicom);
83 static struct timer_list tx;
84 static char re_schedule = 1;
85 #ifdef ISICOM_DEBUG
86 static unsigned long tx_count = 0;
87 #endif
89 static int ISILoad_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
91 static void isicom_tx(unsigned long _data);
92 static void isicom_start(struct tty_struct * tty);
94 static unsigned char * tmp_buf = 0;
95 static DECLARE_MUTEX(tmp_buf_sem);
97 /* baud index mappings from linux defns to isi */
99 static signed char linuxb_to_isib[] = {
100 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17,
101 18, 19
105 * Firmware loader driver specific routines
109 static struct file_operations ISILoad_fops = {
110 owner: THIS_MODULE,
111 ioctl: ISILoad_ioctl,
114 struct miscdevice isiloader_device = {
115 ISILOAD_MISC_MINOR, "isictl", &ISILoad_fops
119 extern inline int WaitTillCardIsFree(unsigned short base)
121 unsigned long count=0;
122 while( (!(inw(base+0xe) & 0x1)) && (count++ < 6000000));
123 if (inw(base+0xe)&0x1)
124 return 0;
125 else
126 return 1;
129 static int ISILoad_ioctl(struct inode *inode, struct file *filp,
130 unsigned int cmd, unsigned long arg)
132 unsigned int card, i, j, signature, status, portcount = 0;
133 unsigned short word_count, base;
134 bin_frame frame;
135 /* exec_record exec_rec; */
137 if(get_user(card, (int *)arg))
138 return -EFAULT;
140 if(card < 0 || card >= BOARD_COUNT)
141 return -ENXIO;
143 base=isi_card[card].base;
145 if(base==0)
146 return -ENXIO; /* disabled or not used */
148 switch(cmd) {
149 case MIOCTL_RESET_CARD:
150 if (!capable(CAP_SYS_ADMIN))
151 return -EPERM;
152 printk(KERN_DEBUG "ISILoad:Resetting Card%d at 0x%x ",card+1,base);
154 inw(base+0x8);
156 for(i=jiffies+HZ/100;time_before(jiffies, i););
158 outw(0,base+0x8); /* Reset */
160 for(j=1;j<=3;j++) {
161 for(i=jiffies+HZ;time_before(jiffies, i););
162 printk(".");
164 signature=(inw(base+0x4)) & 0xff;
165 if (isi_card[card].isa) {
167 if (!(inw(base+0xe) & 0x1) || (inw(base+0x2))) {
168 #ifdef ISICOM_DEBUG
169 printk("\nbase+0x2=0x%x , base+0xe=0x%x",inw(base+0x2),inw(base+0xe));
170 #endif
171 printk("\nISILoad:ISA Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
172 return -EIO;
175 else {
176 portcount = inw(base+0x2);
177 if (!(inw(base+0xe) & 0x1) || ((portcount!=0) && (portcount!=4) && (portcount!=8))) {
178 #ifdef ISICOM_DEBUG
179 printk("\nbase+0x2=0x%x , base+0xe=0x%x",inw(base+0x2),inw(base+0xe));
180 #endif
181 printk("\nISILoad:PCI Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
182 return -EIO;
185 switch(signature) {
186 case 0xa5:
187 case 0xbb:
188 case 0xdd:
189 if (isi_card[card].isa)
190 isi_card[card].port_count = 8;
191 else {
192 if (portcount == 4)
193 isi_card[card].port_count = 4;
194 else
195 isi_card[card].port_count = 8;
197 isi_card[card].shift_count = 12;
198 break;
200 case 0xcc: isi_card[card].port_count = 16;
201 isi_card[card].shift_count = 11;
202 break;
204 default: printk("ISILoad:Card%d reset failure (Possible bad I/O Port Address 0x%x).\n",card+1,base);
205 #ifdef ISICOM_DEBUG
206 printk("Sig=0x%x\n",signature);
207 #endif
208 return -EIO;
210 printk("-Done\n");
211 return put_user(signature,(unsigned int*)arg);
213 case MIOCTL_LOAD_FIRMWARE:
214 if (!capable(CAP_SYS_ADMIN))
215 return -EPERM;
217 if(copy_from_user(&frame, (void *) arg, sizeof(bin_frame)))
218 return -EFAULT;
220 if (WaitTillCardIsFree(base))
221 return -EIO;
223 outw(0xf0,base); /* start upload sequence */
224 outw(0x00,base);
225 outw((frame.addr), base);/* lsb of adderess */
227 word_count=(frame.count >> 1) + frame.count % 2;
228 outw(word_count, base);
229 InterruptTheCard(base);
231 for(i=0;i<=0x2f;i++); /* a wee bit of delay */
233 if (WaitTillCardIsFree(base))
234 return -EIO;
236 if ((status=inw(base+0x4))!=0) {
237 printk(KERN_WARNING "ISILoad:Card%d rejected load header:\nAddress:0x%x \nCount:0x%x \nStatus:0x%x \n",
238 card+1, frame.addr, frame.count, status);
239 return -EIO;
241 outsw(base, (void *) frame.bin_data, word_count);
243 InterruptTheCard(base);
245 for(i=0;i<=0x0f;i++); /* another wee bit of delay */
247 if (WaitTillCardIsFree(base))
248 return -EIO;
250 if ((status=inw(base+0x4))!=0) {
251 printk(KERN_ERR "ISILoad:Card%d got out of sync.Card Status:0x%x\n",card+1, status);
252 return -EIO;
254 return 0;
256 case MIOCTL_READ_FIRMWARE:
257 if (!capable(CAP_SYS_ADMIN))
258 return -EPERM;
260 if(copy_from_user(&frame, (void *) arg, sizeof(bin_header)))
261 return -EFAULT;
263 if (WaitTillCardIsFree(base))
264 return -EIO;
266 outw(0xf1,base); /* start download sequence */
267 outw(0x00,base);
268 outw((frame.addr), base);/* lsb of adderess */
270 word_count=(frame.count >> 1) + frame.count % 2;
271 outw(word_count+1, base);
272 InterruptTheCard(base);
274 for(i=0;i<=0xf;i++); /* a wee bit of delay */
276 if (WaitTillCardIsFree(base))
277 return -EIO;
279 if ((status=inw(base+0x4))!=0) {
280 printk(KERN_WARNING "ISILoad:Card%d rejected verify header:\nAddress:0x%x \nCount:0x%x \nStatus:0x%x \n",
281 card+1, frame.addr, frame.count, status);
282 return -EIO;
285 inw(base);
286 insw(base, frame.bin_data, word_count);
287 InterruptTheCard(base);
289 for(i=0;i<=0x0f;i++); /* another wee bit of delay */
291 if (WaitTillCardIsFree(base))
292 return -EIO;
294 if ((status=inw(base+0x4))!=0) {
295 printk(KERN_ERR "ISILoad:Card%d verify got out of sync.Card Status:0x%x\n",card+1, status);
296 return -EIO;
299 if(copy_to_user((void *) arg, &frame, sizeof(bin_frame)))
300 return -EFAULT;
301 return 0;
303 case MIOCTL_XFER_CTRL:
304 if (!capable(CAP_SYS_ADMIN))
305 return -EPERM;
306 if (WaitTillCardIsFree(base))
307 return -EIO;
309 outw(0xf2, base);
310 outw(0x800, base);
311 outw(0x0, base);
312 outw(0x0, base);
313 InterruptTheCard(base);
314 outw(0x0, base+0x4); /* for ISI4608 cards */
316 isi_card[card].status |= FIRMWARE_LOADED;
317 return 0;
319 default:
320 #ifdef ISICOM_DEBUG
321 printk(KERN_DEBUG "ISILoad: Received Ioctl cmd 0x%x.\n", cmd);
322 #endif
323 return -ENOIOCTLCMD;
331 * ISICOM Driver specific routines ...
335 static inline int isicom_paranoia_check(struct isi_port const * port, kdev_t dev,
336 const char * routine)
338 #ifdef ISICOM_DEBUG
339 static const char * badmagic =
340 KERN_WARNING "ISICOM: Warning: bad isicom magic for dev %s in %s.\n";
341 static const char * badport =
342 KERN_WARNING "ISICOM: Warning: NULL isicom port for dev %s in %s.\n";
343 if (!port) {
344 printk(badport, kdevname(dev), routine);
345 return 1;
347 if (port->magic != ISICOM_MAGIC) {
348 printk(badmagic, kdevname(dev), routine);
349 return 1;
351 #endif
352 return 0;
355 extern inline void schedule_bh(struct isi_port * port)
357 queue_task(&port->bh_tqueue, &tq_isicom);
358 mark_bh(ISICOM_BH);
361 /* Transmitter */
363 static void isicom_tx(unsigned long _data)
365 short count = (BOARD_COUNT-1), card, base;
366 short txcount, wait, wrd, residue, word_count, cnt;
367 struct isi_port * port;
368 struct tty_struct * tty;
369 unsigned long flags;
371 #ifdef ISICOM_DEBUG
372 ++tx_count;
373 #endif
375 /* find next active board */
376 card = (prev_card + 1) & 0x0003;
377 while(count-- > 0) {
378 if (isi_card[card].status & BOARD_ACTIVE)
379 break;
380 card = (card + 1) & 0x0003;
382 if (!(isi_card[card].status & BOARD_ACTIVE))
383 goto sched_again;
385 prev_card = card;
387 count = isi_card[card].port_count;
388 port = isi_card[card].ports;
389 base = isi_card[card].base;
390 for (;count > 0;count--, port++) {
391 /* port not active or tx disabled to force flow control */
392 if (!(port->status & ISI_TXOK))
393 continue;
395 tty = port->tty;
396 save_flags(flags); cli();
397 txcount = MIN(TX_SIZE, port->xmit_cnt);
398 if ((txcount <= 0) || tty->stopped || tty->hw_stopped) {
399 restore_flags(flags);
400 continue;
402 wait = 200;
403 while(((inw(base+0x0e) & 0x01) == 0) && (wait-- > 0));
404 if (wait <= 0) {
405 restore_flags(flags);
406 #ifdef ISICOM_DEBUG
407 printk(KERN_DEBUG "ISICOM: isicom_tx:Card(0x%x) found busy.\n",
408 card);
409 #endif
410 continue;
412 if (!(inw(base + 0x02) & (1 << port->channel))) {
413 restore_flags(flags);
414 #ifdef ISICOM_DEBUG
415 printk(KERN_DEBUG "ISICOM: isicom_tx: cannot tx to 0x%x:%d.\n",
416 base, port->channel + 1);
417 #endif
418 continue;
420 #ifdef ISICOM_DEBUG
421 printk(KERN_DEBUG "ISICOM: txing %d bytes, port%d.\n",
422 txcount, port->channel+1);
423 #endif
424 outw((port->channel << isi_card[card].shift_count) | txcount
425 , base);
426 residue = NO;
427 wrd = 0;
428 while (1) {
429 cnt = MIN(txcount, (SERIAL_XMIT_SIZE - port->xmit_tail));
430 if (residue == YES) {
431 residue = NO;
432 if (cnt > 0) {
433 wrd |= (port->xmit_buf[port->xmit_tail] << 8);
434 port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
435 port->xmit_cnt--;
436 txcount--;
437 cnt--;
438 outw(wrd, base);
440 else {
441 outw(wrd, base);
442 break;
445 if (cnt <= 0) break;
446 word_count = cnt >> 1;
447 outsw(base, port->xmit_buf+port->xmit_tail, word_count);
448 port->xmit_tail = (port->xmit_tail + (word_count << 1)) &
449 (SERIAL_XMIT_SIZE - 1);
450 txcount -= (word_count << 1);
451 port->xmit_cnt -= (word_count << 1);
452 if (cnt & 0x0001) {
453 residue = YES;
454 wrd = port->xmit_buf[port->xmit_tail];
455 port->xmit_tail = (port->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
456 port->xmit_cnt--;
457 txcount--;
461 InterruptTheCard(base);
462 if (port->xmit_cnt <= 0)
463 port->status &= ~ISI_TXOK;
464 if (port->xmit_cnt <= WAKEUP_CHARS)
465 schedule_bh(port);
466 restore_flags(flags);
469 /* schedule another tx for hopefully in about 10ms */
470 sched_again:
471 if (!re_schedule)
472 return;
473 init_timer(&tx);
474 tx.expires = jiffies + HZ/100;
475 tx.data = 0;
476 tx.function = isicom_tx;
477 add_timer(&tx);
479 return;
482 /* Interrupt handlers */
484 static void do_isicom_bh(void)
486 run_task_queue(&tq_isicom);
491 static void isicom_bottomhalf(void * data)
493 struct isi_port * port = (struct isi_port *) data;
494 struct tty_struct * tty = port->tty;
496 if (!tty)
497 return;
499 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
500 tty->ldisc.write_wakeup)
501 (tty->ldisc.write_wakeup)(tty);
502 wake_up_interruptible(&tty->write_wait);
505 /* main interrupt handler routine */
506 static void isicom_interrupt(int irq, void * dev_id, struct pt_regs * regs)
508 struct isi_board * card;
509 struct isi_port * port;
510 struct tty_struct * tty;
511 unsigned short base, header, word_count, count;
512 unsigned char channel;
513 short byte_count;
516 * find the source of interrupt
519 for(count = 0; count < BOARD_COUNT; count++) {
520 card = &isi_card[count];
521 if (card->base != 0) {
522 if (((card->isa == YES) && (card->irq == irq)) ||
523 ((card->isa == NO) && (card->irq == irq) && (inw(card->base+0x0e) & 0x02)))
524 break;
526 card = NULL;
529 if (!card || !(card->status & FIRMWARE_LOADED)) {
530 /* printk(KERN_DEBUG "ISICOM: interrupt: not handling irq%d!.\n", irq);*/
531 return;
534 base = card->base;
535 if (card->isa == NO) {
537 * disable any interrupts from the PCI card and lower the
538 * interrupt line
540 outw(0x8000, base+0x04);
541 ClearInterrupt(base);
544 inw(base); /* get the dummy word out */
545 header = inw(base);
546 channel = (header & 0x7800) >> card->shift_count;
547 byte_count = header & 0xff;
548 #ifdef ISICOM_DEBUG
549 printk(KERN_DEBUG "ISICOM:Intr:(0x%x:%d).\n", base, channel+1);
550 #endif
551 if ((channel+1) > card->port_count) {
552 printk(KERN_WARNING "ISICOM: isicom_interrupt(0x%x): %d(channel) > port_count.\n",
553 base, channel+1);
554 if (card->isa)
555 ClearInterrupt(base);
556 else
557 outw(0x0000, base+0x04); /* enable interrupts */
558 return;
560 port = card->ports + channel;
561 if (!(port->flags & ASYNC_INITIALIZED)) {
562 if (card->isa)
563 ClearInterrupt(base);
564 else
565 outw(0x0000, base+0x04); /* enable interrupts */
566 return;
569 tty = port->tty;
571 if (header & 0x8000) { /* Status Packet */
572 header = inw(base);
573 switch(header & 0xff) {
574 case 0: /* Change in EIA signals */
576 if (port->flags & ASYNC_CHECK_CD) {
577 if (port->status & ISI_DCD) {
578 if (!(header & ISI_DCD)) {
579 /* Carrier has been lost */
580 #ifdef ISICOM_DEBUG
581 printk(KERN_DEBUG "ISICOM: interrupt: DCD->low.\n");
582 #endif
583 port->status &= ~ISI_DCD;
584 if (!((port->flags & ASYNC_CALLOUT_ACTIVE) &&
585 (port->flags & ASYNC_CALLOUT_NOHUP))) {
586 MOD_INC_USE_COUNT;
587 if (schedule_task(&port->hangup_tq) == 0)
588 MOD_DEC_USE_COUNT;
592 else {
593 if (header & ISI_DCD) {
594 /* Carrier has been detected */
595 #ifdef ISICOM_DEBUG
596 printk(KERN_DEBUG "ISICOM: interrupt: DCD->high.\n");
597 #endif
598 port->status |= ISI_DCD;
599 wake_up_interruptible(&port->open_wait);
603 else {
604 if (header & ISI_DCD)
605 port->status |= ISI_DCD;
606 else
607 port->status &= ~ISI_DCD;
610 if (port->flags & ASYNC_CTS_FLOW) {
611 if (port->tty->hw_stopped) {
612 if (header & ISI_CTS) {
613 port->tty->hw_stopped = 0;
614 /* start tx ing */
615 port->status |= (ISI_TXOK | ISI_CTS);
616 schedule_bh(port);
619 else {
620 if (!(header & ISI_CTS)) {
621 port->tty->hw_stopped = 1;
622 /* stop tx ing */
623 port->status &= ~(ISI_TXOK | ISI_CTS);
627 else {
628 if (header & ISI_CTS)
629 port->status |= ISI_CTS;
630 else
631 port->status &= ~ISI_CTS;
634 if (header & ISI_DSR)
635 port->status |= ISI_DSR;
636 else
637 port->status &= ~ISI_DSR;
639 if (header & ISI_RI)
640 port->status |= ISI_RI;
641 else
642 port->status &= ~ISI_RI;
644 break;
646 case 1: /* Received Break !!! */
647 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
648 break;
649 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
650 /* dunno if this is right */
651 *tty->flip.char_buf_ptr++ = 0;
652 tty->flip.count++;
653 if (port->flags & ASYNC_SAK)
654 do_SAK(tty);
655 queue_task(&tty->flip.tqueue, &tq_timer);
656 break;
658 case 2: /* Statistics */
659 printk(KERN_DEBUG "ISICOM: isicom_interrupt: stats!!!.\n");
660 break;
662 default:
663 printk(KERN_WARNING "ISICOM: Intr: Unknown code in status packet.\n");
664 break;
667 else { /* Data Packet */
668 count = MIN(byte_count, (TTY_FLIPBUF_SIZE - tty->flip.count));
669 #ifdef ISICOM_DEBUG
670 printk(KERN_DEBUG "ISICOM: Intr: Can rx %d of %d bytes.\n",
671 count, byte_count);
672 #endif
673 word_count = count >> 1;
674 insw(base, tty->flip.char_buf_ptr, word_count);
675 tty->flip.char_buf_ptr += (word_count << 1);
676 byte_count -= (word_count << 1);
677 if (count & 0x0001) {
678 *tty->flip.char_buf_ptr++ = (char)(inw(base) & 0xff);
679 byte_count -= 2;
681 memset(tty->flip.flag_buf_ptr, 0, count);
682 tty->flip.flag_buf_ptr += count;
683 tty->flip.count += count;
685 if (byte_count > 0) {
686 printk(KERN_DEBUG "ISICOM: Intr(0x%x:%d): Flip buffer overflow! dropping bytes...\n",
687 base, channel+1);
688 while(byte_count > 0) { /* drain out unread xtra data */
689 inw(base);
690 byte_count -= 2;
693 queue_task(&tty->flip.tqueue, &tq_timer);
695 if (card->isa == YES)
696 ClearInterrupt(base);
697 else
698 outw(0x0000, base+0x04); /* enable interrupts */
699 return;
702 /* called with interrupts disabled */
703 static void isicom_config_port(struct isi_port * port)
705 struct isi_board * card = port->card;
706 struct tty_struct * tty;
707 unsigned long baud;
708 unsigned short channel_setup, wait, base = card->base;
709 unsigned short channel = port->channel, shift_count = card->shift_count;
710 unsigned char flow_ctrl;
712 if (!(tty = port->tty) || !tty->termios)
713 return;
714 baud = C_BAUD(tty);
715 if (baud & CBAUDEX) {
716 baud &= ~CBAUDEX;
718 /* if CBAUDEX bit is on and the baud is set to either 50 or 75
719 * then the card is programmed for 57.6Kbps or 115Kbps
720 * respectively.
723 if (baud < 1 || baud > 2)
724 port->tty->termios->c_cflag &= ~CBAUDEX;
725 else
726 baud += 15;
728 if (baud == 15) {
730 /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
731 * by the set_serial_info ioctl ... this is done by
732 * the 'setserial' utility.
735 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
736 baud++; /* 57.6 Kbps */
737 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
738 baud +=2; /* 115 Kbps */
740 if (linuxb_to_isib[baud] == -1) {
741 /* hang up */
742 drop_dtr(port);
743 return;
745 else
746 raise_dtr(port);
748 wait = 100;
749 while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));
750 if (!wait) {
751 printk(KERN_WARNING "ISICOM: Card found busy in isicom_config_port at channel setup.\n");
752 return;
754 outw(0x8000 | (channel << shift_count) |0x03, base);
755 outw(linuxb_to_isib[baud] << 8 | 0x03, base);
756 channel_setup = 0;
757 switch(C_CSIZE(tty)) {
758 case CS5:
759 channel_setup |= ISICOM_CS5;
760 break;
761 case CS6:
762 channel_setup |= ISICOM_CS6;
763 break;
764 case CS7:
765 channel_setup |= ISICOM_CS7;
766 break;
767 case CS8:
768 channel_setup |= ISICOM_CS8;
769 break;
772 if (C_CSTOPB(tty))
773 channel_setup |= ISICOM_2SB;
775 if (C_PARENB(tty))
776 channel_setup |= ISICOM_EVPAR;
777 if (C_PARODD(tty))
778 channel_setup |= ISICOM_ODPAR;
779 outw(channel_setup, base);
780 InterruptTheCard(base);
782 if (C_CLOCAL(tty))
783 port->flags &= ~ASYNC_CHECK_CD;
784 else
785 port->flags |= ASYNC_CHECK_CD;
787 /* flow control settings ...*/
788 flow_ctrl = 0;
789 port->flags &= ~ASYNC_CTS_FLOW;
790 if (C_CRTSCTS(tty)) {
791 port->flags |= ASYNC_CTS_FLOW;
792 flow_ctrl |= ISICOM_CTSRTS;
794 if (I_IXON(tty))
795 flow_ctrl |= ISICOM_RESPOND_XONXOFF;
796 if (I_IXOFF(tty))
797 flow_ctrl |= ISICOM_INITIATE_XONXOFF;
799 wait = 100;
800 while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));
801 if (!wait) {
802 printk(KERN_WARNING "ISICOM: Card found busy in isicom_config_port at flow setup.\n");
803 return;
805 outw(0x8000 | (channel << shift_count) |0x04, base);
806 outw(flow_ctrl << 8 | 0x05, base);
807 outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
808 InterruptTheCard(base);
810 /* rx enabled -> enable port for rx on the card */
811 if (C_CREAD(tty)) {
812 card->port_status |= (1 << channel);
813 outw(card->port_status, base + 0x02);
818 /* open et all */
820 extern inline void isicom_setup_board(struct isi_board * bp)
822 int channel;
823 struct isi_port * port;
824 unsigned long flags;
826 if (bp->status & BOARD_ACTIVE)
827 return;
828 port = bp->ports;
829 #ifdef ISICOM_DEBUG
830 printk(KERN_DEBUG "ISICOM: setup_board: drop_dtr_rts start, port_count %d...\n", bp->port_count);
831 #endif
832 for(channel = 0; channel < bp->port_count; channel++, port++) {
833 save_flags(flags); cli();
834 drop_dtr_rts(port);
835 restore_flags(flags);
837 #ifdef ISICOM_DEBUG
838 printk(KERN_DEBUG "ISICOM: setup_board: drop_dtr_rts stop...\n");
839 #endif
841 bp->status |= BOARD_ACTIVE;
842 MOD_INC_USE_COUNT;
843 return;
846 static int isicom_setup_port(struct isi_port * port)
848 struct isi_board * card = port->card;
849 unsigned long flags;
851 if (port->flags & ASYNC_INITIALIZED)
852 return 0;
853 if (!port->xmit_buf) {
854 unsigned long page;
856 if (!(page = get_free_page(GFP_KERNEL)))
857 return -ENOMEM;
859 if (port->xmit_buf) {
860 free_page(page);
861 return -ERESTARTSYS;
863 port->xmit_buf = (unsigned char *) page;
865 save_flags(flags); cli();
866 if (port->tty)
867 clear_bit(TTY_IO_ERROR, &port->tty->flags);
868 if (port->count == 1)
869 card->count++;
871 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
873 /* discard any residual data */
874 kill_queue(port, ISICOM_KILLTX | ISICOM_KILLRX);
876 isicom_config_port(port);
877 port->flags |= ASYNC_INITIALIZED;
879 restore_flags(flags);
881 return 0;
884 static int block_til_ready(struct tty_struct * tty, struct file * filp, struct isi_port * port)
886 int do_clocal = 0, retval;
887 DECLARE_WAITQUEUE(wait, current);
889 /* block if port is in the process of being closed */
891 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
892 #ifdef ISICOM_DEBUG
893 printk(KERN_DEBUG "ISICOM: block_til_ready: close in progress.\n");
894 #endif
895 interruptible_sleep_on(&port->close_wait);
896 if (port->flags & ASYNC_HUP_NOTIFY)
897 return -EAGAIN;
898 else
899 return -ERESTARTSYS;
902 /* trying to open a callout device... check for constraints */
904 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
905 #ifdef ISICOM_DEBUG
906 printk(KERN_DEBUG "ISICOM: bl_ti_rdy: callout open.\n");
907 #endif
908 if (port->flags & ASYNC_NORMAL_ACTIVE)
909 return -EBUSY;
910 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
911 (port->flags & ASYNC_SESSION_LOCKOUT) &&
912 (port->session != current->session))
913 return -EBUSY;
915 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
916 (port->flags & ASYNC_PGRP_LOCKOUT) &&
917 (port->pgrp != current->pgrp))
918 return -EBUSY;
919 port->flags |= ASYNC_CALLOUT_ACTIVE;
920 cli();
921 raise_dtr_rts(port);
922 sti();
923 return 0;
926 /* if non-blocking mode is set ... */
928 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
929 #ifdef ISICOM_DEBUG
930 printk(KERN_DEBUG "ISICOM: block_til_ready: non-block mode.\n");
931 #endif
932 if (port->flags & ASYNC_CALLOUT_ACTIVE)
933 return -EBUSY;
934 port->flags |= ASYNC_NORMAL_ACTIVE;
935 return 0;
938 if (port->flags & ASYNC_CALLOUT_ACTIVE) {
939 if (port->normal_termios.c_cflag & CLOCAL)
940 do_clocal = 1;
941 } else {
942 if (C_CLOCAL(tty))
943 do_clocal = 1;
945 #ifdef ISICOM_DEBUG
946 if (do_clocal)
947 printk(KERN_DEBUG "ISICOM: block_til_ready: CLOCAL set.\n");
948 #endif
950 /* block waiting for DCD to be asserted, and while
951 callout dev is busy */
952 retval = 0;
953 add_wait_queue(&port->open_wait, &wait);
954 cli();
955 if (!tty_hung_up_p(filp))
956 port->count--;
957 sti();
958 port->blocked_open++;
959 #ifdef ISICOM_DEBUG
960 printk(KERN_DEBUG "ISICOM: block_til_ready: waiting for DCD...\n");
961 #endif
962 while (1) {
963 cli();
964 if (!(port->flags & ASYNC_CALLOUT_ACTIVE))
965 raise_dtr_rts(port);
967 sti();
968 set_current_state(TASK_INTERRUPTIBLE);
969 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
970 if (port->flags & ASYNC_HUP_NOTIFY)
971 retval = -EAGAIN;
972 else
973 retval = -ERESTARTSYS;
974 #ifdef ISICOM_DEBUG
975 printk(KERN_DEBUG "ISICOM: block_til_ready: tty_hung_up_p || not init.\n");
976 #endif
977 break;
979 if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
980 !(port->flags & ASYNC_CLOSING) &&
981 (do_clocal || (port->status & ISI_DCD))) {
982 #ifdef ISICOM_DEBUG
983 printk(KERN_DEBUG "ISICOM: block_til_ready: do_clocal || DCD.\n");
984 #endif
985 break;
987 if (signal_pending(current)) {
988 #ifdef ISICOM_DEBUG
989 printk(KERN_DEBUG "ISICOM: block_til_ready: sig blocked.\n");
990 #endif
991 retval = -ERESTARTSYS;
992 break;
994 schedule();
996 current->state = TASK_RUNNING;
997 remove_wait_queue(&port->open_wait, &wait);
998 if (!tty_hung_up_p(filp))
999 port->count++;
1000 port->blocked_open--;
1001 if (retval)
1002 return retval;
1003 port->flags |= ASYNC_NORMAL_ACTIVE;
1004 return 0;
1007 static int isicom_open(struct tty_struct * tty, struct file * filp)
1009 struct isi_port * port;
1010 struct isi_board * card;
1011 unsigned int line, board;
1012 unsigned long flags;
1013 int error;
1015 #ifdef ISICOM_DEBUG
1016 printk(KERN_DEBUG "ISICOM: open start!!!.\n");
1017 #endif
1018 line = MINOR(tty->device) - tty->driver.minor_start;
1020 #ifdef ISICOM_DEBUG
1021 printk(KERN_DEBUG "line = %d.\n", line);
1022 #endif
1024 if ((line < 0) || (line > (PORT_COUNT-1)))
1025 return -ENODEV;
1026 board = BOARD(line);
1028 #ifdef ISICOM_DEBUG
1029 printk(KERN_DEBUG "board = %d.\n", board);
1030 #endif
1032 card = &isi_card[board];
1033 if (!(card->status & FIRMWARE_LOADED)) {
1034 #ifdef ISICOM_DEBUG
1035 printk(KERN_DEBUG"ISICOM: Firmware not loaded to card%d.\n", board);
1036 #endif
1037 return -ENODEV;
1040 /* open on a port greater than the port count for the card !!! */
1041 if (line > ((board * 16) + card->port_count - 1)) {
1042 printk(KERN_ERR "ISICOM: Open on a port which exceeds the port_count of the card!\n");
1043 return -ENODEV;
1045 port = &isi_ports[line];
1046 if (isicom_paranoia_check(port, tty->device, "isicom_open"))
1047 return -ENODEV;
1049 #ifdef ISICOM_DEBUG
1050 printk(KERN_DEBUG "ISICOM: isicom_setup_board ...\n");
1051 #endif
1052 isicom_setup_board(card);
1054 port->count++;
1055 tty->driver_data = port;
1056 port->tty = tty;
1057 #ifdef ISICOM_DEBUG
1058 printk(KERN_DEBUG "ISICOM: isicom_setup_port ...\n");
1059 #endif
1060 if ((error = isicom_setup_port(port))!=0)
1061 return error;
1062 #ifdef ISICOM_DEBUG
1063 printk(KERN_DEBUG "ISICOM: block_til_ready ...\n");
1064 #endif
1065 if ((error = block_til_ready(tty, filp, port))!=0)
1066 return error;
1068 if ((port->count == 1) && (port->flags & ASYNC_SPLIT_TERMIOS)) {
1069 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1070 *tty->termios = port->normal_termios;
1071 else
1072 *tty->termios = port->callout_termios;
1073 save_flags(flags); cli();
1074 isicom_config_port(port);
1075 restore_flags(flags);
1078 port->session = current->session;
1079 port->pgrp = current->pgrp;
1080 #ifdef ISICOM_DEBUG
1081 printk(KERN_DEBUG "ISICOM: open end!!!.\n");
1082 #endif
1083 return 0;
1086 /* close et all */
1088 extern inline void isicom_shutdown_board(struct isi_board * bp)
1090 int channel;
1091 struct isi_port * port;
1093 if (!(bp->status & BOARD_ACTIVE))
1094 return;
1095 bp->status &= ~BOARD_ACTIVE;
1096 port = bp->ports;
1097 for(channel = 0; channel < bp->port_count; channel++, port++) {
1098 drop_dtr_rts(port);
1100 MOD_DEC_USE_COUNT;
1103 static void isicom_shutdown_port(struct isi_port * port)
1105 struct isi_board * card = port->card;
1106 struct tty_struct * tty;
1108 if (!(port->flags & ASYNC_INITIALIZED))
1109 return;
1110 if (port->xmit_buf) {
1111 free_page((unsigned long) port->xmit_buf);
1112 port->xmit_buf = NULL;
1114 if (!(tty = port->tty) || C_HUPCL(tty))
1115 /* drop dtr on this port */
1116 drop_dtr(port);
1118 /* any other port uninits */
1120 if (tty)
1121 set_bit(TTY_IO_ERROR, &tty->flags);
1122 port->flags &= ~ASYNC_INITIALIZED;
1124 if (--card->count < 0) {
1125 printk(KERN_DEBUG "ISICOM: isicom_shutdown_port: bad board(0x%x) count %d.\n",
1126 card->base, card->count);
1127 card->count = 0;
1130 /* last port was closed , shutdown that boad too */
1131 if (!card->count)
1132 isicom_shutdown_board(card);
1135 static void isicom_close(struct tty_struct * tty, struct file * filp)
1137 struct isi_port * port = (struct isi_port *) tty->driver_data;
1138 struct isi_board * card = port->card;
1139 unsigned long flags;
1141 if (!port)
1142 return;
1143 if (isicom_paranoia_check(port, tty->device, "isicom_close"))
1144 return;
1146 #ifdef ISICOM_DEBUG
1147 printk(KERN_DEBUG "ISICOM: Close start!!!.\n");
1148 #endif
1150 save_flags(flags); cli();
1151 if (tty_hung_up_p(filp)) {
1152 restore_flags(flags);
1153 return;
1156 if ((tty->count == 1) && (port->count != 1)) {
1157 printk(KERN_WARNING "ISICOM:(0x%x) isicom_close: bad port count"
1158 "tty->count = 1 port count = %d.\n",
1159 card->base, port->count);
1160 port->count = 1;
1162 if (--port->count < 0) {
1163 printk(KERN_WARNING "ISICOM:(0x%x) isicom_close: bad port count for"
1164 "channel%d = %d", card->base, port->channel,
1165 port->count);
1166 port->count = 0;
1169 if (port->count) {
1170 restore_flags(flags);
1171 return;
1173 port->flags |= ASYNC_CLOSING;
1175 * save termios struct since callout and dialin termios may be
1176 * different.
1178 if (port->flags & ASYNC_NORMAL_ACTIVE)
1179 port->normal_termios = *tty->termios;
1180 if (port->flags & ASYNC_CALLOUT_ACTIVE)
1181 port->callout_termios = *tty->termios;
1183 tty->closing = 1;
1184 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1185 tty_wait_until_sent(tty, port->closing_wait);
1186 /* indicate to the card that no more data can be received
1187 on this port */
1188 if (port->flags & ASYNC_INITIALIZED) {
1189 card->port_status &= ~(1 << port->channel);
1190 outw(card->port_status, card->base + 0x02);
1192 isicom_shutdown_port(port);
1193 if (tty->driver.flush_buffer)
1194 tty->driver.flush_buffer(tty);
1195 if (tty->ldisc.flush_buffer)
1196 tty->ldisc.flush_buffer(tty);
1197 tty->closing = 0;
1198 port->tty = 0;
1199 if (port->blocked_open) {
1200 if (port->close_delay) {
1201 current->state = TASK_INTERRUPTIBLE;
1202 #ifdef ISICOM_DEBUG
1203 printk(KERN_DEBUG "ISICOM: scheduling until time out.\n");
1204 #endif
1205 schedule_timeout(port->close_delay);
1207 wake_up_interruptible(&port->open_wait);
1209 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE |
1210 ASYNC_CLOSING);
1211 wake_up_interruptible(&port->close_wait);
1212 restore_flags(flags);
1213 #ifdef ISICOM_DEBUG
1214 printk(KERN_DEBUG "ISICOM: Close end!!!.\n");
1215 #endif
1218 /* write et all */
1219 static int isicom_write(struct tty_struct * tty, int from_user,
1220 const unsigned char * buf, int count)
1222 struct isi_port * port = (struct isi_port *) tty->driver_data;
1223 unsigned long flags;
1224 int cnt, total = 0;
1225 #ifdef ISICOM_DEBUG
1226 printk(KERN_DEBUG "ISICOM: isicom_write for port%d: %d bytes.\n",
1227 port->channel+1, count);
1228 #endif
1229 if (isicom_paranoia_check(port, tty->device, "isicom_write"))
1230 return 0;
1232 if (!tty || !port->xmit_buf || !tmp_buf)
1233 return 0;
1234 if (from_user)
1235 down(&tmp_buf_sem); /* acquire xclusive access to tmp_buf */
1237 save_flags(flags);
1238 while(1) {
1239 cli();
1240 cnt = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1241 SERIAL_XMIT_SIZE - port->xmit_head));
1242 if (cnt <= 0)
1243 break;
1245 if (from_user) {
1246 /* the following may block for paging... hence
1247 enabling interrupts but tx routine may have
1248 created more space in xmit_buf when the ctrl
1249 gets back here */
1250 sti();
1251 copy_from_user(tmp_buf, buf, cnt);
1252 cli();
1253 cnt = MIN(cnt, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1254 SERIAL_XMIT_SIZE - port->xmit_head));
1255 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, cnt);
1257 else
1258 memcpy(port->xmit_buf + port->xmit_head, buf, cnt);
1259 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE - 1);
1260 port->xmit_cnt += cnt;
1261 restore_flags(flags);
1262 buf += cnt;
1263 count -= cnt;
1264 total += cnt;
1266 if (from_user)
1267 up(&tmp_buf_sem);
1268 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1269 port->status |= ISI_TXOK;
1270 restore_flags(flags);
1271 #ifdef ISICOM_DEBUG
1272 printk(KERN_DEBUG "ISICOM: isicom_write %d bytes written.\n", total);
1273 #endif
1274 return total;
1277 /* put_char et all */
1278 static void isicom_put_char(struct tty_struct * tty, unsigned char ch)
1280 struct isi_port * port = (struct isi_port *) tty->driver_data;
1281 unsigned long flags;
1283 if (isicom_paranoia_check(port, tty->device, "isicom_put_char"))
1284 return;
1286 if (!tty || !port->xmit_buf)
1287 return;
1288 #ifdef ISICOM_DEBUG
1289 printk(KERN_DEBUG "ISICOM: put_char, port %d, char %c.\n", port->channel+1, ch);
1290 #endif
1292 save_flags(flags); cli();
1294 if (port->xmit_cnt >= (SERIAL_XMIT_SIZE - 1)) {
1295 restore_flags(flags);
1296 return;
1299 port->xmit_buf[port->xmit_head++] = ch;
1300 port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
1301 port->xmit_cnt++;
1302 restore_flags(flags);
1305 /* flush_chars et all */
1306 static void isicom_flush_chars(struct tty_struct * tty)
1308 struct isi_port * port = (struct isi_port *) tty->driver_data;
1310 if (isicom_paranoia_check(port, tty->device, "isicom_flush_chars"))
1311 return;
1313 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1314 !port->xmit_buf)
1315 return;
1317 /* this tells the transmitter to consider this port for
1318 data output to the card ... that's the best we can do. */
1319 port->status |= ISI_TXOK;
1322 /* write_room et all */
1323 static int isicom_write_room(struct tty_struct * tty)
1325 struct isi_port * port = (struct isi_port *) tty->driver_data;
1326 int free;
1327 if (isicom_paranoia_check(port, tty->device, "isicom_write_room"))
1328 return 0;
1330 free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1331 if (free < 0)
1332 free = 0;
1333 return free;
1336 /* chars_in_buffer et all */
1337 static int isicom_chars_in_buffer(struct tty_struct * tty)
1339 struct isi_port * port = (struct isi_port *) tty->driver_data;
1340 if (isicom_paranoia_check(port, tty->device, "isicom_chars_in_buffer"))
1341 return 0;
1342 return port->xmit_cnt;
1345 /* ioctl et all */
1346 extern inline void isicom_send_break(struct isi_port * port, unsigned long length)
1348 struct isi_board * card = port->card;
1349 short wait = 10;
1350 unsigned short base = card->base;
1351 unsigned long flags;
1353 save_flags(flags); cli();
1354 while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));
1355 if (!wait) {
1356 printk(KERN_DEBUG "ISICOM: Card found busy in isicom_send_break.\n");
1357 return;
1359 outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1360 outw((length & 0xff) << 8 | 0x00, base);
1361 outw((length & 0xff00), base);
1362 InterruptTheCard(base);
1363 restore_flags(flags);
1366 static int isicom_get_modem_info(struct isi_port * port, unsigned int * value)
1368 /* just send the port status */
1369 unsigned int info;
1370 unsigned short status = port->status;
1372 info = ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1373 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1374 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1375 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1376 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1377 ((status & ISI_RI ) ? TIOCM_RI : 0);
1378 put_user(info, (unsigned int *) value);
1379 return 0;
1382 static int isicom_set_modem_info(struct isi_port * port, unsigned int cmd,
1383 unsigned int * value)
1385 unsigned int arg;
1386 unsigned long flags;
1388 if(get_user(arg, value))
1389 return -EFAULT;
1391 save_flags(flags); cli();
1393 switch(cmd) {
1394 case TIOCMBIS:
1395 if (arg & TIOCM_RTS)
1396 raise_rts(port);
1397 if (arg & TIOCM_DTR)
1398 raise_dtr(port);
1399 break;
1401 case TIOCMBIC:
1402 if (arg & TIOCM_RTS)
1403 drop_rts(port);
1404 if (arg & TIOCM_DTR)
1405 drop_dtr(port);
1406 break;
1408 case TIOCMSET:
1409 if (arg & TIOCM_RTS)
1410 raise_rts(port);
1411 else
1412 drop_rts(port);
1414 if (arg & TIOCM_DTR)
1415 raise_dtr(port);
1416 else
1417 drop_dtr(port);
1418 break;
1420 default:
1421 restore_flags(flags);
1422 return -EINVAL;
1424 restore_flags(flags);
1425 return 0;
1428 static int isicom_set_serial_info(struct isi_port * port,
1429 struct serial_struct * info)
1431 struct serial_struct newinfo;
1432 unsigned long flags;
1433 int reconfig_port;
1435 if(copy_from_user(&newinfo, info, sizeof(newinfo)))
1436 return -EFAULT;
1438 reconfig_port = ((port->flags & ASYNC_SPD_MASK) !=
1439 (newinfo.flags & ASYNC_SPD_MASK));
1441 if (!suser()) {
1442 if ((newinfo.close_delay != port->close_delay) ||
1443 (newinfo.closing_wait != port->closing_wait) ||
1444 ((newinfo.flags & ~ASYNC_USR_MASK) !=
1445 (port->flags & ~ASYNC_USR_MASK)))
1446 return -EPERM;
1447 port->flags = ((port->flags & ~ ASYNC_USR_MASK) |
1448 (newinfo.flags & ASYNC_USR_MASK));
1450 else {
1451 port->close_delay = newinfo.close_delay;
1452 port->closing_wait = newinfo.closing_wait;
1453 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1454 (newinfo.flags & ASYNC_FLAGS));
1456 if (reconfig_port) {
1457 save_flags(flags); cli();
1458 isicom_config_port(port);
1459 restore_flags(flags);
1461 return 0;
1464 static int isicom_get_serial_info(struct isi_port * port,
1465 struct serial_struct * info)
1467 struct serial_struct out_info;
1469 memset(&out_info, 0, sizeof(out_info));
1470 /* out_info.type = ? */
1471 out_info.line = port - isi_ports;
1472 out_info.port = port->card->base;
1473 out_info.irq = port->card->irq;
1474 out_info.flags = port->flags;
1475 /* out_info.baud_base = ? */
1476 out_info.close_delay = port->close_delay;
1477 out_info.closing_wait = port->closing_wait;
1478 if(copy_to_user(info, &out_info, sizeof(out_info)))
1479 return -EFAULT;
1480 return 0;
1483 static int isicom_ioctl(struct tty_struct * tty, struct file * filp,
1484 unsigned int cmd, unsigned long arg)
1486 struct isi_port * port = (struct isi_port *) tty->driver_data;
1487 int retval;
1489 if (isicom_paranoia_check(port, tty->device, "isicom_ioctl"))
1490 return -ENODEV;
1492 switch(cmd) {
1493 case TCSBRK:
1494 retval = tty_check_change(tty);
1495 if (retval)
1496 return retval;
1497 tty_wait_until_sent(tty, 0);
1498 if (!arg)
1499 isicom_send_break(port, HZ/4);
1500 return 0;
1502 case TCSBRKP:
1503 retval = tty_check_change(tty);
1504 if (retval)
1505 return retval;
1506 tty_wait_until_sent(tty, 0);
1507 isicom_send_break(port, arg ? arg * (HZ/10) : HZ/4);
1508 return 0;
1510 case TIOCGSOFTCAR:
1511 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1513 case TIOCSSOFTCAR:
1514 if(get_user(arg, (unsigned long *) arg))
1515 return -EFAULT;
1516 tty->termios->c_cflag =
1517 ((tty->termios->c_cflag & ~CLOCAL) |
1518 (arg ? CLOCAL : 0));
1519 return 0;
1521 case TIOCMGET:
1522 return isicom_get_modem_info(port, (unsigned int*) arg);
1524 case TIOCMBIS:
1525 case TIOCMBIC:
1526 case TIOCMSET:
1527 return isicom_set_modem_info(port, cmd,
1528 (unsigned int *) arg);
1530 case TIOCGSERIAL:
1531 return isicom_get_serial_info(port,
1532 (struct serial_struct *) arg);
1534 case TIOCSSERIAL:
1535 return isicom_set_serial_info(port,
1536 (struct serial_struct *) arg);
1538 default:
1539 return -ENOIOCTLCMD;
1541 return 0;
1544 /* set_termios et all */
1545 static void isicom_set_termios(struct tty_struct * tty, struct termios * old_termios)
1547 struct isi_port * port = (struct isi_port *) tty->driver_data;
1548 unsigned long flags;
1550 if (isicom_paranoia_check(port, tty->device, "isicom_set_termios"))
1551 return;
1553 if (tty->termios->c_cflag == old_termios->c_cflag &&
1554 tty->termios->c_iflag == old_termios->c_iflag)
1555 return;
1557 save_flags(flags); cli();
1558 isicom_config_port(port);
1559 restore_flags(flags);
1561 if ((old_termios->c_cflag & CRTSCTS) &&
1562 !(tty->termios->c_cflag & CRTSCTS)) {
1563 tty->hw_stopped = 0;
1564 isicom_start(tty);
1568 /* throttle et all */
1569 static void isicom_throttle(struct tty_struct * tty)
1571 struct isi_port * port = (struct isi_port *) tty->driver_data;
1572 struct isi_board * card = port->card;
1573 unsigned long flags;
1575 if (isicom_paranoia_check(port, tty->device, "isicom_throttle"))
1576 return;
1578 /* tell the card that this port cannot handle any more data for now */
1579 save_flags(flags); cli();
1580 card->port_status &= ~(1 << port->channel);
1581 outw(card->port_status, card->base + 0x02);
1582 restore_flags(flags);
1585 /* unthrottle et all */
1586 static void isicom_unthrottle(struct tty_struct * tty)
1588 struct isi_port * port = (struct isi_port *) tty->driver_data;
1589 struct isi_board * card = port->card;
1590 unsigned long flags;
1592 if (isicom_paranoia_check(port, tty->device, "isicom_unthrottle"))
1593 return;
1595 /* tell the card that this port is ready to accept more data */
1596 save_flags(flags); cli();
1597 card->port_status |= (1 << port->channel);
1598 outw(card->port_status, card->base + 0x02);
1599 restore_flags(flags);
1602 /* stop et all */
1603 static void isicom_stop(struct tty_struct * tty)
1605 struct isi_port * port = (struct isi_port *) tty->driver_data;
1607 if (isicom_paranoia_check(port, tty->device, "isicom_stop"))
1608 return;
1610 /* this tells the transmitter not to consider this port for
1611 data output to the card. */
1612 port->status &= ~ISI_TXOK;
1615 /* start et all */
1616 static void isicom_start(struct tty_struct * tty)
1618 struct isi_port * port = (struct isi_port *) tty->driver_data;
1620 if (isicom_paranoia_check(port, tty->device, "isicom_start"))
1621 return;
1623 /* this tells the transmitter to consider this port for
1624 data output to the card. */
1625 port->status |= ISI_TXOK;
1628 /* hangup et all */
1629 static void do_isicom_hangup(void * data)
1631 struct isi_port * port = (struct isi_port *) data;
1632 struct tty_struct * tty;
1634 tty = port->tty;
1635 if (tty)
1636 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
1637 MOD_DEC_USE_COUNT;
1640 static void isicom_hangup(struct tty_struct * tty)
1642 struct isi_port * port = (struct isi_port *) tty->driver_data;
1644 if (isicom_paranoia_check(port, tty->device, "isicom_hangup"))
1645 return;
1647 isicom_shutdown_port(port);
1648 port->count = 0;
1649 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
1650 port->tty = 0;
1651 wake_up_interruptible(&port->open_wait);
1654 /* flush_buffer et all */
1655 static void isicom_flush_buffer(struct tty_struct * tty)
1657 struct isi_port * port = (struct isi_port *) tty->driver_data;
1658 unsigned long flags;
1660 if (isicom_paranoia_check(port, tty->device, "isicom_flush_buffer"))
1661 return;
1663 save_flags(flags); cli();
1664 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1665 restore_flags(flags);
1667 wake_up_interruptible(&tty->write_wait);
1668 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1669 tty->ldisc.write_wakeup)
1670 (tty->ldisc.write_wakeup)(tty);
1674 static int register_ioregion(void)
1676 int count, done=0;
1677 for (count=0; count < BOARD_COUNT; count++ ) {
1678 if (isi_card[count].base) {
1679 if (check_region(isi_card[count].base,16)) {
1680 printk(KERN_DEBUG "ISICOM: I/O Region 0x%x-0x%x is busy. Card%d will be disabled.\n",
1681 isi_card[count].base,isi_card[count].base+15,count+1);
1682 isi_card[count].base=0;
1684 else {
1685 request_region(isi_card[count].base,16,ISICOM_NAME);
1686 #ifdef ISICOM_DEBUG
1687 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);
1688 #endif
1689 done++;
1693 return done;
1696 static void unregister_ioregion(void)
1698 int count;
1699 for (count=0; count < BOARD_COUNT; count++ )
1700 if (isi_card[count].base) {
1701 release_region(isi_card[count].base,16);
1702 #ifdef ISICOM_DEBUG
1703 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);
1704 #endif
1708 static int register_drivers(void)
1710 int error;
1712 /* tty driver structure initialization */
1713 memset(&isicom_normal, 0, sizeof(struct tty_driver));
1714 isicom_normal.magic = TTY_DRIVER_MAGIC;
1715 isicom_normal.name = "ttyM";
1716 isicom_normal.major = ISICOM_NMAJOR;
1717 isicom_normal.minor_start = 0;
1718 isicom_normal.num = PORT_COUNT;
1719 isicom_normal.type = TTY_DRIVER_TYPE_SERIAL;
1720 isicom_normal.subtype = SERIAL_TYPE_NORMAL;
1721 isicom_normal.init_termios = tty_std_termios;
1722 isicom_normal.init_termios.c_cflag =
1723 B9600 | CS8 | CREAD | HUPCL |CLOCAL;
1724 isicom_normal.flags = TTY_DRIVER_REAL_RAW;
1725 isicom_normal.refcount = &isicom_refcount;
1727 isicom_normal.table = isicom_table;
1728 isicom_normal.termios = isicom_termios;
1729 isicom_normal.termios_locked = isicom_termios_locked;
1731 isicom_normal.open = isicom_open;
1732 isicom_normal.close = isicom_close;
1733 isicom_normal.write = isicom_write;
1734 isicom_normal.put_char = isicom_put_char;
1735 isicom_normal.flush_chars = isicom_flush_chars;
1736 isicom_normal.write_room = isicom_write_room;
1737 isicom_normal.chars_in_buffer = isicom_chars_in_buffer;
1738 isicom_normal.ioctl = isicom_ioctl;
1739 isicom_normal.set_termios = isicom_set_termios;
1740 isicom_normal.throttle = isicom_throttle;
1741 isicom_normal.unthrottle = isicom_unthrottle;
1742 isicom_normal.stop = isicom_stop;
1743 isicom_normal.start = isicom_start;
1744 isicom_normal.hangup = isicom_hangup;
1745 isicom_normal.flush_buffer = isicom_flush_buffer;
1747 /* callout device */
1749 isicom_callout = isicom_normal;
1750 isicom_callout.name = "cum";
1751 isicom_callout.major = ISICOM_CMAJOR;
1752 isicom_callout.subtype = SERIAL_TYPE_CALLOUT;
1754 if ((error=tty_register_driver(&isicom_normal))!=0) {
1755 printk(KERN_DEBUG "ISICOM: Couldn't register the dialin driver, error=%d\n",
1756 error);
1757 return error;
1759 if ((error=tty_register_driver(&isicom_callout))!=0) {
1760 tty_unregister_driver(&isicom_normal);
1761 printk(KERN_DEBUG "ISICOM: Couldn't register the callout driver, error=%d\n",
1762 error);
1763 return error;
1765 return 0;
1768 static void unregister_drivers(void)
1770 int error;
1771 if ((error=tty_unregister_driver(&isicom_callout))!=0)
1772 printk(KERN_DEBUG "ISICOM: couldn't unregister callout driver error=%d.\n",error);
1773 if (tty_unregister_driver(&isicom_normal))
1774 printk(KERN_DEBUG "ISICOM: couldn't unregister normal driver error=%d.\n",error);
1777 static int register_isr(void)
1779 int count, done=0, card;
1780 int flag;
1781 unsigned char request;
1782 for (count=0; count < BOARD_COUNT; count++ ) {
1783 if (isi_card[count].base) {
1785 * verify if the required irq has already been requested for
1786 * another ISI Card, if so we already have it, else request it
1788 request = YES;
1789 for(card = 0; card < count; card++)
1790 if ((isi_card[card].base) && (isi_card[card].irq == isi_card[count].irq)) {
1791 request = NO;
1792 if ((isi_card[count].isa == NO) && (isi_card[card].isa == NO))
1793 break;
1795 * ISA cards cannot share interrupts with other
1796 * PCI or ISA devices hence disable this card.
1798 release_region(isi_card[count].base,16);
1799 isi_card[count].base = 0;
1800 break;
1802 flag=0;
1803 if(isi_card[count].isa == NO)
1804 flag |= SA_SHIRQ;
1806 if (request == YES) {
1807 if (request_irq(isi_card[count].irq, isicom_interrupt, SA_INTERRUPT|flag, ISICOM_NAME, NULL)) {
1808 printk(KERN_WARNING "ISICOM: Could not install handler at Irq %d. Card%d will be disabled.\n",
1809 isi_card[count].irq, count+1);
1810 release_region(isi_card[count].base,16);
1811 isi_card[count].base=0;
1813 else {
1814 printk(KERN_INFO "ISICOM: Card%d at 0x%x using irq %d.\n",
1815 count+1, isi_card[count].base, isi_card[count].irq);
1817 irq_to_board[isi_card[count].irq]=&isi_card[count];
1818 done++;
1823 return done;
1826 static void unregister_isr(void)
1828 int count, card;
1829 unsigned char freeirq;
1830 for (count=0; count < BOARD_COUNT; count++ ) {
1831 if (isi_card[count].base) {
1832 freeirq = YES;
1833 for(card = 0; card < count; card++)
1834 if ((isi_card[card].base) && (isi_card[card].irq == isi_card[count].irq)) {
1835 freeirq = NO;
1836 break;
1838 if (freeirq == YES) {
1839 free_irq(isi_card[count].irq, NULL);
1840 #ifdef ISICOM_DEBUG
1841 printk(KERN_DEBUG "ISICOM: Irq %d released for Card%d.\n",isi_card[count].irq, count+1);
1842 #endif
1848 static int isicom_init(void)
1850 int card, channel, base;
1851 struct isi_port * port;
1852 unsigned long page;
1854 if (!tmp_buf) {
1855 page = get_free_page(GFP_KERNEL);
1856 if (!page) {
1857 #ifdef ISICOM_DEBUG
1858 printk(KERN_DEBUG "ISICOM: Couldn't allocate page for tmp_buf.\n");
1859 #else
1860 printk(KERN_ERR "ISICOM: Not enough memory...\n");
1861 #endif
1862 return 0;
1864 tmp_buf = (unsigned char *) page;
1867 if (!register_ioregion())
1869 printk(KERN_ERR "ISICOM: All required I/O space found busy.\n");
1870 free_page((unsigned long)tmp_buf);
1871 return 0;
1873 if (register_drivers())
1875 unregister_ioregion();
1876 free_page((unsigned long)tmp_buf);
1877 return 0;
1879 if (!register_isr())
1881 unregister_drivers();
1882 /* ioports already uregistered in register_isr */
1883 free_page((unsigned long)tmp_buf);
1884 return 0;
1887 /* initialize bottom half */
1888 init_bh(ISICOM_BH, do_isicom_bh);
1891 memset(isi_ports, 0, sizeof(isi_ports));
1892 for (card = 0; card < BOARD_COUNT; card++) {
1893 port = &isi_ports[card * 16];
1894 isi_card[card].ports = port;
1895 base = isi_card[card].base;
1896 for (channel = 0; channel < 16; channel++, port++) {
1897 port->magic = ISICOM_MAGIC;
1898 port->card = &isi_card[card];
1899 port->channel = channel;
1900 port->normal_termios = isicom_normal.init_termios;
1901 port->callout_termios = isicom_callout.init_termios;
1902 port->close_delay = 50 * HZ/100;
1903 port->closing_wait = 3000 * HZ/100;
1904 port->hangup_tq.routine = do_isicom_hangup;
1905 port->hangup_tq.data = port;
1906 port->bh_tqueue.routine = isicom_bottomhalf;
1907 port->bh_tqueue.data = port;
1908 port->status = 0;
1909 init_waitqueue_head(&port->open_wait);
1910 init_waitqueue_head(&port->close_wait);
1911 /* . . . */
1915 return 1;
1919 * Insmod can set static symbols so keep these static
1922 static int io[4];
1923 static int irq[4];
1925 MODULE_AUTHOR("MultiTech");
1926 MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
1927 MODULE_PARM(io, "1-4i");
1928 MODULE_PARM_DESC(io, "I/O ports for the cards");
1929 MODULE_PARM(irq, "1-4i");
1930 MODULE_PARM_DESC(irq, "Interrupts for the cards");
1932 int init_module(void)
1934 struct pci_dev *dev = NULL;
1935 int retval, card, idx, count;
1936 unsigned char pciirq;
1937 unsigned int ioaddr;
1939 card = 0;
1940 for(idx=0; idx < BOARD_COUNT; idx++) {
1941 if (io[idx]) {
1942 isi_card[idx].base=io[idx];
1943 isi_card[idx].irq=irq[idx];
1944 isi_card[idx].isa=YES;
1945 card++;
1947 else {
1948 isi_card[idx].base = 0;
1949 isi_card[idx].irq = 0;
1953 for (idx=0 ;idx < card; idx++) {
1954 if (!((isi_card[idx].irq==2)||(isi_card[idx].irq==3)||
1955 (isi_card[idx].irq==4)||(isi_card[idx].irq==5)||
1956 (isi_card[idx].irq==7)||(isi_card[idx].irq==10)||
1957 (isi_card[idx].irq==11)||(isi_card[idx].irq==12)||
1958 (isi_card[idx].irq==15))) {
1960 if (isi_card[idx].base) {
1961 printk(KERN_ERR "ISICOM: Irq %d unsupported. Disabling Card%d...\n",
1962 isi_card[idx].irq, idx+1);
1963 isi_card[idx].base=0;
1964 card--;
1969 if (pci_present() && (card < BOARD_COUNT)) {
1970 for (idx=0; idx < DEVID_COUNT; idx++) {
1971 dev = NULL;
1972 for (;;){
1973 if (!(dev = pci_find_device(VENDOR_ID, device_id[idx], dev)))
1974 break;
1975 if (card >= BOARD_COUNT)
1976 break;
1978 if (pci_enable_device(dev))
1979 break;
1981 /* found a PCI ISI card! */
1982 ioaddr = pci_resource_start (dev, 3); /* i.e at offset 0x1c in the
1983 * PCI configuration register
1984 * space.
1986 pciirq = dev->irq;
1987 printk(KERN_INFO "ISI PCI Card(Device ID 0x%x)\n", device_id[idx]);
1989 * allot the first empty slot in the array
1991 for (count=0; count < BOARD_COUNT; count++) {
1992 if (isi_card[count].base == 0) {
1993 isi_card[count].base = ioaddr;
1994 isi_card[count].irq = pciirq;
1995 isi_card[count].isa = NO;
1996 card++;
1997 break;
2001 if (card >= BOARD_COUNT) break;
2005 if (!(isi_card[0].base || isi_card[1].base || isi_card[2].base || isi_card[3].base)) {
2006 printk(KERN_ERR "ISICOM: No valid card configuration. Driver cannot be initialized...\n");
2007 return -EIO;
2009 retval=misc_register(&isiloader_device);
2010 if (retval<0) {
2011 printk(KERN_ERR "ISICOM: Unable to register firmware loader driver.\n");
2012 return -EIO;
2015 if (!isicom_init()) {
2016 if (misc_deregister(&isiloader_device))
2017 printk(KERN_ERR "ISICOM: Unable to unregister Firmware Loader driver\n");
2018 return -EIO;
2021 init_timer(&tx);
2022 tx.expires = jiffies + 1;
2023 tx.data = 0;
2024 tx.function = isicom_tx;
2025 re_schedule = 1;
2026 add_timer(&tx);
2028 return 0;
2031 void cleanup_module(void)
2033 re_schedule = 0;
2034 current->state = TASK_INTERRUPTIBLE;
2035 schedule_timeout(HZ);
2037 remove_bh(ISICOM_BH);
2039 #ifdef ISICOM_DEBUG
2040 printk("ISICOM: isicom_tx tx_count = %ld.\n", tx_count);
2041 #endif
2043 #ifdef ISICOM_DEBUG
2044 printk("ISICOM: uregistering isr ...\n");
2045 #endif
2046 unregister_isr();
2048 #ifdef ISICOM_DEBUG
2049 printk("ISICOM: unregistering drivers ...\n");
2050 #endif
2051 unregister_drivers();
2053 #ifdef ISICOM_DEBUG
2054 printk("ISICOM: unregistering ioregion ...\n");
2055 #endif
2056 unregister_ioregion();
2058 #ifdef ISICOM_DEBUG
2059 printk("ISICOM: freeing tmp_buf ...\n");
2060 #endif
2061 free_page((unsigned long)tmp_buf);
2063 #ifdef ISICOM_DEBUG
2064 printk("ISICOM: unregistering firmware loader ...\n");
2065 #endif
2066 if (misc_deregister(&isiloader_device))
2067 printk(KERN_ERR "ISICOM: Unable to unregister Firmware Loader driver\n");