xref: /netbsd/sys/compat/common/usb_subr_30.c (revision befa4c1f)
1*befa4c1fSpgoyette /*	$NetBSD: usb_subr_30.c,v 1.6 2019/12/12 02:15:42 pgoyette Exp $	*/
2cc17ee2eSpgoyette /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
3cc17ee2eSpgoyette 
4cc17ee2eSpgoyette /*
5cc17ee2eSpgoyette  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
6cc17ee2eSpgoyette  * All rights reserved.
7cc17ee2eSpgoyette  *
8cc17ee2eSpgoyette  * This code is derived from software contributed to The NetBSD Foundation
9cc17ee2eSpgoyette  * by Lennart Augustsson (lennart@augustsson.net) at
10cc17ee2eSpgoyette  * Carlstedt Research & Technology.
11cc17ee2eSpgoyette  *
12cc17ee2eSpgoyette  * Redistribution and use in source and binary forms, with or without
13cc17ee2eSpgoyette  * modification, are permitted provided that the following conditions
14cc17ee2eSpgoyette  * are met:
15cc17ee2eSpgoyette  * 1. Redistributions of source code must retain the above copyright
16cc17ee2eSpgoyette  *    notice, this list of conditions and the following disclaimer.
17cc17ee2eSpgoyette  * 2. Redistributions in binary form must reproduce the above copyright
18cc17ee2eSpgoyette  *    notice, this list of conditions and the following disclaimer in the
19cc17ee2eSpgoyette  *    documentation and/or other materials provided with the distribution.
20cc17ee2eSpgoyette  *
21cc17ee2eSpgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22cc17ee2eSpgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23cc17ee2eSpgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24cc17ee2eSpgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25cc17ee2eSpgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26cc17ee2eSpgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27cc17ee2eSpgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28cc17ee2eSpgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29cc17ee2eSpgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30cc17ee2eSpgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31cc17ee2eSpgoyette  * POSSIBILITY OF SUCH DAMAGE.
32cc17ee2eSpgoyette  */
33cc17ee2eSpgoyette 
34cc17ee2eSpgoyette #include <sys/cdefs.h>
35*befa4c1fSpgoyette __KERNEL_RCSID(0, "$NetBSD: usb_subr_30.c,v 1.6 2019/12/12 02:15:42 pgoyette Exp $");
36cc17ee2eSpgoyette 
37cc17ee2eSpgoyette #ifdef _KERNEL_OPT
38cc17ee2eSpgoyette #include "opt_compat_netbsd.h"
39cc17ee2eSpgoyette #include "opt_usb.h"
40cc17ee2eSpgoyette #endif
41cc17ee2eSpgoyette 
42cc17ee2eSpgoyette #include <sys/param.h>
43cc17ee2eSpgoyette #include <sys/systm.h>
44cc17ee2eSpgoyette #include <sys/kernel.h>
45cc17ee2eSpgoyette #include <sys/kmem.h>
46cc17ee2eSpgoyette #include <sys/device.h>
47cc17ee2eSpgoyette #include <sys/select.h>
48cc17ee2eSpgoyette #include <sys/proc.h>
49cc17ee2eSpgoyette 
50cc17ee2eSpgoyette #include <sys/bus.h>
51cc17ee2eSpgoyette #include <sys/module.h>
52cc17ee2eSpgoyette #include <sys/compat_stub.h>
53cc17ee2eSpgoyette 
54cc17ee2eSpgoyette #include <compat/common/compat_mod.h>
55cc17ee2eSpgoyette 
56cc17ee2eSpgoyette #include <dev/usb/usb.h>
57cc17ee2eSpgoyette 
58cc17ee2eSpgoyette #include <dev/usb/usbdi.h>
59cc17ee2eSpgoyette #include <dev/usb/usbdi_util.h>
60cc17ee2eSpgoyette #include <dev/usb/usbdivar.h>
61cc17ee2eSpgoyette #include <dev/usb/usbdevs.h>
62cc17ee2eSpgoyette #include <dev/usb/usb_quirks.h>
63cc17ee2eSpgoyette #include <dev/usb/usb_verbose.h>
64cc17ee2eSpgoyette #include <dev/usb/usbhist.h>
65cc17ee2eSpgoyette 
66cc17ee2eSpgoyette Static void usb_copy_old_devinfo(struct usb_device_info_old *,
67cc17ee2eSpgoyette     const struct usb_device_info *);
68cc17ee2eSpgoyette 
69cc17ee2eSpgoyette Static void
usb_copy_old_devinfo(struct usb_device_info_old * uo,const struct usb_device_info * ue)70cc17ee2eSpgoyette usb_copy_old_devinfo(struct usb_device_info_old *uo,
71cc17ee2eSpgoyette 		     const struct usb_device_info *ue)
72cc17ee2eSpgoyette {
73cc17ee2eSpgoyette 	const unsigned char *p;
74cc17ee2eSpgoyette 	unsigned char *q;
75cc17ee2eSpgoyette 	int i, n;
76cc17ee2eSpgoyette 
77cc17ee2eSpgoyette 	uo->udi_bus = ue->udi_bus;
78cc17ee2eSpgoyette 	uo->udi_addr = ue->udi_addr;
79cc17ee2eSpgoyette 	uo->udi_cookie = ue->udi_cookie;
80cc17ee2eSpgoyette 	for (i = 0, p = (const unsigned char *)ue->udi_product,
81cc17ee2eSpgoyette 	     q = (unsigned char *)uo->udi_product;
82cc17ee2eSpgoyette 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
83cc17ee2eSpgoyette 		if (*p < 0x80)
84cc17ee2eSpgoyette 			q[i++] = *p;
85cc17ee2eSpgoyette 		else {
86cc17ee2eSpgoyette 			q[i++] = '?';
87cc17ee2eSpgoyette 			if ((*p & 0xe0) == 0xe0)
88cc17ee2eSpgoyette 				p++;
89cc17ee2eSpgoyette 			p++;
90cc17ee2eSpgoyette 		}
91cc17ee2eSpgoyette 	}
92cc17ee2eSpgoyette 	q[i] = 0;
93cc17ee2eSpgoyette 
94cc17ee2eSpgoyette 	for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
95cc17ee2eSpgoyette 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
96cc17ee2eSpgoyette 		if (* p < 0x80)
97cc17ee2eSpgoyette 			q[i++] = *p;
98cc17ee2eSpgoyette 		else {
99cc17ee2eSpgoyette 			q[i++] = '?';
100cc17ee2eSpgoyette 			p++;
101cc17ee2eSpgoyette 			if ((*p & 0xe0) == 0xe0)
102cc17ee2eSpgoyette 				p++;
103cc17ee2eSpgoyette 		}
104cc17ee2eSpgoyette 	}
105cc17ee2eSpgoyette 	q[i] = 0;
106cc17ee2eSpgoyette 
107cc17ee2eSpgoyette 	memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
108cc17ee2eSpgoyette 
109cc17ee2eSpgoyette 	uo->udi_productNo = ue->udi_productNo;
110cc17ee2eSpgoyette 	uo->udi_vendorNo = ue->udi_vendorNo;
111cc17ee2eSpgoyette 	uo->udi_releaseNo = ue->udi_releaseNo;
112cc17ee2eSpgoyette 	uo->udi_class = ue->udi_class;
113cc17ee2eSpgoyette 	uo->udi_subclass = ue->udi_subclass;
114cc17ee2eSpgoyette 	uo->udi_protocol = ue->udi_protocol;
115cc17ee2eSpgoyette 	uo->udi_config = ue->udi_config;
116cc17ee2eSpgoyette 	uo->udi_speed = ue->udi_speed;
117cc17ee2eSpgoyette 	uo->udi_power = ue->udi_power;
118cc17ee2eSpgoyette 	uo->udi_nports = ue->udi_nports;
119cc17ee2eSpgoyette 
120cc17ee2eSpgoyette 	for (n=0; n<USB_MAX_DEVNAMES; n++)
121cc17ee2eSpgoyette 		memcpy(uo->udi_devnames[n],
122cc17ee2eSpgoyette 		       ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
123cc17ee2eSpgoyette 	memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
124cc17ee2eSpgoyette }
125cc17ee2eSpgoyette 
126cc17ee2eSpgoyette static int
usbd_fill_deviceinfo_old(struct usbd_device * dev,struct usb_device_info_old * di,int usedev,void (* do_devinfo_vp)(struct usbd_device *,char *,size_t,char *,size_t,int,int),int (* do_printBCD)(char * cp,size_t l,int bcd))127cc17ee2eSpgoyette usbd_fill_deviceinfo_old(struct usbd_device *dev,
128cc17ee2eSpgoyette     struct usb_device_info_old *di, int usedev,
129cc17ee2eSpgoyette     void (*do_devinfo_vp)(struct usbd_device *, char *, size_t, char *,
130cc17ee2eSpgoyette 	    size_t, int, int),
131cc17ee2eSpgoyette     int (*do_printBCD)(char *cp, size_t l, int bcd))
132cc17ee2eSpgoyette {
133cc17ee2eSpgoyette 	struct usbd_port *p;
1348a042863Schristos 	size_t i, j;
1358a042863Schristos 	int err;
136cc17ee2eSpgoyette 
137cc17ee2eSpgoyette 	di->udi_bus = device_unit(dev->ud_bus->ub_usbctl);
138cc17ee2eSpgoyette 	di->udi_addr = dev->ud_addr;
139cc17ee2eSpgoyette 	di->udi_cookie = dev->ud_cookie;
140cc17ee2eSpgoyette 	(*do_devinfo_vp)(dev, di->udi_vendor, sizeof(di->udi_vendor),
141cc17ee2eSpgoyette 	    di->udi_product, sizeof(di->udi_product), usedev, 0);
142cc17ee2eSpgoyette 	(*do_printBCD)(di->udi_release, sizeof(di->udi_release),
143cc17ee2eSpgoyette 	    UGETW(dev->ud_ddesc.bcdDevice));
144cc17ee2eSpgoyette 	di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor);
145cc17ee2eSpgoyette 	di->udi_productNo = UGETW(dev->ud_ddesc.idProduct);
146cc17ee2eSpgoyette 	di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice);
147cc17ee2eSpgoyette 	di->udi_class = dev->ud_ddesc.bDeviceClass;
148cc17ee2eSpgoyette 	di->udi_subclass = dev->ud_ddesc.bDeviceSubClass;
149cc17ee2eSpgoyette 	di->udi_protocol = dev->ud_ddesc.bDeviceProtocol;
150cc17ee2eSpgoyette 	di->udi_config = dev->ud_config;
151cc17ee2eSpgoyette 	di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power;
152cc17ee2eSpgoyette 	di->udi_speed = dev->ud_speed;
153cc17ee2eSpgoyette 
154cc17ee2eSpgoyette 	if (dev->ud_subdevlen > 0) {
155cc17ee2eSpgoyette 		for (i = 0, j = 0; i < dev->ud_subdevlen &&
156cc17ee2eSpgoyette 			     j < USB_MAX_DEVNAMES; i++) {
157cc17ee2eSpgoyette 			if (!dev->ud_subdevs[i])
158cc17ee2eSpgoyette 				continue;
159cc17ee2eSpgoyette 			strncpy(di->udi_devnames[j],
160cc17ee2eSpgoyette 			    device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN);
161cc17ee2eSpgoyette 			di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
162cc17ee2eSpgoyette 			j++;
163cc17ee2eSpgoyette 		}
164cc17ee2eSpgoyette 	} else {
165cc17ee2eSpgoyette 		j = 0;
166cc17ee2eSpgoyette 	}
167cc17ee2eSpgoyette 	for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
168cc17ee2eSpgoyette 		di->udi_devnames[j][0] = 0;		 /* empty */
169cc17ee2eSpgoyette 
170cc17ee2eSpgoyette 	if (!dev->ud_hub) {
171cc17ee2eSpgoyette 		di->udi_nports = 0;
172cc17ee2eSpgoyette 		return 0;
173cc17ee2eSpgoyette 	}
174cc17ee2eSpgoyette 
1758a042863Schristos 	const u_int nports = dev->ud_hub->uh_hubdesc.bNbrPorts;
176cc17ee2eSpgoyette 	for (i = 1; i <= __arraycount(di->udi_ports) && i <= nports;
177cc17ee2eSpgoyette 	     i++) {
178cc17ee2eSpgoyette 		p = &dev->ud_hub->uh_ports[i - 1];
179cc17ee2eSpgoyette 		if (p->up_dev)
180cc17ee2eSpgoyette 			err = p->up_dev->ud_addr;
181cc17ee2eSpgoyette 		else {
182cc17ee2eSpgoyette 			const int s = UGETW(p->up_status.wPortStatus);
183cc17ee2eSpgoyette 			if (s & UPS_PORT_ENABLED)
184cc17ee2eSpgoyette 				err = USB_PORT_ENABLED;
185cc17ee2eSpgoyette 			else if (s & UPS_SUSPEND)
186cc17ee2eSpgoyette 				err = USB_PORT_SUSPENDED;
187cc17ee2eSpgoyette 			else if (s & UPS_PORT_POWER)
188cc17ee2eSpgoyette 				err = USB_PORT_POWERED;
189cc17ee2eSpgoyette 			else
190cc17ee2eSpgoyette 				err = USB_PORT_DISABLED;
191cc17ee2eSpgoyette 		}
192cc17ee2eSpgoyette 		di->udi_ports[i - 1] = err;
193cc17ee2eSpgoyette 	}
194cc17ee2eSpgoyette 	di->udi_nports = nports;
195cc17ee2eSpgoyette 
196cc17ee2eSpgoyette 	return 0;
197cc17ee2eSpgoyette }
198cc17ee2eSpgoyette 
199cc17ee2eSpgoyette static int
usb_copy_to_old30(struct usb_event * ue,struct usb_event_old * ueo,struct uio * uio)200cc17ee2eSpgoyette usb_copy_to_old30(struct usb_event *ue, struct usb_event_old *ueo,
201cc17ee2eSpgoyette     struct uio *uio)
202cc17ee2eSpgoyette {
203cc17ee2eSpgoyette 
204cc17ee2eSpgoyette 	ueo->ue_type = ue->ue_type;
205cc17ee2eSpgoyette 	memcpy(&ueo->ue_time, &ue->ue_time, sizeof(struct timespec));
206cc17ee2eSpgoyette 	switch (ue->ue_type) {
207cc17ee2eSpgoyette 		case USB_EVENT_DEVICE_ATTACH:
208cc17ee2eSpgoyette 		case USB_EVENT_DEVICE_DETACH:
209cc17ee2eSpgoyette 			usb_copy_old_devinfo(&ueo->u.ue_device,
210cc17ee2eSpgoyette 			    &ue->u.ue_device);
211cc17ee2eSpgoyette 			break;
212cc17ee2eSpgoyette 
213cc17ee2eSpgoyette 		case USB_EVENT_CTRLR_ATTACH:
214cc17ee2eSpgoyette 		case USB_EVENT_CTRLR_DETACH:
215cc17ee2eSpgoyette 			ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
216cc17ee2eSpgoyette 			break;
217cc17ee2eSpgoyette 
218cc17ee2eSpgoyette 		case USB_EVENT_DRIVER_ATTACH:
219cc17ee2eSpgoyette 		case USB_EVENT_DRIVER_DETACH:
220cc17ee2eSpgoyette 			ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
221cc17ee2eSpgoyette 			memcpy(ueo->u.ue_driver.ue_devname,
222cc17ee2eSpgoyette 			    ue->u.ue_driver.ue_devname,
223cc17ee2eSpgoyette 			    sizeof(ue->u.ue_driver.ue_devname));
224cc17ee2eSpgoyette 			break;
225cc17ee2eSpgoyette 		default:
226cc17ee2eSpgoyette 			;
227cc17ee2eSpgoyette 	}
228cc17ee2eSpgoyette 
229cc17ee2eSpgoyette 	return 0;
230cc17ee2eSpgoyette }
231cc17ee2eSpgoyette 
232cc17ee2eSpgoyette void
usb_30_init(void)233cc17ee2eSpgoyette usb_30_init(void)
234cc17ee2eSpgoyette {
235cc17ee2eSpgoyette 
236*befa4c1fSpgoyette 	MODULE_HOOK_SET(usb_subr_fill_30_hook, usbd_fill_deviceinfo_old);
237*befa4c1fSpgoyette 	MODULE_HOOK_SET(usb_subr_copy_30_hook, usb_copy_to_old30);
238cc17ee2eSpgoyette }
239cc17ee2eSpgoyette 
240cc17ee2eSpgoyette void
usb_30_fini(void)241cc17ee2eSpgoyette usb_30_fini(void)
242cc17ee2eSpgoyette {
243cc17ee2eSpgoyette 
2441a480a4cSpgoyette 	MODULE_HOOK_UNSET(usb_subr_fill_30_hook);
2451a480a4cSpgoyette 	MODULE_HOOK_UNSET(usb_subr_copy_30_hook);
246cc17ee2eSpgoyette }
247