- Andries Brouwer: final isofs pieces.
[davej-history.git] / drivers / char / applicom.c
blob63f26cc0fdfb1c682ce5b4d8bf55bfe6f27cb0fe
1 /* Derived from Applicom driver ac.c for SCO Unix */
2 /* Ported by David Woodhouse, Axiom (Cambridge) Ltd. */
3 /* Dave@mvhi.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/malloc.h>
26 #include <asm/errno.h>
27 #include <asm/io.h>
28 #include <asm/uaccess.h>
29 #include <linux/miscdevice.h>
30 #include <linux/pci.h>
31 #include <linux/wait.h>
32 #include <linux/init.h>
33 #include <linux/compatmac.h>
35 #include "applicom.h"
37 #if LINUX_VERSION_CODE < 0x20300
38 /* These probably want adding to <linux/compatmac.h> */
39 #define init_waitqueue_head(x) do { *(x) = NULL; } while (0);
40 #define PCI_BASE_ADDRESS(dev) (dev->base_address[0])
41 #define DECLARE_WAIT_QUEUE_HEAD(x) struct wait_queue *x
42 #define __setup(x,y) /* */
43 #else
44 #define PCI_BASE_ADDRESS(dev) (dev->resource[0].start)
45 #endif
47 /* NOTE: We use for loops with {write,read}b() instead of
48 memcpy_{from,to}io throughout this driver. This is because
49 the board doesn't correctly handle word accesses - only
50 bytes.
54 #undef DEBUG
56 #define MAX_BOARD 8 /* maximum of pc board possible */
57 #define MAX_ISA_BOARD 4
58 #define LEN_RAM_IO 0x800
59 #define AC_MINOR 157
61 #ifndef PCI_VENDOR_ID_APPLICOM
62 #define PCI_VENDOR_ID_APPLICOM 0x1389
63 #define PCI_DEVICE_ID_APPLICOM_PCIGENERIC 0x0001
64 #define PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN 0x0002
65 #define PCI_DEVICE_ID_APPLICOM_PCI2000PFB 0x0003
66 #endif
67 #define MAX_PCI_DEVICE_NUM 3
69 static char *applicom_pci_devnames[] = {
70 "PCI board",
71 "PCI2000IBS / PCI2000CAN",
72 "PCI2000PFB"
75 MODULE_AUTHOR("David Woodhouse & Applicom International");
76 MODULE_DESCRIPTION("Driver for Applicom Profibus card");
77 MODULE_PARM(irq, "i");
78 MODULE_PARM_DESC(irq, "IRQ of the Applicom board");
79 MODULE_PARM(mem, "i");
80 MODULE_PARM_DESC(mem, "Shared Memory Address of Applicom board");
82 MODULE_SUPPORTED_DEVICE("ac");
85 struct applicom_board {
86 unsigned long PhysIO;
87 unsigned long RamIO;
88 wait_queue_head_t FlagSleepSend;
89 long irq;
90 spinlock_t mutex;
91 } apbs[MAX_BOARD];
93 static unsigned int irq = 0; /* interrupt number IRQ */
94 static unsigned long mem = 0; /* physical segment of board */
96 static unsigned int numboards; /* number of installed boards */
97 static volatile unsigned char Dummy;
98 static DECLARE_WAIT_QUEUE_HEAD(FlagSleepRec);
99 static unsigned int WriteErrorCount; /* number of write error */
100 static unsigned int ReadErrorCount; /* number of read error */
101 static unsigned int DeviceErrorCount; /* number of device error */
103 static loff_t ac_llseek(struct file *, loff_t, int);
104 static ssize_t ac_read (struct file *, char *, size_t, loff_t *);
105 static ssize_t ac_write (struct file *, const char *, size_t, loff_t *);
106 static int ac_ioctl(struct inode *, struct file *, unsigned int,
107 unsigned long);
108 static void ac_interrupt(int, void *, struct pt_regs *);
110 struct file_operations ac_fops = {
111 owner:THIS_MODULE,
112 llseek:ac_llseek,
113 read:ac_read,
114 write:ac_write,
115 ioctl:ac_ioctl,
118 struct miscdevice ac_miscdev = {
119 AC_MINOR,
120 "ac",
121 &ac_fops
124 static int dummy; /* dev_id for request_irq() */
126 int ac_register_board(unsigned long physloc, unsigned long loc,
127 unsigned char boardno)
129 volatile unsigned char byte_reset_it;
131 if((readb(loc + CONF_END_TEST) != 0x00) ||
132 (readb(loc + CONF_END_TEST + 1) != 0x55) ||
133 (readb(loc + CONF_END_TEST + 2) != 0xAA) ||
134 (readb(loc + CONF_END_TEST + 3) != 0xFF))
135 return 0;
137 if (!boardno)
138 boardno = readb(loc + NUMCARD_OWNER_TO_PC);
140 if (!boardno && boardno > MAX_BOARD) {
141 printk(KERN_WARNING "Board #%d (at 0x%lx) is out of range (1 <= x <= %d).\n",
142 boardno, physloc, MAX_BOARD);
143 return 0;
146 if (apbs[boardno - 1].RamIO) {
147 printk(KERN_WARNING "Board #%d (at 0x%lx) conflicts with previous board #%d (at 0x%lx)\n",
148 boardno, physloc, boardno, apbs[boardno-1].PhysIO);
149 return 0;
152 boardno--;
154 apbs[boardno].PhysIO = physloc;
155 apbs[boardno].RamIO = loc;
156 init_waitqueue_head(&apbs[boardno].FlagSleepSend);
157 spin_lock_init(&apbs[boardno].mutex);
158 byte_reset_it = readb(loc + RAM_IT_TO_PC);
160 numboards++;
161 return boardno + 1;
164 #ifdef MODULE
166 #define applicom_init init_module
168 void cleanup_module(void)
170 int i;
172 misc_deregister(&ac_miscdev);
174 for (i = 0; i < MAX_BOARD; i++) {
176 if (!apbs[i].RamIO)
177 continue;
179 iounmap((void *) apbs[i].RamIO);
181 if (apbs[i].irq)
182 free_irq(apbs[i].irq, &dummy);
186 #endif /* MODULE */
188 int __init applicom_init(void)
190 int i, numisa = 0;
191 struct pci_dev *dev = NULL;
192 void *RamIO;
193 int boardno;
195 printk(KERN_INFO "Applicom driver: $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $\n");
197 /* No mem and irq given - check for a PCI card */
199 while ( (dev = pci_find_class(PCI_CLASS_OTHERS << 16, dev))) {
201 if (dev->vendor != PCI_VENDOR_ID_APPLICOM)
202 continue;
204 if (dev->device > MAX_PCI_DEVICE_NUM || dev->device == 0)
205 continue;
207 if (pci_enable_device(dev))
208 return -EIO;
210 RamIO = ioremap(PCI_BASE_ADDRESS(dev), LEN_RAM_IO);
212 if (!RamIO) {
213 printk(KERN_INFO "ac.o: Failed to ioremap PCI memory space at 0x%lx\n", PCI_BASE_ADDRESS(dev));
214 return -EIO;
217 printk(KERN_INFO "Applicom %s found at mem 0x%lx, irq %d\n",
218 applicom_pci_devnames[dev->device-1], PCI_BASE_ADDRESS(dev),
219 dev->irq);
221 if (!(boardno = ac_register_board(PCI_BASE_ADDRESS(dev),
222 (unsigned long)RamIO,0))) {
223 printk(KERN_INFO "ac.o: PCI Applicom device doesn't have correct signature.\n");
224 iounmap(RamIO);
225 continue;
228 if (request_irq(dev->irq, &ac_interrupt, SA_SHIRQ, "Applicom PCI", &dummy)) {
229 printk(KERN_INFO "Could not allocate IRQ %d for PCI Applicom device.\n", dev->irq);
230 iounmap(RamIO);
231 apbs[boardno - 1].RamIO = 0;
232 continue;
235 /* Enable interrupts. */
237 writeb(0x40, apbs[boardno - 1].RamIO + RAM_IT_FROM_PC);
239 apbs[boardno - 1].irq = dev->irq;
242 /* Finished with PCI cards. If none registered,
243 * and there was no mem/irq specified, exit */
245 if (!mem || !irq) {
246 if (numboards)
247 goto fin;
248 else {
249 printk(KERN_INFO "ac.o: No PCI boards found.\n");
250 printk(KERN_INFO "ac.o: For an ISA board you must supply memory and irq parameters.\n");
251 return -ENXIO;
255 /* Now try the specified ISA cards */
257 RamIO = ioremap(mem, LEN_RAM_IO * MAX_ISA_BOARD);
259 if (!RamIO)
260 printk(KERN_INFO "ac.o: Failed to ioremap ISA memory space at 0x%lx\n", mem);
262 for (i = 0; i < MAX_ISA_BOARD; i++) {
263 RamIO = ioremap(mem + (LEN_RAM_IO * i), LEN_RAM_IO);
265 if (!RamIO) {
266 printk(KERN_INFO "ac.o: Failed to ioremap the ISA card's memory space (slot #%d)\n", i + 1);
267 continue;
270 if (!(boardno = ac_register_board((unsigned long)mem+ (LEN_RAM_IO*i),
271 (unsigned long)RamIO,i+1))) {
272 iounmap(RamIO);
273 continue;
276 printk(KERN_NOTICE "Applicom ISA card found at mem 0x%lx, irq %d\n", mem + (LEN_RAM_IO*i), irq);
278 if (!numisa) {
279 if (request_irq(irq, &ac_interrupt, SA_SHIRQ, "Applicom ISA", &dummy)) {
280 printk(KERN_WARNING "Could not allocate IRQ %d for ISA Applicom device.\n", irq);
281 iounmap((void *) RamIO);
282 apbs[boardno - 1].RamIO = 0;
284 apbs[boardno - 1].irq = irq;
286 else
287 apbs[boardno - 1].irq = 0;
289 numisa++;
292 if (!numisa)
293 printk(KERN_WARNING"ac.o: No valid ISA Applicom boards found at mem 0x%lx\n",mem);
295 fin:
296 init_waitqueue_head(&FlagSleepRec);
298 WriteErrorCount = 0;
299 ReadErrorCount = 0;
300 DeviceErrorCount = 0;
302 if (numboards) {
303 misc_register(&ac_miscdev);
304 for (i = 0; i < MAX_BOARD; i++) {
305 int serial;
306 char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
308 if (!apbs[i].RamIO)
309 continue;
311 for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
312 boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
314 boardname[serial] = 0;
317 printk(KERN_INFO "Applicom board %d: %s, PROM V%d.%d",
318 i+1, boardname,
319 (int)(readb(apbs[i].RamIO + VERS) >> 4),
320 (int)(readb(apbs[i].RamIO + VERS) & 0xF));
322 serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
323 (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
324 (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
326 if (serial != 0)
327 printk(" S/N %d\n", serial);
328 else
329 printk("\n");
331 return 0;
334 else
335 return -ENXIO;
339 #ifndef MODULE
340 __initcall(applicom_init);
341 #endif
343 static loff_t ac_llseek(struct file *file, loff_t offset, int origin)
345 return -ESPIPE;
348 static ssize_t ac_write(struct file *file, const char *buf, size_t count, loff_t * ppos)
350 unsigned int NumCard; /* Board number 1 -> 8 */
351 unsigned int IndexCard; /* Index board number 0 -> 7 */
352 unsigned char TicCard; /* Board TIC to send */
353 unsigned long flags; /* Current priority */
354 struct st_ram_io st_loc;
355 struct mailbox tmpmailbox;
356 #ifdef DEBUG
357 int c;
358 #endif
359 DECLARE_WAITQUEUE(wait, current);
361 if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
362 static int warncount = 5;
363 if (warncount) {
364 printk(KERN_INFO "Hmmm. write() of Applicom card, length %d != expected %d\n",
365 count, sizeof(struct st_ram_io) + sizeof(struct mailbox));
366 warncount--;
368 return -EINVAL;
371 if(copy_from_user(&st_loc, buf, sizeof(struct st_ram_io)))
372 return -EFAULT;
374 if(copy_from_user(&tmpmailbox, &buf[sizeof(struct st_ram_io)],
375 sizeof(struct mailbox)))
376 return -EFAULT;
378 NumCard = st_loc.num_card; /* board number to send */
379 TicCard = st_loc.tic_des_from_pc; /* tic number to send */
380 IndexCard = NumCard - 1;
382 if((NumCard < 1) || (NumCard > MAX_BOARD) || !apbs[IndexCard].RamIO)
383 return -EINVAL;
385 #ifdef DEBUG
386 printk("Write to applicom card #%d. struct st_ram_io follows:",
387 IndexCard+1);
389 for (c = 0; c < sizeof(struct st_ram_io);) {
391 printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
393 for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
394 printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
398 printk("\nstruct mailbox follows:");
400 for (c = 0; c < sizeof(struct mailbox);) {
401 printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
403 for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
404 printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
408 printk("\n");
409 #endif
411 spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
413 /* Test octet ready correct */
414 if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
415 Dummy = readb(apbs[IndexCard].RamIO + VERS);
416 spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
417 printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
418 IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
419 DeviceErrorCount++;
420 return -EIO;
423 /* Place ourselves on the wait queue */
424 current->state = TASK_INTERRUPTIBLE;
425 add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
427 /* Check whether the card is ready for us */
428 while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
429 Dummy = readb(apbs[IndexCard].RamIO + VERS);
430 /* It's busy. Sleep. */
432 spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
433 schedule();
434 if (signal_pending(current)) {
435 remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
436 &wait);
437 return -EINTR;
439 spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
442 /* We may not have actually slept */
443 current->state = TASK_RUNNING;
444 remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
446 writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
448 /* Which is best - lock down the pages with rawio and then
449 copy directly, or use bounce buffers? For now we do the latter
450 because it works with 2.2 still */
452 unsigned char *from = (unsigned char *) &tmpmailbox;
453 unsigned long to = (unsigned long) apbs[IndexCard].RamIO + RAM_FROM_PC;
454 int c;
456 for (c = 0; c < sizeof(struct mailbox); c++)
457 writeb(*(from++), to++);
460 writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
461 writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
462 writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
463 writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
464 writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
465 writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
466 Dummy = readb(apbs[IndexCard].RamIO + VERS);
467 spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
468 return 0;
471 static int do_ac_read(int IndexCard, char *buf)
473 struct st_ram_io st_loc;
474 struct mailbox tmpmailbox; /* bounce buffer - can't copy to user space with cli() */
475 unsigned long from = (unsigned long)apbs[IndexCard].RamIO + RAM_TO_PC;
476 unsigned char *to = (unsigned char *)&tmpmailbox;
477 #ifdef DEBUG
478 int c;
479 #endif
481 st_loc.tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
482 st_loc.numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
486 int c;
488 for (c = 0; c < sizeof(struct mailbox); c++)
489 *(to++) = readb(from++);
491 writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
492 writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
493 writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
494 writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
495 apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
496 writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
497 writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
498 writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
499 Dummy = readb(apbs[IndexCard].RamIO + VERS);
501 #ifdef DEBUG
502 printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
504 for (c = 0; c < sizeof(struct st_ram_io);) {
505 printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
507 for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
508 printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
512 printk("\nstruct mailbox follows:");
514 for (c = 0; c < sizeof(struct mailbox);) {
515 printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
517 for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
518 printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
521 printk("\n");
522 #endif
524 /* Je suis stupide. DW. */
526 if (copy_to_user(buf, &st_loc, sizeof(struct st_ram_io)))
527 return -EFAULT;
528 if (copy_to_user(&buf[sizeof(struct st_ram_io)], &tmpmailbox, sizeof(struct mailbox)))
529 return -EFAULT;
531 return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
534 static ssize_t ac_read (struct file *filp, char *buf, size_t count, loff_t *ptr)
536 unsigned long flags;
537 unsigned int i;
538 unsigned char tmp;
539 int ret = 0;
540 DECLARE_WAITQUEUE(wait, current);
541 #ifdef DEBUG
542 int loopcount=0;
543 #endif
544 /* No need to ratelimit this. Only root can trigger it anyway */
545 if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
546 printk( KERN_WARNING "Hmmm. read() of Applicom card, length %d != expected %d\n",
547 count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
548 return -EINVAL;
551 while(1) {
552 /* Stick ourself on the wait queue */
553 current->state = TASK_INTERRUPTIBLE;
554 add_wait_queue(&FlagSleepRec, &wait);
556 /* Scan each board, looking for one which has a packet for us */
557 for (i=0; i < MAX_BOARD; i++) {
558 if (!apbs[i].RamIO)
559 continue;
560 spin_lock_irqsave(&apbs[i].mutex, flags);
562 tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
564 if (tmp == 2) {
565 /* Got a packet for us */
566 ret = do_ac_read(i, buf);
567 spin_unlock_irqrestore(&apbs[i].mutex, flags);
568 current->state = TASK_RUNNING;
569 remove_wait_queue(&FlagSleepRec, &wait);
570 return tmp;
573 if (tmp > 2) {
574 /* Got an error */
575 Dummy = readb(apbs[i].RamIO + VERS);
577 spin_unlock_irqrestore(&apbs[i].mutex, flags);
578 current->state = TASK_RUNNING;
579 remove_wait_queue(&FlagSleepRec, &wait);
581 printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
582 i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
583 DeviceErrorCount++;
584 return -EIO;
587 /* Nothing for us. Try the next board */
588 Dummy = readb(apbs[i].RamIO + VERS);
589 spin_unlock_irqrestore(&apbs[i].mutex, flags);
591 } /* per board */
593 /* OK - No boards had data for us. Sleep now */
595 schedule();
596 remove_wait_queue(&FlagSleepRec, &wait);
598 if (signal_pending(current))
599 return -EINTR;
601 #ifdef DEBUG
602 if (loopcount++ > 2) {
603 printk("Looping in ac_read. loopcount %d\n", loopcount);
605 #endif
609 static void ac_interrupt(int vec, void *dev_instance, struct pt_regs *regs)
611 unsigned int i;
612 unsigned int FlagInt;
613 unsigned int LoopCount;
615 // printk("Applicom interrupt on IRQ %d occurred\n", vec);
617 LoopCount = 0;
619 do {
620 FlagInt = 0;
621 for (i = 0; i < MAX_BOARD; i++) {
623 /* Skip if this board doesn't exist */
624 if (!apbs[i].RamIO)
625 continue;
627 spin_lock(&apbs[i].mutex);
629 /* Skip if this board doesn't want attention */
630 if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
631 spin_unlock(&apbs[i].mutex);
632 continue;
635 FlagInt = 1;
636 writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
638 if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
639 printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
640 i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
641 DeviceErrorCount++;
644 if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
645 (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
647 printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
648 i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
649 DeviceErrorCount++;
652 if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) { /* mailbox sent by the card ? */
653 if (waitqueue_active(&FlagSleepRec)) {
654 wake_up_interruptible(&FlagSleepRec);
658 if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) { /* ram i/o free for write by pc ? */
659 if (waitqueue_active(&apbs[i].FlagSleepSend)) { /* process sleep during read ? */
660 wake_up_interruptible(&apbs[i].FlagSleepSend);
663 Dummy = readb(apbs[i].RamIO + VERS);
665 if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
666 /* There's another int waiting on this card */
667 spin_unlock(&apbs[i].mutex);
668 i--;
669 } else {
670 spin_unlock(&apbs[i].mutex);
673 if (FlagInt)
674 LoopCount = 0;
675 else
676 LoopCount++;
677 } while(LoopCount < 2);
682 static int ac_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
684 { /* @ ADG ou ATO selon le cas */
685 int i;
686 unsigned char IndexCard;
687 unsigned long pmem;
688 int ret = 0;
689 volatile unsigned char byte_reset_it;
690 struct st_ram_io *adgl;
692 /* In general, the device is only openable by root anyway, so we're not
693 particularly concerned that bogus ioctls can flood the console. */
695 adgl = kmalloc(sizeof(struct st_ram_io), GFP_KERNEL);
696 if (!adgl)
697 return -ENOMEM;
699 if (copy_from_user(adgl, (void *)arg,sizeof(struct st_ram_io))) {
700 kfree(adgl);
701 return -EFAULT;
704 IndexCard = adgl->num_card-1;
706 if(cmd != 0 && cmd != 6 &&
707 ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) {
708 static int warncount = 10;
709 if (warncount) {
710 printk( KERN_WARNING "APPLICOM driver IOCTL, bad board number %d\n",(int)IndexCard+1);
711 warncount--;
713 kfree(adgl);
714 return -EINVAL;
717 switch (cmd) {
719 case 0:
720 pmem = apbs[IndexCard].RamIO;
721 for (i = 0; i < sizeof(struct st_ram_io); i++)
722 ((unsigned char *)adgl)[i]=readb(pmem++);
723 if (copy_to_user((void *)arg, adgl, sizeof(struct st_ram_io)))
724 ret = -EFAULT;
725 break;
726 case 1:
727 pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
728 for (i = 0; i < 4; i++)
729 adgl->conf_end_test[i] = readb(pmem++);
730 for (i = 0; i < 2; i++)
731 adgl->error_code[i] = readb(pmem++);
732 for (i = 0; i < 4; i++)
733 adgl->parameter_error[i] = readb(pmem++);
734 pmem = apbs[IndexCard].RamIO + VERS;
735 adgl->vers = readb(pmem);
736 pmem = apbs[IndexCard].RamIO + TYPE_CARD;
737 for (i = 0; i < 20; i++)
738 adgl->reserv1[i] = readb(pmem++);
739 *(int *)&adgl->reserv1[20] =
740 (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER) << 16) +
741 (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 1) << 8) +
742 (readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 2) );
744 if (copy_to_user((void *)arg, adgl, sizeof(struct st_ram_io)))
745 ret = -EFAULT;
746 break;
747 case 2:
748 pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
749 for (i = 0; i < 10; i++)
750 writeb(0xff, pmem++);
751 writeb(adgl->data_from_pc_ready,
752 apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
754 writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
756 for (i = 0; i < MAX_BOARD; i++) {
757 if (apbs[i].RamIO) {
758 byte_reset_it = readb(apbs[i].RamIO + RAM_IT_TO_PC);
761 break;
762 case 3:
763 pmem = apbs[IndexCard].RamIO + TIC_DES_FROM_PC;
764 writeb(adgl->tic_des_from_pc, pmem);
765 break;
766 case 4:
767 pmem = apbs[IndexCard].RamIO + TIC_OWNER_TO_PC;
768 adgl->tic_owner_to_pc = readb(pmem++);
769 adgl->numcard_owner_to_pc = readb(pmem);
770 if (copy_to_user((void *)arg, adgl,sizeof(struct st_ram_io)))
771 ret = -EFAULT;
772 break;
773 case 5:
774 writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
775 writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
776 writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
777 writeb(4, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
778 writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
779 break;
780 case 6:
781 printk(KERN_INFO "APPLICOM driver release .... V2.8.0 ($Revision: 1.30 $)\n");
782 printk(KERN_INFO "Number of installed boards . %d\n", (int) numboards);
783 printk(KERN_INFO "Segment of board ........... %X\n", (int) mem);
784 printk(KERN_INFO "Interrupt IRQ number ....... %d\n", (int) irq);
785 for (i = 0; i < MAX_BOARD; i++) {
786 int serial;
787 char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
789 if (!apbs[i].RamIO)
790 continue;
792 for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
793 boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
794 boardname[serial] = 0;
796 printk(KERN_INFO "Prom version board %d ....... V%d.%d %s",
797 i+1,
798 (int)(readb(apbs[IndexCard].RamIO + VERS) >> 4),
799 (int)(readb(apbs[IndexCard].RamIO + VERS) & 0xF),
800 boardname);
803 serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
804 (readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
805 (readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
807 if (serial != 0)
808 printk(" S/N %d\n", serial);
809 else
810 printk("\n");
812 if (DeviceErrorCount != 0)
813 printk(KERN_INFO "DeviceErrorCount ........... %d\n", DeviceErrorCount);
814 if (ReadErrorCount != 0)
815 printk(KERN_INFO "ReadErrorCount ............. %d\n", ReadErrorCount);
816 if (WriteErrorCount != 0)
817 printk(KERN_INFO "WriteErrorCount ............ %d\n", WriteErrorCount);
818 if (waitqueue_active(&FlagSleepRec))
819 printk(KERN_INFO "Process in read pending\n");
820 for (i = 0; i < MAX_BOARD; i++) {
821 if (apbs[i].RamIO && waitqueue_active(&apbs[i].FlagSleepSend))
822 printk(KERN_INFO "Process in write pending board %d\n",i+1);
824 break;
825 default:
826 printk(KERN_INFO "APPLICOM driver ioctl, unknown function code %d\n",cmd) ;
827 ret = -EINVAL;
828 break;
830 Dummy = readb(apbs[IndexCard].RamIO + VERS);
831 kfree(adgl);
832 return 0;
835 #ifndef MODULE
836 static int __init applicom_setup(char *str)
838 int ints[4];
840 (void) get_options(str, 4, ints);
842 if (ints[0] > 2) {
843 printk(KERN_WARNING "Too many arguments to 'applicom=', expected mem,irq only.\n");
846 if (ints[0] < 2) {
847 printk(KERN_INFO"applicom numargs: %d\n", ints[0]);
848 return 0;
851 mem = ints[1];
852 irq = ints[2];
853 return 1;
856 __setup("applicom=", applicom_setup);
858 #endif /* MODULE */