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