Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /****************************************************************************** |
2 | |||
3 | USB Host Mass Storage Class SCSI Interface Driver |
||
4 | |||
5 | This file provides the interface between the file system and the USB Host Mass |
||
6 | Storage class. It translates the file system funtionality requirements to the |
||
7 | appropriate SCSI commands, and sends the SCSI commands via the USB Mass |
||
8 | Storage class. |
||
9 | |||
10 | The functions in this file are designed to interface the Microchip Memory Disk |
||
11 | Drive File System library (see Application Note AN1045) to the USB Host Mass |
||
12 | Storage Class, allowing a PIC application to utilize mass storage devices such |
||
13 | as USB flash drives. For ease of integration, this file contains macros to |
||
14 | allow the File System code to reference the functions in this file. |
||
15 | |||
16 | Currently, the file system layer above this interface layer is limited to one |
||
17 | LUN (Logical Unit Number) on a single mass storage device. This layer accepts |
||
18 | and stores the max LUN from the USB MSD layer, but all sector reads and writes |
||
19 | are hard-coded to LUN 0, since the layer above does not specify a LUN in the |
||
20 | sector read and write commands. Also, to interface with the existing file |
||
21 | system code, only one attached device is allowed. |
||
22 | |||
23 | FileName: usb_host_msd_scsi.c |
||
24 | Dependencies: Microchip Memory Disk Drive File System v1.01 |
||
25 | Processor: PIC24/dsPIC30/dsPIC33/PIC32MX |
||
26 | Compiler: C30/C32 |
||
27 | Company: Microchip Technology, Inc. |
||
28 | |||
29 | Software License Agreement |
||
30 | |||
31 | The software supplied herewith by Microchip Technology Incorporated |
||
32 | (the Company) for its PICmicro® Microcontroller is intended and |
||
33 | supplied to you, the Companys customer, for use solely and |
||
34 | exclusively on Microchip PICmicro Microcontroller products. The |
||
35 | software is owned by the Company and/or its supplier, and is |
||
36 | protected under applicable copyright laws. All rights are reserved. |
||
37 | Any use in violation of the foregoing restrictions may subject the |
||
38 | user to criminal sanctions under applicable laws, as well as to |
||
39 | civil liability for the breach of the terms and conditions of this |
||
40 | license. |
||
41 | |||
42 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
43 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
44 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
45 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
46 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
47 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
48 | |||
49 | Change History: |
||
50 | Rev Description |
||
51 | ---------- ---------------------------------------------------------- |
||
52 | 2.6 - 2.7a No change |
||
53 | |||
54 | *******************************************************************************/ |
||
55 | |||
56 | #include <stdlib.h> |
||
57 | #include <string.h> |
||
58 | #include "Compiler.h" |
||
59 | #include "GenericTypeDefs.h" |
||
60 | #include "HardwareProfile.h" |
||
61 | #include "FSconfig.h" |
||
62 | #include "MDD File System\FSDefs.h" |
||
63 | #include "MDD File System\FSIO.h" |
||
64 | #include "USB\usb.h" |
||
65 | #include "USB\usb_host_msd.h" |
||
66 | #include "USB\usb_host_msd_scsi.h" |
||
67 | |||
68 | //#define DEBUG_MODE |
||
69 | #if defined(DEBUG_MODE) |
||
70 | #include "uart2.h" |
||
71 | #endif |
||
72 | |||
73 | |||
74 | // ***************************************************************************** |
||
75 | // ***************************************************************************** |
||
76 | // Section: Constants |
||
77 | // ***************************************************************************** |
||
78 | // ***************************************************************************** |
||
79 | |||
80 | #define FAT_GOOD_SIGN_0 0x55 // The value in the Signature0 field of a valid partition. |
||
81 | #define FAT_GOOD_SIGN_1 0xAA // The value in the Signature1 field of a valid partition. |
||
82 | #define FO_MBR 0L // Master Boot Record sector LBA |
||
83 | #define FUA_ALLOW_CACHE 0x00 // Force Unit Access, allow cache use |
||
84 | #define INITIALIZATION_ATTEMPTS 100 // How many times to try to initialize the media before failing |
||
85 | #define RDPROTECT_NORMAL 0x00 // Normal Read Protect behavior. |
||
86 | #define WRPROTECT_NORMAL 0x00 // Normal Write Protect behavior. |
||
87 | |||
88 | |||
89 | //****************************************************************************** |
||
90 | //****************************************************************************** |
||
91 | // Section: Local Prototypes and Macros |
||
92 | //****************************************************************************** |
||
93 | //****************************************************************************** |
||
94 | |||
95 | #if !defined( USBTasks ) |
||
96 | #define USBTasks() \ |
||
97 | { \ |
||
98 | USBHostTasks(); \ |
||
99 | USBHostMSDTasks(); \ |
||
100 | } |
||
101 | #endif |
||
102 | |||
103 | #if defined( PERFORM_TEST_UNIT_READY ) |
||
104 | BOOL _USBHostMSDSCSI_TestUnitReady( void ); |
||
105 | #endif |
||
106 | |||
107 | |||
108 | //****************************************************************************** |
||
109 | //****************************************************************************** |
||
110 | // Section: SCSI MSD Host Global Variables |
||
111 | //****************************************************************************** |
||
112 | //****************************************************************************** |
||
113 | |||
114 | //****************************************************************************** |
||
115 | // Section: Internal Global Variables |
||
116 | //****************************************************************************** |
||
117 | |||
118 | static BYTE deviceAddress = 0; // USB address of the attached device. |
||
119 | static MEDIA_INFORMATION mediaInformation; // Information about the attached media. |
||
120 | |||
121 | // ***************************************************************************** |
||
122 | // ***************************************************************************** |
||
123 | // Section: MSD Host Stack Callback Functions |
||
124 | // ***************************************************************************** |
||
125 | // ***************************************************************************** |
||
126 | |||
127 | /**************************************************************************** |
||
128 | Function: |
||
129 | BOOL USBHostMSDSCSIInitialize( BYTE address, DWORD flags, BYTE clientDriverID ) |
||
130 | |||
131 | Description: |
||
132 | This function is called when a USB Mass Storage device is being |
||
133 | enumerated. |
||
134 | |||
135 | Precondition: |
||
136 | None |
||
137 | |||
138 | Parameters: |
||
139 | BYTE address - Address of the new device |
||
140 | DWORD flags - Initialization flags |
||
141 | BYTE clientDriverID - ID for this layer. Not used by the media interface layer. |
||
142 | |||
143 | Return Values: |
||
144 | TRUE - We can support the device. |
||
145 | FALSE - We cannot support the device. |
||
146 | |||
147 | Remarks: |
||
148 | None |
||
149 | ***************************************************************************/ |
||
150 | |||
151 | BOOL USBHostMSDSCSIInitialize( BYTE address, DWORD flags, BYTE clientDriverID ) |
||
152 | { |
||
153 | #ifdef DEBUG_MODE |
||
154 | UART2PrintString( "SCSI: Device attached.\r\n" ); |
||
155 | #endif |
||
156 | |||
157 | if (deviceAddress == 0) |
||
158 | { |
||
159 | // Save the address of the new device. |
||
160 | deviceAddress = address; |
||
161 | return TRUE; |
||
162 | } |
||
163 | else |
||
164 | { |
||
165 | // We can currently only handle one device. |
||
166 | return FALSE; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | |||
171 | /**************************************************************************** |
||
172 | Function: |
||
173 | BOOL USBHostMSDSCSIEventHandler( BYTE address, USB_EVENT event, |
||
174 | void *data, DWORD size ) |
||
175 | |||
176 | Description: |
||
177 | This function is called when various events occur in the USB Host Mass |
||
178 | Storage client driver. |
||
179 | |||
180 | Precondition: |
||
181 | The device has been initialized. |
||
182 | |||
183 | Parameters: |
||
184 | BYTE address - Address of the device |
||
185 | USB_EVENT event - Event that has occurred |
||
186 | void *data - Pointer to data pertinent to the event |
||
187 | DWORD size - Size of the data |
||
188 | |||
189 | Return Values: |
||
190 | TRUE - Event was handled |
||
191 | FALSE - Event was not handled |
||
192 | |||
193 | Remarks: |
||
194 | None |
||
195 | ***************************************************************************/ |
||
196 | |||
197 | BOOL USBHostMSDSCSIEventHandler( BYTE address, USB_EVENT event, void *data, DWORD size ) |
||
198 | { |
||
199 | if (deviceAddress == address) |
||
200 | { |
||
201 | switch( event ) |
||
202 | { |
||
203 | case EVENT_MSD_NONE: |
||
204 | case EVENT_MSD_TRANSFER: // A MSD transfer has completed |
||
205 | return TRUE; |
||
206 | break; |
||
207 | |||
208 | case EVENT_MSD_RESET: |
||
209 | #ifdef DEBUG_MODE |
||
210 | UART2PrintString( "SCSI: MSD Reset performed.\r\n" ); |
||
211 | #endif |
||
212 | return TRUE; |
||
213 | break; |
||
214 | |||
215 | case EVENT_MSD_MAX_LUN: |
||
216 | #ifdef DEBUG_MODE |
||
217 | UART2PrintString( "SCSI: Max LUN set.\r\n" ); |
||
218 | #endif |
||
219 | mediaInformation.maxLUN = *((BYTE *)data); |
||
220 | mediaInformation.validityFlags.bits.maxLUN = 1; |
||
221 | return TRUE; |
||
222 | break; |
||
223 | |||
224 | case EVENT_DETACH: // USB cable has been detached (data: BYTE, address of device) |
||
225 | #ifdef DEBUG_MODE |
||
226 | UART2PrintString( "SCSI: Device detached.\r\n" ); |
||
227 | #endif |
||
228 | deviceAddress = 0; |
||
229 | mediaInformation.validityFlags.value = 0; |
||
230 | return TRUE; |
||
231 | break; |
||
232 | |||
233 | default: |
||
234 | return FALSE; |
||
235 | break; |
||
236 | } |
||
237 | } |
||
238 | return FALSE; |
||
239 | } |
||
240 | |||
241 | |||
242 | // ***************************************************************************** |
||
243 | // ***************************************************************************** |
||
244 | // Section: Application Callable Functions |
||
245 | // ***************************************************************************** |
||
246 | // ***************************************************************************** |
||
247 | |||
248 | /**************************************************************************** |
||
249 | Function: |
||
250 | BYTE USBHostMSDSCSIMediaDetect( void ) |
||
251 | |||
252 | Description: |
||
253 | This function determines if a mass storage device is attached and ready |
||
254 | to use. |
||
255 | |||
256 | Precondition: |
||
257 | None |
||
258 | |||
259 | Parameters: |
||
260 | None - None |
||
261 | |||
262 | Return Values: |
||
263 | TRUE - MSD present and ready |
||
264 | FALSE - MSD not present or not ready |
||
265 | |||
266 | Remarks: |
||
267 | Since this will often be called in a loop while waiting for a device, |
||
268 | we need to make sure that USB tasks are executed. |
||
269 | ***************************************************************************/ |
||
270 | |||
271 | BYTE USBHostMSDSCSIMediaDetect( void ) |
||
272 | { |
||
273 | if (USBHostMSDDeviceStatus( deviceAddress ) == USB_MSD_NORMAL_RUNNING) |
||
274 | { |
||
275 | return TRUE; |
||
276 | } |
||
277 | |||
278 | return FALSE; |
||
279 | } |
||
280 | |||
281 | |||
282 | /**************************************************************************** |
||
283 | Function: |
||
284 | MEDIA_INFORMATION * USBHostMSDSCSIMediaInitialize( void ) |
||
285 | |||
286 | Description: |
||
287 | This function initializes the media. |
||
288 | |||
289 | Precondition: |
||
290 | None |
||
291 | |||
292 | Parameters: |
||
293 | None - None |
||
294 | |||
295 | Returns: |
||
296 | The function returns a pointer to the MEDIA_INFORMATION structure. The |
||
297 | errorCode member may contain the following values: |
||
298 | * MEDIA_NO_ERROR - The media initialized successfully, and the |
||
299 | sector size should be valid (confirm using the validityFlags |
||
300 | bit). |
||
301 | * MEDIA_DEVICE_NOT_PRESENT - The requested device is not attached. |
||
302 | * MEDIA_CANNOT_INITIALIZE - Cannot initialize the media. |
||
303 | |||
304 | Remarks: |
||
305 | This function performs the following SCSI commands: |
||
306 | * READ CAPACITY 10 |
||
307 | * REQUEST SENSE |
||
308 | |||
309 | The READ CAPACITY 10 command block is as follows: |
||
310 | |||
311 | <code> |
||
312 | Byte/Bit 7 6 5 4 3 2 1 0 |
||
313 | |||
314 | 1 [ Reserved ] |
||
315 | 2 [ (MSB) |
||
316 | 3 Logical Block Address |
||
317 | 4 |
||
318 | 5 (LSB) ] |
||
319 | 6 [ Reserved |
||
320 | 7 ] |
||
321 | 8 [ Reserved ] [ PMI ] |
||
322 | 9 [ Control ] |
||
323 | </code> |
||
324 | |||
325 | The REQUEST SENSE command block is as follows: |
||
326 | |||
327 | <code> |
||
328 | Byte/Bit 7 6 5 4 3 2 1 0 |
||
329 | |||
330 | 1 [ Reserved ] [ DESC] |
||
331 | 2 [ Reserved |
||
332 | 3 ] |
||
333 | 4 [ Allocation Length ] |
||
334 | 5 [ Control ] |
||
335 | </code> |
||
336 | ***************************************************************************/ |
||
337 | |||
338 | MEDIA_INFORMATION * USBHostMSDSCSIMediaInitialize( void ) |
||
339 | { |
||
340 | BYTE attempts; |
||
341 | DWORD byteCount; |
||
342 | BYTE commandBlock[10]; |
||
343 | BYTE errorCode; |
||
344 | BYTE inquiryData[36]; |
||
345 | |||
346 | // Make sure the device is still attached. |
||
347 | if (deviceAddress == 0) |
||
348 | { |
||
349 | mediaInformation.errorCode = MEDIA_DEVICE_NOT_PRESENT; |
||
350 | return &mediaInformation; |
||
351 | } |
||
352 | |||
353 | attempts = INITIALIZATION_ATTEMPTS; |
||
354 | while (attempts != 0) |
||
355 | { |
||
356 | attempts --; |
||
357 | |||
358 | #ifdef DEBUG_MODE |
||
359 | UART2PrintString( "SCSI: READ CAPACITY 10..." ); |
||
360 | #endif |
||
361 | |||
362 | // Fill in the command block with the READ CAPACITY 10 parameters. |
||
363 | commandBlock[0] = 0x25; // Operation Code |
||
364 | commandBlock[1] = 0; // |
||
365 | commandBlock[2] = 0; // |
||
366 | commandBlock[3] = 0; // |
||
367 | commandBlock[4] = 0; // |
||
368 | commandBlock[5] = 0; // |
||
369 | commandBlock[6] = 0; // |
||
370 | commandBlock[7] = 0; // |
||
371 | commandBlock[8] = 0; // |
||
372 | commandBlock[9] = 0x00; // Control |
||
373 | |||
374 | errorCode = USBHostMSDRead( deviceAddress, 0, commandBlock, 10, inquiryData, 8 ); |
||
375 | #ifdef DEBUG_MODE |
||
376 | UART2PutHex( errorCode ) ; |
||
377 | UART2PutChar( ' ' ); |
||
378 | #endif |
||
379 | |||
380 | if (!errorCode) |
||
381 | { |
||
382 | while (!USBHostMSDTransferIsComplete( deviceAddress, &errorCode, &byteCount )) |
||
383 | { |
||
384 | USBTasks(); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | if (!errorCode) |
||
389 | { |
||
390 | #ifdef DEBUG_MODE |
||
391 | UART2PutHex( errorCode ) ; |
||
392 | UART2PrintString( "\r\n" ); |
||
393 | #endif |
||
394 | |||
395 | // Determine sector size. |
||
396 | #ifdef DEBUG_MODE |
||
397 | UART2PrintString( "SCSI: Sector size:" ); |
||
398 | UART2PutChar( inquiryData[7] + '0' ); |
||
399 | UART2PutChar( inquiryData[6] + '0' ); |
||
400 | UART2PutChar( inquiryData[5] + '0' ); |
||
401 | UART2PutChar( inquiryData[4] + '0' ); |
||
402 | UART2PrintString( "\r\n" ); |
||
403 | #endif |
||
404 | mediaInformation.sectorSize = (inquiryData[7] << 12) + (inquiryData[6] << 8) + (inquiryData[5] << 4) + (inquiryData[4]); |
||
405 | mediaInformation.validityFlags.bits.sectorSize = 1; |
||
406 | |||
407 | mediaInformation.errorCode = MEDIA_NO_ERROR; |
||
408 | return &mediaInformation; |
||
409 | } |
||
410 | else |
||
411 | { |
||
412 | // Perform a Request Sense to try to clear the stall. |
||
413 | #ifdef DEBUG_MODE |
||
414 | UART2PrintString( "SCSI: REQUEST SENSE..." ); |
||
415 | #endif |
||
416 | |||
417 | // Fill in the command block with the REQUEST SENSE parameters. |
||
418 | commandBlock[0] = 0x03; // Operation Code |
||
419 | commandBlock[1] = 0; // |
||
420 | commandBlock[2] = 0; // |
||
421 | commandBlock[3] = 0; // |
||
422 | commandBlock[4] = 18; // Allocation length |
||
423 | commandBlock[5] = 0; // Control |
||
424 | |||
425 | errorCode = USBHostMSDRead( deviceAddress, 0, commandBlock, 6, inquiryData, 18 ); |
||
426 | #ifdef DEBUG_MODE |
||
427 | UART2PutHex( errorCode ) ; |
||
428 | UART2PutChar( ' ' ); |
||
429 | #endif |
||
430 | |||
431 | if (!errorCode) |
||
432 | { |
||
433 | while (!USBHostMSDTransferIsComplete( deviceAddress, &errorCode, &byteCount )) |
||
434 | { |
||
435 | USBTasks(); |
||
436 | } |
||
437 | } |
||
438 | } |
||
439 | } |
||
440 | |||
441 | mediaInformation.errorCode = MEDIA_CANNOT_INITIALIZE; |
||
442 | return &mediaInformation; |
||
443 | |||
444 | } |
||
445 | |||
446 | |||
447 | /**************************************************************************** |
||
448 | Function: |
||
449 | BOOL USBHostMSDSCSIMediaReset( void ) |
||
450 | |||
451 | Summary: |
||
452 | This function resets the media. |
||
453 | |||
454 | Description: |
||
455 | This function resets the media. It is called if an operation returns an |
||
456 | error. Or the application can call it. |
||
457 | |||
458 | Precondition: |
||
459 | None |
||
460 | |||
461 | Parameters: |
||
462 | None - None |
||
463 | |||
464 | Return Values: |
||
465 | USB_SUCCESS - Reset successful |
||
466 | USB_MSD_DEVICE_NOT_FOUND - No device with specified address |
||
467 | USB_ILLEGAL_REQUEST - Device is in an illegal USB state |
||
468 | for reset |
||
469 | |||
470 | Remarks: |
||
471 | None |
||
472 | ***************************************************************************/ |
||
473 | |||
474 | BYTE USBHostMSDSCSIMediaReset( void ) |
||
475 | { |
||
476 | DWORD byteCount; |
||
477 | BYTE errorCode; |
||
478 | |||
479 | errorCode = USBHostMSDResetDevice( deviceAddress ); |
||
480 | if (errorCode) |
||
481 | { |
||
482 | return errorCode; |
||
483 | } |
||
484 | |||
485 | do |
||
486 | { |
||
487 | USBTasks(); |
||
488 | errorCode = USBHostMSDDeviceStatus( deviceAddress ); |
||
489 | } while (errorCode == USB_MSD_RESETTING_DEVICE); |
||
490 | |||
491 | |||
492 | if (USBHostMSDTransferIsComplete( deviceAddress, &errorCode, &byteCount )) |
||
493 | { |
||
494 | return errorCode; |
||
495 | } |
||
496 | |||
497 | return USB_MSD_RESET_ERROR; |
||
498 | } |
||
499 | |||
500 | |||
501 | /**************************************************************************** |
||
502 | Function: |
||
503 | BYTE USBHostMSDSCSISectorRead( DWORD sectorAddress, BYTE *dataBuffer) |
||
504 | |||
505 | Summary: |
||
506 | This function reads one sector. |
||
507 | |||
508 | Description: |
||
509 | This function uses the SCSI command READ10 to read one sector. The size |
||
510 | of the sector was determined in the USBHostMSDSCSIMediaInitialize() |
||
511 | function. The data is stored in the application buffer. |
||
512 | |||
513 | Precondition: |
||
514 | None |
||
515 | |||
516 | Parameters: |
||
517 | DWORD sectorAddress - address of sector to read |
||
518 | BYTE *dataBuffer - buffer to store data |
||
519 | |||
520 | Return Values: |
||
521 | TRUE - read performed successfully |
||
522 | FALSE - read was not successful |
||
523 | |||
524 | Remarks: |
||
525 | The READ10 command block is as follows: |
||
526 | |||
527 | <code> |
||
528 | Byte/Bit 7 6 5 4 3 2 1 0 |
||
529 | |||
530 | 1 [ RDPROTECT ] DPO FUA - FUA_NV - |
||
531 | 2 [ (MSB) |
||
532 | 3 Logical Block Address |
||
533 | 4 |
||
534 | 5 (LSB) ] |
||
535 | 6 [ - ][ Group Number ] |
||
536 | 7 [ (MSB) Transfer Length |
||
537 | 8 (LSB) ] |
||
538 | 9 [ Control ] |
||
539 | </code> |
||
540 | ***************************************************************************/ |
||
541 | |||
542 | BYTE USBHostMSDSCSISectorRead( DWORD sectorAddress, BYTE *dataBuffer ) |
||
543 | { |
||
544 | DWORD byteCount; |
||
545 | BYTE commandBlock[10]; |
||
546 | BYTE errorCode; |
||
547 | |||
548 | #ifdef DEBUG_MODE |
||
549 | UART2PrintString( "SCSI: Reading sector " ); |
||
550 | UART2PutHex(sectorAddress >> 24); |
||
551 | UART2PutHex(sectorAddress >> 16); |
||
552 | UART2PutHex(sectorAddress >> 8); |
||
553 | UART2PutHex(sectorAddress); |
||
554 | UART2PrintString( " Device " ); |
||
555 | UART2PutHex(deviceAddress); |
||
556 | UART2PrintString( "\r\n" ); |
||
557 | #endif |
||
558 | |||
559 | if (deviceAddress == 0) |
||
560 | { |
||
561 | return FALSE; // USB_MSD_DEVICE_NOT_FOUND; |
||
562 | } |
||
563 | |||
564 | // Fill in the command block with the READ10 parameters. |
||
565 | commandBlock[0] = 0x28; // Operation code |
||
566 | commandBlock[1] = RDPROTECT_NORMAL | FUA_ALLOW_CACHE; |
||
567 | commandBlock[2] = (BYTE) (sectorAddress >> 24); // Big endian! |
||
568 | commandBlock[3] = (BYTE) (sectorAddress >> 16); |
||
569 | commandBlock[4] = (BYTE) (sectorAddress >> 8); |
||
570 | commandBlock[5] = (BYTE) (sectorAddress); |
||
571 | commandBlock[6] = 0x00; // Group Number |
||
572 | commandBlock[7] = 0x00; // Number of blocks - Big endian! |
||
573 | commandBlock[8] = 0x01; |
||
574 | commandBlock[9] = 0x00; // Control |
||
575 | |||
576 | // Currently using LUN=0. When the File System supports multiple LUN's, this will change. |
||
577 | errorCode = USBHostMSDRead( deviceAddress, 0, commandBlock, 10, dataBuffer, mediaInformation.sectorSize ); |
||
578 | #ifdef DEBUG_MODE |
||
579 | UART2PrintString( "SCSI: Read sector init error " ); |
||
580 | UART2PutHex( errorCode ); |
||
581 | UART2PrintString( "\r\n" ); |
||
582 | #endif |
||
583 | |||
584 | if (!errorCode) |
||
585 | { |
||
586 | while (!USBHostMSDTransferIsComplete( deviceAddress, &errorCode, &byteCount )) |
||
587 | { |
||
588 | USBTasks(); |
||
589 | } |
||
590 | } |
||
591 | |||
592 | #ifdef DEBUG_MODE |
||
593 | UART2PrintString( "SCSI: Read sector error " ); |
||
594 | UART2PutHex( errorCode ); |
||
595 | UART2PrintString( "\r\n" ); |
||
596 | #endif |
||
597 | |||
598 | if (!errorCode) |
||
599 | { |
||
600 | return TRUE; |
||
601 | } |
||
602 | else |
||
603 | { |
||
604 | // USBHostMSDSCSIMediaReset(); |
||
605 | return FALSE; |
||
606 | } |
||
607 | } |
||
608 | |||
609 | /**************************************************************************** |
||
610 | Function: |
||
611 | BYTE USBHostMSDSCSISectorWrite( DWORD sectorAddress, BYTE *dataBuffer, BYTE allowWriteToZero ) |
||
612 | |||
613 | Summary: |
||
614 | This function writes one sector. |
||
615 | |||
616 | Description: |
||
617 | This function uses the SCSI command WRITE10 to write one sector. The size |
||
618 | of the sector was determined in the USBHostMSDSCSIMediaInitialize() |
||
619 | function. The data is read from the application buffer. |
||
620 | |||
621 | Precondition: |
||
622 | None |
||
623 | |||
624 | Parameters: |
||
625 | DWORD sectorAddress - address of sector to write |
||
626 | BYTE *dataBuffer - buffer with application data |
||
627 | BYTE allowWriteToZero- If a write to sector 0 is allowed. |
||
628 | |||
629 | Return Values: |
||
630 | TRUE - write performed successfully |
||
631 | FALSE - write was not successful |
||
632 | |||
633 | Remarks: |
||
634 | To follow convention, this function blocks until the write is complete. |
||
635 | |||
636 | The WRITE10 command block is as follows: |
||
637 | |||
638 | <code> |
||
639 | Byte/Bit 7 6 5 4 3 2 1 0 |
||
640 | |||
641 | 1 [ WRPROTECT ] DPO FUA - FUA_NV - |
||
642 | 2 [ (MSB) |
||
643 | 3 Logical Block Address |
||
644 | 4 |
||
645 | 5 (LSB) ] |
||
646 | 6 [ - ][ Group Number ] |
||
647 | 7 [ (MSB) Transfer Length |
||
648 | 8 (LSB) ] |
||
649 | 9 [ Control ] |
||
650 | </code> |
||
651 | ***************************************************************************/ |
||
652 | |||
653 | BYTE USBHostMSDSCSISectorWrite( DWORD sectorAddress, BYTE *dataBuffer, BYTE allowWriteToZero ) |
||
654 | { |
||
655 | DWORD byteCount; |
||
656 | BYTE commandBlock[10]; |
||
657 | BYTE errorCode; |
||
658 | |||
659 | #ifdef DEBUG_MODE |
||
660 | UART2PrintString( "SCSI: Writing sector " ); |
||
661 | UART2PutHex(sectorAddress >> 24); |
||
662 | UART2PutHex(sectorAddress >> 16); |
||
663 | UART2PutHex(sectorAddress >> 8); |
||
664 | UART2PutHex(sectorAddress); |
||
665 | UART2PrintString( " Device " ); |
||
666 | UART2PutHex(deviceAddress); |
||
667 | UART2PrintString( "\r\n" ); |
||
668 | #endif |
||
669 | |||
670 | if (deviceAddress == 0) |
||
671 | { |
||
672 | return FALSE; //USB_MSD_DEVICE_NOT_FOUND; |
||
673 | } |
||
674 | |||
675 | if ((sectorAddress == 0) && (allowWriteToZero == FALSE)) |
||
676 | { |
||
677 | return FALSE; |
||
678 | } |
||
679 | |||
680 | // Fill in the command block with the WRITE 10 parameters. |
||
681 | commandBlock[0] = 0x2A; // Operation code |
||
682 | commandBlock[1] = WRPROTECT_NORMAL | FUA_ALLOW_CACHE; |
||
683 | commandBlock[2] = (BYTE) (sectorAddress >> 24); // Big endian! |
||
684 | commandBlock[3] = (BYTE) (sectorAddress >> 16); |
||
685 | commandBlock[4] = (BYTE) (sectorAddress >> 8); |
||
686 | commandBlock[5] = (BYTE) (sectorAddress); |
||
687 | commandBlock[6] = 0x00; // Group Number |
||
688 | commandBlock[7] = 0x00; // Number of blocks - Big endian! |
||
689 | commandBlock[8] = 0x01; |
||
690 | commandBlock[9] = 0x00; // Control |
||
691 | |||
692 | // Currently using LUN=0. When the File System supports multiple LUN's, this will change. |
||
693 | errorCode = USBHostMSDWrite( deviceAddress, 0, commandBlock, 10, dataBuffer, mediaInformation.sectorSize ); |
||
694 | #ifdef DEBUG_MODE |
||
695 | UART2PrintString( "SCSI: Write sector init error " ); |
||
696 | UART2PutHex( errorCode ); |
||
697 | UART2PrintString( "\r\n" ); |
||
698 | #endif |
||
699 | |||
700 | if (!errorCode) |
||
701 | { |
||
702 | while (!USBHostMSDTransferIsComplete( deviceAddress, &errorCode, &byteCount )) |
||
703 | { |
||
704 | USBTasks(); |
||
705 | } |
||
706 | } |
||
707 | |||
708 | #ifdef DEBUG_MODE |
||
709 | UART2PrintString( "SCSI: Write sector error " ); |
||
710 | UART2PutHex( errorCode ); |
||
711 | UART2PrintString( "\r\n" ); |
||
712 | #endif |
||
713 | |||
714 | if (!errorCode) |
||
715 | { |
||
716 | return TRUE; |
||
717 | } |
||
718 | else |
||
719 | { |
||
720 | // USBHostMSDSCSIMediaReset(); |
||
721 | return FALSE; |
||
722 | } |
||
723 | } |
||
724 | |||
725 | |||
726 | /**************************************************************************** |
||
727 | Function: |
||
728 | BYTE USBHostMSDSCSIWriteProtectState( void ) |
||
729 | |||
730 | Description: |
||
731 | This function returns the write protect status of the device. |
||
732 | |||
733 | Precondition: |
||
734 | None |
||
735 | |||
736 | Parameters: |
||
737 | None - None |
||
738 | |||
739 | Return Values: |
||
740 | |||
741 | |||
742 | |||
743 | Remarks: |
||
744 | None |
||
745 | ***************************************************************************/ |
||
746 | |||
747 | BYTE USBHostMSDSCSIWriteProtectState( void ) |
||
748 | { |
||
749 | return 0; |
||
750 | } |
||
751 | |||
752 | |||
753 | // ***************************************************************************** |
||
754 | // ***************************************************************************** |
||
755 | // Section: Internal Functions |
||
756 | // ***************************************************************************** |
||
757 | // ***************************************************************************** |
||
758 | |||
759 | |||
760 | /******************************************************************************* |
||
761 | Function: |
||
762 | BOOL _USBHostMSDSCSI_TestUnitReady( void ) |
||
763 | |||
764 | Precondition: |
||
765 | None |
||
766 | |||
767 | Overview: |
||
768 | This function sends the TEST UNIT READY SCSI command |
||
769 | |||
770 | Parameters: |
||
771 | None - None |
||
772 | |||
773 | Return Values: |
||
774 | TRUE - Command completed without error |
||
775 | FALSE - Error while performing command |
||
776 | |||
777 | Remarks: |
||
778 | The format of the TEST UNIT READY command is as follows: |
||
779 | |||
780 | <code> |
||
781 | Byte/Bit 7 6 5 4 3 2 1 0 |
||
782 | |||
783 | 1 [ |
||
784 | 2 Reserved |
||
785 | 3 |
||
786 | 4 ] |
||
787 | 5 [ Control ] |
||
788 | </code> |
||
789 | ***************************************************************************/ |
||
790 | |||
791 | #ifdef PERFORM_TEST_UNIT_READY |
||
792 | BOOL _USBHostMSDSCSI_TestUnitReady( void ) |
||
793 | { |
||
794 | DWORD byteCount; |
||
795 | BYTE commandBlock[10]; |
||
796 | BYTE errorCode; |
||
797 | BYTE inquiryData[36]; |
||
798 | BYTE unitReadyCount; |
||
799 | |||
800 | // Issue a TEST UNIT READY |
||
801 | #ifdef DEBUG_MODE |
||
802 | UART2PrintString( "SCSI: TEST UNIT READY..." ); |
||
803 | #endif |
||
804 | |||
805 | unitReadyCount = 0; |
||
806 | while (unitReadyCount < 5) |
||
807 | { |
||
808 | unitReadyCount ++; |
||
809 | |||
810 | // Fill in the command block with the TEST UNIT READY parameters. |
||
811 | commandBlock[0] = 0x00; // Operation Code |
||
812 | commandBlock[1] = 0; // Reserved |
||
813 | commandBlock[2] = 0; // Reserved |
||
814 | commandBlock[3] = 0; // Reserved |
||
815 | commandBlock[4] = 0; // Reserved |
||
816 | commandBlock[5] = 0x00; // Control |
||
817 | |||
818 | errorCode = USBHostMSDRead( deviceAddress, 0, commandBlock, 6, inquiryData, 0 ); |
||
819 | #ifdef DEBUG_MODE |
||
820 | UART2PutHex( errorCode ) ; |
||
821 | UART2PutChar( ' ' ); |
||
822 | #endif |
||
823 | |||
824 | if (!errorCode) |
||
825 | { |
||
826 | while (!USBHostMSDTransferIsComplete( deviceAddress, &errorCode, &byteCount )) |
||
827 | { |
||
828 | USBTasks(); |
||
829 | } |
||
830 | } |
||
831 | #ifdef DEBUG_MODE |
||
832 | UART2PutHex( errorCode ) ; |
||
833 | UART2PrintString( "\r\n" ); |
||
834 | #endif |
||
835 | if (!errorCode) |
||
836 | { |
||
837 | return TRUE; |
||
838 | } |
||
839 | } |
||
840 | |||
841 | return FALSE; |
||
842 | } |
||
843 | #endif |
||
844 |
Powered by WebSVN v2.8.3