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