[PATCH] I2C: Kill i2c_algorithm.id (4/7)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / dvb / ttusb-budget / dvb-ttusb-budget.c
blob11afec52f31f890fc60ee06c9ae9838f51a9cba4
1 /*
2 * TTUSB DVB driver
4 * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
5 * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/wait.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/usb.h>
18 #include <linux/delay.h>
19 #include <linux/time.h>
20 #include <linux/errno.h>
21 #include <asm/semaphore.h>
23 #include "dvb_frontend.h"
24 #include "dmxdev.h"
25 #include "dvb_demux.h"
26 #include "dvb_net.h"
27 #include "ves1820.h"
28 #include "cx22700.h"
29 #include "tda1004x.h"
30 #include "stv0299.h"
31 #include "tda8083.h"
33 #include <linux/dvb/frontend.h>
34 #include <linux/dvb/dmx.h>
35 #include <linux/pci.h>
39 TTUSB_HWSECTIONS:
40 the DSP supports filtering in hardware, however, since the "muxstream"
41 is a bit braindead (no matching channel masks or no matching filter mask),
42 we won't support this - yet. it doesn't event support negative filters,
43 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
44 parse TS data. USB bandwith will be a problem when having large
45 datastreams, especially for dvb-net, but hey, that's not my problem.
47 TTUSB_DISEQC, TTUSB_TONE:
48 let the STC do the diseqc/tone stuff. this isn't supported at least with
49 my TTUSB, so let it undef'd unless you want to implement another
50 frontend. never tested.
52 DEBUG:
53 define it to > 3 for really hardcore debugging. you probably don't want
54 this unless the device doesn't load at all. > 2 for bandwidth statistics.
57 static int debug;
59 module_param(debug, int, 0644);
60 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
62 #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
64 #define ISO_BUF_COUNT 4
65 #define FRAMES_PER_ISO_BUF 4
66 #define ISO_FRAME_SIZE 912
67 #define TTUSB_MAXCHANNEL 32
68 #ifdef TTUSB_HWSECTIONS
69 #define TTUSB_MAXFILTER 16 /* ??? */
70 #endif
72 #define TTUSB_REV_2_2 0x22
73 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
75 /**
76 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
77 * the dvb_demux field must be the first in struct!!
79 struct ttusb {
80 struct dvb_demux dvb_demux;
81 struct dmxdev dmxdev;
82 struct dvb_net dvbnet;
84 /* and one for USB access. */
85 struct semaphore semi2c;
86 struct semaphore semusb;
88 struct dvb_adapter adapter;
89 struct usb_device *dev;
91 struct i2c_adapter i2c_adap;
93 int disconnecting;
94 int iso_streaming;
96 unsigned int bulk_out_pipe;
97 unsigned int bulk_in_pipe;
98 unsigned int isoc_in_pipe;
100 void *iso_buffer;
101 dma_addr_t iso_dma_handle;
103 struct urb *iso_urb[ISO_BUF_COUNT];
105 int running_feed_count;
106 int last_channel;
107 int last_filter;
109 u8 c; /* transaction counter, wraps around... */
110 fe_sec_tone_mode_t tone;
111 fe_sec_voltage_t voltage;
113 int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
114 u8 mux_npacks;
115 u8 muxpack[256 + 8];
116 int muxpack_ptr, muxpack_len;
118 int insync;
120 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
121 /* (including stuffing. yes. really.) */
123 u8 last_result[32];
125 int revision;
127 #if 0
128 devfs_handle_t stc_devfs_handle;
129 #endif
131 struct dvb_frontend* fe;
134 /* ugly workaround ... don't know why it's neccessary to read */
135 /* all result codes. */
137 #define DEBUG 0
138 static int ttusb_cmd(struct ttusb *ttusb,
139 const u8 * data, int len, int needresult)
141 int actual_len;
142 int err;
143 #if DEBUG >= 3
144 int i;
146 printk(">");
147 for (i = 0; i < len; ++i)
148 printk(" %02x", data[i]);
149 printk("\n");
150 #endif
152 if (down_interruptible(&ttusb->semusb) < 0)
153 return -EAGAIN;
155 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
156 (u8 *) data, len, &actual_len, 1000);
157 if (err != 0) {
158 dprintk("%s: usb_bulk_msg(send) failed, err == %i!\n",
159 __FUNCTION__, err);
160 up(&ttusb->semusb);
161 return err;
163 if (actual_len != len) {
164 dprintk("%s: only wrote %d of %d bytes\n", __FUNCTION__,
165 actual_len, len);
166 up(&ttusb->semusb);
167 return -1;
170 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
171 ttusb->last_result, 32, &actual_len, 1000);
173 if (err != 0) {
174 printk("%s: failed, receive error %d\n", __FUNCTION__,
175 err);
176 up(&ttusb->semusb);
177 return err;
179 #if DEBUG >= 3
180 actual_len = ttusb->last_result[3] + 4;
181 printk("<");
182 for (i = 0; i < actual_len; ++i)
183 printk(" %02x", ttusb->last_result[i]);
184 printk("\n");
185 #endif
186 if (!needresult)
187 up(&ttusb->semusb);
188 return 0;
191 static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
193 memcpy(data, ttusb->last_result, len);
194 up(&ttusb->semusb);
195 return 0;
198 static int ttusb_i2c_msg(struct ttusb *ttusb,
199 u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
200 u8 rcv_len)
202 u8 b[0x28];
203 u8 id = ++ttusb->c;
204 int i, err;
206 if (snd_len > 0x28 - 7 || rcv_len > 0x20 - 7)
207 return -EINVAL;
209 b[0] = 0xaa;
210 b[1] = id;
211 b[2] = 0x31;
212 b[3] = snd_len + 3;
213 b[4] = addr << 1;
214 b[5] = snd_len;
215 b[6] = rcv_len;
217 for (i = 0; i < snd_len; i++)
218 b[7 + i] = snd_buf[i];
220 err = ttusb_cmd(ttusb, b, snd_len + 7, 1);
222 if (err)
223 return -EREMOTEIO;
225 err = ttusb_result(ttusb, b, 0x20);
227 /* check if the i2c transaction was successful */
228 if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
230 if (rcv_len > 0) {
232 if (err || b[0] != 0x55 || b[1] != id) {
233 dprintk
234 ("%s: usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
235 __FUNCTION__, err, id);
236 return -EREMOTEIO;
239 for (i = 0; i < rcv_len; i++)
240 rcv_buf[i] = b[7 + i];
243 return rcv_len;
246 static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
248 struct ttusb *ttusb = i2c_get_adapdata(adapter);
249 int i = 0;
250 int inc;
252 if (down_interruptible(&ttusb->semi2c) < 0)
253 return -EAGAIN;
255 while (i < num) {
256 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
257 int err;
259 if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
260 addr = msg[i].addr;
261 snd_buf = msg[i].buf;
262 snd_len = msg[i].len;
263 rcv_buf = msg[i + 1].buf;
264 rcv_len = msg[i + 1].len;
265 inc = 2;
266 } else {
267 addr = msg[i].addr;
268 snd_buf = msg[i].buf;
269 snd_len = msg[i].len;
270 rcv_buf = NULL;
271 rcv_len = 0;
272 inc = 1;
275 err = ttusb_i2c_msg(ttusb, addr,
276 snd_buf, snd_len, rcv_buf, rcv_len);
278 if (err < rcv_len) {
279 dprintk("%s: i == %i\n", __FUNCTION__, i);
280 break;
283 i += inc;
286 up(&ttusb->semi2c);
287 return i;
290 #include "dvb-ttusb-dspbootcode.h"
292 static int ttusb_boot_dsp(struct ttusb *ttusb)
294 int i, err;
295 u8 b[40];
297 /* BootBlock */
298 b[0] = 0xaa;
299 b[2] = 0x13;
300 b[3] = 28;
302 /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
303 /* 32 is max packet size, no messages should be splitted. */
304 for (i = 0; i < sizeof(dsp_bootcode); i += 28) {
305 memcpy(&b[4], &dsp_bootcode[i], 28);
307 b[1] = ++ttusb->c;
309 err = ttusb_cmd(ttusb, b, 32, 0);
310 if (err)
311 goto done;
314 /* last block ... */
315 b[1] = ++ttusb->c;
316 b[2] = 0x13;
317 b[3] = 0;
319 err = ttusb_cmd(ttusb, b, 4, 0);
320 if (err)
321 goto done;
323 /* BootEnd */
324 b[1] = ++ttusb->c;
325 b[2] = 0x14;
326 b[3] = 0;
328 err = ttusb_cmd(ttusb, b, 4, 0);
330 done:
331 if (err) {
332 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
333 __FUNCTION__, err);
336 return err;
339 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
340 int pid)
342 int err;
343 /* SetChannel */
344 u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
345 (pid >> 8) & 0xff, pid & 0xff
348 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
349 return err;
352 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
354 int err;
355 /* DelChannel */
356 u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
358 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
359 return err;
362 #ifdef TTUSB_HWSECTIONS
363 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
364 int associated_chan, u8 filter[8], u8 mask[8])
366 int err;
367 /* SetFilter */
368 u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
369 filter[0], filter[1], filter[2], filter[3],
370 filter[4], filter[5], filter[6], filter[7],
371 filter[8], filter[9], filter[10], filter[11],
372 mask[0], mask[1], mask[2], mask[3],
373 mask[4], mask[5], mask[6], mask[7],
374 mask[8], mask[9], mask[10], mask[11]
377 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
378 return err;
381 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
383 int err;
384 /* DelFilter */
385 u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
387 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
388 return err;
390 #endif
392 static int ttusb_init_controller(struct ttusb *ttusb)
394 u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
395 u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
396 u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
397 /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
398 u8 b3[] =
399 { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
400 u8 b4[] =
401 { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
403 u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
404 u8 get_dsp_version[0x20] =
405 { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
406 int err;
408 /* reset board */
409 if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
410 return err;
412 /* reset board (again?) */
413 if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
414 return err;
416 ttusb_boot_dsp(ttusb);
418 /* set i2c bit rate */
419 if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
420 return err;
422 if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
423 return err;
425 err = ttusb_result(ttusb, b4, sizeof(b4));
427 if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
428 return err;
430 if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
431 return err;
433 dprintk("%s: stc-version: %c%c%c%c%c\n", __FUNCTION__,
434 get_version[4], get_version[5], get_version[6],
435 get_version[7], get_version[8]);
437 if (memcmp(get_version + 4, "V 0.0", 5) &&
438 memcmp(get_version + 4, "V 1.1", 5) &&
439 memcmp(get_version + 4, "V 2.1", 5) &&
440 memcmp(get_version + 4, "V 2.2", 5)) {
441 printk
442 ("%s: unknown STC version %c%c%c%c%c, please report!\n",
443 __FUNCTION__, get_version[4], get_version[5],
444 get_version[6], get_version[7], get_version[8]);
447 ttusb->revision = ((get_version[6] - '0') << 4) |
448 (get_version[8] - '0');
450 err =
451 ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
452 if (err)
453 return err;
455 err =
456 ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
457 if (err)
458 return err;
459 printk("%s: dsp-version: %c%c%c\n", __FUNCTION__,
460 get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
461 return 0;
464 #ifdef TTUSB_DISEQC
465 static int ttusb_send_diseqc(struct dvb_frontend* fe,
466 const struct dvb_diseqc_master_cmd *cmd)
468 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
469 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
471 int err;
473 b[3] = 4 + 2 + cmd->msg_len;
474 b[4] = 0xFF; /* send diseqc master, not burst */
475 b[5] = cmd->msg_len;
477 memcpy(b + 5, cmd->msg, cmd->msg_len);
479 /* Diseqc */
480 if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
481 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
482 __FUNCTION__, err);
485 return err;
487 #endif
489 static int lnbp21_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
491 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
492 int ret;
493 u8 data[1];
494 struct i2c_msg msg = { .addr = 0x08, .flags = 0, .buf = data, .len = sizeof(data) };
496 switch(voltage) {
497 case SEC_VOLTAGE_OFF:
498 data[0] = 0x00;
499 break;
500 case SEC_VOLTAGE_13:
501 data[0] = 0x44;
502 break;
503 case SEC_VOLTAGE_18:
504 data[0] = 0x4c;
505 break;
506 default:
507 return -EINVAL;
510 ret = i2c_transfer(&ttusb->i2c_adap, &msg, 1);
511 return (ret != 1) ? -EIO : 0;
514 static int ttusb_update_lnb(struct ttusb *ttusb)
516 u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
517 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
518 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
520 int err;
522 /* SetLNB */
523 if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
524 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
525 __FUNCTION__, err);
528 return err;
531 static int ttusb_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
533 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
535 ttusb->voltage = voltage;
536 return ttusb_update_lnb(ttusb);
539 #ifdef TTUSB_TONE
540 static int ttusb_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
542 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
544 ttusb->tone = tone;
545 return ttusb_update_lnb(ttusb);
547 #endif
550 #if 0
551 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
553 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
554 int err, actual_len;
556 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
557 if (err) {
558 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
559 __FUNCTION__, err);
562 #endif
564 /*****************************************************************************/
566 #ifdef TTUSB_HWSECTIONS
567 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
568 const u8 * data, int len);
569 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
570 const u8 * data, int len);
571 #endif
573 static int numpkt = 0, lastj, numts, numstuff, numsec, numinvalid;
575 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
576 int len)
578 u16 csum = 0, cc;
579 int i;
580 for (i = 0; i < len; i += 2)
581 csum ^= le16_to_cpup((u16 *) (muxpack + i));
582 if (csum) {
583 printk("%s: muxpack with incorrect checksum, ignoring\n",
584 __FUNCTION__);
585 numinvalid++;
586 return;
589 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
590 cc &= 0x7FFF;
591 if ((cc != ttusb->cc) && (ttusb->cc != -1))
592 printk("%s: cc discontinuity (%d frames missing)\n",
593 __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
594 ttusb->cc = (cc + 1) & 0x7FFF;
595 if (muxpack[0] & 0x80) {
596 #ifdef TTUSB_HWSECTIONS
597 /* section data */
598 int pusi = muxpack[0] & 0x40;
599 int channel = muxpack[0] & 0x1F;
600 int payload = muxpack[1];
601 const u8 *data = muxpack + 2;
602 /* check offset flag */
603 if (muxpack[0] & 0x20)
604 data++;
606 ttusb_handle_sec_data(ttusb->channel + channel, data,
607 payload);
608 data += payload;
610 if ((!!(ttusb->muxpack[0] & 0x20)) ^
611 !!(ttusb->muxpack[1] & 1))
612 data++;
613 #warning TODO: pusi
614 printk("cc: %04x\n", (data[0] << 8) | data[1]);
615 #endif
616 numsec++;
617 } else if (muxpack[0] == 0x47) {
618 #ifdef TTUSB_HWSECTIONS
619 /* we have TS data here! */
620 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
621 int channel;
622 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
623 if (ttusb->channel[channel].active
624 && (pid == ttusb->channel[channel].pid))
625 ttusb_handle_ts_data(ttusb->channel +
626 channel, muxpack,
627 188);
628 #endif
629 numts++;
630 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
631 } else if (muxpack[0] != 0) {
632 numinvalid++;
633 printk("illegal muxpack type %02x\n", muxpack[0]);
634 } else
635 numstuff++;
638 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
640 int maxwork = 1024;
641 while (len) {
642 if (!(maxwork--)) {
643 printk("%s: too much work\n", __FUNCTION__);
644 break;
647 switch (ttusb->mux_state) {
648 case 0:
649 case 1:
650 case 2:
651 len--;
652 if (*data++ == 0xAA)
653 ++ttusb->mux_state;
654 else {
655 ttusb->mux_state = 0;
656 #if DEBUG > 3
657 if (ttusb->insync)
658 printk("%02x ", data[-1]);
659 #else
660 if (ttusb->insync) {
661 printk("%s: lost sync.\n",
662 __FUNCTION__);
663 ttusb->insync = 0;
665 #endif
667 break;
668 case 3:
669 ttusb->insync = 1;
670 len--;
671 ttusb->mux_npacks = *data++;
672 ++ttusb->mux_state;
673 ttusb->muxpack_ptr = 0;
674 /* maximum bytes, until we know the length */
675 ttusb->muxpack_len = 2;
676 break;
677 case 4:
679 int avail;
680 avail = len;
681 if (avail >
682 (ttusb->muxpack_len -
683 ttusb->muxpack_ptr))
684 avail =
685 ttusb->muxpack_len -
686 ttusb->muxpack_ptr;
687 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
688 data, avail);
689 ttusb->muxpack_ptr += avail;
690 if (ttusb->muxpack_ptr > 264)
691 BUG();
692 data += avail;
693 len -= avail;
694 /* determine length */
695 if (ttusb->muxpack_ptr == 2) {
696 if (ttusb->muxpack[0] & 0x80) {
697 ttusb->muxpack_len =
698 ttusb->muxpack[1] + 2;
699 if (ttusb->
700 muxpack[0] & 0x20)
701 ttusb->
702 muxpack_len++;
703 if ((!!
704 (ttusb->
705 muxpack[0] & 0x20)) ^
706 !!(ttusb->
707 muxpack[1] & 1))
708 ttusb->
709 muxpack_len++;
710 ttusb->muxpack_len += 4;
711 } else if (ttusb->muxpack[0] ==
712 0x47)
713 ttusb->muxpack_len =
714 188 + 4;
715 else if (ttusb->muxpack[0] == 0x00)
716 ttusb->muxpack_len =
717 ttusb->muxpack[1] + 2 +
719 else {
720 dprintk
721 ("%s: invalid state: first byte is %x\n",
722 __FUNCTION__,
723 ttusb->muxpack[0]);
724 ttusb->mux_state = 0;
729 * if length is valid and we reached the end:
730 * goto next muxpack
732 if ((ttusb->muxpack_ptr >= 2) &&
733 (ttusb->muxpack_ptr ==
734 ttusb->muxpack_len)) {
735 ttusb_process_muxpack(ttusb,
736 ttusb->
737 muxpack,
738 ttusb->
739 muxpack_ptr);
740 ttusb->muxpack_ptr = 0;
741 /* maximum bytes, until we know the length */
742 ttusb->muxpack_len = 2;
745 * no muxpacks left?
746 * return to search-sync state
748 if (!ttusb->mux_npacks--) {
749 ttusb->mux_state = 0;
750 break;
753 break;
755 default:
756 BUG();
757 break;
762 static void ttusb_iso_irq(struct urb *urb, struct pt_regs *ptregs)
764 struct ttusb *ttusb = urb->context;
766 if (!ttusb->iso_streaming)
767 return;
769 #if 0
770 printk("%s: status %d, errcount == %d, length == %i\n",
771 __FUNCTION__,
772 urb->status, urb->error_count, urb->actual_length);
773 #endif
775 if (!urb->status) {
776 int i;
777 for (i = 0; i < urb->number_of_packets; ++i) {
778 struct usb_iso_packet_descriptor *d;
779 u8 *data;
780 int len;
781 numpkt++;
782 if ((jiffies - lastj) >= HZ) {
783 #if DEBUG > 2
784 printk
785 ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
786 numpkt * HZ / (jiffies - lastj),
787 numts, numstuff, numsec, numinvalid,
788 numts + numstuff + numsec +
789 numinvalid);
790 #endif
791 numts = numstuff = numsec = numinvalid = 0;
792 lastj = jiffies;
793 numpkt = 0;
795 d = &urb->iso_frame_desc[i];
796 data = urb->transfer_buffer + d->offset;
797 len = d->actual_length;
798 d->actual_length = 0;
799 d->status = 0;
800 ttusb_process_frame(ttusb, data, len);
803 usb_submit_urb(urb, GFP_ATOMIC);
806 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
808 int i;
810 for (i = 0; i < ISO_BUF_COUNT; i++)
811 if (ttusb->iso_urb[i])
812 usb_free_urb(ttusb->iso_urb[i]);
814 pci_free_consistent(NULL,
815 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
816 ISO_BUF_COUNT, ttusb->iso_buffer,
817 ttusb->iso_dma_handle);
820 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
822 int i;
824 ttusb->iso_buffer = pci_alloc_consistent(NULL,
825 ISO_FRAME_SIZE *
826 FRAMES_PER_ISO_BUF *
827 ISO_BUF_COUNT,
828 &ttusb->iso_dma_handle);
830 memset(ttusb->iso_buffer, 0,
831 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
833 for (i = 0; i < ISO_BUF_COUNT; i++) {
834 struct urb *urb;
836 if (!
837 (urb =
838 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
839 ttusb_free_iso_urbs(ttusb);
840 return -ENOMEM;
843 ttusb->iso_urb[i] = urb;
846 return 0;
849 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
851 int i;
853 for (i = 0; i < ISO_BUF_COUNT; i++)
854 usb_kill_urb(ttusb->iso_urb[i]);
856 ttusb->iso_streaming = 0;
859 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
861 int i, j, err, buffer_offset = 0;
863 if (ttusb->iso_streaming) {
864 printk("%s: iso xfer already running!\n", __FUNCTION__);
865 return 0;
868 ttusb->cc = -1;
869 ttusb->insync = 0;
870 ttusb->mux_state = 0;
872 for (i = 0; i < ISO_BUF_COUNT; i++) {
873 int frame_offset = 0;
874 struct urb *urb = ttusb->iso_urb[i];
876 urb->dev = ttusb->dev;
877 urb->context = ttusb;
878 urb->complete = ttusb_iso_irq;
879 urb->pipe = ttusb->isoc_in_pipe;
880 urb->transfer_flags = URB_ISO_ASAP;
881 urb->interval = 1;
882 urb->number_of_packets = FRAMES_PER_ISO_BUF;
883 urb->transfer_buffer_length =
884 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
885 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
886 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
888 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
889 urb->iso_frame_desc[j].offset = frame_offset;
890 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
891 frame_offset += ISO_FRAME_SIZE;
895 for (i = 0; i < ISO_BUF_COUNT; i++) {
896 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
897 ttusb_stop_iso_xfer(ttusb);
898 printk
899 ("%s: failed urb submission (%i: err = %i)!\n",
900 __FUNCTION__, i, err);
901 return err;
905 ttusb->iso_streaming = 1;
907 return 0;
910 #ifdef TTUSB_HWSECTIONS
911 static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
912 int len)
914 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
917 static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
918 int len)
920 // struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
921 #error TODO: handle ugly stuff
922 // dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
924 #endif
926 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
928 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
929 int feed_type = 1;
931 dprintk("ttusb_start_feed\n");
933 switch (dvbdmxfeed->type) {
934 case DMX_TYPE_TS:
935 break;
936 case DMX_TYPE_SEC:
937 break;
938 default:
939 return -EINVAL;
942 if (dvbdmxfeed->type == DMX_TYPE_TS) {
943 switch (dvbdmxfeed->pes_type) {
944 case DMX_TS_PES_VIDEO:
945 case DMX_TS_PES_AUDIO:
946 case DMX_TS_PES_TELETEXT:
947 case DMX_TS_PES_PCR:
948 case DMX_TS_PES_OTHER:
949 break;
950 default:
951 return -EINVAL;
955 #ifdef TTUSB_HWSECTIONS
956 #error TODO: allocate filters
957 if (dvbdmxfeed->type == DMX_TYPE_TS) {
958 feed_type = 1;
959 } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
960 feed_type = 2;
962 #endif
964 ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
966 if (0 == ttusb->running_feed_count++)
967 ttusb_start_iso_xfer(ttusb);
969 return 0;
972 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
974 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
976 ttusb_del_channel(ttusb, dvbdmxfeed->index);
978 if (--ttusb->running_feed_count == 0)
979 ttusb_stop_iso_xfer(ttusb);
981 return 0;
984 static int ttusb_setup_interfaces(struct ttusb *ttusb)
986 usb_set_interface(ttusb->dev, 1, 1);
988 ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
989 ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
990 ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
992 return 0;
995 #if 0
996 static u8 stc_firmware[8192];
998 static int stc_open(struct inode *inode, struct file *file)
1000 struct ttusb *ttusb = file->private_data;
1001 int addr;
1003 for (addr = 0; addr < 8192; addr += 16) {
1004 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
1005 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
1006 16);
1009 return 0;
1012 static ssize_t stc_read(struct file *file, char *buf, size_t count,
1013 loff_t * offset)
1015 int tc = count;
1017 if ((tc + *offset) > 8192)
1018 tc = 8192 - *offset;
1020 if (tc < 0)
1021 return 0;
1023 if (copy_to_user(buf, stc_firmware + *offset, tc))
1024 return -EFAULT;
1026 *offset += tc;
1028 return tc;
1031 static int stc_release(struct inode *inode, struct file *file)
1033 return 0;
1036 static struct file_operations stc_fops = {
1037 .owner = THIS_MODULE,
1038 .read = stc_read,
1039 .open = stc_open,
1040 .release = stc_release,
1042 #endif
1044 static u32 functionality(struct i2c_adapter *adapter)
1046 return I2C_FUNC_I2C;
1051 static int alps_tdmb7_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1053 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1054 u8 data[4];
1055 struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
1056 u32 div;
1058 div = (params->frequency + 36166667) / 166667;
1060 data[0] = (div >> 8) & 0x7f;
1061 data[1] = div & 0xff;
1062 data[2] = ((div >> 10) & 0x60) | 0x85;
1063 data[3] = params->frequency < 592000000 ? 0x40 : 0x80;
1065 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
1066 return 0;
1069 static struct cx22700_config alps_tdmb7_config = {
1070 .demod_address = 0x43,
1071 .pll_set = alps_tdmb7_pll_set,
1078 static int philips_tdm1316l_pll_init(struct dvb_frontend* fe)
1080 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1081 static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1082 static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1083 struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1085 // setup PLL configuration
1086 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1087 msleep(1);
1089 // disable the mc44BC374c (do not check for errors)
1090 tuner_msg.addr = 0x65;
1091 tuner_msg.buf = disable_mc44BC374c;
1092 tuner_msg.len = sizeof(disable_mc44BC374c);
1093 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1094 i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1097 return 0;
1100 static int philips_tdm1316l_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1102 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1103 u8 tuner_buf[4];
1104 struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1105 int tuner_frequency = 0;
1106 u8 band, cp, filter;
1108 // determine charge pump
1109 tuner_frequency = params->frequency + 36130000;
1110 if (tuner_frequency < 87000000) return -EINVAL;
1111 else if (tuner_frequency < 130000000) cp = 3;
1112 else if (tuner_frequency < 160000000) cp = 5;
1113 else if (tuner_frequency < 200000000) cp = 6;
1114 else if (tuner_frequency < 290000000) cp = 3;
1115 else if (tuner_frequency < 420000000) cp = 5;
1116 else if (tuner_frequency < 480000000) cp = 6;
1117 else if (tuner_frequency < 620000000) cp = 3;
1118 else if (tuner_frequency < 830000000) cp = 5;
1119 else if (tuner_frequency < 895000000) cp = 7;
1120 else return -EINVAL;
1122 // determine band
1123 if (params->frequency < 49000000) return -EINVAL;
1124 else if (params->frequency < 159000000) band = 1;
1125 else if (params->frequency < 444000000) band = 2;
1126 else if (params->frequency < 861000000) band = 4;
1127 else return -EINVAL;
1129 // setup PLL filter
1130 switch (params->u.ofdm.bandwidth) {
1131 case BANDWIDTH_6_MHZ:
1132 tda1004x_write_byte(fe, 0x0C, 0);
1133 filter = 0;
1134 break;
1136 case BANDWIDTH_7_MHZ:
1137 tda1004x_write_byte(fe, 0x0C, 0);
1138 filter = 0;
1139 break;
1141 case BANDWIDTH_8_MHZ:
1142 tda1004x_write_byte(fe, 0x0C, 0xFF);
1143 filter = 1;
1144 break;
1146 default:
1147 return -EINVAL;
1150 // calculate divisor
1151 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1152 tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
1154 // setup tuner buffer
1155 tuner_buf[0] = tuner_frequency >> 8;
1156 tuner_buf[1] = tuner_frequency & 0xff;
1157 tuner_buf[2] = 0xca;
1158 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1160 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1161 return -EIO;
1163 msleep(1);
1164 return 0;
1167 static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1169 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1171 return request_firmware(fw, name, &ttusb->dev->dev);
1174 static struct tda1004x_config philips_tdm1316l_config = {
1176 .demod_address = 0x8,
1177 .invert = 1,
1178 .invert_oclk = 0,
1179 .pll_init = philips_tdm1316l_pll_init,
1180 .pll_set = philips_tdm1316l_pll_set,
1181 .request_firmware = philips_tdm1316l_request_firmware,
1184 static u8 alps_bsbe1_inittab[] = {
1185 0x01, 0x15,
1186 0x02, 0x30,
1187 0x03, 0x00,
1188 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1189 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1190 0x06, 0x40, /* DAC not used, set to high impendance mode */
1191 0x07, 0x00, /* DAC LSB */
1192 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1193 0x09, 0x00, /* FIFO */
1194 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1195 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1196 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1197 0x10, 0x3f, // AGC2 0x3d
1198 0x11, 0x84,
1199 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
1200 0x15, 0xc9, // lock detector threshold
1201 0x16, 0x00,
1202 0x17, 0x00,
1203 0x18, 0x00,
1204 0x19, 0x00,
1205 0x1a, 0x00,
1206 0x1f, 0x50,
1207 0x20, 0x00,
1208 0x21, 0x00,
1209 0x22, 0x00,
1210 0x23, 0x00,
1211 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1212 0x29, 0x1e, // 1/2 threshold
1213 0x2a, 0x14, // 2/3 threshold
1214 0x2b, 0x0f, // 3/4 threshold
1215 0x2c, 0x09, // 5/6 threshold
1216 0x2d, 0x05, // 7/8 threshold
1217 0x2e, 0x01,
1218 0x31, 0x1f, // test all FECs
1219 0x32, 0x19, // viterbi and synchro search
1220 0x33, 0xfc, // rs control
1221 0x34, 0x93, // error control
1222 0x0f, 0x92,
1223 0xff, 0xff
1226 static u8 alps_bsru6_inittab[] = {
1227 0x01, 0x15,
1228 0x02, 0x30,
1229 0x03, 0x00,
1230 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1231 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1232 0x06, 0x40, /* DAC not used, set to high impendance mode */
1233 0x07, 0x00, /* DAC LSB */
1234 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1235 0x09, 0x00, /* FIFO */
1236 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1237 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1238 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1239 0x10, 0x3f, // AGC2 0x3d
1240 0x11, 0x84,
1241 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
1242 0x15, 0xc9, // lock detector threshold
1243 0x16, 0x00,
1244 0x17, 0x00,
1245 0x18, 0x00,
1246 0x19, 0x00,
1247 0x1a, 0x00,
1248 0x1f, 0x50,
1249 0x20, 0x00,
1250 0x21, 0x00,
1251 0x22, 0x00,
1252 0x23, 0x00,
1253 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1254 0x29, 0x1e, // 1/2 threshold
1255 0x2a, 0x14, // 2/3 threshold
1256 0x2b, 0x0f, // 3/4 threshold
1257 0x2c, 0x09, // 5/6 threshold
1258 0x2d, 0x05, // 7/8 threshold
1259 0x2e, 0x01,
1260 0x31, 0x1f, // test all FECs
1261 0x32, 0x19, // viterbi and synchro search
1262 0x33, 0xfc, // rs control
1263 0x34, 0x93, // error control
1264 0x0f, 0x52,
1265 0xff, 0xff
1268 static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1270 u8 aclk = 0;
1271 u8 bclk = 0;
1273 if (srate < 1500000) {
1274 aclk = 0xb7;
1275 bclk = 0x47;
1276 } else if (srate < 3000000) {
1277 aclk = 0xb7;
1278 bclk = 0x4b;
1279 } else if (srate < 7000000) {
1280 aclk = 0xb7;
1281 bclk = 0x4f;
1282 } else if (srate < 14000000) {
1283 aclk = 0xb7;
1284 bclk = 0x53;
1285 } else if (srate < 30000000) {
1286 aclk = 0xb6;
1287 bclk = 0x53;
1288 } else if (srate < 45000000) {
1289 aclk = 0xb4;
1290 bclk = 0x51;
1293 stv0299_writereg(fe, 0x13, aclk);
1294 stv0299_writereg(fe, 0x14, bclk);
1295 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1296 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1297 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1299 return 0;
1302 static int philips_tsa5059_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1304 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1305 u8 buf[4];
1306 u32 div;
1307 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1309 if ((params->frequency < 950000) || (params->frequency > 2150000))
1310 return -EINVAL;
1312 div = (params->frequency + (125 - 1)) / 125; // round correctly
1313 buf[0] = (div >> 8) & 0x7f;
1314 buf[1] = div & 0xff;
1315 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1316 buf[3] = 0xC4;
1318 if (params->frequency > 1530000)
1319 buf[3] = 0xC0;
1321 /* BSBE1 wants XCE bit set */
1322 if (ttusb->revision == TTUSB_REV_2_2)
1323 buf[3] |= 0x20;
1325 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1326 return -EIO;
1328 return 0;
1331 static struct stv0299_config alps_stv0299_config = {
1332 .demod_address = 0x68,
1333 .inittab = alps_bsru6_inittab,
1334 .mclk = 88000000UL,
1335 .invert = 1,
1336 .enhanced_tuning = 0,
1337 .skip_reinit = 0,
1338 .lock_output = STV0229_LOCKOUTPUT_1,
1339 .volt13_op0_op1 = STV0299_VOLT13_OP1,
1340 .min_delay_ms = 100,
1341 .set_symbol_rate = alps_stv0299_set_symbol_rate,
1342 .pll_set = philips_tsa5059_pll_set,
1345 static int ttusb_novas_grundig_29504_491_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1347 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1348 u8 buf[4];
1349 u32 div;
1350 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1352 div = params->frequency / 125;
1354 buf[0] = (div >> 8) & 0x7f;
1355 buf[1] = div & 0xff;
1356 buf[2] = 0x8e;
1357 buf[3] = 0x00;
1359 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1360 return -EIO;
1362 return 0;
1365 static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1367 .demod_address = 0x68,
1368 .pll_set = ttusb_novas_grundig_29504_491_pll_set,
1371 static int alps_tdbe2_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1373 struct ttusb* ttusb = fe->dvb->priv;
1374 u32 div;
1375 u8 data[4];
1376 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1378 div = (params->frequency + 35937500 + 31250) / 62500;
1380 data[0] = (div >> 8) & 0x7f;
1381 data[1] = div & 0xff;
1382 data[2] = 0x85 | ((div >> 10) & 0x60);
1383 data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
1385 if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1386 return -EIO;
1388 return 0;
1392 static struct ves1820_config alps_tdbe2_config = {
1393 .demod_address = 0x09,
1394 .xin = 57840000UL,
1395 .invert = 1,
1396 .selagc = VES1820_SELAGC_SIGNAMPERR,
1397 .pll_set = alps_tdbe2_pll_set,
1400 static u8 read_pwm(struct ttusb* ttusb)
1402 u8 b = 0xff;
1403 u8 pwm;
1404 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1405 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1407 if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1408 pwm = 0x48;
1410 return pwm;
1414 static void frontend_init(struct ttusb* ttusb)
1416 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1417 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1418 // try the stv0299 based first
1419 ttusb->fe = stv0299_attach(&alps_stv0299_config, &ttusb->i2c_adap);
1420 if (ttusb->fe != NULL) {
1421 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1422 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1423 ttusb->fe->ops->set_voltage = lnbp21_set_voltage;
1424 } else { // ALPS BSRU6
1425 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1427 break;
1430 // Grundig 29504-491
1431 ttusb->fe = tda8083_attach(&ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1432 if (ttusb->fe != NULL) {
1433 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1434 break;
1437 break;
1439 case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
1440 ttusb->fe = ves1820_attach(&alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
1441 if (ttusb->fe != NULL)
1442 break;
1443 break;
1445 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1446 // try the ALPS TDMB7 first
1447 ttusb->fe = cx22700_attach(&alps_tdmb7_config, &ttusb->i2c_adap);
1448 if (ttusb->fe != NULL)
1449 break;
1451 // Philips td1316
1452 ttusb->fe = tda10046_attach(&philips_tdm1316l_config, &ttusb->i2c_adap);
1453 if (ttusb->fe != NULL)
1454 break;
1455 break;
1458 if (ttusb->fe == NULL) {
1459 printk("dvb-ttusb-budget: A frontend driver was not found for device %04x/%04x\n",
1460 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1461 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1462 } else {
1463 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1464 printk("dvb-ttusb-budget: Frontend registration failed!\n");
1465 if (ttusb->fe->ops->release)
1466 ttusb->fe->ops->release(ttusb->fe);
1467 ttusb->fe = NULL;
1474 static struct i2c_algorithm ttusb_dec_algo = {
1475 .master_xfer = master_xfer,
1476 .functionality = functionality,
1479 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1481 struct usb_device *udev;
1482 struct ttusb *ttusb;
1483 int result;
1485 dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1487 udev = interface_to_usbdev(intf);
1489 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1491 if (!(ttusb = kmalloc(sizeof(struct ttusb), GFP_KERNEL)))
1492 return -ENOMEM;
1494 memset(ttusb, 0, sizeof(struct ttusb));
1496 ttusb->dev = udev;
1497 ttusb->c = 0;
1498 ttusb->mux_state = 0;
1499 sema_init(&ttusb->semi2c, 0);
1500 sema_init(&ttusb->semusb, 1);
1502 ttusb_setup_interfaces(ttusb);
1504 ttusb_alloc_iso_urbs(ttusb);
1505 if (ttusb_init_controller(ttusb))
1506 printk("ttusb_init_controller: error\n");
1508 up(&ttusb->semi2c);
1510 dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE);
1511 ttusb->adapter.priv = ttusb;
1513 /* i2c */
1514 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1515 strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1517 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1519 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
1520 ttusb->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
1521 #else
1522 ttusb->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
1523 #endif
1524 ttusb->i2c_adap.algo = &ttusb_dec_algo;
1525 ttusb->i2c_adap.algo_data = NULL;
1526 ttusb->i2c_adap.id = I2C_ALGO_BIT;
1528 result = i2c_add_adapter(&ttusb->i2c_adap);
1529 if (result) {
1530 dvb_unregister_adapter (&ttusb->adapter);
1531 return result;
1534 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1536 ttusb->dvb_demux.dmx.capabilities =
1537 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1538 ttusb->dvb_demux.priv = NULL;
1539 #ifdef TTUSB_HWSECTIONS
1540 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1541 #else
1542 ttusb->dvb_demux.filternum = 32;
1543 #endif
1544 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1545 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1546 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1547 ttusb->dvb_demux.write_to_decoder = NULL;
1549 if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1550 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1551 i2c_del_adapter(&ttusb->i2c_adap);
1552 dvb_unregister_adapter (&ttusb->adapter);
1553 return -ENODEV;
1555 //FIXME dmxdev (nur WAS?)
1556 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1557 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1558 ttusb->dmxdev.capabilities = 0;
1560 if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
1561 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1562 result);
1563 dvb_dmx_release(&ttusb->dvb_demux);
1564 i2c_del_adapter(&ttusb->i2c_adap);
1565 dvb_unregister_adapter (&ttusb->adapter);
1566 return -ENODEV;
1569 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1570 printk("ttusb_dvb: dvb_net_init failed!\n");
1571 dvb_dmxdev_release(&ttusb->dmxdev);
1572 dvb_dmx_release(&ttusb->dvb_demux);
1573 i2c_del_adapter(&ttusb->i2c_adap);
1574 dvb_unregister_adapter (&ttusb->adapter);
1575 return -ENODEV;
1578 #if 0
1579 ttusb->stc_devfs_handle =
1580 devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
1581 DEVFS_FL_DEFAULT, 0, 192,
1582 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1583 | S_IROTH | S_IWOTH, &stc_fops, ttusb);
1584 #endif
1585 usb_set_intfdata(intf, (void *) ttusb);
1587 frontend_init(ttusb);
1589 return 0;
1592 static void ttusb_disconnect(struct usb_interface *intf)
1594 struct ttusb *ttusb = usb_get_intfdata(intf);
1596 usb_set_intfdata(intf, NULL);
1598 ttusb->disconnecting = 1;
1600 ttusb_stop_iso_xfer(ttusb);
1602 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1603 dvb_net_release(&ttusb->dvbnet);
1604 dvb_dmxdev_release(&ttusb->dmxdev);
1605 dvb_dmx_release(&ttusb->dvb_demux);
1606 if (ttusb->fe != NULL) dvb_unregister_frontend(ttusb->fe);
1607 i2c_del_adapter(&ttusb->i2c_adap);
1608 dvb_unregister_adapter(&ttusb->adapter);
1610 ttusb_free_iso_urbs(ttusb);
1612 kfree(ttusb);
1614 dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1617 static struct usb_device_id ttusb_table[] = {
1618 {USB_DEVICE(0xb48, 0x1003)},
1619 {USB_DEVICE(0xb48, 0x1004)},
1620 {USB_DEVICE(0xb48, 0x1005)},
1624 MODULE_DEVICE_TABLE(usb, ttusb_table);
1626 static struct usb_driver ttusb_driver = {
1627 .name = "ttusb",
1628 .probe = ttusb_probe,
1629 .disconnect = ttusb_disconnect,
1630 .id_table = ttusb_table,
1633 static int __init ttusb_init(void)
1635 int err;
1637 if ((err = usb_register(&ttusb_driver)) < 0) {
1638 printk("%s: usb_register failed! Error number %d",
1639 __FILE__, err);
1640 return err;
1643 return 0;
1646 static void __exit ttusb_exit(void)
1648 usb_deregister(&ttusb_driver);
1651 module_init(ttusb_init);
1652 module_exit(ttusb_exit);
1654 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1655 MODULE_DESCRIPTION("TTUSB DVB Driver");
1656 MODULE_LICENSE("GPL");