Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / media / usb / ttusb-budget / dvb-ttusb-budget.c
blob9e016b71aa9124fc70b40a409dc8f51682c9b512
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * TTUSB DVB driver
5 * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
6 * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
7 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/wait.h>
14 #include <linux/fs.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/delay.h>
18 #include <linux/time.h>
19 #include <linux/errno.h>
20 #include <linux/jiffies.h>
21 #include <linux/mutex.h>
22 #include <linux/firmware.h>
24 #include <media/dvb_frontend.h>
25 #include <media/dmxdev.h>
26 #include <media/dvb_demux.h>
27 #include <media/dvb_net.h>
28 #include "ves1820.h"
29 #include "cx22700.h"
30 #include "tda1004x.h"
31 #include "stv0299.h"
32 #include "tda8083.h"
33 #include "stv0297.h"
34 #include "lnbp21.h"
36 #include <linux/dvb/frontend.h>
37 #include <linux/dvb/dmx.h>
38 #include <linux/pci.h>
41 TTUSB_HWSECTIONS:
42 the DSP supports filtering in hardware, however, since the "muxstream"
43 is a bit braindead (no matching channel masks or no matching filter mask),
44 we won't support this - yet. it doesn't event support negative filters,
45 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
46 parse TS data. USB bandwidth will be a problem when having large
47 datastreams, especially for dvb-net, but hey, that's not my problem.
49 TTUSB_DISEQC, TTUSB_TONE:
50 let the STC do the diseqc/tone stuff. this isn't supported at least with
51 my TTUSB, so let it undef'd unless you want to implement another
52 frontend. never tested.
54 debug:
55 define it to > 3 for really hardcore debugging. you probably don't want
56 this unless the device doesn't load at all. > 2 for bandwidth statistics.
59 static int debug;
60 module_param(debug, int, 0644);
61 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
63 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
65 #define dprintk(fmt, arg...) do { \
66 if (debug) \
67 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
68 __func__, ##arg); \
69 } while (0)
72 #define ISO_BUF_COUNT 4
73 #define FRAMES_PER_ISO_BUF 4
74 #define ISO_FRAME_SIZE 912
75 #define TTUSB_MAXCHANNEL 32
76 #ifdef TTUSB_HWSECTIONS
77 #define TTUSB_MAXFILTER 16 /* ??? */
78 #endif
80 #define TTUSB_REV_2_2 0x22
81 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
83 #define MAX_SEND 0x28
84 #define MAX_RCV 0x20
87 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
88 * the dvb_demux field must be the first in struct!!
90 struct ttusb {
91 struct dvb_demux dvb_demux;
92 struct dmxdev dmxdev;
93 struct dvb_net dvbnet;
95 /* and one for USB access. */
96 struct mutex semi2c;
97 struct mutex semusb;
99 struct dvb_adapter adapter;
100 struct usb_device *dev;
102 struct i2c_adapter i2c_adap;
104 int disconnecting;
105 int iso_streaming;
107 unsigned int bulk_out_pipe;
108 unsigned int bulk_in_pipe;
109 unsigned int isoc_in_pipe;
111 void *iso_buffer;
113 struct urb *iso_urb[ISO_BUF_COUNT];
115 int running_feed_count;
116 int last_channel;
117 int last_filter;
119 u8 c; /* transaction counter, wraps around... */
120 enum fe_sec_tone_mode tone;
121 enum fe_sec_voltage voltage;
123 int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
124 u8 mux_npacks;
125 u8 muxpack[256 + 8];
126 int muxpack_ptr, muxpack_len;
128 int insync;
130 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
131 /* (including stuffing. yes. really.) */
133 u8 send_buf[MAX_SEND];
134 u8 last_result[MAX_RCV];
136 int revision;
138 struct dvb_frontend* fe;
141 static int ttusb_cmd(struct ttusb *ttusb, u8 *data, int len, int len_result)
143 int actual_len;
144 int err;
146 if (mutex_lock_interruptible(&ttusb->semusb) < 0)
147 return -EAGAIN;
149 if (debug >= 3)
150 dprintk("> %*ph\n", len, data);
152 memcpy(data, ttusb->send_buf, len);
154 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
155 ttusb->send_buf, len, &actual_len, 1000);
156 if (err != 0) {
157 dprintk("usb_bulk_msg(send) failed, err == %i!\n", err);
158 goto err;
160 if (actual_len != len) {
161 err = -EIO;
162 dprintk("only wrote %d of %d bytes\n",
163 actual_len, len);
164 goto err;
167 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
168 ttusb->last_result, MAX_RCV, &actual_len, 1000);
170 if (err != 0) {
171 pr_err("cmd xter failed, receive error %d\n", err);
172 goto err;
175 if (debug >= 3) {
176 actual_len = ttusb->last_result[3] + 4;
177 dprintk("< %*ph\n", actual_len, ttusb->last_result);
180 if (len_result)
181 memcpy(ttusb->send_buf, ttusb->last_result, len_result);
183 err:
184 mutex_unlock(&ttusb->semusb);
185 return err;
188 static int ttusb_i2c_msg(struct ttusb *ttusb,
189 u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
190 u8 rcv_len)
192 u8 b[MAX_SEND];
193 u8 id = ++ttusb->c;
194 int i, err;
196 if (snd_len > MAX_SEND - 7 || rcv_len > MAX_RCV - 7)
197 return -EINVAL;
199 b[0] = 0xaa;
200 b[1] = id;
201 b[2] = 0x31;
202 b[3] = snd_len + 3;
203 b[4] = addr << 1;
204 b[5] = snd_len;
205 b[6] = rcv_len;
207 for (i = 0; i < snd_len; i++)
208 b[7 + i] = snd_buf[i];
210 err = ttusb_cmd(ttusb, b, snd_len + 7, MAX_RCV);
212 if (err)
213 return -EREMOTEIO;
215 /* check if the i2c transaction was successful */
216 if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
218 if (rcv_len > 0) {
220 if (err || b[0] != 0x55 || b[1] != id) {
221 dprintk("usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
222 err, id);
223 return -EREMOTEIO;
226 for (i = 0; i < rcv_len; i++)
227 rcv_buf[i] = b[7 + i];
230 return rcv_len;
233 static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
235 struct ttusb *ttusb = i2c_get_adapdata(adapter);
236 int i = 0;
237 int inc;
239 if (mutex_lock_interruptible(&ttusb->semi2c) < 0)
240 return -EAGAIN;
242 while (i < num) {
243 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
244 int err;
246 if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
247 addr = msg[i].addr;
248 snd_buf = msg[i].buf;
249 snd_len = msg[i].len;
250 rcv_buf = msg[i + 1].buf;
251 rcv_len = msg[i + 1].len;
252 inc = 2;
253 } else {
254 addr = msg[i].addr;
255 snd_buf = msg[i].buf;
256 snd_len = msg[i].len;
257 rcv_buf = NULL;
258 rcv_len = 0;
259 inc = 1;
262 err = ttusb_i2c_msg(ttusb, addr,
263 snd_buf, snd_len, rcv_buf, rcv_len);
265 if (err < rcv_len) {
266 dprintk("i == %i\n", i);
267 break;
270 i += inc;
273 mutex_unlock(&ttusb->semi2c);
274 return i;
277 static int ttusb_boot_dsp(struct ttusb *ttusb)
279 const struct firmware *fw;
280 int i, err;
281 u8 b[40];
283 err = request_firmware(&fw, "ttusb-budget/dspbootcode.bin",
284 &ttusb->dev->dev);
285 if (err) {
286 pr_err("failed to request firmware\n");
287 return err;
290 /* BootBlock */
291 b[0] = 0xaa;
292 b[2] = 0x13;
293 b[3] = 28;
295 /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
296 /* 32 is max packet size, no messages should be split. */
297 for (i = 0; i < fw->size; i += 28) {
298 memcpy(&b[4], &fw->data[i], 28);
300 b[1] = ++ttusb->c;
302 err = ttusb_cmd(ttusb, b, 32, 0);
303 if (err)
304 goto done;
307 /* last block ... */
308 b[1] = ++ttusb->c;
309 b[2] = 0x13;
310 b[3] = 0;
312 err = ttusb_cmd(ttusb, b, 4, 0);
313 if (err)
314 goto done;
316 /* BootEnd */
317 b[1] = ++ttusb->c;
318 b[2] = 0x14;
319 b[3] = 0;
321 err = ttusb_cmd(ttusb, b, 4, 0);
323 done:
324 release_firmware(fw);
325 if (err) {
326 dprintk("usb_bulk_msg() failed, return value %i!\n", err);
329 return err;
332 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
333 int pid)
335 int err;
336 /* SetChannel */
337 u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
338 (pid >> 8) & 0xff, pid & 0xff
341 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
342 return err;
345 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
347 int err;
348 /* DelChannel */
349 u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
351 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
352 return err;
355 #ifdef TTUSB_HWSECTIONS
356 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
357 int associated_chan, u8 filter[8], u8 mask[8])
359 int err;
360 /* SetFilter */
361 u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
362 filter[0], filter[1], filter[2], filter[3],
363 filter[4], filter[5], filter[6], filter[7],
364 filter[8], filter[9], filter[10], filter[11],
365 mask[0], mask[1], mask[2], mask[3],
366 mask[4], mask[5], mask[6], mask[7],
367 mask[8], mask[9], mask[10], mask[11]
370 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
371 return err;
374 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
376 int err;
377 /* DelFilter */
378 u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
380 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
381 return err;
383 #endif
385 static int ttusb_init_controller(struct ttusb *ttusb)
387 u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
388 u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
389 u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
390 /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
391 u8 b3[] =
392 { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
394 u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
395 u8 get_dsp_version[0x20] =
396 { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
397 int err;
399 /* reset board */
400 if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
401 return err;
403 /* reset board (again?) */
404 if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
405 return err;
407 ttusb_boot_dsp(ttusb);
409 /* set i2c bit rate */
410 if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
411 return err;
413 if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 0)))
414 return err;
416 if ((err = ttusb_cmd(ttusb, get_version,
417 sizeof(get_version), sizeof(get_version))))
418 return err;
420 dprintk("stc-version: %c%c%c%c%c\n", get_version[4], get_version[5],
421 get_version[6], get_version[7], get_version[8]);
423 if (memcmp(get_version + 4, "V 0.0", 5) &&
424 memcmp(get_version + 4, "V 1.1", 5) &&
425 memcmp(get_version + 4, "V 2.1", 5) &&
426 memcmp(get_version + 4, "V 2.2", 5)) {
427 pr_err("unknown STC version %c%c%c%c%c, please report!\n",
428 get_version[4], get_version[5],
429 get_version[6], get_version[7], get_version[8]);
432 ttusb->revision = ((get_version[6] - '0') << 4) |
433 (get_version[8] - '0');
435 err =
436 ttusb_cmd(ttusb, get_dsp_version,
437 sizeof(get_dsp_version), sizeof(get_dsp_version));
438 if (err)
439 return err;
441 pr_info("dsp-version: %c%c%c\n",
442 get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
443 return 0;
446 #ifdef TTUSB_DISEQC
447 static int ttusb_send_diseqc(struct dvb_frontend* fe,
448 const struct dvb_diseqc_master_cmd *cmd)
450 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
451 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
453 int err;
455 b[3] = 4 + 2 + cmd->msg_len;
456 b[4] = 0xFF; /* send diseqc master, not burst */
457 b[5] = cmd->msg_len;
459 memcpy(b + 5, cmd->msg, cmd->msg_len);
461 /* Diseqc */
462 if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
463 dprintk("usb_bulk_msg() failed, return value %i!\n", err);
466 return err;
468 #endif
470 static int ttusb_update_lnb(struct ttusb *ttusb)
472 u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
473 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
474 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
476 int err;
478 /* SetLNB */
479 if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
480 dprintk("usb_bulk_msg() failed, return value %i!\n", err);
483 return err;
486 static int ttusb_set_voltage(struct dvb_frontend *fe,
487 enum fe_sec_voltage voltage)
489 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
491 ttusb->voltage = voltage;
492 return ttusb_update_lnb(ttusb);
495 #ifdef TTUSB_TONE
496 static int ttusb_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
498 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
500 ttusb->tone = tone;
501 return ttusb_update_lnb(ttusb);
503 #endif
506 #if 0
507 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
509 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
510 int err, actual_len;
512 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
513 if (err) {
514 dprintk("usb_bulk_msg() failed, return value %i!\n", err);
517 #endif
519 /*****************************************************************************/
521 #ifdef TTUSB_HWSECTIONS
522 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
523 const u8 * data, int len);
524 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
525 const u8 * data, int len);
526 #endif
528 static int numpkt, numts, numstuff, numsec, numinvalid;
529 static unsigned long lastj;
531 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
532 int len)
534 u16 csum = 0, cc;
535 int i;
537 if (len < 4 || len & 0x1) {
538 pr_warn("muxpack has invalid len %d\n", len);
539 numinvalid++;
540 return;
543 for (i = 0; i < len; i += 2)
544 csum ^= le16_to_cpup((__le16 *) (muxpack + i));
545 if (csum) {
546 pr_warn("muxpack with incorrect checksum, ignoring\n");
547 numinvalid++;
548 return;
551 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
552 cc &= 0x7FFF;
553 if ((cc != ttusb->cc) && (ttusb->cc != -1))
554 pr_warn("cc discontinuity (%d frames missing)\n",
555 (cc - ttusb->cc) & 0x7FFF);
556 ttusb->cc = (cc + 1) & 0x7FFF;
557 if (muxpack[0] & 0x80) {
558 #ifdef TTUSB_HWSECTIONS
559 /* section data */
560 int pusi = muxpack[0] & 0x40;
561 int channel = muxpack[0] & 0x1F;
562 int payload = muxpack[1];
563 const u8 *data = muxpack + 2;
564 /* check offset flag */
565 if (muxpack[0] & 0x20)
566 data++;
568 ttusb_handle_sec_data(ttusb->channel + channel, data,
569 payload);
570 data += payload;
572 if ((!!(ttusb->muxpack[0] & 0x20)) ^
573 !!(ttusb->muxpack[1] & 1))
574 data++;
575 #warning TODO: pusi
576 dprintk("cc: %04x\n", (data[0] << 8) | data[1]);
577 #endif
578 numsec++;
579 } else if (muxpack[0] == 0x47) {
580 #ifdef TTUSB_HWSECTIONS
581 /* we have TS data here! */
582 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
583 int channel;
584 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
585 if (ttusb->channel[channel].active
586 && (pid == ttusb->channel[channel].pid))
587 ttusb_handle_ts_data(ttusb->channel +
588 channel, muxpack,
589 188);
590 #endif
591 numts++;
592 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
593 } else if (muxpack[0] != 0) {
594 numinvalid++;
595 pr_err("illegal muxpack type %02x\n", muxpack[0]);
596 } else
597 numstuff++;
600 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
602 int maxwork = 1024;
603 while (len) {
604 if (!(maxwork--)) {
605 pr_err("too much work\n");
606 break;
609 switch (ttusb->mux_state) {
610 case 0:
611 case 1:
612 case 2:
613 len--;
614 if (*data++ == 0xAA)
615 ++ttusb->mux_state;
616 else {
617 ttusb->mux_state = 0;
618 if (ttusb->insync) {
619 pr_info("lost sync.\n");
620 ttusb->insync = 0;
623 break;
624 case 3:
625 ttusb->insync = 1;
626 len--;
627 ttusb->mux_npacks = *data++;
628 ++ttusb->mux_state;
629 ttusb->muxpack_ptr = 0;
630 /* maximum bytes, until we know the length */
631 ttusb->muxpack_len = 2;
632 break;
633 case 4:
635 int avail;
636 avail = len;
637 if (avail >
638 (ttusb->muxpack_len -
639 ttusb->muxpack_ptr))
640 avail =
641 ttusb->muxpack_len -
642 ttusb->muxpack_ptr;
643 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
644 data, avail);
645 ttusb->muxpack_ptr += avail;
646 BUG_ON(ttusb->muxpack_ptr > 264);
647 data += avail;
648 len -= avail;
649 /* determine length */
650 if (ttusb->muxpack_ptr == 2) {
651 if (ttusb->muxpack[0] & 0x80) {
652 ttusb->muxpack_len =
653 ttusb->muxpack[1] + 2;
654 if (ttusb->
655 muxpack[0] & 0x20)
656 ttusb->
657 muxpack_len++;
658 if ((!!
659 (ttusb->
660 muxpack[0] & 0x20)) ^
661 !!(ttusb->
662 muxpack[1] & 1))
663 ttusb->
664 muxpack_len++;
665 ttusb->muxpack_len += 4;
666 } else if (ttusb->muxpack[0] ==
667 0x47)
668 ttusb->muxpack_len =
669 188 + 4;
670 else if (ttusb->muxpack[0] == 0x00)
671 ttusb->muxpack_len =
672 ttusb->muxpack[1] + 2 +
674 else {
675 dprintk("invalid state: first byte is %x\n",
676 ttusb->muxpack[0]);
677 ttusb->mux_state = 0;
682 * if length is valid and we reached the end:
683 * goto next muxpack
685 if ((ttusb->muxpack_ptr >= 2) &&
686 (ttusb->muxpack_ptr ==
687 ttusb->muxpack_len)) {
688 ttusb_process_muxpack(ttusb,
689 ttusb->
690 muxpack,
691 ttusb->
692 muxpack_ptr);
693 ttusb->muxpack_ptr = 0;
694 /* maximum bytes, until we know the length */
695 ttusb->muxpack_len = 2;
698 * no muxpacks left?
699 * return to search-sync state
701 if (!ttusb->mux_npacks--) {
702 ttusb->mux_state = 0;
703 break;
706 break;
708 default:
709 BUG();
710 break;
715 static void ttusb_iso_irq(struct urb *urb)
717 struct ttusb *ttusb = urb->context;
718 struct usb_iso_packet_descriptor *d;
719 u8 *data;
720 int len, i;
722 if (!ttusb->iso_streaming)
723 return;
725 if (!urb->status) {
726 for (i = 0; i < urb->number_of_packets; ++i) {
727 numpkt++;
728 if (time_after_eq(jiffies, lastj + HZ)) {
729 dprintk("frames/s: %lu (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
730 numpkt * HZ / (jiffies - lastj),
731 numts, numstuff, numsec, numinvalid,
732 numts + numstuff + numsec + numinvalid);
733 numts = numstuff = numsec = numinvalid = 0;
734 lastj = jiffies;
735 numpkt = 0;
737 d = &urb->iso_frame_desc[i];
738 data = urb->transfer_buffer + d->offset;
739 len = d->actual_length;
740 d->actual_length = 0;
741 d->status = 0;
742 ttusb_process_frame(ttusb, data, len);
745 usb_submit_urb(urb, GFP_ATOMIC);
748 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
750 int i;
752 for (i = 0; i < ISO_BUF_COUNT; i++)
753 usb_free_urb(ttusb->iso_urb[i]);
754 kfree(ttusb->iso_buffer);
757 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
759 int i;
761 ttusb->iso_buffer = kcalloc(FRAMES_PER_ISO_BUF * ISO_BUF_COUNT,
762 ISO_FRAME_SIZE, GFP_KERNEL);
763 if (!ttusb->iso_buffer)
764 return -ENOMEM;
766 for (i = 0; i < ISO_BUF_COUNT; i++) {
767 struct urb *urb;
769 if (!
770 (urb =
771 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
772 ttusb_free_iso_urbs(ttusb);
773 return -ENOMEM;
776 ttusb->iso_urb[i] = urb;
779 return 0;
782 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
784 int i;
786 for (i = 0; i < ISO_BUF_COUNT; i++)
787 usb_kill_urb(ttusb->iso_urb[i]);
789 ttusb->iso_streaming = 0;
792 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
794 int i, j, err, buffer_offset = 0;
796 if (ttusb->iso_streaming) {
797 pr_err("iso xfer already running!\n");
798 return 0;
801 ttusb->cc = -1;
802 ttusb->insync = 0;
803 ttusb->mux_state = 0;
805 for (i = 0; i < ISO_BUF_COUNT; i++) {
806 int frame_offset = 0;
807 struct urb *urb = ttusb->iso_urb[i];
809 urb->dev = ttusb->dev;
810 urb->context = ttusb;
811 urb->complete = ttusb_iso_irq;
812 urb->pipe = ttusb->isoc_in_pipe;
813 urb->transfer_flags = URB_ISO_ASAP;
814 urb->interval = 1;
815 urb->number_of_packets = FRAMES_PER_ISO_BUF;
816 urb->transfer_buffer_length =
817 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
818 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
819 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
821 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
822 urb->iso_frame_desc[j].offset = frame_offset;
823 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
824 frame_offset += ISO_FRAME_SIZE;
828 for (i = 0; i < ISO_BUF_COUNT; i++) {
829 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
830 ttusb_stop_iso_xfer(ttusb);
831 pr_err("failed urb submission (%i: err = %i)!\n",
832 i, err);
833 return err;
837 ttusb->iso_streaming = 1;
839 return 0;
842 #ifdef TTUSB_HWSECTIONS
843 static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
844 int len)
846 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
849 static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
850 int len)
852 // struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
853 #error TODO: handle ugly stuff
854 // dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
856 #endif
858 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
860 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
861 int feed_type = 1;
863 dprintk("ttusb_start_feed\n");
865 switch (dvbdmxfeed->type) {
866 case DMX_TYPE_TS:
867 break;
868 case DMX_TYPE_SEC:
869 break;
870 default:
871 return -EINVAL;
874 if (dvbdmxfeed->type == DMX_TYPE_TS) {
875 switch (dvbdmxfeed->pes_type) {
876 case DMX_PES_VIDEO:
877 case DMX_PES_AUDIO:
878 case DMX_PES_TELETEXT:
879 case DMX_PES_PCR:
880 case DMX_PES_OTHER:
881 break;
882 default:
883 return -EINVAL;
887 #ifdef TTUSB_HWSECTIONS
888 #error TODO: allocate filters
889 if (dvbdmxfeed->type == DMX_TYPE_TS) {
890 feed_type = 1;
891 } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
892 feed_type = 2;
894 #endif
896 ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
898 if (0 == ttusb->running_feed_count++)
899 ttusb_start_iso_xfer(ttusb);
901 return 0;
904 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
906 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
908 ttusb_del_channel(ttusb, dvbdmxfeed->index);
910 if (--ttusb->running_feed_count == 0)
911 ttusb_stop_iso_xfer(ttusb);
913 return 0;
916 static int ttusb_setup_interfaces(struct ttusb *ttusb)
918 usb_set_interface(ttusb->dev, 1, 1);
920 ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
921 ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
922 ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
924 return 0;
927 #if 0
928 static u8 stc_firmware[8192];
930 static int stc_open(struct inode *inode, struct file *file)
932 struct ttusb *ttusb = file->private_data;
933 int addr;
935 for (addr = 0; addr < 8192; addr += 16) {
936 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
937 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
938 16);
941 return 0;
944 static ssize_t stc_read(struct file *file, char *buf, size_t count,
945 loff_t *offset)
947 return simple_read_from_buffer(buf, count, offset, stc_firmware, 8192);
950 static int stc_release(struct inode *inode, struct file *file)
952 return 0;
955 static const struct file_operations stc_fops = {
956 .owner = THIS_MODULE,
957 .read = stc_read,
958 .open = stc_open,
959 .release = stc_release,
961 #endif
963 static u32 functionality(struct i2c_adapter *adapter)
965 return I2C_FUNC_I2C;
970 static int alps_tdmb7_tuner_set_params(struct dvb_frontend *fe)
972 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
973 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
974 u8 data[4];
975 struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
976 u32 div;
978 div = (p->frequency + 36166667) / 166667;
980 data[0] = (div >> 8) & 0x7f;
981 data[1] = div & 0xff;
982 data[2] = ((div >> 10) & 0x60) | 0x85;
983 data[3] = p->frequency < 592000000 ? 0x40 : 0x80;
985 if (fe->ops.i2c_gate_ctrl)
986 fe->ops.i2c_gate_ctrl(fe, 1);
987 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
988 return 0;
991 static struct cx22700_config alps_tdmb7_config = {
992 .demod_address = 0x43,
999 static int philips_tdm1316l_tuner_init(struct dvb_frontend* fe)
1001 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1002 static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1003 static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1004 struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1006 // setup PLL configuration
1007 if (fe->ops.i2c_gate_ctrl)
1008 fe->ops.i2c_gate_ctrl(fe, 1);
1009 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1010 msleep(1);
1012 // disable the mc44BC374c (do not check for errors)
1013 tuner_msg.addr = 0x65;
1014 tuner_msg.buf = disable_mc44BC374c;
1015 tuner_msg.len = sizeof(disable_mc44BC374c);
1016 if (fe->ops.i2c_gate_ctrl)
1017 fe->ops.i2c_gate_ctrl(fe, 1);
1018 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1019 i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1022 return 0;
1025 static int philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe)
1027 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1028 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1029 u8 tuner_buf[4];
1030 struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1031 int tuner_frequency = 0;
1032 u8 band, cp, filter;
1034 // determine charge pump
1035 tuner_frequency = p->frequency + 36130000;
1036 if (tuner_frequency < 87000000) return -EINVAL;
1037 else if (tuner_frequency < 130000000) cp = 3;
1038 else if (tuner_frequency < 160000000) cp = 5;
1039 else if (tuner_frequency < 200000000) cp = 6;
1040 else if (tuner_frequency < 290000000) cp = 3;
1041 else if (tuner_frequency < 420000000) cp = 5;
1042 else if (tuner_frequency < 480000000) cp = 6;
1043 else if (tuner_frequency < 620000000) cp = 3;
1044 else if (tuner_frequency < 830000000) cp = 5;
1045 else if (tuner_frequency < 895000000) cp = 7;
1046 else return -EINVAL;
1048 // determine band
1049 if (p->frequency < 49000000)
1050 return -EINVAL;
1051 else if (p->frequency < 159000000)
1052 band = 1;
1053 else if (p->frequency < 444000000)
1054 band = 2;
1055 else if (p->frequency < 861000000)
1056 band = 4;
1057 else return -EINVAL;
1059 // setup PLL filter
1060 switch (p->bandwidth_hz) {
1061 case 6000000:
1062 tda1004x_writereg(fe, 0x0C, 0);
1063 filter = 0;
1064 break;
1066 case 7000000:
1067 tda1004x_writereg(fe, 0x0C, 0);
1068 filter = 0;
1069 break;
1071 case 8000000:
1072 tda1004x_writereg(fe, 0x0C, 0xFF);
1073 filter = 1;
1074 break;
1076 default:
1077 return -EINVAL;
1080 // calculate divisor
1081 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1082 tuner_frequency = (((p->frequency / 1000) * 6) + 217280) / 1000;
1084 // setup tuner buffer
1085 tuner_buf[0] = tuner_frequency >> 8;
1086 tuner_buf[1] = tuner_frequency & 0xff;
1087 tuner_buf[2] = 0xca;
1088 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1090 if (fe->ops.i2c_gate_ctrl)
1091 fe->ops.i2c_gate_ctrl(fe, 1);
1092 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1093 return -EIO;
1095 msleep(1);
1096 return 0;
1099 static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1101 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1103 return request_firmware(fw, name, &ttusb->dev->dev);
1106 static struct tda1004x_config philips_tdm1316l_config = {
1108 .demod_address = 0x8,
1109 .invert = 1,
1110 .invert_oclk = 0,
1111 .request_firmware = philips_tdm1316l_request_firmware,
1114 static u8 alps_bsbe1_inittab[] = {
1115 0x01, 0x15,
1116 0x02, 0x30,
1117 0x03, 0x00,
1118 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1119 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1120 0x06, 0x40, /* DAC not used, set to high impendance mode */
1121 0x07, 0x00, /* DAC LSB */
1122 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1123 0x09, 0x00, /* FIFO */
1124 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1125 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1126 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1127 0x10, 0x3f, // AGC2 0x3d
1128 0x11, 0x84,
1129 0x12, 0xb9,
1130 0x15, 0xc9, // lock detector threshold
1131 0x16, 0x00,
1132 0x17, 0x00,
1133 0x18, 0x00,
1134 0x19, 0x00,
1135 0x1a, 0x00,
1136 0x1f, 0x50,
1137 0x20, 0x00,
1138 0x21, 0x00,
1139 0x22, 0x00,
1140 0x23, 0x00,
1141 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1142 0x29, 0x1e, // 1/2 threshold
1143 0x2a, 0x14, // 2/3 threshold
1144 0x2b, 0x0f, // 3/4 threshold
1145 0x2c, 0x09, // 5/6 threshold
1146 0x2d, 0x05, // 7/8 threshold
1147 0x2e, 0x01,
1148 0x31, 0x1f, // test all FECs
1149 0x32, 0x19, // viterbi and synchro search
1150 0x33, 0xfc, // rs control
1151 0x34, 0x93, // error control
1152 0x0f, 0x92,
1153 0xff, 0xff
1156 static u8 alps_bsru6_inittab[] = {
1157 0x01, 0x15,
1158 0x02, 0x30,
1159 0x03, 0x00,
1160 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1161 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1162 0x06, 0x40, /* DAC not used, set to high impendance mode */
1163 0x07, 0x00, /* DAC LSB */
1164 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1165 0x09, 0x00, /* FIFO */
1166 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1167 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1168 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1169 0x10, 0x3f, // AGC2 0x3d
1170 0x11, 0x84,
1171 0x12, 0xb9,
1172 0x15, 0xc9, // lock detector threshold
1173 0x16, 0x00,
1174 0x17, 0x00,
1175 0x18, 0x00,
1176 0x19, 0x00,
1177 0x1a, 0x00,
1178 0x1f, 0x50,
1179 0x20, 0x00,
1180 0x21, 0x00,
1181 0x22, 0x00,
1182 0x23, 0x00,
1183 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1184 0x29, 0x1e, // 1/2 threshold
1185 0x2a, 0x14, // 2/3 threshold
1186 0x2b, 0x0f, // 3/4 threshold
1187 0x2c, 0x09, // 5/6 threshold
1188 0x2d, 0x05, // 7/8 threshold
1189 0x2e, 0x01,
1190 0x31, 0x1f, // test all FECs
1191 0x32, 0x19, // viterbi and synchro search
1192 0x33, 0xfc, // rs control
1193 0x34, 0x93, // error control
1194 0x0f, 0x52,
1195 0xff, 0xff
1198 static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1200 u8 aclk = 0;
1201 u8 bclk = 0;
1203 if (srate < 1500000) {
1204 aclk = 0xb7;
1205 bclk = 0x47;
1206 } else if (srate < 3000000) {
1207 aclk = 0xb7;
1208 bclk = 0x4b;
1209 } else if (srate < 7000000) {
1210 aclk = 0xb7;
1211 bclk = 0x4f;
1212 } else if (srate < 14000000) {
1213 aclk = 0xb7;
1214 bclk = 0x53;
1215 } else if (srate < 30000000) {
1216 aclk = 0xb6;
1217 bclk = 0x53;
1218 } else if (srate < 45000000) {
1219 aclk = 0xb4;
1220 bclk = 0x51;
1223 stv0299_writereg(fe, 0x13, aclk);
1224 stv0299_writereg(fe, 0x14, bclk);
1225 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1226 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1227 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1229 return 0;
1232 static int philips_tsa5059_tuner_set_params(struct dvb_frontend *fe)
1234 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1235 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1236 u8 buf[4];
1237 u32 div;
1238 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1240 if ((p->frequency < 950000) || (p->frequency > 2150000))
1241 return -EINVAL;
1243 div = (p->frequency + (125 - 1)) / 125; /* round correctly */
1244 buf[0] = (div >> 8) & 0x7f;
1245 buf[1] = div & 0xff;
1246 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1247 buf[3] = 0xC4;
1249 if (p->frequency > 1530000)
1250 buf[3] = 0xC0;
1252 /* BSBE1 wants XCE bit set */
1253 if (ttusb->revision == TTUSB_REV_2_2)
1254 buf[3] |= 0x20;
1256 if (fe->ops.i2c_gate_ctrl)
1257 fe->ops.i2c_gate_ctrl(fe, 1);
1258 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1259 return -EIO;
1261 return 0;
1264 static struct stv0299_config alps_stv0299_config = {
1265 .demod_address = 0x68,
1266 .inittab = alps_bsru6_inittab,
1267 .mclk = 88000000UL,
1268 .invert = 1,
1269 .skip_reinit = 0,
1270 .lock_output = STV0299_LOCKOUTPUT_1,
1271 .volt13_op0_op1 = STV0299_VOLT13_OP1,
1272 .min_delay_ms = 100,
1273 .set_symbol_rate = alps_stv0299_set_symbol_rate,
1276 static int ttusb_novas_grundig_29504_491_tuner_set_params(struct dvb_frontend *fe)
1278 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1279 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1280 u8 buf[4];
1281 u32 div;
1282 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1284 div = p->frequency / 125;
1286 buf[0] = (div >> 8) & 0x7f;
1287 buf[1] = div & 0xff;
1288 buf[2] = 0x8e;
1289 buf[3] = 0x00;
1291 if (fe->ops.i2c_gate_ctrl)
1292 fe->ops.i2c_gate_ctrl(fe, 1);
1293 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1294 return -EIO;
1296 return 0;
1299 static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1301 .demod_address = 0x68,
1304 static int alps_tdbe2_tuner_set_params(struct dvb_frontend *fe)
1306 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1307 struct ttusb* ttusb = fe->dvb->priv;
1308 u32 div;
1309 u8 data[4];
1310 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1312 div = (p->frequency + 35937500 + 31250) / 62500;
1314 data[0] = (div >> 8) & 0x7f;
1315 data[1] = div & 0xff;
1316 data[2] = 0x85 | ((div >> 10) & 0x60);
1317 data[3] = (p->frequency < 174000000 ? 0x88 : p->frequency < 470000000 ? 0x84 : 0x81);
1319 if (fe->ops.i2c_gate_ctrl)
1320 fe->ops.i2c_gate_ctrl(fe, 1);
1321 if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1322 return -EIO;
1324 return 0;
1328 static struct ves1820_config alps_tdbe2_config = {
1329 .demod_address = 0x09,
1330 .xin = 57840000UL,
1331 .invert = 1,
1332 .selagc = VES1820_SELAGC_SIGNAMPERR,
1335 static u8 read_pwm(struct ttusb* ttusb)
1337 u8 b = 0xff;
1338 u8 pwm;
1339 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1340 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1342 if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1343 pwm = 0x48;
1345 return pwm;
1349 static int dvbc_philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe)
1351 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1352 struct ttusb *ttusb = (struct ttusb *) fe->dvb->priv;
1353 u8 tuner_buf[5];
1354 struct i2c_msg tuner_msg = {.addr = 0x60,
1355 .flags = 0,
1356 .buf = tuner_buf,
1357 .len = sizeof(tuner_buf) };
1358 int tuner_frequency = 0;
1359 u8 band, cp, filter;
1361 // determine charge pump
1362 tuner_frequency = p->frequency;
1363 if (tuner_frequency < 87000000) {return -EINVAL;}
1364 else if (tuner_frequency < 130000000) {cp = 3; band = 1;}
1365 else if (tuner_frequency < 160000000) {cp = 5; band = 1;}
1366 else if (tuner_frequency < 200000000) {cp = 6; band = 1;}
1367 else if (tuner_frequency < 290000000) {cp = 3; band = 2;}
1368 else if (tuner_frequency < 420000000) {cp = 5; band = 2;}
1369 else if (tuner_frequency < 480000000) {cp = 6; band = 2;}
1370 else if (tuner_frequency < 620000000) {cp = 3; band = 4;}
1371 else if (tuner_frequency < 830000000) {cp = 5; band = 4;}
1372 else if (tuner_frequency < 895000000) {cp = 7; band = 4;}
1373 else {return -EINVAL;}
1375 // assume PLL filter should always be 8MHz for the moment.
1376 filter = 1;
1378 // calculate divisor
1379 // (Finput + Fif)/Fref; Fif = 36125000 Hz, Fref = 62500 Hz
1380 tuner_frequency = ((p->frequency + 36125000) / 62500);
1382 // setup tuner buffer
1383 tuner_buf[0] = tuner_frequency >> 8;
1384 tuner_buf[1] = tuner_frequency & 0xff;
1385 tuner_buf[2] = 0xc8;
1386 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1387 tuner_buf[4] = 0x80;
1389 if (fe->ops.i2c_gate_ctrl)
1390 fe->ops.i2c_gate_ctrl(fe, 1);
1391 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1392 pr_err("dvbc_philips_tdm1316l_pll_set Error 1\n");
1393 return -EIO;
1396 msleep(50);
1398 if (fe->ops.i2c_gate_ctrl)
1399 fe->ops.i2c_gate_ctrl(fe, 1);
1400 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1401 pr_err("dvbc_philips_tdm1316l_pll_set Error 2\n");
1402 return -EIO;
1405 msleep(1);
1407 return 0;
1410 static u8 dvbc_philips_tdm1316l_inittab[] = {
1411 0x80, 0x21,
1412 0x80, 0x20,
1413 0x81, 0x01,
1414 0x81, 0x00,
1415 0x00, 0x09,
1416 0x01, 0x69,
1417 0x03, 0x00,
1418 0x04, 0x00,
1419 0x07, 0x00,
1420 0x08, 0x00,
1421 0x20, 0x00,
1422 0x21, 0x40,
1423 0x22, 0x00,
1424 0x23, 0x00,
1425 0x24, 0x40,
1426 0x25, 0x88,
1427 0x30, 0xff,
1428 0x31, 0x00,
1429 0x32, 0xff,
1430 0x33, 0x00,
1431 0x34, 0x50,
1432 0x35, 0x7f,
1433 0x36, 0x00,
1434 0x37, 0x20,
1435 0x38, 0x00,
1436 0x40, 0x1c,
1437 0x41, 0xff,
1438 0x42, 0x29,
1439 0x43, 0x20,
1440 0x44, 0xff,
1441 0x45, 0x00,
1442 0x46, 0x00,
1443 0x49, 0x04,
1444 0x4a, 0xff,
1445 0x4b, 0x7f,
1446 0x52, 0x30,
1447 0x55, 0xae,
1448 0x56, 0x47,
1449 0x57, 0xe1,
1450 0x58, 0x3a,
1451 0x5a, 0x1e,
1452 0x5b, 0x34,
1453 0x60, 0x00,
1454 0x63, 0x00,
1455 0x64, 0x00,
1456 0x65, 0x00,
1457 0x66, 0x00,
1458 0x67, 0x00,
1459 0x68, 0x00,
1460 0x69, 0x00,
1461 0x6a, 0x02,
1462 0x6b, 0x00,
1463 0x70, 0xff,
1464 0x71, 0x00,
1465 0x72, 0x00,
1466 0x73, 0x00,
1467 0x74, 0x0c,
1468 0x80, 0x00,
1469 0x81, 0x00,
1470 0x82, 0x00,
1471 0x83, 0x00,
1472 0x84, 0x04,
1473 0x85, 0x80,
1474 0x86, 0x24,
1475 0x87, 0x78,
1476 0x88, 0x00,
1477 0x89, 0x00,
1478 0x90, 0x01,
1479 0x91, 0x01,
1480 0xa0, 0x00,
1481 0xa1, 0x00,
1482 0xa2, 0x00,
1483 0xb0, 0x91,
1484 0xb1, 0x0b,
1485 0xc0, 0x4b,
1486 0xc1, 0x00,
1487 0xc2, 0x00,
1488 0xd0, 0x00,
1489 0xd1, 0x00,
1490 0xd2, 0x00,
1491 0xd3, 0x00,
1492 0xd4, 0x00,
1493 0xd5, 0x00,
1494 0xde, 0x00,
1495 0xdf, 0x00,
1496 0x61, 0x38,
1497 0x62, 0x0a,
1498 0x53, 0x13,
1499 0x59, 0x08,
1500 0x55, 0x00,
1501 0x56, 0x40,
1502 0x57, 0x08,
1503 0x58, 0x3d,
1504 0x88, 0x10,
1505 0xa0, 0x00,
1506 0xa0, 0x00,
1507 0xa0, 0x00,
1508 0xa0, 0x04,
1509 0xff, 0xff,
1512 static struct stv0297_config dvbc_philips_tdm1316l_config = {
1513 .demod_address = 0x1c,
1514 .inittab = dvbc_philips_tdm1316l_inittab,
1515 .invert = 0,
1518 static void frontend_init(struct ttusb* ttusb)
1520 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1521 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1522 // try the stv0299 based first
1523 ttusb->fe = dvb_attach(stv0299_attach, &alps_stv0299_config, &ttusb->i2c_adap);
1524 if (ttusb->fe != NULL) {
1525 ttusb->fe->ops.tuner_ops.set_params = philips_tsa5059_tuner_set_params;
1527 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1528 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1529 dvb_attach(lnbp21_attach, ttusb->fe, &ttusb->i2c_adap, 0, 0);
1530 } else { // ALPS BSRU6
1531 ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1533 break;
1536 // Grundig 29504-491
1537 ttusb->fe = dvb_attach(tda8083_attach, &ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1538 if (ttusb->fe != NULL) {
1539 ttusb->fe->ops.tuner_ops.set_params = ttusb_novas_grundig_29504_491_tuner_set_params;
1540 ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1541 break;
1543 break;
1545 case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
1546 ttusb->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
1547 if (ttusb->fe != NULL) {
1548 ttusb->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
1549 break;
1552 ttusb->fe = dvb_attach(stv0297_attach, &dvbc_philips_tdm1316l_config, &ttusb->i2c_adap);
1553 if (ttusb->fe != NULL) {
1554 ttusb->fe->ops.tuner_ops.set_params = dvbc_philips_tdm1316l_tuner_set_params;
1555 break;
1557 break;
1559 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1560 // try the ALPS TDMB7 first
1561 ttusb->fe = dvb_attach(cx22700_attach, &alps_tdmb7_config, &ttusb->i2c_adap);
1562 if (ttusb->fe != NULL) {
1563 ttusb->fe->ops.tuner_ops.set_params = alps_tdmb7_tuner_set_params;
1564 break;
1567 // Philips td1316
1568 ttusb->fe = dvb_attach(tda10046_attach, &philips_tdm1316l_config, &ttusb->i2c_adap);
1569 if (ttusb->fe != NULL) {
1570 ttusb->fe->ops.tuner_ops.init = philips_tdm1316l_tuner_init;
1571 ttusb->fe->ops.tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
1572 break;
1574 break;
1577 if (ttusb->fe == NULL) {
1578 pr_err("no frontend driver found for device [%04x:%04x]\n",
1579 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1580 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1581 } else {
1582 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1583 pr_err("Frontend registration failed!\n");
1584 dvb_frontend_detach(ttusb->fe);
1585 ttusb->fe = NULL;
1592 static const struct i2c_algorithm ttusb_dec_algo = {
1593 .master_xfer = master_xfer,
1594 .functionality = functionality,
1597 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1599 struct usb_device *udev;
1600 struct ttusb *ttusb;
1601 int result;
1603 dprintk("TTUSB DVB connected\n");
1605 udev = interface_to_usbdev(intf);
1607 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1609 if (!(ttusb = kzalloc(sizeof(struct ttusb), GFP_KERNEL)))
1610 return -ENOMEM;
1612 ttusb->dev = udev;
1613 ttusb->c = 0;
1614 ttusb->mux_state = 0;
1615 mutex_init(&ttusb->semi2c);
1617 mutex_lock(&ttusb->semi2c);
1619 mutex_init(&ttusb->semusb);
1621 ttusb_setup_interfaces(ttusb);
1623 result = ttusb_alloc_iso_urbs(ttusb);
1624 if (result < 0) {
1625 dprintk("ttusb_alloc_iso_urbs - failed\n");
1626 mutex_unlock(&ttusb->semi2c);
1627 kfree(ttusb);
1628 return result;
1631 if (ttusb_init_controller(ttusb))
1632 pr_err("ttusb_init_controller: error\n");
1634 mutex_unlock(&ttusb->semi2c);
1636 result = dvb_register_adapter(&ttusb->adapter,
1637 "Technotrend/Hauppauge Nova-USB",
1638 THIS_MODULE, &udev->dev, adapter_nr);
1639 if (result < 0) {
1640 ttusb_free_iso_urbs(ttusb);
1641 kfree(ttusb);
1642 return result;
1644 ttusb->adapter.priv = ttusb;
1646 /* i2c */
1647 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1648 strscpy(ttusb->i2c_adap.name, "TTUSB DEC", sizeof(ttusb->i2c_adap.name));
1650 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1652 ttusb->i2c_adap.algo = &ttusb_dec_algo;
1653 ttusb->i2c_adap.algo_data = NULL;
1654 ttusb->i2c_adap.dev.parent = &udev->dev;
1656 result = i2c_add_adapter(&ttusb->i2c_adap);
1657 if (result)
1658 goto err_unregister_adapter;
1660 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1662 ttusb->dvb_demux.dmx.capabilities =
1663 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1664 ttusb->dvb_demux.priv = NULL;
1665 #ifdef TTUSB_HWSECTIONS
1666 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1667 #else
1668 ttusb->dvb_demux.filternum = 32;
1669 #endif
1670 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1671 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1672 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1673 ttusb->dvb_demux.write_to_decoder = NULL;
1675 result = dvb_dmx_init(&ttusb->dvb_demux);
1676 if (result < 0) {
1677 pr_err("dvb_dmx_init failed (errno = %d)\n", result);
1678 result = -ENODEV;
1679 goto err_i2c_del_adapter;
1681 //FIXME dmxdev (nur WAS?)
1682 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1683 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1684 ttusb->dmxdev.capabilities = 0;
1686 result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter);
1687 if (result < 0) {
1688 pr_err("dvb_dmxdev_init failed (errno = %d)\n",
1689 result);
1690 result = -ENODEV;
1691 goto err_release_dmx;
1694 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1695 pr_err("dvb_net_init failed!\n");
1696 result = -ENODEV;
1697 goto err_release_dmxdev;
1700 usb_set_intfdata(intf, (void *) ttusb);
1702 frontend_init(ttusb);
1704 return 0;
1706 err_release_dmxdev:
1707 dvb_dmxdev_release(&ttusb->dmxdev);
1708 err_release_dmx:
1709 dvb_dmx_release(&ttusb->dvb_demux);
1710 err_i2c_del_adapter:
1711 i2c_del_adapter(&ttusb->i2c_adap);
1712 err_unregister_adapter:
1713 dvb_unregister_adapter (&ttusb->adapter);
1714 ttusb_free_iso_urbs(ttusb);
1715 kfree(ttusb);
1716 return result;
1719 static void ttusb_disconnect(struct usb_interface *intf)
1721 struct ttusb *ttusb = usb_get_intfdata(intf);
1723 usb_set_intfdata(intf, NULL);
1725 ttusb->disconnecting = 1;
1727 ttusb_stop_iso_xfer(ttusb);
1729 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1730 dvb_net_release(&ttusb->dvbnet);
1731 dvb_dmxdev_release(&ttusb->dmxdev);
1732 dvb_dmx_release(&ttusb->dvb_demux);
1733 if (ttusb->fe != NULL) {
1734 dvb_unregister_frontend(ttusb->fe);
1735 dvb_frontend_detach(ttusb->fe);
1737 i2c_del_adapter(&ttusb->i2c_adap);
1738 dvb_unregister_adapter(&ttusb->adapter);
1740 ttusb_free_iso_urbs(ttusb);
1742 kfree(ttusb);
1744 dprintk("TTUSB DVB disconnected\n");
1747 static const struct usb_device_id ttusb_table[] = {
1748 {USB_DEVICE(0xb48, 0x1003)},
1749 {USB_DEVICE(0xb48, 0x1004)},
1750 {USB_DEVICE(0xb48, 0x1005)},
1754 MODULE_DEVICE_TABLE(usb, ttusb_table);
1756 static struct usb_driver ttusb_driver = {
1757 .name = "ttusb",
1758 .probe = ttusb_probe,
1759 .disconnect = ttusb_disconnect,
1760 .id_table = ttusb_table,
1763 module_usb_driver(ttusb_driver);
1765 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1766 MODULE_DESCRIPTION("TTUSB DVB Driver");
1767 MODULE_LICENSE("GPL");
1768 MODULE_FIRMWARE("ttusb-budget/dspbootcode.bin");