xref: /netbsd-src/sys/dev/usb/uipaq.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: uipaq.c,v 1.7 2007/10/25 19:32:15 plunky Exp $	*/
2 /*	$OpenBSD: uipaq.c,v 1.1 2005/06/17 23:50:33 deraadt Exp $	*/
3 
4 /*
5  * Copyright (c) 2000-2005 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * iPAQ driver
43  *
44  * 19 July 2003:	Incorporated changes suggested by Sam Lawrance from
45  * 			the uppc module
46  *
47  *
48  * Contact isis@cs.umd.edu if you have any questions/comments about this driver
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/device.h>
55 #include <sys/conf.h>
56 #include <sys/tty.h>
57 
58 #include <dev/usb/usb.h>
59 
60 #include <dev/usb/usbcdc.h>	/*UCDC_* stuff */
61 
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdevs.h>
65 
66 #include <dev/usb/ucomvar.h>
67 
68 #ifdef UIPAQ_DEBUG
69 #define DPRINTF(x)	if (uipaqdebug) printf x
70 #define DPRINTFN(n,x)	if (uipaqdebug>(n)) printf x
71 int uipaqdebug = 0;
72 #else
73 #define DPRINTF(x)
74 #define DPRINTFN(n,x)
75 #endif
76 
77 #define UIPAQ_CONFIG_NO		1
78 #define UIPAQ_IFACE_INDEX	0
79 
80 #define UIPAQIBUFSIZE 1024
81 #define UIPAQOBUFSIZE 1024
82 
83 struct uipaq_softc {
84 	USBBASEDEVICE		sc_dev;		/* base device */
85 	usbd_device_handle	sc_udev;	/* device */
86 	usbd_interface_handle	sc_iface;	/* interface */
87 
88 	device_ptr_t		sc_subdev;	/* ucom uses that */
89 	u_int16_t		sc_lcr;		/* state for DTR/RTS */
90 
91 	u_int16_t		sc_flags;
92 
93 	u_char			sc_dying;
94 };
95 
96 /* Callback routines */
97 Static void	uipaq_set(void *, int, int, int);
98 
99 
100 /* Support routines. */
101 /* based on uppc module by Sam Lawrance */
102 Static void	uipaq_dtr(struct uipaq_softc *sc, int onoff);
103 Static void	uipaq_rts(struct uipaq_softc *sc, int onoff);
104 Static void	uipaq_break(struct uipaq_softc* sc, int onoff);
105 
106 
107 struct ucom_methods uipaq_methods = {
108 	NULL,
109 	uipaq_set,
110 	NULL,
111 	NULL,
112 	NULL,	/*open*/
113 	NULL,	/*close*/
114 	NULL,
115 	NULL
116 };
117 
118 struct uipaq_type {
119 	struct usb_devno	uv_dev;
120 	u_int16_t		uv_flags;
121 };
122 
123 static const struct uipaq_type uipaq_devs[] = {
124 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 },
125 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0},
126 	{{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0},
127 	{{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0},
128 	{{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS007SH} , 0}
129 };
130 
131 #define uipaq_lookup(v, p) ((const struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
132 
133 USB_DECLARE_DRIVER(uipaq);
134 
135 USB_MATCH(uipaq)
136 {
137 	USB_MATCH_START(uipaq, uaa);
138 
139 	DPRINTFN(20,("uipaq: vendor=0x%x, product=0x%x\n",
140 	    uaa->vendor, uaa->product));
141 
142 	return (uipaq_lookup(uaa->vendor, uaa->product) != NULL ?
143 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
144 }
145 
146 USB_ATTACH(uipaq)
147 {
148 	USB_ATTACH_START(uipaq, sc, uaa);
149 	usbd_device_handle dev = uaa->device;
150 	usbd_interface_handle iface;
151 	usb_interface_descriptor_t *id;
152 	usb_endpoint_descriptor_t *ed;
153 	char *devinfop;
154 	char *devname = USBDEVNAME(sc->sc_dev);
155 	int i;
156 	usbd_status err;
157 	struct ucom_attach_args uca;
158 
159 	DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
160 
161 	/* Move the device into the configured state. */
162 	err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
163 	if (err) {
164 		printf("\n%s: failed to set configuration, err=%s\n",
165 		    devname, usbd_errstr(err));
166 		goto bad;
167 	}
168 
169 	err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
170 	if (err) {
171 		printf("\n%s: failed to get interface, err=%s\n",
172 		    devname, usbd_errstr(err));
173 		goto bad;
174 	}
175 
176 	devinfop = usbd_devinfo_alloc(dev, 0);
177 	USB_ATTACH_SETUP;
178 	printf("%s: %s\n", devname, devinfop);
179 	usbd_devinfo_free(devinfop);
180 
181 	sc->sc_flags = uipaq_lookup(uaa->vendor, uaa->product)->uv_flags;
182 
183 	id = usbd_get_interface_descriptor(iface);
184 
185 	sc->sc_udev = dev;
186 	sc->sc_iface = iface;
187 
188 	uca.ibufsize = UIPAQIBUFSIZE;
189 	uca.obufsize = UIPAQOBUFSIZE;
190 	uca.ibufsizepad = UIPAQIBUFSIZE;
191 	uca.opkthdrlen = 0;
192 	uca.device = dev;
193 	uca.iface = iface;
194 	uca.methods = &uipaq_methods;
195 	uca.arg = sc;
196 	uca.portno = UCOM_UNK_PORTNO;
197 	uca.info = "Generic";
198 
199 /*	err = uipaq_init(sc);
200 	if (err) {
201 		printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
202 		    usbd_errstr(err));
203 		goto bad;
204 	}*/
205 
206 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
207 	    USBDEV(sc->sc_dev));
208 
209 	uca.bulkin = uca.bulkout = -1;
210 	for (i=0; i<id->bNumEndpoints; i++) {
211 		ed = usbd_interface2endpoint_descriptor(iface, i);
212 		if (ed == NULL) {
213 			printf("%s: no endpoint descriptor for %d\n",
214 					devname,i);
215 			goto bad;
216 		}
217 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
218 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
219 			uca.bulkin = ed->bEndpointAddress;
220 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
221 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
222 			uca.bulkout = ed->bEndpointAddress;
223 		}
224 	}
225 	if (uca.bulkin == -1 || uca.bulkout == -1) {
226 		printf("%s: no proper endpoints found (%d,%d) \n",
227 		    devname, uca.bulkin, uca.bulkout);
228 		USB_ATTACH_ERROR_RETURN;
229 	}
230 
231 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
232 					    ucomprint, ucomsubmatch);
233 
234 	USB_ATTACH_SUCCESS_RETURN;
235 
236 bad:
237 	DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
238 	sc->sc_dying = 1;
239 	USB_ATTACH_ERROR_RETURN;
240 }
241 
242 
243 void
244 uipaq_dtr(struct uipaq_softc* sc, int onoff)
245 {
246 	usb_device_request_t req;
247 	usbd_status err;
248 	int retries = 3;
249 
250 	DPRINTF(("%s: uipaq_dtr: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
251 
252 	/* Avoid sending unnecessary requests */
253 	if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
254 		return;
255 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
256 		return;
257 
258 	/* Other parameters depend on reg */
259 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
260 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
261 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
262 	USETW(req.wValue, sc->sc_lcr);
263 	USETW(req.wIndex, 0x0);
264 	USETW(req.wLength, 0);
265 
266 	/* Fire off the request a few times if necessary */
267 	while (retries) {
268 		err = usbd_do_request(sc->sc_udev, &req, NULL);
269 		if (!err)
270 			break;
271 		retries--;
272 	}
273 }
274 
275 
276 void
277 uipaq_rts(struct uipaq_softc* sc, int onoff)
278 {
279 	usb_device_request_t req;
280 	usbd_status err;
281 	int retries = 3;
282 
283 	DPRINTF(("%s: uipaq_rts: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
284 
285 	/* Avoid sending unnecessary requests */
286 	if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
287 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
288 
289 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
290 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
291 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
292 	USETW(req.wValue, sc->sc_lcr);
293 	USETW(req.wIndex, 0x0);
294 	USETW(req.wLength, 0);
295 
296 	while (retries) {
297 		err = usbd_do_request(sc->sc_udev, &req, NULL);
298 		if (!err)
299 			break;
300 		retries--;
301 	}
302 }
303 
304 
305 void
306 uipaq_break(struct uipaq_softc* sc, int onoff)
307 {
308 	usb_device_request_t req;
309 	usbd_status err;
310 	int retries = 3;
311 
312 	DPRINTF(("%s: uipaq_break: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
313 
314 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
315 	req.bRequest = UCDC_SEND_BREAK;
316 
317 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
318 	USETW(req.wIndex, 0x0);
319 	USETW(req.wLength, 0);
320 
321 	while (retries) {
322 		err = usbd_do_request(sc->sc_udev, &req, NULL);
323 		if (!err)
324 			break;
325 		retries--;
326 	}
327 }
328 
329 
330 void
331 uipaq_set(void *addr, int portno, int reg, int onoff)
332 {
333 	struct uipaq_softc* sc = addr;
334 
335 	switch (reg) {
336 	case UCOM_SET_DTR:
337 		uipaq_dtr(addr, onoff);
338 		break;
339 	case UCOM_SET_RTS:
340 		uipaq_rts(addr, onoff);
341 		break;
342 	case UCOM_SET_BREAK:
343 		uipaq_break(addr, onoff);
344 		break;
345 	default:
346 		printf("%s: unhandled set request: reg=%x onoff=%x\n",
347 		    USBDEVNAME(sc->sc_dev), reg, onoff);
348 		return;
349 	}
350 }
351 
352 
353 int
354 uipaq_activate(device_ptr_t self, enum devact act)
355 {
356 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
357 	int rv = 0;
358 
359 	switch (act) {
360 	case DVACT_ACTIVATE:
361 		return (EOPNOTSUPP);
362 		break;
363 
364 	case DVACT_DEACTIVATE:
365 		if (sc->sc_subdev != NULL)
366 			rv = config_deactivate(sc->sc_subdev);
367 		sc->sc_dying = 1;
368 		break;
369 	}
370 	return (rv);
371 }
372 
373 int
374 uipaq_detach(device_ptr_t self, int flags)
375 {
376 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
377 	int rv = 0;
378 
379 	DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
380 	sc->sc_dying = 1;
381 	if (sc->sc_subdev != NULL) {
382 		rv |= config_detach(sc->sc_subdev, flags);
383 		sc->sc_subdev = NULL;
384 	}
385 
386 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
387 	    USBDEV(sc->sc_dev));
388 
389 	return (rv);
390 }
391 
392