xref: /openbsd-src/sys/dev/usb/uipaq.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: uipaq.c,v 1.25 2015/03/14 03:38:50 jsg 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 
52 #include <dev/usb/usbcdc.h>	/*UCDC_* stuff */
53 
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usbdevs.h>
57 
58 #include <dev/usb/ucomvar.h>
59 
60 #ifdef UIPAQ_DEBUG
61 #define DPRINTF(x)	if (uipaqdebug) printf x
62 #define DPRINTFN(n,x)	if (uipaqdebug>(n)) printf x
63 int uipaqdebug = 0;
64 #else
65 #define DPRINTF(x)
66 #define DPRINTFN(n,x)
67 #endif
68 
69 #define UIPAQ_CONFIG_NO		1
70 #define UIPAQ_IFACE_INDEX	0
71 
72 #define UIPAQIBUFSIZE 1024
73 #define UIPAQOBUFSIZE 1024
74 
75 struct uipaq_softc {
76 	struct device		 sc_dev;	/* base device */
77 	struct usbd_device	*sc_udev;	/* device */
78 	struct usbd_interface	*sc_iface;	/* interface */
79 
80 	struct device		*sc_subdev;	/* ucom uses that */
81 	u_int16_t		 sc_lcr;	/* state for DTR/RTS */
82 
83 	u_int16_t		 sc_flags;
84 };
85 
86 /* Callback routines */
87 void	uipaq_set(void *, int, int, int);
88 
89 
90 /* Support routines. */
91 /* based on uppc module by Sam Lawrance */
92 void	uipaq_dtr(struct uipaq_softc *sc, int onoff);
93 void	uipaq_rts(struct uipaq_softc *sc, int onoff);
94 void	uipaq_break(struct uipaq_softc* sc, int onoff);
95 
96 
97 struct ucom_methods uipaq_methods = {
98 	NULL,
99 	uipaq_set,
100 	NULL,
101 	NULL,
102 	NULL,	/*open*/
103 	NULL,	/*close*/
104 	NULL,
105 	NULL
106 };
107 
108 struct uipaq_type {
109 	struct usb_devno	uv_dev;
110 	u_int16_t		uv_flags;
111 };
112 
113 static const struct uipaq_type uipaq_devs[] = {
114 	{{ USB_VENDOR_ASUS, USB_PRODUCT_ASUS_MYPAL_A730} , 0},
115 	{{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0},
116 	{{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0},
117 	{{ USB_VENDOR_HTC, USB_PRODUCT_HTC_SMARTPHONE }, 0},
118 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 },
119 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0},
120 	{{ USB_VENDOR_HTC, USB_PRODUCT_HTC_PPC6700MODEM }, 0}
121 };
122 
123 #define uipaq_lookup(v, p) ((struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
124 
125 int uipaq_match(struct device *, void *, void *);
126 void uipaq_attach(struct device *, struct device *, void *);
127 int uipaq_detach(struct device *, int);
128 
129 struct cfdriver uipaq_cd = {
130 	NULL, "uipaq", DV_DULL
131 };
132 
133 const struct cfattach uipaq_ca = {
134 	sizeof(struct uipaq_softc), uipaq_match, uipaq_attach, uipaq_detach
135 };
136 
137 int
138 uipaq_match(struct device *parent, void *match, void *aux)
139 {
140 	struct usb_attach_arg *uaa = aux;
141 
142 	if (uaa->iface != NULL)
143 		return (UMATCH_NONE);
144 
145 	DPRINTFN(20,("uipaq: vendor=0x%x, product=0x%x\n",
146 	    uaa->vendor, uaa->product));
147 
148 	return (uipaq_lookup(uaa->vendor, uaa->product) != NULL ?
149 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
150 }
151 
152 void
153 uipaq_attach(struct device *parent, struct device *self, void *aux)
154 {
155 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
156 	struct usb_attach_arg *uaa = aux;
157 	struct usbd_device *dev = uaa->device;
158 	struct usbd_interface *iface;
159 	usb_interface_descriptor_t *id;
160 	usb_endpoint_descriptor_t *ed;
161 	char *devname = sc->sc_dev.dv_xname;
162 	int i;
163 	usbd_status err;
164 	struct ucom_attach_args uca;
165 
166 	DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
167 
168 	/* Move the device into the configured state. */
169 	err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
170 	if (err) {
171 		printf(": failed to set configuration, err=%s\n",
172 		    usbd_errstr(err));
173 		goto bad;
174 	}
175 
176 	err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
177 	if (err) {
178 		printf(": failed to get interface, err=%s\n",
179 		    usbd_errstr(err));
180 		goto bad;
181 	}
182 
183 	sc->sc_flags = uipaq_lookup(uaa->vendor, uaa->product)->uv_flags;
184 
185 	id = usbd_get_interface_descriptor(iface);
186 
187 	sc->sc_udev = dev;
188 	sc->sc_iface = iface;
189 
190 	uca.ibufsize = UIPAQIBUFSIZE;
191 	uca.obufsize = UIPAQOBUFSIZE;
192 	uca.ibufsizepad = UIPAQIBUFSIZE;
193 	uca.opkthdrlen = 0;
194 	uca.device = dev;
195 	uca.iface = iface;
196 	uca.methods = &uipaq_methods;
197 	uca.arg = sc;
198 	uca.portno = UCOM_UNK_PORTNO;
199 	uca.info = "Generic";
200 
201 /*	err = uipaq_init(sc);
202 	if (err) {
203 		printf("%s: init failed, %s\n", sc->sc_dev.dv_xname,
204 		    usbd_errstr(err));
205 		goto bad;
206 	}*/
207 
208 	uca.bulkin = uca.bulkout = -1;
209 	for (i=0; i<id->bNumEndpoints; i++) {
210 		ed = usbd_interface2endpoint_descriptor(iface, i);
211 		if (ed == NULL) {
212 			printf("%s: no endpoint descriptor for %d\n",
213 					devname,i);
214 			goto bad;
215 		}
216 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
217 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
218 			uca.bulkin = ed->bEndpointAddress;
219 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
220 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
221 			uca.bulkout = ed->bEndpointAddress;
222 		}
223 	}
224 	if (uca.bulkin != -1 && uca.bulkout != -1)
225 		sc->sc_subdev = config_found_sm(self, &uca,
226 		    ucomprint, ucomsubmatch);
227 	else
228 		printf("%s: no proper endpoints found (%d,%d) \n",
229 		    devname, uca.bulkin, uca.bulkout);
230 
231 	return;
232 
233 bad:
234 	DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
235 	usbd_deactivate(sc->sc_udev);
236 }
237 
238 
239 void
240 uipaq_dtr(struct uipaq_softc* sc, int onoff)
241 {
242 	usb_device_request_t req;
243 	usbd_status err;
244 	int retries = 3;
245 
246 	DPRINTF(("%s: uipaq_dtr: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
247 
248 	/* Avoid sending unnecessary requests */
249 	if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
250 		return;
251 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
252 		return;
253 
254 	/* Other parameters depend on reg */
255 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
256 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
257 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
258 	USETW(req.wValue, sc->sc_lcr);
259 	USETW(req.wIndex, 0x0);
260 	USETW(req.wLength, 0);
261 
262 	/* Fire off the request a few times if necessary */
263 	while (retries) {
264 		err = usbd_do_request(sc->sc_udev, &req, NULL);
265 		if (!err)
266 			break;
267 		retries--;
268 	}
269 }
270 
271 
272 void
273 uipaq_rts(struct uipaq_softc* sc, int onoff)
274 {
275 	usb_device_request_t req;
276 	usbd_status err;
277 	int retries = 3;
278 
279 	DPRINTF(("%s: uipaq_rts: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
280 
281 	/* Avoid sending unnecessary requests */
282 	if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
283 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
284 
285 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
286 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
287 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
288 	USETW(req.wValue, sc->sc_lcr);
289 	USETW(req.wIndex, 0x0);
290 	USETW(req.wLength, 0);
291 
292 	while (retries) {
293 		err = usbd_do_request(sc->sc_udev, &req, NULL);
294 		if (!err)
295 			break;
296 		retries--;
297 	}
298 }
299 
300 
301 void
302 uipaq_break(struct uipaq_softc* sc, int onoff)
303 {
304 	usb_device_request_t req;
305 	usbd_status err;
306 	int retries = 3;
307 
308 	DPRINTF(("%s: uipaq_break: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
309 
310 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
311 	req.bRequest = UCDC_SEND_BREAK;
312 
313 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
314 	USETW(req.wIndex, 0x0);
315 	USETW(req.wLength, 0);
316 
317 	while (retries) {
318 		err = usbd_do_request(sc->sc_udev, &req, NULL);
319 		if (!err)
320 			break;
321 		retries--;
322 	}
323 }
324 
325 
326 void
327 uipaq_set(void *addr, int portno, int reg, int onoff)
328 {
329 	struct uipaq_softc* sc = addr;
330 
331 	switch (reg) {
332 	case UCOM_SET_DTR:
333 		uipaq_dtr(addr, onoff);
334 		break;
335 	case UCOM_SET_RTS:
336 		uipaq_rts(addr, onoff);
337 		break;
338 	case UCOM_SET_BREAK:
339 		uipaq_break(addr, onoff);
340 		break;
341 	default:
342 		printf("%s: unhandled set request: reg=%x onoff=%x\n",
343 		    sc->sc_dev.dv_xname, reg, onoff);
344 		return;
345 	}
346 }
347 
348 
349 int
350 uipaq_detach(struct device *self, int flags)
351 {
352 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
353 	int rv = 0;
354 
355 	DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
356 	if (sc->sc_subdev != NULL) {
357 		rv |= config_detach(sc->sc_subdev, flags);
358 		sc->sc_subdev = NULL;
359 	}
360 
361 	return (rv);
362 }
363 
364