uvisor.c revision 1.14 1 /* $NetBSD: uvisor.c,v 1.14 2002/02/27 23:00:03 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.14 2002/02/27 23:00:03 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_M125 }, PALM4 },
166 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
167 /* {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
168 };
169 #define uvisor_lookup(v, p) ((struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
170
171 USB_DECLARE_DRIVER(uvisor);
172
173 USB_MATCH(uvisor)
174 {
175 USB_MATCH_START(uvisor, uaa);
176
177 if (uaa->iface != NULL)
178 return (UMATCH_NONE);
179
180 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
181 uaa->vendor, uaa->product));
182
183 return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
184 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
185 }
186
187 USB_ATTACH(uvisor)
188 {
189 USB_ATTACH_START(uvisor, sc, uaa);
190 usbd_device_handle dev = uaa->device;
191 usbd_interface_handle iface;
192 usb_interface_descriptor_t *id;
193 struct uvisor_connection_info coninfo;
194 usb_endpoint_descriptor_t *ed;
195 char devinfo[1024];
196 char *devname = USBDEVNAME(sc->sc_dev);
197 int i, j, hasin, hasout, port;
198 usbd_status err;
199 struct ucom_attach_args uca;
200
201 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
202
203 /* Move the device into the configured state. */
204 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
205 if (err) {
206 printf("\n%s: failed to set configuration, err=%s\n",
207 devname, usbd_errstr(err));
208 goto bad;
209 }
210
211 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
212 if (err) {
213 printf("\n%s: failed to get interface, err=%s\n",
214 devname, usbd_errstr(err));
215 goto bad;
216 }
217
218 usbd_devinfo(dev, 0, devinfo);
219 USB_ATTACH_SETUP;
220 printf("%s: %s\n", devname, devinfo);
221
222 sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
223
224 id = usbd_get_interface_descriptor(iface);
225
226 sc->sc_udev = dev;
227 sc->sc_iface = iface;
228
229 uca.ibufsize = UVISORIBUFSIZE;
230 uca.obufsize = UVISOROBUFSIZE;
231 uca.ibufsizepad = UVISORIBUFSIZE;
232 uca.opkthdrlen = 0;
233 uca.device = dev;
234 uca.iface = iface;
235 uca.methods = &uvisor_methods;
236 uca.arg = sc;
237
238 err = uvisor_init(sc, &coninfo);
239 if (err) {
240 printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
241 usbd_errstr(err));
242 goto bad;
243 }
244
245 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
246 USBDEV(sc->sc_dev));
247
248 sc->sc_numcon = UGETW(coninfo.num_ports);
249 if (sc->sc_numcon > UVISOR_MAX_CONN)
250 sc->sc_numcon = UVISOR_MAX_CONN;
251
252 /* Attach a ucom for each connection. */
253 for (i = 0; i < sc->sc_numcon; ++i) {
254 switch (coninfo.connections[i].port_function_id) {
255 case UVISOR_FUNCTION_GENERIC:
256 uca.info = "Generic";
257 break;
258 case UVISOR_FUNCTION_DEBUGGER:
259 uca.info = "Debugger";
260 break;
261 case UVISOR_FUNCTION_HOTSYNC:
262 uca.info = "HotSync";
263 break;
264 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
265 uca.info = "Remote File System";
266 break;
267 default:
268 uca.info = "unknown";
269 break;
270 }
271 port = coninfo.connections[i].port;
272 uca.portno = port;
273 uca.bulkin = port | UE_DIR_IN;
274 uca.bulkout = port | UE_DIR_OUT;
275 /* Verify that endpoints exist. */
276 for (hasin = hasout = j = 0; j < id->bNumEndpoints; j++) {
277 ed = usbd_interface2endpoint_descriptor(iface, j);
278 if (ed == NULL)
279 break;
280 if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
281 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
282 if (UE_GET_DIR(ed->bEndpointAddress)
283 == UE_DIR_IN)
284 hasin++;
285 else
286 hasout++;
287 }
288 }
289 if (hasin == 1 && hasout == 1)
290 sc->sc_subdevs[i] = config_found_sm(self, &uca,
291 ucomprint, ucomsubmatch);
292 else
293 printf("%s: no proper endpoints for port %d (%d,%d)\n",
294 USBDEVNAME(sc->sc_dev), port, hasin, hasout);
295 }
296
297 USB_ATTACH_SUCCESS_RETURN;
298
299 bad:
300 DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
301 sc->sc_dying = 1;
302 USB_ATTACH_ERROR_RETURN;
303 }
304
305 int
306 uvisor_activate(device_ptr_t self, enum devact act)
307 {
308 struct uvisor_softc *sc = (struct uvisor_softc *)self;
309 int rv = 0;
310 int i;
311
312 switch (act) {
313 case DVACT_ACTIVATE:
314 return (EOPNOTSUPP);
315 break;
316
317 case DVACT_DEACTIVATE:
318 for (i = 0; i < sc->sc_numcon; i++)
319 if (sc->sc_subdevs[i] != NULL)
320 rv = config_deactivate(sc->sc_subdevs[i]);
321 sc->sc_dying = 1;
322 break;
323 }
324 return (rv);
325 }
326
327 int
328 uvisor_detach(device_ptr_t self, int flags)
329 {
330 struct uvisor_softc *sc = (struct uvisor_softc *)self;
331 int rv = 0;
332 int i;
333
334 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
335 sc->sc_dying = 1;
336 for (i = 0; i < sc->sc_numcon; i++) {
337 if (sc->sc_subdevs[i] != NULL) {
338 rv |= config_detach(sc->sc_subdevs[i], flags);
339 sc->sc_subdevs[i] = NULL;
340 }
341 }
342
343 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
344 USBDEV(sc->sc_dev));
345
346
347 return (rv);
348 }
349
350 usbd_status
351 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci)
352 {
353 usbd_status err;
354 usb_device_request_t req;
355 int actlen;
356 uWord avail;
357 char buffer[256];
358
359 DPRINTF(("uvisor_init: getting connection info\n"));
360 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
361 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
362 USETW(req.wValue, 0);
363 USETW(req.wIndex, 0);
364 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
365 err = usbd_do_request_flags(sc->sc_udev, &req, ci,
366 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
367 if (err)
368 return (err);
369
370 if (sc->sc_flags & PALM4) {
371 /* Palm OS 4.0 Hack */
372 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
373 req.bRequest = UVISOR_GET_PALM_INFORMATION;
374 USETW(req.wValue, 0);
375 USETW(req.wIndex, 0);
376 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
377 err = usbd_do_request(sc->sc_udev, &req, buffer);
378 if (err)
379 return (err);
380 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
381 req.bRequest = UVISOR_GET_PALM_INFORMATION;
382 USETW(req.wValue, 0);
383 USETW(req.wIndex, 0);
384 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
385 err = usbd_do_request(sc->sc_udev, &req, buffer);
386 if (err)
387 return (err);
388 }
389
390 DPRINTF(("uvisor_init: getting available bytes\n"));
391 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
392 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
393 USETW(req.wValue, 0);
394 USETW(req.wIndex, 5);
395 USETW(req.wLength, sizeof avail);
396 err = usbd_do_request(sc->sc_udev, &req, &avail);
397 if (err)
398 return (err);
399 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
400
401 DPRINTF(("uvisor_init: done\n"));
402 return (err);
403 }
404
405 void
406 uvisor_close(void *addr, int portno)
407 {
408 struct uvisor_softc *sc = addr;
409 usb_device_request_t req;
410 struct uvisor_connection_info coninfo; /* XXX ? */
411 int actlen;
412
413 if (sc->sc_dying)
414 return;
415
416 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
417 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
418 USETW(req.wValue, 0);
419 USETW(req.wIndex, 0);
420 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
421 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
422 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
423 }
424