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