zte_ev.c 8.6 KB
Newer Older
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
/*
 * ZTE_EV USB serial driver
 *
 * Copyright (C) 2012 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
 * Copyright (C) 2012 Linux Foundation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This driver is based on code found in a ZTE_ENV patch that modified
 * the usb-serial generic driver.  Comments were left in that I think
 * show the commands used to talk to the device, but I am not sure.
 */
#include <linux/kernel.h>
#include <linux/tty.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
#include <linux/uaccess.h>

#define  MAX_SETUP_DATA_SIZE	32

static void debug_data(struct device *dev, const char *function, int len,
		       const unsigned char *data, int result)
{
	dev_dbg(dev, "result = %d\n", result);
	if (result == len)
		dev_dbg(dev, "%s - length = %d, data = %*ph\n", function,
			len, len, data);
}

static int zte_ev_usb_serial_open(struct tty_struct *tty,
				  struct usb_serial_port *port)
{
	struct usb_device *udev = port->serial->dev;
	struct device *dev = &port->dev;
	int result = 0;
	int len;
	unsigned char *buf;

	buf = kmalloc(MAX_SETUP_DATA_SIZE, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	/* send 1st ctl cmd(CTL    21 22 01 00  00 00 00 00) */
	len = 0;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x22, 0x21,
				 0x0001, 0x0000, NULL, len,
52
				 USB_CTRL_GET_TIMEOUT);
53 54
	dev_dbg(dev, "result = %d\n", result);

55
	/* send 2st cmd and receive data */
56 57 58 59 60 61 62 63
	/*
	 * 16.0  CTL    a1 21 00 00  00 00 07 00   CLASS              25.1.0(5)
	 * 16.0  DI     00 96 00 00  00 00 08
	 */
	len = 0x0007;
	result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
				 0x21, 0xa1,
				 0x0000, 0x0000, buf, len,
64
				 USB_CTRL_GET_TIMEOUT);
65 66
	debug_data(dev, __func__, len, buf, result);

67
	/* send 3rd cmd */
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	/*
	 * 16.0 CTL    21 20 00 00  00 00 07 00    CLASS                30.1.0
	 * 16.0 DO     80 25 00 00  00 00 08       .%.....              30.2.0
	 */
	len = 0x0007;
	buf[0] = 0x80;
	buf[1] = 0x25;
	buf[2] = 0x00;
	buf[3] = 0x00;
	buf[4] = 0x00;
	buf[5] = 0x00;
	buf[6] = 0x08;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x20, 0x21,
				 0x0000, 0x0000, buf, len,
83
				 USB_CTRL_GET_TIMEOUT);
84 85
	debug_data(dev, __func__, len, buf, result);

86
	/* send 4th cmd */
87 88 89 90 91 92 93
	/*
	 * 16.0 CTL    21 22 03 00  00 00 00 00
	 */
	len = 0;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x22, 0x21,
				 0x0003, 0x0000, NULL, len,
94
				 USB_CTRL_GET_TIMEOUT);
95 96
	dev_dbg(dev, "result = %d\n", result);

97
	/* send 5th cmd */
98 99 100 101 102 103 104 105
	/*
	 * 16.0  CTL    a1 21 00 00  00 00 07 00   CLASS               33.1.0
	 * 16.0  DI     80 25 00 00  00 00 08
	 */
	len = 0x0007;
	result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
				 0x21, 0xa1,
				 0x0000, 0x0000, buf, len,
106
				 USB_CTRL_GET_TIMEOUT);
107 108
	debug_data(dev, __func__, len, buf, result);

109
	/* send 6th cmd */
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	/*
	 * 16.0  CTL    21 20 00 00  00 00 07 00    CLASS               34.1.0
	 * 16.0  DO     80 25 00 00  00 00 08
	 */
	len = 0x0007;
	buf[0] = 0x80;
	buf[1] = 0x25;
	buf[2] = 0x00;
	buf[3] = 0x00;
	buf[4] = 0x00;
	buf[5] = 0x00;
	buf[6] = 0x08;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x20, 0x21,
				 0x0000, 0x0000, buf, len,
125
				 USB_CTRL_GET_TIMEOUT);
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
	debug_data(dev, __func__, len, buf, result);
	kfree(buf);

	return usb_serial_generic_open(tty, port);
}

/*
 *       CTL    21 22 02 00  00 00 00 00         CLASS               338.1.0
 *
 * 16.1  DI     a1 20 00 00  00 00 02 00  02 00  . ........          340.1.0
 * 16.0  CTL    21 22 03 00  00 00 00 00         CLASS               341.1.0
 *
 * 16.0  CTL    a1 21 00 00  00 00 07 00         CLASS               346.1.0(3)
 * 16.0  DI     00 08 07 00  00 00 08            .......             346.2.0
 *
 * 16.0  CTL    21 20 00 00  00 00 07 00         CLASS               349.1.0
 * 16.0  DO     00 c2 01 00  00 00 08            .......             349.2.0
 *
 * 16.0  CTL    21 22 03 00  00 00 00 00         CLASS               350.1.0(2)
 *
 * 16.0  CTL    a1 21 00 00  00 00 07 00         CLASS               352.1.0
 * 16.0  DI     00 c2 01 00  00 00 08            .......             352.2.0
 *
 * 16.1  DI     a1 20 00 00  00 00 02 00  02 00  . ........          353.1.0
 *
 * 16.0  CTL    21 20 00 00  00 00 07 00         CLASS               354.1.0
 * 16.0  DO     00 c2 01 00  00 00 08            .......             354.2.0
 *
 * 16.0  CTL    21 22 03 00  00 00 00 00
*/

static void zte_ev_usb_serial_close(struct usb_serial_port *port)
{
	struct usb_device *udev = port->serial->dev;
	struct device *dev = &port->dev;
	int result = 0;
	int len;
	unsigned char *buf;

	buf = kmalloc(MAX_SETUP_DATA_SIZE, GFP_KERNEL);
	if (!buf)
		return;

	/* send 1st ctl cmd(CTL    21 22 02 00  00 00 00 00) */
	len = 0;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x22, 0x21,
				 0x0002, 0x0000, NULL, len,
174
				 USB_CTRL_GET_TIMEOUT);
175 176 177 178 179 180 181
	dev_dbg(dev, "result = %d\n", result);

	/* send 2st ctl cmd(CTL    21 22 03 00  00 00 00 00 ) */
	len = 0;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x22, 0x21,
				 0x0003, 0x0000, NULL, len,
182
				 USB_CTRL_GET_TIMEOUT);
183 184 185 186 187 188 189 190 191 192 193
	dev_dbg(dev, "result = %d\n", result);

	/* send  3st cmd and recieve data */
	/*
	 * 16.0  CTL    a1 21 00 00  00 00 07 00      CLASS         25.1.0(5)
	 * 16.0  DI     00 08 07 00  00 00 08
	 */
	len = 0x0007;
	result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
				 0x21, 0xa1,
				 0x0000, 0x0000, buf, len,
194
				 USB_CTRL_GET_TIMEOUT);
195 196
	debug_data(dev, __func__, len, buf, result);

197
	/* send 4th cmd */
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	/*
	 * 16.0 CTL    21 20 00 00  00 00 07 00      CLASS            30.1.0
	 * 16.0  DO    00 c2 01 00  00 00 08         .%.....          30.2.0
	 */
	len = 0x0007;
	buf[0] = 0x00;
	buf[1] = 0xc2;
	buf[2] = 0x01;
	buf[3] = 0x00;
	buf[4] = 0x00;
	buf[5] = 0x00;
	buf[6] = 0x08;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x20, 0x21,
				 0x0000, 0x0000, buf, len,
213
				 USB_CTRL_GET_TIMEOUT);
214 215
	debug_data(dev, __func__, len, buf, result);

216
	/* send 5th cmd */
217 218 219 220 221 222 223
	/*
	 * 16.0 CTL    21 22 03 00  00 00 00 00
	 */
	len = 0;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x22, 0x21,
				 0x0003, 0x0000, NULL, len,
224
				 USB_CTRL_GET_TIMEOUT);
225 226
	dev_dbg(dev, "result = %d\n", result);

227
	/* send 6th cmd */
228 229 230 231 232 233 234 235
	/*
	 * 16.0  CTL    a1 21 00 00  00 00 07 00        CLASS          33.1.0
	 * 16.0  DI     00 c2 01 00  00 00 08
	 */
	len = 0x0007;
	result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
				 0x21, 0xa1,
				 0x0000, 0x0000, buf, len,
236
				 USB_CTRL_GET_TIMEOUT);
237 238
	debug_data(dev, __func__, len, buf, result);

239
	/* send 7th cmd */
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	/*
	 * 16.0  CTL    21 20 00 00  00 00 07 00  CLASS               354.1.0
	 * 16.0  DO     00 c2 01 00  00 00 08     .......             354.2.0
	 */
	len = 0x0007;
	buf[0] = 0x00;
	buf[1] = 0xc2;
	buf[2] = 0x01;
	buf[3] = 0x00;
	buf[4] = 0x00;
	buf[5] = 0x00;
	buf[6] = 0x08;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x20, 0x21,
				 0x0000, 0x0000, buf, len,
255
				 USB_CTRL_GET_TIMEOUT);
256 257
	debug_data(dev, __func__, len, buf, result);

258
	/* send 8th cmd */
259 260 261 262 263 264 265
	/*
	 * 16.0 CTL    21 22 03 00  00 00 00 00
	 */
	len = 0;
	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				 0x22, 0x21,
				 0x0003, 0x0000, NULL, len,
266
				 USB_CTRL_GET_TIMEOUT);
267 268 269 270 271 272 273 274
	dev_dbg(dev, "result = %d\n", result);

	kfree(buf);

	usb_serial_generic_close(port);
}

static const struct usb_device_id id_table[] = {
275 276 277 278 279 280 281 282
	/* AC8710, AC8710T */
	{ USB_DEVICE_AND_INTERFACE_INFO(0x19d2, 0xffff, 0xff, 0xff, 0xff) },
	 /* AC8700 */
	{ USB_DEVICE_AND_INTERFACE_INFO(0x19d2, 0xfffe, 0xff, 0xff, 0xff) },
	/* MG880 */
	{ USB_DEVICE(0x19d2, 0xfffd) },
	{ USB_DEVICE(0x19d2, 0xfffc) },
	{ USB_DEVICE(0x19d2, 0xfffb) },
283
	/* AC8710_V3 */
284 285 286 287 288 289 290 291 292 293
	{ USB_DEVICE(0x19d2, 0xfff6) },
	{ USB_DEVICE(0x19d2, 0xfff7) },
	{ USB_DEVICE(0x19d2, 0xfff8) },
	{ USB_DEVICE(0x19d2, 0xfff9) },
	{ USB_DEVICE(0x19d2, 0xffee) },
	/* AC2716, MC2716 */
	{ USB_DEVICE_AND_INTERFACE_INFO(0x19d2, 0xffed, 0xff, 0xff, 0xff) },
	/* AD3812 */
	{ USB_DEVICE_AND_INTERFACE_INFO(0x19d2, 0xffeb, 0xff, 0xff, 0xff) },
	{ USB_DEVICE(0x19d2, 0xffec) },
294 295
	{ USB_DEVICE(0x05C6, 0x3197) },
	{ USB_DEVICE(0x05C6, 0x6000) },
296
	{ USB_DEVICE(0x05C6, 0x9008) },
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	{ },
};
MODULE_DEVICE_TABLE(usb, id_table);

static struct usb_serial_driver zio_device = {
	.driver = {
		.owner =	THIS_MODULE,
		.name =		"zte_ev",
	},
	.id_table =		id_table,
	.num_ports =		1,
	.open =			zte_ev_usb_serial_open,
	.close =		zte_ev_usb_serial_close,
};

static struct usb_serial_driver * const serial_drivers[] = {
	&zio_device, NULL
};

module_usb_serial_driver(serial_drivers, id_table);
MODULE_LICENSE("GPL v2");