1*222f252fSanton.\" $OpenBSD: usb_add_task.9,v 1.3 2021/01/19 16:05:59 anton Exp $ 22b6e0564Sawolk.\" 32b6e0564Sawolk.\" Copyright (c) 2016 Adam Wolk <awolk@openbsd.org> 42b6e0564Sawolk.\" 52b6e0564Sawolk.\" Permission to use, copy, modify, and distribute this software for any 62b6e0564Sawolk.\" purpose with or without fee is hereby granted, provided that the above 72b6e0564Sawolk.\" copyright notice and this permission notice appear in all copies. 82b6e0564Sawolk.\" 92b6e0564Sawolk.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 102b6e0564Sawolk.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 112b6e0564Sawolk.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 122b6e0564Sawolk.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 132b6e0564Sawolk.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 142b6e0564Sawolk.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 152b6e0564Sawolk.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 162b6e0564Sawolk.\" 17*222f252fSanton.Dd $Mdocdate: January 19 2021 $ 182b6e0564Sawolk.Dt USB_ADD_TASK 9 192b6e0564Sawolk.Os 202b6e0564Sawolk.Sh NAME 212b6e0564Sawolk.Nm usb_add_task , 222b6e0564Sawolk.Nm usb_rem_task , 232b6e0564Sawolk.Nm usb_wait_task , 242b6e0564Sawolk.Nm usb_rem_wait_task , 252b6e0564Sawolk.Nm usb_init_task 262b6e0564Sawolk.Nd USB task queues 272b6e0564Sawolk.Sh SYNOPSIS 282b6e0564Sawolk.In dev/usb/usbdi.h 292b6e0564Sawolk.Ft void 302b6e0564Sawolk.Fn usb_add_task "struct usbd_device *dev" "struct usb_task *task" 312b6e0564Sawolk.Ft void 322b6e0564Sawolk.Fn usb_rem_task "struct usbd_device *dev" "struct usb_task *task" 332b6e0564Sawolk.Ft void 342b6e0564Sawolk.Fn usb_wait_task "struct usbd_device *dev" "struct usb_task *task" 352b6e0564Sawolk.Ft void 362b6e0564Sawolk.Fn usb_rem_wait_task "struct usbd_device *dev" "struct usb_task *task" 372b6e0564Sawolk.Ft void 38*222f252fSanton.Fn usb_init_task "struct usb_task *task" "void (*fn)(void *)" "void *arg" "char type" 392b6e0564Sawolk.Sh DESCRIPTION 402b6e0564SawolkThe USB stack provides an API to manage task execution in a thread context at 41b6d746ddSjmcthe soonest opportunity. 42b6d746ddSjmcThere are three different task queues 43b6d746ddSjmcthat are executed on two separate threads. 44b6d746ddSjmcAll tasks executed via the USB task API are 452b6e0564Sawolkserialized with USB events such as device detachments. 462b6e0564Sawolk.Pp 472b6e0564SawolkThe 482b6e0564Sawolk.Fn usb_add_task 492b6e0564Sawolkfunction enqueues a task to be executed by the task thread. 502b6e0564SawolkSubsequent calls to 512b6e0564Sawolk.Fn usb_add_task 522b6e0564Sawolkwill not result in multiple executions being scheduled. 532b6e0564SawolkThe task structure must already be initialised by 542b6e0564Sawolk.Fn usb_init_task . 552b6e0564Sawolk.Pp 562b6e0564SawolkThe 572b6e0564Sawolk.Fn usb_rem_task 582b6e0564Sawolkfunction removes a scheduled task from its queue. 592b6e0564SawolkIf the work was already executed or has not been added to the task queue, 602b6e0564Sawolkthe call will have no effect. 612b6e0564SawolkCalling 622b6e0564Sawolk.Fn usb_rem_task 632b6e0564Sawolkwhile the task is executing will not abort it. 642b6e0564Sawolk.Pp 652b6e0564SawolkThe 662b6e0564Sawolk.Fn usb_wait_task 672b6e0564Sawolkfunction sleeps until the given task is neither queued for execution 682b6e0564Sawolknor currently running. 692b6e0564Sawolk.Pp 702b6e0564SawolkThe 712b6e0564Sawolk.Fn usb_rem_wait_task 722b6e0564Sawolkfunction is the equivalent of calling 732b6e0564Sawolk.Fn usb_rem_task 742b6e0564Sawolkfollowed by 752b6e0564Sawolk.Fn usb_wait_task . 762b6e0564Sawolk.Pp 772b6e0564SawolkThe 782b6e0564Sawolk.Fn usb_init_task 792b6e0564Sawolkmacro prepares the task structure 802b6e0564Sawolk.Fa task 812b6e0564Sawolkto be used in future calls to 822b6e0564Sawolk.Fn usb_add_task , 832b6e0564Sawolk.Fn usb_rem_task , 842b6e0564Sawolk.Fn usb_wait_task , 852b6e0564Sawolkand 862b6e0564Sawolk.Fn usb_rem_wait_task . 872b6e0564Sawolk.Fa task 882b6e0564Sawolkwill be prepared to call the function 892b6e0564Sawolk.Fa fn 902b6e0564Sawolkwith the argument specified by 912b6e0564Sawolk.Fa arg . 922b6e0564SawolkThe 932b6e0564Sawolk.Fa type 94b6d746ddSjmcof the task determines the queue 95b6d746ddSjmcand thread that will be used for its execution: 962b6e0564Sawolk.Bl -tag -width "USB_TASK_TYPE_EXPLORE" -offset indent 972b6e0564Sawolk.It Dv USB_TASK_TYPE_GENERIC 98b6d746ddSjmcTasks used for general work that need to happen in a process context. 992b6e0564Sawolk.It Dv USB_TASK_TYPE_EXPLORE 1002b6e0564SawolkDevice discovery tasks exploring the tree from the root. 1012b6e0564Sawolk.It Dv USB_TASK_TYPE_ABORT 1022b6e0564SawolkTasks of this type execute on a dedicated thread 1032b6e0564Sawolknot shared with other USB task types. 1042b6e0564Sawolk.El 1052b6e0564Sawolk.Pp 1062b6e0564SawolkMost drivers will only define tasks of type 1072b6e0564Sawolk.Dv USB_TASK_TYPE_GENERIC . 1082b6e0564SawolkNote that 1092b6e0564Sawolk.Dv USB_TASK_TYPE_ABORT 110b6d746ddSjmctasks are only used by host controller drivers 111b6d746ddSjmcand should never be used by drivers. 112b6d746ddSjmcOnce initialised, the 1132b6e0564Sawolk.Fa task 114b6d746ddSjmccan be used repeatedly in the API functions listed above 115b6d746ddSjmcand does not need to be reinitialised 116b6d746ddSjmcunless the function called and/or its argument must change. 1172b6e0564Sawolk.Sh CONTEXT 1182b6e0564Sawolk.Fn usb_task_add 1192b6e0564Sawolkcan be called from any context. 1202b6e0564Sawolk.Sh SEE ALSO 1212b6e0564Sawolk.Xr usb 4 , 1222b6e0564Sawolk.Xr task_add 9 123