uvscom.c revision 1.35 1 1.35 mrg /* $NetBSD: uvscom.c,v 1.35 2019/05/09 02:43:35 mrg Exp $ */
2 1.1 augustss /*-
3 1.1 augustss * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama (at) jp.FreeBSD.org>.
4 1.1 augustss * All rights reserved.
5 1.1 augustss *
6 1.1 augustss * Redistribution and use in source and binary forms, with or without
7 1.1 augustss * modification, are permitted provided that the following conditions
8 1.1 augustss * are met:
9 1.1 augustss * 1. Redistributions of source code must retain the above copyright
10 1.1 augustss * notice, this list of conditions and the following disclaimer.
11 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 augustss * notice, this list of conditions and the following disclaimer in the
13 1.1 augustss * documentation and/or other materials provided with the distribution.
14 1.1 augustss *
15 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1 augustss * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 augustss * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 augustss * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 augustss * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 augustss * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 augustss * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 augustss * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 augustss * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 augustss * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 augustss * SUCH DAMAGE.
26 1.1 augustss *
27 1.1 augustss * $FreeBSD: src/sys/dev/usb/uvscom.c,v 1.1 2002/03/18 18:23:39 joe Exp $
28 1.1 augustss */
29 1.1 augustss
30 1.1 augustss /*
31 1.1 augustss * uvscom: SUNTAC Slipper U VS-10U driver.
32 1.1 augustss * Slipper U is a PC card to USB converter for data communication card
33 1.1 augustss * adapter. It supports DDI Pocket's Air H" C@rd, C@rd H" 64, NTT's P-in,
34 1.1 augustss * P-in m@ater and various data communication card adapters.
35 1.1 augustss */
36 1.10 lukem
37 1.10 lukem #include <sys/cdefs.h>
38 1.35 mrg __KERNEL_RCSID(0, "$NetBSD: uvscom.c,v 1.35 2019/05/09 02:43:35 mrg Exp $");
39 1.31 skrll
40 1.31 skrll #ifdef _KERNEL_OPT
41 1.31 skrll #include "opt_usb.h"
42 1.31 skrll #endif
43 1.1 augustss
44 1.1 augustss #include <sys/param.h>
45 1.1 augustss #include <sys/systm.h>
46 1.1 augustss #include <sys/kernel.h>
47 1.29 skrll #include <sys/kmem.h>
48 1.1 augustss #include <sys/fcntl.h>
49 1.1 augustss #include <sys/conf.h>
50 1.1 augustss #include <sys/tty.h>
51 1.1 augustss #include <sys/file.h>
52 1.1 augustss #include <sys/ioctl.h>
53 1.1 augustss #include <sys/device.h>
54 1.1 augustss #include <sys/proc.h>
55 1.1 augustss #include <sys/poll.h>
56 1.1 augustss
57 1.1 augustss #include <dev/usb/usb.h>
58 1.1 augustss #include <dev/usb/usbcdc.h>
59 1.1 augustss
60 1.1 augustss #include <dev/usb/usbdi.h>
61 1.1 augustss #include <dev/usb/usbdi_util.h>
62 1.1 augustss #include <dev/usb/usbdevs.h>
63 1.1 augustss #include <dev/usb/usb_quirks.h>
64 1.1 augustss
65 1.1 augustss #include <dev/usb/ucomvar.h>
66 1.1 augustss
67 1.1 augustss #ifdef UVSCOM_DEBUG
68 1.1 augustss static int uvscomdebug = 1;
69 1.1 augustss
70 1.1 augustss #define DPRINTFN(n, x) do { \
71 1.1 augustss if (uvscomdebug > (n)) \
72 1.25 dyoung printf x; \
73 1.1 augustss } while (0)
74 1.1 augustss #else
75 1.1 augustss #define DPRINTFN(n, x)
76 1.1 augustss #endif
77 1.1 augustss #define DPRINTF(x) DPRINTFN(0, x)
78 1.5 augustss
79 1.1 augustss #define UVSCOM_CONFIG_INDEX 0
80 1.1 augustss #define UVSCOM_IFACE_INDEX 0
81 1.1 augustss
82 1.1 augustss #define UVSCOM_INTR_INTERVAL 100 /* mS */
83 1.1 augustss
84 1.1 augustss #define UVSCOM_UNIT_WAIT 5
85 1.1 augustss
86 1.1 augustss /* Request */
87 1.1 augustss #define UVSCOM_SET_SPEED 0x10
88 1.1 augustss #define UVSCOM_LINE_CTL 0x11
89 1.1 augustss #define UVSCOM_SET_PARAM 0x12
90 1.1 augustss #define UVSCOM_READ_STATUS 0xd0
91 1.1 augustss #define UVSCOM_SHUTDOWN 0xe0
92 1.1 augustss
93 1.1 augustss /* UVSCOM_SET_SPEED parameters */
94 1.1 augustss #define UVSCOM_SPEED_150BPS 0x00
95 1.1 augustss #define UVSCOM_SPEED_300BPS 0x01
96 1.1 augustss #define UVSCOM_SPEED_600BPS 0x02
97 1.1 augustss #define UVSCOM_SPEED_1200BPS 0x03
98 1.1 augustss #define UVSCOM_SPEED_2400BPS 0x04
99 1.1 augustss #define UVSCOM_SPEED_4800BPS 0x05
100 1.1 augustss #define UVSCOM_SPEED_9600BPS 0x06
101 1.1 augustss #define UVSCOM_SPEED_19200BPS 0x07
102 1.1 augustss #define UVSCOM_SPEED_38400BPS 0x08
103 1.1 augustss #define UVSCOM_SPEED_57600BPS 0x09
104 1.1 augustss #define UVSCOM_SPEED_115200BPS 0x0a
105 1.1 augustss
106 1.1 augustss /* UVSCOM_LINE_CTL parameters */
107 1.1 augustss #define UVSCOM_BREAK 0x40
108 1.1 augustss #define UVSCOM_RTS 0x02
109 1.1 augustss #define UVSCOM_DTR 0x01
110 1.1 augustss #define UVSCOM_LINE_INIT 0x08
111 1.1 augustss
112 1.1 augustss /* UVSCOM_SET_PARAM parameters */
113 1.1 augustss #define UVSCOM_DATA_MASK 0x03
114 1.1 augustss #define UVSCOM_DATA_BIT_8 0x03
115 1.1 augustss #define UVSCOM_DATA_BIT_7 0x02
116 1.1 augustss #define UVSCOM_DATA_BIT_6 0x01
117 1.1 augustss #define UVSCOM_DATA_BIT_5 0x00
118 1.1 augustss
119 1.1 augustss #define UVSCOM_STOP_MASK 0x04
120 1.1 augustss #define UVSCOM_STOP_BIT_2 0x04
121 1.1 augustss #define UVSCOM_STOP_BIT_1 0x00
122 1.1 augustss
123 1.1 augustss #define UVSCOM_PARITY_MASK 0x18
124 1.1 augustss #define UVSCOM_PARITY_EVEN 0x18
125 1.1 augustss #if 0
126 1.1 augustss #define UVSCOM_PARITY_UNK 0x10
127 1.1 augustss #endif
128 1.1 augustss #define UVSCOM_PARITY_ODD 0x08
129 1.1 augustss #define UVSCOM_PARITY_NONE 0x00
130 1.1 augustss
131 1.1 augustss /* Status bits */
132 1.1 augustss #define UVSCOM_TXRDY 0x04
133 1.1 augustss #define UVSCOM_RXRDY 0x01
134 1.1 augustss
135 1.1 augustss #define UVSCOM_DCD 0x08
136 1.1 augustss #define UVSCOM_NOCARD 0x04
137 1.1 augustss #define UVSCOM_DSR 0x02
138 1.1 augustss #define UVSCOM_CTS 0x01
139 1.1 augustss #define UVSCOM_USTAT_MASK (UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS)
140 1.1 augustss
141 1.1 augustss struct uvscom_softc {
142 1.25 dyoung device_t sc_dev; /* base device */
143 1.29 skrll struct usbd_device * sc_udev; /* USB device */
144 1.29 skrll struct usbd_interface * sc_iface; /* interface */
145 1.1 augustss int sc_iface_number;/* interface number */
146 1.1 augustss
147 1.29 skrll struct usbd_interface * sc_intr_iface; /* interrupt interface */
148 1.1 augustss int sc_intr_number; /* interrupt number */
149 1.29 skrll struct usbd_pipe * sc_intr_pipe; /* interrupt pipe */
150 1.1 augustss u_char *sc_intr_buf; /* interrupt buffer */
151 1.1 augustss int sc_isize;
152 1.1 augustss
153 1.1 augustss u_char sc_dtr; /* current DTR state */
154 1.1 augustss u_char sc_rts; /* current RTS state */
155 1.1 augustss
156 1.1 augustss u_char sc_lsr; /* Local status register */
157 1.1 augustss u_char sc_msr; /* uvscom status register */
158 1.1 augustss
159 1.1 augustss uint16_t sc_lcr; /* Line control */
160 1.1 augustss u_char sc_usr; /* unit status */
161 1.1 augustss
162 1.25 dyoung device_t sc_subdev; /* ucom device */
163 1.35 mrg bool sc_dying; /* disconnecting */
164 1.1 augustss };
165 1.1 augustss
166 1.1 augustss /*
167 1.1 augustss * These are the maximum number of bytes transferred per frame.
168 1.1 augustss * The output buffer size cannot be increased due to the size encoding.
169 1.1 augustss */
170 1.1 augustss #define UVSCOMIBUFSIZE 512
171 1.1 augustss #define UVSCOMOBUFSIZE 64
172 1.1 augustss
173 1.35 mrg static usbd_status uvscom_readstat(struct uvscom_softc *);
174 1.35 mrg static usbd_status uvscom_shutdown(struct uvscom_softc *);
175 1.35 mrg static usbd_status uvscom_reset(struct uvscom_softc *);
176 1.35 mrg static usbd_status uvscom_set_line_coding(struct uvscom_softc *,
177 1.1 augustss uint16_t, uint16_t);
178 1.35 mrg static usbd_status uvscom_set_line(struct uvscom_softc *, uint16_t);
179 1.35 mrg static usbd_status uvscom_set_crtscts(struct uvscom_softc *);
180 1.35 mrg static void uvscom_get_status(void *, int, u_char *, u_char *);
181 1.35 mrg static void uvscom_dtr(struct uvscom_softc *, int);
182 1.35 mrg static void uvscom_rts(struct uvscom_softc *, int);
183 1.35 mrg static void uvscom_break(struct uvscom_softc *, int);
184 1.35 mrg
185 1.35 mrg static void uvscom_set(void *, int, int, int);
186 1.35 mrg static void uvscom_intr(struct usbd_xfer *, void *, usbd_status);
187 1.35 mrg static int uvscom_param(void *, int, struct termios *);
188 1.35 mrg static int uvscom_open(void *, int);
189 1.35 mrg static void uvscom_close(void *, int);
190 1.1 augustss
191 1.1 augustss struct ucom_methods uvscom_methods = {
192 1.29 skrll .ucom_get_status = uvscom_get_status,
193 1.29 skrll .ucom_set = uvscom_set,
194 1.29 skrll .ucom_param = uvscom_param,
195 1.29 skrll .ucom_ioctl = NULL, /* TODO */
196 1.29 skrll .ucom_open = uvscom_open,
197 1.29 skrll .ucom_close = uvscom_close,
198 1.1 augustss };
199 1.1 augustss
200 1.1 augustss static const struct usb_devno uvscom_devs [] = {
201 1.14 martin /* SUNTAC U-Cable type A4 */
202 1.14 martin { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4 },
203 1.8 ichiro /* SUNTAC U-Cable type D2 */
204 1.8 ichiro { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L },
205 1.1 augustss /* SUNTAC U-Cable type P1 */
206 1.1 augustss { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1 },
207 1.1 augustss /* SUNTAC Slipper U */
208 1.1 augustss { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U },
209 1.9 ichiro /* SUNTAC Ir-Trinity */
210 1.9 ichiro { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U },
211 1.1 augustss };
212 1.1 augustss #define uvscom_lookup(v, p) usb_lookup(uvscom_devs, v, p)
213 1.1 augustss
214 1.22 cube int uvscom_match(device_t, cfdata_t, void *);
215 1.21 dyoung void uvscom_attach(device_t, device_t, void *);
216 1.21 dyoung void uvscom_childdet(device_t, device_t);
217 1.21 dyoung int uvscom_detach(device_t, int);
218 1.34 mrg
219 1.22 cube CFATTACH_DECL2_NEW(uvscom, sizeof(struct uvscom_softc), uvscom_match,
220 1.35 mrg uvscom_attach, uvscom_detach, NULL, NULL, uvscom_childdet);
221 1.1 augustss
222 1.29 skrll int
223 1.25 dyoung uvscom_match(device_t parent, cfdata_t match, void *aux)
224 1.1 augustss {
225 1.25 dyoung struct usb_attach_arg *uaa = aux;
226 1.1 augustss
227 1.29 skrll return uvscom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
228 1.29 skrll UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
229 1.1 augustss }
230 1.1 augustss
231 1.29 skrll void
232 1.25 dyoung uvscom_attach(device_t parent, device_t self, void *aux)
233 1.1 augustss {
234 1.25 dyoung struct uvscom_softc *sc = device_private(self);
235 1.25 dyoung struct usb_attach_arg *uaa = aux;
236 1.29 skrll struct usbd_device *dev = uaa->uaa_device;
237 1.1 augustss usb_config_descriptor_t *cdesc;
238 1.1 augustss usb_interface_descriptor_t *id;
239 1.1 augustss usb_endpoint_descriptor_t *ed;
240 1.15 augustss char *devinfop;
241 1.1 augustss usbd_status err;
242 1.1 augustss int i;
243 1.29 skrll struct ucom_attach_args ucaa;
244 1.1 augustss
245 1.23 plunky aprint_naive("\n");
246 1.23 plunky aprint_normal("\n");
247 1.23 plunky
248 1.15 augustss devinfop = usbd_devinfo_alloc(dev, 0);
249 1.23 plunky aprint_normal_dev(self, "%s\n", devinfop);
250 1.15 augustss usbd_devinfo_free(devinfop);
251 1.1 augustss
252 1.22 cube sc->sc_dev = self;
253 1.35 mrg sc->sc_udev = dev;
254 1.35 mrg sc->sc_dying = false;
255 1.1 augustss
256 1.1 augustss DPRINTF(("uvscom attach: sc = %p\n", sc));
257 1.1 augustss
258 1.6 augustss /* initialize endpoints */
259 1.29 skrll ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
260 1.1 augustss sc->sc_intr_number = -1;
261 1.1 augustss sc->sc_intr_pipe = NULL;
262 1.1 augustss
263 1.1 augustss /* Move the device into the configured state. */
264 1.1 augustss err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1);
265 1.1 augustss if (err) {
266 1.22 cube aprint_error_dev(self, "failed to set configuration, err=%s\n",
267 1.22 cube usbd_errstr(err));
268 1.35 mrg sc->sc_dying = true;
269 1.25 dyoung return;
270 1.1 augustss }
271 1.1 augustss
272 1.1 augustss /* get the config descriptor */
273 1.1 augustss cdesc = usbd_get_config_descriptor(sc->sc_udev);
274 1.1 augustss
275 1.1 augustss if (cdesc == NULL) {
276 1.22 cube aprint_error_dev(self,
277 1.22 cube "failed to get configuration descriptor\n");
278 1.35 mrg sc->sc_dying = true;
279 1.25 dyoung return;
280 1.1 augustss }
281 1.1 augustss
282 1.1 augustss /* get the common interface */
283 1.6 augustss err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX,
284 1.1 augustss &sc->sc_iface);
285 1.1 augustss if (err) {
286 1.22 cube aprint_error_dev(self, "failed to get interface, err=%s\n",
287 1.22 cube usbd_errstr(err));
288 1.35 mrg sc->sc_dying = true;
289 1.25 dyoung return;
290 1.1 augustss }
291 1.1 augustss
292 1.1 augustss id = usbd_get_interface_descriptor(sc->sc_iface);
293 1.1 augustss sc->sc_iface_number = id->bInterfaceNumber;
294 1.1 augustss
295 1.1 augustss /* Find endpoints */
296 1.1 augustss for (i = 0; i < id->bNumEndpoints; i++) {
297 1.1 augustss ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
298 1.1 augustss if (ed == NULL) {
299 1.22 cube aprint_error_dev(self,
300 1.22 cube "no endpoint descriptor for %d\n", i);
301 1.35 mrg sc->sc_dying = true;
302 1.25 dyoung return;
303 1.1 augustss }
304 1.1 augustss
305 1.1 augustss if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
306 1.1 augustss UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
307 1.29 skrll ucaa.ucaa_bulkin = ed->bEndpointAddress;
308 1.1 augustss } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
309 1.1 augustss UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
310 1.29 skrll ucaa.ucaa_bulkout = ed->bEndpointAddress;
311 1.1 augustss } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
312 1.1 augustss UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
313 1.1 augustss sc->sc_intr_number = ed->bEndpointAddress;
314 1.1 augustss sc->sc_isize = UGETW(ed->wMaxPacketSize);
315 1.6 augustss }
316 1.1 augustss }
317 1.1 augustss
318 1.29 skrll if (ucaa.ucaa_bulkin == -1) {
319 1.22 cube aprint_error_dev(self, "Could not find data bulk in\n");
320 1.35 mrg sc->sc_dying = true;
321 1.25 dyoung return;
322 1.1 augustss }
323 1.29 skrll if (ucaa.ucaa_bulkout == -1) {
324 1.22 cube aprint_error_dev(self, "Could not find data bulk out\n");
325 1.35 mrg sc->sc_dying = true;
326 1.25 dyoung return;
327 1.1 augustss }
328 1.1 augustss if (sc->sc_intr_number == -1) {
329 1.22 cube aprint_error_dev(self, "Could not find interrupt in\n");
330 1.35 mrg sc->sc_dying = true;
331 1.25 dyoung return;
332 1.1 augustss }
333 1.1 augustss
334 1.1 augustss sc->sc_dtr = sc->sc_rts = 0;
335 1.1 augustss sc->sc_lcr = UVSCOM_LINE_INIT;
336 1.1 augustss
337 1.29 skrll ucaa.ucaa_portno = UCOM_UNK_PORTNO;
338 1.29 skrll /* ucaa_bulkin, ucaa_bulkout set above */
339 1.29 skrll ucaa.ucaa_ibufsize = UVSCOMIBUFSIZE;
340 1.29 skrll ucaa.ucaa_obufsize = UVSCOMOBUFSIZE;
341 1.29 skrll ucaa.ucaa_ibufsizepad = UVSCOMIBUFSIZE;
342 1.29 skrll ucaa.ucaa_opkthdrlen = 0;
343 1.29 skrll ucaa.ucaa_device = dev;
344 1.29 skrll ucaa.ucaa_iface = sc->sc_iface;
345 1.29 skrll ucaa.ucaa_methods = &uvscom_methods;
346 1.29 skrll ucaa.ucaa_arg = sc;
347 1.29 skrll ucaa.ucaa_info = NULL;
348 1.1 augustss
349 1.1 augustss err = uvscom_reset(sc);
350 1.1 augustss
351 1.1 augustss if (err) {
352 1.22 cube aprint_error_dev(self, "reset failed, %s\n", usbd_errstr(err));
353 1.35 mrg sc->sc_dying = true;
354 1.25 dyoung return;
355 1.1 augustss }
356 1.1 augustss
357 1.1 augustss DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n",
358 1.29 skrll ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number));
359 1.1 augustss
360 1.30 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
361 1.1 augustss
362 1.1 augustss DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
363 1.30 msaitoh ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number ));
364 1.29 skrll sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
365 1.13 drochner ucomprint, ucomsubmatch);
366 1.1 augustss
367 1.25 dyoung return;
368 1.1 augustss }
369 1.1 augustss
370 1.21 dyoung void
371 1.21 dyoung uvscom_childdet(device_t self, device_t child)
372 1.21 dyoung {
373 1.21 dyoung struct uvscom_softc *sc = device_private(self);
374 1.21 dyoung
375 1.21 dyoung KASSERT(sc->sc_subdev == child);
376 1.21 dyoung sc->sc_subdev = NULL;
377 1.21 dyoung }
378 1.21 dyoung
379 1.35 mrg static void
380 1.35 mrg uvscom_close_pipe(struct uvscom_softc *sc)
381 1.35 mrg {
382 1.35 mrg
383 1.35 mrg if (sc->sc_intr_pipe != NULL) {
384 1.35 mrg usbd_abort_pipe(sc->sc_intr_pipe);
385 1.35 mrg usbd_close_pipe(sc->sc_intr_pipe);
386 1.35 mrg sc->sc_intr_pipe = NULL;
387 1.35 mrg }
388 1.35 mrg if (sc->sc_intr_buf) {
389 1.35 mrg kmem_free(sc->sc_intr_buf, sc->sc_isize);
390 1.35 mrg sc->sc_intr_buf = NULL;
391 1.35 mrg }
392 1.35 mrg }
393 1.35 mrg
394 1.29 skrll int
395 1.25 dyoung uvscom_detach(device_t self, int flags)
396 1.1 augustss {
397 1.25 dyoung struct uvscom_softc *sc = device_private(self);
398 1.1 augustss int rv = 0;
399 1.1 augustss
400 1.1 augustss DPRINTF(("uvscom_detach: sc = %p\n", sc));
401 1.1 augustss
402 1.35 mrg sc->sc_dying = true;
403 1.35 mrg
404 1.35 mrg uvscom_close_pipe(sc);
405 1.1 augustss
406 1.35 mrg if (sc->sc_subdev != NULL) {
407 1.35 mrg rv = config_detach(sc->sc_subdev, flags);
408 1.35 mrg sc->sc_subdev = NULL;
409 1.1 augustss }
410 1.1 augustss
411 1.30 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
412 1.1 augustss
413 1.29 skrll return rv;
414 1.1 augustss }
415 1.1 augustss
416 1.35 mrg static usbd_status
417 1.1 augustss uvscom_readstat(struct uvscom_softc *sc)
418 1.1 augustss {
419 1.1 augustss usb_device_request_t req;
420 1.1 augustss usbd_status err;
421 1.1 augustss uint16_t r;
422 1.1 augustss
423 1.25 dyoung DPRINTF(("%s: send readstat\n", device_xname(sc->sc_dev)));
424 1.1 augustss
425 1.1 augustss req.bmRequestType = UT_READ_VENDOR_DEVICE;
426 1.1 augustss req.bRequest = UVSCOM_READ_STATUS;
427 1.1 augustss USETW(req.wValue, 0);
428 1.1 augustss USETW(req.wIndex, 0);
429 1.6 augustss USETW(req.wLength, 2);
430 1.1 augustss
431 1.6 augustss err = usbd_do_request(sc->sc_udev, &req, &r);
432 1.1 augustss if (err) {
433 1.22 cube aprint_error_dev(sc->sc_dev, "uvscom_readstat: %s\n",
434 1.22 cube usbd_errstr(err));
435 1.29 skrll return err;
436 1.1 augustss }
437 1.1 augustss
438 1.1 augustss DPRINTF(("%s: uvscom_readstat: r = %d\n",
439 1.25 dyoung device_xname(sc->sc_dev), r));
440 1.1 augustss
441 1.29 skrll return USBD_NORMAL_COMPLETION;
442 1.1 augustss }
443 1.1 augustss
444 1.35 mrg static usbd_status
445 1.1 augustss uvscom_shutdown(struct uvscom_softc *sc)
446 1.1 augustss {
447 1.1 augustss usb_device_request_t req;
448 1.1 augustss usbd_status err;
449 1.1 augustss
450 1.25 dyoung DPRINTF(("%s: send shutdown\n", device_xname(sc->sc_dev)));
451 1.1 augustss
452 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
453 1.1 augustss req.bRequest = UVSCOM_SHUTDOWN;
454 1.1 augustss USETW(req.wValue, 0);
455 1.1 augustss USETW(req.wIndex, 0);
456 1.6 augustss USETW(req.wLength, 0);
457 1.1 augustss
458 1.6 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
459 1.1 augustss if (err) {
460 1.22 cube aprint_error_dev(sc->sc_dev, "uvscom_shutdown: %s\n",
461 1.22 cube usbd_errstr(err));
462 1.29 skrll return err;
463 1.1 augustss }
464 1.1 augustss
465 1.29 skrll return USBD_NORMAL_COMPLETION;
466 1.1 augustss }
467 1.1 augustss
468 1.35 mrg static usbd_status
469 1.19 christos uvscom_reset(struct uvscom_softc *sc)
470 1.1 augustss {
471 1.25 dyoung DPRINTF(("%s: uvscom_reset\n", device_xname(sc->sc_dev)));
472 1.1 augustss
473 1.29 skrll return USBD_NORMAL_COMPLETION;
474 1.1 augustss }
475 1.1 augustss
476 1.35 mrg static usbd_status
477 1.19 christos uvscom_set_crtscts(struct uvscom_softc *sc)
478 1.1 augustss {
479 1.25 dyoung DPRINTF(("%s: uvscom_set_crtscts\n", device_xname(sc->sc_dev)));
480 1.1 augustss
481 1.29 skrll return USBD_NORMAL_COMPLETION;
482 1.1 augustss }
483 1.1 augustss
484 1.35 mrg static usbd_status
485 1.1 augustss uvscom_set_line(struct uvscom_softc *sc, uint16_t line)
486 1.1 augustss {
487 1.1 augustss usb_device_request_t req;
488 1.1 augustss usbd_status err;
489 1.1 augustss
490 1.1 augustss DPRINTF(("%s: uvscom_set_line: %04x\n",
491 1.25 dyoung device_xname(sc->sc_dev), line));
492 1.1 augustss
493 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
494 1.1 augustss req.bRequest = UVSCOM_LINE_CTL;
495 1.1 augustss USETW(req.wValue, line);
496 1.1 augustss USETW(req.wIndex, 0);
497 1.6 augustss USETW(req.wLength, 0);
498 1.1 augustss
499 1.6 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
500 1.1 augustss if (err) {
501 1.22 cube aprint_error_dev(sc->sc_dev, "uvscom_set_line: %s\n",
502 1.22 cube usbd_errstr(err));
503 1.29 skrll return err;
504 1.1 augustss }
505 1.1 augustss
506 1.29 skrll return USBD_NORMAL_COMPLETION;
507 1.1 augustss }
508 1.1 augustss
509 1.35 mrg static usbd_status
510 1.1 augustss uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls)
511 1.1 augustss {
512 1.1 augustss usb_device_request_t req;
513 1.1 augustss usbd_status err;
514 1.1 augustss
515 1.1 augustss DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n",
516 1.25 dyoung device_xname(sc->sc_dev), lsp, ls));
517 1.1 augustss
518 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
519 1.1 augustss req.bRequest = UVSCOM_SET_SPEED;
520 1.1 augustss USETW(req.wValue, lsp);
521 1.1 augustss USETW(req.wIndex, 0);
522 1.1 augustss USETW(req.wLength, 0);
523 1.1 augustss
524 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
525 1.1 augustss if (err) {
526 1.22 cube aprint_error_dev(sc->sc_dev, "uvscom_set_line_coding: %s\n",
527 1.22 cube usbd_errstr(err));
528 1.29 skrll return err;
529 1.1 augustss }
530 1.1 augustss
531 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
532 1.1 augustss req.bRequest = UVSCOM_SET_PARAM;
533 1.1 augustss USETW(req.wValue, ls);
534 1.1 augustss USETW(req.wIndex, 0);
535 1.1 augustss USETW(req.wLength, 0);
536 1.1 augustss
537 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
538 1.1 augustss if (err) {
539 1.22 cube aprint_error_dev(sc->sc_dev, "uvscom_set_line_coding: %s\n",
540 1.22 cube usbd_errstr(err));
541 1.29 skrll return err;
542 1.1 augustss }
543 1.1 augustss
544 1.29 skrll return USBD_NORMAL_COMPLETION;
545 1.1 augustss }
546 1.1 augustss
547 1.35 mrg static void
548 1.1 augustss uvscom_dtr(struct uvscom_softc *sc, int onoff)
549 1.1 augustss {
550 1.1 augustss DPRINTF(("%s: uvscom_dtr: onoff = %d\n",
551 1.25 dyoung device_xname(sc->sc_dev), onoff));
552 1.1 augustss
553 1.1 augustss if (sc->sc_dtr == onoff)
554 1.1 augustss return; /* no change */
555 1.1 augustss
556 1.1 augustss sc->sc_dtr = onoff;
557 1.1 augustss
558 1.1 augustss if (onoff)
559 1.1 augustss SET(sc->sc_lcr, UVSCOM_DTR);
560 1.1 augustss else
561 1.1 augustss CLR(sc->sc_lcr, UVSCOM_DTR);
562 1.1 augustss
563 1.1 augustss uvscom_set_line(sc, sc->sc_lcr);
564 1.1 augustss }
565 1.1 augustss
566 1.35 mrg static void
567 1.1 augustss uvscom_rts(struct uvscom_softc *sc, int onoff)
568 1.1 augustss {
569 1.1 augustss DPRINTF(("%s: uvscom_rts: onoff = %d\n",
570 1.25 dyoung device_xname(sc->sc_dev), onoff));
571 1.1 augustss
572 1.1 augustss if (sc->sc_rts == onoff)
573 1.1 augustss return; /* no change */
574 1.1 augustss
575 1.1 augustss sc->sc_rts = onoff;
576 1.1 augustss
577 1.1 augustss if (onoff)
578 1.1 augustss SET(sc->sc_lcr, UVSCOM_RTS);
579 1.1 augustss else
580 1.1 augustss CLR(sc->sc_lcr, UVSCOM_RTS);
581 1.1 augustss
582 1.1 augustss uvscom_set_line(sc, sc->sc_lcr);
583 1.1 augustss }
584 1.1 augustss
585 1.35 mrg static void
586 1.1 augustss uvscom_break(struct uvscom_softc *sc, int onoff)
587 1.1 augustss {
588 1.1 augustss DPRINTF(("%s: uvscom_break: onoff = %d\n",
589 1.25 dyoung device_xname(sc->sc_dev), onoff));
590 1.1 augustss
591 1.1 augustss if (onoff)
592 1.1 augustss uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK));
593 1.1 augustss }
594 1.1 augustss
595 1.35 mrg static void
596 1.19 christos uvscom_set(void *addr, int portno, int reg, int onoff)
597 1.1 augustss {
598 1.1 augustss struct uvscom_softc *sc = addr;
599 1.1 augustss
600 1.35 mrg if (sc->sc_dying)
601 1.35 mrg return;
602 1.35 mrg
603 1.1 augustss switch (reg) {
604 1.1 augustss case UCOM_SET_DTR:
605 1.1 augustss uvscom_dtr(sc, onoff);
606 1.1 augustss break;
607 1.1 augustss case UCOM_SET_RTS:
608 1.1 augustss uvscom_rts(sc, onoff);
609 1.1 augustss break;
610 1.1 augustss case UCOM_SET_BREAK:
611 1.1 augustss uvscom_break(sc, onoff);
612 1.1 augustss break;
613 1.1 augustss default:
614 1.1 augustss break;
615 1.1 augustss }
616 1.1 augustss }
617 1.1 augustss
618 1.35 mrg static int
619 1.19 christos uvscom_param(void *addr, int portno, struct termios *t)
620 1.1 augustss {
621 1.1 augustss struct uvscom_softc *sc = addr;
622 1.1 augustss usbd_status err;
623 1.1 augustss uint16_t lsp;
624 1.1 augustss uint16_t ls;
625 1.1 augustss
626 1.1 augustss DPRINTF(("%s: uvscom_param: sc = %p\n",
627 1.25 dyoung device_xname(sc->sc_dev), sc));
628 1.1 augustss
629 1.35 mrg if (sc->sc_dying)
630 1.35 mrg return EIO;
631 1.35 mrg
632 1.1 augustss ls = 0;
633 1.1 augustss
634 1.1 augustss switch (t->c_ospeed) {
635 1.1 augustss case B150:
636 1.1 augustss lsp = UVSCOM_SPEED_150BPS;
637 1.1 augustss break;
638 1.1 augustss case B300:
639 1.1 augustss lsp = UVSCOM_SPEED_300BPS;
640 1.1 augustss break;
641 1.1 augustss case B600:
642 1.1 augustss lsp = UVSCOM_SPEED_600BPS;
643 1.1 augustss break;
644 1.1 augustss case B1200:
645 1.1 augustss lsp = UVSCOM_SPEED_1200BPS;
646 1.1 augustss break;
647 1.1 augustss case B2400:
648 1.1 augustss lsp = UVSCOM_SPEED_2400BPS;
649 1.1 augustss break;
650 1.1 augustss case B4800:
651 1.1 augustss lsp = UVSCOM_SPEED_4800BPS;
652 1.1 augustss break;
653 1.1 augustss case B9600:
654 1.1 augustss lsp = UVSCOM_SPEED_9600BPS;
655 1.1 augustss break;
656 1.1 augustss case B19200:
657 1.1 augustss lsp = UVSCOM_SPEED_19200BPS;
658 1.1 augustss break;
659 1.1 augustss case B38400:
660 1.1 augustss lsp = UVSCOM_SPEED_38400BPS;
661 1.1 augustss break;
662 1.1 augustss case B57600:
663 1.1 augustss lsp = UVSCOM_SPEED_57600BPS;
664 1.1 augustss break;
665 1.1 augustss case B115200:
666 1.1 augustss lsp = UVSCOM_SPEED_115200BPS;
667 1.1 augustss break;
668 1.1 augustss default:
669 1.29 skrll return EIO;
670 1.1 augustss }
671 1.6 augustss
672 1.1 augustss if (ISSET(t->c_cflag, CSTOPB))
673 1.1 augustss SET(ls, UVSCOM_STOP_BIT_2);
674 1.1 augustss else
675 1.1 augustss SET(ls, UVSCOM_STOP_BIT_1);
676 1.1 augustss
677 1.1 augustss if (ISSET(t->c_cflag, PARENB)) {
678 1.1 augustss if (ISSET(t->c_cflag, PARODD))
679 1.1 augustss SET(ls, UVSCOM_PARITY_ODD);
680 1.1 augustss else
681 1.1 augustss SET(ls, UVSCOM_PARITY_EVEN);
682 1.1 augustss } else
683 1.1 augustss SET(ls, UVSCOM_PARITY_NONE);
684 1.1 augustss
685 1.1 augustss switch (ISSET(t->c_cflag, CSIZE)) {
686 1.1 augustss case CS5:
687 1.1 augustss SET(ls, UVSCOM_DATA_BIT_5);
688 1.1 augustss break;
689 1.1 augustss case CS6:
690 1.1 augustss SET(ls, UVSCOM_DATA_BIT_6);
691 1.1 augustss break;
692 1.1 augustss case CS7:
693 1.1 augustss SET(ls, UVSCOM_DATA_BIT_7);
694 1.1 augustss break;
695 1.1 augustss case CS8:
696 1.1 augustss SET(ls, UVSCOM_DATA_BIT_8);
697 1.1 augustss break;
698 1.1 augustss default:
699 1.29 skrll return EIO;
700 1.1 augustss }
701 1.1 augustss
702 1.1 augustss err = uvscom_set_line_coding(sc, lsp, ls);
703 1.1 augustss if (err)
704 1.29 skrll return EIO;
705 1.1 augustss
706 1.1 augustss if (ISSET(t->c_cflag, CRTSCTS)) {
707 1.1 augustss err = uvscom_set_crtscts(sc);
708 1.1 augustss if (err)
709 1.29 skrll return EIO;
710 1.1 augustss }
711 1.1 augustss
712 1.29 skrll return 0;
713 1.1 augustss }
714 1.1 augustss
715 1.35 mrg static int
716 1.19 christos uvscom_open(void *addr, int portno)
717 1.1 augustss {
718 1.1 augustss struct uvscom_softc *sc = addr;
719 1.1 augustss int err;
720 1.1 augustss int i;
721 1.6 augustss
722 1.1 augustss if (sc->sc_dying)
723 1.29 skrll return EIO;
724 1.1 augustss
725 1.1 augustss DPRINTF(("uvscom_open: sc = %p\n", sc));
726 1.1 augustss
727 1.1 augustss if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
728 1.1 augustss DPRINTF(("uvscom_open: open interrupt pipe.\n"));
729 1.1 augustss
730 1.1 augustss sc->sc_usr = 0; /* clear unit status */
731 1.1 augustss
732 1.1 augustss err = uvscom_readstat(sc);
733 1.1 augustss if (err) {
734 1.1 augustss DPRINTF(("%s: uvscom_open: readstat faild\n",
735 1.25 dyoung device_xname(sc->sc_dev)));
736 1.29 skrll return EIO;
737 1.1 augustss }
738 1.1 augustss
739 1.29 skrll sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
740 1.1 augustss err = usbd_open_pipe_intr(sc->sc_iface,
741 1.1 augustss sc->sc_intr_number,
742 1.1 augustss USBD_SHORT_XFER_OK,
743 1.1 augustss &sc->sc_intr_pipe,
744 1.1 augustss sc,
745 1.1 augustss sc->sc_intr_buf,
746 1.1 augustss sc->sc_isize,
747 1.1 augustss uvscom_intr,
748 1.1 augustss UVSCOM_INTR_INTERVAL);
749 1.1 augustss if (err) {
750 1.22 cube aprint_error_dev(sc->sc_dev,
751 1.22 cube "cannot open interrupt pipe (addr %d)\n",
752 1.1 augustss sc->sc_intr_number);
753 1.29 skrll return EIO;
754 1.1 augustss }
755 1.1 augustss } else {
756 1.1 augustss DPRINTF(("uvscom_open: did not open interrupt pipe.\n"));
757 1.1 augustss }
758 1.1 augustss
759 1.1 augustss if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) {
760 1.1 augustss /* unit is not ready */
761 1.1 augustss
762 1.1 augustss for (i = UVSCOM_UNIT_WAIT; i > 0; --i) {
763 1.32 skrll kpause("uvsopen", false, hz, NULL);
764 1.1 augustss if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK))
765 1.1 augustss break;
766 1.1 augustss }
767 1.1 augustss if (i == 0) {
768 1.1 augustss DPRINTF(("%s: unit is not ready\n",
769 1.25 dyoung device_xname(sc->sc_dev)));
770 1.29 skrll return EIO;
771 1.1 augustss }
772 1.1 augustss
773 1.1 augustss /* check PC card was inserted */
774 1.1 augustss if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) {
775 1.1 augustss DPRINTF(("%s: no card\n",
776 1.25 dyoung device_xname(sc->sc_dev)));
777 1.29 skrll return EIO;
778 1.1 augustss }
779 1.1 augustss }
780 1.1 augustss
781 1.29 skrll return 0;
782 1.1 augustss }
783 1.1 augustss
784 1.35 mrg static void
785 1.19 christos uvscom_close(void *addr, int portno)
786 1.1 augustss {
787 1.1 augustss struct uvscom_softc *sc = addr;
788 1.1 augustss
789 1.35 mrg DPRINTF(("uvscom_close: close\n"));
790 1.35 mrg
791 1.35 mrg if (sc->sc_dying)
792 1.1 augustss return;
793 1.1 augustss
794 1.1 augustss uvscom_shutdown(sc);
795 1.35 mrg uvscom_close_pipe(sc);
796 1.1 augustss }
797 1.1 augustss
798 1.35 mrg static void
799 1.29 skrll uvscom_intr(struct usbd_xfer *xfer, void *priv,
800 1.18 christos usbd_status status)
801 1.1 augustss {
802 1.1 augustss struct uvscom_softc *sc = priv;
803 1.1 augustss u_char *buf = sc->sc_intr_buf;
804 1.1 augustss u_char pstatus;
805 1.1 augustss
806 1.1 augustss if (sc->sc_dying)
807 1.1 augustss return;
808 1.1 augustss
809 1.1 augustss if (status != USBD_NORMAL_COMPLETION) {
810 1.1 augustss if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
811 1.1 augustss return;
812 1.1 augustss
813 1.22 cube aprint_error_dev(sc->sc_dev,
814 1.22 cube "uvscom_intr: abnormal status: %s\n",
815 1.22 cube usbd_errstr(status));
816 1.1 augustss usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
817 1.1 augustss return;
818 1.1 augustss }
819 1.1 augustss
820 1.1 augustss DPRINTFN(2, ("%s: uvscom status = %02x %02x\n",
821 1.25 dyoung device_xname(sc->sc_dev), buf[0], buf[1]));
822 1.1 augustss
823 1.1 augustss sc->sc_lsr = sc->sc_msr = 0;
824 1.1 augustss sc->sc_usr = buf[1];
825 1.1 augustss
826 1.1 augustss pstatus = buf[0];
827 1.1 augustss if (ISSET(pstatus, UVSCOM_TXRDY))
828 1.1 augustss SET(sc->sc_lsr, ULSR_TXRDY);
829 1.1 augustss if (ISSET(pstatus, UVSCOM_RXRDY))
830 1.1 augustss SET(sc->sc_lsr, ULSR_RXRDY);
831 1.1 augustss
832 1.1 augustss pstatus = buf[1];
833 1.1 augustss if (ISSET(pstatus, UVSCOM_CTS))
834 1.1 augustss SET(sc->sc_msr, UMSR_CTS);
835 1.1 augustss if (ISSET(pstatus, UVSCOM_DSR))
836 1.1 augustss SET(sc->sc_msr, UMSR_DSR);
837 1.1 augustss if (ISSET(pstatus, UVSCOM_DCD))
838 1.1 augustss SET(sc->sc_msr, UMSR_DCD);
839 1.1 augustss
840 1.22 cube ucom_status_change(device_private(sc->sc_subdev));
841 1.1 augustss }
842 1.1 augustss
843 1.35 mrg static void
844 1.19 christos uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
845 1.1 augustss {
846 1.1 augustss struct uvscom_softc *sc = addr;
847 1.1 augustss
848 1.35 mrg if (sc->sc_dying)
849 1.35 mrg return;
850 1.35 mrg
851 1.33 mrg *lsr = sc->sc_lsr;
852 1.33 mrg *msr = sc->sc_msr;
853 1.1 augustss }
854