uplcom.c revision 1.68 1 /* $NetBSD: uplcom.c,v 1.68 2009/09/23 19:07:19 plunky 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.68 2009/09/23 19:07:19 plunky 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 aprint_naive("\n");
231 aprint_normal("\n");
232
233 devinfop = usbd_devinfo_alloc(dev, 0);
234 aprint_normal_dev(self, "%s\n", devinfop);
235 usbd_devinfo_free(devinfop);
236
237 sc->sc_udev = dev;
238
239 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
240
241 /* initialize endpoints */
242 uca.bulkin = uca.bulkout = -1;
243 sc->sc_intr_number = -1;
244 sc->sc_intr_pipe = NULL;
245
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;
253 }
254
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;
260
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"));
267 }
268 #endif
269
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;
277 }
278
279 /* get the config descriptor */
280 cdesc = usbd_get_config_descriptor(sc->sc_udev);
281
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;
287 }
288
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;
297 }
298
299 /* Find the interrupt endpoints */
300
301 id = usbd_get_interface_descriptor(sc->sc_iface);
302 sc->sc_iface_number = id->bInterfaceNumber;
303
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;
311 }
312
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);
317 }
318 }
319
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;
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->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;
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 aprint_error_dev(self,
361 "no endpoint descriptor for %d\n", i);
362 sc->sc_dying = 1;
363 USB_ATTACH_ERROR_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 aprint_error_dev(self, "Could not find data bulk in\n");
377 sc->sc_dying = 1;
378 USB_ATTACH_ERROR_RETURN;
379 }
380
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;
385 }
386
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;
399
400 err = uplcom_reset(sc);
401
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;
406 }
407
408 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
409 USBDEV(sc->sc_dev));
410
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);
415
416 USB_ATTACH_SUCCESS_RETURN;
417 }
418
419 void
420 uplcom_childdet(device_t self, device_t child)
421 {
422 struct uplcom_softc *sc = device_private(self);
423
424 KASSERT(sc->sc_subdev == child);
425 sc->sc_subdev = NULL;
426 }
427
428 USB_DETACH(uplcom)
429 {
430 USB_DETACH_START(uplcom, sc);
431 int rv = 0;
432
433 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
434
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;
440 }
441
442 sc->sc_dying = 1;
443 if (sc->sc_subdev != NULL)
444 rv = config_detach(sc->sc_subdev, flags);
445
446 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
447 USBDEV(sc->sc_dev));
448
449 return (rv);
450 }
451
452 int
453 uplcom_activate(device_t self, enum devact act)
454 {
455 struct uplcom_softc *sc = device_private(self);
456 int rv = 0;
457
458 switch (act) {
459 case DVACT_ACTIVATE:
460 return (EOPNOTSUPP);
461
462 case DVACT_DEACTIVATE:
463 if (sc->sc_subdev != NULL)
464 rv = config_deactivate(sc->sc_subdev);
465 sc->sc_dying = 1;
466 break;
467 }
468 return (rv);
469 }
470
471 usbd_status
472 uplcom_reset(struct uplcom_softc *sc)
473 {
474 usb_device_request_t req;
475 usbd_status err;
476
477 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
478 req.bRequest = UPLCOM_SET_REQUEST;
479 USETW(req.wValue, 0);
480 USETW(req.wIndex, sc->sc_iface_number);
481 USETW(req.wLength, 0);
482
483 err = usbd_do_request(sc->sc_udev, &req, 0);
484 if (err)
485 return (EIO);
486
487 return (0);
488 }
489
490 struct pl2303x_init {
491 uint8_t req_type;
492 uint8_t request;
493 uint16_t value;
494 uint16_t index;
495 uint16_t length;
496 };
497
498 static const struct pl2303x_init pl2303x[] = {
499 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
500 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 },
501 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
502 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
503 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
504 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 },
505 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
506 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
507 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 },
508 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 },
509 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 }
510 };
511 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
512
513 static usbd_status
514 uplcom_pl2303x_init(struct uplcom_softc *sc)
515 {
516 usb_device_request_t req;
517 usbd_status err;
518 int i;
519
520 for (i = 0; i < N_PL2302X_INIT; i++) {
521 req.bmRequestType = pl2303x[i].req_type;
522 req.bRequest = pl2303x[i].request;
523 USETW(req.wValue, pl2303x[i].value);
524 USETW(req.wIndex, pl2303x[i].index);
525 USETW(req.wLength, pl2303x[i].length);
526
527 err = usbd_do_request(sc->sc_udev, &req, 0);
528 if (err) {
529 aprint_error_dev(sc->sc_dev,
530 "uplcom_pl2303x_init failed: %s\n",
531 usbd_errstr(err));
532 return (EIO);
533 }
534 }
535
536 return (0);
537 }
538
539 void
540 uplcom_set_line_state(struct uplcom_softc *sc)
541 {
542 usb_device_request_t req;
543 int ls;
544
545 /* make sure we have initialized state for sc_dtr and sc_rts */
546 if (sc->sc_dtr == -1)
547 sc->sc_dtr = 0;
548 if (sc->sc_rts == -1)
549 sc->sc_rts = 0;
550
551 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
552 (sc->sc_rts ? UCDC_LINE_RTS : 0);
553
554 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
555 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
556 USETW(req.wValue, ls);
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 void
564 uplcom_set(void *addr, int portno, int reg, int onoff)
565 {
566 struct uplcom_softc *sc = addr;
567
568 switch (reg) {
569 case UCOM_SET_DTR:
570 uplcom_dtr(sc, onoff);
571 break;
572 case UCOM_SET_RTS:
573 uplcom_rts(sc, onoff);
574 break;
575 case UCOM_SET_BREAK:
576 uplcom_break(sc, onoff);
577 break;
578 default:
579 break;
580 }
581 }
582
583 void
584 uplcom_dtr(struct uplcom_softc *sc, int onoff)
585 {
586
587 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
588
589 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
590 return;
591
592 sc->sc_dtr = !!onoff;
593
594 uplcom_set_line_state(sc);
595 }
596
597 void
598 uplcom_rts(struct uplcom_softc *sc, int onoff)
599 {
600 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
601
602 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
603 return;
604
605 sc->sc_rts = !!onoff;
606
607 uplcom_set_line_state(sc);
608 }
609
610 void
611 uplcom_break(struct uplcom_softc *sc, int onoff)
612 {
613 usb_device_request_t req;
614
615 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
616
617 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
618 req.bRequest = UCDC_SEND_BREAK;
619 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
620 USETW(req.wIndex, sc->sc_iface_number);
621 USETW(req.wLength, 0);
622
623 (void)usbd_do_request(sc->sc_udev, &req, 0);
624 }
625
626 usbd_status
627 uplcom_set_crtscts(struct uplcom_softc *sc)
628 {
629 usb_device_request_t req;
630 usbd_status err;
631
632 DPRINTF(("uplcom_set_crtscts: on\n"));
633
634 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
635 req.bRequest = UPLCOM_SET_REQUEST;
636 USETW(req.wValue, 0);
637 if (sc->sc_type == UPLCOM_TYPE_HX)
638 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
639 else
640 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
641 USETW(req.wLength, 0);
642
643 err = usbd_do_request(sc->sc_udev, &req, 0);
644 if (err) {
645 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
646 usbd_errstr(err)));
647 return (err);
648 }
649
650 return (USBD_NORMAL_COMPLETION);
651 }
652
653 usbd_status
654 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
655 {
656 usb_device_request_t req;
657 usbd_status err;
658
659 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
660 UGETDW(state->dwDTERate), state->bCharFormat,
661 state->bParityType, state->bDataBits));
662
663 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
664 DPRINTF(("uplcom_set_line_coding: already set\n"));
665 return (USBD_NORMAL_COMPLETION);
666 }
667
668 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
669 req.bRequest = UCDC_SET_LINE_CODING;
670 USETW(req.wValue, 0);
671 USETW(req.wIndex, sc->sc_iface_number);
672 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
673
674 err = usbd_do_request(sc->sc_udev, &req, state);
675 if (err) {
676 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
677 usbd_errstr(err)));
678 return (err);
679 }
680
681 sc->sc_line_state = *state;
682
683 return (USBD_NORMAL_COMPLETION);
684 }
685
686 int
687 uplcom_param(void *addr, int portno, struct termios *t)
688 {
689 struct uplcom_softc *sc = addr;
690 usbd_status err;
691 usb_cdc_line_state_t ls;
692
693 DPRINTF(("uplcom_param: sc=%p\n", sc));
694
695 USETDW(ls.dwDTERate, t->c_ospeed);
696 if (ISSET(t->c_cflag, CSTOPB))
697 ls.bCharFormat = UCDC_STOP_BIT_2;
698 else
699 ls.bCharFormat = UCDC_STOP_BIT_1;
700 if (ISSET(t->c_cflag, PARENB)) {
701 if (ISSET(t->c_cflag, PARODD))
702 ls.bParityType = UCDC_PARITY_ODD;
703 else
704 ls.bParityType = UCDC_PARITY_EVEN;
705 } else
706 ls.bParityType = UCDC_PARITY_NONE;
707 switch (ISSET(t->c_cflag, CSIZE)) {
708 case CS5:
709 ls.bDataBits = 5;
710 break;
711 case CS6:
712 ls.bDataBits = 6;
713 break;
714 case CS7:
715 ls.bDataBits = 7;
716 break;
717 case CS8:
718 ls.bDataBits = 8;
719 break;
720 }
721
722 err = uplcom_set_line_coding(sc, &ls);
723 if (err) {
724 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
725 return (EIO);
726 }
727
728 if (ISSET(t->c_cflag, CRTSCTS))
729 uplcom_set_crtscts(sc);
730
731 if (sc->sc_rts == -1 || sc->sc_dtr == -1)
732 uplcom_set_line_state(sc);
733
734 if (err) {
735 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
736 return (EIO);
737 }
738
739 return (0);
740 }
741
742 Static usbd_status
743 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
744 {
745 usb_device_request_t req;
746 usbd_status err;
747
748 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
749 req.bRequest = UPLCOM_SET_REQUEST;
750 USETW(req.wValue, value);
751 USETW(req.wIndex, index);
752 USETW(req.wLength, 0);
753
754 err = usbd_do_request(dev, &req, NULL);
755
756 if (err) {
757 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
758 usbd_errstr(err), err));
759 }
760
761 return err;
762 }
763
764 int
765 uplcom_open(void *addr, int portno)
766 {
767 struct uplcom_softc *sc = addr;
768 usbd_status err;
769
770 if (sc->sc_dying)
771 return (EIO);
772
773 DPRINTF(("uplcom_open: sc=%p\n", sc));
774
775 /* Some unknown device frobbing. */
776 if (sc->sc_type == UPLCOM_TYPE_HX)
777 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
778 else
779 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
780
781 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
782 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
783 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
784 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
785 sc->sc_intr_buf, sc->sc_isize,
786 uplcom_intr, USBD_DEFAULT_INTERVAL);
787 if (err) {
788 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
789 USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
790 return (EIO);
791 }
792 }
793
794 if (sc->sc_type == UPLCOM_TYPE_HX)
795 return (uplcom_pl2303x_init(sc));
796
797 return (0);
798 }
799
800 void
801 uplcom_close(void *addr, int portno)
802 {
803 struct uplcom_softc *sc = addr;
804 int err;
805
806 if (sc->sc_dying)
807 return;
808
809 DPRINTF(("uplcom_close: close\n"));
810
811 if (sc->sc_intr_pipe != NULL) {
812 err = usbd_abort_pipe(sc->sc_intr_pipe);
813 if (err)
814 printf("%s: abort interrupt pipe failed: %s\n",
815 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
816 err = usbd_close_pipe(sc->sc_intr_pipe);
817 if (err)
818 printf("%s: close interrupt pipe failed: %s\n",
819 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
820 free(sc->sc_intr_buf, M_USBDEV);
821 sc->sc_intr_pipe = NULL;
822 }
823 }
824
825 void
826 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
827 usbd_status status)
828 {
829 struct uplcom_softc *sc = priv;
830 u_char *buf = sc->sc_intr_buf;
831 u_char pstatus;
832
833 if (sc->sc_dying)
834 return;
835
836 if (status != USBD_NORMAL_COMPLETION) {
837 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
838 return;
839
840 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
841 usbd_errstr(status)));
842 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
843 return;
844 }
845
846 DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
847
848 sc->sc_lsr = sc->sc_msr = 0;
849 pstatus = buf[8];
850 if (ISSET(pstatus, RSAQ_STATUS_DSR))
851 sc->sc_msr |= UMSR_DSR;
852 if (ISSET(pstatus, RSAQ_STATUS_DCD))
853 sc->sc_msr |= UMSR_DCD;
854 ucom_status_change(device_private(sc->sc_subdev));
855 }
856
857 void
858 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
859 {
860 struct uplcom_softc *sc = addr;
861
862 DPRINTF(("uplcom_get_status:\n"));
863
864 if (lsr != NULL)
865 *lsr = sc->sc_lsr;
866 if (msr != NULL)
867 *msr = sc->sc_msr;
868 }
869
870 #if TODO
871 int
872 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
873 usb_proc_ptr p)
874 {
875 struct uplcom_softc *sc = addr;
876 int error = 0;
877
878 if (sc->sc_dying)
879 return (EIO);
880
881 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
882
883 switch (cmd) {
884 case TIOCNOTTY:
885 case TIOCMGET:
886 case TIOCMSET:
887 case USB_GET_CM_OVER_DATA:
888 case USB_SET_CM_OVER_DATA:
889 break;
890
891 default:
892 DPRINTF(("uplcom_ioctl: unknown\n"));
893 error = ENOTTY;
894 break;
895 }
896
897 return (error);
898 }
899 #endif
900