[TG3]: Refine DMA boundary setting.
[linux-2.6/verdex.git] / drivers / media / dvb / ttusb-budget / dvb-ttusb-budget.c
blobafa0e7a0e5067a68180e595a0cd88b4e8c13b904
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 "cx22700.h"
28 #include "tda1004x.h"
29 #include "stv0299.h"
30 #include "tda8083.h"
32 #include <linux/dvb/frontend.h>
33 #include <linux/dvb/dmx.h>
34 #include <linux/pci.h>
38 TTUSB_HWSECTIONS:
39 the DSP supports filtering in hardware, however, since the "muxstream"
40 is a bit braindead (no matching channel masks or no matching filter mask),
41 we won't support this - yet. it doesn't event support negative filters,
42 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
43 parse TS data. USB bandwith will be a problem when having large
44 datastreams, especially for dvb-net, but hey, that's not my problem.
46 TTUSB_DISEQC, TTUSB_TONE:
47 let the STC do the diseqc/tone stuff. this isn't supported at least with
48 my TTUSB, so let it undef'd unless you want to implement another
49 frontend. never tested.
51 DEBUG:
52 define it to > 3 for really hardcore debugging. you probably don't want
53 this unless the device doesn't load at all. > 2 for bandwidth statistics.
56 static int debug;
58 module_param(debug, int, 0644);
59 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
61 #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
63 #define ISO_BUF_COUNT 4
64 #define FRAMES_PER_ISO_BUF 4
65 #define ISO_FRAME_SIZE 912
66 #define TTUSB_MAXCHANNEL 32
67 #ifdef TTUSB_HWSECTIONS
68 #define TTUSB_MAXFILTER 16 /* ??? */
69 #endif
71 #define TTUSB_REV_2_2 0x22
72 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
74 /**
75 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
76 * the dvb_demux field must be the first in struct!!
78 struct ttusb {
79 struct dvb_demux dvb_demux;
80 struct dmxdev dmxdev;
81 struct dvb_net dvbnet;
83 /* and one for USB access. */
84 struct semaphore semi2c;
85 struct semaphore semusb;
87 struct dvb_adapter adapter;
88 struct usb_device *dev;
90 struct i2c_adapter i2c_adap;
92 int disconnecting;
93 int iso_streaming;
95 unsigned int bulk_out_pipe;
96 unsigned int bulk_in_pipe;
97 unsigned int isoc_in_pipe;
99 void *iso_buffer;
100 dma_addr_t iso_dma_handle;
102 struct urb *iso_urb[ISO_BUF_COUNT];
104 int running_feed_count;
105 int last_channel;
106 int last_filter;
108 u8 c; /* transaction counter, wraps around... */
109 fe_sec_tone_mode_t tone;
110 fe_sec_voltage_t voltage;
112 int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
113 u8 mux_npacks;
114 u8 muxpack[256 + 8];
115 int muxpack_ptr, muxpack_len;
117 int insync;
119 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
120 /* (including stuffing. yes. really.) */
122 u8 last_result[32];
124 int revision;
126 #if 0
127 devfs_handle_t stc_devfs_handle;
128 #endif
130 struct dvb_frontend* fe;
133 /* ugly workaround ... don't know why it's neccessary to read */
134 /* all result codes. */
136 #define DEBUG 0
137 static int ttusb_cmd(struct ttusb *ttusb,
138 const u8 * data, int len, int needresult)
140 int actual_len;
141 int err;
142 #if DEBUG >= 3
143 int i;
145 printk(">");
146 for (i = 0; i < len; ++i)
147 printk(" %02x", data[i]);
148 printk("\n");
149 #endif
151 if (down_interruptible(&ttusb->semusb) < 0)
152 return -EAGAIN;
154 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
155 (u8 *) data, len, &actual_len, 1000);
156 if (err != 0) {
157 dprintk("%s: usb_bulk_msg(send) failed, err == %i!\n",
158 __FUNCTION__, err);
159 up(&ttusb->semusb);
160 return err;
162 if (actual_len != len) {
163 dprintk("%s: only wrote %d of %d bytes\n", __FUNCTION__,
164 actual_len, len);
165 up(&ttusb->semusb);
166 return -1;
169 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
170 ttusb->last_result, 32, &actual_len, 1000);
172 if (err != 0) {
173 printk("%s: failed, receive error %d\n", __FUNCTION__,
174 err);
175 up(&ttusb->semusb);
176 return err;
178 #if DEBUG >= 3
179 actual_len = ttusb->last_result[3] + 4;
180 printk("<");
181 for (i = 0; i < actual_len; ++i)
182 printk(" %02x", ttusb->last_result[i]);
183 printk("\n");
184 #endif
185 if (!needresult)
186 up(&ttusb->semusb);
187 return 0;
190 static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
192 memcpy(data, ttusb->last_result, len);
193 up(&ttusb->semusb);
194 return 0;
197 static int ttusb_i2c_msg(struct ttusb *ttusb,
198 u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
199 u8 rcv_len)
201 u8 b[0x28];
202 u8 id = ++ttusb->c;
203 int i, err;
205 if (snd_len > 0x28 - 7 || rcv_len > 0x20 - 7)
206 return -EINVAL;
208 b[0] = 0xaa;
209 b[1] = id;
210 b[2] = 0x31;
211 b[3] = snd_len + 3;
212 b[4] = addr << 1;
213 b[5] = snd_len;
214 b[6] = rcv_len;
216 for (i = 0; i < snd_len; i++)
217 b[7 + i] = snd_buf[i];
219 err = ttusb_cmd(ttusb, b, snd_len + 7, 1);
221 if (err)
222 return -EREMOTEIO;
224 err = ttusb_result(ttusb, b, 0x20);
226 /* check if the i2c transaction was successful */
227 if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
229 if (rcv_len > 0) {
231 if (err || b[0] != 0x55 || b[1] != id) {
232 dprintk
233 ("%s: usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
234 __FUNCTION__, err, id);
235 return -EREMOTEIO;
238 for (i = 0; i < rcv_len; i++)
239 rcv_buf[i] = b[7 + i];
242 return rcv_len;
245 static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
247 struct ttusb *ttusb = i2c_get_adapdata(adapter);
248 int i = 0;
249 int inc;
251 if (down_interruptible(&ttusb->semi2c) < 0)
252 return -EAGAIN;
254 while (i < num) {
255 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
256 int err;
258 if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
259 addr = msg[i].addr;
260 snd_buf = msg[i].buf;
261 snd_len = msg[i].len;
262 rcv_buf = msg[i + 1].buf;
263 rcv_len = msg[i + 1].len;
264 inc = 2;
265 } else {
266 addr = msg[i].addr;
267 snd_buf = msg[i].buf;
268 snd_len = msg[i].len;
269 rcv_buf = NULL;
270 rcv_len = 0;
271 inc = 1;
274 err = ttusb_i2c_msg(ttusb, addr,
275 snd_buf, snd_len, rcv_buf, rcv_len);
277 if (err < rcv_len) {
278 dprintk("%s: i == %i\n", __FUNCTION__, i);
279 break;
282 i += inc;
285 up(&ttusb->semi2c);
286 return i;
289 #include "dvb-ttusb-dspbootcode.h"
291 static int ttusb_boot_dsp(struct ttusb *ttusb)
293 int i, err;
294 u8 b[40];
296 /* BootBlock */
297 b[0] = 0xaa;
298 b[2] = 0x13;
299 b[3] = 28;
301 /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
302 /* 32 is max packet size, no messages should be splitted. */
303 for (i = 0; i < sizeof(dsp_bootcode); i += 28) {
304 memcpy(&b[4], &dsp_bootcode[i], 28);
306 b[1] = ++ttusb->c;
308 err = ttusb_cmd(ttusb, b, 32, 0);
309 if (err)
310 goto done;
313 /* last block ... */
314 b[1] = ++ttusb->c;
315 b[2] = 0x13;
316 b[3] = 0;
318 err = ttusb_cmd(ttusb, b, 4, 0);
319 if (err)
320 goto done;
322 /* BootEnd */
323 b[1] = ++ttusb->c;
324 b[2] = 0x14;
325 b[3] = 0;
327 err = ttusb_cmd(ttusb, b, 4, 0);
329 done:
330 if (err) {
331 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
332 __FUNCTION__, err);
335 return err;
338 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
339 int pid)
341 int err;
342 /* SetChannel */
343 u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
344 (pid >> 8) & 0xff, pid & 0xff
347 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
348 return err;
351 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
353 int err;
354 /* DelChannel */
355 u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
357 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
358 return err;
361 #ifdef TTUSB_HWSECTIONS
362 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
363 int associated_chan, u8 filter[8], u8 mask[8])
365 int err;
366 /* SetFilter */
367 u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
368 filter[0], filter[1], filter[2], filter[3],
369 filter[4], filter[5], filter[6], filter[7],
370 filter[8], filter[9], filter[10], filter[11],
371 mask[0], mask[1], mask[2], mask[3],
372 mask[4], mask[5], mask[6], mask[7],
373 mask[8], mask[9], mask[10], mask[11]
376 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
377 return err;
380 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
382 int err;
383 /* DelFilter */
384 u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
386 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
387 return err;
389 #endif
391 static int ttusb_init_controller(struct ttusb *ttusb)
393 u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
394 u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
395 u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
396 /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
397 u8 b3[] =
398 { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
399 u8 b4[] =
400 { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
402 u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
403 u8 get_dsp_version[0x20] =
404 { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
405 int err;
407 /* reset board */
408 if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
409 return err;
411 /* reset board (again?) */
412 if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
413 return err;
415 ttusb_boot_dsp(ttusb);
417 /* set i2c bit rate */
418 if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
419 return err;
421 if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
422 return err;
424 err = ttusb_result(ttusb, b4, sizeof(b4));
426 if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
427 return err;
429 if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
430 return err;
432 dprintk("%s: stc-version: %c%c%c%c%c\n", __FUNCTION__,
433 get_version[4], get_version[5], get_version[6],
434 get_version[7], get_version[8]);
436 if (memcmp(get_version + 4, "V 0.0", 5) &&
437 memcmp(get_version + 4, "V 1.1", 5) &&
438 memcmp(get_version + 4, "V 2.1", 5) &&
439 memcmp(get_version + 4, "V 2.2", 5)) {
440 printk
441 ("%s: unknown STC version %c%c%c%c%c, please report!\n",
442 __FUNCTION__, get_version[4], get_version[5],
443 get_version[6], get_version[7], get_version[8]);
446 ttusb->revision = ((get_version[6] - '0') << 4) |
447 (get_version[8] - '0');
449 err =
450 ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
451 if (err)
452 return err;
454 err =
455 ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
456 if (err)
457 return err;
458 printk("%s: dsp-version: %c%c%c\n", __FUNCTION__,
459 get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
460 return 0;
463 #ifdef TTUSB_DISEQC
464 static int ttusb_send_diseqc(struct dvb_frontend* fe,
465 const struct dvb_diseqc_master_cmd *cmd)
467 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
468 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
470 int err;
472 b[3] = 4 + 2 + cmd->msg_len;
473 b[4] = 0xFF; /* send diseqc master, not burst */
474 b[5] = cmd->msg_len;
476 memcpy(b + 5, cmd->msg, cmd->msg_len);
478 /* Diseqc */
479 if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
480 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
481 __FUNCTION__, err);
484 return err;
486 #endif
488 static int lnbp21_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
490 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
491 int ret;
492 u8 data[1];
493 struct i2c_msg msg = { .addr = 0x08, .flags = 0, .buf = data, .len = sizeof(data) };
495 switch(voltage) {
496 case SEC_VOLTAGE_OFF:
497 data[0] = 0x00;
498 break;
499 case SEC_VOLTAGE_13:
500 data[0] = 0x44;
501 break;
502 case SEC_VOLTAGE_18:
503 data[0] = 0x4c;
504 break;
505 default:
506 return -EINVAL;
509 ret = i2c_transfer(&ttusb->i2c_adap, &msg, 1);
510 return (ret != 1) ? -EIO : 0;
513 static int ttusb_update_lnb(struct ttusb *ttusb)
515 u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
516 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
517 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
519 int err;
521 /* SetLNB */
522 if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
523 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
524 __FUNCTION__, err);
527 return err;
530 static int ttusb_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
532 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
534 ttusb->voltage = voltage;
535 return ttusb_update_lnb(ttusb);
538 #ifdef TTUSB_TONE
539 static int ttusb_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
541 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
543 ttusb->tone = tone;
544 return ttusb_update_lnb(ttusb);
546 #endif
549 #if 0
550 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
552 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
553 int err, actual_len;
555 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
556 if (err) {
557 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
558 __FUNCTION__, err);
561 #endif
563 /*****************************************************************************/
565 #ifdef TTUSB_HWSECTIONS
566 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
567 const u8 * data, int len);
568 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
569 const u8 * data, int len);
570 #endif
572 static int numpkt = 0, lastj, numts, numstuff, numsec, numinvalid;
574 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
575 int len)
577 u16 csum = 0, cc;
578 int i;
579 for (i = 0; i < len; i += 2)
580 csum ^= le16_to_cpup((u16 *) (muxpack + i));
581 if (csum) {
582 printk("%s: muxpack with incorrect checksum, ignoring\n",
583 __FUNCTION__);
584 numinvalid++;
585 return;
588 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
589 cc &= 0x7FFF;
590 if ((cc != ttusb->cc) && (ttusb->cc != -1))
591 printk("%s: cc discontinuity (%d frames missing)\n",
592 __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
593 ttusb->cc = (cc + 1) & 0x7FFF;
594 if (muxpack[0] & 0x80) {
595 #ifdef TTUSB_HWSECTIONS
596 /* section data */
597 int pusi = muxpack[0] & 0x40;
598 int channel = muxpack[0] & 0x1F;
599 int payload = muxpack[1];
600 const u8 *data = muxpack + 2;
601 /* check offset flag */
602 if (muxpack[0] & 0x20)
603 data++;
605 ttusb_handle_sec_data(ttusb->channel + channel, data,
606 payload);
607 data += payload;
609 if ((!!(ttusb->muxpack[0] & 0x20)) ^
610 !!(ttusb->muxpack[1] & 1))
611 data++;
612 #warning TODO: pusi
613 printk("cc: %04x\n", (data[0] << 8) | data[1]);
614 #endif
615 numsec++;
616 } else if (muxpack[0] == 0x47) {
617 #ifdef TTUSB_HWSECTIONS
618 /* we have TS data here! */
619 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
620 int channel;
621 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
622 if (ttusb->channel[channel].active
623 && (pid == ttusb->channel[channel].pid))
624 ttusb_handle_ts_data(ttusb->channel +
625 channel, muxpack,
626 188);
627 #endif
628 numts++;
629 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
630 } else if (muxpack[0] != 0) {
631 numinvalid++;
632 printk("illegal muxpack type %02x\n", muxpack[0]);
633 } else
634 numstuff++;
637 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
639 int maxwork = 1024;
640 while (len) {
641 if (!(maxwork--)) {
642 printk("%s: too much work\n", __FUNCTION__);
643 break;
646 switch (ttusb->mux_state) {
647 case 0:
648 case 1:
649 case 2:
650 len--;
651 if (*data++ == 0xAA)
652 ++ttusb->mux_state;
653 else {
654 ttusb->mux_state = 0;
655 #if DEBUG > 3
656 if (ttusb->insync)
657 printk("%02x ", data[-1]);
658 #else
659 if (ttusb->insync) {
660 printk("%s: lost sync.\n",
661 __FUNCTION__);
662 ttusb->insync = 0;
664 #endif
666 break;
667 case 3:
668 ttusb->insync = 1;
669 len--;
670 ttusb->mux_npacks = *data++;
671 ++ttusb->mux_state;
672 ttusb->muxpack_ptr = 0;
673 /* maximum bytes, until we know the length */
674 ttusb->muxpack_len = 2;
675 break;
676 case 4:
678 int avail;
679 avail = len;
680 if (avail >
681 (ttusb->muxpack_len -
682 ttusb->muxpack_ptr))
683 avail =
684 ttusb->muxpack_len -
685 ttusb->muxpack_ptr;
686 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
687 data, avail);
688 ttusb->muxpack_ptr += avail;
689 if (ttusb->muxpack_ptr > 264)
690 BUG();
691 data += avail;
692 len -= avail;
693 /* determine length */
694 if (ttusb->muxpack_ptr == 2) {
695 if (ttusb->muxpack[0] & 0x80) {
696 ttusb->muxpack_len =
697 ttusb->muxpack[1] + 2;
698 if (ttusb->
699 muxpack[0] & 0x20)
700 ttusb->
701 muxpack_len++;
702 if ((!!
703 (ttusb->
704 muxpack[0] & 0x20)) ^
705 !!(ttusb->
706 muxpack[1] & 1))
707 ttusb->
708 muxpack_len++;
709 ttusb->muxpack_len += 4;
710 } else if (ttusb->muxpack[0] ==
711 0x47)
712 ttusb->muxpack_len =
713 188 + 4;
714 else if (ttusb->muxpack[0] == 0x00)
715 ttusb->muxpack_len =
716 ttusb->muxpack[1] + 2 +
718 else {
719 dprintk
720 ("%s: invalid state: first byte is %x\n",
721 __FUNCTION__,
722 ttusb->muxpack[0]);
723 ttusb->mux_state = 0;
728 * if length is valid and we reached the end:
729 * goto next muxpack
731 if ((ttusb->muxpack_ptr >= 2) &&
732 (ttusb->muxpack_ptr ==
733 ttusb->muxpack_len)) {
734 ttusb_process_muxpack(ttusb,
735 ttusb->
736 muxpack,
737 ttusb->
738 muxpack_ptr);
739 ttusb->muxpack_ptr = 0;
740 /* maximum bytes, until we know the length */
741 ttusb->muxpack_len = 2;
744 * no muxpacks left?
745 * return to search-sync state
747 if (!ttusb->mux_npacks--) {
748 ttusb->mux_state = 0;
749 break;
752 break;
754 default:
755 BUG();
756 break;
761 static void ttusb_iso_irq(struct urb *urb, struct pt_regs *ptregs)
763 struct ttusb *ttusb = urb->context;
765 if (!ttusb->iso_streaming)
766 return;
768 #if 0
769 printk("%s: status %d, errcount == %d, length == %i\n",
770 __FUNCTION__,
771 urb->status, urb->error_count, urb->actual_length);
772 #endif
774 if (!urb->status) {
775 int i;
776 for (i = 0; i < urb->number_of_packets; ++i) {
777 struct usb_iso_packet_descriptor *d;
778 u8 *data;
779 int len;
780 numpkt++;
781 if ((jiffies - lastj) >= HZ) {
782 #if DEBUG > 2
783 printk
784 ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
785 numpkt * HZ / (jiffies - lastj),
786 numts, numstuff, numsec, numinvalid,
787 numts + numstuff + numsec +
788 numinvalid);
789 #endif
790 numts = numstuff = numsec = numinvalid = 0;
791 lastj = jiffies;
792 numpkt = 0;
794 d = &urb->iso_frame_desc[i];
795 data = urb->transfer_buffer + d->offset;
796 len = d->actual_length;
797 d->actual_length = 0;
798 d->status = 0;
799 ttusb_process_frame(ttusb, data, len);
802 usb_submit_urb(urb, GFP_ATOMIC);
805 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
807 int i;
809 for (i = 0; i < ISO_BUF_COUNT; i++)
810 if (ttusb->iso_urb[i])
811 usb_free_urb(ttusb->iso_urb[i]);
813 pci_free_consistent(NULL,
814 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
815 ISO_BUF_COUNT, ttusb->iso_buffer,
816 ttusb->iso_dma_handle);
819 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
821 int i;
823 ttusb->iso_buffer = pci_alloc_consistent(NULL,
824 ISO_FRAME_SIZE *
825 FRAMES_PER_ISO_BUF *
826 ISO_BUF_COUNT,
827 &ttusb->iso_dma_handle);
829 memset(ttusb->iso_buffer, 0,
830 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
832 for (i = 0; i < ISO_BUF_COUNT; i++) {
833 struct urb *urb;
835 if (!
836 (urb =
837 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
838 ttusb_free_iso_urbs(ttusb);
839 return -ENOMEM;
842 ttusb->iso_urb[i] = urb;
845 return 0;
848 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
850 int i;
852 for (i = 0; i < ISO_BUF_COUNT; i++)
853 usb_kill_urb(ttusb->iso_urb[i]);
855 ttusb->iso_streaming = 0;
858 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
860 int i, j, err, buffer_offset = 0;
862 if (ttusb->iso_streaming) {
863 printk("%s: iso xfer already running!\n", __FUNCTION__);
864 return 0;
867 ttusb->cc = -1;
868 ttusb->insync = 0;
869 ttusb->mux_state = 0;
871 for (i = 0; i < ISO_BUF_COUNT; i++) {
872 int frame_offset = 0;
873 struct urb *urb = ttusb->iso_urb[i];
875 urb->dev = ttusb->dev;
876 urb->context = ttusb;
877 urb->complete = ttusb_iso_irq;
878 urb->pipe = ttusb->isoc_in_pipe;
879 urb->transfer_flags = URB_ISO_ASAP;
880 urb->interval = 1;
881 urb->number_of_packets = FRAMES_PER_ISO_BUF;
882 urb->transfer_buffer_length =
883 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
884 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
885 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
887 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
888 urb->iso_frame_desc[j].offset = frame_offset;
889 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
890 frame_offset += ISO_FRAME_SIZE;
894 for (i = 0; i < ISO_BUF_COUNT; i++) {
895 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
896 ttusb_stop_iso_xfer(ttusb);
897 printk
898 ("%s: failed urb submission (%i: err = %i)!\n",
899 __FUNCTION__, i, err);
900 return err;
904 ttusb->iso_streaming = 1;
906 return 0;
909 #ifdef TTUSB_HWSECTIONS
910 static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
911 int len)
913 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
916 static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
917 int len)
919 // struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
920 #error TODO: handle ugly stuff
921 // dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
923 #endif
925 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
927 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
928 int feed_type = 1;
930 dprintk("ttusb_start_feed\n");
932 switch (dvbdmxfeed->type) {
933 case DMX_TYPE_TS:
934 break;
935 case DMX_TYPE_SEC:
936 break;
937 default:
938 return -EINVAL;
941 if (dvbdmxfeed->type == DMX_TYPE_TS) {
942 switch (dvbdmxfeed->pes_type) {
943 case DMX_TS_PES_VIDEO:
944 case DMX_TS_PES_AUDIO:
945 case DMX_TS_PES_TELETEXT:
946 case DMX_TS_PES_PCR:
947 case DMX_TS_PES_OTHER:
948 break;
949 default:
950 return -EINVAL;
954 #ifdef TTUSB_HWSECTIONS
955 #error TODO: allocate filters
956 if (dvbdmxfeed->type == DMX_TYPE_TS) {
957 feed_type = 1;
958 } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
959 feed_type = 2;
961 #endif
963 ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
965 if (0 == ttusb->running_feed_count++)
966 ttusb_start_iso_xfer(ttusb);
968 return 0;
971 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
973 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
975 ttusb_del_channel(ttusb, dvbdmxfeed->index);
977 if (--ttusb->running_feed_count == 0)
978 ttusb_stop_iso_xfer(ttusb);
980 return 0;
983 static int ttusb_setup_interfaces(struct ttusb *ttusb)
985 usb_set_interface(ttusb->dev, 1, 1);
987 ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
988 ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
989 ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
991 return 0;
994 #if 0
995 static u8 stc_firmware[8192];
997 static int stc_open(struct inode *inode, struct file *file)
999 struct ttusb *ttusb = file->private_data;
1000 int addr;
1002 for (addr = 0; addr < 8192; addr += 16) {
1003 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
1004 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
1005 16);
1008 return 0;
1011 static ssize_t stc_read(struct file *file, char *buf, size_t count,
1012 loff_t * offset)
1014 int tc = count;
1016 if ((tc + *offset) > 8192)
1017 tc = 8192 - *offset;
1019 if (tc < 0)
1020 return 0;
1022 if (copy_to_user(buf, stc_firmware + *offset, tc))
1023 return -EFAULT;
1025 *offset += tc;
1027 return tc;
1030 static int stc_release(struct inode *inode, struct file *file)
1032 return 0;
1035 static struct file_operations stc_fops = {
1036 .owner = THIS_MODULE,
1037 .read = stc_read,
1038 .open = stc_open,
1039 .release = stc_release,
1041 #endif
1043 static u32 functionality(struct i2c_adapter *adapter)
1045 return I2C_FUNC_I2C;
1050 static int alps_tdmb7_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1052 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1053 u8 data[4];
1054 struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
1055 u32 div;
1057 div = (params->frequency + 36166667) / 166667;
1059 data[0] = (div >> 8) & 0x7f;
1060 data[1] = div & 0xff;
1061 data[2] = ((div >> 10) & 0x60) | 0x85;
1062 data[3] = params->frequency < 592000000 ? 0x40 : 0x80;
1064 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
1065 return 0;
1068 static struct cx22700_config alps_tdmb7_config = {
1069 .demod_address = 0x43,
1070 .pll_set = alps_tdmb7_pll_set,
1077 static int philips_tdm1316l_pll_init(struct dvb_frontend* fe)
1079 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1080 static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1081 static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1082 struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1084 // setup PLL configuration
1085 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1086 msleep(1);
1088 // disable the mc44BC374c (do not check for errors)
1089 tuner_msg.addr = 0x65;
1090 tuner_msg.buf = disable_mc44BC374c;
1091 tuner_msg.len = sizeof(disable_mc44BC374c);
1092 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1093 i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1096 return 0;
1099 static int philips_tdm1316l_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1101 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1102 u8 tuner_buf[4];
1103 struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1104 int tuner_frequency = 0;
1105 u8 band, cp, filter;
1107 // determine charge pump
1108 tuner_frequency = params->frequency + 36130000;
1109 if (tuner_frequency < 87000000) return -EINVAL;
1110 else if (tuner_frequency < 130000000) cp = 3;
1111 else if (tuner_frequency < 160000000) cp = 5;
1112 else if (tuner_frequency < 200000000) cp = 6;
1113 else if (tuner_frequency < 290000000) cp = 3;
1114 else if (tuner_frequency < 420000000) cp = 5;
1115 else if (tuner_frequency < 480000000) cp = 6;
1116 else if (tuner_frequency < 620000000) cp = 3;
1117 else if (tuner_frequency < 830000000) cp = 5;
1118 else if (tuner_frequency < 895000000) cp = 7;
1119 else return -EINVAL;
1121 // determine band
1122 if (params->frequency < 49000000) return -EINVAL;
1123 else if (params->frequency < 159000000) band = 1;
1124 else if (params->frequency < 444000000) band = 2;
1125 else if (params->frequency < 861000000) band = 4;
1126 else return -EINVAL;
1128 // setup PLL filter
1129 switch (params->u.ofdm.bandwidth) {
1130 case BANDWIDTH_6_MHZ:
1131 tda1004x_write_byte(fe, 0x0C, 0);
1132 filter = 0;
1133 break;
1135 case BANDWIDTH_7_MHZ:
1136 tda1004x_write_byte(fe, 0x0C, 0);
1137 filter = 0;
1138 break;
1140 case BANDWIDTH_8_MHZ:
1141 tda1004x_write_byte(fe, 0x0C, 0xFF);
1142 filter = 1;
1143 break;
1145 default:
1146 return -EINVAL;
1149 // calculate divisor
1150 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1151 tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
1153 // setup tuner buffer
1154 tuner_buf[0] = tuner_frequency >> 8;
1155 tuner_buf[1] = tuner_frequency & 0xff;
1156 tuner_buf[2] = 0xca;
1157 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1159 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1160 return -EIO;
1162 msleep(1);
1163 return 0;
1166 static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1168 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1170 return request_firmware(fw, name, &ttusb->dev->dev);
1173 static struct tda1004x_config philips_tdm1316l_config = {
1175 .demod_address = 0x8,
1176 .invert = 1,
1177 .invert_oclk = 0,
1178 .pll_init = philips_tdm1316l_pll_init,
1179 .pll_set = philips_tdm1316l_pll_set,
1180 .request_firmware = philips_tdm1316l_request_firmware,
1183 static u8 alps_bsbe1_inittab[] = {
1184 0x01, 0x15,
1185 0x02, 0x30,
1186 0x03, 0x00,
1187 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1188 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1189 0x06, 0x40, /* DAC not used, set to high impendance mode */
1190 0x07, 0x00, /* DAC LSB */
1191 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1192 0x09, 0x00, /* FIFO */
1193 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1194 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1195 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1196 0x10, 0x3f, // AGC2 0x3d
1197 0x11, 0x84,
1198 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
1199 0x15, 0xc9, // lock detector threshold
1200 0x16, 0x00,
1201 0x17, 0x00,
1202 0x18, 0x00,
1203 0x19, 0x00,
1204 0x1a, 0x00,
1205 0x1f, 0x50,
1206 0x20, 0x00,
1207 0x21, 0x00,
1208 0x22, 0x00,
1209 0x23, 0x00,
1210 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1211 0x29, 0x1e, // 1/2 threshold
1212 0x2a, 0x14, // 2/3 threshold
1213 0x2b, 0x0f, // 3/4 threshold
1214 0x2c, 0x09, // 5/6 threshold
1215 0x2d, 0x05, // 7/8 threshold
1216 0x2e, 0x01,
1217 0x31, 0x1f, // test all FECs
1218 0x32, 0x19, // viterbi and synchro search
1219 0x33, 0xfc, // rs control
1220 0x34, 0x93, // error control
1221 0x0f, 0x92,
1222 0xff, 0xff
1225 static u8 alps_bsru6_inittab[] = {
1226 0x01, 0x15,
1227 0x02, 0x30,
1228 0x03, 0x00,
1229 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1230 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1231 0x06, 0x40, /* DAC not used, set to high impendance mode */
1232 0x07, 0x00, /* DAC LSB */
1233 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1234 0x09, 0x00, /* FIFO */
1235 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1236 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1237 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1238 0x10, 0x3f, // AGC2 0x3d
1239 0x11, 0x84,
1240 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
1241 0x15, 0xc9, // lock detector threshold
1242 0x16, 0x00,
1243 0x17, 0x00,
1244 0x18, 0x00,
1245 0x19, 0x00,
1246 0x1a, 0x00,
1247 0x1f, 0x50,
1248 0x20, 0x00,
1249 0x21, 0x00,
1250 0x22, 0x00,
1251 0x23, 0x00,
1252 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1253 0x29, 0x1e, // 1/2 threshold
1254 0x2a, 0x14, // 2/3 threshold
1255 0x2b, 0x0f, // 3/4 threshold
1256 0x2c, 0x09, // 5/6 threshold
1257 0x2d, 0x05, // 7/8 threshold
1258 0x2e, 0x01,
1259 0x31, 0x1f, // test all FECs
1260 0x32, 0x19, // viterbi and synchro search
1261 0x33, 0xfc, // rs control
1262 0x34, 0x93, // error control
1263 0x0f, 0x52,
1264 0xff, 0xff
1267 static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1269 u8 aclk = 0;
1270 u8 bclk = 0;
1272 if (srate < 1500000) {
1273 aclk = 0xb7;
1274 bclk = 0x47;
1275 } else if (srate < 3000000) {
1276 aclk = 0xb7;
1277 bclk = 0x4b;
1278 } else if (srate < 7000000) {
1279 aclk = 0xb7;
1280 bclk = 0x4f;
1281 } else if (srate < 14000000) {
1282 aclk = 0xb7;
1283 bclk = 0x53;
1284 } else if (srate < 30000000) {
1285 aclk = 0xb6;
1286 bclk = 0x53;
1287 } else if (srate < 45000000) {
1288 aclk = 0xb4;
1289 bclk = 0x51;
1292 stv0299_writereg(fe, 0x13, aclk);
1293 stv0299_writereg(fe, 0x14, bclk);
1294 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1295 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1296 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1298 return 0;
1301 static int philips_tsa5059_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1303 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1304 u8 buf[4];
1305 u32 div;
1306 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1308 if ((params->frequency < 950000) || (params->frequency > 2150000))
1309 return -EINVAL;
1311 div = (params->frequency + (125 - 1)) / 125; // round correctly
1312 buf[0] = (div >> 8) & 0x7f;
1313 buf[1] = div & 0xff;
1314 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1315 buf[3] = 0xC4;
1317 if (params->frequency > 1530000)
1318 buf[3] = 0xC0;
1320 /* BSBE1 wants XCE bit set */
1321 if (ttusb->revision == TTUSB_REV_2_2)
1322 buf[3] |= 0x20;
1324 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1325 return -EIO;
1327 return 0;
1330 static struct stv0299_config alps_stv0299_config = {
1331 .demod_address = 0x68,
1332 .inittab = alps_bsru6_inittab,
1333 .mclk = 88000000UL,
1334 .invert = 1,
1335 .enhanced_tuning = 0,
1336 .skip_reinit = 0,
1337 .lock_output = STV0229_LOCKOUTPUT_1,
1338 .volt13_op0_op1 = STV0299_VOLT13_OP1,
1339 .min_delay_ms = 100,
1340 .set_symbol_rate = alps_stv0299_set_symbol_rate,
1341 .pll_set = philips_tsa5059_pll_set,
1344 static int ttusb_novas_grundig_29504_491_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1346 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1347 u8 buf[4];
1348 u32 div;
1349 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1351 div = params->frequency / 125;
1353 buf[0] = (div >> 8) & 0x7f;
1354 buf[1] = div & 0xff;
1355 buf[2] = 0x8e;
1356 buf[3] = 0x00;
1358 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1359 return -EIO;
1361 return 0;
1364 static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1366 .demod_address = 0x68,
1367 .pll_set = ttusb_novas_grundig_29504_491_pll_set,
1372 static void frontend_init(struct ttusb* ttusb)
1374 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1375 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1376 // try the stv0299 based first
1377 ttusb->fe = stv0299_attach(&alps_stv0299_config, &ttusb->i2c_adap);
1378 if (ttusb->fe != NULL) {
1379 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1380 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1381 ttusb->fe->ops->set_voltage = lnbp21_set_voltage;
1382 } else { // ALPS BSRU6
1383 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1385 break;
1388 // Grundig 29504-491
1389 ttusb->fe = tda8083_attach(&ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1390 if (ttusb->fe != NULL) {
1391 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1392 break;
1395 break;
1397 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1398 // try the ALPS TDMB7 first
1399 ttusb->fe = cx22700_attach(&alps_tdmb7_config, &ttusb->i2c_adap);
1400 if (ttusb->fe != NULL)
1401 break;
1403 // Philips td1316
1404 ttusb->fe = tda10046_attach(&philips_tdm1316l_config, &ttusb->i2c_adap);
1405 if (ttusb->fe != NULL)
1406 break;
1407 break;
1410 if (ttusb->fe == NULL) {
1411 printk("dvb-ttusb-budget: A frontend driver was not found for device %04x/%04x\n",
1412 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1413 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1414 } else {
1415 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1416 printk("dvb-ttusb-budget: Frontend registration failed!\n");
1417 if (ttusb->fe->ops->release)
1418 ttusb->fe->ops->release(ttusb->fe);
1419 ttusb->fe = NULL;
1426 static struct i2c_algorithm ttusb_dec_algo = {
1427 .name = "ttusb dec i2c algorithm",
1428 .id = I2C_ALGO_BIT,
1429 .master_xfer = master_xfer,
1430 .functionality = functionality,
1433 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1435 struct usb_device *udev;
1436 struct ttusb *ttusb;
1437 int result;
1439 dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1441 udev = interface_to_usbdev(intf);
1443 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1445 if (!(ttusb = kmalloc(sizeof(struct ttusb), GFP_KERNEL)))
1446 return -ENOMEM;
1448 memset(ttusb, 0, sizeof(struct ttusb));
1450 ttusb->dev = udev;
1451 ttusb->c = 0;
1452 ttusb->mux_state = 0;
1453 sema_init(&ttusb->semi2c, 0);
1454 sema_init(&ttusb->semusb, 1);
1456 ttusb_setup_interfaces(ttusb);
1458 ttusb_alloc_iso_urbs(ttusb);
1459 if (ttusb_init_controller(ttusb))
1460 printk("ttusb_init_controller: error\n");
1462 up(&ttusb->semi2c);
1464 dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE);
1465 ttusb->adapter.priv = ttusb;
1467 /* i2c */
1468 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1469 strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1471 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1473 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
1474 ttusb->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
1475 #else
1476 ttusb->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
1477 #endif
1478 ttusb->i2c_adap.algo = &ttusb_dec_algo;
1479 ttusb->i2c_adap.algo_data = NULL;
1480 ttusb->i2c_adap.id = I2C_ALGO_BIT;
1482 result = i2c_add_adapter(&ttusb->i2c_adap);
1483 if (result) {
1484 dvb_unregister_adapter (&ttusb->adapter);
1485 return result;
1488 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1490 ttusb->dvb_demux.dmx.capabilities =
1491 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1492 ttusb->dvb_demux.priv = NULL;
1493 #ifdef TTUSB_HWSECTIONS
1494 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1495 #else
1496 ttusb->dvb_demux.filternum = 32;
1497 #endif
1498 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1499 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1500 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1501 ttusb->dvb_demux.write_to_decoder = NULL;
1503 if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1504 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1505 i2c_del_adapter(&ttusb->i2c_adap);
1506 dvb_unregister_adapter (&ttusb->adapter);
1507 return -ENODEV;
1509 //FIXME dmxdev (nur WAS?)
1510 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1511 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1512 ttusb->dmxdev.capabilities = 0;
1514 if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
1515 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1516 result);
1517 dvb_dmx_release(&ttusb->dvb_demux);
1518 i2c_del_adapter(&ttusb->i2c_adap);
1519 dvb_unregister_adapter (&ttusb->adapter);
1520 return -ENODEV;
1523 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1524 printk("ttusb_dvb: dvb_net_init failed!\n");
1525 dvb_dmxdev_release(&ttusb->dmxdev);
1526 dvb_dmx_release(&ttusb->dvb_demux);
1527 i2c_del_adapter(&ttusb->i2c_adap);
1528 dvb_unregister_adapter (&ttusb->adapter);
1529 return -ENODEV;
1532 #if 0
1533 ttusb->stc_devfs_handle =
1534 devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
1535 DEVFS_FL_DEFAULT, 0, 192,
1536 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1537 | S_IROTH | S_IWOTH, &stc_fops, ttusb);
1538 #endif
1539 usb_set_intfdata(intf, (void *) ttusb);
1541 frontend_init(ttusb);
1543 return 0;
1546 static void ttusb_disconnect(struct usb_interface *intf)
1548 struct ttusb *ttusb = usb_get_intfdata(intf);
1550 usb_set_intfdata(intf, NULL);
1552 ttusb->disconnecting = 1;
1554 ttusb_stop_iso_xfer(ttusb);
1556 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1557 dvb_net_release(&ttusb->dvbnet);
1558 dvb_dmxdev_release(&ttusb->dmxdev);
1559 dvb_dmx_release(&ttusb->dvb_demux);
1560 if (ttusb->fe != NULL) dvb_unregister_frontend(ttusb->fe);
1561 i2c_del_adapter(&ttusb->i2c_adap);
1562 dvb_unregister_adapter(&ttusb->adapter);
1564 ttusb_free_iso_urbs(ttusb);
1566 kfree(ttusb);
1568 dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1571 static struct usb_device_id ttusb_table[] = {
1572 {USB_DEVICE(0xb48, 0x1003)},
1573 /* {USB_DEVICE(0xb48, 0x1004)},UNDEFINED HARDWARE - mail linuxtv.org list*/ /* to be confirmed ???? */
1574 {USB_DEVICE(0xb48, 0x1005)},
1578 MODULE_DEVICE_TABLE(usb, ttusb_table);
1580 static struct usb_driver ttusb_driver = {
1581 .name = "Technotrend/Hauppauge USB-Nova",
1582 .probe = ttusb_probe,
1583 .disconnect = ttusb_disconnect,
1584 .id_table = ttusb_table,
1587 static int __init ttusb_init(void)
1589 int err;
1591 if ((err = usb_register(&ttusb_driver)) < 0) {
1592 printk("%s: usb_register failed! Error number %d",
1593 __FILE__, err);
1594 return err;
1597 return 0;
1600 static void __exit ttusb_exit(void)
1602 usb_deregister(&ttusb_driver);
1605 module_init(ttusb_init);
1606 module_exit(ttusb_exit);
1608 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1609 MODULE_DESCRIPTION("TTUSB DVB Driver");
1610 MODULE_LICENSE("GPL");