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