usb_workq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file usb_workq.c
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #include "usb_list.h"
  24. #include "usb_osal.h"
  25. #include "usb_workq.h"
  26. #include "usb_config.h"
  27. void usb_workqueue_submit(struct usb_workqueue *queue, struct usb_work *work, usb_worker_t worker, void *arg, uint32_t ticks)
  28. {
  29. size_t flags;
  30. flags = usb_osal_enter_critical_section();
  31. usb_dlist_remove(&work->list);
  32. work->worker = worker;
  33. work->arg = arg;
  34. if (ticks == 0) {
  35. usb_dlist_insert_after(&queue->work_list, &work->list);
  36. usb_osal_sem_give(queue->sem);
  37. }
  38. usb_osal_leave_critical_section(flags);
  39. }
  40. struct usb_workqueue g_hpworkq = { NULL };
  41. struct usb_workqueue g_lpworkq = { NULL };
  42. static void usbh_hpwork_thread(void *argument)
  43. {
  44. struct usb_work *work;
  45. size_t flags;
  46. int ret;
  47. struct usb_workqueue *queue = (struct usb_workqueue *)argument;
  48. while (1) {
  49. ret = usb_osal_sem_take(queue->sem, 0xffffffff);
  50. if (ret < 0) {
  51. continue;
  52. }
  53. flags = usb_osal_enter_critical_section();
  54. if (usb_dlist_isempty(&queue->work_list)) {
  55. usb_osal_leave_critical_section(flags);
  56. continue;
  57. }
  58. work = usb_dlist_first_entry(&queue->work_list, struct usb_work, list);
  59. usb_dlist_remove(&work->list);
  60. usb_osal_leave_critical_section(flags);
  61. work->worker(work->arg);
  62. }
  63. }
  64. static void usbh_lpwork_thread(void *argument)
  65. {
  66. struct usb_work *work;
  67. size_t flags;
  68. int ret;
  69. struct usb_workqueue *queue = (struct usb_workqueue *)argument;
  70. while (1) {
  71. ret = usb_osal_sem_take(queue->sem, 0xffffffff);
  72. if (ret < 0) {
  73. continue;
  74. }
  75. flags = usb_osal_enter_critical_section();
  76. if (usb_dlist_isempty(&queue->work_list)) {
  77. usb_osal_leave_critical_section(flags);
  78. continue;
  79. }
  80. work = usb_dlist_first_entry(&queue->work_list, struct usb_work, list);
  81. usb_dlist_remove(&work->list);
  82. usb_osal_leave_critical_section(flags);
  83. work->worker(work->arg);
  84. }
  85. }
  86. int usbh_workq_initialize(void)
  87. {
  88. #ifdef CONFIG_USBHOST_HIGH_WORKQ
  89. g_hpworkq.sem = usb_osal_sem_create(0);
  90. if (g_hpworkq.sem == NULL) {
  91. return -1;
  92. }
  93. g_hpworkq.thread = usb_osal_thread_create("usbh_hpworkq", CONFIG_USBHOST_HPWORKQ_STACKSIZE, CONFIG_USBHOST_HPWORKQ_PRIO, usbh_hpwork_thread, &g_hpworkq);
  94. if (g_hpworkq.thread == NULL) {
  95. return -1;
  96. }
  97. #endif
  98. #ifdef CONFIG_USBHOST_LOW_WORKQ
  99. g_lpworkq.sem = usb_osal_sem_create(0);
  100. if (g_lpworkq.sem == NULL) {
  101. return -1;
  102. }
  103. g_lpworkq.thread = usb_osal_thread_create("usbh_lpworkq", CONFIG_USBHOST_LPWORKQ_STACKSIZE, CONFIG_USBHOST_LPWORKQ_PRIO, usbh_lpwork_thread, &g_lpworkq);
  104. if (g_lpworkq.thread == NULL) {
  105. return -1;
  106. }
  107. #endif
  108. return 0;
  109. }