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