Merge branch 'linux-next' of git://git.infradead.org/ubifs-2.6
[linux-2.6] / drivers / media / dvb / dm1105 / dm1105.c
1 /*
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
22 #include <linux/version.h>
23 #include <linux/i2c.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/proc_fs.h>
28 #include <linux/pci.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/input.h>
31 #include <media/ir-common.h>
32
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"
42 #include "stv0288.h"
43 #include "stb6000.h"
44 #include "si21xx.h"
45 #include "cx24116.h"
46 #include "z0194a.h"
47
48 /* ----------------------------------------------- */
49 /*
50  * PCI ID's
51  */
52 #ifndef PCI_VENDOR_ID_TRIGEM
53 #define PCI_VENDOR_ID_TRIGEM    0x109f
54 #endif
55 #ifndef PCI_DEVICE_ID_DM1105
56 #define PCI_DEVICE_ID_DM1105    0x036f
57 #endif
58 #ifndef PCI_DEVICE_ID_DW2002
59 #define PCI_DEVICE_ID_DW2002    0x2002
60 #endif
61 #ifndef PCI_DEVICE_ID_DW2004
62 #define PCI_DEVICE_ID_DW2004    0x2004
63 #endif
64 /* ----------------------------------------------- */
65 /* sdmc dm1105 registers */
66
67 /* TS Control */
68 #define DM1105_TSCTR                            0x00
69 #define DM1105_DTALENTH                         0x04
70
71 /* GPIO Interface */
72 #define DM1105_GPIOVAL                          0x08
73 #define DM1105_GPIOCTR                          0x0c
74
75 /* PID serial number */
76 #define DM1105_PIDN                             0x10
77
78 /* Odd-even secret key select */
79 #define DM1105_CWSEL                            0x14
80
81 /* Host Command Interface */
82 #define DM1105_HOST_CTR                         0x18
83 #define DM1105_HOST_AD                          0x1c
84
85 /* PCI Interface */
86 #define DM1105_CR                               0x30
87 #define DM1105_RST                              0x34
88 #define DM1105_STADR                            0x38
89 #define DM1105_RLEN                             0x3c
90 #define DM1105_WRP                              0x40
91 #define DM1105_INTCNT                           0x44
92 #define DM1105_INTMAK                           0x48
93 #define DM1105_INTSTS                           0x4c
94
95 /* CW Value */
96 #define DM1105_ODD                              0x50
97 #define DM1105_EVEN                             0x58
98
99 /* PID Value */
100 #define DM1105_PID                              0x60
101
102 /* IR Control */
103 #define DM1105_IRCTR                            0x64
104 #define DM1105_IRMODE                           0x68
105 #define DM1105_SYSTEMCODE                       0x6c
106 #define DM1105_IRCODE                           0x70
107
108 /* Unknown Values */
109 #define DM1105_ENCRYPT                          0x74
110 #define DM1105_VER                              0x7c
111
112 /* I2C Interface */
113 #define DM1105_I2CCTR                           0x80
114 #define DM1105_I2CSTS                           0x81
115 #define DM1105_I2CDAT                           0x82
116 #define DM1105_I2C_RA                           0x83
117 /* ----------------------------------------------- */
118 /* Interrupt Mask Bits */
119
120 #define INTMAK_TSIRQM                           0x01
121 #define INTMAK_HIRQM                            0x04
122 #define INTMAK_IRM                              0x08
123 #define INTMAK_ALLMASK                          (INTMAK_TSIRQM | \
124                                                 INTMAK_HIRQM | \
125                                                 INTMAK_IRM)
126 #define INTMAK_NONEMASK                         0x00
127
128 /* Interrupt Status Bits */
129 #define INTSTS_TSIRQ                            0x01
130 #define INTSTS_HIRQ                             0x04
131 #define INTSTS_IR                               0x08
132
133 /* IR Control Bits */
134 #define DM1105_IR_EN                            0x01
135 #define DM1105_SYS_CHK                          0x02
136 #define DM1105_REP_FLG                          0x08
137
138 /* EEPROM addr */
139 #define IIC_24C01_addr                          0xa0
140 /* Max board count */
141 #define DM1105_MAX                              0x04
142
143 #define DRIVER_NAME                             "dm1105"
144
145 #define DM1105_DMA_PACKETS                      47
146 #define DM1105_DMA_PACKET_LENGTH                (128*4)
147 #define DM1105_DMA_BYTES                        (128 * 4 * DM1105_DMA_PACKETS)
148
149 /* GPIO's for LNB power control */
150 #define DM1105_LNB_MASK                         0x00000000
151 #define DM1105_LNB_13V                          0x00010100
152 #define DM1105_LNB_18V                          0x00000100
153
154 static int ir_debug;
155 module_param(ir_debug, int, 0644);
156 MODULE_PARM_DESC(ir_debug, "enable debugging information for IR decoding");
157
158 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
159
160 static u16 ir_codes_dm1105_nec[128] = {
161         [0x0a] = KEY_Q,         /*power*/
162         [0x0c] = KEY_M,         /*mute*/
163         [0x11] = KEY_1,
164         [0x12] = KEY_2,
165         [0x13] = KEY_3,
166         [0x14] = KEY_4,
167         [0x15] = KEY_5,
168         [0x16] = KEY_6,
169         [0x17] = KEY_7,
170         [0x18] = KEY_8,
171         [0x19] = KEY_9,
172         [0x10] = KEY_0,
173         [0x1c] = KEY_PAGEUP,    /*ch+*/
174         [0x0f] = KEY_PAGEDOWN,  /*ch-*/
175         [0x1a] = KEY_O,         /*vol+*/
176         [0x0e] = KEY_Z,         /*vol-*/
177         [0x04] = KEY_R,         /*rec*/
178         [0x09] = KEY_D,         /*fav*/
179         [0x08] = KEY_BACKSPACE, /*rewind*/
180         [0x07] = KEY_A,         /*fast*/
181         [0x0b] = KEY_P,         /*pause*/
182         [0x02] = KEY_ESC,       /*cancel*/
183         [0x03] = KEY_G,         /*tab*/
184         [0x00] = KEY_UP,        /*up*/
185         [0x1f] = KEY_ENTER,     /*ok*/
186         [0x01] = KEY_DOWN,      /*down*/
187         [0x05] = KEY_C,         /*cap*/
188         [0x06] = KEY_S,         /*stop*/
189         [0x40] = KEY_F,         /*full*/
190         [0x1e] = KEY_W,         /*tvmode*/
191         [0x1b] = KEY_B,         /*recall*/
192 };
193
194 /* infrared remote control */
195 struct infrared {
196         u16     key_map[128];
197         struct input_dev        *input_dev;
198         char                    input_phys[32];
199         struct tasklet_struct   ir_tasklet;
200         u32                     ir_command;
201 };
202
203 struct dm1105dvb {
204         /* pci */
205         struct pci_dev *pdev;
206         u8 __iomem *io_mem;
207
208         /* ir */
209         struct infrared ir;
210
211         /* dvb */
212         struct dmx_frontend hw_frontend;
213         struct dmx_frontend mem_frontend;
214         struct dmxdev dmxdev;
215         struct dvb_adapter dvb_adapter;
216         struct dvb_demux demux;
217         struct dvb_frontend *fe;
218         struct dvb_net dvbnet;
219         unsigned int full_ts_users;
220
221         /* i2c */
222         struct i2c_adapter i2c_adap;
223
224         /* dma */
225         dma_addr_t dma_addr;
226         unsigned char *ts_buf;
227         u32 wrp;
228         u32 buffer_size;
229         unsigned int    PacketErrorCount;
230         unsigned int dmarst;
231         spinlock_t lock;
232
233 };
234
235 #define dm_io_mem(reg)  ((unsigned long)(&dm1105dvb->io_mem[reg]))
236
237 static struct dm1105dvb *dm1105dvb_local;
238
239 static int dm1105_i2c_xfer(struct i2c_adapter *i2c_adap,
240                             struct i2c_msg *msgs, int num)
241 {
242         struct dm1105dvb *dm1105dvb ;
243
244         int addr, rc, i, j, k, len, byte, data;
245         u8 status;
246
247         dm1105dvb = i2c_adap->algo_data;
248         for (i = 0; i < num; i++) {
249                 outb(0x00, dm_io_mem(DM1105_I2CCTR));
250                 if (msgs[i].flags & I2C_M_RD) {
251                         /* read bytes */
252                         addr  = msgs[i].addr << 1;
253                         addr |= 1;
254                         outb(addr, dm_io_mem(DM1105_I2CDAT));
255                         for (byte = 0; byte < msgs[i].len; byte++)
256                                 outb(0, dm_io_mem(DM1105_I2CDAT + byte + 1));
257
258                         outb(0x81 + msgs[i].len, dm_io_mem(DM1105_I2CCTR));
259                         for (j = 0; j < 55; j++) {
260                                 mdelay(10);
261                                 status = inb(dm_io_mem(DM1105_I2CSTS));
262                                 if ((status & 0xc0) == 0x40)
263                                         break;
264                         }
265                         if (j >= 55)
266                                 return -1;
267
268                         for (byte = 0; byte < msgs[i].len; byte++) {
269                                 rc = inb(dm_io_mem(DM1105_I2CDAT + byte + 1));
270                                 if (rc < 0)
271                                         goto err;
272                                 msgs[i].buf[byte] = rc;
273                         }
274                 } else {
275                         if ((msgs[i].buf[0] == 0xf7) && (msgs[i].addr == 0x55)) {
276                                 /* prepaired for cx24116 firmware */
277                                 /* Write in small blocks */
278                                 len = msgs[i].len - 1;
279                                 k = 1;
280                                 do {
281                                         outb(msgs[i].addr << 1, dm_io_mem(DM1105_I2CDAT));
282                                         outb(0xf7, dm_io_mem(DM1105_I2CDAT + 1));
283                                         for (byte = 0; byte < (len > 48 ? 48 : len); byte++) {
284                                                 data = msgs[i].buf[k+byte];
285                                                 outb(data, dm_io_mem(DM1105_I2CDAT + byte + 2));
286                                         }
287                                         outb(0x82 + (len > 48 ? 48 : len), dm_io_mem(DM1105_I2CCTR));
288                                         for (j = 0; j < 25; j++) {
289                                                 mdelay(10);
290                                                 status = inb(dm_io_mem(DM1105_I2CSTS));
291                                                 if ((status & 0xc0) == 0x40)
292                                                         break;
293                                         }
294
295                                         if (j >= 25)
296                                                 return -1;
297
298                                         k += 48;
299                                         len -= 48;
300                                 } while (len > 0);
301                         } else {
302                                 /* write bytes */
303                                 outb(msgs[i].addr<<1, dm_io_mem(DM1105_I2CDAT));
304                                 for (byte = 0; byte < msgs[i].len; byte++) {
305                                         data = msgs[i].buf[byte];
306                                         outb(data, dm_io_mem(DM1105_I2CDAT + byte + 1));
307                                 }
308                                 outb(0x81 + msgs[i].len, dm_io_mem(DM1105_I2CCTR));
309                                 for (j = 0; j < 25; j++) {
310                                         mdelay(10);
311                                         status = inb(dm_io_mem(DM1105_I2CSTS));
312                                         if ((status & 0xc0) == 0x40)
313                                                 break;
314                                 }
315
316                                 if (j >= 25)
317                                         return -1;
318                         }
319                 }
320         }
321         return num;
322  err:
323         return rc;
324 }
325
326 static u32 functionality(struct i2c_adapter *adap)
327 {
328         return I2C_FUNC_I2C;
329 }
330
331 static struct i2c_algorithm dm1105_algo = {
332         .master_xfer   = dm1105_i2c_xfer,
333         .functionality = functionality,
334 };
335
336 static inline struct dm1105dvb *feed_to_dm1105dvb(struct dvb_demux_feed *feed)
337 {
338         return container_of(feed->demux, struct dm1105dvb, demux);
339 }
340
341 static inline struct dm1105dvb *frontend_to_dm1105dvb(struct dvb_frontend *fe)
342 {
343         return container_of(fe->dvb, struct dm1105dvb, dvb_adapter);
344 }
345
346 static int dm1105dvb_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
347 {
348         struct dm1105dvb *dm1105dvb = frontend_to_dm1105dvb(fe);
349
350                 if (voltage == SEC_VOLTAGE_18) {
351                         outl(DM1105_LNB_MASK, dm_io_mem(DM1105_GPIOCTR));
352                         outl(DM1105_LNB_18V, dm_io_mem(DM1105_GPIOVAL));
353                 } else  {
354                 /*LNB ON-13V by default!*/
355                         outl(DM1105_LNB_MASK, dm_io_mem(DM1105_GPIOCTR));
356                         outl(DM1105_LNB_13V, dm_io_mem(DM1105_GPIOVAL));
357                 }
358
359         return 0;
360 }
361
362 static void dm1105dvb_set_dma_addr(struct dm1105dvb *dm1105dvb)
363 {
364         outl(cpu_to_le32(dm1105dvb->dma_addr), dm_io_mem(DM1105_STADR));
365 }
366
367 static int __devinit dm1105dvb_dma_map(struct dm1105dvb *dm1105dvb)
368 {
369         dm1105dvb->ts_buf = pci_alloc_consistent(dm1105dvb->pdev, 6*DM1105_DMA_BYTES, &dm1105dvb->dma_addr);
370
371         return pci_dma_mapping_error(dm1105dvb->pdev, dm1105dvb->dma_addr);
372 }
373
374 static void dm1105dvb_dma_unmap(struct dm1105dvb *dm1105dvb)
375 {
376         pci_free_consistent(dm1105dvb->pdev, 6*DM1105_DMA_BYTES, dm1105dvb->ts_buf, dm1105dvb->dma_addr);
377 }
378
379 static void __devinit dm1105dvb_enable_irqs(struct dm1105dvb *dm1105dvb)
380 {
381         outb(INTMAK_ALLMASK, dm_io_mem(DM1105_INTMAK));
382         outb(1, dm_io_mem(DM1105_CR));
383 }
384
385 static void dm1105dvb_disable_irqs(struct dm1105dvb *dm1105dvb)
386 {
387         outb(INTMAK_IRM, dm_io_mem(DM1105_INTMAK));
388         outb(0, dm_io_mem(DM1105_CR));
389 }
390
391 static int dm1105dvb_start_feed(struct dvb_demux_feed *f)
392 {
393         struct dm1105dvb *dm1105dvb = feed_to_dm1105dvb(f);
394
395         if (dm1105dvb->full_ts_users++ == 0)
396                 dm1105dvb_enable_irqs(dm1105dvb);
397
398         return 0;
399 }
400
401 static int dm1105dvb_stop_feed(struct dvb_demux_feed *f)
402 {
403         struct dm1105dvb *dm1105dvb = feed_to_dm1105dvb(f);
404
405         if (--dm1105dvb->full_ts_users == 0)
406                 dm1105dvb_disable_irqs(dm1105dvb);
407
408         return 0;
409 }
410
411 /* ir tasklet */
412 static void dm1105_emit_key(unsigned long parm)
413 {
414         struct infrared *ir = (struct infrared *) parm;
415         u32 ircom = ir->ir_command;
416         u8 data;
417         u16 keycode;
418
419         data = (ircom >> 8) & 0x7f;
420
421         input_event(ir->input_dev, EV_MSC, MSC_RAW, (0x0000f8 << 16) | data);
422         input_event(ir->input_dev, EV_MSC, MSC_SCAN, data);
423         keycode = ir->key_map[data];
424
425         if (!keycode)
426                 return;
427
428         input_event(ir->input_dev, EV_KEY, keycode, 1);
429         input_sync(ir->input_dev);
430         input_event(ir->input_dev, EV_KEY, keycode, 0);
431         input_sync(ir->input_dev);
432
433 }
434
435 static irqreturn_t dm1105dvb_irq(int irq, void *dev_id)
436 {
437         struct dm1105dvb *dm1105dvb = dev_id;
438         unsigned int piece;
439         unsigned int nbpackets;
440         u32 command;
441         u32 nextwrp;
442         u32 oldwrp;
443
444         /* Read-Write INSTS Ack's Interrupt for DM1105 chip 16.03.2008 */
445         unsigned int intsts = inb(dm_io_mem(DM1105_INTSTS));
446         outb(intsts, dm_io_mem(DM1105_INTSTS));
447
448         switch (intsts) {
449         case INTSTS_TSIRQ:
450         case (INTSTS_TSIRQ | INTSTS_IR):
451                 nextwrp = inl(dm_io_mem(DM1105_WRP)) -
452                         inl(dm_io_mem(DM1105_STADR)) ;
453                 oldwrp = dm1105dvb->wrp;
454                 spin_lock(&dm1105dvb->lock);
455                 if (!((dm1105dvb->ts_buf[oldwrp] == 0x47) &&
456                                 (dm1105dvb->ts_buf[oldwrp + 188] == 0x47) &&
457                                 (dm1105dvb->ts_buf[oldwrp + 188 * 2] == 0x47))) {
458                         dm1105dvb->PacketErrorCount++;
459                         /* bad packet found */
460                         if ((dm1105dvb->PacketErrorCount >= 2) &&
461                                         (dm1105dvb->dmarst == 0)) {
462                                 outb(1, dm_io_mem(DM1105_RST));
463                                 dm1105dvb->wrp = 0;
464                                 dm1105dvb->PacketErrorCount = 0;
465                                 dm1105dvb->dmarst = 0;
466                                 spin_unlock(&dm1105dvb->lock);
467                                 return IRQ_HANDLED;
468                         }
469                 }
470                 if (nextwrp < oldwrp) {
471                         piece = dm1105dvb->buffer_size - oldwrp;
472                         memcpy(dm1105dvb->ts_buf + dm1105dvb->buffer_size, dm1105dvb->ts_buf, nextwrp);
473                         nbpackets = (piece + nextwrp)/188;
474                 } else  {
475                         nbpackets = (nextwrp - oldwrp)/188;
476                 }
477                 dvb_dmx_swfilter_packets(&dm1105dvb->demux, &dm1105dvb->ts_buf[oldwrp], nbpackets);
478                 dm1105dvb->wrp = nextwrp;
479                 spin_unlock(&dm1105dvb->lock);
480                 break;
481         case INTSTS_IR:
482                 command = inl(dm_io_mem(DM1105_IRCODE));
483                 if (ir_debug)
484                         printk("dm1105: received byte 0x%04x\n", command);
485
486                 dm1105dvb->ir.ir_command = command;
487                 tasklet_schedule(&dm1105dvb->ir.ir_tasklet);
488                 break;
489         }
490         return IRQ_HANDLED;
491
492
493 }
494
495 /* register with input layer */
496 static void input_register_keys(struct infrared *ir)
497 {
498         int i;
499
500         memset(ir->input_dev->keybit, 0, sizeof(ir->input_dev->keybit));
501
502         for (i = 0; i < ARRAY_SIZE(ir->key_map); i++)
503                         set_bit(ir->key_map[i], ir->input_dev->keybit);
504
505         ir->input_dev->keycode = ir->key_map;
506         ir->input_dev->keycodesize = sizeof(ir->key_map[0]);
507         ir->input_dev->keycodemax = ARRAY_SIZE(ir->key_map);
508 }
509
510 int __devinit dm1105_ir_init(struct dm1105dvb *dm1105)
511 {
512         struct input_dev *input_dev;
513         int err;
514
515         dm1105dvb_local = dm1105;
516
517         input_dev = input_allocate_device();
518         if (!input_dev)
519                 return -ENOMEM;
520
521         dm1105->ir.input_dev = input_dev;
522         snprintf(dm1105->ir.input_phys, sizeof(dm1105->ir.input_phys),
523                 "pci-%s/ir0", pci_name(dm1105->pdev));
524
525         input_dev->evbit[0] = BIT(EV_KEY);
526         input_dev->name = "DVB on-card IR receiver";
527
528         input_dev->phys = dm1105->ir.input_phys;
529         input_dev->id.bustype = BUS_PCI;
530         input_dev->id.version = 2;
531         if (dm1105->pdev->subsystem_vendor) {
532                 input_dev->id.vendor = dm1105->pdev->subsystem_vendor;
533                 input_dev->id.product = dm1105->pdev->subsystem_device;
534         } else {
535                 input_dev->id.vendor = dm1105->pdev->vendor;
536                 input_dev->id.product = dm1105->pdev->device;
537         }
538         input_dev->dev.parent = &dm1105->pdev->dev;
539         /* initial keymap */
540         memcpy(dm1105->ir.key_map, ir_codes_dm1105_nec, sizeof dm1105->ir.key_map);
541         input_register_keys(&dm1105->ir);
542         err = input_register_device(input_dev);
543         if (err) {
544                 input_free_device(input_dev);
545                 return err;
546         }
547
548         tasklet_init(&dm1105->ir.ir_tasklet, dm1105_emit_key, (unsigned long) &dm1105->ir);
549
550         return 0;
551 }
552
553
554 void __devexit dm1105_ir_exit(struct dm1105dvb *dm1105)
555 {
556         tasklet_kill(&dm1105->ir.ir_tasklet);
557         input_unregister_device(dm1105->ir.input_dev);
558
559 }
560
561 static int __devinit dm1105dvb_hw_init(struct dm1105dvb *dm1105dvb)
562 {
563         dm1105dvb_disable_irqs(dm1105dvb);
564
565         outb(0, dm_io_mem(DM1105_HOST_CTR));
566
567         /*DATALEN 188,*/
568         outb(188, dm_io_mem(DM1105_DTALENTH));
569         /*TS_STRT TS_VALP MSBFIRST TS_MODE ALPAS TSPES*/
570         outw(0xc10a, dm_io_mem(DM1105_TSCTR));
571
572         /* map DMA and set address */
573         dm1105dvb_dma_map(dm1105dvb);
574         dm1105dvb_set_dma_addr(dm1105dvb);
575         /* big buffer */
576         outl(5*DM1105_DMA_BYTES, dm_io_mem(DM1105_RLEN));
577         outb(47, dm_io_mem(DM1105_INTCNT));
578
579         /* IR NEC mode enable */
580         outb((DM1105_IR_EN | DM1105_SYS_CHK), dm_io_mem(DM1105_IRCTR));
581         outb(0, dm_io_mem(DM1105_IRMODE));
582         outw(0, dm_io_mem(DM1105_SYSTEMCODE));
583
584         return 0;
585 }
586
587 static void dm1105dvb_hw_exit(struct dm1105dvb *dm1105dvb)
588 {
589         dm1105dvb_disable_irqs(dm1105dvb);
590
591         /* IR disable */
592         outb(0, dm_io_mem(DM1105_IRCTR));
593         outb(INTMAK_NONEMASK, dm_io_mem(DM1105_INTMAK));
594
595         dm1105dvb_dma_unmap(dm1105dvb);
596 }
597
598 static struct stv0299_config sharp_z0194a_config = {
599         .demod_address = 0x68,
600         .inittab = sharp_z0194a_inittab,
601         .mclk = 88000000UL,
602         .invert = 1,
603         .skip_reinit = 0,
604         .lock_output = STV0299_LOCKOUTPUT_1,
605         .volt13_op0_op1 = STV0299_VOLT13_OP1,
606         .min_delay_ms = 100,
607         .set_symbol_rate = sharp_z0194a_set_symbol_rate,
608 };
609
610 static struct stv0288_config earda_config = {
611         .demod_address = 0x68,
612         .min_delay_ms = 100,
613 };
614
615 static struct si21xx_config serit_config = {
616         .demod_address = 0x68,
617         .min_delay_ms = 100,
618
619 };
620
621 static struct cx24116_config serit_sp2633_config = {
622         .demod_address = 0x55,
623 };
624
625 static int __devinit frontend_init(struct dm1105dvb *dm1105dvb)
626 {
627         int ret;
628
629         switch (dm1105dvb->pdev->subsystem_device) {
630         case PCI_DEVICE_ID_DW2002:
631                 dm1105dvb->fe = dvb_attach(
632                         stv0299_attach, &sharp_z0194a_config,
633                         &dm1105dvb->i2c_adap);
634
635                 if (dm1105dvb->fe) {
636                         dm1105dvb->fe->ops.set_voltage =
637                                                         dm1105dvb_set_voltage;
638                         dvb_attach(dvb_pll_attach, dm1105dvb->fe, 0x60,
639                                         &dm1105dvb->i2c_adap, DVB_PLL_OPERA1);
640                 }
641
642                 if (!dm1105dvb->fe) {
643                         dm1105dvb->fe = dvb_attach(
644                                 stv0288_attach, &earda_config,
645                                 &dm1105dvb->i2c_adap);
646                         if (dm1105dvb->fe) {
647                                 dm1105dvb->fe->ops.set_voltage =
648                                                         dm1105dvb_set_voltage;
649                                 dvb_attach(stb6000_attach, dm1105dvb->fe, 0x61,
650                                                 &dm1105dvb->i2c_adap);
651                         }
652                 }
653
654                 if (!dm1105dvb->fe) {
655                         dm1105dvb->fe = dvb_attach(
656                                 si21xx_attach, &serit_config,
657                                 &dm1105dvb->i2c_adap);
658                         if (dm1105dvb->fe)
659                                 dm1105dvb->fe->ops.set_voltage =
660                                                         dm1105dvb_set_voltage;
661                 }
662                 break;
663         case PCI_DEVICE_ID_DW2004:
664                 dm1105dvb->fe = dvb_attach(
665                         cx24116_attach, &serit_sp2633_config,
666                         &dm1105dvb->i2c_adap);
667                 if (dm1105dvb->fe)
668                         dm1105dvb->fe->ops.set_voltage = dm1105dvb_set_voltage;
669                 break;
670         }
671
672         if (!dm1105dvb->fe) {
673                 dev_err(&dm1105dvb->pdev->dev, "could not attach frontend\n");
674                 return -ENODEV;
675         }
676
677         ret = dvb_register_frontend(&dm1105dvb->dvb_adapter, dm1105dvb->fe);
678         if (ret < 0) {
679                 if (dm1105dvb->fe->ops.release)
680                         dm1105dvb->fe->ops.release(dm1105dvb->fe);
681                 dm1105dvb->fe = NULL;
682                 return ret;
683         }
684
685         return 0;
686 }
687
688 static void __devinit dm1105dvb_read_mac(struct dm1105dvb *dm1105dvb, u8 *mac)
689 {
690         static u8 command[1] = { 0x28 };
691
692         struct i2c_msg msg[] = {
693                 { .addr = IIC_24C01_addr >> 1, .flags = 0,
694                                 .buf = command, .len = 1 },
695                 { .addr = IIC_24C01_addr >> 1, .flags = I2C_M_RD,
696                                 .buf = mac, .len = 6 },
697         };
698
699         dm1105_i2c_xfer(&dm1105dvb->i2c_adap, msg , 2);
700         dev_info(&dm1105dvb->pdev->dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
701                         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
702 }
703
704 static int __devinit dm1105_probe(struct pci_dev *pdev,
705                                   const struct pci_device_id *ent)
706 {
707         struct dm1105dvb *dm1105dvb;
708         struct dvb_adapter *dvb_adapter;
709         struct dvb_demux *dvbdemux;
710         struct dmx_demux *dmx;
711         int ret = -ENOMEM;
712
713         dm1105dvb = kzalloc(sizeof(struct dm1105dvb), GFP_KERNEL);
714         if (!dm1105dvb)
715                 goto out;
716
717         dm1105dvb->pdev = pdev;
718         dm1105dvb->buffer_size = 5 * DM1105_DMA_BYTES;
719         dm1105dvb->PacketErrorCount = 0;
720         dm1105dvb->dmarst = 0;
721
722         ret = pci_enable_device(pdev);
723         if (ret < 0)
724                 goto err_kfree;
725
726         ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
727         if (ret < 0)
728                 goto err_pci_disable_device;
729
730         pci_set_master(pdev);
731
732         ret = pci_request_regions(pdev, DRIVER_NAME);
733         if (ret < 0)
734                 goto err_pci_disable_device;
735
736         dm1105dvb->io_mem = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
737         if (!dm1105dvb->io_mem) {
738                 ret = -EIO;
739                 goto err_pci_release_regions;
740         }
741
742         spin_lock_init(&dm1105dvb->lock);
743         pci_set_drvdata(pdev, dm1105dvb);
744
745         ret = request_irq(pdev->irq, dm1105dvb_irq, IRQF_SHARED, DRIVER_NAME, dm1105dvb);
746         if (ret < 0)
747                 goto err_pci_iounmap;
748
749         ret = dm1105dvb_hw_init(dm1105dvb);
750         if (ret < 0)
751                 goto err_free_irq;
752
753         /* i2c */
754         i2c_set_adapdata(&dm1105dvb->i2c_adap, dm1105dvb);
755         strcpy(dm1105dvb->i2c_adap.name, DRIVER_NAME);
756         dm1105dvb->i2c_adap.owner = THIS_MODULE;
757         dm1105dvb->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
758         dm1105dvb->i2c_adap.dev.parent = &pdev->dev;
759         dm1105dvb->i2c_adap.algo = &dm1105_algo;
760         dm1105dvb->i2c_adap.algo_data = dm1105dvb;
761         ret = i2c_add_adapter(&dm1105dvb->i2c_adap);
762
763         if (ret < 0)
764                 goto err_dm1105dvb_hw_exit;
765
766         /* dvb */
767         ret = dvb_register_adapter(&dm1105dvb->dvb_adapter, DRIVER_NAME,
768                                         THIS_MODULE, &pdev->dev, adapter_nr);
769         if (ret < 0)
770                 goto err_i2c_del_adapter;
771
772         dvb_adapter = &dm1105dvb->dvb_adapter;
773
774         dm1105dvb_read_mac(dm1105dvb, dvb_adapter->proposed_mac);
775
776         dvbdemux = &dm1105dvb->demux;
777         dvbdemux->filternum = 256;
778         dvbdemux->feednum = 256;
779         dvbdemux->start_feed = dm1105dvb_start_feed;
780         dvbdemux->stop_feed = dm1105dvb_stop_feed;
781         dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
782                         DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
783         ret = dvb_dmx_init(dvbdemux);
784         if (ret < 0)
785                 goto err_dvb_unregister_adapter;
786
787         dmx = &dvbdemux->dmx;
788         dm1105dvb->dmxdev.filternum = 256;
789         dm1105dvb->dmxdev.demux = dmx;
790         dm1105dvb->dmxdev.capabilities = 0;
791
792         ret = dvb_dmxdev_init(&dm1105dvb->dmxdev, dvb_adapter);
793         if (ret < 0)
794                 goto err_dvb_dmx_release;
795
796         dm1105dvb->hw_frontend.source = DMX_FRONTEND_0;
797
798         ret = dmx->add_frontend(dmx, &dm1105dvb->hw_frontend);
799         if (ret < 0)
800                 goto err_dvb_dmxdev_release;
801
802         dm1105dvb->mem_frontend.source = DMX_MEMORY_FE;
803
804         ret = dmx->add_frontend(dmx, &dm1105dvb->mem_frontend);
805         if (ret < 0)
806                 goto err_remove_hw_frontend;
807
808         ret = dmx->connect_frontend(dmx, &dm1105dvb->hw_frontend);
809         if (ret < 0)
810                 goto err_remove_mem_frontend;
811
812         ret = frontend_init(dm1105dvb);
813         if (ret < 0)
814                 goto err_disconnect_frontend;
815
816         dvb_net_init(dvb_adapter, &dm1105dvb->dvbnet, dmx);
817         dm1105_ir_init(dm1105dvb);
818 out:
819         return ret;
820
821 err_disconnect_frontend:
822         dmx->disconnect_frontend(dmx);
823 err_remove_mem_frontend:
824         dmx->remove_frontend(dmx, &dm1105dvb->mem_frontend);
825 err_remove_hw_frontend:
826         dmx->remove_frontend(dmx, &dm1105dvb->hw_frontend);
827 err_dvb_dmxdev_release:
828         dvb_dmxdev_release(&dm1105dvb->dmxdev);
829 err_dvb_dmx_release:
830         dvb_dmx_release(dvbdemux);
831 err_dvb_unregister_adapter:
832         dvb_unregister_adapter(dvb_adapter);
833 err_i2c_del_adapter:
834         i2c_del_adapter(&dm1105dvb->i2c_adap);
835 err_dm1105dvb_hw_exit:
836         dm1105dvb_hw_exit(dm1105dvb);
837 err_free_irq:
838         free_irq(pdev->irq, dm1105dvb);
839 err_pci_iounmap:
840         pci_iounmap(pdev, dm1105dvb->io_mem);
841 err_pci_release_regions:
842         pci_release_regions(pdev);
843 err_pci_disable_device:
844         pci_disable_device(pdev);
845 err_kfree:
846         pci_set_drvdata(pdev, NULL);
847         kfree(dm1105dvb);
848         goto out;
849 }
850
851 static void __devexit dm1105_remove(struct pci_dev *pdev)
852 {
853         struct dm1105dvb *dm1105dvb = pci_get_drvdata(pdev);
854         struct dvb_adapter *dvb_adapter = &dm1105dvb->dvb_adapter;
855         struct dvb_demux *dvbdemux = &dm1105dvb->demux;
856         struct dmx_demux *dmx = &dvbdemux->dmx;
857
858         dm1105_ir_exit(dm1105dvb);
859         dmx->close(dmx);
860         dvb_net_release(&dm1105dvb->dvbnet);
861         if (dm1105dvb->fe)
862                 dvb_unregister_frontend(dm1105dvb->fe);
863
864         dmx->disconnect_frontend(dmx);
865         dmx->remove_frontend(dmx, &dm1105dvb->mem_frontend);
866         dmx->remove_frontend(dmx, &dm1105dvb->hw_frontend);
867         dvb_dmxdev_release(&dm1105dvb->dmxdev);
868         dvb_dmx_release(dvbdemux);
869         dvb_unregister_adapter(dvb_adapter);
870         if (&dm1105dvb->i2c_adap)
871                 i2c_del_adapter(&dm1105dvb->i2c_adap);
872
873         dm1105dvb_hw_exit(dm1105dvb);
874         synchronize_irq(pdev->irq);
875         free_irq(pdev->irq, dm1105dvb);
876         pci_iounmap(pdev, dm1105dvb->io_mem);
877         pci_release_regions(pdev);
878         pci_disable_device(pdev);
879         pci_set_drvdata(pdev, NULL);
880         kfree(dm1105dvb);
881 }
882
883 static struct pci_device_id dm1105_id_table[] __devinitdata = {
884         {
885                 .vendor = PCI_VENDOR_ID_TRIGEM,
886                 .device = PCI_DEVICE_ID_DM1105,
887                 .subvendor = PCI_ANY_ID,
888                 .subdevice = PCI_DEVICE_ID_DW2002,
889         }, {
890                 .vendor = PCI_VENDOR_ID_TRIGEM,
891                 .device = PCI_DEVICE_ID_DM1105,
892                 .subvendor = PCI_ANY_ID,
893                 .subdevice = PCI_DEVICE_ID_DW2004,
894         }, {
895                 /* empty */
896         },
897 };
898
899 MODULE_DEVICE_TABLE(pci, dm1105_id_table);
900
901 static struct pci_driver dm1105_driver = {
902         .name = DRIVER_NAME,
903         .id_table = dm1105_id_table,
904         .probe = dm1105_probe,
905         .remove = __devexit_p(dm1105_remove),
906 };
907
908 static int __init dm1105_init(void)
909 {
910         return pci_register_driver(&dm1105_driver);
911 }
912
913 static void __exit dm1105_exit(void)
914 {
915         pci_unregister_driver(&dm1105_driver);
916 }
917
918 module_init(dm1105_init);
919 module_exit(dm1105_exit);
920
921 MODULE_AUTHOR("Igor M. Liplianin <liplianin@me.by>");
922 MODULE_DESCRIPTION("SDMC DM1105 DVB driver");
923 MODULE_LICENSE("GPL");