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