1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Driver for Rio Karma 4 * 5 * (c) 2006 Bob Copeland <me@bobcopeland.com> 6 * (c) 2006 Keith Bennett <keith@mcs.st-and.ac.uk> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2, or (at your option) any 11 * later version. 12 * 13 * This program is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License along 19 * with this program; if not, write to the Free Software Foundation, Inc., 20 * 675 Mass Ave, Cambridge, MA 02139, USA. 21 */ 22 23 #include <linux/module.h> 24 #include <linux/slab.h> 25 26 #include <scsi/scsi.h> 27 #include <scsi/scsi_cmnd.h> 28 #include <scsi/scsi_device.h> 29 30 #include "usb.h" 31 #include "transport.h" 32 #include "debug.h" 33 #include "scsiglue.h" 34 35 #define DRV_NAME "ums-karma" 36 37 MODULE_DESCRIPTION("Driver for Rio Karma"); 38 MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>, Keith Bennett <keith@mcs.st-and.ac.uk>"); 39 MODULE_LICENSE("GPL"); 40 41 #define RIO_PREFIX "RIOP\x00" 42 #define RIO_PREFIX_LEN 5 43 #define RIO_SEND_LEN 40 44 #define RIO_RECV_LEN 0x200 45 46 #define RIO_ENTER_STORAGE 0x1 47 #define RIO_LEAVE_STORAGE 0x2 48 #define RIO_RESET 0xC 49 50 struct karma_data { 51 int in_storage; 52 char *recv; 53 }; 54 55 static int rio_karma_init(struct us_data *us); 56 57 58 /* 59 * The table of devices 60 */ 61 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ 62 vendorName, productName, useProtocol, useTransport, \ 63 initFunction, flags) \ 64 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ 65 .driver_info = (flags) } 66 67 static struct usb_device_id karma_usb_ids[] = { 68 # include "unusual_karma.h" 69 { } /* Terminating entry */ 70 }; 71 MODULE_DEVICE_TABLE(usb, karma_usb_ids); 72 73 #undef UNUSUAL_DEV 74 75 /* 76 * The flags table 77 */ 78 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ 79 vendor_name, product_name, use_protocol, use_transport, \ 80 init_function, Flags) \ 81 { \ 82 .vendorName = vendor_name, \ 83 .productName = product_name, \ 84 .useProtocol = use_protocol, \ 85 .useTransport = use_transport, \ 86 .initFunction = init_function, \ 87 } 88 89 static struct us_unusual_dev karma_unusual_dev_list[] = { 90 # include "unusual_karma.h" 91 { } /* Terminating entry */ 92 }; 93 94 #undef UNUSUAL_DEV 95 96 97 /* 98 * Send commands to Rio Karma. 99 * 100 * For each command we send 40 bytes starting 'RIOP\0' followed by 101 * the command number and a sequence number, which the device will ack 102 * with a 512-byte packet with the high four bits set and everything 103 * else null. Then we send 'RIOP\x80' followed by a zero and the 104 * sequence number, until byte 5 in the response repeats the sequence 105 * number. 106 */ 107 static int rio_karma_send_command(char cmd, struct us_data *us) 108 { 109 int result; 110 unsigned long timeout; 111 static unsigned char seq = 1; 112 struct karma_data *data = (struct karma_data *) us->extra; 113 114 usb_stor_dbg(us, "sending command %04x\n", cmd); 115 memset(us->iobuf, 0, RIO_SEND_LEN); 116 memcpy(us->iobuf, RIO_PREFIX, RIO_PREFIX_LEN); 117 us->iobuf[5] = cmd; 118 us->iobuf[6] = seq; 119 120 timeout = jiffies + msecs_to_jiffies(6000); 121 for (;;) { 122 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 123 us->iobuf, RIO_SEND_LEN, NULL); 124 if (result != USB_STOR_XFER_GOOD) 125 goto err; 126 127 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 128 data->recv, RIO_RECV_LEN, NULL); 129 if (result != USB_STOR_XFER_GOOD) 130 goto err; 131 132 if (data->recv[5] == seq) 133 break; 134 135 if (time_after(jiffies, timeout)) 136 goto err; 137 138 us->iobuf[4] = 0x80; 139 us->iobuf[5] = 0; 140 msleep(50); 141 } 142 143 seq++; 144 if (seq == 0) 145 seq = 1; 146 147 usb_stor_dbg(us, "sent command %04x\n", cmd); 148 return 0; 149 err: 150 usb_stor_dbg(us, "command %04x failed\n", cmd); 151 return USB_STOR_TRANSPORT_FAILED; 152 } 153 154 /* 155 * Trap START_STOP and READ_10 to leave/re-enter storage mode. 156 * Everything else is propagated to the normal bulk layer. 157 */ 158 static int rio_karma_transport(struct scsi_cmnd *srb, struct us_data *us) 159 { 160 int ret; 161 struct karma_data *data = (struct karma_data *) us->extra; 162 163 if (srb->cmnd[0] == READ_10 && !data->in_storage) { 164 ret = rio_karma_send_command(RIO_ENTER_STORAGE, us); 165 if (ret) 166 return ret; 167 168 data->in_storage = 1; 169 return usb_stor_Bulk_transport(srb, us); 170 } else if (srb->cmnd[0] == START_STOP) { 171 ret = rio_karma_send_command(RIO_LEAVE_STORAGE, us); 172 if (ret) 173 return ret; 174 175 data->in_storage = 0; 176 return rio_karma_send_command(RIO_RESET, us); 177 } 178 return usb_stor_Bulk_transport(srb, us); 179 } 180 181 static void rio_karma_destructor(void *extra) 182 { 183 struct karma_data *data = (struct karma_data *) extra; 184 kfree(data->recv); 185 } 186 187 static int rio_karma_init(struct us_data *us) 188 { 189 int ret = 0; 190 struct karma_data *data = kzalloc(sizeof(struct karma_data), GFP_NOIO); 191 if (!data) 192 goto out; 193 194 data->recv = kmalloc(RIO_RECV_LEN, GFP_NOIO); 195 if (!data->recv) { 196 kfree(data); 197 goto out; 198 } 199 200 us->extra = data; 201 us->extra_destructor = rio_karma_destructor; 202 ret = rio_karma_send_command(RIO_ENTER_STORAGE, us); 203 data->in_storage = (ret == 0); 204 out: 205 return ret; 206 } 207 208 static struct scsi_host_template karma_host_template; 209 210 static int karma_probe(struct usb_interface *intf, 211 const struct usb_device_id *id) 212 { 213 struct us_data *us; 214 int result; 215 216 result = usb_stor_probe1(&us, intf, id, 217 (id - karma_usb_ids) + karma_unusual_dev_list, 218 &karma_host_template); 219 if (result) 220 return result; 221 222 us->transport_name = "Rio Karma/Bulk"; 223 us->transport = rio_karma_transport; 224 us->transport_reset = usb_stor_Bulk_reset; 225 226 result = usb_stor_probe2(us); 227 return result; 228 } 229 230 static struct usb_driver karma_driver = { 231 .name = DRV_NAME, 232 .probe = karma_probe, 233 .disconnect = usb_stor_disconnect, 234 .suspend = usb_stor_suspend, 235 .resume = usb_stor_resume, 236 .reset_resume = usb_stor_reset_resume, 237 .pre_reset = usb_stor_pre_reset, 238 .post_reset = usb_stor_post_reset, 239 .id_table = karma_usb_ids, 240 .soft_unbind = 1, 241 .no_dynamic_id = 1, 242 }; 243 244 module_usb_stor_driver(karma_driver, karma_host_template, DRV_NAME); 245