Line data Source code
1 : /* $OpenBSD: uscom.c,v 1.6 2018/08/22 15:32:49 mpi Exp $ */
2 :
3 : /*
4 : * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
5 : *
6 : * Permission to use, copy, modify, and distribute this software for any
7 : * purpose with or without fee is hereby granted, provided that the above
8 : * copyright notice and this permission notice appear in all copies.
9 : *
10 : * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 : * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 : * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 : * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 : * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 : * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 : * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 : */
18 :
19 : #include <sys/param.h>
20 : #include <sys/systm.h>
21 : #include <sys/kernel.h>
22 : #include <sys/conf.h>
23 : #include <sys/tty.h>
24 : #include <sys/device.h>
25 :
26 : #include <dev/usb/usb.h>
27 : #include <dev/usb/usbdi.h>
28 : #include <dev/usb/usbdi_util.h>
29 : #include <dev/usb/usbdevs.h>
30 :
31 : #include <dev/usb/ucomvar.h>
32 :
33 : #define USCOMBUFSZ 256
34 : #define USCOM_IFACE_NO 0
35 :
36 : struct uscom_softc {
37 : struct device sc_dev;
38 : struct usbd_device *sc_udev;
39 : struct usbd_interface *sc_iface;
40 : struct device *sc_subdev;
41 : };
42 :
43 : struct ucom_methods uscom_methods = {
44 : NULL,
45 : NULL,
46 : NULL,
47 : NULL,
48 : NULL,
49 : NULL,
50 : NULL,
51 : NULL,
52 : };
53 :
54 : static const struct usb_devno uscom_devs[] = {
55 : { USB_VENDOR_HP, USB_PRODUCT_HP_HPX9GP },
56 : { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTUSBM }
57 : };
58 :
59 : int uscom_match(struct device *, void *, void *);
60 : void uscom_attach(struct device *, struct device *, void *);
61 : int uscom_detach(struct device *, int);
62 :
63 : struct cfdriver uscom_cd = {
64 : NULL, "uscom", DV_DULL
65 : };
66 :
67 : const struct cfattach uscom_ca = {
68 : sizeof(struct uscom_softc), uscom_match, uscom_attach, uscom_detach
69 : };
70 :
71 : int
72 0 : uscom_match(struct device *parent, void *match, void *aux)
73 : {
74 0 : struct usb_attach_arg *uaa = aux;
75 :
76 0 : if (uaa->iface == NULL)
77 0 : return UMATCH_NONE;
78 :
79 0 : return (usb_lookup(uscom_devs, uaa->vendor, uaa->product) != NULL) ?
80 : UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
81 0 : }
82 :
83 : void
84 0 : uscom_attach(struct device *parent, struct device *self, void *aux)
85 : {
86 0 : struct uscom_softc *sc = (struct uscom_softc *)self;
87 0 : struct usb_attach_arg *uaa = aux;
88 0 : struct ucom_attach_args uca;
89 : usb_interface_descriptor_t *id;
90 : usb_endpoint_descriptor_t *ed;
91 : usbd_status error;
92 : int i;
93 :
94 0 : bzero(&uca, sizeof(uca));
95 0 : sc->sc_udev = uaa->device;
96 :
97 : /* get the first interface handle */
98 0 : error = usbd_device2interface_handle(sc->sc_udev, USCOM_IFACE_NO,
99 0 : &sc->sc_iface);
100 0 : if (error != 0) {
101 0 : printf("%s: could not get interface handle\n",
102 0 : sc->sc_dev.dv_xname);
103 0 : usbd_deactivate(sc->sc_udev);
104 0 : return;
105 : }
106 :
107 0 : id = usbd_get_interface_descriptor(sc->sc_iface);
108 :
109 0 : uca.bulkin = uca.bulkout = -1;
110 0 : for (i = 0; i < id->bNumEndpoints; i++) {
111 0 : ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
112 0 : if (ed == NULL) {
113 0 : printf("%s: no endpoint descriptor found for %d\n",
114 0 : sc->sc_dev.dv_xname, i);
115 0 : usbd_deactivate(sc->sc_udev);
116 0 : return;
117 : }
118 :
119 0 : if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
120 0 : UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
121 0 : uca.bulkin = ed->bEndpointAddress;
122 0 : else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
123 0 : UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
124 0 : uca.bulkout = ed->bEndpointAddress;
125 : }
126 :
127 0 : if (uca.bulkin == -1 || uca.bulkout == -1) {
128 0 : printf("%s: missing endpoint\n", sc->sc_dev.dv_xname);
129 0 : usbd_deactivate(sc->sc_udev);
130 0 : return;
131 : }
132 :
133 0 : uca.ibufsize = USCOMBUFSZ;
134 0 : uca.obufsize = USCOMBUFSZ;
135 0 : uca.ibufsizepad = USCOMBUFSZ;
136 0 : uca.opkthdrlen = 0;
137 0 : uca.device = sc->sc_udev;
138 0 : uca.iface = sc->sc_iface;
139 0 : uca.methods = &uscom_methods;
140 0 : uca.arg = sc;
141 0 : uca.info = NULL;
142 :
143 0 : sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
144 0 : }
145 :
146 : int
147 0 : uscom_detach(struct device *self, int flags)
148 : {
149 0 : struct uscom_softc *sc = (struct uscom_softc *)self;
150 : int rv = 0;
151 :
152 0 : if (sc->sc_subdev != NULL) {
153 0 : rv = config_detach(sc->sc_subdev, flags);
154 0 : sc->sc_subdev = NULL;
155 0 : }
156 :
157 0 : return (rv);
158 : }
|