]> git.sesse.net Git - vlc/blobdiff - modules/access/dvb/en50221.c
Remove useless <fcntl.h> inclusions
[vlc] / modules / access / dvb / en50221.c
index 75715eb6565407d3d3c31062a7c01866ffa9dd10..6a24fced5b9b69fced8cae8efe2bf34723c7e5df 100644 (file)
@@ -2,7 +2,7 @@
  * en50221.c : implementation of the transport, session and applications
  * layers of EN 50 221
  *****************************************************************************
- * Copyright (C) 2004 the VideoLAN team
+ * Copyright (C) 2004-2005 the VideoLAN team
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  * Based on code from libdvbci Copyright (C) 2000 Klaus Schmidinger
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA    02111, USA.
  *****************************************************************************/
 
-#include <vlc/vlc.h>
-#include <vlc/input.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_access.h>
 
 #include <sys/ioctl.h>
 #include <errno.h>
 
 #include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 #include <time.h>
 #include <unistd.h>
-#include <sys/stat.h>
-#include <sys/poll.h>
+#include <poll.h>
+#include <netinet/in.h>
 
 /* DVB Card Drivers */
 #include <linux/dvb/version.h>
@@ -50,6 +52,8 @@
 #   include <dvbpsi/pmt.h>
 #   include <dvbpsi/dr.h>
 #   include <dvbpsi/psi.h>
+#   include <dvbpsi/demux.h>
+#   include <dvbpsi/sdt.h>
 #else
 #   include "dvbpsi.h"
 #   include "descriptor.h"
 #   include "tables/pmt.h"
 #   include "descriptors/dr.h"
 #   include "psi.h"
+#   include "demux.h"
+#   include "tables/sdt.h"
+#endif
+
+#ifdef ENABLE_HTTPD
+#   include <vlc_httpd.h>
 #endif
 
 #include "dvb.h"
 
+#include <vlc_charset.h>
+
 #undef DEBUG_TPDU
 #define HLCI_WAIT_CAM_READY 0
 #define CAM_PROG_MAX MAX_PROGRAMS
+//#define CAPMT_WAIT 100             /* uncomment this for slow CAMs */
 
 static void ResourceManagerOpen( access_t * p_access, int i_session_id );
 static void ApplicationInformationOpen( access_t * p_access, int i_session_id );
@@ -136,7 +149,7 @@ static uint8_t *SetLength( uint8_t *p_data, int i_length )
  * Transport layer
  */
 
-#define MAX_TPDU_SIZE  2048
+#define MAX_TPDU_SIZE  4096
 #define MAX_TPDU_DATA  (MAX_TPDU_SIZE - 4)
 
 #define DATA_INDICATOR 0x80
@@ -153,7 +166,7 @@ static uint8_t *SetLength( uint8_t *p_data, int i_length )
 #define T_DATA_LAST    0xA0
 #define T_DATA_MORE    0xA1
 
-static void Dump( vlc_bool_t b_outgoing, uint8_t *p_data, int i_size )
+static void Dump( bool b_outgoing, uint8_t *p_data, int i_size )
 {
 #ifdef DEBUG_TPDU
     int i;
@@ -162,6 +175,8 @@ static void Dump( vlc_bool_t b_outgoing, uint8_t *p_data, int i_size )
     for ( i = 0; i < i_size && i < MAX_DUMP; i++)
         fprintf(stderr, "%02X ", p_data[i]);
     fprintf(stderr, "%s\n", i_size >= MAX_DUMP ? "..." : "");
+#else
+    VLC_UNUSED(b_outgoing); VLC_UNUSED(p_data); VLC_UNUSED(i_size);
 #endif
 }
 
@@ -219,12 +234,11 @@ static int TPDUSend( access_t * p_access, uint8_t i_slot, uint8_t i_tag,
     default:
         break;
     }
-    Dump( VLC_TRUE, p_data, i_size );
+    Dump( true, p_data, i_size );
 
     if ( write( p_sys->i_ca_handle, p_data, i_size ) != i_size )
     {
-        msg_Err( p_access, "cannot write to CAM device (%s)",
-                 strerror(errno) );
+        msg_Err( p_access, "cannot write to CAM device (%m)" );
         return VLC_EGENERIC;
     }
 
@@ -255,7 +269,7 @@ static int TPDURecv( access_t * p_access, uint8_t i_slot, uint8_t *pi_tag,
 
     if ( pi_size == NULL )
     {
-        p_data = malloc( MAX_TPDU_SIZE );
+        p_data = xmalloc( MAX_TPDU_SIZE );
     }
 
     for ( ; ; )
@@ -268,8 +282,9 @@ static int TPDURecv( access_t * p_access, uint8_t i_slot, uint8_t *pi_tag,
 
     if ( i_size < 5 )
     {
-        msg_Err( p_access, "cannot read from CAM device (%d:%s)", i_size,
-                 strerror(errno) );
+        msg_Err( p_access, "cannot read from CAM device (%d:%m)", i_size );
+        if( pi_size == NULL )
+            free( p_data );
         return VLC_EGENERIC;
     }
 
@@ -277,6 +292,8 @@ static int TPDURecv( access_t * p_access, uint8_t i_slot, uint8_t *pi_tag,
     {
         msg_Err( p_access, "invalid read from CAM device (%d instead of %d)",
                  p_data[1], i_tcid );
+        if( pi_size == NULL )
+            free( p_data );
         return VLC_EGENERIC;
     }
 
@@ -285,9 +302,9 @@ static int TPDURecv( access_t * p_access, uint8_t i_slot, uint8_t *pi_tag,
                                       && p_data[i_size - 4] == T_SB
                                       && p_data[i_size - 3] == 2
                                       && (p_data[i_size - 1] & DATA_INDICATOR))
-                                        ?  VLC_TRUE : VLC_FALSE;
+                                        ?  true : false;
 
-    Dump( VLC_FALSE, p_data, i_size );
+    Dump( false, p_data, i_size );
 
     if ( pi_size == NULL )
         free( p_data );
@@ -333,7 +350,7 @@ static int SPDUSend( access_t * p_access, int i_session_id,
                      uint8_t *p_data, int i_size )
 {
     access_sys_t *p_sys = p_access->p_sys;
-    uint8_t *p_spdu = malloc( i_size + 4 );
+    uint8_t *p_spdu = xmalloc( i_size + 4 );
     uint8_t *p = p_spdu;
     uint8_t i_tag;
     uint8_t i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
@@ -396,6 +413,8 @@ static int SPDUSend( access_t * p_access, int i_session_id,
 static void SessionOpen( access_t * p_access, uint8_t i_slot,
                          uint8_t *p_spdu, int i_size )
 {
+    VLC_UNUSED( i_size );
+
     access_sys_t *p_sys = p_access->p_sys;
     int i_session_id;
     int i_resource_id = ResourceIdToInt( &p_spdu[2] );
@@ -408,7 +427,7 @@ static void SessionOpen( access_t * p_access, uint8_t i_slot,
         if ( !p_sys->p_sessions[i_session_id - 1].i_resource_id )
             break;
     }
-    if ( i_session_id == MAX_SESSIONS )
+    if ( i_session_id > MAX_SESSIONS )
     {
         msg_Err( p_access, "too many sessions !" );
         return;
@@ -454,15 +473,111 @@ static void SessionOpen( access_t * p_access, uint8_t i_slot,
     switch ( i_resource_id )
     {
     case RI_RESOURCE_MANAGER:
-        ResourceManagerOpen( p_access, i_session_id ); break; 
+        ResourceManagerOpen( p_access, i_session_id ); break;
+    case RI_APPLICATION_INFORMATION:
+        ApplicationInformationOpen( p_access, i_session_id ); break;
+    case RI_CONDITIONAL_ACCESS_SUPPORT:
+        ConditionalAccessOpen( p_access, i_session_id ); break;
+    case RI_DATE_TIME:
+        DateTimeOpen( p_access, i_session_id ); break;
+    case RI_MMI:
+        MMIOpen( p_access, i_session_id ); break;
+
+    case RI_HOST_CONTROL:
+    default:
+        msg_Err( p_access, "unknown resource id (0x%x)", i_resource_id );
+        p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
+    }
+}
+
+#if 0
+/* unused code for the moment - commented out to keep gcc happy */
+/*****************************************************************************
+ * SessionCreate
+ *****************************************************************************/
+static void SessionCreate( access_t * p_access, int i_slot, int i_resource_id )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+    uint8_t p_response[16];
+    uint8_t i_tag;
+    int i_session_id;
+
+    for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
+    {
+        if ( !p_sys->p_sessions[i_session_id - 1].i_resource_id )
+            break;
+    }
+    if ( i_session_id == MAX_SESSIONS )
+    {
+        msg_Err( p_access, "too many sessions !" );
+        return;
+    }
+    p_sys->p_sessions[i_session_id - 1].i_slot = i_slot;
+    p_sys->p_sessions[i_session_id - 1].i_resource_id = i_resource_id;
+    p_sys->p_sessions[i_session_id - 1].pf_close = NULL;
+    p_sys->p_sessions[i_session_id - 1].pf_manage = NULL;
+    p_sys->p_sessions[i_session_id - 1].p_sys = NULL;
+
+    p_response[0] = ST_CREATE_SESSION;
+    p_response[1] = 0x6;
+    p_response[2] = i_resource_id >> 24;
+    p_response[3] = (i_resource_id >> 16) & 0xff;
+    p_response[4] = (i_resource_id >> 8) & 0xff;
+    p_response[5] = i_resource_id & 0xff;
+    p_response[6] = i_session_id >> 8;
+    p_response[7] = i_session_id & 0xff;
+
+    if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 4 ) !=
+            VLC_SUCCESS )
+    {
+        msg_Err( p_access,
+                 "SessionCreate: couldn't send TPDU on slot %d", i_slot );
+        return;
+    }
+    if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
+    {
+        msg_Err( p_access,
+                 "SessionCreate: couldn't recv TPDU on slot %d", i_slot );
+        return;
+    }
+}
+#endif
+
+/*****************************************************************************
+ * SessionCreateResponse
+ *****************************************************************************/
+static void SessionCreateResponse( access_t * p_access, uint8_t i_slot,
+                                   uint8_t *p_spdu, int i_size )
+{
+    VLC_UNUSED( i_size );
+    VLC_UNUSED( i_slot );
+
+    access_sys_t *p_sys = p_access->p_sys;
+    int i_status = p_spdu[2];
+    int i_resource_id = ResourceIdToInt( &p_spdu[3] );
+    int i_session_id = ((int)p_spdu[7] << 8) | p_spdu[8];
+
+    if ( i_status != SS_OK )
+    {
+        msg_Err( p_access, "SessionCreateResponse: failed to open session %d"
+                 " resource=0x%x status=0x%x", i_session_id, i_resource_id,
+                 i_status );
+        p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
+        return;
+    }
+
+    switch ( i_resource_id )
+    {
+    case RI_RESOURCE_MANAGER:
+        ResourceManagerOpen( p_access, i_session_id ); break;
     case RI_APPLICATION_INFORMATION:
-        ApplicationInformationOpen( p_access, i_session_id ); break; 
+        ApplicationInformationOpen( p_access, i_session_id ); break;
     case RI_CONDITIONAL_ACCESS_SUPPORT:
-        ConditionalAccessOpen( p_access, i_session_id ); break; 
+        ConditionalAccessOpen( p_access, i_session_id ); break;
     case RI_DATE_TIME:
-        DateTimeOpen( p_access, i_session_id ); break; 
+        DateTimeOpen( p_access, i_session_id ); break;
     case RI_MMI:
-        MMIOpen( p_access, i_session_id ); break; 
+        MMIOpen( p_access, i_session_id ); break;
 
     case RI_HOST_CONTROL:
     default:
@@ -561,20 +676,39 @@ static void SPDUHandle( access_t * p_access, uint8_t i_slot,
         SessionOpen( p_access, i_slot, p_spdu, i_size );
         break;
 
+    case ST_CREATE_SESSION_RESPONSE:
+        if ( i_size != 9 || p_spdu[1] != 0x7 )
+            return;
+        SessionCreateResponse( p_access, i_slot, p_spdu, i_size );
+        break;
+
     case ST_CLOSE_SESSION_REQUEST:
+        if ( i_size != 4 || p_spdu[1] != 0x2 )
+            return;
         i_session_id = ((int)p_spdu[2] << 8) | p_spdu[3];
         SessionClose( p_access, i_session_id );
         break;
 
     case ST_CLOSE_SESSION_RESPONSE:
-        i_session_id = ((int)p_spdu[2] << 8) | p_spdu[3];
-        if ( p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
-            p_sys->p_sessions[i_session_id - 1].pf_close( p_access,
-                                                          i_session_id );
-        p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
+        if ( i_size != 5 || p_spdu[1] != 0x3 )
+            return;
+        i_session_id = ((int)p_spdu[3] << 8) | p_spdu[4];
+        if ( p_spdu[2] )
+        {
+            msg_Err( p_access, "closing a session which is not allocated (%d)",
+                     i_session_id );
+        }
+        else
+        {
+            if ( p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
+                p_sys->p_sessions[i_session_id - 1].pf_close( p_access,
+                                                              i_session_id );
+            p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
+        }
         break;
 
     default:
+        msg_Err( p_access, "unexpected tag in SPDUHandle (%x)", p_spdu[0] );
         break;
     }
 }
@@ -664,7 +798,7 @@ static int APDUSend( access_t * p_access, int i_session_id, int i_tag,
                      uint8_t *p_data, int i_size )
 {
     access_sys_t *p_sys = p_access->p_sys;
-    uint8_t *p_apdu = malloc( i_size + 12 );
+    uint8_t *p_apdu = xmalloc( i_size + 12 );
     uint8_t *p = p_apdu;
     ca_msg_t ca_msg;
     int i_ret;
@@ -675,28 +809,26 @@ static int APDUSend( access_t * p_access, int i_session_id, int i_tag,
     p = SetLength( p, i_size );
     if ( i_size )
         memcpy( p, p_data, i_size );
-    if( p_sys->i_ca_type == CA_CI_LINK )
+    if ( p_sys->i_ca_type == CA_CI_LINK )
     {
         i_ret = SPDUSend( p_access, i_session_id, p_apdu, i_size + p - p_apdu );
     }
     else
     {
-        if( i_size + p - p_apdu >256 )
+        if ( i_size + p - p_apdu > 256 )
         {
             msg_Err( p_access, "CAM: apdu overflow" );
             i_ret = VLC_EGENERIC;
         }
         else
         {
-            char *psz_hex;
             ca_msg.length = i_size + p - p_apdu;
-            if( i_size == 0 ) ca_msg.length=3;
-            psz_hex = (char*)malloc( ca_msg.length*3 + 1);
+            if ( i_size == 0 ) ca_msg.length=3;
             memcpy( ca_msg.msg, p_apdu, i_size + p - p_apdu );
             i_ret = ioctl(p_sys->i_ca_handle, CA_SEND_MSG, &ca_msg );
-            if( i_ret < 0 )
+            if ( i_ret < 0 )
             {
-                msg_Err( p_access, "Error sending to CAM: %s", strerror(errno) );
+                msg_Err( p_access, "Error sending to CAM: %m" );
                 i_ret = VLC_EGENERIC;
             }
         }
@@ -759,12 +891,27 @@ static void ResourceManagerOpen( access_t * p_access, int i_session_id )
  * Application Information
  */
 
+/*****************************************************************************
+ * ApplicationInformationEnterMenu
+ *****************************************************************************/
+static void ApplicationInformationEnterMenu( access_t * p_access,
+                                             int i_session_id )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+    int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
+
+    msg_Dbg( p_access, "entering MMI menus on session %d", i_session_id );
+    APDUSend( p_access, i_session_id, AOT_ENTER_MENU, NULL, 0 );
+    p_sys->pb_slot_mmi_expected[i_slot] = true;
+}
+
 /*****************************************************************************
  * ApplicationInformationHandle
  *****************************************************************************/
 static void ApplicationInformationHandle( access_t * p_access, int i_session_id,
                                           uint8_t *p_apdu, int i_size )
 {
+    VLC_UNUSED(i_session_id);
     int i_tag = APDUGetTag( p_apdu, i_size );
 
     switch ( i_tag )
@@ -814,32 +961,32 @@ static void ApplicationInformationOpen( access_t * p_access, int i_session_id )
  * Conditional Access
  */
 
-#define MAX_CASYSTEM_IDS 16
+#define MAX_CASYSTEM_IDS 64
 
 typedef struct
 {
     uint16_t pi_system_ids[MAX_CASYSTEM_IDS + 1];
 } system_ids_t;
 
-static vlc_bool_t CheckSystemID( system_ids_t *p_ids, uint16_t i_id )
+static bool CheckSystemID( system_ids_t *p_ids, uint16_t i_id )
 {
     int i = 0;
-    if( !p_ids ) return VLC_TRUE;
+    if( !p_ids ) return true;      /* dummy session for high-level CI intf */
 
     while ( p_ids->pi_system_ids[i] )
     {
         if ( p_ids->pi_system_ids[i] == i_id )
-            return VLC_TRUE;
+            return true;
         i++;
     }
 
-    return VLC_FALSE;
+    return false;
 }
 
 /*****************************************************************************
  * CAPMTNeedsDescrambling
  *****************************************************************************/
-static vlc_bool_t CAPMTNeedsDescrambling( dvbpsi_pmt_t *p_pmt )
+static bool CAPMTNeedsDescrambling( dvbpsi_pmt_t *p_pmt )
 {
     dvbpsi_descriptor_t *p_dr;
     dvbpsi_pmt_es_t *p_es;
@@ -848,10 +995,10 @@ static vlc_bool_t CAPMTNeedsDescrambling( dvbpsi_pmt_t *p_pmt )
     {
         if( p_dr->i_tag == 0x9 )
         {
-            return VLC_TRUE;
+            return true;
         }
     }
-    
     for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
     {
         for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
@@ -859,12 +1006,12 @@ static vlc_bool_t CAPMTNeedsDescrambling( dvbpsi_pmt_t *p_pmt )
         {
             if( p_dr->i_tag == 0x9 )
             {
-                return VLC_TRUE;
+                return true;
             }
         }
     }
 
-    return VLC_FALSE;
+    return false;
 }
 
 /*****************************************************************************
@@ -897,9 +1044,9 @@ static uint8_t *CAPMTHeader( system_ids_t *p_ids, uint8_t i_list_mgt,
     uint8_t *p_data;
 
     if ( i_size )
-        p_data = malloc( 7 + i_size );
+        p_data = xmalloc( 7 + i_size );
     else
-        p_data = malloc( 6 );
+        p_data = xmalloc( 6 );
 
     p_data[0] = i_list_mgt;
     p_data[1] = i_program_number >> 8;
@@ -949,11 +1096,11 @@ static uint8_t *CAPMTES( system_ids_t *p_ids, uint8_t *p_capmt,
 {
     uint8_t *p_data;
     int i;
-    
     if ( i_size )
-        p_data = realloc( p_capmt, i_capmt_size + 6 + i_size );
+        p_data = xrealloc( p_capmt, i_capmt_size + 6 + i_size );
     else
-        p_data = realloc( p_capmt, i_capmt_size + 5 );
+        p_data = xrealloc( p_capmt, i_capmt_size + 5 );
 
     i = i_capmt_size;
 
@@ -1065,8 +1212,11 @@ static void CAPMTFirst( access_t * p_access, int i_session_id,
                           0x3 /* only */, 0x1 /* ok_descrambling */,
                           &i_capmt_size );
 
-    if ( i_capmt_size )
+    if( i_capmt_size )
+    {
         APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
+        free( p_capmt );
+    }
 }
 
 /*****************************************************************************
@@ -1090,8 +1240,11 @@ static void CAPMTAdd( access_t * p_access, int i_session_id,
         CAPMTFirst( p_access, i_session_id, p_pmt );
         return;
     }
-        
-    
+#ifdef CAPMT_WAIT
+    msleep( CAPMT_WAIT * 1000 );
+#endif
     msg_Dbg( p_access, "adding CAPMT for SID %d on session %d",
              p_pmt->i_program_number, i_session_id );
 
@@ -1099,8 +1252,11 @@ static void CAPMTAdd( access_t * p_access, int i_session_id,
                           0x4 /* add */, 0x1 /* ok_descrambling */,
                           &i_capmt_size );
 
-    if ( i_capmt_size )
+    if( i_capmt_size )
+    {
         APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
+        free( p_capmt );
+    }
 }
 
 /*****************************************************************************
@@ -1119,8 +1275,11 @@ static void CAPMTUpdate( access_t * p_access, int i_session_id,
                           0x5 /* update */, 0x1 /* ok_descrambling */,
                           &i_capmt_size );
 
-    if ( i_capmt_size )
+    if( i_capmt_size )
+    {
         APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
+        free( p_capmt );
+    }
 }
 
 /*****************************************************************************
@@ -1132,7 +1291,7 @@ static void CAPMTDelete( access_t * p_access, int i_session_id,
     uint8_t *p_capmt;
     int i_capmt_size;
 
-    p_access->p_sys->i_selected_programs++;
+    p_access->p_sys->i_selected_programs--;
     msg_Dbg( p_access, "deleting CAPMT for SID %d on session %d",
              p_pmt->i_program_number, i_session_id );
 
@@ -1140,8 +1299,11 @@ static void CAPMTDelete( access_t * p_access, int i_session_id,
                           0x5 /* update */, 0x4 /* not selected */,
                           &i_capmt_size );
 
-    if ( i_capmt_size )
+    if( i_capmt_size )
+    {
         APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
+        free( p_capmt );
+    }
 }
 
 /*****************************************************************************
@@ -1190,6 +1352,18 @@ static void ConditionalAccessHandle( access_t * p_access, int i_session_id,
     }
 }
 
+/*****************************************************************************
+ * ConditionalAccessClose
+ *****************************************************************************/
+static void ConditionalAccessClose( access_t * p_access, int i_session_id )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+
+    msg_Dbg( p_access, "closing ConditionalAccess session (%d)", i_session_id );
+
+    free( p_sys->p_sessions[i_session_id - 1].p_sys );
+}
+
 /*****************************************************************************
  * ConditionalAccessOpen
  *****************************************************************************/
@@ -1200,9 +1374,8 @@ static void ConditionalAccessOpen( access_t * p_access, int i_session_id )
     msg_Dbg( p_access, "opening ConditionalAccess session (%d)", i_session_id );
 
     p_sys->p_sessions[i_session_id - 1].pf_handle = ConditionalAccessHandle;
-    p_sys->p_sessions[i_session_id - 1].p_sys = malloc(sizeof(system_ids_t));
-    memset( p_sys->p_sessions[i_session_id - 1].p_sys, 0,
-            sizeof(system_ids_t) );
+    p_sys->p_sessions[i_session_id - 1].pf_close = ConditionalAccessClose;
+    p_sys->p_sessions[i_session_id - 1].p_sys = calloc( 1, sizeof(system_ids_t) );
 
     APDUSend( p_access, i_session_id, AOT_CA_INFO_ENQ, NULL, 0 );
 }
@@ -1308,6 +1481,18 @@ static void DateTimeManage( access_t * p_access, int i_session_id )
     }
 }
 
+/*****************************************************************************
+ * DateTimeClose
+ *****************************************************************************/
+static void DateTimeClose( access_t * p_access, int i_session_id )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+
+    msg_Dbg( p_access, "closing DateTime session (%d)", i_session_id );
+
+    free( p_sys->p_sessions[i_session_id - 1].p_sys );
+}
+
 /*****************************************************************************
  * DateTimeOpen
  *****************************************************************************/
@@ -1319,8 +1504,8 @@ static void DateTimeOpen( access_t * p_access, int i_session_id )
 
     p_sys->p_sessions[i_session_id - 1].pf_handle = DateTimeHandle;
     p_sys->p_sessions[i_session_id - 1].pf_manage = DateTimeManage;
-    p_sys->p_sessions[i_session_id - 1].p_sys = malloc(sizeof(date_time_t));
-    memset( p_sys->p_sessions[i_session_id - 1].p_sys, 0, sizeof(date_time_t) );
+    p_sys->p_sessions[i_session_id - 1].pf_close = DateTimeClose;
+    p_sys->p_sessions[i_session_id - 1].p_sys = calloc( 1, sizeof(date_time_t) );
 
     DateTimeSend( p_access, i_session_id );
 }
@@ -1363,6 +1548,63 @@ static void DateTimeOpen( access_t * p_access, int i_session_id )
 #define AI_CANCEL  0x00
 #define AI_ANSWER  0x01
 
+typedef struct
+{
+    en50221_mmi_object_t last_object;
+} mmi_t;
+
+/*****************************************************************************
+ * MMISendObject
+ *****************************************************************************/
+static void MMISendObject( access_t *p_access, int i_session_id,
+                           en50221_mmi_object_t *p_object )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+    int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
+    uint8_t *p_data;
+    int i_size, i_tag;
+
+    switch ( p_object->i_object_type )
+    {
+    case EN50221_MMI_ANSW:
+        i_tag = AOT_ANSW;
+        i_size = 1 + strlen( p_object->u.answ.psz_answ );
+        p_data = xmalloc( i_size );
+        p_data[0] = (p_object->u.answ.b_ok == true) ? 0x1 : 0x0;
+        strncpy( (char *)&p_data[1], p_object->u.answ.psz_answ, i_size - 1 );
+        break;
+
+    case EN50221_MMI_MENU_ANSW:
+        i_tag = AOT_MENU_ANSW;
+        i_size = 1;
+        p_data = xmalloc( i_size );
+        p_data[0] = p_object->u.menu_answ.i_choice;
+        break;
+
+    default:
+        msg_Err( p_access, "unknown MMI object %d", p_object->i_object_type );
+        return;
+    }
+
+    APDUSend( p_access, i_session_id, i_tag, p_data, i_size );
+    free( p_data );
+
+    p_sys->pb_slot_mmi_expected[i_slot] = true;
+}
+
+/*****************************************************************************
+ * MMISendClose
+ *****************************************************************************/
+static void MMISendClose( access_t *p_access, int i_session_id )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+    int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
+
+    APDUSend( p_access, i_session_id, AOT_CLOSE_MMI, NULL, 0 );
+
+    p_sys->pb_slot_mmi_expected[i_slot] = true;
+}
+
 /*****************************************************************************
  * MMIDisplayReply
  *****************************************************************************/
@@ -1381,8 +1623,7 @@ static void MMIDisplayReply( access_t *p_access, int i_session_id )
 /*****************************************************************************
  * MMIGetText
  *****************************************************************************/
-static char *MMIGetText( access_t *p_access, char *psz_text,
-                         uint8_t **pp_apdu, int *pi_size )
+static char *MMIGetText( access_t *p_access, uint8_t **pp_apdu, int *pi_size )
 {
     int i_tag = APDUGetTag( *pp_apdu, *pi_size );
     int l;
@@ -1391,18 +1632,95 @@ static char *MMIGetText( access_t *p_access, char *psz_text,
     if ( i_tag != AOT_TEXT_LAST )
     {
         msg_Err( p_access, "unexpected text tag: %06x", i_tag );
-        psz_text[0] = '\0';
         *pi_size = 0;
-        return psz_text;
+        return strdup( "" );
     }
 
     d = APDUGetLength( *pp_apdu, &l );
-    strncpy( psz_text, (char *)d, l );
-    psz_text[l] = '\0';
 
     *pp_apdu += l + 4;
     *pi_size -= l + 4;
-    return psz_text;
+
+    return dvbsi_to_utf8((char*)d,l);
+}
+
+/*****************************************************************************
+ * MMIHandleEnq
+ *****************************************************************************/
+static void MMIHandleEnq( access_t *p_access, int i_session_id,
+                          uint8_t *p_apdu, int i_size )
+{
+    VLC_UNUSED( i_size );
+
+    access_sys_t *p_sys = p_access->p_sys;
+    mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
+    int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
+    int l;
+    uint8_t *d = APDUGetLength( p_apdu, &l );
+
+    en50221_MMIFree( &p_mmi->last_object );
+    p_mmi->last_object.i_object_type = EN50221_MMI_ENQ;
+    p_mmi->last_object.u.enq.b_blind = (*d & 0x1) ? true : false;
+    d += 2; /* skip answer_text_length because it is not mandatory */
+    l -= 2;
+    p_mmi->last_object.u.enq.psz_text = xmalloc( l + 1 );
+    strncpy( p_mmi->last_object.u.enq.psz_text, (char *)d, l );
+    p_mmi->last_object.u.enq.psz_text[l] = '\0';
+
+    msg_Dbg( p_access, "MMI enq: %s%s", p_mmi->last_object.u.enq.psz_text,
+             p_mmi->last_object.u.enq.b_blind == true ? " (blind)" : "" );
+    p_sys->pb_slot_mmi_expected[i_slot] = false;
+    p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
+}
+
+/*****************************************************************************
+ * MMIHandleMenu
+ *****************************************************************************/
+static void MMIHandleMenu( access_t *p_access, int i_session_id, int i_tag,
+                           uint8_t *p_apdu, int i_size )
+{
+    VLC_UNUSED(i_size);
+    access_sys_t *p_sys = p_access->p_sys;
+    mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
+    int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
+    int l;
+    uint8_t *d = APDUGetLength( p_apdu, &l );
+
+    en50221_MMIFree( &p_mmi->last_object );
+    p_mmi->last_object.i_object_type = (i_tag == AOT_MENU_LAST) ?
+                                       EN50221_MMI_MENU : EN50221_MMI_LIST;
+    p_mmi->last_object.u.menu.i_choices = 0;
+    p_mmi->last_object.u.menu.ppsz_choices = NULL;
+
+    if ( l > 0 )
+    {
+        l--; d++; /* choice_nb */
+
+#define GET_FIELD( x )                                                      \
+        if ( l > 0 )                                                        \
+        {                                                                   \
+            p_mmi->last_object.u.menu.psz_##x                               \
+                            = MMIGetText( p_access, &d, &l );               \
+            msg_Dbg( p_access, "MMI " STRINGIFY( x ) ": %s",                \
+                     p_mmi->last_object.u.menu.psz_##x );                   \
+        }
+
+        GET_FIELD( title );
+        GET_FIELD( subtitle );
+        GET_FIELD( bottom );
+#undef GET_FIELD
+
+        while ( l > 0 )
+        {
+            char *psz_text = MMIGetText( p_access, &d, &l );
+            TAB_APPEND( p_mmi->last_object.u.menu.i_choices,
+                        p_mmi->last_object.u.menu.ppsz_choices,
+                        psz_text );
+            msg_Dbg( p_access, "MMI choice: %s", psz_text );
+        }
+    }
+    p_sys->pb_slot_mmi_expected[i_slot] = false;
+    p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
 }
 
 /*****************************************************************************
@@ -1440,38 +1758,17 @@ static void MMIHandle( access_t *p_access, int i_session_id,
         break;
     }
 
+    case AOT_ENQ:
+        MMIHandleEnq( p_access, i_session_id, p_apdu, i_size );
+        break;
+
     case AOT_LIST_LAST:
     case AOT_MENU_LAST:
-    {
-        int l;
-        uint8_t *d = APDUGetLength( p_apdu, &l );
-        char psz_text[255];
-
-        if ( l > 0 )
-        {
-            l--; d++; /* choice_nb */
-
-            if ( l > 0 )
-                msg_Info( p_access, "MMI title: %s",
-                          MMIGetText( p_access, psz_text, &d, &l ) );
-            if ( l > 0 )
-                msg_Info( p_access, "MMI subtitle: %s",
-                          MMIGetText( p_access, psz_text, &d, &l ) );
-            if ( l > 0 )
-                msg_Info( p_access, "MMI bottom: %s",
-                          MMIGetText( p_access, psz_text, &d, &l ) );
-            while ( l > 0 )
-            {
-                msg_Info( p_access, "MMI: %s",
-                          MMIGetText( p_access, psz_text, &d, &l ) );
-            }
-        }
+        MMIHandleMenu( p_access, i_session_id, i_tag, p_apdu, i_size );
         break;
-    }
 
     case AOT_CLOSE_MMI:
         SessionSendClose( p_access, i_session_id );
-        msg_Dbg( p_access, "closing MMI session (%d)", i_session_id );
         break;
 
     default:
@@ -1479,16 +1776,38 @@ static void MMIHandle( access_t *p_access, int i_session_id,
     }
 }
 
+/*****************************************************************************
+ * MMIClose
+ *****************************************************************************/
+static void MMIClose( access_t *p_access, int i_session_id )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+    int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
+    mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
+
+    en50221_MMIFree( &p_mmi->last_object );
+    free( p_sys->p_sessions[i_session_id - 1].p_sys );
+
+    msg_Dbg( p_access, "closing MMI session (%d)", i_session_id );
+    p_sys->pb_slot_mmi_expected[i_slot] = false;
+    p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
+}
+
 /*****************************************************************************
  * MMIOpen
  *****************************************************************************/
 static void MMIOpen( access_t *p_access, int i_session_id )
 {
     access_sys_t *p_sys = p_access->p_sys;
+    mmi_t *p_mmi;
 
     msg_Dbg( p_access, "opening MMI session (%d)", i_session_id );
 
     p_sys->p_sessions[i_session_id - 1].pf_handle = MMIHandle;
+    p_sys->p_sessions[i_session_id - 1].pf_close = MMIClose;
+    p_sys->p_sessions[i_session_id - 1].p_sys = xmalloc(sizeof(mmi_t));
+    p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
+    p_mmi->last_object.i_object_type = EN50221_MMI_NONE;
 }
 
 
@@ -1521,7 +1840,7 @@ static int InitSlot( access_t * p_access, int i_slot )
         if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) == VLC_SUCCESS
               && i_tag == T_CTC_REPLY )
         {
-            p_sys->pb_active_slot[i_slot] = VLC_TRUE;
+            p_sys->pb_active_slot[i_slot] = true;
             break;
         }
 
@@ -1534,6 +1853,7 @@ static int InitSlot( access_t * p_access, int i_slot )
             continue;
         }
     }
+
     if ( p_sys->pb_active_slot[i_slot] )
     {
         p_sys->i_ca_timeout = 100000;
@@ -1547,20 +1867,54 @@ static int InitSlot( access_t * p_access, int i_slot )
 /*
  * External entry points
  */
+
 /*****************************************************************************
  * en50221_Init : Initialize the CAM for en50221
  *****************************************************************************/
-int E_(en50221_Init)( access_t * p_access )
+int en50221_Init( access_t * p_access )
 {
     access_sys_t *p_sys = p_access->p_sys;
 
-
-    if( p_sys->i_ca_type != CA_CI ) /* Link level init is done in Poll() */
+    if( p_sys->i_ca_type & CA_CI_LINK )
     {
+        int i_slot;
+        for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
+        {
+            if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
+            {
+                msg_Err( p_access, "en50221_Init: couldn't reset slot %d",
+                         i_slot );
+            }
+        }
+
+        p_sys->i_ca_timeout = 100000;
+        /* Wait a bit otherwise it doesn't initialize properly... */
+        msleep( 1000000 );
+
         return VLC_SUCCESS;
     }
     else
     {
+        struct ca_slot_info info;
+        info.num = 0;
+
+        /* We don't reset the CAM in that case because it's done by the
+         * ASIC. */
+        if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &info ) < 0 )
+        {
+            msg_Err( p_access, "en50221_Init: couldn't get slot info" );
+            close( p_sys->i_ca_handle );
+            p_sys->i_ca_handle = 0;
+            return VLC_EGENERIC;
+        }
+        if( info.flags == 0 )
+        {
+            msg_Err( p_access, "en50221_Init: no CAM inserted" );
+            close( p_sys->i_ca_handle );
+            p_sys->i_ca_handle = 0;
+            return VLC_EGENERIC;
+        }
+
         /* Allocate a dummy sessions */
         p_sys->p_sessions[ 0 ].i_resource_id = RI_CONDITIONAL_ACCESS_SUPPORT;
 
@@ -1575,13 +1929,14 @@ int E_(en50221_Init)( access_t * p_access )
         APDUSend( p_access, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
         if ( ioctl( p_sys->i_ca_handle, CA_GET_MSG, &ca_msg ) < 0 )
         {
-            msg_Err( p_access, "CAM: failed getting message" );
+            msg_Err( p_access, "en50221_Init: failed getting message" );
             return VLC_EGENERIC;
         }
+
 #if HLCI_WAIT_CAM_READY
         while( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
         {
-            if( p_access->b_die ) return VLC_EGENERIC;
+            if( !vlc_object_alive (p_access) ) return VLC_EGENERIC;
             msleep(1);
             msg_Dbg( p_access, "CAM: please wait" );
             APDUSend( p_access, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
@@ -1592,10 +1947,10 @@ int E_(en50221_Init)( access_t * p_access )
             memset( &ca_msg.msg[3], 0, 253 );
             if ( ioctl( p_sys->i_ca_handle, CA_GET_MSG, &ca_msg ) < 0 )
             {
-                msg_Err( p_access, "CAM: failed getting message" );
+                msg_Err( p_access, "en50221_Init: failed getting message" );
                 return VLC_EGENERIC;
             }
-            msg_Dbg( p_access, "CAM: Got length: %d, tag: 0x%x", ca_msg.length, APDUGetTag( ca_msg.msg, ca_msg.length ) );
+            msg_Dbg( p_access, "en50221_Init: Got length: %d, tag: 0x%x", ca_msg.length, APDUGetTag( ca_msg.msg, ca_msg.length ) );
         }
 #else
         if( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
@@ -1607,14 +1962,13 @@ int E_(en50221_Init)( access_t * p_access )
         msg_Dbg( p_access, "found CAM %s using id 0x%x", &ca_msg.msg[12],
                  (ca_msg.msg[8]<<8)|ca_msg.msg[9] );
         return VLC_SUCCESS;
-        
     }
 }
 
 /*****************************************************************************
  * en50221_Poll : Poll the CAM for TPDUs
  *****************************************************************************/
-int E_(en50221_Poll)( access_t * p_access )
+int en50221_Poll( access_t * p_access )
 {
     access_sys_t *p_sys = p_access->p_sys;
     int i_slot;
@@ -1623,28 +1977,65 @@ int E_(en50221_Poll)( access_t * p_access )
     for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
     {
         uint8_t i_tag;
+        ca_slot_info_t sinfo;
+
+        sinfo.num = i_slot;
+        if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
+        {
+            msg_Err( p_access, "en50221_Poll: couldn't get info on slot %d",
+                     i_slot );
+            continue;
+        }
 
-        if ( !p_sys->pb_active_slot[i_slot] )
+        if ( !(sinfo.flags & CA_CI_MODULE_READY) )
         {
-            ca_slot_info_t sinfo;
-            sinfo.num = i_slot;
-            if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
+            if ( p_sys->pb_active_slot[i_slot] )
             {
-                msg_Err( p_access, "en50221_Poll: couldn't get info on slot %d",
+                msg_Dbg( p_access, "en50221_Poll: slot %d has been removed",
                          i_slot );
-                continue;
+                p_sys->pb_active_slot[i_slot] = false;
+                p_sys->pb_slot_mmi_expected[i_slot] = false;
+                p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
+
+                /* Close all sessions for this slot. */
+                for ( i_session_id = 1; i_session_id <= MAX_SESSIONS;
+                      i_session_id++ )
+                {
+                    if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
+                          && p_sys->p_sessions[i_session_id - 1].i_slot
+                               == i_slot )
+                    {
+                        if ( p_sys->p_sessions[i_session_id - 1].pf_close
+                              != NULL )
+                        {
+                            p_sys->p_sessions[i_session_id - 1].pf_close(
+                                                p_access, i_session_id );
+                        }
+                        p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
+                    }
+                }
             }
 
-            if ( sinfo.flags & CA_CI_MODULE_READY )
+            continue;
+        }
+        else if ( !p_sys->pb_active_slot[i_slot] )
+        {
+            InitSlot( p_access, i_slot );
+
+            if ( !p_sys->pb_active_slot[i_slot] )
             {
-                msg_Dbg( p_access, "en50221_Poll: slot %d is active",
-                         i_slot );
-                p_sys->pb_active_slot[i_slot] = VLC_TRUE;
-            }
-            else
+                msg_Dbg( p_access, "en50221_Poll: resetting slot %d", i_slot );
+
+                if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
+                {
+                    msg_Err( p_access, "en50221_Poll: couldn't reset slot %d",
+                             i_slot );
+                }
                 continue;
+            }
 
-            InitSlot( p_access, i_slot );
+            msg_Dbg( p_access, "en50221_Poll: slot %d is active",
+                     i_slot );
         }
 
         if ( !p_sys->pb_tc_has_data[i_slot] )
@@ -1724,12 +2115,12 @@ int E_(en50221_Poll)( access_t * p_access )
 /*****************************************************************************
  * en50221_SetCAPMT :
  *****************************************************************************/
-int E_(en50221_SetCAPMT)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
+int en50221_SetCAPMT( access_t * p_access, dvbpsi_pmt_t *p_pmt )
 {
     access_sys_t *p_sys = p_access->p_sys;
     int i, i_session_id;
-    vlc_bool_t b_update = VLC_FALSE;
-    vlc_bool_t b_needs_descrambling = CAPMTNeedsDescrambling( p_pmt );
+    bool b_update = false;
+    bool b_needs_descrambling = CAPMTNeedsDescrambling( p_pmt );
 
     for ( i = 0; i < MAX_PROGRAMS; i++ )
     {
@@ -1737,7 +2128,7 @@ int E_(en50221_SetCAPMT)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
               && p_sys->pp_selected_programs[i]->i_program_number
                   == p_pmt->i_program_number )
         {
-            b_update = VLC_TRUE;
+            b_update = true;
 
             if ( !b_needs_descrambling )
             {
@@ -1792,13 +2183,138 @@ int E_(en50221_SetCAPMT)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
     return VLC_SUCCESS;
 }
 
+/*****************************************************************************
+ * en50221_OpenMMI :
+ *****************************************************************************/
+int en50221_OpenMMI( access_t * p_access, int i_slot )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+
+    if( p_sys->i_ca_type & CA_CI_LINK )
+    {
+        int i_session_id;
+        for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
+        {
+            if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
+                  && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
+            {
+                msg_Dbg( p_access,
+                         "MMI menu is already opened on slot %d (session=%d)",
+                         i_slot, i_session_id );
+                return VLC_SUCCESS;
+            }
+        }
+
+        for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
+        {
+            if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
+                    == RI_APPLICATION_INFORMATION
+                  && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
+            {
+                ApplicationInformationEnterMenu( p_access, i_session_id );
+                return VLC_SUCCESS;
+            }
+        }
+
+        msg_Err( p_access, "no application information on slot %d", i_slot );
+        return VLC_EGENERIC;
+    }
+    else
+    {
+        msg_Err( p_access, "MMI menu not supported" );
+        return VLC_EGENERIC;
+    }
+}
+
+/*****************************************************************************
+ * en50221_CloseMMI :
+ *****************************************************************************/
+int en50221_CloseMMI( access_t * p_access, int i_slot )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+
+    if( p_sys->i_ca_type & CA_CI_LINK )
+    {
+        int i_session_id;
+        for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
+        {
+            if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
+                  && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
+            {
+                MMISendClose( p_access, i_session_id );
+                return VLC_SUCCESS;
+            }
+        }
+
+        msg_Warn( p_access, "closing a non-existing MMI session on slot %d",
+                  i_slot );
+        return VLC_EGENERIC;
+    }
+    else
+    {
+        msg_Err( p_access, "MMI menu not supported" );
+        return VLC_EGENERIC;
+    }
+}
+
+/*****************************************************************************
+ * en50221_GetMMIObject :
+ *****************************************************************************/
+en50221_mmi_object_t *en50221_GetMMIObject( access_t * p_access,
+                                                int i_slot )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+    int i_session_id;
+
+    if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
+        return NULL; /* should not happen */
+
+    for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
+    {
+        if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
+              && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
+        {
+            mmi_t *p_mmi =
+                (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
+            if ( p_mmi == NULL )
+                return NULL; /* should not happen */
+            return &p_mmi->last_object;
+        }
+    }
+
+    return NULL;
+}
+
+
+/*****************************************************************************
+ * en50221_SendMMIObject :
+ *****************************************************************************/
+void en50221_SendMMIObject( access_t * p_access, int i_slot,
+                                en50221_mmi_object_t *p_object )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+    int i_session_id;
+
+    for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
+    {
+        if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
+              && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
+        {
+            MMISendObject( p_access, i_session_id, p_object );
+            return;
+        }
+    }
+
+    msg_Err( p_access, "SendMMIObject when no MMI session is opened !" );
+}
+
 /*****************************************************************************
  * en50221_End :
  *****************************************************************************/
-void E_(en50221_End)( access_t * p_access )
+void en50221_End( access_t * p_access )
 {
     access_sys_t *p_sys = p_access->p_sys;
-    int i;
+    int i_session_id, i;
 
     for ( i = 0; i < MAX_PROGRAMS; i++ )
     {
@@ -1808,5 +2324,121 @@ void E_(en50221_End)( access_t * p_access )
         }
     }
 
-    /* TODO */
+    for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
+    {
+        if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
+              && p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
+        {
+            p_sys->p_sessions[i_session_id - 1].pf_close( p_access,
+                                                          i_session_id );
+        }
+    }
+
+    /* Leave the CAM configured, so that it can be reused in another
+     * program. */
+}
+
+static inline void *FixUTF8( char *p )
+{
+    EnsureUTF8( p );
+    return p;
+}
+
+char *dvbsi_to_utf8( const char *psz_instring, size_t i_length )
+{
+    const char *psz_encoding, *psz_stringstart;
+    char *psz_outstring, *psz_tmp;
+    char psz_encbuf[12];
+    size_t i_in, i_out;
+    vlc_iconv_t iconv_handle;
+    if( i_length < 1 ) return NULL;
+    if( psz_instring[0] < 0 || psz_instring[0] >= 0x20 )
+    {
+        psz_stringstart = psz_instring;
+        psz_encoding = "ISO_8859-1"; /* should be ISO6937 according to spec, but this seems to be the one used */
+    } else switch( psz_instring[0] )
+    {
+    case 0x01:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-5";
+        break;
+    case 0x02:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-6";
+        break;
+    case 0x03:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-7";
+        break;
+    case 0x04:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-8";
+        break;
+    case 0x05:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-9";
+        break;
+    case 0x06:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-10";
+        break;
+    case 0x07:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-11";
+        break;
+    case 0x08:
+        psz_stringstart = &psz_instring[1]; /*possibly reserved?*/
+        psz_encoding = "ISO_8859-12";
+        break;
+    case 0x09:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-13";
+        break;
+    case 0x0a:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-14";
+        break;
+    case 0x0b:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "ISO_8859-15";
+        break;
+    case 0x10:
+        if( i_length < 3 || psz_instring[1] != '\0' || psz_instring[2] > 0x0f
+            || psz_instring[2] == 0 )
+            return FixUTF8(strndup(psz_instring,i_length));
+        sprintf( psz_encbuf, "ISO_8859-%d", psz_instring[2] );
+        psz_stringstart = &psz_instring[3];
+        psz_encoding = psz_encbuf;
+        break;
+    case 0x11:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "UTF-16";
+        break;
+    case 0x12:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "KSC5601-1987";
+        break;
+    case 0x13:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "GB2312";/*GB-2312-1980 */
+        break;
+    case 0x14:
+        psz_stringstart = &psz_instring[1];
+        psz_encoding = "BIG-5";
+        break;
+    case 0x15:
+        return FixUTF8(strndup(&psz_instring[1],i_length-1));
+        break;
+    default:
+        /* invalid */
+        return FixUTF8(strndup(psz_instring,i_length));
+    }
+    iconv_handle = vlc_iconv_open( "UTF-8", psz_encoding );
+    i_in = i_length - (psz_stringstart - psz_instring );
+    i_out = i_in * 6;
+    psz_outstring = psz_tmp = (char*)xmalloc( i_out + 1 );
+    vlc_iconv( iconv_handle, &psz_stringstart, &i_in, &psz_tmp, &i_out );
+    vlc_iconv_close( iconv_handle );
+    *psz_tmp = '\0';
+    return psz_outstring;
 }