Removed the ENDPOINT_DESCRIPTOR_DIR_* macros in favour of the shorter ENDPOINT_DIR_...
[pub/USBasp.git] / Projects / Incomplete / StandaloneProgrammer / DiskHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #include "DiskHost.h"
32
33 #if defined(USB_CAN_BE_HOST)
34 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
35 * passed to all Mass Storage Class driver functions, so that multiple instances of the same class
36 * within a device can be differentiated from one another.
37 */
38 USB_ClassInfo_MS_Host_t DiskHost_MS_Interface =
39 {
40 .Config =
41 {
42 .DataINPipeNumber = 1,
43 .DataINPipeDoubleBank = false,
44
45 .DataOUTPipeNumber = 2,
46 .DataOUTPipeDoubleBank = false,
47 },
48 };
49
50
51 void DiskHost_USBTask(void)
52 {
53 if (USB_HostState == HOST_STATE_Addressed)
54 {
55 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
56
57 uint16_t ConfigDescriptorSize;
58 uint8_t ConfigDescriptorData[512];
59
60 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
61 sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
62 {
63 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
64 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
65 return;
66 }
67
68 if (MS_Host_ConfigurePipes(&DiskHost_MS_Interface,
69 ConfigDescriptorSize, ConfigDescriptorData) != MS_ENUMERROR_NoError)
70 {
71 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
72 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
73 return;
74 }
75
76 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
77 {
78 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
79 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
80 return;
81 }
82
83 uint8_t MaxLUNIndex;
84 if (MS_Host_GetMaxLUN(&DiskHost_MS_Interface, &MaxLUNIndex))
85 {
86 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
87 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
88 return;
89 }
90
91 if (MS_Host_ResetMSInterface(&DiskHost_MS_Interface))
92 {
93 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
94 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
95 return;
96 }
97
98 USB_HostState = HOST_STATE_Configured;
99
100 /* Note: For the RequestSense call to work, the host state machine must be in the
101 * Configured state, or the call will be aborted */
102 SCSI_Request_Sense_Response_t SenseData;
103 if (MS_Host_RequestSense(&DiskHost_MS_Interface, 0, &SenseData) != 0)
104 {
105 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
106 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
107 return;
108 }
109
110 pf_mount(&DiskFATState);
111
112 LEDs_SetAllLEDs(LEDMASK_USB_READY);
113 }
114
115 MS_Host_USBTask(&DiskHost_MS_Interface);
116 }
117
118 void EVENT_USB_Host_DeviceAttached(void)
119 {
120 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
121 }
122
123 void EVENT_USB_Host_DeviceUnattached(void)
124 {
125 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
126 }
127
128 #endif
129