Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / usbmisc / uvisor / uvisor.c
1 /*      $NetBSD: uvisor.c,v 1.9 2001/01/23 14:04:14 augustss Exp $      */
2 /*      $FreeBSD: src/sys/dev/usb/uvisor.c,v 1.7.2.3 2002/08/27 13:46:28 joe Exp $      */
3
4 /* This version of uvisor is heavily based upon the version in NetBSD
5  * but is missing the following patches:
6  *
7  * 1.10 needed?         connect a ucom to each of the uvisor ports
8  * 1.11 needed          ucom has an "info" attach message - use it
9  * 1.12 not needed      rcsids
10  * 1.13 already merged  extra arg to usbd_do_request_flags
11  * 1.14 already merged  sony and palm support
12  * 1.15 already merged  sony clie
13  * 1.16 already merged  trailing whites
14  */
15
16 /*
17  * Copyright (c) 2000 The NetBSD Foundation, Inc.
18  * All rights reserved.
19  *
20  * This code is derived from software contributed to The NetBSD Foundation
21  * by Lennart Augustsson (lennart@augustsson.net) at
22  * Carlstedt Research & Technology.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  * 2. Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in the
31  *    documentation and/or other materials provided with the distribution.
32  * 3. All advertising materials mentioning features or use of this software
33  *    must display the following acknowledgement:
34  *        This product includes software developed by the NetBSD
35  *        Foundation, Inc. and its contributors.
36  * 4. Neither the name of The NetBSD Foundation nor the names of its
37  *    contributors may be used to endorse or promote products derived
38  *    from this software without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
41  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50  * POSSIBILITY OF SUCH DAMAGE.
51  */
52
53 /*
54  * Handspring Visor (Palmpilot compatible PDA) driver
55  */
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #if defined(__NetBSD__) || defined(__OpenBSD__)
61 #include <sys/device.h>
62 #elif defined(__FreeBSD__)
63 #include <sys/bus.h>
64 #endif
65 #include <sys/conf.h>
66 #include <sys/tty.h>
67 #include <sys/sysctl.h>
68
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbhid.h>
71
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include <dev/usb/usbdevs.h>
75
76 #include <dev/usb/ucomvar.h>
77
78 #ifdef USB_DEBUG
79 #define DPRINTF(x)      if (uvisordebug) printf x
80 #define DPRINTFN(n,x)   if (uvisordebug>(n)) printf x
81 int uvisordebug = 0;
82 SYSCTL_NODE(_hw_usb, OID_AUTO, uvisor, CTLFLAG_RW, 0, "USB uvisor");
83 SYSCTL_INT(_hw_usb_uvisor, OID_AUTO, debug, CTLFLAG_RW,
84            &uvisordebug, 0, "uvisor debug level");
85 #else
86 #define DPRINTF(x)
87 #define DPRINTFN(n,x)
88 #endif
89
90 #define UVISOR_CONFIG_INDEX     0
91 #define UVISOR_IFACE_INDEX      0
92 #define UVISOR_MODVER           1
93
94 /* From the Linux driver */
95 /*
96  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
97  * are available to be transfered to the host for the specified endpoint.
98  * Currently this is not used, and always returns 0x0001
99  */
100 #define UVISOR_REQUEST_BYTES_AVAILABLE          0x01
101
102 /*
103  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
104  * is now closing the pipe. An empty packet is sent in response.
105  */
106 #define UVISOR_CLOSE_NOTIFICATION               0x02
107
108 /*
109  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
110  * get the endpoints used by the connection.
111  */
112 #define UVISOR_GET_CONNECTION_INFORMATION       0x03
113
114
115 /*
116  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
117  */
118 #define UVISOR_MAX_CONN 8
119 struct uvisor_connection_info {
120         uWord   num_ports;
121         struct {
122                 uByte   port_function_id;
123                 uByte   port;
124         } connections[UVISOR_MAX_CONN];
125 };
126 #define UVISOR_CONNECTION_INFO_SIZE 18
127
128 /* struct uvisor_connection_info.connection[x].port defines: */
129 #define UVISOR_ENDPOINT_1               0x01
130 #define UVISOR_ENDPOINT_2               0x02
131
132 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
133 #define UVISOR_FUNCTION_GENERIC         0x00
134 #define UVISOR_FUNCTION_DEBUGGER        0x01
135 #define UVISOR_FUNCTION_HOTSYNC         0x02
136 #define UVISOR_FUNCTION_CONSOLE         0x03
137 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
138
139 /*
140  * Unknown PalmOS stuff.
141  */
142 #define UVISOR_GET_PALM_INFORMATION             0x04
143 #define UVISOR_GET_PALM_INFORMATION_LEN         0x14
144
145
146 #define UVISORIBUFSIZE 1024
147 #define UVISOROBUFSIZE 1024
148
149 struct uvisor_softc {
150         struct ucom_softc       sc_ucom;
151         u_int16_t               sc_flags;
152 };
153
154 Static usbd_status uvisor_init(struct uvisor_softc *);
155
156 Static void uvisor_close(void *, int);
157
158 struct ucom_callback uvisor_callback = {
159         NULL,
160         NULL,
161         NULL,
162         NULL,
163         NULL,
164         uvisor_close,
165         NULL,
166         NULL,
167 };
168
169 Static device_probe_t uvisor_match;
170 Static device_attach_t uvisor_attach;
171 Static device_detach_t uvisor_detach;
172 Static device_method_t uvisor_methods[] = {
173        /* Device interface */
174        DEVMETHOD(device_probe, uvisor_match),
175        DEVMETHOD(device_attach, uvisor_attach),
176        DEVMETHOD(device_detach, uvisor_detach),
177        { 0, 0 }
178  };
179
180
181 Static driver_t uvisor_driver = {
182        "ucom",
183        uvisor_methods,
184        sizeof (struct uvisor_softc)
185 };
186
187 DRIVER_MODULE(uvisor, uhub, uvisor_driver, ucom_devclass, usbd_driver_load, 0);
188 MODULE_DEPEND(uvisor, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
189 MODULE_VERSION(uvisor, UVISOR_MODVER);
190
191 struct uvisor_type {
192         struct usb_devno        uv_dev;
193         u_int16_t               uv_flags;
194 #define PALM4   0x0001
195 };
196 static const struct uvisor_type uvisor_devs[] = {
197         {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, 0 },
198         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
199         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
200         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
201         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
202         {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
203         {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, 0 },
204 /*      {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
205 };
206 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
207
208
209 USB_MATCH(uvisor)
210 {
211         USB_MATCH_START(uvisor, uaa);
212         
213         if (uaa->iface != NULL)
214                 return (UMATCH_NONE);
215
216         DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
217                      uaa->vendor, uaa->product));
218
219         return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
220                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
221 }
222
223 USB_ATTACH(uvisor)
224 {
225         USB_ATTACH_START(uvisor, sc, uaa);
226         usbd_device_handle dev = uaa->device;
227         usbd_interface_handle iface;
228         usb_interface_descriptor_t *id;
229         usb_endpoint_descriptor_t *ed;
230         char *devinfo;
231         const char *devname;
232         int i;
233         usbd_status err;
234         struct ucom_softc *ucom;
235
236         devinfo = malloc(1024, M_USBDEV, M_WAITOK);
237         ucom = &sc->sc_ucom;
238
239         bzero(sc, sizeof (struct uvisor_softc));
240         usbd_devinfo(dev, 0, devinfo);
241
242         ucom->sc_dev = self;
243         device_set_desc_copy(self, devinfo);
244
245         ucom->sc_udev = dev;
246         ucom->sc_iface = uaa->iface;
247
248         devname = USBDEVNAME(ucom->sc_dev);
249         printf("%s: %s\n", devname, devinfo);
250
251         DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
252
253         /* Move the device into the configured state. */
254         err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
255         if (err) {
256                 printf("\n%s: failed to set configuration, err=%s\n",
257                        devname, usbd_errstr(err));
258                 goto bad;
259         }
260
261         err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
262         if (err) {
263                 printf("\n%s: failed to get interface, err=%s\n",
264                        devname, usbd_errstr(err));
265                 goto bad;
266         }
267
268         printf("%s: %s\n", devname, devinfo);
269
270         sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
271
272         id = usbd_get_interface_descriptor(iface);
273
274         ucom->sc_udev = dev;
275         ucom->sc_iface = iface;
276
277         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
278         for (i = 0; i < id->bNumEndpoints; i++) {
279                 int addr, dir, attr;
280                 ed = usbd_interface2endpoint_descriptor(iface, i);
281                 if (ed == NULL) {
282                         printf("%s: could not read endpoint descriptor"
283                                ": %s\n", devname, usbd_errstr(err));
284                         goto bad;
285                 }
286                 
287                 addr = ed->bEndpointAddress;
288                 dir = UE_GET_DIR(ed->bEndpointAddress);
289                 attr = ed->bmAttributes & UE_XFERTYPE;
290                 if (dir == UE_DIR_IN && attr == UE_BULK)
291                         ucom->sc_bulkin_no = addr;
292                 else if (dir == UE_DIR_OUT && attr == UE_BULK)
293                         ucom->sc_bulkout_no = addr;
294                 else {
295                         printf("%s: unexpected endpoint\n", devname);
296                         goto bad;
297                 }
298         }
299         if (ucom->sc_bulkin_no == -1) {
300                 printf("%s: Could not find data bulk in\n",
301                        USBDEVNAME(ucom->sc_dev));
302                 goto bad;
303         }
304         if (ucom->sc_bulkout_no == -1) {
305                 printf("%s: Could not find data bulk out\n",
306                        USBDEVNAME(ucom->sc_dev));
307                 goto bad;
308         }
309         
310         ucom->sc_parent = sc;
311         ucom->sc_portno = UCOM_UNK_PORTNO;
312         /* bulkin, bulkout set above */
313         ucom->sc_ibufsize = UVISORIBUFSIZE;
314         ucom->sc_obufsize = UVISOROBUFSIZE;
315         ucom->sc_ibufsizepad = UVISORIBUFSIZE;
316         ucom->sc_opkthdrlen = 0;
317         ucom->sc_callback = &uvisor_callback;
318
319         err = uvisor_init(sc);
320         if (err) {
321                 printf("%s: init failed, %s\n", USBDEVNAME(ucom->sc_dev),
322                        usbd_errstr(err));
323                 goto bad;
324         }
325
326         DPRINTF(("uvisor: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
327         ucom_attach(&sc->sc_ucom);
328
329         USB_ATTACH_SUCCESS_RETURN;
330
331 bad:
332         DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
333         ucom->sc_dying = 1;
334         USB_ATTACH_ERROR_RETURN;
335 }
336
337 #if 0
338
339 int
340 uvisor_activate(device_ptr_t self, enum devact act)
341 {
342         struct uvisor_softc *sc = (struct uvisor_softc *)self;
343         int rv = 0;
344
345         switch (act) {
346         case DVACT_ACTIVATE:
347                 return (EOPNOTSUPP);
348                 break;
349
350         case DVACT_DEACTIVATE:
351                 if (sc->sc_subdev != NULL)
352                         rv = config_deactivate(sc->sc_subdev);
353                 sc->sc_dying = 1;
354                 break;
355         }
356         return (rv);
357 }
358
359 #endif
360
361 USB_DETACH(uvisor)
362 {
363         USB_DETACH_START(uvisor, sc);
364         int rv = 0;
365
366         DPRINTF(("uvisor_detach: sc=%p\n", sc));
367         sc->sc_ucom.sc_dying = 1;
368         rv = ucom_detach(&sc->sc_ucom);
369
370         return (rv);
371 }
372
373 usbd_status
374 uvisor_init(struct uvisor_softc *sc)
375 {
376         usbd_status err;
377         usb_device_request_t req;
378         struct uvisor_connection_info coninfo;
379         int actlen;
380         uWord avail;
381         char buffer[256];
382
383         DPRINTF(("uvisor_init: getting connection info\n"));
384         req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
385         req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
386         USETW(req.wValue, 0);
387         USETW(req.wIndex, 0);
388         USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
389         err = usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
390                                     USBD_SHORT_XFER_OK, &actlen);
391         if (err)
392                 return (err);
393
394 #ifdef USB_DEBUG
395         {
396                 int i, np;
397                 char *string;
398
399                 np = UGETW(coninfo.num_ports);
400                 printf("%s: Number of ports: %d\n", USBDEVNAME(sc->sc_ucom.sc_dev), np);
401                 for (i = 0; i < np; ++i) {
402                         switch (coninfo.connections[i].port_function_id) {
403                         case UVISOR_FUNCTION_GENERIC:
404                                 string = "Generic";
405                                 break;
406                         case UVISOR_FUNCTION_DEBUGGER:
407                                 string = "Debugger";
408                                 break;
409                         case UVISOR_FUNCTION_HOTSYNC:
410                                 string = "HotSync";
411                                 break;
412                         case UVISOR_FUNCTION_REMOTE_FILE_SYS:
413                                 string = "Remote File System";
414                                 break;
415                         default:
416                                 string = "unknown";
417                                 break;  
418                         }
419                         printf("%s: port %d, is for %s\n",
420                             USBDEVNAME(sc->sc_ucom.sc_dev), coninfo.connections[i].port,
421                             string);
422                 }
423         }
424 #endif
425
426         if (sc->sc_flags & PALM4) {
427                 /* Palm OS 4.0 Hack */
428                 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
429                 req.bRequest = UVISOR_GET_PALM_INFORMATION;
430                 USETW(req.wValue, 0);
431                 USETW(req.wIndex, 0);
432                 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
433                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
434                 if (err)
435                         return (err);
436                 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
437                 req.bRequest = UVISOR_GET_PALM_INFORMATION;
438                 USETW(req.wValue, 0);
439                 USETW(req.wIndex, 0);
440                 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
441                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
442                 if (err)
443                         return (err);
444         }
445
446         DPRINTF(("uvisor_init: getting available bytes\n"));
447         req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
448         req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
449         USETW(req.wValue, 0);
450         USETW(req.wIndex, 5);
451         USETW(req.wLength, sizeof avail);
452         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, &avail);
453         if (err)
454                 return (err);
455         DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
456
457         DPRINTF(("uvisor_init: done\n"));
458         return (err);
459 }
460
461 void
462 uvisor_close(void *addr, int portno)
463 {
464         struct uvisor_softc *sc = addr;
465         usb_device_request_t req;
466         struct uvisor_connection_info coninfo; /* XXX ? */
467         int actlen;
468
469         if (sc->sc_ucom.sc_dying)
470                 return;
471
472         req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
473         req.bRequest = UVISOR_CLOSE_NOTIFICATION;
474         USETW(req.wValue, 0);
475         USETW(req.wIndex, 0);
476         USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
477         (void)usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
478                                     USBD_SHORT_XFER_OK, &actlen);
479 }