summaryrefslogtreecommitdiffstats
path: root/sys/dev/usb/u3g.c
blob: 4fa50df92d8d94a0c10b5fed37c21ec548b103f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * Copyright (c) 2008 AnyWi Technologies
 *   Author: Andrea Guzzo <aguzzo@anywi.com>
 *   * based on uark.c 1.1 2006/08/14 08:30:22 jsg *
 *   * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk *
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/ioccom.h>
#include <sys/fcntl.h>
#include <sys/conf.h>
#include <sys/tty.h>
#include <sys/file.h>
#include <sys/selinfo.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>

#include <dev/usb/ucomvar.h>

#include "usbdevs.h"

#ifdef U3G_DEBUG
#define DPRINTFN(n, x)    do { if (u3gdebug > (n)) printf x; } while (0)
int    u3gtebug = 0;
#else
#define DPRINTFN(n, x)
#endif
#define DPRINTF(x) DPRINTFN(0, x)

#define U3GBUFSZ        1024
#define U3G_MAXPORTS           4

struct u3g_softc {
	struct ucom_softc           sc_ucom[U3G_MAXPORTS];;
	device_t                    sc_dev;
	usbd_device_handle          sc_udev;
	u_char                      sc_msr;
	u_char                      sc_lsr;
	u_char                      numports;

	usbd_interface_handle       sc_intr_iface;   /* interrupt interface */
#ifdef U3G_DEBUG
	int                         sc_intr_number;  /* interrupt number */
	usbd_pipe_handle            sc_intr_pipe;    /* interrupt pipe */
	u_char                      *sc_intr_buf;    /* interrupt buffer */
#endif
	int                         sc_isize;
};

struct ucom_callback u3g_callback = {
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
};

static const struct usb_devno u3g_devs[] = {
	/* OEM: Option */
	{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3G },
	{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GQUAD },
	{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GPLUS },
	{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAX36 },
	{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_VODAFONEMC3G },
	/* OEM: Huawei */
	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE },
	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 },
	/* OEM: Qualcomm */
	{ USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_CDMA_MSM },

	{ 0, 0 }
};

#ifdef U3G_DEBUG
static void
u3g_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
{
	struct u3g_softc *sc = (struct u3g_softc *)priv;
	device_printf(sc->sc_dev, "INTERRUPT CALLBACK\n");
}
#endif

static int
u3g_huawei_reinit(usbd_device_handle dev)
{
	/* The Huawei device presents itself as a umass device with Windows
	 * drivers on it. After installation of the driver, it reinits into a
	 * 3G serial device.
	 */
	usb_device_request_t req;
	usb_config_descriptor_t *cdesc;

	/* Get the config descriptor */
	cdesc = usbd_get_config_descriptor(dev);
	if (cdesc == NULL)
		return (UMATCH_NONE);

	/* One iface means umass mode, more than 1 (4 usually) means 3G mode */
	if (cdesc->bNumInterface > 1)
		return (UMATCH_VENDOR_PRODUCT);

	req.bmRequestType = UT_WRITE_DEVICE;
	req.bRequest = UR_SET_FEATURE;
	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
	USETW(req.wIndex, UHF_PORT_SUSPEND);
	USETW(req.wLength, 0);

	(void) usbd_do_request(dev, &req, 0);

	return UMATCH_NONE;	/* mismatch; it will be gone and reappear */
}

static int
u3g_match(device_t self)
{
	struct usb_attach_arg *uaa = device_get_ivars(self);

	if (uaa->iface != NULL)
		return (UMATCH_NONE);

	if (uaa->vendor == USB_VENDOR_HUAWEI)
		return u3g_huawei_reinit(uaa->device);

	if (usb_lookup(u3g_devs, uaa->vendor, uaa->product))
		return UMATCH_VENDOR_PRODUCT;
	
	return UMATCH_NONE;
}

static int
u3g_attach(device_t self)
{
	struct u3g_softc *sc = device_get_softc(self);
	struct usb_attach_arg *uaa = device_get_ivars(self);
	usbd_device_handle dev = uaa->device;
	usbd_interface_handle iface;
	usb_interface_descriptor_t *id;
	usb_endpoint_descriptor_t *ed;
	usbd_status error;
	int i, n; 
	usb_config_descriptor_t *cdesc;
	struct ucom_softc *ucom = NULL;
	char devnamefmt[32];

	sc->sc_dev = self;
#ifdef U3G_DEBUG
	sc->sc_intr_number = -1;
	sc->sc_intr_pipe = NULL;
#endif
	/* Move the device into the configured state. */
	error = usbd_set_config_index(dev, 1, 1);
	if (error) {
		device_printf(self, "failed to set configuration: %s\n",
			      usbd_errstr(error));
		goto bad;
	}

	/* get the config descriptor */
	cdesc = usbd_get_config_descriptor(dev);

	if (cdesc == NULL) {
		device_printf(self, "failed to get configuration descriptor\n");
		goto bad;
	}

	sc->sc_udev = dev;
	sc->numports = (cdesc->bNumInterface <= U3G_MAXPORTS)?cdesc->bNumInterface:U3G_MAXPORTS;
	for ( i = 0; i < sc->numports; i++ ) {
		ucom = &sc->sc_ucom[i];

		ucom->sc_dev = self;
		ucom->sc_udev = dev;
		error = usbd_device2interface_handle(dev, i, &iface);
		if (error) {
			device_printf(ucom->sc_dev,
				"failed to get interface, err=%s\n",
			usbd_errstr(error));
			ucom->sc_dying = 1;
			goto bad;
		}
		id = usbd_get_interface_descriptor(iface);
		ucom->sc_iface = iface;

		ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
		for (n = 0; n < id->bNumEndpoints; n++) {
			ed = usbd_interface2endpoint_descriptor(iface, n);
			if (ed == NULL) {
				device_printf(ucom->sc_dev,
					"could not read endpoint descriptor\n");
				goto bad;
			}
			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
				ucom->sc_bulkin_no = ed->bEndpointAddress;
			else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
				ucom->sc_bulkout_no = ed->bEndpointAddress;
		}
		if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
			device_printf(ucom->sc_dev, "missing endpoint\n");
			goto bad;
		}
		ucom->sc_parent = sc;
		ucom->sc_ibufsize = U3GBUFSZ;
		ucom->sc_obufsize = U3GBUFSZ;
		ucom->sc_ibufsizepad = U3GBUFSZ;
		ucom->sc_opkthdrlen = 0;

		ucom->sc_callback = &u3g_callback;

		sprintf(devnamefmt,"U%d.%%d", device_get_unit(self));
		DPRINTF(("u3g: in=0x%x out=0x%x, devname=%s\n",
			 ucom->sc_bulkin_no, ucom->sc_bulkout_no, devnamefmt));
#if __FreeBSD_version < 800000
		ucom_attach_tty(ucom, TS_CALLOUT, devnamefmt, i);
#else
		ucom_attach_tty(ucom, devnamefmt, i);
#endif
	}
#ifdef U3G_DEBUG
	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
		error = usbd_open_pipe_intr(sc->sc_intr_iface,
					    sc->sc_intr_number,
					    USBD_SHORT_XFER_OK,
					    &sc->sc_intr_pipe,
					    sc,
					    sc->sc_intr_buf,
					    sc->sc_isize,
					    u3g_intr,
					    100);
		if (error) {
		    device_printf(self,
			    "cannot open interrupt pipe (addr %d)\n",
			    sc->sc_intr_number);
		    goto bad;
		}
	}
#endif
	device_printf(self, "configured %d serial ports (/dev/cuaU%d.X)",
		      sc->numports, device_get_unit(self));

	return 0;

bad:
	DPRINTF(("u3g_attach: ATTACH ERROR\n"));
	ucom->sc_dying = 1;
	return ENXIO;
}

static int
u3g_detach(device_t self)
{
	struct u3g_softc *sc = device_get_softc(self);
	int rv = 0;
	int i;

	DPRINTF(("u3g_detach: sc=%p\n", sc));

	for (i = 0; i < sc->numports; i++) {
		if(sc->sc_ucom[i].sc_udev) {
			sc->sc_ucom[i].sc_dying = 1;
			rv = ucom_detach(&sc->sc_ucom[i]);
			if(rv != 0) {
				device_printf(self, "Can't deallocat port %d", i);
				return rv;
			}
		}
	}

#ifdef U3G_DEBUG
	if (sc->sc_intr_pipe != NULL) {
		int err = usbd_abort_pipe(sc->sc_intr_pipe);
		if (err)
			device_printf(self,
				"abort interrupt pipe failed: %s\n",
				usbd_errstr(err));
		err = usbd_close_pipe(sc->sc_intr_pipe);
		if (err)
			device_printf(self,
			    "close interrupt pipe failed: %s\n",
			    usbd_errstr(err));
		free(sc->sc_intr_buf, M_USBDEV);
		sc->sc_intr_pipe = NULL;
	}
#endif

	return 0;
}

static device_method_t u3g_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe, u3g_match),
	DEVMETHOD(device_attach, u3g_attach),
	DEVMETHOD(device_detach, u3g_detach),

	{ 0, 0 }
};

static driver_t u3g_driver = {
	"ucom",
	u3g_methods,
	sizeof (struct u3g_softc)
};

DRIVER_MODULE(u3g, uhub, u3g_driver, ucom_devclass, usbd_driver_load, 0);
MODULE_DEPEND(u3g, usb, 1, 1, 1);
MODULE_DEPEND(u3g, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
OpenPOWER on IntegriCloud