1 // SPDX-License-Identifier: GPL-2.0+
9 #include <linux/module.h>
10 #include <linux/slab.h>
12 #include <scsi/scsi.h>
13 #include <scsi/scsi_cmnd.h>
14 #include <scsi/scsi_device.h>
17 #include "transport.h"
21 #define DRV_NAME "ums-karma"
23 MODULE_DESCRIPTION("Driver for Rio Karma");
25 MODULE_LICENSE("GPL");
26 MODULE_IMPORT_NS("USB_STORAGE");
28 #define RIO_PREFIX "RIOP\x00"
29 #define RIO_PREFIX_LEN 5
30 #define RIO_SEND_LEN 40
31 #define RIO_RECV_LEN 0x200
33 #define RIO_ENTER_STORAGE 0x1
34 #define RIO_LEAVE_STORAGE 0x2
42 static int rio_karma_init(struct us_data *us);
46 * The table of devices
48 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
49 vendorName, productName, useProtocol, useTransport, \
50 initFunction, flags) \
51 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
52 .driver_info = (flags) }
54 static const struct usb_device_id karma_usb_ids[] = {
55 # include "unusual_karma.h"
56 { } /* Terminating entry */
58 MODULE_DEVICE_TABLE(usb, karma_usb_ids);
65 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
66 vendor_name, product_name, use_protocol, use_transport, \
67 init_function, Flags) \
69 .vendorName = vendor_name, \
70 .productName = product_name, \
71 .useProtocol = use_protocol, \
72 .useTransport = use_transport, \
73 .initFunction = init_function, \
76 static const struct us_unusual_dev karma_unusual_dev_list[] = {
77 # include "unusual_karma.h"
78 { } /* Terminating entry */
85 * Send commands to Rio Karma.
87 * For each command we send 40 bytes starting 'RIOP\0' followed by
88 * the command number and a sequence number, which the device will ack
89 * with a 512-byte packet with the high four bits set and everything
90 * else null. Then we send 'RIOP\x80' followed by a zero and the
91 * sequence number, until byte 5 in the response repeats the sequence
94 static int rio_karma_send_command(char cmd, struct us_data *us)
97 unsigned long timeout;
98 static unsigned char seq = 1;
99 struct karma_data *data = (struct karma_data *) us->extra;
101 usb_stor_dbg(us, "sending command %04x\n", cmd);
102 memset(us->iobuf, 0, RIO_SEND_LEN);
103 memcpy(us->iobuf, RIO_PREFIX, RIO_PREFIX_LEN);
107 timeout = jiffies + msecs_to_jiffies(6000);
109 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
110 us->iobuf, RIO_SEND_LEN, NULL);
111 if (result != USB_STOR_XFER_GOOD)
114 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
115 data->recv, RIO_RECV_LEN, NULL);
116 if (result != USB_STOR_XFER_GOOD)
119 if (data->recv[5] == seq)
122 if (time_after(jiffies, timeout))
134 usb_stor_dbg(us, "sent command %04x\n", cmd);
137 usb_stor_dbg(us, "command %04x failed\n", cmd);
138 return USB_STOR_TRANSPORT_FAILED;
142 * Trap START_STOP and READ_10 to leave/re-enter storage mode.
143 * Everything else is propagated to the normal bulk layer.
145 static int rio_karma_transport(struct scsi_cmnd *srb, struct us_data *us)
148 struct karma_data *data = (struct karma_data *) us->extra;
150 if (srb->cmnd[0] == READ_10 && !data->in_storage) {
151 ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
155 data->in_storage = 1;
156 return usb_stor_Bulk_transport(srb, us);
157 } else if (srb->cmnd[0] == START_STOP) {
158 ret = rio_karma_send_command(RIO_LEAVE_STORAGE, us);
162 data->in_storage = 0;
163 return rio_karma_send_command(RIO_RESET, us);
165 return usb_stor_Bulk_transport(srb, us);
168 static void rio_karma_destructor(void *extra)
170 struct karma_data *data = (struct karma_data *) extra;
175 static int rio_karma_init(struct us_data *us)
177 struct karma_data *data = kzalloc(sizeof(struct karma_data), GFP_NOIO);
182 data->recv = kmalloc(RIO_RECV_LEN, GFP_NOIO);
189 us->extra_destructor = rio_karma_destructor;
190 if (rio_karma_send_command(RIO_ENTER_STORAGE, us))
193 data->in_storage = 1;
198 static struct scsi_host_template karma_host_template;
200 static int karma_probe(struct usb_interface *intf,
201 const struct usb_device_id *id)
206 result = usb_stor_probe1(&us, intf, id,
207 (id - karma_usb_ids) + karma_unusual_dev_list,
208 &karma_host_template);
212 us->transport_name = "Rio Karma/Bulk";
213 us->transport = rio_karma_transport;
214 us->transport_reset = usb_stor_Bulk_reset;
216 result = usb_stor_probe2(us);
220 static struct usb_driver karma_driver = {
222 .probe = karma_probe,
223 .disconnect = usb_stor_disconnect,
224 .suspend = usb_stor_suspend,
225 .resume = usb_stor_resume,
226 .reset_resume = usb_stor_reset_resume,
227 .pre_reset = usb_stor_pre_reset,
228 .post_reset = usb_stor_post_reset,
229 .id_table = karma_usb_ids,
234 module_usb_stor_driver(karma_driver, karma_host_template, DRV_NAME);