xref: /freebsd/sys/dev/usb/serial/umct.c (revision d2b2128a286a00ee53d79cb88b4e59bf42525cf9)
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 /*-
5  * Copyright (c) 2003 Scott Long
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /*
32  * Driver for the MCT (Magic Control Technology) USB-RS232 Converter.
33  * Based on the superb documentation from the linux mct_u232 driver by
34  * Wolfgang Grandeggar <wolfgang@cec.ch>.
35  * This device smells a lot like the Belkin F5U103, except that it has
36  * suffered some mild brain-damage.  This driver is based off of the ubsa.c
37  * driver from Alexander Kabaev <kan@FreeBSD.org>.  Merging the two together
38  * might be useful, though the subtle differences might lead to lots of
39  * #ifdef's.
40  */
41 
42 /*
43  * NOTE: all function names beginning like "umct_cfg_" can only
44  * be called from within the config thread function !
45  */
46 
47 #include "usbdevs.h"
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usb_mfunc.h>
50 #include <dev/usb/usb_error.h>
51 #include <dev/usb/usb_cdc.h>
52 
53 #define	USB_DEBUG_VAR usb2_debug
54 
55 #include <dev/usb/usb_core.h>
56 #include <dev/usb/usb_debug.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_request.h>
59 #include <dev/usb/usb_lookup.h>
60 #include <dev/usb/usb_util.h>
61 #include <dev/usb/usb_busdma.h>
62 #include <dev/usb/usb_device.h>
63 
64 #include <dev/usb/serial/usb_serial.h>
65 
66 /* The UMCT advertises the standard 8250 UART registers */
67 #define	UMCT_GET_MSR		2	/* Get Modem Status Register */
68 #define	UMCT_GET_MSR_SIZE	1
69 #define	UMCT_GET_LCR		6	/* Get Line Control Register */
70 #define	UMCT_GET_LCR_SIZE	1
71 #define	UMCT_SET_BAUD		5	/* Set the Baud Rate Divisor */
72 #define	UMCT_SET_BAUD_SIZE	4
73 #define	UMCT_SET_LCR		7	/* Set Line Control Register */
74 #define	UMCT_SET_LCR_SIZE	1
75 #define	UMCT_SET_MCR		10	/* Set Modem Control Register */
76 #define	UMCT_SET_MCR_SIZE	1
77 
78 #define	UMCT_INTR_INTERVAL	100
79 #define	UMCT_IFACE_INDEX	0
80 #define	UMCT_CONFIG_INDEX	1
81 
82 enum {
83 	UMCT_BULK_DT_WR,
84 	UMCT_BULK_DT_RD,
85 	UMCT_INTR_DT_RD,
86 	UMCT_N_TRANSFER,
87 };
88 
89 struct umct_softc {
90 	struct usb2_com_super_softc sc_super_ucom;
91 	struct usb2_com_softc sc_ucom;
92 
93 	struct usb2_device *sc_udev;
94 	struct usb2_xfer *sc_xfer[UMCT_N_TRANSFER];
95 	struct mtx sc_mtx;
96 
97 	uint32_t sc_unit;
98 
99 	uint16_t sc_obufsize;
100 
101 	uint8_t	sc_lsr;
102 	uint8_t	sc_msr;
103 	uint8_t	sc_lcr;
104 	uint8_t	sc_mcr;
105 	uint8_t	sc_iface_no;
106 	uint8_t	sc_name[16];
107 };
108 
109 /* prototypes */
110 
111 static device_probe_t umct_probe;
112 static device_attach_t umct_attach;
113 static device_detach_t umct_detach;
114 
115 static usb2_callback_t umct_intr_callback;
116 static usb2_callback_t umct_write_callback;
117 static usb2_callback_t umct_read_callback;
118 
119 static void	umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
120 		    uint16_t len, uint32_t value);
121 static void	umct_cfg_get_status(struct usb2_com_softc *, uint8_t *,
122 		    uint8_t *);
123 static void	umct_cfg_set_break(struct usb2_com_softc *, uint8_t);
124 static void	umct_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
125 static void	umct_cfg_set_rts(struct usb2_com_softc *, uint8_t);
126 static uint8_t	umct_calc_baud(uint32_t);
127 static int	umct_pre_param(struct usb2_com_softc *, struct termios *);
128 static void	umct_cfg_param(struct usb2_com_softc *, struct termios *);
129 static void	umct_start_read(struct usb2_com_softc *);
130 static void	umct_stop_read(struct usb2_com_softc *);
131 static void	umct_start_write(struct usb2_com_softc *);
132 static void	umct_stop_write(struct usb2_com_softc *);
133 
134 static const struct usb2_config umct_config[UMCT_N_TRANSFER] = {
135 
136 	[UMCT_BULK_DT_WR] = {
137 		.type = UE_BULK,
138 		.endpoint = UE_ADDR_ANY,
139 		.direction = UE_DIR_OUT,
140 		.mh.bufsize = 0,	/* use wMaxPacketSize */
141 		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
142 		.mh.callback = &umct_write_callback,
143 	},
144 
145 	[UMCT_BULK_DT_RD] = {
146 		.type = UE_INTERRUPT,
147 		.endpoint = UE_ADDR_ANY,
148 		.direction = UE_DIR_IN,
149 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
150 		.mh.bufsize = 0,	/* use wMaxPacketSize */
151 		.mh.callback = &umct_read_callback,
152 		.ep_index = 0,		/* first interrupt endpoint */
153 	},
154 
155 	[UMCT_INTR_DT_RD] = {
156 		.type = UE_INTERRUPT,
157 		.endpoint = UE_ADDR_ANY,
158 		.direction = UE_DIR_IN,
159 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
160 		.mh.bufsize = 0,	/* use wMaxPacketSize */
161 		.mh.callback = &umct_intr_callback,
162 		.ep_index = 1,		/* second interrupt endpoint */
163 	},
164 };
165 
166 static const struct usb2_com_callback umct_callback = {
167 	.usb2_com_cfg_get_status = &umct_cfg_get_status,
168 	.usb2_com_cfg_set_dtr = &umct_cfg_set_dtr,
169 	.usb2_com_cfg_set_rts = &umct_cfg_set_rts,
170 	.usb2_com_cfg_set_break = &umct_cfg_set_break,
171 	.usb2_com_cfg_param = &umct_cfg_param,
172 	.usb2_com_pre_param = &umct_pre_param,
173 	.usb2_com_start_read = &umct_start_read,
174 	.usb2_com_stop_read = &umct_stop_read,
175 	.usb2_com_start_write = &umct_start_write,
176 	.usb2_com_stop_write = &umct_stop_write,
177 };
178 
179 static const struct usb2_device_id umct_devs[] = {
180 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
181 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
182 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
183 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
184 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
185 };
186 
187 static device_method_t umct_methods[] = {
188 	DEVMETHOD(device_probe, umct_probe),
189 	DEVMETHOD(device_attach, umct_attach),
190 	DEVMETHOD(device_detach, umct_detach),
191 	{0, 0}
192 };
193 
194 static devclass_t umct_devclass;
195 
196 static driver_t umct_driver = {
197 	.name = "umct",
198 	.methods = umct_methods,
199 	.size = sizeof(struct umct_softc),
200 };
201 
202 DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
203 MODULE_DEPEND(umct, ucom, 1, 1, 1);
204 MODULE_DEPEND(umct, usb, 1, 1, 1);
205 
206 static int
207 umct_probe(device_t dev)
208 {
209 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
210 
211 	if (uaa->usb2_mode != USB_MODE_HOST) {
212 		return (ENXIO);
213 	}
214 	if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
215 		return (ENXIO);
216 	}
217 	if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
218 		return (ENXIO);
219 	}
220 	return (usb2_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
221 }
222 
223 static int
224 umct_attach(device_t dev)
225 {
226 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
227 	struct umct_softc *sc = device_get_softc(dev);
228 	int32_t error;
229 	uint16_t maxp;
230 	uint8_t iface_index;
231 
232 	sc->sc_udev = uaa->device;
233 	sc->sc_unit = device_get_unit(dev);
234 
235 	device_set_usb2_desc(dev);
236 	mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
237 
238 	snprintf(sc->sc_name, sizeof(sc->sc_name),
239 	    "%s", device_get_nameunit(dev));
240 
241 	sc->sc_iface_no = uaa->info.bIfaceNum;
242 
243 	iface_index = UMCT_IFACE_INDEX;
244 	error = usb2_transfer_setup(uaa->device, &iface_index,
245 	    sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
246 
247 	if (error) {
248 		device_printf(dev, "allocating USB "
249 		    "transfers failed!\n");
250 		goto detach;
251 	}
252 	/*
253 	 * The real bulk-in endpoint is also marked as an interrupt.
254 	 * The only way to differentiate it from the real interrupt
255 	 * endpoint is to look at the wMaxPacketSize field.
256 	 */
257 	maxp = UGETW(sc->sc_xfer[UMCT_BULK_DT_RD]->pipe->edesc->wMaxPacketSize);
258 	if (maxp == 0x2) {
259 
260 		/* guessed wrong - switch around endpoints */
261 
262 		struct usb2_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
263 
264 		sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
265 		sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
266 
267 		sc->sc_xfer[UMCT_BULK_DT_RD]->callback = &umct_read_callback;
268 		sc->sc_xfer[UMCT_INTR_DT_RD]->callback = &umct_intr_callback;
269 	}
270 	sc->sc_obufsize = sc->sc_xfer[UMCT_BULK_DT_WR]->max_data_length;
271 
272 	if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
273 		if (sc->sc_obufsize > 16) {
274 			sc->sc_obufsize = 16;
275 		}
276 	}
277 	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
278 	    &umct_callback, &sc->sc_mtx);
279 	if (error) {
280 		goto detach;
281 	}
282 	return (0);			/* success */
283 
284 detach:
285 	umct_detach(dev);
286 	return (ENXIO);			/* failure */
287 }
288 
289 static int
290 umct_detach(device_t dev)
291 {
292 	struct umct_softc *sc = device_get_softc(dev);
293 
294 	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
295 	usb2_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
296 	mtx_destroy(&sc->sc_mtx);
297 
298 	return (0);
299 }
300 
301 static void
302 umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
303     uint16_t len, uint32_t value)
304 {
305 	struct usb2_device_request req;
306 	usb2_error_t err;
307 	uint8_t temp[4];
308 
309 	if (len > 4)
310 		len = 4;
311 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
312 	req.bRequest = request;
313 	USETW(req.wValue, 0);
314 	req.wIndex[0] = sc->sc_iface_no;
315 	req.wIndex[1] = 0;
316 	USETW(req.wLength, len);
317 	USETDW(temp, value);
318 
319 	err = usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
320 	    &req, temp, 0, 1000);
321 	if (err) {
322 		DPRINTFN(0, "device request failed, err=%s "
323 		    "(ignored)\n", usb2_errstr(err));
324 	}
325 	return;
326 }
327 
328 static void
329 umct_intr_callback(struct usb2_xfer *xfer)
330 {
331 	struct umct_softc *sc = xfer->priv_sc;
332 	uint8_t buf[2];
333 
334 	switch (USB_GET_STATE(xfer)) {
335 	case USB_ST_TRANSFERRED:
336 		if (xfer->actlen < 2) {
337 			DPRINTF("too short message\n");
338 			goto tr_setup;
339 		}
340 		usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
341 
342 		sc->sc_msr = buf[0];
343 		sc->sc_lsr = buf[1];
344 
345 		usb2_com_status_change(&sc->sc_ucom);
346 
347 	case USB_ST_SETUP:
348 tr_setup:
349 		xfer->frlengths[0] = xfer->max_data_length;
350 		usb2_start_hardware(xfer);
351 		return;
352 
353 	default:			/* Error */
354 		if (xfer->error != USB_ERR_CANCELLED) {
355 			/* try to clear stall first */
356 			xfer->flags.stall_pipe = 1;
357 			goto tr_setup;
358 		}
359 		return;
360 	}
361 }
362 
363 static void
364 umct_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
365 {
366 	struct umct_softc *sc = ucom->sc_parent;
367 
368 	*lsr = sc->sc_lsr;
369 	*msr = sc->sc_msr;
370 }
371 
372 static void
373 umct_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
374 {
375 	struct umct_softc *sc = ucom->sc_parent;
376 
377 	if (onoff)
378 		sc->sc_lcr |= 0x40;
379 	else
380 		sc->sc_lcr &= ~0x40;
381 
382 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
383 }
384 
385 static void
386 umct_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
387 {
388 	struct umct_softc *sc = ucom->sc_parent;
389 
390 	if (onoff)
391 		sc->sc_mcr |= 0x01;
392 	else
393 		sc->sc_mcr &= ~0x01;
394 
395 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
396 }
397 
398 static void
399 umct_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
400 {
401 	struct umct_softc *sc = ucom->sc_parent;
402 
403 	if (onoff)
404 		sc->sc_mcr |= 0x02;
405 	else
406 		sc->sc_mcr &= ~0x02;
407 
408 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
409 }
410 
411 static uint8_t
412 umct_calc_baud(uint32_t baud)
413 {
414 	switch (baud) {
415 		case B300:return (0x1);
416 	case B600:
417 		return (0x2);
418 	case B1200:
419 		return (0x3);
420 	case B2400:
421 		return (0x4);
422 	case B4800:
423 		return (0x6);
424 	case B9600:
425 		return (0x8);
426 	case B19200:
427 		return (0x9);
428 	case B38400:
429 		return (0xa);
430 	case B57600:
431 		return (0xb);
432 	case 115200:
433 		return (0xc);
434 	case B0:
435 	default:
436 		break;
437 	}
438 	return (0x0);
439 }
440 
441 static int
442 umct_pre_param(struct usb2_com_softc *ucom, struct termios *t)
443 {
444 	return (0);			/* we accept anything */
445 }
446 
447 static void
448 umct_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
449 {
450 	struct umct_softc *sc = ucom->sc_parent;
451 	uint32_t value;
452 
453 	value = umct_calc_baud(t->c_ospeed);
454 	umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
455 
456 	value = (sc->sc_lcr & 0x40);
457 
458 	switch (t->c_cflag & CSIZE) {
459 	case CS5:
460 		value |= 0x0;
461 		break;
462 	case CS6:
463 		value |= 0x1;
464 		break;
465 	case CS7:
466 		value |= 0x2;
467 		break;
468 	default:
469 	case CS8:
470 		value |= 0x3;
471 		break;
472 	}
473 
474 	value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
475 	if (t->c_cflag & PARENB) {
476 		value |= 0x8;
477 		value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
478 	}
479 	/*
480 	 * XXX There doesn't seem to be a way to tell the device
481 	 * to use flow control.
482 	 */
483 
484 	sc->sc_lcr = value;
485 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
486 }
487 
488 static void
489 umct_start_read(struct usb2_com_softc *ucom)
490 {
491 	struct umct_softc *sc = ucom->sc_parent;
492 
493 	/* start interrupt endpoint */
494 	usb2_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
495 
496 	/* start read endpoint */
497 	usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
498 }
499 
500 static void
501 umct_stop_read(struct usb2_com_softc *ucom)
502 {
503 	struct umct_softc *sc = ucom->sc_parent;
504 
505 	/* stop interrupt endpoint */
506 	usb2_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
507 
508 	/* stop read endpoint */
509 	usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
510 }
511 
512 static void
513 umct_start_write(struct usb2_com_softc *ucom)
514 {
515 	struct umct_softc *sc = ucom->sc_parent;
516 
517 	usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
518 }
519 
520 static void
521 umct_stop_write(struct usb2_com_softc *ucom)
522 {
523 	struct umct_softc *sc = ucom->sc_parent;
524 
525 	usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
526 }
527 
528 static void
529 umct_write_callback(struct usb2_xfer *xfer)
530 {
531 	struct umct_softc *sc = xfer->priv_sc;
532 	uint32_t actlen;
533 
534 	switch (USB_GET_STATE(xfer)) {
535 	case USB_ST_SETUP:
536 	case USB_ST_TRANSFERRED:
537 tr_setup:
538 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
539 		    sc->sc_obufsize, &actlen)) {
540 
541 			xfer->frlengths[0] = actlen;
542 			usb2_start_hardware(xfer);
543 		}
544 		return;
545 
546 	default:			/* Error */
547 		if (xfer->error != USB_ERR_CANCELLED) {
548 			/* try to clear stall first */
549 			xfer->flags.stall_pipe = 1;
550 			goto tr_setup;
551 		}
552 		return;
553 	}
554 }
555 
556 static void
557 umct_read_callback(struct usb2_xfer *xfer)
558 {
559 	struct umct_softc *sc = xfer->priv_sc;
560 
561 	switch (USB_GET_STATE(xfer)) {
562 	case USB_ST_TRANSFERRED:
563 		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers,
564 		    0, xfer->actlen);
565 
566 	case USB_ST_SETUP:
567 tr_setup:
568 		xfer->frlengths[0] = xfer->max_data_length;
569 		usb2_start_hardware(xfer);
570 		return;
571 
572 	default:			/* Error */
573 		if (xfer->error != USB_ERR_CANCELLED) {
574 			/* try to clear stall first */
575 			xfer->flags.stall_pipe = 1;
576 			goto tr_setup;
577 		}
578 		return;
579 	}
580 }
581