| 1 |
/* |
| 2 |
* libiec61883 - Linux IEEE 1394 streaming media library. |
| 3 |
* Copyright (C) 2004 Kristian Hogsberg, Dan Dennedy, and Dan Maas. |
| 4 |
* This file written by Dan Dennedy. |
| 5 |
* |
| 6 |
* Bits of raw1394 ARM handling borrowed from |
| 7 |
* Christian Toegel's <[email protected]> demos. |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Lesser General Public |
| 11 |
* License as published by the Free Software Foundation; either |
| 12 |
* version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* Lesser General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public |
| 20 |
* License along with this library; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include "iec61883.h" |
| 29 |
#include "iec61883-private.h" |
| 30 |
#include "cooked.h" |
| 31 |
|
| 32 |
#include <sys/types.h> |
| 33 |
#include <unistd.h> |
| 34 |
#include <string.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <stdint.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <netinet/in.h> |
| 40 |
|
| 41 |
#include <libraw1394/csr.h> |
| 42 |
|
| 43 |
/* |
| 44 |
* Plug register access functions |
| 45 |
* |
| 46 |
* Please see the convenience macros defined in iec61883.h. |
| 47 |
*/ |
| 48 |
|
| 49 |
int |
| 50 |
iec61883_plug_get(raw1394handle_t h, nodeid_t n, nodeaddr_t a, quadlet_t *value) |
| 51 |
{ |
| 52 |
quadlet_t temp; /* register value */ |
| 53 |
int result; |
| 54 |
|
| 55 |
result = iec61883_cooked_read( h, n, CSR_REGISTER_BASE + a, sizeof(quadlet_t), &temp); |
| 56 |
if (result >= 0) |
| 57 |
*value = ntohl(temp); /* endian conversion */ |
| 58 |
return result; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
int |
| 63 |
iec61883_plug_set(raw1394handle_t h, nodeid_t n, nodeaddr_t a, quadlet_t value) |
| 64 |
{ |
| 65 |
quadlet_t compare, swap, new; |
| 66 |
int result; |
| 67 |
|
| 68 |
/* get the current register value for comparison */ |
| 69 |
result = iec61883_plug_get( h, n, a, &compare ); |
| 70 |
if (result >= 0) |
| 71 |
{ |
| 72 |
/* convert endian */ |
| 73 |
compare = htonl(compare); |
| 74 |
swap = htonl(value); |
| 75 |
result = raw1394_lock( h, n, CSR_REGISTER_BASE + a, EXTCODE_COMPARE_SWAP, swap, compare, &new); |
| 76 |
if (new != compare) |
| 77 |
result = -EAGAIN; |
| 78 |
} |
| 79 |
return result; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
/* |
| 84 |
* Local host plugs implementation |
| 85 |
* |
| 86 |
* This requires the address range mapping feature of libraw1394 1.0. |
| 87 |
*/ |
| 88 |
|
| 89 |
/* ARM context identifiers */ |
| 90 |
static char g_arm_callback_context_out[] = "libiec61883 output context"; |
| 91 |
static char g_arm_callback_context_in[] = "libiec61883 input context"; |
| 92 |
static struct raw1394_arm_reqhandle g_arm_reqhandle_out; |
| 93 |
static struct raw1394_arm_reqhandle g_arm_reqhandle_in; |
| 94 |
|
| 95 |
/* register space */ |
| 96 |
|
| 97 |
/* Please note that this is host global. Each port (host adapter) |
| 98 |
does not get its own register space. Also, this is only intended |
| 99 |
for use by a single application. It appears that when multiple processes |
| 100 |
host these plugs, it still works; as long as the application uses the |
| 101 |
plug access functions above, then it should work. */ |
| 102 |
static struct output_registers { |
| 103 |
struct iec61883_oMPR mpr; |
| 104 |
struct iec61883_oPCR pcr[IEC61883_PCR_MAX]; |
| 105 |
} g_data_out; |
| 106 |
|
| 107 |
static struct input_registers { |
| 108 |
struct iec61883_iMPR mpr; |
| 109 |
struct iec61883_iPCR pcr[IEC61883_PCR_MAX]; |
| 110 |
} g_data_in; |
| 111 |
|
| 112 |
|
| 113 |
/** Send an async packet in response to a register read. |
| 114 |
* |
| 115 |
* This function handles host to bus endian conversion. |
| 116 |
* |
| 117 |
* \param handle A raw1394 handle. |
| 118 |
* \param arm_req A pointer to an arm_request struct from the ARM callback |
| 119 |
* handler. |
| 120 |
* \param a The CSR offset address of the register. |
| 121 |
* \param data The base address of the register space to read. |
| 122 |
* \return 0 for success, -1 on error. |
| 123 |
*/ |
| 124 |
static int |
| 125 |
do_arm_read(raw1394handle_t handle, struct raw1394_arm_request *arm_req, |
| 126 |
nodeaddr_t a, quadlet_t *data) |
| 127 |
{ |
| 128 |
quadlet_t *response; |
| 129 |
int num=4, offset; |
| 130 |
|
| 131 |
/* allocate response packet */ |
| 132 |
response = malloc(num * sizeof(quadlet_t)); |
| 133 |
if (!response) |
| 134 |
FAIL("unable to allocate response packet"); |
| 135 |
memset(response, 0x00, num * sizeof(quadlet_t)); |
| 136 |
|
| 137 |
/* fill data of response */ |
| 138 |
response[0] = |
| 139 |
((arm_req->source_nodeid & 0xFFFF) << 16) + |
| 140 |
((arm_req->tlabel & 0x3F) << 10) + |
| 141 |
(6 << 4); /* tcode = 6 */ |
| 142 |
response[1] = ((arm_req->destination_nodeid & 0xFFFF) << 16); |
| 143 |
/* rcode = resp_complete implied */ |
| 144 |
|
| 145 |
DEBUG (" destination_offset=%d", |
| 146 |
arm_req->destination_offset - CSR_REGISTER_BASE - a); |
| 147 |
offset = (arm_req->destination_offset - CSR_REGISTER_BASE - a)/4; |
| 148 |
response[3] = htonl(data[offset]); |
| 149 |
|
| 150 |
DEBUG(" response: 0x%8.8X",response[0]); |
| 151 |
DEBUG(" 0x%8.8X",response[1]); |
| 152 |
DEBUG(" 0x%8.8X",response[2]); |
| 153 |
DEBUG(" 0x%8.8X",response[3]); |
| 154 |
|
| 155 |
/* send response */ |
| 156 |
raw1394_start_async_send(handle, 16 , 16, 0, response, 0); |
| 157 |
|
| 158 |
free (response); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/** Update a local register value, and send a response packet. |
| 164 |
* |
| 165 |
* This function performs a compare/swap lock operation only. |
| 166 |
* This function handles host to bus endian conversion. |
| 167 |
* |
| 168 |
* \param handle A raw1394 handle. |
| 169 |
* \param arm_req A pointer to an arm_request struct from the ARM callback |
| 170 |
* handler. |
| 171 |
* \param a The CSR offset address of the register. |
| 172 |
* \param data The base address of the register space to update. |
| 173 |
* \return 0 for success, -1 on error. |
| 174 |
*/ |
| 175 |
static int |
| 176 |
do_arm_lock(raw1394handle_t handle, struct raw1394_arm_request *arm_req, |
| 177 |
nodeaddr_t a, quadlet_t *data) |
| 178 |
{ |
| 179 |
quadlet_t *response = NULL; |
| 180 |
int num, offset; |
| 181 |
int rcode = RCODE_COMPLETE; |
| 182 |
int requested_length = 4; |
| 183 |
|
| 184 |
if (arm_req->extended_transaction_code == EXTCODE_COMPARE_SWAP) |
| 185 |
{ |
| 186 |
quadlet_t arg_q, data_q, old_q, new_q; |
| 187 |
|
| 188 |
/* allocate response packet */ |
| 189 |
num = 4 + requested_length; |
| 190 |
response = malloc(num * sizeof(quadlet_t)); |
| 191 |
if (!response) |
| 192 |
FAIL("unable to allocate response packet"); |
| 193 |
memset(response, 0x00, num * sizeof(quadlet_t)); |
| 194 |
|
| 195 |
/* load data */ |
| 196 |
offset = (arm_req->destination_offset - CSR_REGISTER_BASE - a)/4; |
| 197 |
response[4] = htonl(data[offset]); |
| 198 |
|
| 199 |
/* compare */ |
| 200 |
arg_q = *(quadlet_t *) (&arm_req->buffer[0]); |
| 201 |
data_q = *(quadlet_t *) (&arm_req->buffer[4]); |
| 202 |
old_q = *(quadlet_t *) (&response[4]); |
| 203 |
new_q = (old_q == arg_q) ? data_q : old_q; |
| 204 |
|
| 205 |
/* swap */ |
| 206 |
data[offset] = ntohl(new_q); |
| 207 |
|
| 208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
rcode = RCODE_TYPE_ERROR; |
| 212 |
requested_length = 0; |
| 213 |
} |
| 214 |
|
| 215 |
/* fill data of response */ |
| 216 |
response[0] = |
| 217 |
((arm_req->source_nodeid & 0xFFFF) << 16) + |
| 218 |
((arm_req->tlabel & 0x3F) << 10) + |
| 219 |
(0xB << 4); /* tcode = B */ |
| 220 |
response[1] = |
| 221 |
((arm_req->destination_nodeid & 0xFFFF) << 16) + |
| 222 |
((rcode & 0xF) << 12); |
| 223 |
response[3] = |
| 224 |
((requested_length & 0xFFFF) << 16) + |
| 225 |
(arm_req->extended_transaction_code & 0xFF); |
| 226 |
|
| 227 |
DEBUG(" response: 0x%8.8X",response[0]); |
| 228 |
DEBUG(" 0x%8.8X",response[1]); |
| 229 |
DEBUG(" 0x%8.8X",response[2]); |
| 230 |
DEBUG(" 0x%8.8X",response[3]); |
| 231 |
DEBUG(" 0x%8.8X",response[4]); |
| 232 |
|
| 233 |
/* send response */ |
| 234 |
raw1394_start_async_send(handle, requested_length + 16, 16, 0, response, 0); |
| 235 |
|
| 236 |
free (response); |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
/* local plug ARM handler */ |
| 242 |
static int |
| 243 |
iec61883_arm_callback (raw1394handle_t handle, |
| 244 |
struct raw1394_arm_request_response *arm_req_resp, |
| 245 |
unsigned int requested_length, |
| 246 |
void *pcontext, byte_t request_type) |
| 247 |
{ |
| 248 |
struct raw1394_arm_request *arm_req = arm_req_resp->request; |
| 249 |
|
| 250 |
DEBUG( "request type=%d tcode=%d length=%d", request_type, arm_req->tcode, requested_length); |
| 251 |
DEBUG( "context = %s", (char *) pcontext); |
| 252 |
fflush(stdout); |
| 253 |
|
| 254 |
if (pcontext == g_arm_callback_context_out && requested_length == 4) |
| 255 |
{ |
| 256 |
if (request_type == RAW1394_ARM_READ && arm_req->tcode == 4) |
| 257 |
{ |
| 258 |
do_arm_read( handle, arm_req, CSR_O_MPR, (quadlet_t *) &g_data_out ); |
| 259 |
} |
| 260 |
else if (request_type == RAW1394_ARM_LOCK) |
| 261 |
{ |
| 262 |
do_arm_lock( handle, arm_req, CSR_O_MPR, (quadlet_t *) &g_data_out ); |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
/* error response */ |
| 267 |
} |
| 268 |
} |
| 269 |
else if (pcontext == g_arm_callback_context_in && requested_length == 4) |
| 270 |
{ |
| 271 |
if (request_type == RAW1394_ARM_READ) |
| 272 |
{ |
| 273 |
do_arm_read( handle, arm_req, CSR_I_MPR, (quadlet_t *) &g_data_in ); |
| 274 |
} |
| 275 |
else if (request_type == RAW1394_ARM_LOCK) |
| 276 |
{ |
| 277 |
do_arm_lock( handle, arm_req, CSR_I_MPR, (quadlet_t *) &g_data_in ); |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
/* error response */ |
| 282 |
} |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
/* error response */ |
| 287 |
} |
| 288 |
fflush(stdout); |
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
int |
| 294 |
iec61883_plug_impr_init (raw1394handle_t h, unsigned int data_rate) |
| 295 |
{ |
| 296 |
/* validate parameters */ |
| 297 |
if (data_rate >> 2 != 0) |
| 298 |
errno = -EINVAL; |
| 299 |
|
| 300 |
/* initialize data */ |
| 301 |
memset (&g_data_in, 0, sizeof (g_data_in)); |
| 302 |
g_data_in.mpr.data_rate = data_rate; |
| 303 |
|
| 304 |
/* initialize host environment */ |
| 305 |
memset (&g_arm_reqhandle_in, 0, sizeof(g_arm_reqhandle_in)); |
| 306 |
g_arm_reqhandle_in.arm_callback = (arm_req_callback_t) iec61883_arm_callback; |
| 307 |
g_arm_reqhandle_in.pcontext = g_arm_callback_context_in; |
| 308 |
|
| 309 |
/* register callback */ |
| 310 |
return raw1394_arm_register( h, CSR_REGISTER_BASE + CSR_I_MPR, sizeof(g_data_in), |
| 311 |
(byte_t *) &g_data_in, (unsigned long) &g_arm_reqhandle_in, |
| 312 |
0, 0, ( RAW1394_ARM_READ | RAW1394_ARM_LOCK ) ); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
void |
| 317 |
iec61883_plug_impr_clear (raw1394handle_t h) |
| 318 |
{ |
| 319 |
g_data_in.mpr.n_plugs = 0; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
int |
| 324 |
iec61883_plug_impr_close (raw1394handle_t h) |
| 325 |
{ |
| 326 |
g_data_in.mpr.n_plugs = 0; |
| 327 |
return raw1394_arm_unregister(h, CSR_REGISTER_BASE + CSR_I_MPR); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
int |
| 332 |
iec61883_plug_ipcr_add (raw1394handle_t h, unsigned int online) |
| 333 |
{ |
| 334 |
int i = g_data_in.mpr.n_plugs; |
| 335 |
|
| 336 |
/* validate parameters */ |
| 337 |
if (g_arm_reqhandle_in.arm_callback == NULL) |
| 338 |
return -EPERM; |
| 339 |
if (i + 1 > IEC61883_PCR_MAX) |
| 340 |
return -ENOSPC; |
| 341 |
if (online >> 1 != 0) |
| 342 |
return -EINVAL; |
| 343 |
|
| 344 |
/* update data */ |
| 345 |
g_data_in.pcr[i].online = online; |
| 346 |
g_data_in.mpr.n_plugs++; |
| 347 |
|
| 348 |
/* return which plug is added */ |
| 349 |
return i; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
int |
| 354 |
iec61883_plug_ompr_init (raw1394handle_t h, unsigned int data_rate, |
| 355 |
unsigned int bcast_channel) |
| 356 |
{ |
| 357 |
/* validate parameters */ |
| 358 |
if (data_rate >> 2 != 0) |
| 359 |
errno = -EINVAL; |
| 360 |
if (bcast_channel >> 6 != 0) |
| 361 |
errno = -EINVAL; |
| 362 |
|
| 363 |
/* initialize data */ |
| 364 |
memset (&g_data_out, 0, sizeof (g_data_out)); |
| 365 |
g_data_out.mpr.data_rate = data_rate; |
| 366 |
g_data_out.mpr.bcast_channel = bcast_channel; |
| 367 |
|
| 368 |
/* initialize host environment */ |
| 369 |
memset (&g_arm_reqhandle_out, 0, sizeof(g_arm_reqhandle_out)); |
| 370 |
g_arm_reqhandle_out.arm_callback = (arm_req_callback_t) iec61883_arm_callback; |
| 371 |
g_arm_reqhandle_out.pcontext = g_arm_callback_context_out; |
| 372 |
|
| 373 |
/* register callback */ |
| 374 |
return raw1394_arm_register( h, CSR_REGISTER_BASE + CSR_O_MPR, sizeof(g_data_out), |
| 375 |
(byte_t *) &g_data_out, (unsigned long) &g_arm_reqhandle_out, |
| 376 |
0, 0, ( RAW1394_ARM_READ | RAW1394_ARM_LOCK ) ); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
void |
| 381 |
iec61883_plug_ompr_clear (raw1394handle_t h) |
| 382 |
{ |
| 383 |
g_data_out.mpr.n_plugs = 0; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
int |
| 388 |
iec61883_plug_ompr_close (raw1394handle_t h) |
| 389 |
{ |
| 390 |
g_data_out.mpr.n_plugs = 0; |
| 391 |
return raw1394_arm_unregister(h, CSR_REGISTER_BASE + CSR_O_MPR); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
int |
| 396 |
iec61883_plug_opcr_add (raw1394handle_t h, unsigned int online, |
| 397 |
unsigned int overhead_id, unsigned int payload) |
| 398 |
{ |
| 399 |
int i = g_data_out.mpr.n_plugs; |
| 400 |
|
| 401 |
/* validate parameters */ |
| 402 |
if (g_arm_reqhandle_out.arm_callback == NULL) |
| 403 |
return -EPERM; |
| 404 |
if (i + 1 > IEC61883_PCR_MAX) |
| 405 |
return -ENOSPC; |
| 406 |
if (online >> 1 != 0) |
| 407 |
return -EINVAL; |
| 408 |
if (overhead_id >> 4 != 0) |
| 409 |
return -EINVAL; |
| 410 |
if (payload >> 10 != 0) |
| 411 |
return -EINVAL; |
| 412 |
|
| 413 |
/* update plug */ |
| 414 |
g_data_out.pcr[i].online = online; |
| 415 |
g_data_out.pcr[i].overhead_id = overhead_id; |
| 416 |
g_data_out.pcr[i].payload = payload; |
| 417 |
|
| 418 |
/* increment the number of plugs available */ |
| 419 |
g_data_out.mpr.n_plugs++; |
| 420 |
|
| 421 |
/* return which plug is added */ |
| 422 |
return i; |
| 423 |
} |