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