xref: /openbsd-src/sys/dev/usb/uvisor.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: uvisor.c,v 1.15 2003/05/17 17:13:14 nate Exp $	*/
2 /*	$NetBSD: uvisor.c,v 1.20 2003/04/11 01:30:10 simonb 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  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * Handspring Visor (Palmpilot compatible PDA) driver
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 #include <sys/conf.h>
50 #include <sys/tty.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbhid.h>
54 
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdevs.h>
58 
59 #include <dev/usb/ucomvar.h>
60 
61 #ifdef UVISOR_DEBUG
62 #define DPRINTF(x)	if (uvisordebug) printf x
63 #define DPRINTFN(n,x)	if (uvisordebug>(n)) printf x
64 int uvisordebug = 0;
65 #else
66 #define DPRINTF(x)
67 #define DPRINTFN(n,x)
68 #endif
69 
70 #define UVISOR_CONFIG_INDEX	0
71 #define UVISOR_IFACE_INDEX	0
72 
73 /* From the Linux driver */
74 /*
75  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
76  * are available to be transfered to the host for the specified endpoint.
77  * Currently this is not used, and always returns 0x0001
78  */
79 #define UVISOR_REQUEST_BYTES_AVAILABLE		0x01
80 
81 /*
82  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
83  * is now closing the pipe. An empty packet is sent in response.
84  */
85 #define UVISOR_CLOSE_NOTIFICATION		0x02
86 
87 /*
88  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
89  * get the endpoints used by the connection.
90  */
91 #define UVISOR_GET_CONNECTION_INFORMATION	0x03
92 
93 
94 /*
95  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
96  */
97 #define UVISOR_MAX_CONN 8
98 struct uvisor_connection_info {
99 	uWord	num_ports;
100 	struct {
101 		uByte	port_function_id;
102 		uByte	port;
103 	} connections[UVISOR_MAX_CONN];
104 };
105 #define UVISOR_CONNECTION_INFO_SIZE 18
106 
107 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
108 #define UVISOR_FUNCTION_GENERIC		0x00
109 #define UVISOR_FUNCTION_DEBUGGER	0x01
110 #define UVISOR_FUNCTION_HOTSYNC		0x02
111 #define UVISOR_FUNCTION_CONSOLE		0x03
112 #define UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
113 
114 /*
115  * Unknown PalmOS stuff.
116  */
117 #define UVISOR_GET_PALM_INFORMATION		0x04
118 #define UVISOR_GET_PALM_INFORMATION_LEN		0x14
119 
120 
121 #define UVISORIBUFSIZE 1024
122 #define UVISOROBUFSIZE 1024
123 
124 struct uvisor_softc {
125 	USBBASEDEVICE		sc_dev;		/* base device */
126 	usbd_device_handle	sc_udev;	/* device */
127 	usbd_interface_handle	sc_iface;	/* interface */
128 
129 	device_ptr_t		sc_subdevs[UVISOR_MAX_CONN];
130 	int			sc_numcon;
131 
132 	u_int16_t		sc_flags;
133 
134 	u_char			sc_dying;
135 };
136 
137 Static usbd_status uvisor_init(struct uvisor_softc *,
138 			       struct uvisor_connection_info *);
139 
140 Static void uvisor_close(void *, int);
141 
142 
143 struct ucom_methods uvisor_methods = {
144 	NULL,
145 	NULL,
146 	NULL,
147 	NULL,
148 	NULL,
149 	uvisor_close,
150 	NULL,
151 	NULL,
152 };
153 
154 struct uvisor_type {
155 	struct usb_devno	uv_dev;
156 	u_int16_t		uv_flags;
157 #define PALM4	0x0001
158 };
159 static const struct uvisor_type uvisor_devs[] = {
160 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, 0 },
161 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 },
162 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
163 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
164 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
165 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 },
166 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
167 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 },
168 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 },
169 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 },
170 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
171 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
172 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, 0 },
173 /*	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
174 };
175 #define uvisor_lookup(v, p) ((struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
176 
177 USB_DECLARE_DRIVER(uvisor);
178 
179 USB_MATCH(uvisor)
180 {
181 	USB_MATCH_START(uvisor, uaa);
182 
183 	if (uaa->iface != NULL)
184 		return (UMATCH_NONE);
185 
186 	DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
187 		     uaa->vendor, uaa->product));
188 
189 	return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
190 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
191 }
192 
193 USB_ATTACH(uvisor)
194 {
195 	USB_ATTACH_START(uvisor, sc, uaa);
196 	usbd_device_handle dev = uaa->device;
197 	usbd_interface_handle iface;
198 	usb_interface_descriptor_t *id;
199 	struct uvisor_connection_info coninfo;
200 	usb_endpoint_descriptor_t *ed;
201 	char devinfo[1024];
202 	char *devname = USBDEVNAME(sc->sc_dev);
203 	int i, j, hasin, hasout, port;
204 	usbd_status err;
205 	struct ucom_attach_args uca;
206 
207 	DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
208 
209 	/* Move the device into the configured state. */
210 	err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
211 	if (err) {
212 		printf("\n%s: failed to set configuration, err=%s\n",
213 		       devname, usbd_errstr(err));
214 		goto bad;
215 	}
216 
217 	err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
218 	if (err) {
219 		printf("\n%s: failed to get interface, err=%s\n",
220 		       devname, usbd_errstr(err));
221 		goto bad;
222 	}
223 
224 	usbd_devinfo(dev, 0, devinfo, sizeof devinfo);
225 	USB_ATTACH_SETUP;
226 	printf("%s: %s\n", devname, devinfo);
227 
228 	sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
229 
230 	id = usbd_get_interface_descriptor(iface);
231 
232 	sc->sc_udev = dev;
233 	sc->sc_iface = iface;
234 
235 	uca.ibufsize = UVISORIBUFSIZE;
236 	uca.obufsize = UVISOROBUFSIZE;
237 	uca.ibufsizepad = UVISORIBUFSIZE;
238 	uca.opkthdrlen = 0;
239 	uca.device = dev;
240 	uca.iface = iface;
241 	uca.methods = &uvisor_methods;
242 	uca.arg = sc;
243 
244 	err = uvisor_init(sc, &coninfo);
245 	if (err) {
246 		printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
247 		       usbd_errstr(err));
248 		goto bad;
249 	}
250 
251 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
252 			   USBDEV(sc->sc_dev));
253 
254 	sc->sc_numcon = UGETW(coninfo.num_ports);
255 	if (sc->sc_numcon > UVISOR_MAX_CONN)
256 		sc->sc_numcon = UVISOR_MAX_CONN;
257 
258 	/* Attach a ucom for each connection. */
259 	for (i = 0; i < sc->sc_numcon; ++i) {
260 		switch (coninfo.connections[i].port_function_id) {
261 		case UVISOR_FUNCTION_GENERIC:
262 			uca.info = "Generic";
263 			break;
264 		case UVISOR_FUNCTION_DEBUGGER:
265 			uca.info = "Debugger";
266 			break;
267 		case UVISOR_FUNCTION_HOTSYNC:
268 			uca.info = "HotSync";
269 			break;
270 		case UVISOR_FUNCTION_REMOTE_FILE_SYS:
271 			uca.info = "Remote File System";
272 			break;
273 		default:
274 			uca.info = "unknown";
275 			break;
276 		}
277 		port = coninfo.connections[i].port;
278 		uca.portno = port;
279 		uca.bulkin = port | UE_DIR_IN;
280 		uca.bulkout = port | UE_DIR_OUT;
281 		/* Verify that endpoints exist. */
282 		for (hasin = hasout = j = 0; j < id->bNumEndpoints; j++) {
283 			ed = usbd_interface2endpoint_descriptor(iface, j);
284 			if (ed == NULL)
285 				break;
286 			if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
287 			    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
288 				if (UE_GET_DIR(ed->bEndpointAddress)
289 				    == UE_DIR_IN)
290 					hasin++;
291 				else
292 					hasout++;
293 			}
294 		}
295 		if (hasin == 1 && hasout == 1)
296 			sc->sc_subdevs[i] = config_found_sm(self, &uca,
297 			    ucomprint, ucomsubmatch);
298 		else
299 			printf("%s: no proper endpoints for port %d (%d,%d)\n",
300 			    USBDEVNAME(sc->sc_dev), port, hasin, hasout);
301 	}
302 
303 	USB_ATTACH_SUCCESS_RETURN;
304 
305 bad:
306 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
307 	sc->sc_dying = 1;
308 	USB_ATTACH_ERROR_RETURN;
309 }
310 
311 int
312 uvisor_activate(device_ptr_t self, enum devact act)
313 {
314 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
315 	int rv = 0;
316 	int i;
317 
318 	switch (act) {
319 	case DVACT_ACTIVATE:
320 		return (EOPNOTSUPP);
321 		break;
322 
323 	case DVACT_DEACTIVATE:
324 		for (i = 0; i < sc->sc_numcon; i++)
325 			if (sc->sc_subdevs[i] != NULL)
326 				rv = config_deactivate(sc->sc_subdevs[i]);
327 		sc->sc_dying = 1;
328 		break;
329 	}
330 	return (rv);
331 }
332 
333 int
334 uvisor_detach(device_ptr_t self, int flags)
335 {
336 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
337 	int rv = 0;
338 	int i;
339 
340 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
341 	sc->sc_dying = 1;
342 	for (i = 0; i < sc->sc_numcon; i++) {
343 		if (sc->sc_subdevs[i] != NULL) {
344 			rv |= config_detach(sc->sc_subdevs[i], flags);
345 			sc->sc_subdevs[i] = NULL;
346 		}
347 	}
348 
349 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
350 			   USBDEV(sc->sc_dev));
351 
352 	return (rv);
353 }
354 
355 usbd_status
356 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci)
357 {
358 	usbd_status err;
359 	usb_device_request_t req;
360 	int actlen;
361 	uWord avail;
362 	char buffer[256];
363 
364 	DPRINTF(("uvisor_init: getting connection info\n"));
365 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
366 	req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
367 	USETW(req.wValue, 0);
368 	USETW(req.wIndex, 0);
369 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
370 	err = usbd_do_request_flags(sc->sc_udev, &req, ci,
371 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
372 	if (err)
373 		return (err);
374 
375 	if (sc->sc_flags & PALM4) {
376 		/* Palm OS 4.0 Hack */
377 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
378 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
379 		USETW(req.wValue, 0);
380 		USETW(req.wIndex, 0);
381 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
382 		err = usbd_do_request(sc->sc_udev, &req, buffer);
383 		if (err)
384 			return (err);
385 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
386 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
387 		USETW(req.wValue, 0);
388 		USETW(req.wIndex, 0);
389 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
390 		err = usbd_do_request(sc->sc_udev, &req, buffer);
391 		if (err)
392 			return (err);
393 	}
394 
395 	DPRINTF(("uvisor_init: getting available bytes\n"));
396 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
397 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
398 	USETW(req.wValue, 0);
399 	USETW(req.wIndex, 5);
400 	USETW(req.wLength, sizeof avail);
401 	err = usbd_do_request(sc->sc_udev, &req, &avail);
402 	if (err)
403 		return (err);
404 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
405 
406 	DPRINTF(("uvisor_init: done\n"));
407 	return (err);
408 }
409 
410 void
411 uvisor_close(void *addr, int portno)
412 {
413 	struct uvisor_softc *sc = addr;
414 	usb_device_request_t req;
415 	struct uvisor_connection_info coninfo; /* XXX ? */
416 	int actlen;
417 
418 	if (sc->sc_dying)
419 		return;
420 
421 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
422 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
423 	USETW(req.wValue, 0);
424 	USETW(req.wIndex, 0);
425 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
426 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
427 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
428 }
429