]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/dvb/ttusb-budget/dvb-ttusb-budget.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[net-next-2.6.git] / drivers / media / dvb / ttusb-budget / dvb-ttusb-budget.c
CommitLineData
1da177e4
LT
1/*
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>
a8a89b7f 15#include <linux/fs.h>
1da177e4 16#include <linux/module.h>
1da177e4
LT
17#include <linux/usb.h>
18#include <linux/delay.h>
19#include <linux/time.h>
20#include <linux/errno.h>
4da006c6 21#include <linux/jiffies.h>
3593cab5 22#include <linux/mutex.h>
0a2a736a 23#include <linux/firmware.h>
1da177e4
LT
24
25#include "dvb_frontend.h"
26#include "dmxdev.h"
27#include "dvb_demux.h"
28#include "dvb_net.h"
53936391 29#include "ves1820.h"
1da177e4
LT
30#include "cx22700.h"
31#include "tda1004x.h"
32#include "stv0299.h"
33#include "tda8083.h"
b8d4c235 34#include "stv0297.h"
d020542f 35#include "lnbp21.h"
1da177e4
LT
36
37#include <linux/dvb/frontend.h>
38#include <linux/dvb/dmx.h>
39#include <linux/pci.h>
40
1da177e4
LT
41/*
42 TTUSB_HWSECTIONS:
43 the DSP supports filtering in hardware, however, since the "muxstream"
44 is a bit braindead (no matching channel masks or no matching filter mask),
45 we won't support this - yet. it doesn't event support negative filters,
46 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
9aaeded7 47 parse TS data. USB bandwidth will be a problem when having large
1da177e4
LT
48 datastreams, especially for dvb-net, but hey, that's not my problem.
49
50 TTUSB_DISEQC, TTUSB_TONE:
51 let the STC do the diseqc/tone stuff. this isn't supported at least with
52 my TTUSB, so let it undef'd unless you want to implement another
53 frontend. never tested.
54
55 DEBUG:
56 define it to > 3 for really hardcore debugging. you probably don't want
57 this unless the device doesn't load at all. > 2 for bandwidth statistics.
58*/
59
60static int debug;
1da177e4
LT
61module_param(debug, int, 0644);
62MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
63
78e92006
JG
64DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
65
1da177e4
LT
66#define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
67
68#define ISO_BUF_COUNT 4
69#define FRAMES_PER_ISO_BUF 4
70#define ISO_FRAME_SIZE 912
71#define TTUSB_MAXCHANNEL 32
72#ifdef TTUSB_HWSECTIONS
73#define TTUSB_MAXFILTER 16 /* ??? */
74#endif
75
76#define TTUSB_REV_2_2 0x22
77#define TTUSB_BUDGET_NAME "ttusb_stc_fw"
78
79/**
80 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
81 * the dvb_demux field must be the first in struct!!
82 */
83struct ttusb {
84 struct dvb_demux dvb_demux;
85 struct dmxdev dmxdev;
86 struct dvb_net dvbnet;
87
88 /* and one for USB access. */
3593cab5
IM
89 struct mutex semi2c;
90 struct mutex semusb;
1da177e4 91
fdc53a6d 92 struct dvb_adapter adapter;
1da177e4
LT
93 struct usb_device *dev;
94
95 struct i2c_adapter i2c_adap;
96
97 int disconnecting;
98 int iso_streaming;
99
100 unsigned int bulk_out_pipe;
101 unsigned int bulk_in_pipe;
102 unsigned int isoc_in_pipe;
103
104 void *iso_buffer;
105 dma_addr_t iso_dma_handle;
106
107 struct urb *iso_urb[ISO_BUF_COUNT];
108
109 int running_feed_count;
110 int last_channel;
111 int last_filter;
112
113 u8 c; /* transaction counter, wraps around... */
114 fe_sec_tone_mode_t tone;
115 fe_sec_voltage_t voltage;
116
117 int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
118 u8 mux_npacks;
119 u8 muxpack[256 + 8];
120 int muxpack_ptr, muxpack_len;
121
122 int insync;
123
124 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
125 /* (including stuffing. yes. really.) */
126
127 u8 last_result[32];
128
129 int revision;
130
1da177e4
LT
131 struct dvb_frontend* fe;
132};
133
3a4fa0a2 134/* ugly workaround ... don't know why it's necessary to read */
1da177e4
LT
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
3593cab5 152 if (mutex_lock_interruptible(&ttusb->semusb) < 0)
1da177e4
LT
153 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",
fb9393b5 159 __func__, err);
3593cab5 160 mutex_unlock(&ttusb->semusb);
1da177e4
LT
161 return err;
162 }
163 if (actual_len != len) {
fb9393b5 164 dprintk("%s: only wrote %d of %d bytes\n", __func__,
1da177e4 165 actual_len, len);
3593cab5 166 mutex_unlock(&ttusb->semusb);
1da177e4
LT
167 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) {
fb9393b5 174 printk("%s: failed, receive error %d\n", __func__,
1da177e4 175 err);
3593cab5 176 mutex_unlock(&ttusb->semusb);
1da177e4
LT
177 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)
3593cab5 187 mutex_unlock(&ttusb->semusb);
1da177e4
LT
188 return 0;
189}
190
191static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
192{
193 memcpy(data, ttusb->last_result, len);
3593cab5 194 mutex_unlock(&ttusb->semusb);
1da177e4
LT
195 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
9101e622
MCC
227 /* check if the i2c transaction was successful */
228 if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
1da177e4
LT
229
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 == ",
fb9393b5 235 __func__, err, id);
1da177e4
LT
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
3593cab5 252 if (mutex_lock_interruptible(&ttusb->semi2c) < 0)
1da177e4
LT
253 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) {
fb9393b5 279 dprintk("%s: i == %i\n", __func__, i);
1da177e4
LT
280 break;
281 }
282
283 i += inc;
284 }
285
3593cab5 286 mutex_unlock(&ttusb->semi2c);
1da177e4
LT
287 return i;
288}
289
1da177e4
LT
290static int ttusb_boot_dsp(struct ttusb *ttusb)
291{
0a2a736a 292 const struct firmware *fw;
1da177e4
LT
293 int i, err;
294 u8 b[40];
295
0a2a736a
DW
296 err = request_firmware(&fw, "ttusb-budget/dspbootcode.bin",
297 &ttusb->dev->dev);
298 if (err) {
299 printk(KERN_ERR "ttusb-budget: failed to request firmware\n");
300 return err;
301 }
302
1da177e4
LT
303 /* BootBlock */
304 b[0] = 0xaa;
305 b[2] = 0x13;
306 b[3] = 28;
307
308 /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
309 /* 32 is max packet size, no messages should be splitted. */
0a2a736a
DW
310 for (i = 0; i < fw->size; i += 28) {
311 memcpy(&b[4], &fw->data[i], 28);
1da177e4
LT
312
313 b[1] = ++ttusb->c;
314
315 err = ttusb_cmd(ttusb, b, 32, 0);
316 if (err)
317 goto done;
318 }
319
320 /* last block ... */
321 b[1] = ++ttusb->c;
322 b[2] = 0x13;
323 b[3] = 0;
324
325 err = ttusb_cmd(ttusb, b, 4, 0);
326 if (err)
327 goto done;
328
329 /* BootEnd */
330 b[1] = ++ttusb->c;
331 b[2] = 0x14;
332 b[3] = 0;
333
334 err = ttusb_cmd(ttusb, b, 4, 0);
335
336 done:
337 if (err) {
338 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
fb9393b5 339 __func__, err);
1da177e4
LT
340 }
341
342 return err;
343}
344
345static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
346 int pid)
347{
348 int err;
349 /* SetChannel */
350 u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
351 (pid >> 8) & 0xff, pid & 0xff
352 };
353
354 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
355 return err;
356}
357
358static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
359{
360 int err;
361 /* DelChannel */
362 u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
363
364 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
365 return err;
366}
367
368#ifdef TTUSB_HWSECTIONS
369static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
370 int associated_chan, u8 filter[8], u8 mask[8])
371{
372 int err;
373 /* SetFilter */
374 u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
375 filter[0], filter[1], filter[2], filter[3],
376 filter[4], filter[5], filter[6], filter[7],
377 filter[8], filter[9], filter[10], filter[11],
378 mask[0], mask[1], mask[2], mask[3],
379 mask[4], mask[5], mask[6], mask[7],
380 mask[8], mask[9], mask[10], mask[11]
381 };
382
383 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
384 return err;
385}
386
387static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
388{
389 int err;
390 /* DelFilter */
391 u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
392
393 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
394 return err;
395}
396#endif
397
398static int ttusb_init_controller(struct ttusb *ttusb)
399{
400 u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
401 u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
402 u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
403 /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
404 u8 b3[] =
405 { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
406 u8 b4[] =
407 { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
408
409 u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
410 u8 get_dsp_version[0x20] =
411 { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
412 int err;
413
414 /* reset board */
415 if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
416 return err;
417
418 /* reset board (again?) */
419 if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
420 return err;
421
422 ttusb_boot_dsp(ttusb);
423
424 /* set i2c bit rate */
425 if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
426 return err;
427
428 if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
429 return err;
430
431 err = ttusb_result(ttusb, b4, sizeof(b4));
432
433 if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
434 return err;
435
436 if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
437 return err;
438
fb9393b5 439 dprintk("%s: stc-version: %c%c%c%c%c\n", __func__,
1da177e4
LT
440 get_version[4], get_version[5], get_version[6],
441 get_version[7], get_version[8]);
442
443 if (memcmp(get_version + 4, "V 0.0", 5) &&
444 memcmp(get_version + 4, "V 1.1", 5) &&
445 memcmp(get_version + 4, "V 2.1", 5) &&
446 memcmp(get_version + 4, "V 2.2", 5)) {
447 printk
448 ("%s: unknown STC version %c%c%c%c%c, please report!\n",
fb9393b5 449 __func__, get_version[4], get_version[5],
1da177e4
LT
450 get_version[6], get_version[7], get_version[8]);
451 }
452
453 ttusb->revision = ((get_version[6] - '0') << 4) |
454 (get_version[8] - '0');
455
456 err =
457 ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
458 if (err)
459 return err;
460
461 err =
462 ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
463 if (err)
464 return err;
fb9393b5 465 printk("%s: dsp-version: %c%c%c\n", __func__,
1da177e4
LT
466 get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
467 return 0;
468}
469
470#ifdef TTUSB_DISEQC
471static int ttusb_send_diseqc(struct dvb_frontend* fe,
472 const struct dvb_diseqc_master_cmd *cmd)
473{
474 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
475 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
476
477 int err;
478
479 b[3] = 4 + 2 + cmd->msg_len;
480 b[4] = 0xFF; /* send diseqc master, not burst */
481 b[5] = cmd->msg_len;
482
483 memcpy(b + 5, cmd->msg, cmd->msg_len);
484
485 /* Diseqc */
486 if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
487 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
fb9393b5 488 __func__, err);
1da177e4
LT
489 }
490
491 return err;
492}
493#endif
494
1da177e4
LT
495static int ttusb_update_lnb(struct ttusb *ttusb)
496{
497 u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
498 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
499 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
500 };
501 int err;
502
503 /* SetLNB */
504 if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
505 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
fb9393b5 506 __func__, err);
1da177e4
LT
507 }
508
509 return err;
510}
511
512static int ttusb_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
513{
514 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
515
516 ttusb->voltage = voltage;
517 return ttusb_update_lnb(ttusb);
518}
519
520#ifdef TTUSB_TONE
521static int ttusb_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
522{
523 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
524
525 ttusb->tone = tone;
526 return ttusb_update_lnb(ttusb);
527}
528#endif
529
530
531#if 0
532static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
533{
534 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
535 int err, actual_len;
536
537 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
538 if (err) {
539 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
fb9393b5 540 __func__, err);
1da177e4
LT
541 }
542}
543#endif
544
545/*****************************************************************************/
546
547#ifdef TTUSB_HWSECTIONS
548static void ttusb_handle_ts_data(struct ttusb_channel *channel,
549 const u8 * data, int len);
550static void ttusb_handle_sec_data(struct ttusb_channel *channel,
551 const u8 * data, int len);
552#endif
553
ff699e6b 554static int numpkt, numts, numstuff, numsec, numinvalid;
4da006c6 555static unsigned long lastj;
1da177e4
LT
556
557static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
558 int len)
559{
560 u16 csum = 0, cc;
561 int i;
562 for (i = 0; i < len; i += 2)
d4f979a9 563 csum ^= le16_to_cpup((__le16 *) (muxpack + i));
1da177e4
LT
564 if (csum) {
565 printk("%s: muxpack with incorrect checksum, ignoring\n",
fb9393b5 566 __func__);
1da177e4
LT
567 numinvalid++;
568 return;
569 }
570
571 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
572 cc &= 0x7FFF;
573 if ((cc != ttusb->cc) && (ttusb->cc != -1))
574 printk("%s: cc discontinuity (%d frames missing)\n",
fb9393b5 575 __func__, (cc - ttusb->cc) & 0x7FFF);
1da177e4
LT
576 ttusb->cc = (cc + 1) & 0x7FFF;
577 if (muxpack[0] & 0x80) {
578#ifdef TTUSB_HWSECTIONS
579 /* section data */
580 int pusi = muxpack[0] & 0x40;
581 int channel = muxpack[0] & 0x1F;
582 int payload = muxpack[1];
583 const u8 *data = muxpack + 2;
584 /* check offset flag */
585 if (muxpack[0] & 0x20)
586 data++;
587
588 ttusb_handle_sec_data(ttusb->channel + channel, data,
589 payload);
590 data += payload;
591
592 if ((!!(ttusb->muxpack[0] & 0x20)) ^
593 !!(ttusb->muxpack[1] & 1))
594 data++;
595#warning TODO: pusi
596 printk("cc: %04x\n", (data[0] << 8) | data[1]);
597#endif
598 numsec++;
599 } else if (muxpack[0] == 0x47) {
600#ifdef TTUSB_HWSECTIONS
601 /* we have TS data here! */
602 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
603 int channel;
604 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
605 if (ttusb->channel[channel].active
606 && (pid == ttusb->channel[channel].pid))
607 ttusb_handle_ts_data(ttusb->channel +
608 channel, muxpack,
609 188);
610#endif
611 numts++;
612 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
613 } else if (muxpack[0] != 0) {
614 numinvalid++;
615 printk("illegal muxpack type %02x\n", muxpack[0]);
616 } else
617 numstuff++;
618}
619
620static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
621{
622 int maxwork = 1024;
623 while (len) {
624 if (!(maxwork--)) {
fb9393b5 625 printk("%s: too much work\n", __func__);
1da177e4
LT
626 break;
627 }
628
629 switch (ttusb->mux_state) {
630 case 0:
631 case 1:
632 case 2:
633 len--;
634 if (*data++ == 0xAA)
635 ++ttusb->mux_state;
636 else {
637 ttusb->mux_state = 0;
638#if DEBUG > 3
639 if (ttusb->insync)
640 printk("%02x ", data[-1]);
641#else
642 if (ttusb->insync) {
643 printk("%s: lost sync.\n",
fb9393b5 644 __func__);
1da177e4
LT
645 ttusb->insync = 0;
646 }
647#endif
648 }
649 break;
650 case 3:
651 ttusb->insync = 1;
652 len--;
653 ttusb->mux_npacks = *data++;
654 ++ttusb->mux_state;
655 ttusb->muxpack_ptr = 0;
656 /* maximum bytes, until we know the length */
657 ttusb->muxpack_len = 2;
658 break;
659 case 4:
660 {
661 int avail;
662 avail = len;
663 if (avail >
664 (ttusb->muxpack_len -
665 ttusb->muxpack_ptr))
666 avail =
667 ttusb->muxpack_len -
668 ttusb->muxpack_ptr;
669 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
670 data, avail);
671 ttusb->muxpack_ptr += avail;
ae24601b 672 BUG_ON(ttusb->muxpack_ptr > 264);
1da177e4
LT
673 data += avail;
674 len -= avail;
675 /* determine length */
676 if (ttusb->muxpack_ptr == 2) {
677 if (ttusb->muxpack[0] & 0x80) {
678 ttusb->muxpack_len =
679 ttusb->muxpack[1] + 2;
680 if (ttusb->
681 muxpack[0] & 0x20)
682 ttusb->
683 muxpack_len++;
684 if ((!!
685 (ttusb->
686 muxpack[0] & 0x20)) ^
687 !!(ttusb->
688 muxpack[1] & 1))
689 ttusb->
690 muxpack_len++;
691 ttusb->muxpack_len += 4;
692 } else if (ttusb->muxpack[0] ==
693 0x47)
694 ttusb->muxpack_len =
695 188 + 4;
696 else if (ttusb->muxpack[0] == 0x00)
697 ttusb->muxpack_len =
698 ttusb->muxpack[1] + 2 +
699 4;
700 else {
701 dprintk
702 ("%s: invalid state: first byte is %x\n",
fb9393b5 703 __func__,
1da177e4
LT
704 ttusb->muxpack[0]);
705 ttusb->mux_state = 0;
706 }
707 }
708
709 /**
710 * if length is valid and we reached the end:
711 * goto next muxpack
712 */
713 if ((ttusb->muxpack_ptr >= 2) &&
714 (ttusb->muxpack_ptr ==
715 ttusb->muxpack_len)) {
716 ttusb_process_muxpack(ttusb,
717 ttusb->
718 muxpack,
719 ttusb->
720 muxpack_ptr);
721 ttusb->muxpack_ptr = 0;
722 /* maximum bytes, until we know the length */
723 ttusb->muxpack_len = 2;
724
725 /**
726 * no muxpacks left?
727 * return to search-sync state
728 */
729 if (!ttusb->mux_npacks--) {
730 ttusb->mux_state = 0;
731 break;
732 }
733 }
734 break;
735 }
736 default:
737 BUG();
738 break;
739 }
740 }
741}
742
7d12e780 743static void ttusb_iso_irq(struct urb *urb)
1da177e4
LT
744{
745 struct ttusb *ttusb = urb->context;
746
747 if (!ttusb->iso_streaming)
748 return;
749
750#if 0
751 printk("%s: status %d, errcount == %d, length == %i\n",
fb9393b5 752 __func__,
1da177e4
LT
753 urb->status, urb->error_count, urb->actual_length);
754#endif
755
756 if (!urb->status) {
757 int i;
758 for (i = 0; i < urb->number_of_packets; ++i) {
759 struct usb_iso_packet_descriptor *d;
760 u8 *data;
761 int len;
762 numpkt++;
4da006c6 763 if (time_after_eq(jiffies, lastj + HZ)) {
1da177e4
LT
764#if DEBUG > 2
765 printk
766 ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
767 numpkt * HZ / (jiffies - lastj),
768 numts, numstuff, numsec, numinvalid,
769 numts + numstuff + numsec +
770 numinvalid);
771#endif
772 numts = numstuff = numsec = numinvalid = 0;
773 lastj = jiffies;
774 numpkt = 0;
775 }
776 d = &urb->iso_frame_desc[i];
777 data = urb->transfer_buffer + d->offset;
778 len = d->actual_length;
779 d->actual_length = 0;
780 d->status = 0;
781 ttusb_process_frame(ttusb, data, len);
782 }
783 }
784 usb_submit_urb(urb, GFP_ATOMIC);
785}
786
787static void ttusb_free_iso_urbs(struct ttusb *ttusb)
788{
789 int i;
790
791 for (i = 0; i < ISO_BUF_COUNT; i++)
792 if (ttusb->iso_urb[i])
793 usb_free_urb(ttusb->iso_urb[i]);
794
795 pci_free_consistent(NULL,
796 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
797 ISO_BUF_COUNT, ttusb->iso_buffer,
798 ttusb->iso_dma_handle);
799}
800
801static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
802{
803 int i;
804
805 ttusb->iso_buffer = pci_alloc_consistent(NULL,
806 ISO_FRAME_SIZE *
807 FRAMES_PER_ISO_BUF *
808 ISO_BUF_COUNT,
809 &ttusb->iso_dma_handle);
810
11eb260a
DSL
811 if (!ttusb->iso_buffer) {
812 dprintk("%s: pci_alloc_consistent - not enough memory\n",
813 __func__);
814 return -ENOMEM;
815 }
816
1da177e4
LT
817 memset(ttusb->iso_buffer, 0,
818 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
819
820 for (i = 0; i < ISO_BUF_COUNT; i++) {
821 struct urb *urb;
822
823 if (!
824 (urb =
825 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
826 ttusb_free_iso_urbs(ttusb);
827 return -ENOMEM;
828 }
829
830 ttusb->iso_urb[i] = urb;
831 }
832
833 return 0;
834}
835
836static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
837{
838 int i;
839
840 for (i = 0; i < ISO_BUF_COUNT; i++)
841 usb_kill_urb(ttusb->iso_urb[i]);
842
843 ttusb->iso_streaming = 0;
844}
845
846static int ttusb_start_iso_xfer(struct ttusb *ttusb)
847{
848 int i, j, err, buffer_offset = 0;
849
850 if (ttusb->iso_streaming) {
fb9393b5 851 printk("%s: iso xfer already running!\n", __func__);
1da177e4
LT
852 return 0;
853 }
854
855 ttusb->cc = -1;
856 ttusb->insync = 0;
857 ttusb->mux_state = 0;
858
859 for (i = 0; i < ISO_BUF_COUNT; i++) {
860 int frame_offset = 0;
861 struct urb *urb = ttusb->iso_urb[i];
862
863 urb->dev = ttusb->dev;
864 urb->context = ttusb;
865 urb->complete = ttusb_iso_irq;
866 urb->pipe = ttusb->isoc_in_pipe;
867 urb->transfer_flags = URB_ISO_ASAP;
868 urb->interval = 1;
869 urb->number_of_packets = FRAMES_PER_ISO_BUF;
870 urb->transfer_buffer_length =
871 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
872 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
873 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
874
875 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
876 urb->iso_frame_desc[j].offset = frame_offset;
877 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
878 frame_offset += ISO_FRAME_SIZE;
879 }
880 }
881
882 for (i = 0; i < ISO_BUF_COUNT; i++) {
883 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
884 ttusb_stop_iso_xfer(ttusb);
885 printk
886 ("%s: failed urb submission (%i: err = %i)!\n",
fb9393b5 887 __func__, i, err);
1da177e4
LT
888 return err;
889 }
890 }
891
892 ttusb->iso_streaming = 1;
893
894 return 0;
895}
896
897#ifdef TTUSB_HWSECTIONS
898static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
899 int len)
900{
901 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
902}
903
904static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
905 int len)
906{
907// struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
908#error TODO: handle ugly stuff
909// dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
910}
911#endif
912
913static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
914{
915 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
916 int feed_type = 1;
917
918 dprintk("ttusb_start_feed\n");
919
920 switch (dvbdmxfeed->type) {
921 case DMX_TYPE_TS:
922 break;
923 case DMX_TYPE_SEC:
924 break;
925 default:
926 return -EINVAL;
927 }
928
929 if (dvbdmxfeed->type == DMX_TYPE_TS) {
930 switch (dvbdmxfeed->pes_type) {
931 case DMX_TS_PES_VIDEO:
932 case DMX_TS_PES_AUDIO:
933 case DMX_TS_PES_TELETEXT:
934 case DMX_TS_PES_PCR:
935 case DMX_TS_PES_OTHER:
936 break;
937 default:
938 return -EINVAL;
939 }
940 }
941
942#ifdef TTUSB_HWSECTIONS
943#error TODO: allocate filters
944 if (dvbdmxfeed->type == DMX_TYPE_TS) {
945 feed_type = 1;
946 } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
947 feed_type = 2;
948 }
949#endif
950
951 ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
952
953 if (0 == ttusb->running_feed_count++)
954 ttusb_start_iso_xfer(ttusb);
955
956 return 0;
957}
958
959static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
960{
961 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
962
963 ttusb_del_channel(ttusb, dvbdmxfeed->index);
964
965 if (--ttusb->running_feed_count == 0)
966 ttusb_stop_iso_xfer(ttusb);
967
968 return 0;
969}
970
971static int ttusb_setup_interfaces(struct ttusb *ttusb)
972{
973 usb_set_interface(ttusb->dev, 1, 1);
974
975 ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
976 ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
977 ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
978
979 return 0;
980}
981
982#if 0
983static u8 stc_firmware[8192];
984
985static int stc_open(struct inode *inode, struct file *file)
986{
987 struct ttusb *ttusb = file->private_data;
988 int addr;
989
990 for (addr = 0; addr < 8192; addr += 16) {
991 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
992 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
993 16);
994 }
995
996 return 0;
997}
998
999static ssize_t stc_read(struct file *file, char *buf, size_t count,
a8a89b7f 1000 loff_t *offset)
1da177e4 1001{
a8a89b7f 1002 return simple_read_from_buffer(buf, count, offset, stc_firmware, 8192);
1da177e4
LT
1003}
1004
1005static int stc_release(struct inode *inode, struct file *file)
1006{
1007 return 0;
1008}
1009
27a643b1 1010static const struct file_operations stc_fops = {
1da177e4
LT
1011 .owner = THIS_MODULE,
1012 .read = stc_read,
1013 .open = stc_open,
1014 .release = stc_release,
1015};
1016#endif
1017
1018static u32 functionality(struct i2c_adapter *adapter)
1019{
1020 return I2C_FUNC_I2C;
1021}
1022
1023
1024
651b81be 1025static int alps_tdmb7_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1da177e4
LT
1026{
1027 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1028 u8 data[4];
1029 struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
1030 u32 div;
1031
1032 div = (params->frequency + 36166667) / 166667;
1033
1034 data[0] = (div >> 8) & 0x7f;
1035 data[1] = div & 0xff;
1036 data[2] = ((div >> 10) & 0x60) | 0x85;
1037 data[3] = params->frequency < 592000000 ? 0x40 : 0x80;
1038
dea74869
PB
1039 if (fe->ops.i2c_gate_ctrl)
1040 fe->ops.i2c_gate_ctrl(fe, 1);
1da177e4
LT
1041 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
1042 return 0;
1043}
1044
d91b730d 1045static struct cx22700_config alps_tdmb7_config = {
1da177e4 1046 .demod_address = 0x43,
1da177e4
LT
1047};
1048
1049
1050
1051
1052
651b81be 1053static int philips_tdm1316l_tuner_init(struct dvb_frontend* fe)
1da177e4
LT
1054{
1055 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1056 static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1057 static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1058 struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1059
1060 // setup PLL configuration
dea74869
PB
1061 if (fe->ops.i2c_gate_ctrl)
1062 fe->ops.i2c_gate_ctrl(fe, 1);
1da177e4
LT
1063 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1064 msleep(1);
1065
1066 // disable the mc44BC374c (do not check for errors)
1067 tuner_msg.addr = 0x65;
1068 tuner_msg.buf = disable_mc44BC374c;
1069 tuner_msg.len = sizeof(disable_mc44BC374c);
dea74869
PB
1070 if (fe->ops.i2c_gate_ctrl)
1071 fe->ops.i2c_gate_ctrl(fe, 1);
1da177e4
LT
1072 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1073 i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1074 }
1075
1076 return 0;
1077}
1078
651b81be 1079static int philips_tdm1316l_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1da177e4
LT
1080{
1081 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1082 u8 tuner_buf[4];
1083 struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1084 int tuner_frequency = 0;
1085 u8 band, cp, filter;
1086
1087 // determine charge pump
1088 tuner_frequency = params->frequency + 36130000;
1089 if (tuner_frequency < 87000000) return -EINVAL;
1090 else if (tuner_frequency < 130000000) cp = 3;
1091 else if (tuner_frequency < 160000000) cp = 5;
1092 else if (tuner_frequency < 200000000) cp = 6;
1093 else if (tuner_frequency < 290000000) cp = 3;
1094 else if (tuner_frequency < 420000000) cp = 5;
1095 else if (tuner_frequency < 480000000) cp = 6;
1096 else if (tuner_frequency < 620000000) cp = 3;
1097 else if (tuner_frequency < 830000000) cp = 5;
1098 else if (tuner_frequency < 895000000) cp = 7;
1099 else return -EINVAL;
1100
1101 // determine band
1102 if (params->frequency < 49000000) return -EINVAL;
1103 else if (params->frequency < 159000000) band = 1;
1104 else if (params->frequency < 444000000) band = 2;
1105 else if (params->frequency < 861000000) band = 4;
1106 else return -EINVAL;
1107
1108 // setup PLL filter
1109 switch (params->u.ofdm.bandwidth) {
1110 case BANDWIDTH_6_MHZ:
c10d14d6 1111 tda1004x_writereg(fe, 0x0C, 0);
1da177e4
LT
1112 filter = 0;
1113 break;
1114
1115 case BANDWIDTH_7_MHZ:
c10d14d6 1116 tda1004x_writereg(fe, 0x0C, 0);
1da177e4
LT
1117 filter = 0;
1118 break;
1119
1120 case BANDWIDTH_8_MHZ:
c10d14d6 1121 tda1004x_writereg(fe, 0x0C, 0xFF);
1da177e4
LT
1122 filter = 1;
1123 break;
1124
1125 default:
1126 return -EINVAL;
1127 }
1128
1129 // calculate divisor
1130 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1131 tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
1132
1133 // setup tuner buffer
1134 tuner_buf[0] = tuner_frequency >> 8;
1135 tuner_buf[1] = tuner_frequency & 0xff;
1136 tuner_buf[2] = 0xca;
1137 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1138
dea74869
PB
1139 if (fe->ops.i2c_gate_ctrl)
1140 fe->ops.i2c_gate_ctrl(fe, 1);
1da177e4
LT
1141 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1142 return -EIO;
1143
1144 msleep(1);
1145 return 0;
1146}
1147
1148static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1149{
1150 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1151
1152 return request_firmware(fw, name, &ttusb->dev->dev);
1153}
1154
1155static struct tda1004x_config philips_tdm1316l_config = {
1156
1157 .demod_address = 0x8,
1158 .invert = 1,
1159 .invert_oclk = 0,
1da177e4
LT
1160 .request_firmware = philips_tdm1316l_request_firmware,
1161};
1162
1163static u8 alps_bsbe1_inittab[] = {
9101e622
MCC
1164 0x01, 0x15,
1165 0x02, 0x30,
1166 0x03, 0x00,
1167 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1168 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1169 0x06, 0x40, /* DAC not used, set to high impendance mode */
1170 0x07, 0x00, /* DAC LSB */
1171 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1172 0x09, 0x00, /* FIFO */
1173 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1174 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1175 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1176 0x10, 0x3f, // AGC2 0x3d
1177 0x11, 0x84,
1178 0x12, 0xb9,
1179 0x15, 0xc9, // lock detector threshold
1180 0x16, 0x00,
1181 0x17, 0x00,
1182 0x18, 0x00,
1183 0x19, 0x00,
1184 0x1a, 0x00,
1185 0x1f, 0x50,
1186 0x20, 0x00,
1187 0x21, 0x00,
1188 0x22, 0x00,
1189 0x23, 0x00,
1190 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1191 0x29, 0x1e, // 1/2 threshold
1192 0x2a, 0x14, // 2/3 threshold
1193 0x2b, 0x0f, // 3/4 threshold
1194 0x2c, 0x09, // 5/6 threshold
1195 0x2d, 0x05, // 7/8 threshold
1196 0x2e, 0x01,
1197 0x31, 0x1f, // test all FECs
1198 0x32, 0x19, // viterbi and synchro search
1199 0x33, 0xfc, // rs control
1200 0x34, 0x93, // error control
1201 0x0f, 0x92,
1202 0xff, 0xff
1da177e4
LT
1203};
1204
1205static u8 alps_bsru6_inittab[] = {
1206 0x01, 0x15,
1207 0x02, 0x30,
1208 0x03, 0x00,
1209 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1210 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1211 0x06, 0x40, /* DAC not used, set to high impendance mode */
1212 0x07, 0x00, /* DAC LSB */
1213 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1214 0x09, 0x00, /* FIFO */
1215 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1216 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1217 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1218 0x10, 0x3f, // AGC2 0x3d
1219 0x11, 0x84,
7f44dcda 1220 0x12, 0xb9,
1da177e4
LT
1221 0x15, 0xc9, // lock detector threshold
1222 0x16, 0x00,
1223 0x17, 0x00,
1224 0x18, 0x00,
1225 0x19, 0x00,
1226 0x1a, 0x00,
1227 0x1f, 0x50,
1228 0x20, 0x00,
1229 0x21, 0x00,
1230 0x22, 0x00,
1231 0x23, 0x00,
1232 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1233 0x29, 0x1e, // 1/2 threshold
1234 0x2a, 0x14, // 2/3 threshold
1235 0x2b, 0x0f, // 3/4 threshold
1236 0x2c, 0x09, // 5/6 threshold
1237 0x2d, 0x05, // 7/8 threshold
1238 0x2e, 0x01,
1239 0x31, 0x1f, // test all FECs
1240 0x32, 0x19, // viterbi and synchro search
1241 0x33, 0xfc, // rs control
1242 0x34, 0x93, // error control
1243 0x0f, 0x52,
1244 0xff, 0xff
1245};
1246
1247static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1248{
1249 u8 aclk = 0;
1250 u8 bclk = 0;
1251
1252 if (srate < 1500000) {
1253 aclk = 0xb7;
1254 bclk = 0x47;
1255 } else if (srate < 3000000) {
1256 aclk = 0xb7;
1257 bclk = 0x4b;
1258 } else if (srate < 7000000) {
1259 aclk = 0xb7;
1260 bclk = 0x4f;
1261 } else if (srate < 14000000) {
1262 aclk = 0xb7;
1263 bclk = 0x53;
1264 } else if (srate < 30000000) {
1265 aclk = 0xb6;
1266 bclk = 0x53;
1267 } else if (srate < 45000000) {
1268 aclk = 0xb4;
1269 bclk = 0x51;
1270 }
1271
1272 stv0299_writereg(fe, 0x13, aclk);
1273 stv0299_writereg(fe, 0x14, bclk);
1274 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1275 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1276 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1277
1278 return 0;
1279}
1280
651b81be 1281static int philips_tsa5059_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1da177e4
LT
1282{
1283 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1284 u8 buf[4];
1285 u32 div;
1286 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1287
1288 if ((params->frequency < 950000) || (params->frequency > 2150000))
1289 return -EINVAL;
1290
1291 div = (params->frequency + (125 - 1)) / 125; // round correctly
1292 buf[0] = (div >> 8) & 0x7f;
1293 buf[1] = div & 0xff;
1294 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1295 buf[3] = 0xC4;
1296
1297 if (params->frequency > 1530000)
1298 buf[3] = 0xC0;
1299
1300 /* BSBE1 wants XCE bit set */
1301 if (ttusb->revision == TTUSB_REV_2_2)
1302 buf[3] |= 0x20;
1303
dea74869
PB
1304 if (fe->ops.i2c_gate_ctrl)
1305 fe->ops.i2c_gate_ctrl(fe, 1);
651b81be 1306 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1da177e4
LT
1307 return -EIO;
1308
1309 return 0;
1310}
1311
1312static struct stv0299_config alps_stv0299_config = {
1313 .demod_address = 0x68,
1314 .inittab = alps_bsru6_inittab,
1315 .mclk = 88000000UL,
1316 .invert = 1,
1da177e4 1317 .skip_reinit = 0,
da2c7f66 1318 .lock_output = STV0299_LOCKOUTPUT_1,
1da177e4
LT
1319 .volt13_op0_op1 = STV0299_VOLT13_OP1,
1320 .min_delay_ms = 100,
1321 .set_symbol_rate = alps_stv0299_set_symbol_rate,
1da177e4
LT
1322};
1323
651b81be 1324static int ttusb_novas_grundig_29504_491_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1da177e4
LT
1325{
1326 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1327 u8 buf[4];
1328 u32 div;
1329 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1330
9101e622 1331 div = params->frequency / 125;
1da177e4
LT
1332
1333 buf[0] = (div >> 8) & 0x7f;
1334 buf[1] = div & 0xff;
1335 buf[2] = 0x8e;
1336 buf[3] = 0x00;
1337
dea74869
PB
1338 if (fe->ops.i2c_gate_ctrl)
1339 fe->ops.i2c_gate_ctrl(fe, 1);
1da177e4
LT
1340 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1341 return -EIO;
1342
1343 return 0;
1344}
1345
1346static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1347
1348 .demod_address = 0x68,
1da177e4
LT
1349};
1350
651b81be 1351static int alps_tdbe2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
53936391
GH
1352{
1353 struct ttusb* ttusb = fe->dvb->priv;
1354 u32 div;
1355 u8 data[4];
1356 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1357
1358 div = (params->frequency + 35937500 + 31250) / 62500;
1359
1360 data[0] = (div >> 8) & 0x7f;
1361 data[1] = div & 0xff;
1362 data[2] = 0x85 | ((div >> 10) & 0x60);
1363 data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
1364
dea74869
PB
1365 if (fe->ops.i2c_gate_ctrl)
1366 fe->ops.i2c_gate_ctrl(fe, 1);
53936391
GH
1367 if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1368 return -EIO;
1369
1370 return 0;
1371}
1372
1373
1374static struct ves1820_config alps_tdbe2_config = {
1375 .demod_address = 0x09,
1376 .xin = 57840000UL,
1377 .invert = 1,
1378 .selagc = VES1820_SELAGC_SIGNAMPERR,
53936391
GH
1379};
1380
1381static u8 read_pwm(struct ttusb* ttusb)
1382{
1383 u8 b = 0xff;
1384 u8 pwm;
1385 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1386 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1387
1388 if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1389 pwm = 0x48;
1390
1391 return pwm;
1392}
1da177e4
LT
1393
1394
b8d4c235
TK
1395static int dvbc_philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1396{
1397 struct ttusb *ttusb = (struct ttusb *) fe->dvb->priv;
1398 u8 tuner_buf[5];
1399 struct i2c_msg tuner_msg = {.addr = 0x60,
1400 .flags = 0,
1401 .buf = tuner_buf,
1402 .len = sizeof(tuner_buf) };
1403 int tuner_frequency = 0;
1404 u8 band, cp, filter;
1405
1406 // determine charge pump
1407 tuner_frequency = params->frequency;
1408 if (tuner_frequency < 87000000) {return -EINVAL;}
1409 else if (tuner_frequency < 130000000) {cp = 3; band = 1;}
1410 else if (tuner_frequency < 160000000) {cp = 5; band = 1;}
1411 else if (tuner_frequency < 200000000) {cp = 6; band = 1;}
1412 else if (tuner_frequency < 290000000) {cp = 3; band = 2;}
1413 else if (tuner_frequency < 420000000) {cp = 5; band = 2;}
1414 else if (tuner_frequency < 480000000) {cp = 6; band = 2;}
1415 else if (tuner_frequency < 620000000) {cp = 3; band = 4;}
1416 else if (tuner_frequency < 830000000) {cp = 5; band = 4;}
1417 else if (tuner_frequency < 895000000) {cp = 7; band = 4;}
1418 else {return -EINVAL;}
1419
1420 // assume PLL filter should always be 8MHz for the moment.
1421 filter = 1;
1422
1423 // calculate divisor
1424 // (Finput + Fif)/Fref; Fif = 36125000 Hz, Fref = 62500 Hz
1425 tuner_frequency = ((params->frequency + 36125000) / 62500);
1426
1427 // setup tuner buffer
1428 tuner_buf[0] = tuner_frequency >> 8;
1429 tuner_buf[1] = tuner_frequency & 0xff;
1430 tuner_buf[2] = 0xc8;
1431 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1432 tuner_buf[4] = 0x80;
1433
dea74869
PB
1434 if (fe->ops.i2c_gate_ctrl)
1435 fe->ops.i2c_gate_ctrl(fe, 1);
b8d4c235
TK
1436 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1437 printk("dvb-ttusb-budget: dvbc_philips_tdm1316l_pll_set Error 1\n");
1438 return -EIO;
1439 }
1440
1441 msleep(50);
1442
dea74869
PB
1443 if (fe->ops.i2c_gate_ctrl)
1444 fe->ops.i2c_gate_ctrl(fe, 1);
b8d4c235
TK
1445 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1446 printk("dvb-ttusb-budget: dvbc_philips_tdm1316l_pll_set Error 2\n");
1447 return -EIO;
1448 }
1449
1450 msleep(1);
1451
1452 return 0;
1453}
1454
1455static u8 dvbc_philips_tdm1316l_inittab[] = {
1456 0x80, 0x21,
1457 0x80, 0x20,
1458 0x81, 0x01,
1459 0x81, 0x00,
1460 0x00, 0x09,
1461 0x01, 0x69,
1462 0x03, 0x00,
1463 0x04, 0x00,
1464 0x07, 0x00,
1465 0x08, 0x00,
1466 0x20, 0x00,
1467 0x21, 0x40,
1468 0x22, 0x00,
1469 0x23, 0x00,
1470 0x24, 0x40,
1471 0x25, 0x88,
1472 0x30, 0xff,
1473 0x31, 0x00,
1474 0x32, 0xff,
1475 0x33, 0x00,
1476 0x34, 0x50,
1477 0x35, 0x7f,
1478 0x36, 0x00,
1479 0x37, 0x20,
1480 0x38, 0x00,
1481 0x40, 0x1c,
1482 0x41, 0xff,
1483 0x42, 0x29,
1484 0x43, 0x20,
1485 0x44, 0xff,
1486 0x45, 0x00,
1487 0x46, 0x00,
1488 0x49, 0x04,
1489 0x4a, 0xff,
1490 0x4b, 0x7f,
1491 0x52, 0x30,
1492 0x55, 0xae,
1493 0x56, 0x47,
1494 0x57, 0xe1,
1495 0x58, 0x3a,
1496 0x5a, 0x1e,
1497 0x5b, 0x34,
1498 0x60, 0x00,
1499 0x63, 0x00,
1500 0x64, 0x00,
1501 0x65, 0x00,
1502 0x66, 0x00,
1503 0x67, 0x00,
1504 0x68, 0x00,
1505 0x69, 0x00,
1506 0x6a, 0x02,
1507 0x6b, 0x00,
1508 0x70, 0xff,
1509 0x71, 0x00,
1510 0x72, 0x00,
1511 0x73, 0x00,
1512 0x74, 0x0c,
1513 0x80, 0x00,
1514 0x81, 0x00,
1515 0x82, 0x00,
1516 0x83, 0x00,
1517 0x84, 0x04,
1518 0x85, 0x80,
1519 0x86, 0x24,
1520 0x87, 0x78,
1521 0x88, 0x00,
1522 0x89, 0x00,
1523 0x90, 0x01,
1524 0x91, 0x01,
1525 0xa0, 0x00,
1526 0xa1, 0x00,
1527 0xa2, 0x00,
1528 0xb0, 0x91,
1529 0xb1, 0x0b,
1530 0xc0, 0x4b,
1531 0xc1, 0x00,
1532 0xc2, 0x00,
1533 0xd0, 0x00,
1534 0xd1, 0x00,
1535 0xd2, 0x00,
1536 0xd3, 0x00,
1537 0xd4, 0x00,
1538 0xd5, 0x00,
1539 0xde, 0x00,
1540 0xdf, 0x00,
1541 0x61, 0x38,
1542 0x62, 0x0a,
1543 0x53, 0x13,
1544 0x59, 0x08,
1545 0x55, 0x00,
1546 0x56, 0x40,
1547 0x57, 0x08,
1548 0x58, 0x3d,
1549 0x88, 0x10,
1550 0xa0, 0x00,
1551 0xa0, 0x00,
1552 0xa0, 0x00,
1553 0xa0, 0x04,
1554 0xff, 0xff,
1555};
1556
1557static struct stv0297_config dvbc_philips_tdm1316l_config = {
1558 .demod_address = 0x1c,
1559 .inittab = dvbc_philips_tdm1316l_inittab,
1560 .invert = 0,
1561};
1562
1da177e4
LT
1563static void frontend_init(struct ttusb* ttusb)
1564{
1565 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1566 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1567 // try the stv0299 based first
2bfe031d 1568 ttusb->fe = dvb_attach(stv0299_attach, &alps_stv0299_config, &ttusb->i2c_adap);
1da177e4 1569 if (ttusb->fe != NULL) {
dea74869 1570 ttusb->fe->ops.tuner_ops.set_params = philips_tsa5059_tuner_set_params;
651b81be 1571
1da177e4
LT
1572 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1573 alps_stv0299_config.inittab = alps_bsbe1_inittab;
2bfe031d 1574 dvb_attach(lnbp21_attach, ttusb->fe, &ttusb->i2c_adap, 0, 0);
1da177e4 1575 } else { // ALPS BSRU6
dea74869 1576 ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1da177e4
LT
1577 }
1578 break;
1579 }
1580
1581 // Grundig 29504-491
2bfe031d 1582 ttusb->fe = dvb_attach(tda8083_attach, &ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1da177e4 1583 if (ttusb->fe != NULL) {
dea74869
PB
1584 ttusb->fe->ops.tuner_ops.set_params = ttusb_novas_grundig_29504_491_tuner_set_params;
1585 ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1da177e4
LT
1586 break;
1587 }
1da177e4
LT
1588 break;
1589
53936391 1590 case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
2bfe031d 1591 ttusb->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
651b81be 1592 if (ttusb->fe != NULL) {
dea74869 1593 ttusb->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
53936391 1594 break;
651b81be 1595 }
b8d4c235 1596
2bfe031d 1597 ttusb->fe = dvb_attach(stv0297_attach, &dvbc_philips_tdm1316l_config, &ttusb->i2c_adap);
b8d4c235 1598 if (ttusb->fe != NULL) {
dea74869 1599 ttusb->fe->ops.tuner_ops.set_params = dvbc_philips_tdm1316l_tuner_set_params;
b8d4c235
TK
1600 break;
1601 }
53936391
GH
1602 break;
1603
1da177e4
LT
1604 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1605 // try the ALPS TDMB7 first
2bfe031d 1606 ttusb->fe = dvb_attach(cx22700_attach, &alps_tdmb7_config, &ttusb->i2c_adap);
651b81be 1607 if (ttusb->fe != NULL) {
dea74869 1608 ttusb->fe->ops.tuner_ops.set_params = alps_tdmb7_tuner_set_params;
1da177e4 1609 break;
651b81be 1610 }
1da177e4
LT
1611
1612 // Philips td1316
2bfe031d 1613 ttusb->fe = dvb_attach(tda10046_attach, &philips_tdm1316l_config, &ttusb->i2c_adap);
651b81be 1614 if (ttusb->fe != NULL) {
dea74869
PB
1615 ttusb->fe->ops.tuner_ops.init = philips_tdm1316l_tuner_init;
1616 ttusb->fe->ops.tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
1da177e4 1617 break;
651b81be 1618 }
1da177e4
LT
1619 break;
1620 }
1621
1622 if (ttusb->fe == NULL) {
29e66a6c 1623 printk("dvb-ttusb-budget: A frontend driver was not found for device [%04x:%04x]\n",
1da177e4
LT
1624 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1625 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1626 } else {
fdc53a6d 1627 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1da177e4 1628 printk("dvb-ttusb-budget: Frontend registration failed!\n");
f52a838b 1629 dvb_frontend_detach(ttusb->fe);
1da177e4
LT
1630 ttusb->fe = NULL;
1631 }
1632 }
1633}
1634
1635
1636
1637static struct i2c_algorithm ttusb_dec_algo = {
1da177e4
LT
1638 .master_xfer = master_xfer,
1639 .functionality = functionality,
1640};
1641
1642static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1643{
1644 struct usb_device *udev;
1645 struct ttusb *ttusb;
1646 int result;
1647
fb9393b5 1648 dprintk("%s: TTUSB DVB connected\n", __func__);
1da177e4
LT
1649
1650 udev = interface_to_usbdev(intf);
1651
9101e622 1652 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1da177e4 1653
7408187d 1654 if (!(ttusb = kzalloc(sizeof(struct ttusb), GFP_KERNEL)))
1da177e4
LT
1655 return -ENOMEM;
1656
1da177e4
LT
1657 ttusb->dev = udev;
1658 ttusb->c = 0;
1659 ttusb->mux_state = 0;
3593cab5
IM
1660 mutex_init(&ttusb->semi2c);
1661
1662 mutex_lock(&ttusb->semi2c);
1663
1664 mutex_init(&ttusb->semusb);
1da177e4
LT
1665
1666 ttusb_setup_interfaces(ttusb);
1667
b7ed785b
DSL
1668 result = ttusb_alloc_iso_urbs(ttusb);
1669 if (result < 0) {
1670 dprintk("%s: ttusb_alloc_iso_urbs - failed\n", __func__);
1671 mutex_unlock(&ttusb->semi2c);
1672 kfree(ttusb);
1673 return result;
1674 }
1675
1da177e4
LT
1676 if (ttusb_init_controller(ttusb))
1677 printk("ttusb_init_controller: error\n");
1678
3593cab5 1679 mutex_unlock(&ttusb->semi2c);
1da177e4 1680
78e92006
JG
1681 result = dvb_register_adapter(&ttusb->adapter,
1682 "Technotrend/Hauppauge Nova-USB",
1683 THIS_MODULE, &udev->dev, adapter_nr);
1684 if (result < 0) {
a064fad3
AQ
1685 ttusb_free_iso_urbs(ttusb);
1686 kfree(ttusb);
1687 return result;
1688 }
fdc53a6d 1689 ttusb->adapter.priv = ttusb;
1da177e4
LT
1690
1691 /* i2c */
1692 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1693 strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1694
1695 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1696
1da177e4
LT
1697 ttusb->i2c_adap.algo = &ttusb_dec_algo;
1698 ttusb->i2c_adap.algo_data = NULL;
12a917f6 1699 ttusb->i2c_adap.dev.parent = &udev->dev;
1da177e4
LT
1700
1701 result = i2c_add_adapter(&ttusb->i2c_adap);
1702 if (result) {
fdc53a6d 1703 dvb_unregister_adapter (&ttusb->adapter);
1da177e4
LT
1704 return result;
1705 }
1706
1707 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1708
1709 ttusb->dvb_demux.dmx.capabilities =
1710 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1711 ttusb->dvb_demux.priv = NULL;
1712#ifdef TTUSB_HWSECTIONS
1713 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1714#else
1715 ttusb->dvb_demux.filternum = 32;
1716#endif
1717 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1718 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1719 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1720 ttusb->dvb_demux.write_to_decoder = NULL;
1721
1722 if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1723 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1724 i2c_del_adapter(&ttusb->i2c_adap);
fdc53a6d 1725 dvb_unregister_adapter (&ttusb->adapter);
1da177e4
LT
1726 return -ENODEV;
1727 }
1728//FIXME dmxdev (nur WAS?)
1729 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1730 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1731 ttusb->dmxdev.capabilities = 0;
1732
fdc53a6d 1733 if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
1da177e4
LT
1734 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1735 result);
1736 dvb_dmx_release(&ttusb->dvb_demux);
1737 i2c_del_adapter(&ttusb->i2c_adap);
fdc53a6d 1738 dvb_unregister_adapter (&ttusb->adapter);
1da177e4
LT
1739 return -ENODEV;
1740 }
1741
fdc53a6d 1742 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1da177e4
LT
1743 printk("ttusb_dvb: dvb_net_init failed!\n");
1744 dvb_dmxdev_release(&ttusb->dmxdev);
1745 dvb_dmx_release(&ttusb->dvb_demux);
1746 i2c_del_adapter(&ttusb->i2c_adap);
fdc53a6d 1747 dvb_unregister_adapter (&ttusb->adapter);
1da177e4
LT
1748 return -ENODEV;
1749 }
1750
1da177e4
LT
1751 usb_set_intfdata(intf, (void *) ttusb);
1752
1753 frontend_init(ttusb);
1754
1755 return 0;
1756}
1757
1758static void ttusb_disconnect(struct usb_interface *intf)
1759{
1760 struct ttusb *ttusb = usb_get_intfdata(intf);
1761
1762 usb_set_intfdata(intf, NULL);
1763
1764 ttusb->disconnecting = 1;
1765
1766 ttusb_stop_iso_xfer(ttusb);
1767
1768 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1769 dvb_net_release(&ttusb->dvbnet);
1770 dvb_dmxdev_release(&ttusb->dmxdev);
1771 dvb_dmx_release(&ttusb->dvb_demux);
2bfe031d
AQ
1772 if (ttusb->fe != NULL) {
1773 dvb_unregister_frontend(ttusb->fe);
f52a838b 1774 dvb_frontend_detach(ttusb->fe);
2bfe031d 1775 }
1da177e4 1776 i2c_del_adapter(&ttusb->i2c_adap);
fdc53a6d 1777 dvb_unregister_adapter(&ttusb->adapter);
1da177e4
LT
1778
1779 ttusb_free_iso_urbs(ttusb);
1780
1781 kfree(ttusb);
1782
fb9393b5 1783 dprintk("%s: TTUSB DVB disconnected\n", __func__);
1da177e4
LT
1784}
1785
1786static struct usb_device_id ttusb_table[] = {
1787 {USB_DEVICE(0xb48, 0x1003)},
53936391 1788 {USB_DEVICE(0xb48, 0x1004)},
1da177e4
LT
1789 {USB_DEVICE(0xb48, 0x1005)},
1790 {}
1791};
1792
1793MODULE_DEVICE_TABLE(usb, ttusb_table);
1794
1795static struct usb_driver ttusb_driver = {
27b05fd2 1796 .name = "ttusb",
1da177e4
LT
1797 .probe = ttusb_probe,
1798 .disconnect = ttusb_disconnect,
1799 .id_table = ttusb_table,
1800};
1801
1802static int __init ttusb_init(void)
1803{
1804 int err;
1805
1806 if ((err = usb_register(&ttusb_driver)) < 0) {
1807 printk("%s: usb_register failed! Error number %d",
1808 __FILE__, err);
1809 return err;
1810 }
1811
1812 return 0;
1813}
1814
1815static void __exit ttusb_exit(void)
1816{
1817 usb_deregister(&ttusb_driver);
1818}
1819
1820module_init(ttusb_init);
1821module_exit(ttusb_exit);
1822
1823MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1824MODULE_DESCRIPTION("TTUSB DVB Driver");
1825MODULE_LICENSE("GPL");
0a2a736a 1826MODULE_FIRMWARE("ttusb-budget/dspbootcode.bin");