1
0

u3_scsi_sg.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * u3_tool - U3 USB stick manager
  3. * Copyright (C) 2007 Daviedev, daviedev@users.sourceforge.net
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "u3_scsi.h"
  20. #include "u3_error.h"
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/ioctl.h>
  24. #include <stdlib.h>
  25. #include <fcntl.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <linux/cdrom.h>
  30. #include <scsi/sg.h>
  31. #include "sg_err.h"
  32. #define U3_TIMEOUT 2000 //2000 millisecs == 2 seconds
  33. int u3_open(u3_handle_t *device, const char *which)
  34. {
  35. int k;
  36. int sg_fd;
  37. u3_set_error(device, "");
  38. device->dev = NULL;
  39. if ((sg_fd = open(which, O_RDWR)) < 0) {
  40. // Nautilus sends a eject ioctl to the device when unmounting.
  41. // Just have to close the tray ;-)
  42. if (errno == ENOMEDIUM) {
  43. if ((sg_fd = open(which, O_RDONLY | O_NONBLOCK)) < 0) {
  44. u3_set_error(device, "%s", strerror(errno));
  45. return U3_FAILURE;
  46. }
  47. ioctl(sg_fd, CDROMCLOSETRAY);
  48. close(sg_fd);
  49. if ((sg_fd = open(which, O_RDWR)) < 0) {
  50. u3_set_error(device, "%s", strerror(errno));
  51. return U3_FAILURE;
  52. }
  53. } else {
  54. u3_set_error(device, "%s", strerror(errno));
  55. return U3_FAILURE;
  56. }
  57. }
  58. // It is prudent to check we have a sg device by trying an ioctl
  59. if (ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) {
  60. close(sg_fd);
  61. if (k < 30000) {
  62. u3_set_error(device, "device is not an SG device");
  63. } else {
  64. u3_set_error(device, "SG driver to old");
  65. }
  66. return U3_FAILURE;
  67. }
  68. if ((device->dev = malloc(sizeof(int))) == NULL) {
  69. close(sg_fd);
  70. u3_set_error(device, "Failed allocating memory for file descriptor");
  71. return U3_FAILURE;
  72. }
  73. *((int *)device->dev) = sg_fd;
  74. return U3_SUCCESS;
  75. }
  76. void u3_close(u3_handle_t *device)
  77. {
  78. int *sg_fd = (int *)device->dev;
  79. close(*sg_fd);
  80. }
  81. int u3_send_cmd(u3_handle_t *device, uint8_t cmd[U3_CMD_LEN],
  82. int dxfer_direction, int dxfer_length, uint8_t *dxfer_data,
  83. uint8_t *status)
  84. {
  85. sg_io_hdr_t io_hdr;
  86. unsigned char sense_buf[32];
  87. int *sg_fd = (int *)device->dev;
  88. // translate dxfer_direction
  89. switch (dxfer_direction) {
  90. case U3_DATA_NONE:
  91. dxfer_direction = SG_DXFER_NONE;
  92. break;
  93. case U3_DATA_TO_DEV:
  94. dxfer_direction = SG_DXFER_TO_DEV;
  95. break;
  96. case U3_DATA_FROM_DEV:
  97. dxfer_direction = SG_DXFER_FROM_DEV;
  98. break;
  99. }
  100. // Prepare command
  101. memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
  102. io_hdr.interface_id = 'S'; // fixed
  103. io_hdr.cmd_len = U3_CMD_LEN; // length of command in bytes
  104. // io_hdr.iovec_count = 0; // don't use iovector stuff
  105. io_hdr.mx_sb_len = sizeof(sense_buf); // sense buffer size. do we use this???
  106. io_hdr.dxfer_direction = dxfer_direction; // send data to device
  107. io_hdr.dxfer_len = dxfer_length; // Size of data transfered
  108. io_hdr.dxferp = dxfer_data; // Data buffer to transfer
  109. io_hdr.cmdp = cmd; // Command buffer to execute
  110. io_hdr.sbp = sense_buf; // Sense buffer
  111. io_hdr.timeout = U3_TIMEOUT; // timeout
  112. // io_hdr.flags = 0; // take defaults: indirect IO, etc
  113. // io_hdr.pack_id = 0; // internal packet. used by te program to recognize packets
  114. // io_hdr.usr_ptr = NULL; // user data
  115. // preform ioctl on device
  116. if (ioctl(*sg_fd, SG_IO, &io_hdr) < 0) {
  117. u3_set_error(device, "Failed executing scsi command: "
  118. "SG_IO ioctl Failed");
  119. return U3_FAILURE;
  120. }
  121. // evaluate result
  122. if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
  123. if (io_hdr.host_status == SG_ERR_DID_OK &&
  124. (io_hdr.driver_status & SG_ERR_DRIVER_SENSE))
  125. {
  126. //
  127. // The usb-storage driver automatically request sense
  128. // data if a command fails. So this state isn't really
  129. // a error but only indicates that the sense data in
  130. // the buffer is fresh. Currently we aren't don't use
  131. // this sense data, we only wanna know if a command
  132. // failed or not. so ignore this...
  133. //
  134. } else {
  135. u3_set_error(device, "Failed executing scsi command: "
  136. "Status (S:0x%x,H:0x%x,D:0x%x)", io_hdr.status,
  137. io_hdr.host_status, io_hdr.driver_status);
  138. return U3_FAILURE;
  139. }
  140. }
  141. *status = io_hdr.masked_status;
  142. return U3_SUCCESS;
  143. }