Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / usb / uplcom.c
blobc884da6c466a83940bc7d6769c54bd7c746e8a8e
1 /* $NetBSD: uplcom.c,v 1.69 2009/11/12 19:58:27 dyoung Exp $ */
2 /*
3 * Copyright (c) 2001 The NetBSD Foundation, Inc.
4 * All rights reserved.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Ichiro FUKUHARA (ichiro@ichiro.org).
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.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 * General information: http://www.prolific.com.tw/fr_pl2303.htm
33 * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.69 2009/11/12 19:58:27 dyoung Exp $");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/ioctl.h>
44 #include <sys/conf.h>
45 #include <sys/tty.h>
46 #include <sys/file.h>
47 #include <sys/select.h>
48 #include <sys/proc.h>
49 #include <sys/device.h>
50 #include <sys/poll.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbcdc.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdevs.h>
58 #include <dev/usb/usb_quirks.h>
60 #include <dev/usb/ucomvar.h>
62 #ifdef UPLCOM_DEBUG
63 #define DPRINTFN(n, x) if (uplcomdebug > (n)) logprintf x
64 int uplcomdebug = 0;
65 #else
66 #define DPRINTFN(n, x)
67 #endif
68 #define DPRINTF(x) DPRINTFN(0, x)
70 #define UPLCOM_CONFIG_INDEX 0
71 #define UPLCOM_IFACE_INDEX 0
72 #define UPLCOM_SECOND_IFACE_INDEX 1
74 #define UPLCOM_SET_REQUEST 0x01
75 #define UPLCOM_SET_CRTSCTS_0 0x41
76 #define UPLCOM_SET_CRTSCTS_HX 0x61
78 #define UPLCOM_N_SERIAL_CTS 0x80
80 enum pl2303_type {
81 UPLCOM_TYPE_0, /* we use this for all non-HX variants */
82 UPLCOM_TYPE_HX,
85 struct uplcom_softc {
86 USBBASEDEVICE sc_dev; /* base device */
87 usbd_device_handle sc_udev; /* USB device */
88 usbd_interface_handle sc_iface; /* interface */
89 int sc_iface_number; /* interface number */
91 usbd_interface_handle sc_intr_iface; /* interrupt interface */
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;
97 usb_cdc_line_state_t sc_line_state; /* current line state */
98 int sc_dtr; /* current DTR state */
99 int sc_rts; /* current RTS state */
101 device_ptr_t sc_subdev; /* ucom device */
103 u_char sc_dying; /* disconnecting */
105 u_char sc_lsr; /* Local status register */
106 u_char sc_msr; /* uplcom status register */
108 enum pl2303_type sc_type; /* PL2303 chip type */
112 * These are the maximum number of bytes transferred per frame.
113 * The output buffer size cannot be increased due to the size encoding.
115 #define UPLCOMIBUFSIZE 256
116 #define UPLCOMOBUFSIZE 256
118 Static usbd_status uplcom_reset(struct uplcom_softc *);
119 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
120 usb_cdc_line_state_t *state);
121 Static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
122 Static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
124 Static void uplcom_set(void *, int, int, int);
125 Static void uplcom_dtr(struct uplcom_softc *, int);
126 Static void uplcom_rts(struct uplcom_softc *, int);
127 Static void uplcom_break(struct uplcom_softc *, int);
128 Static void uplcom_set_line_state(struct uplcom_softc *);
129 Static void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
130 #if TODO
131 Static int uplcom_ioctl(void *, int, u_long, void *, int, usb_proc_ptr );
132 #endif
133 Static int uplcom_param(void *, int, struct termios *);
134 Static int uplcom_open(void *, int);
135 Static void uplcom_close(void *, int);
136 Static usbd_status uplcom_vendor_control_write(usbd_device_handle, u_int16_t, u_int16_t);
138 struct ucom_methods uplcom_methods = {
139 uplcom_get_status,
140 uplcom_set,
141 uplcom_param,
142 NULL, /* uplcom_ioctl, TODO */
143 uplcom_open,
144 uplcom_close,
145 NULL,
146 NULL,
149 static const struct usb_devno uplcom_devs[] = {
150 /* I/O DATA USB-RSAQ2 */
151 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
152 /* I/O DATA USB-RSAQ3 */
153 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
154 /* I/O DATA USB-RSAQ */
155 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
156 /* I/O DATA USB-RSAQ5 */
157 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
158 /* PLANEX USB-RS232 URS-03 */
159 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
160 /* various */
161 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
162 /* SMART Technologies USB to serial */
163 { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
164 /* IOGEAR/ATENTRIPPLITE */
165 { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
166 /* ELECOM UC-SGT */
167 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
168 /* ELECOM UC-SGT0 */
169 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
170 /* Panasonic 50" Touch Panel */
171 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
172 /* RATOC REX-USB60 */
173 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
174 /* TDK USB-PHS Adapter UHA6400 */
175 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
176 /* TDK USB-PDC Adapter UPA9664 */
177 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
178 /* Sony Ericsson USB Cable */
179 { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
180 /* SOURCENEXT KeikaiDenwa 8 */
181 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
182 /* SOURCENEXT KeikaiDenwa 8 with charger */
183 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
184 /* HAL Corporation Crossam2+USB */
185 { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
186 /* Sitecom USB to serial cable */
187 { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
188 /* Pharos USB GPS - Microsoft version */
189 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
190 /* Willcom WS002IN (DD) */
191 { USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN },
192 /* COREGA CG-USBRS232R */
193 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGUSBRS232R },
195 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
197 int uplcom_match(device_t, cfdata_t, void *);
198 void uplcom_attach(device_t, device_t, void *);
199 void uplcom_childdet(device_t, device_t);
200 int uplcom_detach(device_t, int);
201 int uplcom_activate(device_t, enum devact);
202 extern struct cfdriver uplcom_cd;
203 CFATTACH_DECL2_NEW(uplcom, sizeof(struct uplcom_softc), uplcom_match,
204 uplcom_attach, uplcom_detach, uplcom_activate, NULL, uplcom_childdet);
206 USB_MATCH(uplcom)
208 USB_MATCH_START(uplcom, uaa);
210 return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
211 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
214 USB_ATTACH(uplcom)
216 USB_ATTACH_START(uplcom, sc, uaa);
217 usbd_device_handle dev = uaa->device;
218 usb_device_descriptor_t *ddesc;
219 usb_config_descriptor_t *cdesc;
220 usb_interface_descriptor_t *id;
221 usb_endpoint_descriptor_t *ed;
222 char *devinfop;
223 const char *devname = device_xname(self);
224 usbd_status err;
225 int i;
226 struct ucom_attach_args uca;
228 sc->sc_dev = self;
230 aprint_naive("\n");
231 aprint_normal("\n");
233 devinfop = usbd_devinfo_alloc(dev, 0);
234 aprint_normal_dev(self, "%s\n", devinfop);
235 usbd_devinfo_free(devinfop);
237 sc->sc_udev = dev;
239 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
241 /* initialize endpoints */
242 uca.bulkin = uca.bulkout = -1;
243 sc->sc_intr_number = -1;
244 sc->sc_intr_pipe = NULL;
246 /* Move the device into the configured state. */
247 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
248 if (err) {
249 aprint_error("\n%s: failed to set configuration, err=%s\n",
250 devname, usbd_errstr(err));
251 sc->sc_dying = 1;
252 USB_ATTACH_ERROR_RETURN;
255 /* determine chip type */
256 ddesc = usbd_get_device_descriptor(dev);
257 if (ddesc->bDeviceClass != UDCLASS_COMM &&
258 ddesc->bMaxPacketSize == 0x40)
259 sc->sc_type = UPLCOM_TYPE_HX;
261 #ifdef UPLCOM_DEBUG
262 /* print the chip type */
263 if (sc->sc_type == UPLCOM_TYPE_HX) {
264 DPRINTF(("uplcom_attach: chiptype HX\n"));
265 } else {
266 DPRINTF(("uplcom_attach: chiptype 0\n"));
268 #endif
270 /* Move the device into the configured state. */
271 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
272 if (err) {
273 aprint_error_dev(self, "failed to set configuration: %s\n",
274 usbd_errstr(err));
275 sc->sc_dying = 1;
276 USB_ATTACH_ERROR_RETURN;
279 /* get the config descriptor */
280 cdesc = usbd_get_config_descriptor(sc->sc_udev);
282 if (cdesc == NULL) {
283 aprint_error_dev(self,
284 "failed to get configuration descriptor\n");
285 sc->sc_dying = 1;
286 USB_ATTACH_ERROR_RETURN;
289 /* get the (first/common) interface */
290 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
291 &sc->sc_iface);
292 if (err) {
293 aprint_error("\n%s: failed to get interface, err=%s\n",
294 devname, usbd_errstr(err));
295 sc->sc_dying = 1;
296 USB_ATTACH_ERROR_RETURN;
299 /* Find the interrupt endpoints */
301 id = usbd_get_interface_descriptor(sc->sc_iface);
302 sc->sc_iface_number = id->bInterfaceNumber;
304 for (i = 0; i < id->bNumEndpoints; i++) {
305 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
306 if (ed == NULL) {
307 aprint_error_dev(self,
308 "no endpoint descriptor for %d\n", i);
309 sc->sc_dying = 1;
310 USB_ATTACH_ERROR_RETURN;
313 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
314 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
315 sc->sc_intr_number = ed->bEndpointAddress;
316 sc->sc_isize = UGETW(ed->wMaxPacketSize);
320 if (sc->sc_intr_number== -1) {
321 aprint_error_dev(self, "Could not find interrupt in\n");
322 sc->sc_dying = 1;
323 USB_ATTACH_ERROR_RETURN;
326 /* keep interface for interrupt */
327 sc->sc_intr_iface = sc->sc_iface;
330 * USB-RSAQ1 has two interface
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) |
341 if (cdesc->bNumInterface == 2) {
342 err = usbd_device2interface_handle(dev,
343 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
344 if (err) {
345 aprint_error("\n%s: failed to get second interface, err=%s\n",
346 devname, usbd_errstr(err));
347 sc->sc_dying = 1;
348 USB_ATTACH_ERROR_RETURN;
352 /* Find the bulk{in,out} endpoints */
354 id = usbd_get_interface_descriptor(sc->sc_iface);
355 sc->sc_iface_number = id->bInterfaceNumber;
357 for (i = 0; i < id->bNumEndpoints; i++) {
358 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
359 if (ed == NULL) {
360 aprint_error_dev(self,
361 "no endpoint descriptor for %d\n", i);
362 sc->sc_dying = 1;
363 USB_ATTACH_ERROR_RETURN;
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;
375 if (uca.bulkin == -1) {
376 aprint_error_dev(self, "Could not find data bulk in\n");
377 sc->sc_dying = 1;
378 USB_ATTACH_ERROR_RETURN;
381 if (uca.bulkout == -1) {
382 aprint_error_dev(self, "Could not find data bulk out\n");
383 sc->sc_dying = 1;
384 USB_ATTACH_ERROR_RETURN;
387 sc->sc_dtr = sc->sc_rts = -1;
388 uca.portno = UCOM_UNK_PORTNO;
389 /* bulkin, bulkout set above */
390 uca.ibufsize = UPLCOMIBUFSIZE;
391 uca.obufsize = UPLCOMOBUFSIZE;
392 uca.ibufsizepad = UPLCOMIBUFSIZE;
393 uca.opkthdrlen = 0;
394 uca.device = dev;
395 uca.iface = sc->sc_iface;
396 uca.methods = &uplcom_methods;
397 uca.arg = sc;
398 uca.info = NULL;
400 err = uplcom_reset(sc);
402 if (err) {
403 aprint_error_dev(self, "reset failed, %s\n", usbd_errstr(err));
404 sc->sc_dying = 1;
405 USB_ATTACH_ERROR_RETURN;
408 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
409 USBDEV(sc->sc_dev));
411 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
412 uca.bulkin, uca.bulkout, sc->sc_intr_number ));
413 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
414 ucomprint, ucomsubmatch);
416 USB_ATTACH_SUCCESS_RETURN;
419 void
420 uplcom_childdet(device_t self, device_t child)
422 struct uplcom_softc *sc = device_private(self);
424 KASSERT(sc->sc_subdev == child);
425 sc->sc_subdev = NULL;
428 USB_DETACH(uplcom)
430 USB_DETACH_START(uplcom, sc);
431 int rv = 0;
433 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
435 if (sc->sc_intr_pipe != NULL) {
436 usbd_abort_pipe(sc->sc_intr_pipe);
437 usbd_close_pipe(sc->sc_intr_pipe);
438 free(sc->sc_intr_buf, M_USBDEV);
439 sc->sc_intr_pipe = NULL;
442 sc->sc_dying = 1;
443 if (sc->sc_subdev != NULL)
444 rv = config_detach(sc->sc_subdev, flags);
446 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
447 USBDEV(sc->sc_dev));
449 return (rv);
453 uplcom_activate(device_t self, enum devact act)
455 struct uplcom_softc *sc = device_private(self);
457 switch (act) {
458 case DVACT_DEACTIVATE:
459 sc->sc_dying = 1;
460 return 0;
461 default:
462 return EOPNOTSUPP;
466 usbd_status
467 uplcom_reset(struct uplcom_softc *sc)
469 usb_device_request_t req;
470 usbd_status err;
472 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
473 req.bRequest = UPLCOM_SET_REQUEST;
474 USETW(req.wValue, 0);
475 USETW(req.wIndex, sc->sc_iface_number);
476 USETW(req.wLength, 0);
478 err = usbd_do_request(sc->sc_udev, &req, 0);
479 if (err)
480 return (EIO);
482 return (0);
485 struct pl2303x_init {
486 uint8_t req_type;
487 uint8_t request;
488 uint16_t value;
489 uint16_t index;
490 uint16_t length;
493 static const struct pl2303x_init pl2303x[] = {
494 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
495 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 },
496 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
497 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
498 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
499 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 },
500 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
501 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
502 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 },
503 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 },
504 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 }
506 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
508 static usbd_status
509 uplcom_pl2303x_init(struct uplcom_softc *sc)
511 usb_device_request_t req;
512 usbd_status err;
513 int i;
515 for (i = 0; i < N_PL2302X_INIT; i++) {
516 req.bmRequestType = pl2303x[i].req_type;
517 req.bRequest = pl2303x[i].request;
518 USETW(req.wValue, pl2303x[i].value);
519 USETW(req.wIndex, pl2303x[i].index);
520 USETW(req.wLength, pl2303x[i].length);
522 err = usbd_do_request(sc->sc_udev, &req, 0);
523 if (err) {
524 aprint_error_dev(sc->sc_dev,
525 "uplcom_pl2303x_init failed: %s\n",
526 usbd_errstr(err));
527 return (EIO);
531 return (0);
534 void
535 uplcom_set_line_state(struct uplcom_softc *sc)
537 usb_device_request_t req;
538 int ls;
540 /* make sure we have initialized state for sc_dtr and sc_rts */
541 if (sc->sc_dtr == -1)
542 sc->sc_dtr = 0;
543 if (sc->sc_rts == -1)
544 sc->sc_rts = 0;
546 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
547 (sc->sc_rts ? UCDC_LINE_RTS : 0);
549 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
550 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
551 USETW(req.wValue, ls);
552 USETW(req.wIndex, sc->sc_iface_number);
553 USETW(req.wLength, 0);
555 (void)usbd_do_request(sc->sc_udev, &req, 0);
558 void
559 uplcom_set(void *addr, int portno, int reg, int onoff)
561 struct uplcom_softc *sc = addr;
563 switch (reg) {
564 case UCOM_SET_DTR:
565 uplcom_dtr(sc, onoff);
566 break;
567 case UCOM_SET_RTS:
568 uplcom_rts(sc, onoff);
569 break;
570 case UCOM_SET_BREAK:
571 uplcom_break(sc, onoff);
572 break;
573 default:
574 break;
578 void
579 uplcom_dtr(struct uplcom_softc *sc, int onoff)
582 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
584 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
585 return;
587 sc->sc_dtr = !!onoff;
589 uplcom_set_line_state(sc);
592 void
593 uplcom_rts(struct uplcom_softc *sc, int onoff)
595 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
597 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
598 return;
600 sc->sc_rts = !!onoff;
602 uplcom_set_line_state(sc);
605 void
606 uplcom_break(struct uplcom_softc *sc, int onoff)
608 usb_device_request_t req;
610 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
612 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
613 req.bRequest = UCDC_SEND_BREAK;
614 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
615 USETW(req.wIndex, sc->sc_iface_number);
616 USETW(req.wLength, 0);
618 (void)usbd_do_request(sc->sc_udev, &req, 0);
621 usbd_status
622 uplcom_set_crtscts(struct uplcom_softc *sc)
624 usb_device_request_t req;
625 usbd_status err;
627 DPRINTF(("uplcom_set_crtscts: on\n"));
629 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
630 req.bRequest = UPLCOM_SET_REQUEST;
631 USETW(req.wValue, 0);
632 if (sc->sc_type == UPLCOM_TYPE_HX)
633 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
634 else
635 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
636 USETW(req.wLength, 0);
638 err = usbd_do_request(sc->sc_udev, &req, 0);
639 if (err) {
640 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
641 usbd_errstr(err)));
642 return (err);
645 return (USBD_NORMAL_COMPLETION);
648 usbd_status
649 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
651 usb_device_request_t req;
652 usbd_status err;
654 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
655 UGETDW(state->dwDTERate), state->bCharFormat,
656 state->bParityType, state->bDataBits));
658 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
659 DPRINTF(("uplcom_set_line_coding: already set\n"));
660 return (USBD_NORMAL_COMPLETION);
663 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
664 req.bRequest = UCDC_SET_LINE_CODING;
665 USETW(req.wValue, 0);
666 USETW(req.wIndex, sc->sc_iface_number);
667 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
669 err = usbd_do_request(sc->sc_udev, &req, state);
670 if (err) {
671 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
672 usbd_errstr(err)));
673 return (err);
676 sc->sc_line_state = *state;
678 return (USBD_NORMAL_COMPLETION);
682 uplcom_param(void *addr, int portno, struct termios *t)
684 struct uplcom_softc *sc = addr;
685 usbd_status err;
686 usb_cdc_line_state_t ls;
688 DPRINTF(("uplcom_param: sc=%p\n", sc));
690 USETDW(ls.dwDTERate, t->c_ospeed);
691 if (ISSET(t->c_cflag, CSTOPB))
692 ls.bCharFormat = UCDC_STOP_BIT_2;
693 else
694 ls.bCharFormat = UCDC_STOP_BIT_1;
695 if (ISSET(t->c_cflag, PARENB)) {
696 if (ISSET(t->c_cflag, PARODD))
697 ls.bParityType = UCDC_PARITY_ODD;
698 else
699 ls.bParityType = UCDC_PARITY_EVEN;
700 } else
701 ls.bParityType = UCDC_PARITY_NONE;
702 switch (ISSET(t->c_cflag, CSIZE)) {
703 case CS5:
704 ls.bDataBits = 5;
705 break;
706 case CS6:
707 ls.bDataBits = 6;
708 break;
709 case CS7:
710 ls.bDataBits = 7;
711 break;
712 case CS8:
713 ls.bDataBits = 8;
714 break;
717 err = uplcom_set_line_coding(sc, &ls);
718 if (err) {
719 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
720 return (EIO);
723 if (ISSET(t->c_cflag, CRTSCTS))
724 uplcom_set_crtscts(sc);
726 if (sc->sc_rts == -1 || sc->sc_dtr == -1)
727 uplcom_set_line_state(sc);
729 if (err) {
730 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
731 return (EIO);
734 return (0);
737 Static usbd_status
738 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
740 usb_device_request_t req;
741 usbd_status err;
743 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
744 req.bRequest = UPLCOM_SET_REQUEST;
745 USETW(req.wValue, value);
746 USETW(req.wIndex, index);
747 USETW(req.wLength, 0);
749 err = usbd_do_request(dev, &req, NULL);
751 if (err) {
752 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
753 usbd_errstr(err), err));
756 return err;
760 uplcom_open(void *addr, int portno)
762 struct uplcom_softc *sc = addr;
763 usbd_status err;
765 if (sc->sc_dying)
766 return (EIO);
768 DPRINTF(("uplcom_open: sc=%p\n", sc));
770 /* Some unknown device frobbing. */
771 if (sc->sc_type == UPLCOM_TYPE_HX)
772 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
773 else
774 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
776 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
777 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
778 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
779 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
780 sc->sc_intr_buf, sc->sc_isize,
781 uplcom_intr, USBD_DEFAULT_INTERVAL);
782 if (err) {
783 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
784 USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
785 return (EIO);
789 if (sc->sc_type == UPLCOM_TYPE_HX)
790 return (uplcom_pl2303x_init(sc));
792 return (0);
795 void
796 uplcom_close(void *addr, int portno)
798 struct uplcom_softc *sc = addr;
799 int err;
801 if (sc->sc_dying)
802 return;
804 DPRINTF(("uplcom_close: close\n"));
806 if (sc->sc_intr_pipe != NULL) {
807 err = usbd_abort_pipe(sc->sc_intr_pipe);
808 if (err)
809 printf("%s: abort interrupt pipe failed: %s\n",
810 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
811 err = usbd_close_pipe(sc->sc_intr_pipe);
812 if (err)
813 printf("%s: close interrupt pipe failed: %s\n",
814 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
815 free(sc->sc_intr_buf, M_USBDEV);
816 sc->sc_intr_pipe = NULL;
820 void
821 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
822 usbd_status status)
824 struct uplcom_softc *sc = priv;
825 u_char *buf = sc->sc_intr_buf;
826 u_char pstatus;
828 if (sc->sc_dying)
829 return;
831 if (status != USBD_NORMAL_COMPLETION) {
832 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
833 return;
835 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
836 usbd_errstr(status)));
837 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
838 return;
841 DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
843 sc->sc_lsr = sc->sc_msr = 0;
844 pstatus = buf[8];
845 if (ISSET(pstatus, UPLCOM_N_SERIAL_CTS))
846 sc->sc_msr |= UMSR_CTS;
847 if (ISSET(pstatus, UCDC_N_SERIAL_RI))
848 sc->sc_msr |= UMSR_RI;
849 if (ISSET(pstatus, UCDC_N_SERIAL_DSR))
850 sc->sc_msr |= UMSR_DSR;
851 if (ISSET(pstatus, UCDC_N_SERIAL_DCD))
852 sc->sc_msr |= UMSR_DCD;
853 ucom_status_change(device_private(sc->sc_subdev));
856 void
857 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
859 struct uplcom_softc *sc = addr;
861 DPRINTF(("uplcom_get_status:\n"));
863 if (lsr != NULL)
864 *lsr = sc->sc_lsr;
865 if (msr != NULL)
866 *msr = sc->sc_msr;
869 #if TODO
871 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
872 usb_proc_ptr p)
874 struct uplcom_softc *sc = addr;
875 int error = 0;
877 if (sc->sc_dying)
878 return (EIO);
880 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
882 switch (cmd) {
883 case TIOCNOTTY:
884 case TIOCMGET:
885 case TIOCMSET:
886 case USB_GET_CM_OVER_DATA:
887 case USB_SET_CM_OVER_DATA:
888 break;
890 default:
891 DPRINTF(("uplcom_ioctl: unknown\n"));
892 error = ENOTTY;
893 break;
896 return (error);
898 #endif