f5856ebbdceab93209ea47c3612a0f5ea9b7a555
[pub/USBasp.git] / Projects / Webserver / USBDeviceMode.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 /** \file
32 *
33 * USB Device Mode management functions and variables. This file contains the LUFA code required to
34 * manage the USB Mass Storage device mode.
35 */
36
37 #include "USBDeviceMode.h"
38
39 /** LUFA RNDIS Class driver interface configuration and state information. This structure is
40 * passed to all RNDIS Class driver functions, so that multiple instances of the same class
41 * within a device can be differentiated from one another.
42 */
43 USB_ClassInfo_RNDIS_Device_t Ethernet_RNDIS_Interface_Device =
44 {
45 .Config =
46 {
47 .ControlInterfaceNumber = 0,
48
49 .DataINEndpointNumber = CDC_TX_EPNUM,
50 .DataINEndpointSize = CDC_TXRX_EPSIZE,
51 .DataINEndpointDoubleBank = false,
52
53 .DataOUTEndpointNumber = CDC_RX_EPNUM,
54 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
55 .DataOUTEndpointDoubleBank = false,
56
57 .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
58 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
59 .NotificationEndpointDoubleBank = false,
60
61 .AdapterVendorDescription = "LUFA RNDIS Adapter",
62 .AdapterMACAddress = {{0x02, 0x00, 0x02, 0x00, 0x02, 0x00}},
63 },
64 };
65
66 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
67 * passed to all Mass Storage Class driver functions, so that multiple instances of the same class
68 * within a device can be differentiated from one another.
69 */
70 USB_ClassInfo_MS_Device_t Disk_MS_Interface =
71 {
72 .Config =
73 {
74 .InterfaceNumber = 2,
75
76 .DataINEndpointNumber = MASS_STORAGE_IN_EPNUM,
77 .DataINEndpointSize = MASS_STORAGE_IO_EPSIZE,
78 .DataINEndpointDoubleBank = false,
79
80 .DataOUTEndpointNumber = MASS_STORAGE_OUT_EPNUM,
81 .DataOUTEndpointSize = MASS_STORAGE_IO_EPSIZE,
82 .DataOUTEndpointDoubleBank = false,
83
84 .TotalLUNs = 1,
85 },
86 };
87
88
89 /** USB device mode management task. This function manages the Mass Storage Device class driver when the device is
90 * initialized in USB device mode.
91 */
92 void USBDeviceMode_USBTask(void)
93 {
94 if (USB_CurrentMode != USB_MODE_Device)
95 return;
96
97 uIPManagement_ManageNetwork();
98
99 RNDIS_Device_USBTask(&Ethernet_RNDIS_Interface_Device);
100 MS_Device_USBTask(&Disk_MS_Interface);
101 }
102
103 /** Event handler for the library USB Connection event. */
104 void EVENT_USB_Device_Connect(void)
105 {
106 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
107
108 uIPManagement_Init();
109 }
110
111 /** Event handler for the library USB Disconnection event. */
112 void EVENT_USB_Device_Disconnect(void)
113 {
114 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
115 }
116
117 /** Event handler for the library USB Configuration Changed event. */
118 void EVENT_USB_Device_ConfigurationChanged(void)
119 {
120 bool ConfigSuccess = true;
121
122 ConfigSuccess &= RNDIS_Device_ConfigureEndpoints(&Ethernet_RNDIS_Interface_Device);
123 ConfigSuccess &= MS_Device_ConfigureEndpoints(&Disk_MS_Interface);
124
125 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
126 }
127
128 /** Event handler for the library USB Control Request reception event. */
129 void EVENT_USB_Device_ControlRequest(void)
130 {
131 RNDIS_Device_ProcessControlRequest(&Ethernet_RNDIS_Interface_Device);
132 MS_Device_ProcessControlRequest(&Disk_MS_Interface);
133 }
134
135 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
136 *
137 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
138 */
139 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
140 {
141 bool CommandSuccess;
142
143 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
144 CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
145 LEDs_SetAllLEDs(LEDMASK_USB_READY);
146
147 return CommandSuccess;
148 }
149