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