]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/usb/dvb-usb/pctv452e.c
PM / QoS: Remove global notifiers
[linux.git] / drivers / media / usb / dvb-usb / pctv452e.c
1 /*
2  * PCTV 452e DVB driver
3  *
4  * Copyright (c) 2006-2008 Dominik Kuhlen <dkuhlen@gmx.net>
5  *
6  * TT connect S2-3650-CI Common Interface support, MAC readout
7  * Copyright (C) 2008 Michael H. Schimek <mschimek@gmx.at>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  */
14
15 /* dvb usb framework */
16 #define DVB_USB_LOG_PREFIX "pctv452e"
17 #include "dvb-usb.h"
18
19 /* Demodulator */
20 #include "stb0899_drv.h"
21 #include "stb0899_reg.h"
22 #include "stb0899_cfg.h"
23 /* Tuner */
24 #include "stb6100.h"
25 #include "stb6100_cfg.h"
26 /* FE Power */
27 #include "lnbp22.h"
28
29 #include "dvb_ca_en50221.h"
30 #include "ttpci-eeprom.h"
31
32 static int debug;
33 module_param(debug, int, 0644);
34 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
35
36 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
37
38 #define ISOC_INTERFACE_ALTERNATIVE 3
39
40 #define SYNC_BYTE_OUT 0xaa
41 #define SYNC_BYTE_IN  0x55
42
43 /* guessed: (copied from ttusb-budget) */
44 #define PCTV_CMD_RESET 0x15
45 /* command to poll IR receiver */
46 #define PCTV_CMD_IR    0x1b
47 /* command to send I2C  */
48 #define PCTV_CMD_I2C   0x31
49
50 #define I2C_ADDR_STB0899 (0xd0 >> 1)
51 #define I2C_ADDR_STB6100 (0xc0 >> 1)
52 #define I2C_ADDR_LNBP22  (0x10 >> 1)
53 #define I2C_ADDR_24C16   (0xa0 >> 1)
54 #define I2C_ADDR_24C64   (0xa2 >> 1)
55
56
57 /* pctv452e sends us this amount of data for each issued usb-command */
58 #define PCTV_ANSWER_LEN 64
59 /* Wait up to 1000ms for device  */
60 #define PCTV_TIMEOUT 1000
61
62
63 #define PCTV_LED_GPIO   STB0899_GPIO01
64 #define PCTV_LED_GREEN  0x82
65 #define PCTV_LED_ORANGE 0x02
66
67 #define ci_dbg(format, arg...)                          \
68 do {                                                    \
69         if (0)                                          \
70                 printk(KERN_DEBUG DVB_USB_LOG_PREFIX    \
71                         ": " format "\n" , ## arg);     \
72 } while (0)
73
74 enum {
75         TT3650_CMD_CI_TEST = 0x40,
76         TT3650_CMD_CI_RD_CTRL,
77         TT3650_CMD_CI_WR_CTRL,
78         TT3650_CMD_CI_RD_ATTR,
79         TT3650_CMD_CI_WR_ATTR,
80         TT3650_CMD_CI_RESET,
81         TT3650_CMD_CI_SET_VIDEO_PORT
82 };
83
84
85 static struct stb0899_postproc pctv45e_postproc[] = {
86         { PCTV_LED_GPIO, STB0899_GPIOPULLUP },
87         { 0, 0 }
88 };
89
90 /*
91  * stores all private variables for communication with the PCTV452e DVB-S2
92  */
93 struct pctv452e_state {
94         struct dvb_ca_en50221 ca;
95         struct mutex ca_mutex;
96
97         u8 c;      /* transaction counter, wraps around...  */
98         u8 initialized; /* set to 1 if 0x15 has been sent */
99         u16 last_rc_key;
100
101         unsigned char data[80];
102 };
103
104 static int tt3650_ci_msg(struct dvb_usb_device *d, u8 cmd, u8 *data,
105                          unsigned int write_len, unsigned int read_len)
106 {
107         struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
108         u8 id;
109         unsigned int rlen;
110         int ret;
111
112         if (!data || (write_len > 64 - 4) || (read_len > 64 - 4)) {
113                 err("%s: transfer data invalid", __func__);
114                 return -EIO;
115         }
116
117         mutex_lock(&state->ca_mutex);
118         id = state->c++;
119
120         state->data[0] = SYNC_BYTE_OUT;
121         state->data[1] = id;
122         state->data[2] = cmd;
123         state->data[3] = write_len;
124
125         memcpy(state->data + 4, data, write_len);
126
127         rlen = (read_len > 0) ? 64 : 0;
128         ret = dvb_usb_generic_rw(d, state->data, 4 + write_len,
129                                   state->data, rlen, /* delay_ms */ 0);
130         if (0 != ret)
131                 goto failed;
132
133         ret = -EIO;
134         if (SYNC_BYTE_IN != state->data[0] || id != state->data[1])
135                 goto failed;
136
137         memcpy(data, state->data + 4, read_len);
138
139         mutex_unlock(&state->ca_mutex);
140         return 0;
141
142 failed:
143         err("CI error %d; %02X %02X %02X -> %*ph.",
144              ret, SYNC_BYTE_OUT, id, cmd, 3, state->data);
145
146         mutex_unlock(&state->ca_mutex);
147         return ret;
148 }
149
150 static int tt3650_ci_msg_locked(struct dvb_ca_en50221 *ca,
151                                 u8 cmd, u8 *data, unsigned int write_len,
152                                 unsigned int read_len)
153 {
154         struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
155         struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
156         int ret;
157
158         mutex_lock(&state->ca_mutex);
159         ret = tt3650_ci_msg(d, cmd, data, write_len, read_len);
160         mutex_unlock(&state->ca_mutex);
161
162         return ret;
163 }
164
165 static int tt3650_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
166                                  int slot, int address)
167 {
168         u8 buf[3];
169         int ret;
170
171         if (0 != slot)
172                 return -EINVAL;
173
174         buf[0] = (address >> 8) & 0x0F;
175         buf[1] = address;
176
177         ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_RD_ATTR, buf, 2, 3);
178
179         ci_dbg("%s %04x -> %d 0x%02x",
180                 __func__, address, ret, buf[2]);
181
182         if (ret < 0)
183                 return ret;
184
185         return buf[2];
186 }
187
188 static int tt3650_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
189                                  int slot, int address, u8 value)
190 {
191         u8 buf[3];
192
193         ci_dbg("%s %d 0x%04x 0x%02x",
194                 __func__, slot, address, value);
195
196         if (0 != slot)
197                 return -EINVAL;
198
199         buf[0] = (address >> 8) & 0x0F;
200         buf[1] = address;
201         buf[2] = value;
202
203         return tt3650_ci_msg_locked(ca, TT3650_CMD_CI_WR_ATTR, buf, 3, 3);
204 }
205
206 static int tt3650_ci_read_cam_control(struct dvb_ca_en50221 *ca,
207                                  int                    slot,
208                                  u8                     address)
209 {
210         u8 buf[2];
211         int ret;
212
213         if (0 != slot)
214                 return -EINVAL;
215
216         buf[0] = address & 3;
217
218         ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_RD_CTRL, buf, 1, 2);
219
220         ci_dbg("%s 0x%02x -> %d 0x%02x",
221                 __func__, address, ret, buf[1]);
222
223         if (ret < 0)
224                 return ret;
225
226         return buf[1];
227 }
228
229 static int tt3650_ci_write_cam_control(struct dvb_ca_en50221 *ca,
230                                  int                    slot,
231                                  u8                     address,
232                                  u8                     value)
233 {
234         u8 buf[2];
235
236         ci_dbg("%s %d 0x%02x 0x%02x",
237                 __func__, slot, address, value);
238
239         if (0 != slot)
240                 return -EINVAL;
241
242         buf[0] = address;
243         buf[1] = value;
244
245         return tt3650_ci_msg_locked(ca, TT3650_CMD_CI_WR_CTRL, buf, 2, 2);
246 }
247
248 static int tt3650_ci_set_video_port(struct dvb_ca_en50221 *ca,
249                                  int                    slot,
250                                  int                    enable)
251 {
252         u8 buf[1];
253         int ret;
254
255         ci_dbg("%s %d %d", __func__, slot, enable);
256
257         if (0 != slot)
258                 return -EINVAL;
259
260         enable = !!enable;
261         buf[0] = enable;
262
263         ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_SET_VIDEO_PORT, buf, 1, 1);
264         if (ret < 0)
265                 return ret;
266
267         if (enable != buf[0]) {
268                 err("CI not %sabled.", enable ? "en" : "dis");
269                 return -EIO;
270         }
271
272         return 0;
273 }
274
275 static int tt3650_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
276 {
277         return tt3650_ci_set_video_port(ca, slot, /* enable */ 0);
278 }
279
280 static int tt3650_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
281 {
282         return tt3650_ci_set_video_port(ca, slot, /* enable */ 1);
283 }
284
285 static int tt3650_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
286 {
287         struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
288         struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
289         u8 buf[1];
290         int ret;
291
292         ci_dbg("%s %d", __func__, slot);
293
294         if (0 != slot)
295                 return -EINVAL;
296
297         buf[0] = 0;
298
299         mutex_lock(&state->ca_mutex);
300
301         ret = tt3650_ci_msg(d, TT3650_CMD_CI_RESET, buf, 1, 1);
302         if (0 != ret)
303                 goto failed;
304
305         msleep(500);
306
307         buf[0] = 1;
308
309         ret = tt3650_ci_msg(d, TT3650_CMD_CI_RESET, buf, 1, 1);
310         if (0 != ret)
311                 goto failed;
312
313         msleep(500);
314
315         buf[0] = 0; /* FTA */
316
317         ret = tt3650_ci_msg(d, TT3650_CMD_CI_SET_VIDEO_PORT, buf, 1, 1);
318
319  failed:
320         mutex_unlock(&state->ca_mutex);
321
322         return ret;
323 }
324
325 static int tt3650_ci_poll_slot_status(struct dvb_ca_en50221 *ca,
326                                  int                    slot,
327                                  int                    open)
328 {
329         u8 buf[1];
330         int ret;
331
332         if (0 != slot)
333                 return -EINVAL;
334
335         ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_TEST, buf, 0, 1);
336         if (0 != ret)
337                 return ret;
338
339         if (1 == buf[0])
340                 return DVB_CA_EN50221_POLL_CAM_PRESENT |
341                         DVB_CA_EN50221_POLL_CAM_READY;
342
343         return 0;
344
345 }
346
347 static void tt3650_ci_uninit(struct dvb_usb_device *d)
348 {
349         struct pctv452e_state *state;
350
351         ci_dbg("%s", __func__);
352
353         if (NULL == d)
354                 return;
355
356         state = (struct pctv452e_state *)d->priv;
357         if (NULL == state)
358                 return;
359
360         if (NULL == state->ca.data)
361                 return;
362
363         /* Error ignored. */
364         tt3650_ci_set_video_port(&state->ca, /* slot */ 0, /* enable */ 0);
365
366         dvb_ca_en50221_release(&state->ca);
367
368         memset(&state->ca, 0, sizeof(state->ca));
369 }
370
371 static int tt3650_ci_init(struct dvb_usb_adapter *a)
372 {
373         struct dvb_usb_device *d = a->dev;
374         struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
375         int ret;
376
377         ci_dbg("%s", __func__);
378
379         mutex_init(&state->ca_mutex);
380
381         state->ca.owner = THIS_MODULE;
382         state->ca.read_attribute_mem = tt3650_ci_read_attribute_mem;
383         state->ca.write_attribute_mem = tt3650_ci_write_attribute_mem;
384         state->ca.read_cam_control = tt3650_ci_read_cam_control;
385         state->ca.write_cam_control = tt3650_ci_write_cam_control;
386         state->ca.slot_reset = tt3650_ci_slot_reset;
387         state->ca.slot_shutdown = tt3650_ci_slot_shutdown;
388         state->ca.slot_ts_enable = tt3650_ci_slot_ts_enable;
389         state->ca.poll_slot_status = tt3650_ci_poll_slot_status;
390         state->ca.data = d;
391
392         ret = dvb_ca_en50221_init(&a->dvb_adap,
393                                    &state->ca,
394                                    /* flags */ 0,
395                                    /* n_slots */ 1);
396         if (0 != ret) {
397                 err("Cannot initialize CI: Error %d.", ret);
398                 memset(&state->ca, 0, sizeof(state->ca));
399                 return ret;
400         }
401
402         info("CI initialized.");
403
404         return 0;
405 }
406
407 #define CMD_BUFFER_SIZE 0x28
408 static int pctv452e_i2c_msg(struct dvb_usb_device *d, u8 addr,
409                                 const u8 *snd_buf, u8 snd_len,
410                                 u8 *rcv_buf, u8 rcv_len)
411 {
412         struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
413         u8 id;
414         int ret;
415
416         mutex_lock(&state->ca_mutex);
417         id = state->c++;
418
419         ret = -EINVAL;
420         if (snd_len > 64 - 7 || rcv_len > 64 - 7)
421                 goto failed;
422
423         state->data[0] = SYNC_BYTE_OUT;
424         state->data[1] = id;
425         state->data[2] = PCTV_CMD_I2C;
426         state->data[3] = snd_len + 3;
427         state->data[4] = addr << 1;
428         state->data[5] = snd_len;
429         state->data[6] = rcv_len;
430
431         memcpy(state->data + 7, snd_buf, snd_len);
432
433         ret = dvb_usb_generic_rw(d, state->data, 7 + snd_len,
434                                   state->data, /* rcv_len */ 64,
435                                   /* delay_ms */ 0);
436         if (ret < 0)
437                 goto failed;
438
439         /* TT USB protocol error. */
440         ret = -EIO;
441         if (SYNC_BYTE_IN != state->data[0] || id != state->data[1])
442                 goto failed;
443
444         /* I2C device didn't respond as expected. */
445         ret = -EREMOTEIO;
446         if (state->data[5] < snd_len || state->data[6] < rcv_len)
447                 goto failed;
448
449         memcpy(rcv_buf, state->data + 7, rcv_len);
450         mutex_unlock(&state->ca_mutex);
451
452         return rcv_len;
453
454 failed:
455         err("I2C error %d; %02X %02X  %02X %02X %02X -> %*ph",
456              ret, SYNC_BYTE_OUT, id, addr << 1, snd_len, rcv_len,
457              7, state->data);
458
459         mutex_unlock(&state->ca_mutex);
460         return ret;
461 }
462
463 static int pctv452e_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msg,
464                                 int num)
465 {
466         struct dvb_usb_device *d = i2c_get_adapdata(adapter);
467         int i;
468
469         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
470                 return -EAGAIN;
471
472         for (i = 0; i < num; i++) {
473                 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
474                 int ret;
475
476                 if (msg[i].flags & I2C_M_RD) {
477                         addr = msg[i].addr;
478                         snd_buf = NULL;
479                         snd_len = 0;
480                         rcv_buf = msg[i].buf;
481                         rcv_len = msg[i].len;
482                 } else {
483                         addr = msg[i].addr;
484                         snd_buf = msg[i].buf;
485                         snd_len = msg[i].len;
486                         rcv_buf = NULL;
487                         rcv_len = 0;
488                 }
489
490                 ret = pctv452e_i2c_msg(d, addr, snd_buf, snd_len, rcv_buf,
491                                         rcv_len);
492                 if (ret < rcv_len)
493                         break;
494         }
495
496         mutex_unlock(&d->i2c_mutex);
497         return i;
498 }
499
500 static u32 pctv452e_i2c_func(struct i2c_adapter *adapter)
501 {
502         return I2C_FUNC_I2C;
503 }
504
505 static int pctv452e_power_ctrl(struct dvb_usb_device *d, int i)
506 {
507         struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
508         u8 *rx;
509         int ret;
510
511         info("%s: %d\n", __func__, i);
512
513         if (!i)
514                 return 0;
515
516         if (state->initialized)
517                 return 0;
518
519         rx = kmalloc(PCTV_ANSWER_LEN, GFP_KERNEL);
520         if (!rx)
521                 return -ENOMEM;
522
523         mutex_lock(&state->ca_mutex);
524         /* hmm where shoud this should go? */
525         ret = usb_set_interface(d->udev, 0, ISOC_INTERFACE_ALTERNATIVE);
526         if (ret != 0)
527                 info("%s: Warning set interface returned: %d\n",
528                         __func__, ret);
529
530         /* this is a one-time initialization, dont know where to put */
531         state->data[0] = 0xaa;
532         state->data[1] = state->c++;
533         state->data[2] = PCTV_CMD_RESET;
534         state->data[3] = 1;
535         state->data[4] = 0;
536         /* reset board */
537         ret = dvb_usb_generic_rw(d, state->data, 5, rx, PCTV_ANSWER_LEN, 0);
538         if (ret)
539                 goto ret;
540
541         state->data[1] = state->c++;
542         state->data[4] = 1;
543         /* reset board (again?) */
544         ret = dvb_usb_generic_rw(d, state->data, 5, rx, PCTV_ANSWER_LEN, 0);
545         if (ret)
546                 goto ret;
547
548         state->initialized = 1;
549
550 ret:
551         mutex_unlock(&state->ca_mutex);
552         kfree(rx);
553         return ret;
554 }
555
556 static int pctv452e_rc_query(struct dvb_usb_device *d)
557 {
558         struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
559         int ret, i;
560         u8 id;
561
562         mutex_lock(&state->ca_mutex);
563         id = state->c++;
564
565         /* prepare command header  */
566         state->data[0] = SYNC_BYTE_OUT;
567         state->data[1] = id;
568         state->data[2] = PCTV_CMD_IR;
569         state->data[3] = 0;
570
571         /* send ir request */
572         ret = dvb_usb_generic_rw(d, state->data, 4,
573                                  state->data, PCTV_ANSWER_LEN, 0);
574         if (ret != 0)
575                 goto ret;
576
577         if (debug > 3) {
578                 info("%s: read: %2d: %*ph: ", __func__, ret, 3, state->data);
579                 for (i = 0; (i < state->data[3]) && ((i + 3) < PCTV_ANSWER_LEN); i++)
580                         info(" %02x", state->data[i + 3]);
581
582                 info("\n");
583         }
584
585         if ((state->data[3] == 9) &&  (state->data[12] & 0x01)) {
586                 /* got a "press" event */
587                 state->last_rc_key = RC_SCANCODE_RC5(state->data[7], state->data[6]);
588                 if (debug > 2)
589                         info("%s: cmd=0x%02x sys=0x%02x\n",
590                                 __func__, state->data[6], state->data[7]);
591
592                 rc_keydown(d->rc_dev, RC_TYPE_RC5, state->last_rc_key, 0);
593         } else if (state->last_rc_key) {
594                 rc_keyup(d->rc_dev);
595                 state->last_rc_key = 0;
596         }
597 ret:
598         mutex_unlock(&state->ca_mutex);
599         return ret;
600 }
601
602 static int pctv452e_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
603 {
604         const u8 mem_addr[] = { 0x1f, 0xcc };
605         u8 encoded_mac[20];
606         int ret;
607
608         ret = -EAGAIN;
609         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
610                 goto failed;
611
612         ret = pctv452e_i2c_msg(d, I2C_ADDR_24C16,
613                                 mem_addr + 1, /* snd_len */ 1,
614                                 encoded_mac, /* rcv_len */ 20);
615         if (-EREMOTEIO == ret)
616                 /* Caution! A 24C16 interprets 0xA2 0x1F 0xCC as a
617                    byte write if /WC is low. */
618                 ret = pctv452e_i2c_msg(d, I2C_ADDR_24C64,
619                                         mem_addr, 2,
620                                         encoded_mac, 20);
621
622         mutex_unlock(&d->i2c_mutex);
623
624         if (20 != ret)
625                 goto failed;
626
627         ret = ttpci_eeprom_decode_mac(mac, encoded_mac);
628         if (0 != ret)
629                 goto failed;
630
631         return 0;
632
633 failed:
634         eth_zero_addr(mac);
635
636         return ret;
637 }
638
639 static const struct stb0899_s1_reg pctv452e_init_dev[] = {
640         { STB0899_DISCNTRL1,    0x26 },
641         { STB0899_DISCNTRL2,    0x80 },
642         { STB0899_DISRX_ST0,    0x04 },
643         { STB0899_DISRX_ST1,    0x20 },
644         { STB0899_DISPARITY,    0x00 },
645         { STB0899_DISFIFO,      0x00 },
646         { STB0899_DISF22,       0x99 },
647         { STB0899_DISF22RX,     0x85 }, /* 0xa8 */
648         { STB0899_ACRPRESC,     0x11 },
649         { STB0899_ACRDIV1,      0x0a },
650         { STB0899_ACRDIV2,      0x05 },
651         { STB0899_DACR1 ,       0x00 },
652         { STB0899_DACR2 ,       0x00 },
653         { STB0899_OUTCFG,       0x00 },
654         { STB0899_MODECFG,      0x00 }, /* Inversion */
655         { STB0899_IRQMSK_3,     0xf3 },
656         { STB0899_IRQMSK_2,     0xfc },
657         { STB0899_IRQMSK_1,     0xff },
658         { STB0899_IRQMSK_0,     0xff },
659         { STB0899_I2CCFG,       0x88 },
660         { STB0899_I2CRPT,       0x58 },
661         { STB0899_GPIO00CFG,    0x82 },
662         { STB0899_GPIO01CFG,    0x82 }, /* LED: 0x02 green, 0x82 orange */
663         { STB0899_GPIO02CFG,    0x82 },
664         { STB0899_GPIO03CFG,    0x82 },
665         { STB0899_GPIO04CFG,    0x82 },
666         { STB0899_GPIO05CFG,    0x82 },
667         { STB0899_GPIO06CFG,    0x82 },
668         { STB0899_GPIO07CFG,    0x82 },
669         { STB0899_GPIO08CFG,    0x82 },
670         { STB0899_GPIO09CFG,    0x82 },
671         { STB0899_GPIO10CFG,    0x82 },
672         { STB0899_GPIO11CFG,    0x82 },
673         { STB0899_GPIO12CFG,    0x82 },
674         { STB0899_GPIO13CFG,    0x82 },
675         { STB0899_GPIO14CFG,    0x82 },
676         { STB0899_GPIO15CFG,    0x82 },
677         { STB0899_GPIO16CFG,    0x82 },
678         { STB0899_GPIO17CFG,    0x82 },
679         { STB0899_GPIO18CFG,    0x82 },
680         { STB0899_GPIO19CFG,    0x82 },
681         { STB0899_GPIO20CFG,    0x82 },
682         { STB0899_SDATCFG,      0xb8 },
683         { STB0899_SCLTCFG,      0xba },
684         { STB0899_AGCRFCFG,     0x1c }, /* 0x11 DVB-S; 0x1c DVB-S2 (1c, rjkm) */
685         { STB0899_GPIO22,       0x82 },
686         { STB0899_GPIO21,       0x91 },
687         { STB0899_DIRCLKCFG,    0x82 },
688         { STB0899_CLKOUT27CFG,  0x7e },
689         { STB0899_STDBYCFG,     0x82 },
690         { STB0899_CS0CFG,       0x82 },
691         { STB0899_CS1CFG,       0x82 },
692         { STB0899_DISEQCOCFG,   0x20 },
693         { STB0899_NCOARSE,      0x15 }, /* 0x15 27Mhz, F/3 198MHz, F/6 108MHz */
694         { STB0899_SYNTCTRL,     0x00 }, /* 0x00 CLKI, 0x02 XTALI */
695         { STB0899_FILTCTRL,     0x00 },
696         { STB0899_SYSCTRL,      0x00 },
697         { STB0899_STOPCLK1,     0x20 }, /* orig: 0x00 budget-ci: 0x20 */
698         { STB0899_STOPCLK2,     0x00 },
699         { STB0899_INTBUFCTRL,   0x0a },
700         { STB0899_AGC2I1,       0x00 },
701         { STB0899_AGC2I2,       0x00 },
702         { STB0899_AGCIQIN,      0x00 },
703         { STB0899_TSTRES,       0x40 }, /* rjkm */
704         { 0xffff,               0xff },
705 };
706
707 static const struct stb0899_s1_reg pctv452e_init_s1_demod[] = {
708         { STB0899_DEMOD,        0x00 },
709         { STB0899_RCOMPC,       0xc9 },
710         { STB0899_AGC1CN,       0x01 },
711         { STB0899_AGC1REF,      0x10 },
712         { STB0899_RTC,          0x23 },
713         { STB0899_TMGCFG,       0x4e },
714         { STB0899_AGC2REF,      0x34 },
715         { STB0899_TLSR,         0x84 },
716         { STB0899_CFD,          0xf7 },
717         { STB0899_ACLC,         0x87 },
718         { STB0899_BCLC,         0x94 },
719         { STB0899_EQON,         0x41 },
720         { STB0899_LDT,          0xf1 },
721         { STB0899_LDT2,         0xe3 },
722         { STB0899_EQUALREF,     0xb4 },
723         { STB0899_TMGRAMP,      0x10 },
724         { STB0899_TMGTHD,       0x30 },
725         { STB0899_IDCCOMP,      0xfd },
726         { STB0899_QDCCOMP,      0xff },
727         { STB0899_POWERI,       0x0c },
728         { STB0899_POWERQ,       0x0f },
729         { STB0899_RCOMP,        0x6c },
730         { STB0899_AGCIQIN,      0x80 },
731         { STB0899_AGC2I1,       0x06 },
732         { STB0899_AGC2I2,       0x00 },
733         { STB0899_TLIR,         0x30 },
734         { STB0899_RTF,          0x7f },
735         { STB0899_DSTATUS,      0x00 },
736         { STB0899_LDI,          0xbc },
737         { STB0899_CFRM,         0xea },
738         { STB0899_CFRL,         0x31 },
739         { STB0899_NIRM,         0x2b },
740         { STB0899_NIRL,         0x80 },
741         { STB0899_ISYMB,        0x1d },
742         { STB0899_QSYMB,        0xa6 },
743         { STB0899_SFRH,         0x2f },
744         { STB0899_SFRM,         0x68 },
745         { STB0899_SFRL,         0x40 },
746         { STB0899_SFRUPH,       0x2f },
747         { STB0899_SFRUPM,       0x68 },
748         { STB0899_SFRUPL,       0x40 },
749         { STB0899_EQUAI1,       0x02 },
750         { STB0899_EQUAQ1,       0xff },
751         { STB0899_EQUAI2,       0x04 },
752         { STB0899_EQUAQ2,       0x05 },
753         { STB0899_EQUAI3,       0x02 },
754         { STB0899_EQUAQ3,       0xfd },
755         { STB0899_EQUAI4,       0x03 },
756         { STB0899_EQUAQ4,       0x07 },
757         { STB0899_EQUAI5,       0x08 },
758         { STB0899_EQUAQ5,       0xf5 },
759         { STB0899_DSTATUS2,     0x00 },
760         { STB0899_VSTATUS,      0x00 },
761         { STB0899_VERROR,       0x86 },
762         { STB0899_IQSWAP,       0x2a },
763         { STB0899_ECNT1M,       0x00 },
764         { STB0899_ECNT1L,       0x00 },
765         { STB0899_ECNT2M,       0x00 },
766         { STB0899_ECNT2L,       0x00 },
767         { STB0899_ECNT3M,       0x0a },
768         { STB0899_ECNT3L,       0xad },
769         { STB0899_FECAUTO1,     0x06 },
770         { STB0899_FECM,         0x01 },
771         { STB0899_VTH12,        0xb0 },
772         { STB0899_VTH23,        0x7a },
773         { STB0899_VTH34,        0x58 },
774         { STB0899_VTH56,        0x38 },
775         { STB0899_VTH67,        0x34 },
776         { STB0899_VTH78,        0x24 },
777         { STB0899_PRVIT,        0xff },
778         { STB0899_VITSYNC,      0x19 },
779         { STB0899_RSULC,        0xb1 }, /* DVB = 0xb1, DSS = 0xa1 */
780         { STB0899_TSULC,        0x42 },
781         { STB0899_RSLLC,        0x41 },
782         { STB0899_TSLPL,        0x12 },
783         { STB0899_TSCFGH,       0x0c },
784         { STB0899_TSCFGM,       0x00 },
785         { STB0899_TSCFGL,       0x00 },
786         { STB0899_TSOUT,        0x69 }, /* 0x0d for CAM */
787         { STB0899_RSSYNCDEL,    0x00 },
788         { STB0899_TSINHDELH,    0x02 },
789         { STB0899_TSINHDELM,    0x00 },
790         { STB0899_TSINHDELL,    0x00 },
791         { STB0899_TSLLSTKM,     0x1b },
792         { STB0899_TSLLSTKL,     0xb3 },
793         { STB0899_TSULSTKM,     0x00 },
794         { STB0899_TSULSTKL,     0x00 },
795         { STB0899_PCKLENUL,     0xbc },
796         { STB0899_PCKLENLL,     0xcc },
797         { STB0899_RSPCKLEN,     0xbd },
798         { STB0899_TSSTATUS,     0x90 },
799         { STB0899_ERRCTRL1,     0xb6 },
800         { STB0899_ERRCTRL2,     0x95 },
801         { STB0899_ERRCTRL3,     0x8d },
802         { STB0899_DMONMSK1,     0x27 },
803         { STB0899_DMONMSK0,     0x03 },
804         { STB0899_DEMAPVIT,     0x5c },
805         { STB0899_PLPARM,       0x19 },
806         { STB0899_PDELCTRL,     0x48 },
807         { STB0899_PDELCTRL2,    0x00 },
808         { STB0899_BBHCTRL1,     0x00 },
809         { STB0899_BBHCTRL2,     0x00 },
810         { STB0899_HYSTTHRESH,   0x77 },
811         { STB0899_MATCSTM,      0x00 },
812         { STB0899_MATCSTL,      0x00 },
813         { STB0899_UPLCSTM,      0x00 },
814         { STB0899_UPLCSTL,      0x00 },
815         { STB0899_DFLCSTM,      0x00 },
816         { STB0899_DFLCSTL,      0x00 },
817         { STB0899_SYNCCST,      0x00 },
818         { STB0899_SYNCDCSTM,    0x00 },
819         { STB0899_SYNCDCSTL,    0x00 },
820         { STB0899_ISI_ENTRY,    0x00 },
821         { STB0899_ISI_BIT_EN,   0x00 },
822         { STB0899_MATSTRM,      0xf0 },
823         { STB0899_MATSTRL,      0x02 },
824         { STB0899_UPLSTRM,      0x45 },
825         { STB0899_UPLSTRL,      0x60 },
826         { STB0899_DFLSTRM,      0xe3 },
827         { STB0899_DFLSTRL,      0x00 },
828         { STB0899_SYNCSTR,      0x47 },
829         { STB0899_SYNCDSTRM,    0x05 },
830         { STB0899_SYNCDSTRL,    0x18 },
831         { STB0899_CFGPDELSTATUS1, 0x19 },
832         { STB0899_CFGPDELSTATUS2, 0x2b },
833         { STB0899_BBFERRORM,    0x00 },
834         { STB0899_BBFERRORL,    0x01 },
835         { STB0899_UPKTERRORM,   0x00 },
836         { STB0899_UPKTERRORL,   0x00 },
837         { 0xffff,               0xff },
838 };
839
840 static struct stb0899_config stb0899_config = {
841         .init_dev       = pctv452e_init_dev,
842         .init_s2_demod  = stb0899_s2_init_2,
843         .init_s1_demod  = pctv452e_init_s1_demod,
844         .init_s2_fec    = stb0899_s2_init_4,
845         .init_tst       = stb0899_s1_init_5,
846
847         .demod_address   = I2C_ADDR_STB0899, /* I2C Address */
848         .block_sync_mode = STB0899_SYNC_FORCED, /* ? */
849
850         .xtal_freq       = 27000000,     /* Assume Hz ? */
851         .inversion       = IQ_SWAP_ON,
852
853         .lo_clk   = 76500000,
854         .hi_clk   = 99000000,
855
856         .ts_output_mode  = 0,   /* Use parallel mode */
857         .clock_polarity  = 0,
858         .data_clk_parity = 0,
859         .fec_mode       = 0,
860
861         .esno_ave           = STB0899_DVBS2_ESNO_AVE,
862         .esno_quant       = STB0899_DVBS2_ESNO_QUANT,
863         .avframes_coarse     = STB0899_DVBS2_AVFRAMES_COARSE,
864         .avframes_fine       = STB0899_DVBS2_AVFRAMES_FINE,
865         .miss_threshold      = STB0899_DVBS2_MISS_THRESHOLD,
866         .uwp_threshold_acq   = STB0899_DVBS2_UWP_THRESHOLD_ACQ,
867         .uwp_threshold_track = STB0899_DVBS2_UWP_THRESHOLD_TRACK,
868         .uwp_threshold_sof   = STB0899_DVBS2_UWP_THRESHOLD_SOF,
869         .sof_search_timeout  = STB0899_DVBS2_SOF_SEARCH_TIMEOUT,
870
871         .btr_nco_bits     = STB0899_DVBS2_BTR_NCO_BITS,
872         .btr_gain_shift_offset = STB0899_DVBS2_BTR_GAIN_SHIFT_OFFSET,
873         .crl_nco_bits     = STB0899_DVBS2_CRL_NCO_BITS,
874         .ldpc_max_iter   = STB0899_DVBS2_LDPC_MAX_ITER,
875
876         .tuner_get_frequency    = stb6100_get_frequency,
877         .tuner_set_frequency    = stb6100_set_frequency,
878         .tuner_set_bandwidth    = stb6100_set_bandwidth,
879         .tuner_get_bandwidth    = stb6100_get_bandwidth,
880         .tuner_set_rfsiggain    = NULL,
881
882         /* helper for switching LED green/orange */
883         .postproc = pctv45e_postproc
884 };
885
886 static struct stb6100_config stb6100_config = {
887         .tuner_address = I2C_ADDR_STB6100,
888         .refclock      = 27000000
889 };
890
891
892 static struct i2c_algorithm pctv452e_i2c_algo = {
893         .master_xfer   = pctv452e_i2c_xfer,
894         .functionality = pctv452e_i2c_func
895 };
896
897 static int pctv452e_frontend_attach(struct dvb_usb_adapter *a)
898 {
899         struct usb_device_id *id;
900
901         a->fe_adap[0].fe = dvb_attach(stb0899_attach, &stb0899_config,
902                                                 &a->dev->i2c_adap);
903         if (!a->fe_adap[0].fe)
904                 return -ENODEV;
905         if ((dvb_attach(lnbp22_attach, a->fe_adap[0].fe,
906                                         &a->dev->i2c_adap)) == NULL)
907                 err("Cannot attach lnbp22\n");
908
909         id = a->dev->desc->warm_ids[0];
910         if (USB_VID_TECHNOTREND == id->idVendor
911             && USB_PID_TECHNOTREND_CONNECT_S2_3650_CI == id->idProduct)
912                 /* Error ignored. */
913                 tt3650_ci_init(a);
914
915         return 0;
916 }
917
918 static int pctv452e_tuner_attach(struct dvb_usb_adapter *a)
919 {
920         if (!a->fe_adap[0].fe)
921                 return -ENODEV;
922         if (dvb_attach(stb6100_attach, a->fe_adap[0].fe, &stb6100_config,
923                                         &a->dev->i2c_adap) == NULL) {
924                 err("%s failed\n", __func__);
925                 return -ENODEV;
926         }
927
928         return 0;
929 }
930
931 static struct usb_device_id pctv452e_usb_table[] = {
932         {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PCTV_452E)},
933         {USB_DEVICE(USB_VID_TECHNOTREND, USB_PID_TECHNOTREND_CONNECT_S2_3600)},
934         {USB_DEVICE(USB_VID_TECHNOTREND,
935                                 USB_PID_TECHNOTREND_CONNECT_S2_3650_CI)},
936         {}
937 };
938 MODULE_DEVICE_TABLE(usb, pctv452e_usb_table);
939
940 static struct dvb_usb_device_properties pctv452e_properties = {
941         .caps = DVB_USB_IS_AN_I2C_ADAPTER, /* more ? */
942         .usb_ctrl = DEVICE_SPECIFIC,
943
944         .size_of_priv     = sizeof(struct pctv452e_state),
945
946         .power_ctrl       = pctv452e_power_ctrl,
947
948         .rc.core = {
949                 .rc_codes       = RC_MAP_DIB0700_RC5_TABLE,
950                 .allowed_protos = RC_BIT_RC5,
951                 .rc_query       = pctv452e_rc_query,
952                 .rc_interval    = 100,
953         },
954
955         .num_adapters     = 1,
956         .adapter = {{
957                 .num_frontends = 1,
958                 .fe = {{
959                         .frontend_attach  = pctv452e_frontend_attach,
960                         .tuner_attach     = pctv452e_tuner_attach,
961
962                         /* parameter for the MPEG2-data transfer */
963                         .stream = {
964                                 .type     = USB_ISOC,
965                                 .count    = 4,
966                                 .endpoint = 0x02,
967                                 .u = {
968                                         .isoc = {
969                                                 .framesperurb = 4,
970                                                 .framesize    = 940,
971                                                 .interval     = 1
972                                         }
973                                 }
974                         },
975                 } },
976         } },
977
978         .i2c_algo = &pctv452e_i2c_algo,
979
980         .generic_bulk_ctrl_endpoint = 1, /* allow generice rw function */
981
982         .num_device_descs = 1,
983         .devices = {
984                 { .name = "PCTV HDTV USB",
985                   .cold_ids = { NULL, NULL }, /* this is a warm only device */
986                   .warm_ids = { &pctv452e_usb_table[0], NULL }
987                 },
988                 { NULL },
989         }
990 };
991
992 static struct dvb_usb_device_properties tt_connect_s2_3600_properties = {
993         .caps = DVB_USB_IS_AN_I2C_ADAPTER, /* more ? */
994         .usb_ctrl = DEVICE_SPECIFIC,
995
996         .size_of_priv           = sizeof(struct pctv452e_state),
997
998         .power_ctrl             = pctv452e_power_ctrl,
999         .read_mac_address       = pctv452e_read_mac_address,
1000
1001         .rc.core = {
1002                 .rc_codes       = RC_MAP_TT_1500,
1003                 .allowed_protos = RC_BIT_RC5,
1004                 .rc_query       = pctv452e_rc_query,
1005                 .rc_interval    = 100,
1006         },
1007
1008         .num_adapters           = 1,
1009         .adapter = {{
1010                 .num_frontends = 1,
1011                 .fe = {{
1012                         .frontend_attach = pctv452e_frontend_attach,
1013                         .tuner_attach = pctv452e_tuner_attach,
1014
1015                         /* parameter for the MPEG2-data transfer */
1016                         .stream = {
1017                                 .type = USB_ISOC,
1018                                 .count = 4,
1019                                 .endpoint = 0x02,
1020                                 .u = {
1021                                         .isoc = {
1022                                                 .framesperurb = 64,
1023                                                 .framesize = 940,
1024                                                 .interval = 1
1025                                         }
1026                                 }
1027                         },
1028
1029                 } },
1030         } },
1031
1032         .i2c_algo = &pctv452e_i2c_algo,
1033
1034         .generic_bulk_ctrl_endpoint = 1, /* allow generic rw function*/
1035
1036         .num_device_descs = 2,
1037         .devices = {
1038                 { .name = "Technotrend TT Connect S2-3600",
1039                   .cold_ids = { NULL, NULL }, /* this is a warm only device */
1040                   .warm_ids = { &pctv452e_usb_table[1], NULL }
1041                 },
1042                 { .name = "Technotrend TT Connect S2-3650-CI",
1043                   .cold_ids = { NULL, NULL },
1044                   .warm_ids = { &pctv452e_usb_table[2], NULL }
1045                 },
1046                 { NULL },
1047         }
1048 };
1049
1050 static void pctv452e_usb_disconnect(struct usb_interface *intf)
1051 {
1052         struct dvb_usb_device *d = usb_get_intfdata(intf);
1053
1054         tt3650_ci_uninit(d);
1055         dvb_usb_device_exit(intf);
1056 }
1057
1058 static int pctv452e_usb_probe(struct usb_interface *intf,
1059                                 const struct usb_device_id *id)
1060 {
1061         if (0 == dvb_usb_device_init(intf, &pctv452e_properties,
1062                                         THIS_MODULE, NULL, adapter_nr) ||
1063             0 == dvb_usb_device_init(intf, &tt_connect_s2_3600_properties,
1064                                         THIS_MODULE, NULL, adapter_nr))
1065                 return 0;
1066
1067         return -ENODEV;
1068 }
1069
1070 static struct usb_driver pctv452e_usb_driver = {
1071         .name       = "pctv452e",
1072         .probe      = pctv452e_usb_probe,
1073         .disconnect = pctv452e_usb_disconnect,
1074         .id_table   = pctv452e_usb_table,
1075 };
1076
1077 module_usb_driver(pctv452e_usb_driver);
1078
1079 MODULE_AUTHOR("Dominik Kuhlen <dkuhlen@gmx.net>");
1080 MODULE_AUTHOR("Andre Weidemann <Andre.Weidemann@web.de>");
1081 MODULE_AUTHOR("Michael H. Schimek <mschimek@gmx.at>");
1082 MODULE_DESCRIPTION("Pinnacle PCTV HDTV USB DVB / TT connect S2-3600 Driver");
1083 MODULE_LICENSE("GPL");