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 MODULE_DESCRIPTION("Driver for Rio Karma");
32 MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>, Keith Bennett <keith@mcs.st-and.ac.uk>");
33 MODULE_LICENSE("GPL");
35 #define RIO_PREFIX "RIOP\x00"
36 #define RIO_PREFIX_LEN 5
37 #define RIO_SEND_LEN 40
38 #define RIO_RECV_LEN 0x200
40 #define RIO_ENTER_STORAGE 0x1
41 #define RIO_LEAVE_STORAGE 0x2
49 static int rio_karma_init(struct us_data
*us
);
53 * The table of devices
55 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
56 vendorName, productName, useProtocol, useTransport, \
57 initFunction, flags) \
58 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
59 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
61 struct usb_device_id karma_usb_ids
[] = {
62 # include "unusual_karma.h"
63 { } /* Terminating entry */
65 MODULE_DEVICE_TABLE(usb
, karma_usb_ids
);
72 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
73 vendor_name, product_name, use_protocol, use_transport, \
74 init_function, Flags) \
76 .vendorName = vendor_name, \
77 .productName = product_name, \
78 .useProtocol = use_protocol, \
79 .useTransport = use_transport, \
80 .initFunction = init_function, \
83 static struct us_unusual_dev karma_unusual_dev_list
[] = {
84 # include "unusual_karma.h"
85 { } /* Terminating entry */
92 * Send commands to Rio Karma.
94 * For each command we send 40 bytes starting 'RIOP\0' followed by
95 * the command number and a sequence number, which the device will ack
96 * with a 512-byte packet with the high four bits set and everything
97 * else null. Then we send 'RIOP\x80' followed by a zero and the
98 * sequence number, until byte 5 in the response repeats the sequence
101 static int rio_karma_send_command(char cmd
, struct us_data
*us
)
104 unsigned long timeout
;
105 static unsigned char seq
= 1;
106 struct karma_data
*data
= (struct karma_data
*) us
->extra
;
108 US_DEBUGP("karma: sending command %04x\n", cmd
);
109 memset(us
->iobuf
, 0, RIO_SEND_LEN
);
110 memcpy(us
->iobuf
, RIO_PREFIX
, RIO_PREFIX_LEN
);
114 timeout
= jiffies
+ msecs_to_jiffies(6000);
116 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
117 us
->iobuf
, RIO_SEND_LEN
, &partial
);
118 if (result
!= USB_STOR_XFER_GOOD
)
121 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
122 data
->recv
, RIO_RECV_LEN
, &partial
);
123 if (result
!= USB_STOR_XFER_GOOD
)
126 if (data
->recv
[5] == seq
)
129 if (time_after(jiffies
, timeout
))
141 US_DEBUGP("karma: sent command %04x\n", cmd
);
144 US_DEBUGP("karma: command %04x failed\n", cmd
);
145 return USB_STOR_TRANSPORT_FAILED
;
149 * Trap START_STOP and READ_10 to leave/re-enter storage mode.
150 * Everything else is propagated to the normal bulk layer.
152 static int rio_karma_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
155 struct karma_data
*data
= (struct karma_data
*) us
->extra
;
157 if (srb
->cmnd
[0] == READ_10
&& !data
->in_storage
) {
158 ret
= rio_karma_send_command(RIO_ENTER_STORAGE
, us
);
162 data
->in_storage
= 1;
163 return usb_stor_Bulk_transport(srb
, us
);
164 } else if (srb
->cmnd
[0] == START_STOP
) {
165 ret
= rio_karma_send_command(RIO_LEAVE_STORAGE
, us
);
169 data
->in_storage
= 0;
170 return rio_karma_send_command(RIO_RESET
, us
);
172 return usb_stor_Bulk_transport(srb
, us
);
175 static void rio_karma_destructor(void *extra
)
177 struct karma_data
*data
= (struct karma_data
*) extra
;
181 static int rio_karma_init(struct us_data
*us
)
184 struct karma_data
*data
= kzalloc(sizeof(struct karma_data
), GFP_NOIO
);
188 data
->recv
= kmalloc(RIO_RECV_LEN
, GFP_NOIO
);
195 us
->extra_destructor
= rio_karma_destructor
;
196 ret
= rio_karma_send_command(RIO_ENTER_STORAGE
, us
);
197 data
->in_storage
= (ret
== 0);
202 static int karma_probe(struct usb_interface
*intf
,
203 const struct usb_device_id
*id
)
208 result
= usb_stor_probe1(&us
, intf
, id
,
209 (id
- karma_usb_ids
) + karma_unusual_dev_list
);
213 us
->transport_name
= "Rio Karma/Bulk";
214 us
->transport
= rio_karma_transport
;
215 us
->transport_reset
= usb_stor_Bulk_reset
;
217 result
= usb_stor_probe2(us
);
221 static struct usb_driver karma_driver
= {
223 .probe
= karma_probe
,
224 .disconnect
= usb_stor_disconnect
,
225 .suspend
= usb_stor_suspend
,
226 .resume
= usb_stor_resume
,
227 .reset_resume
= usb_stor_reset_resume
,
228 .pre_reset
= usb_stor_pre_reset
,
229 .post_reset
= usb_stor_post_reset
,
230 .id_table
= karma_usb_ids
,
234 static int __init
karma_init(void)
236 return usb_register(&karma_driver
);
239 static void __exit
karma_exit(void)
241 usb_deregister(&karma_driver
);
244 module_init(karma_init
);
245 module_exit(karma_exit
);