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