+/rtos

git clone https://git.julia.blue/+/rtos

Joseph RyanRe-add usblib files83915e3

master
18.4 KiB514 linesraw
1//*****************************************************************************
2//
3// usbdconfig.c - High level USB device configuration function.
4//
5// Copyright (c) 2008-2017 Texas Instruments Incorporated.  All rights reserved.
6// Software License Agreement
7//
8// Texas Instruments (TI) is supplying this software for use solely and
9// exclusively on TI's microcontroller products. The software is owned by
10// TI and/or its suppliers, and is protected under applicable copyright
11// laws. You may not combine this software with "viral" open-source
12// software in order to form a larger program.
13//
14// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
15// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
16// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
18// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
19// DAMAGES, FOR ANY REASON WHATSOEVER.
20//
21// This is part of revision 2.1.4.178 of the Tiva USB Library.
22//
23//*****************************************************************************
24
25#include "tivaware/hw_memmap.h"
26#include "tivaware/hw_types.h"
27#include "tivaware/usb.h"
28#include "usblib/device/usbdevice.h"
29#include "usblib/device/usbdevicepriv.h"
30#include "usblib/usblib.h"
31#include "usblib/usblibpriv.h"
32#include <stdbool.h>
33#include <stdint.h>
34
35#define ASSERT(X)
36
37//*****************************************************************************
38//
39//! \addtogroup device_api
40//! @{
41//
42//*****************************************************************************
43
44//*****************************************************************************
45//
46// Structure used in compiling FIFO size and endpoint properties from a
47// configuration descriptor.
48//
49//*****************************************************************************
50typedef struct {
51    uint32_t pui32Size[2];
52} tUSBEndpointInfo;
53
54//*****************************************************************************
55//
56// Indices used when accessing the tUSBEndpointInfo structure.
57//
58//*****************************************************************************
59#define EP_INFO_IN 0
60#define EP_INFO_OUT 1
61
62//*****************************************************************************
63//
64// Given a maximum packet size and the user's FIFO scaling requirements,
65// determine the flags to use to configure the endpoint FIFO and the number
66// of bytes of FIFO space occupied.
67//
68//*****************************************************************************
69static uint32_t GetEndpointFIFOSize(uint32_t ui32MaxPktSize,
70                                    uint32_t* pupBytesUsed) {
71    uint32_t ui32Loop, ui32FIFOSize;
72
73    //
74    // Now we need to find the nearest supported size that accommodates the
75    // requested size.  Step through each of the supported sizes until we
76    // find one that will do.
77    //
78    for (ui32Loop = USB_FIFO_SZ_8; ui32Loop <= USB_FIFO_SZ_2048; ui32Loop++) {
79        //
80        // How many bytes does this FIFO value represent?
81        //
82        ui32FIFOSize = USBFIFOSizeToBytes(ui32Loop);
83
84        //
85        // Is this large enough to hold one packet.
86        //
87        if (ui32FIFOSize >= ui32MaxPktSize) {
88            //
89            // Return the FIFO size setting and the USB_FIFO_SZ_ value.
90            //
91            *pupBytesUsed = ui32FIFOSize;
92
93            return (ui32Loop);
94        }
95    }
96
97    //
98    // If we drop out, we can't support the FIFO size requested.  Signal a
99    // problem by returning 0 in the pBytesUsed
100    //
101    *pupBytesUsed = 0;
102
103    return (USB_FIFO_SZ_8);
104}
105
106//*****************************************************************************
107//
108// Translate a USB endpoint descriptor into the values we need to pass to the
109// USBDevEndpointConfigSet() API.
110//
111//*****************************************************************************
112static void GetEPDescriptorType(tEndpointDescriptor* psEndpoint,
113                                uint32_t* pui32EPIndex,
114                                uint32_t* pui32MaxPktSize,
115                                uint32_t* pui32Flags) {
116    //
117    // Get the endpoint index.
118    //
119    *pui32EPIndex = psEndpoint->bEndpointAddress & USB_EP_DESC_NUM_M;
120
121    //
122    // Extract the maximum packet size.
123    //
124    *pui32MaxPktSize = psEndpoint->wMaxPacketSize & USB_EP_MAX_PACKET_COUNT_M;
125
126    //
127    // Is this an IN or an OUT endpoint?
128    //
129    *pui32Flags = (psEndpoint->bEndpointAddress & USB_EP_DESC_IN)
130                      ? USB_EP_DEV_IN
131                      : USB_EP_DEV_OUT;
132
133    //
134    // Set the endpoint mode.
135    //
136    switch (psEndpoint->bmAttributes & USB_EP_ATTR_TYPE_M) {
137    case USB_EP_ATTR_CONTROL: {
138        *pui32Flags |= USB_EP_MODE_CTRL;
139        break;
140    }
141    case USB_EP_ATTR_BULK: {
142        *pui32Flags |= USB_EP_MODE_BULK;
143        break;
144    }
145    case USB_EP_ATTR_INT: {
146        *pui32Flags |= USB_EP_MODE_INT;
147        break;
148    }
149    case USB_EP_ATTR_ISOC: {
150        *pui32Flags |= USB_EP_MODE_ISOC;
151        break;
152    }
153    }
154}
155
156//*****************************************************************************
157//
158//! Configure the USB controller appropriately for the device whose
159//! configuration descriptor is passed.
160//!
161//! \param psDevInst is a pointer to the device instance being configured.
162//! \param psConfig is a pointer to the configuration descriptor that the
163//! USB controller is to be set up to support.
164//!
165//! This function may be used to initialize a USB controller to operate as
166//! the device whose configuration descriptor is passed.  The function
167//! enables the USB controller, partitions the FIFO appropriately and
168//! configures each endpoint required by the configuration.  If the supplied
169//! configuration supports multiple alternate settings for any interface,
170//! the USB FIFO is set up assuming the worst case use (largest packet size
171//! for a given endpoint in any alternate setting using that endpoint) to
172//! allow for on-the-fly alternate setting changes later.  On return from this
173//! function, the USB controller is configured for correct operation of
174//! the default configuration of the device described by the descriptor passed.
175//!
176//! \return Returns \b true on success or \b false on failure.
177//
178//*****************************************************************************
179bool USBDeviceConfig(tDCDInstance* psDevInst, const tConfigHeader* psConfig) {
180    uint32_t ui32Loop, ui32Count, ui32NumInterfaces, ui32EpIndex, ui32EpType,
181        ui32MaxPkt, ui32NumEndpoints, ui32Flags, ui32BytesUsed, ui32Section;
182    tInterfaceDescriptor* psInterface;
183    tEndpointDescriptor* psEndpoint;
184    tUSBEndpointInfo psEPInfo[NUM_USB_EP - 1];
185
186    //
187    // A valid device instance is required.
188    //
189    ASSERT(psDevInst != 0);
190
191    //
192    // Catch bad pointers in a debug build.
193    //
194    ASSERT(psConfig);
195
196    //
197    // Clear out our endpoint info.
198    //
199    for (ui32Loop = 0; ui32Loop < (NUM_USB_EP - 1); ui32Loop++) {
200        psEPInfo[ui32Loop].pui32Size[EP_INFO_IN] = 0;
201        psEPInfo[ui32Loop].pui32Size[EP_INFO_OUT] = 0;
202    }
203
204    //
205    // How many (total) endpoints does this configuration describe?
206    //
207    ui32NumEndpoints = USBDCDConfigDescGetNum(psConfig, USB_DTYPE_ENDPOINT);
208
209    //
210    // How many interfaces are included?
211    //
212    ui32NumInterfaces = USBDCDConfigDescGetNum(psConfig, USB_DTYPE_INTERFACE);
213
214    //
215    // Look at each endpoint and determine the largest max packet size for
216    // each endpoint.  This will determine how we partition the USB FIFO.
217    //
218    for (ui32Loop = 0; ui32Loop < ui32NumEndpoints; ui32Loop++) {
219        //
220        // Get a pointer to the endpoint descriptor.
221        //
222        psEndpoint = (tEndpointDescriptor*)USBDCDConfigDescGet(
223            psConfig, USB_DTYPE_ENDPOINT, ui32Loop, &ui32Section);
224
225        //
226        // Extract the endpoint number and whether it is an IN or OUT
227        // endpoint.
228        //
229        ui32EpIndex =
230            (uint32_t)psEndpoint->bEndpointAddress & USB_EP_DESC_NUM_M;
231        ui32EpType = (psEndpoint->bEndpointAddress & USB_EP_DESC_IN)
232                         ? EP_INFO_IN
233                         : EP_INFO_OUT;
234
235        //
236        // Make sure the endpoint number is valid for our controller.  If not,
237        // return false to indicate an error.  Note that 0 is invalid since
238        // you shouldn't reference endpoint 0 in the config descriptor.
239        //
240        if ((ui32EpIndex >= NUM_USB_EP) || (ui32EpIndex == 0)) {
241            return (false);
242        }
243
244        //
245        // Does this endpoint have a max packet size requirement larger than
246        // any previous use we have seen?
247        //
248        if (psEndpoint->wMaxPacketSize >
249            psEPInfo[ui32EpIndex - 1].pui32Size[ui32EpType]) {
250            //
251            // Yes - remember the new maximum packet size.
252            //
253            psEPInfo[ui32EpIndex - 1].pui32Size[ui32EpType] =
254                psEndpoint->wMaxPacketSize;
255        }
256    }
257
258    //
259    // At this point, we have determined the maximum packet size required
260    // for each endpoint by any possible alternate setting of any interface
261    // in this configuration.  Now determine the endpoint settings required
262    // for the interface setting we are actually going to use.
263    //
264    for (ui32Loop = 0; ui32Loop < ui32NumInterfaces; ui32Loop++) {
265        //
266        // Get the next interface descriptor in the configuration descriptor.
267        //
268        psInterface = USBDCDConfigGetInterface(psConfig, ui32Loop, USB_DESC_ANY,
269                                               &ui32Section);
270
271        //
272        // Is this the default interface (bAlternateSetting set to 0)?
273        //
274        if (psInterface && (psInterface->bAlternateSetting == 0)) {
275            //
276            // This is an interface we are interested in so gather the
277            // information on its endpoints.
278            //
279            ui32NumEndpoints = (uint32_t)psInterface->bNumEndpoints;
280
281            //
282            // Walk through each endpoint in this interface and configure
283            // it appropriately.
284            //
285            for (ui32Count = 0; ui32Count < ui32NumEndpoints; ui32Count++) {
286                //
287                // Get a pointer to the endpoint descriptor.
288                //
289                psEndpoint = USBDCDConfigGetInterfaceEndpoint(
290                    psConfig, psInterface->bInterfaceNumber,
291                    psInterface->bAlternateSetting, ui32Count);
292
293                //
294                // Make sure we got a good pointer.
295                //
296                if (psEndpoint) {
297                    //
298                    // Determine maximum packet size and flags from the
299                    // endpoint descriptor.
300                    //
301                    GetEPDescriptorType(psEndpoint, &ui32EpIndex, &ui32MaxPkt,
302                                        &ui32Flags);
303
304                    //
305                    // Make sure no-one is trying to configure endpoint 0.
306                    //
307                    if (!ui32EpIndex) {
308                        return (false);
309                    }
310
311                    //
312                    // Set the endpoint configuration.
313                    //
314                    USBDevEndpointConfigSet(USB0_BASE,
315                                            IndexToUSBEP(ui32EpIndex),
316                                            ui32MaxPkt, ui32Flags);
317                }
318            }
319        }
320    }
321
322    //
323    // At this point, we have configured all the endpoints that are to be
324    // used by this configuration's alternate setting 0.  Now we go on and
325    // partition the FIFO based on the maximum packet size information we
326    // extracted earlier.  Endpoint 0 is automatically configured to use the
327    // first MAX_PACKET_SIZE_EP0 bytes of the FIFO so we start from there.
328    //
329    ui32Count = MAX_PACKET_SIZE_EP0;
330    for (ui32Loop = 1; ui32Loop < NUM_USB_EP; ui32Loop++) {
331        //
332        // Configure the IN endpoint at this index if it is referred to
333        // anywhere.
334        //
335        if (psEPInfo[ui32Loop - 1].pui32Size[EP_INFO_IN]) {
336            //
337            // What FIFO size flag do we use for this endpoint?
338            //
339            ui32MaxPkt = GetEndpointFIFOSize(
340                psEPInfo[ui32Loop - 1].pui32Size[EP_INFO_IN], &ui32BytesUsed);
341
342            //
343            // The FIFO space could not be allocated.
344            //
345            if (ui32BytesUsed == 0) {
346                return (false);
347            }
348
349            //
350            // Now actually configure the FIFO for this endpoint.
351            //
352            USBFIFOConfigSet(USB0_BASE, IndexToUSBEP(ui32Loop), ui32Count,
353                             ui32MaxPkt, USB_EP_DEV_IN);
354            ui32Count += ui32BytesUsed;
355        }
356
357        //
358        // Configure the OUT endpoint at this index.
359        //
360        if (psEPInfo[ui32Loop - 1].pui32Size[EP_INFO_OUT]) {
361            //
362            // What FIFO size flag do we use for this endpoint?
363            //
364            ui32MaxPkt = GetEndpointFIFOSize(
365                psEPInfo[ui32Loop - 1].pui32Size[EP_INFO_OUT], &ui32BytesUsed);
366
367            //
368            // The FIFO space could not be allocated.
369            //
370            if (ui32BytesUsed == 0) {
371                return (false);
372            }
373
374            //
375            // Now actually configure the FIFO for this endpoint.
376            //
377            USBFIFOConfigSet(USB0_BASE, IndexToUSBEP(ui32Loop), ui32Count,
378                             ui32MaxPkt, USB_EP_DEV_OUT);
379            ui32Count += ui32BytesUsed;
380        }
381    }
382
383    //
384    // If we get to the end, all is well.
385    //
386    return (true);
387}
388
389//*****************************************************************************
390//
391//! Configure the affected USB endpoints appropriately for one alternate
392//! interface setting.
393//!
394//! \param psDevInst is a pointer to the device instance being configured.
395//! \param psConfig is a pointer to the configuration descriptor that contains
396//! the interface whose alternate settings is to be configured.
397//! \param ui8InterfaceNum is the number of the interface whose alternate
398//! setting is to be configured.  This number corresponds to the
399//! bInterfaceNumber field in the desired interface descriptor.
400//! \param ui8AlternateSetting is the alternate setting number for the desired
401//! interface.  This number corresponds to the bAlternateSetting field in the
402//! desired interface descriptor.
403//!
404//! This function may be used to reconfigure the endpoints of an interface
405//! for operation in one of the interface's alternate settings.  Note that this
406//! function assumes that the endpoint FIFO settings will not need to change
407//! and only the endpoint mode is changed.  This assumption is valid if the
408//! USB controller was initialized using a previous call to USBDCDConfig().
409//!
410//! In reconfiguring the interface endpoints, any additional configuration
411//! bits set in the endpoint configuration other than the direction (\b
412//! USB_EP_DEV_IN or \b USB_EP_DEV_OUT) and mode (\b USB_EP_MODE_MASK) are
413//! preserved.
414//!
415//! \return Returns \b true on success or \b false on failure.
416//
417//*****************************************************************************
418bool USBDeviceConfigAlternate(tDCDInstance* psDevInst,
419                              const tConfigHeader* psConfig,
420                              uint8_t ui8InterfaceNum,
421                              uint8_t ui8AlternateSetting) {
422    uint32_t ui32NumInterfaces, ui32NumEndpoints, ui32Loop, ui32Count,
423        ui32MaxPkt, ui32Flags, ui32Section, ui32EpIndex;
424    tInterfaceDescriptor* psInterface;
425    tEndpointDescriptor* psEndpoint;
426
427    //
428    // How many interfaces are included in the descriptor?
429    //
430    ui32NumInterfaces = USBDCDConfigDescGetNum(psConfig, USB_DTYPE_INTERFACE);
431
432    //
433    // Find the interface descriptor for the supplied interface and alternate
434    // setting numbers.
435    //
436
437    for (ui32Loop = 0; ui32Loop < ui32NumInterfaces; ui32Loop++) {
438        //
439        // Get the next interface descriptor in the configuration descriptor.
440        //
441        psInterface = USBDCDConfigGetInterface(psConfig, ui32Loop, USB_DESC_ANY,
442                                               &ui32Section);
443
444        //
445        // Is this the default interface (bAlternateSetting set to 0)?
446        //
447        if (psInterface && (psInterface->bInterfaceNumber == ui8InterfaceNum) &&
448            (psInterface->bAlternateSetting == ui8AlternateSetting)) {
449            //
450            // This is an interface we are interested in and the descriptor
451            // representing the alternate setting we want so go ahead and
452            // reconfigure the endpoints.
453            //
454
455            //
456            // How many endpoints does this interface have?
457            //
458            ui32NumEndpoints = (uint32_t)psInterface->bNumEndpoints;
459
460            //
461            // Walk through each endpoint in turn.
462            //
463            for (ui32Count = 0; ui32Count < ui32NumEndpoints; ui32Count++) {
464                //
465                // Get a pointer to the endpoint descriptor.
466                //
467                psEndpoint = USBDCDConfigGetInterfaceEndpoint(
468                    psConfig, psInterface->bInterfaceNumber,
469                    psInterface->bAlternateSetting, ui32Count);
470
471                //
472                // Make sure we got a good pointer.
473                //
474                if (psEndpoint) {
475                    //
476                    // Determine maximum packet size and flags from the
477                    // endpoint descriptor.
478                    //
479                    GetEPDescriptorType(psEndpoint, &ui32EpIndex, &ui32MaxPkt,
480                                        &ui32Flags);
481
482                    //
483                    // Make sure no-one is trying to configure endpoint 0.
484                    //
485                    if (!ui32EpIndex) {
486                        return (false);
487                    }
488
489                    //
490                    // Set the endpoint configuration.
491                    //
492                    USBDevEndpointConfigSet(USB0_BASE,
493                                            IndexToUSBEP(ui32EpIndex),
494                                            ui32MaxPkt, ui32Flags);
495                }
496            }
497
498            //
499            // At this point, we have reconfigured the desired interface so
500            // return indicating all is well.
501            //
502            return (true);
503        }
504    }
505
506    return (false);
507}
508
509//*****************************************************************************
510//
511// Close the Doxygen group.
512//! @}
513//
514//*****************************************************************************