xref: /netbsd-src/sys/dev/usb/uipaq.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: uipaq.c,v 1.30 2021/08/07 16:19:17 thorpej Exp $	*/
2 /*	$OpenBSD: uipaq.c,v 1.1 2005/06/17 23:50:33 deraadt Exp $	*/
3 
4 /*
5  * Copyright (c) 2000-2005 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * iPAQ driver
36  *
37  * 19 July 2003:	Incorporated changes suggested by Sam Lawrance from
38  * 			the uppc module
39  *
40  *
41  * Contact isis@cs.umd.edu if you have any questions/comments about this driver
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: uipaq.c,v 1.30 2021/08/07 16:19:17 thorpej Exp $");
46 
47 #ifdef _KERNEL_OPT
48 #include "opt_usb.h"
49 #endif
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/device.h>
55 #include <sys/conf.h>
56 #include <sys/tty.h>
57 
58 #include <dev/usb/usb.h>
59 
60 #include <dev/usb/usbcdc.h>	/*UCDC_* stuff */
61 
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdevs.h>
65 
66 #include <dev/usb/ucomvar.h>
67 
68 #ifdef UIPAQ_DEBUG
69 #define DPRINTF(x)	if (uipaqdebug) printf x
70 #define DPRINTFN(n,x)	if (uipaqdebug>(n)) printf x
71 int uipaqdebug = 0;
72 #else
73 #define DPRINTF(x)
74 #define DPRINTFN(n,x)
75 #endif
76 
77 #define UIPAQ_CONFIG_NO		1
78 #define UIPAQ_IFACE_INDEX	0
79 
80 #define UIPAQIBUFSIZE 1024
81 #define UIPAQOBUFSIZE 1024
82 
83 struct uipaq_softc {
84 	device_t		sc_dev;		/* base device */
85 	struct usbd_device *	sc_udev;	/* device */
86 	struct usbd_interface *	sc_iface;	/* interface */
87 
88 	device_t		sc_subdev;	/* ucom uses that */
89 	uint16_t		sc_lcr;		/* state for DTR/RTS */
90 
91 	uint16_t		sc_flags;
92 
93 	bool			sc_dying;
94 };
95 
96 /* Callback routines */
97 static void	uipaq_set(void *, int, int, int);
98 static int	uipaq_open(void *, int);
99 
100 
101 /* Support routines. */
102 /* based on uppc module by Sam Lawrance */
103 static void	uipaq_dtr(struct uipaq_softc *, int);
104 static void	uipaq_rts(struct uipaq_softc *, int);
105 static void	uipaq_break(struct uipaq_softc *, int);
106 
107 
108 static const struct ucom_methods uipaq_methods = {
109 	.ucom_set = uipaq_set,
110 	.ucom_open = uipaq_open,
111 };
112 
113 struct uipaq_type {
114 	struct usb_devno	uv_dev;
115 	uint16_t		uv_flags;
116 };
117 
118 static const struct uipaq_type uipaq_devs[] = {
119 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 },
120 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0},
121 	{{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0},
122 	{{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0},
123 	{{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS007SH} , 0},
124 	{{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS011SH} , 0}
125 };
126 
127 #define uipaq_lookup(v, p) ((const struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
128 
129 static int uipaq_match(device_t, cfdata_t, void *);
130 static void uipaq_attach(device_t, device_t, void *);
131 static void uipaq_childdet(device_t, device_t);
132 static int uipaq_detach(device_t, int);
133 
134 CFATTACH_DECL2_NEW(uipaq, sizeof(struct uipaq_softc), uipaq_match,
135     uipaq_attach, uipaq_detach, NULL, NULL, uipaq_childdet);
136 
137 static int
uipaq_match(device_t parent,cfdata_t match,void * aux)138 uipaq_match(device_t parent, cfdata_t match, void *aux)
139 {
140 	struct usb_attach_arg *uaa = aux;
141 
142 	DPRINTFN(20,("uipaq: vendor=%#x, product=%#x\n",
143 	    uaa->uaa_vendor, uaa->uaa_product));
144 
145 	return uipaq_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
146 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
147 }
148 
149 static void
uipaq_attach(device_t parent,device_t self,void * aux)150 uipaq_attach(device_t parent, device_t self, void *aux)
151 {
152 	struct uipaq_softc *sc = device_private(self);
153 	struct usb_attach_arg *uaa = aux;
154 	struct usbd_device *dev = uaa->uaa_device;
155 	struct usbd_interface *iface;
156 	usb_interface_descriptor_t *id;
157 	usb_endpoint_descriptor_t *ed;
158 	char *devinfop;
159 	const char *devname = device_xname(self);
160 	int i;
161 	usbd_status err;
162 	struct ucom_attach_args ucaa;
163 
164 	DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
165 
166 	sc->sc_dev = self;
167 	sc->sc_dying = false;
168 
169 	aprint_naive("\n");
170 	aprint_normal("\n");
171 
172 	devinfop = usbd_devinfo_alloc(dev, 0);
173 	aprint_normal_dev(self, "%s\n", devinfop);
174 	usbd_devinfo_free(devinfop);
175 
176 	/* Move the device into the configured state. */
177 	err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
178 	if (err) {
179 		aprint_error_dev(self, "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 		aprint_error("\n%s: failed to get interface, err=%s\n",
187 		    devname, usbd_errstr(err));
188 		goto bad;
189 	}
190 
191 	sc->sc_flags = uipaq_lookup(uaa->uaa_vendor, uaa->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 	ucaa.ucaa_ibufsize = UIPAQIBUFSIZE;
199 	ucaa.ucaa_obufsize = UIPAQOBUFSIZE;
200 	ucaa.ucaa_ibufsizepad = UIPAQIBUFSIZE;
201 	ucaa.ucaa_opkthdrlen = 0;
202 	ucaa.ucaa_device = dev;
203 	ucaa.ucaa_iface = iface;
204 	ucaa.ucaa_methods = &uipaq_methods;
205 	ucaa.ucaa_arg = sc;
206 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
207 	ucaa.ucaa_info = "Generic";
208 
209 /*	err = uipaq_init(sc);
210 	if (err) {
211 		printf("%s: init failed, %s\n", device_xname(sc->sc_dev),
212 		    usbd_errstr(err));
213 		goto bad;
214 	}*/
215 
216 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
217 
218 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
219 	for (i=0; i<id->bNumEndpoints; i++) {
220 		ed = usbd_interface2endpoint_descriptor(iface, i);
221 		if (ed == NULL) {
222 			aprint_error_dev(self,
223 			    "no endpoint descriptor for %d\n", i);
224 			goto bad;
225 		}
226 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
227 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
228 			ucaa.ucaa_bulkin = ed->bEndpointAddress;
229 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
230 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
231 			ucaa.ucaa_bulkout = ed->bEndpointAddress;
232 		}
233 	}
234 	if (ucaa.ucaa_bulkin == -1 || ucaa.ucaa_bulkout == -1) {
235 		aprint_error_dev(self, "no proper endpoints found (%d,%d) \n",
236 		    ucaa.ucaa_bulkin, ucaa.ucaa_bulkout);
237 		return;
238 	}
239 
240 	sc->sc_subdev = config_found(self, &ucaa, ucomprint,
241 	    CFARGS(.submatch = ucomsubmatch));
242 
243 	return;
244 
245 bad:
246 	DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
247 	sc->sc_dying = true;
248 	return;
249 }
250 
251 
252 void
uipaq_dtr(struct uipaq_softc * sc,int onoff)253 uipaq_dtr(struct uipaq_softc* sc, int onoff)
254 {
255 	usb_device_request_t req;
256 	usbd_status err;
257 	int retries = 3;
258 
259 	DPRINTF(("%s: uipaq_dtr: onoff=%x\n", device_xname(sc->sc_dev), onoff));
260 
261 	/* Avoid sending unnecessary requests */
262 	if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
263 		return;
264 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
265 		return;
266 
267 	/* Other parameters depend on reg */
268 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
269 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
270 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR
271 	    : sc->sc_lcr & ~UCDC_LINE_DTR;
272 	USETW(req.wValue, sc->sc_lcr);
273 	USETW(req.wIndex, 0x0);
274 	USETW(req.wLength, 0);
275 
276 	/* Fire off the request a few times if necessary */
277 	while (retries) {
278 		err = usbd_do_request(sc->sc_udev, &req, NULL);
279 		if (!err)
280 			break;
281 		retries--;
282 	}
283 }
284 
285 
286 void
uipaq_rts(struct uipaq_softc * sc,int onoff)287 uipaq_rts(struct uipaq_softc* sc, int onoff)
288 {
289 	usb_device_request_t req;
290 	usbd_status err;
291 	int retries = 3;
292 
293 	DPRINTF(("%s: uipaq_rts: onoff=%x\n", device_xname(sc->sc_dev), onoff));
294 
295 	/* Avoid sending unnecessary requests */
296 	if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
297 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
298 
299 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
300 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
301 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS
302 	    : sc->sc_lcr & ~UCDC_LINE_RTS;
303 	USETW(req.wValue, sc->sc_lcr);
304 	USETW(req.wIndex, 0x0);
305 	USETW(req.wLength, 0);
306 
307 	while (retries) {
308 		err = usbd_do_request(sc->sc_udev, &req, NULL);
309 		if (!err)
310 			break;
311 		retries--;
312 	}
313 }
314 
315 
316 void
uipaq_break(struct uipaq_softc * sc,int onoff)317 uipaq_break(struct uipaq_softc* sc, int onoff)
318 {
319 	usb_device_request_t req;
320 	usbd_status err;
321 	int retries = 3;
322 
323 	DPRINTF(("%s: uipaq_break: onoff=%x\n", device_xname(sc->sc_dev),
324 	    onoff));
325 
326 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
327 	req.bRequest = UCDC_SEND_BREAK;
328 
329 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
330 	USETW(req.wIndex, 0x0);
331 	USETW(req.wLength, 0);
332 
333 	while (retries) {
334 		err = usbd_do_request(sc->sc_udev, &req, NULL);
335 		if (!err)
336 			break;
337 		retries--;
338 	}
339 }
340 
341 
342 void
uipaq_set(void * addr,int portno,int reg,int onoff)343 uipaq_set(void *addr, int portno, int reg, int onoff)
344 {
345 	struct uipaq_softc* sc = addr;
346 
347 	if (sc->sc_dying)
348 		return;
349 
350 	switch (reg) {
351 	case UCOM_SET_DTR:
352 		uipaq_dtr(addr, onoff);
353 		break;
354 	case UCOM_SET_RTS:
355 		uipaq_rts(addr, onoff);
356 		break;
357 	case UCOM_SET_BREAK:
358 		uipaq_break(addr, onoff);
359 		break;
360 	default:
361 		aprint_error_dev(sc->sc_dev,
362 		    "unhandled set request: reg=%x onoff=%x\n", reg, onoff);
363 		return;
364 	}
365 }
366 
367 static int
uipaq_open(void * arg,int portno)368 uipaq_open(void *arg, int portno)
369 {
370 	struct uipaq_softc *sc = arg;
371 
372 	if (sc->sc_dying)
373 		return EIO;
374 
375 	return 0;
376 }
377 
378 static void
uipaq_childdet(device_t self,device_t child)379 uipaq_childdet(device_t self, device_t child)
380 {
381 	struct uipaq_softc *sc = device_private(self);
382 
383 	KASSERT(sc->sc_subdev == child);
384 	sc->sc_subdev = NULL;
385 }
386 
387 static int
uipaq_detach(device_t self,int flags)388 uipaq_detach(device_t self, int flags)
389 {
390 	struct uipaq_softc *sc = device_private(self);
391 	int rv = 0;
392 
393 	DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
394 
395 	sc->sc_dying = true;
396 
397 	if (sc->sc_subdev != NULL) {
398 		rv |= config_detach(sc->sc_subdev, flags);
399 		sc->sc_subdev = NULL;
400 	}
401 	if (sc->sc_udev != NULL)
402 		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
403 		    sc->sc_dev);
404 
405 	return rv;
406 }
407