summaryrefslogtreecommitdiff
path: root/usb/device/composite/CDCDFunctionDriver.c
blob: 4e420156875c076f025e55ac57094a8a3c82a3c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//-----------------------------------------------------------------------------
//      Headers
//-----------------------------------------------------------------------------

// GENERAL
#include <utility/trace.h>
#include <utility/assert.h>
// USB
#include <usb/device/core/USBD.h>
#include <usb/device/core/USBDDriver.h>
// CDC
#include <usb/common/cdc/CDCGenericRequest.h>
#include <usb/common/cdc/CDCSetControlLineStateRequest.h>

#include "CDCDFunctionDriver.h"
#include "CDCDFunctionDriverDescriptors.h"

//-----------------------------------------------------------------------------
//         Defines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//         Types
//-----------------------------------------------------------------------------

/// CDC Function Driver Struct
typedef struct {
    USBDDriver     * pUsbdDriver;
    CDCDSerialPort * pCdcPorts;
    unsigned char numPorts;
} CDCFunDriver;

//-----------------------------------------------------------------------------
//         Internal variables
//-----------------------------------------------------------------------------

/// CDC Function Driver instance
static CDCFunDriver cdcFunDriver;

//-----------------------------------------------------------------------------
//         Internal functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/// Callback function which should be invoked after the data of a
/// SetLineCoding request has been retrieved. Sends a zero-length packet
/// to the host for acknowledging the request.
//-----------------------------------------------------------------------------
static void CDCD_SetLineCodingCallback()
{
    USBD_Write(0, 0, 0, 0, 0);
}

//-----------------------------------------------------------------------------
/// Return the port index that host send this request for.
//-----------------------------------------------------------------------------
static char CDCD_GetSerialPort(const USBGenericRequest *request)
{
    unsigned char i;
    for (i = 0; i < cdcFunDriver.numPorts; i ++) {
        if (request->wIndex == cdcFunDriver.pCdcPorts[i].interfaceNum + 1)
            return i;
    }
    return 0xFF;
}

//-----------------------------------------------------------------------------
/// Receives new line coding information from the USB host.
/// \param request Pointer to a USBGenericRequest instance.
//-----------------------------------------------------------------------------
static void CDCD_SetLineCoding(const USBGenericRequest *request)
{
    unsigned char serial;
    serial = CDCD_GetSerialPort(request);

    TRACE_INFO_WP("sLineCoding_%d ", serial);

    USBD_Read(0,
              (void *) &(cdcFunDriver.pCdcPorts[serial].lineCoding),
              sizeof(CDCLineCoding),
              (TransferCallback) CDCD_SetLineCodingCallback,
              0);
}

//-----------------------------------------------------------------------------
/// Sends the current line coding information to the host through Control
/// endpoint 0.
/// \param request Pointer to a USBGenericRequest instance.
//-----------------------------------------------------------------------------
static void CDCD_GetLineCoding(const USBGenericRequest *request)
{
    unsigned char serial;
    serial = CDCD_GetSerialPort(request);

    TRACE_INFO_WP("gLineCoding_%d ", serial);

    USBD_Write(0,
               (void *) &(cdcFunDriver.pCdcPorts[serial].lineCoding),
               sizeof(CDCLineCoding),
               0,
               0);
}

//-----------------------------------------------------------------------------
/// Changes the state of the serial driver according to the information
/// sent by the host via a SetControlLineState request, and acknowledges
/// the request with a zero-length packet.
/// \param request Pointer to a USBGenericRequest instance.
/// \param activateCarrier The active carrier state to set.
/// \param isDTEPresent The DTE status.
//-----------------------------------------------------------------------------
static void CDCD_SetControlLineState(const USBGenericRequest *request,
                                     unsigned char activateCarrier,
                                     unsigned char isDTEPresent)
{
    unsigned char serial;
    serial = CDCD_GetSerialPort(request);

    TRACE_INFO_WP(
              "sControlLineState_%d(%d, %d) ",
              serial,
              activateCarrier,
              isDTEPresent);

    cdcFunDriver.pCdcPorts[serial].isCarrierActivated = activateCarrier;
    USBD_Write(0, 0, 0, 0, 0);
}

//-----------------------------------------------------------------------------
//         Exported functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/// Initializes the USB device CDC serial function driver.
//-----------------------------------------------------------------------------
void CDCDFunctionDriver_Initialize(USBDDriver * pUsbdDriver,
                                   CDCDSerialPort * pCdcPorts,
                                   unsigned char numPorts)
{
    unsigned char serial;

    TRACE_INFO("CDCDFunctionDriver_Initialize\n\r");
    
    cdcFunDriver.pUsbdDriver = pUsbdDriver;
    cdcFunDriver.pCdcPorts   = pCdcPorts;
    cdcFunDriver.numPorts    = numPorts;

    for (serial = 0; serial < numPorts; serial ++) {

        CDCDSerialPort * pSerial = &cdcFunDriver.pCdcPorts[serial];

        // Initialize Abstract Control Model attributes
        CDCLineCoding_Initialize(&(pSerial->lineCoding),
                                 115200,
                                 CDCLineCoding_ONESTOPBIT,
                                 CDCLineCoding_NOPARITY,
                                 8);
        pSerial->isCarrierActivated = 0;
        pSerial->serialState = 0;
    }
}

//-----------------------------------------------------------------------------
/// Handles CDC/ACM-specific USB requests sent by the host
/// \param request Pointer to a USBGenericRequest instance.
/// \return 0 if the request is Unsupported, 1 if the request handled.
//-----------------------------------------------------------------------------
unsigned char CDCDFunctionDriver_RequestHandler(
    const USBGenericRequest *request)
{
    switch (USBGenericRequest_GetRequest(request)) {

        case CDCGenericRequest_SETLINECODING:
            
            CDCD_SetLineCoding(request);
            break;

        case CDCGenericRequest_GETLINECODING:

            CDCD_GetLineCoding(request);
            break;

        case CDCGenericRequest_SETCONTROLLINESTATE:

            CDCD_SetControlLineState(request,
                CDCSetControlLineStateRequest_ActivateCarrier(request),
                CDCSetControlLineStateRequest_IsDtePresent(request));

            break;

        // Unsupported request
        default:
            return 0;

    }
    return 1;
}

//-----------------------------------------------------------------------------
/// Receives data from the host through the virtual COM port created by
/// the CDC function serial driver. This function behaves like <USBD_Read>.
/// \param Port Port index to receive.
/// \param Pointer to the data buffer to send.
/// \param Size of the data buffer in bytes.
/// \param callback Optional callback function to invoke when the transfer
///        finishes.
/// \param argument Optional argument to the callback function.
/// \return <USBD_STATUS_SUCCESS> if the read operation started normally;
///         otherwise, the corresponding error code.
//-----------------------------------------------------------------------------
unsigned char CDCDSerialDriver_Read(unsigned char port,
                                    void *data,
                                    unsigned int size,
                                    TransferCallback callback,
                                    void *argument)
{
    unsigned char ep = cdcFunDriver.pCdcPorts[port].bulkOutEndpoint;
    
    if (port > cdcFunDriver.numPorts)
        return USBD_STATUS_INVALID_PARAMETER;

    return USBD_Read(ep,
                     data,
                     size,
                     callback,
                     argument);
}

//-----------------------------------------------------------------------------
/// Sends a data buffer through the virtual COM port created by the CDC
/// function serial driver. This function behaves exactly like <USBD_Write>.
/// \param port Port index to receive.
/// \param  data - Pointer to the data buffer to send.
/// \param  size - Size of the data buffer in bytes.
/// \param  callback - Optional callback function to invoke when the transfer
///         finishes.
/// \param  argument - Optional argument to the callback function.
/// \return <USBD_STATUS_SUCCESS> if the write operation started normally;
///         otherwise, the corresponding error code.
//-----------------------------------------------------------------------------
unsigned char CDCDSerialDriver_Write(unsigned char port,
                                     void *data,
                                     unsigned int size,
                                     TransferCallback callback,
                                     void *argument)
{
    unsigned char ep = cdcFunDriver.pCdcPorts[port].bulkInEndpoint;

    if (port > cdcFunDriver.numPorts)
        return USBD_STATUS_INVALID_PARAMETER;

    return USBD_Write(ep,
                      data,
                      size,
                      callback,
                      argument);
}

//------------------------------------------------------------------------------
/// Returns the current status of the RS-232 line.
/// \param port The port number that checked.
//------------------------------------------------------------------------------
unsigned short CDCDSerialDriver_GetSerialState(unsigned char port)
{
    if (port > cdcFunDriver.numPorts)
        return USBD_STATUS_INVALID_PARAMETER;

    return cdcFunDriver.pCdcPorts[port].serialState;
}

//------------------------------------------------------------------------------
/// Sets the current serial state of the device to the given value.
/// \param port The port number that the port state should be changed.
/// \param serialState  New device state.
//------------------------------------------------------------------------------
void CDCDSerialDriver_SetSerialState(unsigned char port,
                                     unsigned short serialState)
{
    CDCDSerialPort * pPort;
    unsigned char ep;

    ASSERT((serialState & 0xFF80) == 0,
           "CDCDSerialDriver_SetSerialState: Bits D7-D15 are reserved!\n\r");

    if (port > cdcFunDriver.numPorts)
        return;

    // If new state is different from previous one, send a notification to the
    // host
    pPort = &cdcFunDriver.pCdcPorts[port];
    ep = pPort->interruptInEndpoint;
    if (pPort->serialState != serialState) {

        pPort->serialState = serialState;
        USBD_Write(ep,
                   &(pPort->serialState),
                   2,
                   0,
                   0);

        // Reset one-time flags
        pPort->serialState &= ~(CDCD_STATE_OVERRUN
                              | CDCD_STATE_PARITY
                              | CDCD_STATE_FRAMING
                              | CDCD_STATE_RINGSIGNAL
                              | CDCD_STATE_BREAK);
    }
}
personal git repositories of Harald Welte. Your mileage may vary