| 1 |
/* Hey EMACS -*- linux-c -*- */ |
| 2 |
/* $Id$ */ |
| 3 |
|
| 4 |
/* libticables2 - link cable library, a part of the TiLP project |
| 5 |
* Copyright (C) 1999-2005 Romain Lievin |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software Foundation, |
| 19 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/* "VTi" virtual link cable unit */ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
/* |
| 29 |
This part use Shared Memory Segment for implementing two circular buffers |
| 30 |
between 2 program which use this lib. Each segment is attached 2 times. |
| 31 |
Convention used: 0 is an emulator and 1 is a linking program. |
| 32 |
One shm is used for transferring information from 0 to 1 and the other |
| 33 |
shm is used for transferring from 1 to 0. |
| 34 |
shm0: W -> R |
| 35 |
shm1: R <- W |
| 36 |
*/ |
| 37 |
|
| 38 |
#include <stdio.h> |
| 39 |
#include <string.h> |
| 40 |
#include <sys/types.h> |
| 41 |
#include <sys/ipc.h> |
| 42 |
#include <sys/shm.h> |
| 43 |
|
| 44 |
#include "../ticables.h" |
| 45 |
#include "../logging.h" |
| 46 |
#include "../error.h" |
| 47 |
#include "../gettext.h" |
| 48 |
#include "../internal.h" |
| 49 |
#include "detect.h" |
| 50 |
|
| 51 |
#define BUF_SIZE 1*1024 |
| 52 |
|
| 53 |
/* Circular buffer (0: TIEmu, 1: TiLP) */ |
| 54 |
typedef struct |
| 55 |
{ |
| 56 |
uint8_t buf[BUF_SIZE]; |
| 57 |
int start; |
| 58 |
int end; |
| 59 |
} LinkBuffer; |
| 60 |
|
| 61 |
/* Shm variables */ |
| 62 |
static key_t ipc_key[2]; // IPC key |
| 63 |
static int shmid[2]; // Shm ID |
| 64 |
static LinkBuffer *shm[2]; // Shm address |
| 65 |
|
| 66 |
static LinkBuffer* send_buf[2]; // Swapped buffer |
| 67 |
static LinkBuffer* recv_buf[2]; |
| 68 |
|
| 69 |
static int vti_prepare(CableHandle *h) |
| 70 |
{ |
| 71 |
// in fact, address & device are unused |
| 72 |
switch(h->port) |
| 73 |
{ |
| 74 |
case PORT_0: // automatic setting |
| 75 |
h->address = 0; //ref_cnt; |
| 76 |
break; |
| 77 |
case PORT_1: // forced setting, for compatibility |
| 78 |
case PORT_3: |
| 79 |
h->address = 0; h->device = strdup("0->1"); |
| 80 |
break; |
| 81 |
case PORT_2: |
| 82 |
case PORT_4: |
| 83 |
h->address = 1; h->device = strdup("1->0"); |
| 84 |
break; |
| 85 |
default: return ERR_ILLEGAL_ARG; |
| 86 |
} |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
static int vti_open(CableHandle *h) |
| 92 |
{ |
| 93 |
int i; |
| 94 |
|
| 95 |
/* Get a unique (if possible) key */ |
| 96 |
for (i = 0; i < 2; i++) |
| 97 |
{ |
| 98 |
if ((ipc_key[i] = ftok("/tmp", i)) == -1) |
| 99 |
{ |
| 100 |
ticables_warning("unable to get unique key (ftok).\n"); |
| 101 |
return ERR_VTI_IPCKEY; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/* Open a shared memory segment */ |
| 106 |
for (i = 0; i < 2; i++) |
| 107 |
{ |
| 108 |
if ((shmid[i] = shmget(ipc_key[i], sizeof(LinkBuffer), IPC_CREAT | 0666)) == -1) |
| 109 |
{ |
| 110 |
ticables_warning("unable to open shared memory (shmget).\n"); |
| 111 |
return ERR_VTI_SHMGET; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/* Attach the shm */ |
| 116 |
for (i = 0; i < 2; i++) |
| 117 |
{ |
| 118 |
if ((shm[i] = (LinkBuffer *)shmat(shmid[i], NULL, 0)) == (LinkBuffer *)-1) |
| 119 |
{ |
| 120 |
ticables_warning("unable to attach shared memory (shmat).\n"); |
| 121 |
return ERR_VTI_SHMAT; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/* Swap shm */ |
| 126 |
send_buf[0] = shm[0]; // 0 -> 1: writing |
| 127 |
recv_buf[0] = shm[1]; // 0 <- 1: reading |
| 128 |
send_buf[1] = shm[1]; // 1 -> 0: writing |
| 129 |
recv_buf[1] = shm[0]; // 1 <- 0: reading |
| 130 |
|
| 131 |
for (i = 0; i < 2; i++) |
| 132 |
{ |
| 133 |
shm[i]->start = shm[i]->end = 0; |
| 134 |
} |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
static int vti_close(CableHandle *h) |
| 140 |
{ |
| 141 |
int i; |
| 142 |
|
| 143 |
/* Detach segment */ |
| 144 |
for (i = 0; i < 2; i++) |
| 145 |
{ |
| 146 |
if (shmdt(shm[i]) == -1) |
| 147 |
{ |
| 148 |
ticables_warning("shmdt\n"); |
| 149 |
return ERR_VTI_SHMDT; |
| 150 |
} |
| 151 |
|
| 152 |
/* and destroy it */ |
| 153 |
if (shmctl(shmid[i], IPC_RMID, NULL) == -1) |
| 154 |
{ |
| 155 |
ticables_warning("shmctl\n"); |
| 156 |
return ERR_VTI_SHMCTL; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static int vti_reset(CableHandle *h) |
| 164 |
{ |
| 165 |
int i; |
| 166 |
|
| 167 |
for (i = 0; i < 2; i++) |
| 168 |
{ |
| 169 |
shm[i]->start = shm[i]->end = 0; |
| 170 |
} |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
static int vti_put(CableHandle *h, uint8_t *data, uint32_t len) |
| 176 |
{ |
| 177 |
int p = h->address; |
| 178 |
uint32_t i; |
| 179 |
tiTIME clk; |
| 180 |
|
| 181 |
for(i = 0; i < len; i++) |
| 182 |
{ |
| 183 |
TO_START(clk); |
| 184 |
do |
| 185 |
{ |
| 186 |
if (TO_ELAPSED(clk, h->timeout)) |
| 187 |
{ |
| 188 |
return ERR_WRITE_TIMEOUT; |
| 189 |
} |
| 190 |
} |
| 191 |
while (((send_buf[p]->end + 1) & 255) == send_buf[p]->start); |
| 192 |
|
| 193 |
send_buf[p]->buf[send_buf[p]->end] = data[i]; |
| 194 |
send_buf[p]->end = (send_buf[p]->end + 1) & 255; |
| 195 |
} |
| 196 |
|
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
static int vti_get(CableHandle *h, uint8_t *data, uint32_t len) |
| 201 |
{ |
| 202 |
int p = h->address; |
| 203 |
uint32_t i; |
| 204 |
tiTIME clk; |
| 205 |
|
| 206 |
for(i = 0; i < len; i++) |
| 207 |
{ |
| 208 |
TO_START(clk); |
| 209 |
do |
| 210 |
{ |
| 211 |
if (TO_ELAPSED(clk, h->timeout)) |
| 212 |
{ |
| 213 |
return ERR_READ_TIMEOUT; |
| 214 |
} |
| 215 |
} |
| 216 |
while (recv_buf[p]->start == recv_buf[p]->end); |
| 217 |
|
| 218 |
data[i] = recv_buf[p]->buf[recv_buf[p]->start]; |
| 219 |
recv_buf[p]->start = (recv_buf[p]->start + 1) & 255; |
| 220 |
} |
| 221 |
|
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
static int vti_probe(CableHandle *h) |
| 226 |
{ |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
static int vti_check(CableHandle *h, int *status) |
| 231 |
{ |
| 232 |
int p = h->address; |
| 233 |
*status = !(recv_buf[p]->start == recv_buf[p]->end); |
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
extern const CableFncts cable_vti = |
| 238 |
{ |
| 239 |
CABLE_VTI, |
| 240 |
"VTI", |
| 241 |
N_("Virtual TI"), |
| 242 |
N_("Virtual link for VTi"), |
| 243 |
0, |
| 244 |
&vti_prepare, |
| 245 |
&vti_open, &vti_close, &vti_reset, &vti_probe, NULL, |
| 246 |
&vti_put, &vti_get, &vti_check, |
| 247 |
&noop_set_red_wire, &noop_set_white_wire, |
| 248 |
&noop_get_red_wire, &noop_get_white_wire, |
| 249 |
NULL, NULL, |
| 250 |
&noop_set_device, |
| 251 |
NULL |
| 252 |
}; |