]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-git/Projects/Webserver/USBDeviceMode.c
allow overriding of TARGET
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-git / Projects / Webserver / USBDeviceMode.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  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 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 /** Message buffer for RNDIS messages processed by the RNDIS device class driver. */
40 static uint8_t RNDIS_Message_Buffer[192];
41
42 /** LUFA RNDIS Class driver interface configuration and state information. This structure is
43  *  passed to all RNDIS Class driver functions, so that multiple instances of the same class
44  *  within a device can be differentiated from one another.
45  */
46 USB_ClassInfo_RNDIS_Device_t Ethernet_RNDIS_Interface_Device =
47         {
48                 .Config =
49                         {
50                                 .ControlInterfaceNumber         = INTERFACE_ID_CDC_CCI,
51                                 .DataINEndpoint                 =
52                                         {
53                                                 .Address                = CDC_TX_EPADDR,
54                                                 .Size                   = CDC_TXRX_EPSIZE,
55                                                 .Banks                  = 1,
56                                         },
57                                 .DataOUTEndpoint                =
58                                         {
59                                                 .Address                = CDC_RX_EPADDR,
60                                                 .Size                   = CDC_TXRX_EPSIZE,
61                                                 .Banks                  = 1,
62                                         },
63                                 .NotificationEndpoint           =
64                                         {
65                                                 .Address                = CDC_NOTIFICATION_EPADDR,
66                                                 .Size                   = CDC_NOTIFICATION_EPSIZE,
67                                                 .Banks                  = 1,
68                                         },
69                                 .AdapterVendorDescription       = "LUFA RNDIS Adapter",
70                                 .AdapterMACAddress              = {{0x02, 0x00, 0x02, 0x00, 0x02, 0x00}},
71                                 .MessageBuffer                  = RNDIS_Message_Buffer,
72                                 .MessageBufferLength            = sizeof(RNDIS_Message_Buffer),
73                         },
74         };
75
76 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
77  *  passed to all Mass Storage Class driver functions, so that multiple instances of the same class
78  *  within a device can be differentiated from one another.
79  */
80 USB_ClassInfo_MS_Device_t Disk_MS_Interface =
81         {
82                 .Config =
83                         {
84                                 .InterfaceNumber                = INTERFACE_ID_MassStorage,
85                                 .DataINEndpoint                 =
86                                         {
87                                                 .Address                = MASS_STORAGE_IN_EPADDR,
88                                                 .Size                   = MASS_STORAGE_IO_EPSIZE,
89                                                 .Banks                  = 1,
90                                         },
91                                 .DataOUTEndpoint                =
92                                         {
93                                                 .Address                = MASS_STORAGE_OUT_EPADDR,
94                                                 .Size                   = MASS_STORAGE_IO_EPSIZE,
95                                                 .Banks                  = 1,
96                                         },
97                                 .TotalLUNs                 = 1,
98                         },
99         };
100
101
102 /** USB device mode management task. This function manages the Mass Storage Device class driver when the device is
103  *  initialized in USB device mode.
104  */
105 void USBDeviceMode_USBTask(void)
106 {
107         if (USB_CurrentMode != USB_MODE_Device)
108           return;
109
110         uIPManagement_ManageNetwork();
111
112         RNDIS_Device_USBTask(&Ethernet_RNDIS_Interface_Device);
113         MS_Device_USBTask(&Disk_MS_Interface);
114 }
115
116 /** Event handler for the library USB Connection event. */
117 void EVENT_USB_Device_Connect(void)
118 {
119         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
120
121         uIPManagement_Init();
122 }
123
124 /** Event handler for the library USB Disconnection event. */
125 void EVENT_USB_Device_Disconnect(void)
126 {
127         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
128 }
129
130 /** Event handler for the library USB Configuration Changed event. */
131 void EVENT_USB_Device_ConfigurationChanged(void)
132 {
133         bool ConfigSuccess = true;
134
135         ConfigSuccess &= RNDIS_Device_ConfigureEndpoints(&Ethernet_RNDIS_Interface_Device);
136         ConfigSuccess &= MS_Device_ConfigureEndpoints(&Disk_MS_Interface);
137
138         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
139 }
140
141 /** Event handler for the library USB Control Request reception event. */
142 void EVENT_USB_Device_ControlRequest(void)
143 {
144         RNDIS_Device_ProcessControlRequest(&Ethernet_RNDIS_Interface_Device);
145         MS_Device_ProcessControlRequest(&Disk_MS_Interface);
146 }
147
148 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
149  *
150  *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface configuration structure being referenced
151  */
152 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
153 {
154         bool CommandSuccess;
155
156         LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
157         CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
158         LEDs_SetAllLEDs(LEDMASK_USB_READY);
159
160         return CommandSuccess;
161 }
162