xref: /openbsd/sys/dev/usb/uplcom.c (revision 81508fe3)
1 /*	$OpenBSD: uplcom.c,v 1.81 2024/05/23 03:21:09 jsg Exp $	*/
2 /*	$NetBSD: uplcom.c,v 1.29 2002/09/23 05:51:23 simonb Exp $	*/
3 /*
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Ichiro FUKUHARA (ichiro@ichiro.org).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Simple datasheet
34  * http://www.prolific.com.tw/PDF/PL-2303%20Market%20Spec.pdf
35  * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
36  * 	(english)
37  *
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/tty.h>
44 #include <sys/device.h>
45 
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbcdc.h>
48 
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdevs.h>
51 
52 #include <dev/usb/ucomvar.h>
53 
54 #ifdef UPLCOM_DEBUG
55 #define DPRINTFN(n, x)  do { if (uplcomdebug > (n)) printf x; } while (0)
56 int	uplcomdebug = 0;
57 #else
58 #define DPRINTFN(n, x)
59 #endif
60 #define DPRINTF(x) DPRINTFN(0, x)
61 
62 #define	UPLCOM_IFACE_INDEX	0
63 #define	UPLCOM_SECOND_IFACE_INDEX	1
64 
65 #define	UPLCOM_SET_REQUEST	0x01
66 #define	UPLCOM_HXN_SET_REQUEST	0x80
67 #define	UPLCOM_HXN_SET_CRTSCTS_REG 0x0A
68 #define	UPLCOM_SET_CRTSCTS	0x41
69 #define	UPLCOM_HX_SET_CRTSCTS	0x61
70 #define	UPLCOM_HXN_SET_CRTSCTS	0xFA
71 #define	UPLCOM_HX_STATUS_REG	0x8080
72 #define RSAQ_STATUS_CTS		0x80
73 #define RSAQ_STATUS_DSR		0x02
74 #define RSAQ_STATUS_DCD		0x01
75 
76 #define UPLCOM_TYPE_01		0
77 #define UPLCOM_TYPE_HX		1
78 #define UPLCOM_TYPE_HXN		2
79 
80 struct	uplcom_softc {
81 	struct device		 sc_dev;	/* base device */
82 	struct usbd_device	*sc_udev;	/* USB device */
83 	struct usbd_interface	*sc_iface;	/* interface */
84 	int			 sc_iface_number;	/* interface number */
85 
86 	struct usbd_interface	*sc_intr_iface;	/* interrupt interface */
87 	int			 sc_intr_number;	/* interrupt number */
88 	struct usbd_pipe	*sc_intr_pipe;	/* interrupt pipe */
89 	u_char			*sc_intr_buf;	/* interrupt buffer */
90 	int			 sc_isize;
91 
92 	struct usb_cdc_line_state sc_line_state;/* current line state */
93 	int			 sc_dtr;	/* current DTR state */
94 	int			 sc_rts;	/* current RTS state */
95 
96 	struct device		*sc_subdev;	/* ucom device */
97 
98 	u_char			 sc_lsr;	/* Local status register */
99 	u_char			 sc_msr;	/* uplcom status register */
100 	int			 sc_type;	/* variant */
101 };
102 
103 /*
104  * These are the maximum number of bytes transferred per frame.
105  * The output buffer size cannot be increased due to the size encoding.
106  */
107 #define UPLCOMIBUFSIZE 256
108 #define UPLCOMOBUFSIZE 256
109 
110 usbd_status uplcom_reset(struct uplcom_softc *);
111 usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
112     struct usb_cdc_line_state *state);
113 usbd_status uplcom_set_crtscts(struct uplcom_softc *);
114 void uplcom_intr(struct usbd_xfer *, void *, usbd_status);
115 
116 void uplcom_set(void *, int, int, int);
117 void uplcom_dtr(struct uplcom_softc *, int);
118 void uplcom_rts(struct uplcom_softc *, int);
119 void uplcom_break(struct uplcom_softc *, int);
120 void uplcom_set_line_state(struct uplcom_softc *);
121 void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
122 int  uplcom_param(void *, int, struct termios *);
123 int  uplcom_open(void *, int);
124 void uplcom_close(void *, int);
125 
126 const struct ucom_methods uplcom_methods = {
127 	uplcom_get_status,
128 	uplcom_set,
129 	uplcom_param,
130 	NULL,
131 	uplcom_open,
132 	uplcom_close,
133 	NULL,
134 	NULL,
135 };
136 
137 static const struct usb_devno uplcom_devs[] = {
138 	{ USB_VENDOR_ALCATEL, USB_PRODUCT_ALCATEL_OT535 },
139 	{ USB_VENDOR_ANCHOR, USB_PRODUCT_ANCHOR_SERIAL },
140 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
141 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257 },
142 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
143 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
144 	{ USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
145 	{ USB_VENDOR_HP, USB_PRODUCT_HP_LD220 },
146 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
147 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
148 	{ USB_VENDOR_LEADTEK, USB_PRODUCT_LEADTEK_9531 },
149 	{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_700WX },
150 	{ USB_VENDOR_MOBILEACTION, USB_PRODUCT_MOBILEACTION_MA620 },
151 	{ USB_VENDOR_NOKIA, USB_PRODUCT_NOKIA_CA42 },
152 	{ USB_VENDOR_OTI, USB_PRODUCT_OTI_DKU5 },
153 	{ USB_VENDOR_PLX, USB_PRODUCT_PLX_CA42 },
154 	{ USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
155 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
156 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GB },
157 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GC },
158 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GE },
159 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GL },
160 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GS },
161 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GT },
162 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
163 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X2 },
164 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
165 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303BENQ },
166 	{ USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
167 	{ USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_PL2303 },
168 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
169 	{ USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_SERIAL },
170 	{ USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_SX1 },
171 	{ USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_X65 },
172 	{ USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_X75 },
173 	{ USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
174 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
175 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
176 	{ USB_VENDOR_SPEEDDRAGON, USB_PRODUCT_SPEEDDRAGON_MS3303H },
177 	{ USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU11 },
178 	{ USB_VENDOR_SYNTECH, USB_PRODUCT_SYNTECH_SERIAL },
179 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
180 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
181 	{ USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
182 	{ USB_VENDOR_SMART, USB_PRODUCT_SMART_PL2303 },
183 	{ USB_VENDOR_YCCABLE, USB_PRODUCT_YCCABLE_PL2303 }
184 };
185 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
186 
187 int uplcom_match(struct device *, void *, void *);
188 void uplcom_attach(struct device *, struct device *, void *);
189 int uplcom_detach(struct device *, int);
190 
191 struct cfdriver uplcom_cd = {
192 	NULL, "uplcom", DV_DULL
193 };
194 
195 const struct cfattach uplcom_ca = {
196 	sizeof(struct uplcom_softc), uplcom_match, uplcom_attach, uplcom_detach
197 };
198 
199 int
uplcom_match(struct device * parent,void * match,void * aux)200 uplcom_match(struct device *parent, void *match, void *aux)
201 {
202 	struct usb_attach_arg *uaa = aux;
203 
204 	if (uaa->iface == NULL)
205 		return (UMATCH_NONE);
206 
207 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
208 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
209 }
210 
211 void
uplcom_attach(struct device * parent,struct device * self,void * aux)212 uplcom_attach(struct device *parent, struct device *self, void *aux)
213 {
214 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
215 	struct usb_attach_arg *uaa = aux;
216 	struct usbd_device *dev = uaa->device;
217 	usb_config_descriptor_t *cdesc;
218 	usb_device_descriptor_t *ddesc;
219 	usb_interface_descriptor_t *id;
220 	usb_endpoint_descriptor_t *ed;
221 	char *devname = sc->sc_dev.dv_xname;
222 	usb_device_request_t req;
223 	usbd_status err;
224 	uByte val;
225 	int i;
226 	struct ucom_attach_args uca;
227 
228 	sc->sc_udev = dev;
229 
230 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
231 
232 	/* initialize endpoints */
233 	uca.bulkin = uca.bulkout = -1;
234 	sc->sc_intr_number = -1;
235 	sc->sc_intr_pipe = NULL;
236 
237 	/* get the config descriptor */
238 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
239 
240 	if (cdesc == NULL) {
241 		printf("%s: failed to get configuration descriptor\n",
242 			sc->sc_dev.dv_xname);
243 		usbd_deactivate(sc->sc_udev);
244 		return;
245 	}
246 
247 	/* get the device descriptor */
248 	ddesc = usbd_get_device_descriptor(sc->sc_udev);
249 	if (ddesc == NULL) {
250 		printf("%s: failed to get device descriptor\n",
251 		    sc->sc_dev.dv_xname);
252 		usbd_deactivate(sc->sc_udev);
253 		return;
254 	}
255 
256 	/*
257 	 * The Linux driver suggest this will only be true for the HX
258 	 * variants. The datasheets disagree.
259 	 */
260 	if (ddesc->bDeviceClass == 0x02)
261 		sc->sc_type = UPLCOM_TYPE_01;
262 	else if (ddesc->bMaxPacketSize == 0x40)
263 		sc->sc_type = UPLCOM_TYPE_HX;
264 	else
265 		sc->sc_type = UPLCOM_TYPE_01;
266 
267 	if (sc->sc_type == UPLCOM_TYPE_HX) {
268 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
269 		req.bRequest = UPLCOM_SET_REQUEST;
270 		USETW(req.wValue, UPLCOM_HX_STATUS_REG);
271 		USETW(req.wIndex, sc->sc_iface_number);
272 		USETW(req.wLength, 1);
273 		err = usbd_do_request(sc->sc_udev, &req, &val);
274 		if (err)
275 			sc->sc_type = UPLCOM_TYPE_HXN;
276 	}
277 
278 #ifdef UPLCOM_DEBUG
279 	/* print the chip type */
280 	if (sc->sc_type == UPLCOM_TYPE_HXN) {
281 		DPRINTF(("uplcom_attach: chiptype 2303XN\n"));
282 	} else if (sc->sc_type == UPLCOM_TYPE_HX) {
283 		DPRINTF(("uplcom_attach: chiptype 2303X\n"));
284 	} else {
285 		DPRINTF(("uplcom_attach: chiptype 2303\n"));
286 	}
287 #endif
288 	/* get the (first/common) interface */
289 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
290 							&sc->sc_iface);
291 	if (err) {
292 		printf("\n%s: failed to get interface, err=%s\n",
293 			devname, usbd_errstr(err));
294 		usbd_deactivate(sc->sc_udev);
295 		return;
296 	}
297 
298 	/* Find the interrupt endpoints */
299 
300 	id = usbd_get_interface_descriptor(sc->sc_iface);
301 	sc->sc_iface_number = id->bInterfaceNumber;
302 
303 	for (i = 0; i < id->bNumEndpoints; i++) {
304 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
305 		if (ed == NULL) {
306 			printf("%s: no endpoint descriptor for %d\n",
307 				sc->sc_dev.dv_xname, i);
308 			usbd_deactivate(sc->sc_udev);
309 			return;
310 		}
311 
312 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
313 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
314 			sc->sc_intr_number = ed->bEndpointAddress;
315 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
316 		}
317 	}
318 
319 	if (sc->sc_intr_number== -1) {
320 		printf("%s: Could not find interrupt in\n",
321 			sc->sc_dev.dv_xname);
322 		usbd_deactivate(sc->sc_udev);
323 		return;
324 	}
325 
326 	/* keep interface for interrupt */
327 	sc->sc_intr_iface = sc->sc_iface;
328 
329 	/*
330 	 * USB-RSAQ1 has two interface
331 	 *
332 	 *  USB-RSAQ1       | USB-RSAQ2
333 	 * -----------------+-----------------
334 	 * Interface 0      |Interface 0
335 	 *  Interrupt(0x81) | Interrupt(0x81)
336 	 * -----------------+ BulkIN(0x02)
337 	 * Interface 1	    | BulkOUT(0x83)
338 	 *   BulkIN(0x02)   |
339 	 *   BulkOUT(0x83)  |
340 	 */
341 	if (cdesc->bNumInterfaces == 2) {
342 		err = usbd_device2interface_handle(dev,
343 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
344 		if (err) {
345 			printf("\n%s: failed to get second interface, err=%s\n",
346 			    devname, usbd_errstr(err));
347 			usbd_deactivate(sc->sc_udev);
348 			return;
349 		}
350 	}
351 
352 	/* Find the bulk{in,out} endpoints */
353 
354 	id = usbd_get_interface_descriptor(sc->sc_iface);
355 	sc->sc_iface_number = id->bInterfaceNumber;
356 
357 	for (i = 0; i < id->bNumEndpoints; i++) {
358 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
359 		if (ed == NULL) {
360 			printf("%s: no endpoint descriptor for %d\n",
361 				sc->sc_dev.dv_xname, i);
362 			usbd_deactivate(sc->sc_udev);
363 			return;
364 		}
365 
366 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
367 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
368 			uca.bulkin = ed->bEndpointAddress;
369 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
370 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
371 			uca.bulkout = ed->bEndpointAddress;
372 		}
373 	}
374 
375 	if (uca.bulkin == -1) {
376 		printf("%s: Could not find data bulk in\n",
377 			sc->sc_dev.dv_xname);
378 		usbd_deactivate(sc->sc_udev);
379 		return;
380 	}
381 
382 	if (uca.bulkout == -1) {
383 		printf("%s: Could not find data bulk out\n",
384 			sc->sc_dev.dv_xname);
385 		usbd_deactivate(sc->sc_udev);
386 		return;
387 	}
388 
389 	sc->sc_dtr = sc->sc_rts = -1;
390 	uca.portno = UCOM_UNK_PORTNO;
391 	/* bulkin, bulkout set above */
392 	uca.ibufsize = UPLCOMIBUFSIZE;
393 	uca.obufsize = UPLCOMOBUFSIZE;
394 	uca.ibufsizepad = UPLCOMIBUFSIZE;
395 	uca.opkthdrlen = 0;
396 	uca.device = dev;
397 	uca.iface = sc->sc_iface;
398 	uca.methods = &uplcom_methods;
399 	uca.arg = sc;
400 	uca.info = NULL;
401 
402 	if (sc->sc_type != UPLCOM_TYPE_HXN) {
403 		err = uplcom_reset(sc);
404 		if (err) {
405 			printf("%s: reset failed, %s\n", sc->sc_dev.dv_xname,
406 				usbd_errstr(err));
407 			usbd_deactivate(sc->sc_udev);
408 			return;
409 		}
410 	}
411 
412 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
413 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
414 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
415 }
416 
417 int
uplcom_detach(struct device * self,int flags)418 uplcom_detach(struct device *self, int flags)
419 {
420 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
421 	int rv = 0;
422 
423 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
424 
425 	if (sc->sc_intr_pipe != NULL) {
426 		usbd_close_pipe(sc->sc_intr_pipe);
427 		free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize);
428 		sc->sc_intr_pipe = NULL;
429 	}
430 
431 	if (sc->sc_subdev != NULL) {
432 		rv = config_detach(sc->sc_subdev, flags);
433 		sc->sc_subdev = NULL;
434 	}
435 
436 	return (rv);
437 }
438 
439 usbd_status
uplcom_reset(struct uplcom_softc * sc)440 uplcom_reset(struct uplcom_softc *sc)
441 {
442 	usb_device_request_t req;
443 	usbd_status err;
444 
445 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
446 	req.bRequest = UPLCOM_SET_REQUEST;
447 	USETW(req.wValue, 0);
448 	USETW(req.wIndex, sc->sc_iface_number);
449 	USETW(req.wLength, 0);
450 
451 	err = usbd_do_request(sc->sc_udev, &req, 0);
452 	if (err)
453 		return (EIO);
454 
455 	return (0);
456 }
457 
458 void
uplcom_set_line_state(struct uplcom_softc * sc)459 uplcom_set_line_state(struct uplcom_softc *sc)
460 {
461 	usb_device_request_t req;
462 	int ls;
463 
464 	/* Make sure we have initialized state for sc_dtr and sc_rts */
465 	if (sc->sc_dtr == -1)
466 		sc->sc_dtr = 0;
467 	if (sc->sc_rts == -1)
468 		sc->sc_rts = 0;
469 
470 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
471 	    (sc->sc_rts ? UCDC_LINE_RTS : 0);
472 
473 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
474 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
475 	USETW(req.wValue, ls);
476 	USETW(req.wIndex, sc->sc_iface_number);
477 	USETW(req.wLength, 0);
478 
479 	(void)usbd_do_request(sc->sc_udev, &req, 0);
480 
481 }
482 
483 void
uplcom_set(void * addr,int portno,int reg,int onoff)484 uplcom_set(void *addr, int portno, int reg, int onoff)
485 {
486 	struct uplcom_softc *sc = addr;
487 
488 	switch (reg) {
489 	case UCOM_SET_DTR:
490 		uplcom_dtr(sc, onoff);
491 		break;
492 	case UCOM_SET_RTS:
493 		uplcom_rts(sc, onoff);
494 		break;
495 	case UCOM_SET_BREAK:
496 		uplcom_break(sc, onoff);
497 		break;
498 	default:
499 		break;
500 	}
501 }
502 
503 void
uplcom_dtr(struct uplcom_softc * sc,int onoff)504 uplcom_dtr(struct uplcom_softc *sc, int onoff)
505 {
506 
507 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
508 
509 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
510 		return;
511 
512 	sc->sc_dtr = !!onoff;
513 
514 	uplcom_set_line_state(sc);
515 }
516 
517 void
uplcom_rts(struct uplcom_softc * sc,int onoff)518 uplcom_rts(struct uplcom_softc *sc, int onoff)
519 {
520 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
521 
522 	if (sc->sc_rts == -1 && !sc->sc_rts == !onoff)
523 		return;
524 
525 	sc->sc_rts = !!onoff;
526 
527 	uplcom_set_line_state(sc);
528 }
529 
530 void
uplcom_break(struct uplcom_softc * sc,int onoff)531 uplcom_break(struct uplcom_softc *sc, int onoff)
532 {
533 	usb_device_request_t req;
534 
535 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
536 
537 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
538 	req.bRequest = UCDC_SEND_BREAK;
539 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
540 	USETW(req.wIndex, sc->sc_iface_number);
541 	USETW(req.wLength, 0);
542 
543 	(void)usbd_do_request(sc->sc_udev, &req, 0);
544 }
545 
546 usbd_status
uplcom_set_crtscts(struct uplcom_softc * sc)547 uplcom_set_crtscts(struct uplcom_softc *sc)
548 {
549 	usb_device_request_t req;
550 	usbd_status err;
551 
552 	DPRINTF(("uplcom_set_crtscts: on\n"));
553 
554 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
555 	if (sc->sc_type == UPLCOM_TYPE_HXN) {
556 		req.bRequest = UPLCOM_HXN_SET_REQUEST;
557 		USETW(req.wValue, UPLCOM_HXN_SET_CRTSCTS_REG);
558 		USETW(req.wIndex, UPLCOM_HXN_SET_CRTSCTS);
559 	} else {
560 		req.bRequest = UPLCOM_SET_REQUEST;
561 		USETW(req.wValue, 0);
562 		USETW(req.wIndex, (sc->sc_type == UPLCOM_TYPE_HX ?
563 		    UPLCOM_HX_SET_CRTSCTS : UPLCOM_SET_CRTSCTS));
564 	}
565 	USETW(req.wLength, 0);
566 
567 	err = usbd_do_request(sc->sc_udev, &req, 0);
568 	if (err) {
569 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
570 			usbd_errstr(err)));
571 		return (err);
572 	}
573 
574 	return (USBD_NORMAL_COMPLETION);
575 }
576 
577 usbd_status
uplcom_set_line_coding(struct uplcom_softc * sc,struct usb_cdc_line_state * state)578 uplcom_set_line_coding(struct uplcom_softc *sc,
579     struct usb_cdc_line_state *state)
580 {
581 	usb_device_request_t req;
582 	usbd_status err;
583 
584 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
585 		UGETDW(state->dwDTERate), state->bCharFormat,
586 		state->bParityType, state->bDataBits));
587 
588 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
589 		DPRINTF(("uplcom_set_line_coding: already set\n"));
590 		return (USBD_NORMAL_COMPLETION);
591 	}
592 
593 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
594 	req.bRequest = UCDC_SET_LINE_CODING;
595 	USETW(req.wValue, 0);
596 	USETW(req.wIndex, sc->sc_iface_number);
597 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
598 
599 	err = usbd_do_request(sc->sc_udev, &req, state);
600 	if (err) {
601 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
602 			usbd_errstr(err)));
603 		return (err);
604 	}
605 
606 	sc->sc_line_state = *state;
607 
608 	return (USBD_NORMAL_COMPLETION);
609 }
610 
611 int
uplcom_param(void * addr,int portno,struct termios * t)612 uplcom_param(void *addr, int portno, struct termios *t)
613 {
614 	struct uplcom_softc *sc = addr;
615 	usbd_status err;
616 	struct usb_cdc_line_state ls;
617 
618 	DPRINTF(("uplcom_param: sc=%p\n", sc));
619 
620 	USETDW(ls.dwDTERate, t->c_ospeed);
621 	if (ISSET(t->c_cflag, CSTOPB))
622 		ls.bCharFormat = UCDC_STOP_BIT_2;
623 	else
624 		ls.bCharFormat = UCDC_STOP_BIT_1;
625 	if (ISSET(t->c_cflag, PARENB)) {
626 		if (ISSET(t->c_cflag, PARODD))
627 			ls.bParityType = UCDC_PARITY_ODD;
628 		else
629 			ls.bParityType = UCDC_PARITY_EVEN;
630 	} else
631 		ls.bParityType = UCDC_PARITY_NONE;
632 	switch (ISSET(t->c_cflag, CSIZE)) {
633 	case CS5:
634 		ls.bDataBits = 5;
635 		break;
636 	case CS6:
637 		ls.bDataBits = 6;
638 		break;
639 	case CS7:
640 		ls.bDataBits = 7;
641 		break;
642 	case CS8:
643 		ls.bDataBits = 8;
644 		break;
645 	}
646 
647 	err = uplcom_set_line_coding(sc, &ls);
648 	if (err) {
649 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
650 		return (EIO);
651 	}
652 
653 	if (ISSET(t->c_cflag, CRTSCTS))
654 		uplcom_set_crtscts(sc);
655 
656 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
657 		uplcom_set_line_state(sc);
658 
659 	return (0);
660 }
661 
662 int
uplcom_open(void * addr,int portno)663 uplcom_open(void *addr, int portno)
664 {
665 	struct uplcom_softc *sc = addr;
666 	usb_device_request_t req;
667 	usbd_status uerr;
668 	int err;
669 
670 	if (usbd_is_dying(sc->sc_udev))
671 		return (EIO);
672 
673 	DPRINTF(("uplcom_open: sc=%p\n", sc));
674 
675 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
676 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
677 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
678 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
679 			sc->sc_intr_buf, sc->sc_isize,
680 			uplcom_intr, USBD_DEFAULT_INTERVAL);
681 		if (err) {
682 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
683 				sc->sc_dev.dv_xname, sc->sc_intr_number));
684 					return (EIO);
685 		}
686 	}
687 
688 	if (sc->sc_type == UPLCOM_TYPE_HX) {
689 		/*
690 		 * Undocumented (vendor unresponsive) - possibly changes
691 		 * flow control semantics. It is needed for HX variant devices.
692 		 */
693 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
694 		req.bRequest = UPLCOM_SET_REQUEST;
695 		USETW(req.wValue, 2);
696 		USETW(req.wIndex, 0x44);
697 		USETW(req.wLength, 0);
698 
699 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
700 		if (uerr)
701 			return (EIO);
702 
703 		/* Reset upstream data pipes */
704 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
705 		req.bRequest = UPLCOM_SET_REQUEST;
706 		USETW(req.wValue, 8);
707 		USETW(req.wIndex, 0);
708 		USETW(req.wLength, 0);
709 
710 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
711 		if (uerr)
712 			return (EIO);
713 
714 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
715 		req.bRequest = UPLCOM_SET_REQUEST;
716 		USETW(req.wValue, 9);
717 		USETW(req.wIndex, 0);
718 		USETW(req.wLength, 0);
719 
720 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
721 		if (uerr)
722 			return (EIO);
723 	}
724 
725 	if (sc->sc_type == UPLCOM_TYPE_HXN) {
726 		/* Reset upstream data pipes */
727 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
728 		req.bRequest = UPLCOM_HXN_SET_REQUEST;
729 		USETW(req.wValue, 0x07);
730 		USETW(req.wIndex, 0x03);
731 		USETW(req.wLength, 0);
732 
733 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
734 		if (uerr)
735 			return (EIO);
736 	}
737 
738 	return (0);
739 }
740 
741 void
uplcom_close(void * addr,int portno)742 uplcom_close(void *addr, int portno)
743 {
744 	struct uplcom_softc *sc = addr;
745 	int err;
746 
747 	if (usbd_is_dying(sc->sc_udev))
748 		return;
749 
750 	DPRINTF(("uplcom_close: close\n"));
751 
752 	if (sc->sc_intr_pipe != NULL) {
753 		err = usbd_close_pipe(sc->sc_intr_pipe);
754 		if (err)
755 			printf("%s: close interrupt pipe failed: %s\n",
756 				sc->sc_dev.dv_xname, usbd_errstr(err));
757 		free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize);
758 		sc->sc_intr_pipe = NULL;
759 	}
760 }
761 
762 void
uplcom_intr(struct usbd_xfer * xfer,void * priv,usbd_status status)763 uplcom_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
764 {
765 	struct uplcom_softc *sc = priv;
766 	u_char *buf = sc->sc_intr_buf;
767 	u_char pstatus;
768 
769 	if (usbd_is_dying(sc->sc_udev))
770 		return;
771 
772 	if (status != USBD_NORMAL_COMPLETION) {
773 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
774 			return;
775 
776 		DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname,
777 			usbd_errstr(status)));
778 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
779 		return;
780 	}
781 
782 	DPRINTF(("%s: uplcom status = %02x\n", sc->sc_dev.dv_xname, buf[8]));
783 
784 	sc->sc_lsr = sc->sc_msr = 0;
785 	pstatus = buf[8];
786 	if (ISSET(pstatus, RSAQ_STATUS_CTS))
787 		sc->sc_msr |= UMSR_CTS;
788 	else
789 		sc->sc_msr &= ~UMSR_CTS;
790 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
791 		sc->sc_msr |= UMSR_DSR;
792 	else
793 		sc->sc_msr &= ~UMSR_DSR;
794 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
795 		sc->sc_msr |= UMSR_DCD;
796 	else
797 		sc->sc_msr &= ~UMSR_DCD;
798 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
799 }
800 
801 void
uplcom_get_status(void * addr,int portno,u_char * lsr,u_char * msr)802 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
803 {
804 	struct uplcom_softc *sc = addr;
805 
806 	DPRINTF(("uplcom_get_status:\n"));
807 
808 	if (lsr != NULL)
809 		*lsr = sc->sc_lsr;
810 	if (msr != NULL)
811 		*msr = sc->sc_msr;
812 }
813