xref: /netbsd-src/sys/dev/usb/uipaq.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: uipaq.c,v 1.10 2008/04/05 16:35:35 cegger 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  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * iPAQ driver
43  *
44  * 19 July 2003:	Incorporated changes suggested by Sam Lawrance from
45  * 			the uppc module
46  *
47  *
48  * Contact isis@cs.umd.edu if you have any questions/comments about this driver
49  */
50 
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: uipaq.c,v 1.10 2008/04/05 16:35:35 cegger Exp $");
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/device.h>
58 #include <sys/conf.h>
59 #include <sys/tty.h>
60 
61 #include <dev/usb/usb.h>
62 
63 #include <dev/usb/usbcdc.h>	/*UCDC_* stuff */
64 
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbdevs.h>
68 
69 #include <dev/usb/ucomvar.h>
70 
71 #ifdef UIPAQ_DEBUG
72 #define DPRINTF(x)	if (uipaqdebug) printf x
73 #define DPRINTFN(n,x)	if (uipaqdebug>(n)) printf x
74 int uipaqdebug = 0;
75 #else
76 #define DPRINTF(x)
77 #define DPRINTFN(n,x)
78 #endif
79 
80 #define UIPAQ_CONFIG_NO		1
81 #define UIPAQ_IFACE_INDEX	0
82 
83 #define UIPAQIBUFSIZE 1024
84 #define UIPAQOBUFSIZE 1024
85 
86 struct uipaq_softc {
87 	USBBASEDEVICE		sc_dev;		/* base device */
88 	usbd_device_handle	sc_udev;	/* device */
89 	usbd_interface_handle	sc_iface;	/* interface */
90 
91 	device_t		sc_subdev;	/* ucom uses that */
92 	u_int16_t		sc_lcr;		/* state for DTR/RTS */
93 
94 	u_int16_t		sc_flags;
95 
96 	u_char			sc_dying;
97 };
98 
99 /* Callback routines */
100 Static void	uipaq_set(void *, int, int, int);
101 
102 
103 /* Support routines. */
104 /* based on uppc module by Sam Lawrance */
105 Static void	uipaq_dtr(struct uipaq_softc *sc, int onoff);
106 Static void	uipaq_rts(struct uipaq_softc *sc, int onoff);
107 Static void	uipaq_break(struct uipaq_softc* sc, int onoff);
108 
109 
110 struct ucom_methods uipaq_methods = {
111 	NULL,
112 	uipaq_set,
113 	NULL,
114 	NULL,
115 	NULL,	/*open*/
116 	NULL,	/*close*/
117 	NULL,
118 	NULL
119 };
120 
121 struct uipaq_type {
122 	struct usb_devno	uv_dev;
123 	u_int16_t		uv_flags;
124 };
125 
126 static const struct uipaq_type uipaq_devs[] = {
127 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 },
128 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0},
129 	{{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0},
130 	{{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0},
131 	{{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS007SH} , 0}
132 };
133 
134 #define uipaq_lookup(v, p) ((const struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
135 
136 int uipaq_match(device_t, struct cfdata *, void *);
137 void uipaq_attach(device_t, device_t, void *);
138 void uipaq_childdet(device_t, device_t);
139 int uipaq_detach(device_t, int);
140 int uipaq_activate(device_t, enum devact);
141 extern struct cfdriver uipaq_cd;
142 CFATTACH_DECL2(uipaq, sizeof(struct uipaq_softc), uipaq_match,
143     uipaq_attach, uipaq_detach, uipaq_activate, NULL, uipaq_childdet);
144 
145 USB_MATCH(uipaq)
146 {
147 	USB_MATCH_START(uipaq, uaa);
148 
149 	DPRINTFN(20,("uipaq: vendor=0x%x, product=0x%x\n",
150 	    uaa->vendor, uaa->product));
151 
152 	return (uipaq_lookup(uaa->vendor, uaa->product) != NULL ?
153 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
154 }
155 
156 USB_ATTACH(uipaq)
157 {
158 	USB_ATTACH_START(uipaq, sc, uaa);
159 	usbd_device_handle dev = uaa->device;
160 	usbd_interface_handle iface;
161 	usb_interface_descriptor_t *id;
162 	usb_endpoint_descriptor_t *ed;
163 	char *devinfop;
164 	const char *devname = USBDEVNAME(sc->sc_dev);
165 	int i;
166 	usbd_status err;
167 	struct ucom_attach_args uca;
168 
169 	DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
170 
171 	/* Move the device into the configured state. */
172 	err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
173 	if (err) {
174 		printf("\n%s: failed to set configuration, err=%s\n",
175 		    devname, usbd_errstr(err));
176 		goto bad;
177 	}
178 
179 	err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
180 	if (err) {
181 		printf("\n%s: failed to get interface, err=%s\n",
182 		    devname, usbd_errstr(err));
183 		goto bad;
184 	}
185 
186 	devinfop = usbd_devinfo_alloc(dev, 0);
187 	USB_ATTACH_SETUP;
188 	printf("%s: %s\n", devname, devinfop);
189 	usbd_devinfo_free(devinfop);
190 
191 	sc->sc_flags = uipaq_lookup(uaa->vendor, 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 	uca.ibufsize = UIPAQIBUFSIZE;
199 	uca.obufsize = UIPAQOBUFSIZE;
200 	uca.ibufsizepad = UIPAQIBUFSIZE;
201 	uca.opkthdrlen = 0;
202 	uca.device = dev;
203 	uca.iface = iface;
204 	uca.methods = &uipaq_methods;
205 	uca.arg = sc;
206 	uca.portno = UCOM_UNK_PORTNO;
207 	uca.info = "Generic";
208 
209 /*	err = uipaq_init(sc);
210 	if (err) {
211 		printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
212 		    usbd_errstr(err));
213 		goto bad;
214 	}*/
215 
216 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
217 	    USBDEV(sc->sc_dev));
218 
219 	uca.bulkin = uca.bulkout = -1;
220 	for (i=0; i<id->bNumEndpoints; i++) {
221 		ed = usbd_interface2endpoint_descriptor(iface, i);
222 		if (ed == NULL) {
223 			printf("%s: no endpoint descriptor for %d\n",
224 					devname,i);
225 			goto bad;
226 		}
227 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
228 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
229 			uca.bulkin = ed->bEndpointAddress;
230 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
231 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
232 			uca.bulkout = ed->bEndpointAddress;
233 		}
234 	}
235 	if (uca.bulkin == -1 || uca.bulkout == -1) {
236 		printf("%s: no proper endpoints found (%d,%d) \n",
237 		    devname, uca.bulkin, uca.bulkout);
238 		USB_ATTACH_ERROR_RETURN;
239 	}
240 
241 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
242 					    ucomprint, ucomsubmatch);
243 
244 	USB_ATTACH_SUCCESS_RETURN;
245 
246 bad:
247 	DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
248 	sc->sc_dying = 1;
249 	USB_ATTACH_ERROR_RETURN;
250 }
251 
252 
253 void
254 uipaq_dtr(struct uipaq_softc* sc, int onoff)
255 {
256 	usb_device_request_t req;
257 	usbd_status err;
258 	int retries = 3;
259 
260 	DPRINTF(("%s: uipaq_dtr: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
261 
262 	/* Avoid sending unnecessary requests */
263 	if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
264 		return;
265 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
266 		return;
267 
268 	/* Other parameters depend on reg */
269 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
270 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
271 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : 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
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", USBDEVNAME(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 : sc->sc_lcr & ~UCDC_LINE_RTS;
302 	USETW(req.wValue, sc->sc_lcr);
303 	USETW(req.wIndex, 0x0);
304 	USETW(req.wLength, 0);
305 
306 	while (retries) {
307 		err = usbd_do_request(sc->sc_udev, &req, NULL);
308 		if (!err)
309 			break;
310 		retries--;
311 	}
312 }
313 
314 
315 void
316 uipaq_break(struct uipaq_softc* sc, int onoff)
317 {
318 	usb_device_request_t req;
319 	usbd_status err;
320 	int retries = 3;
321 
322 	DPRINTF(("%s: uipaq_break: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
323 
324 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
325 	req.bRequest = UCDC_SEND_BREAK;
326 
327 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
328 	USETW(req.wIndex, 0x0);
329 	USETW(req.wLength, 0);
330 
331 	while (retries) {
332 		err = usbd_do_request(sc->sc_udev, &req, NULL);
333 		if (!err)
334 			break;
335 		retries--;
336 	}
337 }
338 
339 
340 void
341 uipaq_set(void *addr, int portno, int reg, int onoff)
342 {
343 	struct uipaq_softc* sc = addr;
344 
345 	switch (reg) {
346 	case UCOM_SET_DTR:
347 		uipaq_dtr(addr, onoff);
348 		break;
349 	case UCOM_SET_RTS:
350 		uipaq_rts(addr, onoff);
351 		break;
352 	case UCOM_SET_BREAK:
353 		uipaq_break(addr, onoff);
354 		break;
355 	default:
356 		printf("%s: unhandled set request: reg=%x onoff=%x\n",
357 		    USBDEVNAME(sc->sc_dev), reg, onoff);
358 		return;
359 	}
360 }
361 
362 
363 int
364 uipaq_activate(device_t self, enum devact act)
365 {
366 	struct uipaq_softc *sc = device_private(self);
367 	int rv = 0;
368 
369 	switch (act) {
370 	case DVACT_ACTIVATE:
371 		return (EOPNOTSUPP);
372 		break;
373 
374 	case DVACT_DEACTIVATE:
375 		if (sc->sc_subdev != NULL)
376 			rv = config_deactivate(sc->sc_subdev);
377 		sc->sc_dying = 1;
378 		break;
379 	}
380 	return (rv);
381 }
382 
383 void
384 uipaq_childdet(device_t self, device_t child)
385 {
386 	struct uipaq_softc *sc = device_private(self);
387 
388 	KASSERT(sc->sc_subdev == child);
389 	sc->sc_subdev = NULL;
390 }
391 
392 int
393 uipaq_detach(device_t self, int flags)
394 {
395 	struct uipaq_softc *sc = device_private(self);
396 	int rv = 0;
397 
398 	DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
399 	sc->sc_dying = 1;
400 	if (sc->sc_subdev != NULL)
401 		rv |= config_detach(sc->sc_subdev, flags);
402 
403 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
404 	    USBDEV(sc->sc_dev));
405 
406 	return (rv);
407 }
408 
409