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