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