karma.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Driver for Rio Karma
  2. *
  3. * (c) 2006 Bob Copeland <me@bobcopeland.com>
  4. * (c) 2006 Keith Bennett <keith@mcs.st-and.ac.uk>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <scsi/scsi.h>
  21. #include <scsi/scsi_cmnd.h>
  22. #include <scsi/scsi_device.h>
  23. #include "usb.h"
  24. #include "transport.h"
  25. #include "debug.h"
  26. #include "karma.h"
  27. #define RIO_PREFIX "RIOP\x00"
  28. #define RIO_PREFIX_LEN 5
  29. #define RIO_SEND_LEN 40
  30. #define RIO_RECV_LEN 0x200
  31. #define RIO_ENTER_STORAGE 0x1
  32. #define RIO_LEAVE_STORAGE 0x2
  33. #define RIO_RESET 0xC
  34. extern int usb_stor_Bulk_transport(struct scsi_cmnd *, struct us_data *);
  35. struct karma_data {
  36. int in_storage;
  37. char *recv;
  38. };
  39. /*
  40. * Send commands to Rio Karma.
  41. *
  42. * For each command we send 40 bytes starting 'RIOP\0' followed by
  43. * the command number and a sequence number, which the device will ack
  44. * with a 512-byte packet with the high four bits set and everything
  45. * else null. Then we send 'RIOP\x80' followed by a zero and the
  46. * sequence number, until byte 5 in the response repeats the sequence
  47. * number.
  48. */
  49. static int rio_karma_send_command(char cmd, struct us_data *us)
  50. {
  51. int result, partial;
  52. unsigned long timeout;
  53. static unsigned char seq = 1;
  54. struct karma_data *data = (struct karma_data *) us->extra;
  55. US_DEBUGP("karma: sending command %04x\n", cmd);
  56. memset(us->iobuf, 0, RIO_SEND_LEN);
  57. memcpy(us->iobuf, RIO_PREFIX, RIO_PREFIX_LEN);
  58. us->iobuf[5] = cmd;
  59. us->iobuf[6] = seq;
  60. timeout = jiffies + msecs_to_jiffies(6000);
  61. for (;;) {
  62. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  63. us->iobuf, RIO_SEND_LEN, &partial);
  64. if (result != USB_STOR_XFER_GOOD)
  65. goto err;
  66. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  67. data->recv, RIO_RECV_LEN, &partial);
  68. if (result != USB_STOR_XFER_GOOD)
  69. goto err;
  70. if (data->recv[5] == seq)
  71. break;
  72. if (time_after(jiffies, timeout))
  73. goto err;
  74. us->iobuf[4] = 0x80;
  75. us->iobuf[5] = 0;
  76. msleep(50);
  77. }
  78. seq++;
  79. if (seq == 0)
  80. seq = 1;
  81. US_DEBUGP("karma: sent command %04x\n", cmd);
  82. return 0;
  83. err:
  84. US_DEBUGP("karma: command %04x failed\n", cmd);
  85. return USB_STOR_TRANSPORT_FAILED;
  86. }
  87. /*
  88. * Trap START_STOP and READ_10 to leave/re-enter storage mode.
  89. * Everything else is propagated to the normal bulk layer.
  90. */
  91. int rio_karma_transport(struct scsi_cmnd *srb, struct us_data *us)
  92. {
  93. int ret;
  94. struct karma_data *data = (struct karma_data *) us->extra;
  95. if (srb->cmnd[0] == READ_10 && !data->in_storage) {
  96. ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
  97. if (ret)
  98. return ret;
  99. data->in_storage = 1;
  100. return usb_stor_Bulk_transport(srb, us);
  101. } else if (srb->cmnd[0] == START_STOP) {
  102. ret = rio_karma_send_command(RIO_LEAVE_STORAGE, us);
  103. if (ret)
  104. return ret;
  105. data->in_storage = 0;
  106. return rio_karma_send_command(RIO_RESET, us);
  107. }
  108. return usb_stor_Bulk_transport(srb, us);
  109. }
  110. static void rio_karma_destructor(void *extra)
  111. {
  112. struct karma_data *data = (struct karma_data *) extra;
  113. kfree(data->recv);
  114. }
  115. int rio_karma_init(struct us_data *us)
  116. {
  117. int ret = 0;
  118. struct karma_data *data = kzalloc(sizeof(struct karma_data), GFP_NOIO);
  119. if (!data)
  120. goto out;
  121. data->recv = kmalloc(RIO_RECV_LEN, GFP_NOIO);
  122. if (!data->recv) {
  123. kfree(data);
  124. goto out;
  125. }
  126. us->extra = data;
  127. us->extra_destructor = rio_karma_destructor;
  128. ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
  129. data->in_storage = (ret == 0);
  130. out:
  131. return ret;
  132. }