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