1
0

u3_scsi_sg.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #if HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #ifdef SUBSYS_SG
  23. #include "u3_scsi.h"
  24. #include "u3_error.h"
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/ioctl.h>
  28. #include <stdlib.h>
  29. #include <fcntl.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <errno.h>
  33. #include <linux/cdrom.h>
  34. #include <scsi/sg.h>
  35. #include "sg_err.h"
  36. #define U3_TIMEOUT 2000 //2000 millisecs == 2 seconds
  37. const char *u3_subsystem_name = "sg";
  38. const char *u3_subsystem_help = "'/dev/sda0', '/dev/sg3'";
  39. int u3_open(u3_handle_t *device, const char *which)
  40. {
  41. int k;
  42. int sg_fd;
  43. u3_set_error(device, "");
  44. device->dev = NULL;
  45. if ((sg_fd = open(which, O_RDWR)) < 0) {
  46. // Nautilus sends a eject ioctl to the device when unmounting.
  47. // Just have to close the tray ;-)
  48. if (errno == ENOMEDIUM) {
  49. if ((sg_fd = open(which, O_RDONLY | O_NONBLOCK)) < 0) {
  50. u3_set_error(device, "%s", strerror(errno));
  51. return U3_FAILURE;
  52. }
  53. ioctl(sg_fd, CDROMCLOSETRAY);
  54. close(sg_fd);
  55. if ((sg_fd = open(which, O_RDWR)) < 0) {
  56. u3_set_error(device, "%s", strerror(errno));
  57. return U3_FAILURE;
  58. }
  59. } else {
  60. u3_set_error(device, "%s", strerror(errno));
  61. return U3_FAILURE;
  62. }
  63. }
  64. // It is prudent to check we have a sg device by trying an ioctl
  65. if (ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) {
  66. close(sg_fd);
  67. if (k < 30000) {
  68. u3_set_error(device, "device is not an SG device");
  69. } else {
  70. u3_set_error(device, "SG driver to old");
  71. }
  72. return U3_FAILURE;
  73. }
  74. if ((device->dev = malloc(sizeof(int))) == NULL) {
  75. close(sg_fd);
  76. u3_set_error(device, "Failed allocating memory for file descriptor");
  77. return U3_FAILURE;
  78. }
  79. *((int *)device->dev) = sg_fd;
  80. return U3_SUCCESS;
  81. }
  82. void u3_close(u3_handle_t *device)
  83. {
  84. int *sg_fd = (int *)device->dev;
  85. close(*sg_fd);
  86. }
  87. int u3_send_cmd(u3_handle_t *device, uint8_t cmd[U3_CMD_LEN],
  88. int dxfer_direction, int dxfer_length, uint8_t *dxfer_data,
  89. uint8_t *status)
  90. {
  91. sg_io_hdr_t io_hdr;
  92. unsigned char sense_buf[32];
  93. int *sg_fd = (int *)device->dev;
  94. // translate dxfer_direction
  95. switch (dxfer_direction) {
  96. case U3_DATA_NONE:
  97. dxfer_direction = SG_DXFER_NONE;
  98. break;
  99. case U3_DATA_TO_DEV:
  100. dxfer_direction = SG_DXFER_TO_DEV;
  101. break;
  102. case U3_DATA_FROM_DEV:
  103. dxfer_direction = SG_DXFER_FROM_DEV;
  104. break;
  105. }
  106. // Prepare command
  107. memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
  108. io_hdr.interface_id = 'S'; // fixed
  109. io_hdr.cmd_len = U3_CMD_LEN; // length of command in bytes
  110. // io_hdr.iovec_count = 0; // don't use iovector stuff
  111. io_hdr.mx_sb_len = sizeof(sense_buf); // sense buffer size. do we use this???
  112. io_hdr.dxfer_direction = dxfer_direction; // send data to device
  113. io_hdr.dxfer_len = dxfer_length; // Size of data transfered
  114. io_hdr.dxferp = dxfer_data; // Data buffer to transfer
  115. io_hdr.cmdp = cmd; // Command buffer to execute
  116. io_hdr.sbp = sense_buf; // Sense buffer
  117. io_hdr.timeout = U3_TIMEOUT; // timeout
  118. // io_hdr.flags = 0; // take defaults: indirect IO, etc
  119. // io_hdr.pack_id = 0; // internal packet. used by te program to recognize packets
  120. // io_hdr.usr_ptr = NULL; // user data
  121. // preform ioctl on device
  122. if (ioctl(*sg_fd, SG_IO, &io_hdr) < 0) {
  123. u3_set_error(device, "Failed executing scsi command: "
  124. "SG_IO ioctl Failed");
  125. return U3_FAILURE;
  126. }
  127. // evaluate result
  128. if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
  129. if (io_hdr.host_status == SG_ERR_DID_OK &&
  130. (io_hdr.driver_status & SG_ERR_DRIVER_SENSE))
  131. {
  132. //
  133. // The usb-storage driver automatically request sense
  134. // data if a command fails. So this state isn't really
  135. // a error but only indicates that the sense data in
  136. // the buffer is fresh. Currently we aren't don't use
  137. // this sense data, we only wanna know if a command
  138. // failed or not. so ignore this...
  139. //
  140. } else {
  141. u3_set_error(device, "Failed executing scsi command: "
  142. "Status (S:0x%x,H:0x%x,D:0x%x)", io_hdr.status,
  143. io_hdr.host_status, io_hdr.driver_status);
  144. return U3_FAILURE;
  145. }
  146. }
  147. *status = io_hdr.masked_status;
  148. return U3_SUCCESS;
  149. }
  150. #endif //SUBSYS_SG