Logo Search packages:      
Sourcecode: u3-tool version File versions

int u3_open ( u3_handle_t device,
const char *  which 
)

Open U3 device

This opens the device addressed by the variable 'which'.

Parameters:
device pointer to U3 handle that will be used to access the newly openned device.
Returns:
U3_SUCCESS if successful, else U3_FAILURE and an error string can be obtained using u3_error()

Definition at line 38 of file u3_scsi_sg.c.

References u3_handle::dev, and u3_open().

Referenced by u3_open().

{
      int k;
      int sg_fd;

      u3_set_error(device, "");
      device->dev = NULL;

      if ((sg_fd = open(which, O_RDWR)) < 0) {
            // Nautilus sends a eject ioctl to the device when unmounting.
            // Just have to close the tray ;-)
            if (errno == ENOMEDIUM) {
                  if ((sg_fd = open(which, O_RDONLY | O_NONBLOCK)) < 0) {
                        u3_set_error(device, "%s", strerror(errno));
                        return U3_FAILURE;
                  }
                  ioctl(sg_fd, CDROMCLOSETRAY);
                  close(sg_fd);
                  if ((sg_fd = open(which, O_RDWR)) < 0) {
                        u3_set_error(device, "%s", strerror(errno));
                        return U3_FAILURE;
                  }
            } else {
                  u3_set_error(device, "%s", strerror(errno));
                  return U3_FAILURE;
            }
      }

      // It is prudent to check we have a sg device by trying an ioctl
      if (ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) {
            close(sg_fd);
            if (k < 30000) {
                  u3_set_error(device, "device is not an SG device");
            } else {
                  u3_set_error(device, "SG driver to old");
            }
            return U3_FAILURE;
      }


      if ((device->dev = malloc(sizeof(int))) == NULL) {
            close(sg_fd);
            u3_set_error(device, "Failed allocating memory for file descriptor");
            return U3_FAILURE;
      }
            
      *((int *)device->dev) = sg_fd;
      return U3_SUCCESS;
}


Generated by  Doxygen 1.6.0   Back to index