summaryrefslogtreecommitdiff
path: root/linux/drivers/media/dvb/frontends/sp887x.c
diff options
context:
space:
mode:
authorMichael Hunold <devnull@localhost>2004-06-22 13:49:15 +0000
committerMichael Hunold <devnull@localhost>2004-06-22 13:49:15 +0000
commita2203e12db48c483f6c71fb1cc6b66711854873e (patch)
tree1ad1c68a53618a739537aee61e88b458204095dc /linux/drivers/media/dvb/frontends/sp887x.c
parent0cc01319857fa9e0ddeefc3be2ed9839d510c1c4 (diff)
downloadmediapointer-dvb-s2-a2203e12db48c483f6c71fb1cc6b66711854873e.tar.gz
mediapointer-dvb-s2-a2203e12db48c483f6c71fb1cc6b66711854873e.tar.bz2
- convert two driver to kernel i2c (untested, tester needed for the sp887x)
Diffstat (limited to 'linux/drivers/media/dvb/frontends/sp887x.c')
-rw-r--r--linux/drivers/media/dvb/frontends/sp887x.c302
1 files changed, 154 insertions, 148 deletions
diff --git a/linux/drivers/media/dvb/frontends/sp887x.c b/linux/drivers/media/dvb/frontends/sp887x.c
index d0ada2193..5f7b9b87d 100644
--- a/linux/drivers/media/dvb/frontends/sp887x.c
+++ b/linux/drivers/media/dvb/frontends/sp887x.c
@@ -5,35 +5,26 @@
/*
This driver needs a copy of the Avermedia firmware. The version tested
is part of the Avermedia DVB-T 1.3.26.3 Application. If the software is
- installed in Windows the file will be in the /Program Files/AVerTV DVB-T/
+ installed in Windoze the file will be in the /Program Files/AVerTV DVB-T/
directory and is called sc_main.mc. Alternatively it can "extracted" from
- the install cab files. Copy this file to '/usr/lib/hotplug/firmware/sc_main.mc'.
+ the install cab files.
+
+ Copy this file to '/usr/lib/hotplug/firmware/dvb-fe-sp887x.fw'.
+
With this version of the file the first 10 bytes are discarded and the
next 0x4000 loaded. This may change in future versions.
*/
+#define SP887X_DEFAULT_FIRMWARE "dvb-fe-sp887x.fw"
-#define __KERNEL_SYSCALLS__
-#include <linux/kernel.h>
-#include <linux/vmalloc.h>
-#include <linux/module.h>
#include <linux/init.h>
-#include <linux/string.h>
-#include <linux/slab.h>
-#include <linux/fs.h>
-#include <linux/unistd.h>
-#include <linux/fcntl.h>
-#include <linux/errno.h>
-#include <linux/i2c.h>
-
+#include <linux/module.h>
+#include <linux/firmware.h>
#include "dvb_frontend.h"
#include "dvb_functions.h"
-#ifndef DVB_SP887X_FIRMWARE_FILE
-#define DVB_SP887X_FIRMWARE_FILE "/usr/lib/hotplug/firmware/sc_main.mc"
-#endif
-
-static char *sp887x_firmware = DVB_SP887X_FIRMWARE_FILE;
+/* fixme: add this to i2c-id.h */
+#define I2C_DRIVERID_SP887X I2C_DRIVERID_EXP3
#if 0
#define dprintk(x...) printk(x)
@@ -54,9 +45,7 @@ static char *sp887x_firmware = DVB_SP887X_FIRMWARE_FILE;
#define LOG(dir,addr,buf,len)
#endif
-
-static
-struct dvb_frontend_info sp887x_info = {
+static struct dvb_frontend_info sp887x_info = {
.name = "Microtune MT7202DTF",
.type = FE_OFDM,
.frequency_min = 50500000,
@@ -68,18 +57,19 @@ struct dvb_frontend_info sp887x_info = {
FE_CAN_RECOVER
};
-static int errno;
+struct sp887x_state {
+ struct i2c_adapter *i2c;
+ struct dvb_adapter *dvb;
+};
-static
-int i2c_writebytes (struct dvb_frontend *fe, u8 addr, u8 *buf, u8 len)
+static int i2c_writebytes (struct i2c_adapter *i2c, u8 addr, u8 *buf, u8 len)
{
- struct dvb_i2c_bus *i2c = fe->i2c;
struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
int err;
LOG("i2c_writebytes", msg.addr, msg.buf, msg.len);
- if ((err = i2c->xfer (i2c, &msg, 1)) != 1) {
+ if ((err = i2c_transfer (i2c, &msg, 1)) != 1) {
printk ("%s: i2c write error (addr %02x, err == %i)\n",
__FUNCTION__, addr, err);
return -EREMOTEIO;
@@ -88,19 +78,15 @@ int i2c_writebytes (struct dvb_frontend *fe, u8 addr, u8 *buf, u8 len)
return 0;
}
-
-
-static
-int sp887x_writereg (struct dvb_frontend *fe, u16 reg, u16 data)
+static int sp887x_writereg (struct i2c_adapter *i2c, u16 reg, u16 data)
{
- struct dvb_i2c_bus *i2c = fe->i2c;
u8 b0 [] = { reg >> 8 , reg & 0xff, data >> 8, data & 0xff };
struct i2c_msg msg = { .addr = 0x70, .flags = 0, .buf = b0, .len = 4 };
int ret;
LOG("sp887x_writereg", msg.addr, msg.buf, msg.len);
- if ((ret = i2c->xfer(i2c, &msg, 1)) != 1) {
+ if ((ret = i2c_transfer(i2c, &msg, 1)) != 1) {
/**
* in case of soft reset we ignore ACK errors...
*/
@@ -117,11 +103,8 @@ int sp887x_writereg (struct dvb_frontend *fe, u16 reg, u16 data)
return 0;
}
-
-static
-u16 sp887x_readreg (struct dvb_frontend *fe, u16 reg)
+static u16 sp887x_readreg (struct i2c_adapter *i2c, u16 reg)
{
- struct dvb_i2c_bus *i2c = fe->i2c;
u8 b0 [] = { reg >> 8 , reg & 0xff };
u8 b1 [2];
int ret;
@@ -131,15 +114,13 @@ u16 sp887x_readreg (struct dvb_frontend *fe, u16 reg)
LOG("sp887x_readreg (w)", msg[0].addr, msg[0].buf, msg[0].len);
LOG("sp887x_readreg (r)", msg[1].addr, msg[1].buf, msg[1].len);
- if ((ret = i2c->xfer(i2c, msg, 2)) != 2)
+ if ((ret = i2c_transfer(i2c, msg, 2)) != 2)
printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
return (((b1[0] << 8) | b1[1]) & 0xfff);
}
-
-static
-void sp887x_microcontroller_stop (struct dvb_frontend *fe)
+static void sp887x_microcontroller_stop (struct i2c_adapter *fe)
{
dprintk("%s\n", __FUNCTION__);
sp887x_writereg(fe, 0xf08, 0x000);
@@ -149,9 +130,7 @@ void sp887x_microcontroller_stop (struct dvb_frontend *fe)
sp887x_writereg(fe, 0xf00, 0x000);
}
-
-static
-void sp887x_microcontroller_start (struct dvb_frontend *fe)
+static void sp887x_microcontroller_start (struct i2c_adapter *fe)
{
dprintk("%s\n", __FUNCTION__);
sp887x_writereg(fe, 0xf08, 0x000);
@@ -161,9 +140,7 @@ void sp887x_microcontroller_start (struct dvb_frontend *fe)
sp887x_writereg(fe, 0xf00, 0x001);
}
-
-static
-void sp887x_setup_agc (struct dvb_frontend *fe)
+static void sp887x_setup_agc (struct i2c_adapter *fe)
{
/* setup AGC parameters */
dprintk("%s\n", __FUNCTION__);
@@ -183,72 +160,31 @@ void sp887x_setup_agc (struct dvb_frontend *fe)
sp887x_writereg(fe, 0x303, 0x000);
}
-
#define BLOCKSIZE 30
-
+#define FW_SIZE 0x4000
/**
* load firmware and setup MPEG interface...
*/
-static
-int sp887x_initial_setup (struct dvb_frontend *fe)
+static int sp887x_initial_setup (struct i2c_adapter *fe, const struct firmware *fw)
{
u8 buf [BLOCKSIZE+2];
- unsigned char *firmware = NULL;
int i;
- int fd;
- int filesize;
- int fw_size;
- mm_segment_t fs;
+ int fw_size = fw->size;
+ unsigned char *mem = fw->data;
dprintk("%s\n", __FUNCTION__);
+ /* ignore the first 10 bytes, then we expect 0x4000 bytes of firmware */
+ if (fw_size < FW_SIZE+10)
+ return -ENODEV;
+
+ mem = fw->data + 10;
+
/* soft reset */
sp887x_writereg(fe, 0xf1a, 0x000);
sp887x_microcontroller_stop (fe);
- fs = get_fs();
-
- // Load the firmware
- set_fs(get_ds());
- fd = open(sp887x_firmware, 0, 0);
- if (fd < 0) {
- printk(KERN_WARNING "%s: Unable to open firmware %s\n", __FUNCTION__,
- sp887x_firmware);
- return -EIO;
- }
- filesize = lseek(fd, 0L, 2);
- if (filesize <= 0) {
- printk(KERN_WARNING "%s: Firmware %s is empty\n", __FUNCTION__,
- sp887x_firmware);
- sys_close(fd);
- return -EIO;
- }
-
- fw_size = 0x4000;
-
- // allocate buffer for it
- firmware = vmalloc(fw_size);
- if (firmware == NULL) {
- printk(KERN_WARNING "%s: Out of memory loading firmware\n",
- __FUNCTION__);
- sys_close(fd);
- return -EIO;
- }
-
- // read it!
- // read the first 16384 bytes from the file
- // ignore the first 10 bytes
- lseek(fd, 10, 0);
- if (read(fd, firmware, fw_size) != fw_size) {
- printk(KERN_WARNING "%s: Failed to read firmware\n", __FUNCTION__);
- vfree(firmware);
- sys_close(fd);
- return -EIO;
- }
- sys_close(fd);
- set_fs(fs);
-
printk ("%s: firmware upload... ", __FUNCTION__);
/* setup write pointer to -1 (end of memory) */
@@ -258,7 +194,7 @@ int sp887x_initial_setup (struct dvb_frontend *fe)
/* dummy write (wrap around to start of memory) */
sp887x_writereg(fe, 0x8f0a, 0x0000);
- for (i=0; i<fw_size; i+=BLOCKSIZE) {
+ for (i = 0; i < FW_SIZE; i += BLOCKSIZE) {
int c = BLOCKSIZE;
int err;
@@ -271,18 +207,15 @@ int sp887x_initial_setup (struct dvb_frontend *fe)
buf[0] = 0xcf;
buf[1] = 0x0a;
- memcpy(&buf[2], firmware + i, c);
+ memcpy(&buf[2], mem + i, c);
if ((err = i2c_writebytes (fe, 0x70, buf, c+2)) < 0) {
printk ("failed.\n");
printk ("%s: i2c error (err == %i)\n", __FUNCTION__, err);
- vfree(firmware);
return err;
}
}
- vfree(firmware);
-
/* don't write RS bytes between packets */
sp887x_writereg(fe, 0xc13, 0x001);
@@ -308,13 +241,11 @@ int sp887x_initial_setup (struct dvb_frontend *fe)
return 0;
};
-
/**
* returns the actual tuned center frequency which can be used
* to initialise the AFC registers
*/
-static
-int tsa5060_setup_pll (struct dvb_frontend *fe, int freq)
+static int tsa5060_setup_pll (struct i2c_adapter *fe, int freq)
{
u8 cfg, cpump, band_select;
u8 buf [4];
@@ -341,13 +272,10 @@ int tsa5060_setup_pll (struct dvb_frontend *fe, int freq)
return (div * 166666 - 36000000);
}
-
-
-static
-int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
+static int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
{
int known_parameters = 1;
-
+
*reg0xc05 = 0x000;
switch (p->u.ofdm.constellation) {
@@ -415,13 +343,11 @@ int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
return 0;
}
-
/**
* estimates division of two 24bit numbers,
* derived from the ves1820/stv0299 driver code
*/
-static
-void divide (int n, int d, int *quotient_i, int *quotient_f)
+static void divide (int n, int d, int *quotient_i, int *quotient_f)
{
unsigned int q, r;
@@ -439,9 +365,7 @@ void divide (int n, int d, int *quotient_i, int *quotient_f)
}
}
-
-static
-void sp887x_correct_offsets (struct dvb_frontend *fe,
+static void sp887x_correct_offsets (struct i2c_adapter *fe,
struct dvb_frontend_parameters *p,
int actual_freq)
{
@@ -472,9 +396,7 @@ void sp887x_correct_offsets (struct dvb_frontend *fe,
sp887x_writereg(fe, 0x30a, frequency_shift & 0xfff);
}
-
-static
-int sp887x_setup_frontend_parameters (struct dvb_frontend *fe,
+static int sp887x_setup_frontend_parameters (struct i2c_adapter *fe,
struct dvb_frontend_parameters *p)
{
int actual_freq, err;
@@ -484,7 +406,7 @@ int sp887x_setup_frontend_parameters (struct dvb_frontend *fe,
p->u.ofdm.bandwidth != BANDWIDTH_7_MHZ &&
p->u.ofdm.bandwidth != BANDWIDTH_6_MHZ)
return -EINVAL;
-
+
if ((err = configure_reg0xc05(p, &reg0xc05)))
return err;
@@ -532,10 +454,11 @@ int sp887x_setup_frontend_parameters (struct dvb_frontend *fe,
return 0;
}
-
-static
-int sp887x_ioctl (struct dvb_frontend *fe, unsigned int cmd, void *arg)
+static int sp887x_ioctl(struct dvb_frontend *f, unsigned int cmd, void *arg)
{
+ struct sp887x_state *state = (struct sp887x_state *) f->data;
+ struct i2c_adapter *fe = state->i2c;
+
switch (cmd) {
case FE_GET_INFO:
memcpy (arg, &sp887x_info, sizeof(struct dvb_frontend_info));
@@ -625,10 +548,6 @@ int sp887x_ioctl (struct dvb_frontend *fe, unsigned int cmd, void *arg)
break;
case FE_INIT:
- if (fe->data == NULL) { /* first time initialisation... */
- fe->data = (void*) ~0;
- sp887x_initial_setup (fe);
- }
/* enable TS output and interface pins */
sp887x_writereg(fe, 0xc18, 0x00d);
break;
@@ -640,7 +559,7 @@ int sp887x_ioctl (struct dvb_frontend *fe, unsigned int cmd, void *arg)
fesettings->step_size = 0;
fesettings->max_drift = 0;
return 0;
- }
+ }
default:
return -EOPNOTSUPP;
@@ -649,51 +568,138 @@ int sp887x_ioctl (struct dvb_frontend *fe, unsigned int cmd, void *arg)
return 0;
}
+static struct i2c_client client_template;
-
-static
-int sp887x_attach (struct dvb_i2c_bus *i2c, void **data)
+static int attach_adapter(struct i2c_adapter *adapter)
{
+ struct i2c_client *client;
+ struct sp887x_state *state;
+ const struct firmware *fw;
+ int ret;
+
struct i2c_msg msg = {.addr = 0x70, .flags = 0, .buf = NULL, .len = 0 };
dprintk ("%s\n", __FUNCTION__);
- if (i2c->xfer (i2c, &msg, 1) != 1)
+ if (NULL == (client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
+ return -ENOMEM;
+ }
+
+ if (NULL == (state = kmalloc(sizeof(struct sp887x_state), GFP_KERNEL))) {
+ kfree(client);
+ return -ENOMEM;
+ }
+ state->i2c = adapter;
+
+ if (i2c_transfer (adapter, &msg, 1) != 1) {
+ kfree(state);
+ kfree(client);
return -ENODEV;
+ }
- return dvb_register_frontend (sp887x_ioctl, i2c, NULL, &sp887x_info);
-}
+ memcpy(client, &client_template, sizeof(struct i2c_client));
+ client->adapter = adapter;
+ i2c_set_clientdata(client, (void*)state);
+
+ ret = i2c_attach_client(client);
+ if (ret) {
+ kfree(client);
+ kfree(state);
+ return ret;
+ }
+
+ /* request the firmware, this will block until someone uploads it */
+ printk("sp887x: waiting for firmware upload...\n");
+ ret = request_firmware(&fw, SP887X_DEFAULT_FIRMWARE, &client->dev);
+ if (ret) {
+ printk("sp887x: no firmware upload (timeout or file not found?)\n");
+ goto out;
+ }
+
+ ret = sp887x_initial_setup(adapter, fw);
+ if (ret) {
+ printk("sp887x: writing firmware to device failed\n");
+ goto out;
+ }
+ ret = dvb_register_frontend_new (sp887x_ioctl, state->dvb, (void*) state, &sp887x_info);
+ if (ret) {
+ printk("sp887x: registering frontend to dvb-core failed.\n");
+ goto out;
+ }
-static
-void sp887x_detach (struct dvb_i2c_bus *i2c, void *data)
+ return 0;
+out:
+ release_firmware(fw);
+ i2c_detach_client(client);
+ kfree(client);
+ kfree(state);
+ return ret;
+}
+
+static int detach_client(struct i2c_client *client)
{
+ struct sp887x_state *state = (struct sp887x_state*)i2c_get_clientdata(client);
+
dprintk ("%s\n", __FUNCTION__);
- dvb_unregister_frontend (sp887x_ioctl, i2c);
-}
+ dvb_unregister_frontend_new (sp887x_ioctl, state->dvb);
+ i2c_detach_client(client);
+ BUG_ON(state->dvb);
+ kfree(client);
+ kfree(state);
+ return 0;
+}
-static
-int __init init_sp887x (void)
+static int command (struct i2c_client *client, unsigned int cmd, void *arg)
{
+ struct sp887x_state *state = (struct sp887x_state*)i2c_get_clientdata(client);
+
dprintk ("%s\n", __FUNCTION__);
- return dvb_register_i2c_device (NULL, sp887x_attach, sp887x_detach);
+
+ switch (cmd) {
+ case FE_REGISTER:
+ state->dvb = (struct dvb_adapter*)arg;
+ break;
+ case FE_UNREGISTER:
+ state->dvb = NULL;
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+ return 0;
}
+static struct i2c_driver driver = {
+ .owner = THIS_MODULE,
+ .name = "sp887x",
+ .id = I2C_DRIVERID_SP887X,
+ .flags = I2C_DF_NOTIFY,
+ .attach_adapter = attach_adapter,
+ .detach_client = detach_client,
+ .command = command,
+};
-static
-void __exit exit_sp887x (void)
+static struct i2c_client client_template = {
+ I2C_DEVNAME("sp887x"),
+ .flags = I2C_CLIENT_ALLOW_USE,
+ .driver = &driver,
+};
+
+static int __init init_sp887x(void)
{
- dprintk ("%s\n", __FUNCTION__);
- dvb_unregister_i2c_device (sp887x_attach);
+ return i2c_add_driver(&driver);
}
+static void __exit exit_sp887x(void)
+{
+ if (i2c_del_driver(&driver))
+ printk("sp887x: driver deregistration failed\n");
+}
module_init(init_sp887x);
module_exit(exit_sp887x);
-
MODULE_DESCRIPTION("sp887x DVB-T demodulator driver");
MODULE_LICENSE("GPL");
-