d0f93ba7f52d0770a1bb7cf89c7f938830254765
[SCSI2SD.git] / software / SCSI2SD / src / disk.c
1 //      Copyright (C) 2013 Michael McMaster <michael@codesrc.com>\r
2 //      Copyright (C) 2014 Doug Brown <doug@downtowndougbrown.com>\r
3 //\r
4 //      This file is part of SCSI2SD.\r
5 //\r
6 //      SCSI2SD is free software: you can redistribute it and/or modify\r
7 //      it under the terms of the GNU General Public License as published by\r
8 //      the Free Software Foundation, either version 3 of the License, or\r
9 //      (at your option) any later version.\r
10 //\r
11 //      SCSI2SD is distributed in the hope that it will be useful,\r
12 //      but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 //      GNU General Public License for more details.\r
15 //\r
16 //      You should have received a copy of the GNU General Public License\r
17 //      along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.\r
18 \r
19 #include "device.h"\r
20 #include "scsi.h"\r
21 #include "scsiPhy.h"\r
22 #include "config.h"\r
23 #include "disk.h"\r
24 #include "sd.h"\r
25 \r
26 #include <string.h>\r
27 \r
28 // Global\r
29 BlockDevice blockDev;\r
30 Transfer transfer;\r
31 \r
32 static int doSdInit()\r
33 {\r
34         int result = sdInit();\r
35         if (result)\r
36         {\r
37                 blockDev.state = blockDev.state | DISK_INITIALISED;\r
38         }\r
39         return result;\r
40 }\r
41 \r
42 // Callback once all data has been read in the data out phase.\r
43 static void doFormatUnitComplete(void)\r
44 {\r
45         // TODO start writing the initialisation pattern to the SD\r
46         // card\r
47         scsiDev.phase = STATUS;\r
48 }\r
49 \r
50 static void doFormatUnitSkipData(int bytes)\r
51 {\r
52         // We may not have enough memory to store the initialisation pattern and\r
53         // defect list data.  Since we're not making use of it yet anyway, just\r
54         // discard the bytes.\r
55         scsiEnterPhase(DATA_OUT);\r
56         int i;\r
57         for (i = 0; i < bytes; ++i)\r
58         {\r
59                 scsiReadByte(); \r
60         }\r
61 }\r
62 \r
63 // Callback from the data out phase.\r
64 static void doFormatUnitPatternHeader(void)\r
65 {\r
66         int defectLength =\r
67                 ((((uint16_t)scsiDev.data[2])) << 8) +\r
68                         scsiDev.data[3];\r
69 \r
70         int patternLength =\r
71                 ((((uint16_t)scsiDev.data[4 + 2])) << 8) +\r
72                 scsiDev.data[4 + 3];\r
73 \r
74                 doFormatUnitSkipData(defectLength + patternLength);\r
75                 doFormatUnitComplete();\r
76 }\r
77 \r
78 // Callback from the data out phase.\r
79 static void doFormatUnitHeader(void)\r
80 {\r
81         int IP = (scsiDev.data[1] & 0x08) ? 1 : 0;\r
82         int DSP = (scsiDev.data[1] & 0x04) ? 1 : 0;\r
83         \r
84         if (! DSP) // disable save parameters\r
85         {\r
86                 configSave(); // Save the "MODE SELECT savable parameters"\r
87         }\r
88         \r
89         if (IP)\r
90         {\r
91                 // We need to read the initialisation pattern header first.\r
92                 scsiDev.dataLen += 4;\r
93                 scsiDev.phase = DATA_OUT;\r
94                 scsiDev.postDataOutHook = doFormatUnitPatternHeader;\r
95         }\r
96         else\r
97         {\r
98                 // Read the defect list data\r
99                 int defectLength =\r
100                         ((((uint16_t)scsiDev.data[2])) << 8) +\r
101                         scsiDev.data[3];\r
102                 doFormatUnitSkipData(defectLength);\r
103                 doFormatUnitComplete();\r
104         }\r
105 }\r
106 \r
107 static void doReadCapacity()\r
108 {\r
109         uint32_t lba = (((uint32) scsiDev.cdb[2]) << 24) +\r
110                 (((uint32) scsiDev.cdb[3]) << 16) +\r
111                 (((uint32) scsiDev.cdb[4]) << 8) +\r
112                 scsiDev.cdb[5];\r
113         int pmi = scsiDev.cdb[8] & 1;\r
114 \r
115         uint32_t capacity = getScsiCapacity();\r
116 \r
117         if (!pmi && lba)\r
118         {\r
119                 // error.\r
120                 // We don't do anything with the "partial medium indicator", and\r
121                 // assume that delays are constant across each block. But the spec\r
122                 // says we must return this error if pmi is specified incorrectly.\r
123                 scsiDev.status = CHECK_CONDITION;\r
124                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
125                 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;\r
126                 scsiDev.phase = STATUS;\r
127         }\r
128         else if (capacity > 0)\r
129         {\r
130                 uint32_t highestBlock = capacity - 1;\r
131 \r
132                 scsiDev.data[0] = highestBlock >> 24;\r
133                 scsiDev.data[1] = highestBlock >> 16;\r
134                 scsiDev.data[2] = highestBlock >> 8;\r
135                 scsiDev.data[3] = highestBlock;\r
136 \r
137                 scsiDev.data[4] = config->bytesPerSector >> 24;\r
138                 scsiDev.data[5] = config->bytesPerSector >> 16;\r
139                 scsiDev.data[6] = config->bytesPerSector >> 8;\r
140                 scsiDev.data[7] = config->bytesPerSector;\r
141                 scsiDev.dataLen = 8;\r
142                 scsiDev.phase = DATA_IN;\r
143         }\r
144         else\r
145         {\r
146                 scsiDev.status = CHECK_CONDITION;\r
147                 scsiDev.sense.code = NOT_READY;\r
148                 scsiDev.sense.asc = MEDIUM_NOT_PRESENT;\r
149                 scsiDev.phase = STATUS;\r
150         }\r
151 }\r
152 \r
153 static void doWrite(uint32 lba, uint32 blocks)\r
154 {\r
155         if (blockDev.state & DISK_WP)\r
156         {\r
157                 scsiDev.status = CHECK_CONDITION;\r
158                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
159                 scsiDev.sense.asc = WRITE_PROTECTED;\r
160                 scsiDev.phase = STATUS;\r
161         }\r
162         else if (((uint64) lba) + blocks > getScsiCapacity())\r
163         {\r
164                 scsiDev.status = CHECK_CONDITION;\r
165                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
166                 scsiDev.sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;\r
167                 scsiDev.phase = STATUS;\r
168         }\r
169         else\r
170         {\r
171                 transfer.dir = TRANSFER_WRITE;\r
172                 transfer.lba = lba;\r
173                 transfer.blocks = blocks;\r
174                 transfer.currentBlock = 0;\r
175                 scsiDev.phase = DATA_OUT;\r
176                 scsiDev.dataLen = config->bytesPerSector;\r
177                 scsiDev.dataPtr = config->bytesPerSector; // TODO FIX scsiDiskPoll()\r
178 \r
179                 // No need for single-block writes atm.  Overhead of the\r
180                 // multi-block write is minimal.\r
181                 transfer.multiBlock = 1;\r
182                 \r
183                 sdWriteMultiSectorPrep();\r
184         }\r
185 }\r
186 \r
187 \r
188 static void doRead(uint32 lba, uint32 blocks)\r
189 {\r
190         uint32_t capacity = getScsiCapacity();\r
191         if (((uint64) lba) + blocks > capacity)\r
192         {\r
193                 scsiDev.status = CHECK_CONDITION;\r
194                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
195                 scsiDev.sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;\r
196                 scsiDev.phase = STATUS;\r
197         }\r
198         else\r
199         {\r
200                 transfer.dir = TRANSFER_READ;\r
201                 transfer.lba = lba;\r
202                 transfer.blocks = blocks;\r
203                 transfer.currentBlock = 0;\r
204                 scsiDev.phase = DATA_IN;\r
205                 scsiDev.dataLen = 0; // No data yet\r
206 \r
207                 if ((blocks == 1) ||\r
208                         (((uint64) lba) + blocks == capacity)\r
209                         )\r
210                 {\r
211                         // We get errors on reading the last sector using a multi-sector\r
212                         // read :-(\r
213                         transfer.multiBlock = 0;\r
214                 }\r
215                 else\r
216                 {\r
217                         transfer.multiBlock = 1;\r
218                         sdReadMultiSectorPrep();\r
219                 }\r
220         }\r
221 }\r
222 \r
223 static void doSeek(uint32 lba)\r
224 {\r
225         if (lba >= getScsiCapacity())\r
226         {\r
227                 scsiDev.status = CHECK_CONDITION;\r
228                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
229                 scsiDev.sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;\r
230                 scsiDev.phase = STATUS;\r
231         }\r
232 }\r
233 \r
234 static int doTestUnitReady()\r
235 {\r
236         int ready = 1;\r
237         if (!(blockDev.state & DISK_STARTED))\r
238         {\r
239                 ready = 0;\r
240                 scsiDev.status = CHECK_CONDITION;\r
241                 scsiDev.sense.code = NOT_READY;\r
242                 scsiDev.sense.asc = LOGICAL_UNIT_NOT_READY_INITIALIZING_COMMAND_REQUIRED;\r
243                 scsiDev.phase = STATUS;\r
244         }\r
245         else if (!(blockDev.state & DISK_PRESENT))\r
246         {\r
247                 ready = 0;\r
248                 scsiDev.status = CHECK_CONDITION;\r
249                 scsiDev.sense.code = NOT_READY;\r
250                 scsiDev.sense.asc = MEDIUM_NOT_PRESENT;\r
251                 scsiDev.phase = STATUS;\r
252         }\r
253         else if (!(blockDev.state & DISK_INITIALISED))\r
254         {\r
255                 ready = 0;\r
256                 scsiDev.status = CHECK_CONDITION;\r
257                 scsiDev.sense.code = NOT_READY;\r
258                 scsiDev.sense.asc = LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE;\r
259                 scsiDev.phase = STATUS;\r
260         }\r
261         return ready;\r
262 }\r
263 \r
264 // Handle direct-access scsi device commands\r
265 int scsiDiskCommand()\r
266 {\r
267         int commandHandled = 1;\r
268 \r
269         uint8 command = scsiDev.cdb[0];\r
270         if (command == 0x1B)\r
271         {\r
272                 // START STOP UNIT\r
273                 // Enable or disable media access operations.\r
274                 // Ignore load/eject requests. We can't do that.\r
275                 //int immed = scsiDev.cdb[1] & 1;\r
276                 int start = scsiDev.cdb[4] & 1;\r
277 \r
278                 if (start)\r
279                 {\r
280                         blockDev.state = blockDev.state | DISK_STARTED;\r
281                         if (!(blockDev.state & DISK_INITIALISED))\r
282                         {\r
283                                 doSdInit();\r
284                         }\r
285                 }\r
286                 else\r
287                 {\r
288                         blockDev.state &= ~DISK_STARTED;\r
289                 }\r
290         }\r
291         else if (command == 0x00)\r
292         {\r
293                 // TEST UNIT READY\r
294                 doTestUnitReady();\r
295         }\r
296         else if (!doTestUnitReady())\r
297         {\r
298                 // Status and sense codes already set by doTestUnitReady\r
299         }\r
300         else if (command == 0x04)\r
301         {\r
302                 // FORMAT UNIT\r
303                 // We don't really do any formatting, but we need to read the correct\r
304                 // number of bytes in the DATA_OUT phase to make the SCSI host happy.\r
305                 \r
306                 int fmtData = (scsiDev.cdb[1] & 0x10) ? 1 : 0;\r
307                 if (fmtData)\r
308                 {\r
309                         // We need to read the parameter list, but we don't know how\r
310                         // big it is yet. Start with the header.\r
311                         scsiDev.dataLen = 4;\r
312                         scsiDev.phase = DATA_OUT;\r
313                         scsiDev.postDataOutHook = doFormatUnitHeader;\r
314                 }\r
315                 else\r
316                 {\r
317                         // No data to read, we're already finished!\r
318                 }\r
319         }\r
320         else if (command == 0x08)\r
321         {\r
322                 // READ(6)\r
323                 uint32 lba =\r
324                         (((uint32) scsiDev.cdb[1] & 0x1F) << 16) +\r
325                         (((uint32) scsiDev.cdb[2]) << 8) +\r
326                         scsiDev.cdb[3];\r
327                 uint32 blocks = scsiDev.cdb[4];\r
328                 if (blocks == 0) blocks = 256;\r
329                 doRead(lba, blocks);\r
330         }\r
331 \r
332         else if (command == 0x28)\r
333         {\r
334                 // READ(10)\r
335                 // Ignore all cache control bits - we don't support a memory cache.\r
336 \r
337                 uint32 lba =\r
338                         (((uint32) scsiDev.cdb[2]) << 24) +\r
339                         (((uint32) scsiDev.cdb[3]) << 16) +\r
340                         (((uint32) scsiDev.cdb[4]) << 8) +\r
341                         scsiDev.cdb[5];\r
342                 uint32 blocks =\r
343                         (((uint32) scsiDev.cdb[7]) << 8) +\r
344                         scsiDev.cdb[8];\r
345 \r
346                 doRead(lba, blocks);\r
347         }\r
348 \r
349         else if (command == 0x25)\r
350         {\r
351                 // READ CAPACITY\r
352                 doReadCapacity();\r
353         }\r
354 \r
355         else if (command == 0x0B)\r
356         {\r
357                 // SEEK(6)\r
358                 uint32 lba =\r
359                         (((uint32) scsiDev.cdb[1] & 0x1F) << 16) +\r
360                         (((uint32) scsiDev.cdb[2]) << 8) +\r
361                         scsiDev.cdb[3];\r
362 \r
363                 doSeek(lba);\r
364         }\r
365 \r
366         else if (command == 0x2B)\r
367         {\r
368                 // SEEK(10)\r
369                 uint32 lba =\r
370                         (((uint32) scsiDev.cdb[2]) << 24) +\r
371                         (((uint32) scsiDev.cdb[3]) << 16) +\r
372                         (((uint32) scsiDev.cdb[4]) << 8) +\r
373                         scsiDev.cdb[5];\r
374 \r
375                 doSeek(lba);\r
376         }\r
377         else if (command == 0x0A)\r
378         {\r
379                 // WRITE(6)\r
380                 uint32 lba =\r
381                         (((uint32) scsiDev.cdb[1] & 0x1F) << 16) +\r
382                         (((uint32) scsiDev.cdb[2]) << 8) +\r
383                         scsiDev.cdb[3];\r
384                 uint32 blocks = scsiDev.cdb[4];\r
385                 if (blocks == 0) blocks = 256;\r
386                 doWrite(lba, blocks);\r
387         }\r
388 \r
389         else if (command == 0x2A)\r
390         {\r
391                 // WRITE(10)\r
392                 // Ignore all cache control bits - we don't support a memory cache.\r
393 \r
394                 uint32 lba =\r
395                         (((uint32) scsiDev.cdb[2]) << 24) +\r
396                         (((uint32) scsiDev.cdb[3]) << 16) +\r
397                         (((uint32) scsiDev.cdb[4]) << 8) +\r
398                         scsiDev.cdb[5];\r
399                 uint32 blocks =\r
400                         (((uint32) scsiDev.cdb[7]) << 8) +\r
401                         scsiDev.cdb[8];\r
402 \r
403                 doWrite(lba, blocks);\r
404         }\r
405         else if (command == 0x36)\r
406         {\r
407                 // LOCK UNLOCK CACHE\r
408                 // We don't have a cache to lock data into. do nothing.\r
409         }\r
410         else if (command == 0x34)\r
411         {\r
412                 // PRE-FETCH.\r
413                 // We don't have a cache to pre-fetch into. do nothing.\r
414         }\r
415         else if (command == 0x1E)\r
416         {\r
417                 // PREVENT ALLOW MEDIUM REMOVAL\r
418                 // Not much we can do to prevent the user removing the SD card.\r
419                 // do nothing.\r
420         }\r
421         else if (command == 0x01)\r
422         {\r
423                 // REZERO UNIT\r
424                 // Set the lun to a vendor-specific state. Ignore.\r
425         }\r
426         else if (command == 0x35)\r
427         {\r
428                 // SYNCHRONIZE CACHE\r
429                 // We don't have a cache. do nothing.\r
430         }\r
431         else if (command == 0x2F)\r
432         {\r
433                 // VERIFY\r
434                 // TODO: When they supply data to verify, we should read the data and\r
435                 // verify it. If they don't supply any data, just say success.\r
436                 if ((scsiDev.cdb[1] & 0x02) == 0)\r
437                 {\r
438                         // They are asking us to do a medium verification with no data\r
439                         // comparison. Assume success, do nothing.\r
440                 }\r
441                 else\r
442                 {\r
443                         // TODO. This means they are supplying data to verify against.\r
444                         // Technically we should probably grab the data and compare it.\r
445                         scsiDev.status = CHECK_CONDITION;\r
446                         scsiDev.sense.code = ILLEGAL_REQUEST;\r
447                         scsiDev.sense.asc = INVALID_FIELD_IN_CDB;\r
448                         scsiDev.phase = STATUS;\r
449                 }\r
450         }\r
451         else\r
452         {\r
453                 commandHandled = 0;\r
454         }\r
455 \r
456         return commandHandled;\r
457 }\r
458 \r
459 void scsiDiskPoll()\r
460 {\r
461         if (scsiDev.phase == DATA_IN &&\r
462                 transfer.currentBlock != transfer.blocks)\r
463         {\r
464                 scsiEnterPhase(DATA_IN);\r
465 \r
466                 int totalSDSectors = transfer.blocks * SDSectorsPerSCSISector();\r
467                 uint32_t sdLBA = SCSISector2SD(transfer.lba);\r
468                 int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;\r
469                 int prep = 0;\r
470                 int i = 0;\r
471                 int scsiActive = 0;\r
472                 int sdActive = 0;\r
473                 while ((i < totalSDSectors) &&\r
474                         (scsiDev.phase == DATA_IN) &&\r
475                         !scsiDev.resetFlag)\r
476                 {\r
477                         if ((sdActive == 1) && sdReadSectorDMAPoll())\r
478                         {\r
479                                 sdActive = 0;\r
480                                 prep++;\r
481                         }\r
482                         else if ((sdActive == 0) && (prep - i < buffers) && (prep < totalSDSectors))\r
483                         {\r
484                                 // Start an SD transfer if we have space.\r
485                                 if (transfer.multiBlock)\r
486                                 {\r
487                                         sdReadMultiSectorDMA(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)]);\r
488                                 }\r
489                                 else\r
490                                 {\r
491                                         sdReadSingleSectorDMA(sdLBA + prep, &scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)]);\r
492                                 }\r
493                                 sdActive = 1;\r
494                         }\r
495 \r
496                         if ((scsiActive == 1) && scsiWriteDMAPoll())\r
497                         {\r
498                                 scsiActive = 0;\r
499                                 ++i;\r
500                         }\r
501                         else if ((scsiActive == 0) && ((prep - i) > 0))\r
502                         {\r
503                                 int dmaBytes = SD_SECTOR_SIZE;\r
504                                 if (i % SDSectorsPerSCSISector() == SDSectorsPerSCSISector() - 1)\r
505                                 {\r
506                                         dmaBytes = config->bytesPerSector % SD_SECTOR_SIZE;\r
507                                         if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;\r
508                                 }\r
509                                 scsiWriteDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)], dmaBytes);\r
510                                 scsiActive = 1;\r
511                         }\r
512                 }\r
513                 if (scsiDev.phase == DATA_IN)\r
514                 {\r
515                         scsiDev.phase = STATUS;\r
516                 }\r
517                 scsiDiskReset();\r
518         }\r
519         else if (scsiDev.phase == DATA_OUT &&\r
520                 transfer.currentBlock != transfer.blocks)\r
521         {\r
522                 scsiEnterPhase(DATA_OUT);\r
523                 \r
524                 int totalSDSectors = transfer.blocks * SDSectorsPerSCSISector();\r
525                 int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;\r
526                 int prep = 0;\r
527                 int i = 0;\r
528                 int scsiActive = 0;\r
529                 int sdActive = 0;\r
530                 while ((i < totalSDSectors) &&\r
531                         (scsiDev.phase == DATA_OUT) &&\r
532                         !scsiDev.resetFlag)\r
533                 {\r
534                         if ((sdActive == 1) && sdWriteSectorDMAPoll())\r
535                         {\r
536                                 sdActive = 0;\r
537                                 i++;\r
538                         }\r
539                         else if ((sdActive == 0) && ((prep - i) > 0))\r
540                         {\r
541                                 // Start an SD transfer if we have space.\r
542                                 sdWriteMultiSectorDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)]);\r
543                                 sdActive = 1;\r
544                         }\r
545 \r
546                         if ((scsiActive == 1) && scsiReadDMAPoll())\r
547                         {\r
548                                 scsiActive = 0;\r
549                                 ++prep;\r
550                         }\r
551                         else if ((scsiActive == 0) && ((prep - i) < buffers) && (prep < totalSDSectors))\r
552                         {\r
553                                 int dmaBytes = SD_SECTOR_SIZE;\r
554                                 if (prep % SDSectorsPerSCSISector() == SDSectorsPerSCSISector() - 1)\r
555                                 {\r
556                                         dmaBytes = config->bytesPerSector % SD_SECTOR_SIZE;\r
557                                         if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;\r
558                                 }\r
559                                 scsiReadDMA(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)], dmaBytes);\r
560                                 scsiActive = 1;\r
561                         }\r
562                 }\r
563                 \r
564                 if (scsiDev.phase == DATA_OUT)\r
565                 {\r
566                         if (scsiDev.parityError)\r
567                         {\r
568                                 scsiDev.sense.code = ABORTED_COMMAND;\r
569                                 scsiDev.sense.asc = SCSI_PARITY_ERROR;\r
570                                 scsiDev.status = CHECK_CONDITION;;\r
571                         }\r
572                         scsiDev.phase = STATUS;\r
573                 }\r
574                 scsiDiskReset();\r
575         }\r
576 }\r
577 \r
578 void scsiDiskReset()\r
579 {\r
580         scsiDev.dataPtr = 0;\r
581         scsiDev.savedDataPtr = 0;\r
582         scsiDev.dataLen = 0;\r
583         // transfer.lba = 0; // Needed in Request Sense to determine failure\r
584         transfer.blocks = 0;\r
585         transfer.currentBlock = 0;\r
586 \r
587         // Cancel long running commands!\r
588         if (transfer.inProgress == 1)\r
589         {\r
590                 if (transfer.dir == TRANSFER_WRITE)\r
591                 {\r
592                         sdCompleteWrite();\r
593                 }\r
594                 else\r
595                 {\r
596                         sdCompleteRead();\r
597                 }\r
598         }\r
599         transfer.inProgress = 0;\r
600         transfer.multiBlock = 0;\r
601 }\r
602 \r
603 void scsiDiskInit()\r
604 {\r
605         transfer.inProgress = 0;\r
606         scsiDiskReset();\r
607 \r
608         // Don't require the host to send us a START STOP UNIT command\r
609         blockDev.state = DISK_STARTED;\r
610         // WP pin not available for micro-sd\r
611         // TODO read card WP register\r
612         #if 0\r
613         if (SD_WP_Read())\r
614         {\r
615                 blockDev.state = blockDev.state | DISK_WP;\r
616         }\r
617         #endif\r
618 \r
619         // The Card-detect switches of micro-sd sockets are not standard. Don't make\r
620         // use of SD_CD so we can use sockets from other manufacturers.\r
621         // Detect presence of the card by testing whether it responds to commands.\r
622         // if (SD_CD_Read() == 1)\r
623         {\r
624                 int retry;\r
625                 blockDev.state = blockDev.state | DISK_PRESENT;\r
626 \r
627                 // Wait up to 5 seconds for the SD card to wake up.\r
628                 for (retry = 0; retry < 5; ++retry)\r
629                 {\r
630                         if (doSdInit())\r
631                         {\r
632                                 break;\r
633                         }\r
634                         else\r
635                         {\r
636                                 CyDelay(1000);\r
637                         }\r
638                 }\r
639         }\r
640 }\r
641 \r