1 /*	$OpenBSD: uplcom.c,v 1.22 2005/05/23 21:20:14 deraadt 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Simple datasheet
41  * http://www.prolific.com.tw/PDF/PL-2303%20Market%20Spec.pdf
42  * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
43  * 	(english)
44  *
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/ioctl.h>
52 #include <sys/conf.h>
53 #include <sys/tty.h>
54 #include <sys/file.h>
55 #include <sys/select.h>
56 #include <sys/proc.h>
57 #include <sys/vnode.h>
58 #include <sys/device.h>
59 #include <sys/poll.h>
60 
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbcdc.h>
63 
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbdevs.h>
67 #include <dev/usb/usb_quirks.h>
68 
69 #include <dev/usb/usbdevs.h>
70 #include <dev/usb/ucomvar.h>
71 
72 #ifdef UPLCOM_DEBUG
73 #define DPRINTFN(n, x)  do { if (uplcomdebug > (n)) logprintf x; } while (0)
74 int	uplcomdebug = 0;
75 #else
76 #define DPRINTFN(n, x)
77 #endif
78 #define DPRINTF(x) DPRINTFN(0, x)
79 
80 #define	UPLCOM_CONFIG_INDEX	0
81 #define	UPLCOM_IFACE_INDEX	0
82 #define	UPLCOM_SECOND_IFACE_INDEX	1
83 
84 #define	UPLCOM_SET_REQUEST	0x01
85 #define	UPLCOM_SET_CRTSCTS	0x41
86 #define	UPLCOM_HX_SET_CRTSCTS	0x61
87 #define RSAQ_STATUS_CTS		0x80
88 #define RSAQ_STATUS_DSR		0x02
89 #define RSAQ_STATUS_DCD		0x01
90 
91 #define UPLCOM_FLOW_OUT_CTS	0x0001
92 #define UPLCOM_FLOW_OUT_DSR	0x0002
93 #define UPLCOM_FLOW_IN_DSR	0x0004
94 #define UPLCOM_FLOW_IN_DTR	0x0008
95 #define UPLCOM_FLOW_IN_RTS	0x0010
96 #define UPLCOM_FLOW_OUT_RTS	0x0020
97 #define UPLCOM_FLOW_OUT_XON	0x0080
98 #define UPLCOM_FLOW_IN_XON	0x0100
99 
100 struct	uplcom_softc {
101 	USBBASEDEVICE		sc_dev;		/* base device */
102 	usbd_device_handle	sc_udev;	/* USB device */
103 	usbd_interface_handle	sc_iface;	/* interface */
104 	int			sc_iface_number;	/* interface number */
105 
106 	usbd_interface_handle	sc_intr_iface;	/* interrupt interface */
107 	int			sc_intr_number;	/* interrupt number */
108 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
109 	u_char			*sc_intr_buf;	/* interrupt buffer */
110 	int			sc_isize;
111 
112 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
113 	int			sc_dtr;		/* current DTR state */
114 	int			sc_rts;		/* current RTS state */
115 
116 	device_ptr_t		sc_subdev;	/* ucom device */
117 
118 	u_char			sc_dying;	/* disconnecting */
119 
120 	u_char			sc_lsr;		/* Local status register */
121 	u_char			sc_msr;		/* uplcom status register */
122 	int			sc_type_hx;	/* HX variant */
123 };
124 
125 /*
126  * These are the maximum number of bytes transferred per frame.
127  * The output buffer size cannot be increased due to the size encoding.
128  */
129 #define UPLCOMIBUFSIZE 256
130 #define UPLCOMOBUFSIZE 256
131 
132 Static	usbd_status uplcom_reset(struct uplcom_softc *);
133 Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
134 					   usb_cdc_line_state_t *state);
135 Static	usbd_status uplcom_set_crtscts(struct uplcom_softc *);
136 Static	void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
137 
138 Static	void uplcom_set(void *, int, int, int);
139 Static	void uplcom_dtr(struct uplcom_softc *, int);
140 Static	void uplcom_rts(struct uplcom_softc *, int);
141 Static	void uplcom_break(struct uplcom_softc *, int);
142 Static	void uplcom_set_line_state(struct uplcom_softc *);
143 Static	void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
144 #if TODO
145 Static	int  uplcom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr);
146 #endif
147 Static	int  uplcom_param(void *, int, struct termios *);
148 Static	int  uplcom_open(void *, int);
149 Static	void uplcom_close(void *, int);
150 
151 struct	ucom_methods uplcom_methods = {
152 	uplcom_get_status,
153 	uplcom_set,
154 	uplcom_param,
155 	NULL, /* uplcom_ioctl, TODO */
156 	uplcom_open,
157 	uplcom_close,
158 	NULL,
159 	NULL,
160 };
161 
162 static const struct usb_devno uplcom_devs[] = {
163 	/* I/O DATA USB-RSAQ2 */
164 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
165 	/* I/O DATA USB-RSAQ */
166 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
167 	/* PLANEX USB-RS232 URS-03 */
168 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
169 	/* IOGEAR/ATEN UC-232A */
170 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
171 	/* IOGEAR/ATENTRIPPLITE U209 */
172 	{ USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
173 	/* ELECOM UC-SGT */
174 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
175 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
176 	/* RATOC REX-USB60 */
177 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
178 	/* TDK USB-PHS Adapter UHA6400 */
179 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
180 	/* TDK USB-PDC Adapter UPA9664 */
181 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
182 	/* Ericsson DCU-10 & DCU-11, made by Susteen, Inc. */
183 	{ USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU11 },
184 	/* Sitecom USB to Serial. */
185 	{ USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
186 	/* Pharos USB GPS - Microsoft version */
187 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
188 	/* SOURCENEXT KeikaiDenwa 8 */
189 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
190 	/* SOURCENEXT KeikaiDenwa 8 with charger */
191 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
192 	/* HAL Corporation Crossam2+USB */
193 	{ USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
194 	/* AirPrime CDMA Wireless EVDO card */
195 	{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220 },
196 };
197 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
198 
199 USB_DECLARE_DRIVER(uplcom);
200 
USB_MATCH(uplcom)201 USB_MATCH(uplcom)
202 {
203 	USB_MATCH_START(uplcom, uaa);
204 
205 	if (uaa->iface != NULL)
206 		return (UMATCH_NONE);
207 
208 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
209 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
210 }
211 
USB_ATTACH(uplcom)212 USB_ATTACH(uplcom)
213 {
214 	USB_ATTACH_START(uplcom, sc, uaa);
215 	usbd_device_handle dev = uaa->device;
216 	usb_config_descriptor_t *cdesc;
217 	usb_device_descriptor_t *ddesc;
218 	usb_interface_descriptor_t *id;
219 	usb_endpoint_descriptor_t *ed;
220 
221 	char devinfo[1024];
222 	char *devname = USBDEVNAME(sc->sc_dev);
223 	usbd_status err;
224 	int i;
225 	struct ucom_attach_args uca;
226 
227         usbd_devinfo(dev, 0, devinfo, sizeof devinfo);
228         USB_ATTACH_SETUP;
229         printf("%s: %s\n", devname, devinfo);
230 
231         sc->sc_udev = dev;
232 
233 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
234 
235 	/* initialize endpoints */
236 	uca.bulkin = uca.bulkout = -1;
237 	sc->sc_intr_number = -1;
238 	sc->sc_intr_pipe = NULL;
239 
240 	/* Move the device into the configured state. */
241 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
242 	if (err) {
243 		printf("\n%s: failed to set configuration, err=%s\n",
244 			devname, usbd_errstr(err));
245 		sc->sc_dying = 1;
246 		USB_ATTACH_ERROR_RETURN;
247 	}
248 
249 	/* get the config descriptor */
250 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
251 
252 	if (cdesc == NULL) {
253 		printf("%s: failed to get configuration descriptor\n",
254 			USBDEVNAME(sc->sc_dev));
255 		sc->sc_dying = 1;
256 		USB_ATTACH_ERROR_RETURN;
257 	}
258 
259 	/* get the device descriptor */
260 	ddesc = usbd_get_device_descriptor(sc->sc_udev);
261 	if (ddesc == NULL) {
262 		printf("%s: failed to get device descriptor\n",
263 		    USBDEVNAME(sc->sc_dev));
264 		sc->sc_dying = 1;
265 		USB_ATTACH_ERROR_RETURN;
266 	}
267 
268 	/*
269 	 * The Linux driver suggest this will only be true for the HX
270 	 * variants. The datasheets disagree.
271 	 */
272 	if (ddesc->bMaxPacketSize == 0x40) {
273 		DPRINTF(("%s: Assuming HX variant\n", USBDEVNAME(sc->sc_dev)));
274 		sc->sc_type_hx = 1;
275 	} else
276 		sc->sc_type_hx = 0;
277 
278 	/* get the (first/common) interface */
279 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
280 							&sc->sc_iface);
281 	if (err) {
282 		printf("\n%s: failed to get interface, err=%s\n",
283 			devname, usbd_errstr(err));
284 		sc->sc_dying = 1;
285 		USB_ATTACH_ERROR_RETURN;
286 	}
287 
288 	/* Find the interrupt endpoints */
289 
290 	id = usbd_get_interface_descriptor(sc->sc_iface);
291 	sc->sc_iface_number = id->bInterfaceNumber;
292 
293 	for (i = 0; i < id->bNumEndpoints; i++) {
294 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
295 		if (ed == NULL) {
296 			printf("%s: no endpoint descriptor for %d\n",
297 				USBDEVNAME(sc->sc_dev), i);
298 			sc->sc_dying = 1;
299 			USB_ATTACH_ERROR_RETURN;
300 		}
301 
302 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
303 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
304 			sc->sc_intr_number = ed->bEndpointAddress;
305 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
306 		}
307 	}
308 
309 	if (sc->sc_intr_number== -1) {
310 		printf("%s: Could not find interrupt in\n",
311 			USBDEVNAME(sc->sc_dev));
312 		sc->sc_dying = 1;
313 		USB_ATTACH_ERROR_RETURN;
314 	}
315 
316 	/* keep interface for interrupt */
317 	sc->sc_intr_iface = sc->sc_iface;
318 
319 	/*
320 	 * USB-RSAQ1 has two interface
321 	 *
322 	 *  USB-RSAQ1       | USB-RSAQ2
323  	 * -----------------+-----------------
324 	 * Interface 0      |Interface 0
325 	 *  Interrupt(0x81) | Interrupt(0x81)
326 	 * -----------------+ BulkIN(0x02)
327 	 * Interface 1	    | BulkOUT(0x83)
328 	 *   BulkIN(0x02)   |
329 	 *   BulkOUT(0x83)  |
330 	 */
331 	if (cdesc->bNumInterface == 2) {
332 		err = usbd_device2interface_handle(dev,
333 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
334 		if (err) {
335 			printf("\n%s: failed to get second interface, err=%s\n",
336 							devname, usbd_errstr(err));
337 			sc->sc_dying = 1;
338 			USB_ATTACH_ERROR_RETURN;
339 		}
340 	}
341 
342 	/* Find the bulk{in,out} endpoints */
343 
344 	id = usbd_get_interface_descriptor(sc->sc_iface);
345 	sc->sc_iface_number = id->bInterfaceNumber;
346 
347 	for (i = 0; i < id->bNumEndpoints; i++) {
348 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
349 		if (ed == NULL) {
350 			printf("%s: no endpoint descriptor for %d\n",
351 				USBDEVNAME(sc->sc_dev), i);
352 			sc->sc_dying = 1;
353 			USB_ATTACH_ERROR_RETURN;
354 		}
355 
356 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
357 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
358 			uca.bulkin = ed->bEndpointAddress;
359 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
360 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
361 			uca.bulkout = ed->bEndpointAddress;
362 		}
363 	}
364 
365 	if (uca.bulkin == -1) {
366 		printf("%s: Could not find data bulk in\n",
367 			USBDEVNAME(sc->sc_dev));
368 		sc->sc_dying = 1;
369 		USB_ATTACH_ERROR_RETURN;
370 	}
371 
372 	if (uca.bulkout == -1) {
373 		printf("%s: Could not find data bulk out\n",
374 			USBDEVNAME(sc->sc_dev));
375 		sc->sc_dying = 1;
376 		USB_ATTACH_ERROR_RETURN;
377 	}
378 
379 	sc->sc_dtr = sc->sc_rts = -1;
380 	uca.portno = UCOM_UNK_PORTNO;
381 	/* bulkin, bulkout set above */
382 	uca.ibufsize = UPLCOMIBUFSIZE;
383 	uca.obufsize = UPLCOMOBUFSIZE;
384 	uca.ibufsizepad = UPLCOMIBUFSIZE;
385 	uca.opkthdrlen = 0;
386 	uca.device = dev;
387 	uca.iface = sc->sc_iface;
388 	uca.methods = &uplcom_methods;
389 	uca.arg = sc;
390 	uca.info = NULL;
391 
392 	err = uplcom_reset(sc);
393 
394 	if (err) {
395 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
396 			usbd_errstr(err));
397 		sc->sc_dying = 1;
398 		USB_ATTACH_ERROR_RETURN;
399 	}
400 
401 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
402 			   USBDEV(sc->sc_dev));
403 
404 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
405 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
406 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
407 
408 	USB_ATTACH_SUCCESS_RETURN;
409 }
410 
USB_DETACH(uplcom)411 USB_DETACH(uplcom)
412 {
413 	USB_DETACH_START(uplcom, sc);
414 	int rv = 0;
415 
416 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
417 
418         if (sc->sc_intr_pipe != NULL) {
419                 usbd_abort_pipe(sc->sc_intr_pipe);
420                 usbd_close_pipe(sc->sc_intr_pipe);
421 		free(sc->sc_intr_buf, M_USBDEV);
422                 sc->sc_intr_pipe = NULL;
423         }
424 
425 	sc->sc_dying = 1;
426 	if (sc->sc_subdev != NULL) {
427 		rv = config_detach(sc->sc_subdev, flags);
428 		sc->sc_subdev = NULL;
429 	}
430 
431 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
432 			   USBDEV(sc->sc_dev));
433 
434 	return (rv);
435 }
436 
437 int
uplcom_activate(device_ptr_t self,enum devact act)438 uplcom_activate(device_ptr_t self, enum devact act)
439 {
440 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
441 	int rv = 0;
442 
443 	switch (act) {
444 	case DVACT_ACTIVATE:
445 		return (EOPNOTSUPP);
446 
447 	case DVACT_DEACTIVATE:
448 		if (sc->sc_subdev != NULL)
449 			rv = config_deactivate(sc->sc_subdev);
450 		sc->sc_dying = 1;
451 		break;
452 	}
453 	return (rv);
454 }
455 
456 usbd_status
uplcom_reset(struct uplcom_softc * sc)457 uplcom_reset(struct uplcom_softc *sc)
458 {
459         usb_device_request_t req;
460 	usbd_status err;
461 
462         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
463         req.bRequest = UPLCOM_SET_REQUEST;
464         USETW(req.wValue, 0);
465         USETW(req.wIndex, sc->sc_iface_number);
466         USETW(req.wLength, 0);
467 
468         err = usbd_do_request(sc->sc_udev, &req, 0);
469 	if (err)
470 		return (EIO);
471 
472 	return (0);
473 }
474 
475 void
uplcom_set_line_state(struct uplcom_softc * sc)476 uplcom_set_line_state(struct uplcom_softc *sc)
477 {
478 	usb_device_request_t req;
479 	int ls;
480 
481 	/* Make sure we have initialized state for sc_dtr and sc_rts */
482 	if (sc->sc_dtr == -1)
483 		sc->sc_dtr = 0;
484 	if (sc->sc_rts == -1)
485 		sc->sc_rts = 0;
486 
487 	ls = (sc->sc_dtr ? UPLCOM_FLOW_OUT_DSR : 0) |
488 	    (sc->sc_rts ? UPLCOM_FLOW_OUT_CTS : 0);
489 
490 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
491 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
492 	USETW(req.wValue, ls);
493 	USETW(req.wIndex, sc->sc_iface_number);
494 	USETW(req.wLength, 0);
495 
496 	(void)usbd_do_request(sc->sc_udev, &req, 0);
497 
498 }
499 
500 void
uplcom_set(void * addr,int portno,int reg,int onoff)501 uplcom_set(void *addr, int portno, int reg, int onoff)
502 {
503 	struct uplcom_softc *sc = addr;
504 
505 	switch (reg) {
506 	case UCOM_SET_DTR:
507 		uplcom_dtr(sc, onoff);
508 		break;
509 	case UCOM_SET_RTS:
510 		uplcom_rts(sc, onoff);
511 		break;
512 	case UCOM_SET_BREAK:
513 		uplcom_break(sc, onoff);
514 		break;
515 	default:
516 		break;
517 	}
518 }
519 
520 void
uplcom_dtr(struct uplcom_softc * sc,int onoff)521 uplcom_dtr(struct uplcom_softc *sc, int onoff)
522 {
523 
524 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
525 
526 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
527 		return;
528 
529 	sc->sc_dtr = !!onoff;
530 
531 	uplcom_set_line_state(sc);
532 }
533 
534 void
uplcom_rts(struct uplcom_softc * sc,int onoff)535 uplcom_rts(struct uplcom_softc *sc, int onoff)
536 {
537 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
538 
539 	if (sc->sc_rts == -1 && !sc->sc_rts == !onoff)
540 		return;
541 
542 	sc->sc_rts = !!onoff;
543 
544 	uplcom_set_line_state(sc);
545 }
546 
547 void
uplcom_break(struct uplcom_softc * sc,int onoff)548 uplcom_break(struct uplcom_softc *sc, int onoff)
549 {
550 	usb_device_request_t req;
551 
552 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
553 
554 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
555 	req.bRequest = UCDC_SEND_BREAK;
556 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
557 	USETW(req.wIndex, sc->sc_iface_number);
558 	USETW(req.wLength, 0);
559 
560 	(void)usbd_do_request(sc->sc_udev, &req, 0);
561 }
562 
563 usbd_status
uplcom_set_crtscts(struct uplcom_softc * sc)564 uplcom_set_crtscts(struct uplcom_softc *sc)
565 {
566 	usb_device_request_t req;
567 	usbd_status err;
568 
569 	DPRINTF(("uplcom_set_crtscts: on\n"));
570 
571 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
572 	req.bRequest = UPLCOM_SET_REQUEST;
573 	USETW(req.wValue, 0);
574 	USETW(req.wIndex,
575 	    (sc->sc_type_hx ? UPLCOM_HX_SET_CRTSCTS : UPLCOM_SET_CRTSCTS));
576 	USETW(req.wLength, 0);
577 
578 	err = usbd_do_request(sc->sc_udev, &req, 0);
579 	if (err) {
580 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
581 			usbd_errstr(err)));
582 		return (err);
583 	}
584 
585 	return (USBD_NORMAL_COMPLETION);
586 }
587 
588 usbd_status
uplcom_set_line_coding(struct uplcom_softc * sc,usb_cdc_line_state_t * state)589 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
590 {
591 	usb_device_request_t req;
592 	usbd_status err;
593 
594 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
595 		UGETDW(state->dwDTERate), state->bCharFormat,
596 		state->bParityType, state->bDataBits));
597 
598 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
599 		DPRINTF(("uplcom_set_line_coding: already set\n"));
600 		return (USBD_NORMAL_COMPLETION);
601 	}
602 
603 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
604 	req.bRequest = UCDC_SET_LINE_CODING;
605 	USETW(req.wValue, 0);
606 	USETW(req.wIndex, sc->sc_iface_number);
607 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
608 
609 	err = usbd_do_request(sc->sc_udev, &req, state);
610 	if (err) {
611 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
612 			usbd_errstr(err)));
613 		return (err);
614 	}
615 
616 	sc->sc_line_state = *state;
617 
618 	return (USBD_NORMAL_COMPLETION);
619 }
620 
621 int
uplcom_param(void * addr,int portno,struct termios * t)622 uplcom_param(void *addr, int portno, struct termios *t)
623 {
624 	struct uplcom_softc *sc = addr;
625 	usbd_status err;
626 	usb_cdc_line_state_t ls;
627 
628 	DPRINTF(("uplcom_param: sc=%p\n", sc));
629 
630 	USETDW(ls.dwDTERate, t->c_ospeed);
631 	if (ISSET(t->c_cflag, CSTOPB))
632 		ls.bCharFormat = UCDC_STOP_BIT_2;
633 	else
634 		ls.bCharFormat = UCDC_STOP_BIT_1;
635 	if (ISSET(t->c_cflag, PARENB)) {
636 		if (ISSET(t->c_cflag, PARODD))
637 			ls.bParityType = UCDC_PARITY_ODD;
638 		else
639 			ls.bParityType = UCDC_PARITY_EVEN;
640 	} else
641 		ls.bParityType = UCDC_PARITY_NONE;
642 	switch (ISSET(t->c_cflag, CSIZE)) {
643 	case CS5:
644 		ls.bDataBits = 5;
645 		break;
646 	case CS6:
647 		ls.bDataBits = 6;
648 		break;
649 	case CS7:
650 		ls.bDataBits = 7;
651 		break;
652 	case CS8:
653 		ls.bDataBits = 8;
654 		break;
655 	}
656 
657 	err = uplcom_set_line_coding(sc, &ls);
658 	if (err) {
659 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
660 		return (EIO);
661 	}
662 
663 	if (ISSET(t->c_cflag, CRTSCTS))
664 		uplcom_set_crtscts(sc);
665 
666 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
667 		uplcom_set_line_state(sc);
668 
669 	if (err) {
670 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
671 		return (EIO);
672 	}
673 
674 	return (0);
675 }
676 
677 int
uplcom_open(void * addr,int portno)678 uplcom_open(void *addr, int portno)
679 {
680 	struct uplcom_softc *sc = addr;
681 	usb_device_request_t req;
682 	usbd_status uerr;
683 	int err;
684 
685 	if (sc->sc_dying)
686 		return (EIO);
687 
688 	DPRINTF(("uplcom_open: sc=%p\n", sc));
689 
690 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
691 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
692 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
693 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
694 			sc->sc_intr_buf, sc->sc_isize,
695 			uplcom_intr, USBD_DEFAULT_INTERVAL);
696 		if (err) {
697 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
698 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
699 					return (EIO);
700 		}
701 	}
702 
703 	if (sc->sc_type_hx == 1) {
704 		/*
705 		 * Undocumented (vendor unresponsive) - possibly changes
706 		 * flow control semantics. It is needed for HX variant devices.
707 		 */
708 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
709 		req.bRequest = UPLCOM_SET_REQUEST;
710 		USETW(req.wValue, 2);
711 		USETW(req.wIndex, 0x44);
712 		USETW(req.wLength, 0);
713 
714 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
715 		if (uerr)
716 			return (EIO);
717 
718 		/* Reset upstream data pipes */
719 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
720 		req.bRequest = UPLCOM_SET_REQUEST;
721 		USETW(req.wValue, 8);
722 		USETW(req.wIndex, 0);
723 		USETW(req.wLength, 0);
724 
725 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
726 		if (uerr)
727 			return (EIO);
728 
729 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
730 		req.bRequest = UPLCOM_SET_REQUEST;
731 		USETW(req.wValue, 9);
732 		USETW(req.wIndex, 0);
733 		USETW(req.wLength, 0);
734 
735 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
736 		if (uerr)
737 			return (EIO);
738 	}
739 
740 	return (0);
741 }
742 
743 void
uplcom_close(void * addr,int portno)744 uplcom_close(void *addr, int portno)
745 {
746 	struct uplcom_softc *sc = addr;
747 	int err;
748 
749 	if (sc->sc_dying)
750 		return;
751 
752 	DPRINTF(("uplcom_close: close\n"));
753 
754 	if (sc->sc_intr_pipe != NULL) {
755 		err = usbd_abort_pipe(sc->sc_intr_pipe);
756 		if (err)
757 			printf("%s: abort interrupt pipe failed: %s\n",
758 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
759 		err = usbd_close_pipe(sc->sc_intr_pipe);
760 		if (err)
761 			printf("%s: close interrupt pipe failed: %s\n",
762 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
763 		free(sc->sc_intr_buf, M_USBDEV);
764 		sc->sc_intr_pipe = NULL;
765 	}
766 }
767 
768 void
uplcom_intr(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)769 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
770 {
771 	struct uplcom_softc *sc = priv;
772 	u_char *buf = sc->sc_intr_buf;
773 	u_char pstatus;
774 
775 	if (sc->sc_dying)
776 		return;
777 
778 	if (status != USBD_NORMAL_COMPLETION) {
779 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
780 			return;
781 
782 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
783 			usbd_errstr(status)));
784 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
785 		return;
786 	}
787 
788 	DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
789 
790 	sc->sc_lsr = sc->sc_msr = 0;
791 	pstatus = buf[8];
792 	if (ISSET(pstatus, RSAQ_STATUS_CTS))
793 		sc->sc_msr |= UMSR_CTS;
794 	else
795 		sc->sc_msr &= ~UMSR_CTS;
796 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
797 		sc->sc_msr |= UMSR_DSR;
798 	else
799 		sc->sc_msr &= ~UMSR_DSR;
800 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
801 		sc->sc_msr |= UMSR_DCD;
802 	else
803 		sc->sc_msr &= ~UMSR_DCD;
804 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
805 }
806 
807 void
uplcom_get_status(void * addr,int portno,u_char * lsr,u_char * msr)808 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
809 {
810 	struct uplcom_softc *sc = addr;
811 
812 	DPRINTF(("uplcom_get_status:\n"));
813 
814 	if (lsr != NULL)
815 		*lsr = sc->sc_lsr;
816 	if (msr != NULL)
817 		*msr = sc->sc_msr;
818 }
819 
820 #if TODO
821 int
uplcom_ioctl(void * addr,int portno,u_long cmd,caddr_t data,int flag,usb_proc_ptr p)822 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
823 	     usb_proc_ptr p)
824 {
825 	struct uplcom_softc *sc = addr;
826 	int error = 0;
827 
828 	if (sc->sc_dying)
829 		return (EIO);
830 
831 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
832 
833 	switch (cmd) {
834 	case TIOCNOTTY:
835 	case TIOCMGET:
836 	case TIOCMSET:
837 	case USB_GET_CM_OVER_DATA:
838 	case USB_SET_CM_OVER_DATA:
839 		break;
840 
841 	default:
842 		DPRINTF(("uplcom_ioctl: unknown\n"));
843 		error = ENOTTY;
844 		break;
845 	}
846 
847 	return (error);
848 }
849 #endif
850