xref: /linux/drivers/usb/storage/freecom.c (revision 0d62939fab3cf28a23ac6934cec599793d3a1d9d)
1 /* Driver for Freecom USB/IDE adaptor
2  *
3  * Freecom v0.1:
4  *
5  * First release
6  *
7  * Current development and maintenance by:
8  *   (C) 2000 David Brown <usb-storage@davidb.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the
12  * Free Software Foundation; either version 2, or (at your option) any
13  * later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * This driver was developed with information provided in FREECOM's USB
25  * Programmers Reference Guide.  For further information contact Freecom
26  * (http://www.freecom.de/)
27  */
28 
29 #include <linux/module.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
32 
33 #include "usb.h"
34 #include "transport.h"
35 #include "protocol.h"
36 #include "debug.h"
37 
38 #ifdef CONFIG_USB_STORAGE_DEBUG
39 static void pdump (void *, int);
40 #endif
41 
42 /* Bits of HD_STATUS */
43 #define ERR_STAT		0x01
44 #define DRQ_STAT		0x08
45 
46 /* All of the outgoing packets are 64 bytes long. */
47 struct freecom_cb_wrap {
48 	u8    Type;		/* Command type. */
49 	u8    Timeout;		/* Timeout in seconds. */
50 	u8    Atapi[12];	/* An ATAPI packet. */
51 	u8    Filler[50];	/* Padding Data. */
52 };
53 
54 struct freecom_xfer_wrap {
55 	u8    Type;		/* Command type. */
56 	u8    Timeout;		/* Timeout in seconds. */
57 	__le32   Count;		/* Number of bytes to transfer. */
58 	u8    Pad[58];
59 } __attribute__ ((packed));
60 
61 struct freecom_ide_out {
62 	u8    Type;		/* Type + IDE register. */
63 	u8    Pad;
64 	__le16   Value;		/* Value to write. */
65 	u8    Pad2[60];
66 };
67 
68 struct freecom_ide_in {
69 	u8    Type;		/* Type | IDE register. */
70 	u8    Pad[63];
71 };
72 
73 struct freecom_status {
74 	u8    Status;
75 	u8    Reason;
76 	__le16   Count;
77 	u8    Pad[60];
78 };
79 
80 /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
81  * register. */
82 #define FCM_INT_STATUS		0x02 /* INDEX_STAT */
83 #define FCM_STATUS_BUSY		0x80
84 
85 /* These are the packet types.  The low bit indicates that this command
86  * should wait for an interrupt. */
87 #define FCM_PACKET_ATAPI	0x21
88 #define FCM_PACKET_STATUS	0x20
89 
90 /* Receive data from the IDE interface.  The ATAPI packet has already
91  * waited, so the data should be immediately available. */
92 #define FCM_PACKET_INPUT	0x81
93 
94 /* Send data to the IDE interface. */
95 #define FCM_PACKET_OUTPUT	0x01
96 
97 /* Write a value to an ide register.  Or the ide register to write after
98  * munging the address a bit. */
99 #define FCM_PACKET_IDE_WRITE	0x40
100 #define FCM_PACKET_IDE_READ	0xC0
101 
102 /* All packets (except for status) are 64 bytes long. */
103 #define FCM_PACKET_LENGTH		64
104 #define FCM_STATUS_PACKET_LENGTH	4
105 
106 static int init_freecom(struct us_data *us);
107 
108 
109 /*
110  * The table of devices
111  */
112 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
113 		    vendorName, productName, useProtocol, useTransport, \
114 		    initFunction, flags) \
115 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
116   .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
117 
118 struct usb_device_id freecom_usb_ids[] = {
119 #	include "unusual_freecom.h"
120 	{ }		/* Terminating entry */
121 };
122 MODULE_DEVICE_TABLE(usb, freecom_usb_ids);
123 
124 #undef UNUSUAL_DEV
125 
126 /*
127  * The flags table
128  */
129 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
130 		    vendor_name, product_name, use_protocol, use_transport, \
131 		    init_function, Flags) \
132 { \
133 	.vendorName = vendor_name,	\
134 	.productName = product_name,	\
135 	.useProtocol = use_protocol,	\
136 	.useTransport = use_transport,	\
137 	.initFunction = init_function,	\
138 }
139 
140 static struct us_unusual_dev freecom_unusual_dev_list[] = {
141 #	include "unusual_freecom.h"
142 	{ }		/* Terminating entry */
143 };
144 
145 #undef UNUSUAL_DEV
146 
147 static int
148 freecom_readdata (struct scsi_cmnd *srb, struct us_data *us,
149 		unsigned int ipipe, unsigned int opipe, int count)
150 {
151 	struct freecom_xfer_wrap *fxfr =
152 		(struct freecom_xfer_wrap *) us->iobuf;
153 	int result;
154 
155 	fxfr->Type = FCM_PACKET_INPUT | 0x00;
156 	fxfr->Timeout = 0;    /* Short timeout for debugging. */
157 	fxfr->Count = cpu_to_le32 (count);
158 	memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
159 
160 	US_DEBUGP("Read data Freecom! (c=%d)\n", count);
161 
162 	/* Issue the transfer command. */
163 	result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
164 			FCM_PACKET_LENGTH, NULL);
165 	if (result != USB_STOR_XFER_GOOD) {
166 		US_DEBUGP ("Freecom readdata transport error\n");
167 		return USB_STOR_TRANSPORT_ERROR;
168 	}
169 
170 	/* Now transfer all of our blocks. */
171 	US_DEBUGP("Start of read\n");
172 	result = usb_stor_bulk_srb(us, ipipe, srb);
173 	US_DEBUGP("freecom_readdata done!\n");
174 
175 	if (result > USB_STOR_XFER_SHORT)
176 		return USB_STOR_TRANSPORT_ERROR;
177 	return USB_STOR_TRANSPORT_GOOD;
178 }
179 
180 static int
181 freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
182 		int unsigned ipipe, unsigned int opipe, int count)
183 {
184 	struct freecom_xfer_wrap *fxfr =
185 		(struct freecom_xfer_wrap *) us->iobuf;
186 	int result;
187 
188 	fxfr->Type = FCM_PACKET_OUTPUT | 0x00;
189 	fxfr->Timeout = 0;    /* Short timeout for debugging. */
190 	fxfr->Count = cpu_to_le32 (count);
191 	memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
192 
193 	US_DEBUGP("Write data Freecom! (c=%d)\n", count);
194 
195 	/* Issue the transfer command. */
196 	result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
197 			FCM_PACKET_LENGTH, NULL);
198 	if (result != USB_STOR_XFER_GOOD) {
199 		US_DEBUGP ("Freecom writedata transport error\n");
200 		return USB_STOR_TRANSPORT_ERROR;
201 	}
202 
203 	/* Now transfer all of our blocks. */
204 	US_DEBUGP("Start of write\n");
205 	result = usb_stor_bulk_srb(us, opipe, srb);
206 
207 	US_DEBUGP("freecom_writedata done!\n");
208 	if (result > USB_STOR_XFER_SHORT)
209 		return USB_STOR_TRANSPORT_ERROR;
210 	return USB_STOR_TRANSPORT_GOOD;
211 }
212 
213 /*
214  * Transport for the Freecom USB/IDE adaptor.
215  *
216  */
217 static int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
218 {
219 	struct freecom_cb_wrap *fcb;
220 	struct freecom_status  *fst;
221 	unsigned int ipipe, opipe;		/* We need both pipes. */
222 	int result;
223 	unsigned int partial;
224 	int length;
225 
226 	fcb = (struct freecom_cb_wrap *) us->iobuf;
227 	fst = (struct freecom_status *) us->iobuf;
228 
229 	US_DEBUGP("Freecom TRANSPORT STARTED\n");
230 
231 	/* Get handles for both transports. */
232 	opipe = us->send_bulk_pipe;
233 	ipipe = us->recv_bulk_pipe;
234 
235 	/* The ATAPI Command always goes out first. */
236 	fcb->Type = FCM_PACKET_ATAPI | 0x00;
237 	fcb->Timeout = 0;
238 	memcpy (fcb->Atapi, srb->cmnd, 12);
239 	memset (fcb->Filler, 0, sizeof (fcb->Filler));
240 
241 	US_DEBUG(pdump (srb->cmnd, 12));
242 
243 	/* Send it out. */
244 	result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
245 			FCM_PACKET_LENGTH, NULL);
246 
247 	/* The Freecom device will only fail if there is something wrong in
248 	 * USB land.  It returns the status in its own registers, which
249 	 * come back in the bulk pipe. */
250 	if (result != USB_STOR_XFER_GOOD) {
251 		US_DEBUGP ("freecom transport error\n");
252 		return USB_STOR_TRANSPORT_ERROR;
253 	}
254 
255 	/* There are times we can optimize out this status read, but it
256 	 * doesn't hurt us to always do it now. */
257 	result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
258 			FCM_STATUS_PACKET_LENGTH, &partial);
259 	US_DEBUGP("foo Status result %d %u\n", result, partial);
260 	if (result != USB_STOR_XFER_GOOD)
261 		return USB_STOR_TRANSPORT_ERROR;
262 
263 	US_DEBUG(pdump ((void *) fst, partial));
264 
265 	/* The firmware will time-out commands after 20 seconds. Some commands
266 	 * can legitimately take longer than this, so we use a different
267 	 * command that only waits for the interrupt and then sends status,
268 	 * without having to send a new ATAPI command to the device.
269 	 *
270 	 * NOTE: There is some indication that a data transfer after a timeout
271 	 * may not work, but that is a condition that should never happen.
272 	 */
273 	while (fst->Status & FCM_STATUS_BUSY) {
274 		US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
275 		US_DEBUGP("fst->Status is %x\n", fst->Status);
276 
277 		/* Get the status again */
278 		fcb->Type = FCM_PACKET_STATUS;
279 		fcb->Timeout = 0;
280 		memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
281 		memset (fcb->Filler, 0, sizeof (fcb->Filler));
282 
283 		/* Send it out. */
284 		result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
285 				FCM_PACKET_LENGTH, NULL);
286 
287 		/* The Freecom device will only fail if there is something
288 		 * wrong in USB land.  It returns the status in its own
289 		 * registers, which come back in the bulk pipe.
290 		 */
291 		if (result != USB_STOR_XFER_GOOD) {
292 			US_DEBUGP ("freecom transport error\n");
293 			return USB_STOR_TRANSPORT_ERROR;
294 		}
295 
296 		/* get the data */
297 		result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
298 				FCM_STATUS_PACKET_LENGTH, &partial);
299 
300 		US_DEBUGP("bar Status result %d %u\n", result, partial);
301 		if (result != USB_STOR_XFER_GOOD)
302 			return USB_STOR_TRANSPORT_ERROR;
303 
304 		US_DEBUG(pdump ((void *) fst, partial));
305 	}
306 
307 	if (partial != 4)
308 		return USB_STOR_TRANSPORT_ERROR;
309 	if ((fst->Status & 1) != 0) {
310 		US_DEBUGP("operation failed\n");
311 		return USB_STOR_TRANSPORT_FAILED;
312 	}
313 
314 	/* The device might not have as much data available as we
315 	 * requested.  If you ask for more than the device has, this reads
316 	 * and such will hang. */
317 	US_DEBUGP("Device indicates that it has %d bytes available\n",
318 			le16_to_cpu (fst->Count));
319 	US_DEBUGP("SCSI requested %d\n", scsi_bufflen(srb));
320 
321 	/* Find the length we desire to read. */
322 	switch (srb->cmnd[0]) {
323 		case INQUIRY:
324 		case REQUEST_SENSE:		/* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
325 		case MODE_SENSE:
326 		case MODE_SENSE_10:
327 			length = le16_to_cpu(fst->Count);
328 			break;
329 		default:
330 			length = scsi_bufflen(srb);
331 	}
332 
333 	/* verify that this amount is legal */
334 	if (length > scsi_bufflen(srb)) {
335 		length = scsi_bufflen(srb);
336 		US_DEBUGP("Truncating request to match buffer length: %d\n", length);
337 	}
338 
339 	/* What we do now depends on what direction the data is supposed to
340 	 * move in. */
341 
342 	switch (us->srb->sc_data_direction) {
343 	case DMA_FROM_DEVICE:
344 		/* catch bogus "read 0 length" case */
345 		if (!length)
346 			break;
347 		/* Make sure that the status indicates that the device
348 		 * wants data as well. */
349 		if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
350 			US_DEBUGP("SCSI wants data, drive doesn't have any\n");
351 			return USB_STOR_TRANSPORT_FAILED;
352 		}
353 		result = freecom_readdata (srb, us, ipipe, opipe, length);
354 		if (result != USB_STOR_TRANSPORT_GOOD)
355 			return result;
356 
357 		US_DEBUGP("FCM: Waiting for status\n");
358 		result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
359 				FCM_PACKET_LENGTH, &partial);
360 		US_DEBUG(pdump ((void *) fst, partial));
361 
362 		if (partial != 4 || result > USB_STOR_XFER_SHORT)
363 			return USB_STOR_TRANSPORT_ERROR;
364 		if ((fst->Status & ERR_STAT) != 0) {
365 			US_DEBUGP("operation failed\n");
366 			return USB_STOR_TRANSPORT_FAILED;
367 		}
368 		if ((fst->Reason & 3) != 3) {
369 			US_DEBUGP("Drive seems still hungry\n");
370 			return USB_STOR_TRANSPORT_FAILED;
371 		}
372 		US_DEBUGP("Transfer happy\n");
373 		break;
374 
375 	case DMA_TO_DEVICE:
376 		/* catch bogus "write 0 length" case */
377 		if (!length)
378 			break;
379 		/* Make sure the status indicates that the device wants to
380 		 * send us data. */
381 		/* !!IMPLEMENT!! */
382 		result = freecom_writedata (srb, us, ipipe, opipe, length);
383 		if (result != USB_STOR_TRANSPORT_GOOD)
384 			return result;
385 
386 		US_DEBUGP("FCM: Waiting for status\n");
387 		result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
388 				FCM_PACKET_LENGTH, &partial);
389 
390 		if (partial != 4 || result > USB_STOR_XFER_SHORT)
391 			return USB_STOR_TRANSPORT_ERROR;
392 		if ((fst->Status & ERR_STAT) != 0) {
393 			US_DEBUGP("operation failed\n");
394 			return USB_STOR_TRANSPORT_FAILED;
395 		}
396 		if ((fst->Reason & 3) != 3) {
397 			US_DEBUGP("Drive seems still hungry\n");
398 			return USB_STOR_TRANSPORT_FAILED;
399 		}
400 
401 		US_DEBUGP("Transfer happy\n");
402 		break;
403 
404 
405 	case DMA_NONE:
406 		/* Easy, do nothing. */
407 		break;
408 
409 	default:
410 		/* should never hit here -- filtered in usb.c */
411 		US_DEBUGP ("freecom unimplemented direction: %d\n",
412 				us->srb->sc_data_direction);
413 		// Return fail, SCSI seems to handle this better.
414 		return USB_STOR_TRANSPORT_FAILED;
415 		break;
416 	}
417 
418 	return USB_STOR_TRANSPORT_GOOD;
419 }
420 
421 static int init_freecom(struct us_data *us)
422 {
423 	int result;
424 	char *buffer = us->iobuf;
425 
426 	/* The DMA-mapped I/O buffer is 64 bytes long, just right for
427 	 * all our packets.  No need to allocate any extra buffer space.
428 	 */
429 
430 	result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
431 			0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
432 	buffer[32] = '\0';
433 	US_DEBUGP("String returned from FC init is: %s\n", buffer);
434 
435 	/* Special thanks to the people at Freecom for providing me with
436 	 * this "magic sequence", which they use in their Windows and MacOS
437 	 * drivers to make sure that all the attached perhiperals are
438 	 * properly reset.
439 	 */
440 
441 	/* send reset */
442 	result = usb_stor_control_msg(us, us->send_ctrl_pipe,
443 			0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
444 	US_DEBUGP("result from activate reset is %d\n", result);
445 
446 	/* wait 250ms */
447 	mdelay(250);
448 
449 	/* clear reset */
450 	result = usb_stor_control_msg(us, us->send_ctrl_pipe,
451 			0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
452 	US_DEBUGP("result from clear reset is %d\n", result);
453 
454 	/* wait 3 seconds */
455 	mdelay(3 * 1000);
456 
457 	return USB_STOR_TRANSPORT_GOOD;
458 }
459 
460 static int usb_stor_freecom_reset(struct us_data *us)
461 {
462 	printk (KERN_CRIT "freecom reset called\n");
463 
464 	/* We don't really have this feature. */
465 	return FAILED;
466 }
467 
468 #ifdef CONFIG_USB_STORAGE_DEBUG
469 static void pdump (void *ibuffer, int length)
470 {
471 	static char line[80];
472 	int offset = 0;
473 	unsigned char *buffer = (unsigned char *) ibuffer;
474 	int i, j;
475 	int from, base;
476 
477 	offset = 0;
478 	for (i = 0; i < length; i++) {
479 		if ((i & 15) == 0) {
480 			if (i > 0) {
481 				offset += sprintf (line+offset, " - ");
482 				for (j = i - 16; j < i; j++) {
483 					if (buffer[j] >= 32 && buffer[j] <= 126)
484 						line[offset++] = buffer[j];
485 					else
486 						line[offset++] = '.';
487 				}
488 				line[offset] = 0;
489 				US_DEBUGP("%s\n", line);
490 				offset = 0;
491 			}
492 			offset += sprintf (line+offset, "%08x:", i);
493 		}
494 		else if ((i & 7) == 0) {
495 			offset += sprintf (line+offset, " -");
496 		}
497 		offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
498 	}
499 
500 	/* Add the last "chunk" of data. */
501 	from = (length - 1) % 16;
502 	base = ((length - 1) / 16) * 16;
503 
504 	for (i = from + 1; i < 16; i++)
505 		offset += sprintf (line+offset, "   ");
506 	if (from < 8)
507 		offset += sprintf (line+offset, "  ");
508 	offset += sprintf (line+offset, " - ");
509 
510 	for (i = 0; i <= from; i++) {
511 		if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
512 			line[offset++] = buffer[base+i];
513 		else
514 			line[offset++] = '.';
515 	}
516 	line[offset] = 0;
517 	US_DEBUGP("%s\n", line);
518 	offset = 0;
519 }
520 #endif
521 
522 static int freecom_probe(struct usb_interface *intf,
523 			 const struct usb_device_id *id)
524 {
525 	struct us_data *us;
526 	int result;
527 
528 	result = usb_stor_probe1(&us, intf, id,
529 			(id - freecom_usb_ids) + freecom_unusual_dev_list);
530 	if (result)
531 		return result;
532 
533 	us->transport_name = "Freecom";
534 	us->transport = freecom_transport;
535 	us->transport_reset = usb_stor_freecom_reset;
536 	us->max_lun = 0;
537 
538 	result = usb_stor_probe2(us);
539 	return result;
540 }
541 
542 static struct usb_driver freecom_driver = {
543 	.name =		"ums-freecom",
544 	.probe =	freecom_probe,
545 	.disconnect =	usb_stor_disconnect,
546 	.suspend =	usb_stor_suspend,
547 	.resume =	usb_stor_resume,
548 	.reset_resume =	usb_stor_reset_resume,
549 	.pre_reset =	usb_stor_pre_reset,
550 	.post_reset =	usb_stor_post_reset,
551 	.id_table =	freecom_usb_ids,
552 	.soft_unbind =	1,
553 };
554 
555 static int __init freecom_init(void)
556 {
557 	return usb_register(&freecom_driver);
558 }
559 
560 static void __exit freecom_exit(void)
561 {
562 	usb_deregister(&freecom_driver);
563 }
564 
565 module_init(freecom_init);
566 module_exit(freecom_exit);
567