4c28632f94c421fcbc0bcaddf80a8330901b7085
[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         ir_input_init(input_dev, &dm1105->ir.ir, ir_type, ir_codes);
593         input_dev->name = "DVB on-card IR receiver";
594         input_dev->phys = dm1105->ir.input_phys;
595         input_dev->id.bustype = BUS_PCI;
596         input_dev->id.version = 1;
597         if (dm1105->pdev->subsystem_vendor) {
598                 input_dev->id.vendor = dm1105->pdev->subsystem_vendor;
599                 input_dev->id.product = dm1105->pdev->subsystem_device;
600         } else {
601                 input_dev->id.vendor = dm1105->pdev->vendor;
602                 input_dev->id.product = dm1105->pdev->device;
603         }
604
605         input_dev->dev.parent = &dm1105->pdev->dev;
606
607         INIT_WORK(&dm1105->ir.work, dm1105_emit_key);
608
609         err = input_register_device(input_dev);
610         if (err) {
611                 input_free_device(input_dev);
612                 return err;
613         }
614
615         return 0;
616 }
617
618 void __devexit dm1105_ir_exit(struct dm1105dvb *dm1105)
619 {
620         input_unregister_device(dm1105->ir.input_dev);
621
622 }
623
624 static int __devinit dm1105dvb_hw_init(struct dm1105dvb *dm1105dvb)
625 {
626         dm1105dvb_disable_irqs(dm1105dvb);
627
628         outb(0, dm_io_mem(DM1105_HOST_CTR));
629
630         /*DATALEN 188,*/
631         outb(188, dm_io_mem(DM1105_DTALENTH));
632         /*TS_STRT TS_VALP MSBFIRST TS_MODE ALPAS TSPES*/
633         outw(0xc10a, dm_io_mem(DM1105_TSCTR));
634
635         /* map DMA and set address */
636         dm1105dvb_dma_map(dm1105dvb);
637         dm1105dvb_set_dma_addr(dm1105dvb);
638         /* big buffer */
639         outl(5*DM1105_DMA_BYTES, dm_io_mem(DM1105_RLEN));
640         outb(47, dm_io_mem(DM1105_INTCNT));
641
642         /* IR NEC mode enable */
643         outb((DM1105_IR_EN | DM1105_SYS_CHK), dm_io_mem(DM1105_IRCTR));
644         outb(0, dm_io_mem(DM1105_IRMODE));
645         outw(0, dm_io_mem(DM1105_SYSTEMCODE));
646
647         return 0;
648 }
649
650 static void dm1105dvb_hw_exit(struct dm1105dvb *dm1105dvb)
651 {
652         dm1105dvb_disable_irqs(dm1105dvb);
653
654         /* IR disable */
655         outb(0, dm_io_mem(DM1105_IRCTR));
656         outb(INTMAK_NONEMASK, dm_io_mem(DM1105_INTMAK));
657
658         dm1105dvb_dma_unmap(dm1105dvb);
659 }
660
661 static struct stv0299_config sharp_z0194a_config = {
662         .demod_address = 0x68,
663         .inittab = sharp_z0194a_inittab,
664         .mclk = 88000000UL,
665         .invert = 1,
666         .skip_reinit = 0,
667         .lock_output = STV0299_LOCKOUTPUT_1,
668         .volt13_op0_op1 = STV0299_VOLT13_OP1,
669         .min_delay_ms = 100,
670         .set_symbol_rate = sharp_z0194a_set_symbol_rate,
671 };
672
673 static struct stv0288_config earda_config = {
674         .demod_address = 0x68,
675         .min_delay_ms = 100,
676 };
677
678 static struct si21xx_config serit_config = {
679         .demod_address = 0x68,
680         .min_delay_ms = 100,
681
682 };
683
684 static struct cx24116_config serit_sp2633_config = {
685         .demod_address = 0x55,
686 };
687
688 static int __devinit frontend_init(struct dm1105dvb *dm1105dvb)
689 {
690         int ret;
691
692         switch (dm1105dvb->boardnr) {
693         case DM1105_BOARD_DVBWORLD_2004:
694                 dm1105dvb->fe = dvb_attach(
695                         cx24116_attach, &serit_sp2633_config,
696                         &dm1105dvb->i2c_adap);
697                 if (dm1105dvb->fe)
698                         dm1105dvb->fe->ops.set_voltage = dm1105dvb_set_voltage;
699
700                 break;
701         case DM1105_BOARD_DVBWORLD_2002:
702         case DM1105_BOARD_AXESS_DM05:
703         default:
704                 dm1105dvb->fe = dvb_attach(
705                         stv0299_attach, &sharp_z0194a_config,
706                         &dm1105dvb->i2c_adap);
707                 if (dm1105dvb->fe) {
708                         dm1105dvb->fe->ops.set_voltage =
709                                                         dm1105dvb_set_voltage;
710                         dvb_attach(dvb_pll_attach, dm1105dvb->fe, 0x60,
711                                         &dm1105dvb->i2c_adap, DVB_PLL_OPERA1);
712                         break;
713                 }
714
715                 dm1105dvb->fe = dvb_attach(
716                         stv0288_attach, &earda_config,
717                         &dm1105dvb->i2c_adap);
718                 if (dm1105dvb->fe) {
719                         dm1105dvb->fe->ops.set_voltage =
720                                                 dm1105dvb_set_voltage;
721                         dvb_attach(stb6000_attach, dm1105dvb->fe, 0x61,
722                                         &dm1105dvb->i2c_adap);
723                         break;
724                 }
725
726                 dm1105dvb->fe = dvb_attach(
727                         si21xx_attach, &serit_config,
728                         &dm1105dvb->i2c_adap);
729                 if (dm1105dvb->fe)
730                         dm1105dvb->fe->ops.set_voltage =
731                                                 dm1105dvb_set_voltage;
732
733         }
734
735         if (!dm1105dvb->fe) {
736                 dev_err(&dm1105dvb->pdev->dev, "could not attach frontend\n");
737                 return -ENODEV;
738         }
739
740         ret = dvb_register_frontend(&dm1105dvb->dvb_adapter, dm1105dvb->fe);
741         if (ret < 0) {
742                 if (dm1105dvb->fe->ops.release)
743                         dm1105dvb->fe->ops.release(dm1105dvb->fe);
744                 dm1105dvb->fe = NULL;
745                 return ret;
746         }
747
748         return 0;
749 }
750
751 static void __devinit dm1105dvb_read_mac(struct dm1105dvb *dm1105dvb, u8 *mac)
752 {
753         static u8 command[1] = { 0x28 };
754
755         struct i2c_msg msg[] = {
756                 {
757                         .addr = IIC_24C01_addr >> 1,
758                         .flags = 0,
759                         .buf = command,
760                         .len = 1
761                 }, {
762                         .addr = IIC_24C01_addr >> 1,
763                         .flags = I2C_M_RD,
764                         .buf = mac,
765                         .len = 6
766                 },
767         };
768
769         dm1105_i2c_xfer(&dm1105dvb->i2c_adap, msg , 2);
770         dev_info(&dm1105dvb->pdev->dev, "MAC %pM\n", mac);
771 }
772
773 static int __devinit dm1105_probe(struct pci_dev *pdev,
774                                   const struct pci_device_id *ent)
775 {
776         struct dm1105dvb *dm1105dvb;
777         struct dvb_adapter *dvb_adapter;
778         struct dvb_demux *dvbdemux;
779         struct dmx_demux *dmx;
780         int ret = -ENOMEM;
781         int i;
782
783         dm1105dvb = kzalloc(sizeof(struct dm1105dvb), GFP_KERNEL);
784         if (!dm1105dvb)
785                 return -ENOMEM;
786
787         /* board config */
788         dm1105dvb->nr = dm1105_devcount;
789         dm1105dvb->boardnr = UNSET;
790         if (card[dm1105dvb->nr] < ARRAY_SIZE(dm1105_boards))
791                 dm1105dvb->boardnr = card[dm1105dvb->nr];
792         for (i = 0; UNSET == dm1105dvb->boardnr &&
793                                 i < ARRAY_SIZE(dm1105_subids); i++)
794                 if (pdev->subsystem_vendor ==
795                         dm1105_subids[i].subvendor &&
796                                 pdev->subsystem_device ==
797                                         dm1105_subids[i].subdevice)
798                         dm1105dvb->boardnr = dm1105_subids[i].card;
799
800         if (UNSET == dm1105dvb->boardnr) {
801                 dm1105dvb->boardnr = DM1105_BOARD_UNKNOWN;
802                 dm1105_card_list(pdev);
803         }
804
805         dm1105_devcount++;
806         dm1105dvb->pdev = pdev;
807         dm1105dvb->buffer_size = 5 * DM1105_DMA_BYTES;
808         dm1105dvb->PacketErrorCount = 0;
809         dm1105dvb->dmarst = 0;
810
811         ret = pci_enable_device(pdev);
812         if (ret < 0)
813                 goto err_kfree;
814
815         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
816         if (ret < 0)
817                 goto err_pci_disable_device;
818
819         pci_set_master(pdev);
820
821         ret = pci_request_regions(pdev, DRIVER_NAME);
822         if (ret < 0)
823                 goto err_pci_disable_device;
824
825         dm1105dvb->io_mem = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
826         if (!dm1105dvb->io_mem) {
827                 ret = -EIO;
828                 goto err_pci_release_regions;
829         }
830
831         spin_lock_init(&dm1105dvb->lock);
832         pci_set_drvdata(pdev, dm1105dvb);
833
834         ret = dm1105dvb_hw_init(dm1105dvb);
835         if (ret < 0)
836                 goto err_pci_iounmap;
837
838         /* i2c */
839         i2c_set_adapdata(&dm1105dvb->i2c_adap, dm1105dvb);
840         strcpy(dm1105dvb->i2c_adap.name, DRIVER_NAME);
841         dm1105dvb->i2c_adap.owner = THIS_MODULE;
842         dm1105dvb->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
843         dm1105dvb->i2c_adap.dev.parent = &pdev->dev;
844         dm1105dvb->i2c_adap.algo = &dm1105_algo;
845         dm1105dvb->i2c_adap.algo_data = dm1105dvb;
846         ret = i2c_add_adapter(&dm1105dvb->i2c_adap);
847
848         if (ret < 0)
849                 goto err_dm1105dvb_hw_exit;
850
851         /* dvb */
852         ret = dvb_register_adapter(&dm1105dvb->dvb_adapter, DRIVER_NAME,
853                                         THIS_MODULE, &pdev->dev, adapter_nr);
854         if (ret < 0)
855                 goto err_i2c_del_adapter;
856
857         dvb_adapter = &dm1105dvb->dvb_adapter;
858
859         dm1105dvb_read_mac(dm1105dvb, dvb_adapter->proposed_mac);
860
861         dvbdemux = &dm1105dvb->demux;
862         dvbdemux->filternum = 256;
863         dvbdemux->feednum = 256;
864         dvbdemux->start_feed = dm1105dvb_start_feed;
865         dvbdemux->stop_feed = dm1105dvb_stop_feed;
866         dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
867                         DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
868         ret = dvb_dmx_init(dvbdemux);
869         if (ret < 0)
870                 goto err_dvb_unregister_adapter;
871
872         dmx = &dvbdemux->dmx;
873         dm1105dvb->dmxdev.filternum = 256;
874         dm1105dvb->dmxdev.demux = dmx;
875         dm1105dvb->dmxdev.capabilities = 0;
876
877         ret = dvb_dmxdev_init(&dm1105dvb->dmxdev, dvb_adapter);
878         if (ret < 0)
879                 goto err_dvb_dmx_release;
880
881         dm1105dvb->hw_frontend.source = DMX_FRONTEND_0;
882
883         ret = dmx->add_frontend(dmx, &dm1105dvb->hw_frontend);
884         if (ret < 0)
885                 goto err_dvb_dmxdev_release;
886
887         dm1105dvb->mem_frontend.source = DMX_MEMORY_FE;
888
889         ret = dmx->add_frontend(dmx, &dm1105dvb->mem_frontend);
890         if (ret < 0)
891                 goto err_remove_hw_frontend;
892
893         ret = dmx->connect_frontend(dmx, &dm1105dvb->hw_frontend);
894         if (ret < 0)
895                 goto err_remove_mem_frontend;
896
897         ret = frontend_init(dm1105dvb);
898         if (ret < 0)
899                 goto err_disconnect_frontend;
900
901         dvb_net_init(dvb_adapter, &dm1105dvb->dvbnet, dmx);
902         dm1105_ir_init(dm1105dvb);
903
904         INIT_WORK(&dm1105dvb->work, dm1105_dmx_buffer);
905         sprintf(dm1105dvb->wqn, "%s/%d", dvb_adapter->name, dvb_adapter->num);
906         dm1105dvb->wq = create_singlethread_workqueue(dm1105dvb->wqn);
907         if (!dm1105dvb->wq)
908                 goto err_dvb_net;
909
910         ret = request_irq(pdev->irq, dm1105dvb_irq, IRQF_SHARED,
911                                                 DRIVER_NAME, dm1105dvb);
912         if (ret < 0)
913                 goto err_workqueue;
914
915         return 0;
916
917 err_workqueue:
918         destroy_workqueue(dm1105dvb->wq);
919 err_dvb_net:
920         dvb_net_release(&dm1105dvb->dvbnet);
921 err_disconnect_frontend:
922         dmx->disconnect_frontend(dmx);
923 err_remove_mem_frontend:
924         dmx->remove_frontend(dmx, &dm1105dvb->mem_frontend);
925 err_remove_hw_frontend:
926         dmx->remove_frontend(dmx, &dm1105dvb->hw_frontend);
927 err_dvb_dmxdev_release:
928         dvb_dmxdev_release(&dm1105dvb->dmxdev);
929 err_dvb_dmx_release:
930         dvb_dmx_release(dvbdemux);
931 err_dvb_unregister_adapter:
932         dvb_unregister_adapter(dvb_adapter);
933 err_i2c_del_adapter:
934         i2c_del_adapter(&dm1105dvb->i2c_adap);
935 err_dm1105dvb_hw_exit:
936         dm1105dvb_hw_exit(dm1105dvb);
937 err_pci_iounmap:
938         pci_iounmap(pdev, dm1105dvb->io_mem);
939 err_pci_release_regions:
940         pci_release_regions(pdev);
941 err_pci_disable_device:
942         pci_disable_device(pdev);
943 err_kfree:
944         pci_set_drvdata(pdev, NULL);
945         kfree(dm1105dvb);
946         return ret;
947 }
948
949 static void __devexit dm1105_remove(struct pci_dev *pdev)
950 {
951         struct dm1105dvb *dm1105dvb = pci_get_drvdata(pdev);
952         struct dvb_adapter *dvb_adapter = &dm1105dvb->dvb_adapter;
953         struct dvb_demux *dvbdemux = &dm1105dvb->demux;
954         struct dmx_demux *dmx = &dvbdemux->dmx;
955
956         dm1105_ir_exit(dm1105dvb);
957         dmx->close(dmx);
958         dvb_net_release(&dm1105dvb->dvbnet);
959         if (dm1105dvb->fe)
960                 dvb_unregister_frontend(dm1105dvb->fe);
961
962         dmx->disconnect_frontend(dmx);
963         dmx->remove_frontend(dmx, &dm1105dvb->mem_frontend);
964         dmx->remove_frontend(dmx, &dm1105dvb->hw_frontend);
965         dvb_dmxdev_release(&dm1105dvb->dmxdev);
966         dvb_dmx_release(dvbdemux);
967         dvb_unregister_adapter(dvb_adapter);
968         if (&dm1105dvb->i2c_adap)
969                 i2c_del_adapter(&dm1105dvb->i2c_adap);
970
971         dm1105dvb_hw_exit(dm1105dvb);
972         synchronize_irq(pdev->irq);
973         free_irq(pdev->irq, dm1105dvb);
974         pci_iounmap(pdev, dm1105dvb->io_mem);
975         pci_release_regions(pdev);
976         pci_disable_device(pdev);
977         pci_set_drvdata(pdev, NULL);
978         dm1105_devcount--;
979         kfree(dm1105dvb);
980 }
981
982 static struct pci_device_id dm1105_id_table[] __devinitdata = {
983         {
984                 .vendor = PCI_VENDOR_ID_TRIGEM,
985                 .device = PCI_DEVICE_ID_DM1105,
986                 .subvendor = PCI_ANY_ID,
987                 .subdevice = PCI_ANY_ID,
988         }, {
989                 .vendor = PCI_VENDOR_ID_AXESS,
990                 .device = PCI_DEVICE_ID_DM05,
991                 .subvendor = PCI_ANY_ID,
992                 .subdevice = PCI_ANY_ID,
993         }, {
994                 /* empty */
995         },
996 };
997
998 MODULE_DEVICE_TABLE(pci, dm1105_id_table);
999
1000 static struct pci_driver dm1105_driver = {
1001         .name = DRIVER_NAME,
1002         .id_table = dm1105_id_table,
1003         .probe = dm1105_probe,
1004         .remove = __devexit_p(dm1105_remove),
1005 };
1006
1007 static int __init dm1105_init(void)
1008 {
1009         return pci_register_driver(&dm1105_driver);
1010 }
1011
1012 static void __exit dm1105_exit(void)
1013 {
1014         pci_unregister_driver(&dm1105_driver);
1015 }
1016
1017 module_init(dm1105_init);
1018 module_exit(dm1105_exit);
1019
1020 MODULE_AUTHOR("Igor M. Liplianin <liplianin@me.by>");
1021 MODULE_DESCRIPTION("SDMC DM1105 DVB driver");
1022 MODULE_LICENSE("GPL");