blob: 356342c6e7a24caebbfacf3fed13d9a992949e1c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Driver for SCM Microsystems USB-ATAPI cable
2 *
3 * $Id: shuttle_usbat.c,v 1.17 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
7 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
8 *
9 * Developed with the assistance of:
10 * (c) 2002 Alan Stern <stern@rowland.org>
11 *
12 * Flash support based on earlier work by:
13 * (c) 2002 Thomas Kreiling <usbdev@sm04.de>
14 *
15 * Many originally ATAPI devices were slightly modified to meet the USB
16 * market by using some kind of translation from ATAPI to USB on the host,
17 * and the peripheral would translate from USB back to ATAPI.
18 *
19 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
20 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
21 * their device under nondisclosure agreement, I have been able to write
22 * this driver for Linux.
23 *
24 * The chip used in the device can also be used for EPP and ISA translation
25 * as well. This driver is only guaranteed to work with the ATAPI
26 * translation.
27 *
28 * See the Kconfig help text for a list of devices known to be supported by
29 * this driver.
30 *
31 * This program is free software; you can redistribute it and/or modify it
32 * under the terms of the GNU General Public License as published by the
33 * Free Software Foundation; either version 2, or (at your option) any
34 * later version.
35 *
36 * This program is distributed in the hope that it will be useful, but
37 * WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * General Public License for more details.
40 *
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, write to the Free Software Foundation, Inc.,
43 * 675 Mass Ave, Cambridge, MA 02139, USA.
44 */
45
46#include <linux/sched.h>
47#include <linux/errno.h>
48#include <linux/slab.h>
49#include <linux/cdrom.h>
50
51#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h>
53
54#include "usb.h"
55#include "transport.h"
56#include "protocol.h"
57#include "debug.h"
58#include "shuttle_usbat.h"
59
60#define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
61#define LSB_of(s) ((s)&0xFF)
62#define MSB_of(s) ((s)>>8)
63
64static int transferred = 0;
65
66static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
67static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
68
69/*
70 * Convenience function to produce an ATAPI read/write sectors command
71 * Use cmd=0x20 for read, cmd=0x30 for write
72 */
73static void usbat_pack_atapi_sector_cmd(unsigned char *buf,
74 unsigned char thistime,
75 u32 sector, unsigned char cmd)
76{
77 buf[0] = 0;
78 buf[1] = thistime;
79 buf[2] = sector & 0xFF;
80 buf[3] = (sector >> 8) & 0xFF;
81 buf[4] = (sector >> 16) & 0xFF;
82 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
83 buf[6] = cmd;
84}
85
86/*
87 * Convenience function to get the device type (flash or hp8200)
88 */
89static int usbat_get_device_type(struct us_data *us)
90{
91 return ((struct usbat_info*)us->extra)->devicetype;
92}
93
94/*
95 * Read a register from the device
96 */
97static int usbat_read(struct us_data *us,
98 unsigned char access,
99 unsigned char reg,
100 unsigned char *content)
101{
102 return usb_stor_ctrl_transfer(us,
103 us->recv_ctrl_pipe,
104 access | USBAT_CMD_READ_REG,
105 0xC0,
106 (u16)reg,
107 0,
108 content,
109 1);
110}
111
112/*
113 * Write to a register on the device
114 */
115static int usbat_write(struct us_data *us,
116 unsigned char access,
117 unsigned char reg,
118 unsigned char content)
119{
120 return usb_stor_ctrl_transfer(us,
121 us->send_ctrl_pipe,
122 access | USBAT_CMD_WRITE_REG,
123 0x40,
124 short_pack(reg, content),
125 0,
126 NULL,
127 0);
128}
129
130/*
131 * Convenience function to perform a bulk read
132 */
133static int usbat_bulk_read(struct us_data *us,
134 unsigned char *data,
135 unsigned int len)
136{
137 if (len == 0)
138 return USB_STOR_XFER_GOOD;
139
140 US_DEBUGP("usbat_bulk_read: len = %d\n", len);
141 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, data, len, NULL);
142}
143
144/*
145 * Convenience function to perform a bulk write
146 */
147static int usbat_bulk_write(struct us_data *us,
148 unsigned char *data,
149 unsigned int len)
150{
151 if (len == 0)
152 return USB_STOR_XFER_GOOD;
153
154 US_DEBUGP("usbat_bulk_write: len = %d\n", len);
155 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, data, len, NULL);
156}
157
158/*
159 * Some USBAT-specific commands can only be executed over a command transport
160 * This transport allows one (len=8) or two (len=16) vendor-specific commands
161 * to be executed.
162 */
163static int usbat_execute_command(struct us_data *us,
164 unsigned char *commands,
165 unsigned int len)
166{
167 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
168 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
169 commands, len);
170}
171
172/*
173 * Read the status register
174 */
175static int usbat_get_status(struct us_data *us, unsigned char *status)
176{
177 int rc;
178 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
179
180 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
181 return rc;
182}
183
184/*
185 * Check the device status
186 */
187static int usbat_check_status(struct us_data *us)
188{
189 unsigned char *reply = us->iobuf;
190 int rc;
191
192 if (!us)
193 return USB_STOR_TRANSPORT_ERROR;
194
195 rc = usbat_get_status(us, reply);
196 if (rc != USB_STOR_XFER_GOOD)
197 return USB_STOR_TRANSPORT_FAILED;
198
199 if (*reply & 0x01 && *reply != 0x51) // error/check condition (0x51 is ok)
200 return USB_STOR_TRANSPORT_FAILED;
201
202 if (*reply & 0x20) // device fault
203 return USB_STOR_TRANSPORT_FAILED;
204
205 return USB_STOR_TRANSPORT_GOOD;
206}
207
208/*
209 * Stores critical information in internal registers in prepartion for the execution
210 * of a conditional usbat_read_blocks or usbat_write_blocks call.
211 */
212static int usbat_set_shuttle_features(struct us_data *us,
213 unsigned char external_trigger,
214 unsigned char epp_control,
215 unsigned char mask_byte,
216 unsigned char test_pattern,
217 unsigned char subcountH,
218 unsigned char subcountL)
219{
220 unsigned char *command = us->iobuf;
221
222 command[0] = 0x40;
223 command[1] = USBAT_CMD_SET_FEAT;
224
225 // The only bit relevant to ATA access is bit 6
226 // which defines 8 bit data access (set) or 16 bit (unset)
227 command[2] = epp_control;
228
229 // If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
230 // ET1 and ET2 define an external event to be checked for on event of a
231 // _read_blocks or _write_blocks operation. The read/write will not take
232 // place unless the defined trigger signal is active.
233 command[3] = external_trigger;
234
235 // The resultant byte of the mask operation (see mask_byte) is compared for
236 // equivalence with this test pattern. If equal, the read/write will take
237 // place.
238 command[4] = test_pattern;
239
240 // This value is logically ANDed with the status register field specified
241 // in the read/write command.
242 command[5] = mask_byte;
243
244 // If ALQ is set in the qualifier, this field contains the address of the
245 // registers where the byte count should be read for transferring the data.
246 // If ALQ is not set, then this field contains the number of bytes to be
247 // transferred.
248 command[6] = subcountL;
249 command[7] = subcountH;
250
251 return usbat_execute_command(us, command, 8);
252}
253
254/*
255 * Block, waiting for an ATA device to become not busy or to report
256 * an error condition.
257 */
258static int usbat_wait_not_busy(struct us_data *us, int minutes)
259{
260 int i;
261 int result;
262 unsigned char *status = us->iobuf;
263
264 /* Synchronizing cache on a CDR could take a heck of a long time,
265 * but probably not more than 10 minutes or so. On the other hand,
266 * doing a full blank on a CDRW at speed 1 will take about 75
267 * minutes!
268 */
269
270 for (i=0; i<1200+minutes*60; i++) {
271
272 result = usbat_get_status(us, status);
273
274 if (result!=USB_STOR_XFER_GOOD)
275 return USB_STOR_TRANSPORT_ERROR;
276 if (*status & 0x01) { // check condition
277 result = usbat_read(us, USBAT_ATA, 0x10, status);
278 return USB_STOR_TRANSPORT_FAILED;
279 }
280 if (*status & 0x20) // device fault
281 return USB_STOR_TRANSPORT_FAILED;
282
283 if ((*status & 0x80)==0x00) { // not busy
284 US_DEBUGP("Waited not busy for %d steps\n", i);
285 return USB_STOR_TRANSPORT_GOOD;
286 }
287
288 if (i<500)
289 msleep(10); // 5 seconds
290 else if (i<700)
291 msleep(50); // 10 seconds
292 else if (i<1200)
293 msleep(100); // 50 seconds
294 else
295 msleep(1000); // X minutes
296 }
297
298 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
299 minutes);
300 return USB_STOR_TRANSPORT_FAILED;
301}
302
303/*
304 * Read block data from the data register
305 */
306static int usbat_read_block(struct us_data *us,
307 unsigned char *content,
308 unsigned short len)
309{
310 int result;
311 unsigned char *command = us->iobuf;
312
313 if (!len)
314 return USB_STOR_TRANSPORT_GOOD;
315
316 command[0] = 0xC0;
317 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
318 command[2] = USBAT_ATA_DATA;
319 command[3] = 0;
320 command[4] = 0;
321 command[5] = 0;
322 command[6] = LSB_of(len);
323 command[7] = MSB_of(len);
324
325 result = usbat_execute_command(us, command, 8);
326 if (result != USB_STOR_XFER_GOOD)
327 return USB_STOR_TRANSPORT_ERROR;
328
329 result = usbat_bulk_read(us, content, len);
330 return (result == USB_STOR_XFER_GOOD ?
331 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
332}
333
334/*
335 * Write block data via the data register
336 */
337static int usbat_write_block(struct us_data *us,
338 unsigned char access,
339 unsigned char *content,
340 unsigned short len,
341 int minutes)
342{
343 int result;
344 unsigned char *command = us->iobuf;
345
346 if (!len)
347 return USB_STOR_TRANSPORT_GOOD;
348
349 command[0] = 0x40;
350 command[1] = access | USBAT_CMD_WRITE_BLOCK;
351 command[2] = USBAT_ATA_DATA;
352 command[3] = 0;
353 command[4] = 0;
354 command[5] = 0;
355 command[6] = LSB_of(len);
356 command[7] = MSB_of(len);
357
358 result = usbat_execute_command(us, command, 8);
359
360 if (result != USB_STOR_XFER_GOOD)
361 return USB_STOR_TRANSPORT_ERROR;
362
363 result = usbat_bulk_write(us, content, len);
364 if (result != USB_STOR_XFER_GOOD)
365 return USB_STOR_TRANSPORT_ERROR;
366
367 return usbat_wait_not_busy(us, minutes);
368}
369
370/*
371 * Process read and write requests
372 */
373static int usbat_hp8200e_rw_block_test(struct us_data *us,
374 unsigned char access,
375 unsigned char *registers,
376 unsigned char *data_out,
377 unsigned short num_registers,
378 unsigned char data_reg,
379 unsigned char status_reg,
380 unsigned char timeout,
381 unsigned char qualifier,
382 int direction,
383 unsigned char *content,
384 unsigned short len,
385 int use_sg,
386 int minutes)
387{
388 int result;
389 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
390 us->recv_bulk_pipe : us->send_bulk_pipe;
391
392 unsigned char *command = us->iobuf;
393 int i, j;
394 int cmdlen;
395 unsigned char *data = us->iobuf;
396 unsigned char *status = us->iobuf;
397
398 BUG_ON(num_registers > US_IOBUF_SIZE/2);
399
400 for (i=0; i<20; i++) {
401
402 /*
403 * The first time we send the full command, which consists
404 * of downloading the SCSI command followed by downloading
405 * the data via a write-and-test. Any other time we only
406 * send the command to download the data -- the SCSI command
407 * is still 'active' in some sense in the device.
408 *
409 * We're only going to try sending the data 10 times. After
410 * that, we just return a failure.
411 */
412
413 if (i==0) {
414 cmdlen = 16;
415 // Write to multiple registers
416 // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
417 // but that's what came out of the trace every single time.
418 command[0] = 0x40;
419 command[1] = access | USBAT_CMD_WRITE_REGS;
420 command[2] = 0x07;
421 command[3] = 0x17;
422 command[4] = 0xFC;
423 command[5] = 0xE7;
424 command[6] = LSB_of(num_registers*2);
425 command[7] = MSB_of(num_registers*2);
426 } else
427 cmdlen = 8;
428
429 // Conditionally read or write blocks
430 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
431 command[cmdlen-7] = access |
432 (direction==DMA_TO_DEVICE ?
433 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
434 command[cmdlen-6] = data_reg;
435 command[cmdlen-5] = status_reg;
436 command[cmdlen-4] = timeout;
437 command[cmdlen-3] = qualifier;
438 command[cmdlen-2] = LSB_of(len);
439 command[cmdlen-1] = MSB_of(len);
440
441 result = usbat_execute_command(us, command, cmdlen);
442
443 if (result != USB_STOR_XFER_GOOD)
444 return USB_STOR_TRANSPORT_ERROR;
445
446 if (i==0) {
447
448 for (j=0; j<num_registers; j++) {
449 data[j<<1] = registers[j];
450 data[1+(j<<1)] = data_out[j];
451 }
452
453 result = usbat_bulk_write(us, data, num_registers*2);
454 if (result != USB_STOR_XFER_GOOD)
455 return USB_STOR_TRANSPORT_ERROR;
456
457 }
458
459
460 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
461 // direction == DMA_TO_DEVICE ? "out" : "in",
462 // len, use_sg);
463
464 result = usb_stor_bulk_transfer_sg(us,
465 pipe, content, len, use_sg, NULL);
466
467 /*
468 * If we get a stall on the bulk download, we'll retry
469 * the bulk download -- but not the SCSI command because
470 * in some sense the SCSI command is still 'active' and
471 * waiting for the data. Don't ask me why this should be;
472 * I'm only following what the Windoze driver did.
473 *
474 * Note that a stall for the test-and-read/write command means
475 * that the test failed. In this case we're testing to make
476 * sure that the device is error-free
477 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
478 * hypothesis is that the USBAT chip somehow knows what
479 * the device will accept, but doesn't give the device any
480 * data until all data is received. Thus, the device would
481 * still be waiting for the first byte of data if a stall
482 * occurs, even if the stall implies that some data was
483 * transferred.
484 */
485
486 if (result == USB_STOR_XFER_SHORT ||
487 result == USB_STOR_XFER_STALLED) {
488
489 /*
490 * If we're reading and we stalled, then clear
491 * the bulk output pipe only the first time.
492 */
493
494 if (direction==DMA_FROM_DEVICE && i==0) {
495 if (usb_stor_clear_halt(us,
496 us->send_bulk_pipe) < 0)
497 return USB_STOR_TRANSPORT_ERROR;
498 }
499
500 /*
501 * Read status: is the device angry, or just busy?
502 */
503
504 result = usbat_read(us, USBAT_ATA,
505 direction==DMA_TO_DEVICE ?
506 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
507 status);
508
509 if (result!=USB_STOR_XFER_GOOD)
510 return USB_STOR_TRANSPORT_ERROR;
511 if (*status & 0x01) // check condition
512 return USB_STOR_TRANSPORT_FAILED;
513 if (*status & 0x20) // device fault
514 return USB_STOR_TRANSPORT_FAILED;
515
516 US_DEBUGP("Redoing %s\n",
517 direction==DMA_TO_DEVICE ? "write" : "read");
518
519 } else if (result != USB_STOR_XFER_GOOD)
520 return USB_STOR_TRANSPORT_ERROR;
521 else
522 return usbat_wait_not_busy(us, minutes);
523
524 }
525
526 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
527 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
528
529 return USB_STOR_TRANSPORT_FAILED;
530}
531
532/*
533 * Write to multiple registers:
534 * Allows us to write specific data to any registers. The data to be written
535 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
536 * which gets sent through bulk out.
537 * Not designed for large transfers of data!
538 */
539static int usbat_multiple_write(struct us_data *us,
540 unsigned char *registers,
541 unsigned char *data_out,
542 unsigned short num_registers)
543{
544 int i, result;
545 unsigned char *data = us->iobuf;
546 unsigned char *command = us->iobuf;
547
548 BUG_ON(num_registers > US_IOBUF_SIZE/2);
549
550 // Write to multiple registers, ATA access
551 command[0] = 0x40;
552 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
553
554 // No relevance
555 command[2] = 0;
556 command[3] = 0;
557 command[4] = 0;
558 command[5] = 0;
559
560 // Number of bytes to be transferred (incl. addresses and data)
561 command[6] = LSB_of(num_registers*2);
562 command[7] = MSB_of(num_registers*2);
563
564 // The setup command
565 result = usbat_execute_command(us, command, 8);
566 if (result != USB_STOR_XFER_GOOD)
567 return USB_STOR_TRANSPORT_ERROR;
568
569 // Create the reg/data, reg/data sequence
570 for (i=0; i<num_registers; i++) {
571 data[i<<1] = registers[i];
572 data[1+(i<<1)] = data_out[i];
573 }
574
575 // Send the data
576 result = usbat_bulk_write(us, data, num_registers*2);
577 if (result != USB_STOR_XFER_GOOD)
578 return USB_STOR_TRANSPORT_ERROR;
579
580 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
581 return usbat_wait_not_busy(us, 0);
582 else
583 return USB_STOR_TRANSPORT_GOOD;
584}
585
586/*
587 * Conditionally read blocks from device:
588 * Allows us to read blocks from a specific data register, based upon the
589 * condition that a status register can be successfully masked with a status
590 * qualifier. If this condition is not initially met, the read will wait
591 * up until a maximum amount of time has elapsed, as specified by timeout.
592 * The read will start when the condition is met, otherwise the command aborts.
593 *
594 * The qualifier defined here is not the value that is masked, it defines
595 * conditions for the write to take place. The actual masked qualifier (and
596 * other related details) are defined beforehand with _set_shuttle_features().
597 */
598static int usbat_read_blocks(struct us_data *us,
599 unsigned char *buffer,
600 int len)
601{
602 int result;
603 unsigned char *command = us->iobuf;
604
605 command[0] = 0xC0;
606 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
607 command[2] = USBAT_ATA_DATA;
608 command[3] = USBAT_ATA_STATUS;
609 command[4] = 0xFD; // Timeout (ms);
610 command[5] = USBAT_QUAL_FCQ;
611 command[6] = LSB_of(len);
612 command[7] = MSB_of(len);
613
614 // Multiple block read setup command
615 result = usbat_execute_command(us, command, 8);
616 if (result != USB_STOR_XFER_GOOD)
617 return USB_STOR_TRANSPORT_FAILED;
618
619 // Read the blocks we just asked for
620 result = usbat_bulk_read(us, buffer, len);
621 if (result != USB_STOR_XFER_GOOD)
622 return USB_STOR_TRANSPORT_FAILED;
623
624 return USB_STOR_TRANSPORT_GOOD;
625}
626
627/*
628 * Conditionally write blocks to device:
629 * Allows us to write blocks to a specific data register, based upon the
630 * condition that a status register can be successfully masked with a status
631 * qualifier. If this condition is not initially met, the write will wait
632 * up until a maximum amount of time has elapsed, as specified by timeout.
633 * The read will start when the condition is met, otherwise the command aborts.
634 *
635 * The qualifier defined here is not the value that is masked, it defines
636 * conditions for the write to take place. The actual masked qualifier (and
637 * other related details) are defined beforehand with _set_shuttle_features().
638 */
639static int usbat_write_blocks(struct us_data *us,
640 unsigned char *buffer,
641 int len)
642{
643 int result;
644 unsigned char *command = us->iobuf;
645
646 command[0] = 0x40;
647 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
648 command[2] = USBAT_ATA_DATA;
649 command[3] = USBAT_ATA_STATUS;
650 command[4] = 0xFD; // Timeout (ms)
651 command[5] = USBAT_QUAL_FCQ;
652 command[6] = LSB_of(len);
653 command[7] = MSB_of(len);
654
655 // Multiple block write setup command
656 result = usbat_execute_command(us, command, 8);
657 if (result != USB_STOR_XFER_GOOD)
658 return USB_STOR_TRANSPORT_FAILED;
659
660 // Write the data
661 result = usbat_bulk_write(us, buffer, len);
662 if (result != USB_STOR_XFER_GOOD)
663 return USB_STOR_TRANSPORT_FAILED;
664
665 return USB_STOR_TRANSPORT_GOOD;
666}
667
668/*
669 * Read the User IO register
670 */
671static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
672{
673 int result;
674
675 result = usb_stor_ctrl_transfer(us,
676 us->recv_ctrl_pipe,
677 USBAT_CMD_UIO,
678 0xC0,
679 0,
680 0,
681 data_flags,
682 USBAT_UIO_READ);
683
684 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
685
686 return result;
687}
688
689/*
690 * Write to the User IO register
691 */
692static int usbat_write_user_io(struct us_data *us,
693 unsigned char enable_flags,
694 unsigned char data_flags)
695{
696 return usb_stor_ctrl_transfer(us,
697 us->send_ctrl_pipe,
698 USBAT_CMD_UIO,
699 0x40,
700 short_pack(enable_flags, data_flags),
701 0,
702 NULL,
703 USBAT_UIO_WRITE);
704}
705
706/*
707 * Reset the device
708 * Often needed on media change.
709 */
710static int usbat_device_reset(struct us_data *us)
711{
712 int rc;
713
714 // Reset peripheral, enable peripheral control signals
715 // (bring reset signal up)
716 rc = usbat_write_user_io(us,
717 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
718 USBAT_UIO_EPAD | USBAT_UIO_1);
719 if (rc != USB_STOR_XFER_GOOD)
720 return USB_STOR_TRANSPORT_ERROR;
721
722 // Enable peripheral control signals
723 // (bring reset signal down)
724 rc = usbat_write_user_io(us,
725 USBAT_UIO_OE1 | USBAT_UIO_OE0,
726 USBAT_UIO_EPAD | USBAT_UIO_1);
727 if (rc != USB_STOR_XFER_GOOD)
728 return USB_STOR_TRANSPORT_ERROR;
729
730 return USB_STOR_TRANSPORT_GOOD;
731}
732
733/*
734 * Enable card detect
735 */
736static int usbat_device_enable_cdt(struct us_data *us)
737{
738 int rc;
739
740 // Enable peripheral control signals and card detect
741 rc = usbat_write_user_io(us,
742 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
743 USBAT_UIO_EPAD | USBAT_UIO_1);
744 if (rc != USB_STOR_XFER_GOOD)
745 return USB_STOR_TRANSPORT_ERROR;
746
747 return USB_STOR_TRANSPORT_GOOD;
748}
749
750/*
751 * Determine if media is present.
752 */
753static int usbat_flash_check_media_present(unsigned char *uio)
754{
755 if (*uio & USBAT_UIO_UI0) {
756 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
757 return USBAT_FLASH_MEDIA_NONE;
758 }
759
760 return USBAT_FLASH_MEDIA_CF;
761}
762
763/*
764 * Determine if media has changed since last operation
765 */
766static int usbat_flash_check_media_changed(unsigned char *uio)
767{
768 if (*uio & USBAT_UIO_0) {
769 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
770 return USBAT_FLASH_MEDIA_CHANGED;
771 }
772
773 return USBAT_FLASH_MEDIA_SAME;
774}
775
776/*
777 * Check for media change / no media and handle the situation appropriately
778 */
779static int usbat_flash_check_media(struct us_data *us,
780 struct usbat_info *info)
781{
782 int rc;
783 unsigned char *uio = us->iobuf;
784
785 rc = usbat_read_user_io(us, uio);
786 if (rc != USB_STOR_XFER_GOOD)
787 return USB_STOR_TRANSPORT_ERROR;
788
Steven Cole093cf722005-05-03 19:07:24 -0600789 // Check for media existence
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 rc = usbat_flash_check_media_present(uio);
791 if (rc == USBAT_FLASH_MEDIA_NONE) {
792 info->sense_key = 0x02;
793 info->sense_asc = 0x3A;
794 info->sense_ascq = 0x00;
795 return USB_STOR_TRANSPORT_FAILED;
796 }
797
798 // Check for media change
799 rc = usbat_flash_check_media_changed(uio);
800 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
801
802 // Reset and re-enable card detect
803 rc = usbat_device_reset(us);
804 if (rc != USB_STOR_TRANSPORT_GOOD)
805 return rc;
806 rc = usbat_device_enable_cdt(us);
807 if (rc != USB_STOR_TRANSPORT_GOOD)
808 return rc;
809
810 msleep(50);
811
812 rc = usbat_read_user_io(us, uio);
813 if (rc != USB_STOR_XFER_GOOD)
814 return USB_STOR_TRANSPORT_ERROR;
815
816 info->sense_key = UNIT_ATTENTION;
817 info->sense_asc = 0x28;
818 info->sense_ascq = 0x00;
819 return USB_STOR_TRANSPORT_FAILED;
820 }
821
822 return USB_STOR_TRANSPORT_GOOD;
823}
824
825/*
826 * Determine whether we are controlling a flash-based reader/writer,
827 * or a HP8200-based CD drive.
828 * Sets transport functions as appropriate.
829 */
830static int usbat_identify_device(struct us_data *us,
831 struct usbat_info *info)
832{
833 int rc;
834 unsigned char status;
835
836 if (!us || !info)
837 return USB_STOR_TRANSPORT_ERROR;
838
839 rc = usbat_device_reset(us);
840 if (rc != USB_STOR_TRANSPORT_GOOD)
841 return rc;
Daniel Drake68a64572005-08-10 18:30:04 +0100842 msleep(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /*
Daniel Drake68a64572005-08-10 18:30:04 +0100845 * In attempt to distinguish between HP CDRW's and Flash readers, we now
846 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
847 * readers), this command should fail with error. On ATAPI devices (i.e.
848 * CDROM drives), it should succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
Daniel Drake68a64572005-08-10 18:30:04 +0100850 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
851 if (rc != USB_STOR_XFER_GOOD)
852 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Daniel Drake68a64572005-08-10 18:30:04 +0100854 rc = usbat_get_status(us, &status);
855 if (rc != USB_STOR_XFER_GOOD)
856 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Daniel Drake68a64572005-08-10 18:30:04 +0100858 // Check for error bit
859 if (status & 0x01) {
860 // Device is a CompactFlash reader/writer
861 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
862 info->devicetype = USBAT_DEV_FLASH;
863 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 // Device is HP 8200
865 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
866 info->devicetype = USBAT_DEV_HP8200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 return USB_STOR_TRANSPORT_GOOD;
870}
871
872/*
873 * Set the transport function based on the device type
874 */
875static int usbat_set_transport(struct us_data *us,
876 struct usbat_info *info)
877{
878 int rc;
879
880 if (!info->devicetype) {
881 rc = usbat_identify_device(us, info);
882 if (rc != USB_STOR_TRANSPORT_GOOD) {
883 US_DEBUGP("usbat_set_transport: Could not identify device\n");
884 return 1;
885 }
886 }
887
888 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
889 us->transport = usbat_hp8200e_transport;
890 else if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
891 us->transport = usbat_flash_transport;
892
893 return 0;
894}
895
896/*
897 * Read the media capacity
898 */
899static int usbat_flash_get_sector_count(struct us_data *us,
900 struct usbat_info *info)
901{
902 unsigned char registers[3] = {
903 USBAT_ATA_SECCNT,
904 USBAT_ATA_DEVICE,
905 USBAT_ATA_CMD,
906 };
907 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
908 unsigned char *reply;
909 unsigned char status;
910 int rc;
911
912 if (!us || !info)
913 return USB_STOR_TRANSPORT_ERROR;
914
915 reply = kmalloc(512, GFP_NOIO);
916 if (!reply)
917 return USB_STOR_TRANSPORT_ERROR;
918
919 // ATAPI command : IDENTIFY DEVICE
920 rc = usbat_multiple_write(us, registers, command, 3);
921 if (rc != USB_STOR_XFER_GOOD) {
922 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
923 rc = USB_STOR_TRANSPORT_ERROR;
924 goto leave;
925 }
926
927 // Read device status
928 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
929 rc = USB_STOR_TRANSPORT_ERROR;
930 goto leave;
931 }
932
933 msleep(100);
934
935 // Read the device identification data
936 rc = usbat_read_block(us, reply, 512);
937 if (rc != USB_STOR_TRANSPORT_GOOD)
938 goto leave;
939
940 info->sectors = ((u32)(reply[117]) << 24) |
941 ((u32)(reply[116]) << 16) |
942 ((u32)(reply[115]) << 8) |
943 ((u32)(reply[114]) );
944
945 rc = USB_STOR_TRANSPORT_GOOD;
946
947 leave:
948 kfree(reply);
949 return rc;
950}
951
952/*
953 * Read data from device
954 */
955static int usbat_flash_read_data(struct us_data *us,
956 struct usbat_info *info,
957 u32 sector,
958 u32 sectors)
959{
960 unsigned char registers[7] = {
961 USBAT_ATA_FEATURES,
962 USBAT_ATA_SECCNT,
963 USBAT_ATA_SECNUM,
964 USBAT_ATA_LBA_ME,
965 USBAT_ATA_LBA_HI,
966 USBAT_ATA_DEVICE,
967 USBAT_ATA_STATUS,
968 };
969 unsigned char command[7];
970 unsigned char *buffer;
971 unsigned char thistime;
972 unsigned int totallen, alloclen;
973 int len, result;
974 unsigned int sg_idx = 0, sg_offset = 0;
975
976 result = usbat_flash_check_media(us, info);
977 if (result != USB_STOR_TRANSPORT_GOOD)
978 return result;
979
980 // we're working in LBA mode. according to the ATA spec,
981 // we can support up to 28-bit addressing. I don't know if Jumpshot
982 // supports beyond 24-bit addressing. It's kind of hard to test
983 // since it requires > 8GB CF card.
984
985 if (sector > 0x0FFFFFFF)
986 return USB_STOR_TRANSPORT_ERROR;
987
988 totallen = sectors * info->ssize;
989
990 // Since we don't read more than 64 KB at a time, we have to create
991 // a bounce buffer and move the data a piece at a time between the
992 // bounce buffer and the actual transfer buffer.
993
994 alloclen = min(totallen, 65536u);
995 buffer = kmalloc(alloclen, GFP_NOIO);
996 if (buffer == NULL)
997 return USB_STOR_TRANSPORT_ERROR;
998
999 do {
1000 // loop, never allocate or transfer more than 64k at once
1001 // (min(128k, 255*info->ssize) is the real limit)
1002 len = min(totallen, alloclen);
1003 thistime = (len / info->ssize) & 0xff;
1004
1005 // ATAPI command 0x20 (READ SECTORS)
1006 usbat_pack_atapi_sector_cmd(command, thistime, sector, 0x20);
1007
1008 // Write/execute ATAPI read command
1009 result = usbat_multiple_write(us, registers, command, 7);
1010 if (result != USB_STOR_TRANSPORT_GOOD)
1011 goto leave;
1012
1013 // Read the data we just requested
1014 result = usbat_read_blocks(us, buffer, len);
1015 if (result != USB_STOR_TRANSPORT_GOOD)
1016 goto leave;
1017
1018 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1019
1020 // Store the data in the transfer buffer
1021 usb_stor_access_xfer_buf(buffer, len, us->srb,
1022 &sg_idx, &sg_offset, TO_XFER_BUF);
1023
1024 sector += thistime;
1025 totallen -= len;
1026 } while (totallen > 0);
1027
1028 kfree(buffer);
1029 return USB_STOR_TRANSPORT_GOOD;
1030
1031leave:
1032 kfree(buffer);
1033 return USB_STOR_TRANSPORT_ERROR;
1034}
1035
1036/*
1037 * Write data to device
1038 */
1039static int usbat_flash_write_data(struct us_data *us,
1040 struct usbat_info *info,
1041 u32 sector,
1042 u32 sectors)
1043{
1044 unsigned char registers[7] = {
1045 USBAT_ATA_FEATURES,
1046 USBAT_ATA_SECCNT,
1047 USBAT_ATA_SECNUM,
1048 USBAT_ATA_LBA_ME,
1049 USBAT_ATA_LBA_HI,
1050 USBAT_ATA_DEVICE,
1051 USBAT_ATA_STATUS,
1052 };
1053 unsigned char command[7];
1054 unsigned char *buffer;
1055 unsigned char thistime;
1056 unsigned int totallen, alloclen;
1057 int len, result;
1058 unsigned int sg_idx = 0, sg_offset = 0;
1059
1060 result = usbat_flash_check_media(us, info);
1061 if (result != USB_STOR_TRANSPORT_GOOD)
1062 return result;
1063
1064 // we're working in LBA mode. according to the ATA spec,
1065 // we can support up to 28-bit addressing. I don't know if Jumpshot
1066 // supports beyond 24-bit addressing. It's kind of hard to test
1067 // since it requires > 8GB CF card.
1068
1069 if (sector > 0x0FFFFFFF)
1070 return USB_STOR_TRANSPORT_ERROR;
1071
1072 totallen = sectors * info->ssize;
1073
1074 // Since we don't write more than 64 KB at a time, we have to create
1075 // a bounce buffer and move the data a piece at a time between the
1076 // bounce buffer and the actual transfer buffer.
1077
1078 alloclen = min(totallen, 65536u);
1079 buffer = kmalloc(alloclen, GFP_NOIO);
1080 if (buffer == NULL)
1081 return USB_STOR_TRANSPORT_ERROR;
1082
1083 do {
1084 // loop, never allocate or transfer more than 64k at once
1085 // (min(128k, 255*info->ssize) is the real limit)
1086 len = min(totallen, alloclen);
1087 thistime = (len / info->ssize) & 0xff;
1088
1089 // Get the data from the transfer buffer
1090 usb_stor_access_xfer_buf(buffer, len, us->srb,
1091 &sg_idx, &sg_offset, FROM_XFER_BUF);
1092
1093 // ATAPI command 0x30 (WRITE SECTORS)
1094 usbat_pack_atapi_sector_cmd(command, thistime, sector, 0x30);
1095
1096 // Write/execute ATAPI write command
1097 result = usbat_multiple_write(us, registers, command, 7);
1098 if (result != USB_STOR_TRANSPORT_GOOD)
1099 goto leave;
1100
1101 // Write the data
1102 result = usbat_write_blocks(us, buffer, len);
1103 if (result != USB_STOR_TRANSPORT_GOOD)
1104 goto leave;
1105
1106 sector += thistime;
1107 totallen -= len;
1108 } while (totallen > 0);
1109
1110 kfree(buffer);
1111 return result;
1112
1113leave:
1114 kfree(buffer);
1115 return USB_STOR_TRANSPORT_ERROR;
1116}
1117
1118/*
1119 * Squeeze a potentially huge (> 65535 byte) read10 command into
1120 * a little ( <= 65535 byte) ATAPI pipe
1121 */
1122static int usbat_hp8200e_handle_read10(struct us_data *us,
1123 unsigned char *registers,
1124 unsigned char *data,
1125 struct scsi_cmnd *srb)
1126{
1127 int result = USB_STOR_TRANSPORT_GOOD;
1128 unsigned char *buffer;
1129 unsigned int len;
1130 unsigned int sector;
1131 unsigned int sg_segment = 0;
1132 unsigned int sg_offset = 0;
1133
1134 US_DEBUGP("handle_read10: transfersize %d\n",
1135 srb->transfersize);
1136
1137 if (srb->request_bufflen < 0x10000) {
1138
1139 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1140 registers, data, 19,
1141 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1142 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1143 DMA_FROM_DEVICE,
1144 srb->request_buffer,
1145 srb->request_bufflen, srb->use_sg, 1);
1146
1147 return result;
1148 }
1149
1150 /*
1151 * Since we're requesting more data than we can handle in
1152 * a single read command (max is 64k-1), we will perform
1153 * multiple reads, but each read must be in multiples of
1154 * a sector. Luckily the sector size is in srb->transfersize
1155 * (see linux/drivers/scsi/sr.c).
1156 */
1157
1158 if (data[7+0] == GPCMD_READ_CD) {
1159 len = short_pack(data[7+9], data[7+8]);
1160 len <<= 16;
1161 len |= data[7+7];
1162 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1163 srb->transfersize = srb->request_bufflen/len;
1164 }
1165
1166 if (!srb->transfersize) {
1167 srb->transfersize = 2048; /* A guess */
1168 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1169 srb->transfersize);
1170 }
1171
1172 // Since we only read in one block at a time, we have to create
1173 // a bounce buffer and move the data a piece at a time between the
1174 // bounce buffer and the actual transfer buffer.
1175
1176 len = (65535/srb->transfersize) * srb->transfersize;
1177 US_DEBUGP("Max read is %d bytes\n", len);
1178 len = min(len, srb->request_bufflen);
1179 buffer = kmalloc(len, GFP_NOIO);
1180 if (buffer == NULL) // bloody hell!
1181 return USB_STOR_TRANSPORT_FAILED;
1182 sector = short_pack(data[7+3], data[7+2]);
1183 sector <<= 16;
1184 sector |= short_pack(data[7+5], data[7+4]);
1185 transferred = 0;
1186
1187 sg_segment = 0; // for keeping track of where we are in
1188 sg_offset = 0; // the scatter/gather list
1189
1190 while (transferred != srb->request_bufflen) {
1191
1192 if (len > srb->request_bufflen - transferred)
1193 len = srb->request_bufflen - transferred;
1194
1195 data[3] = len&0xFF; // (cylL) = expected length (L)
1196 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
1197
1198 // Fix up the SCSI command sector and num sectors
1199
1200 data[7+2] = MSB_of(sector>>16); // SCSI command sector
1201 data[7+3] = LSB_of(sector>>16);
1202 data[7+4] = MSB_of(sector&0xFFFF);
1203 data[7+5] = LSB_of(sector&0xFFFF);
1204 if (data[7+0] == GPCMD_READ_CD)
1205 data[7+6] = 0;
1206 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
1207 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
1208
1209 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1210 registers, data, 19,
1211 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1212 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1213 DMA_FROM_DEVICE,
1214 buffer,
1215 len, 0, 1);
1216
1217 if (result != USB_STOR_TRANSPORT_GOOD)
1218 break;
1219
1220 // Store the data in the transfer buffer
1221 usb_stor_access_xfer_buf(buffer, len, srb,
1222 &sg_segment, &sg_offset, TO_XFER_BUF);
1223
1224 // Update the amount transferred and the sector number
1225
1226 transferred += len;
1227 sector += len / srb->transfersize;
1228
1229 } // while transferred != srb->request_bufflen
1230
1231 kfree(buffer);
1232 return result;
1233}
1234
1235static int usbat_select_and_test_registers(struct us_data *us)
1236{
1237 int selector;
1238 unsigned char *status = us->iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 // try device = master, then device = slave.
Daniel Drake68a64572005-08-10 18:30:04 +01001241 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1242 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 USB_STOR_XFER_GOOD)
1244 return USB_STOR_TRANSPORT_ERROR;
1245
1246 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1247 USB_STOR_XFER_GOOD)
1248 return USB_STOR_TRANSPORT_ERROR;
1249
1250 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1251 USB_STOR_XFER_GOOD)
1252 return USB_STOR_TRANSPORT_ERROR;
1253
1254 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1255 USB_STOR_XFER_GOOD)
1256 return USB_STOR_TRANSPORT_ERROR;
1257
1258 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1259 USB_STOR_XFER_GOOD)
1260 return USB_STOR_TRANSPORT_ERROR;
1261
1262 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1263 USB_STOR_XFER_GOOD)
1264 return USB_STOR_TRANSPORT_ERROR;
1265
1266 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1267 USB_STOR_XFER_GOOD)
1268 return USB_STOR_TRANSPORT_ERROR;
1269
1270 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1271 USB_STOR_XFER_GOOD)
1272 return USB_STOR_TRANSPORT_ERROR;
1273
1274 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1275 USB_STOR_XFER_GOOD)
1276 return USB_STOR_TRANSPORT_ERROR;
1277 }
1278
1279 return USB_STOR_TRANSPORT_GOOD;
1280}
1281
1282/*
1283 * Initialize the USBAT processor and the storage device
1284 */
1285int init_usbat(struct us_data *us)
1286{
1287 int rc;
1288 struct usbat_info *info;
1289 unsigned char subcountH = USBAT_ATA_LBA_HI;
1290 unsigned char subcountL = USBAT_ATA_LBA_ME;
1291 unsigned char *status = us->iobuf;
1292
1293 us->extra = kmalloc(sizeof(struct usbat_info), GFP_NOIO);
1294 if (!us->extra) {
1295 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1296 return 1;
1297 }
1298 memset(us->extra, 0, sizeof(struct usbat_info));
1299 info = (struct usbat_info *) (us->extra);
1300
1301 // Enable peripheral control signals
1302 rc = usbat_write_user_io(us,
1303 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1304 USBAT_UIO_EPAD | USBAT_UIO_1);
1305 if (rc != USB_STOR_XFER_GOOD)
1306 return USB_STOR_TRANSPORT_ERROR;
1307
1308 US_DEBUGP("INIT 1\n");
1309
1310 msleep(2000);
1311
1312 rc = usbat_read_user_io(us, status);
1313 if (rc != USB_STOR_TRANSPORT_GOOD)
1314 return rc;
1315
1316 US_DEBUGP("INIT 2\n");
1317
1318 rc = usbat_read_user_io(us, status);
1319 if (rc != USB_STOR_XFER_GOOD)
1320 return USB_STOR_TRANSPORT_ERROR;
1321
1322 rc = usbat_read_user_io(us, status);
1323 if (rc != USB_STOR_XFER_GOOD)
1324 return USB_STOR_TRANSPORT_ERROR;
1325
1326 US_DEBUGP("INIT 3\n");
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 rc = usbat_select_and_test_registers(us);
1329 if (rc != USB_STOR_TRANSPORT_GOOD)
1330 return rc;
1331
Daniel Drake68a64572005-08-10 18:30:04 +01001332 US_DEBUGP("INIT 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 rc = usbat_read_user_io(us, status);
1335 if (rc != USB_STOR_XFER_GOOD)
1336 return USB_STOR_TRANSPORT_ERROR;
1337
Daniel Drake68a64572005-08-10 18:30:04 +01001338 US_DEBUGP("INIT 5\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 // Enable peripheral control signals and card detect
1341 rc = usbat_device_enable_cdt(us);
1342 if (rc != USB_STOR_TRANSPORT_GOOD)
1343 return rc;
1344
Daniel Drake68a64572005-08-10 18:30:04 +01001345 US_DEBUGP("INIT 6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 rc = usbat_read_user_io(us, status);
1348 if (rc != USB_STOR_XFER_GOOD)
1349 return USB_STOR_TRANSPORT_ERROR;
1350
Daniel Drake68a64572005-08-10 18:30:04 +01001351 US_DEBUGP("INIT 7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 msleep(1400);
1354
1355 rc = usbat_read_user_io(us, status);
1356 if (rc != USB_STOR_XFER_GOOD)
1357 return USB_STOR_TRANSPORT_ERROR;
1358
Daniel Drake68a64572005-08-10 18:30:04 +01001359 US_DEBUGP("INIT 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 rc = usbat_select_and_test_registers(us);
1362 if (rc != USB_STOR_TRANSPORT_GOOD)
1363 return rc;
1364
Daniel Drake68a64572005-08-10 18:30:04 +01001365 US_DEBUGP("INIT 9\n");
1366
1367 // At this point, we need to detect which device we are using
1368 if (usbat_set_transport(us, info))
1369 return USB_STOR_TRANSPORT_ERROR;
1370
1371 US_DEBUGP("INIT 10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1374 subcountH = 0x02;
1375 subcountL = 0x00;
1376 }
1377 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1378 0x00, 0x88, 0x08, subcountH, subcountL);
1379 if (rc != USB_STOR_XFER_GOOD)
1380 return USB_STOR_TRANSPORT_ERROR;
1381
Daniel Drake68a64572005-08-10 18:30:04 +01001382 US_DEBUGP("INIT 11\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 return USB_STOR_TRANSPORT_GOOD;
1385}
1386
1387/*
1388 * Transport for the HP 8200e
1389 */
1390static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1391{
1392 int result;
1393 unsigned char *status = us->iobuf;
1394 unsigned char registers[32];
1395 unsigned char data[32];
1396 unsigned int len;
1397 int i;
1398 char string[64];
1399
1400 len = srb->request_bufflen;
1401
1402 /* Send A0 (ATA PACKET COMMAND).
1403 Note: I guess we're never going to get any of the ATA
1404 commands... just ATA Packet Commands.
1405 */
1406
1407 registers[0] = USBAT_ATA_FEATURES;
1408 registers[1] = USBAT_ATA_SECCNT;
1409 registers[2] = USBAT_ATA_SECNUM;
1410 registers[3] = USBAT_ATA_LBA_ME;
1411 registers[4] = USBAT_ATA_LBA_HI;
1412 registers[5] = USBAT_ATA_DEVICE;
1413 registers[6] = USBAT_ATA_CMD;
1414 data[0] = 0x00;
1415 data[1] = 0x00;
1416 data[2] = 0x00;
1417 data[3] = len&0xFF; // (cylL) = expected length (L)
1418 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
1419 data[5] = 0xB0; // (device sel) = slave
1420 data[6] = 0xA0; // (command) = ATA PACKET COMMAND
1421
1422 for (i=7; i<19; i++) {
1423 registers[i] = 0x10;
1424 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1425 }
1426
1427 result = usbat_get_status(us, status);
1428 US_DEBUGP("Status = %02X\n", *status);
1429 if (result != USB_STOR_XFER_GOOD)
1430 return USB_STOR_TRANSPORT_ERROR;
1431 if (srb->cmnd[0] == TEST_UNIT_READY)
1432 transferred = 0;
1433
1434 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1435
1436 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1437 registers, data, 19,
1438 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1439 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1440 DMA_TO_DEVICE,
1441 srb->request_buffer,
1442 len, srb->use_sg, 10);
1443
1444 if (result == USB_STOR_TRANSPORT_GOOD) {
1445 transferred += len;
1446 US_DEBUGP("Wrote %08X bytes\n", transferred);
1447 }
1448
1449 return result;
1450
1451 } else if (srb->cmnd[0] == READ_10 ||
1452 srb->cmnd[0] == GPCMD_READ_CD) {
1453
1454 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1455
1456 }
1457
1458 if (len > 0xFFFF) {
1459 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1460 len);
1461 return USB_STOR_TRANSPORT_ERROR;
1462 }
1463
1464 if ( (result = usbat_multiple_write(us,
1465 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1466 return result;
1467 }
1468
1469 // Write the 12-byte command header.
1470
1471 // If the command is BLANK then set the timer for 75 minutes.
1472 // Otherwise set it for 10 minutes.
1473
1474 // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1475 // AT SPEED 4 IS UNRELIABLE!!!
1476
1477 if ( (result = usbat_write_block(us,
1478 USBAT_ATA, srb->cmnd, 12,
1479 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
1480 USB_STOR_TRANSPORT_GOOD) {
1481 return result;
1482 }
1483
1484 // If there is response data to be read in
1485 // then do it here.
1486
1487 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1488
1489 // How many bytes to read in? Check cylL register
1490
1491 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1492 USB_STOR_XFER_GOOD) {
1493 return USB_STOR_TRANSPORT_ERROR;
1494 }
1495
1496 if (len > 0xFF) { // need to read cylH also
1497 len = *status;
1498 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1499 USB_STOR_XFER_GOOD) {
1500 return USB_STOR_TRANSPORT_ERROR;
1501 }
1502 len += ((unsigned int) *status)<<8;
1503 }
1504 else
1505 len = *status;
1506
1507
1508 result = usbat_read_block(us, srb->request_buffer, len);
1509
1510 /* Debug-print the first 32 bytes of the transfer */
1511
1512 if (!srb->use_sg) {
1513 string[0] = 0;
1514 for (i=0; i<len && i<32; i++) {
1515 sprintf(string+strlen(string), "%02X ",
1516 ((unsigned char *)srb->request_buffer)[i]);
1517 if ((i%16)==15) {
1518 US_DEBUGP("%s\n", string);
1519 string[0] = 0;
1520 }
1521 }
1522 if (string[0]!=0)
1523 US_DEBUGP("%s\n", string);
1524 }
1525 }
1526
1527 return result;
1528}
1529
1530/*
1531 * Transport for USBAT02-based CompactFlash and similar storage devices
1532 */
1533static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1534{
1535 int rc;
1536 struct usbat_info *info = (struct usbat_info *) (us->extra);
1537 unsigned long block, blocks;
1538 unsigned char *ptr = us->iobuf;
1539 static unsigned char inquiry_response[36] = {
1540 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1541 };
1542
1543 if (srb->cmnd[0] == INQUIRY) {
1544 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1545 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1546 fill_inquiry_response(us, ptr, 36);
1547 return USB_STOR_TRANSPORT_GOOD;
1548 }
1549
1550 if (srb->cmnd[0] == READ_CAPACITY) {
1551 rc = usbat_flash_check_media(us, info);
1552 if (rc != USB_STOR_TRANSPORT_GOOD)
1553 return rc;
1554
1555 rc = usbat_flash_get_sector_count(us, info);
1556 if (rc != USB_STOR_TRANSPORT_GOOD)
1557 return rc;
1558
1559 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
1560 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1561 info->sectors, info->ssize);
1562
1563 // build the reply
1564 // note: must return the sector number of the last sector,
1565 // *not* the total number of sectors
1566 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1567 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1568 usb_stor_set_xfer_buf(ptr, 8, srb);
1569
1570 return USB_STOR_TRANSPORT_GOOD;
1571 }
1572
1573 if (srb->cmnd[0] == MODE_SELECT_10) {
1574 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1575 return USB_STOR_TRANSPORT_ERROR;
1576 }
1577
1578 if (srb->cmnd[0] == READ_10) {
1579 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1580 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1581
1582 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1583
1584 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1585 return usbat_flash_read_data(us, info, block, blocks);
1586 }
1587
1588 if (srb->cmnd[0] == READ_12) {
1589 // I don't think we'll ever see a READ_12 but support it anyway...
1590 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1591 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1592
1593 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1594 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1595
1596 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1597 return usbat_flash_read_data(us, info, block, blocks);
1598 }
1599
1600 if (srb->cmnd[0] == WRITE_10) {
1601 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1602 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1603
1604 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1605
1606 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1607 return usbat_flash_write_data(us, info, block, blocks);
1608 }
1609
1610 if (srb->cmnd[0] == WRITE_12) {
1611 // I don't think we'll ever see a WRITE_12 but support it anyway...
1612 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1613 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1614
1615 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1616 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1617
1618 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1619 return usbat_flash_write_data(us, info, block, blocks);
1620 }
1621
1622
1623 if (srb->cmnd[0] == TEST_UNIT_READY) {
1624 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1625
1626 rc = usbat_flash_check_media(us, info);
1627 if (rc != USB_STOR_TRANSPORT_GOOD)
1628 return rc;
1629
1630 return usbat_check_status(us);
1631 }
1632
1633 if (srb->cmnd[0] == REQUEST_SENSE) {
1634 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1635
1636 memset(ptr, 0, 18);
1637 ptr[0] = 0xF0;
1638 ptr[2] = info->sense_key;
1639 ptr[7] = 11;
1640 ptr[12] = info->sense_asc;
1641 ptr[13] = info->sense_ascq;
1642 usb_stor_set_xfer_buf(ptr, 18, srb);
1643
1644 return USB_STOR_TRANSPORT_GOOD;
1645 }
1646
1647 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1648 // sure. whatever. not like we can stop the user from popping
1649 // the media out of the device (no locking doors, etc)
1650 return USB_STOR_TRANSPORT_GOOD;
1651 }
1652
1653 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1654 srb->cmnd[0], srb->cmnd[0]);
1655 info->sense_key = 0x05;
1656 info->sense_asc = 0x20;
1657 info->sense_ascq = 0x00;
1658 return USB_STOR_TRANSPORT_FAILED;
1659}
1660
1661/*
1662 * Default transport function. Attempts to detect which transport function
1663 * should be called, makes it the new default, and calls it.
1664 *
1665 * This function should never be called. Our usbat_init() function detects the
1666 * device type and changes the us->transport ptr to the transport function
1667 * relevant to the device.
1668 * However, we'll support this impossible(?) case anyway.
1669 */
1670int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1671{
1672 struct usbat_info *info = (struct usbat_info*) (us->extra);
1673
1674 if (usbat_set_transport(us, info))
1675 return USB_STOR_TRANSPORT_ERROR;
1676
1677 return us->transport(srb, us);
1678}
1679