karma.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Rio Karma
  4. *
  5. * (c) 2006 Bob Copeland <me@bobcopeland.com>
  6. * (c) 2006 Keith Bennett <keith@mcs.st-and.ac.uk>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <scsi/scsi.h>
  11. #include <scsi/scsi_cmnd.h>
  12. #include <scsi/scsi_device.h>
  13. #include "usb.h"
  14. #include "transport.h"
  15. #include "debug.h"
  16. #include "scsiglue.h"
  17. #define DRV_NAME "ums-karma"
  18. MODULE_DESCRIPTION("Driver for Rio Karma");
  19. MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>, Keith Bennett <keith@mcs.st-and.ac.uk>");
  20. MODULE_LICENSE("GPL");
  21. MODULE_IMPORT_NS(USB_STORAGE);
  22. #define RIO_PREFIX "RIOP\x00"
  23. #define RIO_PREFIX_LEN 5
  24. #define RIO_SEND_LEN 40
  25. #define RIO_RECV_LEN 0x200
  26. #define RIO_ENTER_STORAGE 0x1
  27. #define RIO_LEAVE_STORAGE 0x2
  28. #define RIO_RESET 0xC
  29. struct karma_data {
  30. int in_storage;
  31. char *recv;
  32. };
  33. static int rio_karma_init(struct us_data *us);
  34. /*
  35. * The table of devices
  36. */
  37. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  38. vendorName, productName, useProtocol, useTransport, \
  39. initFunction, flags) \
  40. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  41. .driver_info = (flags) }
  42. static struct usb_device_id karma_usb_ids[] = {
  43. # include "unusual_karma.h"
  44. { } /* Terminating entry */
  45. };
  46. MODULE_DEVICE_TABLE(usb, karma_usb_ids);
  47. #undef UNUSUAL_DEV
  48. /*
  49. * The flags table
  50. */
  51. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  52. vendor_name, product_name, use_protocol, use_transport, \
  53. init_function, Flags) \
  54. { \
  55. .vendorName = vendor_name, \
  56. .productName = product_name, \
  57. .useProtocol = use_protocol, \
  58. .useTransport = use_transport, \
  59. .initFunction = init_function, \
  60. }
  61. static struct us_unusual_dev karma_unusual_dev_list[] = {
  62. # include "unusual_karma.h"
  63. { } /* Terminating entry */
  64. };
  65. #undef UNUSUAL_DEV
  66. /*
  67. * Send commands to Rio Karma.
  68. *
  69. * For each command we send 40 bytes starting 'RIOP\0' followed by
  70. * the command number and a sequence number, which the device will ack
  71. * with a 512-byte packet with the high four bits set and everything
  72. * else null. Then we send 'RIOP\x80' followed by a zero and the
  73. * sequence number, until byte 5 in the response repeats the sequence
  74. * number.
  75. */
  76. static int rio_karma_send_command(char cmd, struct us_data *us)
  77. {
  78. int result;
  79. unsigned long timeout;
  80. static unsigned char seq = 1;
  81. struct karma_data *data = (struct karma_data *) us->extra;
  82. usb_stor_dbg(us, "sending command %04x\n", cmd);
  83. memset(us->iobuf, 0, RIO_SEND_LEN);
  84. memcpy(us->iobuf, RIO_PREFIX, RIO_PREFIX_LEN);
  85. us->iobuf[5] = cmd;
  86. us->iobuf[6] = seq;
  87. timeout = jiffies + msecs_to_jiffies(6000);
  88. for (;;) {
  89. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  90. us->iobuf, RIO_SEND_LEN, NULL);
  91. if (result != USB_STOR_XFER_GOOD)
  92. goto err;
  93. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  94. data->recv, RIO_RECV_LEN, NULL);
  95. if (result != USB_STOR_XFER_GOOD)
  96. goto err;
  97. if (data->recv[5] == seq)
  98. break;
  99. if (time_after(jiffies, timeout))
  100. goto err;
  101. us->iobuf[4] = 0x80;
  102. us->iobuf[5] = 0;
  103. msleep(50);
  104. }
  105. seq++;
  106. if (seq == 0)
  107. seq = 1;
  108. usb_stor_dbg(us, "sent command %04x\n", cmd);
  109. return 0;
  110. err:
  111. usb_stor_dbg(us, "command %04x failed\n", cmd);
  112. return USB_STOR_TRANSPORT_FAILED;
  113. }
  114. /*
  115. * Trap START_STOP and READ_10 to leave/re-enter storage mode.
  116. * Everything else is propagated to the normal bulk layer.
  117. */
  118. static int rio_karma_transport(struct scsi_cmnd *srb, struct us_data *us)
  119. {
  120. int ret;
  121. struct karma_data *data = (struct karma_data *) us->extra;
  122. if (srb->cmnd[0] == READ_10 && !data->in_storage) {
  123. ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
  124. if (ret)
  125. return ret;
  126. data->in_storage = 1;
  127. return usb_stor_Bulk_transport(srb, us);
  128. } else if (srb->cmnd[0] == START_STOP) {
  129. ret = rio_karma_send_command(RIO_LEAVE_STORAGE, us);
  130. if (ret)
  131. return ret;
  132. data->in_storage = 0;
  133. return rio_karma_send_command(RIO_RESET, us);
  134. }
  135. return usb_stor_Bulk_transport(srb, us);
  136. }
  137. static void rio_karma_destructor(void *extra)
  138. {
  139. struct karma_data *data = (struct karma_data *) extra;
  140. kfree(data->recv);
  141. }
  142. static int rio_karma_init(struct us_data *us)
  143. {
  144. int ret = 0;
  145. struct karma_data *data = kzalloc(sizeof(struct karma_data), GFP_NOIO);
  146. if (!data)
  147. goto out;
  148. data->recv = kmalloc(RIO_RECV_LEN, GFP_NOIO);
  149. if (!data->recv) {
  150. kfree(data);
  151. goto out;
  152. }
  153. us->extra = data;
  154. us->extra_destructor = rio_karma_destructor;
  155. ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
  156. data->in_storage = (ret == 0);
  157. out:
  158. return ret;
  159. }
  160. static struct scsi_host_template karma_host_template;
  161. static int karma_probe(struct usb_interface *intf,
  162. const struct usb_device_id *id)
  163. {
  164. struct us_data *us;
  165. int result;
  166. result = usb_stor_probe1(&us, intf, id,
  167. (id - karma_usb_ids) + karma_unusual_dev_list,
  168. &karma_host_template);
  169. if (result)
  170. return result;
  171. us->transport_name = "Rio Karma/Bulk";
  172. us->transport = rio_karma_transport;
  173. us->transport_reset = usb_stor_Bulk_reset;
  174. result = usb_stor_probe2(us);
  175. return result;
  176. }
  177. static struct usb_driver karma_driver = {
  178. .name = DRV_NAME,
  179. .probe = karma_probe,
  180. .disconnect = usb_stor_disconnect,
  181. .suspend = usb_stor_suspend,
  182. .resume = usb_stor_resume,
  183. .reset_resume = usb_stor_reset_resume,
  184. .pre_reset = usb_stor_pre_reset,
  185. .post_reset = usb_stor_post_reset,
  186. .id_table = karma_usb_ids,
  187. .soft_unbind = 1,
  188. .no_dynamic_id = 1,
  189. };
  190. module_usb_stor_driver(karma_driver, karma_host_template, DRV_NAME);