Linux Audio

Check our new training course

Loading...
v5.9
  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 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 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			result = USB_STOR_TRANSPORT_ERROR;
298			goto leave;
299		}
300
301		sector += thistime;
302		totallen -= len;
303	} while (totallen > 0);
304
305	kfree(buffer);
306	return USB_STOR_TRANSPORT_GOOD;
307
308 leave:
309	kfree(buffer);
310	return USB_STOR_TRANSPORT_ERROR;
311}
312
313
314static int datafab_determine_lun(struct us_data *us,
315				 struct datafab_info *info)
316{
317	// Dual-slot readers can be thought of as dual-LUN devices.
318	// We need to determine which card slot is being used.
319	// We'll send an IDENTIFY DEVICE command and see which LUN responds...
320	//
321	// There might be a better way of doing this?
322
323	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
324	unsigned char *command = us->iobuf;
325	unsigned char *buf;
326	int count = 0, rc;
327
328	if (!info)
329		return USB_STOR_TRANSPORT_ERROR;
330
331	memcpy(command, scommand, 8);
332	buf = kmalloc(512, GFP_NOIO);
333	if (!buf)
334		return USB_STOR_TRANSPORT_ERROR;
335
336	usb_stor_dbg(us, "locating...\n");
337
338	// we'll try 3 times before giving up...
339	//
340	while (count++ < 3) {
341		command[5] = 0xa0;
342
343		rc = datafab_bulk_write(us, command, 8);
344		if (rc != USB_STOR_XFER_GOOD) {
345			rc = USB_STOR_TRANSPORT_ERROR;
346			goto leave;
347		}
348
349		rc = datafab_bulk_read(us, buf, 512);
350		if (rc == USB_STOR_XFER_GOOD) {
351			info->lun = 0;
352			rc = USB_STOR_TRANSPORT_GOOD;
353			goto leave;
354		}
355
356		command[5] = 0xb0;
357
358		rc = datafab_bulk_write(us, command, 8);
359		if (rc != USB_STOR_XFER_GOOD) {
360			rc = USB_STOR_TRANSPORT_ERROR;
361			goto leave;
362		}
363
364		rc = datafab_bulk_read(us, buf, 512);
365		if (rc == USB_STOR_XFER_GOOD) {
366			info->lun = 1;
367			rc = USB_STOR_TRANSPORT_GOOD;
368			goto leave;
369		}
370
371		msleep(20);
372	}
373
374	rc = USB_STOR_TRANSPORT_ERROR;
375
376 leave:
377	kfree(buf);
378	return rc;
379}
380
381static int datafab_id_device(struct us_data *us,
382			     struct datafab_info *info)
383{
384	// this is a variation of the ATA "IDENTIFY DEVICE" command...according
385	// to the ATA spec, 'Sector Count' isn't used but the Windows driver
386	// sets this bit so we do too...
387	//
388	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
389	unsigned char *command = us->iobuf;
390	unsigned char *reply;
391	int rc;
392
393	if (!info)
394		return USB_STOR_TRANSPORT_ERROR;
395
396	if (info->lun == -1) {
397		rc = datafab_determine_lun(us, info);
398		if (rc != USB_STOR_TRANSPORT_GOOD)
399			return rc;
400	}
401
402	memcpy(command, scommand, 8);
403	reply = kmalloc(512, GFP_NOIO);
404	if (!reply)
405		return USB_STOR_TRANSPORT_ERROR;
406
407	command[5] += (info->lun << 4);
408
409	rc = datafab_bulk_write(us, command, 8);
410	if (rc != USB_STOR_XFER_GOOD) {
411		rc = USB_STOR_TRANSPORT_ERROR;
412		goto leave;
413	}
414
415	// we'll go ahead and extract the media capacity while we're here...
416	//
417	rc = datafab_bulk_read(us, reply, 512);
418	if (rc == USB_STOR_XFER_GOOD) {
419		// capacity is at word offset 57-58
420		//
421		info->sectors = ((u32)(reply[117]) << 24) | 
422				((u32)(reply[116]) << 16) |
423				((u32)(reply[115]) <<  8) | 
424				((u32)(reply[114])      );
425		rc = USB_STOR_TRANSPORT_GOOD;
426		goto leave;
427	}
428
429	rc = USB_STOR_TRANSPORT_ERROR;
430
431 leave:
432	kfree(reply);
433	return rc;
434}
435
436
437static int datafab_handle_mode_sense(struct us_data *us,
438				     struct scsi_cmnd * srb, 
439				     int sense_6)
440{
441	static unsigned char rw_err_page[12] = {
442		0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
443	};
444	static unsigned char cache_page[12] = {
445		0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
446	};
447	static unsigned char rbac_page[12] = {
448		0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
449	};
450	static unsigned char timer_page[8] = {
451		0x1C, 0x6, 0, 0, 0, 0
452	};
453	unsigned char pc, page_code;
454	unsigned int i = 0;
455	struct datafab_info *info = (struct datafab_info *) (us->extra);
456	unsigned char *ptr = us->iobuf;
457
458	// most of this stuff is just a hack to get things working.  the
459	// datafab reader doesn't present a SCSI interface so we
460	// fudge the SCSI commands...
461	//
462
463	pc = srb->cmnd[2] >> 6;
464	page_code = srb->cmnd[2] & 0x3F;
465
466	switch (pc) {
467	   case 0x0:
468		   usb_stor_dbg(us, "Current values\n");
469		break;
470	   case 0x1:
471		   usb_stor_dbg(us, "Changeable values\n");
472		break;
473	   case 0x2:
474		   usb_stor_dbg(us, "Default values\n");
475		break;
476	   case 0x3:
477		   usb_stor_dbg(us, "Saves values\n");
478		break;
479	}
480
481	memset(ptr, 0, 8);
482	if (sense_6) {
483		ptr[2] = 0x00;		// WP enable: 0x80
484		i = 4;
485	} else {
486		ptr[3] = 0x00;		// WP enable: 0x80
487		i = 8;
488	}
489
490	switch (page_code) {
491	   default:
492		// vendor-specific mode
493		info->sense_key = 0x05;
494		info->sense_asc = 0x24;
495		info->sense_ascq = 0x00;
496		return USB_STOR_TRANSPORT_FAILED;
497
498	   case 0x1:
499		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
500		i += sizeof(rw_err_page);
501		break;
502
503	   case 0x8:
504		memcpy(ptr + i, cache_page, sizeof(cache_page));
505		i += sizeof(cache_page);
506		break;
507
508	   case 0x1B:
509		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
510		i += sizeof(rbac_page);
511		break;
512
513	   case 0x1C:
514		memcpy(ptr + i, timer_page, sizeof(timer_page));
515		i += sizeof(timer_page);
516		break;
517
518	   case 0x3F:		// retrieve all pages
519		memcpy(ptr + i, timer_page, sizeof(timer_page));
520		i += sizeof(timer_page);
521		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
522		i += sizeof(rbac_page);
523		memcpy(ptr + i, cache_page, sizeof(cache_page));
524		i += sizeof(cache_page);
525		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
526		i += sizeof(rw_err_page);
527		break;
528	}
529
530	if (sense_6)
531		ptr[0] = i - 1;
532	else
533		((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
534	usb_stor_set_xfer_buf(ptr, i, srb);
535
536	return USB_STOR_TRANSPORT_GOOD;
537}
538
539static void datafab_info_destructor(void *extra)
540{
541	// this routine is a placeholder...
542	// currently, we don't allocate any extra memory so we're okay
543}
544
545
546// Transport for the Datafab MDCFE-B
547//
548static int datafab_transport(struct scsi_cmnd *srb, struct us_data *us)
549{
550	struct datafab_info *info;
551	int rc;
552	unsigned long block, blocks;
553	unsigned char *ptr = us->iobuf;
554	static unsigned char inquiry_reply[8] = {
555		0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
556	};
557
558	if (!us->extra) {
559		us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
560		if (!us->extra)
561			return USB_STOR_TRANSPORT_ERROR;
562
563		us->extra_destructor = datafab_info_destructor;
564  		((struct datafab_info *)us->extra)->lun = -1;
565	}
566
567	info = (struct datafab_info *) (us->extra);
568
569	if (srb->cmnd[0] == INQUIRY) {
570		usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
571		memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
572		fill_inquiry_response(us, ptr, 36);
573		return USB_STOR_TRANSPORT_GOOD;
574	}
575
576	if (srb->cmnd[0] == READ_CAPACITY) {
577		info->ssize = 0x200;  // hard coded 512 byte sectors as per ATA spec
578		rc = datafab_id_device(us, info);
579		if (rc != USB_STOR_TRANSPORT_GOOD)
580			return rc;
581
582		usb_stor_dbg(us, "READ_CAPACITY:  %ld sectors, %ld bytes per sector\n",
583			     info->sectors, info->ssize);
584
585		// build the reply
586		// we need the last sector, not the number of sectors
587		((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
588		((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
589		usb_stor_set_xfer_buf(ptr, 8, srb);
590
591		return USB_STOR_TRANSPORT_GOOD;
592	}
593
594	if (srb->cmnd[0] == MODE_SELECT_10) {
595		usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
596		return USB_STOR_TRANSPORT_ERROR;
597	}
598
599	// don't bother implementing READ_6 or WRITE_6.
600	//
601	if (srb->cmnd[0] == READ_10) {
602		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
603			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
604
605		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
606
607		usb_stor_dbg(us, "READ_10: read block 0x%04lx  count %ld\n",
608			     block, blocks);
609		return datafab_read_data(us, info, block, blocks);
610	}
611
612	if (srb->cmnd[0] == READ_12) {
613		// we'll probably never see a READ_12 but we'll do it anyway...
614		//
615		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
616			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
617
618		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
619			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
620
621		usb_stor_dbg(us, "READ_12: read block 0x%04lx  count %ld\n",
622			     block, blocks);
623		return datafab_read_data(us, info, block, blocks);
624	}
625
626	if (srb->cmnd[0] == WRITE_10) {
627		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
628			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
629
630		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
631
632		usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
633			     block, blocks);
634		return datafab_write_data(us, info, block, blocks);
635	}
636
637	if (srb->cmnd[0] == WRITE_12) {
638		// we'll probably never see a WRITE_12 but we'll do it anyway...
639		//
640		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
641			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
642
643		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
644			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
645
646		usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
647			     block, blocks);
648		return datafab_write_data(us, info, block, blocks);
649	}
650
651	if (srb->cmnd[0] == TEST_UNIT_READY) {
652		usb_stor_dbg(us, "TEST_UNIT_READY\n");
653		return datafab_id_device(us, info);
654	}
655
656	if (srb->cmnd[0] == REQUEST_SENSE) {
657		usb_stor_dbg(us, "REQUEST_SENSE - Returning faked response\n");
658
659		// this response is pretty bogus right now.  eventually if necessary
660		// we can set the correct sense data.  so far though it hasn't been
661		// necessary
662		//
663		memset(ptr, 0, 18);
664		ptr[0] = 0xF0;
665		ptr[2] = info->sense_key;
666		ptr[7] = 11;
667		ptr[12] = info->sense_asc;
668		ptr[13] = info->sense_ascq;
669		usb_stor_set_xfer_buf(ptr, 18, srb);
670
671		return USB_STOR_TRANSPORT_GOOD;
672	}
673
674	if (srb->cmnd[0] == MODE_SENSE) {
675		usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
676		return datafab_handle_mode_sense(us, srb, 1);
677	}
678
679	if (srb->cmnd[0] == MODE_SENSE_10) {
680		usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
681		return datafab_handle_mode_sense(us, srb, 0);
682	}
683
684	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
685		/*
686		 * sure.  whatever.  not like we can stop the user from
687		 * popping the media out of the device (no locking doors, etc)
688		 */
689		return USB_STOR_TRANSPORT_GOOD;
690	}
691
692	if (srb->cmnd[0] == START_STOP) {
693		/*
694		 * this is used by sd.c'check_scsidisk_media_change to detect
695		 * media change
696		 */
697		usb_stor_dbg(us, "START_STOP\n");
698		/*
699		 * the first datafab_id_device after a media change returns
700		 * an error (determined experimentally)
701		 */
702		rc = datafab_id_device(us, info);
703		if (rc == USB_STOR_TRANSPORT_GOOD) {
704			info->sense_key = NO_SENSE;
705			srb->result = SUCCESS;
706		} else {
707			info->sense_key = UNIT_ATTENTION;
708			srb->result = SAM_STAT_CHECK_CONDITION;
709		}
710		return rc;
711	}
712
713	usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
714		     srb->cmnd[0], srb->cmnd[0]);
715	info->sense_key = 0x05;
716	info->sense_asc = 0x20;
717	info->sense_ascq = 0x00;
718	return USB_STOR_TRANSPORT_FAILED;
719}
720
721static struct scsi_host_template datafab_host_template;
722
723static int datafab_probe(struct usb_interface *intf,
724			 const struct usb_device_id *id)
725{
726	struct us_data *us;
727	int result;
728
729	result = usb_stor_probe1(&us, intf, id,
730			(id - datafab_usb_ids) + datafab_unusual_dev_list,
731			&datafab_host_template);
732	if (result)
733		return result;
734
735	us->transport_name  = "Datafab Bulk-Only";
736	us->transport = datafab_transport;
737	us->transport_reset = usb_stor_Bulk_reset;
738	us->max_lun = 1;
739
740	result = usb_stor_probe2(us);
741	return result;
742}
743
744static struct usb_driver datafab_driver = {
745	.name =		DRV_NAME,
746	.probe =	datafab_probe,
747	.disconnect =	usb_stor_disconnect,
748	.suspend =	usb_stor_suspend,
749	.resume =	usb_stor_resume,
750	.reset_resume =	usb_stor_reset_resume,
751	.pre_reset =	usb_stor_pre_reset,
752	.post_reset =	usb_stor_post_reset,
753	.id_table =	datafab_usb_ids,
754	.soft_unbind =	1,
755	.no_dynamic_id = 1,
756};
757
758module_usb_stor_driver(datafab_driver, datafab_host_template, DRV_NAME);
v5.4
  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 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 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			result = USB_STOR_TRANSPORT_ERROR;
298			goto leave;
299		}
300
301		sector += thistime;
302		totallen -= len;
303	} while (totallen > 0);
304
305	kfree(buffer);
306	return USB_STOR_TRANSPORT_GOOD;
307
308 leave:
309	kfree(buffer);
310	return USB_STOR_TRANSPORT_ERROR;
311}
312
313
314static int datafab_determine_lun(struct us_data *us,
315				 struct datafab_info *info)
316{
317	// Dual-slot readers can be thought of as dual-LUN devices.
318	// We need to determine which card slot is being used.
319	// We'll send an IDENTIFY DEVICE command and see which LUN responds...
320	//
321	// There might be a better way of doing this?
322
323	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
324	unsigned char *command = us->iobuf;
325	unsigned char *buf;
326	int count = 0, rc;
327
328	if (!info)
329		return USB_STOR_TRANSPORT_ERROR;
330
331	memcpy(command, scommand, 8);
332	buf = kmalloc(512, GFP_NOIO);
333	if (!buf)
334		return USB_STOR_TRANSPORT_ERROR;
335
336	usb_stor_dbg(us, "locating...\n");
337
338	// we'll try 3 times before giving up...
339	//
340	while (count++ < 3) {
341		command[5] = 0xa0;
342
343		rc = datafab_bulk_write(us, command, 8);
344		if (rc != USB_STOR_XFER_GOOD) {
345			rc = USB_STOR_TRANSPORT_ERROR;
346			goto leave;
347		}
348
349		rc = datafab_bulk_read(us, buf, 512);
350		if (rc == USB_STOR_XFER_GOOD) {
351			info->lun = 0;
352			rc = USB_STOR_TRANSPORT_GOOD;
353			goto leave;
354		}
355
356		command[5] = 0xb0;
357
358		rc = datafab_bulk_write(us, command, 8);
359		if (rc != USB_STOR_XFER_GOOD) {
360			rc = USB_STOR_TRANSPORT_ERROR;
361			goto leave;
362		}
363
364		rc = datafab_bulk_read(us, buf, 512);
365		if (rc == USB_STOR_XFER_GOOD) {
366			info->lun = 1;
367			rc = USB_STOR_TRANSPORT_GOOD;
368			goto leave;
369		}
370
371		msleep(20);
372	}
373
374	rc = USB_STOR_TRANSPORT_ERROR;
375
376 leave:
377	kfree(buf);
378	return rc;
379}
380
381static int datafab_id_device(struct us_data *us,
382			     struct datafab_info *info)
383{
384	// this is a variation of the ATA "IDENTIFY DEVICE" command...according
385	// to the ATA spec, 'Sector Count' isn't used but the Windows driver
386	// sets this bit so we do too...
387	//
388	static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
389	unsigned char *command = us->iobuf;
390	unsigned char *reply;
391	int rc;
392
393	if (!info)
394		return USB_STOR_TRANSPORT_ERROR;
395
396	if (info->lun == -1) {
397		rc = datafab_determine_lun(us, info);
398		if (rc != USB_STOR_TRANSPORT_GOOD)
399			return rc;
400	}
401
402	memcpy(command, scommand, 8);
403	reply = kmalloc(512, GFP_NOIO);
404	if (!reply)
405		return USB_STOR_TRANSPORT_ERROR;
406
407	command[5] += (info->lun << 4);
408
409	rc = datafab_bulk_write(us, command, 8);
410	if (rc != USB_STOR_XFER_GOOD) {
411		rc = USB_STOR_TRANSPORT_ERROR;
412		goto leave;
413	}
414
415	// we'll go ahead and extract the media capacity while we're here...
416	//
417	rc = datafab_bulk_read(us, reply, 512);
418	if (rc == USB_STOR_XFER_GOOD) {
419		// capacity is at word offset 57-58
420		//
421		info->sectors = ((u32)(reply[117]) << 24) | 
422				((u32)(reply[116]) << 16) |
423				((u32)(reply[115]) <<  8) | 
424				((u32)(reply[114])      );
425		rc = USB_STOR_TRANSPORT_GOOD;
426		goto leave;
427	}
428
429	rc = USB_STOR_TRANSPORT_ERROR;
430
431 leave:
432	kfree(reply);
433	return rc;
434}
435
436
437static int datafab_handle_mode_sense(struct us_data *us,
438				     struct scsi_cmnd * srb, 
439				     int sense_6)
440{
441	static unsigned char rw_err_page[12] = {
442		0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
443	};
444	static unsigned char cache_page[12] = {
445		0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
446	};
447	static unsigned char rbac_page[12] = {
448		0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
449	};
450	static unsigned char timer_page[8] = {
451		0x1C, 0x6, 0, 0, 0, 0
452	};
453	unsigned char pc, page_code;
454	unsigned int i = 0;
455	struct datafab_info *info = (struct datafab_info *) (us->extra);
456	unsigned char *ptr = us->iobuf;
457
458	// most of this stuff is just a hack to get things working.  the
459	// datafab reader doesn't present a SCSI interface so we
460	// fudge the SCSI commands...
461	//
462
463	pc = srb->cmnd[2] >> 6;
464	page_code = srb->cmnd[2] & 0x3F;
465
466	switch (pc) {
467	   case 0x0:
468		   usb_stor_dbg(us, "Current values\n");
469		break;
470	   case 0x1:
471		   usb_stor_dbg(us, "Changeable values\n");
472		break;
473	   case 0x2:
474		   usb_stor_dbg(us, "Default values\n");
475		break;
476	   case 0x3:
477		   usb_stor_dbg(us, "Saves values\n");
478		break;
479	}
480
481	memset(ptr, 0, 8);
482	if (sense_6) {
483		ptr[2] = 0x00;		// WP enable: 0x80
484		i = 4;
485	} else {
486		ptr[3] = 0x00;		// WP enable: 0x80
487		i = 8;
488	}
489
490	switch (page_code) {
491	   default:
492		// vendor-specific mode
493		info->sense_key = 0x05;
494		info->sense_asc = 0x24;
495		info->sense_ascq = 0x00;
496		return USB_STOR_TRANSPORT_FAILED;
497
498	   case 0x1:
499		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
500		i += sizeof(rw_err_page);
501		break;
502
503	   case 0x8:
504		memcpy(ptr + i, cache_page, sizeof(cache_page));
505		i += sizeof(cache_page);
506		break;
507
508	   case 0x1B:
509		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
510		i += sizeof(rbac_page);
511		break;
512
513	   case 0x1C:
514		memcpy(ptr + i, timer_page, sizeof(timer_page));
515		i += sizeof(timer_page);
516		break;
517
518	   case 0x3F:		// retrieve all pages
519		memcpy(ptr + i, timer_page, sizeof(timer_page));
520		i += sizeof(timer_page);
521		memcpy(ptr + i, rbac_page, sizeof(rbac_page));
522		i += sizeof(rbac_page);
523		memcpy(ptr + i, cache_page, sizeof(cache_page));
524		i += sizeof(cache_page);
525		memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
526		i += sizeof(rw_err_page);
527		break;
528	}
529
530	if (sense_6)
531		ptr[0] = i - 1;
532	else
533		((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
534	usb_stor_set_xfer_buf(ptr, i, srb);
535
536	return USB_STOR_TRANSPORT_GOOD;
537}
538
539static void datafab_info_destructor(void *extra)
540{
541	// this routine is a placeholder...
542	// currently, we don't allocate any extra memory so we're okay
543}
544
545
546// Transport for the Datafab MDCFE-B
547//
548static int datafab_transport(struct scsi_cmnd *srb, struct us_data *us)
549{
550	struct datafab_info *info;
551	int rc;
552	unsigned long block, blocks;
553	unsigned char *ptr = us->iobuf;
554	static unsigned char inquiry_reply[8] = {
555		0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
556	};
557
558	if (!us->extra) {
559		us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
560		if (!us->extra)
561			return USB_STOR_TRANSPORT_ERROR;
562
563		us->extra_destructor = datafab_info_destructor;
564  		((struct datafab_info *)us->extra)->lun = -1;
565	}
566
567	info = (struct datafab_info *) (us->extra);
568
569	if (srb->cmnd[0] == INQUIRY) {
570		usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
571		memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
572		fill_inquiry_response(us, ptr, 36);
573		return USB_STOR_TRANSPORT_GOOD;
574	}
575
576	if (srb->cmnd[0] == READ_CAPACITY) {
577		info->ssize = 0x200;  // hard coded 512 byte sectors as per ATA spec
578		rc = datafab_id_device(us, info);
579		if (rc != USB_STOR_TRANSPORT_GOOD)
580			return rc;
581
582		usb_stor_dbg(us, "READ_CAPACITY:  %ld sectors, %ld bytes per sector\n",
583			     info->sectors, info->ssize);
584
585		// build the reply
586		// we need the last sector, not the number of sectors
587		((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
588		((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
589		usb_stor_set_xfer_buf(ptr, 8, srb);
590
591		return USB_STOR_TRANSPORT_GOOD;
592	}
593
594	if (srb->cmnd[0] == MODE_SELECT_10) {
595		usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
596		return USB_STOR_TRANSPORT_ERROR;
597	}
598
599	// don't bother implementing READ_6 or WRITE_6.
600	//
601	if (srb->cmnd[0] == READ_10) {
602		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
603			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
604
605		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
606
607		usb_stor_dbg(us, "READ_10: read block 0x%04lx  count %ld\n",
608			     block, blocks);
609		return datafab_read_data(us, info, block, blocks);
610	}
611
612	if (srb->cmnd[0] == READ_12) {
613		// we'll probably never see a READ_12 but we'll do it anyway...
614		//
615		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
616			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
617
618		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
619			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
620
621		usb_stor_dbg(us, "READ_12: read block 0x%04lx  count %ld\n",
622			     block, blocks);
623		return datafab_read_data(us, info, block, blocks);
624	}
625
626	if (srb->cmnd[0] == WRITE_10) {
627		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
628			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
629
630		blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
631
632		usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
633			     block, blocks);
634		return datafab_write_data(us, info, block, blocks);
635	}
636
637	if (srb->cmnd[0] == WRITE_12) {
638		// we'll probably never see a WRITE_12 but we'll do it anyway...
639		//
640		block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
641			((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
642
643		blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
644			 ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
645
646		usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
647			     block, blocks);
648		return datafab_write_data(us, info, block, blocks);
649	}
650
651	if (srb->cmnd[0] == TEST_UNIT_READY) {
652		usb_stor_dbg(us, "TEST_UNIT_READY\n");
653		return datafab_id_device(us, info);
654	}
655
656	if (srb->cmnd[0] == REQUEST_SENSE) {
657		usb_stor_dbg(us, "REQUEST_SENSE - Returning faked response\n");
658
659		// this response is pretty bogus right now.  eventually if necessary
660		// we can set the correct sense data.  so far though it hasn't been
661		// necessary
662		//
663		memset(ptr, 0, 18);
664		ptr[0] = 0xF0;
665		ptr[2] = info->sense_key;
666		ptr[7] = 11;
667		ptr[12] = info->sense_asc;
668		ptr[13] = info->sense_ascq;
669		usb_stor_set_xfer_buf(ptr, 18, srb);
670
671		return USB_STOR_TRANSPORT_GOOD;
672	}
673
674	if (srb->cmnd[0] == MODE_SENSE) {
675		usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
676		return datafab_handle_mode_sense(us, srb, 1);
677	}
678
679	if (srb->cmnd[0] == MODE_SENSE_10) {
680		usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
681		return datafab_handle_mode_sense(us, srb, 0);
682	}
683
684	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
685		/*
686		 * sure.  whatever.  not like we can stop the user from
687		 * popping the media out of the device (no locking doors, etc)
688		 */
689		return USB_STOR_TRANSPORT_GOOD;
690	}
691
692	if (srb->cmnd[0] == START_STOP) {
693		/*
694		 * this is used by sd.c'check_scsidisk_media_change to detect
695		 * media change
696		 */
697		usb_stor_dbg(us, "START_STOP\n");
698		/*
699		 * the first datafab_id_device after a media change returns
700		 * an error (determined experimentally)
701		 */
702		rc = datafab_id_device(us, info);
703		if (rc == USB_STOR_TRANSPORT_GOOD) {
704			info->sense_key = NO_SENSE;
705			srb->result = SUCCESS;
706		} else {
707			info->sense_key = UNIT_ATTENTION;
708			srb->result = SAM_STAT_CHECK_CONDITION;
709		}
710		return rc;
711	}
712
713	usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
714		     srb->cmnd[0], srb->cmnd[0]);
715	info->sense_key = 0x05;
716	info->sense_asc = 0x20;
717	info->sense_ascq = 0x00;
718	return USB_STOR_TRANSPORT_FAILED;
719}
720
721static struct scsi_host_template datafab_host_template;
722
723static int datafab_probe(struct usb_interface *intf,
724			 const struct usb_device_id *id)
725{
726	struct us_data *us;
727	int result;
728
729	result = usb_stor_probe1(&us, intf, id,
730			(id - datafab_usb_ids) + datafab_unusual_dev_list,
731			&datafab_host_template);
732	if (result)
733		return result;
734
735	us->transport_name  = "Datafab Bulk-Only";
736	us->transport = datafab_transport;
737	us->transport_reset = usb_stor_Bulk_reset;
738	us->max_lun = 1;
739
740	result = usb_stor_probe2(us);
741	return result;
742}
743
744static struct usb_driver datafab_driver = {
745	.name =		DRV_NAME,
746	.probe =	datafab_probe,
747	.disconnect =	usb_stor_disconnect,
748	.suspend =	usb_stor_suspend,
749	.resume =	usb_stor_resume,
750	.reset_resume =	usb_stor_reset_resume,
751	.pre_reset =	usb_stor_pre_reset,
752	.post_reset =	usb_stor_post_reset,
753	.id_table =	datafab_usb_ids,
754	.soft_unbind =	1,
755	.no_dynamic_id = 1,
756};
757
758module_usb_stor_driver(datafab_driver, datafab_host_template, DRV_NAME);