forked from SDL-Hercules-390/hyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardpch.c
297 lines (245 loc) · 9.75 KB
/
cardpch.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
/* CARDPCH.C (c) Copyright Roger Bowler, 1999-2001 */
/* ESA/390 Card Punch Device Handler */
/*-------------------------------------------------------------------*/
/* This module contains device handling functions for emulated */
/* System/370 card punch devices. */
/*-------------------------------------------------------------------*/
#include "hercules.h"
/*-------------------------------------------------------------------*/
/* Internal macro definitions */
/*-------------------------------------------------------------------*/
#define CARD_LENGTH 80
#define SPACE ((BYTE)' ')
#define HEX40 ((BYTE)0x40)
/*-------------------------------------------------------------------*/
/* Subroutine to write data to the card punch */
/*-------------------------------------------------------------------*/
static void
write_buffer (DEVBLK *dev, BYTE *buf, int len, BYTE *unitstat)
{
int rc; /* Return code */
/* Write data to the output file */
rc = write (dev->fd, buf, len);
/* Equipment check if error writing to output file */
if (rc < len)
{
logmsg ("HHC423I Error writing to %s: %s\n",
dev->filename,
(errno == 0 ? "incomplete": strerror(errno)));
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return;
}
} /* end function write_buffer */
/*-------------------------------------------------------------------*/
/* Initialize the device handler */
/*-------------------------------------------------------------------*/
int cardpch_init_handler (DEVBLK *dev, int argc, BYTE *argv[])
{
int i; /* Array subscript */
/* The first argument is the file name */
if (argc == 0 || strlen(argv[0]) > sizeof(dev->filename)-1)
{
logmsg ("HHC421I File name missing or invalid\n");
return -1;
}
/* Save the file name in the device block */
strcpy (dev->filename, argv[0]);
/* Initialize device dependent fields */
dev->fd = -1;
dev->ascii = 0;
dev->crlf = 0;
dev->cardpos = 0;
dev->cardrem = CARD_LENGTH;
/* Process the driver arguments */
for (i = 1; i < argc; i++)
{
if (strcasecmp(argv[i], "ascii") == 0)
{
dev->ascii = 1;
continue;
}
if (strcasecmp(argv[i], "ebcdic") == 0)
{
dev->ascii = 0;
continue;
}
if (strcasecmp(argv[i], "crlf") == 0)
{
dev->crlf = 1;
continue;
}
logmsg ("HHC422I Invalid argument: %s\n",
argv[i]);
return -1;
}
/* Set length of buffer */
dev->bufsize = CARD_LENGTH + 2;
/* Set number of sense bytes */
dev->numsense = 1;
/* Initialize the device identifier bytes */
dev->devid[0] = 0xFF;
dev->devid[1] = 0x28; /* Control unit type is 2821-1 */
dev->devid[2] = 0x21;
dev->devid[3] = 0x01;
dev->devid[4] = dev->devtype >> 8;
dev->devid[5] = dev->devtype & 0xFF;
dev->devid[6] = 0x01;
dev->numdevid = 7;
/* Activate I/O tracing */
// dev->ccwtrace = 1;
return 0;
} /* end function cardpch_init_handler */
/*-------------------------------------------------------------------*/
/* Query the device definition */
/*-------------------------------------------------------------------*/
void cardpch_query_device (DEVBLK *dev, BYTE **class,
int buflen, BYTE *buffer)
{
*class = "PCH";
snprintf (buffer, buflen, "%s%s%s",
dev->filename,
(dev->ascii ? " ascii" : " ebcdic"),
((dev->ascii && dev->crlf) ? " crlf" : ""));
} /* end function cardpch_query_device */
/*-------------------------------------------------------------------*/
/* Close the device */
/*-------------------------------------------------------------------*/
int cardpch_close_device ( DEVBLK *dev )
{
/* Close the device file */
close (dev->fd);
dev->fd = -1;
return 0;
} /* end function cardpch_close_device */
/*-------------------------------------------------------------------*/
/* Execute a Channel Command Word */
/*-------------------------------------------------------------------*/
void cardpch_execute_ccw (DEVBLK *dev, BYTE code, BYTE flags,
BYTE chained, U16 count, BYTE prevcode, int ccwseq,
BYTE *iobuf, BYTE *more, BYTE *unitstat, U16 *residual)
{
int rc; /* Return code */
int i; /* Loop counter */
int num; /* Number of bytes to move */
BYTE c; /* Output character */
/* Open the device file if necessary */
if (dev->fd < 0 && !IS_CCW_SENSE(code))
{
rc = open (dev->filename,
#ifdef WIN32
O_WRONLY | O_CREAT | O_TRUNC | O_SYNC | O_BINARY,
#else /* WIN32 */
O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
#endif /* WIN32 */
S_IRUSR | S_IWUSR | S_IRGRP);
if (rc < 0)
{
/* Handle open failure */
logmsg ("HHC423I Error opening file %s: %s\n",
dev->filename, strerror(errno));
/* Set unit check with intervention required */
dev->sense[0] = SENSE_IR;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return;
}
dev->fd = rc;
}
/* Process depending on CCW opcode */
switch (code) {
case 0x01:
case 0x41:
case 0x81:
/*---------------------------------------------------------------*/
/* WRITE, FEED, SELECT STACKER */
/*---------------------------------------------------------------*/
/* Start a new record if not data-chained from previous CCW */
if ((chained & CCW_FLAGS_CD) == 0)
{
dev->cardpos = 0;
dev->cardrem = CARD_LENGTH;
} /* end if(!data-chained) */
/* Calculate number of bytes to write and set residual count */
num = (count < dev->cardrem) ? count : dev->cardrem;
*residual = count - num;
/* Copy data from channel buffer to card buffer */
for (i = 0; i < num; i++)
{
c = iobuf[i];
if (dev->ascii)
{
c = ebcdic_to_ascii[c];
if (!isprint(c)) c = SPACE;
}
dev->buf[dev->cardpos] = c;
dev->cardpos++;
dev->cardrem--;
} /* end for(i) */
/* Perform end of record processing if not data-chaining */
if ((flags & CCW_FLAGS_CD) == 0)
{
if (dev->ascii)
{
/* Truncate trailing blanks from card buffer */
for (i = dev->cardpos; i > 0; i--)
if (dev->buf[i-1] != SPACE) break;
/* Append carriage return and line feed */
if (dev->crlf) dev->buf[i++] = '\r';
dev->buf[i++] = '\n';
}
else
{
/* Pad card image with blanks */
for (i = dev->cardpos; i < CARD_LENGTH; i++);
dev->buf[i] = HEX40;
}
/* Write card image */
write_buffer (dev, dev->buf, i, unitstat);
if (*unitstat != 0) break;
} /* end if(!data-chaining) */
/* Return normal status */
*unitstat = CSW_CE | CSW_DE;
break;
case 0x03:
/*---------------------------------------------------------------*/
/* CONTROL NO-OPERATION */
/*---------------------------------------------------------------*/
*unitstat = CSW_CE | CSW_DE;
break;
case 0x04:
/*---------------------------------------------------------------*/
/* SENSE */
/*---------------------------------------------------------------*/
/* Calculate residual byte count */
num = (count < dev->numsense) ? count : dev->numsense;
*residual = count - num;
if (count < dev->numsense) *more = 1;
/* Copy device sense bytes to channel I/O buffer */
memcpy (iobuf, dev->sense, num);
/* Clear the device sense bytes */
memset (dev->sense, 0, sizeof(dev->sense));
/* Return unit status */
*unitstat = CSW_CE | CSW_DE;
break;
case 0xE4:
/*---------------------------------------------------------------*/
/* SENSE ID */
/*---------------------------------------------------------------*/
/* Calculate residual byte count */
num = (count < dev->numdevid) ? count : dev->numdevid;
*residual = count - num;
if (count < dev->numdevid) *more = 1;
/* Copy device identifier bytes to channel I/O buffer */
memcpy (iobuf, dev->devid, num);
/* Return unit status */
*unitstat = CSW_CE | CSW_DE;
break;
default:
/*---------------------------------------------------------------*/
/* INVALID OPERATION */
/*---------------------------------------------------------------*/
/* Set command reject sense byte, and unit check status */
dev->sense[0] = SENSE_CR;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
} /* end switch(code) */
} /* end function cardpch_execute_ccw */