karma.c 5.4 KB

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