uvisor.c revision 1.48 1 /* $NetBSD: uvisor.c,v 1.48 2016/11/25 12:56:29 skrll 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 (at) 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.48 2016/11/25 12:56:29 skrll 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 transfered 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 u_char 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 void uvisor_close(void *, int);
153
154
155 struct ucom_methods uvisor_methods = {
156 .ucom_param = NULL,
157 .ucom_ioctl = NULL,
158 .ucom_open = NULL,
159 .ucom_close = uvisor_close,
160 .ucom_read = NULL,
161 .ucom_write = NULL,
162 .ucom_get_status = NULL,
163 .ucom_set = NULL,
164 };
165
166 struct uvisor_type {
167 struct usb_devno uv_dev;
168 uint16_t uv_flags;
169 #define PALM4 0x0001
170 #define VISOR 0x0002
171
172 };
173 static const struct uvisor_type uvisor_devs[] = {
174 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR },
175 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 },
176 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600 }, PALM4 },
177 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
178 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
179 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
180 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 },
181 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
182 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 },
183 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 },
184 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 },
185 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE31 }, PALM4 },
186 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
187 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
188 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 },
189 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 },
190 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 },
191 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_35 }, 0 },
192 /* {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
193 };
194 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
195
196 int uvisor_match(device_t, cfdata_t, void *);
197 void uvisor_attach(device_t, device_t, void *);
198 void uvisor_childdet(device_t, device_t);
199 int uvisor_detach(device_t, int);
200 int uvisor_activate(device_t, enum devact);
201 extern struct cfdriver uvisor_cd;
202 CFATTACH_DECL2_NEW(uvisor, sizeof(struct uvisor_softc), uvisor_match,
203 uvisor_attach, uvisor_detach, uvisor_activate, NULL, uvisor_childdet);
204
205 int
206 uvisor_match(device_t parent, cfdata_t match, void *aux)
207 {
208 struct usb_attach_arg *uaa = aux;
209
210 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
211 uaa->uaa_vendor, uaa->uaa_product));
212
213 return uvisor_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
214 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
215 }
216
217 void
218 uvisor_attach(device_t parent, device_t self, void *aux)
219 {
220 struct uvisor_softc *sc = device_private(self);
221 struct usb_attach_arg *uaa = aux;
222 struct usbd_device *dev = uaa->uaa_device;
223 struct usbd_interface *iface;
224 usb_interface_descriptor_t *id;
225 struct uvisor_connection_info coninfo;
226 struct uvisor_palm_connection_info palmconinfo;
227 usb_endpoint_descriptor_t *ed;
228 char *devinfop;
229 const char *devname = device_xname(self);
230 int i, j, hasin, hasout, port;
231 usbd_status err;
232 struct ucom_attach_args ucaa;
233
234 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
235
236 sc->sc_dev = self;
237
238 aprint_naive("\n");
239 aprint_normal("\n");
240
241 devinfop = usbd_devinfo_alloc(dev, 0);
242 aprint_normal_dev(self, "%s\n", devinfop);
243 usbd_devinfo_free(devinfop);
244
245 /* Move the device into the configured state. */
246 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
247 if (err) {
248 aprint_error("\n%s: failed to set configuration, err=%s\n",
249 devname, usbd_errstr(err));
250 goto bad;
251 }
252
253 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
254 if (err) {
255 aprint_error("\n%s: failed to get interface, err=%s\n",
256 devname, usbd_errstr(err));
257 goto bad;
258 }
259
260 sc->sc_flags = uvisor_lookup(uaa->uaa_vendor, uaa->uaa_product)->uv_flags;
261
262 if ((sc->sc_flags & (VISOR | PALM4)) == 0) {
263 aprint_error_dev(self,
264 "init failed, device type is neither visor nor palm\n");
265 goto bad;
266 }
267
268 id = usbd_get_interface_descriptor(iface);
269
270 sc->sc_udev = dev;
271 sc->sc_iface = iface;
272
273 ucaa.ucaa_ibufsize = UVISORIBUFSIZE;
274 ucaa.ucaa_obufsize = UVISOROBUFSIZE;
275 ucaa.ucaa_ibufsizepad = UVISORIBUFSIZE;
276 ucaa.ucaa_opkthdrlen = 0;
277 ucaa.ucaa_device = dev;
278 ucaa.ucaa_iface = iface;
279 ucaa.ucaa_methods = &uvisor_methods;
280 ucaa.ucaa_arg = sc;
281
282 err = uvisor_init(sc, &coninfo, &palmconinfo);
283 if (err) {
284 aprint_error_dev(self, "init failed, %s\n", usbd_errstr(err));
285 goto bad;
286 }
287
288 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
289
290 if (sc->sc_flags & VISOR) {
291 sc->sc_numcon = UGETW(coninfo.num_ports);
292 if (sc->sc_numcon > UVISOR_MAX_CONN)
293 sc->sc_numcon = UVISOR_MAX_CONN;
294
295 /* Attach a ucom for each connection. */
296 for (i = 0; i < sc->sc_numcon; ++i) {
297 switch (coninfo.connections[i].port_function_id) {
298 case UVISOR_FUNCTION_GENERIC:
299 ucaa.ucaa_info = "Generic";
300 break;
301 case UVISOR_FUNCTION_DEBUGGER:
302 ucaa.ucaa_info = "Debugger";
303 break;
304 case UVISOR_FUNCTION_HOTSYNC:
305 ucaa.ucaa_info = "HotSync";
306 break;
307 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
308 ucaa.ucaa_info = "Remote File System";
309 break;
310 default:
311 ucaa.ucaa_info = "unknown";
312 break;
313 }
314 port = coninfo.connections[i].port;
315 ucaa.ucaa_portno = port;
316 ucaa.ucaa_bulkin = port | UE_DIR_IN;
317 ucaa.ucaa_bulkout = port | UE_DIR_OUT;
318 /* Verify that endpoints exist. */
319 hasin = 0;
320 hasout = 0;
321 for (j = 0; j < id->bNumEndpoints; j++) {
322 ed = usbd_interface2endpoint_descriptor(iface, j);
323 if (ed == NULL)
324 break;
325 if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
326 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
327 if (UE_GET_DIR(ed->bEndpointAddress)
328 == UE_DIR_IN)
329 hasin++;
330 else
331 hasout++;
332 }
333 }
334 if (hasin == 1 && hasout == 1)
335 sc->sc_subdevs[i] = config_found_sm_loc(self,
336 "ucombus", NULL, &ucaa,
337 ucomprint, ucomsubmatch);
338 else
339 aprint_error_dev(self,
340 "no proper endpoints for port %d (%d,%d)\n",
341 port, hasin, hasout);
342 }
343
344 } else {
345 sc->sc_numcon = palmconinfo.num_ports;
346 if (sc->sc_numcon > UVISOR_MAX_CONN)
347 sc->sc_numcon = UVISOR_MAX_CONN;
348
349 /* Attach a ucom for each connection. */
350 for (i = 0; i < sc->sc_numcon; ++i) {
351 /*
352 * XXX this should copy out 4-char string from the
353 * XXX port_function_id, but where would the string go?
354 * XXX ucaa.ucaa_info is a const char *, not an array.
355 */
356 ucaa.ucaa_info = "sync";
357 ucaa.ucaa_portno = i;
358 if (palmconinfo.endpoint_numbers_different) {
359 port = palmconinfo.connections[i].end_point_info;
360 ucaa.ucaa_bulkin = (port >> 4) | UE_DIR_IN;
361 ucaa.ucaa_bulkout = (port & 0xf) | UE_DIR_OUT;
362 } else {
363 port = palmconinfo.connections[i].port;
364 ucaa.ucaa_bulkin = port | UE_DIR_IN;
365 ucaa.ucaa_bulkout = port | UE_DIR_OUT;
366 }
367 sc->sc_subdevs[i] = config_found_sm_loc(self, "ucombus",
368 NULL, &ucaa, ucomprint, ucomsubmatch);
369
370
371 }
372 }
373
374 return;
375
376 bad:
377 DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
378 sc->sc_dying = 1;
379 return;
380 }
381
382 int
383 uvisor_activate(device_t self, enum devact act)
384 {
385 struct uvisor_softc *sc = device_private(self);
386
387 switch (act) {
388 case DVACT_DEACTIVATE:
389 sc->sc_dying = 1;
390 return 0;
391 default:
392 return EOPNOTSUPP;
393 }
394 }
395
396 void
397 uvisor_childdet(device_t self, device_t child)
398 {
399 int i;
400 struct uvisor_softc *sc = device_private(self);
401
402 for (i = 0; i < sc->sc_numcon; i++) {
403 if (sc->sc_subdevs[i] == child)
404 break;
405 }
406 KASSERT(i < sc->sc_numcon);
407 sc->sc_subdevs[i] = NULL;
408 }
409
410 int
411 uvisor_detach(device_t self, int flags)
412 {
413 struct uvisor_softc *sc = device_private(self);
414 int rv = 0;
415 int i;
416
417 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
418 sc->sc_dying = 1;
419 for (i = 0; i < sc->sc_numcon; i++) {
420 if (sc->sc_subdevs[i] != NULL)
421 rv |= config_detach(sc->sc_subdevs[i], flags);
422 }
423
424 if (sc->sc_udev)
425 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
426 sc->sc_dev);
427
428
429 return rv;
430 }
431
432 usbd_status
433 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
434 struct uvisor_palm_connection_info *cpi)
435 {
436 usbd_status err;
437 usb_device_request_t req;
438 int actlen;
439 uWord avail;
440
441 if (sc->sc_flags & VISOR) {
442 DPRINTF(("uvisor_init: getting Visor connection info\n"));
443 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
444 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
445 USETW(req.wValue, 0);
446 USETW(req.wIndex, 0);
447 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
448 err = usbd_do_request_flags(sc->sc_udev, &req, ci,
449 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
450 if (err)
451 return err;
452 }
453
454 if (sc->sc_flags & PALM4) {
455 DPRINTF(("uvisor_init: getting Palm connection info\n"));
456 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
457 req.bRequest = UVISOR_GET_PALM_INFORMATION;
458 USETW(req.wValue, 0);
459 USETW(req.wIndex, 0);
460 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
461 err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
462 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
463 if (err)
464 return err;
465 }
466
467 DPRINTF(("uvisor_init: getting available bytes\n"));
468 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
469 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
470 USETW(req.wValue, 0);
471 USETW(req.wIndex, 5);
472 USETW(req.wLength, sizeof(avail));
473 err = usbd_do_request(sc->sc_udev, &req, &avail);
474 if (err)
475 return err;
476 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
477
478 DPRINTF(("uvisor_init: done\n"));
479 return err;
480 }
481
482 void
483 uvisor_close(void *addr, int portno)
484 {
485 struct uvisor_softc *sc = addr;
486 usb_device_request_t req;
487 struct uvisor_connection_info coninfo; /* XXX ? */
488 int actlen;
489
490 if (sc->sc_dying)
491 return;
492
493 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
494 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
495 USETW(req.wValue, 0);
496 USETW(req.wIndex, 0);
497 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
498 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
499 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
500 }
501