| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 1 | /* DVB USB compliant linux driver for GL861 USB2.0 devices. | 
 | 2 |  * | 
 | 3 |  *	This program is free software; you can redistribute it and/or modify it | 
| Antti Palosaari | fb49558 | 2008-05-28 22:16:31 -0300 | [diff] [blame] | 4 |  *	under the terms of the GNU General Public License as published by the | 
 | 5 |  *	Free Software Foundation, version 2. | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 6 |  * | 
 | 7 |  * see Documentation/dvb/README.dvb-usb for more information | 
 | 8 |  */ | 
 | 9 | #include "gl861.h" | 
 | 10 |  | 
 | 11 | #include "zl10353.h" | 
 | 12 | #include "qt1010.h" | 
 | 13 |  | 
 | 14 | /* debug */ | 
| Adrian Bunk | 3721929 | 2007-04-27 12:31:05 -0300 | [diff] [blame] | 15 | static int dvb_usb_gl861_debug; | 
| Antti Palosaari | fb49558 | 2008-05-28 22:16:31 -0300 | [diff] [blame] | 16 | module_param_named(debug, dvb_usb_gl861_debug, int, 0644); | 
 | 17 | MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." | 
 | 18 | 	DVB_USB_DEBUG_STATUS); | 
| Janne Grunau | 78e9200 | 2008-04-09 19:13:13 -0300 | [diff] [blame] | 19 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); | 
 | 20 |  | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 21 | static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr, | 
| Michael Krufky | 1f78867 | 2006-10-03 17:21:13 -0300 | [diff] [blame] | 22 | 			 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen) | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 23 | { | 
 | 24 | 	u16 index; | 
| Aapo Tahkola | 26a154c | 2007-03-05 18:25:36 -0300 | [diff] [blame] | 25 | 	u16 value = addr << (8 + 1); | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 26 | 	int wo = (rbuf == NULL || rlen == 0); /* write-only */ | 
 | 27 | 	u8 req, type; | 
 | 28 |  | 
 | 29 | 	if (wo) { | 
 | 30 | 		req = GL861_REQ_I2C_WRITE; | 
 | 31 | 		type = GL861_WRITE; | 
 | 32 | 	} else { /* rw */ | 
 | 33 | 		req = GL861_REQ_I2C_READ; | 
 | 34 | 		type = GL861_READ; | 
 | 35 | 	} | 
 | 36 |  | 
 | 37 | 	switch (wlen) { | 
| Michael Krufky | 1f78867 | 2006-10-03 17:21:13 -0300 | [diff] [blame] | 38 | 	case 1: | 
 | 39 | 		index = wbuf[0]; | 
 | 40 | 		break; | 
 | 41 | 	case 2: | 
 | 42 | 		index = wbuf[0]; | 
 | 43 | 		value = value + wbuf[1]; | 
 | 44 | 		break; | 
 | 45 | 	default: | 
 | 46 | 		warn("wlen = %x, aborting.", wlen); | 
 | 47 | 		return -EINVAL; | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 48 | 	} | 
 | 49 |  | 
| Antti Palosaari | 1a78db8 | 2008-06-10 11:41:58 -0300 | [diff] [blame] | 50 | 	msleep(1); /* avoid I2C errors */ | 
| Antti Palosaari | f56ebe1 | 2008-05-28 21:55:06 -0300 | [diff] [blame] | 51 |  | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 52 | 	return usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type, | 
| Michael Krufky | 1f78867 | 2006-10-03 17:21:13 -0300 | [diff] [blame] | 53 | 			       value, index, rbuf, rlen, 2000); | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 54 | } | 
 | 55 |  | 
 | 56 | /* I2C */ | 
 | 57 | static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], | 
| Michael Krufky | 1f78867 | 2006-10-03 17:21:13 -0300 | [diff] [blame] | 58 | 			  int num) | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 59 | { | 
 | 60 | 	struct dvb_usb_device *d = i2c_get_adapdata(adap); | 
 | 61 | 	int i; | 
 | 62 |  | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 63 | 	if (num > 2) | 
 | 64 | 		return -EINVAL; | 
 | 65 |  | 
| Roel Kluin | c80296d | 2007-11-06 10:25:16 -0300 | [diff] [blame] | 66 | 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0) | 
 | 67 | 		return -EAGAIN; | 
 | 68 |  | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 69 | 	for (i = 0; i < num; i++) { | 
 | 70 | 		/* write/read request */ | 
 | 71 | 		if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { | 
 | 72 | 			if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf, | 
| Antti Palosaari | fb49558 | 2008-05-28 22:16:31 -0300 | [diff] [blame] | 73 | 				msg[i].len, msg[i+1].buf, msg[i+1].len) < 0) | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 74 | 				break; | 
 | 75 | 			i++; | 
 | 76 | 		} else | 
 | 77 | 			if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf, | 
| Michael Krufky | 1f78867 | 2006-10-03 17:21:13 -0300 | [diff] [blame] | 78 | 					  msg[i].len, NULL, 0) < 0) | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 79 | 				break; | 
 | 80 | 	} | 
 | 81 |  | 
 | 82 | 	mutex_unlock(&d->i2c_mutex); | 
 | 83 | 	return i; | 
 | 84 | } | 
 | 85 |  | 
 | 86 | static u32 gl861_i2c_func(struct i2c_adapter *adapter) | 
 | 87 | { | 
 | 88 | 	return I2C_FUNC_I2C; | 
 | 89 | } | 
 | 90 |  | 
 | 91 | static struct i2c_algorithm gl861_i2c_algo = { | 
 | 92 | 	.master_xfer   = gl861_i2c_xfer, | 
 | 93 | 	.functionality = gl861_i2c_func, | 
 | 94 | }; | 
 | 95 |  | 
 | 96 | /* Callbacks for DVB USB */ | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 97 | static struct zl10353_config gl861_zl10353_config = { | 
| Aapo Tahkola | 26a154c | 2007-03-05 18:25:36 -0300 | [diff] [blame] | 98 | 	.demod_address = 0x0f, | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 99 | 	.no_tuner = 1, | 
| Carl Lundqvist | 4131fd4 | 2006-10-09 12:49:17 -0300 | [diff] [blame] | 100 | 	.parallel_ts = 1, | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 101 | }; | 
 | 102 |  | 
 | 103 | static int gl861_frontend_attach(struct dvb_usb_adapter *adap) | 
 | 104 | { | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 105 |  | 
| Antti Palosaari | fb49558 | 2008-05-28 22:16:31 -0300 | [diff] [blame] | 106 | 	adap->fe = dvb_attach(zl10353_attach, &gl861_zl10353_config, | 
 | 107 | 		&adap->dev->i2c_adap); | 
 | 108 | 	if (adap->fe == NULL) | 
 | 109 | 		return -EIO; | 
 | 110 |  | 
 | 111 | 	return 0; | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 112 | } | 
 | 113 |  | 
| Antti Palosaari | 4c7e3ea | 2007-01-21 15:56:10 -0300 | [diff] [blame] | 114 | static struct qt1010_config gl861_qt1010_config = { | 
| Aapo Tahkola | 26a154c | 2007-03-05 18:25:36 -0300 | [diff] [blame] | 115 | 	.i2c_address = 0x62 | 
| Antti Palosaari | 4c7e3ea | 2007-01-21 15:56:10 -0300 | [diff] [blame] | 116 | }; | 
 | 117 |  | 
 | 118 | static int gl861_tuner_attach(struct dvb_usb_adapter *adap) | 
 | 119 | { | 
| Antti Palosaari | 4c7e3ea | 2007-01-21 15:56:10 -0300 | [diff] [blame] | 120 | 	return dvb_attach(qt1010_attach, | 
 | 121 | 			  adap->fe, &adap->dev->i2c_adap, | 
 | 122 | 			  &gl861_qt1010_config) == NULL ? -ENODEV : 0; | 
 | 123 | } | 
 | 124 |  | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 125 | /* DVB USB Driver stuff */ | 
 | 126 | static struct dvb_usb_device_properties gl861_properties; | 
 | 127 |  | 
 | 128 | static int gl861_probe(struct usb_interface *intf, | 
| Michael Krufky | 1f78867 | 2006-10-03 17:21:13 -0300 | [diff] [blame] | 129 | 		       const struct usb_device_id *id) | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 130 | { | 
 | 131 | 	struct dvb_usb_device *d; | 
 | 132 | 	struct usb_host_interface *alt; | 
 | 133 | 	int ret; | 
 | 134 |  | 
 | 135 | 	if (intf->num_altsetting < 2) | 
 | 136 | 		return -ENODEV; | 
 | 137 |  | 
| Janne Grunau | 78e9200 | 2008-04-09 19:13:13 -0300 | [diff] [blame] | 138 | 	ret = dvb_usb_device_init(intf, &gl861_properties, THIS_MODULE, &d, | 
 | 139 | 				  adapter_nr); | 
 | 140 | 	if (ret == 0) { | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 141 | 		alt = usb_altnum_to_altsetting(intf, 0); | 
 | 142 |  | 
 | 143 | 		if (alt == NULL) { | 
 | 144 | 			deb_rc("not alt found!\n"); | 
 | 145 | 			return -ENODEV; | 
 | 146 | 		} | 
 | 147 |  | 
 | 148 | 		ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber, | 
| Michael Krufky | 1f78867 | 2006-10-03 17:21:13 -0300 | [diff] [blame] | 149 | 					alt->desc.bAlternateSetting); | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 150 | 	} | 
 | 151 |  | 
 | 152 | 	return ret; | 
 | 153 | } | 
 | 154 |  | 
 | 155 | static struct usb_device_id gl861_table [] = { | 
| Michael Krufky | 6f7880f | 2006-10-03 17:12:14 -0300 | [diff] [blame] | 156 | 		{ USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801) }, | 
| Aapo Tahkola | 4919c49 | 2007-05-08 17:36:40 -0300 | [diff] [blame] | 157 | 		{ USB_DEVICE(USB_VID_ALINK, USB_VID_ALINK_DTU) }, | 
| Michael Krufky | 1f78867 | 2006-10-03 17:21:13 -0300 | [diff] [blame] | 158 | 		{ }		/* Terminating entry */ | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 159 | }; | 
| Antti Palosaari | fb49558 | 2008-05-28 22:16:31 -0300 | [diff] [blame] | 160 | MODULE_DEVICE_TABLE(usb, gl861_table); | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 161 |  | 
 | 162 | static struct dvb_usb_device_properties gl861_properties = { | 
| Jan Nijs | b3b2b8b | 2006-10-07 01:25:53 -0300 | [diff] [blame] | 163 | 	.caps = DVB_USB_IS_AN_I2C_ADAPTER, | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 164 | 	.usb_ctrl = DEVICE_SPECIFIC, | 
 | 165 |  | 
 | 166 | 	.size_of_priv     = 0, | 
 | 167 |  | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 168 | 	.num_adapters = 1, | 
 | 169 | 	.adapter = {{ | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 170 |  | 
 | 171 | 		.frontend_attach  = gl861_frontend_attach, | 
| Antti Palosaari | 4c7e3ea | 2007-01-21 15:56:10 -0300 | [diff] [blame] | 172 | 		.tuner_attach     = gl861_tuner_attach, | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 173 |  | 
 | 174 | 		.stream = { | 
 | 175 | 			.type = USB_BULK, | 
 | 176 | 			.count = 7, | 
 | 177 | 			.endpoint = 0x81, | 
 | 178 | 			.u = { | 
 | 179 | 				.bulk = { | 
 | 180 | 					.buffersize = 512, | 
 | 181 | 				} | 
 | 182 | 			} | 
 | 183 | 		}, | 
| Antti Palosaari | fb49558 | 2008-05-28 22:16:31 -0300 | [diff] [blame] | 184 | 	} }, | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 185 | 	.i2c_algo         = &gl861_i2c_algo, | 
 | 186 |  | 
| Aapo Tahkola | 4919c49 | 2007-05-08 17:36:40 -0300 | [diff] [blame] | 187 | 	.num_device_descs = 2, | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 188 | 	.devices = { | 
| Antti Palosaari | ea3a13b | 2008-05-28 22:04:12 -0300 | [diff] [blame] | 189 | 		{ | 
 | 190 | 			.name = "MSI Mega Sky 55801 DVB-T USB2.0", | 
 | 191 | 			.cold_ids = { NULL }, | 
 | 192 | 			.warm_ids = { &gl861_table[0], NULL }, | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 193 | 		}, | 
| Antti Palosaari | ea3a13b | 2008-05-28 22:04:12 -0300 | [diff] [blame] | 194 | 		{ | 
 | 195 | 			.name = "A-LINK DTU DVB-T USB2.0", | 
 | 196 | 			.cold_ids = { NULL }, | 
 | 197 | 			.warm_ids = { &gl861_table[1], NULL }, | 
| Aapo Tahkola | 4919c49 | 2007-05-08 17:36:40 -0300 | [diff] [blame] | 198 | 		}, | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 199 | 	} | 
 | 200 | }; | 
 | 201 |  | 
 | 202 | static struct usb_driver gl861_driver = { | 
| Michael Krufky | 05ec6cc | 2006-10-03 17:15:26 -0300 | [diff] [blame] | 203 | 	.name		= "dvb_usb_gl861", | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 204 | 	.probe		= gl861_probe, | 
 | 205 | 	.disconnect	= dvb_usb_device_exit, | 
 | 206 | 	.id_table	= gl861_table, | 
 | 207 | }; | 
 | 208 |  | 
 | 209 | /* module stuff */ | 
 | 210 | static int __init gl861_module_init(void) | 
 | 211 | { | 
 | 212 | 	int ret; | 
 | 213 |  | 
| Antti Palosaari | fb49558 | 2008-05-28 22:16:31 -0300 | [diff] [blame] | 214 | 	ret = usb_register(&gl861_driver); | 
 | 215 | 	if (ret) | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 216 | 		err("usb_register failed. Error number %d", ret); | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 217 |  | 
| Antti Palosaari | fb49558 | 2008-05-28 22:16:31 -0300 | [diff] [blame] | 218 | 	return ret; | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 219 | } | 
 | 220 |  | 
 | 221 | static void __exit gl861_module_exit(void) | 
 | 222 | { | 
 | 223 | 	/* deregister this driver from the USB subsystem */ | 
 | 224 | 	usb_deregister(&gl861_driver); | 
 | 225 | } | 
 | 226 |  | 
| Antti Palosaari | fb49558 | 2008-05-28 22:16:31 -0300 | [diff] [blame] | 227 | module_init(gl861_module_init); | 
 | 228 | module_exit(gl861_module_exit); | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 229 |  | 
| Michael Krufky | 947af8f | 2006-10-03 17:14:07 -0300 | [diff] [blame] | 230 | MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>"); | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 231 | MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861"); | 
| Carl Lundqvist | 4131fd4 | 2006-10-09 12:49:17 -0300 | [diff] [blame] | 232 | MODULE_VERSION("0.1"); | 
| Carl Lundqvist | f0c3a2c | 2006-10-03 17:09:30 -0300 | [diff] [blame] | 233 | MODULE_LICENSE("GPL"); |