xref: /OK3568_Linux_fs/kernel/drivers/media/firewire/firedtv-avc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * FireDTV driver (formerly known as FireSAT)
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
6*4882a593Smuzhiyun  * Copyright (C) 2008 Ben Backx <ben@bbackx.com>
7*4882a593Smuzhiyun  * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/bug.h>
11*4882a593Smuzhiyun #include <linux/crc32.h>
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/device.h>
14*4882a593Smuzhiyun #include <linux/jiffies.h>
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/moduleparam.h>
17*4882a593Smuzhiyun #include <linux/mutex.h>
18*4882a593Smuzhiyun #include <linux/string.h>
19*4882a593Smuzhiyun #include <linux/stringify.h>
20*4882a593Smuzhiyun #include <linux/wait.h>
21*4882a593Smuzhiyun #include <linux/workqueue.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <media/dvb_frontend.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include "firedtv.h"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define FCP_COMMAND_REGISTER		0xfffff0000b00ULL
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define AVC_CTYPE_CONTROL		0x0
30*4882a593Smuzhiyun #define AVC_CTYPE_STATUS		0x1
31*4882a593Smuzhiyun #define AVC_CTYPE_NOTIFY		0x3
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define AVC_RESPONSE_ACCEPTED		0x9
34*4882a593Smuzhiyun #define AVC_RESPONSE_STABLE		0xc
35*4882a593Smuzhiyun #define AVC_RESPONSE_CHANGED		0xd
36*4882a593Smuzhiyun #define AVC_RESPONSE_INTERIM		0xf
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define AVC_SUBUNIT_TYPE_TUNER		(0x05 << 3)
39*4882a593Smuzhiyun #define AVC_SUBUNIT_TYPE_UNIT		(0x1f << 3)
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #define AVC_OPCODE_VENDOR		0x00
42*4882a593Smuzhiyun #define AVC_OPCODE_READ_DESCRIPTOR	0x09
43*4882a593Smuzhiyun #define AVC_OPCODE_DSIT			0xc8
44*4882a593Smuzhiyun #define AVC_OPCODE_DSD			0xcb
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define DESCRIPTOR_TUNER_STATUS		0x80
47*4882a593Smuzhiyun #define DESCRIPTOR_SUBUNIT_IDENTIFIER	0x00
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #define SFE_VENDOR_DE_COMPANYID_0	0x00 /* OUI of Digital Everywhere */
50*4882a593Smuzhiyun #define SFE_VENDOR_DE_COMPANYID_1	0x12
51*4882a593Smuzhiyun #define SFE_VENDOR_DE_COMPANYID_2	0x87
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #define SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL 0x0a
54*4882a593Smuzhiyun #define SFE_VENDOR_OPCODE_LNB_CONTROL		0x52
55*4882a593Smuzhiyun #define SFE_VENDOR_OPCODE_TUNE_QPSK		0x58 /* for DVB-S */
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #define SFE_VENDOR_OPCODE_GET_FIRMWARE_VERSION	0x00
58*4882a593Smuzhiyun #define SFE_VENDOR_OPCODE_HOST2CA		0x56
59*4882a593Smuzhiyun #define SFE_VENDOR_OPCODE_CA2HOST		0x57
60*4882a593Smuzhiyun #define SFE_VENDOR_OPCODE_CISTATUS		0x59
61*4882a593Smuzhiyun #define SFE_VENDOR_OPCODE_TUNE_QPSK2		0x60 /* for DVB-S2 */
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #define SFE_VENDOR_TAG_CA_RESET			0x00
64*4882a593Smuzhiyun #define SFE_VENDOR_TAG_CA_APPLICATION_INFO	0x01
65*4882a593Smuzhiyun #define SFE_VENDOR_TAG_CA_PMT			0x02
66*4882a593Smuzhiyun #define SFE_VENDOR_TAG_CA_DATE_TIME		0x04
67*4882a593Smuzhiyun #define SFE_VENDOR_TAG_CA_MMI			0x05
68*4882a593Smuzhiyun #define SFE_VENDOR_TAG_CA_ENTER_MENU		0x07
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #define EN50221_LIST_MANAGEMENT_ONLY	0x03
71*4882a593Smuzhiyun #define EN50221_TAG_APP_INFO		0x9f8021
72*4882a593Smuzhiyun #define EN50221_TAG_CA_INFO		0x9f8031
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun struct avc_command_frame {
75*4882a593Smuzhiyun 	u8 ctype;
76*4882a593Smuzhiyun 	u8 subunit;
77*4882a593Smuzhiyun 	u8 opcode;
78*4882a593Smuzhiyun 	u8 operand[509];
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun struct avc_response_frame {
82*4882a593Smuzhiyun 	u8 response;
83*4882a593Smuzhiyun 	u8 subunit;
84*4882a593Smuzhiyun 	u8 opcode;
85*4882a593Smuzhiyun 	u8 operand[509];
86*4882a593Smuzhiyun };
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun #define LAST_OPERAND (509 - 1)
89*4882a593Smuzhiyun 
clear_operands(struct avc_command_frame * c,int from,int to)90*4882a593Smuzhiyun static inline void clear_operands(struct avc_command_frame *c, int from, int to)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	memset(&c->operand[from], 0, to - from + 1);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
pad_operands(struct avc_command_frame * c,int from)95*4882a593Smuzhiyun static void pad_operands(struct avc_command_frame *c, int from)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	int to = ALIGN(from, 4);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	if (from <= to && to <= LAST_OPERAND)
100*4882a593Smuzhiyun 		clear_operands(c, from, to);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun #define AVC_DEBUG_READ_DESCRIPTOR              0x0001
104*4882a593Smuzhiyun #define AVC_DEBUG_DSIT                         0x0002
105*4882a593Smuzhiyun #define AVC_DEBUG_DSD                          0x0004
106*4882a593Smuzhiyun #define AVC_DEBUG_REGISTER_REMOTE_CONTROL      0x0008
107*4882a593Smuzhiyun #define AVC_DEBUG_LNB_CONTROL                  0x0010
108*4882a593Smuzhiyun #define AVC_DEBUG_TUNE_QPSK                    0x0020
109*4882a593Smuzhiyun #define AVC_DEBUG_TUNE_QPSK2                   0x0040
110*4882a593Smuzhiyun #define AVC_DEBUG_HOST2CA                      0x0080
111*4882a593Smuzhiyun #define AVC_DEBUG_CA2HOST                      0x0100
112*4882a593Smuzhiyun #define AVC_DEBUG_APPLICATION_PMT              0x4000
113*4882a593Smuzhiyun #define AVC_DEBUG_FCP_PAYLOADS                 0x8000
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun static int avc_debug;
116*4882a593Smuzhiyun module_param_named(debug, avc_debug, int, 0644);
117*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Verbose logging (none = 0"
118*4882a593Smuzhiyun 	", FCP subactions"
119*4882a593Smuzhiyun 	": READ DESCRIPTOR = "		__stringify(AVC_DEBUG_READ_DESCRIPTOR)
120*4882a593Smuzhiyun 	", DSIT = "			__stringify(AVC_DEBUG_DSIT)
121*4882a593Smuzhiyun 	", REGISTER_REMOTE_CONTROL = "	__stringify(AVC_DEBUG_REGISTER_REMOTE_CONTROL)
122*4882a593Smuzhiyun 	", LNB CONTROL = "		__stringify(AVC_DEBUG_LNB_CONTROL)
123*4882a593Smuzhiyun 	", TUNE QPSK = "		__stringify(AVC_DEBUG_TUNE_QPSK)
124*4882a593Smuzhiyun 	", TUNE QPSK2 = "		__stringify(AVC_DEBUG_TUNE_QPSK2)
125*4882a593Smuzhiyun 	", HOST2CA = "			__stringify(AVC_DEBUG_HOST2CA)
126*4882a593Smuzhiyun 	", CA2HOST = "			__stringify(AVC_DEBUG_CA2HOST)
127*4882a593Smuzhiyun 	"; Application sent PMT = "	__stringify(AVC_DEBUG_APPLICATION_PMT)
128*4882a593Smuzhiyun 	", FCP payloads = "		__stringify(AVC_DEBUG_FCP_PAYLOADS)
129*4882a593Smuzhiyun 	", or a combination, or all = -1)");
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun  * This is a workaround since there is no vendor specific command to retrieve
133*4882a593Smuzhiyun  * ca_info using AVC. If this parameter is not used, ca_system_id will be
134*4882a593Smuzhiyun  * filled with application_manufacturer from ca_app_info.
135*4882a593Smuzhiyun  * Digital Everywhere have said that adding ca_info is on their TODO list.
136*4882a593Smuzhiyun  */
137*4882a593Smuzhiyun static unsigned int num_fake_ca_system_ids;
138*4882a593Smuzhiyun static int fake_ca_system_ids[4] = { -1, -1, -1, -1 };
139*4882a593Smuzhiyun module_param_array(fake_ca_system_ids, int, &num_fake_ca_system_ids, 0644);
140*4882a593Smuzhiyun MODULE_PARM_DESC(fake_ca_system_ids, "If your CAM application manufacturer "
141*4882a593Smuzhiyun 		 "does not have the same ca_system_id as your CAS, you can "
142*4882a593Smuzhiyun 		 "override what ca_system_ids are presented to the "
143*4882a593Smuzhiyun 		 "application by setting this field to an array of ids.");
144*4882a593Smuzhiyun 
debug_fcp_ctype(unsigned int ctype)145*4882a593Smuzhiyun static const char *debug_fcp_ctype(unsigned int ctype)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	static const char *ctypes[] = {
148*4882a593Smuzhiyun 		[0x0] = "CONTROL",		[0x1] = "STATUS",
149*4882a593Smuzhiyun 		[0x2] = "SPECIFIC INQUIRY",	[0x3] = "NOTIFY",
150*4882a593Smuzhiyun 		[0x4] = "GENERAL INQUIRY",	[0x8] = "NOT IMPLEMENTED",
151*4882a593Smuzhiyun 		[0x9] = "ACCEPTED",		[0xa] = "REJECTED",
152*4882a593Smuzhiyun 		[0xb] = "IN TRANSITION",	[0xc] = "IMPLEMENTED/STABLE",
153*4882a593Smuzhiyun 		[0xd] = "CHANGED",		[0xf] = "INTERIM",
154*4882a593Smuzhiyun 	};
155*4882a593Smuzhiyun 	const char *ret = ctype < ARRAY_SIZE(ctypes) ? ctypes[ctype] : NULL;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	return ret ? ret : "?";
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
debug_fcp_opcode(unsigned int opcode,const u8 * data,int length)160*4882a593Smuzhiyun static const char *debug_fcp_opcode(unsigned int opcode,
161*4882a593Smuzhiyun 				    const u8 *data, int length)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	switch (opcode) {
164*4882a593Smuzhiyun 	case AVC_OPCODE_VENDOR:
165*4882a593Smuzhiyun 		break;
166*4882a593Smuzhiyun 	case AVC_OPCODE_READ_DESCRIPTOR:
167*4882a593Smuzhiyun 		return avc_debug & AVC_DEBUG_READ_DESCRIPTOR ?
168*4882a593Smuzhiyun 				"ReadDescriptor" : NULL;
169*4882a593Smuzhiyun 	case AVC_OPCODE_DSIT:
170*4882a593Smuzhiyun 		return avc_debug & AVC_DEBUG_DSIT ?
171*4882a593Smuzhiyun 				"DirectSelectInfo.Type" : NULL;
172*4882a593Smuzhiyun 	case AVC_OPCODE_DSD:
173*4882a593Smuzhiyun 		return avc_debug & AVC_DEBUG_DSD ? "DirectSelectData" : NULL;
174*4882a593Smuzhiyun 	default:
175*4882a593Smuzhiyun 		return "Unknown";
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	if (length < 7 ||
179*4882a593Smuzhiyun 	    data[3] != SFE_VENDOR_DE_COMPANYID_0 ||
180*4882a593Smuzhiyun 	    data[4] != SFE_VENDOR_DE_COMPANYID_1 ||
181*4882a593Smuzhiyun 	    data[5] != SFE_VENDOR_DE_COMPANYID_2)
182*4882a593Smuzhiyun 		return "Vendor/Unknown";
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	switch (data[6]) {
185*4882a593Smuzhiyun 	case SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL:
186*4882a593Smuzhiyun 		return avc_debug & AVC_DEBUG_REGISTER_REMOTE_CONTROL ?
187*4882a593Smuzhiyun 				"RegisterRC" : NULL;
188*4882a593Smuzhiyun 	case SFE_VENDOR_OPCODE_LNB_CONTROL:
189*4882a593Smuzhiyun 		return avc_debug & AVC_DEBUG_LNB_CONTROL ? "LNBControl" : NULL;
190*4882a593Smuzhiyun 	case SFE_VENDOR_OPCODE_TUNE_QPSK:
191*4882a593Smuzhiyun 		return avc_debug & AVC_DEBUG_TUNE_QPSK ? "TuneQPSK" : NULL;
192*4882a593Smuzhiyun 	case SFE_VENDOR_OPCODE_TUNE_QPSK2:
193*4882a593Smuzhiyun 		return avc_debug & AVC_DEBUG_TUNE_QPSK2 ? "TuneQPSK2" : NULL;
194*4882a593Smuzhiyun 	case SFE_VENDOR_OPCODE_HOST2CA:
195*4882a593Smuzhiyun 		return avc_debug & AVC_DEBUG_HOST2CA ? "Host2CA" : NULL;
196*4882a593Smuzhiyun 	case SFE_VENDOR_OPCODE_CA2HOST:
197*4882a593Smuzhiyun 		return avc_debug & AVC_DEBUG_CA2HOST ? "CA2Host" : NULL;
198*4882a593Smuzhiyun 	}
199*4882a593Smuzhiyun 	return "Vendor/Unknown";
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
debug_fcp(const u8 * data,int length)202*4882a593Smuzhiyun static void debug_fcp(const u8 *data, int length)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	unsigned int subunit_type, subunit_id, opcode;
205*4882a593Smuzhiyun 	const char *op, *prefix;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	prefix       = data[0] > 7 ? "FCP <- " : "FCP -> ";
208*4882a593Smuzhiyun 	subunit_type = data[1] >> 3;
209*4882a593Smuzhiyun 	subunit_id   = data[1] & 7;
210*4882a593Smuzhiyun 	opcode       = subunit_type == 0x1e || subunit_id == 5 ? ~0 : data[2];
211*4882a593Smuzhiyun 	op           = debug_fcp_opcode(opcode, data, length);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (op) {
214*4882a593Smuzhiyun 		printk(KERN_INFO "%ssu=%x.%x l=%d: %-8s - %s\n",
215*4882a593Smuzhiyun 		       prefix, subunit_type, subunit_id, length,
216*4882a593Smuzhiyun 		       debug_fcp_ctype(data[0]), op);
217*4882a593Smuzhiyun 		if (avc_debug & AVC_DEBUG_FCP_PAYLOADS)
218*4882a593Smuzhiyun 			print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_NONE,
219*4882a593Smuzhiyun 				       16, 1, data, length, false);
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
debug_pmt(char * msg,int length)223*4882a593Smuzhiyun static void debug_pmt(char *msg, int length)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun 	printk(KERN_INFO "APP PMT -> l=%d\n", length);
226*4882a593Smuzhiyun 	print_hex_dump(KERN_INFO, "APP PMT -> ", DUMP_PREFIX_NONE,
227*4882a593Smuzhiyun 		       16, 1, msg, length, false);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
avc_write(struct firedtv * fdtv)230*4882a593Smuzhiyun static int avc_write(struct firedtv *fdtv)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	int err, retry;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	fdtv->avc_reply_received = false;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	for (retry = 0; retry < 6; retry++) {
237*4882a593Smuzhiyun 		if (unlikely(avc_debug))
238*4882a593Smuzhiyun 			debug_fcp(fdtv->avc_data, fdtv->avc_data_length);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 		err = fdtv_write(fdtv, FCP_COMMAND_REGISTER,
241*4882a593Smuzhiyun 				 fdtv->avc_data, fdtv->avc_data_length);
242*4882a593Smuzhiyun 		if (err) {
243*4882a593Smuzhiyun 			dev_err(fdtv->device, "FCP command write failed\n");
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 			return err;
246*4882a593Smuzhiyun 		}
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 		/*
249*4882a593Smuzhiyun 		 * AV/C specs say that answers should be sent within 150 ms.
250*4882a593Smuzhiyun 		 * Time out after 200 ms.
251*4882a593Smuzhiyun 		 */
252*4882a593Smuzhiyun 		if (wait_event_timeout(fdtv->avc_wait,
253*4882a593Smuzhiyun 				       fdtv->avc_reply_received,
254*4882a593Smuzhiyun 				       msecs_to_jiffies(200)) != 0)
255*4882a593Smuzhiyun 			return 0;
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun 	dev_err(fdtv->device, "FCP response timed out\n");
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	return -ETIMEDOUT;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
is_register_rc(struct avc_response_frame * r)262*4882a593Smuzhiyun static bool is_register_rc(struct avc_response_frame *r)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	return r->opcode     == AVC_OPCODE_VENDOR &&
265*4882a593Smuzhiyun 	       r->operand[0] == SFE_VENDOR_DE_COMPANYID_0 &&
266*4882a593Smuzhiyun 	       r->operand[1] == SFE_VENDOR_DE_COMPANYID_1 &&
267*4882a593Smuzhiyun 	       r->operand[2] == SFE_VENDOR_DE_COMPANYID_2 &&
268*4882a593Smuzhiyun 	       r->operand[3] == SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
avc_recv(struct firedtv * fdtv,void * data,size_t length)271*4882a593Smuzhiyun int avc_recv(struct firedtv *fdtv, void *data, size_t length)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	struct avc_response_frame *r = data;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (unlikely(avc_debug))
276*4882a593Smuzhiyun 		debug_fcp(data, length);
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	if (length >= 8 && is_register_rc(r)) {
279*4882a593Smuzhiyun 		switch (r->response) {
280*4882a593Smuzhiyun 		case AVC_RESPONSE_CHANGED:
281*4882a593Smuzhiyun 			fdtv_handle_rc(fdtv, r->operand[4] << 8 | r->operand[5]);
282*4882a593Smuzhiyun 			schedule_work(&fdtv->remote_ctrl_work);
283*4882a593Smuzhiyun 			break;
284*4882a593Smuzhiyun 		case AVC_RESPONSE_INTERIM:
285*4882a593Smuzhiyun 			if (is_register_rc((void *)fdtv->avc_data))
286*4882a593Smuzhiyun 				goto wake;
287*4882a593Smuzhiyun 			break;
288*4882a593Smuzhiyun 		default:
289*4882a593Smuzhiyun 			dev_info(fdtv->device,
290*4882a593Smuzhiyun 				 "remote control result = %d\n", r->response);
291*4882a593Smuzhiyun 		}
292*4882a593Smuzhiyun 		return 0;
293*4882a593Smuzhiyun 	}
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	if (fdtv->avc_reply_received) {
296*4882a593Smuzhiyun 		dev_err(fdtv->device, "out-of-order AVC response, ignored\n");
297*4882a593Smuzhiyun 		return -EIO;
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	memcpy(fdtv->avc_data, data, length);
301*4882a593Smuzhiyun 	fdtv->avc_data_length = length;
302*4882a593Smuzhiyun wake:
303*4882a593Smuzhiyun 	fdtv->avc_reply_received = true;
304*4882a593Smuzhiyun 	wake_up(&fdtv->avc_wait);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	return 0;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
add_pid_filter(struct firedtv * fdtv,u8 * operand)309*4882a593Smuzhiyun static int add_pid_filter(struct firedtv *fdtv, u8 *operand)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	int i, n, pos = 1;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	for (i = 0, n = 0; i < 16; i++) {
314*4882a593Smuzhiyun 		if (test_bit(i, &fdtv->channel_active)) {
315*4882a593Smuzhiyun 			operand[pos++] = 0x13; /* flowfunction relay */
316*4882a593Smuzhiyun 			operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
317*4882a593Smuzhiyun 			operand[pos++] = (fdtv->channel_pid[i] >> 8) & 0x1f;
318*4882a593Smuzhiyun 			operand[pos++] = fdtv->channel_pid[i] & 0xff;
319*4882a593Smuzhiyun 			operand[pos++] = 0x00; /* tableID */
320*4882a593Smuzhiyun 			operand[pos++] = 0x00; /* filter_length */
321*4882a593Smuzhiyun 			n++;
322*4882a593Smuzhiyun 		}
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 	operand[0] = n;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	return pos;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /*
330*4882a593Smuzhiyun  * tuning command for setting the relative LNB frequency
331*4882a593Smuzhiyun  * (not supported by the AVC standard)
332*4882a593Smuzhiyun  */
avc_tuner_tuneqpsk(struct firedtv * fdtv,struct dtv_frontend_properties * p)333*4882a593Smuzhiyun static int avc_tuner_tuneqpsk(struct firedtv *fdtv,
334*4882a593Smuzhiyun 			      struct dtv_frontend_properties *p)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	c->opcode = AVC_OPCODE_VENDOR;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
341*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
342*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
343*4882a593Smuzhiyun 	if (fdtv->type == FIREDTV_DVB_S2)
344*4882a593Smuzhiyun 		c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK2;
345*4882a593Smuzhiyun 	else
346*4882a593Smuzhiyun 		c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	c->operand[4] = (p->frequency >> 24) & 0xff;
349*4882a593Smuzhiyun 	c->operand[5] = (p->frequency >> 16) & 0xff;
350*4882a593Smuzhiyun 	c->operand[6] = (p->frequency >> 8) & 0xff;
351*4882a593Smuzhiyun 	c->operand[7] = p->frequency & 0xff;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	c->operand[8] = ((p->symbol_rate / 1000) >> 8) & 0xff;
354*4882a593Smuzhiyun 	c->operand[9] = (p->symbol_rate / 1000) & 0xff;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	switch (p->fec_inner) {
357*4882a593Smuzhiyun 	case FEC_1_2:	c->operand[10] = 0x1; break;
358*4882a593Smuzhiyun 	case FEC_2_3:	c->operand[10] = 0x2; break;
359*4882a593Smuzhiyun 	case FEC_3_4:	c->operand[10] = 0x3; break;
360*4882a593Smuzhiyun 	case FEC_5_6:	c->operand[10] = 0x4; break;
361*4882a593Smuzhiyun 	case FEC_7_8:	c->operand[10] = 0x5; break;
362*4882a593Smuzhiyun 	case FEC_4_5:
363*4882a593Smuzhiyun 	case FEC_8_9:
364*4882a593Smuzhiyun 	case FEC_AUTO:
365*4882a593Smuzhiyun 	default:	c->operand[10] = 0x0;
366*4882a593Smuzhiyun 	}
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	if (fdtv->voltage == 0xff)
369*4882a593Smuzhiyun 		c->operand[11] = 0xff;
370*4882a593Smuzhiyun 	else if (fdtv->voltage == SEC_VOLTAGE_18) /* polarisation */
371*4882a593Smuzhiyun 		c->operand[11] = 0;
372*4882a593Smuzhiyun 	else
373*4882a593Smuzhiyun 		c->operand[11] = 1;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	if (fdtv->tone == 0xff)
376*4882a593Smuzhiyun 		c->operand[12] = 0xff;
377*4882a593Smuzhiyun 	else if (fdtv->tone == SEC_TONE_ON) /* band */
378*4882a593Smuzhiyun 		c->operand[12] = 1;
379*4882a593Smuzhiyun 	else
380*4882a593Smuzhiyun 		c->operand[12] = 0;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	if (fdtv->type == FIREDTV_DVB_S2) {
383*4882a593Smuzhiyun 		if (fdtv->fe.dtv_property_cache.delivery_system == SYS_DVBS2) {
384*4882a593Smuzhiyun 			switch (fdtv->fe.dtv_property_cache.modulation) {
385*4882a593Smuzhiyun 			case QAM_16:		c->operand[13] = 0x1; break;
386*4882a593Smuzhiyun 			case QPSK:		c->operand[13] = 0x2; break;
387*4882a593Smuzhiyun 			case PSK_8:		c->operand[13] = 0x3; break;
388*4882a593Smuzhiyun 			default:		c->operand[13] = 0x2; break;
389*4882a593Smuzhiyun 			}
390*4882a593Smuzhiyun 			switch (fdtv->fe.dtv_property_cache.rolloff) {
391*4882a593Smuzhiyun 			case ROLLOFF_35:	c->operand[14] = 0x2; break;
392*4882a593Smuzhiyun 			case ROLLOFF_20:	c->operand[14] = 0x0; break;
393*4882a593Smuzhiyun 			case ROLLOFF_25:	c->operand[14] = 0x1; break;
394*4882a593Smuzhiyun 			case ROLLOFF_AUTO:
395*4882a593Smuzhiyun 			default:		c->operand[14] = 0x2; break;
396*4882a593Smuzhiyun 			/* case ROLLOFF_NONE:	c->operand[14] = 0xff; break; */
397*4882a593Smuzhiyun 			}
398*4882a593Smuzhiyun 			switch (fdtv->fe.dtv_property_cache.pilot) {
399*4882a593Smuzhiyun 			case PILOT_AUTO:	c->operand[15] = 0x0; break;
400*4882a593Smuzhiyun 			case PILOT_OFF:		c->operand[15] = 0x0; break;
401*4882a593Smuzhiyun 			case PILOT_ON:		c->operand[15] = 0x1; break;
402*4882a593Smuzhiyun 			}
403*4882a593Smuzhiyun 		} else {
404*4882a593Smuzhiyun 			c->operand[13] = 0x1;  /* auto modulation */
405*4882a593Smuzhiyun 			c->operand[14] = 0xff; /* disable rolloff */
406*4882a593Smuzhiyun 			c->operand[15] = 0xff; /* disable pilot */
407*4882a593Smuzhiyun 		}
408*4882a593Smuzhiyun 		return 16;
409*4882a593Smuzhiyun 	} else {
410*4882a593Smuzhiyun 		return 13;
411*4882a593Smuzhiyun 	}
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
avc_tuner_dsd_dvb_c(struct firedtv * fdtv,struct dtv_frontend_properties * p)414*4882a593Smuzhiyun static int avc_tuner_dsd_dvb_c(struct firedtv *fdtv,
415*4882a593Smuzhiyun 			       struct dtv_frontend_properties *p)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	c->opcode = AVC_OPCODE_DSD;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	c->operand[0] = 0;    /* source plug */
422*4882a593Smuzhiyun 	c->operand[1] = 0xd2; /* subfunction replace */
423*4882a593Smuzhiyun 	c->operand[2] = 0x20; /* system id = DVB */
424*4882a593Smuzhiyun 	c->operand[3] = 0x00; /* antenna number */
425*4882a593Smuzhiyun 	c->operand[4] = 0x11; /* system_specific_multiplex selection_length */
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	/* multiplex_valid_flags, high byte */
428*4882a593Smuzhiyun 	c->operand[5] =   0 << 7 /* reserved */
429*4882a593Smuzhiyun 			| 0 << 6 /* Polarisation */
430*4882a593Smuzhiyun 			| 0 << 5 /* Orbital_Pos */
431*4882a593Smuzhiyun 			| 1 << 4 /* Frequency */
432*4882a593Smuzhiyun 			| 1 << 3 /* Symbol_Rate */
433*4882a593Smuzhiyun 			| 0 << 2 /* FEC_outer */
434*4882a593Smuzhiyun 			| (p->fec_inner  != FEC_AUTO ? 1 << 1 : 0)
435*4882a593Smuzhiyun 			| (p->modulation != QAM_AUTO ? 1 << 0 : 0);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	/* multiplex_valid_flags, low byte */
438*4882a593Smuzhiyun 	c->operand[6] =   0 << 7 /* NetworkID */
439*4882a593Smuzhiyun 			| 0 << 0 /* reserved */ ;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	c->operand[7]  = 0x00;
442*4882a593Smuzhiyun 	c->operand[8]  = 0x00;
443*4882a593Smuzhiyun 	c->operand[9]  = 0x00;
444*4882a593Smuzhiyun 	c->operand[10] = 0x00;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	c->operand[11] = (((p->frequency / 4000) >> 16) & 0xff) | (2 << 6);
447*4882a593Smuzhiyun 	c->operand[12] = ((p->frequency / 4000) >> 8) & 0xff;
448*4882a593Smuzhiyun 	c->operand[13] = (p->frequency / 4000) & 0xff;
449*4882a593Smuzhiyun 	c->operand[14] = ((p->symbol_rate / 1000) >> 12) & 0xff;
450*4882a593Smuzhiyun 	c->operand[15] = ((p->symbol_rate / 1000) >> 4) & 0xff;
451*4882a593Smuzhiyun 	c->operand[16] = ((p->symbol_rate / 1000) << 4) & 0xf0;
452*4882a593Smuzhiyun 	c->operand[17] = 0x00;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	switch (p->fec_inner) {
455*4882a593Smuzhiyun 	case FEC_1_2:	c->operand[18] = 0x1; break;
456*4882a593Smuzhiyun 	case FEC_2_3:	c->operand[18] = 0x2; break;
457*4882a593Smuzhiyun 	case FEC_3_4:	c->operand[18] = 0x3; break;
458*4882a593Smuzhiyun 	case FEC_5_6:	c->operand[18] = 0x4; break;
459*4882a593Smuzhiyun 	case FEC_7_8:	c->operand[18] = 0x5; break;
460*4882a593Smuzhiyun 	case FEC_8_9:	c->operand[18] = 0x6; break;
461*4882a593Smuzhiyun 	case FEC_4_5:	c->operand[18] = 0x8; break;
462*4882a593Smuzhiyun 	case FEC_AUTO:
463*4882a593Smuzhiyun 	default:	c->operand[18] = 0x0;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	switch (p->modulation) {
467*4882a593Smuzhiyun 	case QAM_16:	c->operand[19] = 0x08; break;
468*4882a593Smuzhiyun 	case QAM_32:	c->operand[19] = 0x10; break;
469*4882a593Smuzhiyun 	case QAM_64:	c->operand[19] = 0x18; break;
470*4882a593Smuzhiyun 	case QAM_128:	c->operand[19] = 0x20; break;
471*4882a593Smuzhiyun 	case QAM_256:	c->operand[19] = 0x28; break;
472*4882a593Smuzhiyun 	case QAM_AUTO:
473*4882a593Smuzhiyun 	default:	c->operand[19] = 0x00;
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	c->operand[20] = 0x00;
477*4882a593Smuzhiyun 	c->operand[21] = 0x00;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	return 22 + add_pid_filter(fdtv, &c->operand[22]);
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun 
avc_tuner_dsd_dvb_t(struct firedtv * fdtv,struct dtv_frontend_properties * p)482*4882a593Smuzhiyun static int avc_tuner_dsd_dvb_t(struct firedtv *fdtv,
483*4882a593Smuzhiyun 			       struct dtv_frontend_properties *p)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	c->opcode = AVC_OPCODE_DSD;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	c->operand[0] = 0;    /* source plug */
490*4882a593Smuzhiyun 	c->operand[1] = 0xd2; /* subfunction replace */
491*4882a593Smuzhiyun 	c->operand[2] = 0x20; /* system id = DVB */
492*4882a593Smuzhiyun 	c->operand[3] = 0x00; /* antenna number */
493*4882a593Smuzhiyun 	c->operand[4] = 0x0c; /* system_specific_multiplex selection_length */
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	/* multiplex_valid_flags, high byte */
496*4882a593Smuzhiyun 	c->operand[5] =
497*4882a593Smuzhiyun 	      0 << 7 /* reserved */
498*4882a593Smuzhiyun 	    | 1 << 6 /* CenterFrequency */
499*4882a593Smuzhiyun 	    | (p->bandwidth_hz != 0        ? 1 << 5 : 0)
500*4882a593Smuzhiyun 	    | (p->modulation  != QAM_AUTO              ? 1 << 4 : 0)
501*4882a593Smuzhiyun 	    | (p->hierarchy != HIERARCHY_AUTO ? 1 << 3 : 0)
502*4882a593Smuzhiyun 	    | (p->code_rate_HP   != FEC_AUTO              ? 1 << 2 : 0)
503*4882a593Smuzhiyun 	    | (p->code_rate_LP   != FEC_AUTO              ? 1 << 1 : 0)
504*4882a593Smuzhiyun 	    | (p->guard_interval != GUARD_INTERVAL_AUTO   ? 1 << 0 : 0);
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	/* multiplex_valid_flags, low byte */
507*4882a593Smuzhiyun 	c->operand[6] =
508*4882a593Smuzhiyun 	      0 << 7 /* NetworkID */
509*4882a593Smuzhiyun 	    | (p->transmission_mode != TRANSMISSION_MODE_AUTO ? 1 << 6 : 0)
510*4882a593Smuzhiyun 	    | 0 << 5 /* OtherFrequencyFlag */
511*4882a593Smuzhiyun 	    | 0 << 0 /* reserved */ ;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	c->operand[7]  = 0x0;
514*4882a593Smuzhiyun 	c->operand[8]  = (p->frequency / 10) >> 24;
515*4882a593Smuzhiyun 	c->operand[9]  = ((p->frequency / 10) >> 16) & 0xff;
516*4882a593Smuzhiyun 	c->operand[10] = ((p->frequency / 10) >>  8) & 0xff;
517*4882a593Smuzhiyun 	c->operand[11] = (p->frequency / 10) & 0xff;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	switch (p->bandwidth_hz) {
520*4882a593Smuzhiyun 	case 7000000:	c->operand[12] = 0x20; break;
521*4882a593Smuzhiyun 	case 8000000:
522*4882a593Smuzhiyun 	case 6000000:	/* not defined by AVC spec */
523*4882a593Smuzhiyun 	case 0:
524*4882a593Smuzhiyun 	default:		c->operand[12] = 0x00;
525*4882a593Smuzhiyun 	}
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	switch (p->modulation) {
528*4882a593Smuzhiyun 	case QAM_16:	c->operand[13] = 1 << 6; break;
529*4882a593Smuzhiyun 	case QAM_64:	c->operand[13] = 2 << 6; break;
530*4882a593Smuzhiyun 	case QPSK:
531*4882a593Smuzhiyun 	default:	c->operand[13] = 0x00;
532*4882a593Smuzhiyun 	}
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	switch (p->hierarchy) {
535*4882a593Smuzhiyun 	case HIERARCHY_1:	c->operand[13] |= 1 << 3; break;
536*4882a593Smuzhiyun 	case HIERARCHY_2:	c->operand[13] |= 2 << 3; break;
537*4882a593Smuzhiyun 	case HIERARCHY_4:	c->operand[13] |= 3 << 3; break;
538*4882a593Smuzhiyun 	case HIERARCHY_AUTO:
539*4882a593Smuzhiyun 	case HIERARCHY_NONE:
540*4882a593Smuzhiyun 	default:		break;
541*4882a593Smuzhiyun 	}
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	switch (p->code_rate_HP) {
544*4882a593Smuzhiyun 	case FEC_2_3:	c->operand[13] |= 1; break;
545*4882a593Smuzhiyun 	case FEC_3_4:	c->operand[13] |= 2; break;
546*4882a593Smuzhiyun 	case FEC_5_6:	c->operand[13] |= 3; break;
547*4882a593Smuzhiyun 	case FEC_7_8:	c->operand[13] |= 4; break;
548*4882a593Smuzhiyun 	case FEC_1_2:
549*4882a593Smuzhiyun 	default:	break;
550*4882a593Smuzhiyun 	}
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	switch (p->code_rate_LP) {
553*4882a593Smuzhiyun 	case FEC_2_3:	c->operand[14] = 1 << 5; break;
554*4882a593Smuzhiyun 	case FEC_3_4:	c->operand[14] = 2 << 5; break;
555*4882a593Smuzhiyun 	case FEC_5_6:	c->operand[14] = 3 << 5; break;
556*4882a593Smuzhiyun 	case FEC_7_8:	c->operand[14] = 4 << 5; break;
557*4882a593Smuzhiyun 	case FEC_1_2:
558*4882a593Smuzhiyun 	default:	c->operand[14] = 0x00; break;
559*4882a593Smuzhiyun 	}
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	switch (p->guard_interval) {
562*4882a593Smuzhiyun 	case GUARD_INTERVAL_1_16:	c->operand[14] |= 1 << 3; break;
563*4882a593Smuzhiyun 	case GUARD_INTERVAL_1_8:	c->operand[14] |= 2 << 3; break;
564*4882a593Smuzhiyun 	case GUARD_INTERVAL_1_4:	c->operand[14] |= 3 << 3; break;
565*4882a593Smuzhiyun 	case GUARD_INTERVAL_1_32:
566*4882a593Smuzhiyun 	case GUARD_INTERVAL_AUTO:
567*4882a593Smuzhiyun 	default:			break;
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	switch (p->transmission_mode) {
571*4882a593Smuzhiyun 	case TRANSMISSION_MODE_8K:	c->operand[14] |= 1 << 1; break;
572*4882a593Smuzhiyun 	case TRANSMISSION_MODE_2K:
573*4882a593Smuzhiyun 	case TRANSMISSION_MODE_AUTO:
574*4882a593Smuzhiyun 	default:			break;
575*4882a593Smuzhiyun 	}
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	c->operand[15] = 0x00; /* network_ID[0] */
578*4882a593Smuzhiyun 	c->operand[16] = 0x00; /* network_ID[1] */
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	return 17 + add_pid_filter(fdtv, &c->operand[17]);
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun 
avc_tuner_dsd(struct firedtv * fdtv,struct dtv_frontend_properties * p)583*4882a593Smuzhiyun int avc_tuner_dsd(struct firedtv *fdtv,
584*4882a593Smuzhiyun 		  struct dtv_frontend_properties *p)
585*4882a593Smuzhiyun {
586*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
587*4882a593Smuzhiyun 	int pos, ret;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_CONTROL;
592*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	switch (fdtv->type) {
595*4882a593Smuzhiyun 	case FIREDTV_DVB_S:
596*4882a593Smuzhiyun 	case FIREDTV_DVB_S2: pos = avc_tuner_tuneqpsk(fdtv, p); break;
597*4882a593Smuzhiyun 	case FIREDTV_DVB_C: pos = avc_tuner_dsd_dvb_c(fdtv, p); break;
598*4882a593Smuzhiyun 	case FIREDTV_DVB_T: pos = avc_tuner_dsd_dvb_t(fdtv, p); break;
599*4882a593Smuzhiyun 	default:
600*4882a593Smuzhiyun 		BUG();
601*4882a593Smuzhiyun 	}
602*4882a593Smuzhiyun 	pad_operands(c, pos);
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	fdtv->avc_data_length = ALIGN(3 + pos, 4);
605*4882a593Smuzhiyun 	ret = avc_write(fdtv);
606*4882a593Smuzhiyun #if 0
607*4882a593Smuzhiyun 	/*
608*4882a593Smuzhiyun 	 * FIXME:
609*4882a593Smuzhiyun 	 * u8 *status was an out-parameter of avc_tuner_dsd, unused by caller.
610*4882a593Smuzhiyun 	 * Check for AVC_RESPONSE_ACCEPTED here instead?
611*4882a593Smuzhiyun 	 */
612*4882a593Smuzhiyun 	if (status)
613*4882a593Smuzhiyun 		*status = r->operand[2];
614*4882a593Smuzhiyun #endif
615*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	if (ret == 0)
618*4882a593Smuzhiyun 		msleep(500);
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	return ret;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun 
avc_tuner_set_pids(struct firedtv * fdtv,unsigned char pidc,u16 pid[])623*4882a593Smuzhiyun int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
626*4882a593Smuzhiyun 	int ret, pos, k;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	if (pidc > 16 && pidc != 0xff)
629*4882a593Smuzhiyun 		return -EINVAL;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_CONTROL;
634*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
635*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_DSD;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	c->operand[0] = 0;	/* source plug */
638*4882a593Smuzhiyun 	c->operand[1] = 0xd2;	/* subfunction replace */
639*4882a593Smuzhiyun 	c->operand[2] = 0x20;	/* system id = DVB */
640*4882a593Smuzhiyun 	c->operand[3] = 0x00;	/* antenna number */
641*4882a593Smuzhiyun 	c->operand[4] = 0x00;	/* system_specific_multiplex selection_length */
642*4882a593Smuzhiyun 	c->operand[5] = pidc;	/* Nr_of_dsd_sel_specs */
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	pos = 6;
645*4882a593Smuzhiyun 	if (pidc != 0xff)
646*4882a593Smuzhiyun 		for (k = 0; k < pidc; k++) {
647*4882a593Smuzhiyun 			c->operand[pos++] = 0x13; /* flowfunction relay */
648*4882a593Smuzhiyun 			c->operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
649*4882a593Smuzhiyun 			c->operand[pos++] = (pid[k] >> 8) & 0x1f;
650*4882a593Smuzhiyun 			c->operand[pos++] = pid[k] & 0xff;
651*4882a593Smuzhiyun 			c->operand[pos++] = 0x00; /* tableID */
652*4882a593Smuzhiyun 			c->operand[pos++] = 0x00; /* filter_length */
653*4882a593Smuzhiyun 		}
654*4882a593Smuzhiyun 	pad_operands(c, pos);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	fdtv->avc_data_length = ALIGN(3 + pos, 4);
657*4882a593Smuzhiyun 	ret = avc_write(fdtv);
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	/* FIXME: check response code? */
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	if (ret == 0)
664*4882a593Smuzhiyun 		msleep(50);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	return ret;
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun 
avc_tuner_get_ts(struct firedtv * fdtv)669*4882a593Smuzhiyun int avc_tuner_get_ts(struct firedtv *fdtv)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
672*4882a593Smuzhiyun 	int ret, sl;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_CONTROL;
677*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
678*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_DSIT;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	sl = fdtv->type == FIREDTV_DVB_T ? 0x0c : 0x11;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	c->operand[0] = 0;	/* source plug */
683*4882a593Smuzhiyun 	c->operand[1] = 0xd2;	/* subfunction replace */
684*4882a593Smuzhiyun 	c->operand[2] = 0xff;	/* status */
685*4882a593Smuzhiyun 	c->operand[3] = 0x20;	/* system id = DVB */
686*4882a593Smuzhiyun 	c->operand[4] = 0x00;	/* antenna number */
687*4882a593Smuzhiyun 	c->operand[5] = 0x0;	/* system_specific_search_flags */
688*4882a593Smuzhiyun 	c->operand[6] = sl;	/* system_specific_multiplex selection_length */
689*4882a593Smuzhiyun 	/*
690*4882a593Smuzhiyun 	 * operand[7]: valid_flags[0]
691*4882a593Smuzhiyun 	 * operand[8]: valid_flags[1]
692*4882a593Smuzhiyun 	 * operand[7 + sl]: nr_of_dsit_sel_specs (always 0)
693*4882a593Smuzhiyun 	 */
694*4882a593Smuzhiyun 	clear_operands(c, 7, 24);
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	fdtv->avc_data_length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
697*4882a593Smuzhiyun 	ret = avc_write(fdtv);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	/* FIXME: check response code? */
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	if (ret == 0)
704*4882a593Smuzhiyun 		msleep(250);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	return ret;
707*4882a593Smuzhiyun }
708*4882a593Smuzhiyun 
avc_identify_subunit(struct firedtv * fdtv)709*4882a593Smuzhiyun int avc_identify_subunit(struct firedtv *fdtv)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
712*4882a593Smuzhiyun 	struct avc_response_frame *r = (void *)fdtv->avc_data;
713*4882a593Smuzhiyun 	int ret;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_CONTROL;
718*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
719*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_READ_DESCRIPTOR;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	c->operand[0] = DESCRIPTOR_SUBUNIT_IDENTIFIER;
722*4882a593Smuzhiyun 	c->operand[1] = 0xff;
723*4882a593Smuzhiyun 	c->operand[2] = 0x00;
724*4882a593Smuzhiyun 	c->operand[3] = 0x00; /* length highbyte */
725*4882a593Smuzhiyun 	c->operand[4] = 0x08; /* length lowbyte  */
726*4882a593Smuzhiyun 	c->operand[5] = 0x00; /* offset highbyte */
727*4882a593Smuzhiyun 	c->operand[6] = 0x0d; /* offset lowbyte  */
728*4882a593Smuzhiyun 	clear_operands(c, 7, 8); /* padding */
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	fdtv->avc_data_length = 12;
731*4882a593Smuzhiyun 	ret = avc_write(fdtv);
732*4882a593Smuzhiyun 	if (ret < 0)
733*4882a593Smuzhiyun 		goto out;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	if ((r->response != AVC_RESPONSE_STABLE &&
736*4882a593Smuzhiyun 	     r->response != AVC_RESPONSE_ACCEPTED) ||
737*4882a593Smuzhiyun 	    (r->operand[3] << 8) + r->operand[4] != 8) {
738*4882a593Smuzhiyun 		dev_err(fdtv->device, "cannot read subunit identifier\n");
739*4882a593Smuzhiyun 		ret = -EINVAL;
740*4882a593Smuzhiyun 	}
741*4882a593Smuzhiyun out:
742*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	return ret;
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun #define SIZEOF_ANTENNA_INPUT_INFO 22
748*4882a593Smuzhiyun 
avc_tuner_status(struct firedtv * fdtv,struct firedtv_tuner_status * stat)749*4882a593Smuzhiyun int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
752*4882a593Smuzhiyun 	struct avc_response_frame *r = (void *)fdtv->avc_data;
753*4882a593Smuzhiyun 	int length, ret;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_CONTROL;
758*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
759*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_READ_DESCRIPTOR;
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	c->operand[0] = DESCRIPTOR_TUNER_STATUS;
762*4882a593Smuzhiyun 	c->operand[1] = 0xff;	/* read_result_status */
763*4882a593Smuzhiyun 	/*
764*4882a593Smuzhiyun 	 * operand[2]: reserved
765*4882a593Smuzhiyun 	 * operand[3]: SIZEOF_ANTENNA_INPUT_INFO >> 8
766*4882a593Smuzhiyun 	 * operand[4]: SIZEOF_ANTENNA_INPUT_INFO & 0xff
767*4882a593Smuzhiyun 	 */
768*4882a593Smuzhiyun 	clear_operands(c, 2, 31);
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	fdtv->avc_data_length = 12;
771*4882a593Smuzhiyun 	ret = avc_write(fdtv);
772*4882a593Smuzhiyun 	if (ret < 0)
773*4882a593Smuzhiyun 		goto out;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	if (r->response != AVC_RESPONSE_STABLE &&
776*4882a593Smuzhiyun 	    r->response != AVC_RESPONSE_ACCEPTED) {
777*4882a593Smuzhiyun 		dev_err(fdtv->device, "cannot read tuner status\n");
778*4882a593Smuzhiyun 		ret = -EINVAL;
779*4882a593Smuzhiyun 		goto out;
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	length = r->operand[9];
783*4882a593Smuzhiyun 	if (r->operand[1] != 0x10 || length != SIZEOF_ANTENNA_INPUT_INFO) {
784*4882a593Smuzhiyun 		dev_err(fdtv->device, "got invalid tuner status\n");
785*4882a593Smuzhiyun 		ret = -EINVAL;
786*4882a593Smuzhiyun 		goto out;
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	stat->active_system		= r->operand[10];
790*4882a593Smuzhiyun 	stat->searching			= r->operand[11] >> 7 & 1;
791*4882a593Smuzhiyun 	stat->moving			= r->operand[11] >> 6 & 1;
792*4882a593Smuzhiyun 	stat->no_rf			= r->operand[11] >> 5 & 1;
793*4882a593Smuzhiyun 	stat->input			= r->operand[12] >> 7 & 1;
794*4882a593Smuzhiyun 	stat->selected_antenna		= r->operand[12] & 0x7f;
795*4882a593Smuzhiyun 	stat->ber			= r->operand[13] << 24 |
796*4882a593Smuzhiyun 					  r->operand[14] << 16 |
797*4882a593Smuzhiyun 					  r->operand[15] << 8 |
798*4882a593Smuzhiyun 					  r->operand[16];
799*4882a593Smuzhiyun 	stat->signal_strength		= r->operand[17];
800*4882a593Smuzhiyun 	stat->raster_frequency		= r->operand[18] >> 6 & 2;
801*4882a593Smuzhiyun 	stat->rf_frequency		= (r->operand[18] & 0x3f) << 16 |
802*4882a593Smuzhiyun 					  r->operand[19] << 8 |
803*4882a593Smuzhiyun 					  r->operand[20];
804*4882a593Smuzhiyun 	stat->man_dep_info_length	= r->operand[21];
805*4882a593Smuzhiyun 	stat->front_end_error		= r->operand[22] >> 4 & 1;
806*4882a593Smuzhiyun 	stat->antenna_error		= r->operand[22] >> 3 & 1;
807*4882a593Smuzhiyun 	stat->front_end_power_status	= r->operand[22] >> 1 & 1;
808*4882a593Smuzhiyun 	stat->power_supply		= r->operand[22] & 1;
809*4882a593Smuzhiyun 	stat->carrier_noise_ratio	= r->operand[23] << 8 |
810*4882a593Smuzhiyun 					  r->operand[24];
811*4882a593Smuzhiyun 	stat->power_supply_voltage	= r->operand[27];
812*4882a593Smuzhiyun 	stat->antenna_voltage		= r->operand[28];
813*4882a593Smuzhiyun 	stat->firewire_bus_voltage	= r->operand[29];
814*4882a593Smuzhiyun 	stat->ca_mmi			= r->operand[30] & 1;
815*4882a593Smuzhiyun 	stat->ca_pmt_reply		= r->operand[31] >> 7 & 1;
816*4882a593Smuzhiyun 	stat->ca_date_time_request	= r->operand[31] >> 6 & 1;
817*4882a593Smuzhiyun 	stat->ca_application_info	= r->operand[31] >> 5 & 1;
818*4882a593Smuzhiyun 	stat->ca_module_present_status	= r->operand[31] >> 4 & 1;
819*4882a593Smuzhiyun 	stat->ca_dvb_flag		= r->operand[31] >> 3 & 1;
820*4882a593Smuzhiyun 	stat->ca_error_flag		= r->operand[31] >> 2 & 1;
821*4882a593Smuzhiyun 	stat->ca_initialization_status	= r->operand[31] >> 1 & 1;
822*4882a593Smuzhiyun out:
823*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	return ret;
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun 
avc_lnb_control(struct firedtv * fdtv,char voltage,char burst,char conttone,char nrdiseq,struct dvb_diseqc_master_cmd * diseqcmd)828*4882a593Smuzhiyun int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
829*4882a593Smuzhiyun 		    char conttone, char nrdiseq,
830*4882a593Smuzhiyun 		    struct dvb_diseqc_master_cmd *diseqcmd)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
833*4882a593Smuzhiyun 	struct avc_response_frame *r = (void *)fdtv->avc_data;
834*4882a593Smuzhiyun 	int pos, j, k, ret;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_CONTROL;
839*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
840*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
843*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
844*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
845*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_LNB_CONTROL;
846*4882a593Smuzhiyun 	c->operand[4] = voltage;
847*4882a593Smuzhiyun 	c->operand[5] = nrdiseq;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	pos = 6;
850*4882a593Smuzhiyun 	for (j = 0; j < nrdiseq; j++) {
851*4882a593Smuzhiyun 		c->operand[pos++] = diseqcmd[j].msg_len;
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 		for (k = 0; k < diseqcmd[j].msg_len; k++)
854*4882a593Smuzhiyun 			c->operand[pos++] = diseqcmd[j].msg[k];
855*4882a593Smuzhiyun 	}
856*4882a593Smuzhiyun 	c->operand[pos++] = burst;
857*4882a593Smuzhiyun 	c->operand[pos++] = conttone;
858*4882a593Smuzhiyun 	pad_operands(c, pos);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	fdtv->avc_data_length = ALIGN(3 + pos, 4);
861*4882a593Smuzhiyun 	ret = avc_write(fdtv);
862*4882a593Smuzhiyun 	if (ret < 0)
863*4882a593Smuzhiyun 		goto out;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	if (r->response != AVC_RESPONSE_ACCEPTED) {
866*4882a593Smuzhiyun 		dev_err(fdtv->device, "LNB control failed\n");
867*4882a593Smuzhiyun 		ret = -EINVAL;
868*4882a593Smuzhiyun 	}
869*4882a593Smuzhiyun out:
870*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	return ret;
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun 
avc_register_remote_control(struct firedtv * fdtv)875*4882a593Smuzhiyun int avc_register_remote_control(struct firedtv *fdtv)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
878*4882a593Smuzhiyun 	int ret;
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_NOTIFY;
883*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_UNIT | 7;
884*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
887*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
888*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
889*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
890*4882a593Smuzhiyun 	c->operand[4] = 0; /* padding */
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	fdtv->avc_data_length = 8;
893*4882a593Smuzhiyun 	ret = avc_write(fdtv);
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	/* FIXME: check response code? */
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	return ret;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun 
avc_remote_ctrl_work(struct work_struct * work)902*4882a593Smuzhiyun void avc_remote_ctrl_work(struct work_struct *work)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun 	struct firedtv *fdtv =
905*4882a593Smuzhiyun 			container_of(work, struct firedtv, remote_ctrl_work);
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	/* Should it be rescheduled in failure cases? */
908*4882a593Smuzhiyun 	avc_register_remote_control(fdtv);
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun #if 0 /* FIXME: unused */
912*4882a593Smuzhiyun int avc_tuner_host2ca(struct firedtv *fdtv)
913*4882a593Smuzhiyun {
914*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
915*4882a593Smuzhiyun 	int ret;
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_CONTROL;
920*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
921*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
924*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
925*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
926*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
927*4882a593Smuzhiyun 	c->operand[4] = 0; /* slot */
928*4882a593Smuzhiyun 	c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
929*4882a593Smuzhiyun 	clear_operands(c, 6, 8);
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	fdtv->avc_data_length = 12;
932*4882a593Smuzhiyun 	ret = avc_write(fdtv);
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	/* FIXME: check response code? */
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	return ret;
939*4882a593Smuzhiyun }
940*4882a593Smuzhiyun #endif
941*4882a593Smuzhiyun 
get_ca_object_pos(struct avc_response_frame * r)942*4882a593Smuzhiyun static int get_ca_object_pos(struct avc_response_frame *r)
943*4882a593Smuzhiyun {
944*4882a593Smuzhiyun 	int length = 1;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	/* Check length of length field */
947*4882a593Smuzhiyun 	if (r->operand[7] & 0x80)
948*4882a593Smuzhiyun 		length = (r->operand[7] & 0x7f) + 1;
949*4882a593Smuzhiyun 	return length + 7;
950*4882a593Smuzhiyun }
951*4882a593Smuzhiyun 
get_ca_object_length(struct avc_response_frame * r)952*4882a593Smuzhiyun static int get_ca_object_length(struct avc_response_frame *r)
953*4882a593Smuzhiyun {
954*4882a593Smuzhiyun #if 0 /* FIXME: unused */
955*4882a593Smuzhiyun 	int size = 0;
956*4882a593Smuzhiyun 	int i;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	if (r->operand[7] & 0x80)
959*4882a593Smuzhiyun 		for (i = 0; i < (r->operand[7] & 0x7f); i++) {
960*4882a593Smuzhiyun 			size <<= 8;
961*4882a593Smuzhiyun 			size += r->operand[8 + i];
962*4882a593Smuzhiyun 		}
963*4882a593Smuzhiyun #endif
964*4882a593Smuzhiyun 	return r->operand[7];
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun 
avc_ca_app_info(struct firedtv * fdtv,unsigned char * app_info,unsigned int * len)967*4882a593Smuzhiyun int avc_ca_app_info(struct firedtv *fdtv, unsigned char *app_info,
968*4882a593Smuzhiyun 		    unsigned int *len)
969*4882a593Smuzhiyun {
970*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
971*4882a593Smuzhiyun 	struct avc_response_frame *r = (void *)fdtv->avc_data;
972*4882a593Smuzhiyun 	int pos, ret;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_STATUS;
977*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
978*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
981*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
982*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
983*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
984*4882a593Smuzhiyun 	c->operand[4] = 0; /* slot */
985*4882a593Smuzhiyun 	c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
986*4882a593Smuzhiyun 	clear_operands(c, 6, LAST_OPERAND);
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	fdtv->avc_data_length = 12;
989*4882a593Smuzhiyun 	ret = avc_write(fdtv);
990*4882a593Smuzhiyun 	if (ret < 0)
991*4882a593Smuzhiyun 		goto out;
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	/* FIXME: check response code and validate response data */
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 	pos = get_ca_object_pos(r);
996*4882a593Smuzhiyun 	app_info[0] = (EN50221_TAG_APP_INFO >> 16) & 0xff;
997*4882a593Smuzhiyun 	app_info[1] = (EN50221_TAG_APP_INFO >>  8) & 0xff;
998*4882a593Smuzhiyun 	app_info[2] = (EN50221_TAG_APP_INFO >>  0) & 0xff;
999*4882a593Smuzhiyun 	app_info[3] = 6 + r->operand[pos + 4];
1000*4882a593Smuzhiyun 	app_info[4] = 0x01;
1001*4882a593Smuzhiyun 	memcpy(&app_info[5], &r->operand[pos], 5 + r->operand[pos + 4]);
1002*4882a593Smuzhiyun 	*len = app_info[3] + 4;
1003*4882a593Smuzhiyun out:
1004*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	return ret;
1007*4882a593Smuzhiyun }
1008*4882a593Smuzhiyun 
avc_ca_info(struct firedtv * fdtv,unsigned char * app_info,unsigned int * len)1009*4882a593Smuzhiyun int avc_ca_info(struct firedtv *fdtv, unsigned char *app_info,
1010*4882a593Smuzhiyun 		unsigned int *len)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
1013*4882a593Smuzhiyun 	struct avc_response_frame *r = (void *)fdtv->avc_data;
1014*4882a593Smuzhiyun 	int i, pos, ret;
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_STATUS;
1019*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1020*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1023*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1024*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1025*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1026*4882a593Smuzhiyun 	c->operand[4] = 0; /* slot */
1027*4882a593Smuzhiyun 	c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
1028*4882a593Smuzhiyun 	clear_operands(c, 6, LAST_OPERAND);
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 	fdtv->avc_data_length = 12;
1031*4882a593Smuzhiyun 	ret = avc_write(fdtv);
1032*4882a593Smuzhiyun 	if (ret < 0)
1033*4882a593Smuzhiyun 		goto out;
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 	/* FIXME: check response code and validate response data */
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	pos = get_ca_object_pos(r);
1038*4882a593Smuzhiyun 	app_info[0] = (EN50221_TAG_CA_INFO >> 16) & 0xff;
1039*4882a593Smuzhiyun 	app_info[1] = (EN50221_TAG_CA_INFO >>  8) & 0xff;
1040*4882a593Smuzhiyun 	app_info[2] = (EN50221_TAG_CA_INFO >>  0) & 0xff;
1041*4882a593Smuzhiyun 	if (num_fake_ca_system_ids == 0) {
1042*4882a593Smuzhiyun 		app_info[3] = 2;
1043*4882a593Smuzhiyun 		app_info[4] = r->operand[pos + 0];
1044*4882a593Smuzhiyun 		app_info[5] = r->operand[pos + 1];
1045*4882a593Smuzhiyun 	} else {
1046*4882a593Smuzhiyun 		app_info[3] = num_fake_ca_system_ids * 2;
1047*4882a593Smuzhiyun 		for (i = 0; i < num_fake_ca_system_ids; i++) {
1048*4882a593Smuzhiyun 			app_info[4 + i * 2] =
1049*4882a593Smuzhiyun 				(fake_ca_system_ids[i] >> 8) & 0xff;
1050*4882a593Smuzhiyun 			app_info[5 + i * 2] = fake_ca_system_ids[i] & 0xff;
1051*4882a593Smuzhiyun 		}
1052*4882a593Smuzhiyun 	}
1053*4882a593Smuzhiyun 	*len = app_info[3] + 4;
1054*4882a593Smuzhiyun out:
1055*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	return ret;
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun 
avc_ca_reset(struct firedtv * fdtv)1060*4882a593Smuzhiyun int avc_ca_reset(struct firedtv *fdtv)
1061*4882a593Smuzhiyun {
1062*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
1063*4882a593Smuzhiyun 	int ret;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_CONTROL;
1068*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1069*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1072*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1073*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1074*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1075*4882a593Smuzhiyun 	c->operand[4] = 0; /* slot */
1076*4882a593Smuzhiyun 	c->operand[5] = SFE_VENDOR_TAG_CA_RESET; /* ca tag */
1077*4882a593Smuzhiyun 	c->operand[6] = 0; /* more/last */
1078*4882a593Smuzhiyun 	c->operand[7] = 1; /* length */
1079*4882a593Smuzhiyun 	c->operand[8] = 0; /* force hardware reset */
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	fdtv->avc_data_length = 12;
1082*4882a593Smuzhiyun 	ret = avc_write(fdtv);
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	/* FIXME: check response code? */
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	return ret;
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun 
avc_ca_pmt(struct firedtv * fdtv,char * msg,int length)1091*4882a593Smuzhiyun int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
1092*4882a593Smuzhiyun {
1093*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
1094*4882a593Smuzhiyun 	struct avc_response_frame *r = (void *)fdtv->avc_data;
1095*4882a593Smuzhiyun 	int list_management;
1096*4882a593Smuzhiyun 	int program_info_length;
1097*4882a593Smuzhiyun 	int pmt_cmd_id;
1098*4882a593Smuzhiyun 	int read_pos;
1099*4882a593Smuzhiyun 	int write_pos;
1100*4882a593Smuzhiyun 	int es_info_length;
1101*4882a593Smuzhiyun 	int crc32_csum;
1102*4882a593Smuzhiyun 	int ret;
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	if (unlikely(avc_debug & AVC_DEBUG_APPLICATION_PMT))
1105*4882a593Smuzhiyun 		debug_pmt(msg, length);
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_CONTROL;
1110*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1111*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	if (msg[0] != EN50221_LIST_MANAGEMENT_ONLY) {
1114*4882a593Smuzhiyun 		dev_info(fdtv->device, "forcing list_management to ONLY\n");
1115*4882a593Smuzhiyun 		msg[0] = EN50221_LIST_MANAGEMENT_ONLY;
1116*4882a593Smuzhiyun 	}
1117*4882a593Smuzhiyun 	/* We take the cmd_id from the programme level only! */
1118*4882a593Smuzhiyun 	list_management = msg[0];
1119*4882a593Smuzhiyun 	program_info_length = ((msg[4] & 0x0f) << 8) + msg[5];
1120*4882a593Smuzhiyun 	if (program_info_length > 0)
1121*4882a593Smuzhiyun 		program_info_length--; /* Remove pmt_cmd_id */
1122*4882a593Smuzhiyun 	pmt_cmd_id = msg[6];
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1125*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1126*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1127*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1128*4882a593Smuzhiyun 	c->operand[4] = 0; /* slot */
1129*4882a593Smuzhiyun 	c->operand[5] = SFE_VENDOR_TAG_CA_PMT; /* ca tag */
1130*4882a593Smuzhiyun 	c->operand[6] = 0; /* more/last */
1131*4882a593Smuzhiyun 	/* Use three bytes for length field in case length > 127 */
1132*4882a593Smuzhiyun 	c->operand[10] = list_management;
1133*4882a593Smuzhiyun 	c->operand[11] = 0x01; /* pmt_cmd=OK_descramble */
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	/* TS program map table */
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	c->operand[12] = 0x02; /* Table id=2 */
1138*4882a593Smuzhiyun 	c->operand[13] = 0x80; /* Section syntax + length */
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	c->operand[15] = msg[1]; /* Program number */
1141*4882a593Smuzhiyun 	c->operand[16] = msg[2];
1142*4882a593Smuzhiyun 	c->operand[17] = msg[3]; /* Version number and current/next */
1143*4882a593Smuzhiyun 	c->operand[18] = 0x00; /* Section number=0 */
1144*4882a593Smuzhiyun 	c->operand[19] = 0x00; /* Last section number=0 */
1145*4882a593Smuzhiyun 	c->operand[20] = 0x1f; /* PCR_PID=1FFF */
1146*4882a593Smuzhiyun 	c->operand[21] = 0xff;
1147*4882a593Smuzhiyun 	c->operand[22] = (program_info_length >> 8); /* Program info length */
1148*4882a593Smuzhiyun 	c->operand[23] = (program_info_length & 0xff);
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	/* CA descriptors at programme level */
1151*4882a593Smuzhiyun 	read_pos = 6;
1152*4882a593Smuzhiyun 	write_pos = 24;
1153*4882a593Smuzhiyun 	if (program_info_length > 0) {
1154*4882a593Smuzhiyun 		pmt_cmd_id = msg[read_pos++];
1155*4882a593Smuzhiyun 		if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1156*4882a593Smuzhiyun 			dev_err(fdtv->device,
1157*4882a593Smuzhiyun 				"invalid pmt_cmd_id %d\n", pmt_cmd_id);
1158*4882a593Smuzhiyun 		if (program_info_length > sizeof(c->operand) - 4 - write_pos) {
1159*4882a593Smuzhiyun 			ret = -EINVAL;
1160*4882a593Smuzhiyun 			goto out;
1161*4882a593Smuzhiyun 		}
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 		memcpy(&c->operand[write_pos], &msg[read_pos],
1164*4882a593Smuzhiyun 		       program_info_length);
1165*4882a593Smuzhiyun 		read_pos += program_info_length;
1166*4882a593Smuzhiyun 		write_pos += program_info_length;
1167*4882a593Smuzhiyun 	}
1168*4882a593Smuzhiyun 	while (read_pos + 4 < length) {
1169*4882a593Smuzhiyun 		if (write_pos + 4 >= sizeof(c->operand) - 4) {
1170*4882a593Smuzhiyun 			ret = -EINVAL;
1171*4882a593Smuzhiyun 			goto out;
1172*4882a593Smuzhiyun 		}
1173*4882a593Smuzhiyun 		c->operand[write_pos++] = msg[read_pos++];
1174*4882a593Smuzhiyun 		c->operand[write_pos++] = msg[read_pos++];
1175*4882a593Smuzhiyun 		c->operand[write_pos++] = msg[read_pos++];
1176*4882a593Smuzhiyun 		es_info_length =
1177*4882a593Smuzhiyun 			((msg[read_pos] & 0x0f) << 8) + msg[read_pos + 1];
1178*4882a593Smuzhiyun 		read_pos += 2;
1179*4882a593Smuzhiyun 		if (es_info_length > 0)
1180*4882a593Smuzhiyun 			es_info_length--; /* Remove pmt_cmd_id */
1181*4882a593Smuzhiyun 		c->operand[write_pos++] = es_info_length >> 8;
1182*4882a593Smuzhiyun 		c->operand[write_pos++] = es_info_length & 0xff;
1183*4882a593Smuzhiyun 		if (es_info_length > 0) {
1184*4882a593Smuzhiyun 			if (read_pos >= length) {
1185*4882a593Smuzhiyun 				ret = -EINVAL;
1186*4882a593Smuzhiyun 				goto out;
1187*4882a593Smuzhiyun 			}
1188*4882a593Smuzhiyun 			pmt_cmd_id = msg[read_pos++];
1189*4882a593Smuzhiyun 			if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1190*4882a593Smuzhiyun 				dev_err(fdtv->device, "invalid pmt_cmd_id %d at stream level\n",
1191*4882a593Smuzhiyun 					pmt_cmd_id);
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 			if (es_info_length > sizeof(c->operand) - 4 - write_pos ||
1194*4882a593Smuzhiyun 			    es_info_length > length - read_pos) {
1195*4882a593Smuzhiyun 				ret = -EINVAL;
1196*4882a593Smuzhiyun 				goto out;
1197*4882a593Smuzhiyun 			}
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 			memcpy(&c->operand[write_pos], &msg[read_pos],
1200*4882a593Smuzhiyun 			       es_info_length);
1201*4882a593Smuzhiyun 			read_pos += es_info_length;
1202*4882a593Smuzhiyun 			write_pos += es_info_length;
1203*4882a593Smuzhiyun 		}
1204*4882a593Smuzhiyun 	}
1205*4882a593Smuzhiyun 	write_pos += 4; /* CRC */
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	c->operand[7] = 0x82;
1208*4882a593Smuzhiyun 	c->operand[8] = (write_pos - 10) >> 8;
1209*4882a593Smuzhiyun 	c->operand[9] = (write_pos - 10) & 0xff;
1210*4882a593Smuzhiyun 	c->operand[14] = write_pos - 15;
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	crc32_csum = crc32_be(0, &c->operand[10], c->operand[12] - 1);
1213*4882a593Smuzhiyun 	c->operand[write_pos - 4] = (crc32_csum >> 24) & 0xff;
1214*4882a593Smuzhiyun 	c->operand[write_pos - 3] = (crc32_csum >> 16) & 0xff;
1215*4882a593Smuzhiyun 	c->operand[write_pos - 2] = (crc32_csum >>  8) & 0xff;
1216*4882a593Smuzhiyun 	c->operand[write_pos - 1] = (crc32_csum >>  0) & 0xff;
1217*4882a593Smuzhiyun 	pad_operands(c, write_pos);
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	fdtv->avc_data_length = ALIGN(3 + write_pos, 4);
1220*4882a593Smuzhiyun 	ret = avc_write(fdtv);
1221*4882a593Smuzhiyun 	if (ret < 0)
1222*4882a593Smuzhiyun 		goto out;
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 	if (r->response != AVC_RESPONSE_ACCEPTED) {
1225*4882a593Smuzhiyun 		dev_err(fdtv->device,
1226*4882a593Smuzhiyun 			"CA PMT failed with response 0x%x\n", r->response);
1227*4882a593Smuzhiyun 		ret = -EACCES;
1228*4882a593Smuzhiyun 	}
1229*4882a593Smuzhiyun out:
1230*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	return ret;
1233*4882a593Smuzhiyun }
1234*4882a593Smuzhiyun 
avc_ca_get_time_date(struct firedtv * fdtv,int * interval)1235*4882a593Smuzhiyun int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
1236*4882a593Smuzhiyun {
1237*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
1238*4882a593Smuzhiyun 	struct avc_response_frame *r = (void *)fdtv->avc_data;
1239*4882a593Smuzhiyun 	int ret;
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_STATUS;
1244*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1245*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1248*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1249*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1250*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1251*4882a593Smuzhiyun 	c->operand[4] = 0; /* slot */
1252*4882a593Smuzhiyun 	c->operand[5] = SFE_VENDOR_TAG_CA_DATE_TIME; /* ca tag */
1253*4882a593Smuzhiyun 	clear_operands(c, 6, LAST_OPERAND);
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 	fdtv->avc_data_length = 12;
1256*4882a593Smuzhiyun 	ret = avc_write(fdtv);
1257*4882a593Smuzhiyun 	if (ret < 0)
1258*4882a593Smuzhiyun 		goto out;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	/* FIXME: check response code and validate response data */
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	*interval = r->operand[get_ca_object_pos(r)];
1263*4882a593Smuzhiyun out:
1264*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	return ret;
1267*4882a593Smuzhiyun }
1268*4882a593Smuzhiyun 
avc_ca_enter_menu(struct firedtv * fdtv)1269*4882a593Smuzhiyun int avc_ca_enter_menu(struct firedtv *fdtv)
1270*4882a593Smuzhiyun {
1271*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
1272*4882a593Smuzhiyun 	int ret;
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_STATUS;
1277*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1278*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1281*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1282*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1283*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1284*4882a593Smuzhiyun 	c->operand[4] = 0; /* slot */
1285*4882a593Smuzhiyun 	c->operand[5] = SFE_VENDOR_TAG_CA_ENTER_MENU;
1286*4882a593Smuzhiyun 	clear_operands(c, 6, 8);
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	fdtv->avc_data_length = 12;
1289*4882a593Smuzhiyun 	ret = avc_write(fdtv);
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 	/* FIXME: check response code? */
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	return ret;
1296*4882a593Smuzhiyun }
1297*4882a593Smuzhiyun 
avc_ca_get_mmi(struct firedtv * fdtv,char * mmi_object,unsigned int * len)1298*4882a593Smuzhiyun int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
1299*4882a593Smuzhiyun {
1300*4882a593Smuzhiyun 	struct avc_command_frame *c = (void *)fdtv->avc_data;
1301*4882a593Smuzhiyun 	struct avc_response_frame *r = (void *)fdtv->avc_data;
1302*4882a593Smuzhiyun 	int ret;
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	mutex_lock(&fdtv->avc_mutex);
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	c->ctype   = AVC_CTYPE_STATUS;
1307*4882a593Smuzhiyun 	c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1308*4882a593Smuzhiyun 	c->opcode  = AVC_OPCODE_VENDOR;
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1311*4882a593Smuzhiyun 	c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1312*4882a593Smuzhiyun 	c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1313*4882a593Smuzhiyun 	c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1314*4882a593Smuzhiyun 	c->operand[4] = 0; /* slot */
1315*4882a593Smuzhiyun 	c->operand[5] = SFE_VENDOR_TAG_CA_MMI;
1316*4882a593Smuzhiyun 	clear_operands(c, 6, LAST_OPERAND);
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun 	fdtv->avc_data_length = 12;
1319*4882a593Smuzhiyun 	ret = avc_write(fdtv);
1320*4882a593Smuzhiyun 	if (ret < 0)
1321*4882a593Smuzhiyun 		goto out;
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	/* FIXME: check response code and validate response data */
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	*len = get_ca_object_length(r);
1326*4882a593Smuzhiyun 	memcpy(mmi_object, &r->operand[get_ca_object_pos(r)], *len);
1327*4882a593Smuzhiyun out:
1328*4882a593Smuzhiyun 	mutex_unlock(&fdtv->avc_mutex);
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun 	return ret;
1331*4882a593Smuzhiyun }
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun #define CMP_OUTPUT_PLUG_CONTROL_REG_0	0xfffff0000904ULL
1334*4882a593Smuzhiyun 
cmp_read(struct firedtv * fdtv,u64 addr,__be32 * data)1335*4882a593Smuzhiyun static int cmp_read(struct firedtv *fdtv, u64 addr, __be32 *data)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun 	int ret;
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	ret = fdtv_read(fdtv, addr, data);
1340*4882a593Smuzhiyun 	if (ret < 0)
1341*4882a593Smuzhiyun 		dev_err(fdtv->device, "CMP: read I/O error\n");
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	return ret;
1344*4882a593Smuzhiyun }
1345*4882a593Smuzhiyun 
cmp_lock(struct firedtv * fdtv,u64 addr,__be32 data[])1346*4882a593Smuzhiyun static int cmp_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
1347*4882a593Smuzhiyun {
1348*4882a593Smuzhiyun 	int ret;
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	ret = fdtv_lock(fdtv, addr, data);
1351*4882a593Smuzhiyun 	if (ret < 0)
1352*4882a593Smuzhiyun 		dev_err(fdtv->device, "CMP: lock I/O error\n");
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 	return ret;
1355*4882a593Smuzhiyun }
1356*4882a593Smuzhiyun 
get_opcr(__be32 opcr,u32 mask,u32 shift)1357*4882a593Smuzhiyun static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
1358*4882a593Smuzhiyun {
1359*4882a593Smuzhiyun 	return (be32_to_cpu(opcr) >> shift) & mask;
1360*4882a593Smuzhiyun }
1361*4882a593Smuzhiyun 
set_opcr(__be32 * opcr,u32 value,u32 mask,u32 shift)1362*4882a593Smuzhiyun static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
1363*4882a593Smuzhiyun {
1364*4882a593Smuzhiyun 	*opcr &= ~cpu_to_be32(mask << shift);
1365*4882a593Smuzhiyun 	*opcr |= cpu_to_be32((value & mask) << shift);
1366*4882a593Smuzhiyun }
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun #define get_opcr_online(v)		get_opcr((v), 0x1, 31)
1369*4882a593Smuzhiyun #define get_opcr_p2p_connections(v)	get_opcr((v), 0x3f, 24)
1370*4882a593Smuzhiyun #define get_opcr_channel(v)		get_opcr((v), 0x3f, 16)
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun #define set_opcr_p2p_connections(p, v)	set_opcr((p), (v), 0x3f, 24)
1373*4882a593Smuzhiyun #define set_opcr_channel(p, v)		set_opcr((p), (v), 0x3f, 16)
1374*4882a593Smuzhiyun #define set_opcr_data_rate(p, v)	set_opcr((p), (v), 0x3, 14)
1375*4882a593Smuzhiyun #define set_opcr_overhead_id(p, v)	set_opcr((p), (v), 0xf, 10)
1376*4882a593Smuzhiyun 
cmp_establish_pp_connection(struct firedtv * fdtv,int plug,int channel)1377*4882a593Smuzhiyun int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
1378*4882a593Smuzhiyun {
1379*4882a593Smuzhiyun 	__be32 old_opcr, opcr[2];
1380*4882a593Smuzhiyun 	u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1381*4882a593Smuzhiyun 	int attempts = 0;
1382*4882a593Smuzhiyun 	int ret;
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun 	ret = cmp_read(fdtv, opcr_address, opcr);
1385*4882a593Smuzhiyun 	if (ret < 0)
1386*4882a593Smuzhiyun 		return ret;
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun repeat:
1389*4882a593Smuzhiyun 	if (!get_opcr_online(*opcr)) {
1390*4882a593Smuzhiyun 		dev_err(fdtv->device, "CMP: output offline\n");
1391*4882a593Smuzhiyun 		return -EBUSY;
1392*4882a593Smuzhiyun 	}
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	old_opcr = *opcr;
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	if (get_opcr_p2p_connections(*opcr)) {
1397*4882a593Smuzhiyun 		if (get_opcr_channel(*opcr) != channel) {
1398*4882a593Smuzhiyun 			dev_err(fdtv->device, "CMP: cannot change channel\n");
1399*4882a593Smuzhiyun 			return -EBUSY;
1400*4882a593Smuzhiyun 		}
1401*4882a593Smuzhiyun 		dev_info(fdtv->device, "CMP: overlaying connection\n");
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 		/* We don't allocate isochronous resources. */
1404*4882a593Smuzhiyun 	} else {
1405*4882a593Smuzhiyun 		set_opcr_channel(opcr, channel);
1406*4882a593Smuzhiyun 		set_opcr_data_rate(opcr, 2); /* S400 */
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 		/* FIXME: this is for the worst case - optimize */
1409*4882a593Smuzhiyun 		set_opcr_overhead_id(opcr, 0);
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 		/* FIXME: allocate isochronous channel and bandwidth at IRM */
1412*4882a593Smuzhiyun 	}
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun 	set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) + 1);
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	opcr[1] = *opcr;
1417*4882a593Smuzhiyun 	opcr[0] = old_opcr;
1418*4882a593Smuzhiyun 
1419*4882a593Smuzhiyun 	ret = cmp_lock(fdtv, opcr_address, opcr);
1420*4882a593Smuzhiyun 	if (ret < 0)
1421*4882a593Smuzhiyun 		return ret;
1422*4882a593Smuzhiyun 
1423*4882a593Smuzhiyun 	if (old_opcr != *opcr) {
1424*4882a593Smuzhiyun 		/*
1425*4882a593Smuzhiyun 		 * FIXME: if old_opcr.P2P_Connections > 0,
1426*4882a593Smuzhiyun 		 * deallocate isochronous channel and bandwidth at IRM
1427*4882a593Smuzhiyun 		 */
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun 		if (++attempts < 6) /* arbitrary limit */
1430*4882a593Smuzhiyun 			goto repeat;
1431*4882a593Smuzhiyun 		return -EBUSY;
1432*4882a593Smuzhiyun 	}
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	return 0;
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun 
cmp_break_pp_connection(struct firedtv * fdtv,int plug,int channel)1437*4882a593Smuzhiyun void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
1438*4882a593Smuzhiyun {
1439*4882a593Smuzhiyun 	__be32 old_opcr, opcr[2];
1440*4882a593Smuzhiyun 	u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1441*4882a593Smuzhiyun 	int attempts = 0;
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 	if (cmp_read(fdtv, opcr_address, opcr) < 0)
1444*4882a593Smuzhiyun 		return;
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun repeat:
1447*4882a593Smuzhiyun 	if (!get_opcr_online(*opcr) || !get_opcr_p2p_connections(*opcr) ||
1448*4882a593Smuzhiyun 	    get_opcr_channel(*opcr) != channel) {
1449*4882a593Smuzhiyun 		dev_err(fdtv->device, "CMP: no connection to break\n");
1450*4882a593Smuzhiyun 		return;
1451*4882a593Smuzhiyun 	}
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 	old_opcr = *opcr;
1454*4882a593Smuzhiyun 	set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) - 1);
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	opcr[1] = *opcr;
1457*4882a593Smuzhiyun 	opcr[0] = old_opcr;
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 	if (cmp_lock(fdtv, opcr_address, opcr) < 0)
1460*4882a593Smuzhiyun 		return;
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun 	if (old_opcr != *opcr) {
1463*4882a593Smuzhiyun 		/*
1464*4882a593Smuzhiyun 		 * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
1465*4882a593Smuzhiyun 		 * owner, deallocate isochronous channel and bandwidth at IRM
1466*4882a593Smuzhiyun 		 * if (...)
1467*4882a593Smuzhiyun 		 *	fdtv->backend->dealloc_resources(fdtv, channel, bw);
1468*4882a593Smuzhiyun 		 */
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun 		if (++attempts < 6) /* arbitrary limit */
1471*4882a593Smuzhiyun 			goto repeat;
1472*4882a593Smuzhiyun 	}
1473*4882a593Smuzhiyun }
1474