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