home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / quicktime / libraw1394.2217 / main.c < prev    next >
C/C++ Source or Header  |  2000-11-29  |  5KB  |  203 lines

  1. /*
  2.  * libraw1394 - library for raw access to the 1394 bus with the Linux subsystem.
  3.  *
  4.  * Copyright (C) 1999,2000 Andreas Bombe
  5.  *
  6.  * This library is licensed under the GNU Lesser General Public License (LGPL),
  7.  * version 2.1 or later. See the file COPYING.LIB in the distribution for
  8.  * details.
  9.  */
  10.  
  11. #include <config.h>
  12. #include <errno.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16.  
  17. #include "raw1394.h"
  18. #include "kernel-raw1394.h"
  19. #include "raw1394_private.h"
  20.  
  21.  
  22. static int bus_reset_default(struct raw1394_handle *handle)
  23. {
  24.         return 0;
  25. }
  26.  
  27. static int tag_handler_default(struct raw1394_handle *handle, unsigned long tag,
  28.                                int error)
  29. {
  30.         struct raw1394_reqhandle *rh;
  31.  
  32.         if (tag) {
  33.                 rh = (struct raw1394_reqhandle *)tag;
  34.                 return rh->callback(handle, rh->data, error);
  35.         } else {
  36.                 return -1;
  37.         }
  38. }
  39.  
  40. int _raw1394_sync_cb(struct raw1394_handle *unused, struct sync_cb_data *data,
  41.                      int error)
  42. {
  43.         data->errcode = error;
  44.         data->done = 1;
  45.         return 0;
  46. }
  47.  
  48.  
  49.  
  50.  
  51. static unsigned int init_rawdevice(struct raw1394_handle *h)
  52. {
  53.         struct raw1394_request *req = &h->req;
  54.  
  55.         CLEAR_REQ(req);
  56.         req->type = RAW1394_REQ_INITIALIZE;
  57.         req->misc = RAW1394_KERNELAPI_VERSION;
  58.  
  59.         if (write(h->fd, req, sizeof(*req)) < 0) return -1;
  60.         if (read(h->fd, req, sizeof(*req)) < 0) return -1;
  61.         if (req->error) {
  62.                 errno = 0;
  63.                 return -1;
  64.         }
  65.  
  66.         return req->generation;
  67. }
  68.  
  69.  
  70. struct raw1394_handle *raw1394_get_handle(void)
  71. {
  72.         struct raw1394_handle *handle;
  73.  
  74.         handle = malloc(sizeof(struct raw1394_handle));
  75.         if (!handle) {
  76.                 errno = ENOMEM;
  77.                 return NULL;
  78.         }
  79.  
  80.         handle->fd = open("/dev/raw1394", O_RDWR);
  81.         if (handle->fd < 0) {
  82.                 free(handle);
  83.                 return NULL;
  84.         }
  85.  
  86.         handle->generation = init_rawdevice(handle);
  87.         if (handle->generation < 0) {
  88.                 close(handle->fd);
  89.                 free(handle);
  90.                 return NULL;
  91.         }
  92.  
  93.         handle->bus_reset_handler = bus_reset_default;
  94.         handle->tag_handler = tag_handler_default;
  95.         memset(handle->iso_handler, 0, sizeof(handle->iso_handler));
  96.         return handle;
  97. }
  98.  
  99. void raw1394_destroy_handle(struct raw1394_handle *handle)
  100. {
  101.         if (handle) {
  102.                 close(handle->fd);
  103.                 free(handle);
  104.         }
  105. }
  106.  
  107. int raw1394_get_fd(struct raw1394_handle *handle)
  108. {
  109.         return handle->fd;
  110. }
  111.  
  112. unsigned int raw1394_get_generation(struct raw1394_handle *handle)
  113. {
  114.         return handle->generation;
  115. }
  116.  
  117. int raw1394_get_nodecount(struct raw1394_handle *handle)
  118. {
  119.         return handle->num_of_nodes;
  120. }
  121.  
  122. nodeid_t raw1394_get_local_id(struct raw1394_handle *handle)
  123. {
  124.         return handle->local_id;
  125. }
  126.  
  127. void *raw1394_get_userdata(struct raw1394_handle *handle)
  128. {
  129.         return handle->userdata;
  130. }
  131.  
  132. void raw1394_set_userdata(struct raw1394_handle *handle, void *data)
  133. {
  134.         handle->userdata = data;
  135. }
  136.  
  137. int raw1394_get_port_info(struct raw1394_handle *handle, 
  138.                           struct raw1394_portinfo *pinf, int maxports)
  139. {
  140.         int num;
  141.         struct raw1394_request *req = &handle->req;
  142.         struct raw1394_khost_list *khl;
  143.  
  144.         CLEAR_REQ(req);
  145.         req->type = RAW1394_REQ_LIST_CARDS;
  146.         req->generation = handle->generation;
  147.         req->recvb = (__u64)handle->buffer;
  148.         req->length = HBUF_SIZE;
  149.  
  150.         while (1) {
  151.                 if (write(handle->fd, req, sizeof(*req)) < 0) return -1;
  152.                 if (read(handle->fd, req, sizeof(*req)) < 0) return -1;
  153.  
  154.                 if (!req->error) break;
  155.  
  156.                 if (req->error == RAW1394_ERROR_GENERATION) {
  157.                         handle->generation = req->generation;
  158.                         continue;
  159.                 }
  160.  
  161.                 return -1;
  162.         }
  163.  
  164.         for (num = req->misc, khl = (struct raw1394_khost_list *)handle->buffer;
  165.              num && maxports; num--, maxports--, pinf++, khl++) {
  166.                 pinf->nodes = khl->nodes;
  167.                 strcpy(pinf->name, khl->name);
  168.         }
  169.  
  170.         return req->misc;
  171. }
  172.  
  173. int raw1394_set_port(struct raw1394_handle *handle, int port)
  174. {
  175.         struct raw1394_request *req = &handle->req;
  176.  
  177.         CLEAR_REQ(req);
  178.  
  179.         req->type = RAW1394_REQ_SET_CARD;
  180.         req->generation = handle->generation;
  181.         req->misc = port;
  182.  
  183.         if (write(handle->fd, req, sizeof(*req)) < 0) return -1;
  184.         if (read(handle->fd, req, sizeof(*req)) < 0) return -1;
  185.  
  186.         switch (req->error) {
  187.         case RAW1394_ERROR_GENERATION:
  188.                 handle->generation = req->generation;
  189.                 errno = ESTALE;
  190.                 return -1;
  191.         case RAW1394_ERROR_INVALID_ARG:
  192.                 errno = EINVAL;
  193.                 return -1;
  194.         case RAW1394_ERROR_NONE:
  195.                 handle->num_of_nodes = req->misc & 0xffff;
  196.                 handle->local_id = req->misc >> 16;
  197.                 return 0;
  198.         default:
  199.                 errno = 0;
  200.                 return -1;
  201.         }
  202. }
  203.