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
|
/*
* 1394-Based Digital Camera Control Library
*
* Camera control code for Linux
*
* Written by
* Chris Urmson <curmson@ri.cmu.edu>
* Damien Douxchamps <ddouxchamps@users.sf.net>
* Dan Dennedy <ddennedy@users.sf.net>
* David Moore <dcm@acm.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include "dc1394/internal.h"
#include "linux.h"
#include "offsets.h"
#include "types.h"
static int
is_device_available (const char * filename)
{
int fd;
struct stat sbuf;
if (!filename)
return 0;
if (stat (filename, &sbuf) < 0)
return 0;
fd = open (filename, O_RDWR);
if (fd < 0)
return 0;
close (fd);
dc1394_log_debug("linux: Found %s", filename);
return 1;
}
static platform_t *
dc1394_linux_new (void)
{
if (!is_device_available (getenv("RAW1394DEV")) &&
!is_device_available ("/dev/raw1394")) {
dc1394_log_debug("linux: Failed to open RAW1394DEV or /dev/raw1394");
return NULL;
}
platform_t * p = calloc (1, sizeof (platform_t));
return p;
}
static void
dc1394_linux_free (platform_t * p)
{
free (p);
}
struct _platform_device_t {
uint32_t config_rom[256];
int num_quads;
int port, node, generation;
};
static int
read_retry (struct raw1394_handle * handle, nodeid_t node, nodeaddr_t addr,
size_t length, quadlet_t * buffer)
{
int retry = DC1394_MAX_RETRIES;
while (retry > 0) {
if (raw1394_read (handle, node, addr, length, buffer) == 0)
return 0;
if (errno != EAGAIN)
return -1;
usleep (DC1394_SLOW_DOWN);
retry--;
}
return -1;
}
static platform_device_list_t *
dc1394_linux_get_device_list (platform_t * p)
{
platform_device_list_t * list;
uint32_t allocated_size = 64;
raw1394handle_t handle;
int num_ports, i;
handle = raw1394_new_handle ();
if (!handle)
return NULL;
num_ports = raw1394_get_port_info (handle, NULL, 0);
dc1394_log_debug ("linux: Found %d port(s)", num_ports);
raw1394_destroy_handle (handle);
list = calloc (1, sizeof (platform_device_list_t));
if (!list)
return NULL;
list->devices = malloc(allocated_size * sizeof(platform_device_t *));
if (!list->devices) {
free (list);
return NULL;
}
for (i = 0; i < num_ports; i++) {
int num_nodes, j;
handle = raw1394_new_handle_on_port (i);
if (!handle)
continue;
num_nodes = raw1394_get_nodecount (handle);
dc1394_log_debug ("linux: Port %d opened with %d node(s)",
i, num_nodes);
for (j = 0; j < num_nodes; j++) {
platform_device_t * device;
uint32_t quad;
int k;
if (read_retry (handle, 0xFFC0 | j, CONFIG_ROM_BASE + 0x400, 4, &quad) < 0)
continue;
device = malloc (sizeof (platform_device_t));
if (!device)
continue;
device->config_rom[0] = ntohl (quad);
device->port = i;
device->node = j;
device->generation = raw1394_get_generation (handle);
for (k = 1; k < 256; k++) {
if (read_retry (handle, 0xFFC0 | j, CONFIG_ROM_BASE + 0x400 + 4*k, 4, &quad) < 0)
break;
device->config_rom[k] = ntohl (quad);
}
device->num_quads = k;
list->devices[list->num_devices] = device;
list->num_devices++;
if (list->num_devices >= allocated_size) {
allocated_size += 64;
list->devices = realloc (list->devices, allocated_size * sizeof (platform_device_t *));
if (!list->devices)
return NULL;
}
}
raw1394_destroy_handle (handle);
}
return list;
}
static void
dc1394_linux_free_device_list (platform_device_list_t * d)
{
int i;
for (i = 0; i < d->num_devices; i++)
free (d->devices[i]);
free (d->devices);
free (d);
}
static int
dc1394_linux_device_get_config_rom (platform_device_t * device,
uint32_t * quads, int * num_quads)
{
if (*num_quads > device->num_quads)
*num_quads = device->num_quads;
memcpy (quads, device->config_rom, *num_quads * sizeof (uint32_t));
return 0;
}
static platform_camera_t *
dc1394_linux_camera_new (platform_t * p, platform_device_t * device, uint32_t unit_directory_offset)
{
platform_camera_t * camera;
raw1394handle_t handle;
handle = raw1394_new_handle_on_port (device->port);
if (!handle)
return NULL;
if (device->generation != raw1394_get_generation (handle)) {
dc1394_log_error("generation has changed since bus was scanned");
raw1394_destroy_handle (handle);
return NULL;
}
camera = calloc (1, sizeof (platform_camera_t));
camera->handle = handle;
camera->port = device->port;
camera->node = device->node;
camera->generation = device->generation;
return camera;
}
static void
dc1394_linux_camera_free (platform_camera_t * cam)
{
if (cam->capture.dma_device_file != NULL) {
free (cam->capture.dma_device_file);
cam->capture.dma_device_file = NULL;
}
raw1394_destroy_handle (cam->handle);
free (cam);
}
static void
dc1394_linux_camera_set_parent (platform_camera_t * cam,
dc1394camera_t * parent)
{
cam->camera = parent;
}
static dc1394error_t
dc1394_linux_camera_print_info (platform_camera_t * cam, FILE *fd)
{
fprintf(fd,"------ Camera platform-specific information ------\n");
fprintf(fd,"Handle : %p\n", cam->handle);
fprintf(fd,"Port : %d\n", cam->port);
fprintf(fd,"Node : %d\n", cam->node);
return DC1394_SUCCESS;
}
static dc1394error_t
dc1394_linux_camera_read (platform_camera_t * cam, uint64_t offset,
uint32_t * quads, int num_quads)
{
int i, retval, retry = DC1394_MAX_RETRIES;
/* retry a few times if necessary (addition by PDJ) */
while(retry--) {
#ifdef DC1394_DEBUG_LOWEST_LEVEL
fprintf(stderr,"get %d regs at 0x%llx : ",
num_quads, offset + CONFIG_ROM_BASE);
#endif
retval = raw1394_read (cam->handle, 0xffc0 | cam->node, offset + CONFIG_ROM_BASE, 4 * num_quads, quads);
#ifdef DC1394_DEBUG_LOWEST_LEVEL
fprintf(stderr,"0x%lx [...]\n", quads[0]);
#endif
if (!retval)
goto out;
else if (errno != EAGAIN)
return ( retval ? DC1394_RAW1394_FAILURE : DC1394_SUCCESS );
// usleep is executed only if the read fails!!!
usleep(DC1394_SLOW_DOWN);
}
out:
/* conditionally byte swap the value */
for (i = 0; i < num_quads; i++)
quads[i] = ntohl (quads[i]);
return ( retval ? DC1394_RAW1394_FAILURE : DC1394_SUCCESS );
}
static dc1394error_t
dc1394_linux_camera_write (platform_camera_t * cam, uint64_t offset,
const uint32_t * quads, int num_quads)
{
int i, retval, retry= DC1394_MAX_RETRIES;
uint32_t value[num_quads];
/* conditionally byte swap the value (addition by PDJ) */
for (i = 0; i < num_quads; i++)
value[i] = htonl (quads[i]);
/* retry a few times if necessary */
while(retry--) {
#ifdef DC1394_DEBUG_LOWEST_LEVEL
fprintf(stderr,"set %d regs at 0x%llx to value 0x%lx [...]\n",
num_quads, offset + CONFIG_ROM_BASE, value[0]);
#endif
retval = raw1394_write(cam->handle, 0xffc0 | cam->node, offset + CONFIG_ROM_BASE, 4 * num_quads, value);
if (!retval || (errno != EAGAIN))
return ( retval ? DC1394_RAW1394_FAILURE : DC1394_SUCCESS );
// usleep is executed only if the read fails!!!
usleep(DC1394_SLOW_DOWN);
}
return DC1394_RAW1394_FAILURE;
}
static dc1394error_t
dc1394_linux_reset_bus (platform_camera_t * cam)
{
if (raw1394_reset_bus (cam->handle) == 0)
return DC1394_SUCCESS;
else
return DC1394_FAILURE;
}
static dc1394error_t
dc1394_linux_read_cycle_timer (platform_camera_t * cam,
uint32_t * cycle_timer, uint64_t * local_time)
{
quadlet_t quad;
struct timeval tv;
if (raw1394_read (cam->handle, raw1394_get_local_id (cam->handle),
CSR_REGISTER_BASE + CSR_CYCLE_TIME, sizeof (quadlet_t), &quad) < 0)
return DC1394_FAILURE;
gettimeofday (&tv, NULL);
*cycle_timer = ntohl (quad);
*local_time = (uint64_t)tv.tv_sec * 1000000ULL + tv.tv_usec;
return DC1394_SUCCESS;
}
static dc1394error_t
dc1394_linux_set_broadcast(platform_camera_t * craw, dc1394bool_t pwr)
{
if (pwr==DC1394_TRUE) {
if (craw->broadcast_is_set==DC1394_FALSE) {
craw->backup_node_id=craw->node;
craw->node=63;
craw->broadcast_is_set=DC1394_TRUE;
}
}
else if (pwr==DC1394_FALSE) {
if (craw->broadcast_is_set==DC1394_TRUE) {
craw->node=craw->backup_node_id;
craw->broadcast_is_set=DC1394_FALSE;
}
}
else
return DC1394_INVALID_ARGUMENT_VALUE;
return DC1394_SUCCESS;
}
static dc1394error_t
dc1394_linux_get_broadcast(platform_camera_t * craw, dc1394bool_t *pwr)
{
*pwr=craw->broadcast_is_set;
return DC1394_SUCCESS;
}
static dc1394error_t
dc1394_linux_iso_set_persist (platform_camera_t * cam)
{
return DC1394_SUCCESS;
}
static dc1394error_t
dc1394_linux_iso_allocate_channel (platform_camera_t * cam, uint64_t channels_allowed, int * channel)
{
int i;
for (i = 0; i < 64; i++) {
if (!((channels_allowed >> i) & 1))
continue;
if (raw1394_channel_modify (cam->handle, i, RAW1394_MODIFY_ALLOC) == 0)
break;
}
if (i == 64) {
dc1394_log_error ("Error: Failed to allocate iso channel");
return DC1394_NO_ISO_CHANNEL;
}
*channel = i;
return DC1394_SUCCESS;
}
static dc1394error_t
dc1394_linux_iso_release_channel (platform_camera_t * cam, int channel)
{
if (raw1394_channel_modify (cam->handle, channel, RAW1394_MODIFY_FREE) < 0) {
dc1394_log_error("Error: Could not free iso channel");
return DC1394_FAILURE;
}
return DC1394_SUCCESS;
}
static dc1394error_t
dc1394_linux_iso_allocate_bandwidth (platform_camera_t * cam,
int bandwidth_units)
{
if (raw1394_bandwidth_modify (cam->handle, bandwidth_units, RAW1394_MODIFY_ALLOC) < 0) {
dc1394_log_error ("Error: Failed to allocate iso bandwidth");
return DC1394_NO_BANDWIDTH;
}
return DC1394_SUCCESS;
}
static dc1394error_t
dc1394_linux_iso_release_bandwidth (platform_camera_t * cam,
int bandwidth_units)
{
if (raw1394_bandwidth_modify (cam->handle, bandwidth_units, RAW1394_MODIFY_FREE) < 0) {
dc1394_log_error ("Error: Failed to free iso bandwidth");
return DC1394_FAILURE;
}
return DC1394_SUCCESS;
}
static dc1394error_t
dc1394_linux_camera_get_node(platform_camera_t *cam, uint32_t *node,
uint32_t * generation)
{
if (node)
*node = cam->node;
if (generation)
*generation = cam->generation;
return DC1394_SUCCESS;
}
dc1394error_t
dc1394_camera_get_linux_port(dc1394camera_t *camera, uint32_t *port)
{
dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
platform_camera_t * craw = cpriv->pcam;
*port=craw->port;
return DC1394_SUCCESS;
}
static platform_dispatch_t
linux_dispatch = {
.platform_new = dc1394_linux_new,
.platform_free = dc1394_linux_free,
.get_device_list = dc1394_linux_get_device_list,
.free_device_list = dc1394_linux_free_device_list,
.device_get_config_rom = dc1394_linux_device_get_config_rom,
.camera_new = dc1394_linux_camera_new,
.camera_free = dc1394_linux_camera_free,
.camera_set_parent = dc1394_linux_camera_set_parent,
.camera_read = dc1394_linux_camera_read,
.camera_write = dc1394_linux_camera_write,
.reset_bus = dc1394_linux_reset_bus,
.camera_print_info = dc1394_linux_camera_print_info,
.camera_get_node = dc1394_linux_camera_get_node,
.set_broadcast = dc1394_linux_set_broadcast,
.get_broadcast = dc1394_linux_get_broadcast,
.read_cycle_timer = dc1394_linux_read_cycle_timer,
.capture_setup = dc1394_linux_capture_setup,
.capture_stop = dc1394_linux_capture_stop,
.capture_dequeue = dc1394_linux_capture_dequeue,
.capture_enqueue = dc1394_linux_capture_enqueue,
.capture_get_fileno = dc1394_linux_capture_get_fileno,
.capture_set_callback = NULL,
.capture_schedule_with_runloop = NULL,
.iso_set_persist = dc1394_linux_iso_set_persist,
.iso_allocate_channel = dc1394_linux_iso_allocate_channel,
.iso_release_channel = dc1394_linux_iso_release_channel,
.iso_allocate_bandwidth = dc1394_linux_iso_allocate_bandwidth,
.iso_release_bandwidth = dc1394_linux_iso_release_bandwidth,
};
void
linux_init(dc1394_t * d)
{
register_platform (d, &linux_dispatch, "linux");
}
|