karma.c 5.8 KB

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