uslsa.c revision 1.22 1 1.22 skrll /* $NetBSD: uslsa.c,v 1.22 2016/11/25 12:56:29 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.22 skrll __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.22 2016/11/25 12:56:29 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.1 dogcow { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 }
147 1.1 dogcow };
148 1.1 dogcow
149 1.14 jakllsch static int uslsa_match(device_t, cfdata_t, void *);
150 1.14 jakllsch static void uslsa_attach(device_t, device_t, void *);
151 1.14 jakllsch static void uslsa_childdet(device_t, device_t);
152 1.14 jakllsch static int uslsa_detach(device_t, int);
153 1.14 jakllsch static int uslsa_activate(device_t, enum devact);
154 1.14 jakllsch
155 1.8 cube CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
156 1.5 dyoung uslsa_attach, uslsa_detach, uslsa_activate, NULL, uslsa_childdet);
157 1.1 dogcow
158 1.17 jakllsch static int
159 1.12 dyoung uslsa_match(device_t parent, cfdata_t match, void *aux)
160 1.1 dogcow {
161 1.20 skrll const struct usbif_attach_arg *uiaa = aux;
162 1.17 jakllsch
163 1.21 msaitoh if (usb_lookup(uslsa_devs, uiaa->uiaa_vendor, uiaa->uiaa_product)
164 1.21 msaitoh != NULL)
165 1.17 jakllsch return UMATCH_VENDOR_PRODUCT;
166 1.21 msaitoh else
167 1.17 jakllsch return UMATCH_NONE;
168 1.1 dogcow }
169 1.1 dogcow
170 1.17 jakllsch static void
171 1.12 dyoung uslsa_attach(device_t parent, device_t self, void *aux)
172 1.1 dogcow {
173 1.17 jakllsch struct uslsa_softc *sc;
174 1.20 skrll const struct usbif_attach_arg *uiaa = aux;
175 1.17 jakllsch const usb_interface_descriptor_t *id;
176 1.17 jakllsch const usb_endpoint_descriptor_t *ed;
177 1.1 dogcow char *devinfop;
178 1.20 skrll struct ucom_attach_args ucaa;
179 1.1 dogcow int i;
180 1.1 dogcow
181 1.17 jakllsch sc = device_private(self);
182 1.17 jakllsch
183 1.8 cube sc->sc_dev = self;
184 1.20 skrll sc->sc_udev = uiaa->uiaa_device;
185 1.20 skrll sc->sc_iface = uiaa->uiaa_iface;
186 1.1 dogcow
187 1.10 plunky aprint_naive("\n");
188 1.10 plunky aprint_normal("\n");
189 1.10 plunky
190 1.17 jakllsch devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
191 1.10 plunky aprint_normal_dev(self, "%s\n", devinfop);
192 1.10 plunky usbd_devinfo_free(devinfop);
193 1.10 plunky
194 1.17 jakllsch id = usbd_get_interface_descriptor(sc->sc_iface);
195 1.1 dogcow
196 1.17 jakllsch sc->sc_ifnum = id->bInterfaceNumber;
197 1.1 dogcow
198 1.20 skrll ucaa.ucaa_info = "Silicon Labs CP210x";
199 1.20 skrll ucaa.ucaa_portno = UCOM_UNK_PORTNO;
200 1.20 skrll ucaa.ucaa_ibufsize = USLSA_BUFSIZE;
201 1.20 skrll ucaa.ucaa_obufsize = USLSA_BUFSIZE;
202 1.20 skrll ucaa.ucaa_ibufsizepad = USLSA_BUFSIZE;
203 1.20 skrll ucaa.ucaa_opkthdrlen = 0;
204 1.20 skrll ucaa.ucaa_device = sc->sc_udev;
205 1.20 skrll ucaa.ucaa_iface = sc->sc_iface;
206 1.20 skrll ucaa.ucaa_methods = &uslsa_methods;
207 1.20 skrll ucaa.ucaa_arg = sc;
208 1.1 dogcow
209 1.21 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
210 1.1 dogcow
211 1.20 skrll ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
212 1.1 dogcow for (i = 0; i < id->bNumEndpoints; i++) {
213 1.1 dogcow int addr, dir, attr;
214 1.1 dogcow
215 1.17 jakllsch ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
216 1.1 dogcow if (ed == NULL) {
217 1.8 cube aprint_error_dev(self,
218 1.17 jakllsch "could not read endpoint descriptor\n");
219 1.17 jakllsch sc->sc_dying = true;
220 1.17 jakllsch return;
221 1.1 dogcow }
222 1.1 dogcow addr = ed->bEndpointAddress;
223 1.1 dogcow dir = UE_GET_DIR(ed->bEndpointAddress);
224 1.1 dogcow attr = ed->bmAttributes & UE_XFERTYPE;
225 1.17 jakllsch if (dir == UE_DIR_IN && attr == UE_BULK) {
226 1.20 skrll ucaa.ucaa_bulkin = addr;
227 1.17 jakllsch } else if (dir == UE_DIR_OUT && attr == UE_BULK) {
228 1.20 skrll ucaa.ucaa_bulkout = addr;
229 1.17 jakllsch } else {
230 1.8 cube aprint_error_dev(self, "unexpected endpoint\n");
231 1.17 jakllsch }
232 1.1 dogcow }
233 1.17 jakllsch aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n",
234 1.20 skrll ucaa.ucaa_bulkin, ucaa.ucaa_bulkout);
235 1.20 skrll if ((ucaa.ucaa_bulkin == -1) || (ucaa.ucaa_bulkout == -1)) {
236 1.17 jakllsch aprint_error_dev(self, "could not find endpoints\n");
237 1.17 jakllsch sc->sc_dying = true;
238 1.17 jakllsch return;
239 1.1 dogcow }
240 1.1 dogcow
241 1.20 skrll sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
242 1.1 dogcow ucomprint, ucomsubmatch);
243 1.1 dogcow
244 1.12 dyoung return;
245 1.1 dogcow }
246 1.1 dogcow
247 1.14 jakllsch static int
248 1.5 dyoung uslsa_activate(device_t self, enum devact act)
249 1.1 dogcow {
250 1.5 dyoung struct uslsa_softc *sc = device_private(self);
251 1.1 dogcow
252 1.1 dogcow switch (act) {
253 1.1 dogcow case DVACT_DEACTIVATE:
254 1.17 jakllsch sc->sc_dying = true;
255 1.11 dyoung return 0;
256 1.11 dyoung default:
257 1.11 dyoung return EOPNOTSUPP;
258 1.1 dogcow }
259 1.1 dogcow }
260 1.1 dogcow
261 1.14 jakllsch static void
262 1.5 dyoung uslsa_childdet(device_t self, device_t child)
263 1.5 dyoung {
264 1.5 dyoung struct uslsa_softc *sc = device_private(self);
265 1.5 dyoung
266 1.5 dyoung KASSERT(sc->sc_subdev == child);
267 1.5 dyoung sc->sc_subdev = NULL;
268 1.5 dyoung }
269 1.5 dyoung
270 1.17 jakllsch static int
271 1.12 dyoung uslsa_detach(device_t self, int flags)
272 1.1 dogcow {
273 1.12 dyoung struct uslsa_softc *sc = device_private(self);
274 1.1 dogcow int rv = 0;
275 1.1 dogcow
276 1.17 jakllsch DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags));
277 1.1 dogcow
278 1.17 jakllsch sc->sc_dying = true;
279 1.1 dogcow
280 1.17 jakllsch if (sc->sc_subdev != NULL) {
281 1.1 dogcow rv = config_detach(sc->sc_subdev, flags);
282 1.17 jakllsch }
283 1.1 dogcow
284 1.21 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
285 1.1 dogcow
286 1.1 dogcow return (rv);
287 1.1 dogcow }
288 1.1 dogcow
289 1.17 jakllsch static int
290 1.17 jakllsch uslsa_usbd_errno(usbd_status status)
291 1.17 jakllsch {
292 1.17 jakllsch switch (status) {
293 1.17 jakllsch case USBD_NORMAL_COMPLETION:
294 1.17 jakllsch return 0;
295 1.17 jakllsch case USBD_STALLED:
296 1.17 jakllsch return EINVAL;
297 1.17 jakllsch default:
298 1.17 jakllsch return EIO;
299 1.17 jakllsch }
300 1.17 jakllsch }
301 1.17 jakllsch
302 1.1 dogcow static void
303 1.1 dogcow uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
304 1.1 dogcow {
305 1.1 dogcow struct uslsa_softc *sc;
306 1.1 dogcow usb_device_request_t req;
307 1.17 jakllsch usbd_status status;
308 1.17 jakllsch uint8_t mdmsts;
309 1.1 dogcow
310 1.1 dogcow sc = vsc;
311 1.1 dogcow
312 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno));
313 1.17 jakllsch
314 1.17 jakllsch if (sc->sc_dying) {
315 1.17 jakllsch return;
316 1.17 jakllsch }
317 1.1 dogcow
318 1.9 jakllsch req.bmRequestType = UT_READ_VENDOR_INTERFACE;
319 1.17 jakllsch req.bRequest = SLSA_R_GET_MDMSTS;
320 1.1 dogcow USETW(req.wValue, 0);
321 1.17 jakllsch USETW(req.wIndex, sc->sc_ifnum);
322 1.17 jakllsch USETW(req.wLength, SLSA_RL_GET_MDMSTS);
323 1.17 jakllsch
324 1.17 jakllsch status = usbd_do_request(sc->sc_udev, &req, &mdmsts);
325 1.17 jakllsch if (status != USBD_NORMAL_COMPLETION) {
326 1.17 jakllsch device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n",
327 1.17 jakllsch __func__, usbd_errstr(status));
328 1.17 jakllsch return;
329 1.17 jakllsch }
330 1.17 jakllsch
331 1.17 jakllsch DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts));
332 1.17 jakllsch
333 1.17 jakllsch if (lsr != NULL) {
334 1.17 jakllsch *lsr = 0;
335 1.17 jakllsch }
336 1.1 dogcow
337 1.17 jakllsch if (msr != NULL) {
338 1.17 jakllsch *msr = 0;
339 1.17 jakllsch *msr |= ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0;
340 1.17 jakllsch *msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0;
341 1.17 jakllsch *msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0;
342 1.17 jakllsch *msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0;
343 1.17 jakllsch }
344 1.1 dogcow }
345 1.1 dogcow
346 1.1 dogcow static void
347 1.1 dogcow uslsa_set(void *vsc, int portno, int reg, int onoff)
348 1.1 dogcow {
349 1.1 dogcow struct uslsa_softc *sc;
350 1.1 dogcow
351 1.1 dogcow sc = vsc;
352 1.1 dogcow
353 1.21 msaitoh DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno,
354 1.21 msaitoh reg, onoff));
355 1.17 jakllsch
356 1.17 jakllsch if (sc->sc_dying) {
357 1.17 jakllsch return;
358 1.17 jakllsch }
359 1.1 dogcow
360 1.1 dogcow switch (reg) {
361 1.1 dogcow case UCOM_SET_DTR:
362 1.17 jakllsch if (uslsa_request_set(sc, SLSA_R_SET_MHS,
363 1.17 jakllsch SLSA_RV_SET_MHS_DTR_MASK |
364 1.17 jakllsch (onoff ? SLSA_RV_SET_MHS_DTR : 0))) {
365 1.17 jakllsch device_printf(sc->sc_dev, "SET_MHS/DTR failed\n");
366 1.17 jakllsch }
367 1.1 dogcow break;
368 1.1 dogcow case UCOM_SET_RTS:
369 1.17 jakllsch if (uslsa_request_set(sc, SLSA_R_SET_MHS,
370 1.17 jakllsch SLSA_RV_SET_MHS_RTS_MASK |
371 1.17 jakllsch (onoff ? SLSA_RV_SET_MHS_RTS : 0))) {
372 1.17 jakllsch device_printf(sc->sc_dev, "SET_MHS/RTS failed\n");
373 1.17 jakllsch }
374 1.1 dogcow break;
375 1.1 dogcow case UCOM_SET_BREAK:
376 1.17 jakllsch if (uslsa_request_set(sc, SLSA_R_SET_BREAK,
377 1.17 jakllsch (onoff ? SLSA_RV_SET_BREAK_ENABLE :
378 1.17 jakllsch SLSA_RV_SET_BREAK_DISABLE))) {
379 1.17 jakllsch device_printf(sc->sc_dev, "SET_BREAK failed\n");
380 1.17 jakllsch }
381 1.1 dogcow break;
382 1.1 dogcow default:
383 1.1 dogcow break;
384 1.1 dogcow }
385 1.1 dogcow }
386 1.1 dogcow
387 1.1 dogcow static int
388 1.17 jakllsch uslsa_param(void *vsc, int portno, struct termios *t)
389 1.1 dogcow {
390 1.1 dogcow struct uslsa_softc *sc;
391 1.19 reinoud usb_device_request_t req;
392 1.19 reinoud usbd_status status;
393 1.19 reinoud uint16_t value;
394 1.19 reinoud uint32_t baud;
395 1.17 jakllsch int ret;
396 1.1 dogcow
397 1.1 dogcow sc = vsc;
398 1.1 dogcow
399 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t));
400 1.17 jakllsch
401 1.17 jakllsch if (sc->sc_dying) {
402 1.17 jakllsch return EIO;
403 1.17 jakllsch }
404 1.17 jakllsch
405 1.19 reinoud req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
406 1.19 reinoud req.bRequest = SLSA_R_SET_BAUDRATE;
407 1.19 reinoud USETW(req.wValue, 0);
408 1.19 reinoud USETW(req.wIndex, sc->sc_ifnum);
409 1.19 reinoud USETW(req.wLength, 4);
410 1.17 jakllsch
411 1.19 reinoud baud = t->c_ospeed;
412 1.19 reinoud status = usbd_do_request(sc->sc_udev, &req, &baud);
413 1.19 reinoud if (status != USBD_NORMAL_COMPLETION) {
414 1.19 reinoud /* fallback method for devices that don't know SET_BAUDRATE */
415 1.19 reinoud /* hope we calculate it right */
416 1.19 reinoud device_printf(sc->sc_dev, "%s: set baudrate %d, failed %s,"
417 1.19 reinoud " using set bauddiv\n",
418 1.19 reinoud __func__, baud, usbd_errstr(status));
419 1.19 reinoud
420 1.19 reinoud value = SLSA_RV_BAUDDIV(t->c_ospeed);
421 1.21 msaitoh if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value))
422 1.21 msaitoh != 0) {
423 1.19 reinoud device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n",
424 1.19 reinoud __func__);
425 1.19 reinoud return ret;
426 1.19 reinoud }
427 1.17 jakllsch }
428 1.17 jakllsch
429 1.17 jakllsch value = 0;
430 1.1 dogcow
431 1.17 jakllsch if (ISSET(t->c_cflag, CSTOPB)) {
432 1.17 jakllsch value |= SLSA_RV_LINE_CTL_STOP_2;
433 1.17 jakllsch } else {
434 1.17 jakllsch value |= SLSA_RV_LINE_CTL_STOP_1;
435 1.17 jakllsch }
436 1.1 dogcow
437 1.1 dogcow if (ISSET(t->c_cflag, PARENB)) {
438 1.17 jakllsch if (ISSET(t->c_cflag, PARODD)) {
439 1.17 jakllsch value |= SLSA_RV_LINE_CTL_PARITY_ODD;
440 1.17 jakllsch } else {
441 1.17 jakllsch value |= SLSA_RV_LINE_CTL_PARITY_EVEN;
442 1.17 jakllsch }
443 1.17 jakllsch } else {
444 1.17 jakllsch value |= SLSA_RV_LINE_CTL_PARITY_NONE;
445 1.17 jakllsch }
446 1.1 dogcow
447 1.1 dogcow switch (ISSET(t->c_cflag, CSIZE)) {
448 1.1 dogcow case CS5:
449 1.17 jakllsch value |= SLSA_RV_LINE_CTL_LEN_5;
450 1.1 dogcow break;
451 1.1 dogcow case CS6:
452 1.17 jakllsch value |= SLSA_RV_LINE_CTL_LEN_6;
453 1.1 dogcow break;
454 1.1 dogcow case CS7:
455 1.17 jakllsch value |= SLSA_RV_LINE_CTL_LEN_7;
456 1.1 dogcow break;
457 1.1 dogcow case CS8:
458 1.17 jakllsch value |= SLSA_RV_LINE_CTL_LEN_8;
459 1.1 dogcow break;
460 1.1 dogcow }
461 1.1 dogcow
462 1.17 jakllsch DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to 0x%x\n",
463 1.17 jakllsch __func__, value));
464 1.17 jakllsch if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) {
465 1.17 jakllsch device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
466 1.17 jakllsch return ret;
467 1.17 jakllsch }
468 1.17 jakllsch
469 1.17 jakllsch if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) {
470 1.17 jakllsch device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
471 1.17 jakllsch }
472 1.17 jakllsch
473 1.17 jakllsch return ret;
474 1.17 jakllsch }
475 1.17 jakllsch
476 1.17 jakllsch static int
477 1.17 jakllsch uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p)
478 1.17 jakllsch {
479 1.17 jakllsch struct uslsa_softc *sc;
480 1.1 dogcow
481 1.17 jakllsch sc = vsc;
482 1.17 jakllsch
483 1.17 jakllsch switch (cmd) {
484 1.17 jakllsch case TIOCMGET:
485 1.17 jakllsch ucom_status_change(device_private(sc->sc_subdev));
486 1.17 jakllsch return EPASSTHROUGH;
487 1.17 jakllsch default:
488 1.17 jakllsch return EPASSTHROUGH;
489 1.17 jakllsch }
490 1.1 dogcow
491 1.1 dogcow return 0;
492 1.1 dogcow }
493 1.1 dogcow
494 1.1 dogcow static int
495 1.1 dogcow uslsa_open(void *vsc, int portno)
496 1.1 dogcow {
497 1.1 dogcow struct uslsa_softc *sc;
498 1.1 dogcow
499 1.1 dogcow sc = vsc;
500 1.1 dogcow
501 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
502 1.1 dogcow
503 1.17 jakllsch if (sc->sc_dying) {
504 1.17 jakllsch return EIO;
505 1.17 jakllsch }
506 1.1 dogcow
507 1.17 jakllsch return uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
508 1.17 jakllsch SLSA_RV_IFC_ENABLE_ENABLE);
509 1.1 dogcow }
510 1.1 dogcow
511 1.14 jakllsch static void
512 1.1 dogcow uslsa_close(void *vsc, int portno)
513 1.1 dogcow {
514 1.1 dogcow struct uslsa_softc *sc;
515 1.1 dogcow
516 1.1 dogcow sc = vsc;
517 1.1 dogcow
518 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
519 1.17 jakllsch
520 1.17 jakllsch if (sc->sc_dying) {
521 1.1 dogcow return;
522 1.17 jakllsch }
523 1.1 dogcow
524 1.17 jakllsch (void)uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
525 1.17 jakllsch SLSA_RV_IFC_ENABLE_DISABLE);
526 1.1 dogcow }
527 1.1 dogcow
528 1.1 dogcow static int
529 1.1 dogcow uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
530 1.1 dogcow {
531 1.1 dogcow usb_device_request_t req;
532 1.17 jakllsch usbd_status status;
533 1.1 dogcow
534 1.9 jakllsch req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
535 1.1 dogcow req.bRequest = request;
536 1.1 dogcow USETW(req.wValue, value);
537 1.17 jakllsch USETW(req.wIndex, sc->sc_ifnum);
538 1.1 dogcow USETW(req.wLength, 0);
539 1.1 dogcow
540 1.17 jakllsch status = usbd_do_request(sc->sc_udev, &req, NULL);
541 1.17 jakllsch
542 1.17 jakllsch return uslsa_usbd_errno(status);
543 1.1 dogcow }
544 1.1 dogcow
545 1.17 jakllsch static int
546 1.1 dogcow uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
547 1.1 dogcow {
548 1.17 jakllsch struct slsa_fcs fcs;
549 1.1 dogcow usb_device_request_t req;
550 1.17 jakllsch uint32_t ulControlHandshake;
551 1.17 jakllsch uint32_t ulFlowReplace;
552 1.17 jakllsch usbd_status status;
553 1.1 dogcow
554 1.17 jakllsch DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag));
555 1.1 dogcow
556 1.9 jakllsch req.bmRequestType = UT_READ_VENDOR_INTERFACE;
557 1.17 jakllsch req.bRequest = SLSA_R_GET_FLOW;
558 1.1 dogcow USETW(req.wValue, 0);
559 1.17 jakllsch USETW(req.wIndex, sc->sc_ifnum);
560 1.17 jakllsch USETW(req.wLength, SLSA_RL_GET_FLOW);
561 1.17 jakllsch
562 1.17 jakllsch status = usbd_do_request(sc->sc_udev, &req, &fcs);
563 1.17 jakllsch if (status != USBD_NORMAL_COMPLETION) {
564 1.17 jakllsch device_printf(sc->sc_dev, "%s: GET_FLOW %s\n",
565 1.17 jakllsch __func__, usbd_errstr(status));
566 1.17 jakllsch return uslsa_usbd_errno(status);
567 1.17 jakllsch }
568 1.1 dogcow
569 1.17 jakllsch ulControlHandshake = le32toh(fcs.ulControlHandshake);
570 1.17 jakllsch ulFlowReplace = le32toh(fcs.ulFlowReplace);
571 1.1 dogcow
572 1.1 dogcow if (ISSET(cflag, CRTSCTS)) {
573 1.17 jakllsch ulControlHandshake =
574 1.17 jakllsch SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK);
575 1.17 jakllsch ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK);
576 1.1 dogcow } else {
577 1.17 jakllsch ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK);
578 1.17 jakllsch ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK);
579 1.1 dogcow }
580 1.1 dogcow
581 1.17 jakllsch fcs.ulControlHandshake = htole32(ulControlHandshake);
582 1.17 jakllsch fcs.ulFlowReplace = htole32(ulFlowReplace);
583 1.17 jakllsch
584 1.9 jakllsch req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
585 1.17 jakllsch req.bRequest = SLSA_R_SET_FLOW;
586 1.1 dogcow USETW(req.wValue, 0);
587 1.17 jakllsch USETW(req.wIndex, sc->sc_ifnum);
588 1.17 jakllsch USETW(req.wLength, SLSA_RL_SET_FLOW);
589 1.17 jakllsch
590 1.17 jakllsch status = usbd_do_request(sc->sc_udev, &req, &fcs);
591 1.1 dogcow
592 1.17 jakllsch return uslsa_usbd_errno(status);
593 1.1 dogcow }
594