xref: /freebsd/sys/dev/usb/serial/umct.c (revision 39beb93c)
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 #include <dev/usb/usb_defs.h>
53 
54 #define	USB_DEBUG_VAR usb2_debug
55 
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_debug.h>
58 #include <dev/usb/usb_process.h>
59 #include <dev/usb/usb_request.h>
60 #include <dev/usb/usb_lookup.h>
61 #include <dev/usb/usb_util.h>
62 #include <dev/usb/usb_busdma.h>
63 #include <dev/usb/usb_device.h>
64 
65 #include <dev/usb/serial/usb_serial.h>
66 
67 /* The UMCT advertises the standard 8250 UART registers */
68 #define	UMCT_GET_MSR		2	/* Get Modem Status Register */
69 #define	UMCT_GET_MSR_SIZE	1
70 #define	UMCT_GET_LCR		6	/* Get Line Control Register */
71 #define	UMCT_GET_LCR_SIZE	1
72 #define	UMCT_SET_BAUD		5	/* Set the Baud Rate Divisor */
73 #define	UMCT_SET_BAUD_SIZE	4
74 #define	UMCT_SET_LCR		7	/* Set Line Control Register */
75 #define	UMCT_SET_LCR_SIZE	1
76 #define	UMCT_SET_MCR		10	/* Set Modem Control Register */
77 #define	UMCT_SET_MCR_SIZE	1
78 
79 #define	UMCT_INTR_INTERVAL	100
80 #define	UMCT_IFACE_INDEX	0
81 #define	UMCT_CONFIG_INDEX	1
82 
83 enum {
84 	UMCT_BULK_DT_WR,
85 	UMCT_BULK_DT_RD,
86 	UMCT_INTR_DT_RD,
87 	UMCT_N_TRANSFER,
88 };
89 
90 struct umct_softc {
91 	struct usb2_com_super_softc sc_super_ucom;
92 	struct usb2_com_softc sc_ucom;
93 
94 	struct usb2_device *sc_udev;
95 	struct usb2_xfer *sc_xfer[UMCT_N_TRANSFER];
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, ushub, 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 
237 	snprintf(sc->sc_name, sizeof(sc->sc_name),
238 	    "%s", device_get_nameunit(dev));
239 
240 	sc->sc_iface_no = uaa->info.bIfaceNum;
241 
242 	iface_index = UMCT_IFACE_INDEX;
243 	error = usb2_transfer_setup(uaa->device, &iface_index,
244 	    sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &Giant);
245 
246 	if (error) {
247 		device_printf(dev, "allocating USB "
248 		    "transfers failed!\n");
249 		goto detach;
250 	}
251 	/*
252 	 * The real bulk-in endpoint is also marked as an interrupt.
253 	 * The only way to differentiate it from the real interrupt
254 	 * endpoint is to look at the wMaxPacketSize field.
255 	 */
256 	maxp = UGETW(sc->sc_xfer[UMCT_BULK_DT_RD]->pipe->edesc->wMaxPacketSize);
257 	if (maxp == 0x2) {
258 
259 		/* guessed wrong - switch around endpoints */
260 
261 		struct usb2_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
262 
263 		sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
264 		sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
265 
266 		sc->sc_xfer[UMCT_BULK_DT_RD]->callback = &umct_read_callback;
267 		sc->sc_xfer[UMCT_INTR_DT_RD]->callback = &umct_intr_callback;
268 	}
269 	sc->sc_obufsize = sc->sc_xfer[UMCT_BULK_DT_WR]->max_data_length;
270 
271 	if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
272 		if (sc->sc_obufsize > 16) {
273 			sc->sc_obufsize = 16;
274 		}
275 	}
276 	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
277 	    &umct_callback, &Giant);
278 	if (error) {
279 		goto detach;
280 	}
281 	return (0);			/* success */
282 
283 detach:
284 	umct_detach(dev);
285 	return (ENXIO);			/* failure */
286 }
287 
288 static int
289 umct_detach(device_t dev)
290 {
291 	struct umct_softc *sc = device_get_softc(dev);
292 
293 	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
294 
295 	usb2_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
296 
297 	return (0);
298 }
299 
300 static void
301 umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
302     uint16_t len, uint32_t value)
303 {
304 	struct usb2_device_request req;
305 	usb2_error_t err;
306 	uint8_t temp[4];
307 
308 	if (len > 4)
309 		len = 4;
310 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
311 	req.bRequest = request;
312 	USETW(req.wValue, 0);
313 	req.wIndex[0] = sc->sc_iface_no;
314 	req.wIndex[1] = 0;
315 	USETW(req.wLength, len);
316 	USETDW(temp, value);
317 
318 	err = usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
319 	    &req, temp, 0, 1000);
320 	if (err) {
321 		DPRINTFN(0, "device request failed, err=%s "
322 		    "(ignored)\n", usb2_errstr(err));
323 	}
324 	return;
325 }
326 
327 static void
328 umct_intr_callback(struct usb2_xfer *xfer)
329 {
330 	struct umct_softc *sc = xfer->priv_sc;
331 	uint8_t buf[2];
332 
333 	switch (USB_GET_STATE(xfer)) {
334 	case USB_ST_TRANSFERRED:
335 		if (xfer->actlen < 2) {
336 			DPRINTF("too short message\n");
337 			goto tr_setup;
338 		}
339 		usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
340 
341 		sc->sc_msr = buf[0];
342 		sc->sc_lsr = buf[1];
343 
344 		usb2_com_status_change(&sc->sc_ucom);
345 
346 	case USB_ST_SETUP:
347 tr_setup:
348 		xfer->frlengths[0] = xfer->max_data_length;
349 		usb2_start_hardware(xfer);
350 		return;
351 
352 	default:			/* Error */
353 		if (xfer->error != USB_ERR_CANCELLED) {
354 			/* try to clear stall first */
355 			xfer->flags.stall_pipe = 1;
356 			goto tr_setup;
357 		}
358 		return;
359 	}
360 }
361 
362 static void
363 umct_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
364 {
365 	struct umct_softc *sc = ucom->sc_parent;
366 
367 	*lsr = sc->sc_lsr;
368 	*msr = sc->sc_msr;
369 }
370 
371 static void
372 umct_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
373 {
374 	struct umct_softc *sc = ucom->sc_parent;
375 
376 	if (onoff)
377 		sc->sc_lcr |= 0x40;
378 	else
379 		sc->sc_lcr &= ~0x40;
380 
381 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
382 }
383 
384 static void
385 umct_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
386 {
387 	struct umct_softc *sc = ucom->sc_parent;
388 
389 	if (onoff)
390 		sc->sc_mcr |= 0x01;
391 	else
392 		sc->sc_mcr &= ~0x01;
393 
394 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
395 }
396 
397 static void
398 umct_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
399 {
400 	struct umct_softc *sc = ucom->sc_parent;
401 
402 	if (onoff)
403 		sc->sc_mcr |= 0x02;
404 	else
405 		sc->sc_mcr &= ~0x02;
406 
407 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
408 }
409 
410 static uint8_t
411 umct_calc_baud(uint32_t baud)
412 {
413 	switch (baud) {
414 		case B300:return (0x1);
415 	case B600:
416 		return (0x2);
417 	case B1200:
418 		return (0x3);
419 	case B2400:
420 		return (0x4);
421 	case B4800:
422 		return (0x6);
423 	case B9600:
424 		return (0x8);
425 	case B19200:
426 		return (0x9);
427 	case B38400:
428 		return (0xa);
429 	case B57600:
430 		return (0xb);
431 	case 115200:
432 		return (0xc);
433 	case B0:
434 	default:
435 		break;
436 	}
437 	return (0x0);
438 }
439 
440 static int
441 umct_pre_param(struct usb2_com_softc *ucom, struct termios *t)
442 {
443 	return (0);			/* we accept anything */
444 }
445 
446 static void
447 umct_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
448 {
449 	struct umct_softc *sc = ucom->sc_parent;
450 	uint32_t value;
451 
452 	value = umct_calc_baud(t->c_ospeed);
453 	umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
454 
455 	value = (sc->sc_lcr & 0x40);
456 
457 	switch (t->c_cflag & CSIZE) {
458 	case CS5:
459 		value |= 0x0;
460 		break;
461 	case CS6:
462 		value |= 0x1;
463 		break;
464 	case CS7:
465 		value |= 0x2;
466 		break;
467 	default:
468 	case CS8:
469 		value |= 0x3;
470 		break;
471 	}
472 
473 	value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
474 	if (t->c_cflag & PARENB) {
475 		value |= 0x8;
476 		value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
477 	}
478 	/*
479 	 * XXX There doesn't seem to be a way to tell the device
480 	 * to use flow control.
481 	 */
482 
483 	sc->sc_lcr = value;
484 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
485 }
486 
487 static void
488 umct_start_read(struct usb2_com_softc *ucom)
489 {
490 	struct umct_softc *sc = ucom->sc_parent;
491 
492 	/* start interrupt endpoint */
493 	usb2_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
494 
495 	/* start read endpoint */
496 	usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
497 }
498 
499 static void
500 umct_stop_read(struct usb2_com_softc *ucom)
501 {
502 	struct umct_softc *sc = ucom->sc_parent;
503 
504 	/* stop interrupt endpoint */
505 	usb2_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
506 
507 	/* stop read endpoint */
508 	usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
509 }
510 
511 static void
512 umct_start_write(struct usb2_com_softc *ucom)
513 {
514 	struct umct_softc *sc = ucom->sc_parent;
515 
516 	usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
517 }
518 
519 static void
520 umct_stop_write(struct usb2_com_softc *ucom)
521 {
522 	struct umct_softc *sc = ucom->sc_parent;
523 
524 	usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
525 }
526 
527 static void
528 umct_write_callback(struct usb2_xfer *xfer)
529 {
530 	struct umct_softc *sc = xfer->priv_sc;
531 	uint32_t actlen;
532 
533 	switch (USB_GET_STATE(xfer)) {
534 	case USB_ST_SETUP:
535 	case USB_ST_TRANSFERRED:
536 tr_setup:
537 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
538 		    sc->sc_obufsize, &actlen)) {
539 
540 			xfer->frlengths[0] = actlen;
541 			usb2_start_hardware(xfer);
542 		}
543 		return;
544 
545 	default:			/* Error */
546 		if (xfer->error != USB_ERR_CANCELLED) {
547 			/* try to clear stall first */
548 			xfer->flags.stall_pipe = 1;
549 			goto tr_setup;
550 		}
551 		return;
552 	}
553 }
554 
555 static void
556 umct_read_callback(struct usb2_xfer *xfer)
557 {
558 	struct umct_softc *sc = xfer->priv_sc;
559 
560 	switch (USB_GET_STATE(xfer)) {
561 	case USB_ST_TRANSFERRED:
562 		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers,
563 		    0, xfer->actlen);
564 
565 	case USB_ST_SETUP:
566 tr_setup:
567 		xfer->frlengths[0] = xfer->max_data_length;
568 		usb2_start_hardware(xfer);
569 		return;
570 
571 	default:			/* Error */
572 		if (xfer->error != USB_ERR_CANCELLED) {
573 			/* try to clear stall first */
574 			xfer->flags.stall_pipe = 1;
575 			goto tr_setup;
576 		}
577 		return;
578 	}
579 }
580