Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * TTUSB DVB driver
0004  *
0005  * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
0006  * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
0007  */
0008 
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010 
0011 #include <linux/init.h>
0012 #include <linux/slab.h>
0013 #include <linux/wait.h>
0014 #include <linux/fs.h>
0015 #include <linux/module.h>
0016 #include <linux/usb.h>
0017 #include <linux/delay.h>
0018 #include <linux/time.h>
0019 #include <linux/errno.h>
0020 #include <linux/jiffies.h>
0021 #include <linux/mutex.h>
0022 #include <linux/firmware.h>
0023 
0024 #include <media/dvb_frontend.h>
0025 #include <media/dmxdev.h>
0026 #include <media/dvb_demux.h>
0027 #include <media/dvb_net.h>
0028 #include "ves1820.h"
0029 #include "cx22700.h"
0030 #include "tda1004x.h"
0031 #include "stv0299.h"
0032 #include "tda8083.h"
0033 #include "stv0297.h"
0034 #include "lnbp21.h"
0035 
0036 #include <linux/dvb/frontend.h>
0037 #include <linux/dvb/dmx.h>
0038 #include <linux/pci.h>
0039 
0040 /*
0041   TTUSB_HWSECTIONS:
0042     the DSP supports filtering in hardware, however, since the "muxstream"
0043     is a bit braindead (no matching channel masks or no matching filter mask),
0044     we won't support this - yet. it doesn't event support negative filters,
0045     so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
0046     parse TS data. USB bandwidth will be a problem when having large
0047     datastreams, especially for dvb-net, but hey, that's not my problem.
0048 
0049   TTUSB_DISEQC, TTUSB_TONE:
0050     let the STC do the diseqc/tone stuff. this isn't supported at least with
0051     my TTUSB, so let it undef'd unless you want to implement another
0052     frontend. never tested.
0053 
0054   debug:
0055     define it to > 3 for really hardcore debugging. you probably don't want
0056     this unless the device doesn't load at all. > 2 for bandwidth statistics.
0057 */
0058 
0059 static int debug;
0060 module_param(debug, int, 0644);
0061 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
0062 
0063 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0064 
0065 #define dprintk(fmt, arg...) do {                   \
0066     if (debug)                          \
0067         printk(KERN_DEBUG pr_fmt("%s: " fmt),           \
0068                __func__, ##arg);                \
0069 } while (0)
0070 
0071 
0072 #define ISO_BUF_COUNT      4
0073 #define FRAMES_PER_ISO_BUF 4
0074 #define ISO_FRAME_SIZE     912
0075 #define TTUSB_MAXCHANNEL   32
0076 #ifdef TTUSB_HWSECTIONS
0077 #define TTUSB_MAXFILTER    16   /* ??? */
0078 #endif
0079 
0080 #define TTUSB_REV_2_2   0x22
0081 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
0082 
0083 #define MAX_SEND    0x28
0084 #define MAX_RCV     0x20
0085 
0086 /*
0087  *  since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
0088  *  the dvb_demux field must be the first in struct!!
0089  */
0090 struct ttusb {
0091     struct dvb_demux dvb_demux;
0092     struct dmxdev dmxdev;
0093     struct dvb_net dvbnet;
0094 
0095     /* and one for USB access. */
0096     struct mutex semi2c;
0097     struct mutex semusb;
0098 
0099     struct dvb_adapter adapter;
0100     struct usb_device *dev;
0101 
0102     struct i2c_adapter i2c_adap;
0103 
0104     int disconnecting;
0105     int iso_streaming;
0106 
0107     unsigned int bulk_out_pipe;
0108     unsigned int bulk_in_pipe;
0109     unsigned int isoc_in_pipe;
0110 
0111     void *iso_buffer;
0112 
0113     struct urb *iso_urb[ISO_BUF_COUNT];
0114 
0115     int running_feed_count;
0116     int last_channel;
0117     int last_filter;
0118 
0119     u8 c;           /* transaction counter, wraps around...  */
0120     enum fe_sec_tone_mode tone;
0121     enum fe_sec_voltage voltage;
0122 
0123     int mux_state;      // 0..2 - MuxSyncWord, 3 - nMuxPacks,    4 - muxpack
0124     u8 mux_npacks;
0125     u8 muxpack[256 + 8];
0126     int muxpack_ptr, muxpack_len;
0127 
0128     int insync;
0129 
0130     int cc;         /* MuxCounter - will increment on EVERY MUX PACKET */
0131     /* (including stuffing. yes. really.) */
0132 
0133     u8 send_buf[MAX_SEND];
0134     u8 last_result[MAX_RCV];
0135 
0136     int revision;
0137 
0138     struct dvb_frontend* fe;
0139 };
0140 
0141 static int ttusb_cmd(struct ttusb *ttusb, u8 *data, int len, int len_result)
0142 {
0143     int actual_len;
0144     int err;
0145 
0146     if (mutex_lock_interruptible(&ttusb->semusb) < 0)
0147         return -EAGAIN;
0148 
0149     if (debug >= 3)
0150         dprintk("> %*ph\n", len, data);
0151 
0152     memcpy(data, ttusb->send_buf, len);
0153 
0154     err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
0155                ttusb->send_buf, len, &actual_len, 1000);
0156     if (err != 0) {
0157         dprintk("usb_bulk_msg(send) failed, err == %i!\n", err);
0158         goto err;
0159     }
0160     if (actual_len != len) {
0161         err = -EIO;
0162         dprintk("only wrote %d of %d bytes\n",
0163             actual_len, len);
0164         goto err;
0165     }
0166 
0167     err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
0168                ttusb->last_result, MAX_RCV, &actual_len, 1000);
0169 
0170     if (err != 0) {
0171         pr_err("cmd xter failed, receive error %d\n", err);
0172         goto err;
0173     }
0174 
0175     if (debug >= 3) {
0176         actual_len = ttusb->last_result[3] + 4;
0177         dprintk("< %*ph\n", actual_len, ttusb->last_result);
0178     }
0179 
0180     if (len_result)
0181         memcpy(ttusb->send_buf, ttusb->last_result, len_result);
0182 
0183 err:
0184     mutex_unlock(&ttusb->semusb);
0185     return err;
0186 }
0187 
0188 static int ttusb_i2c_msg(struct ttusb *ttusb,
0189           u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
0190           u8 rcv_len)
0191 {
0192     u8 b[MAX_SEND];
0193     u8 id = ++ttusb->c;
0194     int i, err;
0195 
0196     if (snd_len > MAX_SEND - 7 || rcv_len > MAX_RCV - 7)
0197         return -EINVAL;
0198 
0199     b[0] = 0xaa;
0200     b[1] = id;
0201     b[2] = 0x31;
0202     b[3] = snd_len + 3;
0203     b[4] = addr << 1;
0204     b[5] = snd_len;
0205     b[6] = rcv_len;
0206 
0207     for (i = 0; i < snd_len; i++)
0208         b[7 + i] = snd_buf[i];
0209 
0210     err = ttusb_cmd(ttusb, b, snd_len + 7, MAX_RCV);
0211 
0212     if (err)
0213         return -EREMOTEIO;
0214 
0215     /* check if the i2c transaction was successful */
0216     if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
0217 
0218     if (rcv_len > 0) {
0219 
0220         if (err || b[0] != 0x55 || b[1] != id) {
0221             dprintk("usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
0222                 err, id);
0223             return -EREMOTEIO;
0224         }
0225 
0226         for (i = 0; i < rcv_len; i++)
0227             rcv_buf[i] = b[7 + i];
0228     }
0229 
0230     return rcv_len;
0231 }
0232 
0233 static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
0234 {
0235     struct ttusb *ttusb = i2c_get_adapdata(adapter);
0236     int i = 0;
0237     int inc;
0238 
0239     if (mutex_lock_interruptible(&ttusb->semi2c) < 0)
0240         return -EAGAIN;
0241 
0242     while (i < num) {
0243         u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
0244         int err;
0245 
0246         if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
0247             addr = msg[i].addr;
0248             snd_buf = msg[i].buf;
0249             snd_len = msg[i].len;
0250             rcv_buf = msg[i + 1].buf;
0251             rcv_len = msg[i + 1].len;
0252             inc = 2;
0253         } else {
0254             addr = msg[i].addr;
0255             snd_buf = msg[i].buf;
0256             snd_len = msg[i].len;
0257             rcv_buf = NULL;
0258             rcv_len = 0;
0259             inc = 1;
0260         }
0261 
0262         err = ttusb_i2c_msg(ttusb, addr,
0263                     snd_buf, snd_len, rcv_buf, rcv_len);
0264 
0265         if (err < rcv_len) {
0266             dprintk("i == %i\n", i);
0267             break;
0268         }
0269 
0270         i += inc;
0271     }
0272 
0273     mutex_unlock(&ttusb->semi2c);
0274     return i;
0275 }
0276 
0277 static int ttusb_boot_dsp(struct ttusb *ttusb)
0278 {
0279     const struct firmware *fw;
0280     int i, err;
0281     u8 b[40];
0282 
0283     err = request_firmware(&fw, "ttusb-budget/dspbootcode.bin",
0284                    &ttusb->dev->dev);
0285     if (err) {
0286         pr_err("failed to request firmware\n");
0287         return err;
0288     }
0289 
0290     /* BootBlock */
0291     b[0] = 0xaa;
0292     b[2] = 0x13;
0293     b[3] = 28;
0294 
0295     /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
0296     /* 32 is max packet size, no messages should be split. */
0297     for (i = 0; i < fw->size; i += 28) {
0298         memcpy(&b[4], &fw->data[i], 28);
0299 
0300         b[1] = ++ttusb->c;
0301 
0302         err = ttusb_cmd(ttusb, b, 32, 0);
0303         if (err)
0304             goto done;
0305     }
0306 
0307     /* last block ... */
0308     b[1] = ++ttusb->c;
0309     b[2] = 0x13;
0310     b[3] = 0;
0311 
0312     err = ttusb_cmd(ttusb, b, 4, 0);
0313     if (err)
0314         goto done;
0315 
0316     /* BootEnd */
0317     b[1] = ++ttusb->c;
0318     b[2] = 0x14;
0319     b[3] = 0;
0320 
0321     err = ttusb_cmd(ttusb, b, 4, 0);
0322 
0323       done:
0324     release_firmware(fw);
0325     if (err) {
0326         dprintk("usb_bulk_msg() failed, return value %i!\n", err);
0327     }
0328 
0329     return err;
0330 }
0331 
0332 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
0333               int pid)
0334 {
0335     int err;
0336     /* SetChannel */
0337     u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
0338         (pid >> 8) & 0xff, pid & 0xff
0339     };
0340 
0341     err = ttusb_cmd(ttusb, b, sizeof(b), 0);
0342     return err;
0343 }
0344 
0345 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
0346 {
0347     int err;
0348     /* DelChannel */
0349     u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
0350 
0351     err = ttusb_cmd(ttusb, b, sizeof(b), 0);
0352     return err;
0353 }
0354 
0355 #ifdef TTUSB_HWSECTIONS
0356 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
0357              int associated_chan, u8 filter[8], u8 mask[8])
0358 {
0359     int err;
0360     /* SetFilter */
0361     u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
0362         filter[0], filter[1], filter[2], filter[3],
0363         filter[4], filter[5], filter[6], filter[7],
0364         filter[8], filter[9], filter[10], filter[11],
0365         mask[0], mask[1], mask[2], mask[3],
0366         mask[4], mask[5], mask[6], mask[7],
0367         mask[8], mask[9], mask[10], mask[11]
0368     };
0369 
0370     err = ttusb_cmd(ttusb, b, sizeof(b), 0);
0371     return err;
0372 }
0373 
0374 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
0375 {
0376     int err;
0377     /* DelFilter */
0378     u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
0379 
0380     err = ttusb_cmd(ttusb, b, sizeof(b), 0);
0381     return err;
0382 }
0383 #endif
0384 
0385 static int ttusb_init_controller(struct ttusb *ttusb)
0386 {
0387     u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
0388     u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
0389     u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
0390     /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
0391     u8 b3[] =
0392         { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
0393 
0394     u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
0395     u8 get_dsp_version[0x20] =
0396         { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
0397     int err;
0398 
0399     /* reset board */
0400     if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
0401         return err;
0402 
0403     /* reset board (again?) */
0404     if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
0405         return err;
0406 
0407     ttusb_boot_dsp(ttusb);
0408 
0409     /* set i2c bit rate */
0410     if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
0411         return err;
0412 
0413     if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 0)))
0414         return err;
0415 
0416     if ((err = ttusb_cmd(ttusb, get_version,
0417                  sizeof(get_version), sizeof(get_version))))
0418         return err;
0419 
0420     dprintk("stc-version: %c%c%c%c%c\n", get_version[4], get_version[5],
0421         get_version[6], get_version[7], get_version[8]);
0422 
0423     if (memcmp(get_version + 4, "V 0.0", 5) &&
0424         memcmp(get_version + 4, "V 1.1", 5) &&
0425         memcmp(get_version + 4, "V 2.1", 5) &&
0426         memcmp(get_version + 4, "V 2.2", 5)) {
0427         pr_err("unknown STC version %c%c%c%c%c, please report!\n",
0428                get_version[4], get_version[5],
0429                get_version[6], get_version[7], get_version[8]);
0430     }
0431 
0432     ttusb->revision = ((get_version[6] - '0') << 4) |
0433                (get_version[8] - '0');
0434 
0435     err =
0436         ttusb_cmd(ttusb, get_dsp_version,
0437               sizeof(get_dsp_version), sizeof(get_dsp_version));
0438     if (err)
0439         return err;
0440 
0441     pr_info("dsp-version: %c%c%c\n",
0442            get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
0443     return 0;
0444 }
0445 
0446 #ifdef TTUSB_DISEQC
0447 static int ttusb_send_diseqc(struct dvb_frontend* fe,
0448                  const struct dvb_diseqc_master_cmd *cmd)
0449 {
0450     struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
0451     u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
0452 
0453     int err;
0454 
0455     b[3] = 4 + 2 + cmd->msg_len;
0456     b[4] = 0xFF;        /* send diseqc master, not burst */
0457     b[5] = cmd->msg_len;
0458 
0459     memcpy(b + 5, cmd->msg, cmd->msg_len);
0460 
0461     /* Diseqc */
0462     if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
0463         dprintk("usb_bulk_msg() failed, return value %i!\n", err);
0464     }
0465 
0466     return err;
0467 }
0468 #endif
0469 
0470 static int ttusb_update_lnb(struct ttusb *ttusb)
0471 {
0472     u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
0473         ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
0474         ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
0475     };
0476     int err;
0477 
0478     /* SetLNB */
0479     if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
0480         dprintk("usb_bulk_msg() failed, return value %i!\n", err);
0481     }
0482 
0483     return err;
0484 }
0485 
0486 static int ttusb_set_voltage(struct dvb_frontend *fe,
0487                  enum fe_sec_voltage voltage)
0488 {
0489     struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
0490 
0491     ttusb->voltage = voltage;
0492     return ttusb_update_lnb(ttusb);
0493 }
0494 
0495 #ifdef TTUSB_TONE
0496 static int ttusb_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
0497 {
0498     struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
0499 
0500     ttusb->tone = tone;
0501     return ttusb_update_lnb(ttusb);
0502 }
0503 #endif
0504 
0505 
0506 #if 0
0507 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
0508 {
0509     u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
0510     int err, actual_len;
0511 
0512     err = ttusb_cmd(ttusb, b, sizeof(b), 0);
0513     if (err) {
0514         dprintk("usb_bulk_msg() failed, return value %i!\n", err);
0515     }
0516 }
0517 #endif
0518 
0519 /*****************************************************************************/
0520 
0521 #ifdef TTUSB_HWSECTIONS
0522 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
0523                  const u8 * data, int len);
0524 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
0525                   const u8 * data, int len);
0526 #endif
0527 
0528 static int numpkt, numts, numstuff, numsec, numinvalid;
0529 static unsigned long lastj;
0530 
0531 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
0532                int len)
0533 {
0534     u16 csum = 0, cc;
0535     int i;
0536 
0537     if (len < 4 || len & 0x1) {
0538         pr_warn("muxpack has invalid len %d\n", len);
0539         numinvalid++;
0540         return;
0541     }
0542 
0543     for (i = 0; i < len; i += 2)
0544         csum ^= le16_to_cpup((__le16 *) (muxpack + i));
0545     if (csum) {
0546         pr_warn("muxpack with incorrect checksum, ignoring\n");
0547         numinvalid++;
0548         return;
0549     }
0550 
0551     cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
0552     cc &= 0x7FFF;
0553     if ((cc != ttusb->cc) && (ttusb->cc != -1))
0554         pr_warn("cc discontinuity (%d frames missing)\n",
0555             (cc - ttusb->cc) & 0x7FFF);
0556     ttusb->cc = (cc + 1) & 0x7FFF;
0557     if (muxpack[0] & 0x80) {
0558 #ifdef TTUSB_HWSECTIONS
0559         /* section data */
0560         int pusi = muxpack[0] & 0x40;
0561         int channel = muxpack[0] & 0x1F;
0562         int payload = muxpack[1];
0563         const u8 *data = muxpack + 2;
0564         /* check offset flag */
0565         if (muxpack[0] & 0x20)
0566             data++;
0567 
0568         ttusb_handle_sec_data(ttusb->channel + channel, data,
0569                       payload);
0570         data += payload;
0571 
0572         if ((!!(ttusb->muxpack[0] & 0x20)) ^
0573             !!(ttusb->muxpack[1] & 1))
0574             data++;
0575 #warning TODO: pusi
0576         dprintk("cc: %04x\n", (data[0] << 8) | data[1]);
0577 #endif
0578         numsec++;
0579     } else if (muxpack[0] == 0x47) {
0580 #ifdef TTUSB_HWSECTIONS
0581         /* we have TS data here! */
0582         int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
0583         int channel;
0584         for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
0585             if (ttusb->channel[channel].active
0586                 && (pid == ttusb->channel[channel].pid))
0587                 ttusb_handle_ts_data(ttusb->channel +
0588                              channel, muxpack,
0589                              188);
0590 #endif
0591         numts++;
0592         dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
0593     } else if (muxpack[0] != 0) {
0594         numinvalid++;
0595         pr_err("illegal muxpack type %02x\n", muxpack[0]);
0596     } else
0597         numstuff++;
0598 }
0599 
0600 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
0601 {
0602     int maxwork = 1024;
0603     while (len) {
0604         if (!(maxwork--)) {
0605             pr_err("too much work\n");
0606             break;
0607         }
0608 
0609         switch (ttusb->mux_state) {
0610         case 0:
0611         case 1:
0612         case 2:
0613             len--;
0614             if (*data++ == 0xAA)
0615                 ++ttusb->mux_state;
0616             else {
0617                 ttusb->mux_state = 0;
0618                 if (ttusb->insync) {
0619                     pr_info("lost sync.\n");
0620                     ttusb->insync = 0;
0621                 }
0622             }
0623             break;
0624         case 3:
0625             ttusb->insync = 1;
0626             len--;
0627             ttusb->mux_npacks = *data++;
0628             ++ttusb->mux_state;
0629             ttusb->muxpack_ptr = 0;
0630             /* maximum bytes, until we know the length */
0631             ttusb->muxpack_len = 2;
0632             break;
0633         case 4:
0634             {
0635                 int avail;
0636                 avail = len;
0637                 if (avail >
0638                     (ttusb->muxpack_len -
0639                      ttusb->muxpack_ptr))
0640                     avail =
0641                         ttusb->muxpack_len -
0642                         ttusb->muxpack_ptr;
0643                 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
0644                        data, avail);
0645                 ttusb->muxpack_ptr += avail;
0646                 BUG_ON(ttusb->muxpack_ptr > 264);
0647                 data += avail;
0648                 len -= avail;
0649                 /* determine length */
0650                 if (ttusb->muxpack_ptr == 2) {
0651                     if (ttusb->muxpack[0] & 0x80) {
0652                         ttusb->muxpack_len =
0653                             ttusb->muxpack[1] + 2;
0654                         if (ttusb->
0655                             muxpack[0] & 0x20)
0656                             ttusb->
0657                                 muxpack_len++;
0658                         if ((!!
0659                              (ttusb->
0660                               muxpack[0] & 0x20)) ^
0661                             !!(ttusb->
0662                                muxpack[1] & 1))
0663                             ttusb->
0664                                 muxpack_len++;
0665                         ttusb->muxpack_len += 4;
0666                     } else if (ttusb->muxpack[0] ==
0667                            0x47)
0668                         ttusb->muxpack_len =
0669                             188 + 4;
0670                     else if (ttusb->muxpack[0] == 0x00)
0671                         ttusb->muxpack_len =
0672                             ttusb->muxpack[1] + 2 +
0673                             4;
0674                     else {
0675                         dprintk("invalid state: first byte is %x\n",
0676                             ttusb->muxpack[0]);
0677                         ttusb->mux_state = 0;
0678                     }
0679                 }
0680 
0681             /*
0682              * if length is valid and we reached the end:
0683              * goto next muxpack
0684              */
0685                 if ((ttusb->muxpack_ptr >= 2) &&
0686                     (ttusb->muxpack_ptr ==
0687                      ttusb->muxpack_len)) {
0688                     ttusb_process_muxpack(ttusb,
0689                                   ttusb->
0690                                   muxpack,
0691                                   ttusb->
0692                                   muxpack_ptr);
0693                     ttusb->muxpack_ptr = 0;
0694                     /* maximum bytes, until we know the length */
0695                     ttusb->muxpack_len = 2;
0696 
0697                 /*
0698                  * no muxpacks left?
0699                  * return to search-sync state
0700                  */
0701                     if (!ttusb->mux_npacks--) {
0702                         ttusb->mux_state = 0;
0703                         break;
0704                     }
0705                 }
0706                 break;
0707             }
0708         default:
0709             BUG();
0710             break;
0711         }
0712     }
0713 }
0714 
0715 static void ttusb_iso_irq(struct urb *urb)
0716 {
0717     struct ttusb *ttusb = urb->context;
0718     struct usb_iso_packet_descriptor *d;
0719     u8 *data;
0720     int len, i;
0721 
0722     if (!ttusb->iso_streaming)
0723         return;
0724 
0725     if (!urb->status) {
0726         for (i = 0; i < urb->number_of_packets; ++i) {
0727             numpkt++;
0728             if (time_after_eq(jiffies, lastj + HZ)) {
0729                 dprintk("frames/s: %lu (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
0730                     numpkt * HZ / (jiffies - lastj),
0731                     numts, numstuff, numsec, numinvalid,
0732                     numts + numstuff + numsec + numinvalid);
0733                 numts = numstuff = numsec = numinvalid = 0;
0734                 lastj = jiffies;
0735                 numpkt = 0;
0736             }
0737             d = &urb->iso_frame_desc[i];
0738             data = urb->transfer_buffer + d->offset;
0739             len = d->actual_length;
0740             d->actual_length = 0;
0741             d->status = 0;
0742             ttusb_process_frame(ttusb, data, len);
0743         }
0744     }
0745     usb_submit_urb(urb, GFP_ATOMIC);
0746 }
0747 
0748 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
0749 {
0750     int i;
0751 
0752     for (i = 0; i < ISO_BUF_COUNT; i++)
0753         usb_free_urb(ttusb->iso_urb[i]);
0754     kfree(ttusb->iso_buffer);
0755 }
0756 
0757 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
0758 {
0759     int i;
0760 
0761     ttusb->iso_buffer = kcalloc(FRAMES_PER_ISO_BUF * ISO_BUF_COUNT,
0762             ISO_FRAME_SIZE, GFP_KERNEL);
0763     if (!ttusb->iso_buffer)
0764         return -ENOMEM;
0765 
0766     for (i = 0; i < ISO_BUF_COUNT; i++) {
0767         struct urb *urb;
0768 
0769         if (!
0770             (urb =
0771              usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
0772             ttusb_free_iso_urbs(ttusb);
0773             return -ENOMEM;
0774         }
0775 
0776         ttusb->iso_urb[i] = urb;
0777     }
0778 
0779     return 0;
0780 }
0781 
0782 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
0783 {
0784     int i;
0785 
0786     for (i = 0; i < ISO_BUF_COUNT; i++)
0787         usb_kill_urb(ttusb->iso_urb[i]);
0788 
0789     ttusb->iso_streaming = 0;
0790 }
0791 
0792 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
0793 {
0794     int i, j, err, buffer_offset = 0;
0795 
0796     if (ttusb->iso_streaming) {
0797         pr_err("iso xfer already running!\n");
0798         return 0;
0799     }
0800 
0801     ttusb->cc = -1;
0802     ttusb->insync = 0;
0803     ttusb->mux_state = 0;
0804 
0805     for (i = 0; i < ISO_BUF_COUNT; i++) {
0806         int frame_offset = 0;
0807         struct urb *urb = ttusb->iso_urb[i];
0808 
0809         urb->dev = ttusb->dev;
0810         urb->context = ttusb;
0811         urb->complete = ttusb_iso_irq;
0812         urb->pipe = ttusb->isoc_in_pipe;
0813         urb->transfer_flags = URB_ISO_ASAP;
0814         urb->interval = 1;
0815         urb->number_of_packets = FRAMES_PER_ISO_BUF;
0816         urb->transfer_buffer_length =
0817             ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
0818         urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
0819         buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
0820 
0821         for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
0822             urb->iso_frame_desc[j].offset = frame_offset;
0823             urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
0824             frame_offset += ISO_FRAME_SIZE;
0825         }
0826     }
0827 
0828     for (i = 0; i < ISO_BUF_COUNT; i++) {
0829         if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
0830             ttusb_stop_iso_xfer(ttusb);
0831             pr_err("failed urb submission (%i: err = %i)!\n",
0832                    i, err);
0833             return err;
0834         }
0835     }
0836 
0837     ttusb->iso_streaming = 1;
0838 
0839     return 0;
0840 }
0841 
0842 #ifdef TTUSB_HWSECTIONS
0843 static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
0844               int len)
0845 {
0846     dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
0847 }
0848 
0849 static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
0850                int len)
0851 {
0852 //      struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
0853 #error TODO: handle ugly stuff
0854 //      dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
0855 }
0856 #endif
0857 
0858 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
0859 {
0860     struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
0861     int feed_type = 1;
0862 
0863     dprintk("ttusb_start_feed\n");
0864 
0865     switch (dvbdmxfeed->type) {
0866     case DMX_TYPE_TS:
0867         break;
0868     case DMX_TYPE_SEC:
0869         break;
0870     default:
0871         return -EINVAL;
0872     }
0873 
0874     if (dvbdmxfeed->type == DMX_TYPE_TS) {
0875         switch (dvbdmxfeed->pes_type) {
0876         case DMX_PES_VIDEO:
0877         case DMX_PES_AUDIO:
0878         case DMX_PES_TELETEXT:
0879         case DMX_PES_PCR:
0880         case DMX_PES_OTHER:
0881             break;
0882         default:
0883             return -EINVAL;
0884         }
0885     }
0886 
0887 #ifdef TTUSB_HWSECTIONS
0888 #error TODO: allocate filters
0889     if (dvbdmxfeed->type == DMX_TYPE_TS) {
0890         feed_type = 1;
0891     } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
0892         feed_type = 2;
0893     }
0894 #endif
0895 
0896     ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
0897 
0898     if (0 == ttusb->running_feed_count++)
0899         ttusb_start_iso_xfer(ttusb);
0900 
0901     return 0;
0902 }
0903 
0904 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
0905 {
0906     struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
0907 
0908     ttusb_del_channel(ttusb, dvbdmxfeed->index);
0909 
0910     if (--ttusb->running_feed_count == 0)
0911         ttusb_stop_iso_xfer(ttusb);
0912 
0913     return 0;
0914 }
0915 
0916 static int ttusb_setup_interfaces(struct ttusb *ttusb)
0917 {
0918     usb_set_interface(ttusb->dev, 1, 1);
0919 
0920     ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
0921     ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
0922     ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
0923 
0924     return 0;
0925 }
0926 
0927 #if 0
0928 static u8 stc_firmware[8192];
0929 
0930 static int stc_open(struct inode *inode, struct file *file)
0931 {
0932     struct ttusb *ttusb = file->private_data;
0933     int addr;
0934 
0935     for (addr = 0; addr < 8192; addr += 16) {
0936         u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
0937         ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
0938                   16);
0939     }
0940 
0941     return 0;
0942 }
0943 
0944 static ssize_t stc_read(struct file *file, char *buf, size_t count,
0945          loff_t *offset)
0946 {
0947     return simple_read_from_buffer(buf, count, offset, stc_firmware, 8192);
0948 }
0949 
0950 static int stc_release(struct inode *inode, struct file *file)
0951 {
0952     return 0;
0953 }
0954 
0955 static const struct file_operations stc_fops = {
0956     .owner = THIS_MODULE,
0957     .read = stc_read,
0958     .open = stc_open,
0959     .release = stc_release,
0960 };
0961 #endif
0962 
0963 static u32 functionality(struct i2c_adapter *adapter)
0964 {
0965     return I2C_FUNC_I2C;
0966 }
0967 
0968 
0969 
0970 static int alps_tdmb7_tuner_set_params(struct dvb_frontend *fe)
0971 {
0972     struct dtv_frontend_properties *p = &fe->dtv_property_cache;
0973     struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
0974     u8 data[4];
0975     struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
0976     u32 div;
0977 
0978     div = (p->frequency + 36166667) / 166667;
0979 
0980     data[0] = (div >> 8) & 0x7f;
0981     data[1] = div & 0xff;
0982     data[2] = ((div >> 10) & 0x60) | 0x85;
0983     data[3] = p->frequency < 592000000 ? 0x40 : 0x80;
0984 
0985     if (fe->ops.i2c_gate_ctrl)
0986         fe->ops.i2c_gate_ctrl(fe, 1);
0987     if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
0988     return 0;
0989 }
0990 
0991 static struct cx22700_config alps_tdmb7_config = {
0992     .demod_address = 0x43,
0993 };
0994 
0995 
0996 
0997 
0998 
0999 static int philips_tdm1316l_tuner_init(struct dvb_frontend* fe)
1000 {
1001     struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1002     static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1003     static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1004     struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1005 
1006     // setup PLL configuration
1007     if (fe->ops.i2c_gate_ctrl)
1008         fe->ops.i2c_gate_ctrl(fe, 1);
1009     if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1010     msleep(1);
1011 
1012     // disable the mc44BC374c (do not check for errors)
1013     tuner_msg.addr = 0x65;
1014     tuner_msg.buf = disable_mc44BC374c;
1015     tuner_msg.len = sizeof(disable_mc44BC374c);
1016     if (fe->ops.i2c_gate_ctrl)
1017         fe->ops.i2c_gate_ctrl(fe, 1);
1018     if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1019         i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1020     }
1021 
1022     return 0;
1023 }
1024 
1025 static int philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe)
1026 {
1027     struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1028     struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1029     u8 tuner_buf[4];
1030     struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1031     int tuner_frequency = 0;
1032     u8 band, cp, filter;
1033 
1034     // determine charge pump
1035     tuner_frequency = p->frequency + 36130000;
1036     if (tuner_frequency < 87000000) return -EINVAL;
1037     else if (tuner_frequency < 130000000) cp = 3;
1038     else if (tuner_frequency < 160000000) cp = 5;
1039     else if (tuner_frequency < 200000000) cp = 6;
1040     else if (tuner_frequency < 290000000) cp = 3;
1041     else if (tuner_frequency < 420000000) cp = 5;
1042     else if (tuner_frequency < 480000000) cp = 6;
1043     else if (tuner_frequency < 620000000) cp = 3;
1044     else if (tuner_frequency < 830000000) cp = 5;
1045     else if (tuner_frequency < 895000000) cp = 7;
1046     else return -EINVAL;
1047 
1048     // determine band
1049     if (p->frequency < 49000000)
1050         return -EINVAL;
1051     else if (p->frequency < 159000000)
1052         band = 1;
1053     else if (p->frequency < 444000000)
1054         band = 2;
1055     else if (p->frequency < 861000000)
1056         band = 4;
1057     else return -EINVAL;
1058 
1059     // setup PLL filter
1060     switch (p->bandwidth_hz) {
1061     case 6000000:
1062         tda1004x_writereg(fe, 0x0C, 0);
1063         filter = 0;
1064         break;
1065 
1066     case 7000000:
1067         tda1004x_writereg(fe, 0x0C, 0);
1068         filter = 0;
1069         break;
1070 
1071     case 8000000:
1072         tda1004x_writereg(fe, 0x0C, 0xFF);
1073         filter = 1;
1074         break;
1075 
1076     default:
1077         return -EINVAL;
1078     }
1079 
1080     // calculate divisor
1081     // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1082     tuner_frequency = (((p->frequency / 1000) * 6) + 217280) / 1000;
1083 
1084     // setup tuner buffer
1085     tuner_buf[0] = tuner_frequency >> 8;
1086     tuner_buf[1] = tuner_frequency & 0xff;
1087     tuner_buf[2] = 0xca;
1088     tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1089 
1090     if (fe->ops.i2c_gate_ctrl)
1091         fe->ops.i2c_gate_ctrl(fe, 1);
1092     if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1093         return -EIO;
1094 
1095     msleep(1);
1096     return 0;
1097 }
1098 
1099 static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1100 {
1101     struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1102 
1103     return request_firmware(fw, name, &ttusb->dev->dev);
1104 }
1105 
1106 static struct tda1004x_config philips_tdm1316l_config = {
1107 
1108     .demod_address = 0x8,
1109     .invert = 1,
1110     .invert_oclk = 0,
1111     .request_firmware = philips_tdm1316l_request_firmware,
1112 };
1113 
1114 static u8 alps_bsbe1_inittab[] = {
1115     0x01, 0x15,
1116     0x02, 0x30,
1117     0x03, 0x00,
1118     0x04, 0x7d,             /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1119     0x05, 0x35,             /* I2CT = 0, SCLT = 1, SDAT = 1 */
1120     0x06, 0x40,             /* DAC not used, set to high impendance mode */
1121     0x07, 0x00,             /* DAC LSB */
1122     0x08, 0x40,             /* DiSEqC off, LNB power on OP2/LOCK pin on */
1123     0x09, 0x00,             /* FIFO */
1124     0x0c, 0x51,             /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1125     0x0d, 0x82,             /* DC offset compensation = ON, beta_agc1 = 2 */
1126     0x0e, 0x23,             /* alpha_tmg = 2, beta_tmg = 3 */
1127     0x10, 0x3f,             // AGC2  0x3d
1128     0x11, 0x84,
1129     0x12, 0xb9,
1130     0x15, 0xc9,             // lock detector threshold
1131     0x16, 0x00,
1132     0x17, 0x00,
1133     0x18, 0x00,
1134     0x19, 0x00,
1135     0x1a, 0x00,
1136     0x1f, 0x50,
1137     0x20, 0x00,
1138     0x21, 0x00,
1139     0x22, 0x00,
1140     0x23, 0x00,
1141     0x28, 0x00,             // out imp: normal  out type: parallel FEC mode:0
1142     0x29, 0x1e,             // 1/2 threshold
1143     0x2a, 0x14,             // 2/3 threshold
1144     0x2b, 0x0f,             // 3/4 threshold
1145     0x2c, 0x09,             // 5/6 threshold
1146     0x2d, 0x05,             // 7/8 threshold
1147     0x2e, 0x01,
1148     0x31, 0x1f,             // test all FECs
1149     0x32, 0x19,             // viterbi and synchro search
1150     0x33, 0xfc,             // rs control
1151     0x34, 0x93,             // error control
1152     0x0f, 0x92,
1153     0xff, 0xff
1154 };
1155 
1156 static u8 alps_bsru6_inittab[] = {
1157     0x01, 0x15,
1158     0x02, 0x30,
1159     0x03, 0x00,
1160     0x04, 0x7d,     /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1161     0x05, 0x35,     /* I2CT = 0, SCLT = 1, SDAT = 1 */
1162     0x06, 0x40,     /* DAC not used, set to high impendance mode */
1163     0x07, 0x00,     /* DAC LSB */
1164     0x08, 0x40,     /* DiSEqC off, LNB power on OP2/LOCK pin on */
1165     0x09, 0x00,     /* FIFO */
1166     0x0c, 0x51,     /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1167     0x0d, 0x82,     /* DC offset compensation = ON, beta_agc1 = 2 */
1168     0x0e, 0x23,     /* alpha_tmg = 2, beta_tmg = 3 */
1169     0x10, 0x3f,     // AGC2  0x3d
1170     0x11, 0x84,
1171     0x12, 0xb9,
1172     0x15, 0xc9,     // lock detector threshold
1173     0x16, 0x00,
1174     0x17, 0x00,
1175     0x18, 0x00,
1176     0x19, 0x00,
1177     0x1a, 0x00,
1178     0x1f, 0x50,
1179     0x20, 0x00,
1180     0x21, 0x00,
1181     0x22, 0x00,
1182     0x23, 0x00,
1183     0x28, 0x00,     // out imp: normal  out type: parallel FEC mode:0
1184     0x29, 0x1e,     // 1/2 threshold
1185     0x2a, 0x14,     // 2/3 threshold
1186     0x2b, 0x0f,     // 3/4 threshold
1187     0x2c, 0x09,     // 5/6 threshold
1188     0x2d, 0x05,     // 7/8 threshold
1189     0x2e, 0x01,
1190     0x31, 0x1f,     // test all FECs
1191     0x32, 0x19,     // viterbi and synchro search
1192     0x33, 0xfc,     // rs control
1193     0x34, 0x93,     // error control
1194     0x0f, 0x52,
1195     0xff, 0xff
1196 };
1197 
1198 static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1199 {
1200     u8 aclk = 0;
1201     u8 bclk = 0;
1202 
1203     if (srate < 1500000) {
1204         aclk = 0xb7;
1205         bclk = 0x47;
1206     } else if (srate < 3000000) {
1207         aclk = 0xb7;
1208         bclk = 0x4b;
1209     } else if (srate < 7000000) {
1210         aclk = 0xb7;
1211         bclk = 0x4f;
1212     } else if (srate < 14000000) {
1213         aclk = 0xb7;
1214         bclk = 0x53;
1215     } else if (srate < 30000000) {
1216         aclk = 0xb6;
1217         bclk = 0x53;
1218     } else if (srate < 45000000) {
1219         aclk = 0xb4;
1220         bclk = 0x51;
1221     }
1222 
1223     stv0299_writereg(fe, 0x13, aclk);
1224     stv0299_writereg(fe, 0x14, bclk);
1225     stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1226     stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1227     stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1228 
1229     return 0;
1230 }
1231 
1232 static int philips_tsa5059_tuner_set_params(struct dvb_frontend *fe)
1233 {
1234     struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1235     struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1236     u8 buf[4];
1237     u32 div;
1238     struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1239 
1240     if ((p->frequency < 950000) || (p->frequency > 2150000))
1241         return -EINVAL;
1242 
1243     div = (p->frequency + (125 - 1)) / 125; /* round correctly */
1244     buf[0] = (div >> 8) & 0x7f;
1245     buf[1] = div & 0xff;
1246     buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1247     buf[3] = 0xC4;
1248 
1249     if (p->frequency > 1530000)
1250         buf[3] = 0xC0;
1251 
1252     /* BSBE1 wants XCE bit set */
1253     if (ttusb->revision == TTUSB_REV_2_2)
1254         buf[3] |= 0x20;
1255 
1256     if (fe->ops.i2c_gate_ctrl)
1257         fe->ops.i2c_gate_ctrl(fe, 1);
1258     if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1259         return -EIO;
1260 
1261     return 0;
1262 }
1263 
1264 static struct stv0299_config alps_stv0299_config = {
1265     .demod_address = 0x68,
1266     .inittab = alps_bsru6_inittab,
1267     .mclk = 88000000UL,
1268     .invert = 1,
1269     .skip_reinit = 0,
1270     .lock_output = STV0299_LOCKOUTPUT_1,
1271     .volt13_op0_op1 = STV0299_VOLT13_OP1,
1272     .min_delay_ms = 100,
1273     .set_symbol_rate = alps_stv0299_set_symbol_rate,
1274 };
1275 
1276 static int ttusb_novas_grundig_29504_491_tuner_set_params(struct dvb_frontend *fe)
1277 {
1278     struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1279     struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1280     u8 buf[4];
1281     u32 div;
1282     struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1283 
1284     div = p->frequency / 125;
1285 
1286     buf[0] = (div >> 8) & 0x7f;
1287     buf[1] = div & 0xff;
1288     buf[2] = 0x8e;
1289     buf[3] = 0x00;
1290 
1291     if (fe->ops.i2c_gate_ctrl)
1292         fe->ops.i2c_gate_ctrl(fe, 1);
1293     if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1294         return -EIO;
1295 
1296     return 0;
1297 }
1298 
1299 static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1300 
1301     .demod_address = 0x68,
1302 };
1303 
1304 static int alps_tdbe2_tuner_set_params(struct dvb_frontend *fe)
1305 {
1306     struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1307     struct ttusb* ttusb = fe->dvb->priv;
1308     u32 div;
1309     u8 data[4];
1310     struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1311 
1312     div = (p->frequency + 35937500 + 31250) / 62500;
1313 
1314     data[0] = (div >> 8) & 0x7f;
1315     data[1] = div & 0xff;
1316     data[2] = 0x85 | ((div >> 10) & 0x60);
1317     data[3] = (p->frequency < 174000000 ? 0x88 : p->frequency < 470000000 ? 0x84 : 0x81);
1318 
1319     if (fe->ops.i2c_gate_ctrl)
1320         fe->ops.i2c_gate_ctrl(fe, 1);
1321     if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1322         return -EIO;
1323 
1324     return 0;
1325 }
1326 
1327 
1328 static struct ves1820_config alps_tdbe2_config = {
1329     .demod_address = 0x09,
1330     .xin = 57840000UL,
1331     .invert = 1,
1332     .selagc = VES1820_SELAGC_SIGNAMPERR,
1333 };
1334 
1335 static u8 read_pwm(struct ttusb* ttusb)
1336 {
1337     u8 b = 0xff;
1338     u8 pwm;
1339     struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1340                 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1341 
1342     if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1343         pwm = 0x48;
1344 
1345     return pwm;
1346 }
1347 
1348 
1349 static int dvbc_philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe)
1350 {
1351     struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1352     struct ttusb *ttusb = (struct ttusb *) fe->dvb->priv;
1353     u8 tuner_buf[5];
1354     struct i2c_msg tuner_msg = {.addr = 0x60,
1355                     .flags = 0,
1356                     .buf = tuner_buf,
1357                     .len = sizeof(tuner_buf) };
1358     int tuner_frequency = 0;
1359     u8 band, cp, filter;
1360 
1361     // determine charge pump
1362     tuner_frequency = p->frequency;
1363     if      (tuner_frequency <  87000000) {return -EINVAL;}
1364     else if (tuner_frequency < 130000000) {cp = 3; band = 1;}
1365     else if (tuner_frequency < 160000000) {cp = 5; band = 1;}
1366     else if (tuner_frequency < 200000000) {cp = 6; band = 1;}
1367     else if (tuner_frequency < 290000000) {cp = 3; band = 2;}
1368     else if (tuner_frequency < 420000000) {cp = 5; band = 2;}
1369     else if (tuner_frequency < 480000000) {cp = 6; band = 2;}
1370     else if (tuner_frequency < 620000000) {cp = 3; band = 4;}
1371     else if (tuner_frequency < 830000000) {cp = 5; band = 4;}
1372     else if (tuner_frequency < 895000000) {cp = 7; band = 4;}
1373     else {return -EINVAL;}
1374 
1375     // assume PLL filter should always be 8MHz for the moment.
1376     filter = 1;
1377 
1378     // calculate divisor
1379     // (Finput + Fif)/Fref; Fif = 36125000 Hz, Fref = 62500 Hz
1380     tuner_frequency = ((p->frequency + 36125000) / 62500);
1381 
1382     // setup tuner buffer
1383     tuner_buf[0] = tuner_frequency >> 8;
1384     tuner_buf[1] = tuner_frequency & 0xff;
1385     tuner_buf[2] = 0xc8;
1386     tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1387     tuner_buf[4] = 0x80;
1388 
1389     if (fe->ops.i2c_gate_ctrl)
1390         fe->ops.i2c_gate_ctrl(fe, 1);
1391     if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1392         pr_err("dvbc_philips_tdm1316l_pll_set Error 1\n");
1393         return -EIO;
1394     }
1395 
1396     msleep(50);
1397 
1398     if (fe->ops.i2c_gate_ctrl)
1399         fe->ops.i2c_gate_ctrl(fe, 1);
1400     if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1401         pr_err("dvbc_philips_tdm1316l_pll_set Error 2\n");
1402         return -EIO;
1403     }
1404 
1405     msleep(1);
1406 
1407     return 0;
1408 }
1409 
1410 static u8 dvbc_philips_tdm1316l_inittab[] = {
1411     0x80, 0x21,
1412     0x80, 0x20,
1413     0x81, 0x01,
1414     0x81, 0x00,
1415     0x00, 0x09,
1416     0x01, 0x69,
1417     0x03, 0x00,
1418     0x04, 0x00,
1419     0x07, 0x00,
1420     0x08, 0x00,
1421     0x20, 0x00,
1422     0x21, 0x40,
1423     0x22, 0x00,
1424     0x23, 0x00,
1425     0x24, 0x40,
1426     0x25, 0x88,
1427     0x30, 0xff,
1428     0x31, 0x00,
1429     0x32, 0xff,
1430     0x33, 0x00,
1431     0x34, 0x50,
1432     0x35, 0x7f,
1433     0x36, 0x00,
1434     0x37, 0x20,
1435     0x38, 0x00,
1436     0x40, 0x1c,
1437     0x41, 0xff,
1438     0x42, 0x29,
1439     0x43, 0x20,
1440     0x44, 0xff,
1441     0x45, 0x00,
1442     0x46, 0x00,
1443     0x49, 0x04,
1444     0x4a, 0xff,
1445     0x4b, 0x7f,
1446     0x52, 0x30,
1447     0x55, 0xae,
1448     0x56, 0x47,
1449     0x57, 0xe1,
1450     0x58, 0x3a,
1451     0x5a, 0x1e,
1452     0x5b, 0x34,
1453     0x60, 0x00,
1454     0x63, 0x00,
1455     0x64, 0x00,
1456     0x65, 0x00,
1457     0x66, 0x00,
1458     0x67, 0x00,
1459     0x68, 0x00,
1460     0x69, 0x00,
1461     0x6a, 0x02,
1462     0x6b, 0x00,
1463     0x70, 0xff,
1464     0x71, 0x00,
1465     0x72, 0x00,
1466     0x73, 0x00,
1467     0x74, 0x0c,
1468     0x80, 0x00,
1469     0x81, 0x00,
1470     0x82, 0x00,
1471     0x83, 0x00,
1472     0x84, 0x04,
1473     0x85, 0x80,
1474     0x86, 0x24,
1475     0x87, 0x78,
1476     0x88, 0x00,
1477     0x89, 0x00,
1478     0x90, 0x01,
1479     0x91, 0x01,
1480     0xa0, 0x00,
1481     0xa1, 0x00,
1482     0xa2, 0x00,
1483     0xb0, 0x91,
1484     0xb1, 0x0b,
1485     0xc0, 0x4b,
1486     0xc1, 0x00,
1487     0xc2, 0x00,
1488     0xd0, 0x00,
1489     0xd1, 0x00,
1490     0xd2, 0x00,
1491     0xd3, 0x00,
1492     0xd4, 0x00,
1493     0xd5, 0x00,
1494     0xde, 0x00,
1495     0xdf, 0x00,
1496     0x61, 0x38,
1497     0x62, 0x0a,
1498     0x53, 0x13,
1499     0x59, 0x08,
1500     0x55, 0x00,
1501     0x56, 0x40,
1502     0x57, 0x08,
1503     0x58, 0x3d,
1504     0x88, 0x10,
1505     0xa0, 0x00,
1506     0xa0, 0x00,
1507     0xa0, 0x00,
1508     0xa0, 0x04,
1509     0xff, 0xff,
1510 };
1511 
1512 static struct stv0297_config dvbc_philips_tdm1316l_config = {
1513     .demod_address = 0x1c,
1514     .inittab = dvbc_philips_tdm1316l_inittab,
1515     .invert = 0,
1516 };
1517 
1518 static void frontend_init(struct ttusb* ttusb)
1519 {
1520     switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1521     case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1522         // try the stv0299 based first
1523         ttusb->fe = dvb_attach(stv0299_attach, &alps_stv0299_config, &ttusb->i2c_adap);
1524         if (ttusb->fe != NULL) {
1525             ttusb->fe->ops.tuner_ops.set_params = philips_tsa5059_tuner_set_params;
1526 
1527             if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1528                 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1529                 dvb_attach(lnbp21_attach, ttusb->fe, &ttusb->i2c_adap, 0, 0);
1530             } else { // ALPS BSRU6
1531                 ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1532             }
1533             break;
1534         }
1535 
1536         // Grundig 29504-491
1537         ttusb->fe = dvb_attach(tda8083_attach, &ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1538         if (ttusb->fe != NULL) {
1539             ttusb->fe->ops.tuner_ops.set_params = ttusb_novas_grundig_29504_491_tuner_set_params;
1540             ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1541             break;
1542         }
1543         break;
1544 
1545     case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
1546         ttusb->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
1547         if (ttusb->fe != NULL) {
1548             ttusb->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
1549             break;
1550         }
1551 
1552         ttusb->fe = dvb_attach(stv0297_attach, &dvbc_philips_tdm1316l_config, &ttusb->i2c_adap);
1553         if (ttusb->fe != NULL) {
1554             ttusb->fe->ops.tuner_ops.set_params = dvbc_philips_tdm1316l_tuner_set_params;
1555             break;
1556         }
1557         break;
1558 
1559     case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1560         // try the ALPS TDMB7 first
1561         ttusb->fe = dvb_attach(cx22700_attach, &alps_tdmb7_config, &ttusb->i2c_adap);
1562         if (ttusb->fe != NULL) {
1563             ttusb->fe->ops.tuner_ops.set_params = alps_tdmb7_tuner_set_params;
1564             break;
1565         }
1566 
1567         // Philips td1316
1568         ttusb->fe = dvb_attach(tda10046_attach, &philips_tdm1316l_config, &ttusb->i2c_adap);
1569         if (ttusb->fe != NULL) {
1570             ttusb->fe->ops.tuner_ops.init = philips_tdm1316l_tuner_init;
1571             ttusb->fe->ops.tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
1572             break;
1573         }
1574         break;
1575     }
1576 
1577     if (ttusb->fe == NULL) {
1578         pr_err("no frontend driver found for device [%04x:%04x]\n",
1579                le16_to_cpu(ttusb->dev->descriptor.idVendor),
1580                le16_to_cpu(ttusb->dev->descriptor.idProduct));
1581     } else {
1582         if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1583             pr_err("Frontend registration failed!\n");
1584             dvb_frontend_detach(ttusb->fe);
1585             ttusb->fe = NULL;
1586         }
1587     }
1588 }
1589 
1590 
1591 
1592 static const struct i2c_algorithm ttusb_dec_algo = {
1593     .master_xfer    = master_xfer,
1594     .functionality  = functionality,
1595 };
1596 
1597 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1598 {
1599     struct usb_device *udev;
1600     struct ttusb *ttusb;
1601     int result;
1602 
1603     dprintk("TTUSB DVB connected\n");
1604 
1605     udev = interface_to_usbdev(intf);
1606 
1607     if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1608 
1609     if (!(ttusb = kzalloc(sizeof(struct ttusb), GFP_KERNEL)))
1610         return -ENOMEM;
1611 
1612     ttusb->dev = udev;
1613     ttusb->c = 0;
1614     ttusb->mux_state = 0;
1615     mutex_init(&ttusb->semi2c);
1616 
1617     mutex_lock(&ttusb->semi2c);
1618 
1619     mutex_init(&ttusb->semusb);
1620 
1621     ttusb_setup_interfaces(ttusb);
1622 
1623     result = ttusb_alloc_iso_urbs(ttusb);
1624     if (result < 0) {
1625         dprintk("ttusb_alloc_iso_urbs - failed\n");
1626         mutex_unlock(&ttusb->semi2c);
1627         kfree(ttusb);
1628         return result;
1629     }
1630 
1631     if (ttusb_init_controller(ttusb))
1632         pr_err("ttusb_init_controller: error\n");
1633 
1634     mutex_unlock(&ttusb->semi2c);
1635 
1636     result = dvb_register_adapter(&ttusb->adapter,
1637                       "Technotrend/Hauppauge Nova-USB",
1638                       THIS_MODULE, &udev->dev, adapter_nr);
1639     if (result < 0) {
1640         ttusb_free_iso_urbs(ttusb);
1641         kfree(ttusb);
1642         return result;
1643     }
1644     ttusb->adapter.priv = ttusb;
1645 
1646     /* i2c */
1647     memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1648     strscpy(ttusb->i2c_adap.name, "TTUSB DEC", sizeof(ttusb->i2c_adap.name));
1649 
1650     i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1651 
1652     ttusb->i2c_adap.algo              = &ttusb_dec_algo;
1653     ttusb->i2c_adap.algo_data         = NULL;
1654     ttusb->i2c_adap.dev.parent    = &udev->dev;
1655 
1656     result = i2c_add_adapter(&ttusb->i2c_adap);
1657     if (result)
1658         goto err_unregister_adapter;
1659 
1660     memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1661 
1662     ttusb->dvb_demux.dmx.capabilities =
1663         DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1664     ttusb->dvb_demux.priv = NULL;
1665 #ifdef TTUSB_HWSECTIONS
1666     ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1667 #else
1668     ttusb->dvb_demux.filternum = 32;
1669 #endif
1670     ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1671     ttusb->dvb_demux.start_feed = ttusb_start_feed;
1672     ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1673     ttusb->dvb_demux.write_to_decoder = NULL;
1674 
1675     result = dvb_dmx_init(&ttusb->dvb_demux);
1676     if (result < 0) {
1677         pr_err("dvb_dmx_init failed (errno = %d)\n", result);
1678         result = -ENODEV;
1679         goto err_i2c_del_adapter;
1680     }
1681 //FIXME dmxdev (nur WAS?)
1682     ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1683     ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1684     ttusb->dmxdev.capabilities = 0;
1685 
1686     result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter);
1687     if (result < 0) {
1688         pr_err("dvb_dmxdev_init failed (errno = %d)\n",
1689                result);
1690         result = -ENODEV;
1691         goto err_release_dmx;
1692     }
1693 
1694     if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1695         pr_err("dvb_net_init failed!\n");
1696         result = -ENODEV;
1697         goto err_release_dmxdev;
1698     }
1699 
1700     usb_set_intfdata(intf, (void *) ttusb);
1701 
1702     frontend_init(ttusb);
1703 
1704     return 0;
1705 
1706 err_release_dmxdev:
1707     dvb_dmxdev_release(&ttusb->dmxdev);
1708 err_release_dmx:
1709     dvb_dmx_release(&ttusb->dvb_demux);
1710 err_i2c_del_adapter:
1711     i2c_del_adapter(&ttusb->i2c_adap);
1712 err_unregister_adapter:
1713     dvb_unregister_adapter (&ttusb->adapter);
1714     ttusb_free_iso_urbs(ttusb);
1715     kfree(ttusb);
1716     return result;
1717 }
1718 
1719 static void ttusb_disconnect(struct usb_interface *intf)
1720 {
1721     struct ttusb *ttusb = usb_get_intfdata(intf);
1722 
1723     usb_set_intfdata(intf, NULL);
1724 
1725     ttusb->disconnecting = 1;
1726 
1727     ttusb_stop_iso_xfer(ttusb);
1728 
1729     ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1730     dvb_net_release(&ttusb->dvbnet);
1731     dvb_dmxdev_release(&ttusb->dmxdev);
1732     dvb_dmx_release(&ttusb->dvb_demux);
1733     if (ttusb->fe != NULL) {
1734         dvb_unregister_frontend(ttusb->fe);
1735         dvb_frontend_detach(ttusb->fe);
1736     }
1737     i2c_del_adapter(&ttusb->i2c_adap);
1738     dvb_unregister_adapter(&ttusb->adapter);
1739 
1740     ttusb_free_iso_urbs(ttusb);
1741 
1742     kfree(ttusb);
1743 
1744     dprintk("TTUSB DVB disconnected\n");
1745 }
1746 
1747 static const struct usb_device_id ttusb_table[] = {
1748     {USB_DEVICE(0xb48, 0x1003)},
1749     {USB_DEVICE(0xb48, 0x1004)},
1750     {USB_DEVICE(0xb48, 0x1005)},
1751     {}
1752 };
1753 
1754 MODULE_DEVICE_TABLE(usb, ttusb_table);
1755 
1756 static struct usb_driver ttusb_driver = {
1757       .name     = "ttusb",
1758       .probe        = ttusb_probe,
1759       .disconnect   = ttusb_disconnect,
1760       .id_table     = ttusb_table,
1761 };
1762 
1763 module_usb_driver(ttusb_driver);
1764 
1765 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1766 MODULE_DESCRIPTION("TTUSB DVB Driver");
1767 MODULE_LICENSE("GPL");
1768 MODULE_FIRMWARE("ttusb-budget/dspbootcode.bin");