addi_common.h revision babf0ede302697486a6633e72d051bbb8e31e2b2
1/*
2 *  Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code of this module.
3 *
4 *	ADDI-DATA GmbH
5 *	Dieselstrasse 3
6 *	D-77833 Ottersweier
7 *	Tel: +19(0)7223/9493-0
8 *	Fax: +49(0)7223/9493-92
9 *	http://www.addi-data-com
10 *	info@addi-data.com
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/sched.h>
21#include <linux/mm.h>
22#include <linux/slab.h>
23#include <linux/errno.h>
24#include <linux/ioport.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/timex.h>
28#include <linux/timer.h>
29#include <linux/pci.h>
30#include <linux/io.h>
31#include <linux/kmod.h>
32#include <linux/uaccess.h>
33#include "../../comedidev.h"
34#include "addi_amcc_s5933.h"
35
36#define ERROR	-1
37#define SUCCESS	1
38
39/* variable type definition */
40typedef int INT, *PINT;;
41typedef unsigned int UINT, *PUINT;
42typedef int LONG, *PLONG;		/* 32-bit */
43typedef unsigned int ULONG, *PULONG;	/* 32-bit */
44typedef unsigned int DWORD, *PDWORD;	/* 32-bit */
45typedef unsigned long ULONG_PTR;
46
47typedef const struct comedi_lrange *PCRANGE;
48
49#define LOBYTE(W)	(unsigned char)((W) & 0xFF)
50#define HIBYTE(W)	(unsigned char)(((W) >> 8) & 0xFF)
51#define MAKEWORD(H, L)	(unsigned short)((L) | ((H) << 8))
52#define LOWORD(W)	(unsigned short)((W) & 0xFFFF)
53#define HIWORD(W)	(unsigned short)(((W) >> 16) & 0xFFFF)
54#define MAKEDWORD(H, L)	(UINT)((L) | ((H) << 16))
55
56#define ADDI_ENABLE		1
57#define ADDI_DISABLE		0
58#define APCI1710_SAVE_INTERRUPT	1
59
60#define ADDIDATA_EEPROM		1
61#define ADDIDATA_NO_EEPROM	0
62#define ADDIDATA_93C76		"93C76"
63#define ADDIDATA_S5920		"S5920"
64#define ADDIDATA_S5933		"S5933"
65#define ADDIDATA_9054		"9054"
66
67/* ADDIDATA Enable Disable */
68#define ADDIDATA_ENABLE		1
69#define ADDIDATA_DISABLE	0
70
71/* Structures */
72
73/* structure for the boardtype */
74typedef struct {
75	const char *pc_DriverName;	// driver name
76	INT i_VendorId;		//PCI vendor a device ID of card
77	INT i_DeviceId;
78	INT i_IorangeBase0;
79	INT i_IorangeBase1;
80	INT i_IorangeBase2;	//  base 2 range
81	INT i_IorangeBase3;	//  base 3 range
82	INT i_PCIEeprom;	// eeprom present or not
83	char *pc_EepromChip;	// type of chip
84	INT i_NbrAiChannel;	// num of A/D chans
85	INT i_NbrAiChannelDiff;	// num of A/D chans in diff mode
86	INT i_AiChannelList;	// len of chanlist
87	INT i_NbrAoChannel;	// num of D/A chans
88	INT i_AiMaxdata;	// resolution of A/D
89	INT i_AoMaxdata;	// resolution of D/A
90	PCRANGE pr_AiRangelist;	// rangelist for A/D
91	PCRANGE pr_AoRangelist;	// rangelist for D/A
92
93	INT i_NbrDiChannel;	// Number of DI channels
94	INT i_NbrDoChannel;	// Number of DO channels
95	INT i_DoMaxdata;	// data to set all chanels high
96
97	INT i_NbrTTLChannel;	// Number of TTL channels
98	PCRANGE pr_TTLRangelist;	// rangelist for TTL
99
100	INT i_Dma;		// dma present or not
101	INT i_Timer;		//   timer subdevice present or not
102	unsigned char b_AvailableConvertUnit;
103	UINT ui_MinAcquisitiontimeNs;	// Minimum Acquisition in Nano secs
104	UINT ui_MinDelaytimeNs;	// Minimum Delay in Nano secs
105
106	/* interrupt and reset */
107	void (*v_hwdrv_Interrupt)(int irq, void *d);
108	int (*i_hwdrv_Reset)(struct comedi_device *dev);
109
110	/* Subdevice functions */
111
112	/* ANALOG INPUT */
113	int (*i_hwdrv_InsnConfigAnalogInput)(struct comedi_device *dev,
114					     struct comedi_subdevice *s,
115					     struct comedi_insn *insn,
116					     unsigned int *data);
117	int (*i_hwdrv_InsnReadAnalogInput)(struct comedi_device *dev,
118					    struct comedi_subdevice *s,
119					    struct comedi_insn *insn,
120					    unsigned int *data);
121	int (*i_hwdrv_InsnWriteAnalogInput)(struct comedi_device *dev,
122					    struct comedi_subdevice *s,
123					    struct comedi_insn *insn,
124					    unsigned int *data);
125	int (*i_hwdrv_InsnBitsAnalogInput)(struct comedi_device *dev,
126					   struct comedi_subdevice *s,
127					   struct comedi_insn *insn,
128					   unsigned int *data);
129	int (*i_hwdrv_CommandTestAnalogInput)(struct comedi_device *dev,
130					      struct comedi_subdevice *s,
131					      struct comedi_cmd *cmd);
132	int (*i_hwdrv_CommandAnalogInput)(struct comedi_device *dev,
133					  struct comedi_subdevice *s);
134	int (*i_hwdrv_CancelAnalogInput)(struct comedi_device *dev,
135					 struct comedi_subdevice *s);
136
137	/* Analog Output */
138	int (*i_hwdrv_InsnConfigAnalogOutput)(struct comedi_device *dev,
139					      struct comedi_subdevice *s,
140					      struct comedi_insn *insn,
141					      unsigned int *data);
142	int (*i_hwdrv_InsnWriteAnalogOutput)(struct comedi_device *dev,
143					     struct comedi_subdevice *s,
144					     struct comedi_insn *insn,
145					     unsigned int *data);
146	int (*i_hwdrv_InsnBitsAnalogOutput)(struct comedi_device *dev,
147					    struct comedi_subdevice *s,
148					    struct comedi_insn *insn,
149					    unsigned int *data);
150
151	/* Digital Input */
152	int (*i_hwdrv_InsnConfigDigitalInput) (struct comedi_device *dev,
153					       struct comedi_subdevice *s,
154					       struct comedi_insn *insn,
155					       unsigned int *data);
156	int (*i_hwdrv_InsnReadDigitalInput) (struct comedi_device *dev,
157					     struct comedi_subdevice *s,
158					     struct comedi_insn *insn,
159					     unsigned int *data);
160	int (*i_hwdrv_InsnWriteDigitalInput) (struct comedi_device *dev,
161					      struct comedi_subdevice *s,
162					      struct comedi_insn *insn,
163					      unsigned int *data);
164	int (*i_hwdrv_InsnBitsDigitalInput) (struct comedi_device *dev,
165					     struct comedi_subdevice *s,
166					     struct comedi_insn *insn,
167					     unsigned int *data);
168
169	/* Digital Output */
170	int (*i_hwdrv_InsnConfigDigitalOutput)(struct comedi_device *dev,
171					       struct comedi_subdevice *s,
172					       struct comedi_insn *insn,
173					       unsigned int *data);
174	int (*i_hwdrv_InsnWriteDigitalOutput)(struct comedi_device *dev,
175					      struct comedi_subdevice *s,
176					      struct comedi_insn *insn,
177					      unsigned int *data);
178	int (*i_hwdrv_InsnBitsDigitalOutput)(struct comedi_device *dev,
179					     struct comedi_subdevice *s,
180					     struct comedi_insn *insn,
181					     unsigned int *data);
182	int (*i_hwdrv_InsnReadDigitalOutput)(struct comedi_device *dev,
183					     struct comedi_subdevice *s,
184					     struct comedi_insn *insn,
185					     unsigned int *data);
186
187	/* TIMER */
188	int (*i_hwdrv_InsnConfigTimer)(struct comedi_device *dev,
189				       struct comedi_subdevice *s,
190				       struct comedi_insn *insn, unsigned int *data);
191	int (*i_hwdrv_InsnWriteTimer)(struct comedi_device *dev,
192				      struct comedi_subdevice *s, struct comedi_insn *insn,
193				      unsigned int *data);
194	int (*i_hwdrv_InsnReadTimer)(struct comedi_device *dev, struct comedi_subdevice *s,
195				     struct comedi_insn *insn, unsigned int *data);
196	int (*i_hwdrv_InsnBitsTimer)(struct comedi_device *dev, struct comedi_subdevice *s,
197				     struct comedi_insn *insn, unsigned int *data);
198
199	/* TTL IO */
200	int (*i_hwdr_ConfigInitTTLIO)(struct comedi_device *dev,
201				      struct comedi_subdevice *s, struct comedi_insn *insn,
202				      unsigned int *data);
203	int (*i_hwdr_ReadTTLIOBits)(struct comedi_device *dev, struct comedi_subdevice *s,
204				    struct comedi_insn *insn, unsigned int *data);
205	int (*i_hwdr_ReadTTLIOAllPortValue)(struct comedi_device *dev,
206					    struct comedi_subdevice *s,
207					    struct comedi_insn *insn,
208					    unsigned int *data);
209	int (*i_hwdr_WriteTTLIOChlOnOff)(struct comedi_device *dev,
210					 struct comedi_subdevice *s,
211					 struct comedi_insn *insn, unsigned int *data);
212} boardtype;
213
214//MODULE INFO STRUCTURE
215
216typedef union {
217	/* Incremental counter infos */
218	struct {
219		union {
220			struct {
221				unsigned char b_ModeRegister1;
222				unsigned char b_ModeRegister2;
223				unsigned char b_ModeRegister3;
224				unsigned char b_ModeRegister4;
225			} s_ByteModeRegister;
226			DWORD dw_ModeRegister1_2_3_4;
227		} s_ModeRegister;
228
229		struct {
230			unsigned int b_IndexInit:1;
231			unsigned int b_CounterInit:1;
232			unsigned int b_ReferenceInit:1;
233			unsigned int b_IndexInterruptOccur:1;
234			unsigned int b_CompareLogicInit:1;
235			unsigned int b_FrequencyMeasurementInit:1;
236			unsigned int b_FrequencyMeasurementEnable:1;
237		} s_InitFlag;
238
239	} s_SiemensCounterInfo;
240
241	/* SSI infos */
242	struct {
243		unsigned char b_SSIProfile;
244		unsigned char b_PositionTurnLength;
245		unsigned char b_TurnCptLength;
246		unsigned char b_SSIInit;
247	} s_SSICounterInfo;
248
249	/* TTL I/O infos */
250	struct {
251		unsigned char b_TTLInit;
252		unsigned char b_PortConfiguration[4];
253	} s_TTLIOInfo;
254
255	/* Digital I/O infos */
256	struct {
257		unsigned char b_DigitalInit;
258		unsigned char b_ChannelAMode;
259		unsigned char b_ChannelBMode;
260		unsigned char b_OutputMemoryEnabled;
261		DWORD dw_OutputMemory;
262	} s_DigitalIOInfo;
263
264      /*********************/
265	/* 82X54 timer infos */
266      /*********************/
267
268	struct {
269		struct {
270			unsigned char b_82X54Init;
271			unsigned char b_InputClockSelection;
272			unsigned char b_InputClockLevel;
273			unsigned char b_OutputLevel;
274			unsigned char b_HardwareGateLevel;
275			DWORD dw_ConfigurationWord;
276		} s_82X54TimerInfo[3];
277		unsigned char b_InterruptMask;
278	} s_82X54ModuleInfo;
279
280      /*********************/
281	/* Chronometer infos */
282      /*********************/
283
284	struct {
285		unsigned char b_ChronoInit;
286		unsigned char b_InterruptMask;
287		unsigned char b_PCIInputClock;
288		unsigned char b_TimingUnit;
289		unsigned char b_CycleMode;
290		double d_TimingInterval;
291		DWORD dw_ConfigReg;
292	} s_ChronoModuleInfo;
293
294      /***********************/
295	/* Pulse encoder infos */
296      /***********************/
297
298	struct {
299		struct {
300			unsigned char b_PulseEncoderInit;
301		} s_PulseEncoderInfo[4];
302		DWORD dw_SetRegister;
303		DWORD dw_ControlRegister;
304		DWORD dw_StatusRegister;
305	} s_PulseEncoderModuleInfo;
306
307	/* Tor conter infos */
308	struct {
309		struct {
310			unsigned char b_TorCounterInit;
311			unsigned char b_TimingUnit;
312			unsigned char b_InterruptEnable;
313			double d_TimingInterval;
314			ULONG ul_RealTimingInterval;
315		} s_TorCounterInfo[2];
316		unsigned char b_PCIInputClock;
317	} s_TorCounterModuleInfo;
318
319	/* PWM infos */
320	struct {
321		struct {
322			unsigned char b_PWMInit;
323			unsigned char b_TimingUnit;
324			unsigned char b_InterruptEnable;
325			double d_LowTiming;
326			double d_HighTiming;
327			ULONG ul_RealLowTiming;
328			ULONG ul_RealHighTiming;
329		} s_PWMInfo[2];
330		unsigned char b_ClockSelection;
331	} s_PWMModuleInfo;
332
333	/* ETM infos */
334	struct {
335		struct {
336			unsigned char b_ETMEnable;
337			unsigned char b_ETMInterrupt;
338		} s_ETMInfo[2];
339		unsigned char b_ETMInit;
340		unsigned char b_TimingUnit;
341		unsigned char b_ClockSelection;
342		double d_TimingInterval;
343		ULONG ul_Timing;
344	} s_ETMModuleInfo;
345
346	/* CDA infos */
347	struct {
348		unsigned char b_CDAEnable;
349		unsigned char b_CDAInterrupt;
350		unsigned char b_CDAInit;
351		unsigned char b_FctSelection;
352		unsigned char b_CDAReadFIFOOverflow;
353	} s_CDAModuleInfo;
354
355} str_ModuleInfo;
356
357/* Private structure for the addi_apci3120 driver */
358typedef struct {
359
360	INT iobase;
361	INT i_IobaseAmcc;	// base+size for AMCC chip
362	INT i_IobaseAddon;	//addon base address
363	INT i_IobaseReserved;
364	ULONG_PTR dw_AiBase;
365	struct pcilst_struct *amcc;	// ptr too AMCC data
366	unsigned char allocated;		// we have blocked card
367	unsigned char b_ValidDriver;	// driver is ok
368	unsigned char b_AiContinuous;	// we do unlimited AI
369	unsigned char b_AiInitialisation;
370	UINT ui_AiActualScan;	//how many scans we finished
371	UINT ui_AiBufferPtr;	// data buffer ptr in samples
372	UINT ui_AiNbrofChannels;	// how many channels is measured
373	UINT ui_AiScanLength;	// Length of actual scanlist
374	UINT ui_AiActualScanPosition;	// position in actual scan
375	PUINT pui_AiChannelList;	// actual chanlist
376	UINT ui_AiChannelList[32];	// actual chanlist
377	unsigned char b_AiChannelConfiguration[32];	// actual chanlist
378	UINT ui_AiReadData[32];
379	DWORD dw_AiInitialised;
380	UINT ui_AiTimer0;	//Timer Constant for Timer0
381	UINT ui_AiTimer1;	//Timer constant for Timer1
382	UINT ui_AiFlags;
383	UINT ui_AiDataLength;
384	short *AiData;	// Pointer to sample data
385	UINT ui_AiNbrofScans;	// number of scans to do
386	unsigned short us_UseDma;	// To use Dma or not
387	unsigned char b_DmaDoubleBuffer;	// we can use double buffering
388	UINT ui_DmaActualBuffer;	// which buffer is used now
389	//*UPDATE-0.7.57->0.7.68
390	//ULONG               ul_DmaBufferVirtual[2];// pointers to begin of DMA buffer
391	short *ul_DmaBufferVirtual[2];	// pointers to begin of DMA buffer
392	ULONG ul_DmaBufferHw[2];	// hw address of DMA buff
393	UINT ui_DmaBufferSize[2];	// size of dma buffer in bytes
394	UINT ui_DmaBufferUsesize[2];	// which size we may now used for transfer
395	UINT ui_DmaBufferSamples[2];	// size in samples
396	UINT ui_DmaBufferPages[2];	// number of pages in buffer
397	unsigned char b_DigitalOutputRegister;	// Digital Output Register
398	unsigned char b_OutputMemoryStatus;
399	unsigned char b_AnalogInputChannelNbr;	// Analog input channel Nbr
400	unsigned char b_AnalogOutputChannelNbr;	// Analog input Output  Nbr
401	unsigned char b_TimerSelectMode;	// Contain data written at iobase + 0C
402	unsigned char b_ModeSelectRegister;	// Contain data written at iobase + 0E
403	unsigned short us_OutputRegister;	// Contain data written at iobase + 0
404	unsigned char b_InterruptState;
405	unsigned char b_TimerInit;	// Specify if InitTimerWatchdog was load
406	unsigned char b_TimerStarted;	// Specify if timer 2 is running or not
407	unsigned char b_Timer2Mode;	// Specify the timer 2 mode
408	unsigned char b_Timer2Interrupt;	//Timer2  interrupt enable or disable
409	unsigned char b_AiCyclicAcquisition;	// indicate cyclic acquisition
410	unsigned char b_InterruptMode;	// eoc eos or dma
411	unsigned char b_EocEosInterrupt;	// Enable disable eoc eos interrupt
412	UINT ui_EocEosConversionTime;
413	unsigned char b_EocEosConversionTimeBase;
414	unsigned char b_SingelDiff;
415	unsigned char b_ExttrigEnable;	/* To enable or disable external trigger */
416
417	/* Pointer to the current process */
418	struct task_struct *tsk_Current;
419	boardtype *ps_BoardInfo;
420
421	/* Hardware board infos for 1710 */
422	struct {
423		UINT ui_Address;	/* Board address */
424		UINT ui_FlashAddress;
425		unsigned char b_InterruptNbr;	/* Board interrupt number */
426		unsigned char b_SlotNumber;	/* PCI slot number */
427		unsigned char b_BoardVersion;
428		DWORD dw_MolduleConfiguration[4];	/* Module config */
429	} s_BoardInfos;
430
431	/* Interrupt infos */
432	struct {
433		ULONG ul_InterruptOccur;	/* 0   : No interrupt occur */
434						/* > 0 : Interrupt occur */
435		UINT ui_Read;	/* Read FIFO */
436		UINT ui_Write;	/* Write FIFO */
437		struct {
438			unsigned char b_OldModuleMask;
439			ULONG ul_OldInterruptMask;	/* Interrupt mask */
440			ULONG ul_OldCounterLatchValue;	/* Interrupt counter value */
441		} s_FIFOInterruptParameters[APCI1710_SAVE_INTERRUPT];
442	} s_InterruptParameters;
443
444	str_ModuleInfo s_ModuleInfo[4];
445	ULONG ul_TTLPortConfiguration[10];
446
447} addi_private;
448
449static unsigned short pci_list_builded;	/* set to 1 when list of card is known */
450
451/* Function declarations */
452static int i_ADDI_Attach(struct comedi_device *dev, struct comedi_devconfig *it);
453static int i_ADDI_Detach(struct comedi_device *dev);
454static int i_ADDI_Reset(struct comedi_device *dev);
455
456static irqreturn_t v_ADDI_Interrupt(int irq, void *d PT_REGS_ARG);
457static int i_ADDIDATA_InsnReadEeprom(struct comedi_device *dev, struct comedi_subdevice *s,
458				     struct comedi_insn *insn, unsigned int *data);
459