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