1 /*	$OpenBSD: uipaq.c,v 1.1 2005/06/17 23:50:33 deraadt 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * iPAQ driver
42  *
43  * 19 July 2003:	Incorporated changes suggested by Sam Lawrance from
44  * 			the uppc module
45  *
46  *
47  * Contact isis@cs.umd.edu if you have any questions/comments about this driver
48  */
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #include <sys/conf.h>
55 #include <sys/tty.h>
56 
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbhid.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 	USBBASEDEVICE		sc_dev;		/* base device */
85 	usbd_device_handle	sc_udev;	/* device */
86 	usbd_interface_handle	sc_iface;	/* interface */
87 
88 	device_ptr_t		sc_subdev;	/* ucom uses that */
89 	u_int16_t		sc_lcr;		/* state for DTR/RTS */
90 
91 	u_int16_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 *sc, int onoff);
103 Static void	uipaq_rts(struct uipaq_softc *sc, int onoff);
104 Static void	uipaq_break(struct uipaq_softc* sc, int onoff);
105 
106 
107 struct ucom_methods uipaq_methods = {
108 	NULL,
109 	uipaq_set,
110 	NULL,
111 	NULL,
112 	NULL,	/*open*/
113 	NULL,	/*close*/
114 	NULL,
115 	NULL
116 };
117 
118 struct uipaq_type {
119 	struct usb_devno	uv_dev;
120 	u_int16_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 };
129 
130 #define uipaq_lookup(v, p) ((struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
131 
132 USB_DECLARE_DRIVER(uipaq);
133 
USB_MATCH(uipaq)134 USB_MATCH(uipaq)
135 {
136 	USB_MATCH_START(uipaq, uaa);
137 
138 	if (uaa->iface != NULL)
139 		return (UMATCH_NONE);
140 
141 	DPRINTFN(20,("uipaq: vendor=0x%x, product=0x%x\n",
142 	    uaa->vendor, uaa->product));
143 
144 	return (uipaq_lookup(uaa->vendor, uaa->product) != NULL ?
145 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
146 }
147 
USB_ATTACH(uipaq)148 USB_ATTACH(uipaq)
149 {
150 	USB_ATTACH_START(uipaq, sc, uaa);
151 	usbd_device_handle dev = uaa->device;
152 	usbd_interface_handle iface;
153 	usb_interface_descriptor_t *id;
154 	usb_endpoint_descriptor_t *ed;
155 	char devinfo[1024];
156 	char *devname = USBDEVNAME(sc->sc_dev);
157 	int i;
158 	usbd_status err;
159 	struct ucom_attach_args uca;
160 
161 	DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
162 
163 	/* Move the device into the configured state. */
164 	err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
165 	if (err) {
166 		printf("\n%s: failed to set configuration, err=%s\n",
167 		    devname, usbd_errstr(err));
168 		goto bad;
169 	}
170 
171 	err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
172 	if (err) {
173 		printf("\n%s: failed to get interface, err=%s\n",
174 		    devname, usbd_errstr(err));
175 		goto bad;
176 	}
177 
178 	usbd_devinfo(dev, 0, devinfo, sizeof devinfo);
179 	USB_ATTACH_SETUP;
180 	printf("%s: %s\n", devname, devinfo);
181 
182 	sc->sc_flags = uipaq_lookup(uaa->vendor, uaa->product)->uv_flags;
183 
184 	id = usbd_get_interface_descriptor(iface);
185 
186 	sc->sc_udev = dev;
187 	sc->sc_iface = iface;
188 
189 	uca.ibufsize = UIPAQIBUFSIZE;
190 	uca.obufsize = UIPAQOBUFSIZE;
191 	uca.ibufsizepad = UIPAQIBUFSIZE;
192 	uca.opkthdrlen = 0;
193 	uca.device = dev;
194 	uca.iface = iface;
195 	uca.methods = &uipaq_methods;
196 	uca.arg = sc;
197 	uca.portno = UCOM_UNK_PORTNO;
198 	uca.info = "Generic";
199 
200 /*	err = uipaq_init(sc);
201 	if (err) {
202 		printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
203 		    usbd_errstr(err));
204 		goto bad;
205 	}*/
206 
207 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
208 	    USBDEV(sc->sc_dev));
209 
210 	uca.bulkin = uca.bulkout = -1;
211 	for (i=0; i<id->bNumEndpoints; i++) {
212 		ed = usbd_interface2endpoint_descriptor(iface, i);
213 		if (ed == NULL) {
214 			printf("%s: no endpoint descriptor for %d\n",
215 					devname,i);
216 			goto bad;
217 		}
218 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
219 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
220 			uca.bulkin = ed->bEndpointAddress;
221 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
222 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
223 			uca.bulkout = ed->bEndpointAddress;
224 		}
225 	}
226 	if (uca.bulkin != -1 && uca.bulkout != -1)
227 		sc->sc_subdev = config_found_sm(self, &uca,
228 		    ucomprint, ucomsubmatch);
229 	else
230 		printf("%s: no proper endpoints found (%d,%d) \n",
231 		    devname, uca.bulkin, uca.bulkout);
232 
233 	USB_ATTACH_SUCCESS_RETURN;
234 
235 bad:
236 	DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
237 	sc->sc_dying = 1;
238 	USB_ATTACH_ERROR_RETURN;
239 }
240 
241 
242 void
uipaq_dtr(struct uipaq_softc * sc,int onoff)243 uipaq_dtr(struct uipaq_softc* sc, int onoff)
244 {
245 	usb_device_request_t req;
246 	usbd_status err;
247 	int retries = 3;
248 
249 	DPRINTF(("%s: uipaq_dtr: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
250 
251 	/* Avoid sending unnecessary requests */
252 	if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
253 		return;
254 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
255 		return;
256 
257 	/* Other parameters depend on reg */
258 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
259 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
260 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
261 	USETW(req.wValue, sc->sc_lcr);
262 	USETW(req.wIndex, 0x0);
263 	USETW(req.wLength, 0);
264 
265 	/* Fire off the request a few times if necessary */
266 	while (retries) {
267 		err = usbd_do_request(sc->sc_udev, &req, NULL);
268 		if (!err)
269 			break;
270 		retries--;
271 	}
272 }
273 
274 
275 void
uipaq_rts(struct uipaq_softc * sc,int onoff)276 uipaq_rts(struct uipaq_softc* sc, int onoff)
277 {
278 	usb_device_request_t req;
279 	usbd_status err;
280 	int retries = 3;
281 
282 	DPRINTF(("%s: uipaq_rts: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
283 
284 	/* Avoid sending unnecessary requests */
285 	if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
286 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
287 
288 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
289 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
290 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
291 	USETW(req.wValue, sc->sc_lcr);
292 	USETW(req.wIndex, 0x0);
293 	USETW(req.wLength, 0);
294 
295 	while (retries) {
296 		err = usbd_do_request(sc->sc_udev, &req, NULL);
297 		if (!err)
298 			break;
299 		retries--;
300 	}
301 }
302 
303 
304 void
uipaq_break(struct uipaq_softc * sc,int onoff)305 uipaq_break(struct uipaq_softc* sc, int onoff)
306 {
307 	usb_device_request_t req;
308 	usbd_status err;
309 	int retries = 3;
310 
311 	DPRINTF(("%s: uipaq_break: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
312 
313 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
314 	req.bRequest = UCDC_SEND_BREAK;
315 
316 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
317 	USETW(req.wIndex, 0x0);
318 	USETW(req.wLength, 0);
319 
320 	while (retries) {
321 		err = usbd_do_request(sc->sc_udev, &req, NULL);
322 		if (!err)
323 			break;
324 		retries--;
325 	}
326 }
327 
328 
329 void
uipaq_set(void * addr,int portno,int reg,int onoff)330 uipaq_set(void *addr, int portno, int reg, int onoff)
331 {
332 	struct uipaq_softc* sc = addr;
333 
334 	switch (reg) {
335 	case UCOM_SET_DTR:
336 		uipaq_dtr(addr, onoff);
337 		break;
338 	case UCOM_SET_RTS:
339 		uipaq_rts(addr, onoff);
340 		break;
341 	case UCOM_SET_BREAK:
342 		uipaq_break(addr, onoff);
343 		break;
344 	default:
345 		printf("%s: unhandled set request: reg=%x onoff=%x\n",
346 		    USBDEVNAME(sc->sc_dev), reg, onoff);
347 		return;
348 	}
349 }
350 
351 
352 int
uipaq_activate(device_ptr_t self,enum devact act)353 uipaq_activate(device_ptr_t self, enum devact act)
354 {
355 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
356 	int rv = 0;
357 
358 	switch (act) {
359 	case DVACT_ACTIVATE:
360 		return (EOPNOTSUPP);
361 		break;
362 
363 	case DVACT_DEACTIVATE:
364 		if (sc->sc_subdev != NULL)
365 			rv = config_deactivate(sc->sc_subdev);
366 		sc->sc_dying = 1;
367 		break;
368 	}
369 	return (rv);
370 }
371 
372 int
uipaq_detach(device_ptr_t self,int flags)373 uipaq_detach(device_ptr_t self, int flags)
374 {
375 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
376 	int rv = 0;
377 
378 	DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
379 	sc->sc_dying = 1;
380 	if (sc->sc_subdev != NULL) {
381 		rv |= config_detach(sc->sc_subdev, flags);
382 		sc->sc_subdev = NULL;
383 	}
384 
385 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
386 	    USBDEV(sc->sc_dev));
387 
388 	return (rv);
389 }
390 
391