Linux Audio

Check our new training course

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