blob: 383cca378b8c5bb38e8b5935a013405c0bcac315 [file] [log] [blame]
Igor M. Liplianina611d0c2008-09-13 10:10:53 -03001/*
2 * dm1105.c - driver for DVB cards based on SDMC DM1105 PCI chip
3 *
4 * Copyright (C) 2008 Igor M. Liplianin <liplianin@me.by>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 */
21
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030022#include <linux/i2c.h>
23#include <linux/init.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/proc_fs.h>
27#include <linux/pci.h>
28#include <linux/dma-mapping.h>
29#include <linux/input.h>
30#include <media/ir-common.h>
31
32#include "demux.h"
33#include "dmxdev.h"
34#include "dvb_demux.h"
35#include "dvb_frontend.h"
36#include "dvb_net.h"
37#include "dvbdev.h"
38#include "dvb-pll.h"
39
40#include "stv0299.h"
Igor M. Liplianine4aab642008-09-23 15:43:57 -030041#include "stv0288.h"
42#include "stb6000.h"
Igor M. Liplianin04ad28c2008-09-16 18:21:11 -030043#include "si21xx.h"
Igor M. Liplianin35d9c422008-09-13 20:56:59 -030044#include "cx24116.h"
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030045#include "z0194a.h"
Igor M. Liplianinb4a0e812010-01-17 10:32:26 -030046#include "ds3000.h"
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030047
Igor M. Liplianind8300df2009-07-22 17:30:25 -030048#define UNSET (-1U)
49
50#define DM1105_BOARD_NOAUTO UNSET
51#define DM1105_BOARD_UNKNOWN 0
52#define DM1105_BOARD_DVBWORLD_2002 1
53#define DM1105_BOARD_DVBWORLD_2004 2
54#define DM1105_BOARD_AXESS_DM05 3
55
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030056/* ----------------------------------------------- */
57/*
58 * PCI ID's
59 */
60#ifndef PCI_VENDOR_ID_TRIGEM
61#define PCI_VENDOR_ID_TRIGEM 0x109f
62#endif
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -030063#ifndef PCI_VENDOR_ID_AXESS
64#define PCI_VENDOR_ID_AXESS 0x195d
65#endif
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030066#ifndef PCI_DEVICE_ID_DM1105
67#define PCI_DEVICE_ID_DM1105 0x036f
68#endif
69#ifndef PCI_DEVICE_ID_DW2002
70#define PCI_DEVICE_ID_DW2002 0x2002
71#endif
72#ifndef PCI_DEVICE_ID_DW2004
73#define PCI_DEVICE_ID_DW2004 0x2004
74#endif
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -030075#ifndef PCI_DEVICE_ID_DM05
76#define PCI_DEVICE_ID_DM05 0x1105
77#endif
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030078/* ----------------------------------------------- */
79/* sdmc dm1105 registers */
80
81/* TS Control */
82#define DM1105_TSCTR 0x00
83#define DM1105_DTALENTH 0x04
84
85/* GPIO Interface */
86#define DM1105_GPIOVAL 0x08
87#define DM1105_GPIOCTR 0x0c
88
89/* PID serial number */
90#define DM1105_PIDN 0x10
91
92/* Odd-even secret key select */
93#define DM1105_CWSEL 0x14
94
95/* Host Command Interface */
96#define DM1105_HOST_CTR 0x18
97#define DM1105_HOST_AD 0x1c
98
99/* PCI Interface */
100#define DM1105_CR 0x30
101#define DM1105_RST 0x34
102#define DM1105_STADR 0x38
103#define DM1105_RLEN 0x3c
104#define DM1105_WRP 0x40
105#define DM1105_INTCNT 0x44
106#define DM1105_INTMAK 0x48
107#define DM1105_INTSTS 0x4c
108
109/* CW Value */
110#define DM1105_ODD 0x50
111#define DM1105_EVEN 0x58
112
113/* PID Value */
114#define DM1105_PID 0x60
115
116/* IR Control */
117#define DM1105_IRCTR 0x64
118#define DM1105_IRMODE 0x68
119#define DM1105_SYSTEMCODE 0x6c
120#define DM1105_IRCODE 0x70
121
122/* Unknown Values */
123#define DM1105_ENCRYPT 0x74
124#define DM1105_VER 0x7c
125
126/* I2C Interface */
127#define DM1105_I2CCTR 0x80
128#define DM1105_I2CSTS 0x81
129#define DM1105_I2CDAT 0x82
130#define DM1105_I2C_RA 0x83
131/* ----------------------------------------------- */
132/* Interrupt Mask Bits */
133
134#define INTMAK_TSIRQM 0x01
135#define INTMAK_HIRQM 0x04
136#define INTMAK_IRM 0x08
137#define INTMAK_ALLMASK (INTMAK_TSIRQM | \
138 INTMAK_HIRQM | \
139 INTMAK_IRM)
140#define INTMAK_NONEMASK 0x00
141
142/* Interrupt Status Bits */
143#define INTSTS_TSIRQ 0x01
144#define INTSTS_HIRQ 0x04
145#define INTSTS_IR 0x08
146
147/* IR Control Bits */
148#define DM1105_IR_EN 0x01
149#define DM1105_SYS_CHK 0x02
150#define DM1105_REP_FLG 0x08
151
152/* EEPROM addr */
153#define IIC_24C01_addr 0xa0
154/* Max board count */
155#define DM1105_MAX 0x04
156
157#define DRIVER_NAME "dm1105"
158
159#define DM1105_DMA_PACKETS 47
160#define DM1105_DMA_PACKET_LENGTH (128*4)
161#define DM1105_DMA_BYTES (128 * 4 * DM1105_DMA_PACKETS)
162
163/* GPIO's for LNB power control */
164#define DM1105_LNB_MASK 0x00000000
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300165#define DM1105_LNB_OFF 0x00020000
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300166#define DM1105_LNB_13V 0x00010100
167#define DM1105_LNB_18V 0x00000100
168
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300169/* GPIO's for LNB power control for Axess DM05 */
170#define DM05_LNB_MASK 0x00000000
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300171#define DM05_LNB_OFF 0x00020000/* actually 13v */
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300172#define DM05_LNB_13V 0x00020000
173#define DM05_LNB_18V 0x00030000
174
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300175static unsigned int card[] = {[0 ... 3] = UNSET };
176module_param_array(card, int, NULL, 0444);
177MODULE_PARM_DESC(card, "card type");
178
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300179static int ir_debug;
180module_param(ir_debug, int, 0644);
181MODULE_PARM_DESC(ir_debug, "enable debugging information for IR decoding");
182
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300183static unsigned int dm1105_devcount;
184
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300185DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
186
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300187struct dm1105_board {
188 char *name;
189};
190
191struct dm1105_subid {
192 u16 subvendor;
193 u16 subdevice;
194 u32 card;
195};
196
197static const struct dm1105_board dm1105_boards[] = {
198 [DM1105_BOARD_UNKNOWN] = {
199 .name = "UNKNOWN/GENERIC",
200 },
201 [DM1105_BOARD_DVBWORLD_2002] = {
202 .name = "DVBWorld PCI 2002",
203 },
204 [DM1105_BOARD_DVBWORLD_2004] = {
205 .name = "DVBWorld PCI 2004",
206 },
207 [DM1105_BOARD_AXESS_DM05] = {
208 .name = "Axess/EasyTv DM05",
209 },
210};
211
212static const struct dm1105_subid dm1105_subids[] = {
213 {
214 .subvendor = 0x0000,
215 .subdevice = 0x2002,
216 .card = DM1105_BOARD_DVBWORLD_2002,
217 }, {
218 .subvendor = 0x0001,
219 .subdevice = 0x2002,
220 .card = DM1105_BOARD_DVBWORLD_2002,
221 }, {
222 .subvendor = 0x0000,
223 .subdevice = 0x2004,
224 .card = DM1105_BOARD_DVBWORLD_2004,
225 }, {
226 .subvendor = 0x0001,
227 .subdevice = 0x2004,
228 .card = DM1105_BOARD_DVBWORLD_2004,
229 }, {
230 .subvendor = 0x195d,
231 .subdevice = 0x1105,
232 .card = DM1105_BOARD_AXESS_DM05,
233 },
234};
235
236static void dm1105_card_list(struct pci_dev *pci)
237{
238 int i;
239
240 if (0 == pci->subsystem_vendor &&
241 0 == pci->subsystem_device) {
242 printk(KERN_ERR
243 "dm1105: Your board has no valid PCI Subsystem ID\n"
244 "dm1105: and thus can't be autodetected\n"
245 "dm1105: Please pass card=<n> insmod option to\n"
246 "dm1105: workaround that. Redirect complaints to\n"
247 "dm1105: the vendor of the TV card. Best regards,\n"
248 "dm1105: -- tux\n");
249 } else {
250 printk(KERN_ERR
251 "dm1105: Your board isn't known (yet) to the driver.\n"
252 "dm1105: You can try to pick one of the existing\n"
253 "dm1105: card configs via card=<n> insmod option.\n"
254 "dm1105: Updating to the latest version might help\n"
255 "dm1105: as well.\n");
256 }
257 printk(KERN_ERR "Here is a list of valid choices for the card=<n> "
258 "insmod option:\n");
259 for (i = 0; i < ARRAY_SIZE(dm1105_boards); i++)
260 printk(KERN_ERR "dm1105: card=%d -> %s\n",
261 i, dm1105_boards[i].name);
262}
263
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300264/* infrared remote control */
265struct infrared {
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300266 struct input_dev *input_dev;
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300267 struct ir_input_state ir;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300268 char input_phys[32];
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300269 struct work_struct work;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300270 u32 ir_command;
271};
272
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300273struct dm1105_dev {
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300274 /* pci */
275 struct pci_dev *pdev;
276 u8 __iomem *io_mem;
277
278 /* ir */
279 struct infrared ir;
280
281 /* dvb */
282 struct dmx_frontend hw_frontend;
283 struct dmx_frontend mem_frontend;
284 struct dmxdev dmxdev;
285 struct dvb_adapter dvb_adapter;
286 struct dvb_demux demux;
287 struct dvb_frontend *fe;
288 struct dvb_net dvbnet;
289 unsigned int full_ts_users;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300290 unsigned int boardnr;
291 int nr;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300292
293 /* i2c */
294 struct i2c_adapter i2c_adap;
295
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300296 /* irq */
297 struct work_struct work;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300298 struct workqueue_struct *wq;
299 char wqn[16];
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300300
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300301 /* dma */
302 dma_addr_t dma_addr;
303 unsigned char *ts_buf;
304 u32 wrp;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300305 u32 nextwrp;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300306 u32 buffer_size;
307 unsigned int PacketErrorCount;
308 unsigned int dmarst;
309 spinlock_t lock;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300310};
311
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300312#define dm_io_mem(reg) ((unsigned long)(&dev->io_mem[reg]))
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300313
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300314#define dm_readb(reg) inb(dm_io_mem(reg))
315#define dm_writeb(reg, value) outb((value), (dm_io_mem(reg)))
316
317#define dm_readw(reg) inw(dm_io_mem(reg))
318#define dm_writew(reg, value) outw((value), (dm_io_mem(reg)))
319
320#define dm_readl(reg) inl(dm_io_mem(reg))
321#define dm_writel(reg, value) outl((value), (dm_io_mem(reg)))
322
323#define dm_andorl(reg, mask, value) \
324 outl((inl(dm_io_mem(reg)) & ~(mask)) |\
325 ((value) & (mask)), (dm_io_mem(reg)))
326
327#define dm_setl(reg, bit) dm_andorl((reg), (bit), (bit))
328#define dm_clearl(reg, bit) dm_andorl((reg), (bit), 0)
329
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300330static int dm1105_i2c_xfer(struct i2c_adapter *i2c_adap,
331 struct i2c_msg *msgs, int num)
332{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300333 struct dm1105_dev *dev ;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300334
335 int addr, rc, i, j, k, len, byte, data;
336 u8 status;
337
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300338 dev = i2c_adap->algo_data;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300339 for (i = 0; i < num; i++) {
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300340 dm_writeb(DM1105_I2CCTR, 0x00);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300341 if (msgs[i].flags & I2C_M_RD) {
342 /* read bytes */
343 addr = msgs[i].addr << 1;
344 addr |= 1;
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300345 dm_writeb(DM1105_I2CDAT, addr);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300346 for (byte = 0; byte < msgs[i].len; byte++)
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300347 dm_writeb(DM1105_I2CDAT + byte + 1, 0);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300348
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300349 dm_writeb(DM1105_I2CCTR, 0x81 + msgs[i].len);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300350 for (j = 0; j < 55; j++) {
351 mdelay(10);
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300352 status = dm_readb(DM1105_I2CSTS);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300353 if ((status & 0xc0) == 0x40)
354 break;
355 }
356 if (j >= 55)
357 return -1;
358
359 for (byte = 0; byte < msgs[i].len; byte++) {
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300360 rc = dm_readb(DM1105_I2CDAT + byte + 1);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300361 if (rc < 0)
362 goto err;
363 msgs[i].buf[byte] = rc;
364 }
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300365 } else if ((msgs[i].buf[0] == 0xf7) && (msgs[i].addr == 0x55)) {
366 /* prepaired for cx24116 firmware */
367 /* Write in small blocks */
368 len = msgs[i].len - 1;
369 k = 1;
370 do {
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300371 dm_writeb(DM1105_I2CDAT, msgs[i].addr << 1);
372 dm_writeb(DM1105_I2CDAT + 1, 0xf7);
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300373 for (byte = 0; byte < (len > 48 ? 48 : len); byte++) {
374 data = msgs[i].buf[k + byte];
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300375 dm_writeb(DM1105_I2CDAT + byte + 2, data);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300376 }
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300377 dm_writeb(DM1105_I2CCTR, 0x82 + (len > 48 ? 48 : len));
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300378 for (j = 0; j < 25; j++) {
379 mdelay(10);
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300380 status = dm_readb(DM1105_I2CSTS);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300381 if ((status & 0xc0) == 0x40)
382 break;
383 }
384
385 if (j >= 25)
386 return -1;
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300387
388 k += 48;
389 len -= 48;
390 } while (len > 0);
391 } else {
392 /* write bytes */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300393 dm_writeb(DM1105_I2CDAT, msgs[i].addr << 1);
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300394 for (byte = 0; byte < msgs[i].len; byte++) {
395 data = msgs[i].buf[byte];
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300396 dm_writeb(DM1105_I2CDAT + byte + 1, data);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300397 }
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300398 dm_writeb(DM1105_I2CCTR, 0x81 + msgs[i].len);
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300399 for (j = 0; j < 25; j++) {
400 mdelay(10);
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300401 status = dm_readb(DM1105_I2CSTS);
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300402 if ((status & 0xc0) == 0x40)
403 break;
404 }
405
406 if (j >= 25)
407 return -1;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300408 }
409 }
410 return num;
411 err:
412 return rc;
413}
414
415static u32 functionality(struct i2c_adapter *adap)
416{
417 return I2C_FUNC_I2C;
418}
419
420static struct i2c_algorithm dm1105_algo = {
421 .master_xfer = dm1105_i2c_xfer,
422 .functionality = functionality,
423};
424
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300425static inline struct dm1105_dev *feed_to_dm1105_dev(struct dvb_demux_feed *feed)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300426{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300427 return container_of(feed->demux, struct dm1105_dev, demux);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300428}
429
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300430static inline struct dm1105_dev *frontend_to_dm1105_dev(struct dvb_frontend *fe)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300431{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300432 return container_of(fe->dvb, struct dm1105_dev, dvb_adapter);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300433}
434
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300435static int dm1105_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300436{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300437 struct dm1105_dev *dev = frontend_to_dm1105_dev(fe);
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300438 u32 lnb_mask, lnb_13v, lnb_18v, lnb_off;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300439
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300440 switch (dev->boardnr) {
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300441 case DM1105_BOARD_AXESS_DM05:
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300442 lnb_mask = DM05_LNB_MASK;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300443 lnb_off = DM05_LNB_OFF;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300444 lnb_13v = DM05_LNB_13V;
445 lnb_18v = DM05_LNB_18V;
446 break;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300447 case DM1105_BOARD_DVBWORLD_2002:
448 case DM1105_BOARD_DVBWORLD_2004:
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300449 default:
450 lnb_mask = DM1105_LNB_MASK;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300451 lnb_off = DM1105_LNB_OFF;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300452 lnb_13v = DM1105_LNB_13V;
453 lnb_18v = DM1105_LNB_18V;
454 }
455
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300456 dm_writel(DM1105_GPIOCTR, lnb_mask);
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300457 if (voltage == SEC_VOLTAGE_18)
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300458 dm_writel(DM1105_GPIOVAL, lnb_18v);
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300459 else if (voltage == SEC_VOLTAGE_13)
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300460 dm_writel(DM1105_GPIOVAL, lnb_13v);
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300461 else
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300462 dm_writel(DM1105_GPIOVAL, lnb_off);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300463
464 return 0;
465}
466
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300467static void dm1105_set_dma_addr(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300468{
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300469 dm_writel(DM1105_STADR, cpu_to_le32(dev->dma_addr));
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300470}
471
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300472static int __devinit dm1105_dma_map(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300473{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300474 dev->ts_buf = pci_alloc_consistent(dev->pdev,
475 6 * DM1105_DMA_BYTES,
476 &dev->dma_addr);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300477
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300478 return !dev->ts_buf;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300479}
480
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300481static void dm1105_dma_unmap(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300482{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300483 pci_free_consistent(dev->pdev,
484 6 * DM1105_DMA_BYTES,
485 dev->ts_buf,
486 dev->dma_addr);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300487}
488
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300489static void dm1105_enable_irqs(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300490{
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300491 dm_writeb(DM1105_INTMAK, INTMAK_ALLMASK);
492 dm_writeb(DM1105_CR, 1);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300493}
494
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300495static void dm1105_disable_irqs(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300496{
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300497 dm_writeb(DM1105_INTMAK, INTMAK_IRM);
498 dm_writeb(DM1105_CR, 0);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300499}
500
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300501static int dm1105_start_feed(struct dvb_demux_feed *f)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300502{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300503 struct dm1105_dev *dev = feed_to_dm1105_dev(f);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300504
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300505 if (dev->full_ts_users++ == 0)
506 dm1105_enable_irqs(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300507
508 return 0;
509}
510
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300511static int dm1105_stop_feed(struct dvb_demux_feed *f)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300512{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300513 struct dm1105_dev *dev = feed_to_dm1105_dev(f);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300514
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300515 if (--dev->full_ts_users == 0)
516 dm1105_disable_irqs(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300517
518 return 0;
519}
520
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300521/* ir work handler */
522static void dm1105_emit_key(struct work_struct *work)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300523{
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300524 struct infrared *ir = container_of(work, struct infrared, work);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300525 u32 ircom = ir->ir_command;
526 u8 data;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300527
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300528 if (ir_debug)
529 printk(KERN_INFO "%s: received byte 0x%04x\n", __func__, ircom);
530
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300531 data = (ircom >> 8) & 0x7f;
532
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300533 ir_input_keydown(ir->input_dev, &ir->ir, data);
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300534 ir_input_nokey(ir->input_dev, &ir->ir);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300535}
536
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300537/* work handler */
538static void dm1105_dmx_buffer(struct work_struct *work)
539{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300540 struct dm1105_dev *dev = container_of(work, struct dm1105_dev, work);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300541 unsigned int nbpackets;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300542 u32 oldwrp = dev->wrp;
543 u32 nextwrp = dev->nextwrp;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300544
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300545 if (!((dev->ts_buf[oldwrp] == 0x47) &&
546 (dev->ts_buf[oldwrp + 188] == 0x47) &&
547 (dev->ts_buf[oldwrp + 188 * 2] == 0x47))) {
548 dev->PacketErrorCount++;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300549 /* bad packet found */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300550 if ((dev->PacketErrorCount >= 2) &&
551 (dev->dmarst == 0)) {
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300552 dm_writeb(DM1105_RST, 1);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300553 dev->wrp = 0;
554 dev->PacketErrorCount = 0;
555 dev->dmarst = 0;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300556 return;
557 }
558 }
559
560 if (nextwrp < oldwrp) {
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300561 memcpy(dev->ts_buf + dev->buffer_size, dev->ts_buf, nextwrp);
562 nbpackets = ((dev->buffer_size - oldwrp) + nextwrp) / 188;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300563 } else
564 nbpackets = (nextwrp - oldwrp) / 188;
565
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300566 dev->wrp = nextwrp;
567 dvb_dmx_swfilter_packets(&dev->demux, &dev->ts_buf[oldwrp], nbpackets);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300568}
569
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300570static irqreturn_t dm1105_irq(int irq, void *dev_id)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300571{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300572 struct dm1105_dev *dev = dev_id;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300573
574 /* Read-Write INSTS Ack's Interrupt for DM1105 chip 16.03.2008 */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300575 unsigned int intsts = dm_readb(DM1105_INTSTS);
576 dm_writeb(DM1105_INTSTS, intsts);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300577
578 switch (intsts) {
579 case INTSTS_TSIRQ:
580 case (INTSTS_TSIRQ | INTSTS_IR):
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300581 dev->nextwrp = dm_readl(DM1105_WRP) - dm_readl(DM1105_STADR);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300582 queue_work(dev->wq, &dev->work);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300583 break;
584 case INTSTS_IR:
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300585 dev->ir.ir_command = dm_readl(DM1105_IRCODE);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300586 schedule_work(&dev->ir.work);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300587 break;
588 }
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300589
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300590 return IRQ_HANDLED;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300591}
592
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300593int __devinit dm1105_ir_init(struct dm1105_dev *dm1105)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300594{
595 struct input_dev *input_dev;
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300596 struct ir_scancode_table *ir_codes = &ir_codes_dm1105_nec_table;
Mauro Carvalho Chehab971e8292009-12-14 13:53:37 -0300597 u64 ir_type = IR_TYPE_OTHER;
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300598 int err = -ENOMEM;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300599
600 input_dev = input_allocate_device();
601 if (!input_dev)
602 return -ENOMEM;
603
604 dm1105->ir.input_dev = input_dev;
605 snprintf(dm1105->ir.input_phys, sizeof(dm1105->ir.input_phys),
606 "pci-%s/ir0", pci_name(dm1105->pdev));
607
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300608 err = ir_input_init(input_dev, &dm1105->ir.ir, ir_type);
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -0300609 if (err < 0) {
610 input_free_device(input_dev);
611 return err;
612 }
613
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300614 input_dev->name = "DVB on-card IR receiver";
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300615 input_dev->phys = dm1105->ir.input_phys;
616 input_dev->id.bustype = BUS_PCI;
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300617 input_dev->id.version = 1;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300618 if (dm1105->pdev->subsystem_vendor) {
619 input_dev->id.vendor = dm1105->pdev->subsystem_vendor;
620 input_dev->id.product = dm1105->pdev->subsystem_device;
621 } else {
622 input_dev->id.vendor = dm1105->pdev->vendor;
623 input_dev->id.product = dm1105->pdev->device;
624 }
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300625
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300626 input_dev->dev.parent = &dm1105->pdev->dev;
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300627
628 INIT_WORK(&dm1105->ir.work, dm1105_emit_key);
629
Mauro Carvalho Chehabe93854d2009-12-14 00:16:55 -0300630 err = ir_input_register(input_dev, ir_codes, NULL);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300631
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300632 return err;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300633}
634
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300635void __devexit dm1105_ir_exit(struct dm1105_dev *dm1105)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300636{
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300637 ir_input_unregister(dm1105->ir.input_dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300638}
639
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300640static int __devinit dm1105_hw_init(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300641{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300642 dm1105_disable_irqs(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300643
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300644 dm_writeb(DM1105_HOST_CTR, 0);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300645
646 /*DATALEN 188,*/
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300647 dm_writeb(DM1105_DTALENTH, 188);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300648 /*TS_STRT TS_VALP MSBFIRST TS_MODE ALPAS TSPES*/
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300649 dm_writew(DM1105_TSCTR, 0xc10a);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300650
651 /* map DMA and set address */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300652 dm1105_dma_map(dev);
653 dm1105_set_dma_addr(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300654 /* big buffer */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300655 dm_writel(DM1105_RLEN, 5 * DM1105_DMA_BYTES);
656 dm_writeb(DM1105_INTCNT, 47);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300657
658 /* IR NEC mode enable */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300659 dm_writeb(DM1105_IRCTR, (DM1105_IR_EN | DM1105_SYS_CHK));
660 dm_writeb(DM1105_IRMODE, 0);
661 dm_writew(DM1105_SYSTEMCODE, 0);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300662
663 return 0;
664}
665
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300666static void dm1105_hw_exit(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300667{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300668 dm1105_disable_irqs(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300669
670 /* IR disable */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300671 dm_writeb(DM1105_IRCTR, 0);
672 dm_writeb(DM1105_INTMAK, INTMAK_NONEMASK);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300673
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300674 dm1105_dma_unmap(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300675}
Igor M. Liplianine4aab642008-09-23 15:43:57 -0300676
Igor M. Liplianind4305c62008-10-17 13:45:55 -0300677static struct stv0299_config sharp_z0194a_config = {
678 .demod_address = 0x68,
679 .inittab = sharp_z0194a_inittab,
680 .mclk = 88000000UL,
681 .invert = 1,
682 .skip_reinit = 0,
683 .lock_output = STV0299_LOCKOUTPUT_1,
684 .volt13_op0_op1 = STV0299_VOLT13_OP1,
685 .min_delay_ms = 100,
686 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
687};
688
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300689static struct stv0288_config earda_config = {
690 .demod_address = 0x68,
691 .min_delay_ms = 100,
692};
693
694static struct si21xx_config serit_config = {
695 .demod_address = 0x68,
696 .min_delay_ms = 100,
697
698};
699
700static struct cx24116_config serit_sp2633_config = {
701 .demod_address = 0x55,
702};
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300703
Igor M. Liplianinb4a0e812010-01-17 10:32:26 -0300704static struct ds3000_config dvbworld_ds3000_config = {
705 .demod_address = 0x68,
706};
707
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300708static int __devinit frontend_init(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300709{
710 int ret;
711
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300712 switch (dev->boardnr) {
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300713 case DM1105_BOARD_DVBWORLD_2004:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300714 dev->fe = dvb_attach(
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300715 cx24116_attach, &serit_sp2633_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300716 &dev->i2c_adap);
717 if (dev->fe) {
718 dev->fe->ops.set_voltage = dm1105_set_voltage;
Igor M. Liplianinb4a0e812010-01-17 10:32:26 -0300719 break;
720 }
721
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300722 dev->fe = dvb_attach(
Igor M. Liplianinb4a0e812010-01-17 10:32:26 -0300723 ds3000_attach, &dvbworld_ds3000_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300724 &dev->i2c_adap);
725 if (dev->fe)
726 dev->fe->ops.set_voltage = dm1105_set_voltage;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300727
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300728 break;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300729 case DM1105_BOARD_DVBWORLD_2002:
730 case DM1105_BOARD_AXESS_DM05:
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300731 default:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300732 dev->fe = dvb_attach(
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300733 stv0299_attach, &sharp_z0194a_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300734 &dev->i2c_adap);
735 if (dev->fe) {
736 dev->fe->ops.set_voltage = dm1105_set_voltage;
737 dvb_attach(dvb_pll_attach, dev->fe, 0x60,
738 &dev->i2c_adap, DVB_PLL_OPERA1);
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300739 break;
740 }
741
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300742 dev->fe = dvb_attach(
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300743 stv0288_attach, &earda_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300744 &dev->i2c_adap);
745 if (dev->fe) {
746 dev->fe->ops.set_voltage = dm1105_set_voltage;
747 dvb_attach(stb6000_attach, dev->fe, 0x61,
748 &dev->i2c_adap);
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300749 break;
750 }
751
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300752 dev->fe = dvb_attach(
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300753 si21xx_attach, &serit_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300754 &dev->i2c_adap);
755 if (dev->fe)
756 dev->fe->ops.set_voltage = dm1105_set_voltage;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300757
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300758 }
759
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300760 if (!dev->fe) {
761 dev_err(&dev->pdev->dev, "could not attach frontend\n");
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300762 return -ENODEV;
763 }
764
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300765 ret = dvb_register_frontend(&dev->dvb_adapter, dev->fe);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300766 if (ret < 0) {
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300767 if (dev->fe->ops.release)
768 dev->fe->ops.release(dev->fe);
769 dev->fe = NULL;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300770 return ret;
771 }
772
773 return 0;
774}
775
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300776static void __devinit dm1105_read_mac(struct dm1105_dev *dev, u8 *mac)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300777{
778 static u8 command[1] = { 0x28 };
779
780 struct i2c_msg msg[] = {
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300781 {
782 .addr = IIC_24C01_addr >> 1,
783 .flags = 0,
784 .buf = command,
785 .len = 1
786 }, {
787 .addr = IIC_24C01_addr >> 1,
788 .flags = I2C_M_RD,
789 .buf = mac,
790 .len = 6
791 },
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300792 };
793
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300794 dm1105_i2c_xfer(&dev->i2c_adap, msg , 2);
795 dev_info(&dev->pdev->dev, "MAC %pM\n", mac);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300796}
797
798static int __devinit dm1105_probe(struct pci_dev *pdev,
799 const struct pci_device_id *ent)
800{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300801 struct dm1105_dev *dev;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300802 struct dvb_adapter *dvb_adapter;
803 struct dvb_demux *dvbdemux;
804 struct dmx_demux *dmx;
805 int ret = -ENOMEM;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300806 int i;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300807
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300808 dev = kzalloc(sizeof(struct dm1105_dev), GFP_KERNEL);
809 if (!dev)
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300810 return -ENOMEM;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300811
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300812 /* board config */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300813 dev->nr = dm1105_devcount;
814 dev->boardnr = UNSET;
815 if (card[dev->nr] < ARRAY_SIZE(dm1105_boards))
816 dev->boardnr = card[dev->nr];
817 for (i = 0; UNSET == dev->boardnr &&
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300818 i < ARRAY_SIZE(dm1105_subids); i++)
819 if (pdev->subsystem_vendor ==
820 dm1105_subids[i].subvendor &&
821 pdev->subsystem_device ==
822 dm1105_subids[i].subdevice)
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300823 dev->boardnr = dm1105_subids[i].card;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300824
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300825 if (UNSET == dev->boardnr) {
826 dev->boardnr = DM1105_BOARD_UNKNOWN;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300827 dm1105_card_list(pdev);
828 }
829
830 dm1105_devcount++;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300831 dev->pdev = pdev;
832 dev->buffer_size = 5 * DM1105_DMA_BYTES;
833 dev->PacketErrorCount = 0;
834 dev->dmarst = 0;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300835
836 ret = pci_enable_device(pdev);
837 if (ret < 0)
838 goto err_kfree;
839
Yang Hongyang284901a2009-04-06 19:01:15 -0700840 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300841 if (ret < 0)
842 goto err_pci_disable_device;
843
844 pci_set_master(pdev);
845
846 ret = pci_request_regions(pdev, DRIVER_NAME);
847 if (ret < 0)
848 goto err_pci_disable_device;
849
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300850 dev->io_mem = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
851 if (!dev->io_mem) {
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300852 ret = -EIO;
853 goto err_pci_release_regions;
854 }
855
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300856 spin_lock_init(&dev->lock);
857 pci_set_drvdata(pdev, dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300858
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300859 ret = dm1105_hw_init(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300860 if (ret < 0)
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300861 goto err_pci_iounmap;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300862
863 /* i2c */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300864 i2c_set_adapdata(&dev->i2c_adap, dev);
865 strcpy(dev->i2c_adap.name, DRIVER_NAME);
866 dev->i2c_adap.owner = THIS_MODULE;
867 dev->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
868 dev->i2c_adap.dev.parent = &pdev->dev;
869 dev->i2c_adap.algo = &dm1105_algo;
870 dev->i2c_adap.algo_data = dev;
871 ret = i2c_add_adapter(&dev->i2c_adap);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300872
873 if (ret < 0)
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300874 goto err_dm1105_hw_exit;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300875
876 /* dvb */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300877 ret = dvb_register_adapter(&dev->dvb_adapter, DRIVER_NAME,
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300878 THIS_MODULE, &pdev->dev, adapter_nr);
879 if (ret < 0)
880 goto err_i2c_del_adapter;
881
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300882 dvb_adapter = &dev->dvb_adapter;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300883
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300884 dm1105_read_mac(dev, dvb_adapter->proposed_mac);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300885
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300886 dvbdemux = &dev->demux;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300887 dvbdemux->filternum = 256;
888 dvbdemux->feednum = 256;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300889 dvbdemux->start_feed = dm1105_start_feed;
890 dvbdemux->stop_feed = dm1105_stop_feed;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300891 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
892 DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
893 ret = dvb_dmx_init(dvbdemux);
894 if (ret < 0)
895 goto err_dvb_unregister_adapter;
896
897 dmx = &dvbdemux->dmx;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300898 dev->dmxdev.filternum = 256;
899 dev->dmxdev.demux = dmx;
900 dev->dmxdev.capabilities = 0;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300901
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300902 ret = dvb_dmxdev_init(&dev->dmxdev, dvb_adapter);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300903 if (ret < 0)
904 goto err_dvb_dmx_release;
905
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300906 dev->hw_frontend.source = DMX_FRONTEND_0;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300907
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300908 ret = dmx->add_frontend(dmx, &dev->hw_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300909 if (ret < 0)
910 goto err_dvb_dmxdev_release;
911
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300912 dev->mem_frontend.source = DMX_MEMORY_FE;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300913
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300914 ret = dmx->add_frontend(dmx, &dev->mem_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300915 if (ret < 0)
916 goto err_remove_hw_frontend;
917
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300918 ret = dmx->connect_frontend(dmx, &dev->hw_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300919 if (ret < 0)
920 goto err_remove_mem_frontend;
921
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300922 ret = frontend_init(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300923 if (ret < 0)
924 goto err_disconnect_frontend;
925
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300926 dvb_net_init(dvb_adapter, &dev->dvbnet, dmx);
927 dm1105_ir_init(dev);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300928
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300929 INIT_WORK(&dev->work, dm1105_dmx_buffer);
930 sprintf(dev->wqn, "%s/%d", dvb_adapter->name, dvb_adapter->num);
931 dev->wq = create_singlethread_workqueue(dev->wqn);
932 if (!dev->wq)
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300933 goto err_dvb_net;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300934
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300935 ret = request_irq(pdev->irq, dm1105_irq, IRQF_SHARED,
936 DRIVER_NAME, dev);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300937 if (ret < 0)
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300938 goto err_workqueue;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300939
940 return 0;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300941
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300942err_workqueue:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300943 destroy_workqueue(dev->wq);
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300944err_dvb_net:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300945 dvb_net_release(&dev->dvbnet);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300946err_disconnect_frontend:
947 dmx->disconnect_frontend(dmx);
948err_remove_mem_frontend:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300949 dmx->remove_frontend(dmx, &dev->mem_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300950err_remove_hw_frontend:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300951 dmx->remove_frontend(dmx, &dev->hw_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300952err_dvb_dmxdev_release:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300953 dvb_dmxdev_release(&dev->dmxdev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300954err_dvb_dmx_release:
955 dvb_dmx_release(dvbdemux);
956err_dvb_unregister_adapter:
957 dvb_unregister_adapter(dvb_adapter);
958err_i2c_del_adapter:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300959 i2c_del_adapter(&dev->i2c_adap);
960err_dm1105_hw_exit:
961 dm1105_hw_exit(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300962err_pci_iounmap:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300963 pci_iounmap(pdev, dev->io_mem);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300964err_pci_release_regions:
965 pci_release_regions(pdev);
966err_pci_disable_device:
967 pci_disable_device(pdev);
968err_kfree:
969 pci_set_drvdata(pdev, NULL);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300970 kfree(dev);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300971 return ret;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300972}
973
974static void __devexit dm1105_remove(struct pci_dev *pdev)
975{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300976 struct dm1105_dev *dev = pci_get_drvdata(pdev);
977 struct dvb_adapter *dvb_adapter = &dev->dvb_adapter;
978 struct dvb_demux *dvbdemux = &dev->demux;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300979 struct dmx_demux *dmx = &dvbdemux->dmx;
980
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300981 dm1105_ir_exit(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300982 dmx->close(dmx);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300983 dvb_net_release(&dev->dvbnet);
984 if (dev->fe)
985 dvb_unregister_frontend(dev->fe);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300986
987 dmx->disconnect_frontend(dmx);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300988 dmx->remove_frontend(dmx, &dev->mem_frontend);
989 dmx->remove_frontend(dmx, &dev->hw_frontend);
990 dvb_dmxdev_release(&dev->dmxdev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300991 dvb_dmx_release(dvbdemux);
992 dvb_unregister_adapter(dvb_adapter);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300993 if (&dev->i2c_adap)
994 i2c_del_adapter(&dev->i2c_adap);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300995
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300996 dm1105_hw_exit(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300997 synchronize_irq(pdev->irq);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300998 free_irq(pdev->irq, dev);
999 pci_iounmap(pdev, dev->io_mem);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -03001000 pci_release_regions(pdev);
1001 pci_disable_device(pdev);
1002 pci_set_drvdata(pdev, NULL);
Igor M. Liplianind8300df2009-07-22 17:30:25 -03001003 dm1105_devcount--;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -03001004 kfree(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -03001005}
1006
1007static struct pci_device_id dm1105_id_table[] __devinitdata = {
1008 {
1009 .vendor = PCI_VENDOR_ID_TRIGEM,
1010 .device = PCI_DEVICE_ID_DM1105,
1011 .subvendor = PCI_ANY_ID,
Igor M. Liplianind8300df2009-07-22 17:30:25 -03001012 .subdevice = PCI_ANY_ID,
Igor M. Liplianina611d0c2008-09-13 10:10:53 -03001013 }, {
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -03001014 .vendor = PCI_VENDOR_ID_AXESS,
1015 .device = PCI_DEVICE_ID_DM05,
Igor M. Liplianind8300df2009-07-22 17:30:25 -03001016 .subvendor = PCI_ANY_ID,
1017 .subdevice = PCI_ANY_ID,
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -03001018 }, {
Igor M. Liplianina611d0c2008-09-13 10:10:53 -03001019 /* empty */
1020 },
1021};
1022
1023MODULE_DEVICE_TABLE(pci, dm1105_id_table);
1024
1025static struct pci_driver dm1105_driver = {
1026 .name = DRIVER_NAME,
1027 .id_table = dm1105_id_table,
1028 .probe = dm1105_probe,
1029 .remove = __devexit_p(dm1105_remove),
1030};
1031
1032static int __init dm1105_init(void)
1033{
1034 return pci_register_driver(&dm1105_driver);
1035}
1036
1037static void __exit dm1105_exit(void)
1038{
1039 pci_unregister_driver(&dm1105_driver);
1040}
1041
1042module_init(dm1105_init);
1043module_exit(dm1105_exit);
1044
1045MODULE_AUTHOR("Igor M. Liplianin <liplianin@me.by>");
1046MODULE_DESCRIPTION("SDMC DM1105 DVB driver");
1047MODULE_LICENSE("GPL");