initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / media / dvb / ttusb-budget / dvb-ttusb-budget.c
blobcc14ec76d43b15a1f5965234b5dcbd76b7dec111
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/usb.h>
17 #include <linux/delay.h>
18 #include <linux/time.h>
19 #include <linux/errno.h>
20 #include <asm/semaphore.h>
22 #include "dvb_frontend.h"
23 #include "dmxdev.h"
24 #include "dvb_demux.h"
25 #include "dvb_net.h"
27 #include <linux/dvb/frontend.h>
28 #include <linux/dvb/dmx.h>
29 #include <linux/pci.h>
31 #include "dvb_functions.h"
34 TTUSB_HWSECTIONS:
35 the DSP supports filtering in hardware, however, since the "muxstream"
36 is a bit braindead (no matching channel masks or no matching filter mask),
37 we won't support this - yet. it doesn't event support negative filters,
38 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
39 parse TS data. USB bandwith will be a problem when having large
40 datastreams, especially for dvb-net, but hey, that's not my problem.
42 TTUSB_DISEQC, TTUSB_TONE:
43 let the STC do the diseqc/tone stuff. this isn't supported at least with
44 my TTUSB, so let it undef'd unless you want to implement another
45 frontend. never tested.
47 DEBUG:
48 define it to > 3 for really hardcore debugging. you probably don't want
49 this unless the device doesn't load at all. > 2 for bandwidth statistics.
52 static int debug = 0;
54 #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
56 #define ISO_BUF_COUNT 4
57 #define FRAMES_PER_ISO_BUF 4
58 #define ISO_FRAME_SIZE 912
59 #define TTUSB_MAXCHANNEL 32
60 #ifdef TTUSB_HWSECTIONS
61 #define TTUSB_MAXFILTER 16 /* ??? */
62 #endif
64 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
66 /**
67 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
68 * the dvb_demux field must be the first in struct!!
70 struct ttusb {
71 struct dvb_demux dvb_demux;
72 struct dmxdev dmxdev;
73 struct dvb_net dvbnet;
75 /* our semaphore, for channel allocation/deallocation */
76 struct semaphore sem;
77 /* and one for USB access. */
78 struct semaphore semusb;
80 struct dvb_adapter *adapter;
81 struct usb_device *dev;
83 int disconnecting;
84 int iso_streaming;
86 unsigned int bulk_out_pipe;
87 unsigned int bulk_in_pipe;
88 unsigned int isoc_in_pipe;
90 void *iso_buffer;
91 dma_addr_t iso_dma_handle;
93 struct urb *iso_urb[ISO_BUF_COUNT];
95 int running_feed_count;
96 int last_channel;
97 int last_filter;
99 u8 c; /* transaction counter, wraps around... */
100 fe_sec_tone_mode_t tone;
101 fe_sec_voltage_t voltage;
103 int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
104 u8 mux_npacks;
105 u8 muxpack[256 + 8];
106 int muxpack_ptr, muxpack_len;
108 int insync;
110 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
111 /* (including stuffing. yes. really.) */
114 u8 last_result[32];
116 struct ttusb_channel {
117 struct ttusb *ttusb;
118 struct dvb_demux_feed *dvbdmxfeed;
120 int active;
121 int id;
122 int pid;
123 int type; /* 1 - TS, 2 - Filter */
124 #ifdef TTUSB_HWSECTIONS
125 int filterstate[TTUSB_MAXFILTER]; /* 0: not busy, 1: busy */
126 #endif
127 } channel[TTUSB_MAXCHANNEL];
128 #if 0
129 devfs_handle_t stc_devfs_handle;
130 #endif
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, HZ);
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, HZ);
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 ttusb_i2c_xfer(struct dvb_i2c_bus *i2c, const struct i2c_msg msg[],
246 int num)
248 struct ttusb *ttusb = i2c->data;
249 int i = 0;
250 int inc;
252 if (down_interruptible(&ttusb->sem) < 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->sem);
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 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 err =
447 ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
448 if (err)
449 return err;
451 err =
452 ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
453 if (err)
454 return err;
455 printk("%s: dsp-version: %c%c%c\n", __FUNCTION__,
456 get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
457 return 0;
460 #ifdef TTUSB_DISEQC
461 static int ttusb_send_diseqc(struct ttusb *ttusb,
462 const struct dvb_diseqc_master_cmd *cmd)
464 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
466 int err;
468 b[3] = 4 + 2 + cmd->msg_len;
469 b[4] = 0xFF; /* send diseqc master, not burst */
470 b[5] = cmd->msg_len;
472 memcpy(b + 5, cmd->msg, cmd->msg_len);
474 /* Diseqc */
475 if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
476 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
477 __FUNCTION__, err);
480 return err;
482 #endif
484 static int ttusb_update_lnb(struct ttusb *ttusb)
486 u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
487 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
488 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
490 int err;
492 /* SetLNB */
493 if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
494 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
495 __FUNCTION__, err);
498 return err;
501 static int ttusb_set_voltage(struct ttusb *ttusb, fe_sec_voltage_t voltage)
503 ttusb->voltage = voltage;
504 return ttusb_update_lnb(ttusb);
507 #ifdef TTUSB_TONE
508 static int ttusb_set_tone(struct ttusb *ttusb, fe_sec_tone_mode_t tone)
510 ttusb->tone = tone;
511 return ttusb_update_lnb(ttusb);
513 #endif
515 static int ttusb_lnb_ioctl(struct dvb_frontend *fe, unsigned int cmd, void *arg)
517 struct ttusb *ttusb = fe->i2c->data;
519 switch (cmd) {
520 case FE_SET_VOLTAGE:
521 return ttusb_set_voltage(ttusb, (fe_sec_voltage_t) arg);
522 #ifdef TTUSB_TONE
523 case FE_SET_TONE:
524 return ttusb_set_tone(ttusb, (fe_sec_tone_mode_t) arg);
525 #endif
526 #ifdef TTUSB_DISEQC
527 case FE_DISEQC_SEND_MASTER_CMD:
528 return ttusb_send_diseqc(ttusb,
529 (struct dvb_diseqc_master_cmd *)
530 arg);
531 #endif
532 default:
533 return -EOPNOTSUPP;
537 #if 0
538 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
540 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
541 int err, actual_len;
543 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
544 if (err) {
545 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
546 __FUNCTION__, err);
549 #endif
551 /*****************************************************************************/
553 #ifdef TTUSB_HWSECTIONS
554 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
555 const u8 * data, int len);
556 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
557 const u8 * data, int len);
558 #endif
560 int numpkt = 0, lastj, numts, numstuff, numsec, numinvalid;
562 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
563 int len)
565 u16 csum = 0, cc;
566 int i;
567 for (i = 0; i < len; i += 2)
568 csum ^= le16_to_cpup((u16 *) (muxpack + i));
569 if (csum) {
570 printk("%s: muxpack with incorrect checksum, ignoring\n",
571 __FUNCTION__);
572 numinvalid++;
573 return;
576 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
577 cc &= 0x7FFF;
578 if ((cc != ttusb->cc) && (ttusb->cc != -1))
579 printk("%s: cc discontinuity (%d frames missing)\n",
580 __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
581 ttusb->cc = (cc + 1) & 0x7FFF;
582 if (muxpack[0] & 0x80) {
583 #ifdef TTUSB_HWSECTIONS
584 /* section data */
585 int pusi = muxpack[0] & 0x40;
586 int channel = muxpack[0] & 0x1F;
587 int payload = muxpack[1];
588 const u8 *data = muxpack + 2;
589 /* check offset flag */
590 if (muxpack[0] & 0x20)
591 data++;
593 ttusb_handle_sec_data(ttusb->channel + channel, data,
594 payload);
595 data += payload;
597 if ((!!(ttusb->muxpack[0] & 0x20)) ^
598 !!(ttusb->muxpack[1] & 1))
599 data++;
600 #warning TODO: pusi
601 printk("cc: %04x\n", (data[0] << 8) | data[1]);
602 #endif
603 numsec++;
604 } else if (muxpack[0] == 0x47) {
605 #ifdef TTUSB_HWSECTIONS
606 /* we have TS data here! */
607 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
608 int channel;
609 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
610 if (ttusb->channel[channel].active
611 && (pid == ttusb->channel[channel].pid))
612 ttusb_handle_ts_data(ttusb->channel +
613 channel, muxpack,
614 188);
615 #endif
616 numts++;
617 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
618 } else if (muxpack[0] != 0) {
619 numinvalid++;
620 printk("illegal muxpack type %02x\n", muxpack[0]);
621 } else
622 numstuff++;
625 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
627 int maxwork = 1024;
628 while (len) {
629 if (!(maxwork--)) {
630 printk("%s: too much work\n", __FUNCTION__);
631 break;
634 switch (ttusb->mux_state) {
635 case 0:
636 case 1:
637 case 2:
638 len--;
639 if (*data++ == 0xAA)
640 ++ttusb->mux_state;
641 else {
642 ttusb->mux_state = 0;
643 #if DEBUG > 3
644 if (ttusb->insync)
645 printk("%02x ", data[-1]);
646 #else
647 if (ttusb->insync) {
648 printk("%s: lost sync.\n",
649 __FUNCTION__);
650 ttusb->insync = 0;
652 #endif
654 break;
655 case 3:
656 ttusb->insync = 1;
657 len--;
658 ttusb->mux_npacks = *data++;
659 ++ttusb->mux_state;
660 ttusb->muxpack_ptr = 0;
661 /* maximum bytes, until we know the length */
662 ttusb->muxpack_len = 2;
663 break;
664 case 4:
666 int avail;
667 avail = len;
668 if (avail >
669 (ttusb->muxpack_len -
670 ttusb->muxpack_ptr))
671 avail =
672 ttusb->muxpack_len -
673 ttusb->muxpack_ptr;
674 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
675 data, avail);
676 ttusb->muxpack_ptr += avail;
677 if (ttusb->muxpack_ptr > 264)
678 BUG();
679 data += avail;
680 len -= avail;
681 /* determine length */
682 if (ttusb->muxpack_ptr == 2) {
683 if (ttusb->muxpack[0] & 0x80) {
684 ttusb->muxpack_len =
685 ttusb->muxpack[1] + 2;
686 if (ttusb->
687 muxpack[0] & 0x20)
688 ttusb->
689 muxpack_len++;
690 if ((!!
691 (ttusb->
692 muxpack[0] & 0x20)) ^
693 !!(ttusb->
694 muxpack[1] & 1))
695 ttusb->
696 muxpack_len++;
697 ttusb->muxpack_len += 4;
698 } else if (ttusb->muxpack[0] ==
699 0x47)
700 ttusb->muxpack_len =
701 188 + 4;
702 else if (ttusb->muxpack[0] == 0x00)
703 ttusb->muxpack_len =
704 ttusb->muxpack[1] + 2 +
706 else {
707 dprintk
708 ("%s: invalid state: first byte is %x\n",
709 __FUNCTION__,
710 ttusb->muxpack[0]);
711 ttusb->mux_state = 0;
716 * if length is valid and we reached the end:
717 * goto next muxpack
719 if ((ttusb->muxpack_ptr >= 2) &&
720 (ttusb->muxpack_ptr ==
721 ttusb->muxpack_len)) {
722 ttusb_process_muxpack(ttusb,
723 ttusb->
724 muxpack,
725 ttusb->
726 muxpack_ptr);
727 ttusb->muxpack_ptr = 0;
728 /* maximum bytes, until we know the length */
729 ttusb->muxpack_len = 2;
732 * no muxpacks left?
733 * return to search-sync state
735 if (!ttusb->mux_npacks--) {
736 ttusb->mux_state = 0;
737 break;
740 break;
742 default:
743 BUG();
744 break;
749 static void ttusb_iso_irq(struct urb *urb, struct pt_regs *ptregs)
751 struct ttusb *ttusb = urb->context;
753 if (!ttusb->iso_streaming)
754 return;
756 #if 0
757 printk("%s: status %d, errcount == %d, length == %i\n",
758 __FUNCTION__,
759 urb->status, urb->error_count, urb->actual_length);
760 #endif
762 if (!urb->status) {
763 int i;
764 for (i = 0; i < urb->number_of_packets; ++i) {
765 struct usb_iso_packet_descriptor *d;
766 u8 *data;
767 int len;
768 numpkt++;
769 if ((jiffies - lastj) >= HZ) {
770 #if DEBUG > 2
771 printk
772 ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
773 numpkt * HZ / (jiffies - lastj),
774 numts, numstuff, numsec, numinvalid,
775 numts + numstuff + numsec +
776 numinvalid);
777 #endif
778 numts = numstuff = numsec = numinvalid = 0;
779 lastj = jiffies;
780 numpkt = 0;
782 d = &urb->iso_frame_desc[i];
783 data = urb->transfer_buffer + d->offset;
784 len = d->actual_length;
785 d->actual_length = 0;
786 d->status = 0;
787 ttusb_process_frame(ttusb, data, len);
790 usb_submit_urb(urb, GFP_KERNEL);
793 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
795 int i;
797 for (i = 0; i < ISO_BUF_COUNT; i++)
798 if (ttusb->iso_urb[i])
799 usb_free_urb(ttusb->iso_urb[i]);
801 pci_free_consistent(NULL,
802 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
803 ISO_BUF_COUNT, ttusb->iso_buffer,
804 ttusb->iso_dma_handle);
807 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
809 int i;
811 ttusb->iso_buffer = pci_alloc_consistent(NULL,
812 ISO_FRAME_SIZE *
813 FRAMES_PER_ISO_BUF *
814 ISO_BUF_COUNT,
815 &ttusb->iso_dma_handle);
817 memset(ttusb->iso_buffer, 0,
818 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
820 for (i = 0; i < ISO_BUF_COUNT; i++) {
821 struct urb *urb;
823 if (!
824 (urb =
825 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_KERNEL))) {
826 ttusb_free_iso_urbs(ttusb);
827 return -ENOMEM;
830 ttusb->iso_urb[i] = urb;
833 return 0;
836 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
838 int i;
840 for (i = 0; i < ISO_BUF_COUNT; i++)
841 usb_unlink_urb(ttusb->iso_urb[i]);
843 ttusb->iso_streaming = 0;
846 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
848 int i, j, err, buffer_offset = 0;
850 if (ttusb->iso_streaming) {
851 printk("%s: iso xfer already running!\n", __FUNCTION__);
852 return 0;
855 ttusb->cc = -1;
856 ttusb->insync = 0;
857 ttusb->mux_state = 0;
859 for (i = 0; i < ISO_BUF_COUNT; i++) {
860 int frame_offset = 0;
861 struct urb *urb = ttusb->iso_urb[i];
863 urb->dev = ttusb->dev;
864 urb->context = ttusb;
865 urb->complete = ttusb_iso_irq;
866 urb->pipe = ttusb->isoc_in_pipe;
867 urb->transfer_flags = URB_ISO_ASAP;
868 urb->interval = 1;
869 urb->number_of_packets = FRAMES_PER_ISO_BUF;
870 urb->transfer_buffer_length =
871 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
872 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
873 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
875 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
876 urb->iso_frame_desc[j].offset = frame_offset;
877 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
878 frame_offset += ISO_FRAME_SIZE;
882 for (i = 0; i < ISO_BUF_COUNT; i++) {
883 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_KERNEL))) {
884 ttusb_stop_iso_xfer(ttusb);
885 printk
886 ("%s: failed urb submission (%i: err = %i)!\n",
887 __FUNCTION__, i, err);
888 return err;
892 ttusb->iso_streaming = 1;
894 return 0;
897 #ifdef TTUSB_HWSECTIONS
898 static void ttusb_handle_ts_data(struct ttusb_channel *channel, const u8 * data,
899 int len)
901 struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
903 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
906 static void ttusb_handle_sec_data(struct ttusb_channel *channel, const u8 * data,
907 int len)
909 // struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
910 #error TODO: handle ugly stuff
911 // dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
913 #endif
915 static struct ttusb_channel *ttusb_channel_allocate(struct ttusb *ttusb)
917 int i;
919 if (down_interruptible(&ttusb->sem))
920 return NULL;
922 /* lock! */
923 for (i = 0; i < TTUSB_MAXCHANNEL; ++i) {
924 if (!ttusb->channel[i].active) {
925 ttusb->channel[i].active = 1;
926 up(&ttusb->sem);
927 return ttusb->channel + i;
931 up(&ttusb->sem);
933 return NULL;
936 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
938 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
939 struct ttusb_channel *channel;
941 dprintk("ttusb_start_feed\n");
943 switch (dvbdmxfeed->type) {
944 case DMX_TYPE_TS:
945 break;
946 case DMX_TYPE_SEC:
947 break;
948 default:
949 return -EINVAL;
952 if (dvbdmxfeed->type == DMX_TYPE_TS) {
953 switch (dvbdmxfeed->pes_type) {
954 case DMX_TS_PES_VIDEO:
955 case DMX_TS_PES_AUDIO:
956 case DMX_TS_PES_TELETEXT:
957 case DMX_TS_PES_PCR:
958 case DMX_TS_PES_OTHER:
959 channel = ttusb_channel_allocate(ttusb);
960 break;
961 default:
962 return -EINVAL;
964 } else {
965 channel = ttusb_channel_allocate(ttusb);
968 if (!channel)
969 return -EBUSY;
971 dvbdmxfeed->priv = channel;
972 channel->dvbdmxfeed = dvbdmxfeed;
974 channel->pid = dvbdmxfeed->pid;
976 #ifdef TTUSB_HWSECTIONS
977 if (dvbdmxfeed->type == DMX_TYPE_TS) {
978 channel->type = 1;
979 } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
980 channel->type = 2;
981 #error TODO: allocate filters
983 #else
984 channel->type = 1;
985 #endif
987 ttusb_set_channel(ttusb, channel->id, channel->type, channel->pid);
989 if (0 == ttusb->running_feed_count++)
990 ttusb_start_iso_xfer(ttusb);
992 return 0;
995 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
997 struct ttusb_channel *channel =
998 (struct ttusb_channel *) dvbdmxfeed->priv;
999 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
1001 ttusb_del_channel(channel->ttusb, channel->id);
1003 if (--ttusb->running_feed_count == 0)
1004 ttusb_stop_iso_xfer(ttusb);
1006 channel->active = 0;
1008 return 0;
1011 static int ttusb_setup_interfaces(struct ttusb *ttusb)
1013 usb_set_interface(ttusb->dev, 1, 1);
1015 ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
1016 ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
1017 ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
1019 return 0;
1022 #if 0
1023 static u8 stc_firmware[8192];
1025 static int stc_open(struct inode *inode, struct file *file)
1027 struct ttusb *ttusb = file->private_data;
1028 int addr;
1030 for (addr = 0; addr < 8192; addr += 16) {
1031 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
1032 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
1033 16);
1036 return 0;
1039 static ssize_t stc_read(struct file *file, char *buf, size_t count,
1040 loff_t * offset)
1042 int tc = count;
1044 if ((tc + *offset) > 8192)
1045 tc = 8192 - *offset;
1047 if (tc < 0)
1048 return 0;
1050 if (copy_to_user(buf, stc_firmware + *offset, tc))
1051 return -EFAULT;
1053 *offset += tc;
1055 return tc;
1058 static int stc_release(struct inode *inode, struct file *file)
1060 return 0;
1063 static struct file_operations stc_fops = {
1064 .owner = THIS_MODULE,
1065 .read = stc_read,
1066 .open = stc_open,
1067 .release = stc_release,
1069 #endif
1071 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1073 struct usb_device *udev;
1074 struct ttusb *ttusb;
1075 int result, channel;
1077 dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1079 udev = interface_to_usbdev(intf);
1081 /* Device has already been reset; its configuration was chosen.
1082 * If this fault happens, use a hotplug script to choose the
1083 * right configuration (write bConfigurationValue in sysfs).
1085 if (udev->actconfig->desc.bConfigurationValue != 1) {
1086 dev_err(&intf->dev, "device config is #%d, need #1\n",
1087 udev->actconfig->desc.bConfigurationValue);
1088 return -ENODEV;
1092 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1094 if (!(ttusb = kmalloc(sizeof(struct ttusb), GFP_KERNEL)))
1095 return -ENOMEM;
1097 memset(ttusb, 0, sizeof(struct ttusb));
1099 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel) {
1100 ttusb->channel[channel].id = channel;
1101 ttusb->channel[channel].ttusb = ttusb;
1104 ttusb->dev = udev;
1105 ttusb->c = 0;
1106 ttusb->mux_state = 0;
1107 sema_init(&ttusb->sem, 0);
1108 sema_init(&ttusb->semusb, 1);
1110 ttusb_setup_interfaces(ttusb);
1112 ttusb_alloc_iso_urbs(ttusb);
1113 if (ttusb_init_controller(ttusb))
1114 printk("ttusb_init_controller: error\n");
1116 up(&ttusb->sem);
1118 dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE);
1120 dvb_register_i2c_bus(ttusb_i2c_xfer, ttusb, ttusb->adapter, 0);
1121 dvb_add_frontend_ioctls(ttusb->adapter, ttusb_lnb_ioctl, NULL,
1122 ttusb);
1124 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1126 ttusb->dvb_demux.dmx.capabilities =
1127 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1128 ttusb->dvb_demux.priv = NULL;
1129 #ifdef TTUSB_HWSECTIONS
1130 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1131 #else
1132 ttusb->dvb_demux.filternum = 32;
1133 #endif
1134 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1135 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1136 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1137 ttusb->dvb_demux.write_to_decoder = NULL;
1139 if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1140 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n",
1141 result);
1142 goto err;
1144 //FIXME dmxdev (nur WAS?)
1145 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1146 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1147 ttusb->dmxdev.capabilities = 0;
1149 if ((result = dvb_dmxdev_init(&ttusb->dmxdev, ttusb->adapter)) < 0) {
1150 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1151 result);
1152 dvb_dmx_release(&ttusb->dvb_demux);
1153 goto err;
1156 if (dvb_net_init
1157 (ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1158 printk("ttusb_dvb: dvb_net_init failed!\n");
1161 err:
1162 #if 0
1163 ttusb->stc_devfs_handle =
1164 devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
1165 DEVFS_FL_DEFAULT, 0, 192,
1166 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1167 | S_IROTH | S_IWOTH, &stc_fops, ttusb);
1168 #endif
1170 usb_set_intfdata(intf, (void *) ttusb);
1172 return 0;
1175 static void ttusb_disconnect(struct usb_interface *intf)
1177 struct ttusb *ttusb = usb_get_intfdata(intf);
1179 usb_set_intfdata(intf, NULL);
1181 ttusb->disconnecting = 1;
1183 ttusb_stop_iso_xfer(ttusb);
1185 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1186 dvb_net_release(&ttusb->dvbnet);
1187 dvb_dmxdev_release(&ttusb->dmxdev);
1188 dvb_dmx_release(&ttusb->dvb_demux);
1190 dvb_unregister_i2c_bus(ttusb_i2c_xfer, ttusb->adapter, 0);
1191 dvb_unregister_adapter(ttusb->adapter);
1193 ttusb_free_iso_urbs(ttusb);
1195 kfree(ttusb);
1197 dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1200 static struct usb_device_id ttusb_table[] = {
1201 {USB_DEVICE(0xb48, 0x1003)},
1202 {USB_DEVICE(0xb48, 0x1004)}, /* to be confirmed ???? */
1203 {USB_DEVICE(0xb48, 0x1005)},
1207 MODULE_DEVICE_TABLE(usb, ttusb_table);
1209 static struct usb_driver ttusb_driver = {
1210 .name = "Technotrend/Hauppauge USB-Nova",
1211 .probe = ttusb_probe,
1212 .disconnect = ttusb_disconnect,
1213 .id_table = ttusb_table,
1216 static int __init ttusb_init(void)
1218 int err;
1220 if ((err = usb_register(&ttusb_driver)) < 0) {
1221 printk("%s: usb_register failed! Error number %d",
1222 __FILE__, err);
1223 return err;
1226 return 0;
1229 static void __exit ttusb_exit(void)
1231 usb_deregister(&ttusb_driver);
1234 module_init(ttusb_init);
1235 module_exit(ttusb_exit);
1237 MODULE_PARM(debug, "i");
1238 MODULE_PARM_DESC(debug, "Debug or not");
1240 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1241 MODULE_DESCRIPTION("TTUSB DVB Driver");
1242 MODULE_LICENSE("GPL");