karma.c 6.0 KB

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