blob: cd19ba6281017e9e058fe3a443b968b2ae1bb026 [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
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001051static int alps_tdmb7_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071};
1072
1073
1074
1075
1076
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001077static int philips_tdm1316l_tuner_init(struct dvb_frontend* fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
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) };
1083
1084 // setup PLL configuration
1085 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1086 msleep(1);
1087
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);
1094 }
1095
1096 return 0;
1097}
1098
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001099static int philips_tdm1316l_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
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;
1106
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;
1120
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;
1127
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;
1134
1135 case BANDWIDTH_7_MHZ:
1136 tda1004x_write_byte(fe, 0x0C, 0);
1137 filter = 0;
1138 break;
1139
1140 case BANDWIDTH_8_MHZ:
1141 tda1004x_write_byte(fe, 0x0C, 0xFF);
1142 filter = 1;
1143 break;
1144
1145 default:
1146 return -EINVAL;
1147 }
1148
1149 // calculate divisor
1150 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1151 tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
1152
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;
1158
1159 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1160 return -EIO;
1161
1162 msleep(1);
1163 return 0;
1164}
1165
1166static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1167{
1168 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1169
1170 return request_firmware(fw, name, &ttusb->dev->dev);
1171}
1172
1173static struct tda1004x_config philips_tdm1316l_config = {
1174
1175 .demod_address = 0x8,
1176 .invert = 1,
1177 .invert_oclk = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 .request_firmware = philips_tdm1316l_request_firmware,
1179};
1180
1181static u8 alps_bsbe1_inittab[] = {
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08001182 0x01, 0x15,
1183 0x02, 0x30,
1184 0x03, 0x00,
1185 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1186 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1187 0x06, 0x40, /* DAC not used, set to high impendance mode */
1188 0x07, 0x00, /* DAC LSB */
1189 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1190 0x09, 0x00, /* FIFO */
1191 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1192 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1193 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1194 0x10, 0x3f, // AGC2 0x3d
1195 0x11, 0x84,
1196 0x12, 0xb9,
1197 0x15, 0xc9, // lock detector threshold
1198 0x16, 0x00,
1199 0x17, 0x00,
1200 0x18, 0x00,
1201 0x19, 0x00,
1202 0x1a, 0x00,
1203 0x1f, 0x50,
1204 0x20, 0x00,
1205 0x21, 0x00,
1206 0x22, 0x00,
1207 0x23, 0x00,
1208 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1209 0x29, 0x1e, // 1/2 threshold
1210 0x2a, 0x14, // 2/3 threshold
1211 0x2b, 0x0f, // 3/4 threshold
1212 0x2c, 0x09, // 5/6 threshold
1213 0x2d, 0x05, // 7/8 threshold
1214 0x2e, 0x01,
1215 0x31, 0x1f, // test all FECs
1216 0x32, 0x19, // viterbi and synchro search
1217 0x33, 0xfc, // rs control
1218 0x34, 0x93, // error control
1219 0x0f, 0x92,
1220 0xff, 0xff
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221};
1222
1223static u8 alps_bsru6_inittab[] = {
1224 0x01, 0x15,
1225 0x02, 0x30,
1226 0x03, 0x00,
1227 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1228 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1229 0x06, 0x40, /* DAC not used, set to high impendance mode */
1230 0x07, 0x00, /* DAC LSB */
1231 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1232 0x09, 0x00, /* FIFO */
1233 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1234 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1235 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1236 0x10, 0x3f, // AGC2 0x3d
1237 0x11, 0x84,
Oliver Endriss7f44dcd2005-11-08 21:35:44 -08001238 0x12, 0xb9,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 0x15, 0xc9, // lock detector threshold
1240 0x16, 0x00,
1241 0x17, 0x00,
1242 0x18, 0x00,
1243 0x19, 0x00,
1244 0x1a, 0x00,
1245 0x1f, 0x50,
1246 0x20, 0x00,
1247 0x21, 0x00,
1248 0x22, 0x00,
1249 0x23, 0x00,
1250 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1251 0x29, 0x1e, // 1/2 threshold
1252 0x2a, 0x14, // 2/3 threshold
1253 0x2b, 0x0f, // 3/4 threshold
1254 0x2c, 0x09, // 5/6 threshold
1255 0x2d, 0x05, // 7/8 threshold
1256 0x2e, 0x01,
1257 0x31, 0x1f, // test all FECs
1258 0x32, 0x19, // viterbi and synchro search
1259 0x33, 0xfc, // rs control
1260 0x34, 0x93, // error control
1261 0x0f, 0x52,
1262 0xff, 0xff
1263};
1264
1265static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1266{
1267 u8 aclk = 0;
1268 u8 bclk = 0;
1269
1270 if (srate < 1500000) {
1271 aclk = 0xb7;
1272 bclk = 0x47;
1273 } else if (srate < 3000000) {
1274 aclk = 0xb7;
1275 bclk = 0x4b;
1276 } else if (srate < 7000000) {
1277 aclk = 0xb7;
1278 bclk = 0x4f;
1279 } else if (srate < 14000000) {
1280 aclk = 0xb7;
1281 bclk = 0x53;
1282 } else if (srate < 30000000) {
1283 aclk = 0xb6;
1284 bclk = 0x53;
1285 } else if (srate < 45000000) {
1286 aclk = 0xb4;
1287 bclk = 0x51;
1288 }
1289
1290 stv0299_writereg(fe, 0x13, aclk);
1291 stv0299_writereg(fe, 0x14, bclk);
1292 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1293 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1294 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1295
1296 return 0;
1297}
1298
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001299static int philips_tsa5059_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1302 u8 buf[4];
1303 u32 div;
1304 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1305
1306 if ((params->frequency < 950000) || (params->frequency > 2150000))
1307 return -EINVAL;
1308
1309 div = (params->frequency + (125 - 1)) / 125; // round correctly
1310 buf[0] = (div >> 8) & 0x7f;
1311 buf[1] = div & 0xff;
1312 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1313 buf[3] = 0xC4;
1314
1315 if (params->frequency > 1530000)
1316 buf[3] = 0xC0;
1317
1318 /* BSBE1 wants XCE bit set */
1319 if (ttusb->revision == TTUSB_REV_2_2)
1320 buf[3] |= 0x20;
1321
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001322 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return -EIO;
1324
1325 return 0;
1326}
1327
1328static struct stv0299_config alps_stv0299_config = {
1329 .demod_address = 0x68,
1330 .inittab = alps_bsru6_inittab,
1331 .mclk = 88000000UL,
1332 .invert = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 .skip_reinit = 0,
1334 .lock_output = STV0229_LOCKOUTPUT_1,
1335 .volt13_op0_op1 = STV0299_VOLT13_OP1,
1336 .min_delay_ms = 100,
1337 .set_symbol_rate = alps_stv0299_set_symbol_rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338};
1339
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001340static int ttusb_novas_grundig_29504_491_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1343 u8 buf[4];
1344 u32 div;
1345 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1346
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08001347 div = params->frequency / 125;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 buf[0] = (div >> 8) & 0x7f;
1350 buf[1] = div & 0xff;
1351 buf[2] = 0x8e;
1352 buf[3] = 0x00;
1353
1354 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1355 return -EIO;
1356
1357 return 0;
1358}
1359
1360static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1361
1362 .demod_address = 0x68,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363};
1364
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001365static int alps_tdbe2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
Gavin Hamill53936392005-07-07 17:58:04 -07001366{
1367 struct ttusb* ttusb = fe->dvb->priv;
1368 u32 div;
1369 u8 data[4];
1370 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1371
1372 div = (params->frequency + 35937500 + 31250) / 62500;
1373
1374 data[0] = (div >> 8) & 0x7f;
1375 data[1] = div & 0xff;
1376 data[2] = 0x85 | ((div >> 10) & 0x60);
1377 data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
1378
1379 if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1380 return -EIO;
1381
1382 return 0;
1383}
1384
1385
1386static struct ves1820_config alps_tdbe2_config = {
1387 .demod_address = 0x09,
1388 .xin = 57840000UL,
1389 .invert = 1,
1390 .selagc = VES1820_SELAGC_SIGNAMPERR,
Gavin Hamill53936392005-07-07 17:58:04 -07001391};
1392
1393static u8 read_pwm(struct ttusb* ttusb)
1394{
1395 u8 b = 0xff;
1396 u8 pwm;
1397 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1398 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1399
1400 if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1401 pwm = 0x48;
1402
1403 return pwm;
1404}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406
1407static void frontend_init(struct ttusb* ttusb)
1408{
1409 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1410 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1411 // try the stv0299 based first
1412 ttusb->fe = stv0299_attach(&alps_stv0299_config, &ttusb->i2c_adap);
1413 if (ttusb->fe != NULL) {
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001414 ttusb->fe->ops->tuner_ops.set_params = philips_tsa5059_tuner_set_params;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1417 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1418 ttusb->fe->ops->set_voltage = lnbp21_set_voltage;
1419 } else { // ALPS BSRU6
1420 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1421 }
1422 break;
1423 }
1424
1425 // Grundig 29504-491
1426 ttusb->fe = tda8083_attach(&ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1427 if (ttusb->fe != NULL) {
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001428 ttusb->fe->ops->tuner_ops.set_params = ttusb_novas_grundig_29504_491_tuner_set_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1430 break;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
1433
Gavin Hamill53936392005-07-07 17:58:04 -07001434 case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
1435 ttusb->fe = ves1820_attach(&alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001436 if (ttusb->fe != NULL) {
1437 ttusb->fe->ops->tuner_ops.set_params = alps_tdbe2_tuner_set_params;
Gavin Hamill53936392005-07-07 17:58:04 -07001438 break;
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001439 }
Gavin Hamill53936392005-07-07 17:58:04 -07001440 break;
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1443 // try the ALPS TDMB7 first
1444 ttusb->fe = cx22700_attach(&alps_tdmb7_config, &ttusb->i2c_adap);
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001445 if (ttusb->fe != NULL) {
1446 ttusb->fe->ops->tuner_ops.set_params = alps_tdmb7_tuner_set_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 // Philips td1316
1451 ttusb->fe = tda10046_attach(&philips_tdm1316l_config, &ttusb->i2c_adap);
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001452 if (ttusb->fe != NULL) {
1453 ttusb->fe->ops->tuner_ops.init = philips_tdm1316l_tuner_init;
1454 ttusb->fe->ops->tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 break;
1458 }
1459
1460 if (ttusb->fe == NULL) {
1461 printk("dvb-ttusb-budget: A frontend driver was not found for device %04x/%04x\n",
1462 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1463 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1464 } else {
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001465 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 printk("dvb-ttusb-budget: Frontend registration failed!\n");
1467 if (ttusb->fe->ops->release)
1468 ttusb->fe->ops->release(ttusb->fe);
1469 ttusb->fe = NULL;
1470 }
1471 }
1472}
1473
1474
1475
1476static struct i2c_algorithm ttusb_dec_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 .master_xfer = master_xfer,
1478 .functionality = functionality,
1479};
1480
1481static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1482{
1483 struct usb_device *udev;
1484 struct ttusb *ttusb;
1485 int result;
1486
1487 dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1488
1489 udev = interface_to_usbdev(intf);
1490
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08001491 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Panagiotis Issaris74081872006-01-11 19:40:56 -02001493 if (!(ttusb = kzalloc(sizeof(struct ttusb), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -ENOMEM;
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 ttusb->dev = udev;
1497 ttusb->c = 0;
1498 ttusb->mux_state = 0;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001499 mutex_init(&ttusb->semi2c);
1500
1501 mutex_lock(&ttusb->semi2c);
1502
1503 mutex_init(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 ttusb_setup_interfaces(ttusb);
1506
1507 ttusb_alloc_iso_urbs(ttusb);
1508 if (ttusb_init_controller(ttusb))
1509 printk("ttusb_init_controller: error\n");
1510
Ingo Molnar3593cab2006-02-07 06:49:14 -02001511 mutex_unlock(&ttusb->semi2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -03001513 if ((result = dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE, &udev->dev)) < 0) {
Andrew de Quinceya064fad2006-04-06 17:05:46 -03001514 ttusb_free_iso_urbs(ttusb);
1515 kfree(ttusb);
1516 return result;
1517 }
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001518 ttusb->adapter.priv = ttusb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /* i2c */
1521 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1522 strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1523
1524 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1525
1526#ifdef I2C_ADAP_CLASS_TV_DIGITAL
1527 ttusb->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
1528#else
1529 ttusb->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
1530#endif
1531 ttusb->i2c_adap.algo = &ttusb_dec_algo;
1532 ttusb->i2c_adap.algo_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 result = i2c_add_adapter(&ttusb->i2c_adap);
1535 if (result) {
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001536 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return result;
1538 }
1539
1540 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1541
1542 ttusb->dvb_demux.dmx.capabilities =
1543 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1544 ttusb->dvb_demux.priv = NULL;
1545#ifdef TTUSB_HWSECTIONS
1546 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1547#else
1548 ttusb->dvb_demux.filternum = 32;
1549#endif
1550 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1551 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1552 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1553 ttusb->dvb_demux.write_to_decoder = NULL;
1554
1555 if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1556 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1557 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001558 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -ENODEV;
1560 }
1561//FIXME dmxdev (nur WAS?)
1562 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1563 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1564 ttusb->dmxdev.capabilities = 0;
1565
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001566 if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1568 result);
1569 dvb_dmx_release(&ttusb->dvb_demux);
1570 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001571 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return -ENODEV;
1573 }
1574
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001575 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 printk("ttusb_dvb: dvb_net_init failed!\n");
1577 dvb_dmxdev_release(&ttusb->dmxdev);
1578 dvb_dmx_release(&ttusb->dvb_demux);
1579 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001580 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return -ENODEV;
1582 }
1583
1584#if 0
1585 ttusb->stc_devfs_handle =
1586 devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
1587 DEVFS_FL_DEFAULT, 0, 192,
1588 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1589 | S_IROTH | S_IWOTH, &stc_fops, ttusb);
1590#endif
1591 usb_set_intfdata(intf, (void *) ttusb);
1592
1593 frontend_init(ttusb);
1594
1595 return 0;
1596}
1597
1598static void ttusb_disconnect(struct usb_interface *intf)
1599{
1600 struct ttusb *ttusb = usb_get_intfdata(intf);
1601
1602 usb_set_intfdata(intf, NULL);
1603
1604 ttusb->disconnecting = 1;
1605
1606 ttusb_stop_iso_xfer(ttusb);
1607
1608 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1609 dvb_net_release(&ttusb->dvbnet);
1610 dvb_dmxdev_release(&ttusb->dmxdev);
1611 dvb_dmx_release(&ttusb->dvb_demux);
1612 if (ttusb->fe != NULL) dvb_unregister_frontend(ttusb->fe);
1613 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001614 dvb_unregister_adapter(&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 ttusb_free_iso_urbs(ttusb);
1617
1618 kfree(ttusb);
1619
1620 dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1621}
1622
1623static struct usb_device_id ttusb_table[] = {
1624 {USB_DEVICE(0xb48, 0x1003)},
Gavin Hamill53936392005-07-07 17:58:04 -07001625 {USB_DEVICE(0xb48, 0x1004)},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 {USB_DEVICE(0xb48, 0x1005)},
1627 {}
1628};
1629
1630MODULE_DEVICE_TABLE(usb, ttusb_table);
1631
1632static struct usb_driver ttusb_driver = {
Julian Scheel27b05fd2005-07-12 13:58:39 -07001633 .name = "ttusb",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 .probe = ttusb_probe,
1635 .disconnect = ttusb_disconnect,
1636 .id_table = ttusb_table,
1637};
1638
1639static int __init ttusb_init(void)
1640{
1641 int err;
1642
1643 if ((err = usb_register(&ttusb_driver)) < 0) {
1644 printk("%s: usb_register failed! Error number %d",
1645 __FILE__, err);
1646 return err;
1647 }
1648
1649 return 0;
1650}
1651
1652static void __exit ttusb_exit(void)
1653{
1654 usb_deregister(&ttusb_driver);
1655}
1656
1657module_init(ttusb_init);
1658module_exit(ttusb_exit);
1659
1660MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1661MODULE_DESCRIPTION("TTUSB DVB Driver");
1662MODULE_LICENSE("GPL");