1 /* ----------------------------------------------------------------------------
2 * Copyright (c) Huawei Technologies Co., Ltd. 2016-2019. All rights reserved.
3 * Description: LiteOS USB Driver Generic Function
4 * Author: Yannik Li
5 * Create: 2021-02-04
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without specific prior written
15 * permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * --------------------------------------------------------------------------- */
28 /* ----------------------------------------------------------------------------
29 * Notice of Export Control Law
30 * ===============================================
31 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
32 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
33 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
34 * applicable export control laws and regulations.
35 * --------------------------------------------------------------------------- */
36
37 #include <poll.h>
38 #include "implementation/global_implementation.h"
39 #include "f_common.h"
40 #include "los_vm_iomap.h"
41
42 #ifdef __cplusplus
43 #if __cplusplus
44 //extern "C" {
45 #endif /* __cplusplus */
46 #endif /* __cplusplus */
47
48 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
49 #define GENERIC_SPEED_NUM 3
50
51 struct usb_generic_event {
52 union {
53 struct usb_device_request setup;
54 } u;
55
56 /* enum usb_generic_event_type */
57 uint8_t type;
58 };
59
60 struct mem_info {
61 const void *buf;
62 size_t len;
63 };
64
65 struct usb_descriptor_header {
66 uint8_t bLength;
67 uint8_t bDescriptorType;
68 } __attribute__ ((packed));
69
70 struct usb_desc_helper {
71 unsigned interfaces_count;
72 unsigned eps_count;
73 };
74
75 extern EVENT_CB_S eps_event_all;
76 extern uint32_t eps_event_mask;
77
get_unaligned_le16(uint16_t * val)78 static uint16_t get_unaligned_le16(uint16_t *val)
79 {
80 struct {
81 uint16_t val_;
82 } __attribute__((packed))*ptr = (void *)(val);
83 return le16toh(ptr->val_);
84 }
85
get_unaligned_le32(uint32_t * val)86 static uint32_t get_unaligned_le32(uint32_t *val)
87 {
88 struct {
89 uint32_t val_;
90 } __attribute__((packed))*ptr = (void *)(val);
91 return le32toh(ptr->val_);
92 }
93
CmpXchg(unsigned int * lock,int old,int new)94 static inline int CmpXchg(unsigned int *lock, int old, int new)
95 {
96 return LOS_AtomicCmpXchg32bits((Atomic *)lock, new, old);
97 }
98
clear_cancelled(enum generic_setup_state * state)99 static inline enum generic_setup_state clear_cancelled(enum generic_setup_state *state)
100 {
101 return (enum generic_setup_state)
102 CmpXchg(state, GENERIC_SETUP_CANCELLED, GENERIC_NO_SETUP);
103 }
104
generic_parse_single_desc(struct generic_dev_s * dev,uint32_t desc_idx,char * data,uint32_t len,void * priv)105 static int generic_parse_single_desc(struct generic_dev_s *dev, uint32_t desc_idx,
106 char *data, uint32_t len, void *priv)
107 {
108 struct usb_desc_helper *helper = priv;
109 struct usb_descriptor_header *desc = (void *)data;
110 uint8_t length = desc->bLength;
111
112 if ((len < sizeof(*desc)) || (len < length))
113 {
114 usb_err("%s: the length of descriptor is error\n", __FUNCTION__);
115 return -1;
116 }
117
118 /* Parse descriptor depending on type. */
119 switch (desc->bDescriptorType)
120 {
121 case UDESC_DEVICE:
122 case UDESC_CONFIG:
123 case UDESC_STRING:
124 case UDESC_DEVICE_QUALIFIER:
125 usb_err("%s: descriptor %d is not supported\n", __FUNCTION__,
126 desc->bDescriptorType);
127 return -1;
128
129 case UDESC_INTERFACE:
130 {
131 struct usb_interface_descriptor *ds = (void *)desc;
132 /* check the length of the descriptor */
133 if (length != sizeof(struct usb_interface_descriptor))
134 {
135 usb_err("%s: length is error, len=%d descriptor=%d\n",
136 __FUNCTION__, ds->bLength, ds->bDescriptorType);
137 return -1;
138 }
139 if (ds->bNumEndpoints > 0)
140 helper->interfaces_count++;
141 }
142 break;
143
144 case UDESC_ENDPOINT:
145 {
146 struct usb_endpoint_descriptor *ds = (void *)desc;
147 /* check the length of the descriptor */
148 if (length != sizeof(*ds))
149 {
150 usb_err("%s: length is error, len=%d descriptor=%d\n",
151 __FUNCTION__, ds->bLength, ds->bDescriptorType);
152 return -1;
153 }
154
155 /* assign endpoint address */
156 dev->eps_addrmap[helper->eps_count] = ds->bEndpointAddress;
157 dev->eps_descs[helper->eps_count][desc_idx] = ds;
158
159 helper->eps_count++;
160 if (dev->eps_count >= GENERIC_MAX_EPS_COUNT)
161 {
162 usb_err("%s: eps_count is too large\n", __FUNCTION__);
163 return -1;
164 }
165
166 }
167 break;
168
169 case UDESC_OTG:
170 struct usb_otg_descriptor *ds = (struct usb_otg_descriptor *)desc;
171 /* check the length of the descriptor */
172 if (length != sizeof(struct usb_otg_descriptor))
173 {
174 usb_err("%s: length is error, len=%d descriptor=%d\n",
175 __FUNCTION__, ds->bLength, ds->bDescriptorType);
176 return -1;
177 }
178 break;
179
180 case UDESC_IFACE_ASSOC:
181 {
182 struct usb_interface_assoc_descriptor *ds = (void *)desc;
183
184 if (length != sizeof(*ds))
185 {
186 usb_err("%s: length is error, len=%d descriptor=%d\n",
187 __FUNCTION__, ds->bLength, ds->bDescriptorType);
188 return -1;
189 }
190 }
191 break;
192
193 case UDESC_ENDPOINT_SS_COMP:
194 {
195 struct usb_endpoint_ss_comp_descriptor *ds = (struct usb_endpoint_ss_comp_descriptor *)desc;
196 /* check the length of the descriptor */
197 if (length != sizeof(struct usb_endpoint_ss_comp_descriptor))
198 {
199 usb_err("%s: length is error, len=%d descriptor=%d\n",
200 __FUNCTION__, ds->bLength, ds->bDescriptorType);
201 return -1;
202 }
203 }
204 break;
205
206 default:
207 break;
208 }
209
210 return length;
211 }
212
generic_parse_descs(struct generic_dev_s * dev,uint32_t desc_idx,uint32_t count,char * data,uint32_t len,void * priv)213 static int generic_parse_descs(struct generic_dev_s *dev, uint32_t desc_idx,
214 uint32_t count, char *data, uint32_t len, void *priv)
215 {
216 uint32_t _len = len;
217 uint32_t num = 0;
218
219 for (;;)
220 {
221 int ret;
222
223 if (num == count)
224 {
225 data = NULL;
226 }
227
228 if (data == NULL)
229 {
230 return _len - len;
231 }
232
233 ret = generic_parse_single_desc(dev, desc_idx, data, len, priv);
234 if (ret < 0)
235 {
236 usb_debug("%s: parse single desc error %d\n", __FUNCTION__, ret);
237 return ret;
238 }
239 len -= ret;
240 data += ret;
241 ++num;
242 }
243 }
244
generic_got_descs(struct generic_dev_s * dev,char * const buf,size_t len)245 static int generic_got_descs(struct generic_dev_s *dev,
246 char *const buf, size_t len)
247 {
248 char *data = buf, *raw_descs;
249 struct usb_desc_helper helper;
250 uint32_t counts[GENERIC_SPEED_NUM], flags;
251 uint32_t descLen[GENERIC_SPEED_NUM] = {0, 0, 0};
252 int ret, i;
253
254 if (get_unaligned_le32((uint32_t *)(data + 4)) != len)
255 {
256 usb_err("%s: len is error\n", __FUNCTION__);
257 return -1;
258 }
259
260 flags = get_unaligned_le32((uint32_t *)(data + 8));
261 if (flags & ~(GENERIC_HAS_FS_DESC |
262 GENERIC_HAS_HS_DESC |
263 GENERIC_HAS_SS_DESC |
264 GENERIC_ALL_CTRL_RECIP))
265 {
266 usb_err("%s: the flags %u is error\n", __FUNCTION__, flags);
267 return -1;
268 }
269 dev->descs_flags = flags;
270 data += 12;
271 len -= 12;
272
273 /* Read fs_count, hs_count and ss_count */
274 for (i = 0; i < GENERIC_SPEED_NUM; ++i)
275 {
276 if (!(flags & (1 << i)))
277 {
278 counts[i] = 0;
279 }
280 else if (len < 4)
281 {
282 return -1;
283 }
284 else
285 {
286 counts[i] = get_unaligned_le32((uint32_t *)data);
287 data += 4;
288 len -= 4;
289 }
290 }
291
292 /* Read descriptors */
293 raw_descs = data;
294 for (i = 0; i < GENERIC_SPEED_NUM; ++i)
295 {
296 if (!counts[i])
297 {
298 continue;
299 }
300 helper.interfaces_count = 0;
301 helper.eps_count = 0;
302 ret = generic_parse_descs(dev, i, counts[i], data, len, &helper);
303 if (ret < 0)
304 {
305 usb_err("%s: parse desc %d failed\n", __FUNCTION__, i);
306 return ret;
307 }
308 if (dev->eps_count == 0 && dev->interfaces_count == 0)
309 {
310 dev->eps_count = helper.eps_count;
311 dev->interfaces_count = helper.interfaces_count;
312 }
313 else
314 {
315 if (dev->eps_count != helper.eps_count)
316 {
317 return -1;
318 }
319 if (dev->interfaces_count != helper.interfaces_count)
320 {
321 return -1;
322 }
323 }
324 data += ret;
325 len -= ret;
326 descLen[i] = ret;
327 }
328
329 if ((raw_descs == data) || len)
330 {
331 usb_err("%s: no descs data\n", __FUNCTION__);
332 return -1;
333 }
334
335 dev->raw_descs_data = buf;
336 dev->raw_descs = raw_descs;
337 dev->raw_descs_length = data - raw_descs;
338 dev->fs_descs_count = counts[0];
339 dev->hs_descs_count = counts[1];
340 dev->ss_descs_count = counts[2];
341 dev->fs_descs_len = descLen[0];
342 dev->hs_descs_len = descLen[1];
343 dev->ss_descs_len = descLen[2];
344
345 return 0;
346 }
347
generic_got_strings(struct generic_dev_s * dev,char * const buf,size_t len)348 static int generic_got_strings(struct generic_dev_s *dev,
349 char *const buf, size_t len)
350 {
351 int ret;
352 char *data = buf;
353 size_t bytes = offsetof(struct generic_strings_head, length);
354 uint32_t str_count, str_count_all, lang_count;
355 struct generic_strings *dev_strings, *trav_strings;
356
357 if ((len < sizeof(struct generic_strings_head) ||
358 get_unaligned_le32((uint32_t *)data) != GENERIC_STRINGS_MAGIC ||
359 get_unaligned_le32((uint32_t *)(data + bytes)) != len))
360 {
361 return -1;
362 }
363
364 bytes = offsetof(struct generic_strings_head, str_count);
365 str_count = get_unaligned_le32((uint32_t *)(data + bytes));
366 bytes = offsetof(struct generic_strings_head, lang_count);
367 lang_count = get_unaligned_le32((uint32_t *)(data + bytes));
368 if ((!str_count != !lang_count) || (str_count == 0))
369 {
370 usb_err("%s: strings_count %u or lang_count %u is error\n",
371 __FUNCTION__, str_count, lang_count);
372 return -1;
373 }
374
375 dev_strings = usbm_zalloc(&dev->obj, sizeof(struct generic_strings));
376 if (dev_strings == NULL)
377 {
378 usb_err("%s: alloc dev_strings error\n", __FUNCTION__);
379 return -1;
380 }
381 bytes = sizeof(struct usbd_string) * (str_count + 1);
382 dev_strings->strings = usbm_zalloc(&dev->obj, bytes);
383 if (dev_strings == NULL)
384 {
385 usb_err("%s: alloc dev_strings error\n", __FUNCTION__);
386 return -1;
387 }
388
389 trav_strings = dev_strings;
390
391 data += sizeof(struct generic_strings_head);
392 len -= sizeof(struct generic_strings_head);
393
394 while (lang_count--)
395 {
396 struct usbd_string *strings = (struct usbd_string *)trav_strings->strings;
397 uint32_t id = 0;
398
399 if (len < 3)
400 {
401 goto error;
402 }
403
404 trav_strings->language = get_unaligned_le16((uint16_t *)data);
405
406 data += sizeof(uint16_t);
407 len -= sizeof(uint16_t);
408 str_count_all = str_count;
409 while (str_count--)
410 {
411 char *buff;
412 size_t length = strnlen(data, len);
413 if (length == len)
414 {
415 goto error;
416 }
417 buff = usbm_zalloc(&dev->obj, length + 1);
418 if (buff == NULL)
419 {
420 usb_err("%s: alloc dev_strings error\n", __FUNCTION__);
421 goto error;
422 }
423 ret = memcpy_s(buff, (length + 1), data, length);
424 if (ret != EOK)
425 {
426 usb_err("%s: copy string from user failed\n", __FUNCTION__);
427 goto error;
428 }
429 *(buff + length) = '\0';
430 strings->id = id++;
431 strings->s = buff;
432 ++strings;
433 data += (length + 1);
434 len -= (length + 1);
435 }
436 strings->id = 0;
437 strings->s = NULL;
438 ++trav_strings;
439 }
440
441 dev->raw_strings = data;
442 dev->dev_strings = dev_strings;
443 dev->strings_count = str_count_all;
444 return 0;
445
446 error:
447 usbm_free(&dev->obj, dev_strings);
448 return -1;
449 }
450
memory_area_match(struct usb_obj * obj,void * match_data)451 static int memory_area_match(struct usb_obj *obj, void *match_data)
452 {
453 struct usb_memory *usbm = container_of(obj, struct usb_memory, obj);
454 struct mem_info *mem = (struct mem_info *)match_data;
455 unsigned long buff_start = (unsigned long)mem->buf;
456 if (buff_start < usbm->vm_start &&
457 buff_start >= usbm->vm_start + usbm->size)
458 {
459 return 0;
460 }
461
462 return (buff_start + mem->len) <= (usbm->vm_start + usbm->size);
463 }
464
generic_find_memory_area(struct usb_obj * obj,const void * buf,size_t len)465 struct usb_memory *generic_find_memory_area(struct usb_obj *obj,
466 const void *buf, size_t len)
467 {
468 struct usb_obj *mem_obj;
469 struct mem_info mem = { buf, len };
470
471 mem_obj = usbobj_find(obj, memory_area_match, &mem);
472 if (mem_obj != NULL)
473 {
474 return container_of(mem_obj, struct usb_memory, obj);
475 }
476 return NULL;
477 }
478
generic_ep0_req_sync_complete(struct usbdev_ep_s * ep,struct usbdev_req_s * req)479 static void generic_ep0_req_sync_complete(struct usbdev_ep_s *ep, struct usbdev_req_s *req)
480 {
481 struct generic_dev_s *dev = (struct generic_dev_s *)req->priv;
482 int ret;
483
484 if (ep == NULL || req == NULL)
485 {
486 usb_err("Illegal request or ep!\n");
487 return;
488 }
489
490 ret = LOS_EventWrite(&dev->ep0_event, USB_GENERIC_COMPLETE_EVENT);
491 if (ret != OK)
492 {
493 usb_err("%s: write event failed!\r\n", __FUNCTION__);
494 }
495 dev->setup_state = GENERIC_NO_SETUP;
496 }
497
generic_ep0_submit_sync(struct generic_dev_s * dev,struct IoData * ioData)498 static int generic_ep0_submit_sync(struct generic_dev_s *dev, struct IoData *ioData)
499 {
500 struct usbdev_req_s *req = dev->ctrlreq;
501 uint32_t timeout = ioData->timeout > 0 ? ioData->timeout : LOS_WAIT_FOREVER;
502 int ret = -1;
503
504 req->priv = dev;
505 req->callback = generic_ep0_req_sync_complete;
506 req->len = ioData->len;
507 dev->cur_buff = ioData->buf;
508 (void)memcpy_s((void *)req->buf, USB_COMP_EP0_BUFSIZ, (void *)ioData->buf,
509 ioData->len);
510 ret = EP_SUBMIT(dev->usbdev->ep0, req);
511 if (ret != EOK)
512 {
513 usb_err("EP_SUBMIT failed, ret = %d\n", ret);
514 return -EFAULT;
515 }
516 ret = LOS_EventRead(&dev->ep0_event, USB_GENERIC_COMPLETE_EVENT,
517 LOS_WAITMODE_OR | LOS_WAITMODE_CLR, timeout);
518 if (ret == USB_GENERIC_COMPLETE_EVENT)
519 {
520 if (req->result == 0)
521 {
522 (void)memcpy_s((void *)ioData->buf, req->xfrd, (void *)req->buf, req->xfrd);
523 ret = (int)req->xfrd;
524 }
525 else
526 {
527 usb_err("ep failed, ret = %d\n", req->result);
528 return -EFAULT;
529 }
530 }
531 else
532 {
533 usb_err("read timeout, ret = %d\n", ret);
534 return -EFAULT;
535 }
536
537 return ret;
538 }
539
generic_ep0_complete(struct usbdev_ep_s * ep,struct usbdev_req_s * req)540 static void generic_ep0_complete(struct usbdev_ep_s *ep, struct usbdev_req_s *req)
541 {
542 int ret;
543 struct ep0_request *ep0req = NULL;
544 struct generic_dev_s *dev = NULL;
545 unsigned long flags;
546
547 if (ep == NULL || req == NULL)
548 {
549 usb_err("Illegal request or ep!\n");
550 return;
551 }
552 ep0req = (struct ep0_request *)req->priv;
553 if (ep0req == NULL)
554 {
555 usb_err("Illegal ep0req!\n");
556 return;
557 }
558 dev = (struct generic_dev_s *)ep0req->priv;
559 if (dev == NULL)
560 {
561 usb_err("Illegal dev!\n");
562 return;
563 }
564 ep0req->io_data.result = req->result;
565 ep0req->io_data.xfrd = req->xfrd;
566 ep0req->io_data.buf = ep0req->user_buf;
567 (void)memcpy_s((void *)ep0req->io_data.buf, req->xfrd, req->buf, req->xfrd);
568 spin_lock_irqsave(&dev->ep0_lock, flags);
569 list_add_tail(&ep0req->entry, &dev->ep0_comp_list);
570 ret = LOS_EventWrite(&eps_event_all, dev->event_mask);
571 if (ret != OK)
572 {
573 usb_err("%s: write event failed!\r\n", __FUNCTION__);
574 }
575 spin_unlock_irqrestore(&dev->ep0_lock, flags);
576 ret = LOS_EventWrite(&dev->ctrlreq_event, USB_GENERIC_COMPLETE_EVENT);
577 if (ret != OK)
578 {
579 usb_err("%s write event failed!\n", __FUNCTION__);
580 }
581 }
582
generic_ep0_submit_wait(struct ep0_request * ep0req)583 static int generic_ep0_submit_wait(struct ep0_request *ep0req)
584 {
585 struct generic_dev_s *dev = (struct generic_dev_s *)ep0req->priv;
586 struct usbdev_req_s *req = dev->ctrlreq;
587 int ret;
588
589 req->priv = ep0req;
590 req->callback = generic_ep0_complete;
591 (void)memcpy_s((void *)req->buf, USB_COMP_EP0_BUFSIZ, (void *)ep0req->io_data.buf,
592 ep0req->io_data.len);
593 req->len = ep0req->io_data.len;
594 dev->cur_buff = ep0req->io_data.buf;
595 ret = EP_SUBMIT(dev->usbdev->ep0, req);
596 if (ret != OK)
597 {
598 usb_err("generic_ep0_submit_wait response on err %d\n", ret);
599 req->result = OK;
600 return ret;
601 }
602 (void)LOS_EventRead(&dev->ctrlreq_event, USB_GENERIC_COMPLETE_EVENT,
603 LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
604 return req->result;
605 }
606
generic_ep0_read_events(struct generic_dev_s * dev,char * buffer,size_t buflen,int max_n)607 static ssize_t generic_ep0_read_events(struct generic_dev_s *dev,
608 char *buffer, size_t buflen, int max_n)
609 {
610 struct usb_generic_event events[USB_GENERIC_EVENTS_NUM];
611 size_t size;
612 unsigned int event_count = dev->efifo.in - dev->efifo.out;
613 unsigned int mask = USB_GENERIC_EVENTS_NUM - 1;
614 unsigned int i;
615 unsigned long flag;
616 int n;
617 int ret;
618
619 n = min(max_n, event_count);
620 size = n * sizeof(events[0]);
621 (void)memset_s(events, size, 0, size);
622 spin_lock_irqsave(&dev->event_lock, flag);
623 for (i = 0; i < n; i++)
624 {
625 events[i].type = dev->efifo.data[dev->efifo.out++ & mask];
626 if (events[i].type == GENERIC_SETUP)
627 {
628 events[i].u.setup = dev->setup;
629 dev->setup_state = GENERIC_SETUP_PENDING;
630 }
631 }
632
633 spin_unlock_irqrestore(&dev->event_lock, flag);
634 ret = memcpy_s(buffer, buflen, (const void *)events, size);
635 if (ret != EOK)
636 {
637 usb_err("copy failed\n");
638 return -EFAULT;
639 }
640
641 return 0;
642 }
643
generic_ep0_write_descs(struct generic_dev_s * dev,const char * buffer,size_t buflen)644 static ssize_t generic_ep0_write_descs(struct generic_dev_s *dev, const char *buffer, size_t buflen)
645 {
646 char *data;
647 ssize_t ret;
648
649 data = usbm_malloc(&dev->obj, buflen + 1);
650 if (data == NULL)
651 {
652 usb_err("%s malloc failed\n", __FUNCTION__);
653 return -1;
654 }
655
656 ret = memcpy_s(data, buflen + 1, buffer, buflen);
657 if (ret != EOK)
658 {
659 usb_err("copy failed\n");
660 goto err;
661 }
662 ret = generic_got_descs(dev, data, buflen);
663 if (ret)
664 {
665 goto err;
666 }
667 dev->state = GENERIC_READ_STRINGS;
668 return buflen;
669
670 err:
671 usbm_free(&dev->obj, data);
672 return -1;
673 }
674
generic_ep0_write_strings(struct generic_dev_s * dev,const char * buffer,size_t buflen)675 static ssize_t generic_ep0_write_strings(struct generic_dev_s *dev, const char *buffer, size_t buflen)
676 {
677 char *data;
678 ssize_t ret;
679
680 data = usbm_malloc(&dev->obj, buflen + 1);
681 if (data == NULL)
682 {
683 usb_err("%s malloc failed\n", __FUNCTION__);
684 return -1;
685 }
686 ret = memcpy_s(data, buflen + 1, buffer, buflen);
687 if (ret != EOK)
688 {
689 usb_err("copy failed\n");
690 goto err;
691 }
692 ret = generic_got_strings(dev, data, buflen);
693 if (ret)
694 {
695 goto err;
696 }
697
698 ret = generic_epfiles_create(dev);
699 if (ret)
700 {
701 dev->state = GENERIC_CLOSING;
702 goto err;
703 }
704 dev->state = GENERIC_ACTIVE;
705
706 // generic ready
707 if (dev->desc_ready)
708 {
709 dev->state = GENERIC_CLOSING;
710 goto err;
711 }
712 dev->desc_ready = true;
713 return buflen;
714
715 err:
716 usbm_free(&dev->obj, data);
717 return -1;
718 }
719
generic_ep0_write_request(struct generic_dev_s * dev,const char * buffer,size_t buflen)720 static ssize_t generic_ep0_write_request(struct generic_dev_s *dev, const char *buffer, size_t buflen)
721 {
722 struct usb_memory *usbm;
723 struct ep0_request *req = NULL;
724
725 usbm = generic_find_memory_area(&dev->memory_obj, buffer, buflen);
726 if (usbm == NULL)
727 {
728 usb_err("%s generic_find_memory_area\n",__FUNCTION__);
729 return -1;
730 }
731 if (!list_empty(&dev->ep0_req_list))
732 {
733 req = list_first_entry(&dev->ep0_req_list, struct ep0_request, entry);
734 if (req == NULL)
735 {
736 usb_err("%s get req failed\n",__FUNCTION__);
737 return -1;
738 }
739 list_del_init(&req->entry);
740 }
741 else
742 {
743 usb_err("%s get req failed\n",__FUNCTION__);
744 return -1;
745 }
746 req->io_data.buf = (uint32_t)buffer;
747 req->io_data.len = buflen;
748 req->user_buf = (uint32_t)buffer;
749 req->priv = dev;
750
751 return generic_ep0_submit_wait(req);
752 }
753
generic_ep0_cancel_pending_request(struct generic_dev_s * dev,uint32_t buffer)754 static int generic_ep0_cancel_pending_request(struct generic_dev_s *dev,
755 uint32_t buffer)
756 {
757 struct ep0_request *req_iter, *tmp;
758 bool found = false;
759 unsigned long flags;
760 int ret;
761
762 spin_lock_irqsave(&dev->ep0_lock, flags);
763 list_for_each_entry_safe(req_iter, tmp, &dev->ep0_req_list, entry)
764 {
765 if (req_iter->user_buf == buffer)
766 {
767 list_del_init(&req_iter->entry);
768 found = true;
769 break;
770 }
771 }
772 spin_unlock_irqrestore(&dev->ep0_lock, flags);
773
774 if (!found)
775 {
776 usb_err("%s: can not find the buffer\n", __FUNCTION__);
777 return -1;
778 }
779
780 req_iter->io_data.result = -USB_ERR_CANCELLED;
781 req_iter->io_data.xfrd = 0;
782
783 spin_lock_irqsave(&dev->ep0_lock, flags);
784 list_add_tail(&req_iter->entry, &dev->ep0_comp_list);
785 ret = LOS_EventWrite(&eps_event_all, dev->event_mask);
786 if (ret != OK)
787 {
788 usb_err("%s: write event failed!\r\n", __FUNCTION__);
789 }
790 spin_unlock_irqrestore(&dev->ep0_lock, flags);
791
792 return 0;
793 }
794
generic_ep0_request_status(struct generic_dev_s * dev,void * buffer)795 static int generic_ep0_request_status(struct generic_dev_s *dev, void *buffer)
796 {
797 struct generic_request *req_iter, *tmp;
798 unsigned long flags;
799
800 spin_lock_irqsave(&dev->ep0_lock, flags);
801 list_for_each_entry_safe(req_iter, tmp, &dev->ep0_comp_list, entry)
802 {
803 if (req_iter->user_buf == buffer)
804 {
805 spin_unlock_irqrestore(&dev->ep0_lock, flags);
806 return req_iter->io_data.result;
807 }
808 }
809 list_for_each_entry_safe(req_iter, tmp, &dev->ep0_req_list, entry)
810 {
811 if (req_iter->user_buf == buffer)
812 {
813 spin_unlock_irqrestore(&dev->ep0_lock, flags);
814 return USB_ERR_NOT_STARTED;
815 }
816 }
817 spin_unlock_irqrestore(&dev->ep0_lock, flags);
818
819 return -1;
820 }
821
generic_ep0_handle_open(struct usb_handle * handle)822 static int generic_ep0_handle_open(struct usb_handle *handle)
823 {
824 struct generic_dev_s *dev = (struct generic_dev_s *)handle->priv;
825
826 if (dev == NULL)
827 {
828 usb_err("Invalid private parameter!\n");
829 return -1;
830 }
831
832 if (dev->state == GENERIC_CLOSING)
833 {
834 usb_err("Device is closing!\n");
835 return -1;
836 }
837
838 generic_dev_opened(dev);
839
840 return 0;
841 }
842
generic_ep0_handle_close(struct usb_handle * handle)843 static int generic_ep0_handle_close(struct usb_handle *handle)
844 {
845 struct generic_dev_s *dev = (struct generic_dev_s *)handle->priv;
846
847 if (dev == NULL)
848 {
849 usb_err("Invalid private parameter!\n");
850 return -1;
851 }
852 generic_dev_closed(dev);
853
854 return 0;
855 }
856
generic_ep0_handle_read(struct usb_handle * handle,void * buffer,size_t len)857 static ssize_t generic_ep0_handle_read(struct usb_handle *handle, void *buffer, size_t len)
858 {
859 struct generic_dev_s *dev = (struct generic_dev_s *)handle->priv;
860 int n;
861 int ret;
862
863 if (clear_cancelled(&dev->setup_state) == GENERIC_SETUP_CANCELLED)
864 {
865 usb_err("the setup is cancelled\n");
866 return -1;
867 }
868
869 /* Acquire mutex */
870 mtx_lock(&dev->mutex);
871
872 /* Check state */
873 if (dev->state != GENERIC_ACTIVE)
874 {
875 mtx_unlock(&dev->mutex);
876 return -1;
877 }
878
879 switch (dev->setup_state)
880 {
881 case GENERIC_SETUP_CANCELLED:
882 ret = -1;
883 break;
884
885 case GENERIC_NO_SETUP:
886 n = len / sizeof(struct usb_generic_event);
887 if (!n)
888 {
889 ret = -EINVAL;
890 break;
891 }
892 uint32_t mask = 1 << (dev->minor_offset * EVENT_FOR_FUNCTION);
893 (VOID)LOS_EventRead(&eps_event_all, mask,
894 LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
895 ret = generic_ep0_read_events(dev, buffer, len, n);
896 break;
897
898 default:
899 ret = -1;
900 break;
901 }
902
903 mtx_unlock(&dev->mutex);
904 return ret;
905 }
906
generic_ep0_handle_write(struct usb_handle * handle,void * buffer,size_t len)907 static ssize_t generic_ep0_handle_write(struct usb_handle *handle, void *buffer, size_t len)
908 {
909 struct generic_dev_s *dev = (struct generic_dev_s *)handle->priv;
910 ssize_t ret;
911
912 if (clear_cancelled(&dev->setup_state) == GENERIC_SETUP_CANCELLED)
913 {
914 dprintf("[%s][%d] kotei clear_cancelled\n", __func__, __LINE__);
915 return -1;
916 }
917 if (dev == NULL)
918 {
919 dprintf("[%s][%d] dev is null\n", __func__, __LINE__);
920 return -1;
921 }
922 /* Acquire mutex */
923 mtx_lock(&dev->mutex);
924
925 switch (dev->state)
926 {
927 case GENERIC_READ_DESCRIPTORS:
928 ret = generic_ep0_write_descs(dev, buffer, len);
929 break;
930
931 case GENERIC_READ_STRINGS:
932 ret = generic_ep0_write_strings(dev, buffer, len);
933 break;
934
935 case GENERIC_ACTIVE:
936 ret = generic_ep0_write_request(dev, buffer, len);
937 break;
938
939 default:
940 ret = -1;
941 break;
942 }
943
944 mtx_unlock(&dev->mutex);
945 return ret;
946 }
947
generic_ep0_handle_ioctl(struct usb_handle * handle,int cmd,void * arg)948 static int generic_ep0_handle_ioctl(struct usb_handle *handle, int cmd, void *arg)
949 {
950 struct generic_dev_s *dev = (struct generic_dev_s *)handle->priv;
951 void *argp = (void *)arg;
952 struct usb_memory *usbm;
953 struct generic_memory mem;
954 unsigned long flags;
955 struct ep0_request *req;
956 struct ep0_io_data_ret io_ret;
957 int memcpy_len = 0;
958 int ret = -1;
959
960 switch (cmd)
961 {
962 case GENERIC_CMD_FREE_MEM:
963 ret = memcpy_s(&mem, sizeof(mem), argp, sizeof(mem));
964 if (ret != EOK)
965 {
966 usb_err("copy from user failed\n");
967 return -EFAULT;
968 }
969 usbm = generic_find_memory_area(&dev->memory_obj, (void *)mem.buf, mem.size);
970 if (usbm == NULL)
971 {
972 break;
973 }
974 usbobj_put(&usbm->obj);
975 usbm_free(&dev->obj, (void *)usbm->mem);
976 usbm_free(&dev->obj, usbm);
977 break;
978 case GENERIC_CMD_ENDPOINT_IO:
979 {
980 struct IoData ioData;
981 ret = memcpy_s(&ioData, sizeof(ioData), argp, sizeof(ioData));
982 if (ret != EOK)
983 {
984 usb_err("copy from user failed\n");
985 return -EFAULT;
986 }
987 ret = generic_ep0_submit_sync(dev, &ioData);
988 }
989 break;
990 case GENERIC_CMD_CANCEL_REQUEST:
991 ret = memcpy_s(&mem, sizeof(mem), argp, sizeof(mem));
992 if (ret != EOK)
993 {
994 usb_err("copy from user failed\n");
995 return -EFAULT;
996 }
997 if (dev->cur_buff == mem.buf)
998 {
999 ret = EP_CANCEL(dev->usbdev->ep0, dev->ctrlreq);
1000 }
1001 else
1002 {
1003 ret = generic_ep0_cancel_pending_request(dev, mem.buf);
1004 }
1005 break;
1006 case GENERIC_CMD_GET_REQ_STATUS:
1007 {
1008 struct IoData ioData;
1009 int ret;
1010
1011 ret = memcpy_s(&ioData, sizeof(ioData), argp, sizeof(ioData));
1012 if (ret != EOK)
1013 {
1014 usb_err("copy from user failed\n");
1015 return -EFAULT;
1016 }
1017 ret = generic_ep0_request_status(dev, (void *)ioData.buf);
1018 }
1019 break;
1020 case GENERIC_CMD_GET_EP0_EVENT:
1021 (VOID)LOS_EventRead(&eps_event_all, dev->event_mask,
1022 LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
1023 spin_lock_irqsave(&dev->ep0_lock, flags);
1024 if (!list_empty(&dev->ep0_comp_list))
1025 {
1026 req = list_first_entry(&dev->ep0_comp_list, struct ep0_request, entry);
1027 list_del_init(&req->entry);
1028 io_ret.buf = req->io_data.buf;
1029 io_ret.actual = req->io_data.xfrd;
1030 io_ret.status = req->io_data.result;
1031 memcpy_len = sizeof(struct generic_io_data_ret);
1032 ret = memcpy_s(arg, memcpy_len, &io_ret, memcpy_len);
1033 if (ret != EOK)
1034 {
1035 usb_err("copy failed\n");
1036 spin_unlock_irqrestore(&dev->ep0_lock, flags);
1037 return -EFAULT;
1038 }
1039 dev->setup_state = GENERIC_NO_SETUP;
1040 list_add_tail(&req->entry, &dev->ep0_req_list);
1041 }
1042 spin_unlock_irqrestore(&dev->ep0_lock, flags);
1043 break;
1044 default:
1045 if (dev && dev->usbdev->ops->ioctl)
1046 {
1047 ret = dev->usbdev->ops->ioctl(dev->usbdev, cmd, (unsigned long)arg);
1048 }
1049 break;
1050 }
1051
1052 return ret;
1053 }
1054
generic_ep0_handle_mmap(struct usb_handle * handle,uint32_t size)1055 static void *generic_ep0_handle_mmap(struct usb_handle *handle, uint32_t size)
1056 {
1057 struct generic_dev_s *dev = (struct generic_dev_s *)handle->priv;
1058 struct usb_memory *usbm;
1059 void *mem;
1060
1061 if (dev == NULL)
1062 {
1063 usb_err("Invalid private parameter!\n");
1064 return NULL;
1065 }
1066
1067 usbm = usbm_malloc(&dev->obj, sizeof(*usbm));
1068 if (usbm == NULL)
1069 {
1070 usb_err("%s alloc memory failed!\n", __FUNCTION__);
1071 return NULL;
1072 }
1073 usbobj_init(&usbm->obj, "usbm", usbobj_default_release);
1074 mem = usbm_malloc(&dev->obj, size);
1075 if (mem == NULL)
1076 {
1077 usb_err("%s alloc memory failed!\n", __FUNCTION__);
1078 return NULL;
1079 }
1080 usbm->mem = (unsigned long)mem;
1081 usbm->size = size;
1082 usbm->vm_start = (unsigned long)mem;
1083
1084 usbobj_add(&usbm->obj, &dev->memory_obj);
1085 return mem;
1086 }
1087
generic_ep0_handle_poll(struct usb_handle * handle,int timeout)1088 static int generic_ep0_handle_poll(struct usb_handle *handle, int timeout)
1089 {
1090 struct generic_dev_s *dev = (struct generic_dev_s *)handle->priv;
1091 uint32_t revents = POLLWRNORM;
1092 uint32_t ret;
1093 uint32_t ep0_event = 0;
1094
1095 if (dev == NULL)
1096 {
1097 usb_err("Invalid private parameter!\n");
1098 return -1;
1099 }
1100
1101 mtx_lock(&dev->mutex);
1102 ret = LOS_EventRead(&eps_event_all, eps_event_mask,
1103 LOS_WAITMODE_OR, timeout > 0 ? timeout:LOS_WAIT_FOREVER);
1104 switch (dev->state)
1105 {
1106 case GENERIC_READ_DESCRIPTORS:
1107 case GENERIC_READ_STRINGS:
1108 revents |= POLLOUT;
1109 break;
1110
1111 case GENERIC_ACTIVE:
1112 if (dev->setup_state == GENERIC_NO_SETUP)
1113 {
1114 ep0_event = (1 << (dev->minor_offset * EVENT_FOR_FUNCTION));
1115 if ((ret & ep0_event) == 0)
1116 {
1117 mtx_unlock(&dev->mutex);
1118 return revents;
1119 }
1120 if (dev->efifo.in - dev->efifo.out > 0)
1121 {
1122 dprintf("[%s] %s can POLLIN\n", __func__, handle->name);
1123 revents |= POLLIN;
1124 }
1125 }
1126 else
1127 {
1128 if ((ret & dev->event_mask) == 0)
1129 {
1130 mtx_unlock(&dev->mutex);
1131 return revents;
1132 }
1133 if (!list_empty(&dev->ep0_comp_list))
1134 {
1135 revents |= POLLOUT;
1136 dprintf("[%s] %s can POLLOUT\n", __func__, handle->name);
1137 }
1138 }
1139 break;
1140
1141 case GENERIC_CLOSING:
1142 case GENERIC_DEACTIVATED:
1143 break;
1144 }
1145
1146 mtx_unlock(&dev->mutex);
1147
1148 return (ssize_t)revents;
1149 }
1150
1151
1152 static struct UsbHandleOps generic_ep0_handle_ops = {
1153 .open = generic_ep0_handle_open,
1154 .close = generic_ep0_handle_close,
1155 .read = generic_ep0_handle_read,
1156 .write = generic_ep0_handle_write,
1157 .ioctl = generic_ep0_handle_ioctl,
1158 .mmap = generic_ep0_handle_mmap,
1159 .poll = generic_ep0_handle_poll,
1160 };
1161
generic_create_ep0(struct generic_dev_s * dev)1162 int generic_create_ep0(struct generic_dev_s *dev)
1163 {
1164 struct ep0_request *req = NULL;
1165 char *dev_name = NULL;
1166 unsigned long flags;
1167 int ret;
1168 int i;
1169
1170 dev_name = (char *)usbm_malloc(&dev->obj, GENERIC_PATH_LEN);
1171 if (dev_name == NULL)
1172 {
1173 usb_err("%s: malloc dev_name failed\n", __FUNCTION__);
1174 return -1;
1175 }
1176 (void)memset_s(dev_name, GENERIC_PATH_LEN, 0, GENERIC_PATH_LEN);
1177 ret = snprintf_s(dev_name, GENERIC_PATH_LEN, GENERIC_PATH_LEN - 1,
1178 "/dev/%s", dev->name);
1179 if (ret < 0)
1180 {
1181 usb_err("make directory path error! ret = %d, errono = %d\n",
1182 ret, get_errno());
1183 goto err;
1184 }
1185 ret = strncat_s(dev_name, GENERIC_PATH_LEN, "/ep0", strlen("/ep0"));
1186 if (ret != EOK)
1187 {
1188 usb_err("usb make ep0 file path error! ret = %d, errono = %d\n",
1189 ret, get_errno());
1190 goto err;
1191 }
1192 ret = register_handle(dev_name, &generic_ep0_handle_ops, (void *)dev, &dev->obj);
1193 if (ret != 0)
1194 {
1195 usb_err("chardev register failed!\n");
1196 goto err;
1197 }
1198 uint32_t mask = 1 << (dev->minor_offset * EVENT_FOR_FUNCTION + 1);
1199 eps_event_mask |= mask;
1200 dev->event_mask = mask;
1201 INIT_LIST_HEAD(&dev->ep0_comp_list);
1202 INIT_LIST_HEAD(&dev->ep0_req_list);
1203 for (i = 0; i < 0x2; i++)
1204 {
1205 req = usbm_malloc(&dev->obj, sizeof(*req));
1206 if (req == NULL)
1207 {
1208 usb_err("%s usbm_malloc\n",__FUNCTION__);
1209 return -1;
1210 }
1211 INIT_LIST_HEAD(&req->entry);
1212 spin_lock_irqsave(&dev->ep0_lock, flags);
1213 list_add_tail(&req->entry, &dev->ep0_req_list);
1214 spin_unlock_irqrestore(&dev->ep0_lock, flags);
1215 }
1216 return 0;
1217 err:
1218 usbm_free(&dev->obj, dev_name);
1219 return ret;
1220 }
1221
generic_remove_ep0(struct generic_dev_s * dev)1222 int generic_remove_ep0(struct generic_dev_s *dev)
1223 {
1224 struct ep0_request *req = NULL;
1225 struct ep0_request *tmp = NULL;
1226 char dev_name[GENERIC_PATH_LEN];
1227 int ret;
1228 unsigned long flags;
1229
1230 spin_lock_irqsave(&dev->ep0_lock, flags);
1231 list_for_each_entry_safe(req, tmp, &dev->ep0_req_list, entry)
1232 {
1233 list_del_init(&req->entry);
1234 usbm_free(&dev->obj, req);
1235 }
1236 list_for_each_entry_safe(req, tmp, &dev->ep0_comp_list, entry)
1237 {
1238 list_del_init(&req->entry);
1239 usbm_free(&dev->obj, req);
1240 }
1241 spin_unlock_irqrestore(&dev->ep0_lock, flags);
1242 ret = snprintf_s(dev_name, sizeof(dev_name), sizeof(dev_name) - 1,
1243 "/dev/%s/ep0", dev->name);
1244 if (ret < 0)
1245 {
1246 usb_err("usb make ep0 file path error! ret = %d, errono = %d\n",
1247 ret, get_errno());
1248 return ret;
1249 }
1250 ret = unregister_handle(dev_name);
1251 if (ret != OK)
1252 {
1253 usb_err("generic_dev unregister failed!\n");
1254 return ret;
1255 }
1256
1257 return ret;
1258 }
1259
1260 #ifdef __cplusplus
1261 #if __cplusplus
1262 //}
1263 #endif /* __cplusplus */
1264 #endif /* __cplusplus */
1265