karma.c 6.0 KB

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