blob: 248fdc7accfb123d52c37460cc057faec7e2ca52 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * TTUSB DVB driver
3 *
4 * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
5 * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
6 *
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.
11 */
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>
Marcelo Feitoza Parisi4da006c2005-09-09 13:03:15 -070021#include <linux/jiffies.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "dvb_frontend.h"
25#include "dmxdev.h"
26#include "dvb_demux.h"
27#include "dvb_net.h"
Gavin Hamill53936392005-07-07 17:58:04 -070028#include "ves1820.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "cx22700.h"
30#include "tda1004x.h"
31#include "stv0299.h"
32#include "tda8083.h"
33
34#include <linux/dvb/frontend.h>
35#include <linux/dvb/dmx.h>
36#include <linux/pci.h>
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
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.
46
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.
51
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.
55*/
56
57static int debug;
58
59module_param(debug, int, 0644);
60MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
61
62#define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
63
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
71
72#define TTUSB_REV_2_2 0x22
73#define TTUSB_BUDGET_NAME "ttusb_stc_fw"
74
75/**
76 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
77 * the dvb_demux field must be the first in struct!!
78 */
79struct ttusb {
80 struct dvb_demux dvb_demux;
81 struct dmxdev dmxdev;
82 struct dvb_net dvbnet;
83
84 /* and one for USB access. */
Ingo Molnar3593cab2006-02-07 06:49:14 -020085 struct mutex semi2c;
86 struct mutex semusb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -070088 struct dvb_adapter adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct usb_device *dev;
90
91 struct i2c_adapter i2c_adap;
92
93 int disconnecting;
94 int iso_streaming;
95
96 unsigned int bulk_out_pipe;
97 unsigned int bulk_in_pipe;
98 unsigned int isoc_in_pipe;
99
100 void *iso_buffer;
101 dma_addr_t iso_dma_handle;
102
103 struct urb *iso_urb[ISO_BUF_COUNT];
104
105 int running_feed_count;
106 int last_channel;
107 int last_filter;
108
109 u8 c; /* transaction counter, wraps around... */
110 fe_sec_tone_mode_t tone;
111 fe_sec_voltage_t voltage;
112
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;
117
118 int insync;
119
120 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
121 /* (including stuffing. yes. really.) */
122
123 u8 last_result[32];
124
125 int revision;
126
127#if 0
128 devfs_handle_t stc_devfs_handle;
129#endif
130
131 struct dvb_frontend* fe;
132};
133
134/* ugly workaround ... don't know why it's neccessary to read */
135/* all result codes. */
136
137#define DEBUG 0
138static int ttusb_cmd(struct ttusb *ttusb,
139 const u8 * data, int len, int needresult)
140{
141 int actual_len;
142 int err;
143#if DEBUG >= 3
144 int i;
145
146 printk(">");
147 for (i = 0; i < len; ++i)
148 printk(" %02x", data[i]);
149 printk("\n");
150#endif
151
Ingo Molnar3593cab2006-02-07 06:49:14 -0200152 if (mutex_lock_interruptible(&ttusb->semusb) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return -EAGAIN;
154
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);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200160 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return err;
162 }
163 if (actual_len != len) {
164 dprintk("%s: only wrote %d of %d bytes\n", __FUNCTION__,
165 actual_len, len);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200166 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -1;
168 }
169
170 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
171 ttusb->last_result, 32, &actual_len, 1000);
172
173 if (err != 0) {
174 printk("%s: failed, receive error %d\n", __FUNCTION__,
175 err);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200176 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return err;
178 }
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)
Ingo Molnar3593cab2006-02-07 06:49:14 -0200187 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return 0;
189}
190
191static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
192{
193 memcpy(data, ttusb->last_result, len);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200194 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
196}
197
198static int ttusb_i2c_msg(struct ttusb *ttusb,
199 u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
200 u8 rcv_len)
201{
202 u8 b[0x28];
203 u8 id = ++ttusb->c;
204 int i, err;
205
206 if (snd_len > 0x28 - 7 || rcv_len > 0x20 - 7)
207 return -EINVAL;
208
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;
216
217 for (i = 0; i < snd_len; i++)
218 b[7 + i] = snd_buf[i];
219
220 err = ttusb_cmd(ttusb, b, snd_len + 7, 1);
221
222 if (err)
223 return -EREMOTEIO;
224
225 err = ttusb_result(ttusb, b, 0x20);
226
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800227 /* check if the i2c transaction was successful */
228 if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (rcv_len > 0) {
231
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;
237 }
238
239 for (i = 0; i < rcv_len; i++)
240 rcv_buf[i] = b[7 + i];
241 }
242
243 return rcv_len;
244}
245
246static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
247{
248 struct ttusb *ttusb = i2c_get_adapdata(adapter);
249 int i = 0;
250 int inc;
251
Ingo Molnar3593cab2006-02-07 06:49:14 -0200252 if (mutex_lock_interruptible(&ttusb->semi2c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -EAGAIN;
254
255 while (i < num) {
256 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
257 int err;
258
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;
273 }
274
275 err = ttusb_i2c_msg(ttusb, addr,
276 snd_buf, snd_len, rcv_buf, rcv_len);
277
278 if (err < rcv_len) {
279 dprintk("%s: i == %i\n", __FUNCTION__, i);
280 break;
281 }
282
283 i += inc;
284 }
285
Ingo Molnar3593cab2006-02-07 06:49:14 -0200286 mutex_unlock(&ttusb->semi2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return i;
288}
289
290#include "dvb-ttusb-dspbootcode.h"
291
292static int ttusb_boot_dsp(struct ttusb *ttusb)
293{
294 int i, err;
295 u8 b[40];
296
297 /* BootBlock */
298 b[0] = 0xaa;
299 b[2] = 0x13;
300 b[3] = 28;
301
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);
306
307 b[1] = ++ttusb->c;
308
309 err = ttusb_cmd(ttusb, b, 32, 0);
310 if (err)
311 goto done;
312 }
313
314 /* last block ... */
315 b[1] = ++ttusb->c;
316 b[2] = 0x13;
317 b[3] = 0;
318
319 err = ttusb_cmd(ttusb, b, 4, 0);
320 if (err)
321 goto done;
322
323 /* BootEnd */
324 b[1] = ++ttusb->c;
325 b[2] = 0x14;
326 b[3] = 0;
327
328 err = ttusb_cmd(ttusb, b, 4, 0);
329
330 done:
331 if (err) {
332 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
333 __FUNCTION__, err);
334 }
335
336 return err;
337}
338
339static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
340 int pid)
341{
342 int err;
343 /* SetChannel */
344 u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
345 (pid >> 8) & 0xff, pid & 0xff
346 };
347
348 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
349 return err;
350}
351
352static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
353{
354 int err;
355 /* DelChannel */
356 u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
357
358 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
359 return err;
360}
361
362#ifdef TTUSB_HWSECTIONS
363static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
364 int associated_chan, u8 filter[8], u8 mask[8])
365{
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]
375 };
376
377 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
378 return err;
379}
380
381static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
382{
383 int err;
384 /* DelFilter */
385 u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
386
387 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
388 return err;
389}
390#endif
391
392static int ttusb_init_controller(struct ttusb *ttusb)
393{
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 };
402
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;
407
408 /* reset board */
409 if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
410 return err;
411
412 /* reset board (again?) */
413 if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
414 return err;
415
416 ttusb_boot_dsp(ttusb);
417
418 /* set i2c bit rate */
419 if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
420 return err;
421
422 if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
423 return err;
424
425 err = ttusb_result(ttusb, b4, sizeof(b4));
426
427 if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
428 return err;
429
430 if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
431 return err;
432
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]);
436
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]);
445 }
446
447 ttusb->revision = ((get_version[6] - '0') << 4) |
448 (get_version[8] - '0');
449
450 err =
451 ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
452 if (err)
453 return err;
454
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;
462}
463
464#ifdef TTUSB_DISEQC
465static int ttusb_send_diseqc(struct dvb_frontend* fe,
466 const struct dvb_diseqc_master_cmd *cmd)
467{
468 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
469 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
470
471 int err;
472
473 b[3] = 4 + 2 + cmd->msg_len;
474 b[4] = 0xFF; /* send diseqc master, not burst */
475 b[5] = cmd->msg_len;
476
477 memcpy(b + 5, cmd->msg, cmd->msg_len);
478
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);
483 }
484
485 return err;
486}
487#endif
488
489static int lnbp21_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
490{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800491 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) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800496 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;
508 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800510 ret = i2c_transfer(&ttusb->i2c_adap, &msg, 1);
511 return (ret != 1) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514static int ttusb_update_lnb(struct ttusb *ttusb)
515{
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
519 };
520 int err;
521
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);
526 }
527
528 return err;
529}
530
531static int ttusb_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
532{
533 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
534
535 ttusb->voltage = voltage;
536 return ttusb_update_lnb(ttusb);
537}
538
539#ifdef TTUSB_TONE
540static int ttusb_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
541{
542 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
543
544 ttusb->tone = tone;
545 return ttusb_update_lnb(ttusb);
546}
547#endif
548
549
550#if 0
551static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
552{
553 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
554 int err, actual_len;
555
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);
560 }
561}
562#endif
563
564/*****************************************************************************/
565
566#ifdef TTUSB_HWSECTIONS
567static void ttusb_handle_ts_data(struct ttusb_channel *channel,
568 const u8 * data, int len);
569static void ttusb_handle_sec_data(struct ttusb_channel *channel,
570 const u8 * data, int len);
571#endif
572
Marcelo Feitoza Parisi4da006c2005-09-09 13:03:15 -0700573static int numpkt = 0, numts, numstuff, numsec, numinvalid;
574static unsigned long lastj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
577 int len)
578{
579 u16 csum = 0, cc;
580 int i;
581 for (i = 0; i < len; i += 2)
582 csum ^= le16_to_cpup((u16 *) (muxpack + i));
583 if (csum) {
584 printk("%s: muxpack with incorrect checksum, ignoring\n",
585 __FUNCTION__);
586 numinvalid++;
587 return;
588 }
589
590 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
591 cc &= 0x7FFF;
592 if ((cc != ttusb->cc) && (ttusb->cc != -1))
593 printk("%s: cc discontinuity (%d frames missing)\n",
594 __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
595 ttusb->cc = (cc + 1) & 0x7FFF;
596 if (muxpack[0] & 0x80) {
597#ifdef TTUSB_HWSECTIONS
598 /* section data */
599 int pusi = muxpack[0] & 0x40;
600 int channel = muxpack[0] & 0x1F;
601 int payload = muxpack[1];
602 const u8 *data = muxpack + 2;
603 /* check offset flag */
604 if (muxpack[0] & 0x20)
605 data++;
606
607 ttusb_handle_sec_data(ttusb->channel + channel, data,
608 payload);
609 data += payload;
610
611 if ((!!(ttusb->muxpack[0] & 0x20)) ^
612 !!(ttusb->muxpack[1] & 1))
613 data++;
614#warning TODO: pusi
615 printk("cc: %04x\n", (data[0] << 8) | data[1]);
616#endif
617 numsec++;
618 } else if (muxpack[0] == 0x47) {
619#ifdef TTUSB_HWSECTIONS
620 /* we have TS data here! */
621 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
622 int channel;
623 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
624 if (ttusb->channel[channel].active
625 && (pid == ttusb->channel[channel].pid))
626 ttusb_handle_ts_data(ttusb->channel +
627 channel, muxpack,
628 188);
629#endif
630 numts++;
631 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
632 } else if (muxpack[0] != 0) {
633 numinvalid++;
634 printk("illegal muxpack type %02x\n", muxpack[0]);
635 } else
636 numstuff++;
637}
638
639static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
640{
641 int maxwork = 1024;
642 while (len) {
643 if (!(maxwork--)) {
644 printk("%s: too much work\n", __FUNCTION__);
645 break;
646 }
647
648 switch (ttusb->mux_state) {
649 case 0:
650 case 1:
651 case 2:
652 len--;
653 if (*data++ == 0xAA)
654 ++ttusb->mux_state;
655 else {
656 ttusb->mux_state = 0;
657#if DEBUG > 3
658 if (ttusb->insync)
659 printk("%02x ", data[-1]);
660#else
661 if (ttusb->insync) {
662 printk("%s: lost sync.\n",
663 __FUNCTION__);
664 ttusb->insync = 0;
665 }
666#endif
667 }
668 break;
669 case 3:
670 ttusb->insync = 1;
671 len--;
672 ttusb->mux_npacks = *data++;
673 ++ttusb->mux_state;
674 ttusb->muxpack_ptr = 0;
675 /* maximum bytes, until we know the length */
676 ttusb->muxpack_len = 2;
677 break;
678 case 4:
679 {
680 int avail;
681 avail = len;
682 if (avail >
683 (ttusb->muxpack_len -
684 ttusb->muxpack_ptr))
685 avail =
686 ttusb->muxpack_len -
687 ttusb->muxpack_ptr;
688 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
689 data, avail);
690 ttusb->muxpack_ptr += avail;
Eric Sesterhennae246012006-03-13 13:17:11 -0300691 BUG_ON(ttusb->muxpack_ptr > 264);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 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 +
718 4;
719 else {
720 dprintk
721 ("%s: invalid state: first byte is %x\n",
722 __FUNCTION__,
723 ttusb->muxpack[0]);
724 ttusb->mux_state = 0;
725 }
726 }
727
728 /**
729 * if length is valid and we reached the end:
730 * goto next muxpack
731 */
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;
743
744 /**
745 * no muxpacks left?
746 * return to search-sync state
747 */
748 if (!ttusb->mux_npacks--) {
749 ttusb->mux_state = 0;
750 break;
751 }
752 }
753 break;
754 }
755 default:
756 BUG();
757 break;
758 }
759 }
760}
761
762static void ttusb_iso_irq(struct urb *urb, struct pt_regs *ptregs)
763{
764 struct ttusb *ttusb = urb->context;
765
766 if (!ttusb->iso_streaming)
767 return;
768
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
774
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++;
Marcelo Feitoza Parisi4da006c2005-09-09 13:03:15 -0700782 if (time_after_eq(jiffies, lastj + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783#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;
794 }
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);
801 }
802 }
803 usb_submit_urb(urb, GFP_ATOMIC);
804}
805
806static void ttusb_free_iso_urbs(struct ttusb *ttusb)
807{
808 int i;
809
810 for (i = 0; i < ISO_BUF_COUNT; i++)
811 if (ttusb->iso_urb[i])
812 usb_free_urb(ttusb->iso_urb[i]);
813
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);
818}
819
820static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
821{
822 int i;
823
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);
829
830 memset(ttusb->iso_buffer, 0,
831 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
832
833 for (i = 0; i < ISO_BUF_COUNT; i++) {
834 struct urb *urb;
835
836 if (!
837 (urb =
838 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
839 ttusb_free_iso_urbs(ttusb);
840 return -ENOMEM;
841 }
842
843 ttusb->iso_urb[i] = urb;
844 }
845
846 return 0;
847}
848
849static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
850{
851 int i;
852
853 for (i = 0; i < ISO_BUF_COUNT; i++)
854 usb_kill_urb(ttusb->iso_urb[i]);
855
856 ttusb->iso_streaming = 0;
857}
858
859static int ttusb_start_iso_xfer(struct ttusb *ttusb)
860{
861 int i, j, err, buffer_offset = 0;
862
863 if (ttusb->iso_streaming) {
864 printk("%s: iso xfer already running!\n", __FUNCTION__);
865 return 0;
866 }
867
868 ttusb->cc = -1;
869 ttusb->insync = 0;
870 ttusb->mux_state = 0;
871
872 for (i = 0; i < ISO_BUF_COUNT; i++) {
873 int frame_offset = 0;
874 struct urb *urb = ttusb->iso_urb[i];
875
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;
887
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;
892 }
893 }
894
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;
902 }
903 }
904
905 ttusb->iso_streaming = 1;
906
907 return 0;
908}
909
910#ifdef TTUSB_HWSECTIONS
911static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
912 int len)
913{
914 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
915}
916
917static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
918 int len)
919{
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);
923}
924#endif
925
926static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
927{
928 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
929 int feed_type = 1;
930
931 dprintk("ttusb_start_feed\n");
932
933 switch (dvbdmxfeed->type) {
934 case DMX_TYPE_TS:
935 break;
936 case DMX_TYPE_SEC:
937 break;
938 default:
939 return -EINVAL;
940 }
941
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;
952 }
953 }
954
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;
961 }
962#endif
963
964 ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
965
966 if (0 == ttusb->running_feed_count++)
967 ttusb_start_iso_xfer(ttusb);
968
969 return 0;
970}
971
972static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
973{
974 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
975
976 ttusb_del_channel(ttusb, dvbdmxfeed->index);
977
978 if (--ttusb->running_feed_count == 0)
979 ttusb_stop_iso_xfer(ttusb);
980
981 return 0;
982}
983
984static int ttusb_setup_interfaces(struct ttusb *ttusb)
985{
986 usb_set_interface(ttusb->dev, 1, 1);
987
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);
991
992 return 0;
993}
994
995#if 0
996static u8 stc_firmware[8192];
997
998static int stc_open(struct inode *inode, struct file *file)
999{
1000 struct ttusb *ttusb = file->private_data;
1001 int addr;
1002
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);
1007 }
1008
1009 return 0;
1010}
1011
1012static ssize_t stc_read(struct file *file, char *buf, size_t count,
1013 loff_t * offset)
1014{
1015 int tc = count;
1016
1017 if ((tc + *offset) > 8192)
1018 tc = 8192 - *offset;
1019
1020 if (tc < 0)
1021 return 0;
1022
1023 if (copy_to_user(buf, stc_firmware + *offset, tc))
1024 return -EFAULT;
1025
1026 *offset += tc;
1027
1028 return tc;
1029}
1030
1031static int stc_release(struct inode *inode, struct file *file)
1032{
1033 return 0;
1034}
1035
1036static struct file_operations stc_fops = {
1037 .owner = THIS_MODULE,
1038 .read = stc_read,
1039 .open = stc_open,
1040 .release = stc_release,
1041};
1042#endif
1043
1044static u32 functionality(struct i2c_adapter *adapter)
1045{
1046 return I2C_FUNC_I2C;
1047}
1048
1049
1050
1051static int alps_tdmb7_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1052{
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;
1057
1058 div = (params->frequency + 36166667) / 166667;
1059
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;
1064
1065 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
1066 return 0;
1067}
1068
Johannes Stezenbachd91b7302005-05-16 21:54:38 -07001069static struct cx22700_config alps_tdmb7_config = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 .demod_address = 0x43,
1071 .pll_set = alps_tdmb7_pll_set,
1072};
1073
1074
1075
1076
1077
1078static int philips_tdm1316l_pll_init(struct dvb_frontend* fe)
1079{
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) };
1084
1085 // setup PLL configuration
1086 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1087 msleep(1);
1088
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);
1095 }
1096
1097 return 0;
1098}
1099
1100static int philips_tdm1316l_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1101{
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;
1107
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;
1121
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;
1128
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;
1135
1136 case BANDWIDTH_7_MHZ:
1137 tda1004x_write_byte(fe, 0x0C, 0);
1138 filter = 0;
1139 break;
1140
1141 case BANDWIDTH_8_MHZ:
1142 tda1004x_write_byte(fe, 0x0C, 0xFF);
1143 filter = 1;
1144 break;
1145
1146 default:
1147 return -EINVAL;
1148 }
1149
1150 // calculate divisor
1151 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1152 tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
1153
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;
1159
1160 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1161 return -EIO;
1162
1163 msleep(1);
1164 return 0;
1165}
1166
1167static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1168{
1169 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1170
1171 return request_firmware(fw, name, &ttusb->dev->dev);
1172}
1173
1174static struct tda1004x_config philips_tdm1316l_config = {
1175
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,
1182};
1183
1184static u8 alps_bsbe1_inittab[] = {
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08001185 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, 0xb9,
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224};
1225
1226static 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,
Oliver Endriss7f44dcd2005-11-08 21:35:44 -08001241 0x12, 0xb9,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 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
1266};
1267
1268static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1269{
1270 u8 aclk = 0;
1271 u8 bclk = 0;
1272
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;
1291 }
1292
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);
1298
1299 return 0;
1300}
1301
Andreas Oberrittercfbfce12005-09-09 13:02:30 -07001302static int philips_tsa5059_pll_set(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct dvb_frontend_parameters *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
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) };
1308
1309 if ((params->frequency < 950000) || (params->frequency > 2150000))
1310 return -EINVAL;
1311
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;
1317
1318 if (params->frequency > 1530000)
1319 buf[3] = 0xC0;
1320
1321 /* BSBE1 wants XCE bit set */
1322 if (ttusb->revision == TTUSB_REV_2_2)
1323 buf[3] |= 0x20;
1324
Andreas Oberrittercfbfce12005-09-09 13:02:30 -07001325 if (i2c_transfer(i2c, &msg, 1) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return -EIO;
1327
1328 return 0;
1329}
1330
1331static struct stv0299_config alps_stv0299_config = {
1332 .demod_address = 0x68,
1333 .inittab = alps_bsru6_inittab,
1334 .mclk = 88000000UL,
1335 .invert = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 .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,
1342};
1343
1344static int ttusb_novas_grundig_29504_491_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1345{
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) };
1350
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08001351 div = params->frequency / 125;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 buf[0] = (div >> 8) & 0x7f;
1354 buf[1] = div & 0xff;
1355 buf[2] = 0x8e;
1356 buf[3] = 0x00;
1357
1358 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1359 return -EIO;
1360
1361 return 0;
1362}
1363
1364static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1365
1366 .demod_address = 0x68,
1367 .pll_set = ttusb_novas_grundig_29504_491_pll_set,
1368};
1369
Gavin Hamill53936392005-07-07 17:58:04 -07001370static int alps_tdbe2_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1371{
1372 struct ttusb* ttusb = fe->dvb->priv;
1373 u32 div;
1374 u8 data[4];
1375 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1376
1377 div = (params->frequency + 35937500 + 31250) / 62500;
1378
1379 data[0] = (div >> 8) & 0x7f;
1380 data[1] = div & 0xff;
1381 data[2] = 0x85 | ((div >> 10) & 0x60);
1382 data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
1383
1384 if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1385 return -EIO;
1386
1387 return 0;
1388}
1389
1390
1391static struct ves1820_config alps_tdbe2_config = {
1392 .demod_address = 0x09,
1393 .xin = 57840000UL,
1394 .invert = 1,
1395 .selagc = VES1820_SELAGC_SIGNAMPERR,
1396 .pll_set = alps_tdbe2_pll_set,
1397};
1398
1399static u8 read_pwm(struct ttusb* ttusb)
1400{
1401 u8 b = 0xff;
1402 u8 pwm;
1403 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1404 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1405
1406 if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1407 pwm = 0x48;
1408
1409 return pwm;
1410}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412
1413static void frontend_init(struct ttusb* ttusb)
1414{
1415 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1416 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1417 // try the stv0299 based first
1418 ttusb->fe = stv0299_attach(&alps_stv0299_config, &ttusb->i2c_adap);
1419 if (ttusb->fe != NULL) {
1420 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1421 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1422 ttusb->fe->ops->set_voltage = lnbp21_set_voltage;
1423 } else { // ALPS BSRU6
1424 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1425 }
1426 break;
1427 }
1428
1429 // Grundig 29504-491
1430 ttusb->fe = tda8083_attach(&ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1431 if (ttusb->fe != NULL) {
1432 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1433 break;
1434 }
1435
1436 break;
1437
Gavin Hamill53936392005-07-07 17:58:04 -07001438 case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
1439 ttusb->fe = ves1820_attach(&alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
1440 if (ttusb->fe != NULL)
1441 break;
1442 break;
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1445 // try the ALPS TDMB7 first
1446 ttusb->fe = cx22700_attach(&alps_tdmb7_config, &ttusb->i2c_adap);
1447 if (ttusb->fe != NULL)
1448 break;
1449
1450 // Philips td1316
1451 ttusb->fe = tda10046_attach(&philips_tdm1316l_config, &ttusb->i2c_adap);
1452 if (ttusb->fe != NULL)
1453 break;
1454 break;
1455 }
1456
1457 if (ttusb->fe == NULL) {
1458 printk("dvb-ttusb-budget: A frontend driver was not found for device %04x/%04x\n",
1459 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1460 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1461 } else {
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001462 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 printk("dvb-ttusb-budget: Frontend registration failed!\n");
1464 if (ttusb->fe->ops->release)
1465 ttusb->fe->ops->release(ttusb->fe);
1466 ttusb->fe = NULL;
1467 }
1468 }
1469}
1470
1471
1472
1473static struct i2c_algorithm ttusb_dec_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 .master_xfer = master_xfer,
1475 .functionality = functionality,
1476};
1477
1478static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1479{
1480 struct usb_device *udev;
1481 struct ttusb *ttusb;
1482 int result;
1483
1484 dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1485
1486 udev = interface_to_usbdev(intf);
1487
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08001488 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Panagiotis Issaris74081872006-01-11 19:40:56 -02001490 if (!(ttusb = kzalloc(sizeof(struct ttusb), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -ENOMEM;
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 ttusb->dev = udev;
1494 ttusb->c = 0;
1495 ttusb->mux_state = 0;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001496 mutex_init(&ttusb->semi2c);
1497
1498 mutex_lock(&ttusb->semi2c);
1499
1500 mutex_init(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 ttusb_setup_interfaces(ttusb);
1503
1504 ttusb_alloc_iso_urbs(ttusb);
1505 if (ttusb_init_controller(ttusb))
1506 printk("ttusb_init_controller: error\n");
1507
Ingo Molnar3593cab2006-02-07 06:49:14 -02001508 mutex_unlock(&ttusb->semi2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001511 ttusb->adapter.priv = ttusb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /* i2c */
1514 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1515 strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1516
1517 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1518
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 result = i2c_add_adapter(&ttusb->i2c_adap);
1528 if (result) {
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001529 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return result;
1531 }
1532
1533 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1534
1535 ttusb->dvb_demux.dmx.capabilities =
1536 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1537 ttusb->dvb_demux.priv = NULL;
1538#ifdef TTUSB_HWSECTIONS
1539 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1540#else
1541 ttusb->dvb_demux.filternum = 32;
1542#endif
1543 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1544 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1545 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1546 ttusb->dvb_demux.write_to_decoder = NULL;
1547
1548 if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1549 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1550 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001551 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return -ENODEV;
1553 }
1554//FIXME dmxdev (nur WAS?)
1555 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1556 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1557 ttusb->dmxdev.capabilities = 0;
1558
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001559 if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1561 result);
1562 dvb_dmx_release(&ttusb->dvb_demux);
1563 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001564 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return -ENODEV;
1566 }
1567
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001568 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 printk("ttusb_dvb: dvb_net_init failed!\n");
1570 dvb_dmxdev_release(&ttusb->dmxdev);
1571 dvb_dmx_release(&ttusb->dvb_demux);
1572 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001573 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return -ENODEV;
1575 }
1576
1577#if 0
1578 ttusb->stc_devfs_handle =
1579 devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
1580 DEVFS_FL_DEFAULT, 0, 192,
1581 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1582 | S_IROTH | S_IWOTH, &stc_fops, ttusb);
1583#endif
1584 usb_set_intfdata(intf, (void *) ttusb);
1585
1586 frontend_init(ttusb);
1587
1588 return 0;
1589}
1590
1591static void ttusb_disconnect(struct usb_interface *intf)
1592{
1593 struct ttusb *ttusb = usb_get_intfdata(intf);
1594
1595 usb_set_intfdata(intf, NULL);
1596
1597 ttusb->disconnecting = 1;
1598
1599 ttusb_stop_iso_xfer(ttusb);
1600
1601 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1602 dvb_net_release(&ttusb->dvbnet);
1603 dvb_dmxdev_release(&ttusb->dmxdev);
1604 dvb_dmx_release(&ttusb->dvb_demux);
1605 if (ttusb->fe != NULL) dvb_unregister_frontend(ttusb->fe);
1606 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001607 dvb_unregister_adapter(&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 ttusb_free_iso_urbs(ttusb);
1610
1611 kfree(ttusb);
1612
1613 dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1614}
1615
1616static struct usb_device_id ttusb_table[] = {
1617 {USB_DEVICE(0xb48, 0x1003)},
Gavin Hamill53936392005-07-07 17:58:04 -07001618 {USB_DEVICE(0xb48, 0x1004)},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 {USB_DEVICE(0xb48, 0x1005)},
1620 {}
1621};
1622
1623MODULE_DEVICE_TABLE(usb, ttusb_table);
1624
1625static struct usb_driver ttusb_driver = {
Julian Scheel27b05fd2005-07-12 13:58:39 -07001626 .name = "ttusb",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 .probe = ttusb_probe,
1628 .disconnect = ttusb_disconnect,
1629 .id_table = ttusb_table,
1630};
1631
1632static int __init ttusb_init(void)
1633{
1634 int err;
1635
1636 if ((err = usb_register(&ttusb_driver)) < 0) {
1637 printk("%s: usb_register failed! Error number %d",
1638 __FILE__, err);
1639 return err;
1640 }
1641
1642 return 0;
1643}
1644
1645static void __exit ttusb_exit(void)
1646{
1647 usb_deregister(&ttusb_driver);
1648}
1649
1650module_init(ttusb_init);
1651module_exit(ttusb_exit);
1652
1653MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1654MODULE_DESCRIPTION("TTUSB DVB Driver");
1655MODULE_LICENSE("GPL");