STM32: USB: always check host expected size
[bertos.git] / bertos / cpu / cortex-m3 / drv / usb_stm32.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief STM32 USB driver
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include "cfg/cfg_usb.h"
39
40 #define LOG_LEVEL  USB_LOG_LEVEL
41 #define LOG_FORMAT USB_LOG_FORMAT
42
43 #include <cfg/log.h>
44 #include <cfg/debug.h>
45 #include <cfg/macros.h>
46 #include <cfg/module.h>
47
48 #include <cpu/irq.h>
49 #include <cpu/power.h>
50
51 #include <drv/irq_cm3.h>
52 #include <drv/gpio_stm32.h>
53 #include <drv/clock_stm32.h>
54 #include <drv/timer.h>
55 #include <drv/usb.h>
56
57 #include <string.h> /* memcpy() */
58
59 #include "usb_stm32.h"
60
61 #define ALIGN_UP(value, align)  (((value) & ((align) - 1)) ? \
62                                 (((value) + ((align) - 1)) & ~((align) - 1)) : \
63                                 (value))
64 /* STM32 USB registers */
65 struct stm32_usb
66 {
67         reg32_t EP0R;
68         reg32_t EP1R;
69         reg32_t EP2R;
70         reg32_t EP3R;
71         reg32_t EP4R;
72         reg32_t EP5R;
73         reg32_t EP6R;
74         reg32_t EP7R;
75         reg32_t __reserved[8];
76         reg32_t CNTR;
77         reg32_t ISTR;
78         reg32_t FNR;
79         reg32_t DADDR;
80         reg32_t BTABLE;
81 };
82
83 /* Hardware registers */
84 static struct stm32_usb *usb = (struct stm32_usb *)USB_BASE_ADDR;
85
86 /* Endpoint descriptors: used for handling requests to use with endpoints */
87 static stm32_usb_ep_t ep_cnfg[ENP_MAX_NUMB];
88
89 /* USB EP0 control descriptor */
90 static const usb_endpoint_descriptor_t USB_CtrlEpDescr0 =
91 {
92         .bLength = sizeof(USB_CtrlEpDescr0),
93         .bDescriptorType = USB_DT_ENDPOINT,
94         .bEndpointAddress = USB_DIR_OUT | 0,
95         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
96         .wMaxPacketSize = USB_EP0_MAX_SIZE,
97         .bInterval = 0,
98 };
99
100 /* USB EP1 control descriptor */
101 static const usb_endpoint_descriptor_t USB_CtrlEpDescr1 =
102 {
103         .bLength = sizeof(USB_CtrlEpDescr1),
104         .bDescriptorType = USB_DT_ENDPOINT,
105         .bEndpointAddress = USB_DIR_IN | 0,
106         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
107         .wMaxPacketSize = USB_EP0_MAX_SIZE,
108         .bInterval = 0,
109 };
110
111 /* USB setup packet */
112 static usb_ctrlrequest_t setup_packet;
113
114 /* USB device controller: max supported interfaces */
115 #define USB_MAX_INTERFACE       1
116
117 /* USB device controller features */
118 #define STM32_UDC_FEATURE_SELFPOWERED   BV(0)
119 #define STM32_UDC_FEATURE_REMOTE_WAKEUP BV(1)
120
121 /* Hardware-specific USB device controller structure */
122 typedef struct stm32_udc
123 {
124         uint8_t state;
125         uint32_t cfg_id;
126         const usb_config_descriptor_t *cfg;
127         uint32_t interfaces;
128         uint32_t alt[USB_MAX_INTERFACE];
129         uint32_t address;
130         uint8_t feature;
131 } PACKED stm32_udc_t;
132
133 /* Hardware-specific USB Device Controller */
134 static stm32_udc_t udc;
135
136 /* Generic USB Device Controller structure */
137 static struct usb_device *usb_dev;
138
139 /* USB packet memory management: list of allocated chunks */
140 static pack_mem_slot_t *pPacketMemUse;
141
142 /* USB packet memory management: memory buffer metadata */
143 #define EP_MAX_SLOTS    16
144 static pack_mem_slot_t PacketMemBuff[EP_MAX_SLOTS];
145
146 /* Allocate a free block of the packet memory */
147 static pack_mem_slot_t *usb_malloc(void)
148 {
149         unsigned int i;
150
151         for (i = 0; i < countof(PacketMemBuff); i++)
152                 if (PacketMemBuff[i].Size == 0)
153                         return &PacketMemBuff[i];
154         return NULL;
155 }
156
157 /* Release a block of the packet memory */
158 static void usb_free(pack_mem_slot_t *pPntr)
159 {
160         pPntr->Size = 0;
161 }
162
163 /* Allocate a free chunk of the packet memory (inside a block) */
164 static bool USB_AllocateBuffer(uint16_t *pOffset, uint32_t *pPacketSize,
165                             int EndPoint)
166 {
167         pack_mem_slot_t *pPacketMem = pPacketMemUse,
168                         *pPacketMemNext, *pPacketMemUseNew;
169         uint32_t MaxPacketSize = *pPacketSize;
170
171         /*
172          * Packet size alignment:
173          *  - fine-granularity allocation: size alignment by 2;
174          *  - coarse-granularity allocation: size alignment by 32.
175          */
176         if (MaxPacketSize < 62)
177                 MaxPacketSize = ALIGN_UP(MaxPacketSize, 2);
178         else
179                 MaxPacketSize = ALIGN_UP(MaxPacketSize, 32);
180         /*
181          * Finding free memory chunks from the allocated blocks of the USB
182          * packet memory.
183          */
184         *pOffset = 0;
185         while (pPacketMem != NULL)
186         {
187                 /* Offset alignment by 4 */
188                 *pOffset = ALIGN_UP(pPacketMem->Start + pPacketMem->Size, 4);
189                 pPacketMemNext = pPacketMem->next;
190                 if ((pPacketMem->next == NULL) ||
191                                 (pPacketMemNext->Start >=
192                                         *pOffset + MaxPacketSize))
193                         break;
194                 pPacketMem = pPacketMem->next;
195         }
196         /* Check for out-of-memory condition */
197         if ((*pOffset + MaxPacketSize) >= USB_BDT_OFFSET)
198                 return false;
199         /*
200          * Allocate a new memory block, next to the last allocated block.
201          */
202         pPacketMemUseNew = usb_malloc();
203         if (pPacketMemUseNew == NULL)
204                 return false;
205         /* Insert the block to the list of allocated blocks */
206         if (pPacketMemUse == NULL)
207         {
208                 pPacketMemUse = pPacketMemUseNew;
209                 pPacketMemUse->next = NULL;
210         }
211         else
212         {
213                 pPacketMemUseNew->next = pPacketMem->next;
214                 pPacketMem->next = pPacketMemUseNew;
215         }
216         /* Update block's metadata */
217         pPacketMemUseNew->ep_addr = EndPoint;
218         pPacketMemUseNew->Start = *pOffset;
219         pPacketMemUseNew->Size = MaxPacketSize;
220
221         *pPacketSize = MaxPacketSize;
222
223         return true;
224 }
225
226 /* Release a chunk of the packet memory (inside a block) */
227 static void USB_ReleaseBuffer(int EndPoint)
228 {
229         pack_mem_slot_t *pPacketMem, *pPacketMemPrev = NULL;
230         pPacketMem = pPacketMemUse;
231
232         while (pPacketMem != NULL)
233         {
234                 if (pPacketMem->ep_addr == EndPoint)
235                 {
236                         if (UNLIKELY(pPacketMemPrev == NULL))
237                         {
238                                 /* Free the first element of the list */
239                                 pPacketMemUse = pPacketMemUse->next;
240                                 usb_free(pPacketMem);
241                                 pPacketMem = pPacketMemUse;
242                                 continue;
243                         }
244                         pPacketMemPrev->next = pPacketMem->next;
245                         usb_free(pPacketMem);
246                 }
247                 else
248                         pPacketMemPrev = pPacketMem;
249                 pPacketMem = pPacketMemPrev->next;
250         }
251 }
252
253 /*-------------------------------------------------------------------------*/
254
255 /* Connect USB controller */
256 static void usb_connect(void)
257 {
258         stm32_gpioPinWrite((struct stm32_gpio *)GPIOC_BASE, 1 << 11, 0);
259 }
260
261 /* Set USB device address */
262 static void usb_set_address(uint32_t addr)
263 {
264         usb->DADDR = addr | 0x80;
265 }
266
267 /* Suspend USB controller */
268 static void usb_suspend(void)
269 {
270         usb->CNTR |= bmFSUSP | bmLPMODE;
271 }
272
273 /* Resume USB controller */
274 static void usb_resume(void)
275 {
276         uint32_t line_status;
277
278         line_status = usb->FNR & 0xc000;
279         if (!line_status)
280                 return;
281         /* check for noise and eventually return to sleep */
282         if (line_status == 0xc000)
283                 usb_suspend();
284         else
285                 usb->CNTR &= ~(bmFSUSP | bmLPMODE);
286 }
287
288 /* Convert logical EP address to physical EP address */
289 static int USB_EpLogToPhysAdd(uint8_t ep_addr)
290 {
291         int addr = (ep_addr & 0x0f) << 1;
292         return (ep_addr & 0x80) ? addr + 1 : addr;
293 }
294
295 /* Set EP address */
296 static void EpCtrlSet_EA(reg32_t *reg, uint32_t val)
297 {
298         val &= 0x0f;
299         val |= *reg & 0x0700;
300         val |= USB_CTRL_CLEAR_ONLY_MASK;
301         *reg = val;
302 }
303
304 /* Get EP IN status */
305 static uint32_t EpCtrlGet_STAT_TX(reg32_t *reg)
306 {
307         return (*reg & (0x3UL << 4)) >> 4;
308 }
309
310 /* Set EP IN state */
311 static void EpCtrlSet_STAT_TX(reg32_t *reg, ep_state_t val)
312 {
313         uint32_t state;
314         int i;
315
316         /*
317          * The EP can change state between read and write operations from VALID
318          * to NAK and result of set operation will be invalid.
319          */
320         for (i = 0; i < 2; i++)
321         {
322                 if (EpCtrlGet_STAT_TX(reg) == val)
323                         return;
324                 state = val;
325                 state <<= 4;
326                 state ^= *reg;
327                 state |= USB_CTRL_CLEAR_ONLY_MASK;
328                 /* Clear the toggle bits without STAT_TX (4,5) */
329                 state &= ~0x7040;
330                 *reg = state;
331         }
332 }
333
334 /* Set EP DTOG_TX bit (IN) */
335 static void EpCtrlSet_DTOG_TX(reg32_t *reg, uint32_t val)
336 {
337         val = val ? (*reg ^ (1UL << 6)) : *reg;
338         /* Clear the toggle bits without DTOG_TX (6) */
339         val &= ~0x7030;
340         val |= USB_CTRL_CLEAR_ONLY_MASK;
341         *reg = val;
342 }
343
344 /* Clear EP CTR_TX bit (IN) */
345 static void EpCtrlClr_CTR_TX(reg32_t *reg)
346 {
347         uint32_t val = *reg;
348
349         val &= ~(USB_CTRL_TOGGLE_MASK | 1UL << 7);
350         /* Set RX_CTR */
351         val |= 1UL << 15;
352         *reg = val;
353 }
354
355 /* Clear EP CTR_RX bit (OUT) */
356 static void EpCtrlClr_CTR_RX(reg32_t *reg)
357 {
358         uint32_t val = *reg;
359         val &= ~(USB_CTRL_TOGGLE_MASK | 1UL << 15);
360         /* Set TX_CTR */
361         val |= 1UL << 7;
362         *reg = val;
363 }
364
365 /* Set EP KIND bit */
366 static void EpCtrlSet_EP_KIND(reg32_t *reg, uint32_t val)
367 {
368         val = val ? (1UL << 8) : 0;
369         val |= *reg & ~(USB_CTRL_TOGGLE_MASK | (1UL << 8));
370         val |= USB_CTRL_CLEAR_ONLY_MASK;
371         *reg = val;
372 }
373
374 /* Set EP type */
375 static int EpCtrlSet_EP_TYPE(reg32_t *reg, uint8_t val)
376 {
377         uint32_t type;
378
379         if (UNLIKELY(val >= EP_TYPE_MAX))
380         {
381                 ASSERT(0);
382                 return USB_INVAL_ERROR;
383         }
384         type = val;
385         type <<= 9;
386         type |= *reg & ~(USB_CTRL_TOGGLE_MASK | (0x3UL << 9));
387         type |= USB_CTRL_CLEAR_ONLY_MASK;
388         *reg = type;
389
390         return USB_OK;
391 }
392
393 /* Get EP STAT_RX (OUT) */
394 static uint32_t EpCtrlGet_STAT_RX(reg32_t *reg)
395 {
396         uint32_t val = *reg & (0x3UL << 12);
397         return val >> 12;
398 }
399
400 /* Set EP STAT_RX (OUT) */
401 static void EpCtrlSet_STAT_RX(reg32_t *reg, ep_state_t val)
402 {
403         uint32_t state;
404         int i;
405
406         /*
407          * The EP can change state between read and write operations from VALID
408          * to NAK and result of set operation will be invalid.
409          */
410         for (i = 0; i < 2; i++)
411         {
412                 if (EpCtrlGet_STAT_RX(reg) == val)
413                         return;
414                 state = val;
415                 state <<= 12;
416                 state ^= *reg;
417                 state |= USB_CTRL_CLEAR_ONLY_MASK;
418                 /* Clear the toggle bits without STAT_RX (12,13) */
419                 state &= ~0x4070;
420                 *reg = state;
421         }
422 }
423
424 /* Set DTOG_RX bit */
425 static void EpCtrlSet_DTOG_RX(reg32_t *reg, uint32_t val)
426 {
427         val = val ? (*reg ^ (1UL << 14)) : *reg;
428         /* Clear the toggle bits without DTOG_RX (14) */
429         val &= ~0x3070;
430         val |= USB_CTRL_CLEAR_ONLY_MASK;
431         *reg = val;
432 }
433
434 /* Get EP SETUP bit */
435 static uint32_t EpCtrlGet_SETUP(reg32_t *reg)
436 {
437         uint32_t val = *reg & (1UL << 11);
438         return val ? 1 : 0;
439 }
440
441 /* Core endpoint I/O function */
442 static void __usb_ep_io(int EP)
443 {
444         ssize_t Count, CountHold, Offset;
445         uint32_t *pDst, *pSrc, Data;
446         bool CurrentBuffer;
447         stm32_usb_ep_t *epd = &ep_cnfg[EP];
448
449         if (UNLIKELY(epd->hw == NULL))
450         {
451                 LOG_ERR("%s: invalid endpoint (EP%d-%s)\n",
452                                 __func__,
453                                 EP >> 1,
454                                 (EP & 0x01) ? "IN" : "OUT");
455                 ASSERT(0);
456                 return;
457         }
458         if (epd->status != BEGIN_SERVICED && epd->status != NO_SERVICED)
459                 return;
460
461         if (EP & 0x01)
462         {
463                 /* EP IN */
464                 Count = epd->size - epd->offset;
465                 while (epd->avail_data)
466                 {
467                         if (!Count && !(epd->flags & STM32_USB_EP_ZERO_PACKET))
468                                 break;
469
470                         /* Set Status */
471                         epd->status = BEGIN_SERVICED;
472                         /* Get data size */
473                         if ((epd->flags & STM32_USB_EP_ZERO_PACKET) &&
474                                         (Count == epd->max_size))
475                                 epd->flags |= STM32_USB_EP_ZERO_PACKET |
476                                                 STM32_USB_EP_ZERO_POSSIBLE;
477
478                         CountHold = Count = MIN(Count, epd->max_size);
479                         if (!Count)
480                                 epd->flags |= STM32_USB_EP_ZERO_PACKET;
481                         Offset = epd->offset;
482                         epd->offset += Count;
483                         CurrentBuffer = true;
484                         switch (epd->type)
485                         {
486                         case USB_ENDPOINT_XFER_CONTROL:
487                         case USB_ENDPOINT_XFER_INT:
488                                 pDst = (uint32_t *)addr2usbmem(ReadEpDTB_AddrTx(EP >> 1));
489                                 break;
490                         case USB_ENDPOINT_XFER_BULK:
491                                 pDst = (uint32_t *)addr2usbmem(ReadEpDTB_AddrTx(EP >> 1));
492                                 break;
493                         case USB_ENDPOINT_XFER_ISOC:
494                                 LOG_ERR("%s: isochronous transfer not supported\n",
495                                         __func__);
496                                 /* Fallback to default */
497                         default:
498                                 ASSERT(0);
499                                 return;
500                         }
501
502                         /* Write data to packet memory buffer */
503                         while (Count)
504                         {
505                                 Data = *(epd->write_buffer + Offset++);
506                                 if (--Count)
507                                 {
508                                         Data |= (uint32_t)(*(epd->write_buffer + Offset++)) << 8;
509                                         --Count;
510                                 }
511                                 *pDst++ = Data;
512                         }
513
514                         if (CurrentBuffer)
515                                 WriteEpDTB_CountTx(EP >> 1, CountHold);
516                         else
517                                 WriteEpDTB_CountRx(EP >> 1, CountHold);
518
519                         EpCtrlSet_STAT_TX(epd->hw, EP_VALID);
520
521                         --ep_cnfg[EP].avail_data;
522                         Count = epd->size - epd->offset;
523                 }
524                 if (!Count && !(epd->flags & STM32_USB_EP_ZERO_PACKET))
525                 {
526                         epd->status = COMPLETE;
527                         /* call callback function */
528                         if (epd->complete)
529                                 epd->complete(EP);
530                 }
531         }
532         else
533         {
534                 /* EP OUT */
535                 while (epd->avail_data)
536                 {
537                         /* Get data size and buffer pointer */
538                         switch (epd->type)
539                         {
540                         case USB_ENDPOINT_XFER_CONTROL:
541                         case USB_ENDPOINT_XFER_INT:
542                                 /* Get received bytes number */
543                                 Count = ReadEpDTB_CountRx(EP >> 1) & 0x3FF;
544                                 /* Get address of the USB packet buffer for corresponding EP */
545                                 pSrc = (uint32_t *)addr2usbmem(ReadEpDTB_AddrRx(EP >> 1));
546                                 break;
547                         case USB_ENDPOINT_XFER_BULK:
548                                 /* Get received bytes number */
549                                 Count = ReadEpDTB_CountRx(EP >> 1) & 0x3FF;
550                                 /* Get address of the USB packet buffer for corresponding EP */
551                                 pSrc = (uint32_t *)addr2usbmem(ReadEpDTB_AddrRx(EP >> 1));
552                                 break;
553                         case USB_ENDPOINT_XFER_ISOC:
554                                 LOG_ERR("%s: isochronous transfer not supported\n",
555                                         __func__);
556                                 /* Fallback to default */
557                         default:
558                                 ASSERT(0);
559                                 return;
560                         }
561
562                         if (Count > (epd->size - epd->offset))
563                         {
564                                 epd->status = BUFFER_OVERRUN;
565                                 epd->size = ep_cnfg[EP].offset;
566                                 break;
567                         }
568                         else if (Count < ep_cnfg[EP].max_size)
569                         {
570                                 epd->status = BUFFER_UNDERRUN;
571                                 epd->size = ep_cnfg[EP].offset + Count;
572                         }
573                         else
574                                 epd->status = BEGIN_SERVICED;
575
576                         Offset = epd->offset;
577                         epd->offset += Count;
578
579                         /* Read data from packet memory buffer */
580                         while (Count)
581                         {
582                                 Data = *pSrc++;
583                                 *(epd->read_buffer + Offset++) = Data;
584                                 if (--Count)
585                                 {
586                                         Data >>= 8;
587                                         *(epd->read_buffer + Offset++) = Data;
588                                         --Count;
589                                 }
590                         }
591
592                         EpCtrlSet_STAT_RX(epd->hw, EP_VALID);
593
594                         --ep_cnfg[EP].avail_data;
595
596                         if (*epd->hw & (1UL << 11))
597                         {
598                                 ep_cnfg[EP].status = SETUP_OVERWRITE;
599                                 return;
600                         }
601                         if (!(Count = (epd->size - epd->offset)))
602                         {
603                                 epd->status = COMPLETE;
604                                 break;
605                         }
606                 }
607                 if (epd->status != BEGIN_SERVICED && epd->status != NO_SERVICED)
608                 {
609                         /* call callback function */
610                         if (epd->complete)
611                                 epd->complete(EP);
612                 }
613         }
614 }
615
616 /*
617  * Return the lower value from Host expected size and size and set a flag
618  * STM32_USB_EP_ZERO_POSSIBLE when size is lower that host expected size.
619  */
620 static size_t usb_size(size_t size, size_t host_size)
621 {
622         if (size < host_size)
623         {
624                 ep_cnfg[CTRL_ENP_IN].flags |= STM32_USB_EP_ZERO_POSSIBLE;
625                 return size;
626         }
627         return host_size;
628 }
629
630 /* Configure an EP descriptor before performing a I/O operation */
631 #define USB_EP_IO(__EP, __op, __buf, __size, __complete)                \
632 ({                                                                      \
633         cpu_flags_t flags;                                              \
634         stm32_usb_io_status_t ret;                                      \
635                                                                         \
636         /* Fill EP descriptor */                                        \
637         IRQ_SAVE_DISABLE(flags);                                        \
638         if (__size < 0)                                                 \
639         {                                                               \
640                 ep_cnfg[__EP].status = NOT_READY;                       \
641                 ep_cnfg[__EP].complete = NULL;                          \
642                 ret = NOT_READY;                                        \
643                 goto out;                                               \
644         }                                                               \
645         if (ep_cnfg[__EP].status == BEGIN_SERVICED)                     \
646         {                                                               \
647                 ret = NOT_READY;                                        \
648                 goto out;                                               \
649         }                                                               \
650         /*                                                              \
651          * NOTE: the write_buffer and read_buffer are actually the      \
652          * same location in memory (it's a union).                      \
653          *                                                              \
654          * We have to do this trick to silent a build warning by        \
655          * casting the I/O buffer to (void *) or (const void *).        \
656          */                                                             \
657         ep_cnfg[__EP].__op ## _buffer = __buf;                          \
658         ep_cnfg[__EP].offset = 0;                                       \
659         ep_cnfg[__EP].size = __size;                                    \
660         ep_cnfg[__EP].complete = __complete;                            \
661         if (!size)                                                      \
662                 ep_cnfg[__EP].flags = STM32_USB_EP_ZERO_PACKET;         \
663         else                                                            \
664                 ep_cnfg[__EP].flags = 0;                                \
665         ep_cnfg[__EP].status = NO_SERVICED;                             \
666                                                                         \
667         /* Perform the I/O operation */                                 \
668         __usb_ep_io(__EP);                                              \
669                                                                         \
670         ret = ep_cnfg[__EP].status;                                     \
671 out:                                                                    \
672         IRQ_RESTORE(flags);                                             \
673         ret;                                                            \
674 })
675
676 /* Configure and endponint and perform a read operation */
677 static stm32_usb_io_status_t
678 __usb_ep_read(int ep, void *buffer, ssize_t size, void (*complete)(int))
679 {
680         if (UNLIKELY(ep >= ENP_MAX_NUMB))
681         {
682                 ASSERT(0);
683                 return STALLED;
684         }
685         ASSERT(!(ep & 0x01));
686         return USB_EP_IO(ep, read, buffer, size, complete);
687 }
688
689 /* Configure and endponint and perform a write operation */
690 static stm32_usb_io_status_t
691 __usb_ep_write(int ep, const void *buffer, ssize_t size, void (*complete)(int))
692 {
693         if (UNLIKELY(ep >= ENP_MAX_NUMB))
694         {
695                 ASSERT(0);
696                 return STALLED;
697         }
698         ASSERT(ep & 0x01);
699         return USB_EP_IO(ep, write, buffer, size, complete);
700 }
701
702 static void usb_ep_low_level_config(int ep, uint16_t offset, uint16_t size)
703 {
704         stm32_usb_ep_t *epc = &ep_cnfg[ep];
705
706         /* IN EP */
707         if (ep & 0x01)
708         {
709                 /* Disable EP */
710                 EpCtrlSet_STAT_TX(epc->hw, EP_DISABLED);
711                 /* Clear Tx toggle */
712                 EpCtrlSet_DTOG_TX(epc->hw, 0);
713                 /* Clear Correct Transfer for transmission flag */
714                 EpCtrlClr_CTR_TX(epc->hw);
715
716                 /* Update EP description table */
717                 WriteEpDTB_AddrTx(ep >> 1, offset);
718                 WriteEpDTB_CountTx(ep >> 1, 0);
719         }
720         /* OUT EP */
721         else
722         {
723                 uint16_t rx_count = 0;
724
725                 /* Disable EP */
726                 EpCtrlSet_STAT_RX(epc->hw, EP_DISABLED);
727                 /* Clear Rx toggle */
728                 EpCtrlSet_DTOG_RX(epc->hw, 0);
729                 /* Clear Correct Transfer for reception flag */
730                 EpCtrlClr_CTR_RX(epc->hw);
731                 /* Descriptor block size field */
732                 rx_count |= (size > 62) << 15;
733                 /* Descriptor number of blocks field */
734                 rx_count |= (((size > 62) ?  (size >> 5) - 1 : size >> 1) &
735                                 0x1f) << 10;
736                 /* Update EP description table */
737                 WriteEpDTB_AddrRx(ep >> 1, offset);
738                 WriteEpDTB_CountRx(ep >> 1, rx_count);
739         }
740 }
741
742 /* Enable/Disable an endpoint */
743 static int usb_ep_configure(const usb_endpoint_descriptor_t *epd, bool enable)
744 {
745         int EP;
746         stm32_usb_ep_t *ep_hw;
747         reg32_t *hw;
748         uint16_t Offset;
749         uint32_t MaxPacketSizeTmp;
750
751         EP = USB_EpLogToPhysAdd(epd->bEndpointAddress);
752         ep_hw = &ep_cnfg[EP];
753
754         if (enable)
755         {
756                 /*
757                  * Allocate packet memory for EP buffer/s calculate actual size
758                  * only for the OUT EPs.
759                  */
760                 MaxPacketSizeTmp = epd->wMaxPacketSize;
761                 if (!USB_AllocateBuffer(&Offset, &MaxPacketSizeTmp, EP))
762                         return -USB_MEMORY_FULL;
763
764                 /* Set EP status */
765                 ep_hw->status  = NOT_READY;
766                 /* Init EP flags */
767                 ep_hw->flags = 0;
768
769                 /* Set endpoint type */
770                 ep_hw->type = usb_endpoint_type(epd);
771                 /* Init EP max packet size */
772                 ep_hw->max_size = epd->wMaxPacketSize;
773
774                 if (EP & 0x01)
775                         ep_hw->avail_data = 1;
776                 else
777                         ep_hw->avail_data = 0;
778                 hw = (reg32_t *)&usb->EP0R;
779                 hw += EP >> 1;
780
781                 /* Set Ep Address */
782                 EpCtrlSet_EA(hw, EP >> 1);
783                 ep_hw->hw = hw;
784                 LOG_INFO("%s: EP%d-%s configured\n",
785                                 __func__, EP >> 1, EP & 1 ? "IN" : "OUT");
786
787                 /* Low-level endpoint configuration */
788                 usb_ep_low_level_config(EP, Offset, MaxPacketSizeTmp);
789
790                 /* Set EP Kind & enable */
791                 switch (ep_hw->type)
792                 {
793                 case USB_ENDPOINT_XFER_CONTROL:
794                         LOG_INFO("EP%d: CONTROL IN\n", EP >> 1);
795                         EpCtrlSet_EP_TYPE(hw, EP_CTRL);
796                         EpCtrlSet_EP_KIND(hw, 0);
797                         break;
798                 case USB_ENDPOINT_XFER_INT:
799                         LOG_INFO("EP%d: INTERRUPT IN\n", EP >> 1);
800                         EpCtrlSet_EP_TYPE(hw, EP_INTERRUPT);
801                         EpCtrlSet_EP_KIND(hw, 0);
802                         break;
803                 case USB_ENDPOINT_XFER_BULK:
804                         LOG_INFO("EP%d: BULK IN\n", EP >> 1);
805                         EpCtrlSet_EP_TYPE(hw, EP_BULK);
806                         EpCtrlSet_EP_KIND(hw, 0);
807                         break;
808                 case USB_ENDPOINT_XFER_ISOC:
809                         LOG_ERR("EP%d: ISOCHRONOUS IN: not supported\n", EP >> 1);
810                         /* Fallback to default */
811                 default:
812                         ASSERT(0);
813                         return -USB_NODEV_ERROR;
814                 }
815                 if (EP & 0x01)
816                 {
817                         /* Enable EP */
818                         EpCtrlSet_STAT_TX(hw, EP_NAK);
819                         /* Clear Correct Transfer for transmission flag */
820                         EpCtrlClr_CTR_TX(hw);
821                 }
822                 else
823                 {
824                         /* Enable EP */
825                         EpCtrlSet_STAT_RX(hw, EP_VALID);
826                 }
827         }
828         else if (ep_cnfg[EP].hw)
829         {
830                 hw = (reg32_t *)&usb->EP0R;
831                 hw += EP >> 1;
832
833                 /* IN EP */
834                 if (EP & 0x01)
835                 {
836                         /* Disable IN EP */
837                         EpCtrlSet_STAT_TX(hw, EP_DISABLED);
838                         /* Clear Correct Transfer for reception flag */
839                         EpCtrlClr_CTR_TX(hw);
840                 }
841                 /* OUT EP */
842                 else
843                 {
844                         /* Disable OUT EP */
845                         EpCtrlSet_STAT_RX(hw, EP_DISABLED);
846                         /* Clear Correct Transfer for reception flag */
847                         EpCtrlClr_CTR_RX(hw);
848                 }
849                 /* Release buffer */
850                 USB_ReleaseBuffer(EP);
851                 ep_cnfg[EP].hw = NULL;
852         }
853         return 0;
854 }
855
856 /* Get EP stall/unstall */
857 static int USB_GetStallEP(int EP, bool *pStall)
858 {
859         if (ep_cnfg[EP].hw == NULL)
860                 return -USB_NODEV_ERROR;
861
862         *pStall = (EP & 0x01) ?
863                 (EpCtrlGet_STAT_TX(ep_cnfg[EP].hw) == EP_STALL):  /* IN EP  */
864                 (EpCtrlGet_STAT_RX(ep_cnfg[EP].hw) == EP_STALL);  /* OUT EP */
865
866         return USB_OK;
867 }
868
869 /* Set EP stall/unstall */
870 static int USB_SetStallEP(int EP, bool Stall)
871 {
872         if (ep_cnfg[EP].hw == NULL)
873                 return -USB_NODEV_ERROR;
874
875         if (Stall)
876         {
877                 ep_cnfg[EP].status = STALLED;
878                 if (EP & 0x01)
879                 {
880                         /* IN EP */
881                         EpCtrlSet_STAT_TX(ep_cnfg[EP].hw, EP_STALL);
882                         ep_cnfg[EP].avail_data = 1;
883                 }
884                 else
885                 {
886                         /* OUT EP */
887                         EpCtrlSet_STAT_RX(ep_cnfg[EP].hw, EP_STALL);
888                         ep_cnfg[EP].avail_data = 0;
889                 }
890         }
891         else
892         {
893                 ep_cnfg[EP].status = NOT_READY;
894                 if(EP & 0x01)
895                 {
896                         /* IN EP */
897                         ep_cnfg[EP].avail_data = 1;
898                         /* reset Data Toggle bit */
899                         EpCtrlSet_DTOG_TX(ep_cnfg[EP].hw, 0);
900                         EpCtrlSet_STAT_TX(ep_cnfg[EP].hw, EP_NAK);
901                 }
902                 else
903                 {
904                         /* OUT EP */
905                         ep_cnfg[EP].avail_data = 0;
906                         /* reset Data Toggle bit */
907                         EpCtrlSet_DTOG_RX(ep_cnfg[EP].hw, 0);
908                         EpCtrlSet_STAT_RX(ep_cnfg[EP].hw, EP_VALID);
909                 }
910         }
911         return USB_OK;
912 }
913
914 /* Stall both directions of the control EP */
915 static void USB_StallCtrlEP(void)
916 {
917         ep_cnfg[CTRL_ENP_IN].avail_data = 1;
918         ep_cnfg[CTRL_ENP_IN].status = STALLED;
919         ep_cnfg[CTRL_ENP_OUT].avail_data = 0;
920         ep_cnfg[CTRL_ENP_OUT].status = STALLED;
921
922         USB_SetStallEP(CTRL_ENP_IN, true);
923         USB_SetStallEP(CTRL_ENP_OUT, true);
924 }
925
926 /*
927  * Find the position of an interface descriptor inside the configuration
928  * descriptor.
929  */
930 static int usb_find_interface(uint32_t num, uint32_t alt)
931 {
932         const usb_interface_descriptor_t *id;
933         int i;
934
935         for (i = 0; ; i++)
936         {
937                 /* TODO: support more than one configuration per device */
938                 id = (const usb_interface_descriptor_t *)usb_dev->config[i];
939                 if (id == NULL)
940                         break;
941                 if (id->bDescriptorType != USB_DT_INTERFACE)
942                         continue;
943                 if ((id->bInterfaceNumber == num) &&
944                                 (id->bAlternateSetting == alt))
945                         return i;
946         }
947         return -USB_NODEV_ERROR;
948 }
949
950 /*
951  * Configure/deconfigure EPs of a certain interface.
952  */
953 static void
954 usb_configure_ep_interface(unsigned int num, unsigned int alt, bool enable)
955 {
956         const usb_endpoint_descriptor_t *epd;
957         int i, start;
958
959         /*
960          * Find the position of the interface descriptor (inside the
961          * configuration descriptor).
962          */
963         start = usb_find_interface(num, alt);
964         if (start < 0)
965         {
966                 LOG_ERR("%s: interface (%u,%u) not found\n",
967                         __func__, num, alt);
968                 return;
969         }
970         /*
971          * Cycle over endpoint descriptors.
972          *
973          * NOTE: the first endpoint descriptor is placed next to the interface
974          * descriptor, so we need to add +1 to the position of the interface
975          * descriptor to find it.
976          */
977         for (i = start + 1; ; i++)
978         {
979                 epd = (const usb_endpoint_descriptor_t *)usb_dev->config[i];
980                 if ((epd == NULL) || (epd->bDescriptorType == USB_DT_INTERFACE))
981                         break;
982                 if (epd->bDescriptorType != USB_DT_ENDPOINT)
983                         continue;
984                 if (UNLIKELY(usb_ep_configure(epd, enable) < 0))
985                 {
986                         LOG_ERR("%s: out of memory, can't initialize EP\n",
987                                 __func__);
988                         return;
989                 }
990         }
991 }
992
993 /* Set device state */
994 static void usb_set_device_state(int state)
995 {
996         unsigned int i;
997
998         LOG_INFO("%s: new state %d\n", __func__, state);
999
1000         if (udc.state == USB_STATE_CONFIGURED)
1001         {
1002                 /* Deconfigure device */
1003                 for (i = 0; i < udc.interfaces; ++i)
1004                         usb_configure_ep_interface(i,
1005                                 udc.alt[i], false);
1006         }
1007         switch (state)
1008         {
1009         case USB_STATE_ATTACHED:
1010         case USB_STATE_POWERED:
1011         case USB_STATE_DEFAULT:
1012                 usb_set_address(0);
1013                 usb_dev->configured = false;
1014                 udc.address = udc.cfg_id = 0;
1015                 break;
1016         case USB_STATE_ADDRESS:
1017                 udc.cfg_id = 0;
1018                 break;
1019         case USB_STATE_CONFIGURED:
1020                 /* Configure device */
1021                 for (i = 0; i < udc.interfaces; ++i)
1022                         usb_configure_ep_interface(i,
1023                                 udc.alt[i], true);
1024                 break;
1025         default:
1026                 /* Unknown state: disconnected or connection in progress */
1027                 usb_dev->configured = false;
1028                 udc.address = 0;
1029                 udc.cfg_id = 0;
1030                 break;
1031         }
1032         udc.state = state;
1033 }
1034
1035 /* Setup packet: set address status phase end handler */
1036 static void USB_AddStatusEndHandler(UNUSED_ARG(int, EP))
1037 {
1038         uint16_t w_value;
1039
1040         w_value = usb_le16_to_cpu(setup_packet.wValue);
1041         udc.address = w_value & 0xff;
1042         usb_set_address(udc.address);
1043
1044         if (udc.address)
1045                 usb_set_device_state(USB_STATE_ADDRESS);
1046         else
1047                 usb_set_device_state(USB_STATE_DEFAULT);
1048
1049         __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL);
1050         __usb_ep_read(CTRL_ENP_OUT, NULL, -1, NULL);
1051 }
1052
1053 /* Prepare status phase */
1054 static void USB_StatusPhase(bool in)
1055 {
1056         if (in)
1057                 __usb_ep_write(CTRL_ENP_IN, NULL, 0, NULL);
1058 }
1059
1060 /* Setup packet: status phase end handler */
1061 static void USB_StatusEndHandler(UNUSED_ARG(int, EP))
1062 {
1063         __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL);
1064         __usb_ep_read(CTRL_ENP_OUT, NULL, -1, NULL);
1065 }
1066
1067 /* Address status handler */
1068 static void USB_StatusHandler(UNUSED_ARG(int, EP))
1069 {
1070         if (setup_packet.mRequestType & USB_DIR_IN)
1071         {
1072                 USB_StatusPhase(false);
1073                 ep_cnfg[CTRL_ENP_OUT].complete = USB_StatusEndHandler;
1074         }
1075         else
1076         {
1077                 USB_StatusPhase(true);
1078                 ep_cnfg[CTRL_ENP_IN].complete =
1079                         (setup_packet.bRequest == USB_REQ_SET_ADDRESS) ?
1080                                 USB_AddStatusEndHandler :
1081                                 USB_StatusEndHandler;
1082         }
1083 }
1084
1085 static bool rx_done;
1086 static size_t rx_size;
1087
1088 static void usb_ep_read_complete(int ep)
1089 {
1090         if (UNLIKELY(ep >= ENP_MAX_NUMB))
1091         {
1092                 ASSERT(0);
1093                 return;
1094         }
1095         ASSERT(!(ep & 0x01));
1096
1097         rx_done = true;
1098         rx_size = ep_cnfg[ep].size;
1099 }
1100
1101 ssize_t usb_ep_read(int ep, void *buffer, ssize_t size)
1102 {
1103         int ep_num = USB_EpLogToPhysAdd(ep);
1104
1105         /* Non-blocking read for EP0 */
1106         if (ep_num == CTRL_ENP_OUT)
1107         {
1108                 size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength));
1109                 if (!size)
1110                         USB_StatusHandler(ep_num);
1111                 else
1112                         __usb_ep_read(ep_num, buffer, size,
1113                                         USB_StatusHandler);
1114                 return size;
1115         }
1116         if (UNLIKELY(!size))
1117                 return 0;
1118         size = MIN(size, USB_RX_MAX_SIZE);
1119         rx_done = false;
1120         rx_size = 0;
1121
1122         /* Blocking read */
1123         __usb_ep_read(ep_num, buffer, size, usb_ep_read_complete);
1124         while (!rx_done)
1125                 cpu_relax();
1126
1127         return rx_size;
1128 }
1129
1130 static bool tx_done;
1131 static size_t tx_size;
1132
1133 static void usb_ep_write_complete(int ep)
1134 {
1135         if (UNLIKELY(ep >= ENP_MAX_NUMB))
1136         {
1137                 ASSERT(0);
1138                 return;
1139         }
1140         ASSERT(ep & 0x01);
1141
1142         tx_done = true;
1143         tx_size = ep_cnfg[ep].size;
1144 }
1145
1146 ssize_t usb_ep_write(int ep, const void *buffer, ssize_t size)
1147 {
1148         int ep_num = USB_EpLogToPhysAdd(ep);
1149
1150         /* Non-blocking write for EP0 */
1151         if (ep_num == CTRL_ENP_IN)
1152         {
1153                 size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength));
1154                 if (!size)
1155                         USB_StatusHandler(ep_num);
1156                 else
1157                         __usb_ep_write(ep_num, buffer, size,
1158                                         USB_StatusHandler);
1159                 return size;
1160         }
1161         if (UNLIKELY(!size))
1162                 return 0;
1163         size = MIN(size, USB_TX_MAX_SIZE);
1164         tx_done = false;
1165         tx_size = 0;
1166
1167         /* Blocking write */
1168         __usb_ep_write(ep_num, buffer, size, usb_ep_write_complete);
1169         while (!tx_done)
1170                 cpu_relax();
1171
1172         return tx_size;
1173 }
1174
1175 /* Global variable to handle the following non-blocking I/O operations */
1176 static uint32_t InData;
1177
1178 /* Get device status */
1179 static int UsbDevStatus(uint16_t index)
1180 {
1181         size_t size;
1182
1183         if (index)
1184                 return -USB_NODEV_ERROR;
1185
1186         InData = ((uint32_t)udc.feature) & 0xff;
1187         size = usb_size(sizeof(InData), usb_le16_to_cpu(setup_packet.wLength));
1188         __usb_ep_write(CTRL_ENP_IN,
1189                         (uint8_t *)&InData, size, USB_StatusHandler);
1190
1191         return 0;
1192 }
1193
1194 /* Get interface status */
1195 static int UsbInterfaceStatus(UNUSED_ARG(uint16_t, index))
1196 {
1197         size_t size;
1198
1199         InData = 0;
1200         size = usb_size(sizeof(InData), usb_le16_to_cpu(setup_packet.wLength));
1201         __usb_ep_write(CTRL_ENP_IN,
1202                         (uint8_t *)&InData, size, USB_StatusHandler);
1203
1204         return 0;
1205 }
1206
1207 /* Get endpoint status */
1208 static int UsbEpStatus(uint16_t index)
1209 {
1210         size_t size;
1211
1212         if ((index & 0x7F) > 16)
1213                 return -USB_NODEV_ERROR;
1214
1215         InData = 0;
1216         USB_GetStallEP(USB_EpLogToPhysAdd(index), (bool *)&InData);
1217         size = usb_size(sizeof(InData), usb_le16_to_cpu(setup_packet.wLength));
1218         __usb_ep_write(CTRL_ENP_IN,
1219                         (uint8_t *)&InData, size, USB_StatusHandler);
1220
1221         return 0;
1222 }
1223
1224 /* USB setup packet: GET_STATUS request handler */
1225 static void USB_GetStatusHandler(void)
1226 {
1227         uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1228         uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1229         uint16_t w_length = usb_le16_to_cpu(setup_packet.wLength);
1230
1231         /* GET_STATUS sanity checks */
1232         if (udc.state < USB_STATE_ADDRESS)
1233         {
1234                 LOG_WARN("%s: bad GET_STATUS request (State=%02x)\n",
1235                                 __func__, udc.state);
1236                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1237                 return;
1238         }
1239         if (w_length != 2)
1240         {
1241                 LOG_WARN("%s: bad GET_STATUS request (wLength.Word=%02x)\n",
1242                                 __func__, w_length);
1243                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1244                 return;
1245         }
1246         if (!(setup_packet.mRequestType & USB_DIR_IN))
1247         {
1248                 LOG_WARN("%s: bad GET_STATUS request (mRequestType=%02x)\n",
1249                                 __func__, setup_packet.mRequestType);
1250                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1251                 return;
1252         }
1253         if (w_value)
1254         {
1255                 LOG_WARN("%s: bad GET_STATUS request (wValue=%02x)\n",
1256                                 __func__, w_value);
1257                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1258                 return;
1259         }
1260
1261         /* Process GET_STATUS request */
1262         switch (setup_packet.mRequestType & USB_RECIP_MASK)
1263         {
1264         case USB_RECIP_DEVICE:
1265                 if (UsbDevStatus(w_index) < 0)
1266                 {
1267                         LOG_WARN("%s: GET_STATUS: invalid UsbRecipientDevice\n",
1268                                         __func__);
1269                         ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1270                         return;
1271                 }
1272                 LOG_INFO("%s: GET_STATUS: mRequestType=%02x (UsbRecipientDevice)\n",
1273                                 __func__, setup_packet.mRequestType);
1274                 break;
1275         case USB_RECIP_INTERFACE:
1276                 if (UsbInterfaceStatus(w_index) < 0)
1277                 {
1278                         LOG_WARN("%s: GET_STATUS: invalid UsbRecipientInterface\n",
1279                                         __func__);
1280                         ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1281                         return;
1282                 }
1283                 LOG_INFO("%s: GET_STATUS: mRequestType=%02x (UsbRecipientInterface)\n",
1284                                 __func__, setup_packet.mRequestType);
1285                 break;
1286         case USB_RECIP_ENDPOINT:
1287                 if (UsbEpStatus(w_index) < 0)
1288                 {
1289                         LOG_WARN("%s: GET_STATUS: invalid UsbRecipientEndpoint\n",
1290                                         __func__);
1291                         ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1292                         return;
1293                 }
1294                 LOG_INFO("%s: GET_STATUS: mRequestType=%02x (UsbRecipientEndpoint)\n",
1295                                 __func__, setup_packet.mRequestType);
1296                 break;
1297         default:
1298                 LOG_WARN("%s: GET_STATUS: invalid UsbRecipientEndpoint\n",
1299                                 __func__);
1300                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1301                 break;
1302         }
1303 }
1304
1305 static int usb_get_device_descriptor(int id)
1306 {
1307         if (id)
1308                 return -USB_NODEV_ERROR;
1309
1310         usb_dev->device->bMaxPacketSize0 = USB_EP0_MAX_SIZE;
1311         __usb_ep_write(CTRL_ENP_IN, (const uint8_t *)usb_dev->device,
1312                         usb_size(usb_dev->device->bLength,
1313                                 usb_le16_to_cpu(setup_packet.wLength)),
1314                         USB_StatusHandler);
1315         return 0;
1316 }
1317
1318 #define USB_BUFSIZE (128)
1319 static uint8_t usb_cfg_buffer[USB_BUFSIZE];
1320
1321 static int usb_get_configuration_descriptor(int id)
1322 {
1323         const usb_config_descriptor_t **config =
1324                         (const usb_config_descriptor_t **)usb_dev->config;
1325         uint8_t *p = usb_cfg_buffer;
1326         int i;
1327
1328         /* TODO: support more than one configuration per device */
1329         if (UNLIKELY(id > 0))
1330                 return -USB_NODEV_ERROR;
1331
1332         for (i = 0; config[i]; i++)
1333         {
1334                 memcpy(p, config[i], config[i]->bLength);
1335                 p += config[i]->bLength;
1336
1337                 if (UNLIKELY((p - usb_cfg_buffer) > USB_BUFSIZE))
1338                 {
1339                         ASSERT(0);
1340                         return -USB_BUF_OVERFLOW;
1341                 }
1342         }
1343         ((usb_config_descriptor_t *)usb_cfg_buffer)->wTotalLength =
1344                                         usb_cpu_to_le16(p - usb_cfg_buffer);
1345         __usb_ep_write(CTRL_ENP_IN,
1346                         usb_cfg_buffer,
1347                         usb_size(p - usb_cfg_buffer,
1348                                 usb_le16_to_cpu(setup_packet.wLength)),
1349                         USB_StatusHandler);
1350         return 0;
1351 }
1352
1353 static int usb_get_string_descriptor(unsigned int id)
1354 {
1355         const usb_string_descriptor_t *lang_str;
1356         unsigned int lang_id, str_id;
1357         uint16_t w_index_lo = usb_le16_to_cpu(setup_packet.wIndex) & 0x00ff;
1358         uint16_t w_index_hi = (usb_le16_to_cpu(setup_packet.wIndex) &
1359                                                 0xff00) >> 8;
1360
1361         ASSERT(usb_dev->strings != NULL);
1362         ASSERT(usb_dev->strings[0] != NULL);
1363
1364         lang_str = usb_dev->strings[0];
1365         if (id)
1366         {
1367                 /* Find Language index */
1368                 for (lang_id = 0; ; lang_id++)
1369                 {
1370                         const usb_string_descriptor_t *str =
1371                                                 usb_dev->strings[lang_id];
1372                         if (UNLIKELY(str == NULL))
1373                                 return -USB_NODEV_ERROR;
1374                         if ((str->data[0] == w_index_lo) &&
1375                                         (str->data[1] == w_index_hi))
1376                                 break;
1377                 }
1378                 /* Check buffer overflow to find string index */
1379                 for (str_id = 0; str_id < id; str_id++)
1380                 {
1381                         lang_str = usb_dev->strings[lang_id + 1 + str_id];
1382                         if (lang_str == NULL)
1383                                 return -USB_NODEV_ERROR;
1384                 }
1385         }
1386         __usb_ep_write(CTRL_ENP_IN,
1387                         lang_str,
1388                         usb_size(lang_str->bLength,
1389                                 usb_le16_to_cpu(setup_packet.wLength)),
1390                         USB_StatusHandler);
1391         return 0;
1392 }
1393
1394 static void UsbGetDescriptor(void)
1395 {
1396         uint16_t w_value_lo = usb_le16_to_cpu(setup_packet.wValue) & 0x00ff;
1397         uint16_t w_value_hi = (usb_le16_to_cpu(setup_packet.wValue) & 0xff00) >> 8;
1398
1399         if (udc.state < USB_STATE_DEFAULT)
1400         {
1401                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1402                 return;
1403         }
1404         switch (w_value_hi)
1405         {
1406         case USB_DT_DEVICE:
1407                 LOG_INFO("%s: GET_DEVICE_DESCRIPTOR: id=%d, state=%d\n",
1408                                 __func__,
1409                                 w_value_lo,
1410                                 udc.state);
1411                 if (usb_get_device_descriptor(w_value_lo) < 0)
1412                         ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1413                 break;
1414         case USB_DT_CONFIG:
1415                 LOG_INFO("%s: GET_CONFIG_DESCRIPTOR: id=%d, state=%d\n",
1416                                 __func__, w_value_lo, udc.state);
1417                 if (usb_get_configuration_descriptor(w_value_lo) < 0)
1418                         ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1419                 break;
1420         case USB_DT_STRING:
1421                 LOG_INFO("%s: GET_STRING_DESCRIPTOR: id=%d, state=%d\n",
1422                                 __func__, w_value_lo, udc.state);
1423                 if (usb_get_string_descriptor(w_value_lo) < 0)
1424                         ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1425                 break;
1426         default:
1427                 LOG_WARN("%s: GET_UNKNOWN_DESCRIPTOR: id=%d, state=%d\n",
1428                                 __func__, w_value_lo, udc.state);
1429                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1430                 break;
1431         }
1432 }
1433
1434 /* USB setup packet: class/vendor request handler */
1435 static void usb_event_handler(struct usb_device *dev)
1436 {
1437         /*
1438          * TODO: get the appropriate usb_dev in function of the endpoint
1439          * address.
1440          */
1441         if (dev->event_cb)
1442                 dev->event_cb(&setup_packet);
1443 }
1444
1445 /* USB setup packet: GET_DESCRIPTOR handler */
1446 static void UBS_GetDescriptorHandler(void)
1447 {
1448         LOG_INFO("%s: GET_DESCRIPTOR: RECIP = %d\n",
1449                         __func__,
1450                         setup_packet.mRequestType & USB_RECIP_MASK);
1451         if ((setup_packet.mRequestType & USB_RECIP_MASK) ==
1452                         USB_RECIP_DEVICE)
1453                 UsbGetDescriptor();
1454         /* Getting descriptor for a device is a standard request */
1455         else if ((setup_packet.mRequestType & USB_DIR_MASK) == USB_DIR_IN)
1456                 usb_event_handler(usb_dev);
1457         else
1458                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1459 }
1460
1461 /* USB setup packet: SET_ADDRESS handler */
1462 static void USB_SetAddressHandler(void)
1463 {
1464         uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1465         uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1466         uint16_t w_length = usb_le16_to_cpu(setup_packet.wLength);
1467
1468         LOG_INFO("%s: SET_ADDRESS: %d\n",
1469                         __func__, usb_le16_to_cpu(setup_packet.wValue));
1470         if ((udc.state >= USB_STATE_DEFAULT) &&
1471                         ((setup_packet.mRequestType & USB_RECIP_MASK) ==
1472                                         USB_RECIP_DEVICE) &&
1473                         (w_index == 0) && (w_length == 0) && (w_value < 128))
1474                 USB_StatusHandler(CTRL_ENP_IN);
1475         else
1476                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1477 }
1478
1479 /* USB setup packet: GET_CONFIGURATION handler */
1480 static void USB_GetConfigurationHandler(void)
1481 {
1482         uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1483         uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1484
1485         LOG_INFO("%s: GET_CONFIGURATION\n", __func__);
1486         if ((udc.state >= USB_STATE_ADDRESS) &&
1487                         (w_value == 0) && (w_index == 0) && (w_value == 1))
1488         {
1489                 InData = udc.cfg_id;
1490                 __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 1, USB_StatusHandler);
1491         }
1492         else
1493                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1494 }
1495
1496 static const usb_config_descriptor_t *usb_find_configuration(int num)
1497 {
1498         const usb_config_descriptor_t *cfg;
1499         int i;
1500
1501         for (i = 0; ; i++)
1502         {
1503                 cfg = (const usb_config_descriptor_t *)usb_dev->config[i];
1504                 if (cfg == NULL)
1505                         break;
1506                 if (cfg->bDescriptorType != USB_DT_CONFIG)
1507                         continue;
1508                 if (cfg->bConfigurationValue == num)
1509                         return cfg;
1510         }
1511         return NULL;
1512 }
1513
1514 static int UsbSetConfigurationState(uint32_t Configuration)
1515 {
1516         const usb_config_descriptor_t *pCnfg;
1517         unsigned int i;
1518
1519         if (Configuration)
1520         {
1521                 /* Find configuration descriptor */
1522                 pCnfg = usb_find_configuration(Configuration);
1523                 if (pCnfg == NULL)
1524                         return -USB_NODEV_ERROR;
1525
1526                 /* Reset current configuration */
1527                 usb_set_device_state(USB_STATE_ADDRESS);
1528                 usb_dev->configured = false;
1529                 udc.cfg = pCnfg;
1530
1531                 /* Set Interface and Alternative Setting */
1532                 udc.cfg_id = Configuration;
1533                 /* Set self-powered state */
1534                 if (pCnfg->bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1535                         udc.feature |= STM32_UDC_FEATURE_SELFPOWERED;
1536
1537                 /* Configure all existing interfaces to alternative setting 0 */
1538                 ASSERT(pCnfg->bNumInterfaces <= USB_MAX_INTERFACE);
1539                 udc.interfaces = pCnfg->bNumInterfaces;
1540                 for (i = 0; i < udc.interfaces; i++)
1541                         udc.alt[i] = 0;
1542                 usb_set_device_state(USB_STATE_CONFIGURED);
1543                 usb_dev->configured = true;
1544         }
1545         else
1546         {
1547                 usb_dev->configured = false;
1548                 usb_set_device_state(USB_STATE_ADDRESS);
1549         }
1550         return 0;
1551 }
1552
1553 /* USB setup packet: SET_CONFIGURATION handler */
1554 static void USB_SetConfigurationHandler(void)
1555 {
1556         uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1557         uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1558         uint16_t w_length = usb_le16_to_cpu(setup_packet.wLength);
1559
1560         LOG_INFO("%s: SET_CONFIGURATION: %d\n",
1561                         __func__, w_value);
1562         if ((udc.state >= USB_STATE_ADDRESS) &&
1563                         (w_index == 0) && (w_length == 0) &&
1564                         (UsbSetConfigurationState(w_value & 0xff) == 0))
1565                 USB_StatusHandler(CTRL_ENP_OUT);
1566         else
1567                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1568 }
1569
1570 /* USB setup packet: standard request handler */
1571 static void USB_StandardRequestHandler(void)
1572 {
1573         switch (setup_packet.bRequest)
1574         {
1575         case USB_REQ_GET_STATUS:
1576                 USB_GetStatusHandler();
1577                 break;
1578         case USB_REQ_CLEAR_FEATURE:
1579                 LOG_INFO("%s: bRequest=%d (CLEAR_FEATURE)\n",
1580                                 __func__, setup_packet.bRequest);
1581                 break;
1582         case USB_REQ_SET_FEATURE:
1583                 LOG_INFO("%s: bRequest=%d (SET_FEATURE)\n",
1584                                 __func__, setup_packet.bRequest);
1585                 break;
1586         case USB_REQ_SET_ADDRESS:
1587                 USB_SetAddressHandler();
1588                 break;
1589         case USB_REQ_GET_DESCRIPTOR:
1590                 UBS_GetDescriptorHandler();
1591                 break;
1592         case USB_REQ_SET_DESCRIPTOR:
1593                 LOG_INFO("%s: bRequest=%d (SET_DESCRIPTOR)\n",
1594                                 __func__, setup_packet.bRequest);
1595                 break;
1596         case USB_REQ_GET_CONFIGURATION:
1597                 USB_GetConfigurationHandler();
1598                 break;
1599         case USB_REQ_SET_CONFIGURATION:
1600                 USB_SetConfigurationHandler();
1601                 break;
1602         case USB_REQ_GET_INTERFACE:
1603                 LOG_INFO("%s: bRequest=%d (GET_INTERFACE)\n",
1604                                 __func__, setup_packet.bRequest);
1605                 break;
1606         case USB_REQ_SET_INTERFACE:
1607                 LOG_INFO("%s: bRequest=%d (SET_INTERFACE)\n",
1608                                 __func__, setup_packet.bRequest);
1609                 break;
1610         case USB_REQ_SYNCH_FRAME:
1611                 LOG_INFO("%s: bRequest=%d (SYNCH_FRAME)\n",
1612                                 __func__, setup_packet.bRequest);
1613                 break;
1614         default:
1615                 LOG_WARN("%s: bRequest=%d (Unknown)\n",
1616                                 __func__, setup_packet.bRequest);
1617                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1618                 break;
1619         }
1620 }
1621
1622 /* USB setup packet handler */
1623 static void USB_SetupHandler(void)
1624 {
1625         switch (setup_packet.mRequestType & USB_TYPE_MASK)
1626         {
1627         /* Standard */
1628         case USB_TYPE_STANDARD:
1629                 LOG_INFO("%s: bmRequestType=%02x (Standard)\n",
1630                                 __func__, setup_packet.mRequestType);
1631                 USB_StandardRequestHandler();
1632                 break;
1633         /* Class */
1634         case USB_TYPE_CLASS:
1635                 LOG_INFO("%s: bmRequestType=%02x (Class)\n",
1636                                 __func__, setup_packet.mRequestType);
1637                 usb_event_handler(usb_dev);
1638                 break;
1639         /* Vendor */
1640         case USB_TYPE_VENDOR:
1641                 LOG_INFO("%s: bmRequestType=%02x (Vendor)\n",
1642                                 __func__, setup_packet.mRequestType);
1643                 usb_event_handler(usb_dev);
1644                 break;
1645         case USB_TYPE_RESERVED:
1646                 LOG_INFO("%s: bmRequestType=%02x (Reserved)\n",
1647                                 __func__, setup_packet.mRequestType);
1648                 break;
1649         /* Other */
1650         default:
1651                 LOG_WARN("%s: bmRequestType=%02x (Unknown)\n",
1652                                 __func__, setup_packet.mRequestType);
1653                 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1654                 break;
1655         }
1656 }
1657
1658 /* USB: low-level hardware initialization */
1659 static void usb_hw_reset(void)
1660 {
1661         unsigned int i;
1662         int ret;
1663
1664         /* Initialize endpoint descriptors */
1665         for (i = 0; i < countof(ep_cnfg); i++)
1666                 ep_cnfg[i].hw = NULL;
1667
1668         /* Initialize USB memory */
1669         for (i = 0; i < countof(PacketMemBuff); i++)
1670                 PacketMemBuff[i].Size = 0;
1671         usb->BTABLE = USB_BDT_OFFSET;
1672         pPacketMemUse = NULL;
1673
1674         /* Endpoint initialization */
1675         ret = usb_ep_configure(&USB_CtrlEpDescr0, true);
1676         if (UNLIKELY(ret < 0))
1677         {
1678                 LOG_WARN("%s: out of memory, cannot initialize EP0\n",
1679                                 __func__);
1680                 return;
1681         }
1682         ret = usb_ep_configure(&USB_CtrlEpDescr1, true);
1683         if (UNLIKELY(ret < 0))
1684         {
1685                 LOG_WARN("%s: out of memory, cannot initialize EP1\n",
1686                                 __func__);
1687                 return;
1688         }
1689
1690         /* Set default address */
1691         usb_set_address(0);
1692
1693         /* Enable all the device interrupts */
1694         usb->CNTR = bmCTRM | bmRESETM | bmSOFM | bmERRM | bmPMAOVRM |
1695                         bmSUSPM | bmWKUPM;
1696 }
1697
1698 /* Handle a correct transfer under ISR */
1699 static void usb_isr_correct_transfer(stm32_usb_irq_status_t interrupt)
1700 {
1701         int EP;
1702         reg32_t *pReg = (reg32_t *)&usb->EP0R;
1703
1704         /* Find corresponding EP */
1705         pReg += interrupt.EP_ID;
1706         EP = (int)(((*pReg & 0x0f) << 1) + (interrupt.DIR ? 0 : 1));
1707         ep_cnfg[EP].avail_data = 1;
1708
1709         ASSERT(ep_cnfg[EP].hw);
1710         /* IN EP */
1711         if (EP & 0x01)
1712                 EpCtrlClr_CTR_TX(ep_cnfg[EP].hw);
1713         else
1714                 EpCtrlClr_CTR_RX(ep_cnfg[EP].hw);
1715         if (EP == CTRL_ENP_OUT)
1716         {
1717                 /* Determinate type of packet (only for control EP) */
1718                 bool SetupPacket = EpCtrlGet_SETUP(ep_cnfg[CTRL_ENP_OUT].hw);
1719
1720                 if (SetupPacket)
1721                 {
1722                         ep_cnfg[CTRL_ENP_IN].avail_data = 1;
1723                         /* init IO to receive Setup packet */
1724                         __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL);
1725                         __usb_ep_read(CTRL_ENP_OUT, &setup_packet,
1726                                         sizeof(setup_packet), NULL);
1727
1728                         /* reset EP IO ctrl */
1729                         if (setup_packet.mRequestType & USB_DIR_IN)
1730                                 USB_StatusHandler(CTRL_ENP_OUT);
1731                         USB_SetupHandler();
1732                         if (ep_cnfg[CTRL_ENP_OUT].status == STALLED)
1733                                 USB_StallCtrlEP();
1734                 }
1735                 else
1736                 {
1737                         if (ep_cnfg[CTRL_ENP_OUT].complete &&
1738                                         setup_packet.mRequestType & USB_DIR_IN)
1739                                 ep_cnfg[CTRL_ENP_OUT].complete(CTRL_ENP_OUT);
1740                         else
1741                                 __usb_ep_io(EP);
1742                 }
1743         }
1744         else if (EP == CTRL_ENP_IN)
1745         {
1746                 if (ep_cnfg[CTRL_ENP_IN].complete &&
1747                                 !(setup_packet.mRequestType & USB_DIR_IN))
1748                         ep_cnfg[CTRL_ENP_IN].complete(CTRL_ENP_IN);
1749                 else
1750                         __usb_ep_io(EP);
1751
1752         }
1753         else
1754                 __usb_ep_io(EP);
1755 }
1756
1757 /* USB: interrupt service routine */
1758 static void usb_isr(void)
1759 {
1760         stm32_usb_irq_status_t interrupt;
1761
1762         /* Get masked interrupt flags */
1763         interrupt.status = usb->ISTR;
1764         interrupt.status &= usb->CNTR | 0x1f;
1765
1766         if (interrupt.PMAOVR)
1767         {
1768                 LOG_WARN("%s: DMA overrun / underrun\n", __func__);
1769                 usb->ISTR = ~bmPMAOVRM;
1770         }
1771         if (interrupt.ERR)
1772         {
1773                 LOG_WARN("%s: engine error\n", __func__);
1774                 usb->ISTR = ~bmERRM;
1775         }
1776         if (interrupt.RESET)
1777         {
1778                 LOG_INFO("%s: device reset\n", __func__);
1779                 usb->ISTR = ~bmRESETM;
1780                 usb_hw_reset();
1781                 usb_set_device_state(USB_STATE_DEFAULT);
1782         }
1783         if (interrupt.SOF)
1784         {
1785 #if 0
1786                 /*
1787                  * XXX: disable logging of frame interrupts (too much noise!)
1788                  */
1789                 uint16_t frame_nr = usb->FNR & 0x0fff;
1790                 LOG_INFO("%s: frame %#x\n", __func__, frame_nr);
1791 #endif
1792                 usb->ISTR = ~bmSOFM;
1793         }
1794         if (interrupt.WKUP)
1795         {
1796                 LOG_INFO("%s: wake-up\n", __func__);
1797                 usb->ISTR = ~(bmSUSPM | bmWKUPM);
1798                 usb_resume();
1799         }
1800         if (interrupt.SUSP)
1801         {
1802                 LOG_INFO("%s: suspend\n", __func__);
1803                 usb_suspend();
1804                 usb->ISTR = ~(bmSUSPM | bmWKUPM);
1805         }
1806         if (interrupt.ESOF)
1807         {
1808                 LOG_INFO("%s: expected frame\n", __func__);
1809                 usb->ISTR = ~bmESOFM;
1810         }
1811         if (interrupt.CTR)
1812         {
1813                 usb_isr_correct_transfer(interrupt);
1814         }
1815 }
1816
1817 /* USB: hardware initialization */
1818 static void usb_hw_init(void)
1819 {
1820         /* Enable clocking on the required GPIO pins */
1821         RCC->APB2ENR |= RCC_APB2_GPIOA | RCC_APB2_GPIOC;
1822
1823         /* Make sure that the CAN controller is disabled and held in reset */
1824         RCC->APB1ENR &= ~RCC_APB1_CAN;
1825
1826         /* Configure USB_DM and USB_DP to work as USB lines */
1827         stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE,
1828                         USB_DM_PIN | USB_DP_PIN,
1829                         GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
1830         /* Configure USB_DISC to work as USB disconnect */
1831         stm32_gpioPinConfig((struct stm32_gpio *)GPIOC_BASE,
1832                         USB_DISC_PIN,
1833                         GPIO_MODE_OUT_PP, GPIO_SPEED_50MHZ);
1834         stm32_gpioPinWrite((struct stm32_gpio *)GPIOC_BASE,
1835                                 USB_DISC_PIN, 1);
1836
1837         /* Ensure the USB clock is disabled before setting the prescaler */
1838         RCC->APB1ENR &= ~RCC_APB1_USB;
1839
1840         /* Configure USB clock (48MHz) */
1841         *CFGR_USBPRE_BB &= ~RCC_USBCLK_PLLCLK_1DIV5;
1842
1843         /* Activate USB clock */
1844         RCC->APB1ENR |= RCC_APB1_USB;
1845
1846         /* Force USB reset and disable USB interrupts */
1847         usb->CNTR = bmFRES;
1848         timer_delayHp(1);
1849
1850         /* Issue a USB reset */
1851         usb_hw_reset();
1852
1853         /* Clear spurious pending interrupt */
1854         usb->ISTR = 0;
1855
1856         /* Register interrupt handler */
1857         sysirq_setHandler(USB_LP_CAN_RX0_IRQHANDLER, usb_isr);
1858
1859         /* Software connection enable */
1860         usb_connect();
1861 }
1862
1863 /* Initialize the USB controller */
1864 static void usb_init(void)
1865 {
1866         udc.state = USB_STATE_NOTATTACHED;
1867         udc.feature = 0;
1868
1869         usb_hw_init();
1870 }
1871
1872 /* Register an upper layer USB device into the driver */
1873 int usb_device_register(struct usb_device *dev)
1874 {
1875 #if CONFIG_KERN
1876         MOD_CHECK(proc);
1877 #endif
1878         usb_dev = dev;
1879         usb_init();
1880         while (!usb_dev->configured)
1881                 cpu_relax();
1882         return 0;
1883 }