uplcom.c revision 1.59 1 /* $NetBSD: uplcom.c,v 1.59 2008/02/12 22:29:54 mlelstv 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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * General information: http://www.prolific.com.tw/fr_pl2303.htm
40 * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.59 2008/02/12 22:29:54 mlelstv Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/ioctl.h>
51 #include <sys/conf.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/select.h>
55 #include <sys/proc.h>
56 #include <sys/device.h>
57 #include <sys/poll.h>
58
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbcdc.h>
61
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdevs.h>
65 #include <dev/usb/usb_quirks.h>
66
67 #include <dev/usb/ucomvar.h>
68
69 #ifdef UPLCOM_DEBUG
70 #define DPRINTFN(n, x) if (uplcomdebug > (n)) logprintf x
71 int uplcomdebug = 0;
72 #else
73 #define DPRINTFN(n, x)
74 #endif
75 #define DPRINTF(x) DPRINTFN(0, x)
76
77 #define UPLCOM_CONFIG_INDEX 0
78 #define UPLCOM_IFACE_INDEX 0
79 #define UPLCOM_SECOND_IFACE_INDEX 1
80
81 #define UPLCOM_SET_REQUEST 0x01
82 #define UPLCOM_SET_CRTSCTS_0 0x41
83 #define UPLCOM_SET_CRTSCTS_HX 0x61
84 #define RSAQ_STATUS_DSR 0x02
85 #define RSAQ_STATUS_DCD 0x01
86
87 enum pl2303_type {
88 UPLCOM_TYPE_0,
89 UPLCOM_TYPE_HX,
90 };
91
92 struct uplcom_softc {
93 USBBASEDEVICE sc_dev; /* base device */
94 usbd_device_handle sc_udev; /* USB device */
95 usbd_interface_handle sc_iface; /* interface */
96 int sc_iface_number; /* interface number */
97
98 usbd_interface_handle sc_intr_iface; /* interrupt interface */
99 int sc_intr_number; /* interrupt number */
100 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
101 u_char *sc_intr_buf; /* interrupt buffer */
102 int sc_isize;
103
104 usb_cdc_line_state_t sc_line_state; /* current line state */
105 int sc_dtr; /* current DTR state */
106 int sc_rts; /* current RTS state */
107
108 device_ptr_t sc_subdev; /* ucom device */
109
110 u_char sc_dying; /* disconnecting */
111
112 u_char sc_lsr; /* Local status register */
113 u_char sc_msr; /* uplcom status register */
114
115 enum pl2303_type sc_type; /* PL2303 chip type */
116 };
117
118 /*
119 * These are the maximum number of bytes transferred per frame.
120 * The output buffer size cannot be increased due to the size encoding.
121 */
122 #define UPLCOMIBUFSIZE 256
123 #define UPLCOMOBUFSIZE 256
124
125 Static usbd_status uplcom_reset(struct uplcom_softc *);
126 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
127 usb_cdc_line_state_t *state);
128 Static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
129 Static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
130
131 Static void uplcom_set(void *, int, int, int);
132 Static void uplcom_dtr(struct uplcom_softc *, int);
133 Static void uplcom_rts(struct uplcom_softc *, int);
134 Static void uplcom_break(struct uplcom_softc *, int);
135 Static void uplcom_set_line_state(struct uplcom_softc *);
136 Static void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
137 #if TODO
138 Static int uplcom_ioctl(void *, int, u_long, void *, int, usb_proc_ptr );
139 #endif
140 Static int uplcom_param(void *, int, struct termios *);
141 Static int uplcom_open(void *, int);
142 Static void uplcom_close(void *, int);
143 Static usbd_status uplcom_vendor_control_write(usbd_device_handle, u_int16_t, u_int16_t);
144
145 struct ucom_methods uplcom_methods = {
146 uplcom_get_status,
147 uplcom_set,
148 uplcom_param,
149 NULL, /* uplcom_ioctl, TODO */
150 uplcom_open,
151 uplcom_close,
152 NULL,
153 NULL,
154 };
155
156 static const struct uplcom_type {
157 struct usb_devno uplcom_dev;
158 int32_t release;
159 enum pl2303_type chiptype;
160 } uplcom_devs[] = {
161 /* I/O DATA USB-RSAQ2 */
162 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
163 -1, UPLCOM_TYPE_0 },
164 /* I/O DATA USB-RSAQ3 */
165 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
166 -1, UPLCOM_TYPE_HX },
167 /* I/O DATA USB-RSAQ */
168 { { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
169 -1, UPLCOM_TYPE_0 },
170 /* I/O DATA USB-RSAQ5 */
171 { { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
172 -1, UPLCOM_TYPE_HX },
173 /* PLANEX USB-RS232 URS-03 */
174 { { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
175 -1, UPLCOM_TYPE_0 },
176 /* TrendNet TU-S9 */
177 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
178 0x400, UPLCOM_TYPE_HX },
179 /* ST Lab USB-SERIAL-4 */
180 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
181 0x300, UPLCOM_TYPE_HX },
182 /* IOGEAR/ATEN UC-232A */
183 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
184 -1, UPLCOM_TYPE_0 },
185 /* SMART Technologies USB to serial */
186 { { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
187 -1, UPLCOM_TYPE_HX },
188 /* IOGEAR/ATENTRIPPLITE */
189 { { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
190 -1, UPLCOM_TYPE_0 },
191 /* ELECOM UC-SGT */
192 { { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
193 -1, UPLCOM_TYPE_0 },
194 /* ELECOM UC-SGT0 */
195 { { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
196 -1, UPLCOM_TYPE_0 },
197 /* Panasonic 50" Touch Panel */
198 { { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
199 -1, UPLCOM_TYPE_0 },
200 /* RATOC REX-USB60 */
201 { { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
202 -1, UPLCOM_TYPE_0 },
203 /* TDK USB-PHS Adapter UHA6400 */
204 { { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
205 -1, UPLCOM_TYPE_0 },
206 /* TDK USB-PDC Adapter UPA9664 */
207 { { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
208 -1, UPLCOM_TYPE_0 },
209 /* Sony Ericsson USB Cable */
210 { { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
211 -1, UPLCOM_TYPE_0 },
212 /* SOURCENEXT KeikaiDenwa 8 */
213 { { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
214 -1, UPLCOM_TYPE_0 },
215 /* SOURCENEXT KeikaiDenwa 8 with charger */
216 { { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
217 -1, UPLCOM_TYPE_0 },
218 /* HAL Corporation Crossam2+USB */
219 { { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
220 -1, UPLCOM_TYPE_0 },
221 /* Sitecom USB to serial cable */
222 { { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
223 -1, UPLCOM_TYPE_0 },
224 /* Pharos USB GPS - Microsoft version */
225 { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
226 -1, UPLCOM_TYPE_0 },
227 /* Willcom WS002IN (DD) */
228 { { USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN },
229 -1, UPLCOM_TYPE_HX },
230 };
231 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
232
233 USB_DECLARE_DRIVER(uplcom);
234
235 USB_MATCH(uplcom)
236 {
237 USB_MATCH_START(uplcom, uaa);
238
239 return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
240 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
241 }
242
243 USB_ATTACH(uplcom)
244 {
245 USB_ATTACH_START(uplcom, sc, uaa);
246 usbd_device_handle dev = uaa->device;
247 usb_config_descriptor_t *cdesc;
248 usb_interface_descriptor_t *id;
249 usb_endpoint_descriptor_t *ed;
250 char *devinfop;
251 char *devname = USBDEVNAME(sc->sc_dev);
252 usbd_status err;
253 int i;
254 struct ucom_attach_args uca;
255
256 devinfop = usbd_devinfo_alloc(dev, 0);
257 USB_ATTACH_SETUP;
258 printf("%s: %s\n", devname, devinfop);
259 usbd_devinfo_free(devinfop);
260
261 sc->sc_udev = dev;
262
263 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
264
265 /* initialize endpoints */
266 uca.bulkin = uca.bulkout = -1;
267 sc->sc_intr_number = -1;
268 sc->sc_intr_pipe = NULL;
269
270 /* Move the device into the configured state. */
271 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
272 if (err) {
273 printf("\n%s: failed to set configuration, err=%s\n",
274 devname, usbd_errstr(err));
275 sc->sc_dying = 1;
276 USB_ATTACH_ERROR_RETURN;
277 }
278
279 /* determine chip type */
280 for (i = 0; uplcom_devs[i].uplcom_dev.ud_vendor != 0; i++) {
281 if (uplcom_devs[i].uplcom_dev.ud_vendor == uaa->vendor &&
282 uplcom_devs[i].uplcom_dev.ud_product == uaa->product &&
283 (uplcom_devs[i].release == uaa->release ||
284 uplcom_devs[i].release == 1)) {
285 sc->sc_type = uplcom_devs[i].chiptype;
286 break;
287 }
288 }
289
290 #ifdef USB_DEBUG
291 /* print the chip type */
292 if (sc->sc_type == UPLCOM_TYPE_HX) {
293 DPRINTF(("uplcom_attach: chiptype HX\n"));
294 } else {
295 DPRINTF(("uplcom_attach: chiptype 0\n"));
296 }
297 #endif
298
299 /* Move the device into the configured state. */
300 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
301 if (err) {
302 printf("%s: failed to set configuration: %s\n",
303 devname, usbd_errstr(err));
304 sc->sc_dying = 1;
305 USB_ATTACH_ERROR_RETURN;
306 }
307
308 /* get the config descriptor */
309 cdesc = usbd_get_config_descriptor(sc->sc_udev);
310
311 if (cdesc == NULL) {
312 printf("%s: failed to get configuration descriptor\n",
313 USBDEVNAME(sc->sc_dev));
314 sc->sc_dying = 1;
315 USB_ATTACH_ERROR_RETURN;
316 }
317
318 /* get the (first/common) interface */
319 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
320 &sc->sc_iface);
321 if (err) {
322 printf("\n%s: failed to get interface, err=%s\n",
323 devname, usbd_errstr(err));
324 sc->sc_dying = 1;
325 USB_ATTACH_ERROR_RETURN;
326 }
327
328 /* Find the interrupt endpoints */
329
330 id = usbd_get_interface_descriptor(sc->sc_iface);
331 sc->sc_iface_number = id->bInterfaceNumber;
332
333 for (i = 0; i < id->bNumEndpoints; i++) {
334 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
335 if (ed == NULL) {
336 printf("%s: no endpoint descriptor for %d\n",
337 USBDEVNAME(sc->sc_dev), i);
338 sc->sc_dying = 1;
339 USB_ATTACH_ERROR_RETURN;
340 }
341
342 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
343 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
344 sc->sc_intr_number = ed->bEndpointAddress;
345 sc->sc_isize = UGETW(ed->wMaxPacketSize);
346 }
347 }
348
349 if (sc->sc_intr_number== -1) {
350 printf("%s: Could not find interrupt in\n",
351 USBDEVNAME(sc->sc_dev));
352 sc->sc_dying = 1;
353 USB_ATTACH_ERROR_RETURN;
354 }
355
356 /* keep interface for interrupt */
357 sc->sc_intr_iface = sc->sc_iface;
358
359 /*
360 * USB-RSAQ1 has two interface
361 *
362 * USB-RSAQ1 | USB-RSAQ2
363 * -----------------+-----------------
364 * Interface 0 |Interface 0
365 * Interrupt(0x81) | Interrupt(0x81)
366 * -----------------+ BulkIN(0x02)
367 * Interface 1 | BulkOUT(0x83)
368 * BulkIN(0x02) |
369 * BulkOUT(0x83) |
370 */
371 if (cdesc->bNumInterface == 2) {
372 err = usbd_device2interface_handle(dev,
373 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
374 if (err) {
375 printf("\n%s: failed to get second interface, err=%s\n",
376 devname, usbd_errstr(err));
377 sc->sc_dying = 1;
378 USB_ATTACH_ERROR_RETURN;
379 }
380 }
381
382 /* Find the bulk{in,out} endpoints */
383
384 id = usbd_get_interface_descriptor(sc->sc_iface);
385 sc->sc_iface_number = id->bInterfaceNumber;
386
387 for (i = 0; i < id->bNumEndpoints; i++) {
388 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
389 if (ed == NULL) {
390 printf("%s: no endpoint descriptor for %d\n",
391 USBDEVNAME(sc->sc_dev), i);
392 sc->sc_dying = 1;
393 USB_ATTACH_ERROR_RETURN;
394 }
395
396 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
397 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
398 uca.bulkin = ed->bEndpointAddress;
399 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
400 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
401 uca.bulkout = ed->bEndpointAddress;
402 }
403 }
404
405 if (uca.bulkin == -1) {
406 printf("%s: Could not find data bulk in\n",
407 USBDEVNAME(sc->sc_dev));
408 sc->sc_dying = 1;
409 USB_ATTACH_ERROR_RETURN;
410 }
411
412 if (uca.bulkout == -1) {
413 printf("%s: Could not find data bulk out\n",
414 USBDEVNAME(sc->sc_dev));
415 sc->sc_dying = 1;
416 USB_ATTACH_ERROR_RETURN;
417 }
418
419 sc->sc_dtr = sc->sc_rts = -1;
420 uca.portno = UCOM_UNK_PORTNO;
421 /* bulkin, bulkout set above */
422 uca.ibufsize = UPLCOMIBUFSIZE;
423 uca.obufsize = UPLCOMOBUFSIZE;
424 uca.ibufsizepad = UPLCOMIBUFSIZE;
425 uca.opkthdrlen = 0;
426 uca.device = dev;
427 uca.iface = sc->sc_iface;
428 uca.methods = &uplcom_methods;
429 uca.arg = sc;
430 uca.info = NULL;
431
432 err = uplcom_reset(sc);
433
434 if (err) {
435 printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
436 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 USB_DETACH(uplcom)
453 {
454 USB_DETACH_START(uplcom, sc);
455 int rv = 0;
456
457 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
458
459 if (sc->sc_intr_pipe != NULL) {
460 usbd_abort_pipe(sc->sc_intr_pipe);
461 usbd_close_pipe(sc->sc_intr_pipe);
462 free(sc->sc_intr_buf, M_USBDEV);
463 sc->sc_intr_pipe = NULL;
464 }
465
466 sc->sc_dying = 1;
467 if (sc->sc_subdev != NULL) {
468 rv = config_detach(sc->sc_subdev, flags);
469 sc->sc_subdev = NULL;
470 }
471
472 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
473 USBDEV(sc->sc_dev));
474
475 return (rv);
476 }
477
478 int
479 uplcom_activate(device_ptr_t self, enum devact act)
480 {
481 struct uplcom_softc *sc = (struct uplcom_softc *)self;
482 int rv = 0;
483
484 switch (act) {
485 case DVACT_ACTIVATE:
486 return (EOPNOTSUPP);
487
488 case DVACT_DEACTIVATE:
489 if (sc->sc_subdev != NULL)
490 rv = config_deactivate(sc->sc_subdev);
491 sc->sc_dying = 1;
492 break;
493 }
494 return (rv);
495 }
496
497 usbd_status
498 uplcom_reset(struct uplcom_softc *sc)
499 {
500 usb_device_request_t req;
501 usbd_status err;
502
503 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
504 req.bRequest = UPLCOM_SET_REQUEST;
505 USETW(req.wValue, 0);
506 USETW(req.wIndex, sc->sc_iface_number);
507 USETW(req.wLength, 0);
508
509 err = usbd_do_request(sc->sc_udev, &req, 0);
510 if (err)
511 return (EIO);
512
513 return (0);
514 }
515
516 struct pl2303x_init {
517 uint8_t req_type;
518 uint8_t request;
519 uint16_t value;
520 uint16_t index;
521 uint16_t length;
522 };
523
524 static const struct pl2303x_init pl2303x[] = {
525 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
526 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 },
527 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
528 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
529 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
530 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 },
531 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
532 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
533 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 },
534 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 },
535 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 }
536 };
537 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
538
539 static usbd_status
540 uplcom_pl2303x_init(struct uplcom_softc *sc)
541 {
542 usb_device_request_t req;
543 usbd_status err;
544 int i;
545
546 for (i = 0; i < N_PL2302X_INIT; i++) {
547 req.bmRequestType = pl2303x[i].req_type;
548 req.bRequest = pl2303x[i].request;
549 USETW(req.wValue, pl2303x[i].value);
550 USETW(req.wIndex, pl2303x[i].index);
551 USETW(req.wLength, pl2303x[i].length);
552
553 err = usbd_do_request(sc->sc_udev, &req, 0);
554 if (err) {
555 printf("%s: uplcom_pl2303x_init failed: %s\n",
556 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
557 return (EIO);
558 }
559 }
560
561 return (0);
562 }
563
564 void
565 uplcom_set_line_state(struct uplcom_softc *sc)
566 {
567 usb_device_request_t req;
568 int ls;
569
570 /* make sure we have initialized state for sc_dtr and sc_rts */
571 if (sc->sc_dtr == -1)
572 sc->sc_dtr = 0;
573 if (sc->sc_rts == -1)
574 sc->sc_rts = 0;
575
576 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
577 (sc->sc_rts ? UCDC_LINE_RTS : 0);
578
579 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
580 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
581 USETW(req.wValue, ls);
582 USETW(req.wIndex, sc->sc_iface_number);
583 USETW(req.wLength, 0);
584
585 (void)usbd_do_request(sc->sc_udev, &req, 0);
586 }
587
588 void
589 uplcom_set(void *addr, int portno, int reg, int onoff)
590 {
591 struct uplcom_softc *sc = addr;
592
593 switch (reg) {
594 case UCOM_SET_DTR:
595 uplcom_dtr(sc, onoff);
596 break;
597 case UCOM_SET_RTS:
598 uplcom_rts(sc, onoff);
599 break;
600 case UCOM_SET_BREAK:
601 uplcom_break(sc, onoff);
602 break;
603 default:
604 break;
605 }
606 }
607
608 void
609 uplcom_dtr(struct uplcom_softc *sc, int onoff)
610 {
611
612 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
613
614 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
615 return;
616
617 sc->sc_dtr = !!onoff;
618
619 uplcom_set_line_state(sc);
620 }
621
622 void
623 uplcom_rts(struct uplcom_softc *sc, int onoff)
624 {
625 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
626
627 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
628 return;
629
630 sc->sc_rts = !!onoff;
631
632 uplcom_set_line_state(sc);
633 }
634
635 void
636 uplcom_break(struct uplcom_softc *sc, int onoff)
637 {
638 usb_device_request_t req;
639
640 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
641
642 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
643 req.bRequest = UCDC_SEND_BREAK;
644 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
645 USETW(req.wIndex, sc->sc_iface_number);
646 USETW(req.wLength, 0);
647
648 (void)usbd_do_request(sc->sc_udev, &req, 0);
649 }
650
651 usbd_status
652 uplcom_set_crtscts(struct uplcom_softc *sc)
653 {
654 usb_device_request_t req;
655 usbd_status err;
656
657 DPRINTF(("uplcom_set_crtscts: on\n"));
658
659 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
660 req.bRequest = UPLCOM_SET_REQUEST;
661 USETW(req.wValue, 0);
662 if (sc->sc_type == UPLCOM_TYPE_HX)
663 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
664 else
665 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
666 USETW(req.wLength, 0);
667
668 err = usbd_do_request(sc->sc_udev, &req, 0);
669 if (err) {
670 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
671 usbd_errstr(err)));
672 return (err);
673 }
674
675 return (USBD_NORMAL_COMPLETION);
676 }
677
678 usbd_status
679 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
680 {
681 usb_device_request_t req;
682 usbd_status err;
683
684 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
685 UGETDW(state->dwDTERate), state->bCharFormat,
686 state->bParityType, state->bDataBits));
687
688 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
689 DPRINTF(("uplcom_set_line_coding: already set\n"));
690 return (USBD_NORMAL_COMPLETION);
691 }
692
693 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
694 req.bRequest = UCDC_SET_LINE_CODING;
695 USETW(req.wValue, 0);
696 USETW(req.wIndex, sc->sc_iface_number);
697 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
698
699 err = usbd_do_request(sc->sc_udev, &req, state);
700 if (err) {
701 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
702 usbd_errstr(err)));
703 return (err);
704 }
705
706 sc->sc_line_state = *state;
707
708 return (USBD_NORMAL_COMPLETION);
709 }
710
711 int
712 uplcom_param(void *addr, int portno, struct termios *t)
713 {
714 struct uplcom_softc *sc = addr;
715 usbd_status err;
716 usb_cdc_line_state_t ls;
717
718 DPRINTF(("uplcom_param: sc=%p\n", sc));
719
720 USETDW(ls.dwDTERate, t->c_ospeed);
721 if (ISSET(t->c_cflag, CSTOPB))
722 ls.bCharFormat = UCDC_STOP_BIT_2;
723 else
724 ls.bCharFormat = UCDC_STOP_BIT_1;
725 if (ISSET(t->c_cflag, PARENB)) {
726 if (ISSET(t->c_cflag, PARODD))
727 ls.bParityType = UCDC_PARITY_ODD;
728 else
729 ls.bParityType = UCDC_PARITY_EVEN;
730 } else
731 ls.bParityType = UCDC_PARITY_NONE;
732 switch (ISSET(t->c_cflag, CSIZE)) {
733 case CS5:
734 ls.bDataBits = 5;
735 break;
736 case CS6:
737 ls.bDataBits = 6;
738 break;
739 case CS7:
740 ls.bDataBits = 7;
741 break;
742 case CS8:
743 ls.bDataBits = 8;
744 break;
745 }
746
747 err = uplcom_set_line_coding(sc, &ls);
748 if (err) {
749 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
750 return (EIO);
751 }
752
753 if (ISSET(t->c_cflag, CRTSCTS))
754 uplcom_set_crtscts(sc);
755
756 if (sc->sc_rts == -1 || sc->sc_dtr == -1)
757 uplcom_set_line_state(sc);
758
759 if (err) {
760 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
761 return (EIO);
762 }
763
764 return (0);
765 }
766
767 Static usbd_status
768 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
769 {
770 usb_device_request_t req;
771 usbd_status err;
772
773 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
774 req.bRequest = UPLCOM_SET_REQUEST;
775 USETW(req.wValue, value);
776 USETW(req.wIndex, index);
777 USETW(req.wLength, 0);
778
779 err = usbd_do_request(dev, &req, NULL);
780
781 if (err) {
782 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
783 usbd_errstr(err), err));
784 }
785
786 return err;
787 }
788
789 int
790 uplcom_open(void *addr, int portno)
791 {
792 struct uplcom_softc *sc = addr;
793 usbd_status err;
794
795 if (sc->sc_dying)
796 return (EIO);
797
798 DPRINTF(("uplcom_open: sc=%p\n", sc));
799
800 /* Some unknown device frobbing. */
801 if (sc->sc_type == UPLCOM_TYPE_HX)
802 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
803 else
804 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
805
806 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
807 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
808 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
809 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
810 sc->sc_intr_buf, sc->sc_isize,
811 uplcom_intr, USBD_DEFAULT_INTERVAL);
812 if (err) {
813 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
814 USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
815 return (EIO);
816 }
817 }
818
819 if (sc->sc_type == UPLCOM_TYPE_HX)
820 return (uplcom_pl2303x_init(sc));
821
822 return (0);
823 }
824
825 void
826 uplcom_close(void *addr, int portno)
827 {
828 struct uplcom_softc *sc = addr;
829 int err;
830
831 if (sc->sc_dying)
832 return;
833
834 DPRINTF(("uplcom_close: close\n"));
835
836 if (sc->sc_intr_pipe != NULL) {
837 err = usbd_abort_pipe(sc->sc_intr_pipe);
838 if (err)
839 printf("%s: abort interrupt pipe failed: %s\n",
840 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
841 err = usbd_close_pipe(sc->sc_intr_pipe);
842 if (err)
843 printf("%s: close interrupt pipe failed: %s\n",
844 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
845 free(sc->sc_intr_buf, M_USBDEV);
846 sc->sc_intr_pipe = NULL;
847 }
848 }
849
850 void
851 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
852 usbd_status status)
853 {
854 struct uplcom_softc *sc = priv;
855 u_char *buf = sc->sc_intr_buf;
856 u_char pstatus;
857
858 if (sc->sc_dying)
859 return;
860
861 if (status != USBD_NORMAL_COMPLETION) {
862 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
863 return;
864
865 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
866 usbd_errstr(status)));
867 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
868 return;
869 }
870
871 DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
872
873 sc->sc_lsr = sc->sc_msr = 0;
874 pstatus = buf[8];
875 if (ISSET(pstatus, RSAQ_STATUS_DSR))
876 sc->sc_msr |= UMSR_DSR;
877 if (ISSET(pstatus, RSAQ_STATUS_DCD))
878 sc->sc_msr |= UMSR_DCD;
879 ucom_status_change((struct ucom_softc *) sc->sc_subdev);
880 }
881
882 void
883 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
884 {
885 struct uplcom_softc *sc = addr;
886
887 DPRINTF(("uplcom_get_status:\n"));
888
889 if (lsr != NULL)
890 *lsr = sc->sc_lsr;
891 if (msr != NULL)
892 *msr = sc->sc_msr;
893 }
894
895 #if TODO
896 int
897 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
898 usb_proc_ptr p)
899 {
900 struct uplcom_softc *sc = addr;
901 int error = 0;
902
903 if (sc->sc_dying)
904 return (EIO);
905
906 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
907
908 switch (cmd) {
909 case TIOCNOTTY:
910 case TIOCMGET:
911 case TIOCMSET:
912 case USB_GET_CM_OVER_DATA:
913 case USB_SET_CM_OVER_DATA:
914 break;
915
916 default:
917 DPRINTF(("uplcom_ioctl: unknown\n"));
918 error = ENOTTY;
919 break;
920 }
921
922 return (error);
923 }
924 #endif
925