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