1 /* $OpenBSD: uipaq.c,v 1.29 2024/05/23 03:21:09 jsg 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * iPAQ driver
35 *
36 * 19 July 2003: Incorporated changes suggested by Sam Lawrance from
37 * the uppc module
38 *
39 *
40 * Contact isis@cs.umd.edu if you have any questions/comments about this driver
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/tty.h>
47
48 #include <dev/usb/usb.h>
49
50 #include <dev/usb/usbcdc.h> /*UCDC_* stuff */
51
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdevs.h>
54
55 #include <dev/usb/ucomvar.h>
56
57 #ifdef UIPAQ_DEBUG
58 #define DPRINTF(x) if (uipaqdebug) printf x
59 #define DPRINTFN(n,x) if (uipaqdebug>(n)) printf x
60 int uipaqdebug = 0;
61 #else
62 #define DPRINTF(x)
63 #define DPRINTFN(n,x)
64 #endif
65
66 #define UIPAQ_CONFIG_NO 1
67 #define UIPAQ_IFACE_INDEX 0
68
69 #define UIPAQIBUFSIZE 1024
70 #define UIPAQOBUFSIZE 1024
71
72 struct uipaq_softc {
73 struct device sc_dev; /* base device */
74 struct usbd_device *sc_udev; /* device */
75 struct usbd_interface *sc_iface; /* interface */
76
77 struct device *sc_subdev; /* ucom uses that */
78 u_int16_t sc_lcr; /* state for DTR/RTS */
79
80 u_int16_t sc_flags;
81 };
82
83 /* Callback routines */
84 void uipaq_set(void *, int, int, int);
85
86
87 /* Support routines. */
88 /* based on uppc module by Sam Lawrance */
89 void uipaq_dtr(struct uipaq_softc *sc, int onoff);
90 void uipaq_rts(struct uipaq_softc *sc, int onoff);
91 void uipaq_break(struct uipaq_softc* sc, int onoff);
92
93
94 const struct ucom_methods uipaq_methods = {
95 NULL,
96 uipaq_set,
97 NULL,
98 NULL,
99 NULL, /*open*/
100 NULL, /*close*/
101 NULL,
102 NULL
103 };
104
105 struct uipaq_type {
106 struct usb_devno uv_dev;
107 u_int16_t uv_flags;
108 };
109
110 static const struct uipaq_type uipaq_devs[] = {
111 {{ USB_VENDOR_ASUS, USB_PRODUCT_ASUS_MYPAL_A730} , 0},
112 {{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0},
113 {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0},
114 {{ USB_VENDOR_HTC, USB_PRODUCT_HTC_SMARTPHONE }, 0},
115 {{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 },
116 {{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0},
117 {{ USB_VENDOR_HTC, USB_PRODUCT_HTC_PPC6700MODEM }, 0}
118 };
119
120 #define uipaq_lookup(v, p) ((struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
121
122 int uipaq_match(struct device *, void *, void *);
123 void uipaq_attach(struct device *, struct device *, void *);
124 int uipaq_detach(struct device *, int);
125
126 struct cfdriver uipaq_cd = {
127 NULL, "uipaq", DV_DULL
128 };
129
130 const struct cfattach uipaq_ca = {
131 sizeof(struct uipaq_softc), uipaq_match, uipaq_attach, uipaq_detach
132 };
133
134 int
uipaq_match(struct device * parent,void * match,void * aux)135 uipaq_match(struct device *parent, void *match, void *aux)
136 {
137 struct usb_attach_arg *uaa = aux;
138
139 if (uaa->iface == NULL || uaa->configno != UIPAQ_CONFIG_NO)
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 void
uipaq_attach(struct device * parent,struct device * self,void * aux)150 uipaq_attach(struct device *parent, struct device *self, void *aux)
151 {
152 struct uipaq_softc *sc = (struct uipaq_softc *)self;
153 struct usb_attach_arg *uaa = aux;
154 struct usbd_device *dev = uaa->device;
155 struct usbd_interface *iface;
156 usb_interface_descriptor_t *id;
157 usb_endpoint_descriptor_t *ed;
158 char *devname = sc->sc_dev.dv_xname;
159 int i;
160 usbd_status err;
161 struct ucom_attach_args uca;
162
163 DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
164
165 err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
166 if (err) {
167 printf(": failed to get interface, err=%s\n",
168 usbd_errstr(err));
169 goto bad;
170 }
171
172 sc->sc_flags = uipaq_lookup(uaa->vendor, uaa->product)->uv_flags;
173
174 id = usbd_get_interface_descriptor(iface);
175
176 sc->sc_udev = dev;
177 sc->sc_iface = iface;
178
179 uca.ibufsize = UIPAQIBUFSIZE;
180 uca.obufsize = UIPAQOBUFSIZE;
181 uca.ibufsizepad = UIPAQIBUFSIZE;
182 uca.opkthdrlen = 0;
183 uca.device = dev;
184 uca.iface = iface;
185 uca.methods = &uipaq_methods;
186 uca.arg = sc;
187 uca.portno = UCOM_UNK_PORTNO;
188 uca.info = "Generic";
189
190 /* err = uipaq_init(sc);
191 if (err) {
192 printf("%s: init failed, %s\n", sc->sc_dev.dv_xname,
193 usbd_errstr(err));
194 goto bad;
195 }*/
196
197 uca.bulkin = uca.bulkout = -1;
198 for (i=0; i<id->bNumEndpoints; i++) {
199 ed = usbd_interface2endpoint_descriptor(iface, i);
200 if (ed == NULL) {
201 printf("%s: no endpoint descriptor for %d\n",
202 devname,i);
203 goto bad;
204 }
205 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
206 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
207 uca.bulkin = ed->bEndpointAddress;
208 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
209 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
210 uca.bulkout = ed->bEndpointAddress;
211 }
212 }
213 if (uca.bulkin != -1 && uca.bulkout != -1)
214 sc->sc_subdev = config_found_sm(self, &uca,
215 ucomprint, ucomsubmatch);
216 else
217 printf("%s: no proper endpoints found (%d,%d) \n",
218 devname, uca.bulkin, uca.bulkout);
219
220 return;
221
222 bad:
223 DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
224 usbd_deactivate(sc->sc_udev);
225 }
226
227
228 void
uipaq_dtr(struct uipaq_softc * sc,int onoff)229 uipaq_dtr(struct uipaq_softc* sc, int onoff)
230 {
231 usb_device_request_t req;
232 usbd_status err;
233 int retries = 3;
234
235 DPRINTF(("%s: uipaq_dtr: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
236
237 /* Avoid sending unnecessary requests */
238 if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
239 return;
240 if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
241 return;
242
243 /* Other parameters depend on reg */
244 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
245 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
246 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
247 USETW(req.wValue, sc->sc_lcr);
248 USETW(req.wIndex, 0x0);
249 USETW(req.wLength, 0);
250
251 /* Fire off the request a few times if necessary */
252 while (retries) {
253 err = usbd_do_request(sc->sc_udev, &req, NULL);
254 if (!err)
255 break;
256 retries--;
257 }
258 }
259
260
261 void
uipaq_rts(struct uipaq_softc * sc,int onoff)262 uipaq_rts(struct uipaq_softc* sc, int onoff)
263 {
264 usb_device_request_t req;
265 usbd_status err;
266 int retries = 3;
267
268 DPRINTF(("%s: uipaq_rts: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
269
270 /* Avoid sending unnecessary requests */
271 if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
272 if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
273
274 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
275 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
276 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
277 USETW(req.wValue, sc->sc_lcr);
278 USETW(req.wIndex, 0x0);
279 USETW(req.wLength, 0);
280
281 while (retries) {
282 err = usbd_do_request(sc->sc_udev, &req, NULL);
283 if (!err)
284 break;
285 retries--;
286 }
287 }
288
289
290 void
uipaq_break(struct uipaq_softc * sc,int onoff)291 uipaq_break(struct uipaq_softc* sc, int onoff)
292 {
293 usb_device_request_t req;
294 usbd_status err;
295 int retries = 3;
296
297 DPRINTF(("%s: uipaq_break: onoff=%x\n", sc->sc_dev.dv_xname, onoff));
298
299 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
300 req.bRequest = UCDC_SEND_BREAK;
301
302 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
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
uipaq_set(void * addr,int portno,int reg,int onoff)316 uipaq_set(void *addr, int portno, int reg, int onoff)
317 {
318 struct uipaq_softc* sc = addr;
319
320 switch (reg) {
321 case UCOM_SET_DTR:
322 uipaq_dtr(addr, onoff);
323 break;
324 case UCOM_SET_RTS:
325 uipaq_rts(addr, onoff);
326 break;
327 case UCOM_SET_BREAK:
328 uipaq_break(addr, onoff);
329 break;
330 default:
331 printf("%s: unhandled set request: reg=%x onoff=%x\n",
332 sc->sc_dev.dv_xname, reg, onoff);
333 return;
334 }
335 }
336
337
338 int
uipaq_detach(struct device * self,int flags)339 uipaq_detach(struct device *self, int flags)
340 {
341 struct uipaq_softc *sc = (struct uipaq_softc *)self;
342 int rv = 0;
343
344 DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
345 if (sc->sc_subdev != NULL) {
346 rv |= config_detach(sc->sc_subdev, flags);
347 sc->sc_subdev = NULL;
348 }
349
350 return (rv);
351 }
352
353