1 /*******************************************************************************
2 * Copyright 2019-2020 Microchip FPGA Embedded Systems Solutions.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * PolarFire SoC MSS USB Driver Stack
7 * USB Logical Layer (USB-LL)
8 * USBD driver
9 *
10 * USBD driver implementation:
11 * This source file implements the common functionality of USB device mode,
12 * which includes initialization of MSS USB in device mode, USB standard request
13 * handling, distribution of requests to specific class, interface or endpoints.
14 *
15 */
16 #include "mpfs_hal/mss_hal.h"
17 #include "mss_usb_device.h"
18 #include "mss_usb_config.h"
19 #include "mss_usb_common_cif.h"
20 #include "mss_usb_device_cif.h"
21 #include "mss_usb_std_def.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 #ifdef MSS_USB_DEVICE_ENABLED
28
29 #define SETUP_PKT_INIT(); g_setup_pkt.request_type = 0;\
30 g_setup_pkt.request = 0;\
31 g_setup_pkt.value = 0;\
32 g_setup_pkt.index = 0;\
33 g_setup_pkt.length = 0;
34
35 volatile uint8_t capture_start = 0;
36 volatile uint32_t counter_start = 0;
37 volatile uint32_t counter_end = 0;
38 volatile uint32_t counter_end1 = 0;
39 volatile uint32_t g_sys_tick_cntr = 0;
40 volatile uint32_t g_sys_tick_start = 0;
41 volatile uint32_t g_sys_tick_end = 0;
42
43 volatile uint32_t g_rd_sys_tick_start = 0;
44 volatile uint32_t g_rd_sys_tick_end = 0;
45
46 /***************************************************************************//**
47 Global variables used within this file.
48 */
49 /* This structure must be implemented by user application */
50 mss_usbd_user_descr_cb_t *g_usbd_user_descr_cb;
51
52 /* This structure must be implemented by USBD-Class driver*/
53 mss_usbd_class_cb_t *g_usbd_class_cb;
54
55 /* USB current Speed of operation selected by user*/
56 static mss_usb_device_speed_t g_usbd_user_speed;
57
58 /*Status information for SET_FEATURE,GET_FEATURE requests.*/
59 static uint16_t g_usbd_status;
60
61 /*Device configuration information*/
62 static mss_usbd_dev_conf_t g_usbd_dev_conf;
63
64 /*This structure is used to hold the setup packet received on control endpoint*/
65 static mss_usbd_setup_pkt_t g_setup_pkt;
66
67 /*Structure representing each endpoint on MSS USB*/
68 static mss_usb_ep_t gd_tx_ep[5u]; /*idx = 0: Control Endpoint*/
69 static mss_usb_ep_t gd_rx_ep[5u];
70
71 /*
72 This structure should be implemented for bare testing of the USB transfers,
73 when in the test mode, no other class specific components or enumeration
74 related APIs are called.
75 This mode is only provided for internal customers for testing purpose.
76 */
77 #ifdef MSS_USB_DEVICE_TEST_MODE
78 extern mss_usbd_user_test_cb_t usbd_test_cb;
79 #endif
80
81 /***************************************************************************//**
82 Private function declarations for this file (USBD Driver).
83 */
84 static void mss_usbd_ep_rx_cb(mss_usb_ep_num_t ep_num, uint8_t status);
85 static void mss_usbd_ep_tx_complete_cb(mss_usb_ep_num_t num, uint8_t status);
86
87 static void mss_usbd_cep_setup_cb(uint8_t status);
88 static void mss_usbd_cep_rx_cb(uint8_t status);
89 static void mss_usbd_cep_tx_complete_cb(uint8_t status);
90
91 static void mss_usbd_reset_cb(void);
92 static void mss_usbd_sof_cb(uint8_t status);
93 static void mss_usbd_suspend_cb(void);
94 static void mss_usbd_resume_cb(void);
95 static void mss_usbd_disconnect_cb(void);
96 static void mss_usbd_dma_handler_cb(mss_usb_ep_num_t ep_num,
97 mss_usb_dma_dir_t dma_dir, uint8_t status,
98 uint32_t dma_addr_val);
99
100 static uint8_t mss_usbd_class_requests(uint8_t** buf_pp, uint32_t* len_p);
101 static uint8_t mss_usbd_vendor_requests(uint8_t** buf_pp, uint32_t* len_p);
102 static uint8_t mss_usbd_get_descriptor(uint8_t** buf_pp, uint32_t* len_p);
103 static uint8_t mss_usbd_set_address(void);
104 static uint8_t mss_usbd_set_config(void);
105 static uint8_t mss_usbd_get_config(uint8_t** buf_pp, uint32_t* len_p);
106 static uint8_t mss_usbd_get_status(uint8_t** buf_pp, uint32_t* len_p);
107 static uint8_t mss_usbd_set_feature(void);
108 static uint8_t mss_usbd_clr_feature(void);
109 static uint8_t mss_usbd_set_descriptor(void);
110 static uint8_t mss_usbd_std_requests(uint8_t** buf,uint32_t* length);
111
112 /*
113 This structure implements the callback functions which will be called by the
114 USBD-CIFL layer.
115 */
116 mss_usbd_cb_t g_mss_usbd_cb =
117 {
118 mss_usbd_ep_rx_cb,
119 mss_usbd_ep_tx_complete_cb,
120
121 mss_usbd_cep_setup_cb,
122 mss_usbd_cep_rx_cb,
123 mss_usbd_cep_tx_complete_cb,
124
125 mss_usbd_sof_cb,
126 mss_usbd_reset_cb,
127 mss_usbd_suspend_cb,
128 mss_usbd_resume_cb,
129 mss_usbd_disconnect_cb,
130 mss_usbd_dma_handler_cb
131 };
132
133 /***************************************************************************//**
134 Exported functions from this file (USBD Driver)
135 */
136
137 /***************************************************************************//**
138 See mss_usb_device.h for details of how to use this function.
139 */
140 void
MSS_USBD_init(mss_usb_device_speed_t speed)141 MSS_USBD_init
142 (
143 mss_usb_device_speed_t speed
144 )
145 {
146 g_usbd_dev_conf.device_addr = 0x00u;
147 g_usbd_dev_conf.device_total_interfaces = 0x00u;
148 g_usbd_dev_conf.device_total_ep = 0x00u;
149 g_usbd_dev_conf.device_state = MSS_USB_NOT_ATTACHED_STATE;
150
151 /*store this for usage with DevQual, OtherSpeed requests*/
152 g_usbd_user_speed = speed;
153 g_usbd_dev_conf.device_speed = speed;
154
155 gd_tx_ep[MSS_USB_CEP].state = MSS_USB_CEP_IDLE;
156
157 MSS_USBD_CIF_init(g_usbd_dev_conf.device_speed);
158
159 MSS_USBD_CIF_dev_connect();
160
161 g_usbd_dev_conf.device_state = MSS_USB_POWERED_STATE;
162 }
163
164 /***************************************************************************//**
165 See mss_usb_device.h for details of how to use this function.
166 */
167 void
MSS_USBD_set_descr_cb_handler(mss_usbd_user_descr_cb_t * user_desc_cb)168 MSS_USBD_set_descr_cb_handler
169 (
170 mss_usbd_user_descr_cb_t* user_desc_cb
171 )
172 {
173 g_usbd_user_descr_cb = user_desc_cb;
174 }
175 /***************************************************************************//**
176 See mss_usb_device.h for details of how to use this function.
177 */
178 void
MSS_USBD_set_class_cb_handler(mss_usbd_class_cb_t * class_cb)179 MSS_USBD_set_class_cb_handler
180 (
181 mss_usbd_class_cb_t* class_cb
182 )
183 {
184 g_usbd_class_cb = class_cb;
185 }
186
187 /***************************************************************************//**
188 See mss_usb_device.h for details of how to use this function.
189 */
190 void
MSS_USBD_tx_ep_stall(mss_usb_ep_num_t ep_num)191 MSS_USBD_tx_ep_stall
192 (
193 mss_usb_ep_num_t ep_num
194 )
195 {
196 gd_tx_ep[ep_num].stall = 1u;
197 MSS_USBD_CIF_tx_ep_stall(ep_num);
198 }
199
200 /***************************************************************************//**
201 See mss_usb_device.h for details of how to use this function.
202 */
203 void
MSS_USBD_tx_ep_clr_stall(mss_usb_ep_num_t ep_num)204 MSS_USBD_tx_ep_clr_stall
205 (
206 mss_usb_ep_num_t ep_num
207 )
208 {
209 MSS_USBD_CIF_tx_ep_clr_stall(ep_num);
210 }
211
212 /***************************************************************************//**
213 See mss_usb_device.h for details of how to use this function.
214 */
215 void
MSS_USBD_tx_ep_flush_fifo(mss_usb_ep_num_t ep_num)216 MSS_USBD_tx_ep_flush_fifo
217 (
218 mss_usb_ep_num_t ep_num
219 )
220 {
221 MSS_USB_CIF_tx_ep_flush_fifo(ep_num);
222 }
223
224 /***************************************************************************//**
225 See mss_usb_device.h for details of how to use this function.
226 */
227 void
MSS_USBD_rx_ep_stall(mss_usb_ep_num_t ep_num)228 MSS_USBD_rx_ep_stall
229 (
230 mss_usb_ep_num_t ep_num
231 )
232 {
233 gd_rx_ep[ep_num].stall = 1u;
234 MSS_USBD_CIF_rx_ep_stall(ep_num);
235 }
236
237 /***************************************************************************//**
238 See mss_usb_device.h for details of how to use this function.
239 */
240 void
MSS_USBD_rx_ep_clr_stall(mss_usb_ep_num_t ep_num)241 MSS_USBD_rx_ep_clr_stall
242 (
243 mss_usb_ep_num_t ep_num
244 )
245 {
246 MSS_USBD_CIF_rx_ep_clr_stall(ep_num);
247 }
248
249 /***************************************************************************//**
250 See mss_usb_device.h for details of how to use this function.
251 */
252 void
MSS_USBD_rx_ep_flush_fifo(mss_usb_ep_num_t ep_num)253 MSS_USBD_rx_ep_flush_fifo
254 (
255 mss_usb_ep_num_t ep_num
256 )
257 {
258 MSS_USB_CIF_rx_ep_flush_fifo(ep_num);
259 }
260
261 /***************************************************************************//**
262 See mss_usb_device.h for details of how to use this function.
263 */
264 void
MSS_USBD_cep_flush_fifo(void)265 MSS_USBD_cep_flush_fifo
266 (
267 void
268 )
269 {
270 MSS_USB_CIF_cep_flush_fifo();
271 }
272
273 /***************************************************************************//**
274 See mss_usb_device.h for details of how to use this function.
275 */
276 void
MSS_USBD_cep_configure(uint8_t max_pkt_size)277 MSS_USBD_cep_configure
278 (
279 uint8_t max_pkt_size
280 )
281 {
282 mss_usb_ep_t* cep_ptr = &gd_tx_ep[MSS_USB_CEP];
283
284 cep_ptr->num = MSS_USB_CEP;
285 cep_ptr->stall = 0u;
286 cep_ptr->state = MSS_USB_CEP_IDLE;
287 cep_ptr->xfr_type = MSS_USB_XFR_CONTROL;
288
289 /*FIFO address */
290 cep_ptr->buf_addr = 0u;
291
292 cep_ptr->max_pkt_size = max_pkt_size;
293 cep_ptr->txn_length = SETUP_PKT_SIZE;
294
295 MSS_USBD_CIF_cep_configure();
296 }
297
298 /***************************************************************************//**
299 See mss_usb_device.h for details of how to use this function.
300 */
301 void
MSS_USBD_cep_read_prepare(uint8_t * addr,uint32_t length)302 MSS_USBD_cep_read_prepare
303 (
304 uint8_t * addr,
305 uint32_t length
306 )
307 {
308 mss_usb_ep_t* cep_ptr = &gd_tx_ep[MSS_USB_CEP];
309
310 cep_ptr->buf_addr = addr;
311 cep_ptr->xfr_length = length;
312 cep_ptr->xfr_count = 0u;
313 cep_ptr->txn_count = 0u;
314
315 if(cep_ptr->xfr_length > cep_ptr->max_pkt_size)
316 {
317 cep_ptr->txn_length = cep_ptr->max_pkt_size;
318 }
319 else
320 {
321 cep_ptr->txn_length = length;
322 }
323 MSS_USBD_CIF_cep_rx_prepare(&gd_tx_ep[MSS_USB_CEP]);
324 }
325
326 /***************************************************************************//**
327 See mss_usb_device.h for details of how to use this function.
328 */
329 void
MSS_USBD_cep_write(uint8_t * addr,uint32_t length)330 MSS_USBD_cep_write
331 (
332 uint8_t * addr,
333 uint32_t length
334 )
335 {
336 mss_usb_ep_t* cep_ptr = &gd_tx_ep[MSS_USB_CEP];
337
338 cep_ptr->buf_addr = addr;
339 cep_ptr->xfr_length = length;
340
341 cep_ptr->xfr_count = 0u;
342 cep_ptr->txn_count = 0u;
343
344 if(cep_ptr->xfr_length > cep_ptr->max_pkt_size)
345 {
346 cep_ptr->txn_length = cep_ptr->max_pkt_size;
347 }
348 else
349 {
350 cep_ptr->txn_length = length;
351 }
352
353 MSS_USBD_CIF_cep_write_pkt(&gd_tx_ep[MSS_USB_CEP]);
354 }
355
356 /***************************************************************************//**
357 See mss_usb_device.h for details of how to use this function.
358 */
359 void
MSS_USBD_tx_ep_configure(mss_usb_ep_num_t ep_num,uint16_t fifo_addr,uint16_t fifo_size,uint16_t max_pkt_size,uint8_t num_usb_pkt,uint8_t dma_enable,mss_usb_dma_channel_t dma_channel,mss_usb_xfr_type_t xfr_type,uint32_t add_zlp)360 MSS_USBD_tx_ep_configure
361 (
362 mss_usb_ep_num_t ep_num,
363 uint16_t fifo_addr,
364 uint16_t fifo_size, /*This is actual size of FIFO independent of DPB*/
365 uint16_t max_pkt_size,
366 uint8_t num_usb_pkt,
367 uint8_t dma_enable,
368 mss_usb_dma_channel_t dma_channel,
369 mss_usb_xfr_type_t xfr_type,
370 uint32_t add_zlp
371 )
372 {
373 uint8_t err_check = USB_SUCCESS;
374 uint16_t std_max_pkt_sz = USB_HS_BULK_MAX_PKT_SIZE;
375 mss_usb_ep_t* txep_ptr = &gd_tx_ep[ep_num];
376
377 if(MSS_USB_DEVICE_HS == g_usbd_dev_conf.device_speed)
378 {
379 switch(xfr_type)
380 {
381 case MSS_USB_XFR_BULK:
382 std_max_pkt_sz = USB_HS_BULK_MAX_PKT_SIZE;
383 break;
384 case MSS_USB_XFR_INTERRUPT:
385 std_max_pkt_sz = USB_HS_INTERRUPT_MAX_PKT_SIZE;
386 break;
387 case MSS_USB_XFR_ISO:
388 std_max_pkt_sz = USB_HS_ISO_MAX_PKT_SIZE;
389 break;
390 default:
391 err_check = USB_FAIL;
392 }
393 }
394 else if(MSS_USB_DEVICE_FS == g_usbd_dev_conf.device_speed)
395 {
396 switch(xfr_type)
397 {
398 case MSS_USB_XFR_BULK:
399 std_max_pkt_sz = USB_FS_BULK_MAX_PKT_SIZE;
400 break;
401 case MSS_USB_XFR_INTERRUPT:
402 std_max_pkt_sz = USB_FS_INTERRUPT_MAX_PKT_SIZE;
403 break;
404 case MSS_USB_XFR_ISO:
405 std_max_pkt_sz = USB_FS_ISO_MAX_PKT_SIZE;
406 break;
407 default:
408 err_check = USB_FAIL;
409 }
410 }
411
412 if(max_pkt_size > std_max_pkt_sz)
413 {
414 err_check = USB_FAIL;
415 }
416 if(fifo_size < max_pkt_size)
417 {
418 err_check = USB_FAIL;
419 }
420
421 ASSERT(err_check == USB_SUCCESS);
422
423 if(USB_SUCCESS == err_check)
424 {
425 if((max_pkt_size * 2) <= fifo_size)
426 {
427 txep_ptr->dpb_enable = DPB_ENABLE;
428 }
429 else
430 {
431 txep_ptr->dpb_enable = DPB_DISABLE;
432 }
433
434 txep_ptr->num = ep_num;
435 txep_ptr->fifo_addr = fifo_addr;
436 txep_ptr->fifo_size = fifo_size;
437 txep_ptr->max_pkt_size = max_pkt_size;
438 txep_ptr->num_usb_pkt = num_usb_pkt;
439 txep_ptr->dma_enable = dma_enable;
440 txep_ptr->dma_channel = dma_channel;
441 txep_ptr->stall = 0u;
442 txep_ptr->state = MSS_USB_EP_VALID;
443 txep_ptr->xfr_type = xfr_type;
444 txep_ptr->buf_addr = 0u;
445 txep_ptr->add_zlp = add_zlp;
446 MSS_USBD_CIF_tx_ep_configure(txep_ptr);
447 }
448 }
449
450 /***************************************************************************//**
451 See mss_usb_device.h for details of how to use this function.
452 */
453 void
MSS_USBD_rx_ep_configure(mss_usb_ep_num_t ep_num,uint16_t fifo_addr,uint16_t fifo_size,uint16_t max_pkt_size,uint8_t num_usb_pkt,uint8_t dma_enable,mss_usb_dma_channel_t dma_channel,mss_usb_xfr_type_t xfr_type,uint32_t add_zlp)454 MSS_USBD_rx_ep_configure
455 (
456 mss_usb_ep_num_t ep_num,
457 uint16_t fifo_addr,
458 uint16_t fifo_size, /*This is actual size of FIFO independent of DPB*/
459 uint16_t max_pkt_size,
460 uint8_t num_usb_pkt,
461 uint8_t dma_enable,
462 mss_usb_dma_channel_t dma_channel,
463 mss_usb_xfr_type_t xfr_type,
464 uint32_t add_zlp
465 )
466 {
467 uint8_t err_check = USB_SUCCESS;
468
469 uint16_t std_max_pkt_sz = USB_HS_BULK_MAX_PKT_SIZE;
470 mss_usb_ep_t* rxep_ptr = &gd_rx_ep[ep_num];
471
472 if(MSS_USB_DEVICE_HS == g_usbd_dev_conf.device_speed)
473 {
474 switch(xfr_type)
475 {
476 case MSS_USB_XFR_BULK:
477 std_max_pkt_sz = USB_HS_BULK_MAX_PKT_SIZE;
478 break;
479 case MSS_USB_XFR_INTERRUPT:
480 std_max_pkt_sz = USB_HS_INTERRUPT_MAX_PKT_SIZE;
481 break;
482 case MSS_USB_XFR_ISO:
483 std_max_pkt_sz = USB_HS_ISO_MAX_PKT_SIZE;
484 break;
485 default:
486 err_check = USB_FAIL;
487 }
488 }
489 else if(MSS_USB_DEVICE_FS == g_usbd_dev_conf.device_speed)
490 {
491 switch(xfr_type)
492 {
493 case MSS_USB_XFR_BULK:
494 std_max_pkt_sz = USB_FS_BULK_MAX_PKT_SIZE;
495 break;
496 case MSS_USB_XFR_INTERRUPT:
497 std_max_pkt_sz = USB_FS_INTERRUPT_MAX_PKT_SIZE;
498 break;
499 case MSS_USB_XFR_ISO:
500 std_max_pkt_sz = USB_FS_ISO_MAX_PKT_SIZE;
501 break;
502 default:
503 err_check = USB_FAIL;
504 }
505 }
506
507 if(max_pkt_size > std_max_pkt_sz)
508 {
509 err_check = USB_FAIL;
510 }
511 if(fifo_size < max_pkt_size)
512 {
513 err_check = USB_FAIL;
514 }
515
516 ASSERT(err_check == USB_SUCCESS);
517
518 if(USB_SUCCESS == err_check)
519 {
520 if((max_pkt_size * 2u ) <= fifo_size)
521 {
522 rxep_ptr->dpb_enable = DPB_ENABLE;
523 }
524 else
525 {
526 rxep_ptr->dpb_enable = DPB_DISABLE;
527 }
528 rxep_ptr->num = ep_num;
529 rxep_ptr->fifo_addr = fifo_addr;
530 rxep_ptr->fifo_size = fifo_size;
531 rxep_ptr->max_pkt_size = max_pkt_size;
532 rxep_ptr->num_usb_pkt = num_usb_pkt;
533 rxep_ptr->dma_enable = dma_enable;
534 rxep_ptr->dma_channel = dma_channel;
535 rxep_ptr->stall = 0u;
536 rxep_ptr->state = MSS_USB_EP_VALID;
537 rxep_ptr->xfr_type = xfr_type;
538 rxep_ptr->buf_addr = 0u;
539 rxep_ptr->add_zlp = add_zlp;
540
541 MSS_USBD_CIF_rx_ep_configure(rxep_ptr);
542 }
543 }
544
545 /***************************************************************************//**
546 See mss_usb_device.h for details of how to use this function.
547 */
548 void
MSS_USBD_rx_ep_read_prepare(mss_usb_ep_num_t ep_num,uint8_t * addr,uint32_t length)549 MSS_USBD_rx_ep_read_prepare
550 (
551 mss_usb_ep_num_t ep_num,
552 uint8_t * addr,
553 uint32_t length
554 )
555 {
556 mss_usb_ep_t* rxep_ptr = &gd_rx_ep[ep_num];
557
558 ASSERT(ep_num);
559 ASSERT(addr != 0);
560
561 rxep_ptr->buf_addr = addr;
562 rxep_ptr->xfr_length = length;
563 rxep_ptr->xfr_count = 0u;
564 rxep_ptr->txn_count = 0u;
565
566 /*ASSERT when length is 0, address is null or ep number is wrong*/
567 if(length > 0u)
568 {
569 /*
570 TODO: While using DMA, RxMaxP payload value MUST be an even number
571 when dealing with Multi packet Bulk transaction for
572 proper interrupt generation. This needs to be taken care.
573 */
574 /*
575 section 8.4.2.3 -- No support for HB in Interrupt transfers.
576 i.e. maximum one packet(num_usb_pkt=1)can be transferred in one frame.
577 */
578 if(length >= rxep_ptr->max_pkt_size)
579 {
580 rxep_ptr->txn_length = rxep_ptr->max_pkt_size;
581 }
582 else
583 {
584 rxep_ptr->txn_length = length;
585 }
586 }
587
588 MSS_USB_CIF_rx_ep_read_prepare(rxep_ptr->num,
589 rxep_ptr->buf_addr,
590 rxep_ptr->dma_enable,
591 rxep_ptr->dma_channel,
592 rxep_ptr->xfr_type,
593 rxep_ptr->xfr_length);
594 }
595
596 /***************************************************************************//**
597 See mss_usb_device.h for details of how to use this function.
598 */
599 void
MSS_USBD_tx_ep_write(mss_usb_ep_num_t ep_num,uint8_t * addr,uint32_t length)600 MSS_USBD_tx_ep_write
601 (
602 mss_usb_ep_num_t ep_num,
603 uint8_t * addr,
604 uint32_t length
605 )
606 {
607 uint8_t dpb = 1u;
608 mss_usb_ep_t* txep_ptr = &gd_tx_ep[ep_num];
609
610 /* ASSERT if uneven value for BULK transfers */
611 ASSERT(ep_num);
612 ASSERT(addr != 0);
613
614 if((ep_num) && (addr != 0))
615 {
616 if(DPB_ENABLE == txep_ptr->dpb_enable)
617 {
618 dpb = 2u;
619 }
620 txep_ptr->num = ep_num;
621 txep_ptr->buf_addr = addr;
622 txep_ptr->xfr_length = length;
623 txep_ptr->xfr_count = 0u;
624 txep_ptr->txn_count = 0u;
625
626 if(MSS_USB_XFR_BULK == txep_ptr->xfr_type)
627 {
628 /*
629 TODO: TxMaxP payload value MUST be an even number when dealing with
630 Multi packet Bulk transaction for proper interrupt generation.
631 This needs to be taken care.
632 */
633 if(length <= txep_ptr->fifo_size)
634 {
635 txep_ptr->txn_length = length;
636 }
637 else
638 {
639 txep_ptr->txn_length= txep_ptr->fifo_size;
640 }
641 }
642
643 /*
644 section 8.4.2.3 -- No support for HB in Interrupt transfers.
645 i.e. maximum one packet(num_usb_pkt=1)can be transferred in one frame.
646 */
647 else if(MSS_USB_XFR_INTERRUPT == txep_ptr->xfr_type)
648 {
649 if(length >= (txep_ptr->max_pkt_size * dpb))
650 {
651 txep_ptr->txn_length = (txep_ptr->max_pkt_size * dpb);
652 }
653 else
654 {
655 txep_ptr->txn_length = length;
656 }
657 }
658 else if(MSS_USB_XFR_ISO == txep_ptr->xfr_type)
659 {
660 if(length >= (txep_ptr->max_pkt_size * dpb))
661 {
662 txep_ptr->txn_length = (txep_ptr->max_pkt_size * dpb);
663 }
664 else
665 {
666 txep_ptr->txn_length = length;
667 }
668 }
669
670 MSS_USB_CIF_ep_write_pkt(txep_ptr->num,
671 txep_ptr->buf_addr,
672 txep_ptr->dma_enable,
673 txep_ptr->dma_channel,
674 txep_ptr->xfr_type,
675 txep_ptr->xfr_length,
676 txep_ptr->txn_length);
677 }
678 }
679
680 /***************************************************************************//**
681 See mss_usb_device.h for details of how to use this function.
682 */
683 void
MSS_USBD_get_hwcore_info(mss_usb_core_info_t * hw_info)684 MSS_USBD_get_hwcore_info
685 (
686 mss_usb_core_info_t* hw_info
687 )
688 {
689 MSS_USBD_CIF_get_hwcore_info(hw_info);
690 }
691
692 /***************************************************************************//**
693 See mss_usb_device.h for details of how to use this function.
694 */
695 uint8_t
MSS_USBD_get_dev_address(void)696 MSS_USBD_get_dev_address
697 (
698 void
699 )
700 {
701 return(MSS_USBD_CIF_get_dev_addr());
702 }
703
704 /***************************************************************************//**
705 See mss_usb_device.h for details of how to use this function.
706 */
707 void
MSS_USBD_set_isoupdate(void)708 MSS_USBD_set_isoupdate
709 (
710 void
711 )
712 {
713 MSS_USBD_CIF_set_isoupdate();
714 }
715
716 /***************************************************************************//**
717 See mss_usb_device.h for details of how to use this function.
718 */
719 void
MSS_USBD_clr_isoupdate(void)720 MSS_USBD_clr_isoupdate
721 (
722 void
723 )
724 {
725 MSS_USBD_CIF_clr_isoupdate();
726 }
727
728 /***************************************************************************//**
729 See mss_usb_device.h for details of how to use this function.
730 */
731 uint8_t
MSS_USBD_tx_ep_is_fifo_notempty(mss_usb_ep_num_t epnum)732 MSS_USBD_tx_ep_is_fifo_notempty
733 (
734 mss_usb_ep_num_t epnum
735 )
736 {
737 return(MSS_USB_CIF_is_txepfifo_notempty(epnum));
738 }
739
740 /***************************************************************************//**
741 See mss_usb_device.h for details of how to use this function.
742 */
743 uint8_t
MSS_USBD_rx_ep_is_fifo_full(mss_usb_ep_num_t epnum)744 MSS_USBD_rx_ep_is_fifo_full
745 (
746 mss_usb_ep_num_t epnum
747 )
748 {
749 return(MSS_USB_CIF_rx_ep_is_fifo_full(epnum));
750 }
751
752 /***************************************************************************//**
753 See mss_usb_device.h for details of how to use this function.
754 */
755 void
MSS_USBD_tx_ep_disable_irq(mss_usb_ep_num_t epnum)756 MSS_USBD_tx_ep_disable_irq
757 (
758 mss_usb_ep_num_t epnum
759 )
760 {
761 MSS_USB_CIF_tx_ep_disable_irq(epnum);
762 }
763
764 /***************************************************************************//**
765 See mss_usb_device.h for details of how to use this function.
766 */
767 void
MSS_USBD_rx_ep_disable_irq(mss_usb_ep_num_t epnum)768 MSS_USBD_rx_ep_disable_irq
769 (
770 mss_usb_ep_num_t epnum
771 )
772 {
773 MSS_USB_CIF_rx_ep_disable_irq(epnum);
774 }
775
776 /***************************************************************************//**
777 See mss_usb_device.h for details of how to use this function.
778 */
779 void
MSS_USBD_tx_ep_enable_irq(mss_usb_ep_num_t epnum)780 MSS_USBD_tx_ep_enable_irq
781 (
782 mss_usb_ep_num_t epnum
783 )
784 {
785 MSS_USB_CIF_tx_ep_enable_irq(epnum);
786 }
787
788 /***************************************************************************//**
789 See mss_usb_device.h for details of how to use this function.
790 */
791 void
MSS_USBD_rx_ep_enable_irq(mss_usb_ep_num_t epnum)792 MSS_USBD_rx_ep_enable_irq
793 (
794 mss_usb_ep_num_t epnum
795 )
796 {
797 MSS_USB_CIF_rx_ep_enable_irq(epnum);
798 }
799
800 /***************************************************************************//**
801 See mss_usb_device.h for details of how to use this function.
802 */
803 void
MSS_USBD_cep_enable_irq(void)804 MSS_USBD_cep_enable_irq
805 (
806 void
807 )
808 {
809 MSS_USB_CIF_cep_enable_irq();
810 }
811
812 /***************************************************************************//**
813 See mss_usb_device.h for details of how to use this function.
814 */
815 void
MSS_USBD_cep_disable_irq(void)816 MSS_USBD_cep_disable_irq
817 (
818 void
819 )
820 {
821 MSS_USB_CIF_cep_disable_irq();
822 }
823
824 /***************************************************************************//**
825 Private functions to this file (USBD Driver)
826 */
827
828 /***************************************************************************//**
829 This call-back function is executed on receiving SOF interrupt in Device mode.
830 */
831 static void
mss_usbd_sof_cb(uint8_t status)832 mss_usbd_sof_cb
833 (
834 uint8_t status
835 )
836 {
837 #ifndef MSS_USB_DEVICE_TEST_MODE
838 #else
839 usbd_test_cb.test_sof(status);
840 #endif
841
842 }
843
844 /******************************************************************************
845 This Callback function is executed when reset interrupt is received when
846 the MSS USB is in device mode.
847 */
848 static void
mss_usbd_reset_cb(void)849 mss_usbd_reset_cb
850 (
851 void
852 )
853 {
854 #ifndef MSS_USB_DEVICE_TEST_MODE
855 g_usbd_dev_conf.device_addr = 0x00u;
856 g_usbd_dev_conf.device_total_interfaces = 0x00u;
857 g_usbd_dev_conf.device_total_ep = 0x00u;
858 g_usbd_dev_conf.device_state = MSS_USB_DEFAULT_STATE;
859
860 gd_tx_ep[MSS_USB_CEP].state = MSS_USB_CEP_IDLE;
861 g_usbd_dev_conf.remote_wakeup = 0x00u;
862
863 if(MSS_USBD_CIF_is_hs_mode())
864 {
865 g_usbd_dev_conf.device_speed = MSS_USB_DEVICE_HS;
866 }
867 else
868 {
869 g_usbd_dev_conf.device_speed = MSS_USB_DEVICE_FS;
870 }
871
872 SETUP_PKT_INIT();
873
874 MSS_USBD_cep_configure(64u);
875 MSS_USBD_cep_read_prepare((uint8_t*)&g_setup_pkt, USB_SETUP_PKT_LEN);
876 #else
877 usbd_test_cb.test_reset();
878 #endif
879
880 }
881
882 /*******************************************************************************
883 This Callback function is executed when Setup packet is received on Control EP
884 */
885 static void
mss_usbd_cep_setup_cb(uint8_t status)886 mss_usbd_cep_setup_cb
887 (
888 uint8_t status
889 )
890 {
891 #ifndef MSS_USB_DEVICE_TEST_MODE
892 uint8_t* buf = 0;
893 uint32_t length = 0u;
894 uint8_t result = USB_FAIL;
895 mss_usb_ep_t* cep_ptr = &gd_tx_ep[MSS_USB_CEP];
896
897 if(status & (CTRL_EP_SETUP_END_ERROR | CTRL_EP_STALL_ERROR))
898 {
899 /*
900 EP0 was previously stalled, clear the error condition and
901 prepare for next transaction
902 */
903 cep_ptr->state = MSS_USB_CEP_IDLE;
904
905 SETUP_PKT_INIT();
906
907 MSS_USBD_cep_read_prepare((uint8_t*)&g_setup_pkt, USB_SETUP_PKT_LEN);
908 }
909 else
910 {
911 MSS_USBD_CIF_cep_read_pkt(cep_ptr);
912
913 if(SETUP_PKT_SIZE == cep_ptr->txn_length)
914 {
915 cep_ptr->state = MSS_USB_CEP_SETUP;
916
917 if(USB_STANDARD_REQUEST == (g_setup_pkt.request_type &
918 USB_STD_REQ_TYPE_MASK))
919 {
920 result = mss_usbd_std_requests(&buf, &length);
921 }
922 else if(USB_CLASS_REQUEST == (g_setup_pkt.request_type &
923 USB_STD_REQ_TYPE_MASK))
924 {
925 result = mss_usbd_class_requests(&buf, &length);
926 }
927 else if(USB_VENDOR_REQUEST == (g_setup_pkt.request_type &
928 USB_STD_REQ_TYPE_MASK))
929 {
930 result = mss_usbd_vendor_requests(&buf, &length);
931 }
932 }
933 else
934 {
935 ASSERT(0);/*in this transaction the txn_length must be SETUP_PKT_SIZE*/
936 }
937
938 if(result) //USB_SUCCESS
939 {
940 MSS_USBD_CIF_reset_index_reg();
941 if(0u == g_setup_pkt.length)
942 {
943 volatile uint32_t delay = 0;
944 MSS_USBD_CIF_cep_end_zdr(); //zdl complete
945
946 cep_ptr->state = MSS_USB_CEP_IDLE;
947 if(USB_STD_REQ_SET_ADDRESS == g_setup_pkt.request)
948 {
949 //special case SetAddress Request
950 for(delay = 0; delay < 5000 ; delay ++);
951 MSS_USBD_CIF_set_dev_addr(g_usbd_dev_conf.device_addr);
952 }
953
954 if((USB_STD_REQ_SET_FEATURE == g_setup_pkt.request) &&
955 (USB_STD_FEATURE_TEST_MODE == g_setup_pkt.value))
956 {
957 //let the current request status phase complete.
958 for(delay = 0; delay < 5000 ; delay ++);
959 switch(g_setup_pkt.index >> USB_WINDEX_HIBITE_SHIFT)
960 {
961 case USB_TEST_MODE_SELECTOR_TEST_J:
962 MSS_USB_CIF_start_testj();
963 break;
964
965 case USB_TEST_MODE_SELECTOR_TEST_K:
966 MSS_USB_CIF_start_testk();
967 break;
968
969 case USB_TEST_MODE_SELECTOR_TEST_SE0NAK:
970 MSS_USB_CIF_start_testse0nak();
971 break;
972
973 case USB_TEST_MODE_SELECTOR_TEST_PACKET:
974 MSS_USB_CIF_start_testpacket();
975 break;
976 default:
977 break;
978 }
979 }
980 else
981 {
982 SETUP_PKT_INIT();
983 MSS_USBD_cep_read_prepare((uint8_t*)&g_setup_pkt,
984 USB_SETUP_PKT_LEN);
985 }
986 }
987 else
988 {
989 //end of setup phase for Read/Write Req
990 MSS_USBD_CIF_cep_clr_rxpktrdy();
991
992 if((g_setup_pkt.request_type & USB_STD_REQ_DATA_DIR_MASK))
993 {
994 cep_ptr->state = MSS_USB_CEP_TX;
995 if((uint8_t*)0 == buf)
996 {
997 ASSERT(0);
998 }
999
1000 if(length > g_setup_pkt.length)
1001 {
1002 length = g_setup_pkt.length;
1003 }
1004
1005 MSS_USBD_cep_write(buf,length);
1006 }
1007 else
1008 {
1009 cep_ptr->state = MSS_USB_CEP_RX;
1010 MSS_USBD_cep_read_prepare(buf,length);
1011 }
1012 }
1013 }
1014 else
1015 {
1016 cep_ptr->state = MSS_USB_CEP_IDLE;
1017 SETUP_PKT_INIT();
1018 /*both servicedrxpktrdy and Sendstall should be set in setup phase*/
1019 MSS_USBD_CIF_cep_stall();
1020 }
1021 }
1022
1023 #else /*MSS_USB_DEVICE_TEST_MODE*/
1024 usbd_test_cb.test_cep_setup(status);
1025 #endif
1026 }
1027
1028 /*******************************************************************************
1029 This Callback function is called when Data packet is received on Control EP.
1030 DATAOUT Phase.
1031 */
1032 static void
mss_usbd_cep_rx_cb(uint8_t status)1033 mss_usbd_cep_rx_cb
1034 (
1035 uint8_t status
1036 )
1037 {
1038 #ifndef MSS_USB_DEVICE_TEST_MODE
1039
1040 uint32_t rem_length;
1041 mss_usb_ep_t* cep_ptr = &gd_tx_ep[MSS_USB_CEP];
1042
1043 /*
1044 xfr_rem_length should have been setup by the Specific request which needed
1045 data to arrive in the Data phase
1046 */
1047 if(status & (CTRL_EP_SETUP_END_ERROR | CTRL_EP_STALL_ERROR))
1048 {
1049 SETUP_PKT_INIT();
1050
1051 if(0 != g_usbd_class_cb->usbd_class_cep_rx_done)
1052 {
1053 g_usbd_class_cb->usbd_class_cep_rx_done(status);
1054 }
1055
1056 cep_ptr->state = MSS_USB_CEP_IDLE;
1057 MSS_USBD_cep_read_prepare((uint8_t*)&g_setup_pkt, USB_SETUP_PKT_LEN);
1058 }
1059 else
1060 {
1061 MSS_USBD_CIF_cep_read_pkt(cep_ptr);
1062
1063 if(MSS_USB_CEP_RX == cep_ptr->state)
1064 {
1065 if(cep_ptr->xfr_count == cep_ptr->xfr_length)
1066 {
1067 /*Call USBD-Class indicating that the desired data is arrived*/
1068 if(0 != g_usbd_class_cb->usbd_class_cep_rx_done)
1069 {
1070 g_usbd_class_cb->usbd_class_cep_rx_done(status);
1071 }
1072
1073 MSS_USBD_CIF_reset_index_reg();
1074 SETUP_PKT_INIT();
1075 cep_ptr->state = MSS_USB_CEP_IDLE;
1076 MSS_USBD_CIF_cep_end_wdr(); //WriteReq complete
1077 MSS_USBD_cep_read_prepare((uint8_t*)&g_setup_pkt,
1078 USB_SETUP_PKT_LEN);
1079
1080 }
1081 else if(cep_ptr->xfr_count < cep_ptr->xfr_length)
1082 {
1083 MSS_USBD_CIF_cep_clr_rxpktrdy(); //get more data from host
1084
1085 /*Continue to read data on the CEP*/
1086 rem_length = cep_ptr->xfr_length - cep_ptr->xfr_count;
1087
1088 if(rem_length >= cep_ptr->max_pkt_size)
1089 {
1090 cep_ptr->txn_length = cep_ptr->max_pkt_size;
1091 }
1092 else
1093 {
1094 cep_ptr->txn_length = rem_length;
1095 }
1096
1097 cep_ptr->txn_count = 0u;
1098 MSS_USBD_cep_read_prepare((cep_ptr->buf_addr+cep_ptr->xfr_count),
1099 cep_ptr->txn_length);
1100 }
1101 else
1102 {
1103 SETUP_PKT_INIT();
1104 cep_ptr->state = MSS_USB_CEP_IDLE;
1105 MSS_USBD_CIF_cep_stall(); //Send stall
1106 }
1107 }
1108 else
1109 {
1110 /*at this stage CEP stage must not be other than MSS_USB_CEP_RX*/
1111 ASSERT(0);
1112 }
1113 }
1114 #else /*MSS_USB_DEVICE_TEST_MODE*/
1115 usbd_test_cb.test_cep_rx(status);
1116 #endif
1117 }
1118
1119 /*******************************************************************************
1120 This Callback function is executed when Data packet is sent from control EP.
1121 DATAIN phase.
1122 */
1123 static void
mss_usbd_cep_tx_complete_cb(uint8_t status)1124 mss_usbd_cep_tx_complete_cb
1125 (
1126 uint8_t status
1127 )
1128 {
1129 #ifndef MSS_USB_DEVICE_TEST_MODE
1130
1131 mss_usb_ep_t* cep_ptr = &gd_tx_ep[MSS_USB_CEP];
1132
1133 /*
1134 xfr_rem_length should have been setup by the Specific request which needed
1135 data to arrive in the Data phase
1136 */
1137 if(status & (CTRL_EP_SETUP_END_ERROR | CTRL_EP_STALL_ERROR))
1138 {
1139 SETUP_PKT_INIT();
1140 if(0 != g_usbd_class_cb->usbd_class_cep_tx_done)
1141 {
1142 g_usbd_class_cb->usbd_class_cep_tx_done(status);
1143 }
1144 cep_ptr->state = MSS_USB_CEP_IDLE;
1145 MSS_USBD_cep_read_prepare((uint8_t*)&g_setup_pkt, USB_SETUP_PKT_LEN);
1146 }
1147 else
1148 {
1149 /*Device should be in DATAIN phase.*/
1150 if(MSS_USB_CEP_TX == cep_ptr->state)
1151 {
1152 if(cep_ptr->xfr_count < cep_ptr->xfr_length)
1153 {
1154 /*Continue to transmit more data*/
1155 cep_ptr->buf_addr += cep_ptr->txn_count;
1156
1157 if((cep_ptr->xfr_length - cep_ptr->xfr_count) >=
1158 cep_ptr->max_pkt_size)
1159 {
1160 cep_ptr->txn_length = cep_ptr->max_pkt_size;
1161 }
1162 else
1163 {
1164 cep_ptr->txn_length = (cep_ptr->xfr_length - cep_ptr->xfr_count);
1165 }
1166
1167 /*
1168 Reset the txn_count since one transaction out of the transfer
1169 is completed now
1170 */
1171 cep_ptr->txn_count = 0u;
1172 MSS_USBD_CIF_cep_write_pkt(cep_ptr);
1173 }
1174 else
1175 {
1176 /*Call USBD-Class indicating that the desired data is sent*/
1177 if(0 != g_usbd_class_cb->usbd_class_cep_tx_done)
1178 {
1179 g_usbd_class_cb->usbd_class_cep_tx_done(status);
1180 }
1181
1182 cep_ptr->txn_count = 0u;
1183 cep_ptr->xfr_count = 0u;
1184 cep_ptr->xfr_length = 0u;
1185 cep_ptr->txn_length = 0u;
1186 cep_ptr->state = MSS_USB_CEP_IDLE;
1187 SETUP_PKT_INIT();
1188 MSS_USBD_CIF_reset_index_reg();
1189 MSS_USBD_cep_read_prepare((uint8_t*)&g_setup_pkt,
1190 USB_SETUP_PKT_LEN);
1191 }
1192 }
1193 }
1194
1195 #else /*MSS_USB_DEVICE_TEST_MODE*/
1196 usbd_test_cb.test_cep_tx_complete(status);
1197 #endif
1198 }
1199
1200 /*******************************************************************************
1201 This Call-back function is executed when Data packet is received on RX EP
1202 */
1203 static void
mss_usbd_ep_rx_cb(mss_usb_ep_num_t ep_num,uint8_t status)1204 mss_usbd_ep_rx_cb
1205 (
1206 mss_usb_ep_num_t ep_num,
1207 uint8_t status
1208 )
1209 {
1210 uint8_t transfer_complete = 0u;
1211 mss_usb_ep_t* rxep_ptr = &gd_rx_ep[ep_num];
1212 uint32_t received_count = 0u;
1213
1214 /*
1215 While receiving data, xfr_length might be known to application or unknown.
1216 When xfr_length is not known, application is expected to provide
1217 size of(Receive_buffer) as xfr_length which is big enough to receive
1218 unknown data. End of data reception in this case is flagged by
1219 reception of short packet or ZLP(when xfr_length = n*maxpktsz).
1220 When xfr_length is known(via transport protocol), end of data reception
1221 is concluded when xfr_count = xfr_length. No zlp is expected to be
1222 received for the case of xfr_length = n*maxpktsz.
1223 When DMA-m1 is enabled, the control will return here only once after
1224 completion of transfer.
1225 */
1226 if(status & (RX_EP_OVER_RUN_ERROR | RX_EP_STALL_ERROR |
1227 RX_EP_DATA_ERROR | RX_EP_PID_ERROR | RX_EP_ISO_INCOMP_ERROR))
1228 {
1229 transfer_complete = 1;
1230 }
1231 else
1232 {
1233 if(MSS_USB_CIF_rx_ep_is_rxpktrdy(ep_num))
1234 {
1235 uint32_t increamented_addr;
1236
1237 received_count = (uint32_t)MSS_USB_CIF_rx_ep_read_count(ep_num);
1238
1239 if(DMA_ENABLE == rxep_ptr->dma_enable)
1240 {
1241 if(MSS_USB_DMA_MODE1 == (MSS_USB_CIF_rx_ep_get_dma_mode(ep_num)))
1242 {
1243 /*
1244 This means we are in BULK transfer with DMA mode1....
1245 all the rxmaxP size pkts are received and last short pkt
1246 need to be read without DMA or by switching to mode 0.
1247 After switching mode to 0, this ISR handler is invoked
1248 again. Data packet will be read then.
1249 MUSB: section 16
1250 */
1251
1252 /*read 'short packet' without DMA*/
1253 MSS_USB_CIF_dma_stop_xfr(rxep_ptr->dma_channel);
1254 MSS_USB_CIF_rx_ep_clr_autoclr(ep_num);
1255
1256 /*Count number of bytes read so far,since DMA was
1257 operating in m1 with Autoclr.*/
1258 increamented_addr = MSS_USB_CIF_dma_read_addr(rxep_ptr->dma_channel);
1259 rxep_ptr->xfr_count = (increamented_addr - (uint32_t)(rxep_ptr->buf_addr));
1260
1261 if(received_count)
1262 {
1263 MSS_USB_CIF_read_rx_fifo(ep_num,
1264 (rxep_ptr->buf_addr+rxep_ptr->xfr_count),
1265 received_count);
1266
1267 rxep_ptr->xfr_count += received_count;
1268 }
1269 transfer_complete = 1;
1270 }
1271 else
1272 {
1273 MSS_USB_CIF_dma_write_count(rxep_ptr->dma_channel, received_count);
1274 MSS_USB_CIF_dma_start_xfr(rxep_ptr->dma_channel);
1275 transfer_complete = 2u;
1276 /*Upper layer cb will be called from DMA ISR*/
1277 }
1278 }
1279 else // no dma
1280 {
1281 if(received_count)
1282 {
1283 MSS_USB_CIF_read_rx_fifo(ep_num,
1284 (rxep_ptr->buf_addr+rxep_ptr->xfr_count),
1285 received_count);
1286
1287 rxep_ptr->txn_count = received_count;
1288 rxep_ptr->xfr_count += received_count;
1289 }
1290 if(MSS_USB_XFR_BULK == rxep_ptr->xfr_type)
1291 {
1292 if(ADD_ZLP_TO_XFR == rxep_ptr->add_zlp)
1293 {
1294 if(rxep_ptr->xfr_count < rxep_ptr->xfr_length)
1295 {
1296 if(rxep_ptr->txn_count < rxep_ptr->max_pkt_size)
1297 {
1298 transfer_complete = 1;
1299 }
1300 else
1301 {
1302 /*receive short pkt.zlp when xfr_length is multiple of wMaxPktsz*/
1303 transfer_complete = 0;
1304 }
1305 }
1306 else if(rxep_ptr->xfr_count == rxep_ptr->xfr_length)
1307 {
1308 /*buffer is full*/
1309 transfer_complete = 1;
1310 }
1311 else
1312 {
1313 /*If xfr_count is more than xfr_lenght then something
1314 has seriously gone bad.*/
1315 ASSERT(0);
1316 }
1317 }
1318 else // no zlp
1319 {
1320 if(rxep_ptr->xfr_count == rxep_ptr->xfr_length)
1321 {
1322 transfer_complete = 1;
1323 }
1324 else if(rxep_ptr->xfr_count < rxep_ptr->xfr_length)
1325 {
1326 transfer_complete = 0;
1327 }
1328 else
1329 {
1330 /*If xfr_count is more than xfr_lenght then something
1331 has seriously gone bad.*/
1332 ASSERT(0);
1333 }
1334 }
1335 }
1336 else
1337 {
1338 /*
1339 For ISO and interrupt transfers, LB-expects only one packet in
1340 one transaction HB expects at most 3 packets in one transaction
1341 */
1342 if(rxep_ptr->txn_count <= rxep_ptr->max_pkt_size)
1343 {
1344 transfer_complete = 1;
1345 }
1346 else
1347 {
1348 ASSERT(0);/*TODO: replace this with stall EP*/
1349 }
1350 }
1351 }
1352 MSS_USB_CIF_rx_ep_clr_rxpktrdy(ep_num);
1353 }
1354 }
1355
1356 if(transfer_complete == 0)
1357 {
1358 ASSERT(rxep_ptr->xfr_length >= rxep_ptr->xfr_count);
1359
1360 if((rxep_ptr->xfr_length - rxep_ptr->xfr_count) >= rxep_ptr->max_pkt_size)
1361 {
1362 rxep_ptr->txn_length = rxep_ptr->max_pkt_size;
1363 }
1364 else
1365 {
1366 rxep_ptr->txn_length = (rxep_ptr->xfr_length - rxep_ptr->xfr_count);
1367 }
1368
1369 /*
1370 Reset the txn_count since one transaction out of the
1371 transfer is completed.
1372 */
1373 rxep_ptr->txn_count = 0u;
1374 MSS_USB_CIF_rx_ep_read_prepare(rxep_ptr->num,
1375 (rxep_ptr->buf_addr +
1376 rxep_ptr->xfr_count),
1377 rxep_ptr->dma_enable,
1378 rxep_ptr->dma_channel,
1379 rxep_ptr->xfr_type,
1380 rxep_ptr->xfr_length);
1381 }
1382 else if(transfer_complete == 1)
1383 {
1384 #ifndef MSS_USB_DEVICE_TEST_MODE
1385 if(0 != g_usbd_class_cb->usbd_class_rx_done)
1386 {
1387 g_usbd_class_cb->usbd_class_rx_done(ep_num, status, rxep_ptr->xfr_count);
1388 }
1389 #else
1390 usbd_test_cb.test_ep_rx(ep_num, status, rxep_ptr->xfr_count);
1391 #endif
1392 }
1393 else
1394 {
1395 /*Do Nothing. DMA m0 will be handled in DMA_Handler*/
1396 }
1397
1398 }
1399
1400 /******************************************************************************
1401 This Callback function is executed on transmission complete interrupt event when
1402 the MSS USB is in device mode.
1403 */
1404
1405
1406
1407
1408 static void
mss_usbd_ep_tx_complete_cb(mss_usb_ep_num_t num,uint8_t status)1409 mss_usbd_ep_tx_complete_cb
1410 (
1411 mss_usb_ep_num_t num,
1412 uint8_t status
1413 )
1414 {
1415 uint8_t transfer_complete = 0u;
1416 uint32_t increamented_addr = 0u;
1417 mss_usb_ep_t* txep_ptr = &gd_tx_ep[num];
1418
1419 if(status & TX_EP_STALL_ERROR)
1420 {
1421 transfer_complete = 1u;
1422 }
1423 else
1424 {
1425
1426
1427 /*
1428 While transmitting data we always know the xfr_length, use it to
1429 check if the transfer has ended.
1430
1431 DMA Enabled:
1432 Mode-m1 is used for Bulk transfer.In this case all the data
1433 single or multiple packets is handled by DMA since Autoset bit is set.
1434 Control will return here only once when complete data is transmitted.
1435 Mode-m0 is used for ISO/Interrupt transfers. In this case single packet
1436 should have been given by the application and control will return here
1437 only once after the single packet is transmitted.
1438
1439 DMA Disabled:
1440 For multi-packet bulk transfers, control will reach here after every
1441 single packet is transferred. Provide next packet for transmission till
1442 the end of data.
1443 */
1444 if(DMA_ENABLE == txep_ptr->dma_enable)
1445 {
1446 increamented_addr = MSS_USB_CIF_dma_read_addr(txep_ptr->dma_channel);
1447 txep_ptr->xfr_count = increamented_addr - ((uint32_t)txep_ptr->buf_addr);
1448
1449 ASSERT(txep_ptr->xfr_count == txep_ptr->xfr_length);
1450
1451 /*TODO: Move the decision to transmit ZLP from CIFL to here*/
1452 transfer_complete = 1u;
1453 }
1454 else /*DMA_DISABLE*/
1455 {
1456 txep_ptr->txn_count = txep_ptr->txn_length;
1457 txep_ptr->xfr_count += txep_ptr->txn_length;
1458
1459 if(MSS_USB_XFR_BULK == txep_ptr->xfr_type)
1460 {
1461 if(txep_ptr->xfr_count < txep_ptr->xfr_length)
1462 {
1463 transfer_complete = 0u;
1464 }
1465 else if(txep_ptr->xfr_count == txep_ptr->xfr_length)
1466 {
1467 if(ADD_ZLP_TO_XFR == txep_ptr->add_zlp)
1468 {
1469 if(0u == txep_ptr->txn_count)
1470 {
1471 transfer_complete = 1u;
1472 }
1473 else
1474 {
1475 if(txep_ptr->txn_count == txep_ptr->max_pkt_size)
1476 {
1477 transfer_complete = 0u;
1478 }
1479 else if(txep_ptr->txn_count < txep_ptr->max_pkt_size)
1480 {
1481 transfer_complete = 1u;
1482 }
1483 }
1484 }
1485 else //no zlp
1486 {
1487 transfer_complete = 1u;
1488 }
1489 }
1490 else
1491 {
1492 /*If xfr_count is more than xfr_lenght then something
1493 has seriously gone bad.*/
1494 ASSERT(0);
1495 }
1496 }
1497 else /*ISO/INTERRUPT XRF*/
1498 {
1499 if(txep_ptr->txn_count != txep_ptr->txn_length)
1500 {
1501 /*The ISO/Interrupt every transfer must be single transaction*/
1502 ASSERT(0);
1503 }
1504 transfer_complete = 1u;
1505 }
1506 }
1507 }
1508 if(1u == transfer_complete)
1509 {
1510 #ifndef MSS_USB_DEVICE_TEST_MODE
1511 if(0 != g_usbd_class_cb->usbd_class_tx_done)
1512 {
1513
1514 g_usbd_class_cb->usbd_class_tx_done(num, status);
1515 }
1516 #else /*MSS_USB_DEVICE_TEST_MODE*/
1517 usbd_test_cb.test_ep_tx_complete(num, status);
1518 #endif
1519 }
1520 else
1521 {
1522 /*
1523 Reset the txn_count since one transaction out of the transfer
1524 is completed now
1525 */
1526 txep_ptr->txn_count = 0u;
1527 ASSERT(txep_ptr->xfr_length >= txep_ptr->xfr_count);
1528
1529 if((txep_ptr->xfr_length - txep_ptr->xfr_count) >= txep_ptr->max_pkt_size)
1530 {
1531 txep_ptr->txn_length = txep_ptr->max_pkt_size;
1532 }
1533 else
1534 {
1535 txep_ptr->txn_length = (txep_ptr->xfr_length - txep_ptr->xfr_count);
1536 }
1537
1538 while(MSS_USB_CIF_is_txepfifo_notempty(txep_ptr->num));
1539 MSS_USB_CIF_ep_write_pkt(txep_ptr->num,
1540 (txep_ptr->buf_addr +
1541 txep_ptr->xfr_count),
1542 txep_ptr->dma_enable,
1543 txep_ptr->dma_channel,
1544 txep_ptr->xfr_type,
1545 txep_ptr->xfr_length,
1546 txep_ptr->txn_length);
1547 }
1548 }
1549
1550 /*******************************************************************************
1551 This Callback function is executed when suspend interrupt is received when
1552 the MSS USB is in device mode.
1553 */
1554 static void
mss_usbd_suspend_cb(void)1555 mss_usbd_suspend_cb
1556 (
1557 void
1558 )
1559 {
1560 #ifndef MSS_USB_DEVICE_TEST_MODE
1561 MSS_USB_CIF_enable_usbirq(RESUME_IRQ_MASK);
1562 g_usbd_dev_conf.device_state_at_suspend = g_usbd_dev_conf.device_state;
1563 g_usbd_dev_conf.device_state = MSS_USB_SUSPENDED_STATE;
1564 #else
1565 /*usbd_test_cb.test_suspend();*/
1566 #endif
1567 }
1568
1569 /*******************************************************************************
1570 This Callback function is executed when resume interrupt is received when the
1571 MSS USB is in device mode.
1572 */
1573 static void
mss_usbd_resume_cb(void)1574 mss_usbd_resume_cb
1575 (
1576 void
1577 )
1578 {
1579 #ifndef MSS_USB_DEVICE_TEST_MODE
1580 g_usbd_dev_conf.device_state = g_usbd_dev_conf.device_state_at_suspend;
1581 #else
1582 usbd_test_cb.test_resume();
1583 #endif
1584 }
1585
mss_usbd_disconnect_cb(void)1586 static void mss_usbd_disconnect_cb(void)
1587 {
1588 #ifndef MSS_USB_DEVICE_TEST_MODE
1589 g_usbd_dev_conf.device_state = MSS_USB_NOT_ATTACHED_STATE;
1590 MSS_USB_CIF_rx_ep_disable_irq_all();
1591 MSS_USB_CIF_tx_ep_disable_irq_all();
1592 PLIC_EnableIRQ(USB_DMA_PLIC);
1593 PLIC_EnableIRQ(USB_MC_PLIC);
1594 if(0 != g_usbd_class_cb->usbd_class_release)
1595 {
1596 g_usbd_class_cb->usbd_class_release(0xFF);
1597 }
1598 #else
1599 usbd_test_cb.test_resume();
1600 #endif
1601 }
1602 /*******************************************************************************
1603 This function processes standard USB requests.
1604 */
1605 static uint8_t
mss_usbd_std_requests(uint8_t ** buf,uint32_t * length)1606 mss_usbd_std_requests
1607 (
1608 uint8_t** buf,
1609 uint32_t* length
1610 )
1611 {
1612 uint8_t result = USB_FAIL;
1613 /*TODO:result should be used for all the functions*/
1614 switch(g_setup_pkt.request)
1615 {
1616 case USB_STD_REQ_GET_DESCRIPTOR:
1617 result = mss_usbd_get_descriptor(buf, length);
1618 return result;
1619
1620 case USB_STD_REQ_SET_ADDRESS:
1621 mss_usbd_set_address();
1622 break;
1623
1624 case USB_STD_REQ_SET_CONFIG:
1625 mss_usbd_set_config();
1626 break;
1627
1628 case USB_STD_REQ_GET_CONFIG:
1629 mss_usbd_get_config(buf, length);
1630 break;
1631
1632 case USB_STD_REQ_GET_STATUS:
1633 mss_usbd_get_status(buf, length);
1634 break;
1635
1636 case USB_STD_REQ_SET_FEATURE:
1637 result = mss_usbd_set_feature();
1638 return result;
1639
1640 case USB_STD_REQ_CLEAR_FEATURE:
1641 result = mss_usbd_clr_feature();
1642 return result;
1643
1644 case USB_STD_REQ_SET_DESCRIPTOR:
1645 mss_usbd_set_descriptor();
1646 break;
1647
1648 case USB_STD_REQ_SET_INTERFACE:
1649 g_usbd_dev_conf.active_interface_num = (uint8_t)g_setup_pkt.value;
1650 return USB_SUCCESS;
1651
1652 case USB_STD_REQ_GET_INTERFACE:
1653 *buf = &g_usbd_dev_conf.active_interface_num;
1654 *length = 1u;
1655 return USB_SUCCESS;
1656
1657 default:
1658 return USB_FAIL;
1659 }
1660
1661 return USB_SUCCESS;
1662 }
1663
1664 /*******************************************************************************
1665 This function processes USB class requests.
1666 */
1667 static uint8_t
mss_usbd_class_requests(uint8_t ** buf_pp,uint32_t * len_p)1668 mss_usbd_class_requests
1669 (
1670 uint8_t** buf_pp,
1671 uint32_t* len_p
1672 )
1673 {
1674 if((0 != g_usbd_class_cb->usbd_class_process_request))
1675 {
1676 if(USB_SUCCESS ==
1677 g_usbd_class_cb->usbd_class_process_request(&g_setup_pkt,
1678 buf_pp,
1679 len_p))
1680 {
1681 return USB_SUCCESS;
1682 }
1683 else
1684 {
1685 return USB_FAIL;
1686 }
1687 }
1688 else
1689 {
1690 return USB_FAIL;
1691 }
1692 }
1693
1694 /*******************************************************************************
1695 This function processes vendor defined USB requests.
1696 */
1697 static uint8_t
mss_usbd_vendor_requests(uint8_t ** buf_pp,uint32_t * len_p)1698 mss_usbd_vendor_requests
1699 (
1700 uint8_t** buf_pp,
1701 uint32_t* len_p
1702 )
1703 {
1704 if((0 != g_usbd_class_cb->usbd_class_process_request))
1705 {
1706 if(USB_SUCCESS ==
1707 g_usbd_class_cb->usbd_class_process_request(&g_setup_pkt,
1708 buf_pp,
1709 len_p))
1710 {
1711 return USB_SUCCESS;
1712 }
1713 else
1714 {
1715 return USB_FAIL;
1716 }
1717 }
1718 else
1719 {
1720 return USB_FAIL;
1721 }
1722 }
1723
1724 static uint8_t
mss_usbd_get_descriptor(uint8_t ** buf_pp,uint32_t * len_p)1725 mss_usbd_get_descriptor
1726 (
1727 uint8_t** buf_pp,
1728 uint32_t* len_p
1729 )
1730 {
1731 if((g_usbd_dev_conf.device_state >= MSS_USB_DEFAULT_STATE) &&
1732 (USB_STD_REQ_DATA_DIR_IN ==
1733 (g_setup_pkt.request_type & USB_STD_REQ_DATA_DIR_MASK)))
1734 {
1735 if(USB_STD_REQ_RECIPIENT_DEVICE ==
1736 (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK))
1737 {
1738 switch(g_setup_pkt.value >> USB_WVALUE_HIBITE_SHIFT)
1739 {
1740 case USB_DEVICE_DESCRIPTOR_TYPE:
1741
1742 /*descriptor Index and Index field should be zero*/
1743 if((0u == ((uint8_t)(g_setup_pkt.value))) &&
1744 (0u == g_setup_pkt.index) &&
1745 (0 != g_usbd_user_descr_cb->usbd_device_descriptor))
1746 {
1747 *buf_pp = g_usbd_user_descr_cb->usbd_device_descriptor(len_p);
1748 return(USB_SUCCESS);
1749 }
1750 else
1751 {
1752 return(USB_FAIL);
1753 }
1754
1755 case USB_STRING_DESCRIPTOR_TYPE:
1756
1757 /*
1758 * When descriptor index is 0, index field must be 0.
1759 * When descriptor index is >0, index field indicates Lang ID
1760 */
1761 if(0 != g_usbd_user_descr_cb->usbd_string_descriptor)
1762 {
1763 *buf_pp = g_usbd_user_descr_cb->usbd_string_descriptor
1764 ((uint8_t)g_setup_pkt.value, len_p);
1765 return(USB_SUCCESS);
1766 }
1767 else
1768 {
1769 return(USB_FAIL);
1770 }
1771
1772 case USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE:
1773 if(MSS_USB_DEVICE_HS == g_usbd_user_speed)
1774 {
1775 /*descriptor Index should be zero*/
1776 if((0 == ((uint8_t)(g_setup_pkt.value))) &&
1777 (0 == g_setup_pkt.index) &&
1778 (0 != g_usbd_user_descr_cb->usbd_device_qual_descriptor))
1779 {
1780 *buf_pp = g_usbd_user_descr_cb->usbd_device_qual_descriptor
1781 (g_usbd_dev_conf.device_speed, len_p);
1782 return(USB_SUCCESS);
1783 }
1784 else
1785 {
1786 return(USB_FAIL);
1787 }
1788 }
1789 else
1790 {
1791 /*Since User operates USBD at FS, Stall this request*/
1792 return(USB_FAIL);
1793 }
1794
1795 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
1796 if(0 != g_usbd_class_cb->usbd_class_get_descriptor)
1797 {
1798 *buf_pp = g_usbd_class_cb->usbd_class_get_descriptor
1799 (USB_STD_REQ_RECIPIENT_DEVICE,
1800 USB_CONFIGURATION_DESCRIPTOR_TYPE,
1801 len_p,
1802 g_usbd_dev_conf.device_speed);
1803 return(USB_SUCCESS);
1804 }
1805 else
1806 {
1807 return(USB_FAIL);
1808 }
1809
1810 case USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE:
1811 if(MSS_USB_DEVICE_HS == g_usbd_user_speed)
1812 {
1813 /*descriptor Index should be zero and Index field should be zero*/
1814 if((0u == ((uint8_t)(g_setup_pkt.value))) &&
1815 (0u == g_setup_pkt.index))
1816 {
1817 if(0 != g_usbd_class_cb->usbd_class_get_descriptor)
1818 {
1819 *buf_pp = g_usbd_class_cb->usbd_class_get_descriptor
1820 (USB_STD_REQ_RECIPIENT_DEVICE,
1821 USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE,
1822 len_p,
1823 g_usbd_dev_conf.device_speed);
1824
1825 return(USB_SUCCESS);
1826 }
1827 else
1828 {
1829 return(USB_FAIL);
1830 }
1831 }
1832 }
1833 else
1834 {
1835 /*Since User operates USBD at FS, Stall this request*/
1836 return(USB_FAIL);
1837 }
1838 break;
1839 default:
1840 return(USB_FAIL);
1841 }
1842
1843 }
1844 else if(USB_STD_REQ_RECIPIENT_INTERFACE ==
1845 (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK))
1846 {
1847 if(0 != g_usbd_class_cb->usbd_class_get_descriptor)
1848 {
1849 *buf_pp = g_usbd_class_cb->usbd_class_get_descriptor
1850 (USB_STD_REQ_RECIPIENT_INTERFACE,
1851 g_setup_pkt.value >> USB_WVALUE_HIBITE_SHIFT,
1852 len_p,
1853 g_usbd_dev_conf.device_speed);
1854 return(USB_SUCCESS);
1855 }
1856 else
1857 {
1858 return(USB_FAIL);
1859 }
1860 }
1861 else if(USB_STD_REQ_RECIPIENT_ENDPOINT ==
1862 (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK))
1863 {
1864 if((g_setup_pkt.value >> USB_WVALUE_HIBITE_SHIFT) &&
1865 (0 != g_usbd_class_cb->usbd_class_get_descriptor))
1866 {
1867 *buf_pp = g_usbd_class_cb->usbd_class_get_descriptor
1868 (USB_STD_REQ_RECIPIENT_ENDPOINT,
1869 (g_setup_pkt.value >> USB_WVALUE_HIBITE_SHIFT),
1870 len_p,
1871 g_usbd_dev_conf.device_speed);
1872 return(USB_SUCCESS);
1873 }
1874 else
1875 {
1876 return(USB_FAIL);
1877 }
1878 }
1879 }
1880 return USB_FAIL;
1881 }
1882
1883 /******************************************************************************
1884 This function sets the Device address sent by host in the MSS USB register.
1885 */
1886 static uint8_t
mss_usbd_set_address(void)1887 mss_usbd_set_address
1888 (
1889 void
1890 )
1891 {
1892 uint8_t addr;
1893
1894 /*USB device address is 7bit only*/
1895 addr = (uint8_t)(g_setup_pkt.value & 0x7Fu);
1896 g_usbd_dev_conf.device_addr = addr;
1897
1898 addr = (uint8_t)(g_setup_pkt.value & 0x7Fu);
1899
1900 /*USB2.0 section 9.4.6*/
1901 if(MSS_USB_CONFIGURED_STATE == g_usbd_dev_conf.device_state)
1902 {
1903 /*Behaviour not defined by USB2.0, May raise error here*/
1904 return USB_FAIL;
1905 }
1906 else
1907 {
1908 if(MSS_USB_DEFAULT_STATE == g_usbd_dev_conf.device_state)
1909 {
1910 if(0u != addr)
1911 {
1912 g_usbd_dev_conf.device_state = MSS_USB_ADDRESS_STATE;
1913 }
1914 }
1915 else if(MSS_USB_ADDRESS_STATE == g_usbd_dev_conf.device_state)
1916 {
1917 if(0u == addr)
1918 {
1919 g_usbd_dev_conf.device_state = MSS_USB_DEFAULT_STATE;
1920 }
1921 }
1922 /*FADDR register will be updated after Status phase of this Request*/
1923 }
1924 return USB_SUCCESS;
1925 }
1926
1927 /******************************************************************************
1928 This function processes SET_DESCRIPTOR request.
1929 */
1930 static uint8_t
mss_usbd_set_descriptor(void)1931 mss_usbd_set_descriptor
1932 (
1933 void
1934 )
1935 {
1936 return USB_FAIL;
1937 }
1938
1939 /******************************************************************************
1940 This function processes SET_CONFIGURATION request.
1941 */
1942 static uint8_t
mss_usbd_set_config(void)1943 mss_usbd_set_config
1944 (
1945 void
1946 )
1947 {
1948 uint8_t cfgidx;
1949
1950 cfgidx = (uint8_t)g_setup_pkt.value;
1951
1952 /*USB2.0 section 9.4.6*/
1953 if(MSS_USB_DEFAULT_STATE == g_usbd_dev_conf.device_state)
1954 {
1955 /*Undefined behaviour*/
1956 return USB_FAIL;
1957 }
1958 else
1959 {
1960 /*This value will be returned in Get_config command*/
1961 g_usbd_dev_conf.active_config_num = cfgidx;
1962 if(MSS_USB_ADDRESS_STATE == g_usbd_dev_conf.device_state)
1963 {
1964 if(cfgidx)
1965 {
1966 g_usbd_dev_conf.device_state = MSS_USB_CONFIGURED_STATE;
1967
1968 if(0 != g_usbd_class_cb->usbd_class_init)
1969 {
1970 g_usbd_class_cb->usbd_class_init(cfgidx,
1971 g_usbd_dev_conf.device_speed);
1972 }
1973 }
1974 }
1975 else if(MSS_USB_CONFIGURED_STATE == g_usbd_dev_conf.device_state)
1976 {
1977 if(0 == cfgidx)
1978 {
1979 g_usbd_dev_conf.device_state = MSS_USB_ADDRESS_STATE;
1980
1981 if(0 != g_usbd_class_cb->usbd_class_release)
1982 {
1983 g_usbd_class_cb->usbd_class_release(cfgidx);
1984 }
1985 }
1986 else
1987 {
1988 if(0 != g_usbd_class_cb->usbd_class_init)
1989 {
1990 g_usbd_class_cb->usbd_class_init(cfgidx,
1991 g_usbd_dev_conf.device_speed);
1992 }
1993 }
1994 }
1995 }
1996 return USB_SUCCESS;
1997 }
1998
1999 /******************************************************************************
2000 This function processes GET_CONFIGURATION requests
2001 */
2002 static uint8_t
mss_usbd_get_config(uint8_t ** buf_pp,uint32_t * len_p)2003 mss_usbd_get_config
2004 (
2005 uint8_t** buf_pp,
2006 uint32_t* len_p
2007 )
2008 {
2009 /*The field value and index must be 0 and length must be 1*/
2010 if((0u == g_setup_pkt.value) &&
2011 (0u == g_setup_pkt.index) &&
2012 (1u == g_setup_pkt.length))
2013 {
2014 /*This value was set in Set_config command*/
2015 *buf_pp = &g_usbd_dev_conf.active_config_num ;
2016 *len_p = 1u;
2017 return USB_SUCCESS;
2018 }
2019 else
2020 {
2021 return USB_FAIL;
2022 }
2023 }
2024
2025 /******************************************************************************
2026 This function processes GET_STATUS request.
2027 */
2028 static uint8_t
mss_usbd_get_status(uint8_t ** buf_pp,uint32_t * len_p)2029 mss_usbd_get_status
2030 (
2031 uint8_t** buf_pp,
2032 uint32_t* len_p
2033 )
2034 {
2035 if(MSS_USB_ADDRESS_STATE == g_usbd_dev_conf.device_state)
2036 {
2037 /*The field value and index must be 0 and length must be 2*/
2038 if((0u == g_setup_pkt.value) &&
2039 (0u == g_setup_pkt.index) &&
2040 (2u == g_setup_pkt.length))
2041 {
2042 switch (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK)
2043 {
2044 case USB_STD_REQ_RECIPIENT_DEVICE:
2045 /*SF2 device is always self powered. RemoteWakeup NotSupported*/
2046 g_usbd_status = 0x0001;
2047 *buf_pp = (uint8_t*)&g_usbd_status;
2048 break;
2049 case USB_STD_REQ_RECIPIENT_INTERFACE: /*Reserved by USB2.0*/
2050 g_usbd_status = 0x0000u;
2051 *buf_pp = (uint8_t*)&g_usbd_status;
2052 break;
2053
2054 case USB_STD_REQ_RECIPIENT_ENDPOINT:/*Endpoint halt (stall) status*/
2055 g_usbd_status = ((gd_tx_ep[g_setup_pkt.index].stall) ?
2056 0x0001:0x0000);
2057
2058 *buf_pp = (uint8_t*)&g_usbd_status;
2059 break;
2060 default:
2061 return USB_FAIL;
2062 }
2063 * len_p = sizeof(g_usbd_status);
2064 }
2065 else
2066 {
2067 return USB_FAIL;
2068 }
2069 }
2070 else if(MSS_USB_CONFIGURED_STATE == g_usbd_dev_conf.device_state)
2071 {
2072 if((0u == g_setup_pkt.value) && (2u == g_setup_pkt.length))
2073 {
2074 switch (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK)
2075 {
2076 case USB_STD_REQ_RECIPIENT_DEVICE: /*SF2 device is self powered*/
2077 g_usbd_status = 0x0001;
2078 g_usbd_status |= (g_usbd_dev_conf.remote_wakeup << 0x0001u);
2079 *buf_pp = (uint8_t*)&g_usbd_status;
2080
2081 break;
2082
2083 case USB_STD_REQ_RECIPIENT_INTERFACE:
2084 if(g_setup_pkt.index <= g_usbd_dev_conf.device_total_interfaces)
2085 {
2086 g_usbd_status = 0x0000u; /*Reserved by USB2.0*/
2087 *buf_pp = (uint8_t*)&g_usbd_status;
2088 }
2089 else
2090 {
2091 return USB_FAIL;
2092 }
2093 break;
2094 case USB_STD_REQ_RECIPIENT_ENDPOINT:
2095 if(((uint8_t)(g_setup_pkt.index) & 0x80u)) /* IN,TX endpoint*/
2096 {
2097 uint8_t idx = (((uint8_t)(g_setup_pkt.index))& 0x7fu);
2098 g_usbd_status = ((gd_tx_ep[idx].stall) ? 0x0001:0x0000) ;
2099 *buf_pp = (uint8_t*)&g_usbd_status;
2100 }
2101 else /*out, rx endpoint*/
2102 {
2103 g_usbd_status = ((gd_rx_ep[g_setup_pkt.index].stall) ?
2104 0x0001:0x0000) ;
2105 *buf_pp = (uint8_t*)&g_usbd_status;
2106 }
2107 break;
2108
2109 default:
2110 return USB_FAIL;
2111 }
2112 * len_p = sizeof(g_usbd_status);
2113 }
2114 else
2115 {
2116 return USB_FAIL;
2117 }
2118
2119 }
2120 else if(MSS_USB_DEFAULT_STATE == g_usbd_dev_conf.device_state)
2121 {
2122 return USB_FAIL;
2123 }
2124 return USB_SUCCESS;
2125 }
2126
2127 /******************************************************************************
2128 This function processes SET_FEATURE request.
2129 */
2130 static uint8_t
mss_usbd_set_feature(void)2131 mss_usbd_set_feature
2132 (
2133 void
2134 )
2135 {
2136 uint8_t result = USB_SUCCESS;
2137
2138 if(0u == g_setup_pkt.length)
2139 {
2140 switch(g_setup_pkt.value)
2141 {
2142 case USB_STD_FEATURE_REMOTE_WAKEUP:
2143 if((USB_STD_REQ_RECIPIENT_DEVICE ==
2144 (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK)) &&
2145 (g_usbd_dev_conf.device_state > MSS_USB_DEFAULT_STATE))
2146 {
2147 /*Enable Remote wakeup capability for the device*/
2148 g_usbd_dev_conf.remote_wakeup = 0x01u;
2149 ASSERT(0); //RemoteWakeup Not enabled by Config Descr.
2150 }
2151 else
2152 {
2153 result = USB_FAIL;
2154 }
2155 break;
2156 case USB_STD_FEATURE_EP_HALT:
2157 if((USB_STD_REQ_RECIPIENT_ENDPOINT ==
2158 (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK)) &&
2159 ((uint8_t)(g_setup_pkt.index)) &&
2160 (MSS_USB_CONFIGURED_STATE == g_usbd_dev_conf.device_state))
2161 {
2162 /*8.5.3.4 Control EP should not be implementing HALT feature*/
2163 if(((uint8_t)(g_setup_pkt.index) & 0x80u)) /* IN,TX endpoint*/
2164 {
2165 /*Enable HALT*/
2166 gd_tx_ep[(((uint8_t)(g_setup_pkt.index))& 0x7fu)].stall = 0x01;
2167 MSS_USBD_CIF_tx_ep_stall((mss_usb_ep_num_t)((((uint8_t)(g_setup_pkt.index)) & 0x7fu)));
2168 }
2169 else /*out, rx endpoint*/
2170 {
2171 /*Enable HALT*/
2172 gd_rx_ep[(uint8_t)(g_setup_pkt.index)].stall = 0x01;
2173 MSS_USBD_CIF_rx_ep_stall((mss_usb_ep_num_t)(g_setup_pkt.index));
2174 }
2175 }
2176 else
2177 {
2178 result = USB_FAIL;
2179 }
2180 break;
2181 case USB_STD_FEATURE_TEST_MODE:
2182 if((USB_STD_REQ_RECIPIENT_DEVICE ==
2183 (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK)) &&
2184 ((g_setup_pkt.index & 0xFF) == 0x00))
2185 {
2186 result = USB_SUCCESS;
2187 }
2188 else
2189 {
2190 result = USB_FAIL;
2191 }
2192 break;
2193
2194 default:
2195 result = USB_FAIL;
2196 break;
2197 }
2198 }
2199 else
2200 {
2201 result = USB_FAIL;
2202 }
2203 return result;
2204 }
2205
2206 /******************************************************************************
2207 This function processes CLEAR_FEATURE.
2208 */
2209 static uint8_t
mss_usbd_clr_feature(void)2210 mss_usbd_clr_feature
2211 (
2212 void
2213 )
2214 {
2215 uint8_t result = USB_SUCCESS;
2216
2217 /*TestMode Feature cant be cleared by clr_feature.Device power cycle required*/
2218 if((0u == g_setup_pkt.length) &&
2219 (g_usbd_dev_conf.device_state > MSS_USB_DEFAULT_STATE))
2220 {
2221 switch(g_setup_pkt.value)
2222 {
2223 case USB_STD_FEATURE_REMOTE_WAKEUP:
2224 if(USB_STD_REQ_RECIPIENT_DEVICE ==
2225 (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK))
2226 {
2227 /*Disable Remote wakeup capability for the device*/
2228 g_usbd_dev_conf.remote_wakeup = 0x00u;
2229 }
2230 break;
2231 case USB_STD_FEATURE_EP_HALT:
2232 if(USB_STD_REQ_RECIPIENT_ENDPOINT ==
2233 (g_setup_pkt.request_type & USB_STD_REQ_RECIPIENT_MASK))
2234 {
2235 /*8.5.3.4 HALT clear can be performed on CEP and Data EP*/
2236 if((uint8_t)(g_setup_pkt.index)) /*Not a control endpoint*/
2237 {
2238 if(((uint8_t)(g_setup_pkt.index) & 0x80u)) /* IN,TX endpoint*/
2239 {
2240 /*Disable HALT*/
2241 gd_tx_ep[(((uint8_t)(g_setup_pkt.index))& 0x7fu)].stall = 0x00u;
2242 MSS_USBD_CIF_tx_ep_clr_stall((mss_usb_ep_num_t)((((uint8_t)(g_setup_pkt.index)) & 0x7fu)));
2243 }
2244 else /* out, rx endpoint */
2245 {
2246 /*Enable HALT*/
2247 gd_rx_ep[(uint8_t)(g_setup_pkt.index)].stall = 0x00u;
2248 MSS_USBD_CIF_rx_ep_clr_stall((mss_usb_ep_num_t)(g_setup_pkt.index));
2249 }
2250 }
2251 else
2252 {
2253 result = USB_SUCCESS;
2254 }
2255 }
2256 break;
2257 default:
2258 result = USB_FAIL;
2259 break;
2260 }
2261 }
2262 else
2263 {
2264 result = USB_FAIL;
2265 }
2266 return result;
2267 }
2268
mss_usbd_dma_handler_cb(mss_usb_ep_num_t ep_num,mss_usb_dma_dir_t dma_dir,uint8_t status,uint32_t dma_addr_val)2269 static void mss_usbd_dma_handler_cb
2270 (
2271 mss_usb_ep_num_t ep_num,
2272 mss_usb_dma_dir_t dma_dir,
2273 uint8_t status,
2274 uint32_t dma_addr_val
2275 )
2276 {
2277 mss_usb_ep_t *ep_ptr = 0;
2278
2279 if(DMA_XFR_ERROR == status)
2280 {
2281 ASSERT(0);
2282 }
2283 else
2284 {
2285
2286 if(MSS_USB_DMA_READ == dma_dir) /*TX EP*/
2287 {
2288 ep_ptr = &gd_tx_ep[ep_num];
2289
2290 /*EP interrupt wont happen when */
2291 if((MSS_USB_DMA_MODE1 == (MSS_USB_CIF_tx_ep_get_dma_mode(ep_num))) &&
2292 (NO_ZLP_TO_XFR == ep_ptr->add_zlp) &&
2293 (ep_ptr->xfr_length) &&
2294 (!(ep_ptr->xfr_length % ep_ptr->max_pkt_size)))
2295 {
2296 /* wait till last TxMaxPkt size packet is sent.*/
2297 while(MSS_USB_CIF_tx_ep_is_txpktrdy(ep_num));
2298 ep_ptr->xfr_count = dma_addr_val - (uint32_t)ep_ptr->buf_addr;
2299
2300 if(0 != g_usbd_class_cb->usbd_class_tx_done)
2301 {
2302 /* call-back class driver */
2303 g_usbd_class_cb->usbd_class_tx_done(ep_num, status);
2304 }
2305 }
2306 else
2307 {
2308 MSS_USB_CIF_tx_ep_set_txpktrdy(ep_num);
2309 }
2310 }
2311 else if(MSS_USB_DMA_WRITE == dma_dir)/*RX EP*/
2312 {
2313 ep_ptr = &gd_rx_ep[ep_num];
2314
2315 if((NO_ZLP_TO_XFR == ep_ptr->add_zlp) &&
2316 (ep_ptr->xfr_length) &&
2317 (!(ep_ptr->xfr_length % ep_ptr->max_pkt_size)))
2318 {
2319 ep_ptr->xfr_count = dma_addr_val - (uint32_t)ep_ptr->buf_addr;
2320
2321 if(MSS_USB_DMA_MODE0 == (MSS_USB_CIF_rx_ep_get_dma_mode(ep_num)))
2322 {
2323 MSS_USB_CIF_rx_ep_clr_rxpktrdy(ep_num);
2324 }
2325 if(0 != g_usbd_class_cb->usbd_class_rx_done)
2326 {
2327 /* call-back class driver */
2328 g_usbd_class_cb->usbd_class_rx_done(ep_num, status,
2329 ep_ptr->xfr_count);
2330 }
2331 }
2332 }
2333 }
2334 }
2335
2336 #endif //MSS_USB_DEVICE_ENABLED
2337
2338 #ifdef __cplusplus
2339 }
2340 #endif
2341