1// SPDX-License-Identifier: GPL-2.0-only
2/* Derived from Applicom driver ac.c for SCO Unix                            */
3/* Ported by David Woodhouse, Axiom (Cambridge) Ltd.                         */
4/* dwmw2@infradead.org 30/8/98                                               */
5/* $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $			     */
6/* This module is for Linux 2.1 and 2.2 series kernels.                      */
7/*****************************************************************************/
8/* J PAGET 18/02/94 passage V2.4.2 ioctl avec code 2 reset to les interrupt  */
9/* ceci pour reseter correctement apres une sortie sauvage                   */
10/* J PAGET 02/05/94 passage V2.4.3 dans le traitement de d'interruption,     */
11/* LoopCount n'etait pas initialise a 0.                                     */
12/* F LAFORSE 04/07/95 version V2.6.0 lecture bidon apres acces a une carte   */
13/*           pour liberer le bus                                             */
14/* J.PAGET 19/11/95 version V2.6.1 Nombre, addresse,irq n'est plus configure */
15/* et passe en argument a acinit, mais est scrute sur le bus pour s'adapter  */
16/* au nombre de cartes presentes sur le bus. IOCL code 6 affichait V2.4.3    */
17/* F.LAFORSE 28/11/95 creation de fichiers acXX.o avec les differentes       */
18/* addresses de base des cartes, IOCTL 6 plus complet                         */
19/* J.PAGET le 19/08/96 copie de la version V2.6 en V2.8.0 sans modification  */
20/* de code autre que le texte V2.6.1 en V2.8.0                               */
21/*****************************************************************************/
22
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/interrupt.h>
27#include <linux/sched/signal.h>
28#include <linux/slab.h>
29#include <linux/errno.h>
30#include <linux/mutex.h>
31#include <linux/miscdevice.h>
32#include <linux/pci.h>
33#include <linux/wait.h>
34#include <linux/init.h>
35#include <linux/fs.h>
36#include <linux/nospec.h>
37
38#include <asm/io.h>
39#include <linux/uaccess.h>
40
41#include "applicom.h"
42
43
44/* NOTE: We use for loops with {write,read}b() instead of
45   memcpy_{from,to}io throughout this driver. This is because
46   the board doesn't correctly handle word accesses - only
47   bytes.
48*/
49
50
51#undef DEBUG
52
53#define MAX_BOARD 8		/* maximum of pc board possible */
54#define MAX_ISA_BOARD 4
55#define LEN_RAM_IO 0x800
56
57#ifndef PCI_VENDOR_ID_APPLICOM
58#define PCI_VENDOR_ID_APPLICOM                0x1389
59#define PCI_DEVICE_ID_APPLICOM_PCIGENERIC     0x0001
60#define PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN 0x0002
61#define PCI_DEVICE_ID_APPLICOM_PCI2000PFB     0x0003
62#endif
63
64static DEFINE_MUTEX(ac_mutex);
65static char *applicom_pci_devnames[] = {
66	"PCI board",
67	"PCI2000IBS / PCI2000CAN",
68	"PCI2000PFB"
69};
70
71static const struct pci_device_id applicom_pci_tbl[] = {
72	{ PCI_VDEVICE(APPLICOM, PCI_DEVICE_ID_APPLICOM_PCIGENERIC) },
73	{ PCI_VDEVICE(APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000IBS_CAN) },
74	{ PCI_VDEVICE(APPLICOM, PCI_DEVICE_ID_APPLICOM_PCI2000PFB) },
75	{ 0 }
76};
77MODULE_DEVICE_TABLE(pci, applicom_pci_tbl);
78
79MODULE_AUTHOR("David Woodhouse & Applicom International");
80MODULE_DESCRIPTION("Driver for Applicom Profibus card");
81MODULE_LICENSE("GPL");
82MODULE_ALIAS_MISCDEV(AC_MINOR);
83
84static struct applicom_board {
85	unsigned long PhysIO;
86	void __iomem *RamIO;
87	wait_queue_head_t FlagSleepSend;
88	long irq;
89	spinlock_t mutex;
90} apbs[MAX_BOARD];
91
92static unsigned int irq;	/* interrupt number IRQ       */
93static unsigned long mem;	/* physical segment of board  */
94
95module_param_hw(irq, uint, irq, 0);
96MODULE_PARM_DESC(irq, "IRQ of the Applicom board");
97module_param_hw(mem, ulong, iomem, 0);
98MODULE_PARM_DESC(mem, "Shared Memory Address of Applicom board");
99
100static unsigned int numboards;	/* number of installed boards */
101static volatile unsigned char Dummy;
102static DECLARE_WAIT_QUEUE_HEAD(FlagSleepRec);
103static unsigned int WriteErrorCount;	/* number of write error      */
104static unsigned int ReadErrorCount;	/* number of read error       */
105static unsigned int DeviceErrorCount;	/* number of device error     */
106
107static ssize_t ac_read (struct file *, char __user *, size_t, loff_t *);
108static ssize_t ac_write (struct file *, const char __user *, size_t, loff_t *);
109static long ac_ioctl(struct file *, unsigned int, unsigned long);
110static irqreturn_t ac_interrupt(int, void *);
111
112static const struct file_operations ac_fops = {
113	.owner = THIS_MODULE,
114	.llseek = no_llseek,
115	.read = ac_read,
116	.write = ac_write,
117	.unlocked_ioctl = ac_ioctl,
118};
119
120static struct miscdevice ac_miscdev = {
121	AC_MINOR,
122	"ac",
123	&ac_fops
124};
125
126static int dummy;	/* dev_id for request_irq() */
127
128static int ac_register_board(unsigned long physloc, void __iomem *loc,
129		      unsigned char boardno)
130{
131	volatile unsigned char byte_reset_it;
132
133	if((readb(loc + CONF_END_TEST)     != 0x00) ||
134	   (readb(loc + CONF_END_TEST + 1) != 0x55) ||
135	   (readb(loc + CONF_END_TEST + 2) != 0xAA) ||
136	   (readb(loc + CONF_END_TEST + 3) != 0xFF))
137		return 0;
138
139	if (!boardno)
140		boardno = readb(loc + NUMCARD_OWNER_TO_PC);
141
142	if (!boardno || boardno > MAX_BOARD) {
143		printk(KERN_WARNING "Board #%d (at 0x%lx) is out of range (1 <= x <= %d).\n",
144		       boardno, physloc, MAX_BOARD);
145		return 0;
146	}
147
148	if (apbs[boardno - 1].RamIO) {
149		printk(KERN_WARNING "Board #%d (at 0x%lx) conflicts with previous board #%d (at 0x%lx)\n",
150		       boardno, physloc, boardno, apbs[boardno-1].PhysIO);
151		return 0;
152	}
153
154	boardno--;
155
156	apbs[boardno].PhysIO = physloc;
157	apbs[boardno].RamIO = loc;
158	init_waitqueue_head(&apbs[boardno].FlagSleepSend);
159	spin_lock_init(&apbs[boardno].mutex);
160	byte_reset_it = readb(loc + RAM_IT_TO_PC);
161
162	numboards++;
163	return boardno + 1;
164}
165
166static void __exit applicom_exit(void)
167{
168	unsigned int i;
169
170	misc_deregister(&ac_miscdev);
171
172	for (i = 0; i < MAX_BOARD; i++) {
173
174		if (!apbs[i].RamIO)
175			continue;
176
177		if (apbs[i].irq)
178			free_irq(apbs[i].irq, &dummy);
179
180		iounmap(apbs[i].RamIO);
181	}
182}
183
184static int __init applicom_init(void)
185{
186	int i, numisa = 0;
187	struct pci_dev *dev = NULL;
188	void __iomem *RamIO;
189	int boardno, ret;
190
191	printk(KERN_INFO "Applicom driver: $Id: ac.c,v 1.30 2000/03/22 16:03:57 dwmw2 Exp $\n");
192
193	/* No mem and irq given - check for a PCI card */
194
195	while ( (dev = pci_get_class(PCI_CLASS_OTHERS << 16, dev))) {
196
197		if (!pci_match_id(applicom_pci_tbl, dev))
198			continue;
199
200		if (pci_enable_device(dev)) {
201			pci_dev_put(dev);
202			return -EIO;
203		}
204
205		RamIO = ioremap(pci_resource_start(dev, 0), LEN_RAM_IO);
206
207		if (!RamIO) {
208			printk(KERN_INFO "ac.o: Failed to ioremap PCI memory "
209				"space at 0x%llx\n",
210				(unsigned long long)pci_resource_start(dev, 0));
211			pci_disable_device(dev);
212			pci_dev_put(dev);
213			return -EIO;
214		}
215
216		printk(KERN_INFO "Applicom %s found at mem 0x%llx, irq %d\n",
217		       applicom_pci_devnames[dev->device-1],
218			   (unsigned long long)pci_resource_start(dev, 0),
219		       dev->irq);
220
221		boardno = ac_register_board(pci_resource_start(dev, 0),
222				RamIO, 0);
223		if (!boardno) {
224			printk(KERN_INFO "ac.o: PCI Applicom device doesn't have correct signature.\n");
225			iounmap(RamIO);
226			pci_disable_device(dev);
227			continue;
228		}
229
230		if (request_irq(dev->irq, &ac_interrupt, IRQF_SHARED, "Applicom PCI", &dummy)) {
231			printk(KERN_INFO "Could not allocate IRQ %d for PCI Applicom device.\n", dev->irq);
232			iounmap(RamIO);
233			pci_disable_device(dev);
234			apbs[boardno - 1].RamIO = NULL;
235			continue;
236		}
237
238		/* Enable interrupts. */
239
240		writeb(0x40, apbs[boardno - 1].RamIO + RAM_IT_FROM_PC);
241
242		apbs[boardno - 1].irq = dev->irq;
243	}
244
245	/* Finished with PCI cards. If none registered,
246	 * and there was no mem/irq specified, exit */
247
248	if (!mem || !irq) {
249		if (numboards)
250			goto fin;
251		else {
252			printk(KERN_INFO "ac.o: No PCI boards found.\n");
253			printk(KERN_INFO "ac.o: For an ISA board you must supply memory and irq parameters.\n");
254			return -ENXIO;
255		}
256	}
257
258	/* Now try the specified ISA cards */
259
260	for (i = 0; i < MAX_ISA_BOARD; i++) {
261		RamIO = ioremap(mem + (LEN_RAM_IO * i), LEN_RAM_IO);
262
263		if (!RamIO) {
264			printk(KERN_INFO "ac.o: Failed to ioremap the ISA card's memory space (slot #%d)\n", i + 1);
265			continue;
266		}
267
268		if (!(boardno = ac_register_board((unsigned long)mem+ (LEN_RAM_IO*i),
269						  RamIO,i+1))) {
270			iounmap(RamIO);
271			continue;
272		}
273
274		printk(KERN_NOTICE "Applicom ISA card found at mem 0x%lx, irq %d\n", mem + (LEN_RAM_IO*i), irq);
275
276		if (!numisa) {
277			if (request_irq(irq, &ac_interrupt, IRQF_SHARED, "Applicom ISA", &dummy)) {
278				printk(KERN_WARNING "Could not allocate IRQ %d for ISA Applicom device.\n", irq);
279				iounmap(RamIO);
280				apbs[boardno - 1].RamIO = NULL;
281			}
282			else
283				apbs[boardno - 1].irq = irq;
284		}
285		else
286			apbs[boardno - 1].irq = 0;
287
288		numisa++;
289	}
290
291	if (!numisa)
292		printk(KERN_WARNING "ac.o: No valid ISA Applicom boards found "
293				"at mem 0x%lx\n", mem);
294
295 fin:
296	init_waitqueue_head(&FlagSleepRec);
297
298	WriteErrorCount = 0;
299	ReadErrorCount = 0;
300	DeviceErrorCount = 0;
301
302	if (numboards) {
303		ret = misc_register(&ac_miscdev);
304		if (ret) {
305			printk(KERN_WARNING "ac.o: Unable to register misc device\n");
306			goto out;
307		}
308		for (i = 0; i < MAX_BOARD; i++) {
309			int serial;
310			char boardname[(SERIAL_NUMBER - TYPE_CARD) + 1];
311
312			if (!apbs[i].RamIO)
313				continue;
314
315			for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
316				boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
317
318			boardname[serial] = 0;
319
320
321			printk(KERN_INFO "Applicom board %d: %s, PROM V%d.%d",
322			       i+1, boardname,
323			       (int)(readb(apbs[i].RamIO + VERS) >> 4),
324			       (int)(readb(apbs[i].RamIO + VERS) & 0xF));
325
326			serial = (readb(apbs[i].RamIO + SERIAL_NUMBER) << 16) +
327				(readb(apbs[i].RamIO + SERIAL_NUMBER + 1) << 8) +
328				(readb(apbs[i].RamIO + SERIAL_NUMBER + 2) );
329
330			if (serial != 0)
331				printk(" S/N %d\n", serial);
332			else
333				printk("\n");
334		}
335		return 0;
336	}
337
338	else
339		return -ENXIO;
340
341out:
342	for (i = 0; i < MAX_BOARD; i++) {
343		if (!apbs[i].RamIO)
344			continue;
345		if (apbs[i].irq)
346			free_irq(apbs[i].irq, &dummy);
347		iounmap(apbs[i].RamIO);
348	}
349	return ret;
350}
351
352module_init(applicom_init);
353module_exit(applicom_exit);
354
355
356static ssize_t ac_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
357{
358	unsigned int NumCard;	/* Board number 1 -> 8           */
359	unsigned int IndexCard;	/* Index board number 0 -> 7     */
360	unsigned char TicCard;	/* Board TIC to send             */
361	unsigned long flags;	/* Current priority              */
362	struct st_ram_io st_loc;
363	struct mailbox tmpmailbox;
364#ifdef DEBUG
365	int c;
366#endif
367	DECLARE_WAITQUEUE(wait, current);
368
369	if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
370		static int warncount = 5;
371		if (warncount) {
372			printk(KERN_INFO "Hmmm. write() of Applicom card, length %zd != expected %zd\n",
373			       count, sizeof(struct st_ram_io) + sizeof(struct mailbox));
374			warncount--;
375		}
376		return -EINVAL;
377	}
378
379	if(copy_from_user(&st_loc, buf, sizeof(struct st_ram_io)))
380		return -EFAULT;
381
382	if(copy_from_user(&tmpmailbox, &buf[sizeof(struct st_ram_io)],
383			  sizeof(struct mailbox)))
384		return -EFAULT;
385
386	NumCard = st_loc.num_card;	/* board number to send          */
387	TicCard = st_loc.tic_des_from_pc;	/* tic number to send            */
388	IndexCard = NumCard - 1;
389
390	if (IndexCard >= MAX_BOARD)
391		return -EINVAL;
392	IndexCard = array_index_nospec(IndexCard, MAX_BOARD);
393
394	if (!apbs[IndexCard].RamIO)
395		return -EINVAL;
396
397#ifdef DEBUG
398	printk("Write to applicom card #%d. struct st_ram_io follows:",
399	       IndexCard+1);
400
401		for (c = 0; c < sizeof(struct st_ram_io);) {
402
403			printk("\n%5.5X: %2.2X", c, ((unsigned char *) &st_loc)[c]);
404
405			for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
406				printk(" %2.2X", ((unsigned char *) &st_loc)[c]);
407			}
408		}
409
410		printk("\nstruct mailbox follows:");
411
412		for (c = 0; c < sizeof(struct mailbox);) {
413			printk("\n%5.5X: %2.2X", c, ((unsigned char *) &tmpmailbox)[c]);
414
415			for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
416				printk(" %2.2X", ((unsigned char *) &tmpmailbox)[c]);
417			}
418		}
419
420		printk("\n");
421#endif
422
423	spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
424
425	/* Test octet ready correct */
426	if(readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) > 2) {
427		Dummy = readb(apbs[IndexCard].RamIO + VERS);
428		spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
429		printk(KERN_WARNING "APPLICOM driver write error board %d, DataFromPcReady = %d\n",
430		       IndexCard,(int)readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY));
431		DeviceErrorCount++;
432		return -EIO;
433	}
434
435	/* Place ourselves on the wait queue */
436	set_current_state(TASK_INTERRUPTIBLE);
437	add_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
438
439	/* Check whether the card is ready for us */
440	while (readb(apbs[IndexCard].RamIO + DATA_FROM_PC_READY) != 0) {
441		Dummy = readb(apbs[IndexCard].RamIO + VERS);
442		/* It's busy. Sleep. */
443
444		spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
445		schedule();
446		if (signal_pending(current)) {
447			remove_wait_queue(&apbs[IndexCard].FlagSleepSend,
448					  &wait);
449			return -EINTR;
450		}
451		spin_lock_irqsave(&apbs[IndexCard].mutex, flags);
452		set_current_state(TASK_INTERRUPTIBLE);
453	}
454
455	/* We may not have actually slept */
456	set_current_state(TASK_RUNNING);
457	remove_wait_queue(&apbs[IndexCard].FlagSleepSend, &wait);
458
459	writeb(1, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
460
461	/* Which is best - lock down the pages with rawio and then
462	   copy directly, or use bounce buffers? For now we do the latter
463	   because it works with 2.2 still */
464	{
465		unsigned char *from = (unsigned char *) &tmpmailbox;
466		void __iomem *to = apbs[IndexCard].RamIO + RAM_FROM_PC;
467		int c;
468
469		for (c = 0; c < sizeof(struct mailbox); c++)
470			writeb(*(from++), to++);
471	}
472
473	writeb(0x20, apbs[IndexCard].RamIO + TIC_OWNER_FROM_PC);
474	writeb(0xff, apbs[IndexCard].RamIO + NUMCARD_OWNER_FROM_PC);
475	writeb(TicCard, apbs[IndexCard].RamIO + TIC_DES_FROM_PC);
476	writeb(NumCard, apbs[IndexCard].RamIO + NUMCARD_DES_FROM_PC);
477	writeb(2, apbs[IndexCard].RamIO + DATA_FROM_PC_READY);
478	writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
479	Dummy = readb(apbs[IndexCard].RamIO + VERS);
480	spin_unlock_irqrestore(&apbs[IndexCard].mutex, flags);
481	return 0;
482}
483
484static int do_ac_read(int IndexCard, char __user *buf,
485		struct st_ram_io *st_loc, struct mailbox *mailbox)
486{
487	void __iomem *from = apbs[IndexCard].RamIO + RAM_TO_PC;
488	unsigned char *to = (unsigned char *)mailbox;
489#ifdef DEBUG
490	int c;
491#endif
492
493	st_loc->tic_owner_to_pc = readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC);
494	st_loc->numcard_owner_to_pc = readb(apbs[IndexCard].RamIO + NUMCARD_OWNER_TO_PC);
495
496
497	{
498		int c;
499
500		for (c = 0; c < sizeof(struct mailbox); c++)
501			*(to++) = readb(from++);
502	}
503	writeb(1, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
504	writeb(1, apbs[IndexCard].RamIO + TYP_ACK_FROM_PC);
505	writeb(IndexCard+1, apbs[IndexCard].RamIO + NUMCARD_ACK_FROM_PC);
506	writeb(readb(apbs[IndexCard].RamIO + TIC_OWNER_TO_PC),
507	       apbs[IndexCard].RamIO + TIC_ACK_FROM_PC);
508	writeb(2, apbs[IndexCard].RamIO + ACK_FROM_PC_READY);
509	writeb(0, apbs[IndexCard].RamIO + DATA_TO_PC_READY);
510	writeb(2, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
511	Dummy = readb(apbs[IndexCard].RamIO + VERS);
512
513#ifdef DEBUG
514		printk("Read from applicom card #%d. struct st_ram_io follows:", NumCard);
515
516		for (c = 0; c < sizeof(struct st_ram_io);) {
517			printk("\n%5.5X: %2.2X", c, ((unsigned char *)st_loc)[c]);
518
519			for (c++; c % 8 && c < sizeof(struct st_ram_io); c++) {
520				printk(" %2.2X", ((unsigned char *)st_loc)[c]);
521			}
522		}
523
524		printk("\nstruct mailbox follows:");
525
526		for (c = 0; c < sizeof(struct mailbox);) {
527			printk("\n%5.5X: %2.2X", c, ((unsigned char *)mailbox)[c]);
528
529			for (c++; c % 8 && c < sizeof(struct mailbox); c++) {
530				printk(" %2.2X", ((unsigned char *)mailbox)[c]);
531			}
532		}
533		printk("\n");
534#endif
535	return (sizeof(struct st_ram_io) + sizeof(struct mailbox));
536}
537
538static ssize_t ac_read (struct file *filp, char __user *buf, size_t count, loff_t *ptr)
539{
540	unsigned long flags;
541	unsigned int i;
542	unsigned char tmp;
543	int ret = 0;
544	DECLARE_WAITQUEUE(wait, current);
545#ifdef DEBUG
546	int loopcount=0;
547#endif
548	/* No need to ratelimit this. Only root can trigger it anyway */
549	if (count != sizeof(struct st_ram_io) + sizeof(struct mailbox)) {
550		printk( KERN_WARNING "Hmmm. read() of Applicom card, length %zd != expected %zd\n",
551			count,sizeof(struct st_ram_io) + sizeof(struct mailbox));
552		return -EINVAL;
553	}
554
555	while(1) {
556		/* Stick ourself on the wait queue */
557		set_current_state(TASK_INTERRUPTIBLE);
558		add_wait_queue(&FlagSleepRec, &wait);
559
560		/* Scan each board, looking for one which has a packet for us */
561		for (i=0; i < MAX_BOARD; i++) {
562			if (!apbs[i].RamIO)
563				continue;
564			spin_lock_irqsave(&apbs[i].mutex, flags);
565
566			tmp = readb(apbs[i].RamIO + DATA_TO_PC_READY);
567
568			if (tmp == 2) {
569				struct st_ram_io st_loc;
570				struct mailbox mailbox;
571
572				/* Got a packet for us */
573				memset(&st_loc, 0, sizeof(st_loc));
574				ret = do_ac_read(i, buf, &st_loc, &mailbox);
575				spin_unlock_irqrestore(&apbs[i].mutex, flags);
576				set_current_state(TASK_RUNNING);
577				remove_wait_queue(&FlagSleepRec, &wait);
578
579				if (copy_to_user(buf, &st_loc, sizeof(st_loc)))
580					return -EFAULT;
581				if (copy_to_user(buf + sizeof(st_loc), &mailbox, sizeof(mailbox)))
582					return -EFAULT;
583				return tmp;
584			}
585
586			if (tmp > 2) {
587				/* Got an error */
588				Dummy = readb(apbs[i].RamIO + VERS);
589
590				spin_unlock_irqrestore(&apbs[i].mutex, flags);
591				set_current_state(TASK_RUNNING);
592				remove_wait_queue(&FlagSleepRec, &wait);
593
594				printk(KERN_WARNING "APPLICOM driver read error board %d, DataToPcReady = %d\n",
595				       i,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
596				DeviceErrorCount++;
597				return -EIO;
598			}
599
600			/* Nothing for us. Try the next board */
601			Dummy = readb(apbs[i].RamIO + VERS);
602			spin_unlock_irqrestore(&apbs[i].mutex, flags);
603
604		} /* per board */
605
606		/* OK - No boards had data for us. Sleep now */
607
608		schedule();
609		remove_wait_queue(&FlagSleepRec, &wait);
610
611		if (signal_pending(current))
612			return -EINTR;
613
614#ifdef DEBUG
615		if (loopcount++ > 2) {
616			printk(KERN_DEBUG "Looping in ac_read. loopcount %d\n", loopcount);
617		}
618#endif
619	}
620}
621
622static irqreturn_t ac_interrupt(int vec, void *dev_instance)
623{
624	unsigned int i;
625	unsigned int FlagInt;
626	unsigned int LoopCount;
627	int handled = 0;
628
629	//    printk("Applicom interrupt on IRQ %d occurred\n", vec);
630
631	LoopCount = 0;
632
633	do {
634		FlagInt = 0;
635		for (i = 0; i < MAX_BOARD; i++) {
636
637			/* Skip if this board doesn't exist */
638			if (!apbs[i].RamIO)
639				continue;
640
641			spin_lock(&apbs[i].mutex);
642
643			/* Skip if this board doesn't want attention */
644			if(readb(apbs[i].RamIO + RAM_IT_TO_PC) == 0) {
645				spin_unlock(&apbs[i].mutex);
646				continue;
647			}
648
649			handled = 1;
650			FlagInt = 1;
651			writeb(0, apbs[i].RamIO + RAM_IT_TO_PC);
652
653			if (readb(apbs[i].RamIO + DATA_TO_PC_READY) > 2) {
654				printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataToPcReady = %d\n",
655				       i+1,(int)readb(apbs[i].RamIO + DATA_TO_PC_READY));
656				DeviceErrorCount++;
657			}
658
659			if((readb(apbs[i].RamIO + DATA_FROM_PC_READY) > 2) &&
660			   (readb(apbs[i].RamIO + DATA_FROM_PC_READY) != 6)) {
661
662				printk(KERN_WARNING "APPLICOM driver interrupt err board %d, DataFromPcReady = %d\n",
663				       i+1,(int)readb(apbs[i].RamIO + DATA_FROM_PC_READY));
664				DeviceErrorCount++;
665			}
666
667			if (readb(apbs[i].RamIO + DATA_TO_PC_READY) == 2) {	/* mailbox sent by the card ?   */
668				if (waitqueue_active(&FlagSleepRec)) {
669				wake_up_interruptible(&FlagSleepRec);
670			}
671			}
672
673			if (readb(apbs[i].RamIO + DATA_FROM_PC_READY) == 0) {	/* ram i/o free for write by pc ? */
674				if (waitqueue_active(&apbs[i].FlagSleepSend)) {	/* process sleep during read ?    */
675					wake_up_interruptible(&apbs[i].FlagSleepSend);
676				}
677			}
678			Dummy = readb(apbs[i].RamIO + VERS);
679
680			if(readb(apbs[i].RamIO + RAM_IT_TO_PC)) {
681				/* There's another int waiting on this card */
682				spin_unlock(&apbs[i].mutex);
683				i--;
684			} else {
685				spin_unlock(&apbs[i].mutex);
686			}
687		}
688		if (FlagInt)
689			LoopCount = 0;
690		else
691			LoopCount++;
692	} while(LoopCount < 2);
693	return IRQ_RETVAL(handled);
694}
695
696
697
698static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
699
700{				/* @ ADG ou ATO selon le cas */
701	int i;
702	unsigned char IndexCard;
703	void __iomem *pmem;
704	int ret = 0;
705	static int warncount = 10;
706	volatile unsigned char byte_reset_it;
707	struct st_ram_io *adgl;
708	void __user *argp = (void __user *)arg;
709
710	/* In general, the device is only openable by root anyway, so we're not
711	   particularly concerned that bogus ioctls can flood the console. */
712
713	adgl = memdup_user(argp, sizeof(struct st_ram_io));
714	if (IS_ERR(adgl))
715		return PTR_ERR(adgl);
716
717	mutex_lock(&ac_mutex);
718	IndexCard = adgl->num_card-1;
719
720	if (cmd != 6 && IndexCard >= MAX_BOARD)
721		goto err;
722	IndexCard = array_index_nospec(IndexCard, MAX_BOARD);
723
724	if (cmd != 6 && !apbs[IndexCard].RamIO)
725		goto err;
726
727	switch (cmd) {
728
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(argp, 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) );
753
754		if (copy_to_user(argp, 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);
763
764		writeb(1, apbs[IndexCard].RamIO + RAM_IT_FROM_PC);
765
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);
769			}
770		}
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(argp, 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];
798
799			if (!apbs[i].RamIO)
800				continue;
801
802			for (serial = 0; serial < SERIAL_NUMBER - TYPE_CARD; serial++)
803				boardname[serial] = readb(apbs[i].RamIO + TYPE_CARD + serial);
804			boardname[serial] = 0;
805
806			printk(KERN_INFO "Prom version board %d ....... V%d.%d %s",
807			       i+1,
808			       (int)(readb(apbs[i].RamIO + VERS) >> 4),
809			       (int)(readb(apbs[i].RamIO + VERS) & 0xF),
810			       boardname);
811
812
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) );
816
817			if (serial != 0)
818				printk(" S/N %d\n", serial);
819			else
820				printk("\n");
821		}
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);
833		}
834		break;
835	default:
836		ret = -ENOTTY;
837		break;
838	}
839	Dummy = readb(apbs[IndexCard].RamIO + VERS);
840	kfree(adgl);
841	mutex_unlock(&ac_mutex);
842	return ret;
843
844err:
845	if (warncount) {
846		pr_warn("APPLICOM driver IOCTL, bad board number %d\n",
847			(int)IndexCard + 1);
848		warncount--;
849	}
850	kfree(adgl);
851	mutex_unlock(&ac_mutex);
852	return -EINVAL;
853
854}
855
856