xref: /freebsd/sys/dev/usb/usb_dynamic.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
502ac6454SAndrew Thompson  *
602ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
702ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
802ac6454SAndrew Thompson  * are met:
902ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1102ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1302ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1402ac6454SAndrew Thompson  *
1502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1602ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1702ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1802ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1902ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2002ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2102ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2202ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2302ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2402ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2502ac6454SAndrew Thompson  * SUCH DAMAGE.
2602ac6454SAndrew Thompson  */
2702ac6454SAndrew Thompson 
28d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
29d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
30d2b99310SHans Petter Selasky #else
31ed6d949aSAndrew Thompson #include <sys/stdint.h>
32ed6d949aSAndrew Thompson #include <sys/stddef.h>
33ed6d949aSAndrew Thompson #include <sys/param.h>
34ed6d949aSAndrew Thompson #include <sys/queue.h>
35ed6d949aSAndrew Thompson #include <sys/types.h>
36ed6d949aSAndrew Thompson #include <sys/systm.h>
37ed6d949aSAndrew Thompson #include <sys/kernel.h>
38ed6d949aSAndrew Thompson #include <sys/bus.h>
39ed6d949aSAndrew Thompson #include <sys/module.h>
40ed6d949aSAndrew Thompson #include <sys/lock.h>
41ed6d949aSAndrew Thompson #include <sys/mutex.h>
42ed6d949aSAndrew Thompson #include <sys/condvar.h>
43ed6d949aSAndrew Thompson #include <sys/sysctl.h>
44ed6d949aSAndrew Thompson #include <sys/sx.h>
45ed6d949aSAndrew Thompson #include <sys/unistd.h>
46ed6d949aSAndrew Thompson #include <sys/callout.h>
47ed6d949aSAndrew Thompson #include <sys/malloc.h>
48ed6d949aSAndrew Thompson #include <sys/priv.h>
49ed6d949aSAndrew Thompson 
5002ac6454SAndrew Thompson #include <dev/usb/usb.h>
51ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5402ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5502ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
5602ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
572c79a775SHans Petter Selasky #include <dev/usb/usb_request.h>
58d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
5902ac6454SAndrew Thompson 
6002ac6454SAndrew Thompson /* function prototypes */
61a593f6b8SAndrew Thompson static usb_handle_req_t usb_temp_get_desc_w;
62a593f6b8SAndrew Thompson static usb_temp_setup_by_index_t usb_temp_setup_by_index_w;
638d59ecb2SHans Petter Selasky #if USB_HAVE_COMPAT_LINUX
648d59ecb2SHans Petter Selasky static usb_linux_free_device_t usb_linux_free_device_w;
658d59ecb2SHans Petter Selasky #endif
66a593f6b8SAndrew Thompson static usb_temp_unsetup_t usb_temp_unsetup_w;
673f37fb62SHans Petter Selasky static usb_test_quirk_t usb_test_quirk_w;
68a593f6b8SAndrew Thompson static usb_quirk_ioctl_t usb_quirk_ioctl_w;
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson /* global variables */
71a593f6b8SAndrew Thompson usb_handle_req_t *usb_temp_get_desc_p = &usb_temp_get_desc_w;
72a593f6b8SAndrew Thompson usb_temp_setup_by_index_t *usb_temp_setup_by_index_p = &usb_temp_setup_by_index_w;
738d59ecb2SHans Petter Selasky #if USB_HAVE_COMPAT_LINUX
748d59ecb2SHans Petter Selasky usb_linux_free_device_t *usb_linux_free_device_p = &usb_linux_free_device_w;
758d59ecb2SHans Petter Selasky #endif
76a593f6b8SAndrew Thompson usb_temp_unsetup_t *usb_temp_unsetup_p = &usb_temp_unsetup_w;
77a593f6b8SAndrew Thompson usb_test_quirk_t *usb_test_quirk_p = &usb_test_quirk_w;
78a593f6b8SAndrew Thompson usb_quirk_ioctl_t *usb_quirk_ioctl_p = &usb_quirk_ioctl_w;
79ff182db6SHans Petter Selasky devclass_t usb_devclass_ptr;
8002ac6454SAndrew Thompson 
81e0a69b51SAndrew Thompson static usb_error_t
usb_temp_setup_by_index_w(struct usb_device * udev,uint16_t index)82a593f6b8SAndrew Thompson usb_temp_setup_by_index_w(struct usb_device *udev, uint16_t index)
8302ac6454SAndrew Thompson {
8402ac6454SAndrew Thompson 	return (USB_ERR_INVAL);
8502ac6454SAndrew Thompson }
8602ac6454SAndrew Thompson 
873f37fb62SHans Petter Selasky static uint8_t
usb_test_quirk_w(const struct usbd_lookup_info * info,uint16_t quirk)88a593f6b8SAndrew Thompson usb_test_quirk_w(const struct usbd_lookup_info *info, uint16_t quirk)
8902ac6454SAndrew Thompson {
9002ac6454SAndrew Thompson 	return (0);			/* no match */
9102ac6454SAndrew Thompson }
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson static int
usb_quirk_ioctl_w(unsigned long cmd,caddr_t data,int fflag,struct thread * td)94a593f6b8SAndrew Thompson usb_quirk_ioctl_w(unsigned long cmd, caddr_t data, int fflag, struct thread *td)
9502ac6454SAndrew Thompson {
9602ac6454SAndrew Thompson 	return (ENOIOCTL);
9702ac6454SAndrew Thompson }
9802ac6454SAndrew Thompson 
99e0a69b51SAndrew Thompson static usb_error_t
usb_temp_get_desc_w(struct usb_device * udev,struct usb_device_request * req,const void ** pPtr,uint16_t * pLength)100a593f6b8SAndrew Thompson usb_temp_get_desc_w(struct usb_device *udev, struct usb_device_request *req, const void **pPtr, uint16_t *pLength)
10102ac6454SAndrew Thompson {
10202ac6454SAndrew Thompson 	/* stall */
103459d369eSAndrew Thompson 	return (USB_ERR_STALLED);
10402ac6454SAndrew Thompson }
10502ac6454SAndrew Thompson 
10602ac6454SAndrew Thompson static void
usb_temp_unsetup_w(struct usb_device * udev)107a593f6b8SAndrew Thompson usb_temp_unsetup_w(struct usb_device *udev)
10802ac6454SAndrew Thompson {
1092c79a775SHans Petter Selasky 	usbd_free_config_desc(udev, udev->usb_template_ptr);
110a593f6b8SAndrew Thompson 	udev->usb_template_ptr = NULL;
11102ac6454SAndrew Thompson }
11202ac6454SAndrew Thompson 
1138d59ecb2SHans Petter Selasky #if USB_HAVE_COMPAT_LINUX
1148d59ecb2SHans Petter Selasky static void
usb_linux_free_device_w(struct usb_device * udev)1158d59ecb2SHans Petter Selasky usb_linux_free_device_w(struct usb_device *udev)
1168d59ecb2SHans Petter Selasky {
1178d59ecb2SHans Petter Selasky 	/* NOP */
1188d59ecb2SHans Petter Selasky }
1198d59ecb2SHans Petter Selasky #endif
1208d59ecb2SHans Petter Selasky 
12102ac6454SAndrew Thompson void
usb_quirk_unload(void * arg)122a593f6b8SAndrew Thompson usb_quirk_unload(void *arg)
12302ac6454SAndrew Thompson {
12402ac6454SAndrew Thompson 	/* reset function pointers */
12502ac6454SAndrew Thompson 
126a593f6b8SAndrew Thompson 	usb_test_quirk_p = &usb_test_quirk_w;
127a593f6b8SAndrew Thompson 	usb_quirk_ioctl_p = &usb_quirk_ioctl_w;
12802ac6454SAndrew Thompson 
12902ac6454SAndrew Thompson 	/* wait for CPU to exit the loaded functions, if any */
13002ac6454SAndrew Thompson 
13102ac6454SAndrew Thompson 	/* XXX this is a tradeoff */
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson 	pause("WAIT", hz);
13402ac6454SAndrew Thompson }
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson void
usb_temp_unload(void * arg)137a593f6b8SAndrew Thompson usb_temp_unload(void *arg)
13802ac6454SAndrew Thompson {
13902ac6454SAndrew Thompson 	/* reset function pointers */
14002ac6454SAndrew Thompson 
141a593f6b8SAndrew Thompson 	usb_temp_get_desc_p = &usb_temp_get_desc_w;
142a593f6b8SAndrew Thompson 	usb_temp_setup_by_index_p = &usb_temp_setup_by_index_w;
143a593f6b8SAndrew Thompson 	usb_temp_unsetup_p = &usb_temp_unsetup_w;
14402ac6454SAndrew Thompson 
14502ac6454SAndrew Thompson 	/* wait for CPU to exit the loaded functions, if any */
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson 	/* XXX this is a tradeoff */
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson 	pause("WAIT", hz);
15002ac6454SAndrew Thompson }
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson void
usb_bus_unload(void * arg)153a593f6b8SAndrew Thompson usb_bus_unload(void *arg)
15402ac6454SAndrew Thompson {
15502ac6454SAndrew Thompson 	/* reset function pointers */
15602ac6454SAndrew Thompson 
157a593f6b8SAndrew Thompson 	usb_devclass_ptr = NULL;
15802ac6454SAndrew Thompson 
15902ac6454SAndrew Thompson 	/* wait for CPU to exit the loaded functions, if any */
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson 	/* XXX this is a tradeoff */
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson 	pause("WAIT", hz);
16402ac6454SAndrew Thompson }
1658d59ecb2SHans Petter Selasky 
1668d59ecb2SHans Petter Selasky #if USB_HAVE_COMPAT_LINUX
1678d59ecb2SHans Petter Selasky void
usb_linux_unload(void * arg)1688d59ecb2SHans Petter Selasky usb_linux_unload(void *arg)
1698d59ecb2SHans Petter Selasky {
1708d59ecb2SHans Petter Selasky 	/* reset function pointers */
1718d59ecb2SHans Petter Selasky 
1728d59ecb2SHans Petter Selasky 	usb_linux_free_device_p = &usb_linux_free_device_w;
1738d59ecb2SHans Petter Selasky 
1748d59ecb2SHans Petter Selasky 	/* wait for CPU to exit the loaded functions, if any */
1758d59ecb2SHans Petter Selasky 
1768d59ecb2SHans Petter Selasky 	/* XXX this is a tradeoff */
1778d59ecb2SHans Petter Selasky 
1788d59ecb2SHans Petter Selasky 	pause("WAIT", hz);
1798d59ecb2SHans Petter Selasky }
1808d59ecb2SHans Petter Selasky #endif
181