Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / isicom.c
blob52c6b5f1950ed0672d3aa4d09a08bc1db9d10aaa
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 queue_task(&port->hangup_tq,
587 &tq_scheduler);
590 else {
591 if (header & ISI_DCD) {
592 /* Carrier has been detected */
593 #ifdef ISICOM_DEBUG
594 printk(KERN_DEBUG "ISICOM: interrupt: DCD->high.\n");
595 #endif
596 port->status |= ISI_DCD;
597 wake_up_interruptible(&port->open_wait);
601 else {
602 if (header & ISI_DCD)
603 port->status |= ISI_DCD;
604 else
605 port->status &= ~ISI_DCD;
608 if (port->flags & ASYNC_CTS_FLOW) {
609 if (port->tty->hw_stopped) {
610 if (header & ISI_CTS) {
611 port->tty->hw_stopped = 0;
612 /* start tx ing */
613 port->status |= (ISI_TXOK | ISI_CTS);
614 schedule_bh(port);
617 else {
618 if (!(header & ISI_CTS)) {
619 port->tty->hw_stopped = 1;
620 /* stop tx ing */
621 port->status &= ~(ISI_TXOK | ISI_CTS);
625 else {
626 if (header & ISI_CTS)
627 port->status |= ISI_CTS;
628 else
629 port->status &= ~ISI_CTS;
632 if (header & ISI_DSR)
633 port->status |= ISI_DSR;
634 else
635 port->status &= ~ISI_DSR;
637 if (header & ISI_RI)
638 port->status |= ISI_RI;
639 else
640 port->status &= ~ISI_RI;
642 break;
644 case 1: /* Received Break !!! */
645 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
646 break;
647 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
648 /* dunno if this is right */
649 *tty->flip.char_buf_ptr++ = 0;
650 tty->flip.count++;
651 if (port->flags & ASYNC_SAK)
652 do_SAK(tty);
653 queue_task(&tty->flip.tqueue, &tq_timer);
654 break;
656 case 2: /* Statistics */
657 printk(KERN_DEBUG "ISICOM: isicom_interrupt: stats!!!.\n");
658 break;
660 default:
661 printk(KERN_WARNING "ISICOM: Intr: Unknown code in status packet.\n");
662 break;
665 else { /* Data Packet */
666 count = MIN(byte_count, (TTY_FLIPBUF_SIZE - tty->flip.count));
667 #ifdef ISICOM_DEBUG
668 printk(KERN_DEBUG "ISICOM: Intr: Can rx %d of %d bytes.\n",
669 count, byte_count);
670 #endif
671 word_count = count >> 1;
672 insw(base, tty->flip.char_buf_ptr, word_count);
673 tty->flip.char_buf_ptr += (word_count << 1);
674 byte_count -= (word_count << 1);
675 if (count & 0x0001) {
676 *tty->flip.char_buf_ptr++ = (char)(inw(base) & 0xff);
677 byte_count -= 2;
679 memset(tty->flip.flag_buf_ptr, 0, count);
680 tty->flip.flag_buf_ptr += count;
681 tty->flip.count += count;
683 if (byte_count > 0) {
684 printk(KERN_DEBUG "ISICOM: Intr(0x%x:%d): Flip buffer overflow! dropping bytes...\n",
685 base, channel+1);
686 while(byte_count > 0) { /* drain out unread xtra data */
687 inw(base);
688 byte_count -= 2;
691 queue_task(&tty->flip.tqueue, &tq_timer);
693 if (card->isa == YES)
694 ClearInterrupt(base);
695 else
696 outw(0x0000, base+0x04); /* enable interrupts */
697 return;
700 /* called with interrupts disabled */
701 static void isicom_config_port(struct isi_port * port)
703 struct isi_board * card = port->card;
704 struct tty_struct * tty;
705 unsigned long baud;
706 unsigned short channel_setup, wait, base = card->base;
707 unsigned short channel = port->channel, shift_count = card->shift_count;
708 unsigned char flow_ctrl;
710 if (!(tty = port->tty) || !tty->termios)
711 return;
712 baud = C_BAUD(tty);
713 if (baud & CBAUDEX) {
714 baud &= ~CBAUDEX;
716 /* if CBAUDEX bit is on and the baud is set to either 50 or 75
717 * then the card is programmed for 57.6Kbps or 115Kbps
718 * respectively.
721 if (baud < 1 || baud > 2)
722 port->tty->termios->c_cflag &= ~CBAUDEX;
723 else
724 baud += 15;
726 if (baud == 15) {
728 /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
729 * by the set_serial_info ioctl ... this is done by
730 * the 'setserial' utility.
733 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
734 baud++; /* 57.6 Kbps */
735 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
736 baud +=2; /* 115 Kbps */
738 if (linuxb_to_isib[baud] == -1) {
739 /* hang up */
740 drop_dtr(port);
741 return;
743 else
744 raise_dtr(port);
746 wait = 100;
747 while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));
748 if (!wait) {
749 printk(KERN_WARNING "ISICOM: Card found busy in isicom_config_port at channel setup.\n");
750 return;
752 outw(0x8000 | (channel << shift_count) |0x03, base);
753 outw(linuxb_to_isib[baud] << 8 | 0x03, base);
754 channel_setup = 0;
755 switch(C_CSIZE(tty)) {
756 case CS5:
757 channel_setup |= ISICOM_CS5;
758 break;
759 case CS6:
760 channel_setup |= ISICOM_CS6;
761 break;
762 case CS7:
763 channel_setup |= ISICOM_CS7;
764 break;
765 case CS8:
766 channel_setup |= ISICOM_CS8;
767 break;
770 if (C_CSTOPB(tty))
771 channel_setup |= ISICOM_2SB;
773 if (C_PARENB(tty))
774 channel_setup |= ISICOM_EVPAR;
775 if (C_PARODD(tty))
776 channel_setup |= ISICOM_ODPAR;
777 outw(channel_setup, base);
778 InterruptTheCard(base);
780 if (C_CLOCAL(tty))
781 port->flags &= ~ASYNC_CHECK_CD;
782 else
783 port->flags |= ASYNC_CHECK_CD;
785 /* flow control settings ...*/
786 flow_ctrl = 0;
787 port->flags &= ~ASYNC_CTS_FLOW;
788 if (C_CRTSCTS(tty)) {
789 port->flags |= ASYNC_CTS_FLOW;
790 flow_ctrl |= ISICOM_CTSRTS;
792 if (I_IXON(tty))
793 flow_ctrl |= ISICOM_RESPOND_XONXOFF;
794 if (I_IXOFF(tty))
795 flow_ctrl |= ISICOM_INITIATE_XONXOFF;
797 wait = 100;
798 while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));
799 if (!wait) {
800 printk(KERN_WARNING "ISICOM: Card found busy in isicom_config_port at flow setup.\n");
801 return;
803 outw(0x8000 | (channel << shift_count) |0x04, base);
804 outw(flow_ctrl << 8 | 0x05, base);
805 outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
806 InterruptTheCard(base);
808 /* rx enabled -> enable port for rx on the card */
809 if (C_CREAD(tty)) {
810 card->port_status |= (1 << channel);
811 outw(card->port_status, base + 0x02);
816 /* open et all */
818 extern inline void isicom_setup_board(struct isi_board * bp)
820 int channel;
821 struct isi_port * port;
822 unsigned long flags;
824 if (bp->status & BOARD_ACTIVE)
825 return;
826 port = bp->ports;
827 #ifdef ISICOM_DEBUG
828 printk(KERN_DEBUG "ISICOM: setup_board: drop_dtr_rts start, port_count %d...\n", bp->port_count);
829 #endif
830 for(channel = 0; channel < bp->port_count; channel++, port++) {
831 save_flags(flags); cli();
832 drop_dtr_rts(port);
833 restore_flags(flags);
835 #ifdef ISICOM_DEBUG
836 printk(KERN_DEBUG "ISICOM: setup_board: drop_dtr_rts stop...\n");
837 #endif
839 bp->status |= BOARD_ACTIVE;
840 MOD_INC_USE_COUNT;
841 return;
844 static int isicom_setup_port(struct isi_port * port)
846 struct isi_board * card = port->card;
847 unsigned long flags;
849 if (port->flags & ASYNC_INITIALIZED)
850 return 0;
851 if (!port->xmit_buf) {
852 unsigned long page;
854 if (!(page = get_free_page(GFP_KERNEL)))
855 return -ENOMEM;
857 if (port->xmit_buf) {
858 free_page(page);
859 return -ERESTARTSYS;
861 port->xmit_buf = (unsigned char *) page;
863 save_flags(flags); cli();
864 if (port->tty)
865 clear_bit(TTY_IO_ERROR, &port->tty->flags);
866 if (port->count == 1)
867 card->count++;
869 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
871 /* discard any residual data */
872 kill_queue(port, ISICOM_KILLTX | ISICOM_KILLRX);
874 isicom_config_port(port);
875 port->flags |= ASYNC_INITIALIZED;
877 restore_flags(flags);
879 return 0;
882 static int block_til_ready(struct tty_struct * tty, struct file * filp, struct isi_port * port)
884 int do_clocal = 0, retval;
885 DECLARE_WAITQUEUE(wait, current);
887 /* block if port is in the process of being closed */
889 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
890 #ifdef ISICOM_DEBUG
891 printk(KERN_DEBUG "ISICOM: block_til_ready: close in progress.\n");
892 #endif
893 interruptible_sleep_on(&port->close_wait);
894 if (port->flags & ASYNC_HUP_NOTIFY)
895 return -EAGAIN;
896 else
897 return -ERESTARTSYS;
900 /* trying to open a callout device... check for constraints */
902 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
903 #ifdef ISICOM_DEBUG
904 printk(KERN_DEBUG "ISICOM: bl_ti_rdy: callout open.\n");
905 #endif
906 if (port->flags & ASYNC_NORMAL_ACTIVE)
907 return -EBUSY;
908 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
909 (port->flags & ASYNC_SESSION_LOCKOUT) &&
910 (port->session != current->session))
911 return -EBUSY;
913 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
914 (port->flags & ASYNC_PGRP_LOCKOUT) &&
915 (port->pgrp != current->pgrp))
916 return -EBUSY;
917 port->flags |= ASYNC_CALLOUT_ACTIVE;
918 cli();
919 raise_dtr_rts(port);
920 sti();
921 return 0;
924 /* if non-blocking mode is set ... */
926 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
927 #ifdef ISICOM_DEBUG
928 printk(KERN_DEBUG "ISICOM: block_til_ready: non-block mode.\n");
929 #endif
930 if (port->flags & ASYNC_CALLOUT_ACTIVE)
931 return -EBUSY;
932 port->flags |= ASYNC_NORMAL_ACTIVE;
933 return 0;
936 if (port->flags & ASYNC_CALLOUT_ACTIVE) {
937 if (port->normal_termios.c_cflag & CLOCAL)
938 do_clocal = 1;
939 } else {
940 if (C_CLOCAL(tty))
941 do_clocal = 1;
943 #ifdef ISICOM_DEBUG
944 if (do_clocal)
945 printk(KERN_DEBUG "ISICOM: block_til_ready: CLOCAL set.\n");
946 #endif
948 /* block waiting for DCD to be asserted, and while
949 callout dev is busy */
950 retval = 0;
951 add_wait_queue(&port->open_wait, &wait);
952 cli();
953 if (!tty_hung_up_p(filp))
954 port->count--;
955 sti();
956 port->blocked_open++;
957 #ifdef ISICOM_DEBUG
958 printk(KERN_DEBUG "ISICOM: block_til_ready: waiting for DCD...\n");
959 #endif
960 while (1) {
961 cli();
962 if (!(port->flags & ASYNC_CALLOUT_ACTIVE))
963 raise_dtr_rts(port);
965 sti();
966 set_current_state(TASK_INTERRUPTIBLE);
967 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
968 if (port->flags & ASYNC_HUP_NOTIFY)
969 retval = -EAGAIN;
970 else
971 retval = -ERESTARTSYS;
972 #ifdef ISICOM_DEBUG
973 printk(KERN_DEBUG "ISICOM: block_til_ready: tty_hung_up_p || not init.\n");
974 #endif
975 break;
977 if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
978 !(port->flags & ASYNC_CLOSING) &&
979 (do_clocal || (port->status & ISI_DCD))) {
980 #ifdef ISICOM_DEBUG
981 printk(KERN_DEBUG "ISICOM: block_til_ready: do_clocal || DCD.\n");
982 #endif
983 break;
985 if (signal_pending(current)) {
986 #ifdef ISICOM_DEBUG
987 printk(KERN_DEBUG "ISICOM: block_til_ready: sig blocked.\n");
988 #endif
989 retval = -ERESTARTSYS;
990 break;
992 schedule();
994 current->state = TASK_RUNNING;
995 remove_wait_queue(&port->open_wait, &wait);
996 if (!tty_hung_up_p(filp))
997 port->count++;
998 port->blocked_open--;
999 if (retval)
1000 return retval;
1001 port->flags |= ASYNC_NORMAL_ACTIVE;
1002 return 0;
1005 static int isicom_open(struct tty_struct * tty, struct file * filp)
1007 struct isi_port * port;
1008 struct isi_board * card;
1009 unsigned int line, board;
1010 unsigned long flags;
1011 int error;
1013 #ifdef ISICOM_DEBUG
1014 printk(KERN_DEBUG "ISICOM: open start!!!.\n");
1015 #endif
1016 line = MINOR(tty->device) - tty->driver.minor_start;
1018 #ifdef ISICOM_DEBUG
1019 printk(KERN_DEBUG "line = %d.\n", line);
1020 #endif
1022 if ((line < 0) || (line > (PORT_COUNT-1)))
1023 return -ENODEV;
1024 board = BOARD(line);
1026 #ifdef ISICOM_DEBUG
1027 printk(KERN_DEBUG "board = %d.\n", board);
1028 #endif
1030 card = &isi_card[board];
1031 if (!(card->status & FIRMWARE_LOADED)) {
1032 #ifdef ISICOM_DEBUG
1033 printk(KERN_DEBUG"ISICOM: Firmware not loaded to card%d.\n", board);
1034 #endif
1035 return -ENODEV;
1038 /* open on a port greater than the port count for the card !!! */
1039 if (line > ((board * 16) + card->port_count - 1)) {
1040 printk(KERN_ERR "ISICOM: Open on a port which exceeds the port_count of the card!\n");
1041 return -ENODEV;
1043 port = &isi_ports[line];
1044 if (isicom_paranoia_check(port, tty->device, "isicom_open"))
1045 return -ENODEV;
1047 #ifdef ISICOM_DEBUG
1048 printk(KERN_DEBUG "ISICOM: isicom_setup_board ...\n");
1049 #endif
1050 isicom_setup_board(card);
1052 port->count++;
1053 tty->driver_data = port;
1054 port->tty = tty;
1055 #ifdef ISICOM_DEBUG
1056 printk(KERN_DEBUG "ISICOM: isicom_setup_port ...\n");
1057 #endif
1058 if ((error = isicom_setup_port(port))!=0)
1059 return error;
1060 #ifdef ISICOM_DEBUG
1061 printk(KERN_DEBUG "ISICOM: block_til_ready ...\n");
1062 #endif
1063 if ((error = block_til_ready(tty, filp, port))!=0)
1064 return error;
1066 if ((port->count == 1) && (port->flags & ASYNC_SPLIT_TERMIOS)) {
1067 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1068 *tty->termios = port->normal_termios;
1069 else
1070 *tty->termios = port->callout_termios;
1071 save_flags(flags); cli();
1072 isicom_config_port(port);
1073 restore_flags(flags);
1076 port->session = current->session;
1077 port->pgrp = current->pgrp;
1078 #ifdef ISICOM_DEBUG
1079 printk(KERN_DEBUG "ISICOM: open end!!!.\n");
1080 #endif
1081 return 0;
1084 /* close et all */
1086 extern inline void isicom_shutdown_board(struct isi_board * bp)
1088 int channel;
1089 struct isi_port * port;
1091 if (!(bp->status & BOARD_ACTIVE))
1092 return;
1093 bp->status &= ~BOARD_ACTIVE;
1094 port = bp->ports;
1095 for(channel = 0; channel < bp->port_count; channel++, port++) {
1096 drop_dtr_rts(port);
1098 MOD_DEC_USE_COUNT;
1101 static void isicom_shutdown_port(struct isi_port * port)
1103 struct isi_board * card = port->card;
1104 struct tty_struct * tty;
1106 if (!(port->flags & ASYNC_INITIALIZED))
1107 return;
1108 if (port->xmit_buf) {
1109 free_page((unsigned long) port->xmit_buf);
1110 port->xmit_buf = NULL;
1112 if (!(tty = port->tty) || C_HUPCL(tty))
1113 /* drop dtr on this port */
1114 drop_dtr(port);
1116 /* any other port uninits */
1118 if (tty)
1119 set_bit(TTY_IO_ERROR, &tty->flags);
1120 port->flags &= ~ASYNC_INITIALIZED;
1122 if (--card->count < 0) {
1123 printk(KERN_DEBUG "ISICOM: isicom_shutdown_port: bad board(0x%x) count %d.\n",
1124 card->base, card->count);
1125 card->count = 0;
1128 /* last port was closed , shutdown that boad too */
1129 if (!card->count)
1130 isicom_shutdown_board(card);
1133 static void isicom_close(struct tty_struct * tty, struct file * filp)
1135 struct isi_port * port = (struct isi_port *) tty->driver_data;
1136 struct isi_board * card = port->card;
1137 unsigned long flags;
1139 if (!port)
1140 return;
1141 if (isicom_paranoia_check(port, tty->device, "isicom_close"))
1142 return;
1144 #ifdef ISICOM_DEBUG
1145 printk(KERN_DEBUG "ISICOM: Close start!!!.\n");
1146 #endif
1148 save_flags(flags); cli();
1149 if (tty_hung_up_p(filp)) {
1150 restore_flags(flags);
1151 return;
1154 if ((tty->count == 1) && (port->count != 1)) {
1155 printk(KERN_WARNING "ISICOM:(0x%x) isicom_close: bad port count"
1156 "tty->count = 1 port count = %d.\n",
1157 card->base, port->count);
1158 port->count = 1;
1160 if (--port->count < 0) {
1161 printk(KERN_WARNING "ISICOM:(0x%x) isicom_close: bad port count for"
1162 "channel%d = %d", card->base, port->channel,
1163 port->count);
1164 port->count = 0;
1167 if (port->count) {
1168 restore_flags(flags);
1169 return;
1171 port->flags |= ASYNC_CLOSING;
1173 * save termios struct since callout and dialin termios may be
1174 * different.
1176 if (port->flags & ASYNC_NORMAL_ACTIVE)
1177 port->normal_termios = *tty->termios;
1178 if (port->flags & ASYNC_CALLOUT_ACTIVE)
1179 port->callout_termios = *tty->termios;
1181 tty->closing = 1;
1182 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1183 tty_wait_until_sent(tty, port->closing_wait);
1184 /* indicate to the card that no more data can be received
1185 on this port */
1186 if (port->flags & ASYNC_INITIALIZED) {
1187 card->port_status &= ~(1 << port->channel);
1188 outw(card->port_status, card->base + 0x02);
1190 isicom_shutdown_port(port);
1191 if (tty->driver.flush_buffer)
1192 tty->driver.flush_buffer(tty);
1193 if (tty->ldisc.flush_buffer)
1194 tty->ldisc.flush_buffer(tty);
1195 tty->closing = 0;
1196 port->tty = 0;
1197 if (port->blocked_open) {
1198 if (port->close_delay) {
1199 current->state = TASK_INTERRUPTIBLE;
1200 #ifdef ISICOM_DEBUG
1201 printk(KERN_DEBUG "ISICOM: scheduling until time out.\n");
1202 #endif
1203 schedule_timeout(port->close_delay);
1205 wake_up_interruptible(&port->open_wait);
1207 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE |
1208 ASYNC_CLOSING);
1209 wake_up_interruptible(&port->close_wait);
1210 restore_flags(flags);
1211 #ifdef ISICOM_DEBUG
1212 printk(KERN_DEBUG "ISICOM: Close end!!!.\n");
1213 #endif
1216 /* write et all */
1217 static int isicom_write(struct tty_struct * tty, int from_user,
1218 const unsigned char * buf, int count)
1220 struct isi_port * port = (struct isi_port *) tty->driver_data;
1221 unsigned long flags;
1222 int cnt, total = 0;
1223 #ifdef ISICOM_DEBUG
1224 printk(KERN_DEBUG "ISICOM: isicom_write for port%d: %d bytes.\n",
1225 port->channel+1, count);
1226 #endif
1227 if (isicom_paranoia_check(port, tty->device, "isicom_write"))
1228 return 0;
1230 if (!tty || !port->xmit_buf || !tmp_buf)
1231 return 0;
1232 if (from_user)
1233 down(&tmp_buf_sem); /* acquire xclusive access to tmp_buf */
1235 save_flags(flags);
1236 while(1) {
1237 cli();
1238 cnt = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1239 SERIAL_XMIT_SIZE - port->xmit_head));
1240 if (cnt <= 0)
1241 break;
1243 if (from_user) {
1244 /* the following may block for paging... hence
1245 enabling interrupts but tx routine may have
1246 created more space in xmit_buf when the ctrl
1247 gets back here */
1248 sti();
1249 copy_from_user(tmp_buf, buf, cnt);
1250 cli();
1251 cnt = MIN(cnt, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1252 SERIAL_XMIT_SIZE - port->xmit_head));
1253 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, cnt);
1255 else
1256 memcpy(port->xmit_buf + port->xmit_head, buf, cnt);
1257 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE - 1);
1258 port->xmit_cnt += cnt;
1259 restore_flags(flags);
1260 buf += cnt;
1261 count -= cnt;
1262 total += cnt;
1264 if (from_user)
1265 up(&tmp_buf_sem);
1266 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1267 port->status |= ISI_TXOK;
1268 restore_flags(flags);
1269 #ifdef ISICOM_DEBUG
1270 printk(KERN_DEBUG "ISICOM: isicom_write %d bytes written.\n", total);
1271 #endif
1272 return total;
1275 /* put_char et all */
1276 static void isicom_put_char(struct tty_struct * tty, unsigned char ch)
1278 struct isi_port * port = (struct isi_port *) tty->driver_data;
1279 unsigned long flags;
1281 if (isicom_paranoia_check(port, tty->device, "isicom_put_char"))
1282 return;
1284 if (!tty || !port->xmit_buf)
1285 return;
1286 #ifdef ISICOM_DEBUG
1287 printk(KERN_DEBUG "ISICOM: put_char, port %d, char %c.\n", port->channel+1, ch);
1288 #endif
1290 save_flags(flags); cli();
1292 if (port->xmit_cnt >= (SERIAL_XMIT_SIZE - 1)) {
1293 restore_flags(flags);
1294 return;
1297 port->xmit_buf[port->xmit_head++] = ch;
1298 port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
1299 port->xmit_cnt++;
1300 restore_flags(flags);
1303 /* flush_chars et all */
1304 static void isicom_flush_chars(struct tty_struct * tty)
1306 struct isi_port * port = (struct isi_port *) tty->driver_data;
1308 if (isicom_paranoia_check(port, tty->device, "isicom_flush_chars"))
1309 return;
1311 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1312 !port->xmit_buf)
1313 return;
1315 /* this tells the transmitter to consider this port for
1316 data output to the card ... that's the best we can do. */
1317 port->status |= ISI_TXOK;
1320 /* write_room et all */
1321 static int isicom_write_room(struct tty_struct * tty)
1323 struct isi_port * port = (struct isi_port *) tty->driver_data;
1324 int free;
1325 if (isicom_paranoia_check(port, tty->device, "isicom_write_room"))
1326 return 0;
1328 free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1329 if (free < 0)
1330 free = 0;
1331 return free;
1334 /* chars_in_buffer et all */
1335 static int isicom_chars_in_buffer(struct tty_struct * tty)
1337 struct isi_port * port = (struct isi_port *) tty->driver_data;
1338 if (isicom_paranoia_check(port, tty->device, "isicom_chars_in_buffer"))
1339 return 0;
1340 return port->xmit_cnt;
1343 /* ioctl et all */
1344 extern inline void isicom_send_break(struct isi_port * port, unsigned long length)
1346 struct isi_board * card = port->card;
1347 short wait = 10;
1348 unsigned short base = card->base;
1349 unsigned long flags;
1351 save_flags(flags); cli();
1352 while (((inw(base + 0x0e) & 0x0001) == 0) && (wait-- > 0));
1353 if (!wait) {
1354 printk(KERN_DEBUG "ISICOM: Card found busy in isicom_send_break.\n");
1355 return;
1357 outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1358 outw((length & 0xff) << 8 | 0x00, base);
1359 outw((length & 0xff00), base);
1360 InterruptTheCard(base);
1361 restore_flags(flags);
1364 static int isicom_get_modem_info(struct isi_port * port, unsigned int * value)
1366 /* just send the port status */
1367 unsigned int info;
1368 unsigned short status = port->status;
1370 info = ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1371 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1372 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1373 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1374 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1375 ((status & ISI_RI ) ? TIOCM_RI : 0);
1376 put_user(info, (unsigned int *) value);
1377 return 0;
1380 static int isicom_set_modem_info(struct isi_port * port, unsigned int cmd,
1381 unsigned int * value)
1383 unsigned int arg;
1384 unsigned long flags;
1386 if(get_user(arg, value))
1387 return -EFAULT;
1389 save_flags(flags); cli();
1391 switch(cmd) {
1392 case TIOCMBIS:
1393 if (arg & TIOCM_RTS)
1394 raise_rts(port);
1395 if (arg & TIOCM_DTR)
1396 raise_dtr(port);
1397 break;
1399 case TIOCMBIC:
1400 if (arg & TIOCM_RTS)
1401 drop_rts(port);
1402 if (arg & TIOCM_DTR)
1403 drop_dtr(port);
1404 break;
1406 case TIOCMSET:
1407 if (arg & TIOCM_RTS)
1408 raise_rts(port);
1409 else
1410 drop_rts(port);
1412 if (arg & TIOCM_DTR)
1413 raise_dtr(port);
1414 else
1415 drop_dtr(port);
1416 break;
1418 default:
1419 restore_flags(flags);
1420 return -EINVAL;
1422 restore_flags(flags);
1423 return 0;
1426 static int isicom_set_serial_info(struct isi_port * port,
1427 struct serial_struct * info)
1429 struct serial_struct newinfo;
1430 unsigned long flags;
1431 int reconfig_port;
1433 if(copy_from_user(&newinfo, info, sizeof(newinfo)))
1434 return -EFAULT;
1436 reconfig_port = ((port->flags & ASYNC_SPD_MASK) !=
1437 (newinfo.flags & ASYNC_SPD_MASK));
1439 if (!suser()) {
1440 if ((newinfo.close_delay != port->close_delay) ||
1441 (newinfo.closing_wait != port->closing_wait) ||
1442 ((newinfo.flags & ~ASYNC_USR_MASK) !=
1443 (port->flags & ~ASYNC_USR_MASK)))
1444 return -EPERM;
1445 port->flags = ((port->flags & ~ ASYNC_USR_MASK) |
1446 (newinfo.flags & ASYNC_USR_MASK));
1448 else {
1449 port->close_delay = newinfo.close_delay;
1450 port->closing_wait = newinfo.closing_wait;
1451 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1452 (newinfo.flags & ASYNC_FLAGS));
1454 if (reconfig_port) {
1455 save_flags(flags); cli();
1456 isicom_config_port(port);
1457 restore_flags(flags);
1459 return 0;
1462 static int isicom_get_serial_info(struct isi_port * port,
1463 struct serial_struct * info)
1465 struct serial_struct out_info;
1467 memset(&out_info, 0, sizeof(out_info));
1468 /* out_info.type = ? */
1469 out_info.line = port - isi_ports;
1470 out_info.port = port->card->base;
1471 out_info.irq = port->card->irq;
1472 out_info.flags = port->flags;
1473 /* out_info.baud_base = ? */
1474 out_info.close_delay = port->close_delay;
1475 out_info.closing_wait = port->closing_wait;
1476 if(copy_to_user(info, &out_info, sizeof(out_info)))
1477 return -EFAULT;
1478 return 0;
1481 static int isicom_ioctl(struct tty_struct * tty, struct file * filp,
1482 unsigned int cmd, unsigned long arg)
1484 struct isi_port * port = (struct isi_port *) tty->driver_data;
1485 int retval;
1487 if (isicom_paranoia_check(port, tty->device, "isicom_ioctl"))
1488 return -ENODEV;
1490 switch(cmd) {
1491 case TCSBRK:
1492 retval = tty_check_change(tty);
1493 if (retval)
1494 return retval;
1495 tty_wait_until_sent(tty, 0);
1496 if (!arg)
1497 isicom_send_break(port, HZ/4);
1498 return 0;
1500 case TCSBRKP:
1501 retval = tty_check_change(tty);
1502 if (retval)
1503 return retval;
1504 tty_wait_until_sent(tty, 0);
1505 isicom_send_break(port, arg ? arg * (HZ/10) : HZ/4);
1506 return 0;
1508 case TIOCGSOFTCAR:
1509 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1511 case TIOCSSOFTCAR:
1512 if(get_user(arg, (unsigned long *) arg))
1513 return -EFAULT;
1514 tty->termios->c_cflag =
1515 ((tty->termios->c_cflag & ~CLOCAL) |
1516 (arg ? CLOCAL : 0));
1517 return 0;
1519 case TIOCMGET:
1520 return isicom_get_modem_info(port, (unsigned int*) arg);
1522 case TIOCMBIS:
1523 case TIOCMBIC:
1524 case TIOCMSET:
1525 return isicom_set_modem_info(port, cmd,
1526 (unsigned int *) arg);
1528 case TIOCGSERIAL:
1529 return isicom_get_serial_info(port,
1530 (struct serial_struct *) arg);
1532 case TIOCSSERIAL:
1533 return isicom_set_serial_info(port,
1534 (struct serial_struct *) arg);
1536 default:
1537 return -ENOIOCTLCMD;
1539 return 0;
1542 /* set_termios et all */
1543 static void isicom_set_termios(struct tty_struct * tty, struct termios * old_termios)
1545 struct isi_port * port = (struct isi_port *) tty->driver_data;
1546 unsigned long flags;
1548 if (isicom_paranoia_check(port, tty->device, "isicom_set_termios"))
1549 return;
1551 if (tty->termios->c_cflag == old_termios->c_cflag &&
1552 tty->termios->c_iflag == old_termios->c_iflag)
1553 return;
1555 save_flags(flags); cli();
1556 isicom_config_port(port);
1557 restore_flags(flags);
1559 if ((old_termios->c_cflag & CRTSCTS) &&
1560 !(tty->termios->c_cflag & CRTSCTS)) {
1561 tty->hw_stopped = 0;
1562 isicom_start(tty);
1566 /* throttle et all */
1567 static void isicom_throttle(struct tty_struct * tty)
1569 struct isi_port * port = (struct isi_port *) tty->driver_data;
1570 struct isi_board * card = port->card;
1571 unsigned long flags;
1573 if (isicom_paranoia_check(port, tty->device, "isicom_throttle"))
1574 return;
1576 /* tell the card that this port cannot handle any more data for now */
1577 save_flags(flags); cli();
1578 card->port_status &= ~(1 << port->channel);
1579 outw(card->port_status, card->base + 0x02);
1580 restore_flags(flags);
1583 /* unthrottle et all */
1584 static void isicom_unthrottle(struct tty_struct * tty)
1586 struct isi_port * port = (struct isi_port *) tty->driver_data;
1587 struct isi_board * card = port->card;
1588 unsigned long flags;
1590 if (isicom_paranoia_check(port, tty->device, "isicom_unthrottle"))
1591 return;
1593 /* tell the card that this port is ready to accept more data */
1594 save_flags(flags); cli();
1595 card->port_status |= (1 << port->channel);
1596 outw(card->port_status, card->base + 0x02);
1597 restore_flags(flags);
1600 /* stop et all */
1601 static void isicom_stop(struct tty_struct * tty)
1603 struct isi_port * port = (struct isi_port *) tty->driver_data;
1605 if (isicom_paranoia_check(port, tty->device, "isicom_stop"))
1606 return;
1608 /* this tells the transmitter not to consider this port for
1609 data output to the card. */
1610 port->status &= ~ISI_TXOK;
1613 /* start et all */
1614 static void isicom_start(struct tty_struct * tty)
1616 struct isi_port * port = (struct isi_port *) tty->driver_data;
1618 if (isicom_paranoia_check(port, tty->device, "isicom_start"))
1619 return;
1621 /* this tells the transmitter to consider this port for
1622 data output to the card. */
1623 port->status |= ISI_TXOK;
1626 /* hangup et all */
1627 static void do_isicom_hangup(void * data)
1629 struct isi_port * port = (struct isi_port *) data;
1630 struct tty_struct * tty;
1632 tty = port->tty;
1633 if (!tty)
1634 return;
1636 tty_hangup(tty);
1639 static void isicom_hangup(struct tty_struct * tty)
1641 struct isi_port * port = (struct isi_port *) tty->driver_data;
1643 if (isicom_paranoia_check(port, tty->device, "isicom_hangup"))
1644 return;
1646 isicom_shutdown_port(port);
1647 port->count = 0;
1648 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
1649 port->tty = 0;
1650 wake_up_interruptible(&port->open_wait);
1653 /* flush_buffer et all */
1654 static void isicom_flush_buffer(struct tty_struct * tty)
1656 struct isi_port * port = (struct isi_port *) tty->driver_data;
1657 unsigned long flags;
1659 if (isicom_paranoia_check(port, tty->device, "isicom_flush_buffer"))
1660 return;
1662 save_flags(flags); cli();
1663 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1664 restore_flags(flags);
1666 wake_up_interruptible(&tty->write_wait);
1667 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1668 tty->ldisc.write_wakeup)
1669 (tty->ldisc.write_wakeup)(tty);
1673 static int register_ioregion(void)
1675 int count, done=0;
1676 for (count=0; count < BOARD_COUNT; count++ ) {
1677 if (isi_card[count].base) {
1678 if (check_region(isi_card[count].base,16)) {
1679 printk(KERN_DEBUG "ISICOM: I/O Region 0x%x-0x%x is busy. Card%d will be disabled.\n",
1680 isi_card[count].base,isi_card[count].base+15,count+1);
1681 isi_card[count].base=0;
1683 else {
1684 request_region(isi_card[count].base,16,ISICOM_NAME);
1685 #ifdef ISICOM_DEBUG
1686 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);
1687 #endif
1688 done++;
1692 return done;
1695 static void unregister_ioregion(void)
1697 int count;
1698 for (count=0; count < BOARD_COUNT; count++ )
1699 if (isi_card[count].base) {
1700 release_region(isi_card[count].base,16);
1701 #ifdef ISICOM_DEBUG
1702 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);
1703 #endif
1707 static int register_drivers(void)
1709 int error;
1711 /* tty driver structure initialization */
1712 memset(&isicom_normal, 0, sizeof(struct tty_driver));
1713 isicom_normal.magic = TTY_DRIVER_MAGIC;
1714 isicom_normal.name = "ttyM";
1715 isicom_normal.major = ISICOM_NMAJOR;
1716 isicom_normal.minor_start = 0;
1717 isicom_normal.num = PORT_COUNT;
1718 isicom_normal.type = TTY_DRIVER_TYPE_SERIAL;
1719 isicom_normal.subtype = SERIAL_TYPE_NORMAL;
1720 isicom_normal.init_termios = tty_std_termios;
1721 isicom_normal.init_termios.c_cflag =
1722 B9600 | CS8 | CREAD | HUPCL |CLOCAL;
1723 isicom_normal.flags = TTY_DRIVER_REAL_RAW;
1724 isicom_normal.refcount = &isicom_refcount;
1726 isicom_normal.table = isicom_table;
1727 isicom_normal.termios = isicom_termios;
1728 isicom_normal.termios_locked = isicom_termios_locked;
1730 isicom_normal.open = isicom_open;
1731 isicom_normal.close = isicom_close;
1732 isicom_normal.write = isicom_write;
1733 isicom_normal.put_char = isicom_put_char;
1734 isicom_normal.flush_chars = isicom_flush_chars;
1735 isicom_normal.write_room = isicom_write_room;
1736 isicom_normal.chars_in_buffer = isicom_chars_in_buffer;
1737 isicom_normal.ioctl = isicom_ioctl;
1738 isicom_normal.set_termios = isicom_set_termios;
1739 isicom_normal.throttle = isicom_throttle;
1740 isicom_normal.unthrottle = isicom_unthrottle;
1741 isicom_normal.stop = isicom_stop;
1742 isicom_normal.start = isicom_start;
1743 isicom_normal.hangup = isicom_hangup;
1744 isicom_normal.flush_buffer = isicom_flush_buffer;
1746 /* callout device */
1748 isicom_callout = isicom_normal;
1749 isicom_callout.name = "cum";
1750 isicom_callout.major = ISICOM_CMAJOR;
1751 isicom_callout.subtype = SERIAL_TYPE_CALLOUT;
1753 if ((error=tty_register_driver(&isicom_normal))!=0) {
1754 printk(KERN_DEBUG "ISICOM: Couldn't register the dialin driver, error=%d\n",
1755 error);
1756 return error;
1758 if ((error=tty_register_driver(&isicom_callout))!=0) {
1759 tty_unregister_driver(&isicom_normal);
1760 printk(KERN_DEBUG "ISICOM: Couldn't register the callout driver, error=%d\n",
1761 error);
1762 return error;
1764 return 0;
1767 static void unregister_drivers(void)
1769 int error;
1770 if ((error=tty_unregister_driver(&isicom_callout))!=0)
1771 printk(KERN_DEBUG "ISICOM: couldn't unregister callout driver error=%d.\n",error);
1772 if (tty_unregister_driver(&isicom_normal))
1773 printk(KERN_DEBUG "ISICOM: couldn't unregister normal driver error=%d.\n",error);
1776 static int register_isr(void)
1778 int count, done=0, card;
1779 int flag;
1780 unsigned char request;
1781 for (count=0; count < BOARD_COUNT; count++ ) {
1782 if (isi_card[count].base) {
1784 * verify if the required irq has already been requested for
1785 * another ISI Card, if so we already have it, else request it
1787 request = YES;
1788 for(card = 0; card < count; card++)
1789 if ((isi_card[card].base) && (isi_card[card].irq == isi_card[count].irq)) {
1790 request = NO;
1791 if ((isi_card[count].isa == NO) && (isi_card[card].isa == NO))
1792 break;
1794 * ISA cards cannot share interrupts with other
1795 * PCI or ISA devices hence disable this card.
1797 release_region(isi_card[count].base,16);
1798 isi_card[count].base = 0;
1799 break;
1801 flag=0;
1802 if(isi_card[count].isa == NO)
1803 flag |= SA_SHIRQ;
1805 if (request == YES) {
1806 if (request_irq(isi_card[count].irq, isicom_interrupt, SA_INTERRUPT|flag, ISICOM_NAME, NULL)) {
1807 printk(KERN_WARNING "ISICOM: Could not install handler at Irq %d. Card%d will be disabled.\n",
1808 isi_card[count].irq, count+1);
1809 release_region(isi_card[count].base,16);
1810 isi_card[count].base=0;
1812 else {
1813 printk(KERN_INFO "ISICOM: Card%d at 0x%x using irq %d.\n",
1814 count+1, isi_card[count].base, isi_card[count].irq);
1816 irq_to_board[isi_card[count].irq]=&isi_card[count];
1817 done++;
1822 return done;
1825 static void unregister_isr(void)
1827 int count, card;
1828 unsigned char freeirq;
1829 for (count=0; count < BOARD_COUNT; count++ ) {
1830 if (isi_card[count].base) {
1831 freeirq = YES;
1832 for(card = 0; card < count; card++)
1833 if ((isi_card[card].base) && (isi_card[card].irq == isi_card[count].irq)) {
1834 freeirq = NO;
1835 break;
1837 if (freeirq == YES) {
1838 free_irq(isi_card[count].irq, NULL);
1839 #ifdef ISICOM_DEBUG
1840 printk(KERN_DEBUG "ISICOM: Irq %d released for Card%d.\n",isi_card[count].irq, count+1);
1841 #endif
1847 static int isicom_init(void)
1849 int card, channel, base;
1850 struct isi_port * port;
1851 unsigned long page;
1853 if (!tmp_buf) {
1854 page = get_free_page(GFP_KERNEL);
1855 if (!page) {
1856 #ifdef ISICOM_DEBUG
1857 printk(KERN_DEBUG "ISICOM: Couldn't allocate page for tmp_buf.\n");
1858 #else
1859 printk(KERN_ERR "ISICOM: Not enough memory...\n");
1860 #endif
1861 return 0;
1863 tmp_buf = (unsigned char *) page;
1866 if (!register_ioregion())
1868 printk(KERN_ERR "ISICOM: All required I/O space found busy.\n");
1869 free_page((unsigned long)tmp_buf);
1870 return 0;
1872 if (register_drivers())
1874 unregister_ioregion();
1875 free_page((unsigned long)tmp_buf);
1876 return 0;
1878 if (!register_isr())
1880 unregister_drivers();
1881 /* ioports already uregistered in register_isr */
1882 free_page((unsigned long)tmp_buf);
1883 return 0;
1886 /* initialize bottom half */
1887 init_bh(ISICOM_BH, do_isicom_bh);
1890 memset(isi_ports, 0, sizeof(isi_ports));
1891 for (card = 0; card < BOARD_COUNT; card++) {
1892 port = &isi_ports[card * 16];
1893 isi_card[card].ports = port;
1894 base = isi_card[card].base;
1895 for (channel = 0; channel < 16; channel++, port++) {
1896 port->magic = ISICOM_MAGIC;
1897 port->card = &isi_card[card];
1898 port->channel = channel;
1899 port->normal_termios = isicom_normal.init_termios;
1900 port->callout_termios = isicom_callout.init_termios;
1901 port->close_delay = 50 * HZ/100;
1902 port->closing_wait = 3000 * HZ/100;
1903 port->hangup_tq.routine = do_isicom_hangup;
1904 port->hangup_tq.data = port;
1905 port->bh_tqueue.routine = isicom_bottomhalf;
1906 port->bh_tqueue.data = port;
1907 port->status = 0;
1908 init_waitqueue_head(&port->open_wait);
1909 init_waitqueue_head(&port->close_wait);
1910 /* . . . */
1914 return 1;
1918 * Insmod can set static symbols so keep these static
1921 static int io[4];
1922 static int irq[4];
1924 MODULE_AUTHOR("MultiTech");
1925 MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
1926 MODULE_PARM(io, "1-4i");
1927 MODULE_PARM_DESC(io, "I/O ports for the cards");
1928 MODULE_PARM(irq, "1-4i");
1929 MODULE_PARM_DESC(irq, "Interrupts for the cards");
1931 int init_module(void)
1933 struct pci_dev *dev = NULL;
1934 int retval, card, idx, count;
1935 unsigned char pciirq;
1936 unsigned int ioaddr;
1938 card = 0;
1939 for(idx=0; idx < BOARD_COUNT; idx++) {
1940 if (io[idx]) {
1941 isi_card[idx].base=io[idx];
1942 isi_card[idx].irq=irq[idx];
1943 isi_card[idx].isa=YES;
1944 card++;
1946 else {
1947 isi_card[idx].base = 0;
1948 isi_card[idx].irq = 0;
1952 for (idx=0 ;idx < card; idx++) {
1953 if (!((isi_card[idx].irq==2)||(isi_card[idx].irq==3)||
1954 (isi_card[idx].irq==4)||(isi_card[idx].irq==5)||
1955 (isi_card[idx].irq==7)||(isi_card[idx].irq==10)||
1956 (isi_card[idx].irq==11)||(isi_card[idx].irq==12)||
1957 (isi_card[idx].irq==15))) {
1959 if (isi_card[idx].base) {
1960 printk(KERN_ERR "ISICOM: Irq %d unsupported. Disabling Card%d...\n",
1961 isi_card[idx].irq, idx+1);
1962 isi_card[idx].base=0;
1963 card--;
1968 if (pci_present() && (card < BOARD_COUNT)) {
1969 for (idx=0; idx < DEVID_COUNT; idx++) {
1970 dev = NULL;
1971 for (;;){
1972 if (!(dev = pci_find_device(VENDOR_ID, device_id[idx], dev)))
1973 break;
1974 if (card >= BOARD_COUNT)
1975 break;
1977 if (pci_enable_device(dev))
1978 break;
1980 /* found a PCI ISI card! */
1981 ioaddr = pci_resource_start (dev, 3); /* i.e at offset 0x1c in the
1982 * PCI configuration register
1983 * space.
1985 pciirq = dev->irq;
1986 printk(KERN_INFO "ISI PCI Card(Device ID 0x%x)\n", device_id[idx]);
1988 * allot the first empty slot in the array
1990 for (count=0; count < BOARD_COUNT; count++) {
1991 if (isi_card[count].base == 0) {
1992 isi_card[count].base = ioaddr;
1993 isi_card[count].irq = pciirq;
1994 isi_card[count].isa = NO;
1995 card++;
1996 break;
2000 if (card >= BOARD_COUNT) break;
2004 if (!(isi_card[0].base || isi_card[1].base || isi_card[2].base || isi_card[3].base)) {
2005 printk(KERN_ERR "ISICOM: No valid card configuration. Driver cannot be initialized...\n");
2006 return -EIO;
2008 retval=misc_register(&isiloader_device);
2009 if (retval<0) {
2010 printk(KERN_ERR "ISICOM: Unable to register firmware loader driver.\n");
2011 return -EIO;
2014 if (!isicom_init()) {
2015 if (misc_deregister(&isiloader_device))
2016 printk(KERN_ERR "ISICOM: Unable to unregister Firmware Loader driver\n");
2017 return -EIO;
2020 init_timer(&tx);
2021 tx.expires = jiffies + 1;
2022 tx.data = 0;
2023 tx.function = isicom_tx;
2024 re_schedule = 1;
2025 add_timer(&tx);
2027 return 0;
2030 void cleanup_module(void)
2032 re_schedule = 0;
2033 current->state = TASK_INTERRUPTIBLE;
2034 schedule_timeout(HZ);
2036 remove_bh(ISICOM_BH);
2038 #ifdef ISICOM_DEBUG
2039 printk("ISICOM: isicom_tx tx_count = %ld.\n", tx_count);
2040 #endif
2042 #ifdef ISICOM_DEBUG
2043 printk("ISICOM: uregistering isr ...\n");
2044 #endif
2045 unregister_isr();
2047 #ifdef ISICOM_DEBUG
2048 printk("ISICOM: unregistering drivers ...\n");
2049 #endif
2050 unregister_drivers();
2052 #ifdef ISICOM_DEBUG
2053 printk("ISICOM: unregistering ioregion ...\n");
2054 #endif
2055 unregister_ioregion();
2057 #ifdef ISICOM_DEBUG
2058 printk("ISICOM: freeing tmp_buf ...\n");
2059 #endif
2060 free_page((unsigned long)tmp_buf);
2062 #ifdef ISICOM_DEBUG
2063 printk("ISICOM: unregistering firmware loader ...\n");
2064 #endif
2065 if (misc_deregister(&isiloader_device))
2066 printk(KERN_ERR "ISICOM: Unable to unregister Firmware Loader driver\n");