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