xref: /netbsd-src/sys/dev/usb/uvisor.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: uvisor.c,v 1.42 2009/11/12 20:01:15 dyoung 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.42 2009/11/12 20:01:15 dyoung Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/conf.h>
45 #include <sys/tty.h>
46 
47 #include <dev/usb/usb.h>
48 
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
51 #include <dev/usb/usbdevs.h>
52 
53 #include <dev/usb/ucomvar.h>
54 
55 #ifdef UVISOR_DEBUG
56 #define DPRINTF(x)	if (uvisordebug) printf x
57 #define DPRINTFN(n,x)	if (uvisordebug>(n)) printf x
58 int uvisordebug = 0;
59 #else
60 #define DPRINTF(x)
61 #define DPRINTFN(n,x)
62 #endif
63 
64 #define UVISOR_CONFIG_INDEX	0
65 #define UVISOR_IFACE_INDEX	0
66 
67 /* From the Linux driver */
68 /*
69  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
70  * are available to be transfered to the host for the specified endpoint.
71  * Currently this is not used, and always returns 0x0001
72  */
73 #define UVISOR_REQUEST_BYTES_AVAILABLE		0x01
74 
75 /*
76  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
77  * is now closing the pipe. An empty packet is sent in response.
78  */
79 #define UVISOR_CLOSE_NOTIFICATION		0x02
80 
81 /*
82  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
83  * get the endpoints used by the connection.
84  */
85 #define UVISOR_GET_CONNECTION_INFORMATION	0x03
86 
87 
88 /*
89  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
90  */
91 #define UVISOR_MAX_CONN 8
92 struct uvisor_connection_info {
93 	uWord	num_ports;
94 	struct {
95 		uByte	port_function_id;
96 		uByte	port;
97 	} connections[UVISOR_MAX_CONN];
98 };
99 #define UVISOR_CONNECTION_INFO_SIZE 18
100 
101 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
102 #define UVISOR_FUNCTION_GENERIC		0x00
103 #define UVISOR_FUNCTION_DEBUGGER	0x01
104 #define UVISOR_FUNCTION_HOTSYNC		0x02
105 #define UVISOR_FUNCTION_CONSOLE		0x03
106 #define UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
107 
108 /*
109  * Unknown PalmOS stuff.
110  */
111 #define UVISOR_GET_PALM_INFORMATION		0x04
112 #define UVISOR_GET_PALM_INFORMATION_LEN		0x44
113 
114 struct uvisor_palm_connection_info {
115 	uByte	num_ports;
116 	uByte	endpoint_numbers_different;
117 	uWord	reserved1;
118 	struct {
119 		uDWord	port_function_id;
120 		uByte	port;
121 		uByte	end_point_info;
122 		uWord	reserved;
123 	} connections[UVISOR_MAX_CONN];
124 };
125 
126 
127 
128 #define UVISORIBUFSIZE 64
129 #define UVISOROBUFSIZE 1024
130 
131 struct uvisor_softc {
132 	USBBASEDEVICE		sc_dev;		/* base device */
133 	usbd_device_handle	sc_udev;	/* device */
134 	usbd_interface_handle	sc_iface;	/* interface */
135 
136 	device_ptr_t		sc_subdevs[UVISOR_MAX_CONN];
137 	int			sc_numcon;
138 
139 	u_int16_t		sc_flags;
140 
141 	u_char			sc_dying;
142 };
143 
144 Static usbd_status uvisor_init(struct uvisor_softc *,
145 			       struct uvisor_connection_info *,
146 			       struct uvisor_palm_connection_info *);
147 
148 Static void uvisor_close(void *, int);
149 
150 
151 struct ucom_methods uvisor_methods = {
152 	NULL,
153 	NULL,
154 	NULL,
155 	NULL,
156 	NULL,
157 	uvisor_close,
158 	NULL,
159 	NULL,
160 };
161 
162 struct uvisor_type {
163 	struct usb_devno	uv_dev;
164 	u_int16_t		uv_flags;
165 #define PALM4	0x0001
166 #define VISOR	0x0002
167 
168 };
169 static const struct uvisor_type uvisor_devs[] = {
170 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR },
171 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 },
172 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600 }, PALM4 },
173 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
174 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
175 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
176 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 },
177 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
178 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 },
179 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 },
180 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 },
181 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE31 }, PALM4 },
182 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
183 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
184 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 },
185 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 },
186 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 },
187 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_35 }, 0 },
188 /*	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
189 };
190 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
191 
192 int uvisor_match(device_t, cfdata_t, void *);
193 void uvisor_attach(device_t, device_t, void *);
194 void uvisor_childdet(device_t, device_t);
195 int uvisor_detach(device_t, int);
196 int uvisor_activate(device_t, enum devact);
197 extern struct cfdriver uvisor_cd;
198 CFATTACH_DECL2_NEW(uvisor, sizeof(struct uvisor_softc), uvisor_match,
199     uvisor_attach, uvisor_detach, uvisor_activate, NULL, uvisor_childdet);
200 
201 USB_MATCH(uvisor)
202 {
203 	USB_MATCH_START(uvisor, uaa);
204 
205 	DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
206 		     uaa->vendor, uaa->product));
207 
208 	return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
209 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
210 }
211 
212 USB_ATTACH(uvisor)
213 {
214 	USB_ATTACH_START(uvisor, sc, uaa);
215 	usbd_device_handle dev = uaa->device;
216 	usbd_interface_handle 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 uca;
226 
227 	DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
228 
229 	sc->sc_dev = self;
230 
231 	aprint_naive("\n");
232 	aprint_normal("\n");
233 
234 	devinfop = usbd_devinfo_alloc(dev, 0);
235 	aprint_normal_dev(self, "%s\n", devinfop);
236 	usbd_devinfo_free(devinfop);
237 
238 	/* Move the device into the configured state. */
239 	err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
240 	if (err) {
241 		aprint_error("\n%s: failed to set configuration, err=%s\n",
242 		       devname, usbd_errstr(err));
243 		goto bad;
244 	}
245 
246 	err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
247 	if (err) {
248 		aprint_error("\n%s: failed to get interface, err=%s\n",
249 		       devname, usbd_errstr(err));
250 		goto bad;
251 	}
252 
253 	sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
254 
255 	if ((sc->sc_flags & (VISOR | PALM4)) == 0) {
256 		aprint_error_dev(self,
257 		    "init failed, device type is neither visor nor palm\n");
258 		goto bad;
259 	}
260 
261 	id = usbd_get_interface_descriptor(iface);
262 
263 	sc->sc_udev = dev;
264 	sc->sc_iface = iface;
265 
266 	uca.ibufsize = UVISORIBUFSIZE;
267 	uca.obufsize = UVISOROBUFSIZE;
268 	uca.ibufsizepad = UVISORIBUFSIZE;
269 	uca.opkthdrlen = 0;
270 	uca.device = dev;
271 	uca.iface = iface;
272 	uca.methods = &uvisor_methods;
273 	uca.arg = sc;
274 
275 	err = uvisor_init(sc, &coninfo, &palmconinfo);
276 	if (err) {
277 		aprint_error_dev(self, "init failed, %s\n", usbd_errstr(err));
278 		goto bad;
279 	}
280 
281 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
282 			   USBDEV(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 				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_loc(self,
330 					"ucombus", NULL, &uca,
331 					ucomprint, ucomsubmatch);
332 			else
333 				aprint_error_dev(self,
334 				    "no proper endpoints for port %d (%d,%d)\n",
335 				    port, hasin, hasout);
336 		}
337 
338 	} else {
339 		sc->sc_numcon = palmconinfo.num_ports;
340 		if (sc->sc_numcon > UVISOR_MAX_CONN)
341 			sc->sc_numcon = UVISOR_MAX_CONN;
342 
343 		/* Attach a ucom for each connection. */
344 		for (i = 0; i < sc->sc_numcon; ++i) {
345 			/*
346 			 * XXX this should copy out 4-char string from the
347 			 * XXX port_function_id, but where would the string go?
348 			 * XXX uca.info is a const char *, not an array.
349 			 */
350 			uca.info = "sync";
351 			uca.portno = i;
352 			if (palmconinfo.endpoint_numbers_different) {
353 				port = palmconinfo.connections[i].end_point_info;
354 				uca.bulkin = (port >> 4) | UE_DIR_IN;
355 				uca.bulkout = (port & 0xf) | UE_DIR_OUT;
356 			} else {
357 				port = palmconinfo.connections[i].port;
358 				uca.bulkin = port | UE_DIR_IN;
359 				uca.bulkout = port | UE_DIR_OUT;
360 			}
361 			sc->sc_subdevs[i] = config_found_sm_loc(self, "ucombus",
362 				NULL, &uca, ucomprint, ucomsubmatch);
363 
364 
365 		}
366 	}
367 
368 	USB_ATTACH_SUCCESS_RETURN;
369 
370 bad:
371 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
372 	sc->sc_dying = 1;
373 	USB_ATTACH_ERROR_RETURN;
374 }
375 
376 int
377 uvisor_activate(device_ptr_t self, enum devact act)
378 {
379 	struct uvisor_softc *sc = device_private(self);
380 
381 	switch (act) {
382 	case DVACT_DEACTIVATE:
383 		sc->sc_dying = 1;
384 		return 0;
385 	default:
386 		return EOPNOTSUPP;
387 	}
388 }
389 
390 void
391 uvisor_childdet(device_t self, device_t child)
392 {
393 	int i;
394 	struct uvisor_softc *sc = device_private(self);
395 
396 	for (i = 0; i < sc->sc_numcon; i++) {
397 		if (sc->sc_subdevs[i] == child)
398 			break;
399 	}
400 	KASSERT(i < sc->sc_numcon);
401 	sc->sc_subdevs[i] = NULL;
402 }
403 
404 int
405 uvisor_detach(device_t self, int flags)
406 {
407 	struct uvisor_softc *sc = device_private(self);
408 	int rv = 0;
409 	int i;
410 
411 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
412 	sc->sc_dying = 1;
413 	for (i = 0; i < sc->sc_numcon; i++) {
414 		if (sc->sc_subdevs[i] != NULL)
415 			rv |= config_detach(sc->sc_subdevs[i], flags);
416 	}
417 
418 	if (sc->sc_udev)
419 		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
420 				   USBDEV(sc->sc_dev));
421 
422 
423 	return (rv);
424 }
425 
426 usbd_status
427 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
428     struct uvisor_palm_connection_info *cpi)
429 {
430 	usbd_status err;
431 	usb_device_request_t req;
432 	int actlen;
433 	uWord avail;
434 
435 	if (sc->sc_flags & VISOR) {
436 		DPRINTF(("uvisor_init: getting Visor connection info\n"));
437 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
438 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
439 		USETW(req.wValue, 0);
440 		USETW(req.wIndex, 0);
441 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
442 		err = usbd_do_request_flags(sc->sc_udev, &req, ci,
443 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
444 		if (err)
445 			return (err);
446 	}
447 
448 	if (sc->sc_flags & PALM4) {
449 		DPRINTF(("uvisor_init: getting Palm connection info\n"));
450 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
451 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
452 		USETW(req.wValue, 0);
453 		USETW(req.wIndex, 0);
454 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
455 		err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
456 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
457 		if (err)
458 			return (err);
459 	}
460 
461 	DPRINTF(("uvisor_init: getting available bytes\n"));
462 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
463 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
464 	USETW(req.wValue, 0);
465 	USETW(req.wIndex, 5);
466 	USETW(req.wLength, sizeof avail);
467 	err = usbd_do_request(sc->sc_udev, &req, &avail);
468 	if (err)
469 		return (err);
470 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
471 
472 	DPRINTF(("uvisor_init: done\n"));
473 	return (err);
474 }
475 
476 void
477 uvisor_close(void *addr, int portno)
478 {
479 	struct uvisor_softc *sc = addr;
480 	usb_device_request_t req;
481 	struct uvisor_connection_info coninfo; /* XXX ? */
482 	int actlen;
483 
484 	if (sc->sc_dying)
485 		return;
486 
487 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
488 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
489 	USETW(req.wValue, 0);
490 	USETW(req.wIndex, 0);
491 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
492 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
493 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
494 }
495