1 | /* $NetBSD: ugensa.c,v 1.33 2016/07/07 06:55:42 msaitoh Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc. |
5 | * All rights reserved. |
6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Roland C. Dowdeswell <elric@netbsd.org>. |
9 | * |
10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions |
12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #include <sys/cdefs.h> |
33 | __KERNEL_RCSID(0, "$NetBSD: ugensa.c,v 1.33 2016/07/07 06:55:42 msaitoh Exp $" ); |
34 | |
35 | #include <sys/param.h> |
36 | #include <sys/systm.h> |
37 | #include <sys/kernel.h> |
38 | #include <sys/device.h> |
39 | #include <sys/conf.h> |
40 | #include <sys/tty.h> |
41 | |
42 | #include <dev/usb/usb.h> |
43 | |
44 | #include <dev/usb/usbdi.h> |
45 | #include <dev/usb/usbdi_util.h> |
46 | #include <dev/usb/usbdevs.h> |
47 | |
48 | #include <dev/usb/ucomvar.h> |
49 | |
50 | /* XXXrcd: heh */ |
51 | #define UGENSA_DEBUG 1 |
52 | |
53 | #ifdef UGENSA_DEBUG |
54 | #define DPRINTF(x) if (ugensadebug) printf x |
55 | #define DPRINTFN(n,x) if (ugensadebug>(n)) printf x |
56 | int ugensadebug = 0; |
57 | #else |
58 | #define DPRINTF(x) |
59 | #define DPRINTFN(n,x) |
60 | #endif |
61 | |
62 | struct ugensa_softc { |
63 | device_t sc_dev; /* base device */ |
64 | struct usbd_device * sc_udev; /* device */ |
65 | struct usbd_interface * sc_iface; /* interface */ |
66 | |
67 | device_t sc_subdev; |
68 | int sc_numcon; |
69 | |
70 | u_char sc_dying; |
71 | }; |
72 | |
73 | struct ucom_methods ugensa_methods = { |
74 | .ucom_get_status = NULL, |
75 | .ucom_set = NULL, |
76 | .ucom_param = NULL, |
77 | .ucom_ioctl = NULL, |
78 | .ucom_open = NULL, |
79 | .ucom_close = NULL, |
80 | .ucom_read = NULL, |
81 | .ucom_write = NULL, |
82 | }; |
83 | |
84 | #define UGENSA_CONFIG_INDEX 0 |
85 | #define UGENSA_IFACE_INDEX 0 |
86 | #define UGENSA_BUFSIZE 1024 |
87 | |
88 | struct ugensa_type { |
89 | struct usb_devno ugensa_dev; |
90 | uint16_t ugensa_flags; |
91 | #define UNTESTED 0x0001 |
92 | }; |
93 | |
94 | static const struct ugensa_type ugensa_devs[] = { |
95 | {{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220 }, 0 }, |
96 | {{ USB_VENDOR_DELL, USB_PRODUCT_DELL_HSDPA }, 0 }, |
97 | {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_FLEXPACKGPS }, 0 }, |
98 | {{ USB_VENDOR_QUALCOMM_K, USB_PRODUCT_QUALCOMM_K_CDMA_MSM_K }, 0 }, |
99 | {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_USB305 }, 0 }, |
100 | {{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_AC8700 }, 0 }, |
101 | |
102 | /* |
103 | * The following devices are untested, but they are purported to |
104 | * to work in similar device drivers on other OSes: |
105 | */ |
106 | |
107 | {{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A }, UNTESTED }, |
108 | {{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EXPRESSCARD }, UNTESTED }, |
109 | {{ USB_VENDOR_QUALCOMM, USB_PRODUCT_QUALCOMM_MSM_HSDPA }, UNTESTED }, |
110 | {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, UNTESTED }, |
111 | {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, UNTESTED }, |
112 | }; |
113 | #define ugensa_lookup(v, p) \ |
114 | ((const struct ugensa_type *)usb_lookup(ugensa_devs, v, p)) |
115 | |
116 | int ugensa_match(device_t, cfdata_t, void *); |
117 | void ugensa_attach(device_t, device_t, void *); |
118 | void ugensa_childdet(device_t, device_t); |
119 | int ugensa_detach(device_t, int); |
120 | int ugensa_activate(device_t, enum devact); |
121 | extern struct cfdriver ugensa_cd; |
122 | CFATTACH_DECL2_NEW(ugensa, sizeof(struct ugensa_softc), ugensa_match, |
123 | ugensa_attach, ugensa_detach, ugensa_activate, NULL, ugensa_childdet); |
124 | |
125 | int |
126 | ugensa_match(device_t parent, cfdata_t match, void *aux) |
127 | { |
128 | struct usb_attach_arg *uaa = aux; |
129 | |
130 | DPRINTFN(20,("ugensa: vendor=0x%x, product=0x%x\n" , |
131 | uaa->uaa_vendor, uaa->uaa_product)); |
132 | |
133 | return ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? |
134 | UMATCH_VENDOR_PRODUCT : UMATCH_NONE; |
135 | } |
136 | |
137 | void |
138 | ugensa_attach(device_t parent, device_t self, void *aux) |
139 | { |
140 | struct ugensa_softc *sc = device_private(self); |
141 | struct usb_attach_arg *uaa = aux; |
142 | struct usbd_device *dev = uaa->uaa_device; |
143 | struct usbd_interface *iface; |
144 | usb_interface_descriptor_t *id; |
145 | usb_endpoint_descriptor_t *ed; |
146 | char *devinfop; |
147 | const char *devname = device_xname(self); |
148 | usbd_status err; |
149 | struct ucom_attach_args ucaa; |
150 | int i; |
151 | |
152 | DPRINTFN(10,("\nugensa_attach: sc=%p\n" , sc)); |
153 | |
154 | sc->sc_dev = self; |
155 | |
156 | aprint_naive("\n" ); |
157 | aprint_normal("\n" ); |
158 | |
159 | devinfop = usbd_devinfo_alloc(dev, 0); |
160 | aprint_normal_dev(self, "%s\n" , devinfop); |
161 | usbd_devinfo_free(devinfop); |
162 | |
163 | /* Move the device into the configured state. */ |
164 | err = usbd_set_config_index(dev, UGENSA_CONFIG_INDEX, 1); |
165 | if (err) { |
166 | aprint_error("\n%s: failed to set configuration, err=%s\n" , |
167 | devname, usbd_errstr(err)); |
168 | goto bad; |
169 | } |
170 | |
171 | err = usbd_device2interface_handle(dev, UGENSA_IFACE_INDEX, &iface); |
172 | if (err) { |
173 | aprint_error("\n%s: failed to get interface, err=%s\n" , |
174 | devname, usbd_errstr(err)); |
175 | goto bad; |
176 | } |
177 | |
178 | if (ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product)->ugensa_flags & UNTESTED) |
179 | aprint_normal_dev(self, "WARNING: This device is marked as " |
180 | "untested. Please submit a report via send-pr(1).\n" ); |
181 | |
182 | id = usbd_get_interface_descriptor(iface); |
183 | |
184 | sc->sc_udev = dev; |
185 | sc->sc_iface = iface; |
186 | |
187 | ucaa.ucaa_info = "Generic Serial Device" ; |
188 | ucaa.ucaa_ibufsize = UGENSA_BUFSIZE; |
189 | ucaa.ucaa_obufsize = UGENSA_BUFSIZE; |
190 | ucaa.ucaa_ibufsizepad = UGENSA_BUFSIZE; |
191 | ucaa.ucaa_portno = UCOM_UNK_PORTNO; |
192 | ucaa.ucaa_opkthdrlen = 0; |
193 | ucaa.ucaa_device = dev; |
194 | ucaa.ucaa_iface = iface; |
195 | ucaa.ucaa_methods = &ugensa_methods; |
196 | ucaa.ucaa_arg = sc; |
197 | |
198 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); |
199 | |
200 | ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; |
201 | for (i = 0; i < id->bNumEndpoints; i++) { |
202 | int addr, dir, attr; |
203 | |
204 | ed = usbd_interface2endpoint_descriptor(iface, i); |
205 | if (ed == NULL) { |
206 | aprint_error_dev(self, |
207 | "could not read endpoint descriptor: %s\n" , |
208 | usbd_errstr(err)); |
209 | goto bad; |
210 | } |
211 | |
212 | addr = ed->bEndpointAddress; |
213 | dir = UE_GET_DIR(ed->bEndpointAddress); |
214 | attr = ed->bmAttributes & UE_XFERTYPE; |
215 | if (attr == UE_BULK) { |
216 | if (ucaa.ucaa_bulkin == -1 && dir == UE_DIR_IN) { |
217 | DPRINTF(("%s: Bulk in %d\n" , devname, i)); |
218 | ucaa.ucaa_bulkin = addr; |
219 | continue; |
220 | } |
221 | if (ucaa.ucaa_bulkout == -1 && dir == UE_DIR_OUT) { |
222 | DPRINTF(("%s: Bulk out %d\n" , devname, i)); |
223 | ucaa.ucaa_bulkout = addr; |
224 | continue; |
225 | } |
226 | } |
227 | aprint_error_dev(self, "unexpected endpoint\n" ); |
228 | } |
229 | if (ucaa.ucaa_bulkin == -1) { |
230 | aprint_error_dev(self, "Could not find data bulk in\n" ); |
231 | goto bad; |
232 | } |
233 | if (ucaa.ucaa_bulkout == -1) { |
234 | aprint_error_dev(self, "Could not find data bulk out\n" ); |
235 | goto bad; |
236 | } |
237 | |
238 | DPRINTF(("ugensa: in=0x%x out=0x%x\n" , ucaa.ucaa_bulkin, |
239 | ucaa.ucaa_bulkout)); |
240 | sc->sc_subdev = config_found_sm_loc(self, "ucombus" , NULL, &ucaa, |
241 | ucomprint, ucomsubmatch); |
242 | |
243 | if (!pmf_device_register(self, NULL, NULL)) |
244 | aprint_error_dev(self, "couldn't establish power handler\n" ); |
245 | return; |
246 | |
247 | bad: |
248 | DPRINTF(("ugensa_attach: ATTACH ERROR\n" )); |
249 | sc->sc_dying = 1; |
250 | return; |
251 | } |
252 | |
253 | void |
254 | ugensa_childdet(device_t self, device_t child) |
255 | { |
256 | struct ugensa_softc *sc = device_private(self); |
257 | |
258 | KASSERT(sc->sc_subdev == child); |
259 | sc->sc_subdev = NULL; |
260 | } |
261 | |
262 | int |
263 | ugensa_activate(device_t self, enum devact act) |
264 | { |
265 | struct ugensa_softc *sc = device_private(self); |
266 | |
267 | DPRINTF(("ugensa_activate: sc=%p\n" , sc)); |
268 | |
269 | switch (act) { |
270 | case DVACT_DEACTIVATE: |
271 | sc->sc_dying = 1; |
272 | return 0; |
273 | default: |
274 | return EOPNOTSUPP; |
275 | } |
276 | } |
277 | |
278 | int |
279 | ugensa_detach(device_t self, int flags) |
280 | { |
281 | struct ugensa_softc *sc = device_private(self); |
282 | int rv = 0; |
283 | |
284 | DPRINTF(("ugensa_detach: sc=%p flags=%d\n" , sc, flags)); |
285 | |
286 | sc->sc_dying = 1; |
287 | pmf_device_deregister(self); |
288 | |
289 | if (sc->sc_subdev != NULL) |
290 | rv = config_detach(sc->sc_subdev, flags); |
291 | |
292 | usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); |
293 | |
294 | return rv; |
295 | } |
296 | |