blob: 71581303b0ea891fbb8a2a50fffd5fb578a518da [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"
Andrew de Quinceyd0205422006-04-27 21:45:01 -030033#include "lnbp21.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <linux/dvb/frontend.h>
36#include <linux/dvb/dmx.h>
37#include <linux/pci.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 TTUSB_HWSECTIONS:
41 the DSP supports filtering in hardware, however, since the "muxstream"
42 is a bit braindead (no matching channel masks or no matching filter mask),
43 we won't support this - yet. it doesn't event support negative filters,
44 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
45 parse TS data. USB bandwith will be a problem when having large
46 datastreams, especially for dvb-net, but hey, that's not my problem.
47
48 TTUSB_DISEQC, TTUSB_TONE:
49 let the STC do the diseqc/tone stuff. this isn't supported at least with
50 my TTUSB, so let it undef'd unless you want to implement another
51 frontend. never tested.
52
53 DEBUG:
54 define it to > 3 for really hardcore debugging. you probably don't want
55 this unless the device doesn't load at all. > 2 for bandwidth statistics.
56*/
57
58static int debug;
59
60module_param(debug, int, 0644);
61MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
62
63#define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
64
65#define ISO_BUF_COUNT 4
66#define FRAMES_PER_ISO_BUF 4
67#define ISO_FRAME_SIZE 912
68#define TTUSB_MAXCHANNEL 32
69#ifdef TTUSB_HWSECTIONS
70#define TTUSB_MAXFILTER 16 /* ??? */
71#endif
72
73#define TTUSB_REV_2_2 0x22
74#define TTUSB_BUDGET_NAME "ttusb_stc_fw"
75
76/**
77 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
78 * the dvb_demux field must be the first in struct!!
79 */
80struct ttusb {
81 struct dvb_demux dvb_demux;
82 struct dmxdev dmxdev;
83 struct dvb_net dvbnet;
84
85 /* and one for USB access. */
Ingo Molnar3593cab2006-02-07 06:49:14 -020086 struct mutex semi2c;
87 struct mutex semusb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -070089 struct dvb_adapter adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct usb_device *dev;
91
92 struct i2c_adapter i2c_adap;
93
94 int disconnecting;
95 int iso_streaming;
96
97 unsigned int bulk_out_pipe;
98 unsigned int bulk_in_pipe;
99 unsigned int isoc_in_pipe;
100
101 void *iso_buffer;
102 dma_addr_t iso_dma_handle;
103
104 struct urb *iso_urb[ISO_BUF_COUNT];
105
106 int running_feed_count;
107 int last_channel;
108 int last_filter;
109
110 u8 c; /* transaction counter, wraps around... */
111 fe_sec_tone_mode_t tone;
112 fe_sec_voltage_t voltage;
113
114 int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
115 u8 mux_npacks;
116 u8 muxpack[256 + 8];
117 int muxpack_ptr, muxpack_len;
118
119 int insync;
120
121 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
122 /* (including stuffing. yes. really.) */
123
124 u8 last_result[32];
125
126 int revision;
127
128#if 0
129 devfs_handle_t stc_devfs_handle;
130#endif
131
132 struct dvb_frontend* fe;
133};
134
135/* ugly workaround ... don't know why it's neccessary to read */
136/* all result codes. */
137
138#define DEBUG 0
139static int ttusb_cmd(struct ttusb *ttusb,
140 const u8 * data, int len, int needresult)
141{
142 int actual_len;
143 int err;
144#if DEBUG >= 3
145 int i;
146
147 printk(">");
148 for (i = 0; i < len; ++i)
149 printk(" %02x", data[i]);
150 printk("\n");
151#endif
152
Ingo Molnar3593cab2006-02-07 06:49:14 -0200153 if (mutex_lock_interruptible(&ttusb->semusb) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return -EAGAIN;
155
156 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
157 (u8 *) data, len, &actual_len, 1000);
158 if (err != 0) {
159 dprintk("%s: usb_bulk_msg(send) failed, err == %i!\n",
160 __FUNCTION__, err);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200161 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return err;
163 }
164 if (actual_len != len) {
165 dprintk("%s: only wrote %d of %d bytes\n", __FUNCTION__,
166 actual_len, len);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200167 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -1;
169 }
170
171 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
172 ttusb->last_result, 32, &actual_len, 1000);
173
174 if (err != 0) {
175 printk("%s: failed, receive error %d\n", __FUNCTION__,
176 err);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200177 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return err;
179 }
180#if DEBUG >= 3
181 actual_len = ttusb->last_result[3] + 4;
182 printk("<");
183 for (i = 0; i < actual_len; ++i)
184 printk(" %02x", ttusb->last_result[i]);
185 printk("\n");
186#endif
187 if (!needresult)
Ingo Molnar3593cab2006-02-07 06:49:14 -0200188 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
190}
191
192static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
193{
194 memcpy(data, ttusb->last_result, len);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200195 mutex_unlock(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 0;
197}
198
199static int ttusb_i2c_msg(struct ttusb *ttusb,
200 u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
201 u8 rcv_len)
202{
203 u8 b[0x28];
204 u8 id = ++ttusb->c;
205 int i, err;
206
207 if (snd_len > 0x28 - 7 || rcv_len > 0x20 - 7)
208 return -EINVAL;
209
210 b[0] = 0xaa;
211 b[1] = id;
212 b[2] = 0x31;
213 b[3] = snd_len + 3;
214 b[4] = addr << 1;
215 b[5] = snd_len;
216 b[6] = rcv_len;
217
218 for (i = 0; i < snd_len; i++)
219 b[7 + i] = snd_buf[i];
220
221 err = ttusb_cmd(ttusb, b, snd_len + 7, 1);
222
223 if (err)
224 return -EREMOTEIO;
225
226 err = ttusb_result(ttusb, b, 0x20);
227
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800228 /* check if the i2c transaction was successful */
229 if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (rcv_len > 0) {
232
233 if (err || b[0] != 0x55 || b[1] != id) {
234 dprintk
235 ("%s: usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
236 __FUNCTION__, err, id);
237 return -EREMOTEIO;
238 }
239
240 for (i = 0; i < rcv_len; i++)
241 rcv_buf[i] = b[7 + i];
242 }
243
244 return rcv_len;
245}
246
247static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
248{
249 struct ttusb *ttusb = i2c_get_adapdata(adapter);
250 int i = 0;
251 int inc;
252
Ingo Molnar3593cab2006-02-07 06:49:14 -0200253 if (mutex_lock_interruptible(&ttusb->semi2c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return -EAGAIN;
255
256 while (i < num) {
257 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
258 int err;
259
260 if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
261 addr = msg[i].addr;
262 snd_buf = msg[i].buf;
263 snd_len = msg[i].len;
264 rcv_buf = msg[i + 1].buf;
265 rcv_len = msg[i + 1].len;
266 inc = 2;
267 } else {
268 addr = msg[i].addr;
269 snd_buf = msg[i].buf;
270 snd_len = msg[i].len;
271 rcv_buf = NULL;
272 rcv_len = 0;
273 inc = 1;
274 }
275
276 err = ttusb_i2c_msg(ttusb, addr,
277 snd_buf, snd_len, rcv_buf, rcv_len);
278
279 if (err < rcv_len) {
280 dprintk("%s: i == %i\n", __FUNCTION__, i);
281 break;
282 }
283
284 i += inc;
285 }
286
Ingo Molnar3593cab2006-02-07 06:49:14 -0200287 mutex_unlock(&ttusb->semi2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return i;
289}
290
291#include "dvb-ttusb-dspbootcode.h"
292
293static int ttusb_boot_dsp(struct ttusb *ttusb)
294{
295 int i, err;
296 u8 b[40];
297
298 /* BootBlock */
299 b[0] = 0xaa;
300 b[2] = 0x13;
301 b[3] = 28;
302
303 /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
304 /* 32 is max packet size, no messages should be splitted. */
305 for (i = 0; i < sizeof(dsp_bootcode); i += 28) {
306 memcpy(&b[4], &dsp_bootcode[i], 28);
307
308 b[1] = ++ttusb->c;
309
310 err = ttusb_cmd(ttusb, b, 32, 0);
311 if (err)
312 goto done;
313 }
314
315 /* last block ... */
316 b[1] = ++ttusb->c;
317 b[2] = 0x13;
318 b[3] = 0;
319
320 err = ttusb_cmd(ttusb, b, 4, 0);
321 if (err)
322 goto done;
323
324 /* BootEnd */
325 b[1] = ++ttusb->c;
326 b[2] = 0x14;
327 b[3] = 0;
328
329 err = ttusb_cmd(ttusb, b, 4, 0);
330
331 done:
332 if (err) {
333 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
334 __FUNCTION__, err);
335 }
336
337 return err;
338}
339
340static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
341 int pid)
342{
343 int err;
344 /* SetChannel */
345 u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
346 (pid >> 8) & 0xff, pid & 0xff
347 };
348
349 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
350 return err;
351}
352
353static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
354{
355 int err;
356 /* DelChannel */
357 u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
358
359 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
360 return err;
361}
362
363#ifdef TTUSB_HWSECTIONS
364static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
365 int associated_chan, u8 filter[8], u8 mask[8])
366{
367 int err;
368 /* SetFilter */
369 u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
370 filter[0], filter[1], filter[2], filter[3],
371 filter[4], filter[5], filter[6], filter[7],
372 filter[8], filter[9], filter[10], filter[11],
373 mask[0], mask[1], mask[2], mask[3],
374 mask[4], mask[5], mask[6], mask[7],
375 mask[8], mask[9], mask[10], mask[11]
376 };
377
378 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
379 return err;
380}
381
382static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
383{
384 int err;
385 /* DelFilter */
386 u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
387
388 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
389 return err;
390}
391#endif
392
393static int ttusb_init_controller(struct ttusb *ttusb)
394{
395 u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
396 u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
397 u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
398 /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
399 u8 b3[] =
400 { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
401 u8 b4[] =
402 { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
403
404 u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
405 u8 get_dsp_version[0x20] =
406 { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
407 int err;
408
409 /* reset board */
410 if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
411 return err;
412
413 /* reset board (again?) */
414 if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
415 return err;
416
417 ttusb_boot_dsp(ttusb);
418
419 /* set i2c bit rate */
420 if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
421 return err;
422
423 if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
424 return err;
425
426 err = ttusb_result(ttusb, b4, sizeof(b4));
427
428 if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
429 return err;
430
431 if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
432 return err;
433
434 dprintk("%s: stc-version: %c%c%c%c%c\n", __FUNCTION__,
435 get_version[4], get_version[5], get_version[6],
436 get_version[7], get_version[8]);
437
438 if (memcmp(get_version + 4, "V 0.0", 5) &&
439 memcmp(get_version + 4, "V 1.1", 5) &&
440 memcmp(get_version + 4, "V 2.1", 5) &&
441 memcmp(get_version + 4, "V 2.2", 5)) {
442 printk
443 ("%s: unknown STC version %c%c%c%c%c, please report!\n",
444 __FUNCTION__, get_version[4], get_version[5],
445 get_version[6], get_version[7], get_version[8]);
446 }
447
448 ttusb->revision = ((get_version[6] - '0') << 4) |
449 (get_version[8] - '0');
450
451 err =
452 ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
453 if (err)
454 return err;
455
456 err =
457 ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
458 if (err)
459 return err;
460 printk("%s: dsp-version: %c%c%c\n", __FUNCTION__,
461 get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
462 return 0;
463}
464
465#ifdef TTUSB_DISEQC
466static int ttusb_send_diseqc(struct dvb_frontend* fe,
467 const struct dvb_diseqc_master_cmd *cmd)
468{
469 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
470 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
471
472 int err;
473
474 b[3] = 4 + 2 + cmd->msg_len;
475 b[4] = 0xFF; /* send diseqc master, not burst */
476 b[5] = cmd->msg_len;
477
478 memcpy(b + 5, cmd->msg, cmd->msg_len);
479
480 /* Diseqc */
481 if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
482 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
483 __FUNCTION__, err);
484 }
485
486 return err;
487}
488#endif
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static int ttusb_update_lnb(struct ttusb *ttusb)
491{
492 u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
493 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
494 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
495 };
496 int err;
497
498 /* SetLNB */
499 if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
500 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
501 __FUNCTION__, err);
502 }
503
504 return err;
505}
506
507static int ttusb_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
508{
509 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
510
511 ttusb->voltage = voltage;
512 return ttusb_update_lnb(ttusb);
513}
514
515#ifdef TTUSB_TONE
516static int ttusb_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
517{
518 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
519
520 ttusb->tone = tone;
521 return ttusb_update_lnb(ttusb);
522}
523#endif
524
525
526#if 0
527static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
528{
529 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
530 int err, actual_len;
531
532 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
533 if (err) {
534 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
535 __FUNCTION__, err);
536 }
537}
538#endif
539
540/*****************************************************************************/
541
542#ifdef TTUSB_HWSECTIONS
543static void ttusb_handle_ts_data(struct ttusb_channel *channel,
544 const u8 * data, int len);
545static void ttusb_handle_sec_data(struct ttusb_channel *channel,
546 const u8 * data, int len);
547#endif
548
Marcelo Feitoza Parisi4da006c2005-09-09 13:03:15 -0700549static int numpkt = 0, numts, numstuff, numsec, numinvalid;
550static unsigned long lastj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
553 int len)
554{
555 u16 csum = 0, cc;
556 int i;
557 for (i = 0; i < len; i += 2)
558 csum ^= le16_to_cpup((u16 *) (muxpack + i));
559 if (csum) {
560 printk("%s: muxpack with incorrect checksum, ignoring\n",
561 __FUNCTION__);
562 numinvalid++;
563 return;
564 }
565
566 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
567 cc &= 0x7FFF;
568 if ((cc != ttusb->cc) && (ttusb->cc != -1))
569 printk("%s: cc discontinuity (%d frames missing)\n",
570 __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
571 ttusb->cc = (cc + 1) & 0x7FFF;
572 if (muxpack[0] & 0x80) {
573#ifdef TTUSB_HWSECTIONS
574 /* section data */
575 int pusi = muxpack[0] & 0x40;
576 int channel = muxpack[0] & 0x1F;
577 int payload = muxpack[1];
578 const u8 *data = muxpack + 2;
579 /* check offset flag */
580 if (muxpack[0] & 0x20)
581 data++;
582
583 ttusb_handle_sec_data(ttusb->channel + channel, data,
584 payload);
585 data += payload;
586
587 if ((!!(ttusb->muxpack[0] & 0x20)) ^
588 !!(ttusb->muxpack[1] & 1))
589 data++;
590#warning TODO: pusi
591 printk("cc: %04x\n", (data[0] << 8) | data[1]);
592#endif
593 numsec++;
594 } else if (muxpack[0] == 0x47) {
595#ifdef TTUSB_HWSECTIONS
596 /* we have TS data here! */
597 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
598 int channel;
599 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
600 if (ttusb->channel[channel].active
601 && (pid == ttusb->channel[channel].pid))
602 ttusb_handle_ts_data(ttusb->channel +
603 channel, muxpack,
604 188);
605#endif
606 numts++;
607 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
608 } else if (muxpack[0] != 0) {
609 numinvalid++;
610 printk("illegal muxpack type %02x\n", muxpack[0]);
611 } else
612 numstuff++;
613}
614
615static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
616{
617 int maxwork = 1024;
618 while (len) {
619 if (!(maxwork--)) {
620 printk("%s: too much work\n", __FUNCTION__);
621 break;
622 }
623
624 switch (ttusb->mux_state) {
625 case 0:
626 case 1:
627 case 2:
628 len--;
629 if (*data++ == 0xAA)
630 ++ttusb->mux_state;
631 else {
632 ttusb->mux_state = 0;
633#if DEBUG > 3
634 if (ttusb->insync)
635 printk("%02x ", data[-1]);
636#else
637 if (ttusb->insync) {
638 printk("%s: lost sync.\n",
639 __FUNCTION__);
640 ttusb->insync = 0;
641 }
642#endif
643 }
644 break;
645 case 3:
646 ttusb->insync = 1;
647 len--;
648 ttusb->mux_npacks = *data++;
649 ++ttusb->mux_state;
650 ttusb->muxpack_ptr = 0;
651 /* maximum bytes, until we know the length */
652 ttusb->muxpack_len = 2;
653 break;
654 case 4:
655 {
656 int avail;
657 avail = len;
658 if (avail >
659 (ttusb->muxpack_len -
660 ttusb->muxpack_ptr))
661 avail =
662 ttusb->muxpack_len -
663 ttusb->muxpack_ptr;
664 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
665 data, avail);
666 ttusb->muxpack_ptr += avail;
Eric Sesterhennae246012006-03-13 13:17:11 -0300667 BUG_ON(ttusb->muxpack_ptr > 264);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 data += avail;
669 len -= avail;
670 /* determine length */
671 if (ttusb->muxpack_ptr == 2) {
672 if (ttusb->muxpack[0] & 0x80) {
673 ttusb->muxpack_len =
674 ttusb->muxpack[1] + 2;
675 if (ttusb->
676 muxpack[0] & 0x20)
677 ttusb->
678 muxpack_len++;
679 if ((!!
680 (ttusb->
681 muxpack[0] & 0x20)) ^
682 !!(ttusb->
683 muxpack[1] & 1))
684 ttusb->
685 muxpack_len++;
686 ttusb->muxpack_len += 4;
687 } else if (ttusb->muxpack[0] ==
688 0x47)
689 ttusb->muxpack_len =
690 188 + 4;
691 else if (ttusb->muxpack[0] == 0x00)
692 ttusb->muxpack_len =
693 ttusb->muxpack[1] + 2 +
694 4;
695 else {
696 dprintk
697 ("%s: invalid state: first byte is %x\n",
698 __FUNCTION__,
699 ttusb->muxpack[0]);
700 ttusb->mux_state = 0;
701 }
702 }
703
704 /**
705 * if length is valid and we reached the end:
706 * goto next muxpack
707 */
708 if ((ttusb->muxpack_ptr >= 2) &&
709 (ttusb->muxpack_ptr ==
710 ttusb->muxpack_len)) {
711 ttusb_process_muxpack(ttusb,
712 ttusb->
713 muxpack,
714 ttusb->
715 muxpack_ptr);
716 ttusb->muxpack_ptr = 0;
717 /* maximum bytes, until we know the length */
718 ttusb->muxpack_len = 2;
719
720 /**
721 * no muxpacks left?
722 * return to search-sync state
723 */
724 if (!ttusb->mux_npacks--) {
725 ttusb->mux_state = 0;
726 break;
727 }
728 }
729 break;
730 }
731 default:
732 BUG();
733 break;
734 }
735 }
736}
737
738static void ttusb_iso_irq(struct urb *urb, struct pt_regs *ptregs)
739{
740 struct ttusb *ttusb = urb->context;
741
742 if (!ttusb->iso_streaming)
743 return;
744
745#if 0
746 printk("%s: status %d, errcount == %d, length == %i\n",
747 __FUNCTION__,
748 urb->status, urb->error_count, urb->actual_length);
749#endif
750
751 if (!urb->status) {
752 int i;
753 for (i = 0; i < urb->number_of_packets; ++i) {
754 struct usb_iso_packet_descriptor *d;
755 u8 *data;
756 int len;
757 numpkt++;
Marcelo Feitoza Parisi4da006c2005-09-09 13:03:15 -0700758 if (time_after_eq(jiffies, lastj + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759#if DEBUG > 2
760 printk
761 ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
762 numpkt * HZ / (jiffies - lastj),
763 numts, numstuff, numsec, numinvalid,
764 numts + numstuff + numsec +
765 numinvalid);
766#endif
767 numts = numstuff = numsec = numinvalid = 0;
768 lastj = jiffies;
769 numpkt = 0;
770 }
771 d = &urb->iso_frame_desc[i];
772 data = urb->transfer_buffer + d->offset;
773 len = d->actual_length;
774 d->actual_length = 0;
775 d->status = 0;
776 ttusb_process_frame(ttusb, data, len);
777 }
778 }
779 usb_submit_urb(urb, GFP_ATOMIC);
780}
781
782static void ttusb_free_iso_urbs(struct ttusb *ttusb)
783{
784 int i;
785
786 for (i = 0; i < ISO_BUF_COUNT; i++)
787 if (ttusb->iso_urb[i])
788 usb_free_urb(ttusb->iso_urb[i]);
789
790 pci_free_consistent(NULL,
791 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
792 ISO_BUF_COUNT, ttusb->iso_buffer,
793 ttusb->iso_dma_handle);
794}
795
796static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
797{
798 int i;
799
800 ttusb->iso_buffer = pci_alloc_consistent(NULL,
801 ISO_FRAME_SIZE *
802 FRAMES_PER_ISO_BUF *
803 ISO_BUF_COUNT,
804 &ttusb->iso_dma_handle);
805
806 memset(ttusb->iso_buffer, 0,
807 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
808
809 for (i = 0; i < ISO_BUF_COUNT; i++) {
810 struct urb *urb;
811
812 if (!
813 (urb =
814 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
815 ttusb_free_iso_urbs(ttusb);
816 return -ENOMEM;
817 }
818
819 ttusb->iso_urb[i] = urb;
820 }
821
822 return 0;
823}
824
825static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
826{
827 int i;
828
829 for (i = 0; i < ISO_BUF_COUNT; i++)
830 usb_kill_urb(ttusb->iso_urb[i]);
831
832 ttusb->iso_streaming = 0;
833}
834
835static int ttusb_start_iso_xfer(struct ttusb *ttusb)
836{
837 int i, j, err, buffer_offset = 0;
838
839 if (ttusb->iso_streaming) {
840 printk("%s: iso xfer already running!\n", __FUNCTION__);
841 return 0;
842 }
843
844 ttusb->cc = -1;
845 ttusb->insync = 0;
846 ttusb->mux_state = 0;
847
848 for (i = 0; i < ISO_BUF_COUNT; i++) {
849 int frame_offset = 0;
850 struct urb *urb = ttusb->iso_urb[i];
851
852 urb->dev = ttusb->dev;
853 urb->context = ttusb;
854 urb->complete = ttusb_iso_irq;
855 urb->pipe = ttusb->isoc_in_pipe;
856 urb->transfer_flags = URB_ISO_ASAP;
857 urb->interval = 1;
858 urb->number_of_packets = FRAMES_PER_ISO_BUF;
859 urb->transfer_buffer_length =
860 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
861 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
862 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
863
864 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
865 urb->iso_frame_desc[j].offset = frame_offset;
866 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
867 frame_offset += ISO_FRAME_SIZE;
868 }
869 }
870
871 for (i = 0; i < ISO_BUF_COUNT; i++) {
872 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
873 ttusb_stop_iso_xfer(ttusb);
874 printk
875 ("%s: failed urb submission (%i: err = %i)!\n",
876 __FUNCTION__, i, err);
877 return err;
878 }
879 }
880
881 ttusb->iso_streaming = 1;
882
883 return 0;
884}
885
886#ifdef TTUSB_HWSECTIONS
887static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
888 int len)
889{
890 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
891}
892
893static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
894 int len)
895{
896// struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
897#error TODO: handle ugly stuff
898// dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
899}
900#endif
901
902static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
903{
904 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
905 int feed_type = 1;
906
907 dprintk("ttusb_start_feed\n");
908
909 switch (dvbdmxfeed->type) {
910 case DMX_TYPE_TS:
911 break;
912 case DMX_TYPE_SEC:
913 break;
914 default:
915 return -EINVAL;
916 }
917
918 if (dvbdmxfeed->type == DMX_TYPE_TS) {
919 switch (dvbdmxfeed->pes_type) {
920 case DMX_TS_PES_VIDEO:
921 case DMX_TS_PES_AUDIO:
922 case DMX_TS_PES_TELETEXT:
923 case DMX_TS_PES_PCR:
924 case DMX_TS_PES_OTHER:
925 break;
926 default:
927 return -EINVAL;
928 }
929 }
930
931#ifdef TTUSB_HWSECTIONS
932#error TODO: allocate filters
933 if (dvbdmxfeed->type == DMX_TYPE_TS) {
934 feed_type = 1;
935 } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
936 feed_type = 2;
937 }
938#endif
939
940 ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
941
942 if (0 == ttusb->running_feed_count++)
943 ttusb_start_iso_xfer(ttusb);
944
945 return 0;
946}
947
948static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
949{
950 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
951
952 ttusb_del_channel(ttusb, dvbdmxfeed->index);
953
954 if (--ttusb->running_feed_count == 0)
955 ttusb_stop_iso_xfer(ttusb);
956
957 return 0;
958}
959
960static int ttusb_setup_interfaces(struct ttusb *ttusb)
961{
962 usb_set_interface(ttusb->dev, 1, 1);
963
964 ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
965 ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
966 ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
967
968 return 0;
969}
970
971#if 0
972static u8 stc_firmware[8192];
973
974static int stc_open(struct inode *inode, struct file *file)
975{
976 struct ttusb *ttusb = file->private_data;
977 int addr;
978
979 for (addr = 0; addr < 8192; addr += 16) {
980 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
981 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
982 16);
983 }
984
985 return 0;
986}
987
988static ssize_t stc_read(struct file *file, char *buf, size_t count,
989 loff_t * offset)
990{
991 int tc = count;
992
993 if ((tc + *offset) > 8192)
994 tc = 8192 - *offset;
995
996 if (tc < 0)
997 return 0;
998
999 if (copy_to_user(buf, stc_firmware + *offset, tc))
1000 return -EFAULT;
1001
1002 *offset += tc;
1003
1004 return tc;
1005}
1006
1007static int stc_release(struct inode *inode, struct file *file)
1008{
1009 return 0;
1010}
1011
1012static struct file_operations stc_fops = {
1013 .owner = THIS_MODULE,
1014 .read = stc_read,
1015 .open = stc_open,
1016 .release = stc_release,
1017};
1018#endif
1019
1020static u32 functionality(struct i2c_adapter *adapter)
1021{
1022 return I2C_FUNC_I2C;
1023}
1024
1025
1026
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001027static int alps_tdmb7_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1030 u8 data[4];
1031 struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
1032 u32 div;
1033
1034 div = (params->frequency + 36166667) / 166667;
1035
1036 data[0] = (div >> 8) & 0x7f;
1037 data[1] = div & 0xff;
1038 data[2] = ((div >> 10) & 0x60) | 0x85;
1039 data[3] = params->frequency < 592000000 ? 0x40 : 0x80;
1040
1041 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
1042 return 0;
1043}
1044
Johannes Stezenbachd91b7302005-05-16 21:54:38 -07001045static struct cx22700_config alps_tdmb7_config = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 .demod_address = 0x43,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047};
1048
1049
1050
1051
1052
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001053static int philips_tdm1316l_tuner_init(struct dvb_frontend* fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
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
1061 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1062 msleep(1);
1063
1064 // disable the mc44BC374c (do not check for errors)
1065 tuner_msg.addr = 0x65;
1066 tuner_msg.buf = disable_mc44BC374c;
1067 tuner_msg.len = sizeof(disable_mc44BC374c);
1068 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1069 i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1070 }
1071
1072 return 0;
1073}
1074
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001075static int philips_tdm1316l_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1078 u8 tuner_buf[4];
1079 struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1080 int tuner_frequency = 0;
1081 u8 band, cp, filter;
1082
1083 // determine charge pump
1084 tuner_frequency = params->frequency + 36130000;
1085 if (tuner_frequency < 87000000) return -EINVAL;
1086 else if (tuner_frequency < 130000000) cp = 3;
1087 else if (tuner_frequency < 160000000) cp = 5;
1088 else if (tuner_frequency < 200000000) cp = 6;
1089 else if (tuner_frequency < 290000000) cp = 3;
1090 else if (tuner_frequency < 420000000) cp = 5;
1091 else if (tuner_frequency < 480000000) cp = 6;
1092 else if (tuner_frequency < 620000000) cp = 3;
1093 else if (tuner_frequency < 830000000) cp = 5;
1094 else if (tuner_frequency < 895000000) cp = 7;
1095 else return -EINVAL;
1096
1097 // determine band
1098 if (params->frequency < 49000000) return -EINVAL;
1099 else if (params->frequency < 159000000) band = 1;
1100 else if (params->frequency < 444000000) band = 2;
1101 else if (params->frequency < 861000000) band = 4;
1102 else return -EINVAL;
1103
1104 // setup PLL filter
1105 switch (params->u.ofdm.bandwidth) {
1106 case BANDWIDTH_6_MHZ:
1107 tda1004x_write_byte(fe, 0x0C, 0);
1108 filter = 0;
1109 break;
1110
1111 case BANDWIDTH_7_MHZ:
1112 tda1004x_write_byte(fe, 0x0C, 0);
1113 filter = 0;
1114 break;
1115
1116 case BANDWIDTH_8_MHZ:
1117 tda1004x_write_byte(fe, 0x0C, 0xFF);
1118 filter = 1;
1119 break;
1120
1121 default:
1122 return -EINVAL;
1123 }
1124
1125 // calculate divisor
1126 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1127 tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
1128
1129 // setup tuner buffer
1130 tuner_buf[0] = tuner_frequency >> 8;
1131 tuner_buf[1] = tuner_frequency & 0xff;
1132 tuner_buf[2] = 0xca;
1133 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1134
1135 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1136 return -EIO;
1137
1138 msleep(1);
1139 return 0;
1140}
1141
1142static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1143{
1144 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1145
1146 return request_firmware(fw, name, &ttusb->dev->dev);
1147}
1148
1149static struct tda1004x_config philips_tdm1316l_config = {
1150
1151 .demod_address = 0x8,
1152 .invert = 1,
1153 .invert_oclk = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 .request_firmware = philips_tdm1316l_request_firmware,
1155};
1156
1157static u8 alps_bsbe1_inittab[] = {
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08001158 0x01, 0x15,
1159 0x02, 0x30,
1160 0x03, 0x00,
1161 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1162 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1163 0x06, 0x40, /* DAC not used, set to high impendance mode */
1164 0x07, 0x00, /* DAC LSB */
1165 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1166 0x09, 0x00, /* FIFO */
1167 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1168 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1169 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1170 0x10, 0x3f, // AGC2 0x3d
1171 0x11, 0x84,
1172 0x12, 0xb9,
1173 0x15, 0xc9, // lock detector threshold
1174 0x16, 0x00,
1175 0x17, 0x00,
1176 0x18, 0x00,
1177 0x19, 0x00,
1178 0x1a, 0x00,
1179 0x1f, 0x50,
1180 0x20, 0x00,
1181 0x21, 0x00,
1182 0x22, 0x00,
1183 0x23, 0x00,
1184 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1185 0x29, 0x1e, // 1/2 threshold
1186 0x2a, 0x14, // 2/3 threshold
1187 0x2b, 0x0f, // 3/4 threshold
1188 0x2c, 0x09, // 5/6 threshold
1189 0x2d, 0x05, // 7/8 threshold
1190 0x2e, 0x01,
1191 0x31, 0x1f, // test all FECs
1192 0x32, 0x19, // viterbi and synchro search
1193 0x33, 0xfc, // rs control
1194 0x34, 0x93, // error control
1195 0x0f, 0x92,
1196 0xff, 0xff
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197};
1198
1199static u8 alps_bsru6_inittab[] = {
1200 0x01, 0x15,
1201 0x02, 0x30,
1202 0x03, 0x00,
1203 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1204 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1205 0x06, 0x40, /* DAC not used, set to high impendance mode */
1206 0x07, 0x00, /* DAC LSB */
1207 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1208 0x09, 0x00, /* FIFO */
1209 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1210 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1211 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1212 0x10, 0x3f, // AGC2 0x3d
1213 0x11, 0x84,
Oliver Endriss7f44dcd2005-11-08 21:35:44 -08001214 0x12, 0xb9,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 0x15, 0xc9, // lock detector threshold
1216 0x16, 0x00,
1217 0x17, 0x00,
1218 0x18, 0x00,
1219 0x19, 0x00,
1220 0x1a, 0x00,
1221 0x1f, 0x50,
1222 0x20, 0x00,
1223 0x21, 0x00,
1224 0x22, 0x00,
1225 0x23, 0x00,
1226 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1227 0x29, 0x1e, // 1/2 threshold
1228 0x2a, 0x14, // 2/3 threshold
1229 0x2b, 0x0f, // 3/4 threshold
1230 0x2c, 0x09, // 5/6 threshold
1231 0x2d, 0x05, // 7/8 threshold
1232 0x2e, 0x01,
1233 0x31, 0x1f, // test all FECs
1234 0x32, 0x19, // viterbi and synchro search
1235 0x33, 0xfc, // rs control
1236 0x34, 0x93, // error control
1237 0x0f, 0x52,
1238 0xff, 0xff
1239};
1240
1241static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1242{
1243 u8 aclk = 0;
1244 u8 bclk = 0;
1245
1246 if (srate < 1500000) {
1247 aclk = 0xb7;
1248 bclk = 0x47;
1249 } else if (srate < 3000000) {
1250 aclk = 0xb7;
1251 bclk = 0x4b;
1252 } else if (srate < 7000000) {
1253 aclk = 0xb7;
1254 bclk = 0x4f;
1255 } else if (srate < 14000000) {
1256 aclk = 0xb7;
1257 bclk = 0x53;
1258 } else if (srate < 30000000) {
1259 aclk = 0xb6;
1260 bclk = 0x53;
1261 } else if (srate < 45000000) {
1262 aclk = 0xb4;
1263 bclk = 0x51;
1264 }
1265
1266 stv0299_writereg(fe, 0x13, aclk);
1267 stv0299_writereg(fe, 0x14, bclk);
1268 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1269 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1270 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1271
1272 return 0;
1273}
1274
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001275static int philips_tsa5059_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1278 u8 buf[4];
1279 u32 div;
1280 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1281
1282 if ((params->frequency < 950000) || (params->frequency > 2150000))
1283 return -EINVAL;
1284
1285 div = (params->frequency + (125 - 1)) / 125; // round correctly
1286 buf[0] = (div >> 8) & 0x7f;
1287 buf[1] = div & 0xff;
1288 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1289 buf[3] = 0xC4;
1290
1291 if (params->frequency > 1530000)
1292 buf[3] = 0xC0;
1293
1294 /* BSBE1 wants XCE bit set */
1295 if (ttusb->revision == TTUSB_REV_2_2)
1296 buf[3] |= 0x20;
1297
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001298 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -EIO;
1300
1301 return 0;
1302}
1303
1304static struct stv0299_config alps_stv0299_config = {
1305 .demod_address = 0x68,
1306 .inittab = alps_bsru6_inittab,
1307 .mclk = 88000000UL,
1308 .invert = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 .skip_reinit = 0,
1310 .lock_output = STV0229_LOCKOUTPUT_1,
1311 .volt13_op0_op1 = STV0299_VOLT13_OP1,
1312 .min_delay_ms = 100,
1313 .set_symbol_rate = alps_stv0299_set_symbol_rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314};
1315
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001316static 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 -07001317{
1318 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1319 u8 buf[4];
1320 u32 div;
1321 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1322
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08001323 div = params->frequency / 125;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 buf[0] = (div >> 8) & 0x7f;
1326 buf[1] = div & 0xff;
1327 buf[2] = 0x8e;
1328 buf[3] = 0x00;
1329
1330 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1331 return -EIO;
1332
1333 return 0;
1334}
1335
1336static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1337
1338 .demod_address = 0x68,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339};
1340
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001341static int alps_tdbe2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
Gavin Hamill53936392005-07-07 17:58:04 -07001342{
1343 struct ttusb* ttusb = fe->dvb->priv;
1344 u32 div;
1345 u8 data[4];
1346 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1347
1348 div = (params->frequency + 35937500 + 31250) / 62500;
1349
1350 data[0] = (div >> 8) & 0x7f;
1351 data[1] = div & 0xff;
1352 data[2] = 0x85 | ((div >> 10) & 0x60);
1353 data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
1354
1355 if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1356 return -EIO;
1357
1358 return 0;
1359}
1360
1361
1362static struct ves1820_config alps_tdbe2_config = {
1363 .demod_address = 0x09,
1364 .xin = 57840000UL,
1365 .invert = 1,
1366 .selagc = VES1820_SELAGC_SIGNAMPERR,
Gavin Hamill53936392005-07-07 17:58:04 -07001367};
1368
1369static u8 read_pwm(struct ttusb* ttusb)
1370{
1371 u8 b = 0xff;
1372 u8 pwm;
1373 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1374 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1375
1376 if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1377 pwm = 0x48;
1378
1379 return pwm;
1380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382
1383static void frontend_init(struct ttusb* ttusb)
1384{
1385 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1386 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1387 // try the stv0299 based first
1388 ttusb->fe = stv0299_attach(&alps_stv0299_config, &ttusb->i2c_adap);
1389 if (ttusb->fe != NULL) {
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001390 ttusb->fe->ops->tuner_ops.set_params = philips_tsa5059_tuner_set_params;
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1393 alps_stv0299_config.inittab = alps_bsbe1_inittab;
Andrew de Quinceyd0205422006-04-27 21:45:01 -03001394 lnbp21_attach(ttusb->fe, &ttusb->i2c_adap, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 } else { // ALPS BSRU6
1396 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1397 }
1398 break;
1399 }
1400
1401 // Grundig 29504-491
1402 ttusb->fe = tda8083_attach(&ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1403 if (ttusb->fe != NULL) {
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001404 ttusb->fe->ops->tuner_ops.set_params = ttusb_novas_grundig_29504_491_tuner_set_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1406 break;
1407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409
Gavin Hamill53936392005-07-07 17:58:04 -07001410 case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
1411 ttusb->fe = ves1820_attach(&alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001412 if (ttusb->fe != NULL) {
1413 ttusb->fe->ops->tuner_ops.set_params = alps_tdbe2_tuner_set_params;
Gavin Hamill53936392005-07-07 17:58:04 -07001414 break;
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001415 }
Gavin Hamill53936392005-07-07 17:58:04 -07001416 break;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1419 // try the ALPS TDMB7 first
1420 ttusb->fe = cx22700_attach(&alps_tdmb7_config, &ttusb->i2c_adap);
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001421 if (ttusb->fe != NULL) {
1422 ttusb->fe->ops->tuner_ops.set_params = alps_tdmb7_tuner_set_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 break;
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 // Philips td1316
1427 ttusb->fe = tda10046_attach(&philips_tdm1316l_config, &ttusb->i2c_adap);
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001428 if (ttusb->fe != NULL) {
1429 ttusb->fe->ops->tuner_ops.init = philips_tdm1316l_tuner_init;
1430 ttusb->fe->ops->tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 break;
Andrew de Quincey651b81b2006-04-18 17:47:11 -03001432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 break;
1434 }
1435
1436 if (ttusb->fe == NULL) {
1437 printk("dvb-ttusb-budget: A frontend driver was not found for device %04x/%04x\n",
1438 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1439 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1440 } else {
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001441 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 printk("dvb-ttusb-budget: Frontend registration failed!\n");
1443 if (ttusb->fe->ops->release)
1444 ttusb->fe->ops->release(ttusb->fe);
1445 ttusb->fe = NULL;
1446 }
1447 }
1448}
1449
1450
1451
1452static struct i2c_algorithm ttusb_dec_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 .master_xfer = master_xfer,
1454 .functionality = functionality,
1455};
1456
1457static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1458{
1459 struct usb_device *udev;
1460 struct ttusb *ttusb;
1461 int result;
1462
1463 dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1464
1465 udev = interface_to_usbdev(intf);
1466
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08001467 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Panagiotis Issaris74081872006-01-11 19:40:56 -02001469 if (!(ttusb = kzalloc(sizeof(struct ttusb), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -ENOMEM;
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 ttusb->dev = udev;
1473 ttusb->c = 0;
1474 ttusb->mux_state = 0;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001475 mutex_init(&ttusb->semi2c);
1476
1477 mutex_lock(&ttusb->semi2c);
1478
1479 mutex_init(&ttusb->semusb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 ttusb_setup_interfaces(ttusb);
1482
1483 ttusb_alloc_iso_urbs(ttusb);
1484 if (ttusb_init_controller(ttusb))
1485 printk("ttusb_init_controller: error\n");
1486
Ingo Molnar3593cab2006-02-07 06:49:14 -02001487 mutex_unlock(&ttusb->semi2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -03001489 if ((result = dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE, &udev->dev)) < 0) {
Andrew de Quinceya064fad2006-04-06 17:05:46 -03001490 ttusb_free_iso_urbs(ttusb);
1491 kfree(ttusb);
1492 return result;
1493 }
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001494 ttusb->adapter.priv = ttusb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /* i2c */
1497 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1498 strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1499
1500 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1501
1502#ifdef I2C_ADAP_CLASS_TV_DIGITAL
1503 ttusb->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
1504#else
1505 ttusb->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
1506#endif
1507 ttusb->i2c_adap.algo = &ttusb_dec_algo;
1508 ttusb->i2c_adap.algo_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 result = i2c_add_adapter(&ttusb->i2c_adap);
1511 if (result) {
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001512 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return result;
1514 }
1515
1516 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1517
1518 ttusb->dvb_demux.dmx.capabilities =
1519 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1520 ttusb->dvb_demux.priv = NULL;
1521#ifdef TTUSB_HWSECTIONS
1522 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1523#else
1524 ttusb->dvb_demux.filternum = 32;
1525#endif
1526 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1527 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1528 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1529 ttusb->dvb_demux.write_to_decoder = NULL;
1530
1531 if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1532 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1533 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001534 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -ENODEV;
1536 }
1537//FIXME dmxdev (nur WAS?)
1538 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1539 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1540 ttusb->dmxdev.capabilities = 0;
1541
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001542 if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1544 result);
1545 dvb_dmx_release(&ttusb->dvb_demux);
1546 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001547 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -ENODEV;
1549 }
1550
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001551 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 printk("ttusb_dvb: dvb_net_init failed!\n");
1553 dvb_dmxdev_release(&ttusb->dmxdev);
1554 dvb_dmx_release(&ttusb->dvb_demux);
1555 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001556 dvb_unregister_adapter (&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return -ENODEV;
1558 }
1559
1560#if 0
1561 ttusb->stc_devfs_handle =
1562 devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
1563 DEVFS_FL_DEFAULT, 0, 192,
1564 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1565 | S_IROTH | S_IWOTH, &stc_fops, ttusb);
1566#endif
1567 usb_set_intfdata(intf, (void *) ttusb);
1568
1569 frontend_init(ttusb);
1570
1571 return 0;
1572}
1573
1574static void ttusb_disconnect(struct usb_interface *intf)
1575{
1576 struct ttusb *ttusb = usb_get_intfdata(intf);
1577
1578 usb_set_intfdata(intf, NULL);
1579
1580 ttusb->disconnecting = 1;
1581
1582 ttusb_stop_iso_xfer(ttusb);
1583
1584 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1585 dvb_net_release(&ttusb->dvbnet);
1586 dvb_dmxdev_release(&ttusb->dmxdev);
1587 dvb_dmx_release(&ttusb->dvb_demux);
1588 if (ttusb->fe != NULL) dvb_unregister_frontend(ttusb->fe);
1589 i2c_del_adapter(&ttusb->i2c_adap);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001590 dvb_unregister_adapter(&ttusb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 ttusb_free_iso_urbs(ttusb);
1593
1594 kfree(ttusb);
1595
1596 dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1597}
1598
1599static struct usb_device_id ttusb_table[] = {
1600 {USB_DEVICE(0xb48, 0x1003)},
Gavin Hamill53936392005-07-07 17:58:04 -07001601 {USB_DEVICE(0xb48, 0x1004)},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 {USB_DEVICE(0xb48, 0x1005)},
1603 {}
1604};
1605
1606MODULE_DEVICE_TABLE(usb, ttusb_table);
1607
1608static struct usb_driver ttusb_driver = {
Julian Scheel27b05fd2005-07-12 13:58:39 -07001609 .name = "ttusb",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 .probe = ttusb_probe,
1611 .disconnect = ttusb_disconnect,
1612 .id_table = ttusb_table,
1613};
1614
1615static int __init ttusb_init(void)
1616{
1617 int err;
1618
1619 if ((err = usb_register(&ttusb_driver)) < 0) {
1620 printk("%s: usb_register failed! Error number %d",
1621 __FILE__, err);
1622 return err;
1623 }
1624
1625 return 0;
1626}
1627
1628static void __exit ttusb_exit(void)
1629{
1630 usb_deregister(&ttusb_driver);
1631}
1632
1633module_init(ttusb_init);
1634module_exit(ttusb_exit);
1635
1636MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1637MODULE_DESCRIPTION("TTUSB DVB Driver");
1638MODULE_LICENSE("GPL");