Index: analogscan.h
===================================================================
--- analogscan.h	(revision 6786)
+++ analogscan.h	(working copy)
@@ -58,14 +58,8 @@
     /** @brief Stops the scanning thread running */
     void stop();
 
-    /** @brief Scans the given frequency list, implies start()
-        @see start()
-     */
-    bool scan(unsigned nList);
-    /** @brief Scans the given frequency table, implies start()
-        @see start()
-     */
-    bool scan(const QString& table);
+    /** @brief Scans the given frequency list, implies starting the thread() */
+    bool scan();
 
 signals:
     /** @brief Indicates how far through the scan we are as a percentage
@@ -86,43 +80,19 @@
     unsigned sourceid;
     unsigned cardid;
     unsigned nTable;
-    int fd;
-    QString device;
-    QString vbidevice;
-    struct vbi *vbi;
 
-    /** @brief Is the current channel tuned
-        @returns true if tuned
-      */ 
-    bool isTuned();
     /** @brief adds a found channel to the database
         @param name name of the channel
         @param frequency freqency/frequencyid of the channel
       */
-    void addChannel(const QString& name, int frequency);
-    /** @brief find the next available channel id 
-        @param sourceID the source id of the video source to check 
-    */
-    int generateNewChanID(int sourceID);
-    /** @brief Starts the scanning thread running */
-    bool start();
-
-    /** @brief Controls access to the thread*/
-    pthread_mutex_t lock;
+    void addChannel(int number, const QString& channum,
+                    const QString& name, int frequency);
     /** @brief Scanning thread*/
     pthread_t thread;
-    /** @brief vbi thread*/
-    pthread_t threadVBI;
 
     /** @brief Actual scanning proc */
     void doScan();
     /** @brief Actual thread proc , calls doScan*/
     static void *spawn(void *param);
-    /** @brief Actual thread proc for vbi, calls doVBI*/
-    static void *spawnVBI(void *param);
-    /** @brief Actual vbi proc */
-    void doVBI();
-    /** @brief vbi callback proc */
-    static void vbi_event(AnalogScan* data, struct vt_event *ev);
 };
 #endif //ANALOGSCAN_H
Index: videosource.h
===================================================================
--- videosource.h	(revision 6786)
+++ videosource.h	(working copy)
@@ -67,6 +67,7 @@
     static DISEQC_TYPES GetDISEqCType(uint cardid);
 
     static CARD_TYPES   GetDVBType(uint device, QString &name);
+    static QString      GetDefaultInput(uint cardid);
 };
 
 class VideoSource;
Index: videosource.cpp
===================================================================
--- videosource.cpp	(revision 6786)
+++ videosource.cpp	(working copy)
@@ -297,6 +297,28 @@
     return (DISEQC_TYPES)iRet;
 }
 
+/** \fn CardUtil::GetDefaultInput(uint)
+ *  \brief Returns the default input for the card
+ *  \param [in]nCardID card id to check
+ *  \return the default input
+ */
+QString CardUtil::GetDefaultInput(uint nCardID)
+{
+    QString str;
+    MSqlQuery query(MSqlQuery::InitCon());
+    query.prepare("SELECT defaultinput "
+                  "FROM capturecard "
+                  "WHERE capturecard.cardid = :CARDID");
+    query.bindValue(":CARDID", nCardID);
+
+    if (query.exec() && query.isActive() && query.size() > 0)
+    {
+        query.next();
+        str = query.value(0).toString();
+    }
+    return str;
+}
+
 QString VSSetting::whereClause(void)
 {
     return QString("sourceid = %1").arg(parent.getSourceID());
Index: scanwizard.cpp
===================================================================
--- scanwizard.cpp	(revision 6786)
+++ scanwizard.cpp	(working copy)
@@ -308,23 +308,6 @@
         //Create an analog scan object
         analogScan = new AnalogScan(nVideoSource, parent->captureCard());
 
-        MSqlQuery query(MSqlQuery::InitCon());
-
-        QString thequery = QString("SELECT freqtable "
-                               "FROM videosource WHERE "
-                               "sourceid = \"%1\";")
-                               .arg(nVideoSource);
-        query.prepare(thequery);
-
-        if (!query.exec() || !query.isActive())
-            MythContext::DBError("fetchtuningparams", query);
-        if (query.size() <= 0)
-             return;
-        query.next();
-
-        QString freqtable = query.value(0).toString();
-//        cerr << "frequency table = " << freqtable.ascii() << endl; 
-
         popupProgress = new ScanProgressPopup(this,false);
         connect(analogScan,SIGNAL(serviceScanComplete(void)),
                 this,SLOT(scanComplete(void)));
@@ -335,7 +318,7 @@
         popupProgress->progress(0);
         popupProgress->exec(this);
 
-        if (!analogScan->scan(freqtable))
+        if (!analogScan->scan())
         {
              MythPopupBox::showOkPopup(gContext->GetMainWindow(),
                                       tr("ScanWizard"),
Index: channel.h
===================================================================
--- channel.h	(revision 6786)
+++ channel.h	(working copy)
@@ -53,6 +53,8 @@
     bool TuneMultiplex(uint mplexid);
     bool Tune(uint frequency, QString inputname="",
               QString modulation="analog");
+    // V4L scanning stuff
+    bool IsTuned() const;
   private:
     // Helper Sets
     void SetColourAttribute(int attrib, const char *name);
Index: analogscan.cpp
===================================================================
--- analogscan.cpp	(revision 6786)
+++ analogscan.cpp	(working copy)
@@ -38,16 +38,12 @@
 #include <unistd.h>
 #include <fcntl.h>
 
-#include <qmutex.h>
 #include "mythcontext.h"
 #include "videosource.h"
 #include "frequencies.h"
-#include "videodev_myth.h"
+#include "channel.h"
+#include "channelutil.h"
 
-extern "C" {
-#include "vbitext/vbi.h"
-};
-
 #include "analogscan.h"
 
 AnalogScan::AnalogScan(unsigned _sourceid,unsigned _cardid) :
@@ -56,69 +52,18 @@
     sourceid(_sourceid),
     cardid(_cardid)
 {
-    pthread_mutex_init(&lock, NULL);
-    vbi=NULL;
 }
 
 AnalogScan::~AnalogScan()
 {
-    pthread_mutex_destroy(&lock);
 }
 
-bool AnalogScan::start()
-{
-    if (!CardUtil::GetVideoDevice(cardid, device, vbidevice))
-        return false;
-
-    fd = ::open(device.ascii(),O_RDWR); 
-    if (fd < 0)
-        return false;
-
-    pthread_mutex_lock(&lock);
-    fRunning = true;
-    fStop = false;
-    pthread_mutex_unlock(&lock);
-
-    pthread_create(&thread, NULL, spawn, this);
-#ifdef DOVBI
-    pthread_create(&threadVBI, NULL, spawnVBI, this);
-#endif
-
-    return true;
-}
-
 void AnalogScan::stop()
 {
-    bool _stop = false;
-    pthread_mutex_lock(&lock);
-    if (fRunning && !fStop)
-        _stop = true; 
-    fStop = true;
-    pthread_mutex_unlock(&lock);
-
-    if (_stop)
+    if (fRunning)
     {
-        pthread_join(thread,NULL);
-#ifdef DO_VBI
-        pthread_join(threadVBI,NULL);
-#endif
-        if (fd >=0)
-           ::close(fd);
-   }
-}
-
-void AnalogScan::vbi_event(AnalogScan* _this, struct vt_event *ev)
-{
-    (void)_this;
-    //cerr << "AnalogScan::vbi_event " << ev->type << "\n";
-    switch (ev->type)
-    {
-    case EV_HEADER:
-        break;
-    case EV_XPACKET:
-        break;
-//    default:
-//        cerr << "AnalogScan unkown " << ev->type << "\n";
+        fStop = true;
+        pthread_join(thread, NULL);
     }
 }
 
@@ -129,149 +74,90 @@
     return NULL;
 }
 
-void *AnalogScan::spawnVBI(void *param)
-{
-    AnalogScan *_this = (AnalogScan*)param;
-    _this->doVBI();
-    return NULL;
-}
-
-void AnalogScan::doVBI()
-{
-    vbi = ::vbi_open(vbidevice.ascii(), NULL, 99, -1);
-    if (!vbi)
-        return;
-    VERBOSE(VB_IMPORTANT, QString("AnalogScan opened vbi device: %1").arg(vbidevice));
-    vbi_add_handler(vbi, (void*)vbi_event,this);
-
-    while(!fStop)
-    {
-        struct timeval tv;
-        fd_set rdset;
-
-        tv.tv_sec = 0;
-        tv.tv_usec = 10000;
-        FD_ZERO(&rdset);
-        FD_SET(vbi->fd, &rdset);
-
-        switch (::select(vbi->fd + 1, &rdset, 0, 0, &tv))
-        {
-            case -1:
-                  perror("vbi select");
-                  continue;
-            case 0:
-                  //printf("vbi select timeout\n");
-                  continue;
-        }
-        vbi_handler(vbi, vbi->fd);
-    }
-    vbi_del_handler(vbi, (void*)vbi_event,this);
-    ::vbi_close(vbi);
-}
-
 void AnalogScan::doScan()
 {
-    struct CHANLIST *l = chanlists[nTable].list;
-    int count = chanlists[nTable].count;
-    for (int i = 0; i < count && !fStop; i++,l++)
+    fRunning = true;
+    QString device;
+    if (CardUtil::GetVideoDevice(cardid, device))
     {
-        //cerr << i << " " << l->name << " " << l->freq << endl;
-        struct v4l2_frequency vf;
-        memset(&vf, 0, sizeof(vf));
-        vf.frequency = l->freq * 16 / 1000;
-        vf.type = V4L2_TUNER_ANALOG_TV;
-
-        ::ioctl(fd, VIDIOC_S_FREQUENCY, &vf);
-        usleep(200000); /* 0.2 sec */
-        if (isTuned())
-        {
-#ifdef DOVBI
-             usleep(2000000); /* 0.2 sec */
-#endif
-             addChannel(l->name,l->freq);
-             emit serviceScanUpdateText(QObject::tr("Channel %1").arg(l->name));
+         Channel channel(NULL,device);
+         if (channel.Open())
+         {
+             QString input = CardUtil::GetDefaultInput(cardid);
+             struct CHANLIST *l = chanlists[nTable].list;
+             int count = chanlists[nTable].count;
+             for (int i = 0; i < count && !fStop; i++,l++)
+             {
+                 unsigned frequency = l->freq*1000;
+                 channel.Tune(frequency,input);
+                 usleep(200000); /* 0.2 sec */
+                 if (channel.IsTuned())
+                 {
+                      //cerr << i << " " << l->name << " " << frequency <<  " Tuned " << endl;
+                      QString name = QObject::tr("Channel %1").arg(l->name);
+                      addChannel(i,l->name,name,l->freq);
+                      emit serviceScanUpdateText(name);
+                 }
+                 emit serviceScanPCTComplete((i*100)/count);
+             }
+             channel.Close();
         }
-        emit serviceScanPCTComplete((i*100)/count);
     }
     emit serviceScanComplete();
-    pthread_mutex_lock(&lock);
-    fRunning = false;
-    pthread_mutex_unlock(&lock);
 }
 
-bool AnalogScan::scan(const QString& freqtable)
+bool AnalogScan::scan()
 {
     int i = 0;
     char *listname = (char *)chanlists[i].name;
     QString table;
 
+    MSqlQuery query(MSqlQuery::InitCon());
+
+    QString thequery = QString("SELECT freqtable FROM videosource WHERE "
+                               "sourceid = \"%1\";").arg(sourceid);
+    query.prepare(thequery);
+
+    if (!query.exec() || !query.isActive())
+        MythContext::DBError("analog scan freqtable", query);
+    if (query.size() <= 0)
+         return false;
+    query.next();
+
+    QString freqtable = query.value(0).toString();
+//        cerr << "frequency table = " << freqtable.ascii() << endl; 
+
     if (freqtable == "default" || freqtable.isNull() || freqtable.isEmpty())
         table = gContext->GetSetting("FreqTable");
     else
         table = freqtable;
 
+    nTable = 0;
     while (listname != NULL)
     {
         if (table == listname)
-            return scan(i);
+        {
+           nTable = i;
+           break;
+        }
         i++;
         listname = (char *)chanlists[i].name;
     }
 
-    return scan(0);
+    if (!fRunning)
+        pthread_create(&thread, NULL, spawn, this);
+    while (!fRunning)
+        usleep(50);
+    return true;
 }
 
-bool AnalogScan::isTuned()
+void AnalogScan::addChannel(int number,const QString& channumber,
+                            const QString& name, int frequency)
 {
-    struct v4l2_tuner tuner;
-
-    memset(&tuner,0,sizeof(tuner));
-    if (-1 == ::ioctl(fd,VIDIOC_G_TUNER,&tuner,0))
-        return 0;
-    return tuner.signal ? true : false;
-}
-
-bool AnalogScan::scan(unsigned table)
-{
-    nTable = table;
-    return start();
-}
-
-int AnalogScan::generateNewChanID(int sourceID)
-{
     MSqlQuery query(MSqlQuery::InitCon());
-
-    QString theQuery =
-        QString("SELECT max(chanid) as maxchan "
-                "FROM channel WHERE sourceid=%1").arg(sourceID);
-    query.prepare(theQuery);
-
-    if(!query.exec())
-        MythContext::DBError("Calculating new ChanID", query);
-
-    if (!query.isActive())
-        MythContext::DBError("Calculating new ChanID for Analog Channel",query);
-
-    query.next();
-
-    // If transport not present add it, and move on to the next
-    if (query.size() <= 0)
-        return sourceID * 1000;
-
-    int MaxChanID = query.value(0).toInt();
-
-    if (MaxChanID == 0)
-        return sourceID * 1000;
-    else
-        return MaxChanID + 1;
-}
-
-void AnalogScan::addChannel(const QString& name, int frequency)
-{
-    MSqlQuery query(MSqlQuery::InitCon());
     query.prepare("DELETE FROM channel WHERE channum=:CHANNUM AND "
                   "sourceid=:SOURCEID");
-    query.bindValue(":CHANNUM",name);
+    query.bindValue(":CHANNUM",channumber);
     query.bindValue(":SOURCEID",sourceid);
     query.exec();
 
@@ -280,16 +166,17 @@
                    "VALUES (:CHANID,:CHANNUM,:SOURCEID,:CALLSIGN,"
                    ":NAME,:FREQID);");
 
-    query.bindValue(":CHANID",generateNewChanID(sourceid));
-    query.bindValue(":CHANNUM",name);
+    int chanid = ChannelUtil::CreateChanID(sourceid,QString::number(number));
+    query.bindValue(":CHANID",chanid);
+    query.bindValue(":CHANNUM",channumber);
     query.bindValue(":SOURCEID",sourceid);
     query.bindValue(":CALLSIGN",name);
     query.bindValue(":NAME",name);
     query.bindValue(":FREQID",frequency);
 
     if(!query.exec())
-        MythContext::DBError("Adding new DVB Channel", query);
+        MythContext::DBError("Adding new Channel", query);
 
     if (!query.isActive())
-        MythContext::DBError("Adding new DVB Channel", query);
+        MythContext::DBError("Adding new Channel", query);
 }
Index: channel.cpp
===================================================================
--- channel.cpp	(revision 6786)
+++ channel.cpp	(working copy)
@@ -636,6 +636,27 @@
     return true;
 }
 
+bool Channel::IsTuned() const
+{
+    if (usingv4l2)
+    {
+        struct v4l2_tuner tuner;
+
+        memset(&tuner,0,sizeof(tuner));
+        if (-1 == ::ioctl(videofd,VIDIOC_G_TUNER,&tuner,0))
+            return false;
+        return tuner.signal ? true : false;
+    }
+    else 
+    {
+        struct video_tuner tuner;
+        memset(&tuner,0,sizeof(tuner));
+        if (-1 == ::ioctl(videofd,VIDIOCGTUNER,&tuner,0))
+             return false;
+        return tuner.signal ? true : false;
+    }
+}
+
 int Channel::GetCardID() const
 {
     if (pParent)
