24 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/poll.h>
31 #include <linux/pci.h>
34 #include <linux/i2c.h>
35 #include <linux/swab.h>
57 .buf =
val, .len = 1 } };
64 .buf = &
reg, .len = 1 },
66 .buf =
val, .len = 1 } };
70 static int i2c_read_reg16(
struct i2c_adapter *adapter,
u8 adr,
73 u8 msg[2] = {reg>>8, reg&0xff};
75 .buf =
msg, .len = 2},
77 .buf =
val, .len = 1} };
105 static int ddb_i2c_master_xfer(
struct i2c_adapter *adapter,
108 struct ddb_i2c *i2c = (
struct ddb_i2c *)i2c_get_adapdata(adapter);
109 struct ddb *dev = i2c->
dev;
121 if (!ddb_i2c_cmd(i2c, addr, 1)) {
129 if (num == 1 && !(msg[0].
flags & I2C_M_RD)) {
132 if (!ddb_i2c_cmd(i2c, addr, 2))
135 if (num == 1 && (msg[0].
flags & I2C_M_RD)) {
137 if (!ddb_i2c_cmd(i2c, addr, 3)) {
153 .master_xfer = ddb_i2c_master_xfer,
154 .functionality = ddb_i2c_functionality,
157 static void ddb_i2c_release(
struct ddb *dev)
163 for (i = 0; i < dev->
info->port_num; i++) {
170 static int ddb_i2c_init(
struct ddb *dev)
176 for (i = 0; i < dev->
info->port_num; i++) {
182 i2c->
regs = 0x80 + i * 0x20;
189 i2c_set_adapdata(adap, i2c);
190 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
191 adap->
class = I2C_ADAP_CLASS_TV_DIGITAL|I2C_CLASS_TV_ANALOG;
193 #ifdef I2C_CLASS_TV_ANALOG
194 adap->
class = I2C_CLASS_TV_ANALOG;
200 adap->
dev.parent = &dev->
pdev->dev;
206 for (j = 0; j <
i; j++) {
220 static void set_table(
struct ddb *dev,
u32 off,
227 for (i = 0; i < num; i++) {
229 ddbwritel(mem & 0xffffffff, base + i * 8);
235 static void ddb_address_table(
struct ddb *dev)
241 for (i = 0; i < dev->
info->port_num * 2; i++) {
243 pbuf = dev->
input[
i].pbuf;
244 for (j = 0; j < dev->
input[
i].dma_buf_num; j++) {
246 ddbwritel(mem & 0xffffffff, base + j * 8);
250 for (i = 0; i < dev->
info->port_num; i++) {
253 for (j = 0; j < dev->
output[
i].dma_buf_num; j++) {
255 ddbwritel(mem & 0xffffffff, base + j * 8);
261 static void io_free(
struct pci_dev *pdev,
u8 **vbuf,
266 for (i = 0; i < num; i++) {
274 static int io_alloc(
struct pci_dev *pdev,
u8 **vbuf,
279 for (i = 0; i < num; i++) {
287 static int ddb_buffers_alloc(
struct ddb *dev)
292 for (i = 0; i < dev->
info->port_num; i++) {
293 port = &dev->
port[
i];
294 switch (port->
class) {
296 if (io_alloc(dev->
pdev, port->
input[0]->vbuf,
297 port->
input[0]->pbuf,
298 port->
input[0]->dma_buf_size,
299 port->
input[0]->dma_buf_num) < 0)
301 if (io_alloc(dev->
pdev, port->
input[1]->vbuf,
302 port->
input[1]->pbuf,
303 port->
input[1]->dma_buf_size,
304 port->
input[1]->dma_buf_num) < 0)
308 if (io_alloc(dev->
pdev, port->
input[0]->vbuf,
309 port->
input[0]->pbuf,
310 port->
input[0]->dma_buf_size,
311 port->
input[0]->dma_buf_num) < 0)
315 port->
output->dma_buf_size,
316 port->
output->dma_buf_num) < 0)
323 ddb_address_table(dev);
327 static void ddb_buffers_free(
struct ddb *dev)
332 for (i = 0; i < dev->
info->port_num; i++) {
333 port = &dev->
port[
i];
335 port->
input[0]->pbuf,
336 port->
input[0]->dma_buf_size,
337 port->
input[0]->dma_buf_num);
339 port->
input[1]->pbuf,
340 port->
input[1]->dma_buf_size,
341 port->
input[1]->dma_buf_num);
344 port->
output->dma_buf_size,
345 port->
output->dma_buf_num);
351 struct ddb *dev = input->
port->dev;
353 spin_lock_irq(&input->
lock);
372 spin_unlock_irq(&input->
lock);
375 static void ddb_input_stop(
struct ddb_input *input)
377 struct ddb *dev = input->
port->dev;
379 spin_lock_irq(&input->
lock);
383 spin_unlock_irq(&input->
lock);
388 struct ddb *dev = output->
port->dev;
390 spin_lock_irq(&output->
lock);
408 spin_unlock_irq(&output->
lock);
411 static void ddb_output_stop(
struct ddb_output *output)
413 struct ddb *dev = output->
port->dev;
415 spin_lock_irq(&output->
lock);
419 spin_unlock_irq(&output->
lock);
427 idx = (stat >> 11) & 0x1f;
428 off = (stat & 0x7ff) << 7;
430 if (output->
cbuf != idx) {
436 diff = off - output->
coff;
437 if (diff <= 0 || diff > 188)
445 struct ddb *dev = output->
port->dev;
449 idx = (stat >> 11) & 0x1f;
450 off = (stat & 0x7ff) << 7;
460 if (output->
cbuf == idx) {
461 if (off > output->
coff) {
463 len = off - output->
coff;
492 struct ddb *dev = input->
port->dev;
496 idx = (stat >> 11) & 0x1f;
497 off = (stat & 0x7ff) << 7;
504 if (input->
cbuf != idx)
511 struct ddb *dev = input->
port->dev;
516 idx = (stat >> 11) & 0x1f;
519 if (input->
cbuf == idx)
549 for (i = 0; i < dev->
info->port_num * 2; i++) {
550 if (dev->
input[i].fe == fe)
573 static int demod_attach_drxk(
struct ddb_input *input)
580 config.microcode_name =
"drxk_a3.mc";
581 config.qam_demod_parameter_count = 4;
582 config.adr = 0x29 + (input->
nr & 1);
591 fe->
ops.i2c_gate_ctrl = drxk_gate_ctrl;
595 static int tuner_attach_tda18271(
struct ddb_input *input)
600 if (input->
fe->ops.i2c_gate_ctrl)
601 input->
fe->ops.i2c_gate_ctrl(input->
fe, 1);
607 if (input->
fe->ops.i2c_gate_ctrl)
608 input->
fe->ops.i2c_gate_ctrl(input->
fe, 0);
632 .diseqc_envelope_mode =
true,
651 .diseqc_envelope_mode =
true,
666 static int demod_attach_stv0900(
struct ddb_input *input,
int type)
680 (0x09 - type) : (0x0b - type))) {
687 static int tuner_attach_stv6110(
struct ddb_input *input,
int type)
692 &stv6110b : &stv6110a;
701 input->
nr, tunerconf->
addr);
718 static int my_dvb_dmx_ts_card_init(
struct dvb_demux *dvbdemux,
char *
id,
736 static int my_dvb_dmxdev_ts_card_init(
struct dmxdev *
dmxdev,
752 dvbdemux->
dmx.add_frontend(&dvbdemux->
dmx, hw_frontend);
754 dvbdemux->
dmx.add_frontend(&dvbdemux->
dmx, mem_frontend);
755 return dvbdemux->
dmx.connect_frontend(&dvbdemux->
dmx, hw_frontend);
764 ddb_input_start(input);
766 return ++input->
users;
777 ddb_input_stop(input);
782 static void dvb_input_detach(
struct ddb_input *input)
784 struct dvb_adapter *adap = &input->
adap;
800 dvbdemux->
dmx.close(&dvbdemux->
dmx);
801 dvbdemux->
dmx.remove_frontend(&dvbdemux->
dmx,
803 dvbdemux->
dmx.remove_frontend(&dvbdemux->
dmx,
816 static int dvb_input_attach(
struct ddb_input *input)
820 struct dvb_adapter *adap = &input->
adap;
824 &input->
port->dev->pdev->dev,
828 "Check if you enabled enough adapters in dvb-core!\n");
833 ret = my_dvb_dmx_ts_card_init(dvbdemux,
"SW demux",
840 ret = my_dvb_dmxdev_ts_card_init(&input->
dmxdev, &input->
demux,
853 switch (port->
type) {
855 if (demod_attach_stv0900(input, 0) < 0)
857 if (tuner_attach_stv6110(input, 0) < 0)
865 if (demod_attach_stv0900(input, 1) < 0)
867 if (tuner_attach_stv6110(input, 1) < 0)
875 if (demod_attach_drxk(input) < 0)
877 if (tuner_attach_tda18271(input) < 0)
886 input->
fe2->tuner_priv = input->
fe->tuner_priv;
888 &input->
fe->ops.tuner_ops,
901 size_t count, loff_t *ppos)
909 if (ddb_output_free(output) < 188) {
913 output->
wq, ddb_output_free(output) >= 188) < 0)
916 stat = ddb_output_write(output, buf, left);
922 return (left == count) ? -
EAGAIN : (count -
left);
925 static ssize_t ts_read(
struct file *file,
char *buf,
926 size_t count, loff_t *ppos)
933 count -= count % 188;
936 if (ddb_input_avail(input) < 188) {
940 input->
wq, ddb_input_avail(input) >= 188) < 0)
943 read = ddb_input_read(input, buf, left);
949 return (left == count) ? -
EAGAIN : (count -
left);
959 unsigned int mask = 0;
962 if (data_avail_to_read)
964 if (data_avail_to_write)
967 poll_wait(file, &read_queue, wait);
968 poll_wait(file, &write_queue, wait);
995 static void input_tasklet(
unsigned long data)
998 struct ddb *dev = input->
port->dev;
1000 spin_lock(&input->
lock);
1002 spin_unlock(&input->
lock);
1010 while (input->
cbuf != ((input->
stat >> 11) & 0x1f)
1024 spin_unlock(&input->
lock);
1027 static void output_tasklet(
unsigned long data)
1030 struct ddb *dev = output->
port->dev;
1032 spin_lock(&output->
lock);
1034 spin_unlock(&output->
lock);
1039 spin_unlock(&output->
lock);
1050 static int ddb_ci_attach(
struct ddb_port *port)
1057 &port->
dev->pdev->dev,
1066 ddb_input_start(port->
input[0]);
1067 ddb_output_start(port->
output);
1071 &dvbdev_ci, (
void *) port->
output,
1076 static int ddb_port_attach(
struct ddb_port *port)
1080 switch (port->
class) {
1082 ret = dvb_input_attach(port->
input[0]);
1085 ret = dvb_input_attach(port->
input[1]);
1088 ret = ddb_ci_attach(port);
1098 static int ddb_ports_attach(
struct ddb *dev)
1103 for (i = 0; i < dev->
info->port_num; i++) {
1104 port = &dev->
port[
i];
1105 ret = ddb_port_attach(port);
1112 static void ddb_ports_detach(
struct ddb *dev)
1117 for (i = 0; i < dev->
info->port_num; i++) {
1118 port = &dev->
port[
i];
1119 switch (port->
class) {
1121 dvb_input_detach(port->
input[0]);
1122 dvb_input_detach(port->
input[1]);
1128 ddb_input_stop(port->
input[0]);
1129 ddb_output_stop(port->
output);
1143 static int port_has_ci(
struct ddb_port *port)
1146 return i2c_read_reg(&port->
i2c->adap, 0x40, 0, &val) ? 0 : 1;
1149 static int port_has_stv0900(
struct ddb_port *port)
1152 if (i2c_read_reg16(&port->
i2c->adap, 0x69, 0xf100, &val) < 0)
1157 static int port_has_stv0900_aa(
struct ddb_port *port)
1160 if (i2c_read_reg16(&port->
i2c->adap, 0x68, 0xf100, &val) < 0)
1165 static int port_has_drxks(
struct ddb_port *port)
1175 static void ddb_port_probe(
struct ddb_port *port)
1177 struct ddb *dev = port->
dev;
1182 if (port_has_ci(port)) {
1186 }
else if (port_has_stv0900(port)) {
1187 modname =
"DUAL DVB-S2";
1191 }
else if (port_has_stv0900_aa(port)) {
1192 modname =
"DUAL DVB-S2";
1196 }
else if (port_has_drxks(port)) {
1197 modname =
"DUAL DVB-C/T";
1203 port->
nr, port->
nr+1, modname);
1206 static void ddb_input_init(
struct ddb_port *port,
int nr)
1208 struct ddb *dev = port->
dev;
1224 static void ddb_output_init(
struct ddb_port *port,
int nr)
1226 struct ddb *dev = port->
dev;
1240 static void ddb_ports_init(
struct ddb *dev)
1245 for (i = 0; i < dev->
info->port_num; i++) {
1246 port = &dev->
port[
i];
1255 ddb_port_probe(port);
1256 ddb_input_init(port, 2 * i);
1257 ddb_input_init(port, 2 * i + 1);
1258 ddb_output_init(port, i);
1262 static void ddb_ports_release(
struct ddb *dev)
1267 for (i = 0; i < dev->
info->port_num; i++) {
1268 port = &dev->
port[
i];
1280 static void irq_handle_i2c(
struct ddb *dev,
int n)
1290 struct ddb *dev = (
struct ddb *) dev_id;
1300 irq_handle_i2c(dev, 0);
1302 irq_handle_i2c(dev, 1);
1304 irq_handle_i2c(dev, 2);
1306 irq_handle_i2c(dev, 3);
1309 tasklet_schedule(&dev->
input[0].tasklet);
1311 tasklet_schedule(&dev->
input[1].tasklet);
1313 tasklet_schedule(&dev->
input[2].tasklet);
1315 tasklet_schedule(&dev->
input[3].tasklet);
1317 tasklet_schedule(&dev->
input[4].tasklet);
1319 tasklet_schedule(&dev->
input[5].tasklet);
1321 tasklet_schedule(&dev->
input[6].tasklet);
1323 tasklet_schedule(&dev->
input[7].tasklet);
1326 tasklet_schedule(&dev->
output[0].tasklet);
1328 tasklet_schedule(&dev->
output[1].tasklet);
1330 tasklet_schedule(&dev->
output[2].tasklet);
1332 tasklet_schedule(&dev->
output[3].tasklet);
1344 static int flashio(
struct ddb *dev,
u8 *wbuf,
u32 wlen,
u8 *rbuf,
u32 rlen)
1366 shift = ((4 - wlen) * 8);
1404 data <<= ((4 - rlen) * 8);
1407 *rbuf = ((data >> 24) & 0xff);
1415 #define DDB_MAGIC 'd'
1424 #define IOCTL_DDB_FLASHIO _IOWR(DDB_MAGIC, 0x00, struct ddb_flashio)
1426 #define DDB_NAME "ddbridge"
1429 static struct ddb *ddbs[32];
1430 static struct class *ddb_class;
1431 static int ddb_major;
1433 static int ddb_open(
struct inode *
inode,
struct file *file)
1435 struct ddb *dev = ddbs[iminor(inode)];
1441 static long ddb_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
1444 void *parg = (
void *)arg;
1456 if (fio.write_len > 1028 || fio.read_len > 1028)
1458 if (fio.write_len + fio.read_len > 1028)
1461 wbuf = &dev->
iobuf[0];
1462 rbuf = wbuf + fio.write_len;
1466 res = flashio(dev, wbuf, fio.write_len, rbuf, fio.read_len);
1480 .unlocked_ioctl = ddb_ioctl,
1491 static int ddb_class_create(
void)
1493 ddb_major = register_chrdev(0,
DDB_NAME, &ddb_fops);
1498 if (IS_ERR(ddb_class)) {
1499 unregister_chrdev(ddb_major,
DDB_NAME);
1500 return PTR_ERR(ddb_class);
1502 ddb_class->
devnode = ddb_devnode;
1506 static void ddb_class_destroy(
void)
1509 unregister_chrdev(ddb_major,
DDB_NAME);
1512 static int ddb_device_create(
struct ddb *dev)
1514 dev->
nr = ddb_num++;
1517 dev,
"ddbridge%d", dev->
nr);
1518 ddbs[dev->
nr] =
dev;
1524 static void ddb_device_destroy(
struct ddb *dev)
1537 static void ddb_unmap(
struct ddb *dev)
1547 struct ddb *dev = (
struct ddb *) pci_get_drvdata(pdev);
1549 ddb_ports_detach(dev);
1550 ddb_i2c_release(dev);
1554 #ifdef CONFIG_PCI_MSI
1558 ddb_ports_release(dev);
1559 ddb_buffers_free(dev);
1560 ddb_device_destroy(dev);
1563 pci_set_drvdata(pdev, 0);
1584 pci_set_drvdata(pdev, dev);
1596 #ifdef CONFIG_PCI_MSI
1598 stat = pci_enable_msi(dev->
pdev);
1607 irq_flag,
"DDBridge", (
void *) dev);
1616 if (ddb_i2c_init(dev) < 0)
1618 ddb_ports_init(dev);
1619 if (ddb_buffers_alloc(dev) < 0) {
1623 if (ddb_ports_attach(dev) < 0)
1625 ddb_device_create(dev);
1629 ddb_ports_detach(dev);
1631 ddb_ports_release(dev);
1634 ddb_buffers_free(dev);
1643 pci_set_drvdata(pdev, 0);
1652 static struct ddb_info ddb_none = {
1654 .name =
"Digital Devices PCIe bridge",
1657 static struct ddb_info ddb_octopus = {
1659 .name =
"Digital Devices Octopus DVB adapter",
1663 static struct ddb_info ddb_octopus_le = {
1665 .name =
"Digital Devices Octopus LE DVB adapter",
1671 .name =
"Digital Devices Cine S2 V6 DVB adapter",
1675 #define DDVID 0xdd01
1677 #define DDB_ID(_vend, _dev, _subvend, _subdev, _driverdata) { \
1678 .vendor = _vend, .device = _dev, \
1679 .subvendor = _subvend, .subdevice = _subdev, \
1680 .driver_data = (unsigned long)&_driverdata }
1697 .id_table = ddb_id_tbl,
1702 static __init int module_init_ddbridge(
void)
1707 "Copyright (C) 2010-11 Digital Devices GmbH\n");
1709 ret = ddb_class_create();
1712 ret = pci_register_driver(&ddb_pci_driver);
1714 ddb_class_destroy();
1718 static __exit void module_exit_ddbridge(
void)
1721 ddb_class_destroy();