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