xref: /freebsd/sys/dev/usb/serial/ugensa.c (revision 0957b409)
1 /* $FreeBSD$ */
2 /*	$NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6  *
7  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Roland C. Dowdeswell <elric@netbsd.org>.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * NOTE: all function names beginning like "ugensa_cfg_" can only
37  * be called from within the config thread function !
38  */
39 
40 #include <sys/stdint.h>
41 #include <sys/stddef.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bus.h>
48 #include <sys/module.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/condvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/sx.h>
54 #include <sys/unistd.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
57 #include <sys/priv.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include "usbdevs.h"
62 
63 #define	USB_DEBUG_VAR usb_debug
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_process.h>
66 
67 #include <dev/usb/serial/usb_serial.h>
68 
69 #define	UGENSA_BUF_SIZE		2048	/* bytes */
70 #define	UGENSA_CONFIG_INDEX	0
71 #define	UGENSA_IFACE_INDEX	0
72 #define	UGENSA_IFACE_MAX	8	/* exclusivly */
73 
74 enum {
75 	UGENSA_BULK_DT_WR,
76 	UGENSA_BULK_DT_RD,
77 	UGENSA_N_TRANSFER,
78 };
79 
80 struct ugensa_sub_softc {
81 	struct ucom_softc *sc_ucom_ptr;
82 	struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
83 };
84 
85 struct ugensa_softc {
86 	struct ucom_super_softc sc_super_ucom;
87 	struct ucom_softc sc_ucom[UGENSA_IFACE_MAX];
88 	struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX];
89 
90 	struct mtx sc_mtx;
91 	uint8_t	sc_niface;
92 };
93 
94 /* prototypes */
95 
96 static device_probe_t ugensa_probe;
97 static device_attach_t ugensa_attach;
98 static device_detach_t ugensa_detach;
99 static void ugensa_free_softc(struct ugensa_softc *);
100 
101 static usb_callback_t ugensa_bulk_write_callback;
102 static usb_callback_t ugensa_bulk_read_callback;
103 
104 static void	ugensa_free(struct ucom_softc *);
105 static void	ugensa_start_read(struct ucom_softc *);
106 static void	ugensa_stop_read(struct ucom_softc *);
107 static void	ugensa_start_write(struct ucom_softc *);
108 static void	ugensa_stop_write(struct ucom_softc *);
109 static void	ugensa_poll(struct ucom_softc *ucom);
110 
111 static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
112 
113 	[UGENSA_BULK_DT_WR] = {
114 		.type = UE_BULK,
115 		.endpoint = UE_ADDR_ANY,
116 		.direction = UE_DIR_OUT,
117 		.bufsize = UGENSA_BUF_SIZE,
118 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
119 		.callback = &ugensa_bulk_write_callback,
120 	},
121 
122 	[UGENSA_BULK_DT_RD] = {
123 		.type = UE_BULK,
124 		.endpoint = UE_ADDR_ANY,
125 		.direction = UE_DIR_IN,
126 		.bufsize = UGENSA_BUF_SIZE,
127 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
128 		.callback = &ugensa_bulk_read_callback,
129 	},
130 };
131 
132 static const struct ucom_callback ugensa_callback = {
133 	.ucom_start_read = &ugensa_start_read,
134 	.ucom_stop_read = &ugensa_stop_read,
135 	.ucom_start_write = &ugensa_start_write,
136 	.ucom_stop_write = &ugensa_stop_write,
137 	.ucom_poll = &ugensa_poll,
138 	.ucom_free = &ugensa_free,
139 };
140 
141 static device_method_t ugensa_methods[] = {
142 	/* Device methods */
143 	DEVMETHOD(device_probe, ugensa_probe),
144 	DEVMETHOD(device_attach, ugensa_attach),
145 	DEVMETHOD(device_detach, ugensa_detach),
146 	DEVMETHOD_END
147 };
148 
149 static devclass_t ugensa_devclass;
150 
151 static driver_t ugensa_driver = {
152 	.name = "ugensa",
153 	.methods = ugensa_methods,
154 	.size = sizeof(struct ugensa_softc),
155 };
156 
157 static const STRUCT_USB_HOST_ID ugensa_devs[] = {
158 	{USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
159 	{USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
160 	{USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
161 	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
162 	{USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
163 };
164 
165 DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
166 MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
167 MODULE_DEPEND(ugensa, usb, 1, 1, 1);
168 MODULE_VERSION(ugensa, 1);
169 USB_PNP_HOST_INFO(ugensa_devs);
170 
171 static int
172 ugensa_probe(device_t dev)
173 {
174 	struct usb_attach_arg *uaa = device_get_ivars(dev);
175 
176 	if (uaa->usb_mode != USB_MODE_HOST) {
177 		return (ENXIO);
178 	}
179 	if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
180 		return (ENXIO);
181 	}
182 	if (uaa->info.bIfaceIndex != 0) {
183 		return (ENXIO);
184 	}
185 	return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
186 }
187 
188 static int
189 ugensa_attach(device_t dev)
190 {
191 	struct usb_attach_arg *uaa = device_get_ivars(dev);
192 	struct ugensa_softc *sc = device_get_softc(dev);
193 	struct ugensa_sub_softc *ssc;
194 	struct usb_interface *iface;
195 	int32_t error;
196 	uint8_t iface_index;
197 	int x, cnt;
198 
199 	device_set_usb_desc(dev);
200 	mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
201 	ucom_ref(&sc->sc_super_ucom);
202 
203 	/* Figure out how many interfaces this device has got */
204 	for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
205 		if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
206 		    (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
207 			/* we have reached the end */
208 			break;
209 		}
210 	}
211 
212 	if (cnt == 0) {
213 		device_printf(dev, "No interfaces\n");
214 		goto detach;
215 	}
216 	for (x = 0; x < cnt; x++) {
217 		iface = usbd_get_iface(uaa->device, x);
218 		if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
219 			/* Not a serial port, most likely a SD reader */
220 			continue;
221 
222 		ssc = sc->sc_sub + sc->sc_niface;
223 		ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
224 
225 		iface_index = (UGENSA_IFACE_INDEX + x);
226 		error = usbd_transfer_setup(uaa->device,
227 		    &iface_index, ssc->sc_xfer, ugensa_xfer_config,
228 		    UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
229 
230 		if (error) {
231 			device_printf(dev, "allocating USB "
232 			    "transfers failed\n");
233 			goto detach;
234 		}
235 		/* clear stall at first run */
236 		mtx_lock(&sc->sc_mtx);
237 		usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
238 		usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
239 		mtx_unlock(&sc->sc_mtx);
240 
241 		/* initialize port number */
242 		ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
243 		sc->sc_niface++;
244 		if (x != uaa->info.bIfaceIndex)
245 			usbd_set_parent_iface(uaa->device, x,
246 			    uaa->info.bIfaceIndex);
247 	}
248 	device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
249 
250 	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
251 	    &ugensa_callback, &sc->sc_mtx);
252 	if (error) {
253 		DPRINTF("attach failed\n");
254 		goto detach;
255 	}
256 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
257 
258 	return (0);			/* success */
259 
260 detach:
261 	ugensa_detach(dev);
262 	return (ENXIO);			/* failure */
263 }
264 
265 static int
266 ugensa_detach(device_t dev)
267 {
268 	struct ugensa_softc *sc = device_get_softc(dev);
269 	uint8_t x;
270 
271 	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
272 
273 	for (x = 0; x < sc->sc_niface; x++) {
274 		usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
275 	}
276 
277 	device_claim_softc(dev);
278 
279 	ugensa_free_softc(sc);
280 
281 	return (0);
282 }
283 
284 UCOM_UNLOAD_DRAIN(ugensa);
285 
286 static void
287 ugensa_free_softc(struct ugensa_softc *sc)
288 {
289 	if (ucom_unref(&sc->sc_super_ucom)) {
290 		mtx_destroy(&sc->sc_mtx);
291 		device_free_softc(sc);
292 	}
293 }
294 
295 static void
296 ugensa_free(struct ucom_softc *ucom)
297 {
298 	ugensa_free_softc(ucom->sc_parent);
299 }
300 
301 static void
302 ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
303 {
304 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
305 	struct usb_page_cache *pc;
306 	uint32_t actlen;
307 
308 	switch (USB_GET_STATE(xfer)) {
309 	case USB_ST_SETUP:
310 	case USB_ST_TRANSFERRED:
311 tr_setup:
312 		pc = usbd_xfer_get_frame(xfer, 0);
313 		if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
314 		    UGENSA_BUF_SIZE, &actlen)) {
315 			usbd_xfer_set_frame_len(xfer, 0, actlen);
316 			usbd_transfer_submit(xfer);
317 		}
318 		return;
319 
320 	default:			/* Error */
321 		if (error != USB_ERR_CANCELLED) {
322 			/* try to clear stall first */
323 			usbd_xfer_set_stall(xfer);
324 			goto tr_setup;
325 		}
326 		return;
327 	}
328 }
329 
330 static void
331 ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
332 {
333 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
334 	struct usb_page_cache *pc;
335 	int actlen;
336 
337 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
338 
339 	switch (USB_GET_STATE(xfer)) {
340 	case USB_ST_TRANSFERRED:
341 		pc = usbd_xfer_get_frame(xfer, 0);
342 		ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
343 
344 	case USB_ST_SETUP:
345 tr_setup:
346 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
347 		usbd_transfer_submit(xfer);
348 		return;
349 
350 	default:			/* Error */
351 		if (error != USB_ERR_CANCELLED) {
352 			/* try to clear stall first */
353 			usbd_xfer_set_stall(xfer);
354 			goto tr_setup;
355 		}
356 		return;
357 	}
358 }
359 
360 static void
361 ugensa_start_read(struct ucom_softc *ucom)
362 {
363 	struct ugensa_softc *sc = ucom->sc_parent;
364 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
365 
366 	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
367 }
368 
369 static void
370 ugensa_stop_read(struct ucom_softc *ucom)
371 {
372 	struct ugensa_softc *sc = ucom->sc_parent;
373 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
374 
375 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
376 }
377 
378 static void
379 ugensa_start_write(struct ucom_softc *ucom)
380 {
381 	struct ugensa_softc *sc = ucom->sc_parent;
382 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
383 
384 	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
385 }
386 
387 static void
388 ugensa_stop_write(struct ucom_softc *ucom)
389 {
390 	struct ugensa_softc *sc = ucom->sc_parent;
391 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
392 
393 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
394 }
395 
396 static void
397 ugensa_poll(struct ucom_softc *ucom)
398 {
399 	struct ugensa_softc *sc = ucom->sc_parent;
400 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
401 
402 	usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
403 }
404