Linux Audio

Check our new training course

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