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