Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Driver for Datafab USB Compact Flash reader
  4 *
  5 * datafab driver v0.1:
  6 *
  7 * First release
  8 *
  9 * Current development and maintenance by:
 10 *   (c) 2000 Jimmie Mayfield (mayfield+datafab@sackheads.org)
 11 *
 12 *   Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
 13 *   which I used as a template for this driver.
 14 *
 15 *   Some bugfixes and scatter-gather code by Gregory P. Smith 
 16 *   (greg-usb@electricrain.com)
 17 *
 18 *   Fix for media change by Joerg Schneider (js@joergschneider.com)
 19 *
 20 * Other contributors:
 21 *   (c) 2002 Alan Stern <stern@rowland.org>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 22 */
 23
 24/*
 25 * This driver attempts to support USB CompactFlash reader/writer devices
 26 * based on Datafab USB-to-ATA chips.  It was specifically developed for the 
 27 * Datafab MDCFE-B USB CompactFlash reader but has since been found to work 
 28 * with a variety of Datafab-based devices from a number of manufacturers.
 29 * I've received a report of this driver working with a Datafab-based
 30 * SmartMedia device though please be aware that I'm personally unable to
 31 * test SmartMedia support.
 32 *
 33 * This driver supports reading and writing.  If you're truly paranoid,
 34 * however, you can force the driver into a write-protected state by setting
 35 * the WP enable bits in datafab_handle_mode_sense().  See the comments
 36 * in that routine.
 37 */
 38
 39#include <linux/errno.h>
 40#include <linux/module.h>
 41#include <linux/slab.h>
 42
 43#include <scsi/scsi.h>
 44#include <scsi/scsi_cmnd.h>
 45
 46#include "usb.h"
 47#include "transport.h"
 48#include "protocol.h"
 49#include "debug.h"
 50#include "scsiglue.h"
 51
 52#define DRV_NAME "ums-datafab"
 53
 54MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
 55MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
 56MODULE_LICENSE("GPL");
 57MODULE_IMPORT_NS("USB_STORAGE");
 58
 59struct datafab_info {
 60	unsigned long   sectors;	/* total sector count */
 61	unsigned long   ssize;		/* sector size in bytes */
 62	signed char	lun;		/* used for dual-slot readers */
 63
 64	/* the following aren't used yet */
 65	unsigned char   sense_key;
 66	unsigned long   sense_asc;	/* additional sense code */
 67	unsigned long   sense_ascq;	/* additional sense code qualifier */
 68};
 69
 70static int datafab_determine_lun(struct us_data *us,
 71				 struct datafab_info *info);
 72
 73
 74/*
 75 * The table of devices
 76 */
 77#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
 78		    vendorName, productName, useProtocol, useTransport, \
 79		    initFunction, flags) \
 80{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
 81  .driver_info = (flags) }
 82
 83static const struct usb_device_id datafab_usb_ids[] = {
 84#	include "unusual_datafab.h"
 85	{ }		/* Terminating entry */
 86};
 87MODULE_DEVICE_TABLE(usb, datafab_usb_ids);
 88
 89#undef UNUSUAL_DEV
 90
 91/*
 92 * The flags table
 93 */
 94#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
 95		    vendor_name, product_name, use_protocol, use_transport, \
 96		    init_function, Flags) \
 97{ \
 98	.vendorName = vendor_name,	\
 99	.productName = product_name,	\
100	.useProtocol = use_protocol,	\
101	.useTransport = use_transport,	\
102	.initFunction = init_function,	\
103}
104
105static const struct us_unusual_dev datafab_unusual_dev_list[] = {
106#	include "unusual_datafab.h"
107	{ }		/* Terminating entry */
108};
109
110#undef UNUSUAL_DEV
111
112
113static inline int
114datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
115	if (len == 0)
116		return USB_STOR_XFER_GOOD;
117
118	usb_stor_dbg(us, "len = %d\n", len);
119	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
120			data, len, NULL);
121}
122
123
124static inline int
125datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
126	if (len == 0)
127		return USB_STOR_XFER_GOOD;
128
129	usb_stor_dbg(us, "len = %d\n", len);
130	return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
131			data, len, NULL);
132}
133
134
135static int datafab_read_data(struct us_data *us,
136			     struct datafab_info *info,
137			     u32 sector,
138			     u32 sectors)
139{
140	unsigned char *command = us->iobuf;
141	unsigned char *buffer;
142	unsigned char  thistime;
143	unsigned int totallen, alloclen;
144	int len, result;
145	unsigned int sg_offset = 0;
146	struct scatterlist *sg = NULL;
147
148	// we're working in LBA mode.  according to the ATA spec, 
149	// we can support up to 28-bit addressing.  I don't know if Datafab
150	// supports beyond 24-bit addressing.  It's kind of hard to test 
151	// since it requires > 8GB CF card.
152	//
153	if (sectors > 0x0FFFFFFF)
154		return USB_STOR_TRANSPORT_ERROR;
155
156	if (info->lun == -1) {
157		result = datafab_determine_lun(us, info);
158		if (result != USB_STOR_TRANSPORT_GOOD)
159			return result;
160	}
161
162	totallen = sectors * info->ssize;
163
164	// Since we don't read more than 64 KB at a time, we have to create
165	// a bounce buffer and move the data a piece at a time between the
166	// bounce buffer and the actual transfer buffer.
167
168	alloclen = min(totallen, 65536u);
169	buffer = kmalloc(alloclen, GFP_NOIO);
170	if (buffer == NULL)
171		return USB_STOR_TRANSPORT_ERROR;
172
173	do {
174		// loop, never allocate or transfer more than 64k at once
175		// (min(128k, 255*info->ssize) is the real limit)
176
177		len = min(totallen, alloclen);
178		thistime = (len / info->ssize) & 0xff;
179
180		command[0] = 0;
181		command[1] = thistime;
182		command[2] = sector & 0xFF;
183		command[3] = (sector >> 8) & 0xFF;
184		command[4] = (sector >> 16) & 0xFF;
185
186		command[5] = 0xE0 + (info->lun << 4);
187		command[5] |= (sector >> 24) & 0x0F;
188		command[6] = 0x20;
189		command[7] = 0x01;
190
191		// send the read command
192		result = datafab_bulk_write(us, command, 8);
193		if (result != USB_STOR_XFER_GOOD)
194			goto leave;
195
196		// read the result
197		result = datafab_bulk_read(us, buffer, len);
198		if (result != USB_STOR_XFER_GOOD)
199			goto leave;
200
201		// Store the data in the transfer buffer
202		usb_stor_access_xfer_buf(buffer, len, us->srb,
203				 &sg, &sg_offset, TO_XFER_BUF);
204
205		sector += thistime;
206		totallen -= len;
207	} while (totallen > 0);
208
209	kfree(buffer);
210	return USB_STOR_TRANSPORT_GOOD;
211
212 leave:
213	kfree(buffer);
214	return USB_STOR_TRANSPORT_ERROR;
215}
216
217
218static int datafab_write_data(struct us_data *us,
219			      struct datafab_info *info,
220			      u32 sector,
221			      u32 sectors)
222{
223	unsigned char *command = us->iobuf;
224	unsigned char *reply = us->iobuf;
225	unsigned char *buffer;
226	unsigned char thistime;
227	unsigned int totallen, alloclen;
228	int len, result;
229	unsigned int sg_offset = 0;
230	struct scatterlist *sg = NULL;
231
232	// we're working in LBA mode.  according to the ATA spec, 
233	// we can support up to 28-bit addressing.  I don't know if Datafab
234	// supports beyond 24-bit addressing.  It's kind of hard to test 
235	// since it requires > 8GB CF card.
236	//
237	if (sectors > 0x0FFFFFFF)
238		return USB_STOR_TRANSPORT_ERROR;
239
240	if (info->lun == -1) {
241		result = datafab_determine_lun(us, info);
242		if (result != USB_STOR_TRANSPORT_GOOD)
243			return result;
244	}
245
246	totallen = sectors * info->ssize;
247
248	// Since we don't write more than 64 KB at a time, we have to create
249	// a bounce buffer and move the data a piece at a time between the
250	// bounce buffer and the actual transfer buffer.
251
252	alloclen = min(totallen, 65536u);
253	buffer = kmalloc(alloclen, GFP_NOIO);
254	if (buffer == NULL)
255		return USB_STOR_TRANSPORT_ERROR;
256
257	do {
258		// loop, never allocate or transfer more than 64k at once
259		// (min(128k, 255*info->ssize) is the real limit)
260
261		len = min(totallen, alloclen);
262		thistime = (len / info->ssize) & 0xff;
263
264		// Get the data from the transfer buffer
265		usb_stor_access_xfer_buf(buffer, len, us->srb,
266				&sg, &sg_offset, FROM_XFER_BUF);
267
268		command[0] = 0;
269		command[1] = thistime;
270		command[2] = sector & 0xFF;
271		command[3] = (sector >> 8) & 0xFF;
272		command[4] = (sector >> 16) & 0xFF;
273
274		command[5] = 0xE0 + (info->lun << 4);
275		command[5] |= (sector >> 24) & 0x0F;
276		command[6] = 0x30;
277		command[7] = 0x02;
278
279		// send the command
280		result = datafab_bulk_write(us, command, 8);
281		if (result != USB_STOR_XFER_GOOD)
282			goto leave;
283
284		// send the data
285		result = datafab_bulk_write(us, buffer, len);
286		if (result != USB_STOR_XFER_GOOD)
287			goto leave;
288
289		// read the result
290		result = datafab_bulk_read(us, reply, 2);
291		if (result != USB_STOR_XFER_GOOD)
292			goto leave;
293
294		if (reply[0] != 0x50 && reply[1] != 0) {
295			usb_stor_dbg(us, "Gah! write return code: %02x %02x\n",
296				     reply[0], reply[1]);
 
297			goto leave;
298		}
299
300		sector += thistime;
301		totallen -= len;
302	} while (totallen > 0);
303
304	kfree(buffer);
305	return USB_STOR_TRANSPORT_GOOD;
306
307 leave:
308	kfree(buffer);
309	return USB_STOR_TRANSPORT_ERROR;
310}
311
312
313static int datafab_determine_lun(struct us_data *us,
314				 struct datafab_info *info)
315{
316	// Dual-slot readers can be thought of as dual-LUN devices.
317	// We need to determine which card slot is being used.
318	// We'll send an IDENTIFY DEVICE command and see which LUN responds...
319	//
320	// There might be a better way of doing this?
321
322	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
323	unsigned char *command = us->iobuf;
324	unsigned char *buf;
325	int count = 0, rc;
326
327	if (!info)
328		return USB_STOR_TRANSPORT_ERROR;
329
330	memcpy(command, scommand, 8);
331	buf = kmalloc(512, GFP_NOIO);
332	if (!buf)
333		return USB_STOR_TRANSPORT_ERROR;
334
335	usb_stor_dbg(us, "locating...\n");
336
337	// we'll try 3 times before giving up...
338	//
339	while (count++ < 3) {
340		command[5] = 0xa0;
341
342		rc = datafab_bulk_write(us, command, 8);
343		if (rc != USB_STOR_XFER_GOOD) {
344			rc = USB_STOR_TRANSPORT_ERROR;
345			goto leave;
346		}
347
348		rc = datafab_bulk_read(us, buf, 512);
349		if (rc == USB_STOR_XFER_GOOD) {
350			info->lun = 0;
351			rc = USB_STOR_TRANSPORT_GOOD;
352			goto leave;
353		}
354
355		command[5] = 0xb0;
356
357		rc = datafab_bulk_write(us, command, 8);
358		if (rc != USB_STOR_XFER_GOOD) {
359			rc = USB_STOR_TRANSPORT_ERROR;
360			goto leave;
361		}
362
363		rc = datafab_bulk_read(us, buf, 512);
364		if (rc == USB_STOR_XFER_GOOD) {
365			info->lun = 1;
366			rc = USB_STOR_TRANSPORT_GOOD;
367			goto leave;
368		}
369
370		msleep(20);
371	}
372
373	rc = USB_STOR_TRANSPORT_ERROR;
374
375 leave:
376	kfree(buf);
377	return rc;
378}
379
380static int datafab_id_device(struct us_data *us,
381			     struct datafab_info *info)
382{
383	// this is a variation of the ATA "IDENTIFY DEVICE" command...according
384	// to the ATA spec, 'Sector Count' isn't used but the Windows driver
385	// sets this bit so we do too...
386	//
387	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
388	unsigned char *command = us->iobuf;
389	unsigned char *reply;
390	int rc;
391
392	if (!info)
393		return USB_STOR_TRANSPORT_ERROR;
394
395	if (info->lun == -1) {
396		rc = datafab_determine_lun(us, info);
397		if (rc != USB_STOR_TRANSPORT_GOOD)
398			return rc;
399	}
400
401	memcpy(command, scommand, 8);
402	reply = kmalloc(512, GFP_NOIO);
403	if (!reply)
404		return USB_STOR_TRANSPORT_ERROR;
405
406	command[5] += (info->lun << 4);
407
408	rc = datafab_bulk_write(us, command, 8);
409	if (rc != USB_STOR_XFER_GOOD) {
410		rc = USB_STOR_TRANSPORT_ERROR;
411		goto leave;
412	}
413
414	// we'll go ahead and extract the media capacity while we're here...
415	//
416	rc = datafab_bulk_read(us, reply, 512);
417	if (rc == USB_STOR_XFER_GOOD) {
418		// capacity is at word offset 57-58
419		//
420		info->sectors = ((u32)(reply[117]) << 24) | 
421				((u32)(reply[116]) << 16) |
422				((u32)(reply[115]) <<  8) | 
423				((u32)(reply[114])      );
424		rc = USB_STOR_TRANSPORT_GOOD;
425		goto leave;
426	}
427
428	rc = USB_STOR_TRANSPORT_ERROR;
429
430 leave:
431	kfree(reply);
432	return rc;
433}
434
435
436static int datafab_handle_mode_sense(struct us_data *us,
437				     struct scsi_cmnd * srb, 
438				     int sense_6)
439{
440	static unsigned char rw_err_page[12] = {
441		0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
442	};
443	static unsigned char cache_page[12] = {
444		0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
445	};
446	static unsigned char rbac_page[12] = {
447		0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
448	};
449	static unsigned char timer_page[8] = {
450		0x1C, 0x6, 0, 0, 0, 0
451	};
452	unsigned char pc, page_code;
453	unsigned int i = 0;
454	struct datafab_info *info = (struct datafab_info *) (us->extra);
455	unsigned char *ptr = us->iobuf;
456
457	// most of this stuff is just a hack to get things working.  the
458	// datafab reader doesn't present a SCSI interface so we
459	// fudge the SCSI commands...
460	//
461
462	pc = srb->cmnd[2] >> 6;
463	page_code = srb->cmnd[2] & 0x3F;
464
465	switch (pc) {
466	   case 0x0:
467		   usb_stor_dbg(us, "Current values\n");
468		break;
469	   case 0x1:
470		   usb_stor_dbg(us, "Changeable values\n");
471		break;
472	   case 0x2:
473		   usb_stor_dbg(us, "Default values\n");
474		break;
475	   case 0x3:
476		   usb_stor_dbg(us, "Saves values\n");
477		break;
478	}
479
480	memset(ptr, 0, 8);
481	if (sense_6) {
482		ptr[2] = 0x00;		// WP enable: 0x80
483		i = 4;
484	} else {
485		ptr[3] = 0x00;		// WP enable: 0x80
486		i = 8;
487	}
488
489	switch (page_code) {
490	   default:
491		// vendor-specific mode
492		info->sense_key = 0x05;
493		info->sense_asc = 0x24;
494		info->sense_ascq = 0x00;
495		return USB_STOR_TRANSPORT_FAILED;
496
497	   case 0x1:
498		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
499		i += sizeof(rw_err_page);
500		break;
501
502	   case 0x8:
503		memcpy(ptr + i, cache_page, sizeof(cache_page));
504		i += sizeof(cache_page);
505		break;
506
507	   case 0x1B:
508		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
509		i += sizeof(rbac_page);
510		break;
511
512	   case 0x1C:
513		memcpy(ptr + i, timer_page, sizeof(timer_page));
514		i += sizeof(timer_page);
515		break;
516
517	   case 0x3F:		// retrieve all pages
518		memcpy(ptr + i, timer_page, sizeof(timer_page));
519		i += sizeof(timer_page);
520		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
521		i += sizeof(rbac_page);
522		memcpy(ptr + i, cache_page, sizeof(cache_page));
523		i += sizeof(cache_page);
524		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
525		i += sizeof(rw_err_page);
526		break;
527	}
528
529	if (sense_6)
530		ptr[0] = i - 1;
531	else
532		((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
533	usb_stor_set_xfer_buf(ptr, i, srb);
534
535	return USB_STOR_TRANSPORT_GOOD;
536}
537
538static void datafab_info_destructor(void *extra)
539{
540	// this routine is a placeholder...
541	// currently, we don't allocate any extra memory so we're okay
542}
543
544
545// Transport for the Datafab MDCFE-B
546//
547static int datafab_transport(struct scsi_cmnd *srb, struct us_data *us)
548{
549	struct datafab_info *info;
550	int rc;
551	unsigned long block, blocks;
552	unsigned char *ptr = us->iobuf;
553	static unsigned char inquiry_reply[8] = {
554		0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
555	};
556
557	if (!us->extra) {
558		us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
559		if (!us->extra)
560			return USB_STOR_TRANSPORT_ERROR;
561
562		us->extra_destructor = datafab_info_destructor;
563  		((struct datafab_info *)us->extra)->lun = -1;
564	}
565
566	info = (struct datafab_info *) (us->extra);
567
568	if (srb->cmnd[0] == INQUIRY) {
569		usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
570		memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
571		fill_inquiry_response(us, ptr, 36);
572		return USB_STOR_TRANSPORT_GOOD;
573	}
574
575	if (srb->cmnd[0] == READ_CAPACITY) {
576		info->ssize = 0x200;  // hard coded 512 byte sectors as per ATA spec
577		rc = datafab_id_device(us, info);
578		if (rc != USB_STOR_TRANSPORT_GOOD)
579			return rc;
580
581		usb_stor_dbg(us, "READ_CAPACITY:  %ld sectors, %ld bytes per sector\n",
582			     info->sectors, info->ssize);
583
584		// build the reply
585		// we need the last sector, not the number of sectors
586		((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
587		((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
588		usb_stor_set_xfer_buf(ptr, 8, srb);
589
590		return USB_STOR_TRANSPORT_GOOD;
591	}
592
593	if (srb->cmnd[0] == MODE_SELECT_10) {
594		usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
595		return USB_STOR_TRANSPORT_ERROR;
596	}
597
598	// don't bother implementing READ_6 or WRITE_6.
599	//
600	if (srb->cmnd[0] == READ_10) {
601		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
602			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
603
604		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
605
606		usb_stor_dbg(us, "READ_10: read block 0x%04lx  count %ld\n",
607			     block, blocks);
608		return datafab_read_data(us, info, block, blocks);
609	}
610
611	if (srb->cmnd[0] == READ_12) {
612		// we'll probably never see a READ_12 but we'll do it anyway...
613		//
614		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
615			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
616
617		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
618			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
619
620		usb_stor_dbg(us, "READ_12: read block 0x%04lx  count %ld\n",
621			     block, blocks);
622		return datafab_read_data(us, info, block, blocks);
623	}
624
625	if (srb->cmnd[0] == WRITE_10) {
626		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
627			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
628
629		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
630
631		usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
632			     block, blocks);
633		return datafab_write_data(us, info, block, blocks);
634	}
635
636	if (srb->cmnd[0] == WRITE_12) {
637		// we'll probably never see a WRITE_12 but we'll do it anyway...
638		//
639		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
640			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
641
642		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
643			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
644
645		usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
646			     block, blocks);
647		return datafab_write_data(us, info, block, blocks);
648	}
649
650	if (srb->cmnd[0] == TEST_UNIT_READY) {
651		usb_stor_dbg(us, "TEST_UNIT_READY\n");
652		return datafab_id_device(us, info);
653	}
654
655	if (srb->cmnd[0] == REQUEST_SENSE) {
656		usb_stor_dbg(us, "REQUEST_SENSE - Returning faked response\n");
657
658		// this response is pretty bogus right now.  eventually if necessary
659		// we can set the correct sense data.  so far though it hasn't been
660		// necessary
661		//
662		memset(ptr, 0, 18);
663		ptr[0] = 0xF0;
664		ptr[2] = info->sense_key;
665		ptr[7] = 11;
666		ptr[12] = info->sense_asc;
667		ptr[13] = info->sense_ascq;
668		usb_stor_set_xfer_buf(ptr, 18, srb);
669
670		return USB_STOR_TRANSPORT_GOOD;
671	}
672
673	if (srb->cmnd[0] == MODE_SENSE) {
674		usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
675		return datafab_handle_mode_sense(us, srb, 1);
676	}
677
678	if (srb->cmnd[0] == MODE_SENSE_10) {
679		usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
680		return datafab_handle_mode_sense(us, srb, 0);
681	}
682
683	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
684		/*
685		 * sure.  whatever.  not like we can stop the user from
686		 * popping the media out of the device (no locking doors, etc)
687		 */
688		return USB_STOR_TRANSPORT_GOOD;
689	}
690
691	if (srb->cmnd[0] == START_STOP) {
692		/*
693		 * this is used by sd.c'check_scsidisk_media_change to detect
694		 * media change
695		 */
696		usb_stor_dbg(us, "START_STOP\n");
697		/*
698		 * the first datafab_id_device after a media change returns
699		 * an error (determined experimentally)
700		 */
701		rc = datafab_id_device(us, info);
702		if (rc == USB_STOR_TRANSPORT_GOOD) {
703			info->sense_key = NO_SENSE;
704			srb->result = SUCCESS;
705		} else {
706			info->sense_key = UNIT_ATTENTION;
707			srb->result = SAM_STAT_CHECK_CONDITION;
708		}
709		return rc;
710	}
711
712	usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
713		     srb->cmnd[0], srb->cmnd[0]);
714	info->sense_key = 0x05;
715	info->sense_asc = 0x20;
716	info->sense_ascq = 0x00;
717	return USB_STOR_TRANSPORT_FAILED;
718}
719
720static struct scsi_host_template datafab_host_template;
721
722static int datafab_probe(struct usb_interface *intf,
723			 const struct usb_device_id *id)
724{
725	struct us_data *us;
726	int result;
727
728	result = usb_stor_probe1(&us, intf, id,
729			(id - datafab_usb_ids) + datafab_unusual_dev_list,
730			&datafab_host_template);
731	if (result)
732		return result;
733
734	us->transport_name  = "Datafab Bulk-Only";
735	us->transport = datafab_transport;
736	us->transport_reset = usb_stor_Bulk_reset;
737	us->max_lun = 1;
738
739	result = usb_stor_probe2(us);
740	return result;
741}
742
743static struct usb_driver datafab_driver = {
744	.name =		DRV_NAME,
745	.probe =	datafab_probe,
746	.disconnect =	usb_stor_disconnect,
747	.suspend =	usb_stor_suspend,
748	.resume =	usb_stor_resume,
749	.reset_resume =	usb_stor_reset_resume,
750	.pre_reset =	usb_stor_pre_reset,
751	.post_reset =	usb_stor_post_reset,
752	.id_table =	datafab_usb_ids,
753	.soft_unbind =	1,
754	.no_dynamic_id = 1,
755};
756
757module_usb_stor_driver(datafab_driver, datafab_host_template, DRV_NAME);
v4.6
  1/* Driver for Datafab USB Compact Flash reader
 
 
  2 *
  3 * datafab driver v0.1:
  4 *
  5 * First release
  6 *
  7 * Current development and maintenance by:
  8 *   (c) 2000 Jimmie Mayfield (mayfield+datafab@sackheads.org)
  9 *
 10 *   Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
 11 *   which I used as a template for this driver.
 12 *
 13 *   Some bugfixes and scatter-gather code by Gregory P. Smith 
 14 *   (greg-usb@electricrain.com)
 15 *
 16 *   Fix for media change by Joerg Schneider (js@joergschneider.com)
 17 *
 18 * Other contributors:
 19 *   (c) 2002 Alan Stern <stern@rowland.org>
 20 *
 21 * This program is free software; you can redistribute it and/or modify it
 22 * under the terms of the GNU General Public License as published by the
 23 * Free Software Foundation; either version 2, or (at your option) any
 24 * later version.
 25 *
 26 * This program is distributed in the hope that it will be useful, but
 27 * WITHOUT ANY WARRANTY; without even the implied warranty of
 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 29 * General Public License for more details.
 30 *
 31 * You should have received a copy of the GNU General Public License along
 32 * with this program; if not, write to the Free Software Foundation, Inc.,
 33 * 675 Mass Ave, Cambridge, MA 02139, USA.
 34 */
 35
 36/*
 37 * This driver attempts to support USB CompactFlash reader/writer devices
 38 * based on Datafab USB-to-ATA chips.  It was specifically developed for the 
 39 * Datafab MDCFE-B USB CompactFlash reader but has since been found to work 
 40 * with a variety of Datafab-based devices from a number of manufacturers.
 41 * I've received a report of this driver working with a Datafab-based
 42 * SmartMedia device though please be aware that I'm personally unable to
 43 * test SmartMedia support.
 44 *
 45 * This driver supports reading and writing.  If you're truly paranoid,
 46 * however, you can force the driver into a write-protected state by setting
 47 * the WP enable bits in datafab_handle_mode_sense().  See the comments
 48 * in that routine.
 49 */
 50
 51#include <linux/errno.h>
 52#include <linux/module.h>
 53#include <linux/slab.h>
 54
 55#include <scsi/scsi.h>
 56#include <scsi/scsi_cmnd.h>
 57
 58#include "usb.h"
 59#include "transport.h"
 60#include "protocol.h"
 61#include "debug.h"
 62#include "scsiglue.h"
 63
 64#define DRV_NAME "ums-datafab"
 65
 66MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
 67MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
 68MODULE_LICENSE("GPL");
 
 69
 70struct datafab_info {
 71	unsigned long   sectors;	/* total sector count */
 72	unsigned long   ssize;		/* sector size in bytes */
 73	signed char	lun;		/* used for dual-slot readers */
 74
 75	/* the following aren't used yet */
 76	unsigned char   sense_key;
 77	unsigned long   sense_asc;	/* additional sense code */
 78	unsigned long   sense_ascq;	/* additional sense code qualifier */
 79};
 80
 81static int datafab_determine_lun(struct us_data *us,
 82				 struct datafab_info *info);
 83
 84
 85/*
 86 * The table of devices
 87 */
 88#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
 89		    vendorName, productName, useProtocol, useTransport, \
 90		    initFunction, flags) \
 91{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
 92  .driver_info = (flags) }
 93
 94static struct usb_device_id datafab_usb_ids[] = {
 95#	include "unusual_datafab.h"
 96	{ }		/* Terminating entry */
 97};
 98MODULE_DEVICE_TABLE(usb, datafab_usb_ids);
 99
100#undef UNUSUAL_DEV
101
102/*
103 * The flags table
104 */
105#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
106		    vendor_name, product_name, use_protocol, use_transport, \
107		    init_function, Flags) \
108{ \
109	.vendorName = vendor_name,	\
110	.productName = product_name,	\
111	.useProtocol = use_protocol,	\
112	.useTransport = use_transport,	\
113	.initFunction = init_function,	\
114}
115
116static struct us_unusual_dev datafab_unusual_dev_list[] = {
117#	include "unusual_datafab.h"
118	{ }		/* Terminating entry */
119};
120
121#undef UNUSUAL_DEV
122
123
124static inline int
125datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
126	if (len == 0)
127		return USB_STOR_XFER_GOOD;
128
129	usb_stor_dbg(us, "len = %d\n", len);
130	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
131			data, len, NULL);
132}
133
134
135static inline int
136datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
137	if (len == 0)
138		return USB_STOR_XFER_GOOD;
139
140	usb_stor_dbg(us, "len = %d\n", len);
141	return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
142			data, len, NULL);
143}
144
145
146static int datafab_read_data(struct us_data *us,
147			     struct datafab_info *info,
148			     u32 sector,
149			     u32 sectors)
150{
151	unsigned char *command = us->iobuf;
152	unsigned char *buffer;
153	unsigned char  thistime;
154	unsigned int totallen, alloclen;
155	int len, result;
156	unsigned int sg_offset = 0;
157	struct scatterlist *sg = NULL;
158
159	// we're working in LBA mode.  according to the ATA spec, 
160	// we can support up to 28-bit addressing.  I don't know if Datafab
161	// supports beyond 24-bit addressing.  It's kind of hard to test 
162	// since it requires > 8GB CF card.
163	//
164	if (sectors > 0x0FFFFFFF)
165		return USB_STOR_TRANSPORT_ERROR;
166
167	if (info->lun == -1) {
168		result = datafab_determine_lun(us, info);
169		if (result != USB_STOR_TRANSPORT_GOOD)
170			return result;
171	}
172
173	totallen = sectors * info->ssize;
174
175	// Since we don't read more than 64 KB at a time, we have to create
176	// a bounce buffer and move the data a piece at a time between the
177	// bounce buffer and the actual transfer buffer.
178
179	alloclen = min(totallen, 65536u);
180	buffer = kmalloc(alloclen, GFP_NOIO);
181	if (buffer == NULL)
182		return USB_STOR_TRANSPORT_ERROR;
183
184	do {
185		// loop, never allocate or transfer more than 64k at once
186		// (min(128k, 255*info->ssize) is the real limit)
187
188		len = min(totallen, alloclen);
189		thistime = (len / info->ssize) & 0xff;
190
191		command[0] = 0;
192		command[1] = thistime;
193		command[2] = sector & 0xFF;
194		command[3] = (sector >> 8) & 0xFF;
195		command[4] = (sector >> 16) & 0xFF;
196
197		command[5] = 0xE0 + (info->lun << 4);
198		command[5] |= (sector >> 24) & 0x0F;
199		command[6] = 0x20;
200		command[7] = 0x01;
201
202		// send the read command
203		result = datafab_bulk_write(us, command, 8);
204		if (result != USB_STOR_XFER_GOOD)
205			goto leave;
206
207		// read the result
208		result = datafab_bulk_read(us, buffer, len);
209		if (result != USB_STOR_XFER_GOOD)
210			goto leave;
211
212		// Store the data in the transfer buffer
213		usb_stor_access_xfer_buf(buffer, len, us->srb,
214				 &sg, &sg_offset, TO_XFER_BUF);
215
216		sector += thistime;
217		totallen -= len;
218	} while (totallen > 0);
219
220	kfree(buffer);
221	return USB_STOR_TRANSPORT_GOOD;
222
223 leave:
224	kfree(buffer);
225	return USB_STOR_TRANSPORT_ERROR;
226}
227
228
229static int datafab_write_data(struct us_data *us,
230			      struct datafab_info *info,
231			      u32 sector,
232			      u32 sectors)
233{
234	unsigned char *command = us->iobuf;
235	unsigned char *reply = us->iobuf;
236	unsigned char *buffer;
237	unsigned char thistime;
238	unsigned int totallen, alloclen;
239	int len, result;
240	unsigned int sg_offset = 0;
241	struct scatterlist *sg = NULL;
242
243	// we're working in LBA mode.  according to the ATA spec, 
244	// we can support up to 28-bit addressing.  I don't know if Datafab
245	// supports beyond 24-bit addressing.  It's kind of hard to test 
246	// since it requires > 8GB CF card.
247	//
248	if (sectors > 0x0FFFFFFF)
249		return USB_STOR_TRANSPORT_ERROR;
250
251	if (info->lun == -1) {
252		result = datafab_determine_lun(us, info);
253		if (result != USB_STOR_TRANSPORT_GOOD)
254			return result;
255	}
256
257	totallen = sectors * info->ssize;
258
259	// Since we don't write more than 64 KB at a time, we have to create
260	// a bounce buffer and move the data a piece at a time between the
261	// bounce buffer and the actual transfer buffer.
262
263	alloclen = min(totallen, 65536u);
264	buffer = kmalloc(alloclen, GFP_NOIO);
265	if (buffer == NULL)
266		return USB_STOR_TRANSPORT_ERROR;
267
268	do {
269		// loop, never allocate or transfer more than 64k at once
270		// (min(128k, 255*info->ssize) is the real limit)
271
272		len = min(totallen, alloclen);
273		thistime = (len / info->ssize) & 0xff;
274
275		// Get the data from the transfer buffer
276		usb_stor_access_xfer_buf(buffer, len, us->srb,
277				&sg, &sg_offset, FROM_XFER_BUF);
278
279		command[0] = 0;
280		command[1] = thistime;
281		command[2] = sector & 0xFF;
282		command[3] = (sector >> 8) & 0xFF;
283		command[4] = (sector >> 16) & 0xFF;
284
285		command[5] = 0xE0 + (info->lun << 4);
286		command[5] |= (sector >> 24) & 0x0F;
287		command[6] = 0x30;
288		command[7] = 0x02;
289
290		// send the command
291		result = datafab_bulk_write(us, command, 8);
292		if (result != USB_STOR_XFER_GOOD)
293			goto leave;
294
295		// send the data
296		result = datafab_bulk_write(us, buffer, len);
297		if (result != USB_STOR_XFER_GOOD)
298			goto leave;
299
300		// read the result
301		result = datafab_bulk_read(us, reply, 2);
302		if (result != USB_STOR_XFER_GOOD)
303			goto leave;
304
305		if (reply[0] != 0x50 && reply[1] != 0) {
306			usb_stor_dbg(us, "Gah! write return code: %02x %02x\n",
307				     reply[0], reply[1]);
308			result = USB_STOR_TRANSPORT_ERROR;
309			goto leave;
310		}
311
312		sector += thistime;
313		totallen -= len;
314	} while (totallen > 0);
315
316	kfree(buffer);
317	return USB_STOR_TRANSPORT_GOOD;
318
319 leave:
320	kfree(buffer);
321	return USB_STOR_TRANSPORT_ERROR;
322}
323
324
325static int datafab_determine_lun(struct us_data *us,
326				 struct datafab_info *info)
327{
328	// Dual-slot readers can be thought of as dual-LUN devices.
329	// We need to determine which card slot is being used.
330	// We'll send an IDENTIFY DEVICE command and see which LUN responds...
331	//
332	// There might be a better way of doing this?
333
334	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
335	unsigned char *command = us->iobuf;
336	unsigned char *buf;
337	int count = 0, rc;
338
339	if (!info)
340		return USB_STOR_TRANSPORT_ERROR;
341
342	memcpy(command, scommand, 8);
343	buf = kmalloc(512, GFP_NOIO);
344	if (!buf)
345		return USB_STOR_TRANSPORT_ERROR;
346
347	usb_stor_dbg(us, "locating...\n");
348
349	// we'll try 3 times before giving up...
350	//
351	while (count++ < 3) {
352		command[5] = 0xa0;
353
354		rc = datafab_bulk_write(us, command, 8);
355		if (rc != USB_STOR_XFER_GOOD) {
356			rc = USB_STOR_TRANSPORT_ERROR;
357			goto leave;
358		}
359
360		rc = datafab_bulk_read(us, buf, 512);
361		if (rc == USB_STOR_XFER_GOOD) {
362			info->lun = 0;
363			rc = USB_STOR_TRANSPORT_GOOD;
364			goto leave;
365		}
366
367		command[5] = 0xb0;
368
369		rc = datafab_bulk_write(us, command, 8);
370		if (rc != USB_STOR_XFER_GOOD) {
371			rc = USB_STOR_TRANSPORT_ERROR;
372			goto leave;
373		}
374
375		rc = datafab_bulk_read(us, buf, 512);
376		if (rc == USB_STOR_XFER_GOOD) {
377			info->lun = 1;
378			rc = USB_STOR_TRANSPORT_GOOD;
379			goto leave;
380		}
381
382		msleep(20);
383	}
384
385	rc = USB_STOR_TRANSPORT_ERROR;
386
387 leave:
388	kfree(buf);
389	return rc;
390}
391
392static int datafab_id_device(struct us_data *us,
393			     struct datafab_info *info)
394{
395	// this is a variation of the ATA "IDENTIFY DEVICE" command...according
396	// to the ATA spec, 'Sector Count' isn't used but the Windows driver
397	// sets this bit so we do too...
398	//
399	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
400	unsigned char *command = us->iobuf;
401	unsigned char *reply;
402	int rc;
403
404	if (!info)
405		return USB_STOR_TRANSPORT_ERROR;
406
407	if (info->lun == -1) {
408		rc = datafab_determine_lun(us, info);
409		if (rc != USB_STOR_TRANSPORT_GOOD)
410			return rc;
411	}
412
413	memcpy(command, scommand, 8);
414	reply = kmalloc(512, GFP_NOIO);
415	if (!reply)
416		return USB_STOR_TRANSPORT_ERROR;
417
418	command[5] += (info->lun << 4);
419
420	rc = datafab_bulk_write(us, command, 8);
421	if (rc != USB_STOR_XFER_GOOD) {
422		rc = USB_STOR_TRANSPORT_ERROR;
423		goto leave;
424	}
425
426	// we'll go ahead and extract the media capacity while we're here...
427	//
428	rc = datafab_bulk_read(us, reply, 512);
429	if (rc == USB_STOR_XFER_GOOD) {
430		// capacity is at word offset 57-58
431		//
432		info->sectors = ((u32)(reply[117]) << 24) | 
433				((u32)(reply[116]) << 16) |
434				((u32)(reply[115]) <<  8) | 
435				((u32)(reply[114])      );
436		rc = USB_STOR_TRANSPORT_GOOD;
437		goto leave;
438	}
439
440	rc = USB_STOR_TRANSPORT_ERROR;
441
442 leave:
443	kfree(reply);
444	return rc;
445}
446
447
448static int datafab_handle_mode_sense(struct us_data *us,
449				     struct scsi_cmnd * srb, 
450				     int sense_6)
451{
452	static unsigned char rw_err_page[12] = {
453		0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
454	};
455	static unsigned char cache_page[12] = {
456		0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
457	};
458	static unsigned char rbac_page[12] = {
459		0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
460	};
461	static unsigned char timer_page[8] = {
462		0x1C, 0x6, 0, 0, 0, 0
463	};
464	unsigned char pc, page_code;
465	unsigned int i = 0;
466	struct datafab_info *info = (struct datafab_info *) (us->extra);
467	unsigned char *ptr = us->iobuf;
468
469	// most of this stuff is just a hack to get things working.  the
470	// datafab reader doesn't present a SCSI interface so we
471	// fudge the SCSI commands...
472	//
473
474	pc = srb->cmnd[2] >> 6;
475	page_code = srb->cmnd[2] & 0x3F;
476
477	switch (pc) {
478	   case 0x0:
479		   usb_stor_dbg(us, "Current values\n");
480		break;
481	   case 0x1:
482		   usb_stor_dbg(us, "Changeable values\n");
483		break;
484	   case 0x2:
485		   usb_stor_dbg(us, "Default values\n");
486		break;
487	   case 0x3:
488		   usb_stor_dbg(us, "Saves values\n");
489		break;
490	}
491
492	memset(ptr, 0, 8);
493	if (sense_6) {
494		ptr[2] = 0x00;		// WP enable: 0x80
495		i = 4;
496	} else {
497		ptr[3] = 0x00;		// WP enable: 0x80
498		i = 8;
499	}
500
501	switch (page_code) {
502	   default:
503		// vendor-specific mode
504		info->sense_key = 0x05;
505		info->sense_asc = 0x24;
506		info->sense_ascq = 0x00;
507		return USB_STOR_TRANSPORT_FAILED;
508
509	   case 0x1:
510		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
511		i += sizeof(rw_err_page);
512		break;
513
514	   case 0x8:
515		memcpy(ptr + i, cache_page, sizeof(cache_page));
516		i += sizeof(cache_page);
517		break;
518
519	   case 0x1B:
520		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
521		i += sizeof(rbac_page);
522		break;
523
524	   case 0x1C:
525		memcpy(ptr + i, timer_page, sizeof(timer_page));
526		i += sizeof(timer_page);
527		break;
528
529	   case 0x3F:		// retrieve all pages
530		memcpy(ptr + i, timer_page, sizeof(timer_page));
531		i += sizeof(timer_page);
532		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
533		i += sizeof(rbac_page);
534		memcpy(ptr + i, cache_page, sizeof(cache_page));
535		i += sizeof(cache_page);
536		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
537		i += sizeof(rw_err_page);
538		break;
539	}
540
541	if (sense_6)
542		ptr[0] = i - 1;
543	else
544		((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
545	usb_stor_set_xfer_buf(ptr, i, srb);
546
547	return USB_STOR_TRANSPORT_GOOD;
548}
549
550static void datafab_info_destructor(void *extra)
551{
552	// this routine is a placeholder...
553	// currently, we don't allocate any extra memory so we're okay
554}
555
556
557// Transport for the Datafab MDCFE-B
558//
559static int datafab_transport(struct scsi_cmnd *srb, struct us_data *us)
560{
561	struct datafab_info *info;
562	int rc;
563	unsigned long block, blocks;
564	unsigned char *ptr = us->iobuf;
565	static unsigned char inquiry_reply[8] = {
566		0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
567	};
568
569	if (!us->extra) {
570		us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
571		if (!us->extra)
572			return USB_STOR_TRANSPORT_ERROR;
573
574		us->extra_destructor = datafab_info_destructor;
575  		((struct datafab_info *)us->extra)->lun = -1;
576	}
577
578	info = (struct datafab_info *) (us->extra);
579
580	if (srb->cmnd[0] == INQUIRY) {
581		usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
582		memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
583		fill_inquiry_response(us, ptr, 36);
584		return USB_STOR_TRANSPORT_GOOD;
585	}
586
587	if (srb->cmnd[0] == READ_CAPACITY) {
588		info->ssize = 0x200;  // hard coded 512 byte sectors as per ATA spec
589		rc = datafab_id_device(us, info);
590		if (rc != USB_STOR_TRANSPORT_GOOD)
591			return rc;
592
593		usb_stor_dbg(us, "READ_CAPACITY:  %ld sectors, %ld bytes per sector\n",
594			     info->sectors, info->ssize);
595
596		// build the reply
597		// we need the last sector, not the number of sectors
598		((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
599		((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
600		usb_stor_set_xfer_buf(ptr, 8, srb);
601
602		return USB_STOR_TRANSPORT_GOOD;
603	}
604
605	if (srb->cmnd[0] == MODE_SELECT_10) {
606		usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
607		return USB_STOR_TRANSPORT_ERROR;
608	}
609
610	// don't bother implementing READ_6 or WRITE_6.
611	//
612	if (srb->cmnd[0] == READ_10) {
613		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
614			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
615
616		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
617
618		usb_stor_dbg(us, "READ_10: read block 0x%04lx  count %ld\n",
619			     block, blocks);
620		return datafab_read_data(us, info, block, blocks);
621	}
622
623	if (srb->cmnd[0] == READ_12) {
624		// we'll probably never see a READ_12 but we'll do it anyway...
625		//
626		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
627			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
628
629		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
630			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
631
632		usb_stor_dbg(us, "READ_12: read block 0x%04lx  count %ld\n",
633			     block, blocks);
634		return datafab_read_data(us, info, block, blocks);
635	}
636
637	if (srb->cmnd[0] == WRITE_10) {
638		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
639			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
640
641		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
642
643		usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
644			     block, blocks);
645		return datafab_write_data(us, info, block, blocks);
646	}
647
648	if (srb->cmnd[0] == WRITE_12) {
649		// we'll probably never see a WRITE_12 but we'll do it anyway...
650		//
651		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
652			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
653
654		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
655			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
656
657		usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
658			     block, blocks);
659		return datafab_write_data(us, info, block, blocks);
660	}
661
662	if (srb->cmnd[0] == TEST_UNIT_READY) {
663		usb_stor_dbg(us, "TEST_UNIT_READY\n");
664		return datafab_id_device(us, info);
665	}
666
667	if (srb->cmnd[0] == REQUEST_SENSE) {
668		usb_stor_dbg(us, "REQUEST_SENSE - Returning faked response\n");
669
670		// this response is pretty bogus right now.  eventually if necessary
671		// we can set the correct sense data.  so far though it hasn't been
672		// necessary
673		//
674		memset(ptr, 0, 18);
675		ptr[0] = 0xF0;
676		ptr[2] = info->sense_key;
677		ptr[7] = 11;
678		ptr[12] = info->sense_asc;
679		ptr[13] = info->sense_ascq;
680		usb_stor_set_xfer_buf(ptr, 18, srb);
681
682		return USB_STOR_TRANSPORT_GOOD;
683	}
684
685	if (srb->cmnd[0] == MODE_SENSE) {
686		usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
687		return datafab_handle_mode_sense(us, srb, 1);
688	}
689
690	if (srb->cmnd[0] == MODE_SENSE_10) {
691		usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
692		return datafab_handle_mode_sense(us, srb, 0);
693	}
694
695	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
696		// sure.  whatever.  not like we can stop the user from
697		// popping the media out of the device (no locking doors, etc)
698		//
 
699		return USB_STOR_TRANSPORT_GOOD;
700	}
701
702	if (srb->cmnd[0] == START_STOP) {
703		/* this is used by sd.c'check_scsidisk_media_change to detect
704		   media change */
 
 
705		usb_stor_dbg(us, "START_STOP\n");
706		/* the first datafab_id_device after a media change returns
707		   an error (determined experimentally) */
 
 
708		rc = datafab_id_device(us, info);
709		if (rc == USB_STOR_TRANSPORT_GOOD) {
710			info->sense_key = NO_SENSE;
711			srb->result = SUCCESS;
712		} else {
713			info->sense_key = UNIT_ATTENTION;
714			srb->result = SAM_STAT_CHECK_CONDITION;
715		}
716		return rc;
717	}
718
719	usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
720		     srb->cmnd[0], srb->cmnd[0]);
721	info->sense_key = 0x05;
722	info->sense_asc = 0x20;
723	info->sense_ascq = 0x00;
724	return USB_STOR_TRANSPORT_FAILED;
725}
726
727static struct scsi_host_template datafab_host_template;
728
729static int datafab_probe(struct usb_interface *intf,
730			 const struct usb_device_id *id)
731{
732	struct us_data *us;
733	int result;
734
735	result = usb_stor_probe1(&us, intf, id,
736			(id - datafab_usb_ids) + datafab_unusual_dev_list,
737			&datafab_host_template);
738	if (result)
739		return result;
740
741	us->transport_name  = "Datafab Bulk-Only";
742	us->transport = datafab_transport;
743	us->transport_reset = usb_stor_Bulk_reset;
744	us->max_lun = 1;
745
746	result = usb_stor_probe2(us);
747	return result;
748}
749
750static struct usb_driver datafab_driver = {
751	.name =		DRV_NAME,
752	.probe =	datafab_probe,
753	.disconnect =	usb_stor_disconnect,
754	.suspend =	usb_stor_suspend,
755	.resume =	usb_stor_resume,
756	.reset_resume =	usb_stor_reset_resume,
757	.pre_reset =	usb_stor_pre_reset,
758	.post_reset =	usb_stor_post_reset,
759	.id_table =	datafab_usb_ids,
760	.soft_unbind =	1,
761	.no_dynamic_id = 1,
762};
763
764module_usb_stor_driver(datafab_driver, datafab_host_template, DRV_NAME);