uipaq.c revision 1.12 1 /* $NetBSD: uipaq.c,v 1.12 2008/05/24 16:40:58 cube 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 *
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 (at) 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.12 2008/05/24 16:40:58 cube Exp $");
46
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>
53
54 #include <dev/usb/usb.h>
55
56 #include <dev/usb/usbcdc.h> /*UCDC_* stuff */
57
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbdevs.h>
61
62 #include <dev/usb/ucomvar.h>
63
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
72
73 #define UIPAQ_CONFIG_NO 1
74 #define UIPAQ_IFACE_INDEX 0
75
76 #define UIPAQIBUFSIZE 1024
77 #define UIPAQOBUFSIZE 1024
78
79 struct uipaq_softc {
80 USBBASEDEVICE sc_dev; /* base device */
81 usbd_device_handle sc_udev; /* device */
82 usbd_interface_handle sc_iface; /* interface */
83
84 device_t sc_subdev; /* ucom uses that */
85 u_int16_t sc_lcr; /* state for DTR/RTS */
86
87 u_int16_t sc_flags;
88
89 u_char sc_dying;
90 };
91
92 /* Callback routines */
93 Static void uipaq_set(void *, int, int, int);
94
95
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);
101
102
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
112 };
113
114 struct uipaq_type {
115 struct usb_devno uv_dev;
116 u_int16_t uv_flags;
117 };
118
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 };
126
127 #define uipaq_lookup(v, p) ((const struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
128
129 int uipaq_match(device_t, cfdata_t, void *);
130 void uipaq_attach(device_t, device_t, void *);
131 void uipaq_childdet(device_t, device_t);
132 int uipaq_detach(device_t, int);
133 int uipaq_activate(device_t, enum devact);
134 extern struct cfdriver uipaq_cd;
135 CFATTACH_DECL2_NEW(uipaq, sizeof(struct uipaq_softc), uipaq_match,
136 uipaq_attach, uipaq_detach, uipaq_activate, NULL, uipaq_childdet);
137
138 USB_MATCH(uipaq)
139 {
140 USB_MATCH_START(uipaq, uaa);
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 const char *devname = device_xname(self);
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 sc->sc_dev = self;
165
166 /* Move the device into the configured state. */
167 err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
168 if (err) {
169 aprint_error("\n%s: failed to set configuration, err=%s\n",
170 devname, usbd_errstr(err));
171 goto bad;
172 }
173
174 err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
175 if (err) {
176 aprint_error("\n%s: failed to get interface, err=%s\n",
177 devname, usbd_errstr(err));
178 goto bad;
179 }
180
181 devinfop = usbd_devinfo_alloc(dev, 0);
182 USB_ATTACH_SETUP;
183 aprint_normal_dev(self, "%s\n", devinfop);
184 usbd_devinfo_free(devinfop);
185
186 sc->sc_flags = uipaq_lookup(uaa->vendor, uaa->product)->uv_flags;
187
188 id = usbd_get_interface_descriptor(iface);
189
190 sc->sc_udev = dev;
191 sc->sc_iface = iface;
192
193 uca.ibufsize = UIPAQIBUFSIZE;
194 uca.obufsize = UIPAQOBUFSIZE;
195 uca.ibufsizepad = UIPAQIBUFSIZE;
196 uca.opkthdrlen = 0;
197 uca.device = dev;
198 uca.iface = iface;
199 uca.methods = &uipaq_methods;
200 uca.arg = sc;
201 uca.portno = UCOM_UNK_PORTNO;
202 uca.info = "Generic";
203
204 /* err = uipaq_init(sc);
205 if (err) {
206 printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
207 usbd_errstr(err));
208 goto bad;
209 }*/
210
211 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
212 USBDEV(sc->sc_dev));
213
214 uca.bulkin = uca.bulkout = -1;
215 for (i=0; i<id->bNumEndpoints; i++) {
216 ed = usbd_interface2endpoint_descriptor(iface, i);
217 if (ed == NULL) {
218 aprint_error_dev(self,
219 "no endpoint descriptor for %d\n", i);
220 goto bad;
221 }
222 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
223 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
224 uca.bulkin = ed->bEndpointAddress;
225 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
226 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
227 uca.bulkout = ed->bEndpointAddress;
228 }
229 }
230 if (uca.bulkin == -1 || uca.bulkout == -1) {
231 aprint_error_dev(self, "no proper endpoints found (%d,%d) \n",
232 uca.bulkin, uca.bulkout);
233 USB_ATTACH_ERROR_RETURN;
234 }
235
236 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
237 ucomprint, ucomsubmatch);
238
239 USB_ATTACH_SUCCESS_RETURN;
240
241 bad:
242 DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
243 sc->sc_dying = 1;
244 USB_ATTACH_ERROR_RETURN;
245 }
246
247
248 void
249 uipaq_dtr(struct uipaq_softc* sc, int onoff)
250 {
251 usb_device_request_t req;
252 usbd_status err;
253 int retries = 3;
254
255 DPRINTF(("%s: uipaq_dtr: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
256
257 /* Avoid sending unnecessary requests */
258 if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
259 return;
260 if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
261 return;
262
263 /* Other parameters depend on reg */
264 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
265 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
266 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
267 USETW(req.wValue, sc->sc_lcr);
268 USETW(req.wIndex, 0x0);
269 USETW(req.wLength, 0);
270
271 /* Fire off the request a few times if necessary */
272 while (retries) {
273 err = usbd_do_request(sc->sc_udev, &req, NULL);
274 if (!err)
275 break;
276 retries--;
277 }
278 }
279
280
281 void
282 uipaq_rts(struct uipaq_softc* sc, int onoff)
283 {
284 usb_device_request_t req;
285 usbd_status err;
286 int retries = 3;
287
288 DPRINTF(("%s: uipaq_rts: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
289
290 /* Avoid sending unnecessary requests */
291 if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
292 if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
293
294 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
295 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
296 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
297 USETW(req.wValue, sc->sc_lcr);
298 USETW(req.wIndex, 0x0);
299 USETW(req.wLength, 0);
300
301 while (retries) {
302 err = usbd_do_request(sc->sc_udev, &req, NULL);
303 if (!err)
304 break;
305 retries--;
306 }
307 }
308
309
310 void
311 uipaq_break(struct uipaq_softc* sc, int onoff)
312 {
313 usb_device_request_t req;
314 usbd_status err;
315 int retries = 3;
316
317 DPRINTF(("%s: uipaq_break: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
318
319 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
320 req.bRequest = UCDC_SEND_BREAK;
321
322 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
323 USETW(req.wIndex, 0x0);
324 USETW(req.wLength, 0);
325
326 while (retries) {
327 err = usbd_do_request(sc->sc_udev, &req, NULL);
328 if (!err)
329 break;
330 retries--;
331 }
332 }
333
334
335 void
336 uipaq_set(void *addr, int portno, int reg, int onoff)
337 {
338 struct uipaq_softc* sc = addr;
339
340 switch (reg) {
341 case UCOM_SET_DTR:
342 uipaq_dtr(addr, onoff);
343 break;
344 case UCOM_SET_RTS:
345 uipaq_rts(addr, onoff);
346 break;
347 case UCOM_SET_BREAK:
348 uipaq_break(addr, onoff);
349 break;
350 default:
351 aprint_error_dev(sc->sc_dev,
352 "unhandled set request: reg=%x onoff=%x\n", reg, onoff);
353 return;
354 }
355 }
356
357
358 int
359 uipaq_activate(device_t self, enum devact act)
360 {
361 struct uipaq_softc *sc = device_private(self);
362 int rv = 0;
363
364 switch (act) {
365 case DVACT_ACTIVATE:
366 return (EOPNOTSUPP);
367 break;
368
369 case DVACT_DEACTIVATE:
370 if (sc->sc_subdev != NULL)
371 rv = config_deactivate(sc->sc_subdev);
372 sc->sc_dying = 1;
373 break;
374 }
375 return (rv);
376 }
377
378 void
379 uipaq_childdet(device_t self, device_t child)
380 {
381 struct uipaq_softc *sc = device_private(self);
382
383 KASSERT(sc->sc_subdev == child);
384 sc->sc_subdev = NULL;
385 }
386
387 int
388 uipaq_detach(device_t self, int flags)
389 {
390 struct uipaq_softc *sc = device_private(self);
391 int rv = 0;
392
393 DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
394 sc->sc_dying = 1;
395 if (sc->sc_subdev != NULL)
396 rv |= config_detach(sc->sc_subdev, flags);
397
398 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
399 USBDEV(sc->sc_dev));
400
401 return (rv);
402 }
403
404