gl861.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* DVB USB compliant linux driver for GL861 USB2.0 devices.
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, version 2.
 *
 * see Documentation/dvb/README.dvb-usb for more information
 */
#include "gl861.h"

#include "zl10353.h"
#include "qt1010.h"

/* debug */
int dvb_usb_gl861_debug;
module_param_named(debug,dvb_usb_gl861_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);

static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
20
			 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
{
	u16 index;
	u16 value = addr << 8;
	int wo = (rbuf == NULL || rlen == 0); /* write-only */
	u8 req, type;

	if (wo) {
		req = GL861_REQ_I2C_WRITE;
		type = GL861_WRITE;
	} else { /* rw */
		req = GL861_REQ_I2C_READ;
		type = GL861_READ;
	}

	switch (wlen) {
36 37 38 39 40 41 42 43 44 45
	case 1:
		index = wbuf[0];
		break;
	case 2:
		index = wbuf[0];
		value = value + wbuf[1];
		break;
	default:
		warn("wlen = %x, aborting.", wlen);
		return -EINVAL;
46 47 48
	}

	return usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type,
49
			       value, index, rbuf, rlen, 2000);
50 51 52 53
}

/* I2C */
static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
54
			  int num)
55 56 57 58 59 60 61 62 63 64 65 66 67 68
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
	int i;

	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

	if (num > 2)
		return -EINVAL;

	for (i = 0; i < num; i++) {
		/* write/read request */
		if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
			if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
69
					  msg[i].len, msg[i+1].buf, msg[i+1].len) < 0)
70 71 72 73
				break;
			i++;
		} else
			if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
74
					  msg[i].len, NULL, 0) < 0)
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
				break;
	}

	mutex_unlock(&d->i2c_mutex);
	return i;
}

static u32 gl861_i2c_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_I2C;
}

static struct i2c_algorithm gl861_i2c_algo = {
	.master_xfer   = gl861_i2c_xfer,
	.functionality = gl861_i2c_func,
};

/* Callbacks for DVB USB */
static int gl861_identify_state(struct usb_device *udev,
				struct dvb_usb_device_properties *props,
				struct dvb_usb_device_description **desc,
				int *cold)
{
	*cold = 0;

	return 0;
}

static struct zl10353_config gl861_zl10353_config = {
	.demod_address = 0x1e,
	.no_tuner = 1,
106
	.parallel_ts = 1,
107 108 109 110 111
};

static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
{
	if ((adap->fe = dvb_attach(zl10353_attach, &gl861_zl10353_config,
112
				   &adap->dev->i2c_adap)) != NULL) {
113 114 115 116 117 118
		return 0;
	}

	return -EIO;
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
static struct qt1010_config gl861_qt1010_config = {
	.i2c_address = 0xc4
};

static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
{
	/* TODO FIXME; probably I2C gate.
	QT1010 tuner does not respond before we write 0x1a to ZL10353 demodulator
	register 0x62. This ought to be done somewhere in demodulator initialization.
	This solution is temporary hack. */
	u8 buf[2] = { 0x62, 0x1a };
	struct i2c_msg msg = {
		.addr = gl861_zl10353_config.demod_address, .flags = 0, .buf = buf, .len = 2
	};
	if (i2c_transfer(&adap->dev->i2c_adap, &msg, 1) != 1) {
		printk(KERN_WARNING "gl861 tuner attach failed\n");
		return -EREMOTEIO;
	}
	return dvb_attach(qt1010_attach,
			  adap->fe, &adap->dev->i2c_adap,
			  &gl861_qt1010_config) == NULL ? -ENODEV : 0;
}

142 143 144 145
/* DVB USB Driver stuff */
static struct dvb_usb_device_properties gl861_properties;

static int gl861_probe(struct usb_interface *intf,
146
		       const struct usb_device_id *id)
147 148 149 150 151 152 153 154
{
	struct dvb_usb_device *d;
	struct usb_host_interface *alt;
	int ret;

	if (intf->num_altsetting < 2)
		return -ENODEV;

155
	if ((ret = dvb_usb_device_init(intf, &gl861_properties, THIS_MODULE, &d)) == 0) {
156 157 158 159 160 161 162 163
		alt = usb_altnum_to_altsetting(intf, 0);

		if (alt == NULL) {
			deb_rc("not alt found!\n");
			return -ENODEV;
		}

		ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
164
					alt->desc.bAlternateSetting);
165 166 167 168 169 170
	}

	return ret;
}

static struct usb_device_id gl861_table [] = {
171
		{ USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801) },
172
		{ }		/* Terminating entry */
173 174 175 176
};
MODULE_DEVICE_TABLE (usb, gl861_table);

static struct dvb_usb_device_properties gl861_properties = {
177
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
178 179 180 181 182 183 184 185 186
	.usb_ctrl = DEVICE_SPECIFIC,

	.size_of_priv     = 0,

	.identify_state   = gl861_identify_state,
	.num_adapters = 1,
	.adapter = {{

		.frontend_attach  = gl861_frontend_attach,
187
		.tuner_attach     = gl861_tuner_attach,
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

		.stream = {
			.type = USB_BULK,
			.count = 7,
			.endpoint = 0x81,
			.u = {
				.bulk = {
					.buffersize = 512,
				}
			}
		},
	}},
	.i2c_algo         = &gl861_i2c_algo,

	.num_device_descs = 1,
	.devices = {
		{   "MSI Mega Sky 55801 DVB-T USB2.0",
			{ &gl861_table[0], NULL },
			{ NULL },
		},
	}
};

static struct usb_driver gl861_driver = {
212
	.name		= "dvb_usb_gl861",
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
	.probe		= gl861_probe,
	.disconnect	= dvb_usb_device_exit,
	.id_table	= gl861_table,
};

/* module stuff */
static int __init gl861_module_init(void)
{
	int ret;

	if ((ret = usb_register(&gl861_driver))) {
		err("usb_register failed. Error number %d", ret);
		return ret;
	}

	return 0;
}

static void __exit gl861_module_exit(void)
{
	/* deregister this driver from the USB subsystem */
	usb_deregister(&gl861_driver);
}

module_init (gl861_module_init);
module_exit (gl861_module_exit);

240
MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
241
MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
242
MODULE_VERSION("0.1");
243
MODULE_LICENSE("GPL");