blob: bca07c0bcd01e4b872bd02a364bc760fd38735d5 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
David Härdeman15100d82010-04-15 18:46:15 -030031#include <media/ir-core.h>
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030032
33#include "demux.h"
34#include "dmxdev.h"
35#include "dvb_demux.h"
36#include "dvb_frontend.h"
37#include "dvb_net.h"
38#include "dvbdev.h"
39#include "dvb-pll.h"
40
41#include "stv0299.h"
Igor M. Liplianine4aab642008-09-23 15:43:57 -030042#include "stv0288.h"
43#include "stb6000.h"
Igor M. Liplianin04ad28c2008-09-16 18:21:11 -030044#include "si21xx.h"
Igor M. Liplianin35d9c422008-09-13 20:56:59 -030045#include "cx24116.h"
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030046#include "z0194a.h"
Igor M. Liplianinb4a0e812010-01-17 10:32:26 -030047#include "ds3000.h"
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030048
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -030049#define MODULE_NAME "dm1105"
50
Igor M. Liplianind8300df2009-07-22 17:30:25 -030051#define UNSET (-1U)
52
53#define DM1105_BOARD_NOAUTO UNSET
54#define DM1105_BOARD_UNKNOWN 0
55#define DM1105_BOARD_DVBWORLD_2002 1
56#define DM1105_BOARD_DVBWORLD_2004 2
57#define DM1105_BOARD_AXESS_DM05 3
58
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030059/* ----------------------------------------------- */
60/*
61 * PCI ID's
62 */
63#ifndef PCI_VENDOR_ID_TRIGEM
64#define PCI_VENDOR_ID_TRIGEM 0x109f
65#endif
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -030066#ifndef PCI_VENDOR_ID_AXESS
67#define PCI_VENDOR_ID_AXESS 0x195d
68#endif
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030069#ifndef PCI_DEVICE_ID_DM1105
70#define PCI_DEVICE_ID_DM1105 0x036f
71#endif
72#ifndef PCI_DEVICE_ID_DW2002
73#define PCI_DEVICE_ID_DW2002 0x2002
74#endif
75#ifndef PCI_DEVICE_ID_DW2004
76#define PCI_DEVICE_ID_DW2004 0x2004
77#endif
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -030078#ifndef PCI_DEVICE_ID_DM05
79#define PCI_DEVICE_ID_DM05 0x1105
80#endif
Igor M. Liplianina611d0c2008-09-13 10:10:53 -030081/* ----------------------------------------------- */
82/* sdmc dm1105 registers */
83
84/* TS Control */
85#define DM1105_TSCTR 0x00
86#define DM1105_DTALENTH 0x04
87
88/* GPIO Interface */
89#define DM1105_GPIOVAL 0x08
90#define DM1105_GPIOCTR 0x0c
91
92/* PID serial number */
93#define DM1105_PIDN 0x10
94
95/* Odd-even secret key select */
96#define DM1105_CWSEL 0x14
97
98/* Host Command Interface */
99#define DM1105_HOST_CTR 0x18
100#define DM1105_HOST_AD 0x1c
101
102/* PCI Interface */
103#define DM1105_CR 0x30
104#define DM1105_RST 0x34
105#define DM1105_STADR 0x38
106#define DM1105_RLEN 0x3c
107#define DM1105_WRP 0x40
108#define DM1105_INTCNT 0x44
109#define DM1105_INTMAK 0x48
110#define DM1105_INTSTS 0x4c
111
112/* CW Value */
113#define DM1105_ODD 0x50
114#define DM1105_EVEN 0x58
115
116/* PID Value */
117#define DM1105_PID 0x60
118
119/* IR Control */
120#define DM1105_IRCTR 0x64
121#define DM1105_IRMODE 0x68
122#define DM1105_SYSTEMCODE 0x6c
123#define DM1105_IRCODE 0x70
124
125/* Unknown Values */
126#define DM1105_ENCRYPT 0x74
127#define DM1105_VER 0x7c
128
129/* I2C Interface */
130#define DM1105_I2CCTR 0x80
131#define DM1105_I2CSTS 0x81
132#define DM1105_I2CDAT 0x82
133#define DM1105_I2C_RA 0x83
134/* ----------------------------------------------- */
135/* Interrupt Mask Bits */
136
137#define INTMAK_TSIRQM 0x01
138#define INTMAK_HIRQM 0x04
139#define INTMAK_IRM 0x08
140#define INTMAK_ALLMASK (INTMAK_TSIRQM | \
141 INTMAK_HIRQM | \
142 INTMAK_IRM)
143#define INTMAK_NONEMASK 0x00
144
145/* Interrupt Status Bits */
146#define INTSTS_TSIRQ 0x01
147#define INTSTS_HIRQ 0x04
148#define INTSTS_IR 0x08
149
150/* IR Control Bits */
151#define DM1105_IR_EN 0x01
152#define DM1105_SYS_CHK 0x02
153#define DM1105_REP_FLG 0x08
154
155/* EEPROM addr */
156#define IIC_24C01_addr 0xa0
157/* Max board count */
158#define DM1105_MAX 0x04
159
160#define DRIVER_NAME "dm1105"
161
162#define DM1105_DMA_PACKETS 47
163#define DM1105_DMA_PACKET_LENGTH (128*4)
164#define DM1105_DMA_BYTES (128 * 4 * DM1105_DMA_PACKETS)
165
166/* GPIO's for LNB power control */
167#define DM1105_LNB_MASK 0x00000000
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300168#define DM1105_LNB_OFF 0x00020000
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300169#define DM1105_LNB_13V 0x00010100
170#define DM1105_LNB_18V 0x00000100
171
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300172/* GPIO's for LNB power control for Axess DM05 */
173#define DM05_LNB_MASK 0x00000000
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300174#define DM05_LNB_OFF 0x00020000/* actually 13v */
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300175#define DM05_LNB_13V 0x00020000
176#define DM05_LNB_18V 0x00030000
177
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300178static unsigned int card[] = {[0 ... 3] = UNSET };
179module_param_array(card, int, NULL, 0444);
180MODULE_PARM_DESC(card, "card type");
181
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300182static int ir_debug;
183module_param(ir_debug, int, 0644);
184MODULE_PARM_DESC(ir_debug, "enable debugging information for IR decoding");
185
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300186static unsigned int dm1105_devcount;
187
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300188DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
189
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300190struct dm1105_board {
191 char *name;
192};
193
194struct dm1105_subid {
195 u16 subvendor;
196 u16 subdevice;
197 u32 card;
198};
199
200static const struct dm1105_board dm1105_boards[] = {
201 [DM1105_BOARD_UNKNOWN] = {
202 .name = "UNKNOWN/GENERIC",
203 },
204 [DM1105_BOARD_DVBWORLD_2002] = {
205 .name = "DVBWorld PCI 2002",
206 },
207 [DM1105_BOARD_DVBWORLD_2004] = {
208 .name = "DVBWorld PCI 2004",
209 },
210 [DM1105_BOARD_AXESS_DM05] = {
211 .name = "Axess/EasyTv DM05",
212 },
213};
214
215static const struct dm1105_subid dm1105_subids[] = {
216 {
217 .subvendor = 0x0000,
218 .subdevice = 0x2002,
219 .card = DM1105_BOARD_DVBWORLD_2002,
220 }, {
221 .subvendor = 0x0001,
222 .subdevice = 0x2002,
223 .card = DM1105_BOARD_DVBWORLD_2002,
224 }, {
225 .subvendor = 0x0000,
226 .subdevice = 0x2004,
227 .card = DM1105_BOARD_DVBWORLD_2004,
228 }, {
229 .subvendor = 0x0001,
230 .subdevice = 0x2004,
231 .card = DM1105_BOARD_DVBWORLD_2004,
232 }, {
233 .subvendor = 0x195d,
234 .subdevice = 0x1105,
235 .card = DM1105_BOARD_AXESS_DM05,
236 },
237};
238
239static void dm1105_card_list(struct pci_dev *pci)
240{
241 int i;
242
243 if (0 == pci->subsystem_vendor &&
244 0 == pci->subsystem_device) {
245 printk(KERN_ERR
246 "dm1105: Your board has no valid PCI Subsystem ID\n"
247 "dm1105: and thus can't be autodetected\n"
248 "dm1105: Please pass card=<n> insmod option to\n"
249 "dm1105: workaround that. Redirect complaints to\n"
250 "dm1105: the vendor of the TV card. Best regards,\n"
251 "dm1105: -- tux\n");
252 } else {
253 printk(KERN_ERR
254 "dm1105: Your board isn't known (yet) to the driver.\n"
255 "dm1105: You can try to pick one of the existing\n"
256 "dm1105: card configs via card=<n> insmod option.\n"
257 "dm1105: Updating to the latest version might help\n"
258 "dm1105: as well.\n");
259 }
260 printk(KERN_ERR "Here is a list of valid choices for the card=<n> "
261 "insmod option:\n");
262 for (i = 0; i < ARRAY_SIZE(dm1105_boards); i++)
263 printk(KERN_ERR "dm1105: card=%d -> %s\n",
264 i, dm1105_boards[i].name);
265}
266
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300267/* infrared remote control */
268struct infrared {
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300269 struct input_dev *input_dev;
270 char input_phys[32];
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300271 struct work_struct work;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300272 u32 ir_command;
273};
274
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300275struct dm1105_dev {
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300276 /* pci */
277 struct pci_dev *pdev;
278 u8 __iomem *io_mem;
279
280 /* ir */
281 struct infrared ir;
282
283 /* dvb */
284 struct dmx_frontend hw_frontend;
285 struct dmx_frontend mem_frontend;
286 struct dmxdev dmxdev;
287 struct dvb_adapter dvb_adapter;
288 struct dvb_demux demux;
289 struct dvb_frontend *fe;
290 struct dvb_net dvbnet;
291 unsigned int full_ts_users;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300292 unsigned int boardnr;
293 int nr;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300294
295 /* i2c */
296 struct i2c_adapter i2c_adap;
297
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300298 /* irq */
299 struct work_struct work;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300300 struct workqueue_struct *wq;
301 char wqn[16];
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300302
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300303 /* dma */
304 dma_addr_t dma_addr;
305 unsigned char *ts_buf;
306 u32 wrp;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300307 u32 nextwrp;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300308 u32 buffer_size;
309 unsigned int PacketErrorCount;
310 unsigned int dmarst;
311 spinlock_t lock;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300312};
313
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300314#define dm_io_mem(reg) ((unsigned long)(&dev->io_mem[reg]))
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300315
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300316#define dm_readb(reg) inb(dm_io_mem(reg))
317#define dm_writeb(reg, value) outb((value), (dm_io_mem(reg)))
318
319#define dm_readw(reg) inw(dm_io_mem(reg))
320#define dm_writew(reg, value) outw((value), (dm_io_mem(reg)))
321
322#define dm_readl(reg) inl(dm_io_mem(reg))
323#define dm_writel(reg, value) outl((value), (dm_io_mem(reg)))
324
325#define dm_andorl(reg, mask, value) \
326 outl((inl(dm_io_mem(reg)) & ~(mask)) |\
327 ((value) & (mask)), (dm_io_mem(reg)))
328
329#define dm_setl(reg, bit) dm_andorl((reg), (bit), (bit))
330#define dm_clearl(reg, bit) dm_andorl((reg), (bit), 0)
331
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300332static int dm1105_i2c_xfer(struct i2c_adapter *i2c_adap,
333 struct i2c_msg *msgs, int num)
334{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300335 struct dm1105_dev *dev ;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300336
337 int addr, rc, i, j, k, len, byte, data;
338 u8 status;
339
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300340 dev = i2c_adap->algo_data;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300341 for (i = 0; i < num; i++) {
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300342 dm_writeb(DM1105_I2CCTR, 0x00);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300343 if (msgs[i].flags & I2C_M_RD) {
344 /* read bytes */
345 addr = msgs[i].addr << 1;
346 addr |= 1;
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300347 dm_writeb(DM1105_I2CDAT, addr);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300348 for (byte = 0; byte < msgs[i].len; byte++)
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300349 dm_writeb(DM1105_I2CDAT + byte + 1, 0);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300350
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300351 dm_writeb(DM1105_I2CCTR, 0x81 + msgs[i].len);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300352 for (j = 0; j < 55; j++) {
353 mdelay(10);
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300354 status = dm_readb(DM1105_I2CSTS);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300355 if ((status & 0xc0) == 0x40)
356 break;
357 }
358 if (j >= 55)
359 return -1;
360
361 for (byte = 0; byte < msgs[i].len; byte++) {
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300362 rc = dm_readb(DM1105_I2CDAT + byte + 1);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300363 if (rc < 0)
364 goto err;
365 msgs[i].buf[byte] = rc;
366 }
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300367 } else if ((msgs[i].buf[0] == 0xf7) && (msgs[i].addr == 0x55)) {
368 /* prepaired for cx24116 firmware */
369 /* Write in small blocks */
370 len = msgs[i].len - 1;
371 k = 1;
372 do {
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300373 dm_writeb(DM1105_I2CDAT, msgs[i].addr << 1);
374 dm_writeb(DM1105_I2CDAT + 1, 0xf7);
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300375 for (byte = 0; byte < (len > 48 ? 48 : len); byte++) {
376 data = msgs[i].buf[k + byte];
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300377 dm_writeb(DM1105_I2CDAT + byte + 2, data);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300378 }
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300379 dm_writeb(DM1105_I2CCTR, 0x82 + (len > 48 ? 48 : len));
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300380 for (j = 0; j < 25; j++) {
381 mdelay(10);
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300382 status = dm_readb(DM1105_I2CSTS);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300383 if ((status & 0xc0) == 0x40)
384 break;
385 }
386
387 if (j >= 25)
388 return -1;
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300389
390 k += 48;
391 len -= 48;
392 } while (len > 0);
393 } else {
394 /* write bytes */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300395 dm_writeb(DM1105_I2CDAT, msgs[i].addr << 1);
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300396 for (byte = 0; byte < msgs[i].len; byte++) {
397 data = msgs[i].buf[byte];
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300398 dm_writeb(DM1105_I2CDAT + byte + 1, data);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300399 }
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300400 dm_writeb(DM1105_I2CCTR, 0x81 + msgs[i].len);
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300401 for (j = 0; j < 25; j++) {
402 mdelay(10);
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300403 status = dm_readb(DM1105_I2CSTS);
Igor M. Liplianined7c8472010-01-17 11:15:06 -0300404 if ((status & 0xc0) == 0x40)
405 break;
406 }
407
408 if (j >= 25)
409 return -1;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300410 }
411 }
412 return num;
413 err:
414 return rc;
415}
416
417static u32 functionality(struct i2c_adapter *adap)
418{
419 return I2C_FUNC_I2C;
420}
421
422static struct i2c_algorithm dm1105_algo = {
423 .master_xfer = dm1105_i2c_xfer,
424 .functionality = functionality,
425};
426
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300427static inline struct dm1105_dev *feed_to_dm1105_dev(struct dvb_demux_feed *feed)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300428{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300429 return container_of(feed->demux, struct dm1105_dev, demux);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300430}
431
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300432static inline struct dm1105_dev *frontend_to_dm1105_dev(struct dvb_frontend *fe)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300433{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300434 return container_of(fe->dvb, struct dm1105_dev, dvb_adapter);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300435}
436
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300437static int dm1105_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300438{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300439 struct dm1105_dev *dev = frontend_to_dm1105_dev(fe);
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300440 u32 lnb_mask, lnb_13v, lnb_18v, lnb_off;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300441
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300442 switch (dev->boardnr) {
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300443 case DM1105_BOARD_AXESS_DM05:
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300444 lnb_mask = DM05_LNB_MASK;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300445 lnb_off = DM05_LNB_OFF;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300446 lnb_13v = DM05_LNB_13V;
447 lnb_18v = DM05_LNB_18V;
448 break;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300449 case DM1105_BOARD_DVBWORLD_2002:
450 case DM1105_BOARD_DVBWORLD_2004:
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300451 default:
452 lnb_mask = DM1105_LNB_MASK;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300453 lnb_off = DM1105_LNB_OFF;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300454 lnb_13v = DM1105_LNB_13V;
455 lnb_18v = DM1105_LNB_18V;
456 }
457
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300458 dm_writel(DM1105_GPIOCTR, lnb_mask);
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300459 if (voltage == SEC_VOLTAGE_18)
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300460 dm_writel(DM1105_GPIOVAL, lnb_18v);
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300461 else if (voltage == SEC_VOLTAGE_13)
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300462 dm_writel(DM1105_GPIOVAL, lnb_13v);
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300463 else
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300464 dm_writel(DM1105_GPIOVAL, lnb_off);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300465
466 return 0;
467}
468
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300469static void dm1105_set_dma_addr(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300470{
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300471 dm_writel(DM1105_STADR, cpu_to_le32(dev->dma_addr));
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300472}
473
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300474static int __devinit dm1105_dma_map(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300475{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300476 dev->ts_buf = pci_alloc_consistent(dev->pdev,
477 6 * DM1105_DMA_BYTES,
478 &dev->dma_addr);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300479
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300480 return !dev->ts_buf;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300481}
482
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300483static void dm1105_dma_unmap(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300484{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300485 pci_free_consistent(dev->pdev,
486 6 * DM1105_DMA_BYTES,
487 dev->ts_buf,
488 dev->dma_addr);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300489}
490
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300491static void dm1105_enable_irqs(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300492{
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300493 dm_writeb(DM1105_INTMAK, INTMAK_ALLMASK);
494 dm_writeb(DM1105_CR, 1);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300495}
496
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300497static void dm1105_disable_irqs(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300498{
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300499 dm_writeb(DM1105_INTMAK, INTMAK_IRM);
500 dm_writeb(DM1105_CR, 0);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300501}
502
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300503static int dm1105_start_feed(struct dvb_demux_feed *f)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300504{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300505 struct dm1105_dev *dev = feed_to_dm1105_dev(f);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300506
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300507 if (dev->full_ts_users++ == 0)
508 dm1105_enable_irqs(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300509
510 return 0;
511}
512
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300513static int dm1105_stop_feed(struct dvb_demux_feed *f)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300514{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300515 struct dm1105_dev *dev = feed_to_dm1105_dev(f);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300516
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300517 if (--dev->full_ts_users == 0)
518 dm1105_disable_irqs(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300519
520 return 0;
521}
522
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300523/* ir work handler */
524static void dm1105_emit_key(struct work_struct *work)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300525{
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300526 struct infrared *ir = container_of(work, struct infrared, work);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300527 u32 ircom = ir->ir_command;
528 u8 data;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300529
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300530 if (ir_debug)
531 printk(KERN_INFO "%s: received byte 0x%04x\n", __func__, ircom);
532
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300533 data = (ircom >> 8) & 0x7f;
534
David Härdeman15100d82010-04-15 18:46:15 -0300535 ir_keydown(ir->input_dev, data, 0);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300536}
537
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300538/* work handler */
539static void dm1105_dmx_buffer(struct work_struct *work)
540{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300541 struct dm1105_dev *dev = container_of(work, struct dm1105_dev, work);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300542 unsigned int nbpackets;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300543 u32 oldwrp = dev->wrp;
544 u32 nextwrp = dev->nextwrp;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300545
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300546 if (!((dev->ts_buf[oldwrp] == 0x47) &&
547 (dev->ts_buf[oldwrp + 188] == 0x47) &&
548 (dev->ts_buf[oldwrp + 188 * 2] == 0x47))) {
549 dev->PacketErrorCount++;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300550 /* bad packet found */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300551 if ((dev->PacketErrorCount >= 2) &&
552 (dev->dmarst == 0)) {
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300553 dm_writeb(DM1105_RST, 1);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300554 dev->wrp = 0;
555 dev->PacketErrorCount = 0;
556 dev->dmarst = 0;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300557 return;
558 }
559 }
560
561 if (nextwrp < oldwrp) {
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300562 memcpy(dev->ts_buf + dev->buffer_size, dev->ts_buf, nextwrp);
563 nbpackets = ((dev->buffer_size - oldwrp) + nextwrp) / 188;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300564 } else
565 nbpackets = (nextwrp - oldwrp) / 188;
566
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300567 dev->wrp = nextwrp;
568 dvb_dmx_swfilter_packets(&dev->demux, &dev->ts_buf[oldwrp], nbpackets);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300569}
570
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300571static irqreturn_t dm1105_irq(int irq, void *dev_id)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300572{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300573 struct dm1105_dev *dev = dev_id;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300574
575 /* Read-Write INSTS Ack's Interrupt for DM1105 chip 16.03.2008 */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300576 unsigned int intsts = dm_readb(DM1105_INTSTS);
577 dm_writeb(DM1105_INTSTS, intsts);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300578
579 switch (intsts) {
580 case INTSTS_TSIRQ:
581 case (INTSTS_TSIRQ | INTSTS_IR):
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300582 dev->nextwrp = dm_readl(DM1105_WRP) - dm_readl(DM1105_STADR);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300583 queue_work(dev->wq, &dev->work);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300584 break;
585 case INTSTS_IR:
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300586 dev->ir.ir_command = dm_readl(DM1105_IRCODE);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300587 schedule_work(&dev->ir.work);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300588 break;
589 }
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300590
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300591 return IRQ_HANDLED;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300592}
593
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300594int __devinit dm1105_ir_init(struct dm1105_dev *dm1105)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300595{
596 struct input_dev *input_dev;
Igor M. Liplianin84b14f12010-05-26 23:31:21 -0300597 char *ir_codes = RC_MAP_DM1105_NEC;
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
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300608 input_dev->name = "DVB on-card IR receiver";
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300609 input_dev->phys = dm1105->ir.input_phys;
610 input_dev->id.bustype = BUS_PCI;
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300611 input_dev->id.version = 1;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300612 if (dm1105->pdev->subsystem_vendor) {
613 input_dev->id.vendor = dm1105->pdev->subsystem_vendor;
614 input_dev->id.product = dm1105->pdev->subsystem_device;
615 } else {
616 input_dev->id.vendor = dm1105->pdev->vendor;
617 input_dev->id.product = dm1105->pdev->device;
618 }
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300619
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300620 input_dev->dev.parent = &dm1105->pdev->dev;
Igor M. Liplianinb72857d2009-02-26 03:49:44 -0300621
622 INIT_WORK(&dm1105->ir.work, dm1105_emit_key);
623
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300624 err = ir_input_register(input_dev, ir_codes, NULL, MODULE_NAME);
David Härdeman15100d82010-04-15 18:46:15 -0300625 if (err < 0) {
626 input_free_device(input_dev);
627 return err;
628 }
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300629
David Härdeman15100d82010-04-15 18:46:15 -0300630 return 0;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300631}
632
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300633void __devexit dm1105_ir_exit(struct dm1105_dev *dm1105)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300634{
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300635 ir_input_unregister(dm1105->ir.input_dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300636}
637
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300638static int __devinit dm1105_hw_init(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300639{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300640 dm1105_disable_irqs(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300641
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300642 dm_writeb(DM1105_HOST_CTR, 0);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300643
644 /*DATALEN 188,*/
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300645 dm_writeb(DM1105_DTALENTH, 188);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300646 /*TS_STRT TS_VALP MSBFIRST TS_MODE ALPAS TSPES*/
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300647 dm_writew(DM1105_TSCTR, 0xc10a);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300648
649 /* map DMA and set address */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300650 dm1105_dma_map(dev);
651 dm1105_set_dma_addr(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300652 /* big buffer */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300653 dm_writel(DM1105_RLEN, 5 * DM1105_DMA_BYTES);
654 dm_writeb(DM1105_INTCNT, 47);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300655
656 /* IR NEC mode enable */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300657 dm_writeb(DM1105_IRCTR, (DM1105_IR_EN | DM1105_SYS_CHK));
658 dm_writeb(DM1105_IRMODE, 0);
659 dm_writew(DM1105_SYSTEMCODE, 0);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300660
661 return 0;
662}
663
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300664static void dm1105_hw_exit(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300665{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300666 dm1105_disable_irqs(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300667
668 /* IR disable */
Igor M. Liplianin5eb32912010-01-17 12:23:04 -0300669 dm_writeb(DM1105_IRCTR, 0);
670 dm_writeb(DM1105_INTMAK, INTMAK_NONEMASK);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300671
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300672 dm1105_dma_unmap(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300673}
Igor M. Liplianine4aab642008-09-23 15:43:57 -0300674
Igor M. Liplianind4305c62008-10-17 13:45:55 -0300675static struct stv0299_config sharp_z0194a_config = {
676 .demod_address = 0x68,
677 .inittab = sharp_z0194a_inittab,
678 .mclk = 88000000UL,
679 .invert = 1,
680 .skip_reinit = 0,
681 .lock_output = STV0299_LOCKOUTPUT_1,
682 .volt13_op0_op1 = STV0299_VOLT13_OP1,
683 .min_delay_ms = 100,
684 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
685};
686
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300687static struct stv0288_config earda_config = {
688 .demod_address = 0x68,
689 .min_delay_ms = 100,
690};
691
692static struct si21xx_config serit_config = {
693 .demod_address = 0x68,
694 .min_delay_ms = 100,
695
696};
697
698static struct cx24116_config serit_sp2633_config = {
699 .demod_address = 0x55,
700};
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300701
Igor M. Liplianinb4a0e812010-01-17 10:32:26 -0300702static struct ds3000_config dvbworld_ds3000_config = {
703 .demod_address = 0x68,
704};
705
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300706static int __devinit frontend_init(struct dm1105_dev *dev)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300707{
708 int ret;
709
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300710 switch (dev->boardnr) {
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300711 case DM1105_BOARD_DVBWORLD_2004:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300712 dev->fe = dvb_attach(
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300713 cx24116_attach, &serit_sp2633_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300714 &dev->i2c_adap);
715 if (dev->fe) {
716 dev->fe->ops.set_voltage = dm1105_set_voltage;
Igor M. Liplianinb4a0e812010-01-17 10:32:26 -0300717 break;
718 }
719
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300720 dev->fe = dvb_attach(
Igor M. Liplianinb4a0e812010-01-17 10:32:26 -0300721 ds3000_attach, &dvbworld_ds3000_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300722 &dev->i2c_adap);
723 if (dev->fe)
724 dev->fe->ops.set_voltage = dm1105_set_voltage;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300725
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300726 break;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300727 case DM1105_BOARD_DVBWORLD_2002:
728 case DM1105_BOARD_AXESS_DM05:
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300729 default:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300730 dev->fe = dvb_attach(
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300731 stv0299_attach, &sharp_z0194a_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300732 &dev->i2c_adap);
733 if (dev->fe) {
734 dev->fe->ops.set_voltage = dm1105_set_voltage;
735 dvb_attach(dvb_pll_attach, dev->fe, 0x60,
736 &dev->i2c_adap, DVB_PLL_OPERA1);
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300737 break;
738 }
739
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300740 dev->fe = dvb_attach(
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300741 stv0288_attach, &earda_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300742 &dev->i2c_adap);
743 if (dev->fe) {
744 dev->fe->ops.set_voltage = dm1105_set_voltage;
745 dvb_attach(stb6000_attach, dev->fe, 0x61,
746 &dev->i2c_adap);
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300747 break;
748 }
749
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300750 dev->fe = dvb_attach(
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300751 si21xx_attach, &serit_config,
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300752 &dev->i2c_adap);
753 if (dev->fe)
754 dev->fe->ops.set_voltage = dm1105_set_voltage;
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300755
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300756 }
757
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300758 if (!dev->fe) {
759 dev_err(&dev->pdev->dev, "could not attach frontend\n");
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300760 return -ENODEV;
761 }
762
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300763 ret = dvb_register_frontend(&dev->dvb_adapter, dev->fe);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300764 if (ret < 0) {
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300765 if (dev->fe->ops.release)
766 dev->fe->ops.release(dev->fe);
767 dev->fe = NULL;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300768 return ret;
769 }
770
771 return 0;
772}
773
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300774static void __devinit dm1105_read_mac(struct dm1105_dev *dev, u8 *mac)
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300775{
776 static u8 command[1] = { 0x28 };
777
778 struct i2c_msg msg[] = {
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300779 {
780 .addr = IIC_24C01_addr >> 1,
781 .flags = 0,
782 .buf = command,
783 .len = 1
784 }, {
785 .addr = IIC_24C01_addr >> 1,
786 .flags = I2C_M_RD,
787 .buf = mac,
788 .len = 6
789 },
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300790 };
791
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300792 dm1105_i2c_xfer(&dev->i2c_adap, msg , 2);
793 dev_info(&dev->pdev->dev, "MAC %pM\n", mac);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300794}
795
796static int __devinit dm1105_probe(struct pci_dev *pdev,
797 const struct pci_device_id *ent)
798{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300799 struct dm1105_dev *dev;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300800 struct dvb_adapter *dvb_adapter;
801 struct dvb_demux *dvbdemux;
802 struct dmx_demux *dmx;
803 int ret = -ENOMEM;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300804 int i;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300805
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300806 dev = kzalloc(sizeof(struct dm1105_dev), GFP_KERNEL);
807 if (!dev)
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300808 return -ENOMEM;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300809
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300810 /* board config */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300811 dev->nr = dm1105_devcount;
812 dev->boardnr = UNSET;
813 if (card[dev->nr] < ARRAY_SIZE(dm1105_boards))
814 dev->boardnr = card[dev->nr];
815 for (i = 0; UNSET == dev->boardnr &&
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300816 i < ARRAY_SIZE(dm1105_subids); i++)
817 if (pdev->subsystem_vendor ==
818 dm1105_subids[i].subvendor &&
819 pdev->subsystem_device ==
820 dm1105_subids[i].subdevice)
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300821 dev->boardnr = dm1105_subids[i].card;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300822
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300823 if (UNSET == dev->boardnr) {
824 dev->boardnr = DM1105_BOARD_UNKNOWN;
Igor M. Liplianind8300df2009-07-22 17:30:25 -0300825 dm1105_card_list(pdev);
826 }
827
828 dm1105_devcount++;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300829 dev->pdev = pdev;
830 dev->buffer_size = 5 * DM1105_DMA_BYTES;
831 dev->PacketErrorCount = 0;
832 dev->dmarst = 0;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300833
834 ret = pci_enable_device(pdev);
835 if (ret < 0)
836 goto err_kfree;
837
Yang Hongyang284901a2009-04-06 19:01:15 -0700838 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300839 if (ret < 0)
840 goto err_pci_disable_device;
841
842 pci_set_master(pdev);
843
844 ret = pci_request_regions(pdev, DRIVER_NAME);
845 if (ret < 0)
846 goto err_pci_disable_device;
847
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300848 dev->io_mem = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
849 if (!dev->io_mem) {
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300850 ret = -EIO;
851 goto err_pci_release_regions;
852 }
853
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300854 spin_lock_init(&dev->lock);
855 pci_set_drvdata(pdev, dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300856
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300857 ret = dm1105_hw_init(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300858 if (ret < 0)
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300859 goto err_pci_iounmap;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300860
861 /* i2c */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300862 i2c_set_adapdata(&dev->i2c_adap, dev);
863 strcpy(dev->i2c_adap.name, DRIVER_NAME);
864 dev->i2c_adap.owner = THIS_MODULE;
865 dev->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
866 dev->i2c_adap.dev.parent = &pdev->dev;
867 dev->i2c_adap.algo = &dm1105_algo;
868 dev->i2c_adap.algo_data = dev;
869 ret = i2c_add_adapter(&dev->i2c_adap);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300870
871 if (ret < 0)
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300872 goto err_dm1105_hw_exit;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300873
874 /* dvb */
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300875 ret = dvb_register_adapter(&dev->dvb_adapter, DRIVER_NAME,
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300876 THIS_MODULE, &pdev->dev, adapter_nr);
877 if (ret < 0)
878 goto err_i2c_del_adapter;
879
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300880 dvb_adapter = &dev->dvb_adapter;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300881
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300882 dm1105_read_mac(dev, dvb_adapter->proposed_mac);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300883
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300884 dvbdemux = &dev->demux;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300885 dvbdemux->filternum = 256;
886 dvbdemux->feednum = 256;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300887 dvbdemux->start_feed = dm1105_start_feed;
888 dvbdemux->stop_feed = dm1105_stop_feed;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300889 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
890 DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
891 ret = dvb_dmx_init(dvbdemux);
892 if (ret < 0)
893 goto err_dvb_unregister_adapter;
894
895 dmx = &dvbdemux->dmx;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300896 dev->dmxdev.filternum = 256;
897 dev->dmxdev.demux = dmx;
898 dev->dmxdev.capabilities = 0;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300899
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300900 ret = dvb_dmxdev_init(&dev->dmxdev, dvb_adapter);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300901 if (ret < 0)
902 goto err_dvb_dmx_release;
903
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300904 dev->hw_frontend.source = DMX_FRONTEND_0;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300905
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300906 ret = dmx->add_frontend(dmx, &dev->hw_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300907 if (ret < 0)
908 goto err_dvb_dmxdev_release;
909
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300910 dev->mem_frontend.source = DMX_MEMORY_FE;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300911
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300912 ret = dmx->add_frontend(dmx, &dev->mem_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300913 if (ret < 0)
914 goto err_remove_hw_frontend;
915
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300916 ret = dmx->connect_frontend(dmx, &dev->hw_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300917 if (ret < 0)
918 goto err_remove_mem_frontend;
919
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300920 ret = frontend_init(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300921 if (ret < 0)
922 goto err_disconnect_frontend;
923
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300924 dvb_net_init(dvb_adapter, &dev->dvbnet, dmx);
925 dm1105_ir_init(dev);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300926
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300927 INIT_WORK(&dev->work, dm1105_dmx_buffer);
928 sprintf(dev->wqn, "%s/%d", dvb_adapter->name, dvb_adapter->num);
929 dev->wq = create_singlethread_workqueue(dev->wqn);
930 if (!dev->wq)
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300931 goto err_dvb_net;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300932
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300933 ret = request_irq(pdev->irq, dm1105_irq, IRQF_SHARED,
934 DRIVER_NAME, dev);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300935 if (ret < 0)
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300936 goto err_workqueue;
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300937
938 return 0;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300939
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300940err_workqueue:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300941 destroy_workqueue(dev->wq);
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -0300942err_dvb_net:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300943 dvb_net_release(&dev->dvbnet);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300944err_disconnect_frontend:
945 dmx->disconnect_frontend(dmx);
946err_remove_mem_frontend:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300947 dmx->remove_frontend(dmx, &dev->mem_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300948err_remove_hw_frontend:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300949 dmx->remove_frontend(dmx, &dev->hw_frontend);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300950err_dvb_dmxdev_release:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300951 dvb_dmxdev_release(&dev->dmxdev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300952err_dvb_dmx_release:
953 dvb_dmx_release(dvbdemux);
954err_dvb_unregister_adapter:
955 dvb_unregister_adapter(dvb_adapter);
956err_i2c_del_adapter:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300957 i2c_del_adapter(&dev->i2c_adap);
958err_dm1105_hw_exit:
959 dm1105_hw_exit(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300960err_pci_iounmap:
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300961 pci_iounmap(pdev, dev->io_mem);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300962err_pci_release_regions:
963 pci_release_regions(pdev);
964err_pci_disable_device:
965 pci_disable_device(pdev);
966err_kfree:
967 pci_set_drvdata(pdev, NULL);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300968 kfree(dev);
Igor M. Liplianind1498ff2009-02-26 03:40:41 -0300969 return ret;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300970}
971
972static void __devexit dm1105_remove(struct pci_dev *pdev)
973{
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300974 struct dm1105_dev *dev = pci_get_drvdata(pdev);
975 struct dvb_adapter *dvb_adapter = &dev->dvb_adapter;
976 struct dvb_demux *dvbdemux = &dev->demux;
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300977 struct dmx_demux *dmx = &dvbdemux->dmx;
978
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300979 dm1105_ir_exit(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300980 dmx->close(dmx);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300981 dvb_net_release(&dev->dvbnet);
982 if (dev->fe)
983 dvb_unregister_frontend(dev->fe);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300984
985 dmx->disconnect_frontend(dmx);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300986 dmx->remove_frontend(dmx, &dev->mem_frontend);
987 dmx->remove_frontend(dmx, &dev->hw_frontend);
988 dvb_dmxdev_release(&dev->dmxdev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300989 dvb_dmx_release(dvbdemux);
990 dvb_unregister_adapter(dvb_adapter);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300991 if (&dev->i2c_adap)
992 i2c_del_adapter(&dev->i2c_adap);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300993
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300994 dm1105_hw_exit(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300995 synchronize_irq(pdev->irq);
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -0300996 free_irq(pdev->irq, dev);
997 pci_iounmap(pdev, dev->io_mem);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -0300998 pci_release_regions(pdev);
999 pci_disable_device(pdev);
1000 pci_set_drvdata(pdev, NULL);
Igor M. Liplianind8300df2009-07-22 17:30:25 -03001001 dm1105_devcount--;
Igor M. Liplianin34d2f9b2010-01-17 12:11:00 -03001002 kfree(dev);
Igor M. Liplianina611d0c2008-09-13 10:10:53 -03001003}
1004
1005static struct pci_device_id dm1105_id_table[] __devinitdata = {
1006 {
1007 .vendor = PCI_VENDOR_ID_TRIGEM,
1008 .device = PCI_DEVICE_ID_DM1105,
1009 .subvendor = PCI_ANY_ID,
Igor M. Liplianind8300df2009-07-22 17:30:25 -03001010 .subdevice = PCI_ANY_ID,
Igor M. Liplianina611d0c2008-09-13 10:10:53 -03001011 }, {
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -03001012 .vendor = PCI_VENDOR_ID_AXESS,
1013 .device = PCI_DEVICE_ID_DM05,
Igor M. Liplianind8300df2009-07-22 17:30:25 -03001014 .subvendor = PCI_ANY_ID,
1015 .subdevice = PCI_ANY_ID,
Igor M. Liplianin519a4bd2009-06-14 21:34:12 -03001016 }, {
Igor M. Liplianina611d0c2008-09-13 10:10:53 -03001017 /* empty */
1018 },
1019};
1020
1021MODULE_DEVICE_TABLE(pci, dm1105_id_table);
1022
1023static struct pci_driver dm1105_driver = {
1024 .name = DRIVER_NAME,
1025 .id_table = dm1105_id_table,
1026 .probe = dm1105_probe,
1027 .remove = __devexit_p(dm1105_remove),
1028};
1029
1030static int __init dm1105_init(void)
1031{
1032 return pci_register_driver(&dm1105_driver);
1033}
1034
1035static void __exit dm1105_exit(void)
1036{
1037 pci_unregister_driver(&dm1105_driver);
1038}
1039
1040module_init(dm1105_init);
1041module_exit(dm1105_exit);
1042
1043MODULE_AUTHOR("Igor M. Liplianin <liplianin@me.by>");
1044MODULE_DESCRIPTION("SDMC DM1105 DVB driver");
1045MODULE_LICENSE("GPL");