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 <scsi/scsi.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_device.h>
26 #include "transport.h"
30 #define RIO_PREFIX "RIOP\x00"
31 #define RIO_PREFIX_LEN 5
32 #define RIO_SEND_LEN 40
33 #define RIO_RECV_LEN 0x200
35 #define RIO_ENTER_STORAGE 0x1
36 #define RIO_LEAVE_STORAGE 0x2
39 extern int usb_stor_Bulk_transport(struct scsi_cmnd *, struct us_data *);
47 * Send commands to Rio Karma.
49 * For each command we send 40 bytes starting 'RIOP\0' followed by
50 * the command number and a sequence number, which the device will ack
51 * with a 512-byte packet with the high four bits set and everything
52 * else null. Then we send 'RIOP\x80' followed by a zero and the
53 * sequence number, until byte 5 in the response repeats the sequence
56 static int rio_karma_send_command(char cmd, struct us_data *us)
59 unsigned long timeout;
60 static unsigned char seq = 1;
61 struct karma_data *data = (struct karma_data *) us->extra;
63 US_DEBUGP("karma: sending command %04x\n", cmd);
64 memset(us->iobuf, 0, RIO_SEND_LEN);
65 memcpy(us->iobuf, RIO_PREFIX, RIO_PREFIX_LEN);
69 timeout = jiffies + msecs_to_jiffies(6000);
71 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
72 us->iobuf, RIO_SEND_LEN, &partial);
73 if (result != USB_STOR_XFER_GOOD)
76 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
77 data->recv, RIO_RECV_LEN, &partial);
78 if (result != USB_STOR_XFER_GOOD)
81 if (data->recv[5] == seq)
84 if (time_after(jiffies, timeout))
96 US_DEBUGP("karma: sent command %04x\n", cmd);
99 US_DEBUGP("karma: command %04x failed\n", cmd);
100 return USB_STOR_TRANSPORT_FAILED;
104 * Trap START_STOP and READ_10 to leave/re-enter storage mode.
105 * Everything else is propagated to the normal bulk layer.
107 int rio_karma_transport(struct scsi_cmnd *srb, struct us_data *us)
110 struct karma_data *data = (struct karma_data *) us->extra;
112 if (srb->cmnd[0] == READ_10 && !data->in_storage) {
113 ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
117 data->in_storage = 1;
118 return usb_stor_Bulk_transport(srb, us);
119 } else if (srb->cmnd[0] == START_STOP) {
120 ret = rio_karma_send_command(RIO_LEAVE_STORAGE, us);
124 data->in_storage = 0;
125 return rio_karma_send_command(RIO_RESET, us);
127 return usb_stor_Bulk_transport(srb, us);
130 static void rio_karma_destructor(void *extra)
132 struct karma_data *data = (struct karma_data *) extra;
136 int rio_karma_init(struct us_data *us)
139 struct karma_data *data = kzalloc(sizeof(struct karma_data), GFP_NOIO);
143 data->recv = kmalloc(RIO_RECV_LEN, GFP_NOIO);
150 us->extra_destructor = rio_karma_destructor;
151 ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
152 data->in_storage = (ret == 0);