uplcom.c revision 1.67 1 /* $NetBSD: uplcom.c,v 1.67 2008/11/20 10:50:42 jnemeth 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 (at) 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 *
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.
29 */
30
31 /*
32 * General information: http://www.prolific.com.tw/fr_pl2303.htm
33 * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.67 2008/11/20 10:50:42 jnemeth Exp $");
38
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>
51
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbcdc.h>
54
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>
59
60 #include <dev/usb/ucomvar.h>
61
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)
69
70 #define UPLCOM_CONFIG_INDEX 0
71 #define UPLCOM_IFACE_INDEX 0
72 #define UPLCOM_SECOND_IFACE_INDEX 1
73
74 #define UPLCOM_SET_REQUEST 0x01
75 #define UPLCOM_SET_CRTSCTS_0 0x41
76 #define UPLCOM_SET_CRTSCTS_HX 0x61
77 #define RSAQ_STATUS_DSR 0x02
78 #define RSAQ_STATUS_DCD 0x01
79
80 enum pl2303_type {
81 UPLCOM_TYPE_0, /* we use this for all non-HX variants */
82 UPLCOM_TYPE_HX,
83 };
84
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 */
90
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;
96
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 */
100
101 device_ptr_t sc_subdev; /* ucom device */
102
103 u_char sc_dying; /* disconnecting */
104
105 u_char sc_lsr; /* Local status register */
106 u_char sc_msr; /* uplcom status register */
107
108 enum pl2303_type sc_type; /* PL2303 chip type */
109 };
110
111 /*
112 * These are the maximum number of bytes transferred per frame.
113 * The output buffer size cannot be increased due to the size encoding.
114 */
115 #define UPLCOMIBUFSIZE 256
116 #define UPLCOMOBUFSIZE 256
117
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);
123
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);
137
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,
147 };
148
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 },
194 };
195 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
196
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);
205
206 USB_MATCH(uplcom)
207 {
208 USB_MATCH_START(uplcom, uaa);
209
210 return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
211 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
212 }
213
214 USB_ATTACH(uplcom)
215 {
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;
227
228 sc->sc_dev = self;
229
230 devinfop = usbd_devinfo_alloc(dev, 0);
231 USB_ATTACH_SETUP;
232 aprint_normal_dev(self, "%s\n", devinfop);
233 usbd_devinfo_free(devinfop);
234
235 sc->sc_udev = dev;
236
237 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
238
239 /* initialize endpoints */
240 uca.bulkin = uca.bulkout = -1;
241 sc->sc_intr_number = -1;
242 sc->sc_intr_pipe = NULL;
243
244 /* Move the device into the configured state. */
245 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
246 if (err) {
247 aprint_error("\n%s: failed to set configuration, err=%s\n",
248 devname, usbd_errstr(err));
249 sc->sc_dying = 1;
250 USB_ATTACH_ERROR_RETURN;
251 }
252
253 /* determine chip type */
254 ddesc = usbd_get_device_descriptor(dev);
255 if (ddesc->bDeviceClass != UDCLASS_COMM &&
256 ddesc->bMaxPacketSize == 0x40)
257 sc->sc_type = UPLCOM_TYPE_HX;
258
259 #ifdef UPLCOM_DEBUG
260 /* print the chip type */
261 if (sc->sc_type == UPLCOM_TYPE_HX) {
262 DPRINTF(("uplcom_attach: chiptype HX\n"));
263 } else {
264 DPRINTF(("uplcom_attach: chiptype 0\n"));
265 }
266 #endif
267
268 /* Move the device into the configured state. */
269 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
270 if (err) {
271 aprint_error_dev(self, "failed to set configuration: %s\n",
272 usbd_errstr(err));
273 sc->sc_dying = 1;
274 USB_ATTACH_ERROR_RETURN;
275 }
276
277 /* get the config descriptor */
278 cdesc = usbd_get_config_descriptor(sc->sc_udev);
279
280 if (cdesc == NULL) {
281 aprint_error_dev(self,
282 "failed to get configuration descriptor\n");
283 sc->sc_dying = 1;
284 USB_ATTACH_ERROR_RETURN;
285 }
286
287 /* get the (first/common) interface */
288 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
289 &sc->sc_iface);
290 if (err) {
291 aprint_error("\n%s: failed to get interface, err=%s\n",
292 devname, usbd_errstr(err));
293 sc->sc_dying = 1;
294 USB_ATTACH_ERROR_RETURN;
295 }
296
297 /* Find the interrupt endpoints */
298
299 id = usbd_get_interface_descriptor(sc->sc_iface);
300 sc->sc_iface_number = id->bInterfaceNumber;
301
302 for (i = 0; i < id->bNumEndpoints; i++) {
303 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
304 if (ed == NULL) {
305 aprint_error_dev(self,
306 "no endpoint descriptor for %d\n", i);
307 sc->sc_dying = 1;
308 USB_ATTACH_ERROR_RETURN;
309 }
310
311 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
312 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
313 sc->sc_intr_number = ed->bEndpointAddress;
314 sc->sc_isize = UGETW(ed->wMaxPacketSize);
315 }
316 }
317
318 if (sc->sc_intr_number== -1) {
319 aprint_error_dev(self, "Could not find interrupt in\n");
320 sc->sc_dying = 1;
321 USB_ATTACH_ERROR_RETURN;
322 }
323
324 /* keep interface for interrupt */
325 sc->sc_intr_iface = sc->sc_iface;
326
327 /*
328 * USB-RSAQ1 has two interface
329 *
330 * USB-RSAQ1 | USB-RSAQ2
331 * -----------------+-----------------
332 * Interface 0 |Interface 0
333 * Interrupt(0x81) | Interrupt(0x81)
334 * -----------------+ BulkIN(0x02)
335 * Interface 1 | BulkOUT(0x83)
336 * BulkIN(0x02) |
337 * BulkOUT(0x83) |
338 */
339 if (cdesc->bNumInterface == 2) {
340 err = usbd_device2interface_handle(dev,
341 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
342 if (err) {
343 aprint_error("\n%s: failed to get second interface, err=%s\n",
344 devname, usbd_errstr(err));
345 sc->sc_dying = 1;
346 USB_ATTACH_ERROR_RETURN;
347 }
348 }
349
350 /* Find the bulk{in,out} endpoints */
351
352 id = usbd_get_interface_descriptor(sc->sc_iface);
353 sc->sc_iface_number = id->bInterfaceNumber;
354
355 for (i = 0; i < id->bNumEndpoints; i++) {
356 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
357 if (ed == NULL) {
358 aprint_error_dev(self,
359 "no endpoint descriptor for %d\n", i);
360 sc->sc_dying = 1;
361 USB_ATTACH_ERROR_RETURN;
362 }
363
364 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
365 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
366 uca.bulkin = ed->bEndpointAddress;
367 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
368 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
369 uca.bulkout = ed->bEndpointAddress;
370 }
371 }
372
373 if (uca.bulkin == -1) {
374 aprint_error_dev(self, "Could not find data bulk in\n");
375 sc->sc_dying = 1;
376 USB_ATTACH_ERROR_RETURN;
377 }
378
379 if (uca.bulkout == -1) {
380 aprint_error_dev(self, "Could not find data bulk out\n");
381 sc->sc_dying = 1;
382 USB_ATTACH_ERROR_RETURN;
383 }
384
385 sc->sc_dtr = sc->sc_rts = -1;
386 uca.portno = UCOM_UNK_PORTNO;
387 /* bulkin, bulkout set above */
388 uca.ibufsize = UPLCOMIBUFSIZE;
389 uca.obufsize = UPLCOMOBUFSIZE;
390 uca.ibufsizepad = UPLCOMIBUFSIZE;
391 uca.opkthdrlen = 0;
392 uca.device = dev;
393 uca.iface = sc->sc_iface;
394 uca.methods = &uplcom_methods;
395 uca.arg = sc;
396 uca.info = NULL;
397
398 err = uplcom_reset(sc);
399
400 if (err) {
401 aprint_error_dev(self, "reset failed, %s\n", usbd_errstr(err));
402 sc->sc_dying = 1;
403 USB_ATTACH_ERROR_RETURN;
404 }
405
406 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
407 USBDEV(sc->sc_dev));
408
409 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
410 uca.bulkin, uca.bulkout, sc->sc_intr_number ));
411 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
412 ucomprint, ucomsubmatch);
413
414 USB_ATTACH_SUCCESS_RETURN;
415 }
416
417 void
418 uplcom_childdet(device_t self, device_t child)
419 {
420 struct uplcom_softc *sc = device_private(self);
421
422 KASSERT(sc->sc_subdev == child);
423 sc->sc_subdev = NULL;
424 }
425
426 USB_DETACH(uplcom)
427 {
428 USB_DETACH_START(uplcom, sc);
429 int rv = 0;
430
431 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
432
433 if (sc->sc_intr_pipe != NULL) {
434 usbd_abort_pipe(sc->sc_intr_pipe);
435 usbd_close_pipe(sc->sc_intr_pipe);
436 free(sc->sc_intr_buf, M_USBDEV);
437 sc->sc_intr_pipe = NULL;
438 }
439
440 sc->sc_dying = 1;
441 if (sc->sc_subdev != NULL)
442 rv = config_detach(sc->sc_subdev, flags);
443
444 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
445 USBDEV(sc->sc_dev));
446
447 return (rv);
448 }
449
450 int
451 uplcom_activate(device_t self, enum devact act)
452 {
453 struct uplcom_softc *sc = device_private(self);
454 int rv = 0;
455
456 switch (act) {
457 case DVACT_ACTIVATE:
458 return (EOPNOTSUPP);
459
460 case DVACT_DEACTIVATE:
461 if (sc->sc_subdev != NULL)
462 rv = config_deactivate(sc->sc_subdev);
463 sc->sc_dying = 1;
464 break;
465 }
466 return (rv);
467 }
468
469 usbd_status
470 uplcom_reset(struct uplcom_softc *sc)
471 {
472 usb_device_request_t req;
473 usbd_status err;
474
475 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
476 req.bRequest = UPLCOM_SET_REQUEST;
477 USETW(req.wValue, 0);
478 USETW(req.wIndex, sc->sc_iface_number);
479 USETW(req.wLength, 0);
480
481 err = usbd_do_request(sc->sc_udev, &req, 0);
482 if (err)
483 return (EIO);
484
485 return (0);
486 }
487
488 struct pl2303x_init {
489 uint8_t req_type;
490 uint8_t request;
491 uint16_t value;
492 uint16_t index;
493 uint16_t length;
494 };
495
496 static const struct pl2303x_init pl2303x[] = {
497 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
498 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 },
499 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
500 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
501 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
502 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 },
503 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
504 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
505 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 },
506 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 },
507 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 }
508 };
509 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
510
511 static usbd_status
512 uplcom_pl2303x_init(struct uplcom_softc *sc)
513 {
514 usb_device_request_t req;
515 usbd_status err;
516 int i;
517
518 for (i = 0; i < N_PL2302X_INIT; i++) {
519 req.bmRequestType = pl2303x[i].req_type;
520 req.bRequest = pl2303x[i].request;
521 USETW(req.wValue, pl2303x[i].value);
522 USETW(req.wIndex, pl2303x[i].index);
523 USETW(req.wLength, pl2303x[i].length);
524
525 err = usbd_do_request(sc->sc_udev, &req, 0);
526 if (err) {
527 aprint_error_dev(sc->sc_dev,
528 "uplcom_pl2303x_init failed: %s\n",
529 usbd_errstr(err));
530 return (EIO);
531 }
532 }
533
534 return (0);
535 }
536
537 void
538 uplcom_set_line_state(struct uplcom_softc *sc)
539 {
540 usb_device_request_t req;
541 int ls;
542
543 /* make sure we have initialized state for sc_dtr and sc_rts */
544 if (sc->sc_dtr == -1)
545 sc->sc_dtr = 0;
546 if (sc->sc_rts == -1)
547 sc->sc_rts = 0;
548
549 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
550 (sc->sc_rts ? UCDC_LINE_RTS : 0);
551
552 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
553 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
554 USETW(req.wValue, ls);
555 USETW(req.wIndex, sc->sc_iface_number);
556 USETW(req.wLength, 0);
557
558 (void)usbd_do_request(sc->sc_udev, &req, 0);
559 }
560
561 void
562 uplcom_set(void *addr, int portno, int reg, int onoff)
563 {
564 struct uplcom_softc *sc = addr;
565
566 switch (reg) {
567 case UCOM_SET_DTR:
568 uplcom_dtr(sc, onoff);
569 break;
570 case UCOM_SET_RTS:
571 uplcom_rts(sc, onoff);
572 break;
573 case UCOM_SET_BREAK:
574 uplcom_break(sc, onoff);
575 break;
576 default:
577 break;
578 }
579 }
580
581 void
582 uplcom_dtr(struct uplcom_softc *sc, int onoff)
583 {
584
585 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
586
587 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
588 return;
589
590 sc->sc_dtr = !!onoff;
591
592 uplcom_set_line_state(sc);
593 }
594
595 void
596 uplcom_rts(struct uplcom_softc *sc, int onoff)
597 {
598 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
599
600 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
601 return;
602
603 sc->sc_rts = !!onoff;
604
605 uplcom_set_line_state(sc);
606 }
607
608 void
609 uplcom_break(struct uplcom_softc *sc, int onoff)
610 {
611 usb_device_request_t req;
612
613 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
614
615 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
616 req.bRequest = UCDC_SEND_BREAK;
617 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
618 USETW(req.wIndex, sc->sc_iface_number);
619 USETW(req.wLength, 0);
620
621 (void)usbd_do_request(sc->sc_udev, &req, 0);
622 }
623
624 usbd_status
625 uplcom_set_crtscts(struct uplcom_softc *sc)
626 {
627 usb_device_request_t req;
628 usbd_status err;
629
630 DPRINTF(("uplcom_set_crtscts: on\n"));
631
632 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
633 req.bRequest = UPLCOM_SET_REQUEST;
634 USETW(req.wValue, 0);
635 if (sc->sc_type == UPLCOM_TYPE_HX)
636 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
637 else
638 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
639 USETW(req.wLength, 0);
640
641 err = usbd_do_request(sc->sc_udev, &req, 0);
642 if (err) {
643 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
644 usbd_errstr(err)));
645 return (err);
646 }
647
648 return (USBD_NORMAL_COMPLETION);
649 }
650
651 usbd_status
652 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
653 {
654 usb_device_request_t req;
655 usbd_status err;
656
657 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
658 UGETDW(state->dwDTERate), state->bCharFormat,
659 state->bParityType, state->bDataBits));
660
661 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
662 DPRINTF(("uplcom_set_line_coding: already set\n"));
663 return (USBD_NORMAL_COMPLETION);
664 }
665
666 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
667 req.bRequest = UCDC_SET_LINE_CODING;
668 USETW(req.wValue, 0);
669 USETW(req.wIndex, sc->sc_iface_number);
670 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
671
672 err = usbd_do_request(sc->sc_udev, &req, state);
673 if (err) {
674 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
675 usbd_errstr(err)));
676 return (err);
677 }
678
679 sc->sc_line_state = *state;
680
681 return (USBD_NORMAL_COMPLETION);
682 }
683
684 int
685 uplcom_param(void *addr, int portno, struct termios *t)
686 {
687 struct uplcom_softc *sc = addr;
688 usbd_status err;
689 usb_cdc_line_state_t ls;
690
691 DPRINTF(("uplcom_param: sc=%p\n", sc));
692
693 USETDW(ls.dwDTERate, t->c_ospeed);
694 if (ISSET(t->c_cflag, CSTOPB))
695 ls.bCharFormat = UCDC_STOP_BIT_2;
696 else
697 ls.bCharFormat = UCDC_STOP_BIT_1;
698 if (ISSET(t->c_cflag, PARENB)) {
699 if (ISSET(t->c_cflag, PARODD))
700 ls.bParityType = UCDC_PARITY_ODD;
701 else
702 ls.bParityType = UCDC_PARITY_EVEN;
703 } else
704 ls.bParityType = UCDC_PARITY_NONE;
705 switch (ISSET(t->c_cflag, CSIZE)) {
706 case CS5:
707 ls.bDataBits = 5;
708 break;
709 case CS6:
710 ls.bDataBits = 6;
711 break;
712 case CS7:
713 ls.bDataBits = 7;
714 break;
715 case CS8:
716 ls.bDataBits = 8;
717 break;
718 }
719
720 err = uplcom_set_line_coding(sc, &ls);
721 if (err) {
722 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
723 return (EIO);
724 }
725
726 if (ISSET(t->c_cflag, CRTSCTS))
727 uplcom_set_crtscts(sc);
728
729 if (sc->sc_rts == -1 || sc->sc_dtr == -1)
730 uplcom_set_line_state(sc);
731
732 if (err) {
733 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
734 return (EIO);
735 }
736
737 return (0);
738 }
739
740 Static usbd_status
741 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
742 {
743 usb_device_request_t req;
744 usbd_status err;
745
746 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
747 req.bRequest = UPLCOM_SET_REQUEST;
748 USETW(req.wValue, value);
749 USETW(req.wIndex, index);
750 USETW(req.wLength, 0);
751
752 err = usbd_do_request(dev, &req, NULL);
753
754 if (err) {
755 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
756 usbd_errstr(err), err));
757 }
758
759 return err;
760 }
761
762 int
763 uplcom_open(void *addr, int portno)
764 {
765 struct uplcom_softc *sc = addr;
766 usbd_status err;
767
768 if (sc->sc_dying)
769 return (EIO);
770
771 DPRINTF(("uplcom_open: sc=%p\n", sc));
772
773 /* Some unknown device frobbing. */
774 if (sc->sc_type == UPLCOM_TYPE_HX)
775 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
776 else
777 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
778
779 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
780 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
781 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
782 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
783 sc->sc_intr_buf, sc->sc_isize,
784 uplcom_intr, USBD_DEFAULT_INTERVAL);
785 if (err) {
786 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
787 USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
788 return (EIO);
789 }
790 }
791
792 if (sc->sc_type == UPLCOM_TYPE_HX)
793 return (uplcom_pl2303x_init(sc));
794
795 return (0);
796 }
797
798 void
799 uplcom_close(void *addr, int portno)
800 {
801 struct uplcom_softc *sc = addr;
802 int err;
803
804 if (sc->sc_dying)
805 return;
806
807 DPRINTF(("uplcom_close: close\n"));
808
809 if (sc->sc_intr_pipe != NULL) {
810 err = usbd_abort_pipe(sc->sc_intr_pipe);
811 if (err)
812 printf("%s: abort interrupt pipe failed: %s\n",
813 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
814 err = usbd_close_pipe(sc->sc_intr_pipe);
815 if (err)
816 printf("%s: close interrupt pipe failed: %s\n",
817 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
818 free(sc->sc_intr_buf, M_USBDEV);
819 sc->sc_intr_pipe = NULL;
820 }
821 }
822
823 void
824 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
825 usbd_status status)
826 {
827 struct uplcom_softc *sc = priv;
828 u_char *buf = sc->sc_intr_buf;
829 u_char pstatus;
830
831 if (sc->sc_dying)
832 return;
833
834 if (status != USBD_NORMAL_COMPLETION) {
835 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
836 return;
837
838 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
839 usbd_errstr(status)));
840 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
841 return;
842 }
843
844 DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
845
846 sc->sc_lsr = sc->sc_msr = 0;
847 pstatus = buf[8];
848 if (ISSET(pstatus, RSAQ_STATUS_DSR))
849 sc->sc_msr |= UMSR_DSR;
850 if (ISSET(pstatus, RSAQ_STATUS_DCD))
851 sc->sc_msr |= UMSR_DCD;
852 ucom_status_change(device_private(sc->sc_subdev));
853 }
854
855 void
856 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
857 {
858 struct uplcom_softc *sc = addr;
859
860 DPRINTF(("uplcom_get_status:\n"));
861
862 if (lsr != NULL)
863 *lsr = sc->sc_lsr;
864 if (msr != NULL)
865 *msr = sc->sc_msr;
866 }
867
868 #if TODO
869 int
870 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
871 usb_proc_ptr p)
872 {
873 struct uplcom_softc *sc = addr;
874 int error = 0;
875
876 if (sc->sc_dying)
877 return (EIO);
878
879 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
880
881 switch (cmd) {
882 case TIOCNOTTY:
883 case TIOCMGET:
884 case TIOCMSET:
885 case USB_GET_CM_OVER_DATA:
886 case USB_SET_CM_OVER_DATA:
887 break;
888
889 default:
890 DPRINTF(("uplcom_ioctl: unknown\n"));
891 error = ENOTTY;
892 break;
893 }
894
895 return (error);
896 }
897 #endif
898