xref: /freebsd/sys/dev/usb/serial/ubser.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*-
2  * Copyright (c) 2004 Bernd Walter <ticso@FreeBSD.org>
3  *
4  * $URL: https://devel.bwct.de/svn/projects/ubser/ubser.c $
5  * $Date: 2004-02-29 01:53:10 +0100 (Sun, 29 Feb 2004) $
6  * $Author: ticso $
7  * $Rev: 1127 $
8  */
9 
10 /*-
11  * SPDX-License-Identifier: BSD-2-Clause
12  *
13  * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 /*-
39  * Copyright (c) 2000 The NetBSD Foundation, Inc.
40  * All rights reserved.
41  *
42  * This code is derived from software contributed to The NetBSD Foundation
43  * by Lennart Augustsson (lennart@augustsson.net).
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64  * POSSIBILITY OF SUCH DAMAGE.
65  */
66 
67 #include <sys/cdefs.h>
68 /*
69  * BWCT serial adapter driver
70  */
71 
72 #include <sys/stdint.h>
73 #include <sys/stddef.h>
74 #include <sys/param.h>
75 #include <sys/queue.h>
76 #include <sys/types.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/bus.h>
80 #include <sys/module.h>
81 #include <sys/lock.h>
82 #include <sys/mutex.h>
83 #include <sys/condvar.h>
84 #include <sys/sysctl.h>
85 #include <sys/sx.h>
86 #include <sys/unistd.h>
87 #include <sys/callout.h>
88 #include <sys/malloc.h>
89 #include <sys/priv.h>
90 
91 #include <dev/usb/usb.h>
92 #include <dev/usb/usbdi.h>
93 #include <dev/usb/usbdi_util.h>
94 #include "usbdevs.h"
95 
96 #define	USB_DEBUG_VAR ubser_debug
97 #include <dev/usb/usb_debug.h>
98 #include <dev/usb/usb_process.h>
99 
100 #include <dev/usb/serial/usb_serial.h>
101 
102 #define	UBSER_UNIT_MAX	32
103 
104 /* Vendor Interface Requests */
105 #define	VENDOR_GET_NUMSER		0x01
106 #define	VENDOR_SET_BREAK		0x02
107 #define	VENDOR_CLEAR_BREAK		0x03
108 
109 #ifdef USB_DEBUG
110 static int ubser_debug = 0;
111 
112 static SYSCTL_NODE(_hw_usb, OID_AUTO, ubser, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
113     "USB ubser");
114 SYSCTL_INT(_hw_usb_ubser, OID_AUTO, debug, CTLFLAG_RWTUN,
115     &ubser_debug, 0, "ubser debug level");
116 #endif
117 
118 enum {
119 	UBSER_BULK_DT_WR,
120 	UBSER_BULK_DT_RD,
121 	UBSER_N_TRANSFER,
122 };
123 
124 struct ubser_softc {
125 	struct ucom_super_softc sc_super_ucom;
126 	struct ucom_softc sc_ucom[UBSER_UNIT_MAX];
127 
128 	struct usb_xfer *sc_xfer[UBSER_N_TRANSFER];
129 	struct usb_device *sc_udev;
130 	struct mtx sc_mtx;
131 
132 	uint16_t sc_tx_size;
133 
134 	uint8_t	sc_numser;
135 	uint8_t	sc_iface_no;
136 	uint8_t	sc_iface_index;
137 	uint8_t	sc_curr_tx_unit;
138 };
139 
140 /* prototypes */
141 
142 static device_probe_t ubser_probe;
143 static device_attach_t ubser_attach;
144 static device_detach_t ubser_detach;
145 static void ubser_free_softc(struct ubser_softc *);
146 
147 static usb_callback_t ubser_write_callback;
148 static usb_callback_t ubser_read_callback;
149 
150 static void	ubser_free(struct ucom_softc *);
151 static int	ubser_pre_param(struct ucom_softc *, struct termios *);
152 static void	ubser_cfg_set_break(struct ucom_softc *, uint8_t);
153 static void	ubser_cfg_get_status(struct ucom_softc *, uint8_t *,
154 		    uint8_t *);
155 static void	ubser_start_read(struct ucom_softc *);
156 static void	ubser_stop_read(struct ucom_softc *);
157 static void	ubser_start_write(struct ucom_softc *);
158 static void	ubser_stop_write(struct ucom_softc *);
159 static void	ubser_poll(struct ucom_softc *ucom);
160 
161 static const struct usb_config ubser_config[UBSER_N_TRANSFER] = {
162 	[UBSER_BULK_DT_WR] = {
163 		.type = UE_BULK,
164 		.endpoint = UE_ADDR_ANY,
165 		.direction = UE_DIR_OUT,
166 		.bufsize = 0,	/* use wMaxPacketSize */
167 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
168 		.callback = &ubser_write_callback,
169 	},
170 
171 	[UBSER_BULK_DT_RD] = {
172 		.type = UE_BULK,
173 		.endpoint = UE_ADDR_ANY,
174 		.direction = UE_DIR_IN,
175 		.bufsize = 0,	/* use wMaxPacketSize */
176 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
177 		.callback = &ubser_read_callback,
178 	},
179 };
180 
181 static const struct ucom_callback ubser_callback = {
182 	.ucom_cfg_set_break = &ubser_cfg_set_break,
183 	.ucom_cfg_get_status = &ubser_cfg_get_status,
184 	.ucom_pre_param = &ubser_pre_param,
185 	.ucom_start_read = &ubser_start_read,
186 	.ucom_stop_read = &ubser_stop_read,
187 	.ucom_start_write = &ubser_start_write,
188 	.ucom_stop_write = &ubser_stop_write,
189 	.ucom_poll = &ubser_poll,
190 	.ucom_free = &ubser_free,
191 };
192 
193 static device_method_t ubser_methods[] = {
194 	DEVMETHOD(device_probe, ubser_probe),
195 	DEVMETHOD(device_attach, ubser_attach),
196 	DEVMETHOD(device_detach, ubser_detach),
197 	DEVMETHOD_END
198 };
199 
200 static driver_t ubser_driver = {
201 	.name = "ubser",
202 	.methods = ubser_methods,
203 	.size = sizeof(struct ubser_softc),
204 };
205 
206 DRIVER_MODULE(ubser, uhub, ubser_driver, NULL, NULL);
207 MODULE_DEPEND(ubser, ucom, 1, 1, 1);
208 MODULE_DEPEND(ubser, usb, 1, 1, 1);
209 MODULE_VERSION(ubser, 1);
210 
211 static int
212 ubser_probe(device_t dev)
213 {
214 	struct usb_attach_arg *uaa = device_get_ivars(dev);
215 
216 	if (uaa->usb_mode != USB_MODE_HOST) {
217 		return (ENXIO);
218 	}
219 	/* check if this is a BWCT vendor specific ubser interface */
220 	if ((strcmp(usb_get_manufacturer(uaa->device), "BWCT") == 0) &&
221 	    (uaa->info.bInterfaceClass == 0xff) &&
222 	    (uaa->info.bInterfaceSubClass == 0x00))
223 		return (0);
224 
225 	return (ENXIO);
226 }
227 
228 static int
229 ubser_attach(device_t dev)
230 {
231 	struct usb_attach_arg *uaa = device_get_ivars(dev);
232 	struct ubser_softc *sc = device_get_softc(dev);
233 	struct usb_device_request req;
234 	uint8_t n;
235 	int error;
236 
237 	device_set_usb_desc(dev);
238 	mtx_init(&sc->sc_mtx, "ubser", NULL, MTX_DEF);
239 	ucom_ref(&sc->sc_super_ucom);
240 
241 	sc->sc_iface_no = uaa->info.bIfaceNum;
242 	sc->sc_iface_index = uaa->info.bIfaceIndex;
243 	sc->sc_udev = uaa->device;
244 
245 	/* get number of serials */
246 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
247 	req.bRequest = VENDOR_GET_NUMSER;
248 	USETW(req.wValue, 0);
249 	req.wIndex[0] = sc->sc_iface_no;
250 	req.wIndex[1] = 0;
251 	USETW(req.wLength, 1);
252 	error = usbd_do_request_flags(uaa->device, NULL,
253 	    &req, &sc->sc_numser,
254 	    0, NULL, USB_DEFAULT_TIMEOUT);
255 
256 	if (error || (sc->sc_numser == 0)) {
257 		device_printf(dev, "failed to get number "
258 		    "of serial ports: %s\n",
259 		    usbd_errstr(error));
260 		goto detach;
261 	}
262 	if (sc->sc_numser > UBSER_UNIT_MAX)
263 		sc->sc_numser = UBSER_UNIT_MAX;
264 
265 	device_printf(dev, "found %i serials\n", sc->sc_numser);
266 
267 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
268 	    sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &sc->sc_mtx);
269 	if (error) {
270 		goto detach;
271 	}
272 	sc->sc_tx_size = usbd_xfer_max_len(sc->sc_xfer[UBSER_BULK_DT_WR]);
273 
274 	if (sc->sc_tx_size == 0) {
275 		DPRINTFN(0, "invalid tx_size\n");
276 		goto detach;
277 	}
278 	/* initialize port numbers */
279 
280 	for (n = 0; n < sc->sc_numser; n++) {
281 		sc->sc_ucom[n].sc_portno = n;
282 	}
283 
284 	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
285 	    sc->sc_numser, sc, &ubser_callback, &sc->sc_mtx);
286 	if (error) {
287 		goto detach;
288 	}
289 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
290 
291 	mtx_lock(&sc->sc_mtx);
292 	usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]);
293 	usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]);
294 	usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
295 	mtx_unlock(&sc->sc_mtx);
296 
297 	return (0);			/* success */
298 
299 detach:
300 	ubser_detach(dev);
301 	return (ENXIO);			/* failure */
302 }
303 
304 static int
305 ubser_detach(device_t dev)
306 {
307 	struct ubser_softc *sc = device_get_softc(dev);
308 
309 	DPRINTF("\n");
310 
311 	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
312 	usbd_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER);
313 
314 	device_claim_softc(dev);
315 
316 	ubser_free_softc(sc);
317 
318 	return (0);
319 }
320 
321 UCOM_UNLOAD_DRAIN(ubser);
322 
323 static void
324 ubser_free_softc(struct ubser_softc *sc)
325 {
326 	if (ucom_unref(&sc->sc_super_ucom)) {
327 		mtx_destroy(&sc->sc_mtx);
328 		device_free_softc(sc);
329 	}
330 }
331 
332 static void
333 ubser_free(struct ucom_softc *ucom)
334 {
335 	ubser_free_softc(ucom->sc_parent);
336 }
337 
338 static int
339 ubser_pre_param(struct ucom_softc *ucom, struct termios *t)
340 {
341 	DPRINTF("\n");
342 
343 	/*
344 	 * The firmware on our devices can only do 8n1@9600bps
345 	 * without handshake.
346 	 * We refuse to accept other configurations.
347 	 */
348 
349 	/* ensure 9600bps */
350 	switch (t->c_ospeed) {
351 	case 9600:
352 		break;
353 	default:
354 		return (EINVAL);
355 	}
356 
357 	/* 2 stop bits not possible */
358 	if (t->c_cflag & CSTOPB)
359 		return (EINVAL);
360 
361 	/* XXX parity handling not possible with current firmware */
362 	if (t->c_cflag & PARENB)
363 		return (EINVAL);
364 
365 	/* we can only do 8 data bits */
366 	switch (t->c_cflag & CSIZE) {
367 	case CS8:
368 		break;
369 	default:
370 		return (EINVAL);
371 	}
372 
373 	/* we can't do any kind of hardware handshaking */
374 	if ((t->c_cflag &
375 	    (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
376 		return (EINVAL);
377 
378 	/*
379 	 * XXX xon/xoff not supported by the firmware!
380 	 * This is handled within FreeBSD only and may overflow buffers
381 	 * because of delayed reaction due to device buffering.
382 	 */
383 
384 	return (0);
385 }
386 
387 static __inline void
388 ubser_inc_tx_unit(struct ubser_softc *sc)
389 {
390 	sc->sc_curr_tx_unit++;
391 	if (sc->sc_curr_tx_unit >= sc->sc_numser) {
392 		sc->sc_curr_tx_unit = 0;
393 	}
394 }
395 
396 static void
397 ubser_write_callback(struct usb_xfer *xfer, usb_error_t error)
398 {
399 	struct ubser_softc *sc = usbd_xfer_softc(xfer);
400 	struct usb_page_cache *pc;
401 	uint8_t buf[1];
402 	uint8_t first_unit = sc->sc_curr_tx_unit;
403 	uint32_t actlen;
404 
405 	switch (USB_GET_STATE(xfer)) {
406 	case USB_ST_SETUP:
407 	case USB_ST_TRANSFERRED:
408 tr_setup:
409 		pc = usbd_xfer_get_frame(xfer, 0);
410 		do {
411 			if (ucom_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
412 			    pc, 1, sc->sc_tx_size - 1,
413 			    &actlen)) {
414 				buf[0] = sc->sc_curr_tx_unit;
415 
416 				usbd_copy_in(pc, 0, buf, 1);
417 
418 				usbd_xfer_set_frame_len(xfer, 0, actlen + 1);
419 				usbd_transfer_submit(xfer);
420 
421 				ubser_inc_tx_unit(sc);	/* round robin */
422 
423 				break;
424 			}
425 			ubser_inc_tx_unit(sc);
426 
427 		} while (sc->sc_curr_tx_unit != first_unit);
428 
429 		return;
430 
431 	default:			/* Error */
432 		if (error != USB_ERR_CANCELLED) {
433 			/* try to clear stall first */
434 			usbd_xfer_set_stall(xfer);
435 			goto tr_setup;
436 		}
437 		return;
438 	}
439 }
440 
441 static void
442 ubser_read_callback(struct usb_xfer *xfer, usb_error_t error)
443 {
444 	struct ubser_softc *sc = usbd_xfer_softc(xfer);
445 	struct usb_page_cache *pc;
446 	uint8_t buf[1];
447 	int actlen;
448 
449 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
450 
451 	switch (USB_GET_STATE(xfer)) {
452 	case USB_ST_TRANSFERRED:
453 		if (actlen < 1) {
454 			DPRINTF("invalid actlen=0!\n");
455 			goto tr_setup;
456 		}
457 		pc = usbd_xfer_get_frame(xfer, 0);
458 		usbd_copy_out(pc, 0, buf, 1);
459 
460 		if (buf[0] >= sc->sc_numser) {
461 			DPRINTF("invalid serial number!\n");
462 			goto tr_setup;
463 		}
464 		ucom_put_data(sc->sc_ucom + buf[0], pc, 1, actlen - 1);
465 
466 	case USB_ST_SETUP:
467 tr_setup:
468 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
469 		usbd_transfer_submit(xfer);
470 		return;
471 
472 	default:			/* Error */
473 		if (error != USB_ERR_CANCELLED) {
474 			/* try to clear stall first */
475 			usbd_xfer_set_stall(xfer);
476 			goto tr_setup;
477 		}
478 		return;
479 	}
480 }
481 
482 static void
483 ubser_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
484 {
485 	struct ubser_softc *sc = ucom->sc_parent;
486 	uint8_t x = ucom->sc_portno;
487 	struct usb_device_request req;
488 	usb_error_t err;
489 
490 	if (onoff) {
491 		req.bmRequestType = UT_READ_VENDOR_INTERFACE;
492 		req.bRequest = VENDOR_SET_BREAK;
493 		req.wValue[0] = x;
494 		req.wValue[1] = 0;
495 		req.wIndex[0] = sc->sc_iface_no;
496 		req.wIndex[1] = 0;
497 		USETW(req.wLength, 0);
498 
499 		err = ucom_cfg_do_request(sc->sc_udev, ucom,
500 		    &req, NULL, 0, 1000);
501 		if (err) {
502 			DPRINTFN(0, "send break failed, error=%s\n",
503 			    usbd_errstr(err));
504 		}
505 	}
506 }
507 
508 static void
509 ubser_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
510 {
511 	/* fake status bits */
512 	*lsr = 0;
513 	*msr = SER_DCD;
514 }
515 
516 static void
517 ubser_start_read(struct ucom_softc *ucom)
518 {
519 	struct ubser_softc *sc = ucom->sc_parent;
520 
521 	usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
522 }
523 
524 static void
525 ubser_stop_read(struct ucom_softc *ucom)
526 {
527 	struct ubser_softc *sc = ucom->sc_parent;
528 
529 	usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]);
530 }
531 
532 static void
533 ubser_start_write(struct ucom_softc *ucom)
534 {
535 	struct ubser_softc *sc = ucom->sc_parent;
536 
537 	usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]);
538 }
539 
540 static void
541 ubser_stop_write(struct ucom_softc *ucom)
542 {
543 	struct ubser_softc *sc = ucom->sc_parent;
544 
545 	usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]);
546 }
547 
548 static void
549 ubser_poll(struct ucom_softc *ucom)
550 {
551 	struct ubser_softc *sc = ucom->sc_parent;
552 	usbd_transfer_poll(sc->sc_xfer, UBSER_N_TRANSFER);
553 }
554