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