alpha: fix trivial section mismatch warnings
[pv_ops_mirror.git] / drivers / usb / storage / sddr55.c
blob0b1b5b59ca7b6d2423a002a143ef3cf2f1f207a5
1 /* Driver for SanDisk SDDR-55 SmartMedia reader
3 * $Id:$
5 * SDDR55 driver v0.1:
7 * First release
9 * Current development and maintenance by:
10 * (c) 2002 Simon Munton
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
15 * later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/jiffies.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
34 #include "usb.h"
35 #include "transport.h"
36 #include "protocol.h"
37 #include "debug.h"
38 #include "sddr55.h"
41 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
42 #define LSB_of(s) ((s)&0xFF)
43 #define MSB_of(s) ((s)>>8)
44 #define PAGESIZE 512
46 #define set_sense_info(sk, asc, ascq) \
47 do { \
48 info->sense_data[2] = sk; \
49 info->sense_data[12] = asc; \
50 info->sense_data[13] = ascq; \
51 } while (0)
54 struct sddr55_card_info {
55 unsigned long capacity; /* Size of card in bytes */
56 int max_log_blks; /* maximum number of logical blocks */
57 int pageshift; /* log2 of pagesize */
58 int smallpageshift; /* 1 if pagesize == 256 */
59 int blocksize; /* Size of block in pages */
60 int blockshift; /* log2 of blocksize */
61 int blockmask; /* 2^blockshift - 1 */
62 int read_only; /* non zero if card is write protected */
63 int force_read_only; /* non zero if we find a map error*/
64 int *lba_to_pba; /* logical to physical map */
65 int *pba_to_lba; /* physical to logical map */
66 int fatal_error; /* set if we detect something nasty */
67 unsigned long last_access; /* number of jiffies since we last talked to device */
68 unsigned char sense_data[18];
72 #define NOT_ALLOCATED 0xffffffff
73 #define BAD_BLOCK 0xffff
74 #define CIS_BLOCK 0x400
75 #define UNUSED_BLOCK 0x3ff
77 static int
78 sddr55_bulk_transport(struct us_data *us, int direction,
79 unsigned char *data, unsigned int len) {
80 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
81 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
82 us->recv_bulk_pipe : us->send_bulk_pipe;
84 if (!len)
85 return USB_STOR_XFER_GOOD;
86 info->last_access = jiffies;
87 return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL);
90 /* check if card inserted, if there is, update read_only status
91 * return non zero if no card
94 static int sddr55_status(struct us_data *us)
96 int result;
97 unsigned char *command = us->iobuf;
98 unsigned char *status = us->iobuf;
99 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
101 /* send command */
102 memset(command, 0, 8);
103 command[5] = 0xB0;
104 command[7] = 0x80;
105 result = sddr55_bulk_transport(us,
106 DMA_TO_DEVICE, command, 8);
108 US_DEBUGP("Result for send_command in status %d\n",
109 result);
111 if (result != USB_STOR_XFER_GOOD) {
112 set_sense_info (4, 0, 0); /* hardware error */
113 return USB_STOR_TRANSPORT_ERROR;
116 result = sddr55_bulk_transport(us,
117 DMA_FROM_DEVICE, status, 4);
119 /* expect to get short transfer if no card fitted */
120 if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {
121 /* had a short transfer, no card inserted, free map memory */
122 kfree(info->lba_to_pba);
123 kfree(info->pba_to_lba);
124 info->lba_to_pba = NULL;
125 info->pba_to_lba = NULL;
127 info->fatal_error = 0;
128 info->force_read_only = 0;
130 set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
131 return USB_STOR_TRANSPORT_FAILED;
134 if (result != USB_STOR_XFER_GOOD) {
135 set_sense_info (4, 0, 0); /* hardware error */
136 return USB_STOR_TRANSPORT_FAILED;
139 /* check write protect status */
140 info->read_only = (status[0] & 0x20);
142 /* now read status */
143 result = sddr55_bulk_transport(us,
144 DMA_FROM_DEVICE, status, 2);
146 if (result != USB_STOR_XFER_GOOD) {
147 set_sense_info (4, 0, 0); /* hardware error */
150 return (result == USB_STOR_XFER_GOOD ?
151 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);
155 static int sddr55_read_data(struct us_data *us,
156 unsigned int lba,
157 unsigned int page,
158 unsigned short sectors) {
160 int result = USB_STOR_TRANSPORT_GOOD;
161 unsigned char *command = us->iobuf;
162 unsigned char *status = us->iobuf;
163 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
164 unsigned char *buffer;
166 unsigned int pba;
167 unsigned long address;
169 unsigned short pages;
170 unsigned int len, index, offset;
172 // Since we only read in one block at a time, we have to create
173 // a bounce buffer and move the data a piece at a time between the
174 // bounce buffer and the actual transfer buffer.
176 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
177 info->smallpageshift) * PAGESIZE;
178 buffer = kmalloc(len, GFP_NOIO);
179 if (buffer == NULL)
180 return USB_STOR_TRANSPORT_ERROR; /* out of memory */
181 index = offset = 0;
183 while (sectors>0) {
185 /* have we got to end? */
186 if (lba >= info->max_log_blks)
187 break;
189 pba = info->lba_to_pba[lba];
191 // Read as many sectors as possible in this block
193 pages = min((unsigned int) sectors << info->smallpageshift,
194 info->blocksize - page);
195 len = pages << info->pageshift;
197 US_DEBUGP("Read %02X pages, from PBA %04X"
198 " (LBA %04X) page %02X\n",
199 pages, pba, lba, page);
201 if (pba == NOT_ALLOCATED) {
202 /* no pba for this lba, fill with zeroes */
203 memset (buffer, 0, len);
204 } else {
206 address = (pba << info->blockshift) + page;
208 command[0] = 0;
209 command[1] = LSB_of(address>>16);
210 command[2] = LSB_of(address>>8);
211 command[3] = LSB_of(address);
213 command[4] = 0;
214 command[5] = 0xB0;
215 command[6] = LSB_of(pages << (1 - info->smallpageshift));
216 command[7] = 0x85;
218 /* send command */
219 result = sddr55_bulk_transport(us,
220 DMA_TO_DEVICE, command, 8);
222 US_DEBUGP("Result for send_command in read_data %d\n",
223 result);
225 if (result != USB_STOR_XFER_GOOD) {
226 result = USB_STOR_TRANSPORT_ERROR;
227 goto leave;
230 /* read data */
231 result = sddr55_bulk_transport(us,
232 DMA_FROM_DEVICE, buffer, len);
234 if (result != USB_STOR_XFER_GOOD) {
235 result = USB_STOR_TRANSPORT_ERROR;
236 goto leave;
239 /* now read status */
240 result = sddr55_bulk_transport(us,
241 DMA_FROM_DEVICE, status, 2);
243 if (result != USB_STOR_XFER_GOOD) {
244 result = USB_STOR_TRANSPORT_ERROR;
245 goto leave;
248 /* check status for error */
249 if (status[0] == 0xff && status[1] == 0x4) {
250 set_sense_info (3, 0x11, 0);
251 result = USB_STOR_TRANSPORT_FAILED;
252 goto leave;
256 // Store the data in the transfer buffer
257 usb_stor_access_xfer_buf(buffer, len, us->srb,
258 &index, &offset, TO_XFER_BUF);
260 page = 0;
261 lba++;
262 sectors -= pages >> info->smallpageshift;
265 result = USB_STOR_TRANSPORT_GOOD;
267 leave:
268 kfree(buffer);
270 return result;
273 static int sddr55_write_data(struct us_data *us,
274 unsigned int lba,
275 unsigned int page,
276 unsigned short sectors) {
278 int result = USB_STOR_TRANSPORT_GOOD;
279 unsigned char *command = us->iobuf;
280 unsigned char *status = us->iobuf;
281 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
282 unsigned char *buffer;
284 unsigned int pba;
285 unsigned int new_pba;
286 unsigned long address;
288 unsigned short pages;
289 int i;
290 unsigned int len, index, offset;
292 /* check if we are allowed to write */
293 if (info->read_only || info->force_read_only) {
294 set_sense_info (7, 0x27, 0); /* read only */
295 return USB_STOR_TRANSPORT_FAILED;
298 // Since we only write one block at a time, we have to create
299 // a bounce buffer and move the data a piece at a time between the
300 // bounce buffer and the actual transfer buffer.
302 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
303 info->smallpageshift) * PAGESIZE;
304 buffer = kmalloc(len, GFP_NOIO);
305 if (buffer == NULL)
306 return USB_STOR_TRANSPORT_ERROR;
307 index = offset = 0;
309 while (sectors > 0) {
311 /* have we got to end? */
312 if (lba >= info->max_log_blks)
313 break;
315 pba = info->lba_to_pba[lba];
317 // Write as many sectors as possible in this block
319 pages = min((unsigned int) sectors << info->smallpageshift,
320 info->blocksize - page);
321 len = pages << info->pageshift;
323 // Get the data from the transfer buffer
324 usb_stor_access_xfer_buf(buffer, len, us->srb,
325 &index, &offset, FROM_XFER_BUF);
327 US_DEBUGP("Write %02X pages, to PBA %04X"
328 " (LBA %04X) page %02X\n",
329 pages, pba, lba, page);
331 command[4] = 0;
333 if (pba == NOT_ALLOCATED) {
334 /* no pba allocated for this lba, find a free pba to use */
336 int max_pba = (info->max_log_blks / 250 ) * 256;
337 int found_count = 0;
338 int found_pba = -1;
340 /* set pba to first block in zone lba is in */
341 pba = (lba / 1000) * 1024;
343 US_DEBUGP("No PBA for LBA %04X\n",lba);
345 if (max_pba > 1024)
346 max_pba = 1024;
349 * Scan through the map looking for an unused block
350 * leave 16 unused blocks at start (or as many as
351 * possible) since the sddr55 seems to reuse a used
352 * block when it shouldn't if we don't leave space.
354 for (i = 0; i < max_pba; i++, pba++) {
355 if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
356 found_pba = pba;
357 if (found_count++ > 16)
358 break;
362 pba = found_pba;
364 if (pba == -1) {
365 /* oh dear */
366 US_DEBUGP("Couldn't find unallocated block\n");
368 set_sense_info (3, 0x31, 0); /* medium error */
369 result = USB_STOR_TRANSPORT_FAILED;
370 goto leave;
373 US_DEBUGP("Allocating PBA %04X for LBA %04X\n", pba, lba);
375 /* set writing to unallocated block flag */
376 command[4] = 0x40;
379 address = (pba << info->blockshift) + page;
381 command[1] = LSB_of(address>>16);
382 command[2] = LSB_of(address>>8);
383 command[3] = LSB_of(address);
385 /* set the lba into the command, modulo 1000 */
386 command[0] = LSB_of(lba % 1000);
387 command[6] = MSB_of(lba % 1000);
389 command[4] |= LSB_of(pages >> info->smallpageshift);
390 command[5] = 0xB0;
391 command[7] = 0x86;
393 /* send command */
394 result = sddr55_bulk_transport(us,
395 DMA_TO_DEVICE, command, 8);
397 if (result != USB_STOR_XFER_GOOD) {
398 US_DEBUGP("Result for send_command in write_data %d\n",
399 result);
401 /* set_sense_info is superfluous here? */
402 set_sense_info (3, 0x3, 0);/* peripheral write error */
403 result = USB_STOR_TRANSPORT_FAILED;
404 goto leave;
407 /* send the data */
408 result = sddr55_bulk_transport(us,
409 DMA_TO_DEVICE, buffer, len);
411 if (result != USB_STOR_XFER_GOOD) {
412 US_DEBUGP("Result for send_data in write_data %d\n",
413 result);
415 /* set_sense_info is superfluous here? */
416 set_sense_info (3, 0x3, 0);/* peripheral write error */
417 result = USB_STOR_TRANSPORT_FAILED;
418 goto leave;
421 /* now read status */
422 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
424 if (result != USB_STOR_XFER_GOOD) {
425 US_DEBUGP("Result for get_status in write_data %d\n",
426 result);
428 /* set_sense_info is superfluous here? */
429 set_sense_info (3, 0x3, 0);/* peripheral write error */
430 result = USB_STOR_TRANSPORT_FAILED;
431 goto leave;
434 new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
435 >> info->blockshift;
437 /* check status for error */
438 if (status[0] == 0xff && status[1] == 0x4) {
439 info->pba_to_lba[new_pba] = BAD_BLOCK;
441 set_sense_info (3, 0x0c, 0);
442 result = USB_STOR_TRANSPORT_FAILED;
443 goto leave;
446 US_DEBUGP("Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
447 lba, pba, new_pba);
449 /* update the lba<->pba maps, note new_pba might be the same as pba */
450 info->lba_to_pba[lba] = new_pba;
451 info->pba_to_lba[pba] = UNUSED_BLOCK;
453 /* check that new_pba wasn't already being used */
454 if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
455 printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
456 new_pba, info->pba_to_lba[new_pba]);
457 info->fatal_error = 1;
458 set_sense_info (3, 0x31, 0);
459 result = USB_STOR_TRANSPORT_FAILED;
460 goto leave;
463 /* update the pba<->lba maps for new_pba */
464 info->pba_to_lba[new_pba] = lba % 1000;
466 page = 0;
467 lba++;
468 sectors -= pages >> info->smallpageshift;
470 result = USB_STOR_TRANSPORT_GOOD;
472 leave:
473 kfree(buffer);
474 return result;
477 static int sddr55_read_deviceID(struct us_data *us,
478 unsigned char *manufacturerID,
479 unsigned char *deviceID) {
481 int result;
482 unsigned char *command = us->iobuf;
483 unsigned char *content = us->iobuf;
485 memset(command, 0, 8);
486 command[5] = 0xB0;
487 command[7] = 0x84;
488 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
490 US_DEBUGP("Result of send_control for device ID is %d\n",
491 result);
493 if (result != USB_STOR_XFER_GOOD)
494 return USB_STOR_TRANSPORT_ERROR;
496 result = sddr55_bulk_transport(us,
497 DMA_FROM_DEVICE, content, 4);
499 if (result != USB_STOR_XFER_GOOD)
500 return USB_STOR_TRANSPORT_ERROR;
502 *manufacturerID = content[0];
503 *deviceID = content[1];
505 if (content[0] != 0xff) {
506 result = sddr55_bulk_transport(us,
507 DMA_FROM_DEVICE, content, 2);
510 return USB_STOR_TRANSPORT_GOOD;
514 int sddr55_reset(struct us_data *us) {
515 return 0;
519 static unsigned long sddr55_get_capacity(struct us_data *us) {
521 unsigned char manufacturerID;
522 unsigned char deviceID;
523 int result;
524 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
526 US_DEBUGP("Reading capacity...\n");
528 result = sddr55_read_deviceID(us,
529 &manufacturerID,
530 &deviceID);
532 US_DEBUGP("Result of read_deviceID is %d\n",
533 result);
535 if (result != USB_STOR_XFER_GOOD)
536 return 0;
538 US_DEBUGP("Device ID = %02X\n", deviceID);
539 US_DEBUGP("Manuf ID = %02X\n", manufacturerID);
541 info->pageshift = 9;
542 info->smallpageshift = 0;
543 info->blocksize = 16;
544 info->blockshift = 4;
545 info->blockmask = 15;
547 switch (deviceID) {
549 case 0x6e: // 1MB
550 case 0xe8:
551 case 0xec:
552 info->pageshift = 8;
553 info->smallpageshift = 1;
554 return 0x00100000;
556 case 0xea: // 2MB
557 case 0x64:
558 info->pageshift = 8;
559 info->smallpageshift = 1;
560 case 0x5d: // 5d is a ROM card with pagesize 512.
561 return 0x00200000;
563 case 0xe3: // 4MB
564 case 0xe5:
565 case 0x6b:
566 case 0xd5:
567 return 0x00400000;
569 case 0xe6: // 8MB
570 case 0xd6:
571 return 0x00800000;
573 case 0x73: // 16MB
574 info->blocksize = 32;
575 info->blockshift = 5;
576 info->blockmask = 31;
577 return 0x01000000;
579 case 0x75: // 32MB
580 info->blocksize = 32;
581 info->blockshift = 5;
582 info->blockmask = 31;
583 return 0x02000000;
585 case 0x76: // 64MB
586 info->blocksize = 32;
587 info->blockshift = 5;
588 info->blockmask = 31;
589 return 0x04000000;
591 case 0x79: // 128MB
592 info->blocksize = 32;
593 info->blockshift = 5;
594 info->blockmask = 31;
595 return 0x08000000;
597 default: // unknown
598 return 0;
603 static int sddr55_read_map(struct us_data *us) {
605 struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
606 int numblocks;
607 unsigned char *buffer;
608 unsigned char *command = us->iobuf;
609 int i;
610 unsigned short lba;
611 unsigned short max_lba;
612 int result;
614 if (!info->capacity)
615 return -1;
617 numblocks = info->capacity >> (info->blockshift + info->pageshift);
619 buffer = kmalloc( numblocks * 2, GFP_NOIO );
621 if (!buffer)
622 return -1;
624 memset(command, 0, 8);
625 command[5] = 0xB0;
626 command[6] = numblocks * 2 / 256;
627 command[7] = 0x8A;
629 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
631 if ( result != USB_STOR_XFER_GOOD) {
632 kfree (buffer);
633 return -1;
636 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
638 if ( result != USB_STOR_XFER_GOOD) {
639 kfree (buffer);
640 return -1;
643 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
645 if ( result != USB_STOR_XFER_GOOD) {
646 kfree (buffer);
647 return -1;
650 kfree(info->lba_to_pba);
651 kfree(info->pba_to_lba);
652 info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
653 info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
655 if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
656 kfree(info->lba_to_pba);
657 kfree(info->pba_to_lba);
658 info->lba_to_pba = NULL;
659 info->pba_to_lba = NULL;
660 kfree(buffer);
661 return -1;
664 memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
665 memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
667 /* set maximum lba */
668 max_lba = info->max_log_blks;
669 if (max_lba > 1000)
670 max_lba = 1000;
672 // Each block is 64 bytes of control data, so block i is located in
673 // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
675 for (i=0; i<numblocks; i++) {
676 int zone = i / 1024;
678 lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
680 /* Every 1024 physical blocks ("zone"), the LBA numbers
681 * go back to zero, but are within a higher
682 * block of LBA's. Also, there is a maximum of
683 * 1000 LBA's per zone. In other words, in PBA
684 * 1024-2047 you will find LBA 0-999 which are
685 * really LBA 1000-1999. Yes, this wastes 24
686 * physical blocks per zone. Go figure.
687 * These devices can have blocks go bad, so there
688 * are 24 spare blocks to use when blocks do go bad.
691 /* SDDR55 returns 0xffff for a bad block, and 0x400 for the
692 * CIS block. (Is this true for cards 8MB or less??)
693 * Record these in the physical to logical map
696 info->pba_to_lba[i] = lba;
698 if (lba >= max_lba) {
699 continue;
702 if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
703 !info->force_read_only) {
704 printk("sddr55: map inconsistency at LBA %04X\n", lba + zone * 1000);
705 info->force_read_only = 1;
708 if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
709 US_DEBUGP("LBA %04X <-> PBA %04X\n", lba, i);
711 info->lba_to_pba[lba + zone * 1000] = i;
714 kfree(buffer);
715 return 0;
719 static void sddr55_card_info_destructor(void *extra) {
720 struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
722 if (!extra)
723 return;
725 kfree(info->lba_to_pba);
726 kfree(info->pba_to_lba);
731 * Transport for the Sandisk SDDR-55
733 int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
735 int result;
736 static unsigned char inquiry_response[8] = {
737 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
739 // write-protected for now, no block descriptor support
740 static unsigned char mode_page_01[20] = {
741 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
742 0x01, 0x0A,
743 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
745 unsigned char *ptr = us->iobuf;
746 unsigned long capacity;
747 unsigned int lba;
748 unsigned int pba;
749 unsigned int page;
750 unsigned short pages;
751 struct sddr55_card_info *info;
753 if (!us->extra) {
754 us->extra = kzalloc(
755 sizeof(struct sddr55_card_info), GFP_NOIO);
756 if (!us->extra)
757 return USB_STOR_TRANSPORT_ERROR;
758 us->extra_destructor = sddr55_card_info_destructor;
761 info = (struct sddr55_card_info *)(us->extra);
763 if (srb->cmnd[0] == REQUEST_SENSE) {
764 US_DEBUGP("SDDR55: request sense %02x/%02x/%02x\n", info->sense_data[2], info->sense_data[12], info->sense_data[13]);
766 memcpy (ptr, info->sense_data, sizeof info->sense_data);
767 ptr[0] = 0x70;
768 ptr[7] = 11;
769 usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb);
770 memset (info->sense_data, 0, sizeof info->sense_data);
772 return USB_STOR_TRANSPORT_GOOD;
775 memset (info->sense_data, 0, sizeof info->sense_data);
777 /* Dummy up a response for INQUIRY since SDDR55 doesn't
778 respond to INQUIRY commands */
780 if (srb->cmnd[0] == INQUIRY) {
781 memcpy(ptr, inquiry_response, 8);
782 fill_inquiry_response(us, ptr, 36);
783 return USB_STOR_TRANSPORT_GOOD;
786 /* only check card status if the map isn't allocated, ie no card seen yet
787 * or if it's been over half a second since we last accessed it
789 if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
791 /* check to see if a card is fitted */
792 result = sddr55_status (us);
793 if (result) {
794 result = sddr55_status (us);
795 if (!result) {
796 set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
798 return USB_STOR_TRANSPORT_FAILED;
802 /* if we detected a problem with the map when writing,
803 don't allow any more access */
804 if (info->fatal_error) {
806 set_sense_info (3, 0x31, 0);
807 return USB_STOR_TRANSPORT_FAILED;
810 if (srb->cmnd[0] == READ_CAPACITY) {
812 capacity = sddr55_get_capacity(us);
814 if (!capacity) {
815 set_sense_info (3, 0x30, 0); /* incompatible medium */
816 return USB_STOR_TRANSPORT_FAILED;
819 info->capacity = capacity;
821 /* figure out the maximum logical block number, allowing for
822 * the fact that only 250 out of every 256 are used */
823 info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
825 /* Last page in the card, adjust as we only use 250 out of
826 * every 256 pages */
827 capacity = (capacity / 256) * 250;
829 capacity /= PAGESIZE;
830 capacity--;
832 ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
833 ((__be32 *) ptr)[1] = cpu_to_be32(PAGESIZE);
834 usb_stor_set_xfer_buf(ptr, 8, srb);
836 sddr55_read_map(us);
838 return USB_STOR_TRANSPORT_GOOD;
841 if (srb->cmnd[0] == MODE_SENSE_10) {
843 memcpy(ptr, mode_page_01, sizeof mode_page_01);
844 ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0;
845 usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
847 if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
848 US_DEBUGP(
849 "SDDR55: Dummy up request for mode page 1\n");
850 return USB_STOR_TRANSPORT_GOOD;
852 } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
853 US_DEBUGP(
854 "SDDR55: Dummy up request for all mode pages\n");
855 return USB_STOR_TRANSPORT_GOOD;
858 set_sense_info (5, 0x24, 0); /* invalid field in command */
859 return USB_STOR_TRANSPORT_FAILED;
862 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
864 US_DEBUGP(
865 "SDDR55: %s medium removal. Not that I can do"
866 " anything about it...\n",
867 (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
869 return USB_STOR_TRANSPORT_GOOD;
873 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
875 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
876 page <<= 16;
877 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
878 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
880 page <<= info->smallpageshift;
882 // convert page to block and page-within-block
884 lba = page >> info->blockshift;
885 page = page & info->blockmask;
887 // locate physical block corresponding to logical block
889 if (lba >= info->max_log_blks) {
891 US_DEBUGP("Error: Requested LBA %04X exceeds maximum "
892 "block %04X\n", lba, info->max_log_blks-1);
894 set_sense_info (5, 0x24, 0); /* invalid field in command */
896 return USB_STOR_TRANSPORT_FAILED;
899 pba = info->lba_to_pba[lba];
901 if (srb->cmnd[0] == WRITE_10) {
902 US_DEBUGP("WRITE_10: write block %04X (LBA %04X) page %01X"
903 " pages %d\n",
904 pba, lba, page, pages);
906 return sddr55_write_data(us, lba, page, pages);
907 } else {
908 US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X"
909 " pages %d\n",
910 pba, lba, page, pages);
912 return sddr55_read_data(us, lba, page, pages);
917 if (srb->cmnd[0] == TEST_UNIT_READY) {
918 return USB_STOR_TRANSPORT_GOOD;
921 if (srb->cmnd[0] == START_STOP) {
922 return USB_STOR_TRANSPORT_GOOD;
925 set_sense_info (5, 0x20, 0); /* illegal command */
927 return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?