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