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