1 /* Driver for Rio Karma
3 * (c) 2006 Bob Copeland <me@bobcopeland.com>
4 * (c) 2006 Keith Bennett <keith@mcs.st-and.ac.uk>
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
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.
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.
21 #include <linux/module.h>
23 #include <scsi/scsi.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_device.h>
28 #include "transport.h"
31 #define RIO_PREFIX "RIOP\x00"
32 #define RIO_PREFIX_LEN 5
33 #define RIO_SEND_LEN 40
34 #define RIO_RECV_LEN 0x200
36 #define RIO_ENTER_STORAGE 0x1
37 #define RIO_LEAVE_STORAGE 0x2
45 static int rio_karma_init(struct us_data *us);
49 * The table of devices
51 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
52 vendorName, productName, useProtocol, useTransport, \
53 initFunction, flags) \
54 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
55 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
57 struct usb_device_id karma_usb_ids[] = {
58 # include "unusual_karma.h"
59 { } /* Terminating entry */
61 MODULE_DEVICE_TABLE(usb, karma_usb_ids);
68 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
69 vendor_name, product_name, use_protocol, use_transport, \
70 init_function, Flags) \
72 .vendorName = vendor_name, \
73 .productName = product_name, \
74 .useProtocol = use_protocol, \
75 .useTransport = use_transport, \
76 .initFunction = init_function, \
79 static struct us_unusual_dev karma_unusual_dev_list[] = {
80 # include "unusual_karma.h"
81 { } /* Terminating entry */
88 * Send commands to Rio Karma.
90 * For each command we send 40 bytes starting 'RIOP\0' followed by
91 * the command number and a sequence number, which the device will ack
92 * with a 512-byte packet with the high four bits set and everything
93 * else null. Then we send 'RIOP\x80' followed by a zero and the
94 * sequence number, until byte 5 in the response repeats the sequence
97 static int rio_karma_send_command(char cmd, struct us_data *us)
100 unsigned long timeout;
101 static unsigned char seq = 1;
102 struct karma_data *data = (struct karma_data *) us->extra;
104 US_DEBUGP("karma: sending command %04x\n", cmd);
105 memset(us->iobuf, 0, RIO_SEND_LEN);
106 memcpy(us->iobuf, RIO_PREFIX, RIO_PREFIX_LEN);
110 timeout = jiffies + msecs_to_jiffies(6000);
112 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
113 us->iobuf, RIO_SEND_LEN, &partial);
114 if (result != USB_STOR_XFER_GOOD)
117 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
118 data->recv, RIO_RECV_LEN, &partial);
119 if (result != USB_STOR_XFER_GOOD)
122 if (data->recv[5] == seq)
125 if (time_after(jiffies, timeout))
137 US_DEBUGP("karma: sent command %04x\n", cmd);
140 US_DEBUGP("karma: command %04x failed\n", cmd);
141 return USB_STOR_TRANSPORT_FAILED;
145 * Trap START_STOP and READ_10 to leave/re-enter storage mode.
146 * Everything else is propagated to the normal bulk layer.
148 static int rio_karma_transport(struct scsi_cmnd *srb, struct us_data *us)
151 struct karma_data *data = (struct karma_data *) us->extra;
153 if (srb->cmnd[0] == READ_10 && !data->in_storage) {
154 ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
158 data->in_storage = 1;
159 return usb_stor_Bulk_transport(srb, us);
160 } else if (srb->cmnd[0] == START_STOP) {
161 ret = rio_karma_send_command(RIO_LEAVE_STORAGE, us);
165 data->in_storage = 0;
166 return rio_karma_send_command(RIO_RESET, us);
168 return usb_stor_Bulk_transport(srb, us);
171 static void rio_karma_destructor(void *extra)
173 struct karma_data *data = (struct karma_data *) extra;
177 static int rio_karma_init(struct us_data *us)
180 struct karma_data *data = kzalloc(sizeof(struct karma_data), GFP_NOIO);
184 data->recv = kmalloc(RIO_RECV_LEN, GFP_NOIO);
191 us->extra_destructor = rio_karma_destructor;
192 ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
193 data->in_storage = (ret == 0);
198 static int karma_probe(struct usb_interface *intf,
199 const struct usb_device_id *id)
204 result = usb_stor_probe1(&us, intf, id,
205 (id - karma_usb_ids) + karma_unusual_dev_list);
209 us->transport_name = "Rio Karma/Bulk";
210 us->transport = rio_karma_transport;
211 us->transport_reset = usb_stor_Bulk_reset;
213 result = usb_stor_probe2(us);
217 static struct usb_driver karma_driver = {
219 .probe = karma_probe,
220 .disconnect = usb_stor_disconnect,
221 .suspend = usb_stor_suspend,
222 .resume = usb_stor_resume,
223 .reset_resume = usb_stor_reset_resume,
224 .pre_reset = usb_stor_pre_reset,
225 .post_reset = usb_stor_post_reset,
226 .id_table = karma_usb_ids,
230 static int __init karma_init(void)
232 return usb_register(&karma_driver);
235 static void __exit karma_exit(void)
237 usb_deregister(&karma_driver);
240 module_init(karma_init);
241 module_exit(karma_exit);