]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Projects/Webserver/USBHostMode.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / Projects / Webserver / USBHostMode.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  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 disclaims 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 Host Mode management functions and variables. This file contains the LUFA code required to
34  *  manage the USB RNDIS host mode.
35  */
36
37 #include "USBHostMode.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_Host_t Ethernet_RNDIS_Interface_Host =
44         {
45                 .Config =
46                         {
47                                 .DataINPipe             =
48                                         {
49                                                 .Address        = (PIPE_DIR_IN  | 1),
50                                                 .Banks          = 1,
51                                         },
52                                 .DataOUTPipe            =
53                                         {
54                                                 .Address        = (PIPE_DIR_OUT | 2),
55                                                 .Banks          = 1,
56                                         },
57                                 .NotificationPipe       =
58                                         {
59                                                 .Address        = (PIPE_DIR_IN  | 3),
60                                                 .Banks          = 1,
61                                         },
62                                 .HostMaxPacketSize      = UIP_CONF_BUFFER_SIZE,
63                         },
64         };
65
66
67 /** USB host mode management task. This function manages the RNDIS Host class driver and uIP stack when the device is
68  *  initialized in USB host mode.
69  */
70 void USBHostMode_USBTask(void)
71 {
72         if (USB_CurrentMode != USB_MODE_Host)
73           return;
74
75         uIPManagement_ManageNetwork();
76
77         RNDIS_Host_USBTask(&Ethernet_RNDIS_Interface_Host);
78 }
79
80 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
81  *  starts the library USB task to begin the enumeration and USB management process.
82  */
83 void EVENT_USB_Host_DeviceAttached(void)
84 {
85         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
86 }
87
88 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
89  *  stops the library USB task management process.
90  */
91 void EVENT_USB_Host_DeviceUnattached(void)
92 {
93         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
94 }
95
96 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
97  *  enumerated by the host and is now ready to be used by the application.
98  */
99 void EVENT_USB_Host_DeviceEnumerationComplete(void)
100 {
101         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
102
103         uint16_t ConfigDescriptorSize;
104         uint8_t  ConfigDescriptorData[512];
105
106         if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
107                                                sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
108         {
109                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
110                 return;
111         }
112
113         if (RNDIS_Host_ConfigurePipes(&Ethernet_RNDIS_Interface_Host,
114                                       ConfigDescriptorSize, ConfigDescriptorData) != RNDIS_ENUMERROR_NoError)
115         {
116                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
117                 return;
118         }
119
120         if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
121         {
122                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
123                 return;
124         }
125
126         if (RNDIS_Host_InitializeDevice(&Ethernet_RNDIS_Interface_Host) != HOST_SENDCONTROL_Successful)
127         {
128                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
129                 USB_Host_SetDeviceConfiguration(0);
130                 return;
131         }
132
133         uint32_t PacketFilter = (REMOTE_NDIS_PACKET_DIRECTED | REMOTE_NDIS_PACKET_BROADCAST);
134         if (RNDIS_Host_SetRNDISProperty(&Ethernet_RNDIS_Interface_Host, OID_GEN_CURRENT_PACKET_FILTER,
135                                         &PacketFilter, sizeof(PacketFilter)) != HOST_SENDCONTROL_Successful)
136         {
137                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
138                 USB_Host_SetDeviceConfiguration(0);
139                 return;
140         }
141
142         if (RNDIS_Host_QueryRNDISProperty(&Ethernet_RNDIS_Interface_Host, OID_802_3_CURRENT_ADDRESS,
143                                           &MACAddress, sizeof(MACAddress)) != HOST_SENDCONTROL_Successful)
144         {
145                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
146                 USB_Host_SetDeviceConfiguration(0);
147                 return;
148         }
149
150         /* Initialize uIP stack */
151         uIPManagement_Init();
152
153         LEDs_SetAllLEDs(LEDMASK_USB_READY);
154 }
155
156 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
157 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
158 {
159         USB_Disable();
160
161         LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
162         for(;;);
163 }
164
165 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
166  *  enumerating an attached USB device.
167  */
168 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
169 {
170         LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
171 }
172