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