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