| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | #include <stdio.h> |
|---|
| 17 | #include <stdlib.h> |
|---|
| 18 | #include <unistd.h> |
|---|
| 19 | #include <fcntl.h> |
|---|
| 20 | #include <errno.h> |
|---|
| 21 | #include <stdarg.h> |
|---|
| 22 | #include <math.h> |
|---|
| 23 | |
|---|
| 24 | #include "backend.h" |
|---|
| 25 | #define PARPORT_DEVICE "/dev/parport0" |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | #include <sys/ioctl.h> |
|---|
| 30 | #include <linux/ppdev.h> |
|---|
| 31 | #include <linux/parport.h> |
|---|
| 32 | #include <sys/time.h> |
|---|
| 33 | |
|---|
| 34 | int puncher_backend_mtime(long long since) { |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | struct timeval time; |
|---|
| 39 | gettimeofday(&time, NULL); |
|---|
| 40 | |
|---|
| 41 | return (time.tv_sec * 1000000 + time.tv_usec) - since; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | PuncherBackend* puncher_backend_new(int debug_flag) { |
|---|
| 45 | int mode; |
|---|
| 46 | PuncherBackend* c = malloc(sizeof(PuncherBackend)); |
|---|
| 47 | c->debug_flag = debug_flag; |
|---|
| 48 | |
|---|
| 49 | printf("FACIT 4070 Tape punch-75 CPS userspace driver\n"); |
|---|
| 50 | printf("Starting up...\n"); |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | c->parport_fd = open(PARPORT_DEVICE, O_RDWR); |
|---|
| 54 | if(c->parport_fd == -1) { |
|---|
| 55 | perror("opening device failed"); |
|---|
| 56 | return NULL; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | if(ioctl(c->parport_fd, PPCLAIM)) { |
|---|
| 62 | perror ("claiming port (PPCLAIM)"); |
|---|
| 63 | close (c->parport_fd); |
|---|
| 64 | return NULL; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | mode = IEEE1284_MODE_COMPAT; |
|---|
| 70 | if(ioctl(c->parport_fd, PPNEGOT, &mode)) { |
|---|
| 71 | perror ("Setting compatibilty mode (PPNEGOT)"); |
|---|
| 72 | close (c->parport_fd); |
|---|
| 73 | return NULL; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | PUNCHER_BACKEND_DPRINTF("Waiting for Puncher beeing ready for signals...\n"); |
|---|
| 77 | { |
|---|
| 78 | unsigned char status; |
|---|
| 79 | if(ioctl(c->parport_fd, PPRSTATUS, &status)) return NULL; |
|---|
| 80 | if((status & PARPORT_STATUS_BUSY) == PARPORT_STATUS_BUSY) { |
|---|
| 81 | PUNCHER_BACKEND_DPRINTF("Puncher is BUSY!\n"); |
|---|
| 82 | usleep(8000); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | PUNCHER_BACKEND_DPRINTF("Puncher ready.\n"); |
|---|
| 87 | return c; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | int puncher_backend_emergency_stop(PuncherBackend* c) { |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | static unsigned char mask = PARPORT_CONTROL_STROBE; |
|---|
| 97 | PUNCHER_BACKEND_DPRINTF("Setting null mask to control pins (strobe,etc.)...\n"); |
|---|
| 98 | if(ioctl(c->parport_fd, PPWCONTROL, &mask)) { |
|---|
| 99 | perror("backend emergency exit: null mask"); |
|---|
| 100 | close(c->parport_fd); |
|---|
| 101 | return 1; |
|---|
| 102 | } |
|---|
| 103 | return 0; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | int puncher_backend_destroy(PuncherBackend* c) { |
|---|
| 107 | puncher_backend_emergency_stop(c); |
|---|
| 108 | if(ioctl(c->parport_fd, PPRELEASE)) { |
|---|
| 109 | perror ("Releasing the parport"); |
|---|
| 110 | return 1; |
|---|
| 111 | } |
|---|
| 112 | close(c->parport_fd); |
|---|
| 113 | PUNCHER_BACKEND_DPRINTF("Goodbye from the parport.\n"); |
|---|
| 114 | return 0; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | int puncher_backend_write_byte(PuncherBackend* c, unsigned char buf) { |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | static unsigned char strobe_on = (PARPORT_CONTROL_STROBE); |
|---|
| 128 | static unsigned char null_mask = 0x00; |
|---|
| 129 | unsigned char status; |
|---|
| 130 | long long time; |
|---|
| 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 | if(ioctl(c->parport_fd, PPWDATA, &buf)) |
|---|
| 157 | return -2; |
|---|
| 158 | |
|---|
| 159 | time = puncher_backend_mtime(0); |
|---|
| 160 | |
|---|
| 161 | usleep(10); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | if(ioctl(c->parport_fd, PPWCONTROL, &null_mask)) |
|---|
| 165 | return -3; |
|---|
| 166 | PUNCHER_BACKEND_DPRINTF("data set; strobe pulsed...\n"); |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | usleep(220); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | if(ioctl(c->parport_fd, PPRSTATUS, &status)) |
|---|
| 174 | return -4; |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | PUNCHER_BACKEND_DPRINTF("Control is %x. Ending data pins... ",status); |
|---|
| 189 | if(ioctl(c->parport_fd, PPWDATA, &null_mask)) return -5; |
|---|
| 190 | PUNCHER_BACKEND_DPRINTF("+ strobe... "); |
|---|
| 191 | if(ioctl(c->parport_fd, PPWCONTROL, &strobe_on)) return -5; |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | { |
|---|
| 195 | int wait = 40*1000-puncher_backend_mtime(time); |
|---|
| 196 | PUNCHER_BACKEND_DPRINTF("\nWaiting for Puncher Ready (%i usec)...", wait); |
|---|
| 197 | if(wait>0) usleep(wait); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | if(ioctl(c->parport_fd, PPRSTATUS, &status)) return -6; |
|---|
| 201 | if((status & PARPORT_STATUS_BUSY) == PARPORT_STATUS_BUSY) { |
|---|
| 202 | PUNCHER_BACKEND_DPRINTF("Still busy (%x).\n", status); |
|---|
| 203 | return 1; |
|---|
| 204 | } else { |
|---|
| 205 | PUNCHER_BACKEND_DPRINTF("Finished successfully (%x)\n", status); |
|---|
| 206 | return 0; |
|---|
| 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 | |
|---|