xref: /openbsd-src/sys/dev/usb/uipaq.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: uipaq.c,v 1.20 2011/07/03 15:47:17 matthew 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 *, int);
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 	uca.bulkin = uca.bulkout = -1;
217 	for (i=0; i<id->bNumEndpoints; i++) {
218 		ed = usbd_interface2endpoint_descriptor(iface, i);
219 		if (ed == NULL) {
220 			printf("%s: no endpoint descriptor for %d\n",
221 					devname,i);
222 			goto bad;
223 		}
224 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
225 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
226 			uca.bulkin = ed->bEndpointAddress;
227 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
228 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
229 			uca.bulkout = ed->bEndpointAddress;
230 		}
231 	}
232 	if (uca.bulkin != -1 && uca.bulkout != -1)
233 		sc->sc_subdev = config_found_sm(self, &uca,
234 		    ucomprint, ucomsubmatch);
235 	else
236 		printf("%s: no proper endpoints found (%d,%d) \n",
237 		    devname, uca.bulkin, uca.bulkout);
238 
239 	return;
240 
241 bad:
242 	DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
243 	sc->sc_dying = 1;
244 }
245 
246 
247 void
248 uipaq_dtr(struct uipaq_softc* sc, int onoff)
249 {
250 	usb_device_request_t req;
251 	usbd_status err;
252 	int retries = 3;
253 
254 	DPRINTF(("%s: uipaq_dtr: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
255 
256 	/* Avoid sending unnecessary requests */
257 	if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
258 		return;
259 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
260 		return;
261 
262 	/* Other parameters depend on reg */
263 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
264 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
265 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
266 	USETW(req.wValue, sc->sc_lcr);
267 	USETW(req.wIndex, 0x0);
268 	USETW(req.wLength, 0);
269 
270 	/* Fire off the request a few times if necessary */
271 	while (retries) {
272 		err = usbd_do_request(sc->sc_udev, &req, NULL);
273 		if (!err)
274 			break;
275 		retries--;
276 	}
277 }
278 
279 
280 void
281 uipaq_rts(struct uipaq_softc* sc, int onoff)
282 {
283 	usb_device_request_t req;
284 	usbd_status err;
285 	int retries = 3;
286 
287 	DPRINTF(("%s: uipaq_rts: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
288 
289 	/* Avoid sending unnecessary requests */
290 	if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
291 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
292 
293 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
294 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
295 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
296 	USETW(req.wValue, sc->sc_lcr);
297 	USETW(req.wIndex, 0x0);
298 	USETW(req.wLength, 0);
299 
300 	while (retries) {
301 		err = usbd_do_request(sc->sc_udev, &req, NULL);
302 		if (!err)
303 			break;
304 		retries--;
305 	}
306 }
307 
308 
309 void
310 uipaq_break(struct uipaq_softc* sc, int onoff)
311 {
312 	usb_device_request_t req;
313 	usbd_status err;
314 	int retries = 3;
315 
316 	DPRINTF(("%s: uipaq_break: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
317 
318 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
319 	req.bRequest = UCDC_SEND_BREAK;
320 
321 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
322 	USETW(req.wIndex, 0x0);
323 	USETW(req.wLength, 0);
324 
325 	while (retries) {
326 		err = usbd_do_request(sc->sc_udev, &req, NULL);
327 		if (!err)
328 			break;
329 		retries--;
330 	}
331 }
332 
333 
334 void
335 uipaq_set(void *addr, int portno, int reg, int onoff)
336 {
337 	struct uipaq_softc* sc = addr;
338 
339 	switch (reg) {
340 	case UCOM_SET_DTR:
341 		uipaq_dtr(addr, onoff);
342 		break;
343 	case UCOM_SET_RTS:
344 		uipaq_rts(addr, onoff);
345 		break;
346 	case UCOM_SET_BREAK:
347 		uipaq_break(addr, onoff);
348 		break;
349 	default:
350 		printf("%s: unhandled set request: reg=%x onoff=%x\n",
351 		    sc->sc_dev.dv_xname, reg, onoff);
352 		return;
353 	}
354 }
355 
356 
357 int
358 uipaq_activate(struct device *self, int act)
359 {
360 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
361 	int rv = 0;
362 
363 	switch (act) {
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(struct device *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 	if (sc->sc_subdev != NULL) {
381 		rv |= config_detach(sc->sc_subdev, flags);
382 		sc->sc_subdev = NULL;
383 	}
384 
385 	return (rv);
386 }
387 
388