uvisor.c revision 1.18 1 /* $NetBSD: uvisor.c,v 1.18 2003/02/05 00:50:14 augustss 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.18 2003/02/05 00:50:14 augustss 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 #include <dev/usb/usbhid.h>
56
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/usbdevs.h>
60
61 #include <dev/usb/ucomvar.h>
62
63 #ifdef UVISOR_DEBUG
64 #define DPRINTF(x) if (uvisordebug) printf x
65 #define DPRINTFN(n,x) if (uvisordebug>(n)) printf x
66 int uvisordebug = 0;
67 #else
68 #define DPRINTF(x)
69 #define DPRINTFN(n,x)
70 #endif
71
72 #define UVISOR_CONFIG_INDEX 0
73 #define UVISOR_IFACE_INDEX 0
74
75 /* From the Linux driver */
76 /*
77 * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
78 * are available to be transfered to the host for the specified endpoint.
79 * Currently this is not used, and always returns 0x0001
80 */
81 #define UVISOR_REQUEST_BYTES_AVAILABLE 0x01
82
83 /*
84 * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
85 * is now closing the pipe. An empty packet is sent in response.
86 */
87 #define UVISOR_CLOSE_NOTIFICATION 0x02
88
89 /*
90 * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
91 * get the endpoints used by the connection.
92 */
93 #define UVISOR_GET_CONNECTION_INFORMATION 0x03
94
95
96 /*
97 * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
98 */
99 #define UVISOR_MAX_CONN 8
100 struct uvisor_connection_info {
101 uWord num_ports;
102 struct {
103 uByte port_function_id;
104 uByte port;
105 } connections[UVISOR_MAX_CONN];
106 };
107 #define UVISOR_CONNECTION_INFO_SIZE 18
108
109 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
110 #define UVISOR_FUNCTION_GENERIC 0x00
111 #define UVISOR_FUNCTION_DEBUGGER 0x01
112 #define UVISOR_FUNCTION_HOTSYNC 0x02
113 #define UVISOR_FUNCTION_CONSOLE 0x03
114 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
115
116 /*
117 * Unknown PalmOS stuff.
118 */
119 #define UVISOR_GET_PALM_INFORMATION 0x04
120 #define UVISOR_GET_PALM_INFORMATION_LEN 0x14
121
122
123 #define UVISORIBUFSIZE 1024
124 #define UVISOROBUFSIZE 1024
125
126 struct uvisor_softc {
127 USBBASEDEVICE sc_dev; /* base device */
128 usbd_device_handle sc_udev; /* device */
129 usbd_interface_handle sc_iface; /* interface */
130
131 device_ptr_t sc_subdevs[UVISOR_MAX_CONN];
132 int sc_numcon;
133
134 u_int16_t sc_flags;
135
136 u_char sc_dying;
137 };
138
139 Static usbd_status uvisor_init(struct uvisor_softc *,
140 struct uvisor_connection_info *);
141
142 Static void uvisor_close(void *, int);
143
144
145 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 };
161 static const struct uvisor_type uvisor_devs[] = {
162 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, 0 },
163 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
164 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
165 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
166 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
167 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
168 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
169 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, 0 },
170 /* {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
171 };
172 #define uvisor_lookup(v, p) ((struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
173
174 USB_DECLARE_DRIVER(uvisor);
175
176 USB_MATCH(uvisor)
177 {
178 USB_MATCH_START(uvisor, uaa);
179
180 if (uaa->iface != NULL)
181 return (UMATCH_NONE);
182
183 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
184 uaa->vendor, uaa->product));
185
186 return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
187 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
188 }
189
190 USB_ATTACH(uvisor)
191 {
192 USB_ATTACH_START(uvisor, sc, uaa);
193 usbd_device_handle dev = uaa->device;
194 usbd_interface_handle iface;
195 usb_interface_descriptor_t *id;
196 struct uvisor_connection_info coninfo;
197 usb_endpoint_descriptor_t *ed;
198 char devinfo[1024];
199 char *devname = USBDEVNAME(sc->sc_dev);
200 int i, j, hasin, hasout, port;
201 usbd_status err;
202 struct ucom_attach_args uca;
203
204 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
205
206 /* Move the device into the configured state. */
207 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
208 if (err) {
209 printf("\n%s: failed to set configuration, err=%s\n",
210 devname, usbd_errstr(err));
211 goto bad;
212 }
213
214 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
215 if (err) {
216 printf("\n%s: failed to get interface, err=%s\n",
217 devname, usbd_errstr(err));
218 goto bad;
219 }
220
221 usbd_devinfo(dev, 0, devinfo);
222 USB_ATTACH_SETUP;
223 printf("%s: %s\n", devname, devinfo);
224
225 sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
226
227 id = usbd_get_interface_descriptor(iface);
228
229 sc->sc_udev = dev;
230 sc->sc_iface = iface;
231
232 uca.ibufsize = UVISORIBUFSIZE;
233 uca.obufsize = UVISOROBUFSIZE;
234 uca.ibufsizepad = UVISORIBUFSIZE;
235 uca.opkthdrlen = 0;
236 uca.device = dev;
237 uca.iface = iface;
238 uca.methods = &uvisor_methods;
239 uca.arg = sc;
240
241 err = uvisor_init(sc, &coninfo);
242 if (err) {
243 printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
244 usbd_errstr(err));
245 goto bad;
246 }
247
248 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
249 USBDEV(sc->sc_dev));
250
251 sc->sc_numcon = UGETW(coninfo.num_ports);
252 if (sc->sc_numcon > UVISOR_MAX_CONN)
253 sc->sc_numcon = UVISOR_MAX_CONN;
254
255 /* Attach a ucom for each connection. */
256 for (i = 0; i < sc->sc_numcon; ++i) {
257 switch (coninfo.connections[i].port_function_id) {
258 case UVISOR_FUNCTION_GENERIC:
259 uca.info = "Generic";
260 break;
261 case UVISOR_FUNCTION_DEBUGGER:
262 uca.info = "Debugger";
263 break;
264 case UVISOR_FUNCTION_HOTSYNC:
265 uca.info = "HotSync";
266 break;
267 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
268 uca.info = "Remote File System";
269 break;
270 default:
271 uca.info = "unknown";
272 break;
273 }
274 port = coninfo.connections[i].port;
275 uca.portno = port;
276 uca.bulkin = port | UE_DIR_IN;
277 uca.bulkout = port | UE_DIR_OUT;
278 /* Verify that endpoints exist. */
279 for (hasin = hasout = j = 0; j < id->bNumEndpoints; j++) {
280 ed = usbd_interface2endpoint_descriptor(iface, j);
281 if (ed == NULL)
282 break;
283 if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
284 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
285 if (UE_GET_DIR(ed->bEndpointAddress)
286 == UE_DIR_IN)
287 hasin++;
288 else
289 hasout++;
290 }
291 }
292 if (hasin == 1 && hasout == 1)
293 sc->sc_subdevs[i] = config_found_sm(self, &uca,
294 ucomprint, ucomsubmatch);
295 else
296 printf("%s: no proper endpoints for port %d (%d,%d)\n",
297 USBDEVNAME(sc->sc_dev), port, hasin, hasout);
298 }
299
300 USB_ATTACH_SUCCESS_RETURN;
301
302 bad:
303 DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
304 sc->sc_dying = 1;
305 USB_ATTACH_ERROR_RETURN;
306 }
307
308 int
309 uvisor_activate(device_ptr_t self, enum devact act)
310 {
311 struct uvisor_softc *sc = (struct uvisor_softc *)self;
312 int rv = 0;
313 int i;
314
315 switch (act) {
316 case DVACT_ACTIVATE:
317 return (EOPNOTSUPP);
318 break;
319
320 case DVACT_DEACTIVATE:
321 for (i = 0; i < sc->sc_numcon; i++)
322 if (sc->sc_subdevs[i] != NULL)
323 rv = config_deactivate(sc->sc_subdevs[i]);
324 sc->sc_dying = 1;
325 break;
326 }
327 return (rv);
328 }
329
330 int
331 uvisor_detach(device_ptr_t self, int flags)
332 {
333 struct uvisor_softc *sc = (struct uvisor_softc *)self;
334 int rv = 0;
335 int i;
336
337 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
338 sc->sc_dying = 1;
339 for (i = 0; i < sc->sc_numcon; i++) {
340 if (sc->sc_subdevs[i] != NULL) {
341 rv |= config_detach(sc->sc_subdevs[i], flags);
342 sc->sc_subdevs[i] = NULL;
343 }
344 }
345
346 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
347 USBDEV(sc->sc_dev));
348
349
350 return (rv);
351 }
352
353 usbd_status
354 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci)
355 {
356 usbd_status err;
357 usb_device_request_t req;
358 int actlen;
359 uWord avail;
360 char buffer[256];
361
362 DPRINTF(("uvisor_init: getting connection info\n"));
363 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
364 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
365 USETW(req.wValue, 0);
366 USETW(req.wIndex, 0);
367 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
368 err = usbd_do_request_flags(sc->sc_udev, &req, ci,
369 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
370 if (err)
371 return (err);
372
373 if (sc->sc_flags & PALM4) {
374 /* Palm OS 4.0 Hack */
375 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
376 req.bRequest = UVISOR_GET_PALM_INFORMATION;
377 USETW(req.wValue, 0);
378 USETW(req.wIndex, 0);
379 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
380 err = usbd_do_request(sc->sc_udev, &req, buffer);
381 if (err)
382 return (err);
383 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
384 req.bRequest = UVISOR_GET_PALM_INFORMATION;
385 USETW(req.wValue, 0);
386 USETW(req.wIndex, 0);
387 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
388 err = usbd_do_request(sc->sc_udev, &req, buffer);
389 if (err)
390 return (err);
391 }
392
393 DPRINTF(("uvisor_init: getting available bytes\n"));
394 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
395 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
396 USETW(req.wValue, 0);
397 USETW(req.wIndex, 5);
398 USETW(req.wLength, sizeof avail);
399 err = usbd_do_request(sc->sc_udev, &req, &avail);
400 if (err)
401 return (err);
402 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
403
404 DPRINTF(("uvisor_init: done\n"));
405 return (err);
406 }
407
408 void
409 uvisor_close(void *addr, int portno)
410 {
411 struct uvisor_softc *sc = addr;
412 usb_device_request_t req;
413 struct uvisor_connection_info coninfo; /* XXX ? */
414 int actlen;
415
416 if (sc->sc_dying)
417 return;
418
419 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
420 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
421 USETW(req.wValue, 0);
422 USETW(req.wIndex, 0);
423 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
424 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
425 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
426 }
427