Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / drivers / char / applicom.c
blobaf61070b9707c97d158dbdedf3ea87d63c8f02fa
1 /* Derived from Applicom driver ac.c for SCO Unix */
2 /* Ported by David Woodhouse, Axiom (Cambridge) Ltd. */
3 /* dwmw2@redhat.com 30/8/98 */
4 /* $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $ */
5 /* This module is for Linux 2.1 and 2.2 series kernels. */
6 /*****************************************************************************/
7 /* J PAGET 18/02/94 passage V2.4.2 ioctl avec code 2 reset to les interrupt */
8 /* ceci pour reseter correctement apres une sortie sauvage */
9 /* J PAGET 02/05/94 passage V2.4.3 dans le traitement de d'interruption, */
10 /* LoopCount n'etait pas initialise a 0. */
11 /* F LAFORSE 04/07/95 version V2.6.0 lecture bidon apres acces a une carte */
12 /* pour liberer le bus */
13 /* J.PAGET 19/11/95 version V2.6.1 Nombre, addresse,irq n'est plus configure */
14 /* et passe en argument a acinit, mais est scrute sur le bus pour s'adapter */
15 /* au nombre de cartes presentes sur le bus. IOCL code 6 affichait V2.4.3 */
16 /* F.LAFORSE 28/11/95 creation de fichiers acXX.o avec les differentes */
17 /* adresses de base des cartes, IOCTL 6 plus complet */
18 /* J.PAGET le 19/08/96 copie de la version V2.6 en V2.8.0 sans modification */
19 /* de code autre que le texte V2.6.1 en V2.8.0 */
20 /*****************************************************************************/
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/miscdevice.h>
29 #include <linux/pci.h>
30 #include <linux/wait.h>
31 #include <linux/init.h>
32 #include <linux/compatmac.h>
34 #include <asm/io.h>
35 #include <asm/uaccess.h>
37 #include "applicom.h"
39 #if LINUX_VERSION_CODE < 0x20300
40 /* These probably want adding to <linux/compatmac.h> */
41 #define init_waitqueue_head(x) do { *(x) = NULL; } while (0)
42 #define PCI_BASE_ADDRESS(dev) (dev->base_address[0])
43 #define DECLARE_WAIT_QUEUE_HEAD(x) struct wait_queue *x
44 #define __setup(x,y) /* */
45 #else
46 #define PCI_BASE_ADDRESS(dev) (dev->resource[0].start)
47 #endif
49 /* NOTE: We use for loops with {write,read}b() instead of
50 memcpy_{from,to}io throughout this driver. This is because
51 the board doesn't correctly handle word accesses - only
52 bytes.
56 #undef DEBUG
58 #define MAX_BOARD 8 /* maximum of pc board possible */
59 #define MAX_ISA_BOARD 4
60 #define LEN_RAM_IO 0x800
61 #define AC_MINOR 157
63 #ifndef PCI_VENDOR_ID_APPLICOM
64 #define PCI_VENDOR_ID_APPLICOM 0x1389
65 #define PCI_DEVICE_ID_APPLICOM_PCIGENERIC 0x0001
66 #define PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN 0x0002
67 #define PCI_DEVICE_ID_APPLICOM_PCI2000PFB 0x0003
68 #endif
69 #define MAX_PCI_DEVICE_NUM 3
71 static char *applicom_pci_devnames[] = {
72 "PCI board",
73 "PCI2000IBS / PCI2000CAN",
74 "PCI2000PFB"
77 static struct pci_device_id applicom_pci_tbl[] = {
78 { PCI_VENDOR_ID_APPLICOM, PCI_DEVICE_ID_APPLICOM_PCIGENERIC,
79 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
80 { PCI_VENDOR_ID_APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN,
81 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
82 { PCI_VENDOR_ID_APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000PFB,
83 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
84 { 0 }
86 MODULE_DEVICE_TABLE(pci, applicom_pci_tbl);
88 MODULE_AUTHOR("David Woodhouse & Applicom International");
89 MODULE_DESCRIPTION("Driver for Applicom Profibus card");
90 MODULE_LICENSE("GPL");
91 MODULE_PARM(irq, "i");
92 MODULE_PARM_DESC(irq, "IRQ of the Applicom board");
93 MODULE_PARM(mem, "i");
94 MODULE_PARM_DESC(mem, "Shared Memory Address of Applicom board");
96 MODULE_SUPPORTED_DEVICE("ac");
99 static struct applicom_board {
100 unsigned long PhysIO;
101 unsigned long RamIO;
102 wait_queue_head_t FlagSleepSend;
103 long irq;
104 spinlock_t mutex;
105 } apbs[MAX_BOARD];
107 static unsigned int irq = 0; /* interrupt number IRQ */
108 static unsigned long mem = 0; /* physical segment of board */
110 static unsigned int numboards; /* number of installed boards */
111 static volatile unsigned char Dummy;
112 static DECLARE_WAIT_QUEUE_HEAD(FlagSleepRec);
113 static unsigned int WriteErrorCount; /* number of write error */
114 static unsigned int ReadErrorCount; /* number of read error */
115 static unsigned int DeviceErrorCount; /* number of device error */
117 static ssize_t ac_read (struct file *, char *, size_t, loff_t *);
118 static ssize_t ac_write (struct file *, const char *, size_t, loff_t *);
119 static int ac_ioctl(struct inode *, struct file *, unsigned int,
120 unsigned long);
121 static void ac_interrupt(int, void *, struct pt_regs *);
123 static struct file_operations ac_fops = {
124 .owner = THIS_MODULE,
125 .llseek = no_llseek,
126 .read = ac_read,
127 .write = ac_write,
128 .ioctl = ac_ioctl,
131 static struct miscdevice ac_miscdev = {
132 AC_MINOR,
133 "ac",
134 &ac_fops
137 static int dummy; /* dev_id for request_irq() */
139 static int ac_register_board(unsigned long physloc, unsigned long loc,
140 unsigned char boardno)
142 volatile unsigned char byte_reset_it;
144 if((readb(loc + CONF_END_TEST) != 0x00) ||
145 (readb(loc + CONF_END_TEST + 1) != 0x55) ||
146 (readb(loc + CONF_END_TEST + 2) != 0xAA) ||
147 (readb(loc + CONF_END_TEST + 3) != 0xFF))
148 return 0;
150 if (!boardno)
151 boardno = readb(loc + NUMCARD_OWNER_TO_PC);
153 if (!boardno && boardno > MAX_BOARD) {
154 printk(KERN_WARNING "Board #%d (at 0x%lx) is out of range (1 <= x <= %d).\n",
155 boardno, physloc, MAX_BOARD);
156 return 0;
159 if (apbs[boardno - 1].RamIO) {
160 printk(KERN_WARNING "Board #%d (at 0x%lx) conflicts with previous board #%d (at 0x%lx)\n",
161 boardno, physloc, boardno, apbs[boardno-1].PhysIO);
162 return 0;
165 boardno--;
167 apbs[boardno].PhysIO = physloc;
168 apbs[boardno].RamIO = loc;
169 init_waitqueue_head(&apbs[boardno].FlagSleepSend);
170 spin_lock_init(&apbs[boardno].mutex);
171 byte_reset_it = readb(loc + RAM_IT_TO_PC);
173 numboards++;
174 return boardno + 1;
177 #ifdef MODULE
179 #define applicom_init init_module
181 void cleanup_module(void)
183 int i;
185 misc_deregister(&ac_miscdev);
187 for (i = 0; i < MAX_BOARD; i++) {
189 if (!apbs[i].RamIO)
190 continue;
192 iounmap((void *) apbs[i].RamIO);
194 if (apbs[i].irq)
195 free_irq(apbs[i].irq, &dummy);
199 #endif /* MODULE */
201 int __init applicom_init(void)
203 int i, numisa = 0;
204 struct pci_dev *dev = NULL;
205 void *RamIO;
206 int boardno;
208 printk(KERN_INFO "Applicom driver: $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $\n");
210 /* No mem and irq given - check for a PCI card */
212 while ( (dev = pci_find_class(PCI_CLASS_OTHERS << 16, dev))) {
214 if (dev->vendor != PCI_VENDOR_ID_APPLICOM)
215 continue;
217 if (dev->device > MAX_PCI_DEVICE_NUM || dev->device == 0)
218 continue;
220 if (pci_enable_device(dev))
221 return -EIO;
223 RamIO = ioremap(PCI_BASE_ADDRESS(dev), LEN_RAM_IO);
225 if (!RamIO) {
226 printk(KERN_INFO "ac.o: Failed to ioremap PCI memory space at 0x%lx\n", PCI_BASE_ADDRESS(dev));
227 return -EIO;
230 printk(KERN_INFO "Applicom %s found at mem 0x%lx, irq %d\n",
231 applicom_pci_devnames[dev->device-1], PCI_BASE_ADDRESS(dev),
232 dev->irq);
234 if (!(boardno = ac_register_board(PCI_BASE_ADDRESS(dev),
235 (unsigned long)RamIO,0))) {
236 printk(KERN_INFO "ac.o: PCI Applicom device doesn't have correct signature.\n");
237 iounmap(RamIO);
238 continue;
241 if (request_irq(dev->irq, &ac_interrupt, SA_SHIRQ, "Applicom PCI", &dummy)) {
242 printk(KERN_INFO "Could not allocate IRQ %d for PCI Applicom device.\n", dev->irq);
243 iounmap(RamIO);
244 apbs[boardno - 1].RamIO = 0;
245 continue;
248 /* Enable interrupts. */
250 writeb(0x40, apbs[boardno - 1].RamIO + RAM_IT_FROM_PC);
252 apbs[boardno - 1].irq = dev->irq;
255 /* Finished with PCI cards. If none registered,
256 * and there was no mem/irq specified, exit */
258 if (!mem || !irq) {
259 if (numboards)
260 goto fin;
261 else {
262 printk(KERN_INFO "ac.o: No PCI boards found.\n");
263 printk(KERN_INFO "ac.o: For an ISA board you must supply memory and irq parameters.\n");
264 return -ENXIO;
268 /* Now try the specified ISA cards */
270 #warning "LEAK"
271 RamIO = ioremap(mem, LEN_RAM_IO * MAX_ISA_BOARD);
273 if (!RamIO)
274 printk(KERN_INFO "ac.o: Failed to ioremap ISA memory space at 0x%lx\n", mem);
276 for (i = 0; i < MAX_ISA_BOARD; i++) {
277 RamIO = ioremap(mem + (LEN_RAM_IO * i), LEN_RAM_IO);
279 if (!RamIO) {
280 printk(KERN_INFO "ac.o: Failed to ioremap the ISA card's memory space (slot #%d)\n", i + 1);
281 continue;
284 if (!(boardno = ac_register_board((unsigned long)mem+ (LEN_RAM_IO*i),
285 (unsigned long)RamIO,i+1))) {
286 iounmap(RamIO);
287 continue;
290 printk(KERN_NOTICE "Applicom ISA card found at mem 0x%lx, irq %d\n", mem + (LEN_RAM_IO*i), irq);
292 if (!numisa) {
293 if (request_irq(irq, &ac_interrupt, SA_SHIRQ, "Applicom ISA", &dummy)) {
294 printk(KERN_WARNING "Could not allocate IRQ %d for ISA Applicom device.\n", irq);
295 iounmap((void *) RamIO);
296 apbs[boardno - 1].RamIO = 0;
298 apbs[boardno - 1].irq = irq;
300 else
301 apbs[boardno - 1].irq = 0;
303 numisa++;
306 if (!numisa)
307 printk(KERN_WARNING"ac.o: No valid ISA Applicom boards found at mem 0x%lx\n",mem);
309 fin:
310 init_waitqueue_head(&FlagSleepRec);
312 WriteErrorCount = 0;
313 ReadErrorCount = 0;
314 DeviceErrorCount = 0;
316 if (numboards) {
317 misc_register(&ac_miscdev);
318 for (i = 0; i < MAX_BOARD; i++) {
319 int serial;
320 char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
322 if (!apbs[i].RamIO)
323 continue;
325 for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
326 boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
328 boardname[serial] = 0;
331 printk(KERN_INFO "Applicom board %d: %s, PROM V%d.%d",
332 i+1, boardname,
333 (int)(readb(apbs[i].RamIO + VERS) >> 4),
334 (int)(readb(apbs[i].RamIO + VERS) & 0xF));
336 serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
337 (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
338 (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
340 if (serial != 0)
341 printk(" S/N %d\n", serial);
342 else
343 printk("\n");
345 return 0;
348 else
349 return -ENXIO;
353 #ifndef MODULE
354 __initcall(applicom_init);
355 #endif
357 static ssize_t ac_write(struct file *file, const char *buf, size_t count, loff_t * ppos)
359 unsigned int NumCard; /* Board number 1 -> 8 */
360 unsigned int IndexCard; /* Index board number 0 -> 7 */
361 unsigned char TicCard; /* Board TIC to send */
362 unsigned long flags; /* Current priority */
363 struct st_ram_io st_loc;
364 struct mailbox tmpmailbox;
365 #ifdef DEBUG
366 int c;
367 #endif
368 DECLARE_WAITQUEUE(wait, current);
370 if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
371 static int warncount = 5;
372 if (warncount) {
373 printk(KERN_INFO "Hmmm. write() of Applicom card, length %d != expected %d\n",
374 count, sizeof(struct st_ram_io) + sizeof(struct mailbox));
375 warncount--;
377 return -EINVAL;
380 if(copy_from_user(&st_loc, buf, sizeof(struct st_ram_io)))
381 return -EFAULT;
383 if(copy_from_user(&tmpmailbox, &buf[sizeof(struct st_ram_io)],
384 sizeof(struct mailbox)))
385 return -EFAULT;
387 NumCard = st_loc.num_card; /* board number to send */
388 TicCard = st_loc.tic_des_from_pc; /* tic number to send */
389 IndexCard = NumCard - 1;
391 if((NumCard < 1) || (NumCard > MAX_BOARD) || !apbs[IndexCard].RamIO)
392 return -EINVAL;
394 #ifdef DEBUG
395 printk("Write to applicom card #%d. struct st_ram_io follows:",
396 IndexCard+1);
398 for (c = 0; c < sizeof(struct st_ram_io);) {
400 printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
402 for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
403 printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
407 printk("\nstruct mailbox follows:");
409 for (c = 0; c < sizeof(struct mailbox);) {
410 printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
412 for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
413 printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
417 printk("\n");
418 #endif
420 spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
422 /* Test octet ready correct */
423 if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
424 Dummy = readb(apbs[IndexCard].RamIO + VERS);
425 spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
426 printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
427 IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
428 DeviceErrorCount++;
429 return -EIO;
432 /* Place ourselves on the wait queue */
433 set_current_state(TASK_INTERRUPTIBLE);
434 add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
436 /* Check whether the card is ready for us */
437 while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
438 Dummy = readb(apbs[IndexCard].RamIO + VERS);
439 /* It's busy. Sleep. */
441 spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
442 schedule();
443 if (signal_pending(current)) {
444 remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
445 &wait);
446 return -EINTR;
448 spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
449 set_current_state(TASK_INTERRUPTIBLE);
452 /* We may not have actually slept */
453 set_current_state(TASK_RUNNING);
454 remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
456 writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
458 /* Which is best - lock down the pages with rawio and then
459 copy directly, or use bounce buffers? For now we do the latter
460 because it works with 2.2 still */
462 unsigned char *from = (unsigned char *) &tmpmailbox;
463 unsigned long to = (unsigned long) apbs[IndexCard].RamIO + RAM_FROM_PC;
464 int c;
466 for (c = 0; c < sizeof(struct mailbox); c++)
467 writeb(*(from++), to++);
470 writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
471 writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
472 writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
473 writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
474 writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
475 writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
476 Dummy = readb(apbs[IndexCard].RamIO + VERS);
477 spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
478 return 0;
481 static int do_ac_read(int IndexCard, char *buf)
483 struct st_ram_io st_loc;
484 struct mailbox tmpmailbox; /* bounce buffer - can't copy to user space with cli() */
485 unsigned long from = (unsigned long)apbs[IndexCard].RamIO + RAM_TO_PC;
486 unsigned char *to = (unsigned char *)&tmpmailbox;
487 #ifdef DEBUG
488 int c;
489 #endif
491 st_loc.tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
492 st_loc.numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
496 int c;
498 for (c = 0; c < sizeof(struct mailbox); c++)
499 *(to++) = readb(from++);
501 writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
502 writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
503 writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
504 writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
505 apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
506 writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
507 writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
508 writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
509 Dummy = readb(apbs[IndexCard].RamIO + VERS);
511 #ifdef DEBUG
512 printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
514 for (c = 0; c < sizeof(struct st_ram_io);) {
515 printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
517 for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
518 printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
522 printk("\nstruct mailbox follows:");
524 for (c = 0; c < sizeof(struct mailbox);) {
525 printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
527 for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
528 printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
531 printk("\n");
532 #endif
534 #warning "Je suis stupide. DW. - copy*user in cli"
536 if (copy_to_user(buf, &st_loc, sizeof(struct st_ram_io)))
537 return -EFAULT;
538 if (copy_to_user(&buf[sizeof(struct st_ram_io)], &tmpmailbox, sizeof(struct mailbox)))
539 return -EFAULT;
541 return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
544 static ssize_t ac_read (struct file *filp, char *buf, size_t count, loff_t *ptr)
546 unsigned long flags;
547 unsigned int i;
548 unsigned char tmp;
549 int ret = 0;
550 DECLARE_WAITQUEUE(wait, current);
551 #ifdef DEBUG
552 int loopcount=0;
553 #endif
554 /* No need to ratelimit this. Only root can trigger it anyway */
555 if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
556 printk( KERN_WARNING "Hmmm. read() of Applicom card, length %d != expected %d\n",
557 count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
558 return -EINVAL;
561 while(1) {
562 /* Stick ourself on the wait queue */
563 set_current_state(TASK_INTERRUPTIBLE);
564 add_wait_queue(&FlagSleepRec, &wait);
566 /* Scan each board, looking for one which has a packet for us */
567 for (i=0; i < MAX_BOARD; i++) {
568 if (!apbs[i].RamIO)
569 continue;
570 spin_lock_irqsave(&apbs[i].mutex, flags);
572 tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
574 if (tmp == 2) {
575 /* Got a packet for us */
576 ret = do_ac_read(i, buf);
577 spin_unlock_irqrestore(&apbs[i].mutex, flags);
578 set_current_state(TASK_RUNNING);
579 remove_wait_queue(&FlagSleepRec, &wait);
580 return tmp;
583 if (tmp > 2) {
584 /* Got an error */
585 Dummy = readb(apbs[i].RamIO + VERS);
587 spin_unlock_irqrestore(&apbs[i].mutex, flags);
588 set_current_state(TASK_RUNNING);
589 remove_wait_queue(&FlagSleepRec, &wait);
591 printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
592 i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
593 DeviceErrorCount++;
594 return -EIO;
597 /* Nothing for us. Try the next board */
598 Dummy = readb(apbs[i].RamIO + VERS);
599 spin_unlock_irqrestore(&apbs[i].mutex, flags);
601 } /* per board */
603 /* OK - No boards had data for us. Sleep now */
605 schedule();
606 remove_wait_queue(&FlagSleepRec, &wait);
608 if (signal_pending(current))
609 return -EINTR;
611 #ifdef DEBUG
612 if (loopcount++ > 2) {
613 printk("Looping in ac_read. loopcount %d\n", loopcount);
615 #endif
619 static void ac_interrupt(int vec, void *dev_instance, struct pt_regs *regs)
621 unsigned int i;
622 unsigned int FlagInt;
623 unsigned int LoopCount;
625 // printk("Applicom interrupt on IRQ %d occurred\n", vec);
627 LoopCount = 0;
629 do {
630 FlagInt = 0;
631 for (i = 0; i < MAX_BOARD; i++) {
633 /* Skip if this board doesn't exist */
634 if (!apbs[i].RamIO)
635 continue;
637 spin_lock(&apbs[i].mutex);
639 /* Skip if this board doesn't want attention */
640 if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
641 spin_unlock(&apbs[i].mutex);
642 continue;
645 FlagInt = 1;
646 writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
648 if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
649 printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
650 i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
651 DeviceErrorCount++;
654 if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
655 (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
657 printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
658 i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
659 DeviceErrorCount++;
662 if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) { /* mailbox sent by the card ? */
663 if (waitqueue_active(&FlagSleepRec)) {
664 wake_up_interruptible(&FlagSleepRec);
668 if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) { /* ram i/o free for write by pc ? */
669 if (waitqueue_active(&apbs[i].FlagSleepSend)) { /* process sleep during read ? */
670 wake_up_interruptible(&apbs[i].FlagSleepSend);
673 Dummy = readb(apbs[i].RamIO + VERS);
675 if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
676 /* There's another int waiting on this card */
677 spin_unlock(&apbs[i].mutex);
678 i--;
679 } else {
680 spin_unlock(&apbs[i].mutex);
683 if (FlagInt)
684 LoopCount = 0;
685 else
686 LoopCount++;
687 } while(LoopCount < 2);
692 static int ac_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
694 { /* @ ADG ou ATO selon le cas */
695 int i;
696 unsigned char IndexCard;
697 unsigned long pmem;
698 int ret = 0;
699 volatile unsigned char byte_reset_it;
700 struct st_ram_io *adgl;
702 /* In general, the device is only openable by root anyway, so we're not
703 particularly concerned that bogus ioctls can flood the console. */
705 adgl = kmalloc(sizeof(struct st_ram_io), GFP_KERNEL);
706 if (!adgl)
707 return -ENOMEM;
709 if (copy_from_user(adgl, (void *)arg,sizeof(struct st_ram_io))) {
710 kfree(adgl);
711 return -EFAULT;
714 IndexCard = adgl->num_card-1;
716 if(cmd != 0 && cmd != 6 &&
717 ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) {
718 static int warncount = 10;
719 if (warncount) {
720 printk( KERN_WARNING "APPLICOM driver IOCTL, bad board number %d\n",(int)IndexCard+1);
721 warncount--;
723 kfree(adgl);
724 return -EINVAL;
727 switch (cmd) {
729 case 0:
730 pmem = apbs[IndexCard].RamIO;
731 for (i = 0; i < sizeof(struct st_ram_io); i++)
732 ((unsigned char *)adgl)[i]=readb(pmem++);
733 if (copy_to_user((void *)arg, adgl, sizeof(struct st_ram_io)))
734 ret = -EFAULT;
735 break;
736 case 1:
737 pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
738 for (i = 0; i < 4; i++)
739 adgl->conf_end_test[i] = readb(pmem++);
740 for (i = 0; i < 2; i++)
741 adgl->error_code[i] = readb(pmem++);
742 for (i = 0; i < 4; i++)
743 adgl->parameter_error[i] = readb(pmem++);
744 pmem = apbs[IndexCard].RamIO + VERS;
745 adgl->vers = readb(pmem);
746 pmem = apbs[IndexCard].RamIO + TYPE_CARD;
747 for (i = 0; i < 20; i++)
748 adgl->reserv1[i] = readb(pmem++);
749 *(int *)&adgl->reserv1[20] =
750 (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER) << 16) +
751 (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 1) << 8) +
752 (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 2) );
754 if (copy_to_user((void *)arg, adgl, sizeof(struct st_ram_io)))
755 ret = -EFAULT;
756 break;
757 case 2:
758 pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
759 for (i = 0; i < 10; i++)
760 writeb(0xff, pmem++);
761 writeb(adgl->data_from_pc_ready,
762 apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
764 writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
766 for (i = 0; i < MAX_BOARD; i++) {
767 if (apbs[i].RamIO) {
768 byte_reset_it = readb(apbs[i].RamIO + RAM_IT_TO_PC);
771 break;
772 case 3:
773 pmem = apbs[IndexCard].RamIO + TIC_DES_FROM_PC;
774 writeb(adgl->tic_des_from_pc, pmem);
775 break;
776 case 4:
777 pmem = apbs[IndexCard].RamIO + TIC_OWNER_TO_PC;
778 adgl->tic_owner_to_pc = readb(pmem++);
779 adgl->numcard_owner_to_pc = readb(pmem);
780 if (copy_to_user((void *)arg, adgl,sizeof(struct st_ram_io)))
781 ret = -EFAULT;
782 break;
783 case 5:
784 writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
785 writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
786 writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
787 writeb(4, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
788 writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
789 break;
790 case 6:
791 printk(KERN_INFO "APPLICOM driver release .... V2.8.0 ($Revision: 1.30 $)\n");
792 printk(KERN_INFO "Number of installed boards . %d\n", (int) numboards);
793 printk(KERN_INFO "Segment of board ........... %X\n", (int) mem);
794 printk(KERN_INFO "Interrupt IRQ number ....... %d\n", (int) irq);
795 for (i = 0; i < MAX_BOARD; i++) {
796 int serial;
797 char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
799 if (!apbs[i].RamIO)
800 continue;
802 for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
803 boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
804 boardname[serial] = 0;
806 printk(KERN_INFO "Prom version board %d ....... V%d.%d %s",
807 i+1,
808 (int)(readb(apbs[IndexCard].RamIO + VERS) >> 4),
809 (int)(readb(apbs[IndexCard].RamIO + VERS) & 0xF),
810 boardname);
813 serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
814 (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
815 (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
817 if (serial != 0)
818 printk(" S/N %d\n", serial);
819 else
820 printk("\n");
822 if (DeviceErrorCount != 0)
823 printk(KERN_INFO "DeviceErrorCount ........... %d\n", DeviceErrorCount);
824 if (ReadErrorCount != 0)
825 printk(KERN_INFO "ReadErrorCount ............. %d\n", ReadErrorCount);
826 if (WriteErrorCount != 0)
827 printk(KERN_INFO "WriteErrorCount ............ %d\n", WriteErrorCount);
828 if (waitqueue_active(&FlagSleepRec))
829 printk(KERN_INFO "Process in read pending\n");
830 for (i = 0; i < MAX_BOARD; i++) {
831 if (apbs[i].RamIO && waitqueue_active(&apbs[i].FlagSleepSend))
832 printk(KERN_INFO "Process in write pending board %d\n",i+1);
834 break;
835 default:
836 printk(KERN_INFO "APPLICOM driver ioctl, unknown function code %d\n",cmd) ;
837 ret = -EINVAL;
838 break;
840 Dummy = readb(apbs[IndexCard].RamIO + VERS);
841 kfree(adgl);
842 return 0;
845 #ifndef MODULE
846 static int __init applicom_setup(char *str)
848 int ints[4];
850 (void) get_options(str, 4, ints);
852 if (ints[0] > 2) {
853 printk(KERN_WARNING "Too many arguments to 'applicom=', expected mem,irq only.\n");
856 if (ints[0] < 2) {
857 printk(KERN_INFO"applicom numargs: %d\n", ints[0]);
858 return 0;
861 mem = ints[1];
862 irq = ints[2];
863 return 1;
866 __setup("applicom=", applicom_setup);
868 #endif /* MODULE */