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