xref: /netbsd-src/sys/dev/usb/umct.c (revision 06be8101a16cc95f40783b3cb7afd12112103a9a)
1 /*	$NetBSD: umct.c,v 1.3 2001/11/13 06:24:56 lukem Exp $	*/
2 /*
3  * Copyright (c) 2001 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Ichiro FUKUHARA (ichiro@ichiro.org).
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * This driver is for the device MCT USB-RS232 Interfact Controller.
40  * http://www.mct.com.tw/p_u232.html
41  * http://www.dlink.com/products/usb/dsbs25
42  *
43  */
44 
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: umct.c,v 1.3 2001/11/13 06:24:56 lukem Exp $");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/ioctl.h>
53 #include <sys/conf.h>
54 #include <sys/tty.h>
55 #include <sys/file.h>
56 #include <sys/select.h>
57 #include <sys/proc.h>
58 #include <sys/vnode.h>
59 #include <sys/device.h>
60 #include <sys/poll.h>
61 
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbcdc.h>
64 
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbdevs.h>
68 #include <dev/usb/usb_quirks.h>
69 
70 #include <dev/usb/usbdevs.h>
71 #include <dev/usb/ucomvar.h>
72 
73 #include <dev/usb/umct.h>
74 
75 #ifdef UMCT_DEBUG
76 #define DPRINTFN(n, x)  if (umctdebug > (n)) logprintf x
77 int	umctdebug = 0;
78 #else
79 #define DPRINTFN(n, x)
80 #endif
81 #define DPRINTF(x) DPRINTFN(0, x)
82 
83 #define	UMCT_CONFIG_INDEX	0
84 #define	UMCT_IFACE_INDEX	0
85 
86 struct	umct_softc {
87 	USBBASEDEVICE		sc_dev;		/* base device */
88 	usbd_device_handle	sc_udev;	/* USB device */
89 	usbd_interface_handle	sc_iface;	/* interface */
90 	int			sc_iface_number;	/* interface number */
91 
92 	int			sc_intr_number;	/* interrupt number */
93 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
94 	u_char			*sc_intr_buf;	/* interrupt buffer */
95 	int			sc_isize;
96 
97 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
98 	u_char			sc_dtr;		/* current DTR state */
99 	u_char			sc_rts;		/* current RTS state */
100 	u_char			sc_break;	/* set break */
101 
102 	u_char			sc_status;
103 
104 	device_ptr_t		sc_subdev;	/* ucom device */
105 
106 	u_char			sc_dying;	/* disconnecting */
107 
108 	u_char			sc_lsr;		/* Local status register */
109 	u_char			sc_msr;		/* umct status register */
110 };
111 
112 /*
113  * These are the maximum number of bytes transferred per frame.
114  * The output buffer size cannot be increased due to the size encoding.
115  */
116 #define UMCTIBUFSIZE 256
117 #define UMCTOBUFSIZE 256
118 
119 Static	void umct_init(struct umct_softc *);
120 Static	void umct_set_baudrate(struct umct_softc *, void *);
121 Static	void umct_set_lcr(struct umct_softc *, void *);
122 Static	void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
123 
124 Static	void umct_set(void *, int, int, int);
125 Static	void umct_dtr(struct umct_softc *, int);
126 Static	void umct_rts(struct umct_softc *, int);
127 Static	void umct_break(struct umct_softc *, int);
128 Static	void umct_set_line_state(struct umct_softc *);
129 Static	void umct_get_status(void *, int portno, u_char *lsr, u_char *msr);
130 Static	int  umct_param(void *, int, struct termios *);
131 Static	int  umct_open(void *, int);
132 Static	void umct_close(void *, int);
133 
134 struct	ucom_methods umct_methods = {
135 	umct_get_status,
136 	umct_set,
137 	umct_param,
138 	NULL,
139 	umct_open,
140 	umct_close,
141 	NULL,
142 	NULL,
143 };
144 
145 static const struct umct_product {
146 	uint16_t	vendor;
147 	uint16_t	product;
148 } umct_products [] = {
149 	/* MCT USB-232 Interface Products */
150 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 },
151 	/* Sitecom USB-232 Products */
152 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
153 	/* D-Link DU-H3SP USB BAY Hub Products */
154 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 },
155 
156 	{ 0, 0 }
157 };
158 
159 USB_DECLARE_DRIVER(umct);
160 
161 USB_MATCH(umct)
162 {
163 	USB_MATCH_START(umct, uaa);
164 	int i;
165 
166 	if (uaa->iface != NULL)
167 		return (UMATCH_NONE);
168 
169 	for (i = 0; umct_products[i].vendor != 0; i++) {
170 		if (umct_products[i].vendor == uaa->vendor &&
171  		    umct_products[i].product == uaa->product) {
172 			return (UMATCH_VENDOR_PRODUCT);
173 		}
174 	}
175 	return (UMATCH_NONE);
176 }
177 
178 USB_ATTACH(umct)
179 {
180 	USB_ATTACH_START(umct, sc, uaa);
181 	usbd_device_handle dev = uaa->device;
182 	usb_config_descriptor_t *cdesc;
183 	usb_interface_descriptor_t *id;
184 	usb_endpoint_descriptor_t *ed;
185 
186 	char devinfo[1024];
187 	char *devname = USBDEVNAME(sc->sc_dev);
188 	usbd_status err;
189 	int i,found;
190 	struct ucom_attach_args uca;
191 
192         usbd_devinfo(dev, 0, devinfo);
193         USB_ATTACH_SETUP;
194         printf("%s: %s\n", devname, devinfo);
195 
196         sc->sc_udev = dev;
197 
198 	DPRINTF(("\n\numct attach: sc=%p\n", sc));
199 
200 	/* initialize endpoints */
201 	uca.bulkin = uca.bulkout = -1;
202 	sc->sc_intr_number = -1;
203 	sc->sc_intr_pipe = NULL;
204 
205 	/* Move the device into the configured state. */
206 	err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
207 	if (err) {
208 		printf("\n%s: failed to set configuration, err=%s\n",
209 			devname, usbd_errstr(err));
210 		sc->sc_dying = 1;
211 		USB_ATTACH_ERROR_RETURN;
212 	}
213 
214 	/* get the config descriptor */
215 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
216 
217 	if (cdesc == NULL) {
218 		printf("%s: failed to get configuration descriptor\n",
219 			USBDEVNAME(sc->sc_dev));
220 		sc->sc_dying = 1;
221 		USB_ATTACH_ERROR_RETURN;
222 	}
223 
224 	/* get the interface */
225 	err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
226 							&sc->sc_iface);
227 	if (err) {
228 		printf("\n%s: failed to get interface, err=%s\n",
229 			devname, usbd_errstr(err));
230 		sc->sc_dying = 1;
231 		USB_ATTACH_ERROR_RETURN;
232 	}
233 
234 	/* Find the bulk{in,out} and interrupt endpoints */
235 
236 	id = usbd_get_interface_descriptor(sc->sc_iface);
237 	sc->sc_iface_number = id->bInterfaceNumber;
238 	found = 0;
239 
240 	for (i = 0; i < id->bNumEndpoints; i++) {
241 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
242 		if (ed == NULL) {
243 			printf("%s: no endpoint descriptor for %d\n",
244 				USBDEVNAME(sc->sc_dev), i);
245 			sc->sc_dying = 1;
246 			USB_ATTACH_ERROR_RETURN;
247 		}
248 
249 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
250 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT &&
251 		    found == 0) {
252 			uca.bulkin = ed->bEndpointAddress;
253 			found = 1;
254 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
255 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
256 			uca.bulkout = ed->bEndpointAddress;
257 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
258 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
259 			sc->sc_intr_number = ed->bEndpointAddress;
260 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
261 		}
262 	}
263 
264 	if (uca.bulkin == -1) {
265 		printf("%s: Could not find data bulk in\n",
266 			USBDEVNAME(sc->sc_dev));
267 		sc->sc_dying = 1;
268 		USB_ATTACH_ERROR_RETURN;
269 	}
270 
271 	if (uca.bulkout == -1) {
272 		printf("%s: Could not find data bulk out\n",
273 			USBDEVNAME(sc->sc_dev));
274 		sc->sc_dying = 1;
275 		USB_ATTACH_ERROR_RETURN;
276 	}
277 
278 	if (sc->sc_intr_number== -1) {
279 		printf("%s: Could not find interrupt in\n",
280 			USBDEVNAME(sc->sc_dev));
281 		sc->sc_dying = 1;
282 		USB_ATTACH_ERROR_RETURN;
283 	}
284 
285 	sc->sc_dtr = sc->sc_rts = 0;
286 	uca.portno = UCOM_UNK_PORTNO;
287 	/* bulkin, bulkout set above */
288 	uca.ibufsize = UMCTIBUFSIZE;
289 	uca.obufsize = UMCTOBUFSIZE;
290 	uca.ibufsizepad = UMCTIBUFSIZE;
291 	uca.opkthdrlen = 0;
292 	uca.device = dev;
293 	uca.iface = sc->sc_iface;
294 	uca.methods = &umct_methods;
295 	uca.arg = sc;
296 	uca.info = NULL;
297 
298 	umct_init(sc);
299 
300 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
301 			   USBDEV(sc->sc_dev));
302 
303 	DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n",
304 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
305 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
306 
307 	USB_ATTACH_SUCCESS_RETURN;
308 }
309 
310 USB_DETACH(umct)
311 {
312 	USB_DETACH_START(umct, sc);
313 	int rv = 0;
314 
315 	DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags));
316 
317         if (sc->sc_intr_pipe != NULL) {
318                 usbd_abort_pipe(sc->sc_intr_pipe);
319                 usbd_close_pipe(sc->sc_intr_pipe);
320 		free(sc->sc_intr_buf, M_USBDEV);
321                 sc->sc_intr_pipe = NULL;
322         }
323 
324 	sc->sc_dying = 1;
325 	if (sc->sc_subdev != NULL) {
326 		rv = config_detach(sc->sc_subdev, flags);
327 		sc->sc_subdev = NULL;
328 	}
329 
330 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
331 			   USBDEV(sc->sc_dev));
332 
333 	return (rv);
334 }
335 
336 int
337 umct_activate(device_ptr_t self, enum devact act)
338 {
339 	struct umct_softc *sc = (struct umct_softc *)self;
340 	int rv = 0;
341 
342 	switch (act) {
343 	case DVACT_ACTIVATE:
344 		return (EOPNOTSUPP);
345 		break;
346 
347 	case DVACT_DEACTIVATE:
348 		if (sc->sc_subdev != NULL)
349 			rv = config_deactivate(sc->sc_subdev);
350 		sc->sc_dying = 1;
351 		break;
352 	}
353 	return (rv);
354 }
355 
356 void
357 umct_set_line_state(struct umct_softc *sc)
358 {
359 	usb_device_request_t req;
360 	int ls = MCR_NONE;
361 
362 	ls = (sc->sc_dtr ? MCR_DTR : 0) |
363 		(sc->sc_rts ? MCR_RTS : 0);
364 
365 	DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n",
366 			sc->sc_dtr, sc->sc_rts, ls));
367 
368 	req.bmRequestType = UMCT_SET_REQUEST;
369 	req.bRequest = REQ_SET_MCR;
370 	USETW(req.wValue, 0);
371 	USETW(req.wIndex, sc->sc_iface_number);
372 	USETW(req.wLength, LENGTH_SET_MCR);
373 
374 	(void)usbd_do_request(sc->sc_udev, &req, &ls);
375 }
376 
377 void
378 umct_set(void *addr, int portno, int reg, int onoff)
379 {
380 	struct umct_softc *sc = addr;
381 
382 	switch (reg) {
383 	case UCOM_SET_DTR:
384 		umct_dtr(sc, onoff);
385 		break;
386 	case UCOM_SET_RTS:
387 		umct_rts(sc, onoff);
388 		break;
389 	case UCOM_SET_BREAK:
390 		umct_break(sc, onoff);
391 		break;
392 	default:
393 		break;
394 	}
395 }
396 
397 void
398 umct_dtr(struct umct_softc *sc, int onoff)
399 {
400 
401 	DPRINTF(("umct_dtr: onoff=%d\n", onoff));
402 
403 	if (sc->sc_dtr == onoff)
404 		return;
405 	sc->sc_dtr = onoff;
406 
407 	umct_set_line_state(sc);
408 }
409 
410 void
411 umct_rts(struct umct_softc *sc, int onoff)
412 {
413 	DPRINTF(("umct_rts: onoff=%d\n", onoff));
414 
415 	if (sc->sc_rts == onoff)
416 		return;
417 	sc->sc_rts = onoff;
418 
419 	umct_set_line_state(sc);
420 }
421 
422 void
423 umct_break(struct umct_softc *sc, int onoff)
424 {
425 	u_char data;
426 
427 	DPRINTF(("umct_break: onoff=%d\n", onoff));
428 
429 	data = onoff ? LCR_SET_BREAK : 0;
430 
431 	umct_set_lcr(sc, &data);
432 }
433 
434 void
435 umct_set_lcr(struct umct_softc *sc, void *data)
436 {
437 	usb_device_request_t req;
438 
439 	req.bmRequestType = UMCT_SET_REQUEST;
440 	req.bRequest = REQ_SET_LCR;
441 	USETW(req.wValue, 0);
442 	USETW(req.wIndex, sc->sc_iface_number);
443 	USETW(req.wLength, LENGTH_SET_LCR);
444 
445 	(void)usbd_do_request(sc->sc_udev, &req, data);
446 }
447 
448 void
449 umct_set_baudrate(struct umct_softc *sc, void *rate)
450 {
451         usb_device_request_t req;
452 
453         req.bmRequestType = UMCT_SET_REQUEST;
454         req.bRequest = REQ_SET_BAUD_RATE;
455         USETW(req.wValue, 0);
456         USETW(req.wIndex, sc->sc_iface_number);
457         USETW(req.wLength, LENGTH_BAUD_RATE);
458 
459         (void)usbd_do_request(sc->sc_udev, &req, rate);
460 }
461 
462 void
463 umct_init(struct umct_softc *sc)
464 {
465 	int brate, data;
466 
467 	brate = UMCT_BAUD_RATE(9600);
468 	data |= LCR_DATA_BITS_8 | LCR_PARITY_NONE |
469 		LCR_STOP_BITS_1;
470 
471 	umct_set_baudrate(sc, &brate);
472 	umct_set_lcr(sc, &data);
473 }
474 
475 int
476 umct_param(void *addr, int portno, struct termios *t)
477 {
478 	struct umct_softc *sc = addr;
479 	int divisor = 0;
480 	u_char data = NULL;
481 
482 	DPRINTF(("umct_param: sc=%p\n", sc));
483 
484 	divisor = UMCT_BAUD_RATE(t->c_ospeed);
485 	DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed));
486 
487 	if (ISSET(t->c_cflag, CSTOPB))
488 		data |= LCR_STOP_BITS_2;
489 	else
490 		data |= LCR_STOP_BITS_1;
491 	if (ISSET(t->c_cflag, PARENB)) {
492 		if (ISSET(t->c_cflag, PARODD))
493 			data |= LCR_PARITY_ODD;
494 		else
495 			data |= LCR_PARITY_EVEN;
496 	} else
497 		data |= LCR_PARITY_NONE;
498 	switch (ISSET(t->c_cflag, CSIZE)) {
499 	case CS5:
500 		data |= LCR_DATA_BITS_5;
501 		break;
502 	case CS6:
503 		data |= LCR_DATA_BITS_6;
504 		break;
505 	case CS7:
506 		data |= LCR_DATA_BITS_7;
507 		break;
508 	case CS8:
509 		data |= LCR_DATA_BITS_8;
510 		break;
511 	}
512 
513 	umct_set_baudrate(sc, &divisor);
514 
515 	umct_set_lcr(sc, &data);
516 
517 	return (0);
518 }
519 
520 int
521 umct_open(void *addr, int portno)
522 {
523 	struct umct_softc *sc = addr;
524 	int err, lcr_data;
525 
526 	if (sc->sc_dying)
527 		return (EIO);
528 
529 	DPRINTF(("umct_open: sc=%p\n", sc));
530 
531 	/* initialize LCR */
532         lcr_data |= LCR_DATA_BITS_8 | LCR_PARITY_NONE |
533                 LCR_STOP_BITS_1;
534         umct_set_lcr(sc, &lcr_data);
535 
536 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
537 		sc->sc_status = 0; /* clear status bit */
538 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
539 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
540 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
541 			sc->sc_intr_buf, sc->sc_isize,
542 			umct_intr, USBD_DEFAULT_INTERVAL);
543 		if (err) {
544 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
545 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
546 					return (EIO);
547 		}
548 	}
549 
550 	return (0);
551 }
552 
553 void
554 umct_close(void *addr, int portno)
555 {
556 	struct umct_softc *sc = addr;
557 	int err;
558 
559 	if (sc->sc_dying)
560 		return;
561 
562 	DPRINTF(("umct_close: close\n"));
563 
564 	if (sc->sc_intr_pipe != NULL) {
565 		err = usbd_abort_pipe(sc->sc_intr_pipe);
566 		if (err)
567 			printf("%s: abort interrupt pipe failed: %s\n",
568 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
569 		err = usbd_close_pipe(sc->sc_intr_pipe);
570 		if (err)
571 			printf("%s: close interrupt pipe failed: %s\n",
572 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
573 		free(sc->sc_intr_buf, M_USBDEV);
574 		sc->sc_intr_pipe = NULL;
575 	}
576 }
577 
578 void
579 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
580 {
581 	struct umct_softc *sc = priv;
582 	u_char *buf = sc->sc_intr_buf;
583 	u_char mstatus, lstatus;
584 
585 	if (sc->sc_dying)
586 		return;
587 
588 	if (status != USBD_NORMAL_COMPLETION) {
589 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
590 			return;
591 
592 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
593 			usbd_errstr(status)));
594 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
595 		return;
596 	}
597 
598 	DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n",
599 		 USBDEVNAME(sc->sc_dev), buf[0],buf[1]));
600 
601 	sc->sc_lsr = sc->sc_msr = 0;
602 	mstatus = buf[0];
603 	lstatus = buf[1];
604 	if (ISSET(mstatus, MSR_DSR))
605 		sc->sc_msr |= UMSR_DSR;
606 	if (ISSET(mstatus, MSR_DCD))
607 		sc->sc_msr |= UMSR_DCD;
608 	ucom_status_change((struct ucom_softc *)sc->sc_subdev);
609 }
610 
611 void
612 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
613 {
614 	struct umct_softc *sc = addr;
615 
616 	DPRINTF(("umct_get_status:\n"));
617 
618 	if (lsr != NULL)
619 		*lsr = sc->sc_lsr;
620 	if (msr != NULL)
621 		*msr = sc->sc_msr;
622 }
623