No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / usb / uipaq.c
blob47a6d3681087fe142df909f1af0ba1cc07788118
1 /* $NetBSD: uipaq.c,v 1.14 2009/09/23 19:07:19 plunky Exp $ */
2 /* $OpenBSD: uipaq.c,v 1.1 2005/06/17 23:50:33 deraadt Exp $ */
4 /*
5 * Copyright (c) 2000-2005 The NetBSD Foundation, Inc.
6 * All rights reserved.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
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.
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.
35 * iPAQ driver
37 * 19 July 2003: Incorporated changes suggested by Sam Lawrance from
38 * the uppc module
41 * Contact isis@cs.umd.edu if you have any questions/comments about this driver
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: uipaq.c,v 1.14 2009/09/23 19:07:19 plunky Exp $");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/device.h>
51 #include <sys/conf.h>
52 #include <sys/tty.h>
54 #include <dev/usb/usb.h>
56 #include <dev/usb/usbcdc.h> /*UCDC_* stuff */
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/ucomvar.h>
64 #ifdef UIPAQ_DEBUG
65 #define DPRINTF(x) if (uipaqdebug) printf x
66 #define DPRINTFN(n,x) if (uipaqdebug>(n)) printf x
67 int uipaqdebug = 0;
68 #else
69 #define DPRINTF(x)
70 #define DPRINTFN(n,x)
71 #endif
73 #define UIPAQ_CONFIG_NO 1
74 #define UIPAQ_IFACE_INDEX 0
76 #define UIPAQIBUFSIZE 1024
77 #define UIPAQOBUFSIZE 1024
79 struct uipaq_softc {
80 USBBASEDEVICE sc_dev; /* base device */
81 usbd_device_handle sc_udev; /* device */
82 usbd_interface_handle sc_iface; /* interface */
84 device_t sc_subdev; /* ucom uses that */
85 u_int16_t sc_lcr; /* state for DTR/RTS */
87 u_int16_t sc_flags;
89 u_char sc_dying;
92 /* Callback routines */
93 Static void uipaq_set(void *, int, int, int);
96 /* Support routines. */
97 /* based on uppc module by Sam Lawrance */
98 Static void uipaq_dtr(struct uipaq_softc *sc, int onoff);
99 Static void uipaq_rts(struct uipaq_softc *sc, int onoff);
100 Static void uipaq_break(struct uipaq_softc* sc, int onoff);
103 struct ucom_methods uipaq_methods = {
104 NULL,
105 uipaq_set,
106 NULL,
107 NULL,
108 NULL, /*open*/
109 NULL, /*close*/
110 NULL,
111 NULL
114 struct uipaq_type {
115 struct usb_devno uv_dev;
116 u_int16_t uv_flags;
119 static const struct uipaq_type uipaq_devs[] = {
120 {{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 },
121 {{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0},
122 {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0},
123 {{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0},
124 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS007SH} , 0},
125 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS011SH} , 0}
128 #define uipaq_lookup(v, p) ((const struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
130 int uipaq_match(device_t, cfdata_t, void *);
131 void uipaq_attach(device_t, device_t, void *);
132 void uipaq_childdet(device_t, device_t);
133 int uipaq_detach(device_t, int);
134 int uipaq_activate(device_t, enum devact);
135 extern struct cfdriver uipaq_cd;
136 CFATTACH_DECL2_NEW(uipaq, sizeof(struct uipaq_softc), uipaq_match,
137 uipaq_attach, uipaq_detach, uipaq_activate, NULL, uipaq_childdet);
139 USB_MATCH(uipaq)
141 USB_MATCH_START(uipaq, uaa);
143 DPRINTFN(20,("uipaq: vendor=0x%x, product=0x%x\n",
144 uaa->vendor, uaa->product));
146 return (uipaq_lookup(uaa->vendor, uaa->product) != NULL ?
147 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
150 USB_ATTACH(uipaq)
152 USB_ATTACH_START(uipaq, sc, uaa);
153 usbd_device_handle dev = uaa->device;
154 usbd_interface_handle iface;
155 usb_interface_descriptor_t *id;
156 usb_endpoint_descriptor_t *ed;
157 char *devinfop;
158 const char *devname = device_xname(self);
159 int i;
160 usbd_status err;
161 struct ucom_attach_args uca;
163 DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
165 sc->sc_dev = self;
167 aprint_naive("\n");
168 aprint_normal("\n");
170 devinfop = usbd_devinfo_alloc(dev, 0);
171 aprint_normal_dev(self, "%s\n", devinfop);
172 usbd_devinfo_free(devinfop);
174 /* Move the device into the configured state. */
175 err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
176 if (err) {
177 aprint_error("\n%s: failed to set configuration, err=%s\n",
178 devname, usbd_errstr(err));
179 goto bad;
182 err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
183 if (err) {
184 aprint_error("\n%s: failed to get interface, err=%s\n",
185 devname, usbd_errstr(err));
186 goto bad;
189 sc->sc_flags = uipaq_lookup(uaa->vendor, uaa->product)->uv_flags;
191 id = usbd_get_interface_descriptor(iface);
193 sc->sc_udev = dev;
194 sc->sc_iface = iface;
196 uca.ibufsize = UIPAQIBUFSIZE;
197 uca.obufsize = UIPAQOBUFSIZE;
198 uca.ibufsizepad = UIPAQIBUFSIZE;
199 uca.opkthdrlen = 0;
200 uca.device = dev;
201 uca.iface = iface;
202 uca.methods = &uipaq_methods;
203 uca.arg = sc;
204 uca.portno = UCOM_UNK_PORTNO;
205 uca.info = "Generic";
207 /* err = uipaq_init(sc);
208 if (err) {
209 printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
210 usbd_errstr(err));
211 goto bad;
214 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
215 USBDEV(sc->sc_dev));
217 uca.bulkin = uca.bulkout = -1;
218 for (i=0; i<id->bNumEndpoints; i++) {
219 ed = usbd_interface2endpoint_descriptor(iface, i);
220 if (ed == NULL) {
221 aprint_error_dev(self,
222 "no endpoint descriptor for %d\n", i);
223 goto bad;
225 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
226 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
227 uca.bulkin = ed->bEndpointAddress;
228 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
229 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
230 uca.bulkout = ed->bEndpointAddress;
233 if (uca.bulkin == -1 || uca.bulkout == -1) {
234 aprint_error_dev(self, "no proper endpoints found (%d,%d) \n",
235 uca.bulkin, uca.bulkout);
236 USB_ATTACH_ERROR_RETURN;
239 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
240 ucomprint, ucomsubmatch);
242 USB_ATTACH_SUCCESS_RETURN;
244 bad:
245 DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
246 sc->sc_dying = 1;
247 USB_ATTACH_ERROR_RETURN;
251 void
252 uipaq_dtr(struct uipaq_softc* sc, int onoff)
254 usb_device_request_t req;
255 usbd_status err;
256 int retries = 3;
258 DPRINTF(("%s: uipaq_dtr: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
260 /* Avoid sending unnecessary requests */
261 if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
262 return;
263 if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
264 return;
266 /* Other parameters depend on reg */
267 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
268 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
269 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
270 USETW(req.wValue, sc->sc_lcr);
271 USETW(req.wIndex, 0x0);
272 USETW(req.wLength, 0);
274 /* Fire off the request a few times if necessary */
275 while (retries) {
276 err = usbd_do_request(sc->sc_udev, &req, NULL);
277 if (!err)
278 break;
279 retries--;
284 void
285 uipaq_rts(struct uipaq_softc* sc, int onoff)
287 usb_device_request_t req;
288 usbd_status err;
289 int retries = 3;
291 DPRINTF(("%s: uipaq_rts: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
293 /* Avoid sending unnecessary requests */
294 if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
295 if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
297 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
298 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
299 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
300 USETW(req.wValue, sc->sc_lcr);
301 USETW(req.wIndex, 0x0);
302 USETW(req.wLength, 0);
304 while (retries) {
305 err = usbd_do_request(sc->sc_udev, &req, NULL);
306 if (!err)
307 break;
308 retries--;
313 void
314 uipaq_break(struct uipaq_softc* sc, int onoff)
316 usb_device_request_t req;
317 usbd_status err;
318 int retries = 3;
320 DPRINTF(("%s: uipaq_break: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
322 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
323 req.bRequest = UCDC_SEND_BREAK;
325 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
326 USETW(req.wIndex, 0x0);
327 USETW(req.wLength, 0);
329 while (retries) {
330 err = usbd_do_request(sc->sc_udev, &req, NULL);
331 if (!err)
332 break;
333 retries--;
338 void
339 uipaq_set(void *addr, int portno, int reg, int onoff)
341 struct uipaq_softc* sc = addr;
343 switch (reg) {
344 case UCOM_SET_DTR:
345 uipaq_dtr(addr, onoff);
346 break;
347 case UCOM_SET_RTS:
348 uipaq_rts(addr, onoff);
349 break;
350 case UCOM_SET_BREAK:
351 uipaq_break(addr, onoff);
352 break;
353 default:
354 aprint_error_dev(sc->sc_dev,
355 "unhandled set request: reg=%x onoff=%x\n", reg, onoff);
356 return;
362 uipaq_activate(device_t self, enum devact act)
364 struct uipaq_softc *sc = device_private(self);
366 switch (act) {
367 case DVACT_DEACTIVATE:
368 sc->sc_dying = 1;
369 return 0;
370 default:
371 return EOPNOTSUPP;
375 void
376 uipaq_childdet(device_t self, device_t child)
378 struct uipaq_softc *sc = device_private(self);
380 KASSERT(sc->sc_subdev == child);
381 sc->sc_subdev = NULL;
385 uipaq_detach(device_t self, int flags)
387 struct uipaq_softc *sc = device_private(self);
388 int rv = 0;
390 DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
391 sc->sc_dying = 1;
392 if (sc->sc_subdev != NULL)
393 rv |= config_detach(sc->sc_subdev, flags);
395 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
396 USBDEV(sc->sc_dev));
398 return (rv);