uvisor.c revision 1.10 1 1.10 augustss /* $NetBSD: uvisor.c,v 1.10 2001/01/23 21:19:44 augustss Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.6 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.1 augustss * Carlstedt Research & Technology.
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss * 3. All advertising materials mentioning features or use of this software
20 1.1 augustss * must display the following acknowledgement:
21 1.1 augustss * This product includes software developed by the NetBSD
22 1.1 augustss * Foundation, Inc. and its contributors.
23 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 augustss * contributors may be used to endorse or promote products derived
25 1.1 augustss * from this software without specific prior written permission.
26 1.1 augustss *
27 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
38 1.1 augustss */
39 1.1 augustss
40 1.1 augustss /*
41 1.2 hubertf * Handspring Visor (Palmpilot compatible PDA) driver
42 1.1 augustss */
43 1.1 augustss
44 1.1 augustss #include <sys/param.h>
45 1.1 augustss #include <sys/systm.h>
46 1.1 augustss #include <sys/kernel.h>
47 1.1 augustss #include <sys/device.h>
48 1.1 augustss #include <sys/conf.h>
49 1.1 augustss #include <sys/tty.h>
50 1.1 augustss
51 1.1 augustss #include <dev/usb/usb.h>
52 1.1 augustss #include <dev/usb/usbhid.h>
53 1.1 augustss
54 1.1 augustss #include <dev/usb/usbdi.h>
55 1.1 augustss #include <dev/usb/usbdi_util.h>
56 1.1 augustss #include <dev/usb/usbdevs.h>
57 1.1 augustss
58 1.1 augustss #include <dev/usb/ucomvar.h>
59 1.1 augustss
60 1.1 augustss #ifdef UVISOR_DEBUG
61 1.1 augustss #define DPRINTF(x) if (uvisordebug) printf x
62 1.1 augustss #define DPRINTFN(n,x) if (uvisordebug>(n)) printf x
63 1.3 augustss int uvisordebug = 0;
64 1.1 augustss #else
65 1.1 augustss #define DPRINTF(x)
66 1.1 augustss #define DPRINTFN(n,x)
67 1.1 augustss #endif
68 1.1 augustss
69 1.1 augustss #define UVISOR_CONFIG_INDEX 0
70 1.1 augustss #define UVISOR_IFACE_INDEX 0
71 1.1 augustss
72 1.1 augustss /* From the Linux driver */
73 1.1 augustss /*
74 1.1 augustss * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
75 1.1 augustss * are available to be transfered to the host for the specified endpoint.
76 1.1 augustss * Currently this is not used, and always returns 0x0001
77 1.1 augustss */
78 1.1 augustss #define UVISOR_REQUEST_BYTES_AVAILABLE 0x01
79 1.1 augustss
80 1.1 augustss /*
81 1.1 augustss * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
82 1.1 augustss * is now closing the pipe. An empty packet is sent in response.
83 1.1 augustss */
84 1.1 augustss #define UVISOR_CLOSE_NOTIFICATION 0x02
85 1.1 augustss
86 1.1 augustss /*
87 1.1 augustss * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
88 1.1 augustss * get the endpoints used by the connection.
89 1.1 augustss */
90 1.1 augustss #define UVISOR_GET_CONNECTION_INFORMATION 0x03
91 1.1 augustss
92 1.1 augustss
93 1.1 augustss /*
94 1.1 augustss * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
95 1.1 augustss */
96 1.10 augustss #define UVISOR_MAX_CONN 8
97 1.1 augustss struct uvisor_connection_info {
98 1.1 augustss uWord num_ports;
99 1.1 augustss struct {
100 1.1 augustss uByte port_function_id;
101 1.1 augustss uByte port;
102 1.10 augustss } connections[UVISOR_MAX_CONN];
103 1.1 augustss };
104 1.1 augustss #define UVISOR_CONNECTION_INFO_SIZE 18
105 1.1 augustss
106 1.1 augustss
107 1.1 augustss /* struct uvisor_connection_info.connection[x].port defines: */
108 1.1 augustss #define UVISOR_ENDPOINT_1 0x01
109 1.1 augustss #define UVISOR_ENDPOINT_2 0x02
110 1.1 augustss
111 1.1 augustss /* struct uvisor_connection_info.connection[x].port_function_id defines: */
112 1.1 augustss #define UVISOR_FUNCTION_GENERIC 0x00
113 1.1 augustss #define UVISOR_FUNCTION_DEBUGGER 0x01
114 1.1 augustss #define UVISOR_FUNCTION_HOTSYNC 0x02
115 1.1 augustss #define UVISOR_FUNCTION_CONSOLE 0x03
116 1.1 augustss #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
117 1.1 augustss
118 1.1 augustss
119 1.4 augustss #define UVISORIBUFSIZE 1024
120 1.4 augustss #define UVISOROBUFSIZE 1024
121 1.4 augustss
122 1.1 augustss struct uvisor_softc {
123 1.1 augustss USBBASEDEVICE sc_dev; /* base device */
124 1.1 augustss usbd_device_handle sc_udev; /* device */
125 1.1 augustss usbd_interface_handle sc_iface; /* interface */
126 1.1 augustss
127 1.10 augustss device_ptr_t sc_subdevs[UVISOR_MAX_CONN];
128 1.10 augustss int sc_numcon;
129 1.1 augustss
130 1.1 augustss u_char sc_dying;
131 1.1 augustss };
132 1.1 augustss
133 1.10 augustss Static usbd_status uvisor_init(struct uvisor_softc *,
134 1.10 augustss struct uvisor_connection_info *);
135 1.1 augustss
136 1.7 augustss Static void uvisor_close(void *, int);
137 1.1 augustss
138 1.1 augustss
139 1.1 augustss struct ucom_methods uvisor_methods = {
140 1.1 augustss NULL,
141 1.1 augustss NULL,
142 1.1 augustss NULL,
143 1.1 augustss NULL,
144 1.1 augustss NULL,
145 1.1 augustss uvisor_close,
146 1.5 augustss NULL,
147 1.5 augustss NULL,
148 1.1 augustss };
149 1.1 augustss
150 1.1 augustss USB_DECLARE_DRIVER(uvisor);
151 1.1 augustss
152 1.1 augustss USB_MATCH(uvisor)
153 1.1 augustss {
154 1.1 augustss USB_MATCH_START(uvisor, uaa);
155 1.1 augustss
156 1.1 augustss if (uaa->iface != NULL)
157 1.1 augustss return (UMATCH_NONE);
158 1.1 augustss
159 1.1 augustss DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
160 1.1 augustss uaa->vendor, uaa->product));
161 1.1 augustss
162 1.1 augustss if (uaa->vendor == USB_VENDOR_HANDSPRING &&
163 1.3 augustss uaa->product == USB_PRODUCT_HANDSPRING_VISOR)
164 1.1 augustss return (UMATCH_VENDOR_PRODUCT);
165 1.1 augustss
166 1.1 augustss return (UMATCH_NONE);
167 1.1 augustss }
168 1.1 augustss
169 1.1 augustss USB_ATTACH(uvisor)
170 1.1 augustss {
171 1.1 augustss USB_ATTACH_START(uvisor, sc, uaa);
172 1.1 augustss usbd_device_handle dev = uaa->device;
173 1.1 augustss usbd_interface_handle iface;
174 1.1 augustss usb_interface_descriptor_t *id;
175 1.10 augustss struct uvisor_connection_info coninfo;
176 1.1 augustss usb_endpoint_descriptor_t *ed;
177 1.1 augustss char devinfo[1024];
178 1.1 augustss char *devname = USBDEVNAME(sc->sc_dev);
179 1.10 augustss int i, j, hasin, hasout, port;
180 1.10 augustss char *string;
181 1.1 augustss usbd_status err;
182 1.1 augustss struct ucom_attach_args uca;
183 1.1 augustss
184 1.1 augustss DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
185 1.1 augustss
186 1.1 augustss /* Move the device into the configured state. */
187 1.1 augustss err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
188 1.1 augustss if (err) {
189 1.1 augustss printf("\n%s: failed to set configuration, err=%s\n",
190 1.1 augustss devname, usbd_errstr(err));
191 1.1 augustss goto bad;
192 1.1 augustss }
193 1.1 augustss
194 1.1 augustss err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
195 1.1 augustss if (err) {
196 1.1 augustss printf("\n%s: failed to get interface, err=%s\n",
197 1.1 augustss devname, usbd_errstr(err));
198 1.1 augustss goto bad;
199 1.1 augustss }
200 1.1 augustss
201 1.1 augustss usbd_devinfo(dev, 0, devinfo);
202 1.1 augustss USB_ATTACH_SETUP;
203 1.1 augustss printf("%s: %s\n", devname, devinfo);
204 1.1 augustss
205 1.1 augustss id = usbd_get_interface_descriptor(iface);
206 1.3 augustss
207 1.3 augustss sc->sc_udev = dev;
208 1.1 augustss sc->sc_iface = iface;
209 1.1 augustss
210 1.4 augustss uca.ibufsize = UVISORIBUFSIZE;
211 1.4 augustss uca.obufsize = UVISOROBUFSIZE;
212 1.5 augustss uca.ibufsizepad = UVISORIBUFSIZE;
213 1.8 augustss uca.opkthdrlen = 0;
214 1.4 augustss uca.device = dev;
215 1.4 augustss uca.iface = iface;
216 1.4 augustss uca.methods = &uvisor_methods;
217 1.4 augustss uca.arg = sc;
218 1.4 augustss
219 1.10 augustss err = uvisor_init(sc, &coninfo);
220 1.1 augustss if (err) {
221 1.1 augustss printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
222 1.1 augustss usbd_errstr(err));
223 1.1 augustss goto bad;
224 1.1 augustss }
225 1.1 augustss
226 1.9 augustss usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
227 1.9 augustss USBDEV(sc->sc_dev));
228 1.9 augustss
229 1.10 augustss sc->sc_numcon = UGETW(coninfo.num_ports);
230 1.10 augustss /*printf("%s: Number of ports: %d\n", USBDEVNAME(sc->sc_dev), np);*/
231 1.10 augustss for (i = 0; i < sc->sc_numcon; ++i) {
232 1.10 augustss switch (coninfo.connections[i].port_function_id) {
233 1.10 augustss case UVISOR_FUNCTION_GENERIC:
234 1.10 augustss string = "Generic";
235 1.10 augustss break;
236 1.10 augustss case UVISOR_FUNCTION_DEBUGGER:
237 1.10 augustss string = "Debugger";
238 1.10 augustss break;
239 1.10 augustss case UVISOR_FUNCTION_HOTSYNC:
240 1.10 augustss string = "HotSync";
241 1.10 augustss break;
242 1.10 augustss case UVISOR_FUNCTION_REMOTE_FILE_SYS:
243 1.10 augustss string = "Remote File System";
244 1.10 augustss break;
245 1.10 augustss default:
246 1.10 augustss string = "unknown";
247 1.10 augustss break;
248 1.10 augustss }
249 1.10 augustss port = coninfo.connections[i].port;
250 1.10 augustss printf("%s: portno %d is %s\n", USBDEVNAME(sc->sc_dev), port,
251 1.10 augustss string);
252 1.10 augustss
253 1.10 augustss uca.portno = port;
254 1.10 augustss uca.bulkin = port | UE_DIR_IN;
255 1.10 augustss uca.bulkout = port | UE_DIR_OUT;
256 1.10 augustss /* Verify that endpoints exist. */
257 1.10 augustss for (hasin = hasout = j = 0; j < id->bNumEndpoints; j++) {
258 1.10 augustss ed = usbd_interface2endpoint_descriptor(iface, j);
259 1.10 augustss if (ed == NULL)
260 1.10 augustss break;
261 1.10 augustss if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
262 1.10 augustss (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
263 1.10 augustss if (UE_GET_DIR(ed->bEndpointAddress)
264 1.10 augustss == UE_DIR_IN)
265 1.10 augustss hasin++;
266 1.10 augustss else
267 1.10 augustss hasout++;
268 1.10 augustss }
269 1.10 augustss }
270 1.10 augustss if (hasin == 1 && hasout == 1)
271 1.10 augustss sc->sc_subdevs[i] = config_found_sm(self, &uca,
272 1.10 augustss ucomprint, ucomsubmatch);
273 1.10 augustss else
274 1.10 augustss printf("%s: no proper endpoints for port %d (%d,%d)\n",
275 1.10 augustss USBDEVNAME(sc->sc_dev), port, hasin, hasout);
276 1.10 augustss }
277 1.10 augustss
278 1.1 augustss USB_ATTACH_SUCCESS_RETURN;
279 1.1 augustss
280 1.1 augustss bad:
281 1.1 augustss DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
282 1.1 augustss sc->sc_dying = 1;
283 1.1 augustss USB_ATTACH_ERROR_RETURN;
284 1.1 augustss }
285 1.1 augustss
286 1.1 augustss int
287 1.7 augustss uvisor_activate(device_ptr_t self, enum devact act)
288 1.1 augustss {
289 1.1 augustss struct uvisor_softc *sc = (struct uvisor_softc *)self;
290 1.1 augustss int rv = 0;
291 1.10 augustss int i;
292 1.1 augustss
293 1.1 augustss switch (act) {
294 1.1 augustss case DVACT_ACTIVATE:
295 1.1 augustss return (EOPNOTSUPP);
296 1.1 augustss break;
297 1.1 augustss
298 1.1 augustss case DVACT_DEACTIVATE:
299 1.10 augustss for (i = 0; i < sc->sc_numcon; i++)
300 1.10 augustss if (sc->sc_subdevs[i] != NULL)
301 1.10 augustss rv = config_deactivate(sc->sc_subdevs[i]);
302 1.1 augustss sc->sc_dying = 1;
303 1.1 augustss break;
304 1.1 augustss }
305 1.1 augustss return (rv);
306 1.1 augustss }
307 1.1 augustss
308 1.1 augustss int
309 1.7 augustss uvisor_detach(device_ptr_t self, int flags)
310 1.1 augustss {
311 1.1 augustss struct uvisor_softc *sc = (struct uvisor_softc *)self;
312 1.1 augustss int rv = 0;
313 1.10 augustss int i;
314 1.1 augustss
315 1.1 augustss DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
316 1.1 augustss sc->sc_dying = 1;
317 1.10 augustss for (i = 0; i < sc->sc_numcon; i++) {
318 1.10 augustss if (sc->sc_subdevs[i] != NULL) {
319 1.10 augustss rv |= config_detach(sc->sc_subdevs[i], flags);
320 1.10 augustss sc->sc_subdevs[i] = NULL;
321 1.10 augustss }
322 1.1 augustss }
323 1.9 augustss
324 1.9 augustss usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
325 1.9 augustss USBDEV(sc->sc_dev));
326 1.9 augustss
327 1.9 augustss
328 1.10 augustss return (rv);
329 1.1 augustss }
330 1.1 augustss
331 1.1 augustss usbd_status
332 1.10 augustss uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci)
333 1.1 augustss {
334 1.1 augustss usbd_status err;
335 1.1 augustss usb_device_request_t req;
336 1.1 augustss int actlen;
337 1.1 augustss uWord avail;
338 1.1 augustss
339 1.1 augustss DPRINTF(("uvisor_init: getting connection info\n"));
340 1.1 augustss req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
341 1.1 augustss req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
342 1.1 augustss USETW(req.wValue, 0);
343 1.1 augustss USETW(req.wIndex, 0);
344 1.1 augustss USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
345 1.10 augustss err = usbd_do_request_flags(sc->sc_udev, &req, ci,
346 1.1 augustss USBD_SHORT_XFER_OK, &actlen);
347 1.1 augustss if (err)
348 1.1 augustss return (err);
349 1.1 augustss
350 1.1 augustss DPRINTF(("uvisor_init: getting available bytes\n"));
351 1.1 augustss req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
352 1.1 augustss req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
353 1.1 augustss USETW(req.wValue, 0);
354 1.1 augustss USETW(req.wIndex, 5);
355 1.1 augustss USETW(req.wLength, sizeof avail);
356 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, &avail);
357 1.1 augustss if (err)
358 1.1 augustss return (err);
359 1.1 augustss DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
360 1.1 augustss
361 1.1 augustss DPRINTF(("uvisor_init: done\n"));
362 1.1 augustss return (err);
363 1.1 augustss }
364 1.1 augustss
365 1.1 augustss void
366 1.7 augustss uvisor_close(void *addr, int portno)
367 1.1 augustss {
368 1.1 augustss struct uvisor_softc *sc = addr;
369 1.1 augustss usb_device_request_t req;
370 1.1 augustss struct uvisor_connection_info coninfo; /* XXX ? */
371 1.1 augustss int actlen;
372 1.3 augustss
373 1.3 augustss if (sc->sc_dying)
374 1.3 augustss return;
375 1.1 augustss
376 1.1 augustss req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
377 1.1 augustss req.bRequest = UVISOR_CLOSE_NOTIFICATION;
378 1.1 augustss USETW(req.wValue, 0);
379 1.1 augustss USETW(req.wIndex, 0);
380 1.1 augustss USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
381 1.1 augustss (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
382 1.1 augustss USBD_SHORT_XFER_OK, &actlen);
383 1.1 augustss }
384