-
Notifications
You must be signed in to change notification settings - Fork 54k
/
sddr09.c
1790 lines (1486 loc) · 44.1 KB
/
sddr09.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0+
/*
* Driver for SanDisk SDDR-09 SmartMedia reader
*
* (c) 2000, 2001 Robert Baruch (autophile@starband.net)
* (c) 2002 Andries Brouwer (aeb@cwi.nl)
* Developed with the assistance of:
* (c) 2002 Alan Stern <stern@rowland.org>
*
* The SanDisk SDDR-09 SmartMedia reader uses the Shuttle EUSB-01 chip.
* This chip is a programmable USB controller. In the SDDR-09, it has
* been programmed to obey a certain limited set of SCSI commands.
* This driver translates the "real" SCSI commands to the SDDR-09 SCSI
* commands.
*/
/*
* Known vendor commands: 12 bytes, first byte is opcode
*
* E7: read scatter gather
* E8: read
* E9: write
* EA: erase
* EB: reset
* EC: read status
* ED: read ID
* EE: write CIS (?)
* EF: compute checksum (?)
*/
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include "usb.h"
#include "transport.h"
#include "protocol.h"
#include "debug.h"
#include "scsiglue.h"
#define DRV_NAME "ums-sddr09"
MODULE_DESCRIPTION("Driver for SanDisk SDDR-09 SmartMedia reader");
MODULE_AUTHOR("Andries Brouwer <aeb@cwi.nl>, Robert Baruch <autophile@starband.net>");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS(USB_STORAGE);
static int usb_stor_sddr09_dpcm_init(struct us_data *us);
static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us);
static int usb_stor_sddr09_init(struct us_data *us);
/*
* The table of devices
*/
#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
vendorName, productName, useProtocol, useTransport, \
initFunction, flags) \
{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
.driver_info = (flags) }
static const struct usb_device_id sddr09_usb_ids[] = {
# include "unusual_sddr09.h"
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, sddr09_usb_ids);
#undef UNUSUAL_DEV
/*
* The flags table
*/
#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
vendor_name, product_name, use_protocol, use_transport, \
init_function, Flags) \
{ \
.vendorName = vendor_name, \
.productName = product_name, \
.useProtocol = use_protocol, \
.useTransport = use_transport, \
.initFunction = init_function, \
}
static const struct us_unusual_dev sddr09_unusual_dev_list[] = {
# include "unusual_sddr09.h"
{ } /* Terminating entry */
};
#undef UNUSUAL_DEV
#define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
#define LSB_of(s) ((s)&0xFF)
#define MSB_of(s) ((s)>>8)
/*
* First some stuff that does not belong here:
* data on SmartMedia and other cards, completely
* unrelated to this driver.
* Similar stuff occurs in <linux/mtd/nand_ids.h>.
*/
struct nand_flash_dev {
int model_id;
int chipshift; /* 1<<cs bytes total capacity */
char pageshift; /* 1<<ps bytes in a page */
char blockshift; /* 1<<bs pages in an erase block */
char zoneshift; /* 1<<zs blocks in a zone */
/* # of logical blocks is 125/128 of this */
char pageadrlen; /* length of an address in bytes - 1 */
};
/*
* NAND Flash Manufacturer ID Codes
*/
#define NAND_MFR_AMD 0x01
#define NAND_MFR_NATSEMI 0x8f
#define NAND_MFR_TOSHIBA 0x98
#define NAND_MFR_SAMSUNG 0xec
static inline char *nand_flash_manufacturer(int manuf_id) {
switch(manuf_id) {
case NAND_MFR_AMD:
return "AMD";
case NAND_MFR_NATSEMI:
return "NATSEMI";
case NAND_MFR_TOSHIBA:
return "Toshiba";
case NAND_MFR_SAMSUNG:
return "Samsung";
default:
return "unknown";
}
}
/*
* It looks like it is unnecessary to attach manufacturer to the
* remaining data: SSFDC prescribes manufacturer-independent id codes.
*
* 256 MB NAND flash has a 5-byte ID with 2nd byte 0xaa, 0xba, 0xca or 0xda.
*/
static struct nand_flash_dev nand_flash_ids[] = {
/* NAND flash */
{ 0x6e, 20, 8, 4, 8, 2}, /* 1 MB */
{ 0xe8, 20, 8, 4, 8, 2}, /* 1 MB */
{ 0xec, 20, 8, 4, 8, 2}, /* 1 MB */
{ 0x64, 21, 8, 4, 9, 2}, /* 2 MB */
{ 0xea, 21, 8, 4, 9, 2}, /* 2 MB */
{ 0x6b, 22, 9, 4, 9, 2}, /* 4 MB */
{ 0xe3, 22, 9, 4, 9, 2}, /* 4 MB */
{ 0xe5, 22, 9, 4, 9, 2}, /* 4 MB */
{ 0xe6, 23, 9, 4, 10, 2}, /* 8 MB */
{ 0x73, 24, 9, 5, 10, 2}, /* 16 MB */
{ 0x75, 25, 9, 5, 10, 2}, /* 32 MB */
{ 0x76, 26, 9, 5, 10, 3}, /* 64 MB */
{ 0x79, 27, 9, 5, 10, 3}, /* 128 MB */
/* MASK ROM */
{ 0x5d, 21, 9, 4, 8, 2}, /* 2 MB */
{ 0xd5, 22, 9, 4, 9, 2}, /* 4 MB */
{ 0xd6, 23, 9, 4, 10, 2}, /* 8 MB */
{ 0x57, 24, 9, 4, 11, 2}, /* 16 MB */
{ 0x58, 25, 9, 4, 12, 2}, /* 32 MB */
{ 0,}
};
static struct nand_flash_dev *
nand_find_id(unsigned char id) {
int i;
for (i = 0; i < ARRAY_SIZE(nand_flash_ids); i++)
if (nand_flash_ids[i].model_id == id)
return &(nand_flash_ids[i]);
return NULL;
}
/*
* ECC computation.
*/
static unsigned char parity[256];
static unsigned char ecc2[256];
static void nand_init_ecc(void) {
int i, j, a;
parity[0] = 0;
for (i = 1; i < 256; i++)
parity[i] = (parity[i&(i-1)] ^ 1);
for (i = 0; i < 256; i++) {
a = 0;
for (j = 0; j < 8; j++) {
if (i & (1<<j)) {
if ((j & 1) == 0)
a ^= 0x04;
if ((j & 2) == 0)
a ^= 0x10;
if ((j & 4) == 0)
a ^= 0x40;
}
}
ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
}
}
/* compute 3-byte ecc on 256 bytes */
static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
int i, j, a;
unsigned char par = 0, bit, bits[8] = {0};
/* collect 16 checksum bits */
for (i = 0; i < 256; i++) {
par ^= data[i];
bit = parity[data[i]];
for (j = 0; j < 8; j++)
if ((i & (1<<j)) == 0)
bits[j] ^= bit;
}
/* put 4+4+4 = 12 bits in the ecc */
a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
ecc[2] = ecc2[par];
}
static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
}
static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
memcpy(data, ecc, 3);
}
/*
* The actual driver starts here.
*/
struct sddr09_card_info {
unsigned long capacity; /* Size of card in bytes */
int pagesize; /* Size of page in bytes */
int pageshift; /* log2 of pagesize */
int blocksize; /* Size of block in pages */
int blockshift; /* log2 of blocksize */
int blockmask; /* 2^blockshift - 1 */
int *lba_to_pba; /* logical to physical map */
int *pba_to_lba; /* physical to logical map */
int lbact; /* number of available pages */
int flags;
#define SDDR09_WP 1 /* write protected */
};
/*
* On my 16MB card, control blocks have size 64 (16 real control bytes,
* and 48 junk bytes). In reality of course the card uses 16 control bytes,
* so the reader makes up the remaining 48. Don't know whether these numbers
* depend on the card. For now a constant.
*/
#define CONTROL_SHIFT 6
/*
* On my Combo CF/SM reader, the SM reader has LUN 1.
* (and things fail with LUN 0).
* It seems LUN is irrelevant for others.
*/
#define LUN 1
#define LUNBITS (LUN << 5)
/*
* LBA and PBA are unsigned ints. Special values.
*/
#define UNDEF 0xffffffff
#define SPARE 0xfffffffe
#define UNUSABLE 0xfffffffd
static const int erase_bad_lba_entries = 0;
/* send vendor interface command (0x41) */
/* called for requests 0, 1, 8 */
static int
sddr09_send_command(struct us_data *us,
unsigned char request,
unsigned char direction,
unsigned char *xfer_data,
unsigned int xfer_len) {
unsigned int pipe;
unsigned char requesttype = (0x41 | direction);
int rc;
// Get the receive or send control pipe number
if (direction == USB_DIR_IN)
pipe = us->recv_ctrl_pipe;
else
pipe = us->send_ctrl_pipe;
rc = usb_stor_ctrl_transfer(us, pipe, request, requesttype,
0, 0, xfer_data, xfer_len);
switch (rc) {
case USB_STOR_XFER_GOOD: return 0;
case USB_STOR_XFER_STALLED: return -EPIPE;
default: return -EIO;
}
}
static int
sddr09_send_scsi_command(struct us_data *us,
unsigned char *command,
unsigned int command_len) {
return sddr09_send_command(us, 0, USB_DIR_OUT, command, command_len);
}
#if 0
/*
* Test Unit Ready Command: 12 bytes.
* byte 0: opcode: 00
*/
static int
sddr09_test_unit_ready(struct us_data *us) {
unsigned char *command = us->iobuf;
int result;
memset(command, 0, 6);
command[1] = LUNBITS;
result = sddr09_send_scsi_command(us, command, 6);
usb_stor_dbg(us, "sddr09_test_unit_ready returns %d\n", result);
return result;
}
#endif
/*
* Request Sense Command: 12 bytes.
* byte 0: opcode: 03
* byte 4: data length
*/
static int
sddr09_request_sense(struct us_data *us, unsigned char *sensebuf, int buflen) {
unsigned char *command = us->iobuf;
int result;
memset(command, 0, 12);
command[0] = 0x03;
command[1] = LUNBITS;
command[4] = buflen;
result = sddr09_send_scsi_command(us, command, 12);
if (result)
return result;
result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
sensebuf, buflen, NULL);
return (result == USB_STOR_XFER_GOOD ? 0 : -EIO);
}
/*
* Read Command: 12 bytes.
* byte 0: opcode: E8
* byte 1: last two bits: 00: read data, 01: read blockwise control,
* 10: read both, 11: read pagewise control.
* It turns out we need values 20, 21, 22, 23 here (LUN 1).
* bytes 2-5: address (interpretation depends on byte 1, see below)
* bytes 10-11: count (idem)
*
* A page has 512 data bytes and 64 control bytes (16 control and 48 junk).
* A read data command gets data in 512-byte pages.
* A read control command gets control in 64-byte chunks.
* A read both command gets data+control in 576-byte chunks.
*
* Blocks are groups of 32 pages, and read blockwise control jumps to the
* next block, while read pagewise control jumps to the next page after
* reading a group of 64 control bytes.
* [Here 512 = 1<<pageshift, 32 = 1<<blockshift, 64 is constant?]
*
* (1 MB and 2 MB cards are a bit different, but I have only a 16 MB card.)
*/
static int
sddr09_readX(struct us_data *us, int x, unsigned long fromaddress,
int nr_of_pages, int bulklen, unsigned char *buf,
int use_sg) {
unsigned char *command = us->iobuf;
int result;
command[0] = 0xE8;
command[1] = LUNBITS | x;
command[2] = MSB_of(fromaddress>>16);
command[3] = LSB_of(fromaddress>>16);
command[4] = MSB_of(fromaddress & 0xFFFF);
command[5] = LSB_of(fromaddress & 0xFFFF);
command[6] = 0;
command[7] = 0;
command[8] = 0;
command[9] = 0;
command[10] = MSB_of(nr_of_pages);
command[11] = LSB_of(nr_of_pages);
result = sddr09_send_scsi_command(us, command, 12);
if (result) {
usb_stor_dbg(us, "Result for send_control in sddr09_read2%d %d\n",
x, result);
return result;
}
result = usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe,
buf, bulklen, use_sg, NULL);
if (result != USB_STOR_XFER_GOOD) {
usb_stor_dbg(us, "Result for bulk_transfer in sddr09_read2%d %d\n",
x, result);
return -EIO;
}
return 0;
}
/*
* Read Data
*
* fromaddress counts data shorts:
* increasing it by 256 shifts the bytestream by 512 bytes;
* the last 8 bits are ignored.
*
* nr_of_pages counts pages of size (1 << pageshift).
*/
static int
sddr09_read20(struct us_data *us, unsigned long fromaddress,
int nr_of_pages, int pageshift, unsigned char *buf, int use_sg) {
int bulklen = nr_of_pages << pageshift;
/* The last 8 bits of fromaddress are ignored. */
return sddr09_readX(us, 0, fromaddress, nr_of_pages, bulklen,
buf, use_sg);
}
/*
* Read Blockwise Control
*
* fromaddress gives the starting position (as in read data;
* the last 8 bits are ignored); increasing it by 32*256 shifts
* the output stream by 64 bytes.
*
* count counts control groups of size (1 << controlshift).
* For me, controlshift = 6. Is this constant?
*
* After getting one control group, jump to the next block
* (fromaddress += 8192).
*/
static int
sddr09_read21(struct us_data *us, unsigned long fromaddress,
int count, int controlshift, unsigned char *buf, int use_sg) {
int bulklen = (count << controlshift);
return sddr09_readX(us, 1, fromaddress, count, bulklen,
buf, use_sg);
}
/*
* Read both Data and Control
*
* fromaddress counts data shorts, ignoring control:
* increasing it by 256 shifts the bytestream by 576 = 512+64 bytes;
* the last 8 bits are ignored.
*
* nr_of_pages counts pages of size (1 << pageshift) + (1 << controlshift).
*/
static int
sddr09_read22(struct us_data *us, unsigned long fromaddress,
int nr_of_pages, int pageshift, unsigned char *buf, int use_sg) {
int bulklen = (nr_of_pages << pageshift) + (nr_of_pages << CONTROL_SHIFT);
usb_stor_dbg(us, "reading %d pages, %d bytes\n", nr_of_pages, bulklen);
return sddr09_readX(us, 2, fromaddress, nr_of_pages, bulklen,
buf, use_sg);
}
#if 0
/*
* Read Pagewise Control
*
* fromaddress gives the starting position (as in read data;
* the last 8 bits are ignored); increasing it by 256 shifts
* the output stream by 64 bytes.
*
* count counts control groups of size (1 << controlshift).
* For me, controlshift = 6. Is this constant?
*
* After getting one control group, jump to the next page
* (fromaddress += 256).
*/
static int
sddr09_read23(struct us_data *us, unsigned long fromaddress,
int count, int controlshift, unsigned char *buf, int use_sg) {
int bulklen = (count << controlshift);
return sddr09_readX(us, 3, fromaddress, count, bulklen,
buf, use_sg);
}
#endif
/*
* Erase Command: 12 bytes.
* byte 0: opcode: EA
* bytes 6-9: erase address (big-endian, counting shorts, sector aligned).
*
* Always precisely one block is erased; bytes 2-5 and 10-11 are ignored.
* The byte address being erased is 2*Eaddress.
* The CIS cannot be erased.
*/
static int
sddr09_erase(struct us_data *us, unsigned long Eaddress) {
unsigned char *command = us->iobuf;
int result;
usb_stor_dbg(us, "erase address %lu\n", Eaddress);
memset(command, 0, 12);
command[0] = 0xEA;
command[1] = LUNBITS;
command[6] = MSB_of(Eaddress>>16);
command[7] = LSB_of(Eaddress>>16);
command[8] = MSB_of(Eaddress & 0xFFFF);
command[9] = LSB_of(Eaddress & 0xFFFF);
result = sddr09_send_scsi_command(us, command, 12);
if (result)
usb_stor_dbg(us, "Result for send_control in sddr09_erase %d\n",
result);
return result;
}
/*
* Write CIS Command: 12 bytes.
* byte 0: opcode: EE
* bytes 2-5: write address in shorts
* bytes 10-11: sector count
*
* This writes at the indicated address. Don't know how it differs
* from E9. Maybe it does not erase? However, it will also write to
* the CIS.
*
* When two such commands on the same page follow each other directly,
* the second one is not done.
*/
/*
* Write Command: 12 bytes.
* byte 0: opcode: E9
* bytes 2-5: write address (big-endian, counting shorts, sector aligned).
* bytes 6-9: erase address (big-endian, counting shorts, sector aligned).
* bytes 10-11: sector count (big-endian, in 512-byte sectors).
*
* If write address equals erase address, the erase is done first,
* otherwise the write is done first. When erase address equals zero
* no erase is done?
*/
static int
sddr09_writeX(struct us_data *us,
unsigned long Waddress, unsigned long Eaddress,
int nr_of_pages, int bulklen, unsigned char *buf, int use_sg) {
unsigned char *command = us->iobuf;
int result;
command[0] = 0xE9;
command[1] = LUNBITS;
command[2] = MSB_of(Waddress>>16);
command[3] = LSB_of(Waddress>>16);
command[4] = MSB_of(Waddress & 0xFFFF);
command[5] = LSB_of(Waddress & 0xFFFF);
command[6] = MSB_of(Eaddress>>16);
command[7] = LSB_of(Eaddress>>16);
command[8] = MSB_of(Eaddress & 0xFFFF);
command[9] = LSB_of(Eaddress & 0xFFFF);
command[10] = MSB_of(nr_of_pages);
command[11] = LSB_of(nr_of_pages);
result = sddr09_send_scsi_command(us, command, 12);
if (result) {
usb_stor_dbg(us, "Result for send_control in sddr09_writeX %d\n",
result);
return result;
}
result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe,
buf, bulklen, use_sg, NULL);
if (result != USB_STOR_XFER_GOOD) {
usb_stor_dbg(us, "Result for bulk_transfer in sddr09_writeX %d\n",
result);
return -EIO;
}
return 0;
}
/* erase address, write same address */
static int
sddr09_write_inplace(struct us_data *us, unsigned long address,
int nr_of_pages, int pageshift, unsigned char *buf,
int use_sg) {
int bulklen = (nr_of_pages << pageshift) + (nr_of_pages << CONTROL_SHIFT);
return sddr09_writeX(us, address, address, nr_of_pages, bulklen,
buf, use_sg);
}
#if 0
/*
* Read Scatter Gather Command: 3+4n bytes.
* byte 0: opcode E7
* byte 2: n
* bytes 4i-1,4i,4i+1: page address
* byte 4i+2: page count
* (i=1..n)
*
* This reads several pages from the card to a single memory buffer.
* The last two bits of byte 1 have the same meaning as for E8.
*/
static int
sddr09_read_sg_test_only(struct us_data *us) {
unsigned char *command = us->iobuf;
int result, bulklen, nsg, ct;
unsigned char *buf;
unsigned long address;
nsg = bulklen = 0;
command[0] = 0xE7;
command[1] = LUNBITS;
command[2] = 0;
address = 040000; ct = 1;
nsg++;
bulklen += (ct << 9);
command[4*nsg+2] = ct;
command[4*nsg+1] = ((address >> 9) & 0xFF);
command[4*nsg+0] = ((address >> 17) & 0xFF);
command[4*nsg-1] = ((address >> 25) & 0xFF);
address = 0340000; ct = 1;
nsg++;
bulklen += (ct << 9);
command[4*nsg+2] = ct;
command[4*nsg+1] = ((address >> 9) & 0xFF);
command[4*nsg+0] = ((address >> 17) & 0xFF);
command[4*nsg-1] = ((address >> 25) & 0xFF);
address = 01000000; ct = 2;
nsg++;
bulklen += (ct << 9);
command[4*nsg+2] = ct;
command[4*nsg+1] = ((address >> 9) & 0xFF);
command[4*nsg+0] = ((address >> 17) & 0xFF);
command[4*nsg-1] = ((address >> 25) & 0xFF);
command[2] = nsg;
result = sddr09_send_scsi_command(us, command, 4*nsg+3);
if (result) {
usb_stor_dbg(us, "Result for send_control in sddr09_read_sg %d\n",
result);
return result;
}
buf = kmalloc(bulklen, GFP_NOIO);
if (!buf)
return -ENOMEM;
result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
buf, bulklen, NULL);
kfree(buf);
if (result != USB_STOR_XFER_GOOD) {
usb_stor_dbg(us, "Result for bulk_transfer in sddr09_read_sg %d\n",
result);
return -EIO;
}
return 0;
}
#endif
/*
* Read Status Command: 12 bytes.
* byte 0: opcode: EC
*
* Returns 64 bytes, all zero except for the first.
* bit 0: 1: Error
* bit 5: 1: Suspended
* bit 6: 1: Ready
* bit 7: 1: Not write-protected
*/
static int
sddr09_read_status(struct us_data *us, unsigned char *status) {
unsigned char *command = us->iobuf;
unsigned char *data = us->iobuf;
int result;
usb_stor_dbg(us, "Reading status...\n");
memset(command, 0, 12);
command[0] = 0xEC;
command[1] = LUNBITS;
result = sddr09_send_scsi_command(us, command, 12);
if (result)
return result;
result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
data, 64, NULL);
*status = data[0];
return (result == USB_STOR_XFER_GOOD ? 0 : -EIO);
}
static int
sddr09_read_data(struct us_data *us,
unsigned long address,
unsigned int sectors) {
struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
unsigned char *buffer;
unsigned int lba, maxlba, pba;
unsigned int page, pages;
unsigned int len, offset;
struct scatterlist *sg;
int result;
// Figure out the initial LBA and page
lba = address >> info->blockshift;
page = (address & info->blockmask);
maxlba = info->capacity >> (info->pageshift + info->blockshift);
if (lba >= maxlba)
return -EIO;
// Since we only read in one block at a time, we have to create
// a bounce buffer and move the data a piece at a time between the
// bounce buffer and the actual transfer buffer.
len = min(sectors, (unsigned int) info->blocksize) * info->pagesize;
buffer = kmalloc(len, GFP_NOIO);
if (!buffer)
return -ENOMEM;
// This could be made much more efficient by checking for
// contiguous LBA's. Another exercise left to the student.
result = 0;
offset = 0;
sg = NULL;
while (sectors > 0) {
/* Find number of pages we can read in this block */
pages = min(sectors, info->blocksize - page);
len = pages << info->pageshift;
/* Not overflowing capacity? */
if (lba >= maxlba) {
usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
lba, maxlba);
result = -EIO;
break;
}
/* Find where this lba lives on disk */
pba = info->lba_to_pba[lba];
if (pba == UNDEF) { /* this lba was never written */
usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
pages, lba, page);
/*
* This is not really an error. It just means
* that the block has never been written.
* Instead of returning an error
* it is better to return all zero data.
*/
memset(buffer, 0, len);
} else {
usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
pages, pba, lba, page);
address = ((pba << info->blockshift) + page) <<
info->pageshift;
result = sddr09_read20(us, address>>1,
pages, info->pageshift, buffer, 0);
if (result)
break;
}
// Store the data in the transfer buffer
usb_stor_access_xfer_buf(buffer, len, us->srb,
&sg, &offset, TO_XFER_BUF);
page = 0;
lba++;
sectors -= pages;
}
kfree(buffer);
return result;
}
static unsigned int
sddr09_find_unused_pba(struct sddr09_card_info *info, unsigned int lba) {
static unsigned int lastpba = 1;
int zonestart, end, i;
zonestart = (lba/1000) << 10;
end = info->capacity >> (info->blockshift + info->pageshift);
end -= zonestart;
if (end > 1024)
end = 1024;
for (i = lastpba+1; i < end; i++) {
if (info->pba_to_lba[zonestart+i] == UNDEF) {
lastpba = i;
return zonestart+i;
}
}
for (i = 0; i <= lastpba; i++) {
if (info->pba_to_lba[zonestart+i] == UNDEF) {
lastpba = i;
return zonestart+i;
}
}
return 0;
}
static int
sddr09_write_lba(struct us_data *us, unsigned int lba,
unsigned int page, unsigned int pages,
unsigned char *ptr, unsigned char *blockbuffer) {
struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
unsigned long address;
unsigned int pba, lbap;
unsigned int pagelen;
unsigned char *bptr, *cptr, *xptr;
unsigned char ecc[3];
int i, result;
lbap = ((lba % 1000) << 1) | 0x1000;
if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
lbap ^= 1;
pba = info->lba_to_pba[lba];
if (pba == UNDEF) {
pba = sddr09_find_unused_pba(info, lba);
if (!pba) {
printk(KERN_WARNING
"sddr09_write_lba: Out of unused blocks\n");
return -ENOSPC;
}
info->pba_to_lba[pba] = lba;
info->lba_to_pba[lba] = pba;
}
if (pba == 1) {
/*
* Maybe it is impossible to write to PBA 1.
* Fake success, but don't do anything.
*/
printk(KERN_WARNING "sddr09: avoid writing to pba 1\n");
return 0;
}
pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT);
/* read old contents */
address = (pba << (info->pageshift + info->blockshift));
result = sddr09_read22(us, address>>1, info->blocksize,
info->pageshift, blockbuffer, 0);
if (result)
return result;
/* check old contents and fill lba */
for (i = 0; i < info->blocksize; i++) {
bptr = blockbuffer + i*pagelen;
cptr = bptr + info->pagesize;
nand_compute_ecc(bptr, ecc);
if (!nand_compare_ecc(cptr+13, ecc)) {
usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
i, pba);
nand_store_ecc(cptr+13, ecc);
}
nand_compute_ecc(bptr+(info->pagesize / 2), ecc);
if (!nand_compare_ecc(cptr+8, ecc)) {
usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
i, pba);
nand_store_ecc(cptr+8, ecc);
}
cptr[6] = cptr[11] = MSB_of(lbap);
cptr[7] = cptr[12] = LSB_of(lbap);
}
/* copy in new stuff and compute ECC */
xptr = ptr;
for (i = page; i < page+pages; i++) {
bptr = blockbuffer + i*pagelen;
cptr = bptr + info->pagesize;
memcpy(bptr, xptr, info->pagesize);
xptr += info->pagesize;
nand_compute_ecc(bptr, ecc);
nand_store_ecc(cptr+13, ecc);
nand_compute_ecc(bptr+(info->pagesize / 2), ecc);
nand_store_ecc(cptr+8, ecc);
}
usb_stor_dbg(us, "Rewrite PBA %d (LBA %d)\n", pba, lba);
result = sddr09_write_inplace(us, address>>1, info->blocksize,
info->pageshift, blockbuffer, 0);
usb_stor_dbg(us, "sddr09_write_inplace returns %d\n", result);
#if 0
{
unsigned char status = 0;
int result2 = sddr09_read_status(us, &status);
if (result2)
usb_stor_dbg(us, "cannot read status\n");
else if (status != 0xc0)
usb_stor_dbg(us, "status after write: 0x%x\n", status);
}
#endif
#if 0
{
int result2 = sddr09_test_unit_ready(us);
}
#endif
return result;
}
static int
sddr09_write_data(struct us_data *us,
unsigned long address,
unsigned int sectors) {
struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
unsigned int lba, maxlba, page, pages;
unsigned int pagelen, blocklen;
unsigned char *blockbuffer;
unsigned char *buffer;
unsigned int len, offset;
struct scatterlist *sg;
int result;
/* Figure out the initial LBA and page */
lba = address >> info->blockshift;
page = (address & info->blockmask);
maxlba = info->capacity >> (info->pageshift + info->blockshift);
if (lba >= maxlba)
return -EIO;
/*
* blockbuffer is used for reading in the old data, overwriting
* with the new data, and performing ECC calculations
*/
/*
* TODO: instead of doing kmalloc/kfree for each write,
* add a bufferpointer to the info structure
*/
pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT);
blocklen = (pagelen << info->blockshift);
blockbuffer = kmalloc(blocklen, GFP_NOIO);
if (!blockbuffer)
return -ENOMEM;
/*
* Since we don't write the user data directly to the device,
* we have to create a bounce buffer and move the data a piece
* at a time between the bounce buffer and the actual transfer buffer.
*/
len = min(sectors, (unsigned int) info->blocksize) * info->pagesize;