u3_scsi_debug.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <stdio.h>
  30. #include "sg_err.h"
  31. #define U3_TIMEOUT 2000 //2000 millisecs == 2 seconds
  32. int u3_open(u3_handle_t *device, const char *which)
  33. {
  34. FILE *fp;
  35. u3_set_error(device, "");
  36. device->dev = NULL;
  37. if (strcmp(which, "stdout") == 0) {
  38. fp = stdout;
  39. } else if (strcmp(which, "stderr") == 0) {
  40. fp = stderr;
  41. } else {
  42. u3_set_error(device, "unknown output '%s', 'stdout' and "
  43. "'stderr' only supported", which);
  44. return U3_FAILURE;
  45. }
  46. device->dev = fp;
  47. return U3_SUCCESS;
  48. }
  49. void u3_close(u3_handle_t *device)
  50. {
  51. FILE *fp = (FILE *)(device->dev);
  52. // if (fp != stdout && fp != stderr) {
  53. // fclose(fp);
  54. // }
  55. }
  56. int u3_send_cmd(u3_handle_t *device, uint8_t cmd[U3_CMD_LEN],
  57. int dxfer_direction, int dxfer_length, uint8_t *dxfer_data,
  58. uint8_t *status)
  59. {
  60. FILE *fp = (FILE *)device->dev;
  61. int i;
  62. fprintf(fp, "---------------------------------------------------------"
  63. "-----------------------\n");
  64. fprintf(fp, "Command block:\n");
  65. for (i=0; i < U3_CMD_LEN; i++) {
  66. fprintf(fp, "%.2X ", cmd[i]);
  67. }
  68. fprintf(fp, "\n");
  69. fprintf(fp, "\n");
  70. switch (dxfer_direction) {
  71. case U3_DATA_NONE:
  72. fprintf(fp, "No data\n");
  73. break;
  74. case U3_DATA_TO_DEV:
  75. fprintf(fp, "Sending %d bytes of data to device\n", dxfer_length);
  76. fprintf(fp, "Data:");
  77. for (i=0; i < dxfer_length; i++) {
  78. if (i % 8 == 0) fprintf(fp, "\n%.4x\t", i);
  79. fprintf(fp, "%.2x ", dxfer_data[i]);
  80. }
  81. fprintf(fp, "\n");
  82. break;
  83. case U3_DATA_FROM_DEV:
  84. fprintf(fp, "Reading %d bytes of data from device\n", dxfer_length);
  85. memset(dxfer_data, 0, dxfer_length);
  86. break;
  87. }
  88. fprintf(fp, "---------------------------------------------------------"
  89. "-----------------------\n");
  90. *status = 0;
  91. return U3_SUCCESS;
  92. }