1 /* $OpenBSD: uvisor.c,v 1.54 2024/05/23 03:21:09 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/device.h>
41 #include <sys/tty.h>
42
43 #include <dev/usb/usb.h>
44
45 #include <dev/usb/usbdi.h>
46 #include <dev/usb/usbdevs.h>
47
48 #include <dev/usb/ucomvar.h>
49
50 #ifdef UVISOR_DEBUG
51 #define DPRINTF(x) if (uvisordebug) printf x
52 #define DPRINTFN(n,x) if (uvisordebug>(n)) printf x
53 int uvisordebug = 0;
54 #else
55 #define DPRINTF(x)
56 #define DPRINTFN(n,x)
57 #endif
58
59 #define UVISOR_IFACE_INDEX 0
60
61 /* From the Linux driver */
62 /*
63 * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
64 * are available to be transferred to the host for the specified endpoint.
65 * Currently this is not used, and always returns 0x0001
66 */
67 #define UVISOR_REQUEST_BYTES_AVAILABLE 0x01
68
69 /*
70 * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
71 * is now closing the pipe. An empty packet is sent in response.
72 */
73 #define UVISOR_CLOSE_NOTIFICATION 0x02
74
75 /*
76 * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
77 * get the endpoints used by the connection.
78 */
79 #define UVISOR_GET_CONNECTION_INFORMATION 0x03
80
81
82 /*
83 * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
84 */
85 #define UVISOR_MAX_CONN 8
86 struct uvisor_connection_info {
87 uWord num_ports;
88 struct {
89 uByte port_function_id;
90 uByte port;
91 } connections[UVISOR_MAX_CONN];
92 };
93 #define UVISOR_CONNECTION_INFO_SIZE 18
94
95 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
96 #define UVISOR_FUNCTION_GENERIC 0x00
97 #define UVISOR_FUNCTION_DEBUGGER 0x01
98 #define UVISOR_FUNCTION_HOTSYNC 0x02
99 #define UVISOR_FUNCTION_CONSOLE 0x03
100 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
101
102 /*
103 * Unknown PalmOS stuff.
104 */
105 #define UVISOR_GET_PALM_INFORMATION 0x04
106 #define UVISOR_GET_PALM_INFORMATION_LEN 0x44
107
108 struct uvisor_palm_connection_info {
109 uByte num_ports;
110 uByte endpoint_numbers_different;
111 uWord reserved1;
112 struct {
113 uDWord port_function_id;
114 uByte port;
115 uByte end_point_info;
116 uWord reserved;
117 } connections[UVISOR_MAX_CONN];
118 };
119
120 #define UVISORIBUFSIZE 64
121 #define UVISOROBUFSIZE 1024
122
123 struct uvisor_softc {
124 struct device sc_dev; /* base device */
125 struct usbd_device *sc_udev; /* device */
126 struct usbd_interface *sc_iface; /* interface */
127 /*
128 * added sc_vendor for later interrogation in failed initialisations
129 */
130 int sc_vendor; /* USB device vendor */
131
132 struct device *sc_subdevs[UVISOR_MAX_CONN];
133 int sc_numcon;
134
135 u_int16_t sc_flags;
136 };
137
138 usbd_status uvisor_init(struct uvisor_softc *,
139 struct uvisor_connection_info *,
140 struct uvisor_palm_connection_info *);
141
142 void uvisor_close(void *, int);
143
144
145 const struct ucom_methods uvisor_methods = {
146 NULL,
147 NULL,
148 NULL,
149 NULL,
150 NULL,
151 uvisor_close,
152 NULL,
153 NULL,
154 };
155
156 struct uvisor_type {
157 struct usb_devno uv_dev;
158 u_int16_t uv_flags;
159 #define PALM4 0x0001
160 #define VISOR 0x0002
161 #define NOFRE 0x0004
162 #define CLIE4 (VISOR|NOFRE)
163 };
164 static const struct uvisor_type uvisor_devs[] = {
165 {{ USB_VENDOR_ACEECA, USB_PRODUCT_ACEECA_MEZ1000 }, PALM4 },
166 {{ USB_VENDOR_FOSSIL, USB_PRODUCT_FOSSIL_WRISTPDA }, PALM4 },
167 {{ USB_VENDOR_GARMIN, USB_PRODUCT_GARMIN_IQUE3600 }, PALM4 },
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 }, VISOR },
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_ZIRE }, PALM4 },
180 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE_31 }, 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_TJ25 }, PALM4 },
186 /* {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
187 {{ USB_VENDOR_TAPWAVE, USB_PRODUCT_TAPWAVE_ZODIAC }, PALM4 },
188 };
189 #define uvisor_lookup(v, p) ((struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
190
191 int uvisor_match(struct device *, void *, void *);
192 void uvisor_attach(struct device *, struct device *, void *);
193 int uvisor_detach(struct device *, int);
194
195 struct cfdriver uvisor_cd = {
196 NULL, "uvisor", DV_DULL
197 };
198
199 const struct cfattach uvisor_ca = {
200 sizeof(struct uvisor_softc), uvisor_match, uvisor_attach, uvisor_detach
201 };
202
203 int
uvisor_match(struct device * parent,void * match,void * aux)204 uvisor_match(struct device *parent, void *match, void *aux)
205 {
206 struct usb_attach_arg *uaa = aux;
207
208 if (uaa->iface == NULL)
209 return (UMATCH_NONE);
210
211 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
212 uaa->vendor, uaa->product));
213
214 return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
215 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
216 }
217
218 void
uvisor_attach(struct device * parent,struct device * self,void * aux)219 uvisor_attach(struct device *parent, struct device *self, void *aux)
220 {
221 struct uvisor_softc *sc = (struct uvisor_softc *)self;
222 struct usb_attach_arg *uaa = aux;
223 struct usbd_device *dev = uaa->device;
224 struct usbd_interface *iface;
225 usb_interface_descriptor_t *id;
226 struct uvisor_connection_info coninfo;
227 struct uvisor_palm_connection_info palmconinfo;
228 usb_endpoint_descriptor_t *ed;
229 int i, j, hasin, hasout, port;
230 usbd_status err;
231 struct ucom_attach_args uca;
232
233 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
234
235 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
236 if (err) {
237 printf(": failed to get interface, err=%s\n",
238 usbd_errstr(err));
239 goto bad;
240 }
241
242 sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
243 sc->sc_vendor = uaa->vendor;
244
245 if ((sc->sc_flags & (VISOR | PALM4)) == 0) {
246 printf("%s: device is neither visor nor palm\n",
247 sc->sc_dev.dv_xname);
248 goto bad;
249 }
250
251 id = usbd_get_interface_descriptor(iface);
252
253 sc->sc_udev = dev;
254 sc->sc_iface = iface;
255
256 uca.ibufsize = UVISORIBUFSIZE;
257 uca.obufsize = UVISOROBUFSIZE;
258 uca.ibufsizepad = UVISORIBUFSIZE;
259 uca.opkthdrlen = 0;
260 uca.device = dev;
261 uca.iface = iface;
262 uca.methods = &uvisor_methods;
263 uca.arg = sc;
264
265 err = uvisor_init(sc, &coninfo, &palmconinfo);
266 if (err) {
267 printf("%s: init failed, %s\n", sc->sc_dev.dv_xname,
268 usbd_errstr(err));
269 goto bad;
270 }
271
272 if (sc->sc_flags & VISOR) {
273 sc->sc_numcon = UGETW(coninfo.num_ports);
274 if (sc->sc_numcon > UVISOR_MAX_CONN)
275 sc->sc_numcon = UVISOR_MAX_CONN;
276
277 /* Attach a ucom for each connection. */
278 for (i = 0; i < sc->sc_numcon; ++i) {
279 switch (coninfo.connections[i].port_function_id) {
280 case UVISOR_FUNCTION_GENERIC:
281 uca.info = "Generic";
282 break;
283 case UVISOR_FUNCTION_DEBUGGER:
284 uca.info = "Debugger";
285 break;
286 case UVISOR_FUNCTION_HOTSYNC:
287 uca.info = "HotSync";
288 break;
289 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
290 uca.info = "Remote File System";
291 break;
292 default:
293 uca.info = "unknown";
294 break;
295 }
296 port = coninfo.connections[i].port;
297 uca.portno = port;
298 uca.bulkin = port | UE_DIR_IN;
299 uca.bulkout = port | UE_DIR_OUT;
300 /* Verify that endpoints exist. */
301 hasin = 0;
302 hasout = 0;
303 for (j = 0; j < id->bNumEndpoints; j++) {
304 ed = usbd_interface2endpoint_descriptor(iface, j);
305 if (ed == NULL)
306 break;
307 if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
308 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
309 if (UE_GET_DIR(ed->bEndpointAddress)
310 == UE_DIR_IN)
311 hasin++;
312 else
313 hasout++;
314 }
315 }
316 if (hasin == 1 && hasout == 1)
317 sc->sc_subdevs[i] = config_found_sm(self, &uca,
318 ucomprint, ucomsubmatch);
319 else
320 printf("%s: no proper endpoints for port %d (%d,%d)\n",
321 sc->sc_dev.dv_xname, port, hasin, hasout);
322 }
323 } else {
324 sc->sc_numcon = palmconinfo.num_ports;
325 if (sc->sc_numcon > UVISOR_MAX_CONN)
326 sc->sc_numcon = UVISOR_MAX_CONN;
327
328 /* Attach a ucom for each connection. */
329 for (i = 0; i < sc->sc_numcon; ++i) {
330 /*
331 * XXX this should copy out 4-char string from the
332 * XXX port_function_id, but where would the string go?
333 * XXX uca.info is a const char *, not an array.
334 */
335 uca.info = "sync";
336 uca.portno = i;
337 if (palmconinfo.endpoint_numbers_different) {
338 port = palmconinfo.connections[i].end_point_info;
339 uca.bulkin = (port >> 4) | UE_DIR_IN;
340 uca.bulkout = (port & 0xf) | UE_DIR_OUT;
341 } else {
342 port = palmconinfo.connections[i].port;
343 uca.bulkin = port | UE_DIR_IN;
344 uca.bulkout = port | UE_DIR_OUT;
345 }
346 sc->sc_subdevs[i] = config_found_sm(self, &uca,
347 ucomprint, ucomsubmatch);
348 }
349 }
350
351 return;
352
353 bad:
354 DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
355 usbd_deactivate(sc->sc_udev);
356 }
357
358 int
uvisor_detach(struct device * self,int flags)359 uvisor_detach(struct device *self, int flags)
360 {
361 struct uvisor_softc *sc = (struct uvisor_softc *)self;
362 int rv = 0;
363 int i;
364
365 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
366 for (i = 0; i < sc->sc_numcon; i++) {
367 if (sc->sc_subdevs[i] != NULL) {
368 rv |= config_detach(sc->sc_subdevs[i], flags);
369 sc->sc_subdevs[i] = NULL;
370 }
371 }
372
373 return (rv);
374 }
375
376 usbd_status
uvisor_init(struct uvisor_softc * sc,struct uvisor_connection_info * ci,struct uvisor_palm_connection_info * cpi)377 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
378 struct uvisor_palm_connection_info *cpi)
379 {
380 usbd_status err = 0;
381 usb_device_request_t req;
382 int actlen;
383 uWord avail;
384
385 if (sc->sc_flags & PALM4) {
386 DPRINTF(("uvisor_init: getting Palm connection info\n"));
387 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
388 req.bRequest = UVISOR_GET_PALM_INFORMATION;
389 USETW(req.wValue, 0);
390 USETW(req.wIndex, 0);
391 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
392 err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
393 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
394 if (err == USBD_STALLED && sc->sc_vendor == USB_VENDOR_SONY) {
395 /* some sony clie devices stall on palm4 requests,
396 * switch them over to using visor. dont do free space
397 * checks on them since they dont like them either.
398 */
399 DPRINTF(("switching role for CLIE probe\n"));
400 sc->sc_flags = CLIE4;
401 err = 0;
402 }
403 if (err)
404 return (err);
405 }
406
407 if (sc->sc_flags & VISOR) {
408 DPRINTF(("uvisor_init: getting Visor connection info\n"));
409 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
410 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
411 USETW(req.wValue, 0);
412 USETW(req.wIndex, 0);
413 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
414 err = usbd_do_request_flags(sc->sc_udev, &req, ci,
415 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
416 if (err)
417 return (err);
418 }
419
420 if (sc->sc_flags & NOFRE)
421 return (err);
422
423 DPRINTF(("uvisor_init: getting available bytes\n"));
424 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
425 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
426 USETW(req.wValue, 0);
427 USETW(req.wIndex, 5);
428 USETW(req.wLength, sizeof avail);
429 err = usbd_do_request(sc->sc_udev, &req, &avail);
430 if (err)
431 return (err);
432 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
433 DPRINTF(("uvisor_init: done\n"));
434 return (err);
435 }
436
437 void
uvisor_close(void * addr,int portno)438 uvisor_close(void *addr, int portno)
439 {
440 struct uvisor_softc *sc = addr;
441 usb_device_request_t req;
442 struct uvisor_connection_info coninfo; /* XXX ? */
443 int actlen;
444
445 if (usbd_is_dying(sc->sc_udev))
446 return;
447
448 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
449 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
450 USETW(req.wValue, 0);
451 USETW(req.wIndex, 0);
452 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
453 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
454 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
455 }
456