uplcom.c revision 1.65 1 /* $NetBSD: uplcom.c,v 1.65 2008/05/24 16:40:58 cube 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.65 2008/05/24 16:40:58 cube 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,
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 uplcom_type {
150 struct usb_devno uplcom_dev;
151 int32_t release;
152 enum pl2303_type chiptype;
153 } uplcom_devs[] = {
154 /* I/O DATA USB-RSAQ2 */
155 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
156 -1, UPLCOM_TYPE_0 },
157 /* I/O DATA USB-RSAQ3 */
158 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
159 -1, UPLCOM_TYPE_HX },
160 /* I/O DATA USB-RSAQ */
161 { { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
162 -1, UPLCOM_TYPE_0 },
163 /* I/O DATA USB-RSAQ5 */
164 { { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
165 -1, UPLCOM_TYPE_HX },
166 /* PLANEX USB-RS232 URS-03 */
167 { { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
168 -1, UPLCOM_TYPE_0 },
169 /* TrendNet TU-S9 */
170 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
171 0x400, UPLCOM_TYPE_HX },
172 /* ST Lab USB-SERIAL-4 */
173 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
174 0x300, UPLCOM_TYPE_HX },
175 /* IOGEAR/ATEN UC-232A */
176 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
177 -1, UPLCOM_TYPE_0 },
178 /* SMART Technologies USB to serial */
179 { { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
180 -1, UPLCOM_TYPE_HX },
181 /* IOGEAR/ATENTRIPPLITE */
182 { { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
183 0x300, UPLCOM_TYPE_HX },
184 { { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
185 -1, UPLCOM_TYPE_0 },
186 /* ELECOM UC-SGT */
187 { { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
188 -1, UPLCOM_TYPE_0 },
189 /* ELECOM UC-SGT0 */
190 { { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
191 -1, UPLCOM_TYPE_0 },
192 /* Panasonic 50" Touch Panel */
193 { { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
194 -1, UPLCOM_TYPE_0 },
195 /* RATOC REX-USB60 */
196 { { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
197 -1, UPLCOM_TYPE_0 },
198 /* TDK USB-PHS Adapter UHA6400 */
199 { { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
200 -1, UPLCOM_TYPE_0 },
201 /* TDK USB-PDC Adapter UPA9664 */
202 { { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
203 -1, UPLCOM_TYPE_0 },
204 /* Sony Ericsson USB Cable */
205 { { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
206 -1, UPLCOM_TYPE_0 },
207 /* SOURCENEXT KeikaiDenwa 8 */
208 { { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
209 -1, UPLCOM_TYPE_0 },
210 /* SOURCENEXT KeikaiDenwa 8 with charger */
211 { { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
212 -1, UPLCOM_TYPE_0 },
213 /* HAL Corporation Crossam2+USB */
214 { { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
215 -1, UPLCOM_TYPE_0 },
216 /* Sitecom USB to serial cable */
217 { { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
218 -1, UPLCOM_TYPE_0 },
219 /* Pharos USB GPS - Microsoft version */
220 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
221 -1, UPLCOM_TYPE_0 },
222 /* Willcom WS002IN (DD) */
223 { { USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN },
224 -1, UPLCOM_TYPE_HX },
225 };
226 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
227
228 int uplcom_match(device_t, cfdata_t, void *);
229 void uplcom_attach(device_t, device_t, void *);
230 void uplcom_childdet(device_t, device_t);
231 int uplcom_detach(device_t, int);
232 int uplcom_activate(device_t, enum devact);
233 extern struct cfdriver uplcom_cd;
234 CFATTACH_DECL2_NEW(uplcom, sizeof(struct uplcom_softc), uplcom_match,
235 uplcom_attach, uplcom_detach, uplcom_activate, NULL, uplcom_childdet);
236
237 USB_MATCH(uplcom)
238 {
239 USB_MATCH_START(uplcom, uaa);
240
241 return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
242 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
243 }
244
245 USB_ATTACH(uplcom)
246 {
247 USB_ATTACH_START(uplcom, sc, uaa);
248 usbd_device_handle dev = uaa->device;
249 usb_config_descriptor_t *cdesc;
250 usb_interface_descriptor_t *id;
251 usb_endpoint_descriptor_t *ed;
252 char *devinfop;
253 const char *devname = device_xname(self);
254 usbd_status err;
255 int i;
256 struct ucom_attach_args uca;
257
258 sc->sc_dev = self;
259
260 devinfop = usbd_devinfo_alloc(dev, 0);
261 USB_ATTACH_SETUP;
262 aprint_normal_dev(self, "%s\n", devinfop);
263 usbd_devinfo_free(devinfop);
264
265 sc->sc_udev = dev;
266
267 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
268
269 /* initialize endpoints */
270 uca.bulkin = uca.bulkout = -1;
271 sc->sc_intr_number = -1;
272 sc->sc_intr_pipe = NULL;
273
274 /* Move the device into the configured state. */
275 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
276 if (err) {
277 aprint_error("\n%s: failed to set configuration, err=%s\n",
278 devname, usbd_errstr(err));
279 sc->sc_dying = 1;
280 USB_ATTACH_ERROR_RETURN;
281 }
282
283 /* determine chip type */
284 for (i = 0; uplcom_devs[i].uplcom_dev.ud_vendor != 0; i++) {
285 if (uplcom_devs[i].uplcom_dev.ud_vendor == uaa->vendor &&
286 uplcom_devs[i].uplcom_dev.ud_product == uaa->product &&
287 (uplcom_devs[i].release == uaa->release ||
288 uplcom_devs[i].release == -1)) {
289 sc->sc_type = uplcom_devs[i].chiptype;
290 break;
291 }
292 }
293
294 #ifdef USB_DEBUG
295 /* print the chip type */
296 if (sc->sc_type == UPLCOM_TYPE_HX) {
297 DPRINTF(("uplcom_attach: chiptype HX\n"));
298 } else {
299 DPRINTF(("uplcom_attach: chiptype 0\n"));
300 }
301 #endif
302
303 /* Move the device into the configured state. */
304 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
305 if (err) {
306 aprint_error_dev(self, "failed to set configuration: %s\n",
307 usbd_errstr(err));
308 sc->sc_dying = 1;
309 USB_ATTACH_ERROR_RETURN;
310 }
311
312 /* get the config descriptor */
313 cdesc = usbd_get_config_descriptor(sc->sc_udev);
314
315 if (cdesc == NULL) {
316 aprint_error_dev(self,
317 "failed to get configuration descriptor\n");
318 sc->sc_dying = 1;
319 USB_ATTACH_ERROR_RETURN;
320 }
321
322 /* get the (first/common) interface */
323 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
324 &sc->sc_iface);
325 if (err) {
326 aprint_error("\n%s: failed to get interface, err=%s\n",
327 devname, usbd_errstr(err));
328 sc->sc_dying = 1;
329 USB_ATTACH_ERROR_RETURN;
330 }
331
332 /* Find the interrupt endpoints */
333
334 id = usbd_get_interface_descriptor(sc->sc_iface);
335 sc->sc_iface_number = id->bInterfaceNumber;
336
337 for (i = 0; i < id->bNumEndpoints; i++) {
338 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
339 if (ed == NULL) {
340 aprint_error_dev(self,
341 "no endpoint descriptor for %d\n", i);
342 sc->sc_dying = 1;
343 USB_ATTACH_ERROR_RETURN;
344 }
345
346 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
347 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
348 sc->sc_intr_number = ed->bEndpointAddress;
349 sc->sc_isize = UGETW(ed->wMaxPacketSize);
350 }
351 }
352
353 if (sc->sc_intr_number== -1) {
354 aprint_error_dev(self, "Could not find interrupt in\n");
355 sc->sc_dying = 1;
356 USB_ATTACH_ERROR_RETURN;
357 }
358
359 /* keep interface for interrupt */
360 sc->sc_intr_iface = sc->sc_iface;
361
362 /*
363 * USB-RSAQ1 has two interface
364 *
365 * USB-RSAQ1 | USB-RSAQ2
366 * -----------------+-----------------
367 * Interface 0 |Interface 0
368 * Interrupt(0x81) | Interrupt(0x81)
369 * -----------------+ BulkIN(0x02)
370 * Interface 1 | BulkOUT(0x83)
371 * BulkIN(0x02) |
372 * BulkOUT(0x83) |
373 */
374 if (cdesc->bNumInterface == 2) {
375 err = usbd_device2interface_handle(dev,
376 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
377 if (err) {
378 aprint_error("\n%s: failed to get second interface, err=%s\n",
379 devname, usbd_errstr(err));
380 sc->sc_dying = 1;
381 USB_ATTACH_ERROR_RETURN;
382 }
383 }
384
385 /* Find the bulk{in,out} endpoints */
386
387 id = usbd_get_interface_descriptor(sc->sc_iface);
388 sc->sc_iface_number = id->bInterfaceNumber;
389
390 for (i = 0; i < id->bNumEndpoints; i++) {
391 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
392 if (ed == NULL) {
393 aprint_error_dev(self,
394 "no endpoint descriptor for %d\n", i);
395 sc->sc_dying = 1;
396 USB_ATTACH_ERROR_RETURN;
397 }
398
399 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
400 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
401 uca.bulkin = ed->bEndpointAddress;
402 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
403 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
404 uca.bulkout = ed->bEndpointAddress;
405 }
406 }
407
408 if (uca.bulkin == -1) {
409 aprint_error_dev(self, "Could not find data bulk in\n");
410 sc->sc_dying = 1;
411 USB_ATTACH_ERROR_RETURN;
412 }
413
414 if (uca.bulkout == -1) {
415 aprint_error_dev(self, "Could not find data bulk out\n");
416 sc->sc_dying = 1;
417 USB_ATTACH_ERROR_RETURN;
418 }
419
420 sc->sc_dtr = sc->sc_rts = -1;
421 uca.portno = UCOM_UNK_PORTNO;
422 /* bulkin, bulkout set above */
423 uca.ibufsize = UPLCOMIBUFSIZE;
424 uca.obufsize = UPLCOMOBUFSIZE;
425 uca.ibufsizepad = UPLCOMIBUFSIZE;
426 uca.opkthdrlen = 0;
427 uca.device = dev;
428 uca.iface = sc->sc_iface;
429 uca.methods = &uplcom_methods;
430 uca.arg = sc;
431 uca.info = NULL;
432
433 err = uplcom_reset(sc);
434
435 if (err) {
436 aprint_error_dev(self, "reset failed, %s\n", usbd_errstr(err));
437 sc->sc_dying = 1;
438 USB_ATTACH_ERROR_RETURN;
439 }
440
441 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
442 USBDEV(sc->sc_dev));
443
444 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
445 uca.bulkin, uca.bulkout, sc->sc_intr_number ));
446 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
447 ucomprint, ucomsubmatch);
448
449 USB_ATTACH_SUCCESS_RETURN;
450 }
451
452 void
453 uplcom_childdet(device_t self, device_t child)
454 {
455 struct uplcom_softc *sc = device_private(self);
456
457 KASSERT(sc->sc_subdev == child);
458 sc->sc_subdev = NULL;
459 }
460
461 USB_DETACH(uplcom)
462 {
463 USB_DETACH_START(uplcom, sc);
464 int rv = 0;
465
466 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
467
468 if (sc->sc_intr_pipe != NULL) {
469 usbd_abort_pipe(sc->sc_intr_pipe);
470 usbd_close_pipe(sc->sc_intr_pipe);
471 free(sc->sc_intr_buf, M_USBDEV);
472 sc->sc_intr_pipe = NULL;
473 }
474
475 sc->sc_dying = 1;
476 if (sc->sc_subdev != NULL)
477 rv = config_detach(sc->sc_subdev, flags);
478
479 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
480 USBDEV(sc->sc_dev));
481
482 return (rv);
483 }
484
485 int
486 uplcom_activate(device_t self, enum devact act)
487 {
488 struct uplcom_softc *sc = device_private(self);
489 int rv = 0;
490
491 switch (act) {
492 case DVACT_ACTIVATE:
493 return (EOPNOTSUPP);
494
495 case DVACT_DEACTIVATE:
496 if (sc->sc_subdev != NULL)
497 rv = config_deactivate(sc->sc_subdev);
498 sc->sc_dying = 1;
499 break;
500 }
501 return (rv);
502 }
503
504 usbd_status
505 uplcom_reset(struct uplcom_softc *sc)
506 {
507 usb_device_request_t req;
508 usbd_status err;
509
510 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
511 req.bRequest = UPLCOM_SET_REQUEST;
512 USETW(req.wValue, 0);
513 USETW(req.wIndex, sc->sc_iface_number);
514 USETW(req.wLength, 0);
515
516 err = usbd_do_request(sc->sc_udev, &req, 0);
517 if (err)
518 return (EIO);
519
520 return (0);
521 }
522
523 struct pl2303x_init {
524 uint8_t req_type;
525 uint8_t request;
526 uint16_t value;
527 uint16_t index;
528 uint16_t length;
529 };
530
531 static const struct pl2303x_init pl2303x[] = {
532 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
533 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 },
534 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
535 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
536 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
537 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 },
538 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
539 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
540 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 },
541 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 },
542 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 }
543 };
544 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
545
546 static usbd_status
547 uplcom_pl2303x_init(struct uplcom_softc *sc)
548 {
549 usb_device_request_t req;
550 usbd_status err;
551 int i;
552
553 for (i = 0; i < N_PL2302X_INIT; i++) {
554 req.bmRequestType = pl2303x[i].req_type;
555 req.bRequest = pl2303x[i].request;
556 USETW(req.wValue, pl2303x[i].value);
557 USETW(req.wIndex, pl2303x[i].index);
558 USETW(req.wLength, pl2303x[i].length);
559
560 err = usbd_do_request(sc->sc_udev, &req, 0);
561 if (err) {
562 aprint_error_dev(sc->sc_dev,
563 "uplcom_pl2303x_init failed: %s\n",
564 usbd_errstr(err));
565 return (EIO);
566 }
567 }
568
569 return (0);
570 }
571
572 void
573 uplcom_set_line_state(struct uplcom_softc *sc)
574 {
575 usb_device_request_t req;
576 int ls;
577
578 /* make sure we have initialized state for sc_dtr and sc_rts */
579 if (sc->sc_dtr == -1)
580 sc->sc_dtr = 0;
581 if (sc->sc_rts == -1)
582 sc->sc_rts = 0;
583
584 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
585 (sc->sc_rts ? UCDC_LINE_RTS : 0);
586
587 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
588 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
589 USETW(req.wValue, ls);
590 USETW(req.wIndex, sc->sc_iface_number);
591 USETW(req.wLength, 0);
592
593 (void)usbd_do_request(sc->sc_udev, &req, 0);
594 }
595
596 void
597 uplcom_set(void *addr, int portno, int reg, int onoff)
598 {
599 struct uplcom_softc *sc = addr;
600
601 switch (reg) {
602 case UCOM_SET_DTR:
603 uplcom_dtr(sc, onoff);
604 break;
605 case UCOM_SET_RTS:
606 uplcom_rts(sc, onoff);
607 break;
608 case UCOM_SET_BREAK:
609 uplcom_break(sc, onoff);
610 break;
611 default:
612 break;
613 }
614 }
615
616 void
617 uplcom_dtr(struct uplcom_softc *sc, int onoff)
618 {
619
620 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
621
622 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
623 return;
624
625 sc->sc_dtr = !!onoff;
626
627 uplcom_set_line_state(sc);
628 }
629
630 void
631 uplcom_rts(struct uplcom_softc *sc, int onoff)
632 {
633 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
634
635 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
636 return;
637
638 sc->sc_rts = !!onoff;
639
640 uplcom_set_line_state(sc);
641 }
642
643 void
644 uplcom_break(struct uplcom_softc *sc, int onoff)
645 {
646 usb_device_request_t req;
647
648 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
649
650 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
651 req.bRequest = UCDC_SEND_BREAK;
652 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
653 USETW(req.wIndex, sc->sc_iface_number);
654 USETW(req.wLength, 0);
655
656 (void)usbd_do_request(sc->sc_udev, &req, 0);
657 }
658
659 usbd_status
660 uplcom_set_crtscts(struct uplcom_softc *sc)
661 {
662 usb_device_request_t req;
663 usbd_status err;
664
665 DPRINTF(("uplcom_set_crtscts: on\n"));
666
667 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
668 req.bRequest = UPLCOM_SET_REQUEST;
669 USETW(req.wValue, 0);
670 if (sc->sc_type == UPLCOM_TYPE_HX)
671 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
672 else
673 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
674 USETW(req.wLength, 0);
675
676 err = usbd_do_request(sc->sc_udev, &req, 0);
677 if (err) {
678 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
679 usbd_errstr(err)));
680 return (err);
681 }
682
683 return (USBD_NORMAL_COMPLETION);
684 }
685
686 usbd_status
687 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
688 {
689 usb_device_request_t req;
690 usbd_status err;
691
692 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
693 UGETDW(state->dwDTERate), state->bCharFormat,
694 state->bParityType, state->bDataBits));
695
696 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
697 DPRINTF(("uplcom_set_line_coding: already set\n"));
698 return (USBD_NORMAL_COMPLETION);
699 }
700
701 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
702 req.bRequest = UCDC_SET_LINE_CODING;
703 USETW(req.wValue, 0);
704 USETW(req.wIndex, sc->sc_iface_number);
705 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
706
707 err = usbd_do_request(sc->sc_udev, &req, state);
708 if (err) {
709 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
710 usbd_errstr(err)));
711 return (err);
712 }
713
714 sc->sc_line_state = *state;
715
716 return (USBD_NORMAL_COMPLETION);
717 }
718
719 int
720 uplcom_param(void *addr, int portno, struct termios *t)
721 {
722 struct uplcom_softc *sc = addr;
723 usbd_status err;
724 usb_cdc_line_state_t ls;
725
726 DPRINTF(("uplcom_param: sc=%p\n", sc));
727
728 USETDW(ls.dwDTERate, t->c_ospeed);
729 if (ISSET(t->c_cflag, CSTOPB))
730 ls.bCharFormat = UCDC_STOP_BIT_2;
731 else
732 ls.bCharFormat = UCDC_STOP_BIT_1;
733 if (ISSET(t->c_cflag, PARENB)) {
734 if (ISSET(t->c_cflag, PARODD))
735 ls.bParityType = UCDC_PARITY_ODD;
736 else
737 ls.bParityType = UCDC_PARITY_EVEN;
738 } else
739 ls.bParityType = UCDC_PARITY_NONE;
740 switch (ISSET(t->c_cflag, CSIZE)) {
741 case CS5:
742 ls.bDataBits = 5;
743 break;
744 case CS6:
745 ls.bDataBits = 6;
746 break;
747 case CS7:
748 ls.bDataBits = 7;
749 break;
750 case CS8:
751 ls.bDataBits = 8;
752 break;
753 }
754
755 err = uplcom_set_line_coding(sc, &ls);
756 if (err) {
757 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
758 return (EIO);
759 }
760
761 if (ISSET(t->c_cflag, CRTSCTS))
762 uplcom_set_crtscts(sc);
763
764 if (sc->sc_rts == -1 || sc->sc_dtr == -1)
765 uplcom_set_line_state(sc);
766
767 if (err) {
768 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
769 return (EIO);
770 }
771
772 return (0);
773 }
774
775 Static usbd_status
776 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
777 {
778 usb_device_request_t req;
779 usbd_status err;
780
781 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
782 req.bRequest = UPLCOM_SET_REQUEST;
783 USETW(req.wValue, value);
784 USETW(req.wIndex, index);
785 USETW(req.wLength, 0);
786
787 err = usbd_do_request(dev, &req, NULL);
788
789 if (err) {
790 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
791 usbd_errstr(err), err));
792 }
793
794 return err;
795 }
796
797 int
798 uplcom_open(void *addr, int portno)
799 {
800 struct uplcom_softc *sc = addr;
801 usbd_status err;
802
803 if (sc->sc_dying)
804 return (EIO);
805
806 DPRINTF(("uplcom_open: sc=%p\n", sc));
807
808 /* Some unknown device frobbing. */
809 if (sc->sc_type == UPLCOM_TYPE_HX)
810 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
811 else
812 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
813
814 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
815 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
816 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
817 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
818 sc->sc_intr_buf, sc->sc_isize,
819 uplcom_intr, USBD_DEFAULT_INTERVAL);
820 if (err) {
821 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
822 USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
823 return (EIO);
824 }
825 }
826
827 if (sc->sc_type == UPLCOM_TYPE_HX)
828 return (uplcom_pl2303x_init(sc));
829
830 return (0);
831 }
832
833 void
834 uplcom_close(void *addr, int portno)
835 {
836 struct uplcom_softc *sc = addr;
837 int err;
838
839 if (sc->sc_dying)
840 return;
841
842 DPRINTF(("uplcom_close: close\n"));
843
844 if (sc->sc_intr_pipe != NULL) {
845 err = usbd_abort_pipe(sc->sc_intr_pipe);
846 if (err)
847 printf("%s: abort interrupt pipe failed: %s\n",
848 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
849 err = usbd_close_pipe(sc->sc_intr_pipe);
850 if (err)
851 printf("%s: close interrupt pipe failed: %s\n",
852 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
853 free(sc->sc_intr_buf, M_USBDEV);
854 sc->sc_intr_pipe = NULL;
855 }
856 }
857
858 void
859 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
860 usbd_status status)
861 {
862 struct uplcom_softc *sc = priv;
863 u_char *buf = sc->sc_intr_buf;
864 u_char pstatus;
865
866 if (sc->sc_dying)
867 return;
868
869 if (status != USBD_NORMAL_COMPLETION) {
870 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
871 return;
872
873 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
874 usbd_errstr(status)));
875 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
876 return;
877 }
878
879 DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
880
881 sc->sc_lsr = sc->sc_msr = 0;
882 pstatus = buf[8];
883 if (ISSET(pstatus, RSAQ_STATUS_DSR))
884 sc->sc_msr |= UMSR_DSR;
885 if (ISSET(pstatus, RSAQ_STATUS_DCD))
886 sc->sc_msr |= UMSR_DCD;
887 ucom_status_change(device_private(sc->sc_subdev));
888 }
889
890 void
891 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
892 {
893 struct uplcom_softc *sc = addr;
894
895 DPRINTF(("uplcom_get_status:\n"));
896
897 if (lsr != NULL)
898 *lsr = sc->sc_lsr;
899 if (msr != NULL)
900 *msr = sc->sc_msr;
901 }
902
903 #if TODO
904 int
905 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
906 usb_proc_ptr p)
907 {
908 struct uplcom_softc *sc = addr;
909 int error = 0;
910
911 if (sc->sc_dying)
912 return (EIO);
913
914 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
915
916 switch (cmd) {
917 case TIOCNOTTY:
918 case TIOCMGET:
919 case TIOCMSET:
920 case USB_GET_CM_OVER_DATA:
921 case USB_SET_CM_OVER_DATA:
922 break;
923
924 default:
925 DPRINTF(("uplcom_ioctl: unknown\n"));
926 error = ENOTTY;
927 break;
928 }
929
930 return (error);
931 }
932 #endif
933