Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Support for emulating SAT (ata pass through) on devices based
  3 *       on the Cypress USB/ATA bridge supporting ATACB.
  4 *
  5 * Copyright (c) 2008 Matthieu Castet (castet.matthieu@free.fr)
  6 *
  7 * This program is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU General Public License as published by the
  9 * Free Software Foundation; either version 2, or (at your option) any
 10 * later version.
 11 *
 12 * This program is distributed in the hope that it will be useful, but
 13 * WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15 * General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License along
 18 * with this program; if not, write to the Free Software Foundation, Inc.,
 19 * 675 Mass Ave, Cambridge, MA 02139, USA.
 20 */
 21
 22#include <linux/module.h>
 23#include <scsi/scsi.h>
 24#include <scsi/scsi_cmnd.h>
 25#include <scsi/scsi_eh.h>
 26#include <linux/ata.h>
 27
 28#include "usb.h"
 29#include "protocol.h"
 30#include "scsiglue.h"
 31#include "debug.h"
 32
 
 
 33MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
 34MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
 35MODULE_LICENSE("GPL");
 36
 37/*
 38 * The table of devices
 39 */
 40#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
 41		    vendorName, productName, useProtocol, useTransport, \
 42		    initFunction, flags) \
 43{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
 44  .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
 45
 46struct usb_device_id cypress_usb_ids[] = {
 47#	include "unusual_cypress.h"
 48	{ }		/* Terminating entry */
 49};
 50MODULE_DEVICE_TABLE(usb, cypress_usb_ids);
 51
 52#undef UNUSUAL_DEV
 53
 54/*
 55 * The flags table
 56 */
 57#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
 58		    vendor_name, product_name, use_protocol, use_transport, \
 59		    init_function, Flags) \
 60{ \
 61	.vendorName = vendor_name,	\
 62	.productName = product_name,	\
 63	.useProtocol = use_protocol,	\
 64	.useTransport = use_transport,	\
 65	.initFunction = init_function,	\
 66}
 67
 68static struct us_unusual_dev cypress_unusual_dev_list[] = {
 69#	include "unusual_cypress.h"
 70	{ }		/* Terminating entry */
 71};
 72
 73#undef UNUSUAL_DEV
 74
 75
 76/*
 77 * ATACB is a protocol used on cypress usb<->ata bridge to
 78 * send raw ATA command over mass storage
 79 * There is a ATACB2 protocol that support LBA48 on newer chip.
 80 * More info that be found on cy7c68310_8.pdf and cy7c68300c_8.pdf
 81 * datasheet from cypress.com.
 82 */
 83static void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
 84{
 85	unsigned char save_cmnd[MAX_COMMAND_SIZE];
 86
 87	if (likely(srb->cmnd[0] != ATA_16 && srb->cmnd[0] != ATA_12)) {
 88		usb_stor_transparent_scsi_command(srb, us);
 89		return;
 90	}
 91
 92	memcpy(save_cmnd, srb->cmnd, sizeof(save_cmnd));
 93	memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
 94
 95	/* check if we support the command */
 96	if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */
 97		goto invalid_fld;
 98	/* check protocol */
 99	switch((save_cmnd[1] >> 1) & 0xf) {
100		case 3: /*no DATA */
101		case 4: /* PIO in */
102		case 5: /* PIO out */
103			break;
104		default:
105			goto invalid_fld;
106	}
107
108	/* first build the ATACB command */
109	srb->cmd_len = 16;
110
111	srb->cmnd[0] = 0x24; /* bVSCBSignature : vendor-specific command
112	                        this value can change, but most(all ?) manufacturers
113							keep the cypress default : 0x24 */
 
 
114	srb->cmnd[1] = 0x24; /* bVSCBSubCommand : 0x24 for ATACB */
115
116	srb->cmnd[3] = 0xff - 1; /* features, sector count, lba low, lba med
117								lba high, device, command are valid */
 
 
118	srb->cmnd[4] = 1; /* TransferBlockCount : 512 */
119
120	if (save_cmnd[0] == ATA_16) {
121		srb->cmnd[ 6] = save_cmnd[ 4]; /* features */
122		srb->cmnd[ 7] = save_cmnd[ 6]; /* sector count */
123		srb->cmnd[ 8] = save_cmnd[ 8]; /* lba low */
124		srb->cmnd[ 9] = save_cmnd[10]; /* lba med */
125		srb->cmnd[10] = save_cmnd[12]; /* lba high */
126		srb->cmnd[11] = save_cmnd[13]; /* device */
127		srb->cmnd[12] = save_cmnd[14]; /* command */
128
129		if (save_cmnd[1] & 0x01) {/* extended bit set for LBA48 */
130			/* this could be supported by atacb2 */
131			if (save_cmnd[3] || save_cmnd[5] || save_cmnd[7] || save_cmnd[9]
132					|| save_cmnd[11])
133				goto invalid_fld;
134		}
135	}
136	else { /* ATA12 */
137		srb->cmnd[ 6] = save_cmnd[3]; /* features */
138		srb->cmnd[ 7] = save_cmnd[4]; /* sector count */
139		srb->cmnd[ 8] = save_cmnd[5]; /* lba low */
140		srb->cmnd[ 9] = save_cmnd[6]; /* lba med */
141		srb->cmnd[10] = save_cmnd[7]; /* lba high */
142		srb->cmnd[11] = save_cmnd[8]; /* device */
143		srb->cmnd[12] = save_cmnd[9]; /* command */
144
145	}
146	/* Filter SET_FEATURES - XFER MODE command */
147	if ((srb->cmnd[12] == ATA_CMD_SET_FEATURES)
148			&& (srb->cmnd[6] == SETFEATURES_XFER))
149		goto invalid_fld;
150
151	if (srb->cmnd[12] == ATA_CMD_ID_ATA || srb->cmnd[12] == ATA_CMD_ID_ATAPI)
152		srb->cmnd[2] |= (1<<7); /* set  IdentifyPacketDevice for these cmds */
153
154
155	usb_stor_transparent_scsi_command(srb, us);
156
157	/* if the device doesn't support ATACB
158	 */
159	if (srb->result == SAM_STAT_CHECK_CONDITION &&
160			memcmp(srb->sense_buffer, usb_stor_sense_invalidCDB,
161				sizeof(usb_stor_sense_invalidCDB)) == 0) {
162		US_DEBUGP("cypress atacb not supported ???\n");
163		goto end;
164	}
165
166	/* if ck_cond flags is set, and there wasn't critical error,
 
167	 * build the special sense
168	 */
169	if ((srb->result != (DID_ERROR << 16) &&
170				srb->result != (DID_ABORT << 16)) &&
171			save_cmnd[2] & 0x20) {
172		struct scsi_eh_save ses;
173		unsigned char regs[8];
174		unsigned char *sb = srb->sense_buffer;
175		unsigned char *desc = sb + 8;
176		int tmp_result;
177
178		/* build the command for
179		 * reading the ATA registers */
180		scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sizeof(regs));
181
182		/* we use the same command as before, but we set
 
183		 * the read taskfile bit, for not executing atacb command,
184		 * but reading register selected in srb->cmnd[4]
185		 */
186		srb->cmd_len = 16;
187		srb->cmnd = ses.cmnd;
188		srb->cmnd[2] = 1;
189
190		usb_stor_transparent_scsi_command(srb, us);
191		memcpy(regs, srb->sense_buffer, sizeof(regs));
192		tmp_result = srb->result;
193		scsi_eh_restore_cmnd(srb, &ses);
194		/* we fail to get registers, report invalid command */
195		if (tmp_result != SAM_STAT_GOOD)
196			goto invalid_fld;
197
198		/* build the sense */
199		memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
200
201		/* set sk, asc for a good command */
202		sb[1] = RECOVERED_ERROR;
203		sb[2] = 0; /* ATA PASS THROUGH INFORMATION AVAILABLE */
204		sb[3] = 0x1D;
205
206		/* XXX we should generate sk, asc, ascq from status and error
 
207		 * regs
208		 * (see 11.1 Error translation ATA device error to SCSI error
209		 *  map, and ata_to_sense_error from libata.)
210		 */
211
212		/* Sense data is current and format is descriptor. */
213		sb[0] = 0x72;
214		desc[0] = 0x09; /* ATA_RETURN_DESCRIPTOR */
215
216		/* set length of additional sense data */
217		sb[7] = 14;
218		desc[1] = 12;
219
220		/* Copy registers into sense buffer. */
221		desc[ 2] = 0x00;
222		desc[ 3] = regs[1];  /* features */
223		desc[ 5] = regs[2];  /* sector count */
224		desc[ 7] = regs[3];  /* lba low */
225		desc[ 9] = regs[4];  /* lba med */
226		desc[11] = regs[5];  /* lba high */
227		desc[12] = regs[6];  /* device */
228		desc[13] = regs[7];  /* command */
229
230		srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
231	}
232	goto end;
233invalid_fld:
234	srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
235
236	memcpy(srb->sense_buffer,
237			usb_stor_sense_invalidCDB,
238			sizeof(usb_stor_sense_invalidCDB));
239end:
240	memcpy(srb->cmnd, save_cmnd, sizeof(save_cmnd));
241	if (srb->cmnd[0] == ATA_12)
242		srb->cmd_len = 12;
243}
244
 
245
246static int cypress_probe(struct usb_interface *intf,
247			 const struct usb_device_id *id)
248{
249	struct us_data *us;
250	int result;
 
251
252	result = usb_stor_probe1(&us, intf, id,
253			(id - cypress_usb_ids) + cypress_unusual_dev_list);
 
254	if (result)
255		return result;
256
257	us->protocol_name = "Transparent SCSI with Cypress ATACB";
258	us->proto_handler = cypress_atacb_passthrough;
 
 
 
 
 
 
 
 
 
 
 
 
259
260	result = usb_stor_probe2(us);
261	return result;
262}
263
264static struct usb_driver cypress_driver = {
265	.name =		"ums-cypress",
266	.probe =	cypress_probe,
267	.disconnect =	usb_stor_disconnect,
268	.suspend =	usb_stor_suspend,
269	.resume =	usb_stor_resume,
270	.reset_resume =	usb_stor_reset_resume,
271	.pre_reset =	usb_stor_pre_reset,
272	.post_reset =	usb_stor_post_reset,
273	.id_table =	cypress_usb_ids,
274	.soft_unbind =	1,
 
275};
276
277static int __init cypress_init(void)
278{
279	return usb_register(&cypress_driver);
280}
281
282static void __exit cypress_exit(void)
283{
284	usb_deregister(&cypress_driver);
285}
286
287module_init(cypress_init);
288module_exit(cypress_exit);
v4.10.11
  1/*
  2 * Support for emulating SAT (ata pass through) on devices based
  3 *       on the Cypress USB/ATA bridge supporting ATACB.
  4 *
  5 * Copyright (c) 2008 Matthieu Castet (castet.matthieu@free.fr)
  6 *
  7 * This program is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU General Public License as published by the
  9 * Free Software Foundation; either version 2, or (at your option) any
 10 * later version.
 11 *
 12 * This program is distributed in the hope that it will be useful, but
 13 * WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15 * General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License along
 18 * with this program; if not, write to the Free Software Foundation, Inc.,
 19 * 675 Mass Ave, Cambridge, MA 02139, USA.
 20 */
 21
 22#include <linux/module.h>
 23#include <scsi/scsi.h>
 24#include <scsi/scsi_cmnd.h>
 25#include <scsi/scsi_eh.h>
 26#include <linux/ata.h>
 27
 28#include "usb.h"
 29#include "protocol.h"
 30#include "scsiglue.h"
 31#include "debug.h"
 32
 33#define DRV_NAME "ums-cypress"
 34
 35MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
 36MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
 37MODULE_LICENSE("GPL");
 38
 39/*
 40 * The table of devices
 41 */
 42#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
 43		    vendorName, productName, useProtocol, useTransport, \
 44		    initFunction, flags) \
 45{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
 46  .driver_info = (flags) }
 47
 48static struct usb_device_id cypress_usb_ids[] = {
 49#	include "unusual_cypress.h"
 50	{ }		/* Terminating entry */
 51};
 52MODULE_DEVICE_TABLE(usb, cypress_usb_ids);
 53
 54#undef UNUSUAL_DEV
 55
 56/*
 57 * The flags table
 58 */
 59#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
 60		    vendor_name, product_name, use_protocol, use_transport, \
 61		    init_function, Flags) \
 62{ \
 63	.vendorName = vendor_name,	\
 64	.productName = product_name,	\
 65	.useProtocol = use_protocol,	\
 66	.useTransport = use_transport,	\
 67	.initFunction = init_function,	\
 68}
 69
 70static struct us_unusual_dev cypress_unusual_dev_list[] = {
 71#	include "unusual_cypress.h"
 72	{ }		/* Terminating entry */
 73};
 74
 75#undef UNUSUAL_DEV
 76
 77
 78/*
 79 * ATACB is a protocol used on cypress usb<->ata bridge to
 80 * send raw ATA command over mass storage
 81 * There is a ATACB2 protocol that support LBA48 on newer chip.
 82 * More info that be found on cy7c68310_8.pdf and cy7c68300c_8.pdf
 83 * datasheet from cypress.com.
 84 */
 85static void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
 86{
 87	unsigned char save_cmnd[MAX_COMMAND_SIZE];
 88
 89	if (likely(srb->cmnd[0] != ATA_16 && srb->cmnd[0] != ATA_12)) {
 90		usb_stor_transparent_scsi_command(srb, us);
 91		return;
 92	}
 93
 94	memcpy(save_cmnd, srb->cmnd, sizeof(save_cmnd));
 95	memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
 96
 97	/* check if we support the command */
 98	if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */
 99		goto invalid_fld;
100	/* check protocol */
101	switch ((save_cmnd[1] >> 1) & 0xf) {
102	case 3: /*no DATA */
103	case 4: /* PIO in */
104	case 5: /* PIO out */
105		break;
106	default:
107		goto invalid_fld;
108	}
109
110	/* first build the ATACB command */
111	srb->cmd_len = 16;
112
113	srb->cmnd[0] = 0x24; /*
114			      * bVSCBSignature : vendor-specific command
115			      * this value can change, but most(all ?) manufacturers
116			      * keep the cypress default : 0x24
117			      */
118	srb->cmnd[1] = 0x24; /* bVSCBSubCommand : 0x24 for ATACB */
119
120	srb->cmnd[3] = 0xff - 1; /*
121				  * features, sector count, lba low, lba med
122				  * lba high, device, command are valid
123				  */
124	srb->cmnd[4] = 1; /* TransferBlockCount : 512 */
125
126	if (save_cmnd[0] == ATA_16) {
127		srb->cmnd[ 6] = save_cmnd[ 4]; /* features */
128		srb->cmnd[ 7] = save_cmnd[ 6]; /* sector count */
129		srb->cmnd[ 8] = save_cmnd[ 8]; /* lba low */
130		srb->cmnd[ 9] = save_cmnd[10]; /* lba med */
131		srb->cmnd[10] = save_cmnd[12]; /* lba high */
132		srb->cmnd[11] = save_cmnd[13]; /* device */
133		srb->cmnd[12] = save_cmnd[14]; /* command */
134
135		if (save_cmnd[1] & 0x01) {/* extended bit set for LBA48 */
136			/* this could be supported by atacb2 */
137			if (save_cmnd[3] || save_cmnd[5] || save_cmnd[7] || save_cmnd[9]
138					|| save_cmnd[11])
139				goto invalid_fld;
140		}
141	} else { /* ATA12 */
 
142		srb->cmnd[ 6] = save_cmnd[3]; /* features */
143		srb->cmnd[ 7] = save_cmnd[4]; /* sector count */
144		srb->cmnd[ 8] = save_cmnd[5]; /* lba low */
145		srb->cmnd[ 9] = save_cmnd[6]; /* lba med */
146		srb->cmnd[10] = save_cmnd[7]; /* lba high */
147		srb->cmnd[11] = save_cmnd[8]; /* device */
148		srb->cmnd[12] = save_cmnd[9]; /* command */
149
150	}
151	/* Filter SET_FEATURES - XFER MODE command */
152	if ((srb->cmnd[12] == ATA_CMD_SET_FEATURES)
153			&& (srb->cmnd[6] == SETFEATURES_XFER))
154		goto invalid_fld;
155
156	if (srb->cmnd[12] == ATA_CMD_ID_ATA || srb->cmnd[12] == ATA_CMD_ID_ATAPI)
157		srb->cmnd[2] |= (1<<7); /* set  IdentifyPacketDevice for these cmds */
158
159
160	usb_stor_transparent_scsi_command(srb, us);
161
162	/* if the device doesn't support ATACB */
 
163	if (srb->result == SAM_STAT_CHECK_CONDITION &&
164			memcmp(srb->sense_buffer, usb_stor_sense_invalidCDB,
165				sizeof(usb_stor_sense_invalidCDB)) == 0) {
166		usb_stor_dbg(us, "cypress atacb not supported ???\n");
167		goto end;
168	}
169
170	/*
171	 * if ck_cond flags is set, and there wasn't critical error,
172	 * build the special sense
173	 */
174	if ((srb->result != (DID_ERROR << 16) &&
175				srb->result != (DID_ABORT << 16)) &&
176			save_cmnd[2] & 0x20) {
177		struct scsi_eh_save ses;
178		unsigned char regs[8];
179		unsigned char *sb = srb->sense_buffer;
180		unsigned char *desc = sb + 8;
181		int tmp_result;
182
183		/* build the command for reading the ATA registers */
 
184		scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sizeof(regs));
185
186		/*
187		 * we use the same command as before, but we set
188		 * the read taskfile bit, for not executing atacb command,
189		 * but reading register selected in srb->cmnd[4]
190		 */
191		srb->cmd_len = 16;
192		srb->cmnd = ses.cmnd;
193		srb->cmnd[2] = 1;
194
195		usb_stor_transparent_scsi_command(srb, us);
196		memcpy(regs, srb->sense_buffer, sizeof(regs));
197		tmp_result = srb->result;
198		scsi_eh_restore_cmnd(srb, &ses);
199		/* we fail to get registers, report invalid command */
200		if (tmp_result != SAM_STAT_GOOD)
201			goto invalid_fld;
202
203		/* build the sense */
204		memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
205
206		/* set sk, asc for a good command */
207		sb[1] = RECOVERED_ERROR;
208		sb[2] = 0; /* ATA PASS THROUGH INFORMATION AVAILABLE */
209		sb[3] = 0x1D;
210
211		/*
212		 * XXX we should generate sk, asc, ascq from status and error
213		 * regs
214		 * (see 11.1 Error translation ATA device error to SCSI error
215		 * map, and ata_to_sense_error from libata.)
216		 */
217
218		/* Sense data is current and format is descriptor. */
219		sb[0] = 0x72;
220		desc[0] = 0x09; /* ATA_RETURN_DESCRIPTOR */
221
222		/* set length of additional sense data */
223		sb[7] = 14;
224		desc[1] = 12;
225
226		/* Copy registers into sense buffer. */
227		desc[ 2] = 0x00;
228		desc[ 3] = regs[1];  /* features */
229		desc[ 5] = regs[2];  /* sector count */
230		desc[ 7] = regs[3];  /* lba low */
231		desc[ 9] = regs[4];  /* lba med */
232		desc[11] = regs[5];  /* lba high */
233		desc[12] = regs[6];  /* device */
234		desc[13] = regs[7];  /* command */
235
236		srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
237	}
238	goto end;
239invalid_fld:
240	srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
241
242	memcpy(srb->sense_buffer,
243			usb_stor_sense_invalidCDB,
244			sizeof(usb_stor_sense_invalidCDB));
245end:
246	memcpy(srb->cmnd, save_cmnd, sizeof(save_cmnd));
247	if (srb->cmnd[0] == ATA_12)
248		srb->cmd_len = 12;
249}
250
251static struct scsi_host_template cypress_host_template;
252
253static int cypress_probe(struct usb_interface *intf,
254			 const struct usb_device_id *id)
255{
256	struct us_data *us;
257	int result;
258	struct usb_device *device;
259
260	result = usb_stor_probe1(&us, intf, id,
261			(id - cypress_usb_ids) + cypress_unusual_dev_list,
262			&cypress_host_template);
263	if (result)
264		return result;
265
266	/*
267	 * Among CY7C68300 chips, the A revision does not support Cypress ATACB
268	 * Filter out this revision from EEPROM default descriptor values
269	 */
270	device = interface_to_usbdev(intf);
271	if (device->descriptor.iManufacturer != 0x38 ||
272	    device->descriptor.iProduct != 0x4e ||
273	    device->descriptor.iSerialNumber != 0x64) {
274		us->protocol_name = "Transparent SCSI with Cypress ATACB";
275		us->proto_handler = cypress_atacb_passthrough;
276	} else {
277		us->protocol_name = "Transparent SCSI";
278		us->proto_handler = usb_stor_transparent_scsi_command;
279	}
280
281	result = usb_stor_probe2(us);
282	return result;
283}
284
285static struct usb_driver cypress_driver = {
286	.name =		DRV_NAME,
287	.probe =	cypress_probe,
288	.disconnect =	usb_stor_disconnect,
289	.suspend =	usb_stor_suspend,
290	.resume =	usb_stor_resume,
291	.reset_resume =	usb_stor_reset_resume,
292	.pre_reset =	usb_stor_pre_reset,
293	.post_reset =	usb_stor_post_reset,
294	.id_table =	cypress_usb_ids,
295	.soft_unbind =	1,
296	.no_dynamic_id = 1,
297};
298
299module_usb_stor_driver(cypress_driver, cypress_host_template, DRV_NAME);