moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / indi / apogee / CameraIO_PCI.cpp
blob3e6cc43e8c2a88aa4253ea96986bbd1662f80611
1 // CameraIO_PCI.cpp: implementation of the CCameraIO_PCI class.
2 //
3 // Copyright (c) 2000 Apogee Instruments Inc.
4 //////////////////////////////////////////////////////////////////////
6 #include "stdafx.h"
7 #include <winioctl.h>
9 #include "ApogeeLinux.h" // This defines the IOCTL constants.
10 #include "CameraIO_PCI.h"
11 #include "time.h"
14 //////////////////////////////////////////////////////////////////////
15 // Construction/Destruction
16 //////////////////////////////////////////////////////////////////////
18 CCameraIO_PCI::CCameraIO_PCI()
20 m_IsWDM = false;
21 m_hDriver = NULL;
24 CCameraIO_PCI::~CCameraIO_PCI()
26 CloseDriver();
29 long CCameraIO_PCI::Read(unsigned short reg, unsigned short& val)
31 BOOLEAN IoctlResult;
32 ULONG ReturnedLength;
33 USHORT RegNumber;
34 USHORT ReadBuffer;
36 switch ( reg )
38 case Reg_Command:
39 RegNumber = RegPCI_CommandRead;
40 break;
41 case Reg_Timer:
42 RegNumber = RegPCI_TimerRead;
43 break;
44 case Reg_VBinning:
45 RegNumber = RegPCI_VBinningRead;
46 break;
47 case Reg_AICCounter:
48 RegNumber = RegPCI_AICCounterRead;
49 break;
50 case Reg_TempSetPoint:
51 RegNumber = RegPCI_TempSetPointRead;
52 break;
53 case Reg_PixelCounter:
54 RegNumber = RegPCI_PixelCounterRead;
55 break;
56 case Reg_LineCounter:
57 RegNumber = RegPCI_LineCounterRead;
58 break;
59 case Reg_BICCounter:
60 RegNumber = RegPCI_BICCounterRead;
61 break;
62 case Reg_ImageData:
63 RegNumber = RegPCI_ImageData;
64 break;
65 case Reg_TempData:
66 RegNumber = RegPCI_TempData;
67 break;
68 case Reg_Status:
69 RegNumber = RegPCI_Status;
70 break;
71 case Reg_CommandReadback:
72 RegNumber = RegPCI_CommandReadback;
73 break;
74 default:
75 _ASSERT( FALSE ); // Application program bug
76 val = 0;
77 return 0;
80 if ( m_IsWDM )
82 IoctlResult = DeviceIoControl(
83 m_hDriver, // Handle to device
84 IOCTL_WDM_READ_PCI_USHORT, // IO Control code for Read
85 &RegNumber, // Buffer to driver.
86 sizeof(RegNumber), // Length of buffer in bytes.
87 &ReadBuffer, // Buffer from driver.
88 sizeof(ReadBuffer), // Length of buffer in bytes.
89 &ReturnedLength, // Bytes placed in DataBuffer.
90 NULL // NULL means wait till op. completes.
93 else
95 IoctlResult = DeviceIoControl(
96 m_hDriver, // Handle to device
97 IOCTL_GPD_READ_PCI_USHORT, // IO Control code for Read
98 &RegNumber, // Buffer to driver.
99 sizeof(RegNumber), // Length of buffer in bytes.
100 &ReadBuffer, // Buffer from driver.
101 sizeof(ReadBuffer), // Length of buffer in bytes.
102 &ReturnedLength, // Bytes placed in DataBuffer.
103 NULL // NULL means wait till op. completes.
107 if ( (ReturnedLength != 2) || (IoctlResult == FALSE) )
109 return 1;
112 val = ReadBuffer;
114 return 0;
117 long CCameraIO_PCI::Write(unsigned short reg, unsigned short val)
119 BOOLEAN IoctlResult;
120 ULONG InBuffer[2];
121 ULONG ReturnedLength;
122 USHORT RegNumber;
124 switch ( reg )
126 case Reg_Command:
127 RegNumber = RegPCI_Command;
128 break;
129 case Reg_Timer:
130 RegNumber = RegPCI_Timer;
131 break;
132 case Reg_VBinning:
133 RegNumber = RegPCI_VBinning;
134 break;
135 case Reg_AICCounter:
136 RegNumber = RegPCI_AICCounter;
137 break;
138 case Reg_TempSetPoint:
139 RegNumber = RegPCI_TempSetPoint;
140 break;
141 case Reg_PixelCounter:
142 RegNumber = RegPCI_PixelCounter;
143 break;
144 case Reg_LineCounter:
145 RegNumber = RegPCI_LineCounter;
146 break;
147 case Reg_BICCounter:
148 RegNumber = RegPCI_BICCounter;
149 break;
150 default:
151 _ASSERT ( false );
152 return 0;
155 InBuffer[0] = RegNumber;
156 InBuffer[1] = val;
158 // Do an I/O write
159 if ( m_IsWDM )
161 IoctlResult = DeviceIoControl(
162 m_hDriver, // Handle to device
163 IOCTL_WDM_WRITE_PCI_USHORT, // IO Control code for Write
164 &InBuffer, // Buffer to driver. Holds register/data.
165 sizeof ( InBuffer ), // Length of buffer in bytes.
166 NULL, // Buffer from driver. Not used.
167 0, // Length of buffer in bytes.
168 &ReturnedLength, // Bytes placed in outbuf. Should be 0.
169 NULL // NULL means wait till I/O completes.
172 else
174 IoctlResult = DeviceIoControl(
175 m_hDriver, // Handle to device
176 IOCTL_GPD_WRITE_PCI_USHORT, // IO Control code for Write
177 &InBuffer, // Buffer to driver. Holds register/data.
178 sizeof ( InBuffer ), // Length of buffer in bytes.
179 NULL, // Buffer from driver. Not used.
180 0, // Length of buffer in bytes.
181 &ReturnedLength, // Bytes placed in outbuf. Should be 0.
182 NULL // NULL means wait till I/O completes.
186 if ( (IoctlResult == FALSE) || (ReturnedLength != 0) )
188 return 1;
191 return 0;
195 long CCameraIO_PCI::ReadLine( long SkipPixels, long Pixels, unsigned short* pLineBuffer )
197 BOOLEAN IoctlResult;
198 ULONG InBuffer[3];
199 ULONG ReturnedLength; // Number of bytes returned in output buffer
200 ULONG NumBytes;
201 USHORT* DataBuffer;
203 InBuffer[0] = RegPCI_ImageData;
204 InBuffer[1] = SkipPixels; // Data points to skip
205 InBuffer[2] = Pixels; // Data points to keep
207 NumBytes = Pixels * sizeof( unsigned short );
208 DataBuffer = pLineBuffer;
210 if ( !m_TDI )
212 /////////////////////////////////////
213 // Clock out the line
214 m_RegShadow[ Reg_Command ] |= RegBit_StartNextLine; // set bit to 1
215 Write( Reg_Command, m_RegShadow[ Reg_Command ] );
217 m_RegShadow[ Reg_Command ] &= ~RegBit_StartNextLine; // set bit to 0
218 Write( Reg_Command, m_RegShadow[ Reg_Command ] );
219 /////////////////////////////////////
222 if ( m_IsWDM )
224 IoctlResult = DeviceIoControl(
225 m_hDriver, // Handle to device
226 IOCTL_WDM_READ_PCI_LINE, // IO Control code for Read line
227 &InBuffer, // Buffer to driver.
228 sizeof(InBuffer), // Length of buffer in bytes.
229 DataBuffer, // Buffer from driver.
230 NumBytes, // Length of buffer in bytes.
231 &ReturnedLength, // Bytes placed in DataBuffer.
232 NULL // NULL means wait till op. completes.
235 else
237 IoctlResult = DeviceIoControl(
238 m_hDriver, // Handle to device
239 IOCTL_GPD_READ_PCI_LINE, // IO Control code for Read line
240 &InBuffer, // Buffer to driver.
241 sizeof(InBuffer), // Length of buffer in bytes.
242 DataBuffer, // Buffer from driver.
243 NumBytes, // Length of buffer in bytes.
244 &ReturnedLength, // Bytes placed in DataBuffer.
245 NULL // NULL means wait till op. completes.
249 if ( (ReturnedLength != NumBytes) || (!IoctlResult) )
251 return 1; // Failed to get line info
254 /////////////////////////////////////
255 // Assert done reading line
256 m_RegShadow[ Reg_Command ] |= RegBit_DoneReading; // set bit to 1
257 Write( Reg_Command, m_RegShadow[ Reg_Command ] );
259 m_RegShadow[ Reg_Command ] &= ~RegBit_DoneReading; // set bit to 0
260 Write( Reg_Command, m_RegShadow[ Reg_Command ] );
261 /////////////////////////////////////
263 if ( !m_TDI )
265 /////////////////////////////////////
266 // Wait until camera is done
267 clock_t StopTime = clock() + CLOCKS_PER_SEC; // wait at most one second
268 while ( true )
270 unsigned short val = 0;
271 Read( Reg_Status, val );
272 if ( ( val & RegBit_LineDone ) != 0 ) break;// Line done
274 if ( clock() > StopTime ) return 1; // Timed out
278 return 0;
281 bool CCameraIO_PCI::InitDriver()
283 OSVERSIONINFO OSVerInfo;
284 BOOLEAN IsPostWin98OS;
285 BOOLEAN IsNT4OS;
286 BOOLEAN IsPostNT4OS;
288 IsPostWin98OS = false;
289 IsNT4OS = false;
290 IsPostNT4OS = false;
292 CloseDriver();
294 OSVerInfo.dwOSVersionInfoSize = sizeof ( OSVERSIONINFO );
295 GetVersionEx( &OSVerInfo );
297 // Check for Win9x versions. Pre-Win98 is unsupported.
298 if ( OSVerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
300 // Check for pre-Win98
301 if (( OSVerInfo.dwMajorVersion < 4 ) ||
302 (( OSVerInfo.dwMajorVersion == 4 ) && ( OSVerInfo.dwMinorVersion == 0 )))
304 return false; // Pre-Win98 not supported
306 else
308 IsPostWin98OS = true;
312 else if ( OSVerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT )
314 // Check if NT4
315 if ( OSVerInfo.dwMajorVersion < 4 )
317 // NT3.51 is not supported. Right??
318 return false;
320 else if (OSVerInfo.dwMajorVersion == 4 )
322 IsNT4OS = true;
324 else if (OSVerInfo.dwMajorVersion > 4 )
326 IsPostNT4OS = true;
330 if ( IsNT4OS )
332 ULONG ReturnedLength;
333 ULONG DataBuffer[2];
335 // Open the driver
336 m_hDriver = CreateFile(
337 "\\\\.\\ApogeeIO",
338 GENERIC_WRITE | GENERIC_READ,
339 FILE_SHARE_WRITE | FILE_SHARE_READ,
340 NULL,
341 OPEN_EXISTING,
343 NULL);
345 if ( m_hDriver == INVALID_HANDLE_VALUE )
347 m_hDriver = NULL;
348 return false;
351 BOOL IoctlResult = DeviceIoControl(
352 m_hDriver, // Handle to device
353 IOCTL_PCI_BUS_SCAN, // IO Control code for PCI Bus Scan
354 NULL, // Buffer to driver.
355 0, // Length of buffer in bytes.
356 DataBuffer, // Buffer from driver.
357 sizeof( DataBuffer ), // Length of buffer in bytes.
358 &ReturnedLength, // Bytes placed in DataBuffer.
359 NULL // NULL means wait till op. completes.
362 if ( (!IoctlResult) || (ReturnedLength != sizeof(DataBuffer)) )
364 return false;
367 else if ( IsPostWin98OS || IsPostNT4OS )
369 // Should be okay to use the WDM driver. Note that the kernel
370 // driver will actually check to see if WDM services are available
372 // Open the driver
373 m_hDriver = CreateFile(
374 "\\\\.\\ApPCI",
375 GENERIC_WRITE | GENERIC_READ,
376 FILE_SHARE_WRITE | FILE_SHARE_READ,
377 NULL,
378 OPEN_EXISTING,
380 NULL
383 if ( m_hDriver == INVALID_HANDLE_VALUE )
385 m_hDriver = NULL;
386 return false;
389 // Safe to assume we're using the WDM driver at this point.
390 m_IsWDM = true;
393 return true;
396 void CCameraIO_PCI::CloseDriver()
398 // Close the driver if it already exists
399 if ( m_hDriver != NULL )
401 CloseHandle ( m_hDriver );
404 m_hDriver = NULL;