xref: /openbsd/sys/dev/usb/uipaq.c (revision 274d7c50)
1 /*	$OpenBSD: uipaq.c,v 1.26 2016/11/06 12:58:01 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 
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 || uaa->configno != UIPAQ_CONFIG_NO)
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 	err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
169 	if (err) {
170 		printf(": failed to get interface, err=%s\n",
171 		    usbd_errstr(err));
172 		goto bad;
173 	}
174 
175 	sc->sc_flags = uipaq_lookup(uaa->vendor, uaa->product)->uv_flags;
176 
177 	id = usbd_get_interface_descriptor(iface);
178 
179 	sc->sc_udev = dev;
180 	sc->sc_iface = iface;
181 
182 	uca.ibufsize = UIPAQIBUFSIZE;
183 	uca.obufsize = UIPAQOBUFSIZE;
184 	uca.ibufsizepad = UIPAQIBUFSIZE;
185 	uca.opkthdrlen = 0;
186 	uca.device = dev;
187 	uca.iface = iface;
188 	uca.methods = &uipaq_methods;
189 	uca.arg = sc;
190 	uca.portno = UCOM_UNK_PORTNO;
191 	uca.info = "Generic";
192 
193 /*	err = uipaq_init(sc);
194 	if (err) {
195 		printf("%s: init failed, %s\n", sc->sc_dev.dv_xname,
196 		    usbd_errstr(err));
197 		goto bad;
198 	}*/
199 
200 	uca.bulkin = uca.bulkout = -1;
201 	for (i=0; i<id->bNumEndpoints; i++) {
202 		ed = usbd_interface2endpoint_descriptor(iface, i);
203 		if (ed == NULL) {
204 			printf("%s: no endpoint descriptor for %d\n",
205 					devname,i);
206 			goto bad;
207 		}
208 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
209 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
210 			uca.bulkin = ed->bEndpointAddress;
211 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
212 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
213 			uca.bulkout = ed->bEndpointAddress;
214 		}
215 	}
216 	if (uca.bulkin != -1 && uca.bulkout != -1)
217 		sc->sc_subdev = config_found_sm(self, &uca,
218 		    ucomprint, ucomsubmatch);
219 	else
220 		printf("%s: no proper endpoints found (%d,%d) \n",
221 		    devname, uca.bulkin, uca.bulkout);
222 
223 	return;
224 
225 bad:
226 	DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
227 	usbd_deactivate(sc->sc_udev);
228 }
229 
230 
231 void
232 uipaq_dtr(struct uipaq_softc* sc, int onoff)
233 {
234 	usb_device_request_t req;
235 	usbd_status err;
236 	int retries = 3;
237 
238 	DPRINTF(("%s: uipaq_dtr: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
239 
240 	/* Avoid sending unnecessary requests */
241 	if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
242 		return;
243 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
244 		return;
245 
246 	/* Other parameters depend on reg */
247 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
248 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
249 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
250 	USETW(req.wValue, sc->sc_lcr);
251 	USETW(req.wIndex, 0x0);
252 	USETW(req.wLength, 0);
253 
254 	/* Fire off the request a few times if necessary */
255 	while (retries) {
256 		err = usbd_do_request(sc->sc_udev, &req, NULL);
257 		if (!err)
258 			break;
259 		retries--;
260 	}
261 }
262 
263 
264 void
265 uipaq_rts(struct uipaq_softc* sc, int onoff)
266 {
267 	usb_device_request_t req;
268 	usbd_status err;
269 	int retries = 3;
270 
271 	DPRINTF(("%s: uipaq_rts: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
272 
273 	/* Avoid sending unnecessary requests */
274 	if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
275 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
276 
277 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
278 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
279 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
280 	USETW(req.wValue, sc->sc_lcr);
281 	USETW(req.wIndex, 0x0);
282 	USETW(req.wLength, 0);
283 
284 	while (retries) {
285 		err = usbd_do_request(sc->sc_udev, &req, NULL);
286 		if (!err)
287 			break;
288 		retries--;
289 	}
290 }
291 
292 
293 void
294 uipaq_break(struct uipaq_softc* sc, int onoff)
295 {
296 	usb_device_request_t req;
297 	usbd_status err;
298 	int retries = 3;
299 
300 	DPRINTF(("%s: uipaq_break: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
301 
302 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
303 	req.bRequest = UCDC_SEND_BREAK;
304 
305 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
306 	USETW(req.wIndex, 0x0);
307 	USETW(req.wLength, 0);
308 
309 	while (retries) {
310 		err = usbd_do_request(sc->sc_udev, &req, NULL);
311 		if (!err)
312 			break;
313 		retries--;
314 	}
315 }
316 
317 
318 void
319 uipaq_set(void *addr, int portno, int reg, int onoff)
320 {
321 	struct uipaq_softc* sc = addr;
322 
323 	switch (reg) {
324 	case UCOM_SET_DTR:
325 		uipaq_dtr(addr, onoff);
326 		break;
327 	case UCOM_SET_RTS:
328 		uipaq_rts(addr, onoff);
329 		break;
330 	case UCOM_SET_BREAK:
331 		uipaq_break(addr, onoff);
332 		break;
333 	default:
334 		printf("%s: unhandled set request: reg=%x onoff=%x\n",
335 		    sc->sc_dev.dv_xname, reg, onoff);
336 		return;
337 	}
338 }
339 
340 
341 int
342 uipaq_detach(struct device *self, int flags)
343 {
344 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
345 	int rv = 0;
346 
347 	DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
348 	if (sc->sc_subdev != NULL) {
349 		rv |= config_detach(sc->sc_subdev, flags);
350 		sc->sc_subdev = NULL;
351 	}
352 
353 	return (rv);
354 }
355 
356