xref: /netbsd-src/sys/dev/usb/uvisor.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: uvisor.c,v 1.57 2021/08/07 16:19:17 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net) at
9  * Carlstedt Research & Technology.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Handspring Visor (Palmpilot compatible PDA) driver
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: uvisor.c,v 1.57 2021/08/07 16:19:17 thorpej Exp $");
39 
40 #ifdef _KERNEL_OPT
41 #include "opt_usb.h"
42 #endif
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/device.h>
48 #include <sys/conf.h>
49 #include <sys/tty.h>
50 
51 #include <dev/usb/usb.h>
52 
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
55 #include <dev/usb/usbdevs.h>
56 
57 #include <dev/usb/ucomvar.h>
58 
59 #ifdef UVISOR_DEBUG
60 #define DPRINTF(x)	if (uvisordebug) printf x
61 #define DPRINTFN(n,x)	if (uvisordebug>(n)) printf x
62 int uvisordebug = 0;
63 #else
64 #define DPRINTF(x)
65 #define DPRINTFN(n,x)
66 #endif
67 
68 #define UVISOR_CONFIG_INDEX	0
69 #define UVISOR_IFACE_INDEX	0
70 
71 /* From the Linux driver */
72 /*
73  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
74  * are available to be transferred to the host for the specified endpoint.
75  * Currently this is not used, and always returns 0x0001
76  */
77 #define UVISOR_REQUEST_BYTES_AVAILABLE		0x01
78 
79 /*
80  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
81  * is now closing the pipe. An empty packet is sent in response.
82  */
83 #define UVISOR_CLOSE_NOTIFICATION		0x02
84 
85 /*
86  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
87  * get the endpoints used by the connection.
88  */
89 #define UVISOR_GET_CONNECTION_INFORMATION	0x03
90 
91 
92 /*
93  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
94  */
95 #define UVISOR_MAX_CONN 8
96 struct uvisor_connection_info {
97 	uWord	num_ports;
98 	struct {
99 		uByte	port_function_id;
100 		uByte	port;
101 	} connections[UVISOR_MAX_CONN];
102 };
103 #define UVISOR_CONNECTION_INFO_SIZE 18
104 
105 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
106 #define UVISOR_FUNCTION_GENERIC		0x00
107 #define UVISOR_FUNCTION_DEBUGGER	0x01
108 #define UVISOR_FUNCTION_HOTSYNC		0x02
109 #define UVISOR_FUNCTION_CONSOLE		0x03
110 #define UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
111 
112 /*
113  * Unknown PalmOS stuff.
114  */
115 #define UVISOR_GET_PALM_INFORMATION		0x04
116 #define UVISOR_GET_PALM_INFORMATION_LEN		0x44
117 
118 struct uvisor_palm_connection_info {
119 	uByte	num_ports;
120 	uByte	endpoint_numbers_different;
121 	uWord	reserved1;
122 	struct {
123 		uDWord	port_function_id;
124 		uByte	port;
125 		uByte	end_point_info;
126 		uWord	reserved;
127 	} connections[UVISOR_MAX_CONN];
128 };
129 
130 
131 
132 #define UVISORIBUFSIZE 64
133 #define UVISOROBUFSIZE 1024
134 
135 struct uvisor_softc {
136 	device_t		sc_dev;		/* base device */
137 	struct usbd_device *	sc_udev;	/* device */
138 	struct usbd_interface *	sc_iface;	/* interface */
139 
140 	device_t		sc_subdevs[UVISOR_MAX_CONN];
141 	int			sc_numcon;
142 
143 	uint16_t		sc_flags;
144 
145 	bool			sc_dying;
146 };
147 
148 static usbd_status uvisor_init(struct uvisor_softc *,
149 			       struct uvisor_connection_info *,
150 			       struct uvisor_palm_connection_info *);
151 
152 static int uvisor_open(void *, int);
153 static void uvisor_close(void *, int);
154 
155 static const struct ucom_methods uvisor_methods = {
156 	.ucom_open = uvisor_open,
157 	.ucom_close = uvisor_close,
158 };
159 
160 struct uvisor_type {
161 	struct usb_devno	uv_dev;
162 	uint16_t		uv_flags;
163 #define PALM4	0x0001
164 #define VISOR	0x0002
165 
166 };
167 static const struct uvisor_type uvisor_devs[] = {
168 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR },
169 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 },
170 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600 }, PALM4 },
171 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
172 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
173 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
174 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 },
175 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
176 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 },
177 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 },
178 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 },
179 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE31 }, PALM4 },
180 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
181 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
182 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 },
183 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 },
184 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 },
185 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_35 }, 0 },
186 /*	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
187 };
188 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
189 
190 static int	uvisor_match(device_t, cfdata_t, void *);
191 static void	uvisor_attach(device_t, device_t, void *);
192 static void	uvisor_childdet(device_t, device_t);
193 static int	uvisor_detach(device_t, int);
194 
195 CFATTACH_DECL2_NEW(uvisor, sizeof(struct uvisor_softc), uvisor_match,
196     uvisor_attach, uvisor_detach, NULL, NULL, uvisor_childdet);
197 
198 static int
uvisor_match(device_t parent,cfdata_t match,void * aux)199 uvisor_match(device_t parent, cfdata_t match, void *aux)
200 {
201 	struct usb_attach_arg *uaa = aux;
202 
203 	DPRINTFN(20,("uvisor: vendor=%#x, product=%#x\n",
204 		     uaa->uaa_vendor, uaa->uaa_product));
205 
206 	return uvisor_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
207 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
208 }
209 
210 static void
uvisor_attach(device_t parent,device_t self,void * aux)211 uvisor_attach(device_t parent, device_t self, void *aux)
212 {
213 	struct uvisor_softc *sc = device_private(self);
214 	struct usb_attach_arg *uaa = aux;
215 	struct usbd_device *dev = uaa->uaa_device;
216 	struct usbd_interface *iface;
217 	usb_interface_descriptor_t *id;
218 	struct uvisor_connection_info coninfo;
219 	struct uvisor_palm_connection_info palmconinfo;
220 	usb_endpoint_descriptor_t *ed;
221 	char *devinfop;
222 	const char *devname = device_xname(self);
223 	int i, j, hasin, hasout, port;
224 	usbd_status err;
225 	struct ucom_attach_args ucaa;
226 
227 	DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
228 
229 	sc->sc_dev = self;
230 	sc->sc_dying = false;
231 
232 	aprint_naive("\n");
233 	aprint_normal("\n");
234 
235 	devinfop = usbd_devinfo_alloc(dev, 0);
236 	aprint_normal_dev(self, "%s\n", devinfop);
237 	usbd_devinfo_free(devinfop);
238 
239 	/* Move the device into the configured state. */
240 	err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
241 	if (err) {
242 		aprint_error("\n%s: failed to set configuration, err=%s\n",
243 		       devname, usbd_errstr(err));
244 		goto bad;
245 	}
246 
247 	err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
248 	if (err) {
249 		aprint_error("\n%s: failed to get interface, err=%s\n",
250 		       devname, usbd_errstr(err));
251 		goto bad;
252 	}
253 
254 	sc->sc_flags = uvisor_lookup(uaa->uaa_vendor, uaa->uaa_product)->uv_flags;
255 
256 	if ((sc->sc_flags & (VISOR | PALM4)) == 0) {
257 		aprint_error_dev(self,
258 		    "init failed, device type is neither visor nor palm\n");
259 		goto bad;
260 	}
261 
262 	id = usbd_get_interface_descriptor(iface);
263 
264 	sc->sc_udev = dev;
265 	sc->sc_iface = iface;
266 
267 	ucaa.ucaa_ibufsize = UVISORIBUFSIZE;
268 	ucaa.ucaa_obufsize = UVISOROBUFSIZE;
269 	ucaa.ucaa_ibufsizepad = UVISORIBUFSIZE;
270 	ucaa.ucaa_opkthdrlen = 0;
271 	ucaa.ucaa_device = dev;
272 	ucaa.ucaa_iface = iface;
273 	ucaa.ucaa_methods = &uvisor_methods;
274 	ucaa.ucaa_arg = sc;
275 
276 	err = uvisor_init(sc, &coninfo, &palmconinfo);
277 	if (err) {
278 		aprint_error_dev(self, "init failed, %s\n", usbd_errstr(err));
279 		goto bad;
280 	}
281 
282 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
283 
284 	if (sc->sc_flags & VISOR) {
285 		sc->sc_numcon = UGETW(coninfo.num_ports);
286 		if (sc->sc_numcon > UVISOR_MAX_CONN)
287 			sc->sc_numcon = UVISOR_MAX_CONN;
288 
289 		/* Attach a ucom for each connection. */
290 		for (i = 0; i < sc->sc_numcon; ++i) {
291 			switch (coninfo.connections[i].port_function_id) {
292 			case UVISOR_FUNCTION_GENERIC:
293 				ucaa.ucaa_info = "Generic";
294 				break;
295 			case UVISOR_FUNCTION_DEBUGGER:
296 				ucaa.ucaa_info = "Debugger";
297 				break;
298 			case UVISOR_FUNCTION_HOTSYNC:
299 				ucaa.ucaa_info = "HotSync";
300 				break;
301 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
302 				ucaa.ucaa_info = "Remote File System";
303 				break;
304 			default:
305 				ucaa.ucaa_info = "unknown";
306 				break;
307 			}
308 			port = coninfo.connections[i].port;
309 			ucaa.ucaa_portno = port;
310 			ucaa.ucaa_bulkin = port | UE_DIR_IN;
311 			ucaa.ucaa_bulkout = port | UE_DIR_OUT;
312 			/* Verify that endpoints exist. */
313 			hasin = 0;
314 			hasout = 0;
315 			for (j = 0; j < id->bNumEndpoints; j++) {
316 				ed = usbd_interface2endpoint_descriptor(iface, j);
317 				if (ed == NULL)
318 					break;
319 				if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
320 				    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
321 					if (UE_GET_DIR(ed->bEndpointAddress)
322 					    == UE_DIR_IN)
323 						hasin++;
324 					else
325 						hasout++;
326 				}
327 			}
328 			if (hasin == 1 && hasout == 1)
329 				sc->sc_subdevs[i] =
330 				    config_found(self, &ucaa, ucomprint,
331 						 CFARGS(.submatch =
332 						            ucomsubmatch));
333 			else
334 				aprint_error_dev(self,
335 				    "no proper endpoints for port %d (%d,%d)\n",
336 				    port, hasin, hasout);
337 		}
338 
339 	} else {
340 		sc->sc_numcon = palmconinfo.num_ports;
341 		if (sc->sc_numcon > UVISOR_MAX_CONN)
342 			sc->sc_numcon = UVISOR_MAX_CONN;
343 
344 		/* Attach a ucom for each connection. */
345 		for (i = 0; i < sc->sc_numcon; ++i) {
346 			/*
347 			 * XXX this should copy out 4-char string from the
348 			 * XXX port_function_id, but where would the string go?
349 			 * XXX ucaa.ucaa_info is a const char *, not an array.
350 			 */
351 			ucaa.ucaa_info = "sync";
352 			ucaa.ucaa_portno = i;
353 			if (palmconinfo.endpoint_numbers_different) {
354 				port = palmconinfo.connections[i].end_point_info;
355 				ucaa.ucaa_bulkin = (port >> 4) | UE_DIR_IN;
356 				ucaa.ucaa_bulkout = (port & 0xf) | UE_DIR_OUT;
357 			} else {
358 				port = palmconinfo.connections[i].port;
359 				ucaa.ucaa_bulkin = port | UE_DIR_IN;
360 				ucaa.ucaa_bulkout = port | UE_DIR_OUT;
361 			}
362 			sc->sc_subdevs[i] =
363 			    config_found(self, &ucaa, ucomprint,
364 					 CFARGS(.submatch = ucomsubmatch));
365 		}
366 	}
367 
368 	return;
369 
370 bad:
371 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
372 	sc->sc_dying = true;
373 	return;
374 }
375 
376 static void
uvisor_childdet(device_t self,device_t child)377 uvisor_childdet(device_t self, device_t child)
378 {
379 	int i;
380 	struct uvisor_softc *sc = device_private(self);
381 
382 	for (i = 0; i < sc->sc_numcon; i++) {
383 		if (sc->sc_subdevs[i] == child)
384 			break;
385 	}
386 	KASSERT(i < sc->sc_numcon);
387 	sc->sc_subdevs[i] = NULL;
388 }
389 
390 static int
uvisor_detach(device_t self,int flags)391 uvisor_detach(device_t self, int flags)
392 {
393 	struct uvisor_softc *sc = device_private(self);
394 	int rv = 0;
395 	int i;
396 
397 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
398 
399 	sc->sc_dying = true;
400 
401 	for (i = 0; i < sc->sc_numcon; i++) {
402 		if (sc->sc_subdevs[i] != NULL) {
403 			rv |= config_detach(sc->sc_subdevs[i], flags);
404 			sc->sc_subdevs[i] = NULL;
405 		}
406 	}
407 	if (sc->sc_udev != NULL)
408 		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
409 		    sc->sc_dev);
410 
411 	return rv;
412 }
413 
414 static usbd_status
uvisor_init(struct uvisor_softc * sc,struct uvisor_connection_info * ci,struct uvisor_palm_connection_info * cpi)415 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
416     struct uvisor_palm_connection_info *cpi)
417 {
418 	usbd_status err;
419 	usb_device_request_t req;
420 	int actlen;
421 	uWord avail;
422 
423 	if (sc->sc_flags & VISOR) {
424 		DPRINTF(("uvisor_init: getting Visor connection info\n"));
425 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
426 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
427 		USETW(req.wValue, 0);
428 		USETW(req.wIndex, 0);
429 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
430 		err = usbd_do_request_flags(sc->sc_udev, &req, ci,
431 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
432 		if (err)
433 			return err;
434 	}
435 
436 	if (sc->sc_flags & PALM4) {
437 		DPRINTF(("uvisor_init: getting Palm connection info\n"));
438 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
439 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
440 		USETW(req.wValue, 0);
441 		USETW(req.wIndex, 0);
442 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
443 		err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
444 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
445 		if (err)
446 			return err;
447 	}
448 
449 	DPRINTF(("uvisor_init: getting available bytes\n"));
450 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
451 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
452 	USETW(req.wValue, 0);
453 	USETW(req.wIndex, 5);
454 	USETW(req.wLength, sizeof(avail));
455 	err = usbd_do_request(sc->sc_udev, &req, &avail);
456 	if (err)
457 		return err;
458 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
459 
460 	DPRINTF(("uvisor_init: done\n"));
461 	return err;
462 }
463 
464 static int
uvisor_open(void * arg,int portno)465 uvisor_open(void *arg, int portno)
466 {
467 	struct uvisor_softc *sc = arg;
468 
469 	if (sc->sc_dying)
470 		return EIO;
471 
472 	return 0;
473 }
474 
475 void
uvisor_close(void * addr,int portno)476 uvisor_close(void *addr, int portno)
477 {
478 	struct uvisor_softc *sc = addr;
479 	usb_device_request_t req;
480 	struct uvisor_connection_info coninfo; /* XXX ? */
481 	int actlen;
482 
483 	if (sc->sc_dying)
484 		return;
485 
486 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
487 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
488 	USETW(req.wValue, 0);
489 	USETW(req.wIndex, 0);
490 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
491 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
492 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
493 }
494