uslsa.c revision 1.26 1 1.26 skrll /* $NetBSD: uslsa.c,v 1.26 2019/01/22 06:47:20 skrll Exp $ */
2 1.9 jakllsch
3 1.9 jakllsch /* from ugensa.c */
4 1.2 xtraeme
5 1.1 dogcow /*
6 1.9 jakllsch * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
7 1.1 dogcow * All rights reserved.
8 1.1 dogcow *
9 1.1 dogcow * This code is derived from software contributed to The NetBSD Foundation
10 1.9 jakllsch * by Roland C. Dowdeswell <elric (at) netbsd.org>.
11 1.1 dogcow *
12 1.1 dogcow * Redistribution and use in source and binary forms, with or without
13 1.1 dogcow * modification, are permitted provided that the following conditions
14 1.1 dogcow * are met:
15 1.1 dogcow * 1. Redistributions of source code must retain the above copyright
16 1.1 dogcow * notice, this list of conditions and the following disclaimer.
17 1.1 dogcow * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 dogcow * notice, this list of conditions and the following disclaimer in the
19 1.1 dogcow * documentation and/or other materials provided with the distribution.
20 1.1 dogcow *
21 1.1 dogcow * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.1 dogcow * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 dogcow * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 dogcow * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 dogcow * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 dogcow * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 dogcow * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 dogcow * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 dogcow * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 dogcow * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 dogcow * POSSIBILITY OF SUCH DAMAGE.
32 1.1 dogcow */
33 1.1 dogcow
34 1.9 jakllsch /*
35 1.9 jakllsch * Copyright (c) 2007, 2009 Jonathan A. Kollasch.
36 1.9 jakllsch * All rights reserved.
37 1.9 jakllsch *
38 1.9 jakllsch * Redistribution and use in source and binary forms, with or without
39 1.9 jakllsch * modification, are permitted provided that the following conditions
40 1.9 jakllsch * are met:
41 1.9 jakllsch * 1. Redistributions of source code must retain the above copyright
42 1.9 jakllsch * notice, this list of conditions and the following disclaimer.
43 1.9 jakllsch * 2. Redistributions in binary form must reproduce the above copyright
44 1.9 jakllsch * notice, this list of conditions and the following disclaimer in the
45 1.9 jakllsch * documentation and/or other materials provided with the distribution.
46 1.9 jakllsch *
47 1.9 jakllsch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.9 jakllsch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.9 jakllsch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.9 jakllsch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.9 jakllsch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 1.9 jakllsch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 1.9 jakllsch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 1.9 jakllsch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 1.9 jakllsch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 1.9 jakllsch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 1.9 jakllsch *
58 1.9 jakllsch */
59 1.9 jakllsch
60 1.2 xtraeme #include <sys/cdefs.h>
61 1.26 skrll __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.26 2019/01/22 06:47:20 skrll Exp $");
62 1.22 skrll
63 1.22 skrll #ifdef _KERNEL_OPT
64 1.22 skrll #include "opt_usb.h"
65 1.22 skrll #endif
66 1.2 xtraeme
67 1.1 dogcow #include <sys/param.h>
68 1.1 dogcow #include <sys/systm.h>
69 1.1 dogcow #include <sys/kernel.h>
70 1.1 dogcow #include <sys/device.h>
71 1.1 dogcow #include <sys/conf.h>
72 1.1 dogcow #include <sys/tty.h>
73 1.1 dogcow
74 1.1 dogcow #include <dev/usb/usb.h>
75 1.1 dogcow
76 1.1 dogcow #include <dev/usb/usbdi.h>
77 1.1 dogcow #include <dev/usb/usbdi_util.h>
78 1.1 dogcow #include <dev/usb/usbdevs.h>
79 1.1 dogcow
80 1.1 dogcow #include <dev/usb/ucomvar.h>
81 1.1 dogcow
82 1.17 jakllsch #include <dev/usb/uslsareg.h>
83 1.17 jakllsch
84 1.17 jakllsch #include <fs/unicode.h>
85 1.1 dogcow
86 1.1 dogcow #ifdef USLSA_DEBUG
87 1.17 jakllsch #define DPRINTF(x) if (uslsadebug) device_printf x
88 1.1 dogcow int uslsadebug = 0;
89 1.1 dogcow #else
90 1.1 dogcow #define DPRINTF(x)
91 1.1 dogcow #endif
92 1.1 dogcow
93 1.1 dogcow struct uslsa_softc {
94 1.12 dyoung device_t sc_dev; /* base device */
95 1.17 jakllsch device_t sc_subdev; /* ucom device */
96 1.20 skrll struct usbd_device * sc_udev; /* usb device */
97 1.20 skrll struct usbd_interface * sc_iface; /* interface */
98 1.17 jakllsch uint8_t sc_ifnum; /* interface number */
99 1.17 jakllsch bool sc_dying; /* disconnecting */
100 1.1 dogcow };
101 1.1 dogcow
102 1.1 dogcow static void uslsa_get_status(void *sc, int, u_char *, u_char *);
103 1.1 dogcow static void uslsa_set(void *, int, int, int);
104 1.1 dogcow static int uslsa_param(void *, int, struct termios *);
105 1.17 jakllsch static int uslsa_ioctl(void *, int, u_long, void *, int, proc_t *);
106 1.17 jakllsch
107 1.1 dogcow static int uslsa_open(void *, int);
108 1.1 dogcow static void uslsa_close(void *, int);
109 1.1 dogcow
110 1.17 jakllsch static int uslsa_usbd_errno(usbd_status);
111 1.1 dogcow static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t);
112 1.17 jakllsch static int uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t);
113 1.17 jakllsch
114 1.14 jakllsch static const struct ucom_methods uslsa_methods = {
115 1.20 skrll .ucom_get_status = uslsa_get_status,
116 1.20 skrll .ucom_set = uslsa_set,
117 1.20 skrll .ucom_param = uslsa_param,
118 1.20 skrll .ucom_ioctl = uslsa_ioctl,
119 1.20 skrll .ucom_open = uslsa_open,
120 1.20 skrll .ucom_close = uslsa_close,
121 1.20 skrll .ucom_read = NULL,
122 1.20 skrll .ucom_write = NULL,
123 1.1 dogcow };
124 1.1 dogcow
125 1.1 dogcow #define USLSA_CONFIG_INDEX 0
126 1.1 dogcow #define USLSA_IFACE_INDEX 0
127 1.1 dogcow #define USLSA_BUFSIZE 256
128 1.1 dogcow
129 1.1 dogcow static const struct usb_devno uslsa_devs[] = {
130 1.1 dogcow { USB_VENDOR_BALTECH, USB_PRODUCT_BALTECH_CARDREADER },
131 1.1 dogcow { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
132 1.1 dogcow { USB_VENDOR_JABLOTRON, USB_PRODUCT_JABLOTRON_PC60B },
133 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_ARGUSISP },
134 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CRUMB128 },
135 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DEGREECONT },
136 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DESKTOPMOBILE },
137 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_IPLINK1220 },
138 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_HARP },
139 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
140 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_LIN },
141 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_POLOLU },
142 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_1 },
143 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_2 },
144 1.1 dogcow { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SUNNTO },
145 1.1 dogcow { USB_VENDOR_SILABS2, USB_PRODUCT_SILABS2_DCU11CLONE },
146 1.25 jakllsch { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 },
147 1.24 jakllsch { USB_VENDOR_WMR, USB_PRODUCT_WMR_RIGBLASTER },
148 1.1 dogcow };
149 1.1 dogcow
150 1.14 jakllsch static int uslsa_match(device_t, cfdata_t, void *);
151 1.14 jakllsch static void uslsa_attach(device_t, device_t, void *);
152 1.14 jakllsch static void uslsa_childdet(device_t, device_t);
153 1.14 jakllsch static int uslsa_detach(device_t, int);
154 1.14 jakllsch static int uslsa_activate(device_t, enum devact);
155 1.14 jakllsch
156 1.8 cube CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
157 1.5 dyoung uslsa_attach, uslsa_detach, uslsa_activate, NULL, uslsa_childdet);
158 1.1 dogcow
159 1.17 jakllsch static int
160 1.12 dyoung uslsa_match(device_t parent, cfdata_t match, void *aux)
161 1.1 dogcow {
162 1.20 skrll const struct usbif_attach_arg *uiaa = aux;
163 1.17 jakllsch
164 1.21 msaitoh if (usb_lookup(uslsa_devs, uiaa->uiaa_vendor, uiaa->uiaa_product)
165 1.21 msaitoh != NULL)
166 1.17 jakllsch return UMATCH_VENDOR_PRODUCT;
167 1.21 msaitoh else
168 1.17 jakllsch return UMATCH_NONE;
169 1.1 dogcow }
170 1.1 dogcow
171 1.17 jakllsch static void
172 1.12 dyoung uslsa_attach(device_t parent, device_t self, void *aux)
173 1.1 dogcow {
174 1.17 jakllsch struct uslsa_softc *sc;
175 1.20 skrll const struct usbif_attach_arg *uiaa = aux;
176 1.17 jakllsch const usb_interface_descriptor_t *id;
177 1.17 jakllsch const usb_endpoint_descriptor_t *ed;
178 1.1 dogcow char *devinfop;
179 1.20 skrll struct ucom_attach_args ucaa;
180 1.1 dogcow int i;
181 1.1 dogcow
182 1.17 jakllsch sc = device_private(self);
183 1.17 jakllsch
184 1.8 cube sc->sc_dev = self;
185 1.20 skrll sc->sc_udev = uiaa->uiaa_device;
186 1.20 skrll sc->sc_iface = uiaa->uiaa_iface;
187 1.1 dogcow
188 1.10 plunky aprint_naive("\n");
189 1.10 plunky aprint_normal("\n");
190 1.10 plunky
191 1.17 jakllsch devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
192 1.10 plunky aprint_normal_dev(self, "%s\n", devinfop);
193 1.10 plunky usbd_devinfo_free(devinfop);
194 1.10 plunky
195 1.17 jakllsch id = usbd_get_interface_descriptor(sc->sc_iface);
196 1.1 dogcow
197 1.17 jakllsch sc->sc_ifnum = id->bInterfaceNumber;
198 1.1 dogcow
199 1.20 skrll ucaa.ucaa_info = "Silicon Labs CP210x";
200 1.20 skrll ucaa.ucaa_portno = UCOM_UNK_PORTNO;
201 1.20 skrll ucaa.ucaa_ibufsize = USLSA_BUFSIZE;
202 1.20 skrll ucaa.ucaa_obufsize = USLSA_BUFSIZE;
203 1.20 skrll ucaa.ucaa_ibufsizepad = USLSA_BUFSIZE;
204 1.20 skrll ucaa.ucaa_opkthdrlen = 0;
205 1.20 skrll ucaa.ucaa_device = sc->sc_udev;
206 1.20 skrll ucaa.ucaa_iface = sc->sc_iface;
207 1.20 skrll ucaa.ucaa_methods = &uslsa_methods;
208 1.20 skrll ucaa.ucaa_arg = sc;
209 1.1 dogcow
210 1.21 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
211 1.1 dogcow
212 1.20 skrll ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
213 1.1 dogcow for (i = 0; i < id->bNumEndpoints; i++) {
214 1.1 dogcow int addr, dir, attr;
215 1.1 dogcow
216 1.17 jakllsch ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
217 1.1 dogcow if (ed == NULL) {
218 1.8 cube aprint_error_dev(self,
219 1.17 jakllsch "could not read endpoint descriptor\n");
220 1.17 jakllsch sc->sc_dying = true;
221 1.17 jakllsch return;
222 1.1 dogcow }
223 1.1 dogcow addr = ed->bEndpointAddress;
224 1.1 dogcow dir = UE_GET_DIR(ed->bEndpointAddress);
225 1.1 dogcow attr = ed->bmAttributes & UE_XFERTYPE;
226 1.17 jakllsch if (dir == UE_DIR_IN && attr == UE_BULK) {
227 1.20 skrll ucaa.ucaa_bulkin = addr;
228 1.17 jakllsch } else if (dir == UE_DIR_OUT && attr == UE_BULK) {
229 1.20 skrll ucaa.ucaa_bulkout = addr;
230 1.17 jakllsch } else {
231 1.8 cube aprint_error_dev(self, "unexpected endpoint\n");
232 1.17 jakllsch }
233 1.1 dogcow }
234 1.17 jakllsch aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n",
235 1.20 skrll ucaa.ucaa_bulkin, ucaa.ucaa_bulkout);
236 1.20 skrll if ((ucaa.ucaa_bulkin == -1) || (ucaa.ucaa_bulkout == -1)) {
237 1.17 jakllsch aprint_error_dev(self, "could not find endpoints\n");
238 1.17 jakllsch sc->sc_dying = true;
239 1.17 jakllsch return;
240 1.1 dogcow }
241 1.1 dogcow
242 1.20 skrll sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
243 1.1 dogcow ucomprint, ucomsubmatch);
244 1.1 dogcow
245 1.23 maya if (!pmf_device_register(self, NULL, NULL))
246 1.23 maya aprint_error_dev(self, "couldn't establish power handler\n");
247 1.23 maya
248 1.12 dyoung return;
249 1.1 dogcow }
250 1.1 dogcow
251 1.14 jakllsch static int
252 1.5 dyoung uslsa_activate(device_t self, enum devact act)
253 1.1 dogcow {
254 1.5 dyoung struct uslsa_softc *sc = device_private(self);
255 1.1 dogcow
256 1.1 dogcow switch (act) {
257 1.1 dogcow case DVACT_DEACTIVATE:
258 1.17 jakllsch sc->sc_dying = true;
259 1.11 dyoung return 0;
260 1.11 dyoung default:
261 1.11 dyoung return EOPNOTSUPP;
262 1.1 dogcow }
263 1.1 dogcow }
264 1.1 dogcow
265 1.14 jakllsch static void
266 1.5 dyoung uslsa_childdet(device_t self, device_t child)
267 1.5 dyoung {
268 1.5 dyoung struct uslsa_softc *sc = device_private(self);
269 1.5 dyoung
270 1.5 dyoung KASSERT(sc->sc_subdev == child);
271 1.5 dyoung sc->sc_subdev = NULL;
272 1.5 dyoung }
273 1.5 dyoung
274 1.17 jakllsch static int
275 1.12 dyoung uslsa_detach(device_t self, int flags)
276 1.1 dogcow {
277 1.12 dyoung struct uslsa_softc *sc = device_private(self);
278 1.1 dogcow int rv = 0;
279 1.1 dogcow
280 1.17 jakllsch DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags));
281 1.1 dogcow
282 1.17 jakllsch sc->sc_dying = true;
283 1.1 dogcow
284 1.17 jakllsch if (sc->sc_subdev != NULL) {
285 1.1 dogcow rv = config_detach(sc->sc_subdev, flags);
286 1.17 jakllsch }
287 1.1 dogcow
288 1.21 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
289 1.1 dogcow
290 1.26 skrll return rv;
291 1.1 dogcow }
292 1.1 dogcow
293 1.17 jakllsch static int
294 1.17 jakllsch uslsa_usbd_errno(usbd_status status)
295 1.17 jakllsch {
296 1.17 jakllsch switch (status) {
297 1.17 jakllsch case USBD_NORMAL_COMPLETION:
298 1.17 jakllsch return 0;
299 1.17 jakllsch case USBD_STALLED:
300 1.17 jakllsch return EINVAL;
301 1.17 jakllsch default:
302 1.17 jakllsch return EIO;
303 1.17 jakllsch }
304 1.17 jakllsch }
305 1.17 jakllsch
306 1.1 dogcow static void
307 1.1 dogcow uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
308 1.1 dogcow {
309 1.1 dogcow struct uslsa_softc *sc;
310 1.1 dogcow usb_device_request_t req;
311 1.17 jakllsch usbd_status status;
312 1.17 jakllsch uint8_t mdmsts;
313 1.1 dogcow
314 1.1 dogcow sc = vsc;
315 1.1 dogcow
316 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno));
317 1.17 jakllsch
318 1.17 jakllsch if (sc->sc_dying) {
319 1.17 jakllsch return;
320 1.17 jakllsch }
321 1.1 dogcow
322 1.9 jakllsch req.bmRequestType = UT_READ_VENDOR_INTERFACE;
323 1.17 jakllsch req.bRequest = SLSA_R_GET_MDMSTS;
324 1.1 dogcow USETW(req.wValue, 0);
325 1.17 jakllsch USETW(req.wIndex, sc->sc_ifnum);
326 1.17 jakllsch USETW(req.wLength, SLSA_RL_GET_MDMSTS);
327 1.17 jakllsch
328 1.17 jakllsch status = usbd_do_request(sc->sc_udev, &req, &mdmsts);
329 1.17 jakllsch if (status != USBD_NORMAL_COMPLETION) {
330 1.17 jakllsch device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n",
331 1.17 jakllsch __func__, usbd_errstr(status));
332 1.17 jakllsch return;
333 1.17 jakllsch }
334 1.17 jakllsch
335 1.17 jakllsch DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts));
336 1.17 jakllsch
337 1.17 jakllsch if (lsr != NULL) {
338 1.17 jakllsch *lsr = 0;
339 1.17 jakllsch }
340 1.1 dogcow
341 1.17 jakllsch if (msr != NULL) {
342 1.17 jakllsch *msr = 0;
343 1.17 jakllsch *msr |= ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0;
344 1.17 jakllsch *msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0;
345 1.17 jakllsch *msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0;
346 1.17 jakllsch *msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0;
347 1.17 jakllsch }
348 1.1 dogcow }
349 1.1 dogcow
350 1.1 dogcow static void
351 1.1 dogcow uslsa_set(void *vsc, int portno, int reg, int onoff)
352 1.1 dogcow {
353 1.1 dogcow struct uslsa_softc *sc;
354 1.1 dogcow
355 1.1 dogcow sc = vsc;
356 1.1 dogcow
357 1.21 msaitoh DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno,
358 1.21 msaitoh reg, onoff));
359 1.17 jakllsch
360 1.17 jakllsch if (sc->sc_dying) {
361 1.17 jakllsch return;
362 1.17 jakllsch }
363 1.1 dogcow
364 1.1 dogcow switch (reg) {
365 1.1 dogcow case UCOM_SET_DTR:
366 1.17 jakllsch if (uslsa_request_set(sc, SLSA_R_SET_MHS,
367 1.17 jakllsch SLSA_RV_SET_MHS_DTR_MASK |
368 1.17 jakllsch (onoff ? SLSA_RV_SET_MHS_DTR : 0))) {
369 1.17 jakllsch device_printf(sc->sc_dev, "SET_MHS/DTR failed\n");
370 1.17 jakllsch }
371 1.1 dogcow break;
372 1.1 dogcow case UCOM_SET_RTS:
373 1.17 jakllsch if (uslsa_request_set(sc, SLSA_R_SET_MHS,
374 1.17 jakllsch SLSA_RV_SET_MHS_RTS_MASK |
375 1.17 jakllsch (onoff ? SLSA_RV_SET_MHS_RTS : 0))) {
376 1.17 jakllsch device_printf(sc->sc_dev, "SET_MHS/RTS failed\n");
377 1.17 jakllsch }
378 1.1 dogcow break;
379 1.1 dogcow case UCOM_SET_BREAK:
380 1.17 jakllsch if (uslsa_request_set(sc, SLSA_R_SET_BREAK,
381 1.17 jakllsch (onoff ? SLSA_RV_SET_BREAK_ENABLE :
382 1.17 jakllsch SLSA_RV_SET_BREAK_DISABLE))) {
383 1.17 jakllsch device_printf(sc->sc_dev, "SET_BREAK failed\n");
384 1.17 jakllsch }
385 1.1 dogcow break;
386 1.1 dogcow default:
387 1.1 dogcow break;
388 1.1 dogcow }
389 1.1 dogcow }
390 1.1 dogcow
391 1.1 dogcow static int
392 1.17 jakllsch uslsa_param(void *vsc, int portno, struct termios *t)
393 1.1 dogcow {
394 1.1 dogcow struct uslsa_softc *sc;
395 1.19 reinoud usb_device_request_t req;
396 1.19 reinoud usbd_status status;
397 1.19 reinoud uint16_t value;
398 1.19 reinoud uint32_t baud;
399 1.17 jakllsch int ret;
400 1.1 dogcow
401 1.1 dogcow sc = vsc;
402 1.1 dogcow
403 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t));
404 1.17 jakllsch
405 1.17 jakllsch if (sc->sc_dying) {
406 1.17 jakllsch return EIO;
407 1.17 jakllsch }
408 1.17 jakllsch
409 1.19 reinoud req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
410 1.19 reinoud req.bRequest = SLSA_R_SET_BAUDRATE;
411 1.19 reinoud USETW(req.wValue, 0);
412 1.19 reinoud USETW(req.wIndex, sc->sc_ifnum);
413 1.19 reinoud USETW(req.wLength, 4);
414 1.17 jakllsch
415 1.19 reinoud baud = t->c_ospeed;
416 1.19 reinoud status = usbd_do_request(sc->sc_udev, &req, &baud);
417 1.19 reinoud if (status != USBD_NORMAL_COMPLETION) {
418 1.19 reinoud /* fallback method for devices that don't know SET_BAUDRATE */
419 1.19 reinoud /* hope we calculate it right */
420 1.19 reinoud device_printf(sc->sc_dev, "%s: set baudrate %d, failed %s,"
421 1.19 reinoud " using set bauddiv\n",
422 1.19 reinoud __func__, baud, usbd_errstr(status));
423 1.19 reinoud
424 1.19 reinoud value = SLSA_RV_BAUDDIV(t->c_ospeed);
425 1.21 msaitoh if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value))
426 1.21 msaitoh != 0) {
427 1.19 reinoud device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n",
428 1.19 reinoud __func__);
429 1.19 reinoud return ret;
430 1.19 reinoud }
431 1.17 jakllsch }
432 1.17 jakllsch
433 1.17 jakllsch value = 0;
434 1.1 dogcow
435 1.17 jakllsch if (ISSET(t->c_cflag, CSTOPB)) {
436 1.17 jakllsch value |= SLSA_RV_LINE_CTL_STOP_2;
437 1.17 jakllsch } else {
438 1.17 jakllsch value |= SLSA_RV_LINE_CTL_STOP_1;
439 1.17 jakllsch }
440 1.1 dogcow
441 1.1 dogcow if (ISSET(t->c_cflag, PARENB)) {
442 1.17 jakllsch if (ISSET(t->c_cflag, PARODD)) {
443 1.17 jakllsch value |= SLSA_RV_LINE_CTL_PARITY_ODD;
444 1.17 jakllsch } else {
445 1.17 jakllsch value |= SLSA_RV_LINE_CTL_PARITY_EVEN;
446 1.17 jakllsch }
447 1.17 jakllsch } else {
448 1.17 jakllsch value |= SLSA_RV_LINE_CTL_PARITY_NONE;
449 1.17 jakllsch }
450 1.1 dogcow
451 1.1 dogcow switch (ISSET(t->c_cflag, CSIZE)) {
452 1.1 dogcow case CS5:
453 1.17 jakllsch value |= SLSA_RV_LINE_CTL_LEN_5;
454 1.1 dogcow break;
455 1.1 dogcow case CS6:
456 1.17 jakllsch value |= SLSA_RV_LINE_CTL_LEN_6;
457 1.1 dogcow break;
458 1.1 dogcow case CS7:
459 1.17 jakllsch value |= SLSA_RV_LINE_CTL_LEN_7;
460 1.1 dogcow break;
461 1.1 dogcow case CS8:
462 1.17 jakllsch value |= SLSA_RV_LINE_CTL_LEN_8;
463 1.1 dogcow break;
464 1.1 dogcow }
465 1.1 dogcow
466 1.17 jakllsch DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to 0x%x\n",
467 1.17 jakllsch __func__, value));
468 1.17 jakllsch if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) {
469 1.17 jakllsch device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
470 1.17 jakllsch return ret;
471 1.17 jakllsch }
472 1.17 jakllsch
473 1.17 jakllsch if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) {
474 1.17 jakllsch device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
475 1.17 jakllsch }
476 1.17 jakllsch
477 1.17 jakllsch return ret;
478 1.17 jakllsch }
479 1.17 jakllsch
480 1.17 jakllsch static int
481 1.17 jakllsch uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p)
482 1.17 jakllsch {
483 1.17 jakllsch struct uslsa_softc *sc;
484 1.1 dogcow
485 1.17 jakllsch sc = vsc;
486 1.17 jakllsch
487 1.17 jakllsch switch (cmd) {
488 1.17 jakllsch case TIOCMGET:
489 1.17 jakllsch ucom_status_change(device_private(sc->sc_subdev));
490 1.17 jakllsch return EPASSTHROUGH;
491 1.17 jakllsch default:
492 1.17 jakllsch return EPASSTHROUGH;
493 1.17 jakllsch }
494 1.1 dogcow
495 1.1 dogcow return 0;
496 1.1 dogcow }
497 1.1 dogcow
498 1.1 dogcow static int
499 1.1 dogcow uslsa_open(void *vsc, int portno)
500 1.1 dogcow {
501 1.1 dogcow struct uslsa_softc *sc;
502 1.1 dogcow
503 1.1 dogcow sc = vsc;
504 1.1 dogcow
505 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
506 1.1 dogcow
507 1.17 jakllsch if (sc->sc_dying) {
508 1.17 jakllsch return EIO;
509 1.17 jakllsch }
510 1.1 dogcow
511 1.17 jakllsch return uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
512 1.17 jakllsch SLSA_RV_IFC_ENABLE_ENABLE);
513 1.1 dogcow }
514 1.1 dogcow
515 1.14 jakllsch static void
516 1.1 dogcow uslsa_close(void *vsc, int portno)
517 1.1 dogcow {
518 1.1 dogcow struct uslsa_softc *sc;
519 1.1 dogcow
520 1.1 dogcow sc = vsc;
521 1.1 dogcow
522 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
523 1.17 jakllsch
524 1.17 jakllsch if (sc->sc_dying) {
525 1.1 dogcow return;
526 1.17 jakllsch }
527 1.1 dogcow
528 1.17 jakllsch (void)uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
529 1.17 jakllsch SLSA_RV_IFC_ENABLE_DISABLE);
530 1.1 dogcow }
531 1.1 dogcow
532 1.1 dogcow static int
533 1.1 dogcow uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
534 1.1 dogcow {
535 1.1 dogcow usb_device_request_t req;
536 1.17 jakllsch usbd_status status;
537 1.1 dogcow
538 1.9 jakllsch req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
539 1.1 dogcow req.bRequest = request;
540 1.1 dogcow USETW(req.wValue, value);
541 1.17 jakllsch USETW(req.wIndex, sc->sc_ifnum);
542 1.1 dogcow USETW(req.wLength, 0);
543 1.1 dogcow
544 1.17 jakllsch status = usbd_do_request(sc->sc_udev, &req, NULL);
545 1.17 jakllsch
546 1.17 jakllsch return uslsa_usbd_errno(status);
547 1.1 dogcow }
548 1.1 dogcow
549 1.17 jakllsch static int
550 1.1 dogcow uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
551 1.1 dogcow {
552 1.17 jakllsch struct slsa_fcs fcs;
553 1.1 dogcow usb_device_request_t req;
554 1.17 jakllsch uint32_t ulControlHandshake;
555 1.17 jakllsch uint32_t ulFlowReplace;
556 1.17 jakllsch usbd_status status;
557 1.1 dogcow
558 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag));
559 1.1 dogcow
560 1.9 jakllsch req.bmRequestType = UT_READ_VENDOR_INTERFACE;
561 1.17 jakllsch req.bRequest = SLSA_R_GET_FLOW;
562 1.1 dogcow USETW(req.wValue, 0);
563 1.17 jakllsch USETW(req.wIndex, sc->sc_ifnum);
564 1.17 jakllsch USETW(req.wLength, SLSA_RL_GET_FLOW);
565 1.17 jakllsch
566 1.17 jakllsch status = usbd_do_request(sc->sc_udev, &req, &fcs);
567 1.17 jakllsch if (status != USBD_NORMAL_COMPLETION) {
568 1.17 jakllsch device_printf(sc->sc_dev, "%s: GET_FLOW %s\n",
569 1.17 jakllsch __func__, usbd_errstr(status));
570 1.17 jakllsch return uslsa_usbd_errno(status);
571 1.17 jakllsch }
572 1.1 dogcow
573 1.17 jakllsch ulControlHandshake = le32toh(fcs.ulControlHandshake);
574 1.17 jakllsch ulFlowReplace = le32toh(fcs.ulFlowReplace);
575 1.1 dogcow
576 1.1 dogcow if (ISSET(cflag, CRTSCTS)) {
577 1.17 jakllsch ulControlHandshake =
578 1.17 jakllsch SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK);
579 1.17 jakllsch ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK);
580 1.1 dogcow } else {
581 1.17 jakllsch ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK);
582 1.17 jakllsch ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK);
583 1.1 dogcow }
584 1.1 dogcow
585 1.17 jakllsch fcs.ulControlHandshake = htole32(ulControlHandshake);
586 1.17 jakllsch fcs.ulFlowReplace = htole32(ulFlowReplace);
587 1.17 jakllsch
588 1.9 jakllsch req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
589 1.17 jakllsch req.bRequest = SLSA_R_SET_FLOW;
590 1.1 dogcow USETW(req.wValue, 0);
591 1.17 jakllsch USETW(req.wIndex, sc->sc_ifnum);
592 1.17 jakllsch USETW(req.wLength, SLSA_RL_SET_FLOW);
593 1.17 jakllsch
594 1.17 jakllsch status = usbd_do_request(sc->sc_udev, &req, &fcs);
595 1.1 dogcow
596 1.17 jakllsch return uslsa_usbd_errno(status);
597 1.1 dogcow }
598