firesat: update isochronous interface, add CI support

I have finally managed to get the CI support for the card working. The
implementation is a bare minimum to get encrypted channels to work in
kaffeine. It works fine with my T/CI card. Now and then I get an AVC
timeout and have to retune a channel in order to get it to work. Once
the CAM seemed to hang so I needed to remove and insert it again. I.e.
there are a number of glitches.

The latest version contains the following changes:

  - Implemented the new hpsb iso interface so that data can be received
    from the card
  - Reduced some timers for demux setup which caused scanning to timeout
  - Added possibility to unload driver
  - Added support for getting C/N ratio
  - Added two debug parameters to the driver; ca_debug and
    avc_comm_debug.
  - Added CI support that works for me in kaffeine
  - Started working on CI MMI support. It now supports:
      o Enter menu
      o Receiving MMI objects
  - Added support for 64-bit platforms
  - Corrected DVB-C modulations problems

Signed-off-by: Henrik Kurelid <henrik@kurelid.se>
Signed-off-by: Stefan Richter <stefanr@s5r6.in-berlin.de> (rebased, whitespace)
diff --git a/drivers/media/dvb/firesat/firesat.h b/drivers/media/dvb/firesat/firesat.h
index d1e2ce3..1beed17 100644
--- a/drivers/media/dvb/firesat/firesat.h
+++ b/drivers/media/dvb/firesat/firesat.h
@@ -1,3 +1,15 @@
+/*
+ * FireSAT DVB driver
+ *
+ * Copyright (c) ?
+ * Copyright (c) 2008 Henrik Kurelid <henrik@kurelid.se>
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License as
+ *	published by the Free Software Foundation; either version 2 of
+ *	the License, or (at your option) any later version.
+ */
+
 #ifndef __FIRESAT_H
 #define __FIRESAT_H
 
@@ -6,15 +18,108 @@
 #include "dvb_demux.h"
 #include "dvb_net.h"
 
+#include <linux/version.h>
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 25)
 #include <linux/semaphore.h>
+#endif
 #include <linux/dvb/frontend.h>
 #include <linux/dvb/dmx.h>
+#include <iso.h>
+
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 25)
+#define DVB_REGISTER_ADAPTER(x, y, z, w, v) dvb_register_adapter(x, y, z, w, v)
+#else
+#define DVB_REGISTER_ADAPTER(x, y, z, w, v) dvb_register_adapter(x, y, z, w)
+#define DVB_DEFINE_MOD_OPT_ADAPTER_NR(x)
+#endif
+
+/*****************************************************************
+ * CA message command constants from en50221_app_tags.h of libdvb
+ *****************************************************************/
+/*	Resource Manager		*/
+#define TAG_PROFILE_ENQUIRY		0x9f8010
+#define TAG_PROFILE			0x9f8011
+#define TAG_PROFILE_CHANGE		0x9f8012
+
+/*	Application Info		*/
+#define TAG_APP_INFO_ENQUIRY		0x9f8020
+#define TAG_APP_INFO			0x9f8021
+#define TAG_ENTER_MENU			0x9f8022
+
+/*	CA Support			*/
+#define TAG_CA_INFO_ENQUIRY		0x9f8030
+#define TAG_CA_INFO			0x9f8031
+#define TAG_CA_PMT			0x9f8032
+#define TAG_CA_PMT_REPLY		0x9f8033
+
+/*	Host Control			*/
+#define TAG_TUNE			0x9f8400
+#define TAG_REPLACE			0x9f8401
+#define TAG_CLEAR_REPLACE		0x9f8402
+#define TAG_ASK_RELEASE			0x9f8403
+
+/*	Date and Time			*/
+#define TAG_DATE_TIME_ENQUIRY		0x9f8440
+#define TAG_DATE_TIME			0x9f8441
+
+/*	Man Machine Interface (MMI)	*/
+#define TAG_CLOSE_MMI			0x9f8800
+#define TAG_DISPLAY_CONTROL		0x9f8801
+#define TAG_DISPLAY_REPLY		0x9f8802
+#define TAG_TEXT_LAST			0x9f8803
+#define TAG_TEXT_MORE			0x9f8804
+#define TAG_KEYPAD_CONTROL		0x9f8805
+#define TAG_KEYPRESS			0x9f8806
+#define TAG_ENQUIRY			0x9f8807
+#define TAG_ANSWER			0x9f8808
+#define TAG_MENU_LAST			0x9f8809
+#define TAG_MENU_MORE			0x9f880a
+#define TAG_MENU_ANSWER			0x9f880b
+#define TAG_LIST_LAST			0x9f880c
+#define TAG_LIST_MORE			0x9f880d
+#define TAG_SUBTITLE_SEGMENT_LAST	0x9f880e
+#define TAG_SUBTITLE_SEGMENT_MORE	0x9f880f
+#define TAG_DISPLAY_MESSAGE		0x9f8810
+#define TAG_SCENE_END_MARK		0x9f8811
+#define TAG_SCENE_DONE			0x9f8812
+#define TAG_SCENE_CONTROL		0x9f8813
+#define TAG_SUBTITLE_DOWNLOAD_LAST	0x9f8814
+#define TAG_SUBTITLE_DOWNLOAD_MORE	0x9f8815
+#define TAG_FLUSH_DOWNLOAD		0x9f8816
+#define TAG_DOWNLOAD_REPLY		0x9f8817
+
+/*	Low Speed Communications	*/
+#define TAG_COMMS_COMMAND		0x9f8c00
+#define TAG_CONNECTION_DESCRIPTOR	0x9f8c01
+#define TAG_COMMS_REPLY			0x9f8c02
+#define TAG_COMMS_SEND_LAST		0x9f8c03
+#define TAG_COMMS_SEND_MORE		0x9f8c04
+#define TAG_COMMS_RECV_LAST		0x9f8c05
+#define TAG_COMMS_RECV_MORE		0x9f8c06
+
+/* Authentication */
+#define TAG_AUTH_REQ			0x9f8200
+#define TAG_AUTH_RESP			0x9f8201
+
+/* Teletext */
+#define TAG_TELETEXT_EBU		0x9f9000
+
+/* Smartcard */
+#define TAG_SMARTCARD_COMMAND		0x9f8e00
+#define TAG_SMARTCARD_REPLY		0x9f8e01
+#define TAG_SMARTCARD_SEND		0x9f8e02
+#define TAG_SMARTCARD_RCV		0x9f8e03
+
+/* EPG */
+#define TAG_EPG_ENQUIRY         	0x9f8f00
+#define TAG_EPG_REPLY           	0x9f8f01
+
 
 enum model_type {
-    FireSAT_DVB_S = 1,
-    FireSAT_DVB_C = 2,
-    FireSAT_DVB_T = 3,
-    FireSAT_DVB_S2 = 4
+	FireSAT_DVB_S = 1,
+	FireSAT_DVB_C = 2,
+	FireSAT_DVB_T = 3,
+	FireSAT_DVB_S2 = 4
 };
 
 struct firesat {
@@ -31,12 +136,13 @@
 	struct dvb_frontend		*fe;
 
 	struct dvb_device		*cadev;
-	int				has_ci;
+	int				ca_last_command;
+	int				ca_time_interval;
 
 	struct semaphore		avc_sem;
-	atomic_t				avc_reply_received;
+	atomic_t			avc_reply_received;
 
-	atomic_t				reschedule_remotecontrol;
+	atomic_t			reschedule_remotecontrol;
 
 	struct firesat_channel {
 		struct firesat *firesat;
@@ -53,20 +159,54 @@
 	void *respfrm;
 	int resp_length;
 
-//    nodeid_t nodeid;
-    struct hpsb_host *host;
+	struct hpsb_host *host;
 	u64 guid;			/* GUID of this node */
 	u32 guid_vendor_id;		/* Top 24bits of guid */
 	struct node_entry *nodeentry;
 
-    enum model_type type;
-    char subunit;
+	enum model_type type;
+	char subunit;
 	fe_sec_voltage_t voltage;
 	fe_sec_tone_mode_t tone;
 
 	int isochannel;
+	struct hpsb_iso *iso_handle;
 
-    struct list_head list;
+	struct list_head list;
+};
+
+struct firewireheader {
+	union {
+		struct {
+			__u8 tcode:4;
+			__u8 sy:4;
+			__u8 tag:2;
+			__u8 channel:6;
+
+			__u8 length_l;
+			__u8 length_h;
+		} hdr;
+		__u32 val;
+	};
+};
+
+struct CIPHeader {
+	union {
+		struct {
+			__u8 syncbits:2;
+			__u8 sid:6;
+			__u8 dbs;
+			__u8 fn:2;
+			__u8 qpc:3;
+			__u8 sph:1;
+			__u8 rsv:2;
+			__u8 dbc;
+			__u8 syncbits2:2;
+			__u8 fmt:6;
+			__u32 fdf:24;
+		} cip;
+		__u64 val;
+	};
 };
 
 extern struct list_head firesat_list;
@@ -76,11 +216,15 @@
 extern int firesat_start_feed(struct dvb_demux_feed *dvbdmxfeed);
 extern int firesat_stop_feed(struct dvb_demux_feed *dvbdmxfeed);
 extern int firesat_dvbdev_init(struct firesat *firesat,
-				struct device *dev,
-				struct dvb_frontend *fe);
+			       struct device *dev,
+			       struct dvb_frontend *fe);
 
 /* firesat_fe.c */
-extern int firesat_frontend_attach(struct firesat *firesat, struct dvb_frontend *fe);
+extern int firesat_frontend_attach(struct firesat *firesat,
+				   struct dvb_frontend *fe);
 
+/* firesat_iso.c */
+extern int setup_iso_channel(struct firesat *firesat);
+extern void tear_down_iso_channel(struct firesat *firesat);
 
 #endif