karma.c 5.9 KB

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