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