File: /usr/src/linux/drivers/char/applicom.c

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     /*****************************************************************************/
21     
22     
23     #include <linux/kernel.h>
24     #include <linux/module.h>
25     #include <linux/slab.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>
34     
35     #include "applicom.h"
36     
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
46     
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. 
51     */
52     
53     
54     #undef DEBUG
55     
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
60     
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
68     
69     static char *applicom_pci_devnames[] = {
70     	"PCI board",
71     	"PCI2000IBS / PCI2000CAN",
72     	"PCI2000PFB"
73     };
74     
75     MODULE_AUTHOR("David Woodhouse & Applicom International");
76     MODULE_DESCRIPTION("Driver for Applicom Profibus card");
77     MODULE_LICENSE("GPL");
78     MODULE_PARM(irq, "i");
79     MODULE_PARM_DESC(irq, "IRQ of the Applicom board");
80     MODULE_PARM(mem, "i");
81     MODULE_PARM_DESC(mem, "Shared Memory Address of Applicom board");
82     
83     MODULE_SUPPORTED_DEVICE("ac");
84     
85     
86     static struct applicom_board {
87     	unsigned long PhysIO;
88     	unsigned long RamIO;
89     	wait_queue_head_t FlagSleepSend;
90     	long irq;
91     	spinlock_t mutex;
92     } apbs[MAX_BOARD];
93     
94     static unsigned int irq = 0;	/* interrupt number IRQ       */
95     static unsigned long mem = 0;	/* physical segment of board  */
96     
97     static unsigned int numboards;	/* number of installed boards */
98     static volatile unsigned char Dummy;
99     static DECLARE_WAIT_QUEUE_HEAD(FlagSleepRec);
100     static unsigned int WriteErrorCount;	/* number of write error      */
101     static unsigned int ReadErrorCount;	/* number of read error       */
102     static unsigned int DeviceErrorCount;	/* number of device error     */
103     
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 *);
109     
110     static struct file_operations ac_fops = {
111     	owner:THIS_MODULE,
112     	llseek:no_llseek,
113     	read:ac_read,
114     	write:ac_write,
115     	ioctl:ac_ioctl,
116     };
117     
118     static struct miscdevice ac_miscdev = {
119     	AC_MINOR,
120     	"ac",
121     	&ac_fops
122     };
123     
124     static int dummy;	/* dev_id for request_irq() */
125     
126     static int ac_register_board(unsigned long physloc, unsigned long loc, 
127     		      unsigned char boardno)
128     {
129     	volatile unsigned char byte_reset_it;
130     
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;
136     
137     	if (!boardno)
138     		boardno = readb(loc + NUMCARD_OWNER_TO_PC);
139     
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;
144     	}
145     
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;
150     	}
151     
152     	boardno--;
153     
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);
159     
160     	numboards++;
161     	return boardno + 1;
162     }
163     
164     #ifdef MODULE
165     
166     #define applicom_init init_module
167     
168     void cleanup_module(void)
169     {
170     	int i;
171     
172     	misc_deregister(&ac_miscdev);
173     
174     	for (i = 0; i < MAX_BOARD; i++) {
175     
176     		if (!apbs[i].RamIO)
177     			continue;
178     		
179     		iounmap((void *) apbs[i].RamIO);
180     
181     		if (apbs[i].irq)
182     			free_irq(apbs[i].irq, &dummy);
183     	}
184     }
185     
186     #endif				/* MODULE */
187     
188     int __init applicom_init(void)
189     {
190     	int i, numisa = 0;
191     	struct pci_dev *dev = NULL;
192     	void *RamIO;
193     	int boardno;
194     
195     	printk(KERN_INFO "Applicom driver: $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $\n");
196     
197     	/* No mem and irq given - check for a PCI card */
198     
199     	while ( (dev = pci_find_class(PCI_CLASS_OTHERS << 16, dev))) {
200     
201     		if (dev->vendor != PCI_VENDOR_ID_APPLICOM)
202     			continue;
203     		
204     		if (dev->device  > MAX_PCI_DEVICE_NUM || dev->device == 0)
205     			continue;
206     		
207     		if (pci_enable_device(dev))
208     			return -EIO;
209     
210     		RamIO = ioremap(PCI_BASE_ADDRESS(dev), LEN_RAM_IO);
211     
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;
215     		}
216     
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);
220     
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;
226     		}
227     
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;
233     		}
234     
235     		/* Enable interrupts. */
236     
237     		writeb(0x40, apbs[boardno - 1].RamIO + RAM_IT_FROM_PC);
238     
239     		apbs[boardno - 1].irq = dev->irq;
240     	}
241     
242     	/* Finished with PCI cards. If none registered, 
243     	 * and there was no mem/irq specified, exit */
244     
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;
252     		}
253     	}
254     
255     	/* Now try the specified ISA cards */
256     
257     #warning "LEAK"
258     	RamIO = ioremap(mem, LEN_RAM_IO * MAX_ISA_BOARD);
259     
260     	if (!RamIO) 
261     		printk(KERN_INFO "ac.o: Failed to ioremap ISA memory space at 0x%lx\n", mem);
262     
263     	for (i = 0; i < MAX_ISA_BOARD; i++) {
264     		RamIO = ioremap(mem + (LEN_RAM_IO * i), LEN_RAM_IO);
265     
266     		if (!RamIO) {
267     			printk(KERN_INFO "ac.o: Failed to ioremap the ISA card's memory space (slot #%d)\n", i + 1);
268     			continue;
269     		}
270     
271     		if (!(boardno = ac_register_board((unsigned long)mem+ (LEN_RAM_IO*i),
272     						  (unsigned long)RamIO,i+1))) {
273     			iounmap(RamIO);
274     			continue;
275     		}
276     
277     		printk(KERN_NOTICE "Applicom ISA card found at mem 0x%lx, irq %d\n", mem + (LEN_RAM_IO*i), irq);
278     
279     		if (!numisa) {
280     			if (request_irq(irq, &ac_interrupt, SA_SHIRQ, "Applicom ISA", &dummy)) {
281     				printk(KERN_WARNING "Could not allocate IRQ %d for ISA Applicom device.\n", irq);
282     				iounmap((void *) RamIO);
283     				apbs[boardno - 1].RamIO = 0;
284     			}
285     			apbs[boardno - 1].irq = irq;
286     		}
287     		else
288     			apbs[boardno - 1].irq = 0;
289     
290     		numisa++;
291     	}
292     
293     	if (!numisa)
294     		printk(KERN_WARNING"ac.o: No valid ISA Applicom boards found at mem 0x%lx\n",mem);
295     
296      fin:
297     	init_waitqueue_head(&FlagSleepRec);
298     
299     	WriteErrorCount = 0;
300     	ReadErrorCount = 0;
301     	DeviceErrorCount = 0;
302     
303     	if (numboards) {
304     		misc_register(&ac_miscdev);
305     		for (i = 0; i < MAX_BOARD; i++) {
306     			int serial;
307     			char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
308     
309     			if (!apbs[i].RamIO)
310     				continue;
311     
312     			for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
313     				boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
314     
315     			boardname[serial] = 0;
316     
317     
318     			printk(KERN_INFO "Applicom board %d: %s, PROM V%d.%d",
319     			       i+1, boardname,
320     			       (int)(readb(apbs[i].RamIO + VERS) >> 4),
321     			       (int)(readb(apbs[i].RamIO + VERS) & 0xF));
322     			
323     			serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) + 
324     				(readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) + 
325     				(readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
326     
327     			if (serial != 0)
328     				printk(" S/N %d\n", serial);
329     			else
330     				printk("\n");
331     		}
332     		return 0;
333     	}
334     
335     	else
336     		return -ENXIO;
337     }
338     
339     
340     #ifndef MODULE
341     __initcall(applicom_init);
342     #endif
343     
344     static ssize_t ac_write(struct file *file, const char *buf, size_t count, loff_t * ppos)
345     {
346     	unsigned int NumCard;	/* Board number 1 -> 8           */
347     	unsigned int IndexCard;	/* Index board number 0 -> 7     */
348     	unsigned char TicCard;	/* Board TIC to send             */
349     	unsigned long flags;	/* Current priority              */
350     	struct st_ram_io st_loc;
351     	struct mailbox tmpmailbox;
352     #ifdef DEBUG
353     	int c;
354     #endif
355     	DECLARE_WAITQUEUE(wait, current);
356     
357     	if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
358     		static int warncount = 5;
359     		if (warncount) {
360     			printk(KERN_INFO "Hmmm. write() of Applicom card, length %d != expected %d\n",
361     			       count, sizeof(struct st_ram_io) + sizeof(struct mailbox));
362     			warncount--;
363     		}
364     		return -EINVAL;
365     	}
366     
367     	if(copy_from_user(&st_loc, buf, sizeof(struct st_ram_io))) 
368     		return -EFAULT;
369     	
370     	if(copy_from_user(&tmpmailbox, &buf[sizeof(struct st_ram_io)],
371     			  sizeof(struct mailbox))) 
372     		return -EFAULT;
373     
374     	NumCard = st_loc.num_card;	/* board number to send          */
375     	TicCard = st_loc.tic_des_from_pc;	/* tic number to send            */
376     	IndexCard = NumCard - 1;
377     
378     	if((NumCard < 1) || (NumCard > MAX_BOARD) || !apbs[IndexCard].RamIO)
379     		return -EINVAL;
380     
381     #ifdef DEBUG
382     	printk("Write to applicom card #%d. struct st_ram_io follows:",
383     	       IndexCard+1);
384     
385     		for (c = 0; c < sizeof(struct st_ram_io);) {
386     		
387     			printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
388     
389     			for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
390     				printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
391     			}
392     		}
393     
394     		printk("\nstruct mailbox follows:");
395     
396     		for (c = 0; c < sizeof(struct mailbox);) {
397     			printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
398     
399     			for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
400     				printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
401     			}
402     		}
403     
404     		printk("\n");
405     #endif
406     
407     	spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
408     
409     	/* Test octet ready correct */
410     	if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) { 
411     		Dummy = readb(apbs[IndexCard].RamIO + VERS);
412     		spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
413     		printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
414     		       IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
415     		DeviceErrorCount++;
416     		return -EIO;
417     	}
418     	
419     	/* Place ourselves on the wait queue */
420     	set_current_state(TASK_INTERRUPTIBLE);
421     	add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
422     
423     	/* Check whether the card is ready for us */
424     	while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
425     		Dummy = readb(apbs[IndexCard].RamIO + VERS);
426     		/* It's busy. Sleep. */
427     
428     		spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
429     		schedule();
430     		if (signal_pending(current)) {
431     			remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
432     					  &wait);
433     			return -EINTR;
434     		}
435     		spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
436     		set_current_state(TASK_INTERRUPTIBLE);
437     	}
438     
439     	/* We may not have actually slept */
440     	current->state = TASK_RUNNING;
441     	remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
442     
443     	writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
444     
445     	/* Which is best - lock down the pages with rawio and then
446     	   copy directly, or use bounce buffers? For now we do the latter 
447     	   because it works with 2.2 still */
448     	{
449     		unsigned char *from = (unsigned char *) &tmpmailbox;
450     		unsigned long to = (unsigned long) apbs[IndexCard].RamIO + RAM_FROM_PC;
451     		int c;
452     
453     		for (c = 0; c < sizeof(struct mailbox); c++)
454     			writeb(*(from++), to++);
455     	}
456     
457     	writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
458     	writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
459     	writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
460     	writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
461     	writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
462     	writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
463     	Dummy = readb(apbs[IndexCard].RamIO + VERS);
464     	spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
465     	return 0;
466     }
467     
468     static int do_ac_read(int IndexCard, char *buf)
469     {
470     	struct st_ram_io st_loc;
471     	struct mailbox tmpmailbox;	/* bounce buffer - can't copy to user space with cli() */
472     	unsigned long from = (unsigned long)apbs[IndexCard].RamIO + RAM_TO_PC;
473     	unsigned char *to = (unsigned char *)&tmpmailbox;
474     #ifdef DEBUG
475     	int c;
476     #endif
477     
478     	st_loc.tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
479     	st_loc.numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
480     
481     
482     	{
483     		int c;
484     
485     		for (c = 0; c < sizeof(struct mailbox); c++)
486     			*(to++) = readb(from++);
487     	}
488     	writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
489     	writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
490     	writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
491     	writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC), 
492     	       apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
493     	writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
494     	writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
495     	writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
496     	Dummy = readb(apbs[IndexCard].RamIO + VERS);
497     
498     #ifdef DEBUG
499     		printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
500     
501     		for (c = 0; c < sizeof(struct st_ram_io);) {
502     			printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
503     
504     			for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
505     				printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
506     			}
507     		}
508     
509     		printk("\nstruct mailbox follows:");
510     
511     		for (c = 0; c < sizeof(struct mailbox);) {
512     			printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
513     
514     			for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
515     				printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
516     			}
517     		}
518     		printk("\n");
519     #endif
520     
521     #warning "Je suis stupide. DW. - copy*user in cli"
522     
523     	if (copy_to_user(buf, &st_loc, sizeof(struct st_ram_io)))
524     		return -EFAULT;
525     	if (copy_to_user(&buf[sizeof(struct st_ram_io)], &tmpmailbox, sizeof(struct mailbox)))
526     		return -EFAULT;
527     
528     	return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
529     }
530     
531     static ssize_t ac_read (struct file *filp, char *buf, size_t count, loff_t *ptr)
532     {
533     	unsigned long flags;
534     	unsigned int i;
535     	unsigned char tmp;
536     	int ret = 0;
537     	DECLARE_WAITQUEUE(wait, current);
538     #ifdef DEBUG
539     	int loopcount=0;
540     #endif
541     	/* No need to ratelimit this. Only root can trigger it anyway */
542     	if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
543     		printk( KERN_WARNING "Hmmm. read() of Applicom card, length %d != expected %d\n",
544     			count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
545     		return -EINVAL;
546     	}
547     	
548     	while(1) {
549     		/* Stick ourself on the wait queue */
550     		set_current_state(TASK_INTERRUPTIBLE);
551     		add_wait_queue(&FlagSleepRec, &wait);
552     		
553     		/* Scan each board, looking for one which has a packet for us */
554     		for (i=0; i < MAX_BOARD; i++) {
555     			if (!apbs[i].RamIO)
556     				continue;
557     			spin_lock_irqsave(&apbs[i].mutex, flags);
558     			
559     			tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
560     			
561     			if (tmp == 2) {
562     				/* Got a packet for us */
563     				ret = do_ac_read(i, buf);
564     				spin_unlock_irqrestore(&apbs[i].mutex, flags);
565     				set_current_state(TASK_RUNNING);
566     				remove_wait_queue(&FlagSleepRec, &wait);
567     				return tmp;
568     			}
569     			
570     			if (tmp > 2) {
571     				/* Got an error */
572     				Dummy = readb(apbs[i].RamIO + VERS);
573     				
574     				spin_unlock_irqrestore(&apbs[i].mutex, flags);
575     				set_current_state(TASK_RUNNING);
576     				remove_wait_queue(&FlagSleepRec, &wait);
577     				
578     				printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
579     				       i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
580     				DeviceErrorCount++;
581     				return -EIO;
582     			}
583     			
584     			/* Nothing for us. Try the next board */
585     			Dummy = readb(apbs[i].RamIO + VERS);
586     			spin_unlock_irqrestore(&apbs[i].mutex, flags);
587     			
588     		} /* per board */
589     
590     		/* OK - No boards had data for us. Sleep now */
591     
592     		schedule();
593     		remove_wait_queue(&FlagSleepRec, &wait);
594     
595     		if (signal_pending(current))
596     			return -EINTR;
597     
598     #ifdef DEBUG
599     		if (loopcount++ > 2) {
600     			printk("Looping in ac_read. loopcount %d\n", loopcount);
601     		}
602     #endif
603     	} 
604     }
605     
606     static void ac_interrupt(int vec, void *dev_instance, struct pt_regs *regs)
607     {
608     	unsigned int i;
609     	unsigned int FlagInt;
610     	unsigned int LoopCount;
611     
612     	//    printk("Applicom interrupt on IRQ %d occurred\n", vec);
613     
614     	LoopCount = 0;
615     
616     	do {
617     		FlagInt = 0;
618     		for (i = 0; i < MAX_BOARD; i++) {
619     			
620     			/* Skip if this board doesn't exist */
621     			if (!apbs[i].RamIO)
622     				continue;
623     
624     			spin_lock(&apbs[i].mutex);
625     
626     			/* Skip if this board doesn't want attention */
627     			if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
628     				spin_unlock(&apbs[i].mutex);
629     				continue;
630     			}
631     
632     			FlagInt = 1;
633     			writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
634     
635     			if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
636     				printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
637     				       i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
638     				DeviceErrorCount++;
639     			}
640     
641     			if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) && 
642     			   (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
643     				
644     				printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
645     				       i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
646     				DeviceErrorCount++;
647     			}
648     
649     			if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) {	/* mailbox sent by the card ?   */
650     				if (waitqueue_active(&FlagSleepRec)) {
651     				wake_up_interruptible(&FlagSleepRec);
652     			}
653     			}
654     
655     			if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) {	/* ram i/o free for write by pc ? */
656     				if (waitqueue_active(&apbs[i].FlagSleepSend)) {	/* process sleep during read ?    */
657     					wake_up_interruptible(&apbs[i].FlagSleepSend);
658     				}
659     			}
660     			Dummy = readb(apbs[i].RamIO + VERS);
661     
662     			if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
663     				/* There's another int waiting on this card */
664     				spin_unlock(&apbs[i].mutex);
665     				i--;
666     			} else {
667     				spin_unlock(&apbs[i].mutex);
668     			}
669     		}
670     		if (FlagInt)
671     			LoopCount = 0;
672     		else
673     			LoopCount++;
674     	} while(LoopCount < 2);
675     }
676     
677     
678     
679     static int ac_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
680          
681     {				/* @ ADG ou ATO selon le cas */
682     	int i;
683     	unsigned char IndexCard;
684     	unsigned long pmem;
685     	int ret = 0;
686     	volatile unsigned char byte_reset_it;
687     	struct st_ram_io *adgl;
688     
689     	/* In general, the device is only openable by root anyway, so we're not
690     	   particularly concerned that bogus ioctls can flood the console. */
691     
692     	adgl = kmalloc(sizeof(struct st_ram_io), GFP_KERNEL);
693     	if (!adgl)
694     		return -ENOMEM;
695     
696     	if (copy_from_user(adgl, (void *)arg,sizeof(struct st_ram_io))) {
697     		kfree(adgl);
698     		return -EFAULT;
699     	}
700     	
701     	IndexCard = adgl->num_card-1;
702     	 
703     	if(cmd != 0 && cmd != 6 &&
704     	   ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) {
705     		static int warncount = 10;
706     		if (warncount) {
707     			printk( KERN_WARNING "APPLICOM driver IOCTL, bad board number %d\n",(int)IndexCard+1);
708     			warncount--;
709     		}
710     		kfree(adgl);
711     		return -EINVAL;
712     	}
713     
714     	switch (cmd) {
715     		
716     	case 0:
717     		pmem = apbs[IndexCard].RamIO;
718     		for (i = 0; i < sizeof(struct st_ram_io); i++)
719     			((unsigned char *)adgl)[i]=readb(pmem++);
720     		if (copy_to_user((void *)arg, adgl, sizeof(struct st_ram_io)))
721     			ret = -EFAULT;
722     		break;
723     	case 1:
724     		pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
725     		for (i = 0; i < 4; i++)
726     			adgl->conf_end_test[i] = readb(pmem++);
727     		for (i = 0; i < 2; i++)
728     			adgl->error_code[i] = readb(pmem++);
729     		for (i = 0; i < 4; i++)
730     			adgl->parameter_error[i] = readb(pmem++);
731     		pmem = apbs[IndexCard].RamIO + VERS;
732     		adgl->vers = readb(pmem);
733     		pmem = apbs[IndexCard].RamIO + TYPE_CARD;
734     		for (i = 0; i < 20; i++)
735     			adgl->reserv1[i] = readb(pmem++);
736     		*(int *)&adgl->reserv1[20] =  
737     			(readb(apbs[IndexCard].RamIO + SERIAL_NUMBER) << 16) + 
738     			(readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 1) << 8) + 
739     			(readb(apbs[IndexCard].RamIO + SERIAL_NUMBER + 2) );
740     
741     		if (copy_to_user((void *)arg, adgl, sizeof(struct st_ram_io)))
742     			ret = -EFAULT;
743     		break;
744     	case 2:
745     		pmem = apbs[IndexCard].RamIO + CONF_END_TEST;
746     		for (i = 0; i < 10; i++)
747     			writeb(0xff, pmem++);
748     		writeb(adgl->data_from_pc_ready, 
749     		       apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
750     
751     		writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
752     		
753     		for (i = 0; i < MAX_BOARD; i++) {
754     			if (apbs[i].RamIO) {
755     				byte_reset_it = readb(apbs[i].RamIO + RAM_IT_TO_PC);
756     			}
757     		}
758     		break;
759     	case 3:
760     		pmem = apbs[IndexCard].RamIO + TIC_DES_FROM_PC;
761     		writeb(adgl->tic_des_from_pc, pmem);
762     		break;
763     	case 4:
764     		pmem = apbs[IndexCard].RamIO + TIC_OWNER_TO_PC;
765     		adgl->tic_owner_to_pc     = readb(pmem++);
766     		adgl->numcard_owner_to_pc = readb(pmem);
767     		if (copy_to_user((void *)arg, adgl,sizeof(struct st_ram_io)))
768     			ret = -EFAULT;
769     		break;
770     	case 5:
771     		writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
772     		writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
773     		writeb(adgl->num_card, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
774     		writeb(4, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
775     		writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
776     		break;
777     	case 6:
778     		printk(KERN_INFO "APPLICOM driver release .... V2.8.0 ($Revision: 1.30 $)\n");
779     		printk(KERN_INFO "Number of installed boards . %d\n", (int) numboards);
780     		printk(KERN_INFO "Segment of board ........... %X\n", (int) mem);
781     		printk(KERN_INFO "Interrupt IRQ number ....... %d\n", (int) irq);
782     		for (i = 0; i < MAX_BOARD; i++) {
783     			int serial;
784     			char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
785     
786     			if (!apbs[i].RamIO)
787     				continue;
788     
789     			for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
790     				boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
791     			boardname[serial] = 0;
792     
793     			printk(KERN_INFO "Prom version board %d ....... V%d.%d %s",
794     			       i+1,
795     			       (int)(readb(apbs[IndexCard].RamIO + VERS) >> 4),
796     			       (int)(readb(apbs[IndexCard].RamIO + VERS) & 0xF),
797     			       boardname);
798     
799     
800     			serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) + 
801     				(readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) + 
802     				(readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
803     
804     			if (serial != 0)
805     				printk(" S/N %d\n", serial);
806     			else
807     				printk("\n");
808     		}
809     		if (DeviceErrorCount != 0)
810     			printk(KERN_INFO "DeviceErrorCount ........... %d\n", DeviceErrorCount);
811     		if (ReadErrorCount != 0)
812     			printk(KERN_INFO "ReadErrorCount ............. %d\n", ReadErrorCount);
813     		if (WriteErrorCount != 0)
814     			printk(KERN_INFO "WriteErrorCount ............ %d\n", WriteErrorCount);
815     		if (waitqueue_active(&FlagSleepRec))
816     			printk(KERN_INFO "Process in read pending\n");
817     		for (i = 0; i < MAX_BOARD; i++) {
818     			if (apbs[i].RamIO && waitqueue_active(&apbs[i].FlagSleepSend))
819     				printk(KERN_INFO "Process in write pending board %d\n",i+1);
820     		}
821     		break;
822     	default:
823     		printk(KERN_INFO "APPLICOM driver ioctl, unknown function code %d\n",cmd) ;
824     		ret = -EINVAL;
825     		break;
826     	}
827     	Dummy = readb(apbs[IndexCard].RamIO + VERS);
828     	kfree(adgl);
829     	return 0;
830     }
831     
832     #ifndef MODULE
833     static int __init applicom_setup(char *str)
834     {
835     	int ints[4];
836     
837     	(void) get_options(str, 4, ints);
838     
839     	if (ints[0] > 2) {
840     		printk(KERN_WARNING "Too many arguments to 'applicom=', expected mem,irq only.\n");
841     	}
842     
843     	if (ints[0] < 2) {
844     		printk(KERN_INFO"applicom numargs: %d\n", ints[0]);
845     		return 0;
846     	}
847     
848     	mem = ints[1];
849     	irq = ints[2];
850     	return 1;
851     }
852     
853     __setup("applicom=", applicom_setup);
854     
855     #endif				/* MODULE */
856     
857