forked from k5mwe/DtCyber
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pci_channel_win32.c
762 lines (673 loc) · 22 KB
/
pci_channel_win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003-2011, Tom Hunter
**
** Name: pci_channel.c
**
** Description:
** Interface to PCI channel adapter.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License version 3 as
** published by the Free Software Foundation.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License version 3 for more details.
**
** You should have received a copy of the GNU General Public License
** version 3 along with this program in file "license-gpl-3.0.txt".
** If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
**
**--------------------------------------------------------------------------
*/
#define DEBUG 0
/*
** -------------
** Include Files
** -------------
*/
#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <winioctl.h>
#include "const.h"
#include "types.h"
#include "proto.h"
#include "cyber_channel_win32.h"
/*
** -----------------
** Private Constants
** -----------------
*/
#define PciCmdNop 0x0000
#define PciCmdFunction 0x2000
#define PciCmdFull 0x4000
#define PciCmdEmpty 0x6000
#define PciCmdActive 0x8000
#define PciCmdInactive 0xA000
#define PciCmdClear 0xC000
#define PciCmdMasterClear 0xE000
#define PciStaFull 0x2000
#define PciStaActive 0x4000
#define PciStaBusy 0x8000
#define PciMaskData 0x0FFF
#define PciMaskParity 0x1000
#define PciShiftParity 12
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
typedef struct pciParam
{
PpWord data;
} PciParam;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static FcStatus pciFunc(PpWord funcCode);
static void pciIo(void);
static PpWord pciIn(void);
static void pciOut(PpWord data);
static void pciFull(void);
static void pciEmpty(void);
static void pciActivate(void);
static void pciDisconnect(void);
static u16 pciFlags(void);
static void pciCmd(PpWord data);
static u16 pciStatus(void);
static u16 pciParity(PpWord val);
static BOOL GetDevicePath();
static BOOL GetDeviceHandle();
/*
** ----------------
** Public Variables
** ----------------
*/
/*
** -----------------
** Private Variables
** -----------------
*/
static PciParam *pci;
static PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetail = NULL;
static HANDLE hDevice = INVALID_HANDLE_VALUE;
static HDEVINFO hDevInfo;
#if DEBUG
static FILE *pciLog = NULL;
static bool active = FALSE;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise PCI channel interface.
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo unit number
** channelNo channel number the device is attached to
** deviceName optional device file name
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void pciInit(u8 eqNo, u8 unitNo, u8 channelNo, char *deviceName)
{
DevSlot *dp;
BOOL retValue;
(void)unitNo;
(void)deviceName;
#if DEBUG
if (pciLog == NULL)
{
pciLog = fopen("pcilog.txt", "wt");
}
#endif
/*
** Attach device to channel and initialise device control block.
*/
dp = channelAttach(channelNo, eqNo, DtPciChannel);
dp->activate = pciActivate;
dp->disconnect = pciDisconnect;
dp->func = pciFunc;
dp->io = pciIo;
dp->flags = pciFlags;
dp->in = pciIn;
dp->out = pciOut;
dp->full = pciFull;
dp->empty = pciEmpty;
/*
** Allocate and initialise channel parameters.
*/
pci = calloc(1, sizeof(PciParam));
if (pci == NULL)
{
fprintf(stderr, "Failed to allocate PCI channel context block\n");
exit(1);
}
retValue = GetDeviceHandle();
if (!retValue)
{
fprintf(stderr, "Can't open CYBER channel interface.\n");
exit(1);
}
pciCmd(PciCmdMasterClear);
/*
** Print a friendly message.
*/
printf("PCI channel interface initialised on channel %o unit %o\n", channelNo, unitNo);
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Execute function code on channel.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus pciFunc(PpWord funcCode)
{
#if DEBUG
fprintf(pciLog, "\n%06d PP:%02o CH:%02o f:%04o > ",
traceSequenceNo,
activePpu->id,
activeChannel->id,
funcCode);
#endif
pciCmd((u16)(PciCmdFunction | funcCode | (pciParity(funcCode) << PciShiftParity)));
return(FcAccepted);
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O on channel (not used).
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciIo(void)
{
}
/*--------------------------------------------------------------------------
** Purpose: Perform input from PCI channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static PpWord pciIn(void)
{
PpWord data = pciStatus() & Mask12;
#if DEBUG
fprintf(pciLog, " I(%03X)", data);
#endif
return(data);
}
/*--------------------------------------------------------------------------
** Purpose: Save output for PCI channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciOut(PpWord data)
{
pci->data = data;
}
/*--------------------------------------------------------------------------
** Purpose: Set the channel full with data previously saved
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciFull(void)
{
#if DEBUG
fprintf(pciLog, " O(%03X)", pci->data);
#endif
pciCmd((u16)(PciCmdFull | pci->data | (pciParity(pci->data) << PciShiftParity)));
}
/*--------------------------------------------------------------------------
** Purpose: Set the channel empty
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciEmpty(void)
{
#if DEBUG
fprintf(pciLog, " E");
#endif
pciCmd(PciCmdEmpty);
}
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciActivate(void)
{
#if DEBUG
fprintf(pciLog, " A");
active = TRUE;
#endif
pciCmd(PciCmdActive);
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciDisconnect(void)
{
#if DEBUG
fprintf(pciLog, " D");
active = FALSE;
#endif
pciCmd(PciCmdInactive);
}
/*--------------------------------------------------------------------------
** Purpose: Update full/active channel flags.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static u16 pciFlags(void)
{
#if DEBUG
u16 s = pciStatus();
if (active)
{
// fprintf(pciLog, " S(%04X)", s);
}
return(s);
#else
return(pciStatus());
#endif
}
/*--------------------------------------------------------------------------
** Purpose: Send a PCI command
**
** Parameters: Name Description.
** data command data
**
** Returns: nothing
**
**------------------------------------------------------------------------*/
static void pciCmd(PpWord data)
{
DWORD bytesReturned;
u16 status = 0;
do
{
DeviceIoControl(hDevice, IOCTL_CYBER_CHANNEL_GET, NULL, 0, &status, sizeof(status), &bytesReturned, NULL);
} while ((status & PciStaBusy) != 0);
DeviceIoControl(hDevice, IOCTL_CYBER_CHANNEL_PUT, &data, sizeof(data), NULL, 0, &bytesReturned, NULL);
}
/*--------------------------------------------------------------------------
** Purpose: Get PCI status.
**
** Parameters: Name Description.
**
** Returns: PCI status word
**
**------------------------------------------------------------------------*/
static u16 pciStatus(void)
{
DWORD bytesReturned;
u16 data;
DeviceIoControl(hDevice, IOCTL_CYBER_CHANNEL_GET, NULL, 0, &data, sizeof(data), &bytesReturned, NULL);
return(data);
}
/*--------------------------------------------------------------------------
** Purpose: Calculate odd parity over 12 bit PP word.
**
** Parameters: Name Description.
** data 12 bit PP word
**
** Returns: Parity
**
**------------------------------------------------------------------------*/
static u16 pciParity(PpWord data)
{
u16 ret = 1;
while (data != 0)
{
ret ^= data & 1;
data >>= 1;
}
return(ret);
}
/*--------------------------------------------------------------------------
** Purpose: Determine the Windows path for the CYBER channel PCI
** board driver.
**
** Parameters: Name Description.
**
** Returns: TRUE if successful, FALSE otherwise.
**
**------------------------------------------------------------------------*/
static BOOL GetDevicePath()
{
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
SP_DEVINFO_DATA DeviceInfoData;
ULONG size;
int count, i, index;
BOOL status = TRUE;
TCHAR *DeviceName = NULL;
TCHAR *DeviceLocation = NULL;
//
// Retrieve the device information for all devices.
//
hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVINTERFACE_CYBER_CHANNEL,
NULL,
NULL,
DIGCF_DEVICEINTERFACE |
DIGCF_PRESENT);
//
// Initialize the SP_DEVICE_INTERFACE_DATA Structure.
//
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
//
// Determine how many devices are present.
//
count = 0;
while (SetupDiEnumDeviceInterfaces(hDevInfo,
NULL,
(LPGUID)&GUID_DEVINTERFACE_CYBER_CHANNEL,
count++, //Cycle through the available devices.
&DeviceInterfaceData)
);
//
// Since the last call fails when all devices have been enumerated,
// decrement the count to get the true device count.
//
count--;
//
// If the count is zero then there are no devices present.
//
if (count == 0)
{
printf("No PLX devices are present and enabled in the system.\n");
return FALSE;
}
//
// Initialize the appropriate data structures in preparation for
// the SetupDi calls.
//
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
//
// Loop through the device list to allow user to choose
// a device. If there is only one device, select it
// by default.
//
i = 0;
while (SetupDiEnumDeviceInterfaces(hDevInfo,
NULL,
(LPGUID)&GUID_DEVINTERFACE_CYBER_CHANNEL,
i,
&DeviceInterfaceData))
{
//
// Determine the size required for the DeviceInterfaceData
//
SetupDiGetDeviceInterfaceDetail(hDevInfo,
&DeviceInterfaceData,
NULL,
0,
&size,
NULL);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
printf("SetupDiGetDeviceInterfaceDetail failed, Error: %d", GetLastError());
return FALSE;
}
pDeviceInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(size);
if (!pDeviceInterfaceDetail)
{
printf("Insufficient memory.\n");
return FALSE;
}
//
// Initialize structure and retrieve data.
//
pDeviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
status = SetupDiGetDeviceInterfaceDetail(hDevInfo,
&DeviceInterfaceData,
pDeviceInterfaceDetail,
size,
NULL,
&DeviceInfoData);
free(pDeviceInterfaceDetail);
if (!status)
{
printf("SetupDiGetDeviceInterfaceDetail failed, Error: %d", GetLastError());
return status;
}
//
// Get the Device Name
// Calls to SetupDiGetDeviceRegistryProperty require two consecutive
// calls, first to get required buffer size and second to get
// the data.
//
SetupDiGetDeviceRegistryProperty(hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
NULL,
(PBYTE)DeviceName,
0,
&size);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
printf("SetupDiGetDeviceRegistryProperty failed, Error: %d", GetLastError());
return FALSE;
}
DeviceName = (TCHAR*) malloc(size);
if (!DeviceName)
{
printf("Insufficient memory.\n");
return FALSE;
}
status = SetupDiGetDeviceRegistryProperty(hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
NULL,
(PBYTE)DeviceName,
size,
NULL);
if (!status)
{
printf("SetupDiGetDeviceRegistryProperty failed, Error: %d", GetLastError());
free(DeviceName);
return status;
}
//
// Now retrieve the Device Location.
//
SetupDiGetDeviceRegistryProperty(hDevInfo,
&DeviceInfoData,
SPDRP_LOCATION_INFORMATION,
NULL,
(PBYTE)DeviceLocation,
0,
&size);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
DeviceLocation = (TCHAR*) malloc(size);
if (DeviceLocation != NULL)
{
status = SetupDiGetDeviceRegistryProperty(hDevInfo,
&DeviceInfoData,
SPDRP_LOCATION_INFORMATION,
NULL,
(PBYTE)DeviceLocation,
size,
NULL);
if (!status)
{
free(DeviceLocation);
DeviceLocation = NULL;
}
}
}
else
{
DeviceLocation = NULL;
}
//
// Print description.
//
printf("%d - ", i);
printf("%s\n", DeviceName);
if (DeviceLocation)
{
printf(" %s\n", DeviceLocation);
}
free(DeviceName);
DeviceName = NULL;
if (DeviceLocation)
{
free(DeviceLocation);
DeviceLocation = NULL;
}
i++; // Cycle through the available devices.
}
//
// Select device.
//
index = 0;
if (count > 1)
{
printf("Too many CYBER channel boards\n");
exit(1);
}
//
// Get information for specific device.
//
status = SetupDiEnumDeviceInterfaces(hDevInfo,
NULL,
(LPGUID)&GUID_DEVINTERFACE_CYBER_CHANNEL,
index,
&DeviceInterfaceData);
if (!status)
{
printf("SetupDiEnumDeviceInterfaces failed, Error: %d", GetLastError());
return status;
}
//
// Determine the size required for the DeviceInterfaceData
//
SetupDiGetDeviceInterfaceDetail(hDevInfo,
&DeviceInterfaceData,
NULL,
0,
&size,
NULL);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
printf("SetupDiGetDeviceInterfaceDetail failed, Error: %d", GetLastError());
return FALSE;
}
pDeviceInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(size);
if (!pDeviceInterfaceDetail)
{
printf("Insufficient memory.\n");
return FALSE;
}
//
// Initialize structure and retrieve data.
//
pDeviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
status = SetupDiGetDeviceInterfaceDetail(hDevInfo,
&DeviceInterfaceData,
pDeviceInterfaceDetail,
size,
NULL,
&DeviceInfoData);
if (!status)
{
printf("SetupDiGetDeviceInterfaceDetail failed, Error: %d", GetLastError());
return status;
}
return status;
}
/*--------------------------------------------------------------------------
** Purpose: Open the CYBER channel PCI board driver.
**
** Parameters: Name Description.
**
** Returns: PCI status word
**
**------------------------------------------------------------------------*/
static BOOL GetDeviceHandle()
{
BOOL status = TRUE;
if (pDeviceInterfaceDetail == NULL)
{
status = GetDevicePath();
}
if (pDeviceInterfaceDetail == NULL)
{
status = FALSE;
}
if (status)
{
//
// Get handle to device.
//
printf("\nDevice path = %s\n", pDeviceInterfaceDetail->DevicePath);
hDevice = CreateFile(pDeviceInterfaceDetail->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
status = FALSE;
printf("CreateFile failed. Error:%d", GetLastError());
}
}
return(status);
}
/*--------------------------- End Of File ------------------------------*/