uplcom.c revision 1.30 1 /* $NetBSD: uplcom.c,v 1.30 2003/08/11 08:16:34 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 * Simple datasheet
40 * http://www.prolific.com.tw/download/DataSheet/pl2303_ds11.PDF
41 * http://www.nisseisg.co.jp/jyouhou/_cp/@gif/2303.pdf
42 * (english)
43 *
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.30 2003/08/11 08:16:34 augustss Exp $");
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/ioctl.h>
54 #include <sys/conf.h>
55 #include <sys/tty.h>
56 #include <sys/file.h>
57 #include <sys/select.h>
58 #include <sys/proc.h>
59 #include <sys/vnode.h>
60 #include <sys/device.h>
61 #include <sys/poll.h>
62
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbcdc.h>
65
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/usbdevs.h>
69 #include <dev/usb/usb_quirks.h>
70
71 #include <dev/usb/usbdevs.h>
72 #include <dev/usb/ucomvar.h>
73
74 #ifdef UPLCOM_DEBUG
75 #define DPRINTFN(n, x) if (uplcomdebug > (n)) logprintf x
76 int uplcomdebug = 0;
77 #else
78 #define DPRINTFN(n, x)
79 #endif
80 #define DPRINTF(x) DPRINTFN(0, x)
81
82 #define UPLCOM_CONFIG_INDEX 0
83 #define UPLCOM_IFACE_INDEX 0
84 #define UPLCOM_SECOND_IFACE_INDEX 1
85
86 #define UPLCOM_SET_REQUEST 0x01
87 #define UPLCOM_SET_CRTSCTS 0x41
88 #define RSAQ_STATUS_DSR 0x02
89 #define RSAQ_STATUS_DCD 0x01
90
91 struct uplcom_softc {
92 USBBASEDEVICE sc_dev; /* base device */
93 usbd_device_handle sc_udev; /* USB device */
94 usbd_interface_handle sc_iface; /* interface */
95 int sc_iface_number; /* interface number */
96
97 usbd_interface_handle sc_intr_iface; /* interrupt interface */
98 int sc_intr_number; /* interrupt number */
99 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
100 u_char *sc_intr_buf; /* interrupt buffer */
101 int sc_isize;
102
103 usb_cdc_line_state_t sc_line_state; /* current line state */
104 u_char sc_dtr; /* current DTR state */
105 u_char sc_rts; /* current RTS state */
106 u_char sc_status;
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
116 /*
117 * These are the maximum number of bytes transferred per frame.
118 * The output buffer size cannot be increased due to the size encoding.
119 */
120 #define UPLCOMIBUFSIZE 256
121 #define UPLCOMOBUFSIZE 256
122
123 Static usbd_status uplcom_reset(struct uplcom_softc *);
124 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
125 usb_cdc_line_state_t *state);
126 Static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
127 Static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
128
129 Static void uplcom_set(void *, int, int, int);
130 Static void uplcom_dtr(struct uplcom_softc *, int);
131 Static void uplcom_rts(struct uplcom_softc *, int);
132 Static void uplcom_break(struct uplcom_softc *, int);
133 Static void uplcom_set_line_state(struct uplcom_softc *);
134 Static void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
135 #if TODO
136 Static int uplcom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr );
137 #endif
138 Static int uplcom_param(void *, int, struct termios *);
139 Static int uplcom_open(void *, int);
140 Static void uplcom_close(void *, int);
141
142 struct ucom_methods uplcom_methods = {
143 uplcom_get_status,
144 uplcom_set,
145 uplcom_param,
146 NULL, /* uplcom_ioctl, TODO */
147 uplcom_open,
148 uplcom_close,
149 NULL,
150 NULL,
151 };
152
153 static const struct usb_devno uplcom_devs[] = {
154 /* I/O DATA USB-RSAQ2 */
155 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
156 /* I/O DATA USB-RSAQ */
157 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
158 /* PLANEX USB-RS232 URS-03 */
159 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
160 /* IOGEAR/ATEN UC-232A */
161 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
162 /* ELECOM UC-SGT */
163 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
164 /* RATOC REX-USB60 */
165 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
166 /* TDK USB-PHS Adapter UHA6400 */
167 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
168 /* TDK USB-PDC Adapter UPA9664 */
169 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
170 /* Sony Ericsson USB Cable */
171 { USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10 },
172 };
173 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
174
175 USB_DECLARE_DRIVER(uplcom);
176
177 USB_MATCH(uplcom)
178 {
179 USB_MATCH_START(uplcom, uaa);
180
181 if (uaa->iface != NULL)
182 return (UMATCH_NONE);
183
184 return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
185 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
186 }
187
188 USB_ATTACH(uplcom)
189 {
190 USB_ATTACH_START(uplcom, sc, uaa);
191 usbd_device_handle dev = uaa->device;
192 usb_config_descriptor_t *cdesc;
193 usb_interface_descriptor_t *id;
194 usb_endpoint_descriptor_t *ed;
195
196 char devinfo[1024];
197 char *devname = USBDEVNAME(sc->sc_dev);
198 usbd_status err;
199 int i;
200 struct ucom_attach_args uca;
201
202 usbd_devinfo(dev, 0, devinfo);
203 USB_ATTACH_SETUP;
204 printf("%s: %s\n", devname, devinfo);
205
206 sc->sc_udev = dev;
207
208 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
209
210 /* initialize endpoints */
211 uca.bulkin = uca.bulkout = -1;
212 sc->sc_intr_number = -1;
213 sc->sc_intr_pipe = NULL;
214
215 /* Move the device into the configured state. */
216 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
217 if (err) {
218 printf("\n%s: failed to set configuration, err=%s\n",
219 devname, usbd_errstr(err));
220 sc->sc_dying = 1;
221 USB_ATTACH_ERROR_RETURN;
222 }
223
224 /* get the config descriptor */
225 cdesc = usbd_get_config_descriptor(sc->sc_udev);
226
227 if (cdesc == NULL) {
228 printf("%s: failed to get configuration descriptor\n",
229 USBDEVNAME(sc->sc_dev));
230 sc->sc_dying = 1;
231 USB_ATTACH_ERROR_RETURN;
232 }
233
234 /* get the (first/common) interface */
235 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
236 &sc->sc_iface);
237 if (err) {
238 printf("\n%s: failed to get interface, err=%s\n",
239 devname, usbd_errstr(err));
240 sc->sc_dying = 1;
241 USB_ATTACH_ERROR_RETURN;
242 }
243
244 /* Find the interrupt endpoints */
245
246 id = usbd_get_interface_descriptor(sc->sc_iface);
247 sc->sc_iface_number = id->bInterfaceNumber;
248
249 for (i = 0; i < id->bNumEndpoints; i++) {
250 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
251 if (ed == NULL) {
252 printf("%s: no endpoint descriptor for %d\n",
253 USBDEVNAME(sc->sc_dev), i);
254 sc->sc_dying = 1;
255 USB_ATTACH_ERROR_RETURN;
256 }
257
258 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
259 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
260 sc->sc_intr_number = ed->bEndpointAddress;
261 sc->sc_isize = UGETW(ed->wMaxPacketSize);
262 }
263 }
264
265 if (sc->sc_intr_number== -1) {
266 printf("%s: Could not find interrupt in\n",
267 USBDEVNAME(sc->sc_dev));
268 sc->sc_dying = 1;
269 USB_ATTACH_ERROR_RETURN;
270 }
271
272 /* keep interface for interrupt */
273 sc->sc_intr_iface = sc->sc_iface;
274
275 /*
276 * USB-RSAQ1 has two interface
277 *
278 * USB-RSAQ1 | USB-RSAQ2
279 * -----------------+-----------------
280 * Interface 0 |Interface 0
281 * Interrupt(0x81) | Interrupt(0x81)
282 * -----------------+ BulkIN(0x02)
283 * Interface 1 | BulkOUT(0x83)
284 * BulkIN(0x02) |
285 * BulkOUT(0x83) |
286 */
287 if (cdesc->bNumInterface == 2) {
288 err = usbd_device2interface_handle(dev,
289 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
290 if (err) {
291 printf("\n%s: failed to get second interface, err=%s\n",
292 devname, usbd_errstr(err));
293 sc->sc_dying = 1;
294 USB_ATTACH_ERROR_RETURN;
295 }
296 }
297
298 /* Find the bulk{in,out} 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_BULK) {
314 uca.bulkin = ed->bEndpointAddress;
315 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
316 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
317 uca.bulkout = ed->bEndpointAddress;
318 }
319 }
320
321 if (uca.bulkin == -1) {
322 printf("%s: Could not find data bulk in\n",
323 USBDEVNAME(sc->sc_dev));
324 sc->sc_dying = 1;
325 USB_ATTACH_ERROR_RETURN;
326 }
327
328 if (uca.bulkout == -1) {
329 printf("%s: Could not find data bulk out\n",
330 USBDEVNAME(sc->sc_dev));
331 sc->sc_dying = 1;
332 USB_ATTACH_ERROR_RETURN;
333 }
334
335 sc->sc_dtr = sc->sc_rts = -1;
336 uca.portno = UCOM_UNK_PORTNO;
337 /* bulkin, bulkout set above */
338 uca.ibufsize = UPLCOMIBUFSIZE;
339 uca.obufsize = UPLCOMOBUFSIZE;
340 uca.ibufsizepad = UPLCOMIBUFSIZE;
341 uca.opkthdrlen = 0;
342 uca.device = dev;
343 uca.iface = sc->sc_iface;
344 uca.methods = &uplcom_methods;
345 uca.arg = sc;
346 uca.info = NULL;
347
348 err = uplcom_reset(sc);
349
350 if (err) {
351 printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
352 usbd_errstr(err));
353 sc->sc_dying = 1;
354 USB_ATTACH_ERROR_RETURN;
355 }
356
357 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
358 USBDEV(sc->sc_dev));
359
360 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
361 uca.bulkin, uca.bulkout, sc->sc_intr_number ));
362 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
363
364 USB_ATTACH_SUCCESS_RETURN;
365 }
366
367 USB_DETACH(uplcom)
368 {
369 USB_DETACH_START(uplcom, sc);
370 int rv = 0;
371
372 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
373
374 if (sc->sc_intr_pipe != NULL) {
375 usbd_abort_pipe(sc->sc_intr_pipe);
376 usbd_close_pipe(sc->sc_intr_pipe);
377 free(sc->sc_intr_buf, M_USBDEV);
378 sc->sc_intr_pipe = NULL;
379 }
380
381 sc->sc_dying = 1;
382 if (sc->sc_subdev != NULL) {
383 rv = config_detach(sc->sc_subdev, flags);
384 sc->sc_subdev = NULL;
385 }
386
387 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
388 USBDEV(sc->sc_dev));
389
390 return (rv);
391 }
392
393 int
394 uplcom_activate(device_ptr_t self, enum devact act)
395 {
396 struct uplcom_softc *sc = (struct uplcom_softc *)self;
397 int rv = 0;
398
399 switch (act) {
400 case DVACT_ACTIVATE:
401 return (EOPNOTSUPP);
402
403 case DVACT_DEACTIVATE:
404 if (sc->sc_subdev != NULL)
405 rv = config_deactivate(sc->sc_subdev);
406 sc->sc_dying = 1;
407 break;
408 }
409 return (rv);
410 }
411
412 usbd_status
413 uplcom_reset(struct uplcom_softc *sc)
414 {
415 usb_device_request_t req;
416 usbd_status err;
417
418 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
419 req.bRequest = UPLCOM_SET_REQUEST;
420 USETW(req.wValue, 0);
421 USETW(req.wIndex, sc->sc_iface_number);
422 USETW(req.wLength, 0);
423
424 err = usbd_do_request(sc->sc_udev, &req, 0);
425 if (err)
426 return (EIO);
427
428 return (0);
429 }
430
431 void
432 uplcom_set_line_state(struct uplcom_softc *sc)
433 {
434 usb_device_request_t req;
435 int ls;
436
437 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
438 (sc->sc_rts ? UCDC_LINE_RTS : 0);
439 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
440 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
441 USETW(req.wValue, ls);
442 USETW(req.wIndex, sc->sc_iface_number);
443 USETW(req.wLength, 0);
444
445 (void)usbd_do_request(sc->sc_udev, &req, 0);
446
447 }
448
449 void
450 uplcom_set(void *addr, int portno, int reg, int onoff)
451 {
452 struct uplcom_softc *sc = addr;
453
454 switch (reg) {
455 case UCOM_SET_DTR:
456 uplcom_dtr(sc, onoff);
457 break;
458 case UCOM_SET_RTS:
459 uplcom_rts(sc, onoff);
460 break;
461 case UCOM_SET_BREAK:
462 uplcom_break(sc, onoff);
463 break;
464 default:
465 break;
466 }
467 }
468
469 void
470 uplcom_dtr(struct uplcom_softc *sc, int onoff)
471 {
472
473 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
474
475 if (sc->sc_dtr == onoff)
476 return;
477 sc->sc_dtr = onoff;
478
479 uplcom_set_line_state(sc);
480 }
481
482 void
483 uplcom_rts(struct uplcom_softc *sc, int onoff)
484 {
485 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
486
487 if (sc->sc_rts == onoff)
488 return;
489 sc->sc_rts = onoff;
490
491 uplcom_set_line_state(sc);
492 }
493
494 void
495 uplcom_break(struct uplcom_softc *sc, int onoff)
496 {
497 usb_device_request_t req;
498
499 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
500
501 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
502 req.bRequest = UCDC_SEND_BREAK;
503 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
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 usbd_status
511 uplcom_set_crtscts(struct uplcom_softc *sc)
512 {
513 usb_device_request_t req;
514 usbd_status err;
515
516 DPRINTF(("uplcom_set_crtscts: on\n"));
517
518 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
519 req.bRequest = UPLCOM_SET_REQUEST;
520 USETW(req.wValue, 0);
521 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
522 USETW(req.wLength, 0);
523
524 err = usbd_do_request(sc->sc_udev, &req, 0);
525 if (err) {
526 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
527 usbd_errstr(err)));
528 return (err);
529 }
530
531 return (USBD_NORMAL_COMPLETION);
532 }
533
534 usbd_status
535 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
536 {
537 usb_device_request_t req;
538 usbd_status err;
539
540 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
541 UGETDW(state->dwDTERate), state->bCharFormat,
542 state->bParityType, state->bDataBits));
543
544 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
545 DPRINTF(("uplcom_set_line_coding: already set\n"));
546 return (USBD_NORMAL_COMPLETION);
547 }
548
549 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
550 req.bRequest = UCDC_SET_LINE_CODING;
551 USETW(req.wValue, 0);
552 USETW(req.wIndex, sc->sc_iface_number);
553 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
554
555 err = usbd_do_request(sc->sc_udev, &req, state);
556 if (err) {
557 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
558 usbd_errstr(err)));
559 return (err);
560 }
561
562 sc->sc_line_state = *state;
563
564 return (USBD_NORMAL_COMPLETION);
565 }
566
567 int
568 uplcom_param(void *addr, int portno, struct termios *t)
569 {
570 struct uplcom_softc *sc = addr;
571 usbd_status err;
572 usb_cdc_line_state_t ls;
573
574 DPRINTF(("uplcom_param: sc=%p\n", sc));
575
576 USETDW(ls.dwDTERate, t->c_ospeed);
577 if (ISSET(t->c_cflag, CSTOPB))
578 ls.bCharFormat = UCDC_STOP_BIT_2;
579 else
580 ls.bCharFormat = UCDC_STOP_BIT_1;
581 if (ISSET(t->c_cflag, PARENB)) {
582 if (ISSET(t->c_cflag, PARODD))
583 ls.bParityType = UCDC_PARITY_ODD;
584 else
585 ls.bParityType = UCDC_PARITY_EVEN;
586 } else
587 ls.bParityType = UCDC_PARITY_NONE;
588 switch (ISSET(t->c_cflag, CSIZE)) {
589 case CS5:
590 ls.bDataBits = 5;
591 break;
592 case CS6:
593 ls.bDataBits = 6;
594 break;
595 case CS7:
596 ls.bDataBits = 7;
597 break;
598 case CS8:
599 ls.bDataBits = 8;
600 break;
601 }
602
603 err = uplcom_set_line_coding(sc, &ls);
604 if (err) {
605 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
606 return (EIO);
607 }
608
609 if (ISSET(t->c_cflag, CRTSCTS))
610 uplcom_set_crtscts(sc);
611
612 if (err) {
613 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
614 return (EIO);
615 }
616
617 return (0);
618 }
619
620 int
621 uplcom_open(void *addr, int portno)
622 {
623 struct uplcom_softc *sc = addr;
624 int err;
625
626 if (sc->sc_dying)
627 return (EIO);
628
629 DPRINTF(("uplcom_open: sc=%p\n", sc));
630
631 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
632 sc->sc_status = 0; /* clear status bit */
633 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
634 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
635 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
636 sc->sc_intr_buf, sc->sc_isize,
637 uplcom_intr, USBD_DEFAULT_INTERVAL);
638 if (err) {
639 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
640 USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
641 return (EIO);
642 }
643 }
644
645 return (0);
646 }
647
648 void
649 uplcom_close(void *addr, int portno)
650 {
651 struct uplcom_softc *sc = addr;
652 int err;
653
654 if (sc->sc_dying)
655 return;
656
657 DPRINTF(("uplcom_close: close\n"));
658
659 if (sc->sc_intr_pipe != NULL) {
660 err = usbd_abort_pipe(sc->sc_intr_pipe);
661 if (err)
662 printf("%s: abort interrupt pipe failed: %s\n",
663 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
664 err = usbd_close_pipe(sc->sc_intr_pipe);
665 if (err)
666 printf("%s: close interrupt pipe failed: %s\n",
667 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
668 free(sc->sc_intr_buf, M_USBDEV);
669 sc->sc_intr_pipe = NULL;
670 }
671 }
672
673 void
674 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
675 {
676 struct uplcom_softc *sc = priv;
677 u_char *buf = sc->sc_intr_buf;
678 u_char pstatus;
679
680 if (sc->sc_dying)
681 return;
682
683 if (status != USBD_NORMAL_COMPLETION) {
684 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
685 return;
686
687 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
688 usbd_errstr(status)));
689 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
690 return;
691 }
692
693 DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
694
695 sc->sc_lsr = sc->sc_msr = 0;
696 pstatus = buf[8];
697 if (ISSET(pstatus, RSAQ_STATUS_DSR))
698 sc->sc_msr |= UMSR_DSR;
699 if (ISSET(pstatus, RSAQ_STATUS_DCD))
700 sc->sc_msr |= UMSR_DCD;
701 ucom_status_change((struct ucom_softc *) sc->sc_subdev);
702 }
703
704 void
705 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
706 {
707 struct uplcom_softc *sc = addr;
708
709 DPRINTF(("uplcom_get_status:\n"));
710
711 if (lsr != NULL)
712 *lsr = sc->sc_lsr;
713 if (msr != NULL)
714 *msr = sc->sc_msr;
715 }
716
717 #if TODO
718 int
719 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
720 usb_proc_ptr p)
721 {
722 struct uplcom_softc *sc = addr;
723 int error = 0;
724
725 if (sc->sc_dying)
726 return (EIO);
727
728 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
729
730 switch (cmd) {
731 case TIOCNOTTY:
732 case TIOCMGET:
733 case TIOCMSET:
734 case USB_GET_CM_OVER_DATA:
735 case USB_SET_CM_OVER_DATA:
736 break;
737
738 default:
739 DPRINTF(("uplcom_ioctl: unknown\n"));
740 error = ENOTTY;
741 break;
742 }
743
744 return (error);
745 }
746 #endif
747