uipaq.c revision 1.2.24.2 1 /* $NetBSD: uipaq.c,v 1.2.24.2 2006/12/10 07:18:17 yamt 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 (at) 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 (at) cs.umd.edu if you have any questions/comments about this driver
49 */
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 #include <dev/usb/usbhid.h>
60
61 #include <dev/usb/usbcdc.h> /*UCDC_* stuff */
62
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdevs.h>
66
67 #include <dev/usb/ucomvar.h>
68
69 #ifdef UIPAQ_DEBUG
70 #define DPRINTF(x) if (uipaqdebug) printf x
71 #define DPRINTFN(n,x) if (uipaqdebug>(n)) printf x
72 int uipaqdebug = 0;
73 #else
74 #define DPRINTF(x)
75 #define DPRINTFN(n,x)
76 #endif
77
78 #define UIPAQ_CONFIG_NO 1
79 #define UIPAQ_IFACE_INDEX 0
80
81 #define UIPAQIBUFSIZE 1024
82 #define UIPAQOBUFSIZE 1024
83
84 struct uipaq_softc {
85 USBBASEDEVICE sc_dev; /* base device */
86 usbd_device_handle sc_udev; /* device */
87 usbd_interface_handle sc_iface; /* interface */
88
89 device_ptr_t sc_subdev; /* ucom uses that */
90 u_int16_t sc_lcr; /* state for DTR/RTS */
91
92 u_int16_t sc_flags;
93
94 u_char sc_dying;
95 };
96
97 /* Callback routines */
98 Static void uipaq_set(void *, int, int, int);
99
100
101 /* Support routines. */
102 /* based on uppc module by Sam Lawrance */
103 Static void uipaq_dtr(struct uipaq_softc *sc, int onoff);
104 Static void uipaq_rts(struct uipaq_softc *sc, int onoff);
105 Static void uipaq_break(struct uipaq_softc* sc, int onoff);
106
107
108 struct ucom_methods uipaq_methods = {
109 NULL,
110 uipaq_set,
111 NULL,
112 NULL,
113 NULL, /*open*/
114 NULL, /*close*/
115 NULL,
116 NULL
117 };
118
119 struct uipaq_type {
120 struct usb_devno uv_dev;
121 u_int16_t uv_flags;
122 };
123
124 static const struct uipaq_type uipaq_devs[] = {
125 {{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 },
126 {{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0},
127 {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0},
128 {{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0}
129 };
130
131 #define uipaq_lookup(v, p) ((const struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
132
133 USB_DECLARE_DRIVER(uipaq);
134
135 USB_MATCH(uipaq)
136 {
137 USB_MATCH_START(uipaq, uaa);
138
139 if (uaa->iface != NULL)
140 return (UMATCH_NONE);
141
142 DPRINTFN(20,("uipaq: vendor=0x%x, product=0x%x\n",
143 uaa->vendor, uaa->product));
144
145 return (uipaq_lookup(uaa->vendor, uaa->product) != NULL ?
146 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
147 }
148
149 USB_ATTACH(uipaq)
150 {
151 USB_ATTACH_START(uipaq, sc, uaa);
152 usbd_device_handle dev = uaa->device;
153 usbd_interface_handle iface;
154 usb_interface_descriptor_t *id;
155 usb_endpoint_descriptor_t *ed;
156 char *devinfop;
157 char *devname = USBDEVNAME(sc->sc_dev);
158 int i;
159 usbd_status err;
160 struct ucom_attach_args uca;
161
162 DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
163
164 /* Move the device into the configured state. */
165 err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
166 if (err) {
167 printf("\n%s: failed to set configuration, err=%s\n",
168 devname, usbd_errstr(err));
169 goto bad;
170 }
171
172 err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
173 if (err) {
174 printf("\n%s: failed to get interface, err=%s\n",
175 devname, usbd_errstr(err));
176 goto bad;
177 }
178
179 devinfop = usbd_devinfo_alloc(dev, 0);
180 USB_ATTACH_SETUP;
181 printf("%s: %s\n", devname, devinfop);
182 usbd_devinfo_free(devinfop);
183
184 sc->sc_flags = uipaq_lookup(uaa->vendor, uaa->product)->uv_flags;
185
186 id = usbd_get_interface_descriptor(iface);
187
188 sc->sc_udev = dev;
189 sc->sc_iface = iface;
190
191 uca.ibufsize = UIPAQIBUFSIZE;
192 uca.obufsize = UIPAQOBUFSIZE;
193 uca.ibufsizepad = UIPAQIBUFSIZE;
194 uca.opkthdrlen = 0;
195 uca.device = dev;
196 uca.iface = iface;
197 uca.methods = &uipaq_methods;
198 uca.arg = sc;
199 uca.portno = UCOM_UNK_PORTNO;
200 uca.info = "Generic";
201
202 /* err = uipaq_init(sc);
203 if (err) {
204 printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
205 usbd_errstr(err));
206 goto bad;
207 }*/
208
209 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
210 USBDEV(sc->sc_dev));
211
212 uca.bulkin = uca.bulkout = -1;
213 for (i=0; i<id->bNumEndpoints; i++) {
214 ed = usbd_interface2endpoint_descriptor(iface, i);
215 if (ed == NULL) {
216 printf("%s: no endpoint descriptor for %d\n",
217 devname,i);
218 goto bad;
219 }
220 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
221 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
222 uca.bulkin = ed->bEndpointAddress;
223 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
224 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
225 uca.bulkout = ed->bEndpointAddress;
226 }
227 }
228 if (uca.bulkin == -1 || uca.bulkout == -1) {
229 printf("%s: no proper endpoints found (%d,%d) \n",
230 devname, uca.bulkin, uca.bulkout);
231 USB_ATTACH_ERROR_RETURN;
232 }
233
234 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
235 ucomprint, ucomsubmatch);
236
237 USB_ATTACH_SUCCESS_RETURN;
238
239 bad:
240 DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
241 sc->sc_dying = 1;
242 USB_ATTACH_ERROR_RETURN;
243 }
244
245
246 void
247 uipaq_dtr(struct uipaq_softc* sc, int onoff)
248 {
249 usb_device_request_t req;
250 usbd_status err;
251 int retries = 3;
252
253 DPRINTF(("%s: uipaq_dtr: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
254
255 /* Avoid sending unnecessary requests */
256 if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
257 return;
258 if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
259 return;
260
261 /* Other parameters depend on reg */
262 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
263 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
264 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
265 USETW(req.wValue, sc->sc_lcr);
266 USETW(req.wIndex, 0x0);
267 USETW(req.wLength, 0);
268
269 /* Fire off the request a few times if necessary */
270 while (retries) {
271 err = usbd_do_request(sc->sc_udev, &req, NULL);
272 if (!err)
273 break;
274 retries--;
275 }
276 }
277
278
279 void
280 uipaq_rts(struct uipaq_softc* sc, int onoff)
281 {
282 usb_device_request_t req;
283 usbd_status err;
284 int retries = 3;
285
286 DPRINTF(("%s: uipaq_rts: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
287
288 /* Avoid sending unnecessary requests */
289 if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
290 if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
291
292 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
293 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
294 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
295 USETW(req.wValue, sc->sc_lcr);
296 USETW(req.wIndex, 0x0);
297 USETW(req.wLength, 0);
298
299 while (retries) {
300 err = usbd_do_request(sc->sc_udev, &req, NULL);
301 if (!err)
302 break;
303 retries--;
304 }
305 }
306
307
308 void
309 uipaq_break(struct uipaq_softc* sc, int onoff)
310 {
311 usb_device_request_t req;
312 usbd_status err;
313 int retries = 3;
314
315 DPRINTF(("%s: uipaq_break: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
316
317 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
318 req.bRequest = UCDC_SEND_BREAK;
319
320 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
321 USETW(req.wIndex, 0x0);
322 USETW(req.wLength, 0);
323
324 while (retries) {
325 err = usbd_do_request(sc->sc_udev, &req, NULL);
326 if (!err)
327 break;
328 retries--;
329 }
330 }
331
332
333 void
334 uipaq_set(void *addr, int portno, int reg, int onoff)
335 {
336 struct uipaq_softc* sc = addr;
337
338 switch (reg) {
339 case UCOM_SET_DTR:
340 uipaq_dtr(addr, onoff);
341 break;
342 case UCOM_SET_RTS:
343 uipaq_rts(addr, onoff);
344 break;
345 case UCOM_SET_BREAK:
346 uipaq_break(addr, onoff);
347 break;
348 default:
349 printf("%s: unhandled set request: reg=%x onoff=%x\n",
350 USBDEVNAME(sc->sc_dev), reg, onoff);
351 return;
352 }
353 }
354
355
356 int
357 uipaq_activate(device_ptr_t self, enum devact act)
358 {
359 struct uipaq_softc *sc = (struct uipaq_softc *)self;
360 int rv = 0;
361
362 switch (act) {
363 case DVACT_ACTIVATE:
364 return (EOPNOTSUPP);
365 break;
366
367 case DVACT_DEACTIVATE:
368 if (sc->sc_subdev != NULL)
369 rv = config_deactivate(sc->sc_subdev);
370 sc->sc_dying = 1;
371 break;
372 }
373 return (rv);
374 }
375
376 int
377 uipaq_detach(device_ptr_t self, int flags)
378 {
379 struct uipaq_softc *sc = (struct uipaq_softc *)self;
380 int rv = 0;
381
382 DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
383 sc->sc_dying = 1;
384 if (sc->sc_subdev != NULL) {
385 rv |= config_detach(sc->sc_subdev, flags);
386 sc->sc_subdev = NULL;
387 }
388
389 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
390 USBDEV(sc->sc_dev));
391
392 return (rv);
393 }
394
395