xref: /freebsd/sys/dev/usb/serial/ugensa.c (revision ccbb355988afe3c303bba020ef96d11682d78903)
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 #define	UGENSA_PORT_MAX		8	/* exclusivly */
74 
75 enum {
76 	UGENSA_BULK_DT_WR,
77 	UGENSA_BULK_DT_RD,
78 	UGENSA_N_TRANSFER,
79 };
80 
81 struct ugensa_sub_softc {
82 	struct ucom_softc *sc_ucom_ptr;
83 	struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
84 };
85 
86 struct ugensa_softc {
87 	struct ucom_super_softc sc_super_ucom;
88 	struct ucom_softc sc_ucom[UGENSA_PORT_MAX];
89 	struct ugensa_sub_softc sc_sub[UGENSA_PORT_MAX];
90 
91 	struct mtx sc_mtx;
92 	uint8_t	sc_nports;
93 };
94 
95 /* prototypes */
96 
97 static device_probe_t ugensa_probe;
98 static device_attach_t ugensa_attach;
99 static device_detach_t ugensa_detach;
100 static void ugensa_free_softc(struct ugensa_softc *);
101 
102 static usb_callback_t ugensa_bulk_write_callback;
103 static usb_callback_t ugensa_bulk_read_callback;
104 
105 static void	ugensa_free(struct ucom_softc *);
106 static void	ugensa_start_read(struct ucom_softc *);
107 static void	ugensa_stop_read(struct ucom_softc *);
108 static void	ugensa_start_write(struct ucom_softc *);
109 static void	ugensa_stop_write(struct ucom_softc *);
110 static void	ugensa_poll(struct ucom_softc *ucom);
111 
112 static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
113 
114 	[UGENSA_BULK_DT_WR] = {
115 		.type = UE_BULK,
116 		.endpoint = UE_ADDR_ANY,
117 		.direction = UE_DIR_OUT,
118 		.bufsize = UGENSA_BUF_SIZE,
119 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
120 		.callback = &ugensa_bulk_write_callback,
121 	},
122 
123 	[UGENSA_BULK_DT_RD] = {
124 		.type = UE_BULK,
125 		.endpoint = UE_ADDR_ANY,
126 		.direction = UE_DIR_IN,
127 		.bufsize = UGENSA_BUF_SIZE,
128 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
129 		.callback = &ugensa_bulk_read_callback,
130 	},
131 };
132 
133 static const struct ucom_callback ugensa_callback = {
134 	.ucom_start_read = &ugensa_start_read,
135 	.ucom_stop_read = &ugensa_stop_read,
136 	.ucom_start_write = &ugensa_start_write,
137 	.ucom_stop_write = &ugensa_stop_write,
138 	.ucom_poll = &ugensa_poll,
139 	.ucom_free = &ugensa_free,
140 };
141 
142 static device_method_t ugensa_methods[] = {
143 	/* Device methods */
144 	DEVMETHOD(device_probe, ugensa_probe),
145 	DEVMETHOD(device_attach, ugensa_attach),
146 	DEVMETHOD(device_detach, ugensa_detach),
147 	DEVMETHOD_END
148 };
149 
150 static devclass_t ugensa_devclass;
151 
152 static driver_t ugensa_driver = {
153 	.name = "ugensa",
154 	.methods = ugensa_methods,
155 	.size = sizeof(struct ugensa_softc),
156 };
157 
158 /* Driver-info is max number of serial ports per interface */
159 static const STRUCT_USB_HOST_ID ugensa_devs[] = {
160 	{USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 1)},
161 	{USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 1)},
162 	{USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 1)},
163 	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 1)},
164 	{USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 3)},
165 };
166 
167 DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
168 MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
169 MODULE_DEPEND(ugensa, usb, 1, 1, 1);
170 MODULE_VERSION(ugensa, 1);
171 USB_PNP_HOST_INFO(ugensa_devs);
172 
173 static int
174 ugensa_probe(device_t dev)
175 {
176 	struct usb_attach_arg *uaa = device_get_ivars(dev);
177 
178 	if (uaa->usb_mode != USB_MODE_HOST) {
179 		return (ENXIO);
180 	}
181 	if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
182 		return (ENXIO);
183 	}
184 	if (uaa->info.bIfaceIndex != 0) {
185 		return (ENXIO);
186 	}
187 	return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
188 }
189 
190 static int
191 ugensa_attach(device_t dev)
192 {
193 	struct usb_attach_arg *uaa = device_get_ivars(dev);
194 	struct ugensa_softc *sc = device_get_softc(dev);
195 	struct ugensa_sub_softc *ssc;
196 	struct usb_interface *iface;
197 	struct usb_config xfer_config[UGENSA_N_TRANSFER];
198 	int32_t error;
199 	uint8_t iface_index;
200 	int x, maxports;
201 
202 	maxports = USB_GET_DRIVER_INFO(uaa);
203 	device_set_usb_desc(dev);
204 	mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
205 	ucom_ref(&sc->sc_super_ucom);
206 
207 	for (iface_index = UGENSA_IFACE_INDEX; iface_index < UGENSA_IFACE_MAX; iface_index++) {
208 
209 		iface = usbd_get_iface(uaa->device, iface_index);
210 		if (iface == NULL || iface->idesc->bInterfaceClass != UICLASS_VENDOR)
211 			/* Not a serial port, most likely a SD reader */
212 			continue;
213 
214 		/* Loop over all endpoints pairwise */
215 		for (x = 0; x < maxports && sc->sc_nports < UGENSA_PORT_MAX; x++) {
216 
217 			ssc = sc->sc_sub + sc->sc_nports;
218 			ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_nports;
219 
220 			memcpy(xfer_config, ugensa_xfer_config, sizeof ugensa_xfer_config);
221 			xfer_config[UGENSA_BULK_DT_RD].ep_index = x;
222 			xfer_config[UGENSA_BULK_DT_WR].ep_index = x;
223 
224 			error = usbd_transfer_setup(uaa->device,
225 			    &iface_index, ssc->sc_xfer, xfer_config,
226 			    UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
227 
228 			if (error) {
229 				if (x == 0) {
230 					device_printf(dev, "allocating USB "
231 					    "transfers failed (%d)\n", error);
232 					goto detach;
233 				}
234 				break;
235 			}
236 
237 			/* clear stall at first run */
238 			mtx_lock(&sc->sc_mtx);
239 			usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
240 			usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
241 			mtx_unlock(&sc->sc_mtx);
242 
243 			/* initialize port number */
244 			ssc->sc_ucom_ptr->sc_portno = sc->sc_nports;
245 			if (iface_index != uaa->info.bIfaceIndex) {
246 				usbd_set_parent_iface(uaa->device, iface_index,
247 				    uaa->info.bIfaceIndex);
248 			}
249 			sc->sc_nports++;
250 		}
251 	}
252 	device_printf(dev, "Found %d serial ports.\n", sc->sc_nports);
253 
254 	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_nports, sc,
255 	    &ugensa_callback, &sc->sc_mtx);
256 
257 	if (error) {
258 		DPRINTF("ucom attach failed\n");
259 		goto detach;
260 	}
261 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
262 
263 	return (0);			/* success */
264 
265 detach:
266 	ugensa_detach(dev);
267 	return (ENXIO);			/* failure */
268 }
269 
270 static int
271 ugensa_detach(device_t dev)
272 {
273 	struct ugensa_softc *sc = device_get_softc(dev);
274 	uint8_t x;
275 
276 	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
277 
278 	for (x = 0; x < sc->sc_nports; x++) {
279 		usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
280 	}
281 
282 	device_claim_softc(dev);
283 
284 	ugensa_free_softc(sc);
285 
286 	return (0);
287 }
288 
289 UCOM_UNLOAD_DRAIN(ugensa);
290 
291 static void
292 ugensa_free_softc(struct ugensa_softc *sc)
293 {
294 	if (ucom_unref(&sc->sc_super_ucom)) {
295 		mtx_destroy(&sc->sc_mtx);
296 		device_free_softc(sc);
297 	}
298 }
299 
300 static void
301 ugensa_free(struct ucom_softc *ucom)
302 {
303 	ugensa_free_softc(ucom->sc_parent);
304 }
305 
306 static void
307 ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
308 {
309 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
310 	struct usb_page_cache *pc;
311 	uint32_t actlen;
312 
313 	switch (USB_GET_STATE(xfer)) {
314 	case USB_ST_SETUP:
315 	case USB_ST_TRANSFERRED:
316 tr_setup:
317 		pc = usbd_xfer_get_frame(xfer, 0);
318 		if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
319 		    UGENSA_BUF_SIZE, &actlen)) {
320 			usbd_xfer_set_frame_len(xfer, 0, actlen);
321 			usbd_transfer_submit(xfer);
322 		}
323 		return;
324 
325 	default:			/* Error */
326 		if (error != USB_ERR_CANCELLED) {
327 			/* try to clear stall first */
328 			usbd_xfer_set_stall(xfer);
329 			goto tr_setup;
330 		}
331 		return;
332 	}
333 }
334 
335 static void
336 ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
337 {
338 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
339 	struct usb_page_cache *pc;
340 	int actlen;
341 
342 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
343 
344 	switch (USB_GET_STATE(xfer)) {
345 	case USB_ST_TRANSFERRED:
346 		pc = usbd_xfer_get_frame(xfer, 0);
347 		ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
348 
349 	case USB_ST_SETUP:
350 tr_setup:
351 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
352 		usbd_transfer_submit(xfer);
353 		return;
354 
355 	default:			/* Error */
356 		if (error != USB_ERR_CANCELLED) {
357 			/* try to clear stall first */
358 			usbd_xfer_set_stall(xfer);
359 			goto tr_setup;
360 		}
361 		return;
362 	}
363 }
364 
365 static void
366 ugensa_start_read(struct ucom_softc *ucom)
367 {
368 	struct ugensa_softc *sc = ucom->sc_parent;
369 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
370 
371 	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
372 }
373 
374 static void
375 ugensa_stop_read(struct ucom_softc *ucom)
376 {
377 	struct ugensa_softc *sc = ucom->sc_parent;
378 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
379 
380 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
381 }
382 
383 static void
384 ugensa_start_write(struct ucom_softc *ucom)
385 {
386 	struct ugensa_softc *sc = ucom->sc_parent;
387 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
388 
389 	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
390 }
391 
392 static void
393 ugensa_stop_write(struct ucom_softc *ucom)
394 {
395 	struct ugensa_softc *sc = ucom->sc_parent;
396 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
397 
398 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
399 }
400 
401 static void
402 ugensa_poll(struct ucom_softc *ucom)
403 {
404 	struct ugensa_softc *sc = ucom->sc_parent;
405 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
406 
407 	usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
408 }
409