xref: /netbsd-src/sys/dev/usb/uipaq.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: uipaq.c,v 1.22 2016/11/25 12:56:29 skrll 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.22 2016/11/25 12:56:29 skrll 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 	u_char			sc_dying;
94 };
95 
96 /* Callback routines */
97 Static void	uipaq_set(void *, int, int, int);
98 
99 
100 /* Support routines. */
101 /* based on uppc module by Sam Lawrance */
102 Static void	uipaq_dtr(struct uipaq_softc *, int);
103 Static void	uipaq_rts(struct uipaq_softc *, int);
104 Static void	uipaq_break(struct uipaq_softc *, int);
105 
106 
107 struct ucom_methods uipaq_methods = {
108 	.ucom_get_status = NULL,
109 	.ucom_set = uipaq_set,
110 	.ucom_param = NULL,
111 	.ucom_ioctl = NULL,
112 	.ucom_open = NULL,
113 	.ucom_close = NULL,
114 	.ucom_read = NULL,
115 	.ucom_write = NULL,
116 };
117 
118 struct uipaq_type {
119 	struct usb_devno	uv_dev;
120 	uint16_t		uv_flags;
121 };
122 
123 static const struct uipaq_type uipaq_devs[] = {
124 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 },
125 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0},
126 	{{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0},
127 	{{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0},
128 	{{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS007SH} , 0},
129 	{{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS011SH} , 0}
130 };
131 
132 #define uipaq_lookup(v, p) ((const struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
133 
134 int uipaq_match(device_t, cfdata_t, void *);
135 void uipaq_attach(device_t, device_t, void *);
136 void uipaq_childdet(device_t, device_t);
137 int uipaq_detach(device_t, int);
138 int uipaq_activate(device_t, enum devact);
139 extern struct cfdriver uipaq_cd;
140 CFATTACH_DECL2_NEW(uipaq, sizeof(struct uipaq_softc), uipaq_match,
141     uipaq_attach, uipaq_detach, uipaq_activate, NULL, uipaq_childdet);
142 
143 int
144 uipaq_match(device_t parent, cfdata_t match, void *aux)
145 {
146 	struct usb_attach_arg *uaa = aux;
147 
148 	DPRINTFN(20,("uipaq: vendor=0x%x, product=0x%x\n",
149 	    uaa->uaa_vendor, uaa->uaa_product));
150 
151 	return uipaq_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
152 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
153 }
154 
155 void
156 uipaq_attach(device_t parent, device_t self, void *aux)
157 {
158 	struct uipaq_softc *sc = device_private(self);
159 	struct usb_attach_arg *uaa = aux;
160 	struct usbd_device *dev = uaa->uaa_device;
161 	struct usbd_interface *iface;
162 	usb_interface_descriptor_t *id;
163 	usb_endpoint_descriptor_t *ed;
164 	char *devinfop;
165 	const char *devname = device_xname(self);
166 	int i;
167 	usbd_status err;
168 	struct ucom_attach_args ucaa;
169 
170 	DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
171 
172 	sc->sc_dev = self;
173 
174 	aprint_naive("\n");
175 	aprint_normal("\n");
176 
177 	devinfop = usbd_devinfo_alloc(dev, 0);
178 	aprint_normal_dev(self, "%s\n", devinfop);
179 	usbd_devinfo_free(devinfop);
180 
181 	/* Move the device into the configured state. */
182 	err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
183 	if (err) {
184 		aprint_error_dev(self, "failed to set configuration, err=%s\n",
185 		    usbd_errstr(err));
186 		goto bad;
187 	}
188 
189 	err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
190 	if (err) {
191 		aprint_error("\n%s: failed to get interface, err=%s\n",
192 		    devname, usbd_errstr(err));
193 		goto bad;
194 	}
195 
196 	sc->sc_flags = uipaq_lookup(uaa->uaa_vendor, uaa->uaa_product)->uv_flags;
197 
198 	id = usbd_get_interface_descriptor(iface);
199 
200 	sc->sc_udev = dev;
201 	sc->sc_iface = iface;
202 
203 	ucaa.ucaa_ibufsize = UIPAQIBUFSIZE;
204 	ucaa.ucaa_obufsize = UIPAQOBUFSIZE;
205 	ucaa.ucaa_ibufsizepad = UIPAQIBUFSIZE;
206 	ucaa.ucaa_opkthdrlen = 0;
207 	ucaa.ucaa_device = dev;
208 	ucaa.ucaa_iface = iface;
209 	ucaa.ucaa_methods = &uipaq_methods;
210 	ucaa.ucaa_arg = sc;
211 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
212 	ucaa.ucaa_info = "Generic";
213 
214 /*	err = uipaq_init(sc);
215 	if (err) {
216 		printf("%s: init failed, %s\n", device_xname(sc->sc_dev),
217 		    usbd_errstr(err));
218 		goto bad;
219 	}*/
220 
221 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
222 
223 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
224 	for (i=0; i<id->bNumEndpoints; i++) {
225 		ed = usbd_interface2endpoint_descriptor(iface, i);
226 		if (ed == NULL) {
227 			aprint_error_dev(self,
228 			    "no endpoint descriptor for %d\n", i);
229 			goto bad;
230 		}
231 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
232 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
233 			ucaa.ucaa_bulkin = ed->bEndpointAddress;
234 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
235 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
236 			ucaa.ucaa_bulkout = ed->bEndpointAddress;
237 		}
238 	}
239 	if (ucaa.ucaa_bulkin == -1 || ucaa.ucaa_bulkout == -1) {
240 		aprint_error_dev(self, "no proper endpoints found (%d,%d) \n",
241 		    ucaa.ucaa_bulkin, ucaa.ucaa_bulkout);
242 		return;
243 	}
244 
245 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
246 					    ucomprint, ucomsubmatch);
247 
248 	return;
249 
250 bad:
251 	DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
252 	sc->sc_dying = 1;
253 	return;
254 }
255 
256 
257 void
258 uipaq_dtr(struct uipaq_softc* sc, int onoff)
259 {
260 	usb_device_request_t req;
261 	usbd_status err;
262 	int retries = 3;
263 
264 	DPRINTF(("%s: uipaq_dtr: onoff=%x\n", device_xname(sc->sc_dev), onoff));
265 
266 	/* Avoid sending unnecessary requests */
267 	if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
268 		return;
269 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
270 		return;
271 
272 	/* Other parameters depend on reg */
273 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
274 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
275 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR
276 	    : sc->sc_lcr & ~UCDC_LINE_DTR;
277 	USETW(req.wValue, sc->sc_lcr);
278 	USETW(req.wIndex, 0x0);
279 	USETW(req.wLength, 0);
280 
281 	/* Fire off the request a few times if necessary */
282 	while (retries) {
283 		err = usbd_do_request(sc->sc_udev, &req, NULL);
284 		if (!err)
285 			break;
286 		retries--;
287 	}
288 }
289 
290 
291 void
292 uipaq_rts(struct uipaq_softc* sc, int onoff)
293 {
294 	usb_device_request_t req;
295 	usbd_status err;
296 	int retries = 3;
297 
298 	DPRINTF(("%s: uipaq_rts: onoff=%x\n", device_xname(sc->sc_dev), onoff));
299 
300 	/* Avoid sending unnecessary requests */
301 	if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
302 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
303 
304 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
305 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
306 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS
307 	    : sc->sc_lcr & ~UCDC_LINE_RTS;
308 	USETW(req.wValue, sc->sc_lcr);
309 	USETW(req.wIndex, 0x0);
310 	USETW(req.wLength, 0);
311 
312 	while (retries) {
313 		err = usbd_do_request(sc->sc_udev, &req, NULL);
314 		if (!err)
315 			break;
316 		retries--;
317 	}
318 }
319 
320 
321 void
322 uipaq_break(struct uipaq_softc* sc, int onoff)
323 {
324 	usb_device_request_t req;
325 	usbd_status err;
326 	int retries = 3;
327 
328 	DPRINTF(("%s: uipaq_break: onoff=%x\n", device_xname(sc->sc_dev),
329 	    onoff));
330 
331 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
332 	req.bRequest = UCDC_SEND_BREAK;
333 
334 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
335 	USETW(req.wIndex, 0x0);
336 	USETW(req.wLength, 0);
337 
338 	while (retries) {
339 		err = usbd_do_request(sc->sc_udev, &req, NULL);
340 		if (!err)
341 			break;
342 		retries--;
343 	}
344 }
345 
346 
347 void
348 uipaq_set(void *addr, int portno, int reg, int onoff)
349 {
350 	struct uipaq_softc* sc = addr;
351 
352 	switch (reg) {
353 	case UCOM_SET_DTR:
354 		uipaq_dtr(addr, onoff);
355 		break;
356 	case UCOM_SET_RTS:
357 		uipaq_rts(addr, onoff);
358 		break;
359 	case UCOM_SET_BREAK:
360 		uipaq_break(addr, onoff);
361 		break;
362 	default:
363 		aprint_error_dev(sc->sc_dev,
364 		    "unhandled set request: reg=%x onoff=%x\n", reg, onoff);
365 		return;
366 	}
367 }
368 
369 
370 int
371 uipaq_activate(device_t self, enum devact act)
372 {
373 	struct uipaq_softc *sc = device_private(self);
374 
375 	switch (act) {
376 	case DVACT_DEACTIVATE:
377 		sc->sc_dying = 1;
378 		return 0;
379 	default:
380 		return EOPNOTSUPP;
381 	}
382 }
383 
384 void
385 uipaq_childdet(device_t self, device_t child)
386 {
387 	struct uipaq_softc *sc = device_private(self);
388 
389 	KASSERT(sc->sc_subdev == child);
390 	sc->sc_subdev = NULL;
391 }
392 
393 int
394 uipaq_detach(device_t self, int flags)
395 {
396 	struct uipaq_softc *sc = device_private(self);
397 	int rv = 0;
398 
399 	DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
400 	sc->sc_dying = 1;
401 	if (sc->sc_subdev != NULL)
402 		rv |= config_detach(sc->sc_subdev, flags);
403 
404 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
405 
406 	return rv;
407 }
408 
409