uchcom.c revision 1.1.6.2 1 1.1.6.2 joerg /* $NetBSD: uchcom.c,v 1.1.6.2 2007/10/02 18:28:42 joerg Exp $ */
2 1.1.6.2 joerg
3 1.1.6.2 joerg /*
4 1.1.6.2 joerg * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1.6.2 joerg * All rights reserved.
6 1.1.6.2 joerg *
7 1.1.6.2 joerg * This code is derived from software contributed to The NetBSD Foundation
8 1.1.6.2 joerg * by Takuya SHIOZAKI (tshiozak (at) netbsd.org).
9 1.1.6.2 joerg *
10 1.1.6.2 joerg * Redistribution and use in source and binary forms, with or without
11 1.1.6.2 joerg * modification, are permitted provided that the following conditions
12 1.1.6.2 joerg * are met:
13 1.1.6.2 joerg * 1. Redistributions of source code must retain the above copyright
14 1.1.6.2 joerg * notice, this list of conditions and the following disclaimer.
15 1.1.6.2 joerg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.6.2 joerg * notice, this list of conditions and the following disclaimer in the
17 1.1.6.2 joerg * documentation and/or other materials provided with the distribution.
18 1.1.6.2 joerg * 3. All advertising materials mentioning features or use of this software
19 1.1.6.2 joerg * must display the following acknowledgement:
20 1.1.6.2 joerg * This product includes software developed by the NetBSD
21 1.1.6.2 joerg * Foundation, Inc. and its contributors.
22 1.1.6.2 joerg * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.6.2 joerg * contributors may be used to endorse or promote products derived
24 1.1.6.2 joerg * from this software without specific prior written permission.
25 1.1.6.2 joerg *
26 1.1.6.2 joerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.6.2 joerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.6.2 joerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.6.2 joerg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.6.2 joerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.6.2 joerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.6.2 joerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.6.2 joerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.6.2 joerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.6.2 joerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.6.2 joerg * POSSIBILITY OF SUCH DAMAGE.
37 1.1.6.2 joerg */
38 1.1.6.2 joerg
39 1.1.6.2 joerg #include <sys/cdefs.h>
40 1.1.6.2 joerg __KERNEL_RCSID(0, "$NetBSD: uchcom.c,v 1.1.6.2 2007/10/02 18:28:42 joerg Exp $");
41 1.1.6.2 joerg
42 1.1.6.2 joerg /*
43 1.1.6.2 joerg * driver for WinChipHead CH341/340, the worst USB-serial chip in the world.
44 1.1.6.2 joerg */
45 1.1.6.2 joerg
46 1.1.6.2 joerg #include <sys/param.h>
47 1.1.6.2 joerg #include <sys/systm.h>
48 1.1.6.2 joerg #include <sys/kernel.h>
49 1.1.6.2 joerg #include <sys/malloc.h>
50 1.1.6.2 joerg #include <sys/ioctl.h>
51 1.1.6.2 joerg #include <sys/conf.h>
52 1.1.6.2 joerg #include <sys/tty.h>
53 1.1.6.2 joerg #include <sys/file.h>
54 1.1.6.2 joerg #include <sys/select.h>
55 1.1.6.2 joerg #include <sys/proc.h>
56 1.1.6.2 joerg #include <sys/device.h>
57 1.1.6.2 joerg #include <sys/poll.h>
58 1.1.6.2 joerg #include <sys/workqueue.h>
59 1.1.6.2 joerg
60 1.1.6.2 joerg #include <dev/usb/usb.h>
61 1.1.6.2 joerg #include <dev/usb/usbcdc.h>
62 1.1.6.2 joerg
63 1.1.6.2 joerg #include <dev/usb/usbdi.h>
64 1.1.6.2 joerg #include <dev/usb/usbdi_util.h>
65 1.1.6.2 joerg #include <dev/usb/usbdevs.h>
66 1.1.6.2 joerg #include <dev/usb/usb_quirks.h>
67 1.1.6.2 joerg
68 1.1.6.2 joerg #include <dev/usb/ucomvar.h>
69 1.1.6.2 joerg
70 1.1.6.2 joerg #ifdef UCHCOM_DEBUG
71 1.1.6.2 joerg #define DPRINTFN(n, x) if (uchcomdebug > (n)) logprintf x
72 1.1.6.2 joerg int uchcomdebug = 0;
73 1.1.6.2 joerg #else
74 1.1.6.2 joerg #define DPRINTFN(n, x)
75 1.1.6.2 joerg #endif
76 1.1.6.2 joerg #define DPRINTF(x) DPRINTFN(0, x)
77 1.1.6.2 joerg
78 1.1.6.2 joerg #define UCHCOM_IFACE_INDEX 0
79 1.1.6.2 joerg #define UCHCOM_CONFIG_INDEX 0
80 1.1.6.2 joerg
81 1.1.6.2 joerg #define UCHCOM_REV_CH340 0x0250
82 1.1.6.2 joerg #define UCHCOM_INPUT_BUF_SIZE 8
83 1.1.6.2 joerg
84 1.1.6.2 joerg #define UCHCOM_REQ_GET_VERSION 0x5F
85 1.1.6.2 joerg #define UCHCOM_REQ_READ_REG 0x95
86 1.1.6.2 joerg #define UCHCOM_REQ_WRITE_REG 0x9A
87 1.1.6.2 joerg #define UCHCOM_REQ_RESET 0xA1
88 1.1.6.2 joerg #define UCHCOM_REQ_SET_DTRRTS 0xA4
89 1.1.6.2 joerg
90 1.1.6.2 joerg #define UCHCOM_REG_STAT1 0x06
91 1.1.6.2 joerg #define UCHCOM_REG_STAT2 0x07
92 1.1.6.2 joerg #define UCHCOM_REG_BPS_PRE 0x12
93 1.1.6.2 joerg #define UCHCOM_REG_BPS_DIV 0x13
94 1.1.6.2 joerg #define UCHCOM_REG_BPS_MOD 0x14
95 1.1.6.2 joerg #define UCHCOM_REG_BPS_PAD 0x0F
96 1.1.6.2 joerg #define UCHCOM_REG_BREAK1 0x05
97 1.1.6.2 joerg #define UCHCOM_REG_BREAK2 0x18
98 1.1.6.2 joerg #define UCHCOM_REG_LCR1 0x18
99 1.1.6.2 joerg #define UCHCOM_REG_LCR2 0x25
100 1.1.6.2 joerg
101 1.1.6.2 joerg #define UCHCOM_VER_20 0x20
102 1.1.6.2 joerg
103 1.1.6.2 joerg #define UCHCOM_BASE_UNKNOWN 0
104 1.1.6.2 joerg #define UCHCOM_BPS_MOD_BASE 20000000
105 1.1.6.2 joerg #define UCHCOM_BPS_MOD_BASE_OFS 1100
106 1.1.6.2 joerg
107 1.1.6.2 joerg #define UCHCOM_DTR_MASK 0x20
108 1.1.6.2 joerg #define UCHCOM_RTS_MASK 0x40
109 1.1.6.2 joerg
110 1.1.6.2 joerg #define UCHCOM_BRK1_MASK 0x01
111 1.1.6.2 joerg #define UCHCOM_BRK2_MASK 0x40
112 1.1.6.2 joerg
113 1.1.6.2 joerg #define UCHCOM_LCR1_MASK 0xAF
114 1.1.6.2 joerg #define UCHCOM_LCR2_MASK 0x07
115 1.1.6.2 joerg #define UCHCOM_LCR1_PARENB 0x80
116 1.1.6.2 joerg #define UCHCOM_LCR2_PAREVEN 0x07
117 1.1.6.2 joerg #define UCHCOM_LCR2_PARODD 0x06
118 1.1.6.2 joerg #define UCHCOM_LCR2_PARMARK 0x05
119 1.1.6.2 joerg #define UCHCOM_LCR2_PARSPACE 0x04
120 1.1.6.2 joerg
121 1.1.6.2 joerg #define UCHCOM_INTR_STAT1 0x02
122 1.1.6.2 joerg #define UCHCOM_INTR_STAT2 0x03
123 1.1.6.2 joerg #define UCHCOM_INTR_LEAST 4
124 1.1.6.2 joerg
125 1.1.6.2 joerg #define UCHCOMIBUFSIZE 256
126 1.1.6.2 joerg #define UCHCOMOBUFSIZE 256
127 1.1.6.2 joerg
128 1.1.6.2 joerg struct uchcom_softc
129 1.1.6.2 joerg {
130 1.1.6.2 joerg USBBASEDEVICE sc_dev;
131 1.1.6.2 joerg usbd_device_handle sc_udev;
132 1.1.6.2 joerg device_ptr_t sc_subdev;
133 1.1.6.2 joerg usbd_interface_handle sc_iface;
134 1.1.6.2 joerg int sc_dying;
135 1.1.6.2 joerg /* */
136 1.1.6.2 joerg int sc_intr_endpoint;
137 1.1.6.2 joerg int sc_intr_size;
138 1.1.6.2 joerg usbd_pipe_handle sc_intr_pipe;
139 1.1.6.2 joerg u_char *sc_intr_buf;
140 1.1.6.2 joerg /* */
141 1.1.6.2 joerg uint8_t sc_version;
142 1.1.6.2 joerg int sc_dtr;
143 1.1.6.2 joerg int sc_rts;
144 1.1.6.2 joerg u_char sc_lsr;
145 1.1.6.2 joerg u_char sc_msr;
146 1.1.6.2 joerg int sc_lcr1;
147 1.1.6.2 joerg int sc_lcr2;
148 1.1.6.2 joerg };
149 1.1.6.2 joerg
150 1.1.6.2 joerg struct uchcom_endpoints
151 1.1.6.2 joerg {
152 1.1.6.2 joerg int ep_bulkin;
153 1.1.6.2 joerg int ep_bulkout;
154 1.1.6.2 joerg int ep_intr;
155 1.1.6.2 joerg int ep_intr_size;
156 1.1.6.2 joerg };
157 1.1.6.2 joerg
158 1.1.6.2 joerg struct uchcom_divider
159 1.1.6.2 joerg {
160 1.1.6.2 joerg uint8_t dv_prescaler;
161 1.1.6.2 joerg uint8_t dv_div;
162 1.1.6.2 joerg uint8_t dv_mod;
163 1.1.6.2 joerg };
164 1.1.6.2 joerg
165 1.1.6.2 joerg struct uchcom_divider_record
166 1.1.6.2 joerg {
167 1.1.6.2 joerg uint32_t dvr_high;
168 1.1.6.2 joerg uint32_t dvr_low;
169 1.1.6.2 joerg uint32_t dvr_base_clock;
170 1.1.6.2 joerg struct uchcom_divider dvr_divider;
171 1.1.6.2 joerg };
172 1.1.6.2 joerg
173 1.1.6.2 joerg static const struct uchcom_divider_record dividers[] =
174 1.1.6.2 joerg {
175 1.1.6.2 joerg { 307200, 307200, UCHCOM_BASE_UNKNOWN, { 7, 0xD9, 0 } },
176 1.1.6.2 joerg { 921600, 921600, UCHCOM_BASE_UNKNOWN, { 7, 0xF3, 0 } },
177 1.1.6.2 joerg { 2999999, 23530, 6000000, { 3, 0, 0 } },
178 1.1.6.2 joerg { 23529, 2942, 750000, { 2, 0, 0 } },
179 1.1.6.2 joerg { 2941, 368, 93750, { 1, 0, 0 } },
180 1.1.6.2 joerg { 367, 1, 11719, { 0, 0, 0 } },
181 1.1.6.2 joerg };
182 1.1.6.2 joerg #define NUM_DIVIDERS (sizeof (dividers) / sizeof (dividers[0]))
183 1.1.6.2 joerg
184 1.1.6.2 joerg static const struct usb_devno uchcom_devs[] = {
185 1.1.6.2 joerg { USB_VENDOR_WINCHIPHEAD, USB_PRODUCT_WINCHIPHEAD_CH341SER },
186 1.1.6.2 joerg };
187 1.1.6.2 joerg #define uchcom_lookup(v, p) usb_lookup(uchcom_devs, v, p)
188 1.1.6.2 joerg
189 1.1.6.2 joerg Static void uchcom_get_status(void *, int, u_char *, u_char *);
190 1.1.6.2 joerg Static void uchcom_set(void *, int, int, int);
191 1.1.6.2 joerg Static int uchcom_param(void *, int, struct termios *);
192 1.1.6.2 joerg Static int uchcom_open(void *, int);
193 1.1.6.2 joerg Static void uchcom_close(void *, int);
194 1.1.6.2 joerg Static void uchcom_intr(usbd_xfer_handle, usbd_private_handle,
195 1.1.6.2 joerg usbd_status);
196 1.1.6.2 joerg
197 1.1.6.2 joerg static int set_config(struct uchcom_softc *);
198 1.1.6.2 joerg static int find_ifaces(struct uchcom_softc *, usbd_interface_handle *);
199 1.1.6.2 joerg static int find_endpoints(struct uchcom_softc *,
200 1.1.6.2 joerg struct uchcom_endpoints *);
201 1.1.6.2 joerg static void close_intr_pipe(struct uchcom_softc *);
202 1.1.6.2 joerg
203 1.1.6.2 joerg
204 1.1.6.2 joerg struct ucom_methods uchcom_methods = {
205 1.1.6.2 joerg .ucom_get_status = uchcom_get_status,
206 1.1.6.2 joerg .ucom_set = uchcom_set,
207 1.1.6.2 joerg .ucom_param = uchcom_param,
208 1.1.6.2 joerg .ucom_ioctl = NULL,
209 1.1.6.2 joerg .ucom_open = uchcom_open,
210 1.1.6.2 joerg .ucom_close = uchcom_close,
211 1.1.6.2 joerg .ucom_read = NULL,
212 1.1.6.2 joerg .ucom_write = NULL,
213 1.1.6.2 joerg };
214 1.1.6.2 joerg
215 1.1.6.2 joerg USB_DECLARE_DRIVER(uchcom);
216 1.1.6.2 joerg
217 1.1.6.2 joerg
218 1.1.6.2 joerg /* ----------------------------------------------------------------------
219 1.1.6.2 joerg * driver entry points
220 1.1.6.2 joerg */
221 1.1.6.2 joerg
222 1.1.6.2 joerg USB_MATCH(uchcom)
223 1.1.6.2 joerg {
224 1.1.6.2 joerg USB_MATCH_START(uchcom, uaa);
225 1.1.6.2 joerg
226 1.1.6.2 joerg return (uchcom_lookup(uaa->vendor, uaa->product) != NULL ?
227 1.1.6.2 joerg UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
228 1.1.6.2 joerg }
229 1.1.6.2 joerg
230 1.1.6.2 joerg USB_ATTACH(uchcom)
231 1.1.6.2 joerg {
232 1.1.6.2 joerg USB_ATTACH_START(uchcom, sc, uaa);
233 1.1.6.2 joerg usbd_device_handle dev = uaa->device;
234 1.1.6.2 joerg char *devinfop;
235 1.1.6.2 joerg char *devname = USBDEVNAME(sc->sc_dev);
236 1.1.6.2 joerg struct uchcom_endpoints endpoints;
237 1.1.6.2 joerg struct ucom_attach_args uca;
238 1.1.6.2 joerg
239 1.1.6.2 joerg devinfop = usbd_devinfo_alloc(dev, 0);
240 1.1.6.2 joerg USB_ATTACH_SETUP;
241 1.1.6.2 joerg printf("%s: %s\n", devname, devinfop);
242 1.1.6.2 joerg usbd_devinfo_free(devinfop);
243 1.1.6.2 joerg
244 1.1.6.2 joerg sc->sc_udev = dev;
245 1.1.6.2 joerg sc->sc_dying = 0;
246 1.1.6.2 joerg sc->sc_dtr = sc->sc_rts = -1;
247 1.1.6.2 joerg sc->sc_lsr = sc->sc_msr = 0;
248 1.1.6.2 joerg
249 1.1.6.2 joerg DPRINTF(("\n\nuchcom attach: sc=%p\n", sc));
250 1.1.6.2 joerg
251 1.1.6.2 joerg if (set_config(sc))
252 1.1.6.2 joerg goto failed;
253 1.1.6.2 joerg
254 1.1.6.2 joerg switch (uaa->release) {
255 1.1.6.2 joerg case UCHCOM_REV_CH340:
256 1.1.6.2 joerg printf("%s: CH340 detected\n", devname);
257 1.1.6.2 joerg break;
258 1.1.6.2 joerg default:
259 1.1.6.2 joerg printf("%s: CH341 detected\n", devname);
260 1.1.6.2 joerg break;
261 1.1.6.2 joerg }
262 1.1.6.2 joerg
263 1.1.6.2 joerg if (find_ifaces(sc, &sc->sc_iface))
264 1.1.6.2 joerg goto failed;
265 1.1.6.2 joerg
266 1.1.6.2 joerg if (find_endpoints(sc, &endpoints))
267 1.1.6.2 joerg goto failed;
268 1.1.6.2 joerg
269 1.1.6.2 joerg sc->sc_intr_endpoint = endpoints.ep_intr;
270 1.1.6.2 joerg sc->sc_intr_size = endpoints.ep_intr_size;
271 1.1.6.2 joerg
272 1.1.6.2 joerg /* setup ucom layer */
273 1.1.6.2 joerg uca.portno = UCOM_UNK_PORTNO;
274 1.1.6.2 joerg uca.bulkin = endpoints.ep_bulkin;
275 1.1.6.2 joerg uca.bulkout = endpoints.ep_bulkout;
276 1.1.6.2 joerg uca.ibufsize = UCHCOMIBUFSIZE;
277 1.1.6.2 joerg uca.obufsize = UCHCOMOBUFSIZE;
278 1.1.6.2 joerg uca.ibufsizepad = UCHCOMIBUFSIZE;
279 1.1.6.2 joerg uca.opkthdrlen = 0;
280 1.1.6.2 joerg uca.device = dev;
281 1.1.6.2 joerg uca.iface = sc->sc_iface;
282 1.1.6.2 joerg uca.methods = &uchcom_methods;
283 1.1.6.2 joerg uca.arg = sc;
284 1.1.6.2 joerg uca.info = NULL;
285 1.1.6.2 joerg
286 1.1.6.2 joerg usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
287 1.1.6.2 joerg USBDEV(sc->sc_dev));
288 1.1.6.2 joerg
289 1.1.6.2 joerg sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
290 1.1.6.2 joerg ucomprint, ucomsubmatch);
291 1.1.6.2 joerg
292 1.1.6.2 joerg USB_ATTACH_SUCCESS_RETURN;
293 1.1.6.2 joerg
294 1.1.6.2 joerg failed:
295 1.1.6.2 joerg sc->sc_dying = 1;
296 1.1.6.2 joerg USB_ATTACH_ERROR_RETURN;
297 1.1.6.2 joerg }
298 1.1.6.2 joerg
299 1.1.6.2 joerg USB_DETACH(uchcom)
300 1.1.6.2 joerg {
301 1.1.6.2 joerg USB_DETACH_START(uchcom, sc);
302 1.1.6.2 joerg int rv = 0;
303 1.1.6.2 joerg
304 1.1.6.2 joerg DPRINTF(("uchcom_detach: sc=%p flags=%d\n", sc, flags));
305 1.1.6.2 joerg
306 1.1.6.2 joerg close_intr_pipe(sc);
307 1.1.6.2 joerg
308 1.1.6.2 joerg sc->sc_dying = 1;
309 1.1.6.2 joerg
310 1.1.6.2 joerg if (sc->sc_subdev != NULL) {
311 1.1.6.2 joerg rv = config_detach(sc->sc_subdev, flags);
312 1.1.6.2 joerg sc->sc_subdev = NULL;
313 1.1.6.2 joerg }
314 1.1.6.2 joerg
315 1.1.6.2 joerg usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
316 1.1.6.2 joerg USBDEV(sc->sc_dev));
317 1.1.6.2 joerg
318 1.1.6.2 joerg return rv;
319 1.1.6.2 joerg }
320 1.1.6.2 joerg
321 1.1.6.2 joerg int
322 1.1.6.2 joerg uchcom_activate(device_ptr_t self, enum devact act)
323 1.1.6.2 joerg {
324 1.1.6.2 joerg struct uchcom_softc *sc = (struct uchcom_softc *)self;
325 1.1.6.2 joerg int rv = 0;
326 1.1.6.2 joerg
327 1.1.6.2 joerg switch (act) {
328 1.1.6.2 joerg case DVACT_ACTIVATE:
329 1.1.6.2 joerg rv = EOPNOTSUPP;
330 1.1.6.2 joerg break;
331 1.1.6.2 joerg case DVACT_DEACTIVATE:
332 1.1.6.2 joerg close_intr_pipe(sc);
333 1.1.6.2 joerg sc->sc_dying = 1;
334 1.1.6.2 joerg if (sc->sc_subdev != NULL)
335 1.1.6.2 joerg rv = config_deactivate(sc->sc_subdev);
336 1.1.6.2 joerg break;
337 1.1.6.2 joerg }
338 1.1.6.2 joerg return rv;
339 1.1.6.2 joerg }
340 1.1.6.2 joerg
341 1.1.6.2 joerg static int
342 1.1.6.2 joerg set_config(struct uchcom_softc *sc)
343 1.1.6.2 joerg {
344 1.1.6.2 joerg usbd_status err;
345 1.1.6.2 joerg
346 1.1.6.2 joerg err = usbd_set_config_index(sc->sc_udev, UCHCOM_CONFIG_INDEX, 1);
347 1.1.6.2 joerg if (err) {
348 1.1.6.2 joerg printf("%s: failed to set configuration: %s\n",
349 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
350 1.1.6.2 joerg return -1;
351 1.1.6.2 joerg }
352 1.1.6.2 joerg
353 1.1.6.2 joerg return 0;
354 1.1.6.2 joerg }
355 1.1.6.2 joerg
356 1.1.6.2 joerg static int
357 1.1.6.2 joerg find_ifaces(struct uchcom_softc *sc, usbd_interface_handle *riface)
358 1.1.6.2 joerg {
359 1.1.6.2 joerg usbd_status err;
360 1.1.6.2 joerg
361 1.1.6.2 joerg err = usbd_device2interface_handle(sc->sc_udev, UCHCOM_IFACE_INDEX,
362 1.1.6.2 joerg riface);
363 1.1.6.2 joerg if (err) {
364 1.1.6.2 joerg printf("\n%s: failed to get interface: %s\n",
365 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
366 1.1.6.2 joerg return -1;
367 1.1.6.2 joerg }
368 1.1.6.2 joerg
369 1.1.6.2 joerg return 0;
370 1.1.6.2 joerg }
371 1.1.6.2 joerg
372 1.1.6.2 joerg static int
373 1.1.6.2 joerg find_endpoints(struct uchcom_softc *sc, struct uchcom_endpoints *endpoints)
374 1.1.6.2 joerg {
375 1.1.6.2 joerg int i, bin=-1, bout=-1, intr=-1, isize=0;
376 1.1.6.2 joerg usb_interface_descriptor_t *id;
377 1.1.6.2 joerg usb_endpoint_descriptor_t *ed;
378 1.1.6.2 joerg
379 1.1.6.2 joerg id = usbd_get_interface_descriptor(sc->sc_iface);
380 1.1.6.2 joerg
381 1.1.6.2 joerg for (i = 0; i < id->bNumEndpoints; i++) {
382 1.1.6.2 joerg ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
383 1.1.6.2 joerg if (ed == NULL) {
384 1.1.6.2 joerg printf("%s: no endpoint descriptor for %d\n",
385 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), i);
386 1.1.6.2 joerg return -1;
387 1.1.6.2 joerg }
388 1.1.6.2 joerg
389 1.1.6.2 joerg if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
390 1.1.6.2 joerg UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
391 1.1.6.2 joerg intr = ed->bEndpointAddress;
392 1.1.6.2 joerg isize = UGETW(ed->wMaxPacketSize);
393 1.1.6.2 joerg } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
394 1.1.6.2 joerg UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
395 1.1.6.2 joerg bin = ed->bEndpointAddress;
396 1.1.6.2 joerg } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
397 1.1.6.2 joerg UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
398 1.1.6.2 joerg bout = ed->bEndpointAddress;
399 1.1.6.2 joerg }
400 1.1.6.2 joerg }
401 1.1.6.2 joerg
402 1.1.6.2 joerg if (intr == -1 || bin == -1 || bout == -1) {
403 1.1.6.2 joerg if (intr == -1) {
404 1.1.6.2 joerg printf("%s: no interrupt end point\n",
405 1.1.6.2 joerg USBDEVNAME(sc->sc_dev));
406 1.1.6.2 joerg }
407 1.1.6.2 joerg if (bin == -1) {
408 1.1.6.2 joerg printf("%s: no data bulk in end point\n",
409 1.1.6.2 joerg USBDEVNAME(sc->sc_dev));
410 1.1.6.2 joerg }
411 1.1.6.2 joerg if (bout == -1) {
412 1.1.6.2 joerg printf("%s: no data bulk out end point\n",
413 1.1.6.2 joerg USBDEVNAME(sc->sc_dev));
414 1.1.6.2 joerg }
415 1.1.6.2 joerg return -1;
416 1.1.6.2 joerg }
417 1.1.6.2 joerg if (isize < UCHCOM_INTR_LEAST) {
418 1.1.6.2 joerg printf("%s: intr pipe is too short", USBDEVNAME(sc->sc_dev));
419 1.1.6.2 joerg return -1;
420 1.1.6.2 joerg }
421 1.1.6.2 joerg
422 1.1.6.2 joerg DPRINTF(("%s: bulkin=%d, bulkout=%d, intr=%d, isize=%d\n",
423 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), bin, bout, intr, isize));
424 1.1.6.2 joerg
425 1.1.6.2 joerg endpoints->ep_intr = intr;
426 1.1.6.2 joerg endpoints->ep_intr_size = isize;
427 1.1.6.2 joerg endpoints->ep_bulkin = bin;
428 1.1.6.2 joerg endpoints->ep_bulkout = bout;
429 1.1.6.2 joerg
430 1.1.6.2 joerg return 0;
431 1.1.6.2 joerg }
432 1.1.6.2 joerg
433 1.1.6.2 joerg
434 1.1.6.2 joerg /* ----------------------------------------------------------------------
435 1.1.6.2 joerg * low level i/o
436 1.1.6.2 joerg */
437 1.1.6.2 joerg
438 1.1.6.2 joerg static __inline usbd_status
439 1.1.6.2 joerg generic_control_out(struct uchcom_softc *sc, uint8_t reqno,
440 1.1.6.2 joerg uint16_t value, uint16_t index)
441 1.1.6.2 joerg {
442 1.1.6.2 joerg usb_device_request_t req;
443 1.1.6.2 joerg
444 1.1.6.2 joerg req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
445 1.1.6.2 joerg req.bRequest = reqno;
446 1.1.6.2 joerg USETW(req.wValue, value);
447 1.1.6.2 joerg USETW(req.wIndex, index);
448 1.1.6.2 joerg USETW(req.wLength, 0);
449 1.1.6.2 joerg
450 1.1.6.2 joerg return usbd_do_request(sc->sc_udev, &req, 0);
451 1.1.6.2 joerg }
452 1.1.6.2 joerg
453 1.1.6.2 joerg static __inline usbd_status
454 1.1.6.2 joerg generic_control_in(struct uchcom_softc *sc, uint8_t reqno,
455 1.1.6.2 joerg uint16_t value, uint16_t index, void *buf, int buflen,
456 1.1.6.2 joerg int *actlen)
457 1.1.6.2 joerg {
458 1.1.6.2 joerg usb_device_request_t req;
459 1.1.6.2 joerg
460 1.1.6.2 joerg req.bmRequestType = UT_READ_VENDOR_DEVICE;
461 1.1.6.2 joerg req.bRequest = reqno;
462 1.1.6.2 joerg USETW(req.wValue, value);
463 1.1.6.2 joerg USETW(req.wIndex, index);
464 1.1.6.2 joerg USETW(req.wLength, (uint16_t)buflen);
465 1.1.6.2 joerg
466 1.1.6.2 joerg return usbd_do_request_flags(sc->sc_udev, &req, buf,
467 1.1.6.2 joerg USBD_SHORT_XFER_OK, actlen,
468 1.1.6.2 joerg USBD_DEFAULT_TIMEOUT);
469 1.1.6.2 joerg }
470 1.1.6.2 joerg
471 1.1.6.2 joerg static __inline usbd_status
472 1.1.6.2 joerg write_reg(struct uchcom_softc *sc,
473 1.1.6.2 joerg uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
474 1.1.6.2 joerg {
475 1.1.6.2 joerg DPRINTF(("uchcom: write reg 0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
476 1.1.6.2 joerg (unsigned)reg1, (unsigned)val1,
477 1.1.6.2 joerg (unsigned)reg2, (unsigned)val2));
478 1.1.6.2 joerg return generic_control_out(
479 1.1.6.2 joerg sc, UCHCOM_REQ_WRITE_REG,
480 1.1.6.2 joerg reg1|((uint16_t)reg2<<8), val1|((uint16_t)val2<<8));
481 1.1.6.2 joerg }
482 1.1.6.2 joerg
483 1.1.6.2 joerg static __inline usbd_status
484 1.1.6.2 joerg read_reg(struct uchcom_softc *sc,
485 1.1.6.2 joerg uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
486 1.1.6.2 joerg {
487 1.1.6.2 joerg uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
488 1.1.6.2 joerg usbd_status err;
489 1.1.6.2 joerg int actin;
490 1.1.6.2 joerg
491 1.1.6.2 joerg err = generic_control_in(
492 1.1.6.2 joerg sc, UCHCOM_REQ_READ_REG,
493 1.1.6.2 joerg reg1|((uint16_t)reg2<<8), 0, buf, sizeof buf, &actin);
494 1.1.6.2 joerg if (err)
495 1.1.6.2 joerg return err;
496 1.1.6.2 joerg
497 1.1.6.2 joerg DPRINTF(("uchcom: read reg 0x%02X->0x%02X, 0x%02X->0x%02X\n",
498 1.1.6.2 joerg (unsigned)reg1, (unsigned)buf[0],
499 1.1.6.2 joerg (unsigned)reg2, (unsigned)buf[1]));
500 1.1.6.2 joerg
501 1.1.6.2 joerg if (rval1) *rval1 = buf[0];
502 1.1.6.2 joerg if (rval2) *rval2 = buf[1];
503 1.1.6.2 joerg
504 1.1.6.2 joerg return USBD_NORMAL_COMPLETION;
505 1.1.6.2 joerg }
506 1.1.6.2 joerg
507 1.1.6.2 joerg static __inline usbd_status
508 1.1.6.2 joerg get_version(struct uchcom_softc *sc, uint8_t *rver)
509 1.1.6.2 joerg {
510 1.1.6.2 joerg uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
511 1.1.6.2 joerg usbd_status err;
512 1.1.6.2 joerg int actin;
513 1.1.6.2 joerg
514 1.1.6.2 joerg err = generic_control_in(
515 1.1.6.2 joerg sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof buf, &actin);
516 1.1.6.2 joerg if (err)
517 1.1.6.2 joerg return err;
518 1.1.6.2 joerg
519 1.1.6.2 joerg if (rver) *rver = buf[0];
520 1.1.6.2 joerg
521 1.1.6.2 joerg return USBD_NORMAL_COMPLETION;
522 1.1.6.2 joerg }
523 1.1.6.2 joerg
524 1.1.6.2 joerg static __inline usbd_status
525 1.1.6.2 joerg get_status(struct uchcom_softc *sc, uint8_t *rval)
526 1.1.6.2 joerg {
527 1.1.6.2 joerg return read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
528 1.1.6.2 joerg }
529 1.1.6.2 joerg
530 1.1.6.2 joerg static __inline usbd_status
531 1.1.6.2 joerg set_dtrrts_10(struct uchcom_softc *sc, uint8_t val)
532 1.1.6.2 joerg {
533 1.1.6.2 joerg return write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
534 1.1.6.2 joerg }
535 1.1.6.2 joerg
536 1.1.6.2 joerg static __inline usbd_status
537 1.1.6.2 joerg set_dtrrts_20(struct uchcom_softc *sc, uint8_t val)
538 1.1.6.2 joerg {
539 1.1.6.2 joerg return generic_control_out(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
540 1.1.6.2 joerg }
541 1.1.6.2 joerg
542 1.1.6.2 joerg
543 1.1.6.2 joerg /* ----------------------------------------------------------------------
544 1.1.6.2 joerg * middle layer
545 1.1.6.2 joerg */
546 1.1.6.2 joerg
547 1.1.6.2 joerg static int
548 1.1.6.2 joerg update_version(struct uchcom_softc *sc)
549 1.1.6.2 joerg {
550 1.1.6.2 joerg usbd_status err;
551 1.1.6.2 joerg
552 1.1.6.2 joerg err = get_version(sc, &sc->sc_version);
553 1.1.6.2 joerg if (err) {
554 1.1.6.2 joerg printf("%s: cannot get version: %s\n",
555 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
556 1.1.6.2 joerg return EIO;
557 1.1.6.2 joerg }
558 1.1.6.2 joerg
559 1.1.6.2 joerg return 0;
560 1.1.6.2 joerg }
561 1.1.6.2 joerg
562 1.1.6.2 joerg static void
563 1.1.6.2 joerg convert_status(struct uchcom_softc *sc, uint8_t cur)
564 1.1.6.2 joerg {
565 1.1.6.2 joerg sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
566 1.1.6.2 joerg sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
567 1.1.6.2 joerg
568 1.1.6.2 joerg cur = ~cur & 0x0F;
569 1.1.6.2 joerg sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
570 1.1.6.2 joerg }
571 1.1.6.2 joerg
572 1.1.6.2 joerg static int
573 1.1.6.2 joerg update_status(struct uchcom_softc *sc)
574 1.1.6.2 joerg {
575 1.1.6.2 joerg usbd_status err;
576 1.1.6.2 joerg uint8_t cur;
577 1.1.6.2 joerg
578 1.1.6.2 joerg err = get_status(sc, &cur);
579 1.1.6.2 joerg if (err) {
580 1.1.6.2 joerg printf("%s: cannot update status: %s\n",
581 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
582 1.1.6.2 joerg return EIO;
583 1.1.6.2 joerg }
584 1.1.6.2 joerg convert_status(sc, cur);
585 1.1.6.2 joerg
586 1.1.6.2 joerg return 0;
587 1.1.6.2 joerg }
588 1.1.6.2 joerg
589 1.1.6.2 joerg
590 1.1.6.2 joerg static int
591 1.1.6.2 joerg set_dtrrts(struct uchcom_softc *sc, int dtr, int rts)
592 1.1.6.2 joerg {
593 1.1.6.2 joerg usbd_status err;
594 1.1.6.2 joerg uint8_t val = 0;
595 1.1.6.2 joerg
596 1.1.6.2 joerg if (dtr) val |= UCHCOM_DTR_MASK;
597 1.1.6.2 joerg if (rts) val |= UCHCOM_RTS_MASK;
598 1.1.6.2 joerg
599 1.1.6.2 joerg if (sc->sc_version < UCHCOM_VER_20)
600 1.1.6.2 joerg err = set_dtrrts_10(sc, ~val);
601 1.1.6.2 joerg else
602 1.1.6.2 joerg err = set_dtrrts_20(sc, ~val);
603 1.1.6.2 joerg
604 1.1.6.2 joerg if (err) {
605 1.1.6.2 joerg printf("%s: cannot set DTR/RTS: %s\n",
606 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
607 1.1.6.2 joerg return EIO;
608 1.1.6.2 joerg }
609 1.1.6.2 joerg
610 1.1.6.2 joerg return 0;
611 1.1.6.2 joerg }
612 1.1.6.2 joerg
613 1.1.6.2 joerg static int
614 1.1.6.2 joerg set_break(struct uchcom_softc *sc, int onoff)
615 1.1.6.2 joerg {
616 1.1.6.2 joerg usbd_status err;
617 1.1.6.2 joerg uint8_t brk1, brk2;
618 1.1.6.2 joerg
619 1.1.6.2 joerg err = read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
620 1.1.6.2 joerg if (err)
621 1.1.6.2 joerg return EIO;
622 1.1.6.2 joerg if (onoff) {
623 1.1.6.2 joerg /* on - clear bits */
624 1.1.6.2 joerg brk1 &= ~UCHCOM_BRK1_MASK;
625 1.1.6.2 joerg brk2 &= ~UCHCOM_BRK2_MASK;
626 1.1.6.2 joerg } else {
627 1.1.6.2 joerg /* off - set bits */
628 1.1.6.2 joerg brk1 |= UCHCOM_BRK1_MASK;
629 1.1.6.2 joerg brk2 |= UCHCOM_BRK2_MASK;
630 1.1.6.2 joerg }
631 1.1.6.2 joerg err = write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
632 1.1.6.2 joerg if (err)
633 1.1.6.2 joerg return EIO;
634 1.1.6.2 joerg
635 1.1.6.2 joerg return 0;
636 1.1.6.2 joerg }
637 1.1.6.2 joerg
638 1.1.6.2 joerg static int
639 1.1.6.2 joerg calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
640 1.1.6.2 joerg {
641 1.1.6.2 joerg int i;
642 1.1.6.2 joerg const struct uchcom_divider_record *rp;
643 1.1.6.2 joerg uint32_t div, rem, mod;
644 1.1.6.2 joerg
645 1.1.6.2 joerg /* find record */
646 1.1.6.2 joerg for (i=0; i<NUM_DIVIDERS; i++) {
647 1.1.6.2 joerg if (dividers[i].dvr_high >= rate &&
648 1.1.6.2 joerg dividers[i].dvr_low <= rate) {
649 1.1.6.2 joerg rp = ÷rs[i];
650 1.1.6.2 joerg goto found;
651 1.1.6.2 joerg }
652 1.1.6.2 joerg }
653 1.1.6.2 joerg return -1;
654 1.1.6.2 joerg
655 1.1.6.2 joerg found:
656 1.1.6.2 joerg dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
657 1.1.6.2 joerg if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
658 1.1.6.2 joerg dp->dv_div = rp->dvr_divider.dv_div;
659 1.1.6.2 joerg else {
660 1.1.6.2 joerg div = rp->dvr_base_clock / rate;
661 1.1.6.2 joerg rem = rp->dvr_base_clock % rate;
662 1.1.6.2 joerg if (div==0 || div>=0xFF)
663 1.1.6.2 joerg return -1;
664 1.1.6.2 joerg if ((rem<<1) >= rate)
665 1.1.6.2 joerg div += 1;
666 1.1.6.2 joerg dp->dv_div = (uint8_t)-div;
667 1.1.6.2 joerg }
668 1.1.6.2 joerg
669 1.1.6.2 joerg mod = UCHCOM_BPS_MOD_BASE/rate + UCHCOM_BPS_MOD_BASE_OFS;
670 1.1.6.2 joerg mod = mod + mod/2;
671 1.1.6.2 joerg
672 1.1.6.2 joerg dp->dv_mod = mod / 0x100;
673 1.1.6.2 joerg
674 1.1.6.2 joerg return 0;
675 1.1.6.2 joerg }
676 1.1.6.2 joerg
677 1.1.6.2 joerg static int
678 1.1.6.2 joerg set_dte_rate(struct uchcom_softc *sc, uint32_t rate)
679 1.1.6.2 joerg {
680 1.1.6.2 joerg usbd_status err;
681 1.1.6.2 joerg struct uchcom_divider dv;
682 1.1.6.2 joerg
683 1.1.6.2 joerg if (calc_divider_settings(&dv, rate))
684 1.1.6.2 joerg return EINVAL;
685 1.1.6.2 joerg
686 1.1.6.2 joerg if ((err = write_reg(sc,
687 1.1.6.2 joerg UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
688 1.1.6.2 joerg UCHCOM_REG_BPS_DIV, dv.dv_div)) ||
689 1.1.6.2 joerg (err = write_reg(sc,
690 1.1.6.2 joerg UCHCOM_REG_BPS_MOD, dv.dv_mod,
691 1.1.6.2 joerg UCHCOM_REG_BPS_PAD, 0))) {
692 1.1.6.2 joerg printf("%s: cannot set DTE rate: %s\n",
693 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
694 1.1.6.2 joerg return EIO;
695 1.1.6.2 joerg }
696 1.1.6.2 joerg
697 1.1.6.2 joerg return 0;
698 1.1.6.2 joerg }
699 1.1.6.2 joerg
700 1.1.6.2 joerg static int
701 1.1.6.2 joerg set_line_control(struct uchcom_softc *sc, tcflag_t cflag)
702 1.1.6.2 joerg {
703 1.1.6.2 joerg usbd_status err;
704 1.1.6.2 joerg uint8_t lcr1 = 0, lcr2 = 0;
705 1.1.6.2 joerg
706 1.1.6.2 joerg err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1, UCHCOM_REG_LCR2, &lcr2);
707 1.1.6.2 joerg if (err) {
708 1.1.6.2 joerg printf("%s: cannot get LCR: %s\n",
709 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
710 1.1.6.2 joerg return EIO;
711 1.1.6.2 joerg }
712 1.1.6.2 joerg
713 1.1.6.2 joerg lcr1 &= ~UCHCOM_LCR1_MASK;
714 1.1.6.2 joerg lcr2 &= ~UCHCOM_LCR2_MASK;
715 1.1.6.2 joerg
716 1.1.6.2 joerg /*
717 1.1.6.2 joerg * XXX: it is difficult to handle the line control appropriately:
718 1.1.6.2 joerg * - CS8, !CSTOPB and any parity mode seems ok, but
719 1.1.6.2 joerg * - the chip doesn't have the function to calculate parity
720 1.1.6.2 joerg * in !CS8 mode.
721 1.1.6.2 joerg * - it is unclear that the chip supports CS5,6 mode.
722 1.1.6.2 joerg * - it is unclear how to handle stop bits.
723 1.1.6.2 joerg */
724 1.1.6.2 joerg
725 1.1.6.2 joerg switch (ISSET(cflag, CSIZE)) {
726 1.1.6.2 joerg case CS5:
727 1.1.6.2 joerg case CS6:
728 1.1.6.2 joerg case CS7:
729 1.1.6.2 joerg return EINVAL;
730 1.1.6.2 joerg case CS8:
731 1.1.6.2 joerg break;
732 1.1.6.2 joerg }
733 1.1.6.2 joerg
734 1.1.6.2 joerg if (ISSET(cflag, PARENB)) {
735 1.1.6.2 joerg lcr1 |= UCHCOM_LCR1_PARENB;
736 1.1.6.2 joerg if (ISSET(cflag, PARODD))
737 1.1.6.2 joerg lcr2 |= UCHCOM_LCR2_PARODD;
738 1.1.6.2 joerg else
739 1.1.6.2 joerg lcr2 |= UCHCOM_LCR2_PAREVEN;
740 1.1.6.2 joerg }
741 1.1.6.2 joerg
742 1.1.6.2 joerg err = write_reg(sc, UCHCOM_REG_LCR1, lcr1, UCHCOM_REG_LCR2, lcr2);
743 1.1.6.2 joerg if (err) {
744 1.1.6.2 joerg printf("%s: cannot set LCR: %s\n",
745 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
746 1.1.6.2 joerg return EIO;
747 1.1.6.2 joerg }
748 1.1.6.2 joerg
749 1.1.6.2 joerg return 0;
750 1.1.6.2 joerg }
751 1.1.6.2 joerg
752 1.1.6.2 joerg static int
753 1.1.6.2 joerg clear_chip(struct uchcom_softc *sc)
754 1.1.6.2 joerg {
755 1.1.6.2 joerg usbd_status err;
756 1.1.6.2 joerg
757 1.1.6.2 joerg DPRINTF(("%s: clear\n", USBDEVNAME(sc->sc_dev)));
758 1.1.6.2 joerg err = generic_control_out(sc, UCHCOM_REQ_RESET, 0, 0);
759 1.1.6.2 joerg if (err) {
760 1.1.6.2 joerg printf("%s: cannot clear: %s\n",
761 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
762 1.1.6.2 joerg return EIO;
763 1.1.6.2 joerg }
764 1.1.6.2 joerg
765 1.1.6.2 joerg return 0;
766 1.1.6.2 joerg }
767 1.1.6.2 joerg
768 1.1.6.2 joerg static int
769 1.1.6.2 joerg reset_chip(struct uchcom_softc *sc)
770 1.1.6.2 joerg {
771 1.1.6.2 joerg usbd_status err;
772 1.1.6.2 joerg uint8_t lcr1, lcr2, pre, div, mod;
773 1.1.6.2 joerg uint16_t val=0, idx=0;
774 1.1.6.2 joerg
775 1.1.6.2 joerg err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1, UCHCOM_REG_LCR2, &lcr2);
776 1.1.6.2 joerg if (err)
777 1.1.6.2 joerg goto failed;
778 1.1.6.2 joerg
779 1.1.6.2 joerg err = read_reg(sc, UCHCOM_REG_BPS_PRE, &pre, UCHCOM_REG_BPS_DIV, &div);
780 1.1.6.2 joerg if (err)
781 1.1.6.2 joerg goto failed;
782 1.1.6.2 joerg
783 1.1.6.2 joerg err = read_reg(sc, UCHCOM_REG_BPS_MOD, &mod, UCHCOM_REG_BPS_PAD, NULL);
784 1.1.6.2 joerg if (err)
785 1.1.6.2 joerg goto failed;
786 1.1.6.2 joerg
787 1.1.6.2 joerg val |= (uint16_t)(lcr1&0xF0) << 8;
788 1.1.6.2 joerg val |= 0x01;
789 1.1.6.2 joerg val |= (uint16_t)(lcr2&0x0F) << 8;
790 1.1.6.2 joerg val |= 0x02;
791 1.1.6.2 joerg idx |= pre & 0x07;
792 1.1.6.2 joerg val |= 0x04;
793 1.1.6.2 joerg idx |= (uint16_t)div << 8;
794 1.1.6.2 joerg val |= 0x08;
795 1.1.6.2 joerg idx |= mod & 0xF8;
796 1.1.6.2 joerg val |= 0x10;
797 1.1.6.2 joerg
798 1.1.6.2 joerg DPRINTF(("%s: reset v=0x%04X, i=0x%04X\n",
799 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), val, idx));
800 1.1.6.2 joerg
801 1.1.6.2 joerg err = generic_control_out(sc, UCHCOM_REQ_RESET, val, idx);
802 1.1.6.2 joerg if (err)
803 1.1.6.2 joerg goto failed;
804 1.1.6.2 joerg
805 1.1.6.2 joerg return 0;
806 1.1.6.2 joerg
807 1.1.6.2 joerg failed:
808 1.1.6.2 joerg printf("%s: cannot reset: %s\n",
809 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
810 1.1.6.2 joerg return EIO;
811 1.1.6.2 joerg }
812 1.1.6.2 joerg
813 1.1.6.2 joerg static int
814 1.1.6.2 joerg setup_comm(struct uchcom_softc *sc)
815 1.1.6.2 joerg {
816 1.1.6.2 joerg int ret;
817 1.1.6.2 joerg
818 1.1.6.2 joerg ret = update_version(sc);
819 1.1.6.2 joerg if (ret)
820 1.1.6.2 joerg return ret;
821 1.1.6.2 joerg
822 1.1.6.2 joerg ret = clear_chip(sc);
823 1.1.6.2 joerg if (ret)
824 1.1.6.2 joerg return ret;
825 1.1.6.2 joerg
826 1.1.6.2 joerg ret = set_dte_rate(sc, TTYDEF_SPEED);
827 1.1.6.2 joerg if (ret)
828 1.1.6.2 joerg return ret;
829 1.1.6.2 joerg
830 1.1.6.2 joerg ret = set_line_control(sc, CS8);
831 1.1.6.2 joerg if (ret)
832 1.1.6.2 joerg return ret;
833 1.1.6.2 joerg
834 1.1.6.2 joerg ret = update_status(sc);
835 1.1.6.2 joerg if (ret)
836 1.1.6.2 joerg return ret;
837 1.1.6.2 joerg
838 1.1.6.2 joerg ret = reset_chip(sc);
839 1.1.6.2 joerg if (ret)
840 1.1.6.2 joerg return ret;
841 1.1.6.2 joerg
842 1.1.6.2 joerg ret = set_dte_rate(sc, TTYDEF_SPEED); /* XXX */
843 1.1.6.2 joerg if (ret)
844 1.1.6.2 joerg return ret;
845 1.1.6.2 joerg
846 1.1.6.2 joerg sc->sc_dtr = sc->sc_rts = 1;
847 1.1.6.2 joerg ret = set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
848 1.1.6.2 joerg if (ret)
849 1.1.6.2 joerg return ret;
850 1.1.6.2 joerg
851 1.1.6.2 joerg return 0;
852 1.1.6.2 joerg }
853 1.1.6.2 joerg
854 1.1.6.2 joerg static int
855 1.1.6.2 joerg setup_intr_pipe(struct uchcom_softc *sc)
856 1.1.6.2 joerg {
857 1.1.6.2 joerg usbd_status err;
858 1.1.6.2 joerg
859 1.1.6.2 joerg if (sc->sc_intr_endpoint != -1 && sc->sc_intr_pipe == NULL) {
860 1.1.6.2 joerg sc->sc_intr_buf = malloc(sc->sc_intr_size, M_USBDEV, M_WAITOK);
861 1.1.6.2 joerg err = usbd_open_pipe_intr(sc->sc_iface,
862 1.1.6.2 joerg sc->sc_intr_endpoint,
863 1.1.6.2 joerg USBD_SHORT_XFER_OK,
864 1.1.6.2 joerg &sc->sc_intr_pipe, sc,
865 1.1.6.2 joerg sc->sc_intr_buf,
866 1.1.6.2 joerg sc->sc_intr_size,
867 1.1.6.2 joerg uchcom_intr, USBD_DEFAULT_INTERVAL);
868 1.1.6.2 joerg if (err) {
869 1.1.6.2 joerg printf("%s: cannot open interrupt pipe: %s\n",
870 1.1.6.2 joerg USBDEVNAME(sc->sc_dev),
871 1.1.6.2 joerg usbd_errstr(err));
872 1.1.6.2 joerg return EIO;
873 1.1.6.2 joerg }
874 1.1.6.2 joerg }
875 1.1.6.2 joerg return 0;
876 1.1.6.2 joerg }
877 1.1.6.2 joerg
878 1.1.6.2 joerg static void
879 1.1.6.2 joerg close_intr_pipe(struct uchcom_softc *sc)
880 1.1.6.2 joerg {
881 1.1.6.2 joerg usbd_status err;
882 1.1.6.2 joerg
883 1.1.6.2 joerg if (sc->sc_dying)
884 1.1.6.2 joerg return;
885 1.1.6.2 joerg
886 1.1.6.2 joerg if (sc->sc_intr_pipe != NULL) {
887 1.1.6.2 joerg err = usbd_abort_pipe(sc->sc_intr_pipe);
888 1.1.6.2 joerg if (err)
889 1.1.6.2 joerg printf("%s: abort interrupt pipe failed: %s\n",
890 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
891 1.1.6.2 joerg err = usbd_close_pipe(sc->sc_intr_pipe);
892 1.1.6.2 joerg if (err)
893 1.1.6.2 joerg printf("%s: close interrupt pipe failed: %s\n",
894 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(err));
895 1.1.6.2 joerg free(sc->sc_intr_buf, M_USBDEV);
896 1.1.6.2 joerg sc->sc_intr_pipe = NULL;
897 1.1.6.2 joerg }
898 1.1.6.2 joerg }
899 1.1.6.2 joerg
900 1.1.6.2 joerg
901 1.1.6.2 joerg /* ----------------------------------------------------------------------
902 1.1.6.2 joerg * methods for ucom
903 1.1.6.2 joerg */
904 1.1.6.2 joerg void
905 1.1.6.2 joerg uchcom_get_status(void *arg, int portno, u_char *rlsr, u_char *rmsr)
906 1.1.6.2 joerg {
907 1.1.6.2 joerg struct uchcom_softc *sc = arg;
908 1.1.6.2 joerg
909 1.1.6.2 joerg if (sc->sc_dying)
910 1.1.6.2 joerg return;
911 1.1.6.2 joerg
912 1.1.6.2 joerg *rlsr = sc->sc_lsr;
913 1.1.6.2 joerg *rmsr = sc->sc_msr;
914 1.1.6.2 joerg }
915 1.1.6.2 joerg
916 1.1.6.2 joerg void
917 1.1.6.2 joerg uchcom_set(void *arg, int portno, int reg, int onoff)
918 1.1.6.2 joerg {
919 1.1.6.2 joerg struct uchcom_softc *sc = arg;
920 1.1.6.2 joerg
921 1.1.6.2 joerg if (sc->sc_dying)
922 1.1.6.2 joerg return;
923 1.1.6.2 joerg
924 1.1.6.2 joerg switch (reg) {
925 1.1.6.2 joerg case UCOM_SET_DTR:
926 1.1.6.2 joerg sc->sc_dtr = !!onoff;
927 1.1.6.2 joerg set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
928 1.1.6.2 joerg break;
929 1.1.6.2 joerg case UCOM_SET_RTS:
930 1.1.6.2 joerg sc->sc_rts = !!onoff;
931 1.1.6.2 joerg set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
932 1.1.6.2 joerg break;
933 1.1.6.2 joerg case UCOM_SET_BREAK:
934 1.1.6.2 joerg set_break(sc, onoff);
935 1.1.6.2 joerg break;
936 1.1.6.2 joerg }
937 1.1.6.2 joerg }
938 1.1.6.2 joerg
939 1.1.6.2 joerg int
940 1.1.6.2 joerg uchcom_param(void *arg, int portno, struct termios *t)
941 1.1.6.2 joerg {
942 1.1.6.2 joerg struct uchcom_softc *sc = arg;
943 1.1.6.2 joerg int ret;
944 1.1.6.2 joerg
945 1.1.6.2 joerg if (sc->sc_dying)
946 1.1.6.2 joerg return 0;
947 1.1.6.2 joerg
948 1.1.6.2 joerg ret = set_line_control(sc, t->c_cflag);
949 1.1.6.2 joerg if (ret)
950 1.1.6.2 joerg return ret;
951 1.1.6.2 joerg
952 1.1.6.2 joerg ret = set_dte_rate(sc, t->c_ospeed);
953 1.1.6.2 joerg if (ret)
954 1.1.6.2 joerg return ret;
955 1.1.6.2 joerg
956 1.1.6.2 joerg return 0;
957 1.1.6.2 joerg }
958 1.1.6.2 joerg
959 1.1.6.2 joerg int
960 1.1.6.2 joerg uchcom_open(void *arg, int portno)
961 1.1.6.2 joerg {
962 1.1.6.2 joerg int ret;
963 1.1.6.2 joerg struct uchcom_softc *sc = arg;
964 1.1.6.2 joerg
965 1.1.6.2 joerg if (sc->sc_dying)
966 1.1.6.2 joerg return EIO;
967 1.1.6.2 joerg
968 1.1.6.2 joerg ret = setup_intr_pipe(sc);
969 1.1.6.2 joerg if (ret)
970 1.1.6.2 joerg return ret;
971 1.1.6.2 joerg
972 1.1.6.2 joerg ret = setup_comm(sc);
973 1.1.6.2 joerg if (ret)
974 1.1.6.2 joerg return ret;
975 1.1.6.2 joerg
976 1.1.6.2 joerg return 0;
977 1.1.6.2 joerg }
978 1.1.6.2 joerg
979 1.1.6.2 joerg void
980 1.1.6.2 joerg uchcom_close(void *arg, int portno)
981 1.1.6.2 joerg {
982 1.1.6.2 joerg struct uchcom_softc *sc = arg;
983 1.1.6.2 joerg
984 1.1.6.2 joerg if (sc->sc_dying)
985 1.1.6.2 joerg return;
986 1.1.6.2 joerg
987 1.1.6.2 joerg close_intr_pipe(sc);
988 1.1.6.2 joerg }
989 1.1.6.2 joerg
990 1.1.6.2 joerg
991 1.1.6.2 joerg /* ----------------------------------------------------------------------
992 1.1.6.2 joerg * callback when the modem status is changed.
993 1.1.6.2 joerg */
994 1.1.6.2 joerg void
995 1.1.6.2 joerg uchcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
996 1.1.6.2 joerg usbd_status status)
997 1.1.6.2 joerg {
998 1.1.6.2 joerg struct uchcom_softc *sc = priv;
999 1.1.6.2 joerg u_char *buf = sc->sc_intr_buf;
1000 1.1.6.2 joerg
1001 1.1.6.2 joerg if (sc->sc_dying)
1002 1.1.6.2 joerg return;
1003 1.1.6.2 joerg
1004 1.1.6.2 joerg if (status != USBD_NORMAL_COMPLETION) {
1005 1.1.6.2 joerg if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1006 1.1.6.2 joerg return;
1007 1.1.6.2 joerg
1008 1.1.6.2 joerg DPRINTF(("%s: abnormal status: %s\n",
1009 1.1.6.2 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(status)));
1010 1.1.6.2 joerg usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
1011 1.1.6.2 joerg return;
1012 1.1.6.2 joerg }
1013 1.1.6.2 joerg DPRINTF(("%s: intr: 0x%02X 0x%02X 0x%02X 0x%02X "
1014 1.1.6.2 joerg "0x%02X 0x%02X 0x%02X 0x%02X\n",
1015 1.1.6.2 joerg USBDEVNAME(sc->sc_dev),
1016 1.1.6.2 joerg (unsigned)buf[0], (unsigned)buf[1],
1017 1.1.6.2 joerg (unsigned)buf[2], (unsigned)buf[3],
1018 1.1.6.2 joerg (unsigned)buf[4], (unsigned)buf[5],
1019 1.1.6.2 joerg (unsigned)buf[6], (unsigned)buf[7]));
1020 1.1.6.2 joerg
1021 1.1.6.2 joerg convert_status(sc, buf[UCHCOM_INTR_STAT1]);
1022 1.1.6.2 joerg ucom_status_change((struct ucom_softc *) sc->sc_subdev);
1023 1.1.6.2 joerg }
1024