1 /* Driver for SanDisk SDDR-55 SmartMedia reader
2  *
3  * $Id:$
4  *
5  * SDDR55 driver v0.1:
6  *
7  * First release
8  *
9  * Current development and maintenance by:
10  *   (c) 2002 Simon Munton
11  *
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.
16  *
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.
21  *
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.
25  */
26 
27 #include "transport.h"
28 #include "protocol.h"
29 #include "usb.h"
30 #include "debug.h"
31 #include "sddr55.h"
32 
33 #include <linux/sched.h>
34 #include <linux/errno.h>
35 #include <linux/slab.h>
36 
37 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
38 #define LSB_of(s) ((s)&0xFF)
39 #define MSB_of(s) ((s)>>8)
40 #define PAGESIZE  512
41 
42 #define set_sense_info(sk, asc, ascq)	\
43     do {				\
44 	info->sense_data[2] = sk;	\
45 	info->sense_data[12] = asc;	\
46 	info->sense_data[13] = ascq;	\
47 	} while (0)
48 
49 
50 struct sddr55_card_info {
51 	unsigned long	capacity;	/* Size of card in bytes */
52 	int		max_log_blks;	/* maximum number of logical blocks */
53 	int		pageshift;	/* log2 of pagesize */
54 	int		smallpageshift;	/* 1 if pagesize == 256 */
55 	int		blocksize;	/* Size of block in pages */
56 	int		blockshift;	/* log2 of blocksize */
57 	int		blockmask;	/* 2^blockshift - 1 */
58 	int		read_only;	/* non zero if card is write protected */
59 	int		force_read_only;	/* non zero if we find a map error*/
60 	int		*lba_to_pba;	/* logical to physical map */
61 	int		*pba_to_lba;	/* physical to logical map */
62 	int		fatal_error;	/* set if we detect something nasty */
63 	unsigned long 	last_access;	/* number of jiffies since we last talked to device */
64 	unsigned char   sense_data[18];
65 };
66 
67 
68 #define NOT_ALLOCATED		0xffffffff
69 #define BAD_BLOCK		0xffff
70 #define CIS_BLOCK		0x400
71 #define UNUSED_BLOCK		0x3ff
72 
73 
74 
sddr55_raw_bulk(struct us_data * us,int direction,unsigned char * data,unsigned int len)75 static int sddr55_raw_bulk(struct us_data *us,
76 		int direction,
77 		unsigned char *data,
78 		unsigned int len) {
79 
80 	int result;
81 	int act_len;
82 	int pipe;
83 
84 	if (direction == SCSI_DATA_READ)
85 		pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
86 	else
87 		pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
88 
89 	result = usb_stor_bulk_msg(us, data, pipe, len, &act_len);
90 
91 	/* if we stall, we need to clear it before we go on */
92 	if (result == -EPIPE) {
93 		US_DEBUGP("EPIPE: clearing endpoint halt for"
94 			" pipe 0x%x, stalled at %d bytes\n",
95 			pipe, act_len);
96 		usb_clear_halt(us->pusb_dev, pipe);
97 	}
98 
99 	if (result) {
100 
101 		/* NAK - that means we've retried a few times already */
102 		if (result == -ETIMEDOUT) {
103 			US_DEBUGP("usbat_raw_bulk():"
104 				" device NAKed\n");
105 
106 			return US_BULK_TRANSFER_FAILED;
107 		}
108 
109 		/* -ECONNRESET -- we canceled this transfer */
110 		if (result == -ECONNRESET) {
111 			US_DEBUGP("usbat_raw_bulk():"
112 				" transfer aborted\n");
113 			return US_BULK_TRANSFER_ABORTED;
114 		}
115 
116 		if (result == -EPIPE) {
117 			US_DEBUGP("usbat_raw_bulk():"
118 				" output pipe stalled\n");
119 			return US_BULK_TRANSFER_FAILED;
120 		}
121 
122 		/* the catch-all case */
123 		US_DEBUGP("us_transfer_partial(): unknown error\n");
124 		return US_BULK_TRANSFER_FAILED;
125 	}
126 
127 	if (act_len != len) {
128 		US_DEBUGP("Warning: Transferred only %d bytes\n",
129 			act_len);
130 		return US_BULK_TRANSFER_SHORT;
131 	}
132 
133 	US_DEBUGP("Transferred %d of %d bytes\n", act_len, len);
134 
135 	return US_BULK_TRANSFER_GOOD;
136 }
137 
138 /*
139  * Note: direction must be set if command_len == 0.
140  */
141 
sddr55_bulk_transport(struct us_data * us,int direction,unsigned char * data,unsigned int len)142 static int sddr55_bulk_transport(struct us_data *us,
143 			  int direction,
144 			  unsigned char *data,
145 			  unsigned int len) {
146 
147 	int result = USB_STOR_TRANSPORT_GOOD;
148 	struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
149 
150 	if (len==0)
151 		return USB_STOR_TRANSPORT_GOOD;
152 
153 	info->last_access = jiffies;
154 
155 #ifdef CONFIG_USB_STORAGE_DEBUG
156 	if (direction == SCSI_DATA_WRITE) {
157 		int i;
158 		char string[64];
159 
160 		/* Debug-print the first 48 bytes of the write transfer */
161 
162 		strcpy(string, "wr: ");
163 		for (i=0; i<len && i<48; i++) {
164 			sprintf(string+strlen(string), "%02X ",
165 			  data[i]);
166 			if ((i%16)==15) {
167 				US_DEBUGP("%s\n", string);
168 				strcpy(string, "wr: ");
169 			}
170 		}
171 		if ((i%16)!=0)
172 			US_DEBUGP("%s\n", string);
173 	}
174 #endif
175 
176 	/* transfer the data */
177 
178 	US_DEBUGP("SCM data %s transfer %d\n",
179 		  ( direction==SCSI_DATA_READ ? "in" : "out"),
180 		  len);
181 
182 	result = sddr55_raw_bulk(us, direction, data, len);
183 
184 #ifdef CONFIG_USB_STORAGE_DEBUG
185 	if (direction == SCSI_DATA_READ) {
186 		int i;
187 		char string[64];
188 
189 		/* Debug-print the first 48 bytes of the read transfer */
190 
191 		strcpy(string, "rd: ");
192 		for (i=0; i<len && i<48; i++) {
193 			sprintf(string+strlen(string), "%02X ",
194 			  data[i]);
195 			if ((i%16)==15) {
196 				US_DEBUGP("%s\n", string);
197 				strcpy(string, "rd: ");
198 			}
199 		}
200 		if ((i%16)!=0)
201 			US_DEBUGP("%s\n", string);
202 	}
203 #endif
204 
205 	return result;
206 }
207 
208 
209 /* check if card inserted, if there is, update read_only status
210  * return non zero if no card
211  */
212 
sddr55_status(struct us_data * us)213 static int sddr55_status(struct us_data *us)
214 {
215 	int result;
216 	unsigned char command[8] = {
217 		0, 0, 0, 0, 0, 0xb0, 0, 0x80
218 	};
219 	unsigned char status[8];
220 	struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
221 
222 	/* send command */
223 	result = sddr55_bulk_transport(us,
224 		SCSI_DATA_WRITE, command, 8);
225 
226 	US_DEBUGP("Result for send_command in status %d\n",
227 		result);
228 
229 	if (result != US_BULK_TRANSFER_GOOD) {
230 		set_sense_info (4, 0, 0);	/* hardware error */
231 		return result;
232 	}
233 
234 	result = sddr55_bulk_transport(us,
235 		SCSI_DATA_READ, status,	4);
236 
237 	/* expect to get short transfer if no card fitted */
238 	if (result == US_BULK_TRANSFER_SHORT) {
239 		/* had a short transfer, no card inserted, free map memory */
240 		if (info->lba_to_pba)
241 			kfree(info->lba_to_pba);
242 		if (info->pba_to_lba)
243 			kfree(info->pba_to_lba);
244 		info->lba_to_pba = NULL;
245 		info->pba_to_lba = NULL;
246 
247 		info->fatal_error = 0;
248 		info->force_read_only = 0;
249 
250 		set_sense_info (2, 0x3a, 0);	/* not ready, medium not present */
251 		return result;
252 	}
253 
254 	if (result != US_BULK_TRANSFER_GOOD) {
255 		set_sense_info (4, 0, 0);	/* hardware error */
256 		return result;
257 	}
258 
259 	/* check write protect status */
260 	info->read_only = (status[0] & 0x20);
261 
262 	/* now read status */
263 	result = sddr55_bulk_transport(us,
264 		SCSI_DATA_READ, status,	2);
265 
266 	if (result != US_BULK_TRANSFER_GOOD) {
267 		set_sense_info (4, 0, 0);	/* hardware error */
268 	}
269 
270 	return result;
271 }
272 
273 
sddr55_read_data(struct us_data * us,unsigned int lba,unsigned int page,unsigned short sectors,unsigned char * content,int use_sg)274 static int sddr55_read_data(struct us_data *us,
275 		unsigned int lba,
276 		unsigned int page,
277 		unsigned short sectors,
278 		unsigned char *content,
279 		int use_sg) {
280 
281 	int result;
282 	unsigned char command[8] = {
283 		0, 0, 0, 0, 0, 0xb0, 0, 0x85
284 	};
285 	unsigned char status[8];
286 	struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
287 
288 	unsigned int pba;
289 	unsigned long address;
290 
291 	unsigned short pages;
292 	unsigned char *buffer = NULL;
293 	unsigned char *ptr;
294 	struct scatterlist *sg = NULL;
295 	int i;
296 	int len;
297 	int transferred;
298 
299 	// If we're using scatter-gather, we have to create a new
300 	// buffer to read all of the data in first, since a
301 	// scatter-gather buffer could in theory start in the middle
302 	// of a page, which would be bad. A developer who wants a
303 	// challenge might want to write a limited-buffer
304 	// version of this code.
305 
306 	len = sectors * PAGESIZE;
307 
308 	if (use_sg) {
309 		sg = (struct scatterlist *)content;
310 		buffer = kmalloc(len, GFP_NOIO);
311 		if (buffer == NULL)
312 			return USB_STOR_TRANSPORT_ERROR;
313 		ptr = buffer;
314 	} else
315 		ptr = content;
316 
317 	// This could be made much more efficient by checking for
318 	// contiguous LBA's. Another exercise left to the student.
319 
320 	while (sectors>0) {
321 
322 		/* have we got to end? */
323 		if (lba >= info->max_log_blks)
324 			break;
325 
326 		pba = info->lba_to_pba[lba];
327 
328 		// Read as many sectors as possible in this block
329 
330 		pages = info->blocksize - page;
331 		if (pages > (sectors << info->smallpageshift))
332 			pages = (sectors << info->smallpageshift);
333 
334 		US_DEBUGP("Read %02X pages, from PBA %04X"
335 			" (LBA %04X) page %02X\n",
336 			pages, pba, lba, page);
337 
338 		if (pba == NOT_ALLOCATED) {
339 			/* no pba for this lba, fill with zeroes */
340 			memset (ptr, 0, pages << info->pageshift);
341 		} else {
342 
343 			address = (pba << info->blockshift) + page;
344 
345 			command[1] = LSB_of(address>>16);
346 			command[2] = LSB_of(address>>8);
347 			command[3] = LSB_of(address);
348 
349 			command[6] = LSB_of(pages << (1 - info->smallpageshift));
350 
351 			/* send command */
352 			result = sddr55_bulk_transport(us,
353 				SCSI_DATA_WRITE, command, 8);
354 
355 			US_DEBUGP("Result for send_command in read_data %d\n",
356 				result);
357 
358 			if (result != US_BULK_TRANSFER_GOOD) {
359 				if (use_sg)
360 					kfree(buffer);
361 				return result;
362 			}
363 
364 			/* read data */
365 			result = sddr55_bulk_transport(us,
366 				SCSI_DATA_READ, ptr,
367 				pages<<info->pageshift);
368 
369 			if (result != US_BULK_TRANSFER_GOOD) {
370 				if (use_sg)
371 					kfree(buffer);
372 				return result;
373 			}
374 
375 			/* now read status */
376 			result = sddr55_bulk_transport(us,
377 				SCSI_DATA_READ, status, 2);
378 
379 			if (result != US_BULK_TRANSFER_GOOD) {
380 				if (use_sg)
381 					kfree(buffer);
382 				return result;
383 			}
384 
385 			/* check status for error */
386 			if (status[0] == 0xff && status[1] == 0x4) {
387 				set_sense_info (3, 0x11, 0);
388 				if (use_sg)
389 					kfree(buffer);
390 
391 				return USB_STOR_TRANSPORT_FAILED;
392 			}
393 
394 		}
395 
396 		page = 0;
397 		lba++;
398 		sectors -= pages >> info->smallpageshift;
399 		ptr += (pages << info->pageshift);
400 	}
401 
402 	if (use_sg) {
403 		transferred = 0;
404 		for (i=0; i<use_sg && transferred<len; i++) {
405 			memcpy(sg[i].address, buffer+transferred,
406 				len-transferred > sg[i].length ?
407 					sg[i].length : len-transferred);
408 			transferred += sg[i].length;
409 		}
410 		kfree(buffer);
411 	}
412 
413 	return USB_STOR_TRANSPORT_GOOD;
414 }
415 
sddr55_write_data(struct us_data * us,unsigned int lba,unsigned int page,unsigned short sectors,unsigned char * content,int use_sg)416 static int sddr55_write_data(struct us_data *us,
417 		unsigned int lba,
418 		unsigned int page,
419 		unsigned short sectors,
420 		unsigned char *content,
421 		int use_sg) {
422 
423 	int result;
424 	unsigned char command[8] = {
425 		0, 0, 0, 0, 0, 0xb0, 0, 0x86
426 	};
427 	unsigned char status[8];
428 	struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
429 
430 	unsigned int pba;
431 	unsigned int new_pba;
432 	unsigned long address;
433 
434 	unsigned short pages;
435 	unsigned char *buffer = NULL;
436 	unsigned char *ptr;
437 	struct scatterlist *sg = NULL;
438 	int i;
439 	int len;
440 	int transferred;
441 
442 	/* check if we are allowed to write */
443 	if (info->read_only || info->force_read_only) {
444 		set_sense_info (7, 0x27, 0);	/* read only */
445 		return USB_STOR_TRANSPORT_FAILED;
446 	}
447 
448 	// If we're using scatter-gather, we have to create a new
449 	// buffer to write all of the data in first, since a
450 	// scatter-gather buffer could in theory start in the middle
451 	// of a page, which would be bad. A developer who wants a
452 	// challenge might want to write a limited-buffer
453 	// version of this code.
454 
455 	len = sectors * PAGESIZE;
456 
457 	if (use_sg) {
458 		sg = (struct scatterlist *)content;
459 		buffer = kmalloc(len, GFP_NOIO);
460 		if (buffer == NULL)
461 			return USB_STOR_TRANSPORT_ERROR;
462 
463 		transferred = 0;
464 		for (i=0; i<use_sg && transferred<len; i++) {
465 			memcpy(buffer+transferred, sg[i].address,
466 				len-transferred > sg[i].length ?
467 					sg[i].length : len-transferred);
468 			transferred += sg[i].length;
469 		}
470 
471 		ptr = buffer;
472 	} else
473 		ptr = content;
474 
475 	while (sectors > 0) {
476 
477 		/* have we got to end? */
478 		if (lba >= info->max_log_blks)
479 			break;
480 
481 		pba = info->lba_to_pba[lba];
482 
483 		// Write as many sectors as possible in this block
484 
485 		pages = info->blocksize - page;
486 		if (pages > (sectors << info->smallpageshift))
487 			pages = (sectors << info->smallpageshift);
488 
489 		US_DEBUGP("Write %02X pages, to PBA %04X"
490 			" (LBA %04X) page %02X\n",
491 			pages, pba, lba, page);
492 
493 		command[4] = 0;
494 
495 		if (pba == NOT_ALLOCATED) {
496 			/* no pba allocated for this lba, find a free pba to use */
497 
498 			int max_pba = (info->max_log_blks / 250 ) * 256;
499 			int found_count = 0;
500 			int found_pba = -1;
501 
502 			/* set pba to first block in zone lba is in */
503 			pba = (lba / 1000) * 1024;
504 
505 			US_DEBUGP("No PBA for LBA %04X\n",lba);
506 
507 			if (max_pba > 1024)
508 				max_pba = 1024;
509 
510 			/* scan through the map lookiong for an unused block
511 			 * leave 16 unused blocks at start (or as many as possible)
512 			 * since the sddr55 seems to reuse a used block when it shouldn't
513 			 * if we don't leave space */
514 			for (i = 0; i < max_pba; i++, pba++) {
515 				if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
516 					found_pba = pba;
517 					if (found_count++ > 16)
518 						break;
519 				}
520 			}
521 
522 			pba = found_pba;
523 
524 			if (pba == -1) {
525 				/* oh dear, couldn't find an unallocated block */
526 				US_DEBUGP("Couldn't find unallocated block\n");
527 
528 				set_sense_info (3, 0x31, 0);	/* medium error */
529 
530 				if (use_sg)
531 					kfree(buffer);
532 
533 				return USB_STOR_TRANSPORT_FAILED;
534 			}
535 
536 			US_DEBUGP("Allocating PBA %04X for LBA %04X\n", pba, lba);
537 
538 			/* set writing to unallocated block flag */
539 			command[4] = 0x40;
540 		}
541 
542 		address = (pba << info->blockshift) + page;
543 
544 		command[1] = LSB_of(address>>16);
545 		command[2] = LSB_of(address>>8);
546 		command[3] = LSB_of(address);
547 
548 		/* set the lba into the command, modulo 1000 */
549 		command[0] = LSB_of(lba % 1000);
550 		command[6] = MSB_of(lba % 1000);
551 
552 		command[4] |= LSB_of(pages >> info->smallpageshift);
553 
554 		/* send command */
555 		result = sddr55_bulk_transport(us,
556 			SCSI_DATA_WRITE, command, 8);
557 
558 		if (result != US_BULK_TRANSFER_GOOD) {
559 			US_DEBUGP("Result for send_command in write_data %d\n",
560 			result);
561 
562 			set_sense_info (3, 0x3, 0);	/* peripheral write error */
563 
564 			if (use_sg)
565 				kfree(buffer);
566 			return result;
567 		}
568 
569 		/* send the data */
570 		result = sddr55_bulk_transport(us,
571 			SCSI_DATA_WRITE, ptr,
572 			pages<<info->pageshift);
573 
574 		if (result != US_BULK_TRANSFER_GOOD) {
575 			US_DEBUGP("Result for send_data in write_data %d\n",
576 			result);
577 
578 			set_sense_info (3, 0x3, 0);	/* peripheral write error */
579 
580 			if (use_sg)
581 				kfree(buffer);
582 			return result;
583 		}
584 
585 		/* now read status */
586 		result = sddr55_bulk_transport(us,
587 			SCSI_DATA_READ, status,	6);
588 
589 		if (result != US_BULK_TRANSFER_GOOD) {
590 			US_DEBUGP("Result for get_status in write_data %d\n",
591 			result);
592 
593 			set_sense_info (3, 0x3, 0);	/* peripheral write error */
594 
595 			if (use_sg)
596 				kfree(buffer);
597 			return result;
598 		}
599 
600 		new_pba = (status[3] + (status[4] << 8) + (status[5] << 16)) >> info->blockshift;
601 
602 		/* check status for error */
603 		if (status[0] == 0xff && status[1] == 0x4) {
604 			set_sense_info (3, 0x0c, 0);
605 			if (use_sg)
606 				kfree(buffer);
607 
608 			info->pba_to_lba[new_pba] = BAD_BLOCK;
609 
610 			return USB_STOR_TRANSPORT_FAILED;
611 		}
612 
613 		US_DEBUGP("Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
614 			lba, pba, new_pba);
615 
616 		/* update the lba<->pba maps, note new_pba might be the same as pba */
617 		info->lba_to_pba[lba] = new_pba;
618 		info->pba_to_lba[pba] = UNUSED_BLOCK;
619 
620 		/* check that new_pba wasn't already being used */
621 		if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
622 			printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
623 				new_pba, info->pba_to_lba[new_pba]);
624 			info->fatal_error = 1;
625 			set_sense_info (3, 0x31, 0);
626 			if (use_sg)
627 				kfree(buffer);
628 
629 			return USB_STOR_TRANSPORT_FAILED;
630 		}
631 
632 		/* update the pba<->lba maps for new_pba */
633 		info->pba_to_lba[new_pba] = lba % 1000;
634 
635 		page = 0;
636 		lba++;
637 		sectors -= pages >> info->smallpageshift;
638 		ptr += (pages << info->pageshift);
639 	}
640 
641 	if (use_sg) {
642 		kfree(buffer);
643 	}
644 
645 	return USB_STOR_TRANSPORT_GOOD;
646 }
647 
sddr55_read_deviceID(struct us_data * us,unsigned char * manufacturerID,unsigned char * deviceID)648 static int sddr55_read_deviceID(struct us_data *us,
649 		unsigned char *manufacturerID,
650 		unsigned char *deviceID) {
651 
652 	int result;
653 	unsigned char command[8] = {
654 		0, 0, 0, 0, 0, 0xb0, 0, 0x84
655 	};
656 	unsigned char content[64];
657 
658 	result = sddr55_bulk_transport(us, SCSI_DATA_WRITE, command, 8);
659 
660 	US_DEBUGP("Result of send_control for device ID is %d\n",
661 		result);
662 
663 	if (result != US_BULK_TRANSFER_GOOD)
664 		return result;
665 
666 	result = sddr55_bulk_transport(us,
667 		SCSI_DATA_READ, content, 4);
668 
669 	if (result != US_BULK_TRANSFER_GOOD)
670 		return result;
671 
672 	*manufacturerID = content[0];
673 	*deviceID = content[1];
674 
675 	if (content[0] != 0xff)	{
676     		result = sddr55_bulk_transport(us,
677 			SCSI_DATA_READ, content, 2);
678 	}
679 
680 	return result;
681 }
682 
683 
sddr55_reset(struct us_data * us)684 int sddr55_reset(struct us_data *us) {
685 	return 0;
686 }
687 
688 
sddr55_get_capacity(struct us_data * us)689 static unsigned long sddr55_get_capacity(struct us_data *us) {
690 
691 	unsigned char manufacturerID;
692 	unsigned char deviceID;
693 	int result;
694 	struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
695 
696 	US_DEBUGP("Reading capacity...\n");
697 
698 	result = sddr55_read_deviceID(us,
699 		&manufacturerID,
700 		&deviceID);
701 
702 	US_DEBUGP("Result of read_deviceID is %d\n",
703 		result);
704 
705 	if (result != US_BULK_TRANSFER_GOOD)
706 		return 0;
707 
708 	US_DEBUGP("Device ID = %02X\n", deviceID);
709 	US_DEBUGP("Manuf  ID = %02X\n", manufacturerID);
710 
711 	info->pageshift = 9;
712 	info->smallpageshift = 0;
713 	info->blocksize = 16;
714 	info->blockshift = 4;
715 	info->blockmask = 15;
716 
717 	switch (deviceID) {
718 
719 	case 0x6e: // 1MB
720 	case 0xe8:
721 	case 0xec:
722 		info->pageshift = 8;
723 		info->smallpageshift = 1;
724 		return 0x00100000;
725 
726 	case 0xea: // 2MB
727 	case 0x64:
728 		info->pageshift = 8;
729 		info->smallpageshift = 1;
730 	case 0x5d: // 5d is a ROM card with pagesize 512.
731 		return 0x00200000;
732 
733 	case 0xe3: // 4MB
734 	case 0xe5:
735 	case 0x6b:
736 	case 0xd5:
737 		return 0x00400000;
738 
739 	case 0xe6: // 8MB
740 	case 0xd6:
741 		return 0x00800000;
742 
743 	case 0x73: // 16MB
744 		info->blocksize = 32;
745 		info->blockshift = 5;
746 		info->blockmask = 31;
747 		return 0x01000000;
748 
749 	case 0x75: // 32MB
750 		info->blocksize = 32;
751 		info->blockshift = 5;
752 		info->blockmask = 31;
753 		return 0x02000000;
754 
755 	case 0x76: // 64MB
756 		info->blocksize = 32;
757 		info->blockshift = 5;
758 		info->blockmask = 31;
759 		return 0x04000000;
760 
761 	case 0x79: // 128MB
762 		info->blocksize = 32;
763 		info->blockshift = 5;
764 		info->blockmask = 31;
765 		return 0x08000000;
766 
767 	default: // unknown
768 		return 0;
769 
770 	}
771 }
772 
sddr55_read_map(struct us_data * us)773 static int sddr55_read_map(struct us_data *us) {
774 
775 	struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
776 	int numblocks;
777 	unsigned char *buffer;
778 	unsigned char command[8] = { 0, 0, 0, 0, 0, 0xb0, 0, 0x8a};
779 	int i;
780 	unsigned short lba;
781 	unsigned short max_lba;
782 	int result;
783 
784 	if (!info->capacity)
785 		return -1;
786 
787 	numblocks = info->capacity >> (info->blockshift + info->pageshift);
788 
789 	buffer = kmalloc( numblocks * 2, GFP_NOIO );
790 
791 	if (!buffer)
792 		return -1;
793 
794 	command[6] = numblocks * 2 / 256;
795 
796 	result = sddr55_bulk_transport(us, SCSI_DATA_WRITE, command, 8);
797 
798 	if ( result != US_BULK_TRANSFER_GOOD) {
799 		kfree (buffer);
800 		return -1;
801 	}
802 
803 	result = sddr55_bulk_transport(us, SCSI_DATA_READ, buffer, numblocks * 2);
804 
805 	if ( result != US_BULK_TRANSFER_GOOD) {
806 		kfree (buffer);
807 		return -1;
808 	}
809 
810 	result = sddr55_bulk_transport(us, SCSI_DATA_READ, command, 2);
811 
812 	if ( result != US_BULK_TRANSFER_GOOD) {
813 		kfree (buffer);
814 		return -1;
815 	}
816 
817 	if (info->lba_to_pba)
818 		kfree(info->lba_to_pba);
819 	if (info->pba_to_lba)
820 		kfree(info->pba_to_lba);
821 	info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
822 	info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
823 
824 	if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
825 		if (info->lba_to_pba != NULL)
826 			kfree(info->lba_to_pba);
827 		if (info->pba_to_lba != NULL)
828 			kfree(info->pba_to_lba);
829 		info->lba_to_pba = NULL;
830 		info->pba_to_lba = NULL;
831 		kfree(buffer);
832 		return -1;
833 	}
834 
835 	memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
836 	memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
837 
838 	/* set maximum lba */
839 	max_lba = info->max_log_blks;
840 	if (max_lba > 1000)
841 		max_lba = 1000;
842 
843 	// Each block is 64 bytes of control data, so block i is located in
844 	// scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
845 
846 	for (i=0; i<numblocks; i++) {
847 		int zone = i / 1024;
848 
849 		lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
850 
851 			/* Every 1024 physical blocks ("zone"), the LBA numbers
852 			 * go back to zero, but are within a higher
853 			 * block of LBA's. Also, there is a maximum of
854 			 * 1000 LBA's per zone. In other words, in PBA
855 			 * 1024-2047 you will find LBA 0-999 which are
856 			 * really LBA 1000-1999. Yes, this wastes 24
857 			 * physical blocks per zone. Go figure.
858 			 * These devices can have blocks go bad, so there
859 			 * are 24 spare blocks to use when blocks do go bad.
860 			 */
861 
862 			/* SDDR55 returns 0xffff for a bad block, and 0x400 for the
863 			 * CIS block. (Is this true for cards 8MB or less??)
864 			 * Record these in the physical to logical map
865 			 */
866 
867 		info->pba_to_lba[i] = lba;
868 
869 		if (lba >= max_lba) {
870 			continue;
871 		}
872 
873 		if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
874 		    !info->force_read_only) {
875 			printk("sddr55: map inconsistency at LBA %04X\n", lba + zone * 1000);
876 			info->force_read_only = 1;
877 		}
878 
879 		if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
880 			US_DEBUGP("LBA %04X <-> PBA %04X\n", lba, i);
881 
882 		info->lba_to_pba[lba + zone * 1000] = i;
883 	}
884 
885 	kfree(buffer);
886 	return 0;
887 }
888 
889 
sddr55_card_info_destructor(void * extra)890 static void sddr55_card_info_destructor(void *extra) {
891 	struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
892 
893 	if (!extra)
894 		return;
895 
896 	if (info->lba_to_pba)
897 		kfree(info->lba_to_pba);
898 	if (info->pba_to_lba)
899 		kfree(info->pba_to_lba);
900 }
901 
902 
903 /*
904  * Transport for the Sandisk SDDR-55
905  */
sddr55_transport(Scsi_Cmnd * srb,struct us_data * us)906 int sddr55_transport(Scsi_Cmnd *srb, struct us_data *us)
907 {
908 	int result;
909 	int i;
910 	unsigned char inquiry_response[36] = {
911 		0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
912 	};
913 	unsigned char mode_page_01[16] = { // write-protected for now
914 		0x03, 0x00, 0x80, 0x00,
915 		0x01, 0x0A,
916 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
917 	};
918 	unsigned char *ptr;
919 	unsigned long capacity;
920 	unsigned int lba;
921 	unsigned int pba;
922 	unsigned int page;
923 	unsigned short pages;
924 	struct sddr55_card_info *info;
925 
926 	if (!us->extra) {
927 		us->extra = kmalloc(
928 			sizeof(struct sddr55_card_info), GFP_NOIO);
929 		if (!us->extra)
930 			return USB_STOR_TRANSPORT_ERROR;
931 		memset(us->extra, 0, sizeof(struct sddr55_card_info));
932 		us->extra_destructor = sddr55_card_info_destructor;
933 	}
934 
935 	info = (struct sddr55_card_info *)(us->extra);
936 
937 	ptr = (unsigned char *)srb->request_buffer;
938 
939 	if (srb->cmnd[0] == REQUEST_SENSE) {
940 		i = srb->cmnd[4];
941 
942 		if (i > sizeof info->sense_data)
943 			i = sizeof info->sense_data;
944 
945 
946 		US_DEBUGP("SDDR55: request sense %02x/%02x/%02x\n", info->sense_data[2], info->sense_data[12], info->sense_data[13]);
947 
948 		info->sense_data[0] = 0x70;
949 		info->sense_data[7] = 10;
950 
951 		memcpy (ptr, info->sense_data, i);
952 		memset (info->sense_data, 0, sizeof info->sense_data);
953 
954 		return USB_STOR_TRANSPORT_GOOD;
955 	}
956 
957 	memset (info->sense_data, 0, sizeof info->sense_data);
958 
959 	/* Dummy up a response for INQUIRY since SDDR55 doesn't
960 	   respond to INQUIRY commands */
961 
962 	if (srb->cmnd[0] == INQUIRY) {
963 		memset(inquiry_response+8, 0, 28);
964 		fill_inquiry_response(us, inquiry_response, 36);
965 		return USB_STOR_TRANSPORT_GOOD;
966 	}
967 
968 	/* only check card status if the map isn't allocated, ie no card seen yet
969 	 * or if it's been over half a second since we last accessed it
970 	 */
971 	if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
972 
973 		/* check to see if a card is fitted */
974 		result = sddr55_status (us);
975 		if (result) {
976 			result = sddr55_status (us);
977 			if (!result) {
978 			set_sense_info (6, 0x28, 0);	/* new media, set unit attention, not ready to ready */
979 			}
980 			return USB_STOR_TRANSPORT_FAILED;
981 		}
982 	}
983 
984 	/* if we detected a problem with the map when writing, don't allow any more access */
985 	if (info->fatal_error) {
986 
987 		set_sense_info (3, 0x31, 0);
988 		return USB_STOR_TRANSPORT_FAILED;
989 	}
990 
991 	if (srb->cmnd[0] == READ_CAPACITY) {
992 
993 		capacity = sddr55_get_capacity(us);
994 
995 		if (!capacity) {
996 			set_sense_info (3, 0x30, 0);	/* incompatible medium */
997 			return USB_STOR_TRANSPORT_FAILED;
998 		}
999 
1000 		info->capacity = capacity;
1001 
1002                 /* figure out the maximum logical block number, allowing for the fact
1003                  * that only 250 out of every 256 are used */
1004 		info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
1005 
1006 		/* Last page in the card, adjust as we only use 250 out of every 256 pages */
1007 		capacity = (capacity / 256) * 250;
1008 
1009 		capacity /= PAGESIZE;
1010 		capacity--;
1011 
1012 		ptr[0] = MSB_of(capacity>>16);
1013 		ptr[1] = LSB_of(capacity>>16);
1014 		ptr[2] = MSB_of(capacity&0xFFFF);
1015 		ptr[3] = LSB_of(capacity&0xFFFF);
1016 
1017 		// The page size
1018 
1019 		ptr[4] = MSB_of(PAGESIZE>>16);
1020 		ptr[5] = LSB_of(PAGESIZE>>16);
1021 		ptr[6] = MSB_of(PAGESIZE&0xFFFF);
1022 		ptr[7] = LSB_of(PAGESIZE&0xFFFF);
1023 
1024 		sddr55_read_map(us);
1025 
1026 		return USB_STOR_TRANSPORT_GOOD;
1027 	}
1028 
1029 	if (srb->cmnd[0] == MODE_SENSE) {
1030 
1031 		mode_page_01[2] = (info->read_only || info->force_read_only) ? 0x80 : 0;
1032 
1033 		if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
1034 
1035 			US_DEBUGP(
1036 			  "SDDR55: Dummy up request for mode page 1\n");
1037 
1038 			if (ptr==NULL ||
1039 			  srb->request_bufflen<sizeof(mode_page_01)) {
1040 				set_sense_info (5, 0x24, 0);	/* invalid field in command */
1041 				return USB_STOR_TRANSPORT_FAILED;
1042 			}
1043 
1044 			memcpy(ptr, mode_page_01, sizeof(mode_page_01));
1045 			return USB_STOR_TRANSPORT_GOOD;
1046 
1047 		} else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
1048 
1049 			US_DEBUGP(
1050 			  "SDDR55: Dummy up request for all mode pages\n");
1051 
1052 			if (ptr==NULL ||
1053 			  srb->request_bufflen<sizeof(mode_page_01)) {
1054 				set_sense_info (5, 0x24, 0);	/* invalid field in command */
1055 				return USB_STOR_TRANSPORT_FAILED;
1056 			}
1057 
1058 			memcpy(ptr, mode_page_01, sizeof(mode_page_01));
1059 			return USB_STOR_TRANSPORT_GOOD;
1060 		}
1061 
1062 		set_sense_info (5, 0x24, 0);	/* invalid field in command */
1063 
1064 		return USB_STOR_TRANSPORT_FAILED;
1065 	}
1066 
1067 	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1068 
1069 		US_DEBUGP(
1070 		  "SDDR55: %s medium removal. Not that I can do"
1071 		  " anything about it...\n",
1072 		  (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
1073 
1074 		return USB_STOR_TRANSPORT_GOOD;
1075 
1076 	}
1077 
1078 	if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
1079 
1080 		page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1081 		page <<= 16;
1082 		page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1083 		pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1084 
1085 		page <<= info->smallpageshift;
1086 
1087 		// convert page to block and page-within-block
1088 
1089 		lba = page >> info->blockshift;
1090 		page = page & info->blockmask;
1091 
1092 		// locate physical block corresponding to logical block
1093 
1094 		if (lba >= info->max_log_blks) {
1095 
1096 			US_DEBUGP("Error: Requested LBA %04X exceeds maximum "
1097 			  "block %04X\n", lba, info->max_log_blks-1);
1098 
1099 			set_sense_info (5, 0x24, 0);	/* invalid field in command */
1100 
1101 			return USB_STOR_TRANSPORT_FAILED;
1102 		}
1103 
1104 		pba = info->lba_to_pba[lba];
1105 
1106 		if (srb->cmnd[0] == WRITE_10) {
1107 			US_DEBUGP("WRITE_10: write block %04X (LBA %04X) page %01X"
1108 			        " pages %d\n",
1109 			        pba, lba, page, pages);
1110 
1111 			return sddr55_write_data(us, lba, page, pages, ptr, srb->use_sg);
1112 		} else {
1113 			US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X"
1114 			        " pages %d\n",
1115 			        pba, lba, page, pages);
1116 
1117 			return sddr55_read_data(us, lba, page, pages, ptr, srb->use_sg);
1118 		}
1119 	}
1120 
1121 
1122 	if (srb->cmnd[0] == TEST_UNIT_READY) {
1123 		return USB_STOR_TRANSPORT_GOOD;
1124 	}
1125 
1126 	if (srb->cmnd[0] == START_STOP) {
1127 		return USB_STOR_TRANSPORT_GOOD;
1128 	}
1129 
1130 	set_sense_info (5, 0x20, 0);	/* illegal command */
1131 
1132 	return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
1133 }
1134 
1135