karma.c 6.0 KB

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