uplcom.c revision 1.43 1 /* $NetBSD: uplcom.c,v 1.43 2005/05/11 20:25:01 augustss 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.43 2005/05/11 20:25:01 augustss 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/usbdevs.h>
68 #include <dev/usb/ucomvar.h>
69
70 #ifdef UPLCOM_DEBUG
71 #define DPRINTFN(n, x) if (uplcomdebug > (n)) logprintf x
72 int uplcomdebug = 0;
73 #else
74 #define DPRINTFN(n, x)
75 #endif
76 #define DPRINTF(x) DPRINTFN(0, x)
77
78 #define UPLCOM_CONFIG_INDEX 0
79 #define UPLCOM_IFACE_INDEX 0
80 #define UPLCOM_SECOND_IFACE_INDEX 1
81
82 #define UPLCOM_SET_REQUEST 0x01
83 #define UPLCOM_SET_CRTSCTS_0 0x41
84 #define UPLCOM_SET_CRTSCTS_HX 0x61
85 #define RSAQ_STATUS_DSR 0x02
86 #define RSAQ_STATUS_DCD 0x01
87
88 #define UPLCOM_FLOW_OUT_CTS 0x0001
89 #define UPLCOM_FLOW_OUT_DSR 0x0002
90 #define UPLCOM_FLOW_IN_DSR 0x0004
91 #define UPLCOM_FLOW_IN_DTR 0x0008
92 #define UPLCOM_FLOW_IN_RTS 0x0010
93 #define UPLCOM_FLOW_OUT_RTS 0x0020
94 #define UPLCOM_FLOW_OUT_XON 0x0080
95 #define UPLCOM_FLOW_IN_XON 0x0100
96
97 enum pl2303_type {
98 UPLCOM_TYPE_0,
99 UPLCOM_TYPE_HX,
100 };
101
102 struct uplcom_softc {
103 USBBASEDEVICE sc_dev; /* base device */
104 usbd_device_handle sc_udev; /* USB device */
105 usbd_interface_handle sc_iface; /* interface */
106 int sc_iface_number; /* interface number */
107
108 usbd_interface_handle sc_intr_iface; /* interrupt interface */
109 int sc_intr_number; /* interrupt number */
110 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
111 u_char *sc_intr_buf; /* interrupt buffer */
112 int sc_isize;
113
114 usb_cdc_line_state_t sc_line_state; /* current line state */
115 int sc_dtr; /* current DTR state */
116 int sc_rts; /* current RTS state */
117
118 device_ptr_t sc_subdev; /* ucom device */
119
120 u_char sc_dying; /* disconnecting */
121
122 u_char sc_lsr; /* Local status register */
123 u_char sc_msr; /* uplcom status register */
124
125 enum pl2303_type sc_type; /* PL2303 chip type */
126 };
127
128 /*
129 * These are the maximum number of bytes transferred per frame.
130 * The output buffer size cannot be increased due to the size encoding.
131 */
132 #define UPLCOMIBUFSIZE 256
133 #define UPLCOMOBUFSIZE 256
134
135 Static usbd_status uplcom_reset(struct uplcom_softc *);
136 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
137 usb_cdc_line_state_t *state);
138 Static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
139 Static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
140
141 Static void uplcom_set(void *, int, int, int);
142 Static void uplcom_dtr(struct uplcom_softc *, int);
143 Static void uplcom_rts(struct uplcom_softc *, int);
144 Static void uplcom_break(struct uplcom_softc *, int);
145 Static void uplcom_set_line_state(struct uplcom_softc *);
146 Static void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
147 #if TODO
148 Static int uplcom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr );
149 #endif
150 Static int uplcom_param(void *, int, struct termios *);
151 Static int uplcom_open(void *, int);
152 Static void uplcom_close(void *, int);
153 Static usbd_status uplcom_vendor_control_write(usbd_device_handle, u_int16_t, u_int16_t);
154
155 struct ucom_methods uplcom_methods = {
156 uplcom_get_status,
157 uplcom_set,
158 uplcom_param,
159 NULL, /* uplcom_ioctl, TODO */
160 uplcom_open,
161 uplcom_close,
162 NULL,
163 NULL,
164 };
165
166 static const struct usb_devno uplcom_devs[] = {
167 /* I/O DATA USB-RSAQ2 */
168 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
169 /* I/O DATA USB-RSAQ */
170 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
171 /* PLANEX USB-RS232 URS-03 */
172 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
173 /* IOGEAR/ATEN UC-232A */
174 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
175 /* IOGEAR/ATENTRIPPLITE */
176 { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
177 /* ELECOM UC-SGT */
178 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
179 /* RATOC REX-USB60 */
180 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
181 /* TDK USB-PHS Adapter UHA6400 */
182 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
183 /* TDK USB-PDC Adapter UPA9664 */
184 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
185 /* Sony Ericsson USB Cable */
186 { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
187 /* SOURCENEXT KeikaiDenwa 8 */
188 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
189 /* SOURCENEXT KeikaiDenwa 8 with charger */
190 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
191 /* HAL Corporation Crossam2+USB */
192 { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
193 /* Sitecom USB to serial cable */
194 { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
195 /* Pharos USB GPS - Microsoft version */
196 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
197 };
198 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
199
200
201 USB_DECLARE_DRIVER(uplcom);
202
203 USB_MATCH(uplcom)
204 {
205 USB_MATCH_START(uplcom, uaa);
206
207 if (uaa->iface != NULL)
208 return (UMATCH_NONE);
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 char *devname = USBDEVNAME(sc->sc_dev);
224 usbd_status err;
225 int i;
226 struct ucom_attach_args uca;
227
228 devinfop = usbd_devinfo_alloc(dev, 0);
229 USB_ATTACH_SETUP;
230 printf("%s: %s\n", devname, 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 printf("\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 /* get the device descriptor */
252 ddesc = usbd_get_device_descriptor(sc->sc_udev);
253 if (ddesc == NULL) {
254 printf("%s: failed to get device descriptor\n",
255 USBDEVNAME(sc->sc_dev));
256 sc->sc_dying = 1;
257 USB_ATTACH_ERROR_RETURN;
258 }
259
260 /*
261 * NOTE: The Linux driver distinguishes between UPLCOM_TYPE_0
262 * and UPLCOM_TYPE_1 type chips by testing other fields in the
263 * device descriptor. As far as the uplcom driver is
264 * concerned, both types are identical.
265 * The bcdDevice field should also distinguish these versions,
266 * but who knows.
267 */
268 if (UGETW(ddesc->bcdDevice) == 0x0300)
269 sc->sc_type = UPLCOM_TYPE_HX;
270 else
271 sc->sc_type = UPLCOM_TYPE_0;
272
273 /* get the config descriptor */
274 cdesc = usbd_get_config_descriptor(sc->sc_udev);
275
276 if (cdesc == NULL) {
277 printf("%s: failed to get configuration descriptor\n",
278 USBDEVNAME(sc->sc_dev));
279 sc->sc_dying = 1;
280 USB_ATTACH_ERROR_RETURN;
281 }
282
283 /* get the (first/common) interface */
284 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
285 &sc->sc_iface);
286 if (err) {
287 printf("\n%s: failed to get interface, err=%s\n",
288 devname, usbd_errstr(err));
289 sc->sc_dying = 1;
290 USB_ATTACH_ERROR_RETURN;
291 }
292
293 /* Find the interrupt endpoints */
294
295 id = usbd_get_interface_descriptor(sc->sc_iface);
296 sc->sc_iface_number = id->bInterfaceNumber;
297
298 for (i = 0; i < id->bNumEndpoints; i++) {
299 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
300 if (ed == NULL) {
301 printf("%s: no endpoint descriptor for %d\n",
302 USBDEVNAME(sc->sc_dev), i);
303 sc->sc_dying = 1;
304 USB_ATTACH_ERROR_RETURN;
305 }
306
307 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
308 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
309 sc->sc_intr_number = ed->bEndpointAddress;
310 sc->sc_isize = UGETW(ed->wMaxPacketSize);
311 }
312 }
313
314 if (sc->sc_intr_number== -1) {
315 printf("%s: Could not find interrupt in\n",
316 USBDEVNAME(sc->sc_dev));
317 sc->sc_dying = 1;
318 USB_ATTACH_ERROR_RETURN;
319 }
320
321 /* keep interface for interrupt */
322 sc->sc_intr_iface = sc->sc_iface;
323
324 /*
325 * USB-RSAQ1 has two interface
326 *
327 * USB-RSAQ1 | USB-RSAQ2
328 * -----------------+-----------------
329 * Interface 0 |Interface 0
330 * Interrupt(0x81) | Interrupt(0x81)
331 * -----------------+ BulkIN(0x02)
332 * Interface 1 | BulkOUT(0x83)
333 * BulkIN(0x02) |
334 * BulkOUT(0x83) |
335 */
336 if (cdesc->bNumInterface == 2) {
337 err = usbd_device2interface_handle(dev,
338 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
339 if (err) {
340 printf("\n%s: failed to get second interface, err=%s\n",
341 devname, usbd_errstr(err));
342 sc->sc_dying = 1;
343 USB_ATTACH_ERROR_RETURN;
344 }
345 }
346
347 /* Find the bulk{in,out} endpoints */
348
349 id = usbd_get_interface_descriptor(sc->sc_iface);
350 sc->sc_iface_number = id->bInterfaceNumber;
351
352 for (i = 0; i < id->bNumEndpoints; i++) {
353 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
354 if (ed == NULL) {
355 printf("%s: no endpoint descriptor for %d\n",
356 USBDEVNAME(sc->sc_dev), i);
357 sc->sc_dying = 1;
358 USB_ATTACH_ERROR_RETURN;
359 }
360
361 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
362 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
363 uca.bulkin = ed->bEndpointAddress;
364 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
365 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
366 uca.bulkout = ed->bEndpointAddress;
367 }
368 }
369
370 if (uca.bulkin == -1) {
371 printf("%s: Could not find data bulk in\n",
372 USBDEVNAME(sc->sc_dev));
373 sc->sc_dying = 1;
374 USB_ATTACH_ERROR_RETURN;
375 }
376
377 if (uca.bulkout == -1) {
378 printf("%s: Could not find data bulk out\n",
379 USBDEVNAME(sc->sc_dev));
380 sc->sc_dying = 1;
381 USB_ATTACH_ERROR_RETURN;
382 }
383
384 sc->sc_dtr = sc->sc_rts = -1;
385 uca.portno = UCOM_UNK_PORTNO;
386 /* bulkin, bulkout set above */
387 uca.ibufsize = UPLCOMIBUFSIZE;
388 uca.obufsize = UPLCOMOBUFSIZE;
389 uca.ibufsizepad = UPLCOMIBUFSIZE;
390 uca.opkthdrlen = 0;
391 uca.device = dev;
392 uca.iface = sc->sc_iface;
393 uca.methods = &uplcom_methods;
394 uca.arg = sc;
395 uca.info = NULL;
396
397 err = uplcom_reset(sc);
398
399 if (err) {
400 printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
401 usbd_errstr(err));
402 sc->sc_dying = 1;
403 USB_ATTACH_ERROR_RETURN;
404 }
405
406 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
407 USBDEV(sc->sc_dev));
408
409 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
410 uca.bulkin, uca.bulkout, sc->sc_intr_number ));
411 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
412 ucomprint, ucomsubmatch);
413
414 USB_ATTACH_SUCCESS_RETURN;
415 }
416
417 USB_DETACH(uplcom)
418 {
419 USB_DETACH_START(uplcom, sc);
420 int rv = 0;
421
422 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
423
424 if (sc->sc_intr_pipe != NULL) {
425 usbd_abort_pipe(sc->sc_intr_pipe);
426 usbd_close_pipe(sc->sc_intr_pipe);
427 free(sc->sc_intr_buf, M_USBDEV);
428 sc->sc_intr_pipe = NULL;
429 }
430
431 sc->sc_dying = 1;
432 if (sc->sc_subdev != NULL) {
433 rv = config_detach(sc->sc_subdev, flags);
434 sc->sc_subdev = NULL;
435 }
436
437 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
438 USBDEV(sc->sc_dev));
439
440 return (rv);
441 }
442
443 int
444 uplcom_activate(device_ptr_t self, enum devact act)
445 {
446 struct uplcom_softc *sc = (struct uplcom_softc *)self;
447 int rv = 0;
448
449 switch (act) {
450 case DVACT_ACTIVATE:
451 return (EOPNOTSUPP);
452
453 case DVACT_DEACTIVATE:
454 if (sc->sc_subdev != NULL)
455 rv = config_deactivate(sc->sc_subdev);
456 sc->sc_dying = 1;
457 break;
458 }
459 return (rv);
460 }
461
462 usbd_status
463 uplcom_reset(struct uplcom_softc *sc)
464 {
465 usb_device_request_t req;
466 usbd_status err;
467
468 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
469 req.bRequest = UPLCOM_SET_REQUEST;
470 USETW(req.wValue, 0);
471 USETW(req.wIndex, sc->sc_iface_number);
472 USETW(req.wLength, 0);
473
474 err = usbd_do_request(sc->sc_udev, &req, 0);
475 if (err)
476 return (EIO);
477
478 return (0);
479 }
480
481 void
482 uplcom_set_line_state(struct uplcom_softc *sc)
483 {
484 usb_device_request_t req;
485 int ls;
486
487 /* make sure we have initialized state for sc_dtr and sc_rts */
488 if (sc->sc_dtr == -1)
489 sc->sc_dtr = 0;
490 if (sc->sc_rts == -1)
491 sc->sc_rts = 0;
492
493 ls = (sc->sc_dtr ? UPLCOM_FLOW_OUT_DSR : 0) |
494 (sc->sc_rts ? UPLCOM_FLOW_OUT_CTS : 0);
495
496 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
497 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
498 USETW(req.wValue, ls);
499 USETW(req.wIndex, sc->sc_iface_number);
500 USETW(req.wLength, 0);
501
502 (void)usbd_do_request(sc->sc_udev, &req, 0);
503 }
504
505 void
506 uplcom_set(void *addr, int portno, int reg, int onoff)
507 {
508 struct uplcom_softc *sc = addr;
509
510 switch (reg) {
511 case UCOM_SET_DTR:
512 uplcom_dtr(sc, onoff);
513 break;
514 case UCOM_SET_RTS:
515 uplcom_rts(sc, onoff);
516 break;
517 case UCOM_SET_BREAK:
518 uplcom_break(sc, onoff);
519 break;
520 default:
521 break;
522 }
523 }
524
525 void
526 uplcom_dtr(struct uplcom_softc *sc, int onoff)
527 {
528
529 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
530
531 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
532 return;
533
534 sc->sc_dtr = !!onoff;
535
536 uplcom_set_line_state(sc);
537 }
538
539 void
540 uplcom_rts(struct uplcom_softc *sc, int onoff)
541 {
542 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
543
544 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
545 return;
546
547 sc->sc_rts = !!onoff;
548
549 uplcom_set_line_state(sc);
550 }
551
552 void
553 uplcom_break(struct uplcom_softc *sc, int onoff)
554 {
555 usb_device_request_t req;
556
557 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
558
559 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
560 req.bRequest = UCDC_SEND_BREAK;
561 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
562 USETW(req.wIndex, sc->sc_iface_number);
563 USETW(req.wLength, 0);
564
565 (void)usbd_do_request(sc->sc_udev, &req, 0);
566 }
567
568 usbd_status
569 uplcom_set_crtscts(struct uplcom_softc *sc)
570 {
571 usb_device_request_t req;
572 usbd_status err;
573
574 DPRINTF(("uplcom_set_crtscts: on\n"));
575
576 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
577 req.bRequest = UPLCOM_SET_REQUEST;
578 USETW(req.wValue, 0);
579 if (sc->sc_type == UPLCOM_TYPE_HX)
580 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
581 else
582 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
583 USETW(req.wLength, 0);
584
585 err = usbd_do_request(sc->sc_udev, &req, 0);
586 if (err) {
587 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
588 usbd_errstr(err)));
589 return (err);
590 }
591
592 return (USBD_NORMAL_COMPLETION);
593 }
594
595 usbd_status
596 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
597 {
598 usb_device_request_t req;
599 usbd_status err;
600
601 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
602 UGETDW(state->dwDTERate), state->bCharFormat,
603 state->bParityType, state->bDataBits));
604
605 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
606 DPRINTF(("uplcom_set_line_coding: already set\n"));
607 return (USBD_NORMAL_COMPLETION);
608 }
609
610 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
611 req.bRequest = UCDC_SET_LINE_CODING;
612 USETW(req.wValue, 0);
613 USETW(req.wIndex, sc->sc_iface_number);
614 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
615
616 err = usbd_do_request(sc->sc_udev, &req, state);
617 if (err) {
618 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
619 usbd_errstr(err)));
620 return (err);
621 }
622
623 sc->sc_line_state = *state;
624
625 return (USBD_NORMAL_COMPLETION);
626 }
627
628 int
629 uplcom_param(void *addr, int portno, struct termios *t)
630 {
631 struct uplcom_softc *sc = addr;
632 usbd_status err;
633 usb_cdc_line_state_t ls;
634
635 DPRINTF(("uplcom_param: sc=%p\n", sc));
636
637 USETDW(ls.dwDTERate, t->c_ospeed);
638 if (ISSET(t->c_cflag, CSTOPB))
639 ls.bCharFormat = UCDC_STOP_BIT_2;
640 else
641 ls.bCharFormat = UCDC_STOP_BIT_1;
642 if (ISSET(t->c_cflag, PARENB)) {
643 if (ISSET(t->c_cflag, PARODD))
644 ls.bParityType = UCDC_PARITY_ODD;
645 else
646 ls.bParityType = UCDC_PARITY_EVEN;
647 } else
648 ls.bParityType = UCDC_PARITY_NONE;
649 switch (ISSET(t->c_cflag, CSIZE)) {
650 case CS5:
651 ls.bDataBits = 5;
652 break;
653 case CS6:
654 ls.bDataBits = 6;
655 break;
656 case CS7:
657 ls.bDataBits = 7;
658 break;
659 case CS8:
660 ls.bDataBits = 8;
661 break;
662 }
663
664 err = uplcom_set_line_coding(sc, &ls);
665 if (err) {
666 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
667 return (EIO);
668 }
669
670 if (ISSET(t->c_cflag, CRTSCTS))
671 uplcom_set_crtscts(sc);
672
673 if (sc->sc_rts == -1 || sc->sc_dtr == -1)
674 uplcom_set_line_state(sc);
675
676 if (err) {
677 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
678 return (EIO);
679 }
680
681 return (0);
682 }
683
684 Static usbd_status
685 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
686 {
687 usb_device_request_t req;
688 usbd_status err;
689
690 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
691 req.bRequest = UPLCOM_SET_REQUEST;
692 USETW(req.wValue, value);
693 USETW(req.wIndex, index);
694 USETW(req.wLength, 0);
695
696 err = usbd_do_request(dev, &req, NULL);
697
698 if (err) {
699 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
700 usbd_errstr(err), err));
701 }
702
703 return err;
704 }
705
706 int
707 uplcom_open(void *addr, int portno)
708 {
709 struct uplcom_softc *sc = addr;
710 usbd_status err;
711
712 if (sc->sc_dying)
713 return (EIO);
714
715 DPRINTF(("uplcom_open: sc=%p\n", sc));
716
717 /* Some unknown device frobbing. */
718 if (sc->sc_type == UPLCOM_TYPE_HX)
719 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
720 else
721 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
722
723 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
724 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
725 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
726 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
727 sc->sc_intr_buf, sc->sc_isize,
728 uplcom_intr, USBD_DEFAULT_INTERVAL);
729 if (err) {
730 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
731 USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
732 return (EIO);
733 }
734 }
735
736 return (0);
737 }
738
739 void
740 uplcom_close(void *addr, int portno)
741 {
742 struct uplcom_softc *sc = addr;
743 int err;
744
745 if (sc->sc_dying)
746 return;
747
748 DPRINTF(("uplcom_close: close\n"));
749
750 if (sc->sc_intr_pipe != NULL) {
751 err = usbd_abort_pipe(sc->sc_intr_pipe);
752 if (err)
753 printf("%s: abort interrupt pipe failed: %s\n",
754 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
755 err = usbd_close_pipe(sc->sc_intr_pipe);
756 if (err)
757 printf("%s: close interrupt pipe failed: %s\n",
758 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
759 free(sc->sc_intr_buf, M_USBDEV);
760 sc->sc_intr_pipe = NULL;
761 }
762 }
763
764 void
765 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
766 {
767 struct uplcom_softc *sc = priv;
768 u_char *buf = sc->sc_intr_buf;
769 u_char pstatus;
770
771 if (sc->sc_dying)
772 return;
773
774 if (status != USBD_NORMAL_COMPLETION) {
775 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
776 return;
777
778 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
779 usbd_errstr(status)));
780 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
781 return;
782 }
783
784 DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
785
786 sc->sc_lsr = sc->sc_msr = 0;
787 pstatus = buf[8];
788 if (ISSET(pstatus, RSAQ_STATUS_DSR))
789 sc->sc_msr |= UMSR_DSR;
790 if (ISSET(pstatus, RSAQ_STATUS_DCD))
791 sc->sc_msr |= UMSR_DCD;
792 ucom_status_change((struct ucom_softc *) sc->sc_subdev);
793 }
794
795 void
796 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
797 {
798 struct uplcom_softc *sc = addr;
799
800 DPRINTF(("uplcom_get_status:\n"));
801
802 if (lsr != NULL)
803 *lsr = sc->sc_lsr;
804 if (msr != NULL)
805 *msr = sc->sc_msr;
806 }
807
808 #if TODO
809 int
810 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
811 usb_proc_ptr p)
812 {
813 struct uplcom_softc *sc = addr;
814 int error = 0;
815
816 if (sc->sc_dying)
817 return (EIO);
818
819 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
820
821 switch (cmd) {
822 case TIOCNOTTY:
823 case TIOCMGET:
824 case TIOCMSET:
825 case USB_GET_CM_OVER_DATA:
826 case USB_SET_CM_OVER_DATA:
827 break;
828
829 default:
830 DPRINTF(("uplcom_ioctl: unknown\n"));
831 error = ENOTTY;
832 break;
833 }
834
835 return (error);
836 }
837 #endif
838