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