blob: 280b5ffea5922f032c5b76e86c688dd12486f17e [file] [log] [blame]
Stefan Richter15490792009-02-23 14:21:10 +01001/*
2 * FireDTV driver (formerly known as FireSAT)
3 *
4 * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
5 * Copyright (C) 2008 Ben Backx <ben@bbackx.com>
6 * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 */
13
14#include <linux/bug.h>
15#include <linux/crc32.h>
16#include <linux/delay.h>
17#include <linux/device.h>
18#include <linux/jiffies.h>
19#include <linux/kernel.h>
20#include <linux/moduleparam.h>
21#include <linux/mutex.h>
22#include <linux/string.h>
23#include <linux/stringify.h>
24#include <linux/wait.h>
25#include <linux/workqueue.h>
26
Tommy Jonssond2fd44a2010-09-12 16:03:45 -030027#include <dvb_frontend.h>
28
Stefan Richter15490792009-02-23 14:21:10 +010029#include "firedtv.h"
30
31#define FCP_COMMAND_REGISTER 0xfffff0000b00ULL
32
33#define AVC_CTYPE_CONTROL 0x0
34#define AVC_CTYPE_STATUS 0x1
35#define AVC_CTYPE_NOTIFY 0x3
36
37#define AVC_RESPONSE_ACCEPTED 0x9
38#define AVC_RESPONSE_STABLE 0xc
39#define AVC_RESPONSE_CHANGED 0xd
40#define AVC_RESPONSE_INTERIM 0xf
41
42#define AVC_SUBUNIT_TYPE_TUNER (0x05 << 3)
43#define AVC_SUBUNIT_TYPE_UNIT (0x1f << 3)
44
45#define AVC_OPCODE_VENDOR 0x00
46#define AVC_OPCODE_READ_DESCRIPTOR 0x09
47#define AVC_OPCODE_DSIT 0xc8
48#define AVC_OPCODE_DSD 0xcb
49
50#define DESCRIPTOR_TUNER_STATUS 0x80
51#define DESCRIPTOR_SUBUNIT_IDENTIFIER 0x00
52
53#define SFE_VENDOR_DE_COMPANYID_0 0x00 /* OUI of Digital Everywhere */
54#define SFE_VENDOR_DE_COMPANYID_1 0x12
55#define SFE_VENDOR_DE_COMPANYID_2 0x87
56
57#define SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL 0x0a
58#define SFE_VENDOR_OPCODE_LNB_CONTROL 0x52
59#define SFE_VENDOR_OPCODE_TUNE_QPSK 0x58 /* for DVB-S */
60
61#define SFE_VENDOR_OPCODE_GET_FIRMWARE_VERSION 0x00
62#define SFE_VENDOR_OPCODE_HOST2CA 0x56
63#define SFE_VENDOR_OPCODE_CA2HOST 0x57
64#define SFE_VENDOR_OPCODE_CISTATUS 0x59
65#define SFE_VENDOR_OPCODE_TUNE_QPSK2 0x60 /* for DVB-S2 */
66
67#define SFE_VENDOR_TAG_CA_RESET 0x00
68#define SFE_VENDOR_TAG_CA_APPLICATION_INFO 0x01
69#define SFE_VENDOR_TAG_CA_PMT 0x02
70#define SFE_VENDOR_TAG_CA_DATE_TIME 0x04
71#define SFE_VENDOR_TAG_CA_MMI 0x05
72#define SFE_VENDOR_TAG_CA_ENTER_MENU 0x07
73
74#define EN50221_LIST_MANAGEMENT_ONLY 0x03
75#define EN50221_TAG_APP_INFO 0x9f8021
76#define EN50221_TAG_CA_INFO 0x9f8031
77
78struct avc_command_frame {
Stefan Richter15490792009-02-23 14:21:10 +010079 u8 ctype;
80 u8 subunit;
81 u8 opcode;
82 u8 operand[509];
83};
84
85struct avc_response_frame {
Stefan Richter15490792009-02-23 14:21:10 +010086 u8 response;
87 u8 subunit;
88 u8 opcode;
89 u8 operand[509];
90};
91
Stefan Richter1e4348c2009-11-18 16:03:31 -030092#define LAST_OPERAND (509 - 1)
93
94static inline void clear_operands(struct avc_command_frame *c, int from, int to)
95{
96 memset(&c->operand[from], 0, to - from + 1);
97}
98
99static void pad_operands(struct avc_command_frame *c, int from)
100{
101 int to = ALIGN(from, 4);
102
103 if (from <= to && to <= LAST_OPERAND)
104 clear_operands(c, from, to);
105}
106
Henrik Kurelid15344772009-08-01 08:04:06 -0300107#define AVC_DEBUG_READ_DESCRIPTOR 0x0001
108#define AVC_DEBUG_DSIT 0x0002
109#define AVC_DEBUG_DSD 0x0004
110#define AVC_DEBUG_REGISTER_REMOTE_CONTROL 0x0008
111#define AVC_DEBUG_LNB_CONTROL 0x0010
112#define AVC_DEBUG_TUNE_QPSK 0x0020
113#define AVC_DEBUG_TUNE_QPSK2 0x0040
114#define AVC_DEBUG_HOST2CA 0x0080
115#define AVC_DEBUG_CA2HOST 0x0100
116#define AVC_DEBUG_APPLICATION_PMT 0x4000
117#define AVC_DEBUG_FCP_PAYLOADS 0x8000
Stefan Richter15490792009-02-23 14:21:10 +0100118
119static int avc_debug;
120module_param_named(debug, avc_debug, int, 0644);
Stefan Richter96e242a52009-08-01 08:05:16 -0300121MODULE_PARM_DESC(debug, "Verbose logging (none = 0"
122 ", FCP subactions"
123 ": READ DESCRIPTOR = " __stringify(AVC_DEBUG_READ_DESCRIPTOR)
124 ", DSIT = " __stringify(AVC_DEBUG_DSIT)
125 ", REGISTER_REMOTE_CONTROL = " __stringify(AVC_DEBUG_REGISTER_REMOTE_CONTROL)
126 ", LNB CONTROL = " __stringify(AVC_DEBUG_LNB_CONTROL)
127 ", TUNE QPSK = " __stringify(AVC_DEBUG_TUNE_QPSK)
128 ", TUNE QPSK2 = " __stringify(AVC_DEBUG_TUNE_QPSK2)
129 ", HOST2CA = " __stringify(AVC_DEBUG_HOST2CA)
130 ", CA2HOST = " __stringify(AVC_DEBUG_CA2HOST)
131 "; Application sent PMT = " __stringify(AVC_DEBUG_APPLICATION_PMT)
132 ", FCP payloads = " __stringify(AVC_DEBUG_FCP_PAYLOADS)
133 ", or a combination, or all = -1)");
Stefan Richter15490792009-02-23 14:21:10 +0100134
Henrik Kurelid52065c52010-03-01 08:56:42 -0300135/*
136 * This is a workaround since there is no vendor specific command to retrieve
137 * ca_info using AVC. If this parameter is not used, ca_system_id will be
138 * filled with application_manufacturer from ca_app_info.
139 * Digital Everywhere have said that adding ca_info is on their TODO list.
140 */
141static unsigned int num_fake_ca_system_ids;
142static int fake_ca_system_ids[4] = { -1, -1, -1, -1 };
143module_param_array(fake_ca_system_ids, int, &num_fake_ca_system_ids, 0644);
144MODULE_PARM_DESC(fake_ca_system_ids, "If your CAM application manufacturer "
145 "does not have the same ca_system_id as your CAS, you can "
146 "override what ca_system_ids are presented to the "
147 "application by setting this field to an array of ids.");
148
Stefan Richter15490792009-02-23 14:21:10 +0100149static const char *debug_fcp_ctype(unsigned int ctype)
150{
151 static const char *ctypes[] = {
152 [0x0] = "CONTROL", [0x1] = "STATUS",
153 [0x2] = "SPECIFIC INQUIRY", [0x3] = "NOTIFY",
154 [0x4] = "GENERAL INQUIRY", [0x8] = "NOT IMPLEMENTED",
155 [0x9] = "ACCEPTED", [0xa] = "REJECTED",
156 [0xb] = "IN TRANSITION", [0xc] = "IMPLEMENTED/STABLE",
157 [0xd] = "CHANGED", [0xf] = "INTERIM",
158 };
159 const char *ret = ctype < ARRAY_SIZE(ctypes) ? ctypes[ctype] : NULL;
160
161 return ret ? ret : "?";
162}
163
164static const char *debug_fcp_opcode(unsigned int opcode,
Stefan Richter40cf65d2009-03-05 19:13:43 +0100165 const u8 *data, int length)
Stefan Richter15490792009-02-23 14:21:10 +0100166{
167 switch (opcode) {
Stefan Richter96e242a52009-08-01 08:05:16 -0300168 case AVC_OPCODE_VENDOR:
169 break;
170 case AVC_OPCODE_READ_DESCRIPTOR:
171 return avc_debug & AVC_DEBUG_READ_DESCRIPTOR ?
172 "ReadDescriptor" : NULL;
173 case AVC_OPCODE_DSIT:
174 return avc_debug & AVC_DEBUG_DSIT ?
175 "DirectSelectInfo.Type" : NULL;
176 case AVC_OPCODE_DSD:
177 return avc_debug & AVC_DEBUG_DSD ? "DirectSelectData" : NULL;
178 default:
179 return "Unknown";
Stefan Richter15490792009-02-23 14:21:10 +0100180 }
181
182 if (length < 7 ||
183 data[3] != SFE_VENDOR_DE_COMPANYID_0 ||
184 data[4] != SFE_VENDOR_DE_COMPANYID_1 ||
185 data[5] != SFE_VENDOR_DE_COMPANYID_2)
Stefan Richter96e242a52009-08-01 08:05:16 -0300186 return "Vendor/Unknown";
Stefan Richter15490792009-02-23 14:21:10 +0100187
188 switch (data[6]) {
Stefan Richter96e242a52009-08-01 08:05:16 -0300189 case SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL:
190 return avc_debug & AVC_DEBUG_REGISTER_REMOTE_CONTROL ?
191 "RegisterRC" : NULL;
192 case SFE_VENDOR_OPCODE_LNB_CONTROL:
193 return avc_debug & AVC_DEBUG_LNB_CONTROL ? "LNBControl" : NULL;
194 case SFE_VENDOR_OPCODE_TUNE_QPSK:
195 return avc_debug & AVC_DEBUG_TUNE_QPSK ? "TuneQPSK" : NULL;
196 case SFE_VENDOR_OPCODE_TUNE_QPSK2:
197 return avc_debug & AVC_DEBUG_TUNE_QPSK2 ? "TuneQPSK2" : NULL;
198 case SFE_VENDOR_OPCODE_HOST2CA:
199 return avc_debug & AVC_DEBUG_HOST2CA ? "Host2CA" : NULL;
200 case SFE_VENDOR_OPCODE_CA2HOST:
201 return avc_debug & AVC_DEBUG_CA2HOST ? "CA2Host" : NULL;
Stefan Richter15490792009-02-23 14:21:10 +0100202 }
Stefan Richter96e242a52009-08-01 08:05:16 -0300203 return "Vendor/Unknown";
Henrik Kurelid15344772009-08-01 08:04:06 -0300204}
205
Stefan Richter40cf65d2009-03-05 19:13:43 +0100206static void debug_fcp(const u8 *data, int length)
Stefan Richter15490792009-02-23 14:21:10 +0100207{
Stefan Richter96e242a52009-08-01 08:05:16 -0300208 unsigned int subunit_type, subunit_id, opcode;
209 const char *op, *prefix;
Stefan Richter15490792009-02-23 14:21:10 +0100210
Stefan Richter96e242a52009-08-01 08:05:16 -0300211 prefix = data[0] > 7 ? "FCP <- " : "FCP -> ";
Henrik Kurelid15344772009-08-01 08:04:06 -0300212 subunit_type = data[1] >> 3;
Stefan Richter96e242a52009-08-01 08:05:16 -0300213 subunit_id = data[1] & 7;
214 opcode = subunit_type == 0x1e || subunit_id == 5 ? ~0 : data[2];
215 op = debug_fcp_opcode(opcode, data, length);
216
217 if (op) {
Stefan Richter14edcd52009-04-01 17:25:00 -0300218 printk(KERN_INFO "%ssu=%x.%x l=%d: %-8s - %s\n",
Stefan Richter15490792009-02-23 14:21:10 +0100219 prefix, subunit_type, subunit_id, length,
Stefan Richter96e242a52009-08-01 08:05:16 -0300220 debug_fcp_ctype(data[0]), op);
Henrik Kurelid15344772009-08-01 08:04:06 -0300221 if (avc_debug & AVC_DEBUG_FCP_PAYLOADS)
222 print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_NONE,
223 16, 1, data, length, false);
Stefan Richter15490792009-02-23 14:21:10 +0100224 }
Henrik Kurelid15344772009-08-01 08:04:06 -0300225}
Stefan Richter15490792009-02-23 14:21:10 +0100226
Henrik Kurelid15344772009-08-01 08:04:06 -0300227static void debug_pmt(char *msg, int length)
228{
229 printk(KERN_INFO "APP PMT -> l=%d\n", length);
230 print_hex_dump(KERN_INFO, "APP PMT -> ", DUMP_PREFIX_NONE,
231 16, 1, msg, length, false);
Stefan Richter15490792009-02-23 14:21:10 +0100232}
233
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300234static int avc_write(struct firedtv *fdtv)
Stefan Richter15490792009-02-23 14:21:10 +0100235{
236 int err, retry;
237
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300238 fdtv->avc_reply_received = false;
Stefan Richter15490792009-02-23 14:21:10 +0100239
240 for (retry = 0; retry < 6; retry++) {
241 if (unlikely(avc_debug))
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300242 debug_fcp(fdtv->avc_data, fdtv->avc_data_length);
Stefan Richter15490792009-02-23 14:21:10 +0100243
Stefan Richter92374e82011-02-06 11:41:44 -0300244 err = fdtv_write(fdtv, FCP_COMMAND_REGISTER,
245 fdtv->avc_data, fdtv->avc_data_length);
Stefan Richter15490792009-02-23 14:21:10 +0100246 if (err) {
Stefan Richter15490792009-02-23 14:21:10 +0100247 dev_err(fdtv->device, "FCP command write failed\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300248
Stefan Richter15490792009-02-23 14:21:10 +0100249 return err;
250 }
251
Stefan Richter15490792009-02-23 14:21:10 +0100252 /*
253 * AV/C specs say that answers should be sent within 150 ms.
254 * Time out after 200 ms.
255 */
256 if (wait_event_timeout(fdtv->avc_wait,
257 fdtv->avc_reply_received,
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300258 msecs_to_jiffies(200)) != 0)
Stefan Richter15490792009-02-23 14:21:10 +0100259 return 0;
Stefan Richter15490792009-02-23 14:21:10 +0100260 }
261 dev_err(fdtv->device, "FCP response timed out\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300262
Stefan Richter15490792009-02-23 14:21:10 +0100263 return -ETIMEDOUT;
264}
265
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300266static bool is_register_rc(struct avc_response_frame *r)
Stefan Richter15490792009-02-23 14:21:10 +0100267{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300268 return r->opcode == AVC_OPCODE_VENDOR &&
269 r->operand[0] == SFE_VENDOR_DE_COMPANYID_0 &&
270 r->operand[1] == SFE_VENDOR_DE_COMPANYID_1 &&
271 r->operand[2] == SFE_VENDOR_DE_COMPANYID_2 &&
272 r->operand[3] == SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
Stefan Richter15490792009-02-23 14:21:10 +0100273}
274
275int avc_recv(struct firedtv *fdtv, void *data, size_t length)
276{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300277 struct avc_response_frame *r = data;
Stefan Richter15490792009-02-23 14:21:10 +0100278
279 if (unlikely(avc_debug))
280 debug_fcp(data, length);
281
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300282 if (length >= 8 && is_register_rc(r)) {
283 switch (r->response) {
284 case AVC_RESPONSE_CHANGED:
285 fdtv_handle_rc(fdtv, r->operand[4] << 8 | r->operand[5]);
Stefan Richter15490792009-02-23 14:21:10 +0100286 schedule_work(&fdtv->remote_ctrl_work);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300287 break;
288 case AVC_RESPONSE_INTERIM:
289 if (is_register_rc((void *)fdtv->avc_data))
290 goto wake;
291 break;
292 default:
Stefan Richter15490792009-02-23 14:21:10 +0100293 dev_info(fdtv->device,
294 "remote control result = %d\n", r->response);
295 }
296 return 0;
297 }
298
299 if (fdtv->avc_reply_received) {
300 dev_err(fdtv->device, "out-of-order AVC response, ignored\n");
301 return -EIO;
302 }
303
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300304 memcpy(fdtv->avc_data, data, length);
305 fdtv->avc_data_length = length;
306wake:
Stefan Richter15490792009-02-23 14:21:10 +0100307 fdtv->avc_reply_received = true;
308 wake_up(&fdtv->avc_wait);
309
310 return 0;
311}
312
Henrik Kurelid166987c2009-08-01 08:02:38 -0300313static int add_pid_filter(struct firedtv *fdtv, u8 *operand)
314{
315 int i, n, pos = 1;
316
317 for (i = 0, n = 0; i < 16; i++) {
318 if (test_bit(i, &fdtv->channel_active)) {
319 operand[pos++] = 0x13; /* flowfunction relay */
320 operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
321 operand[pos++] = (fdtv->channel_pid[i] >> 8) & 0x1f;
322 operand[pos++] = fdtv->channel_pid[i] & 0xff;
323 operand[pos++] = 0x00; /* tableID */
324 operand[pos++] = 0x00; /* filter_length */
325 n++;
326 }
327 }
328 operand[0] = n;
329
330 return pos;
331}
332
Stefan Richter15490792009-02-23 14:21:10 +0100333/*
334 * tuning command for setting the relative LNB frequency
335 * (not supported by the AVC standard)
336 */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300337static int avc_tuner_tuneqpsk(struct firedtv *fdtv,
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300338 struct dtv_frontend_properties *p)
Stefan Richter15490792009-02-23 14:21:10 +0100339{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300340 struct avc_command_frame *c = (void *)fdtv->avc_data;
341
Stefan Richter15490792009-02-23 14:21:10 +0100342 c->opcode = AVC_OPCODE_VENDOR;
343
344 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
345 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
346 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
Beat Michel Liechti32a0f482009-03-26 22:36:52 +0100347 if (fdtv->type == FIREDTV_DVB_S2)
348 c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK2;
349 else
350 c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK;
Stefan Richter15490792009-02-23 14:21:10 +0100351
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300352 c->operand[4] = (p->frequency >> 24) & 0xff;
353 c->operand[5] = (p->frequency >> 16) & 0xff;
354 c->operand[6] = (p->frequency >> 8) & 0xff;
355 c->operand[7] = p->frequency & 0xff;
Stefan Richter15490792009-02-23 14:21:10 +0100356
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300357 c->operand[8] = ((p->symbol_rate / 1000) >> 8) & 0xff;
358 c->operand[9] = (p->symbol_rate / 1000) & 0xff;
Stefan Richter15490792009-02-23 14:21:10 +0100359
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300360 switch (p->fec_inner) {
Stefan Richter15490792009-02-23 14:21:10 +0100361 case FEC_1_2: c->operand[10] = 0x1; break;
362 case FEC_2_3: c->operand[10] = 0x2; break;
363 case FEC_3_4: c->operand[10] = 0x3; break;
364 case FEC_5_6: c->operand[10] = 0x4; break;
365 case FEC_7_8: c->operand[10] = 0x5; break;
366 case FEC_4_5:
367 case FEC_8_9:
368 case FEC_AUTO:
369 default: c->operand[10] = 0x0;
370 }
371
372 if (fdtv->voltage == 0xff)
373 c->operand[11] = 0xff;
374 else if (fdtv->voltage == SEC_VOLTAGE_18) /* polarisation */
375 c->operand[11] = 0;
376 else
377 c->operand[11] = 1;
378
379 if (fdtv->tone == 0xff)
380 c->operand[12] = 0xff;
381 else if (fdtv->tone == SEC_TONE_ON) /* band */
382 c->operand[12] = 1;
383 else
384 c->operand[12] = 0;
385
386 if (fdtv->type == FIREDTV_DVB_S2) {
Tommy Jonssond2fd44a2010-09-12 16:03:45 -0300387 if (fdtv->fe.dtv_property_cache.delivery_system == SYS_DVBS2) {
388 switch (fdtv->fe.dtv_property_cache.modulation) {
389 case QAM_16: c->operand[13] = 0x1; break;
390 case QPSK: c->operand[13] = 0x2; break;
391 case PSK_8: c->operand[13] = 0x3; break;
392 default: c->operand[13] = 0x2; break;
393 }
394 switch (fdtv->fe.dtv_property_cache.rolloff) {
Tommy Jonssond2fd44a2010-09-12 16:03:45 -0300395 case ROLLOFF_35: c->operand[14] = 0x2; break;
396 case ROLLOFF_20: c->operand[14] = 0x0; break;
397 case ROLLOFF_25: c->operand[14] = 0x1; break;
Mauro Carvalho Chehab4a5006f2011-11-24 15:04:18 -0200398 case ROLLOFF_AUTO:
399 default: c->operand[14] = 0x2; break;
Tommy Jonssond2fd44a2010-09-12 16:03:45 -0300400 /* case ROLLOFF_NONE: c->operand[14] = 0xff; break; */
401 }
402 switch (fdtv->fe.dtv_property_cache.pilot) {
403 case PILOT_AUTO: c->operand[15] = 0x0; break;
404 case PILOT_OFF: c->operand[15] = 0x0; break;
405 case PILOT_ON: c->operand[15] = 0x1; break;
406 }
407 } else {
408 c->operand[13] = 0x1; /* auto modulation */
409 c->operand[14] = 0xff; /* disable rolloff */
410 c->operand[15] = 0xff; /* disable pilot */
411 }
Stefan Richter1e4348c2009-11-18 16:03:31 -0300412 return 16;
Stefan Richter15490792009-02-23 14:21:10 +0100413 } else {
Stefan Richter1e4348c2009-11-18 16:03:31 -0300414 return 13;
Stefan Richter15490792009-02-23 14:21:10 +0100415 }
416}
417
Stefan Richter1e4348c2009-11-18 16:03:31 -0300418static int avc_tuner_dsd_dvb_c(struct firedtv *fdtv,
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300419 struct dtv_frontend_properties *p)
Stefan Richter15490792009-02-23 14:21:10 +0100420{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300421 struct avc_command_frame *c = (void *)fdtv->avc_data;
422
Stefan Richter15490792009-02-23 14:21:10 +0100423 c->opcode = AVC_OPCODE_DSD;
424
425 c->operand[0] = 0; /* source plug */
426 c->operand[1] = 0xd2; /* subfunction replace */
427 c->operand[2] = 0x20; /* system id = DVB */
428 c->operand[3] = 0x00; /* antenna number */
429 c->operand[4] = 0x11; /* system_specific_multiplex selection_length */
430
431 /* multiplex_valid_flags, high byte */
432 c->operand[5] = 0 << 7 /* reserved */
433 | 0 << 6 /* Polarisation */
434 | 0 << 5 /* Orbital_Pos */
435 | 1 << 4 /* Frequency */
436 | 1 << 3 /* Symbol_Rate */
437 | 0 << 2 /* FEC_outer */
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300438 | (p->fec_inner != FEC_AUTO ? 1 << 1 : 0)
439 | (p->modulation != QAM_AUTO ? 1 << 0 : 0);
Stefan Richter15490792009-02-23 14:21:10 +0100440
441 /* multiplex_valid_flags, low byte */
442 c->operand[6] = 0 << 7 /* NetworkID */
443 | 0 << 0 /* reserved */ ;
444
445 c->operand[7] = 0x00;
446 c->operand[8] = 0x00;
447 c->operand[9] = 0x00;
448 c->operand[10] = 0x00;
449
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300450 c->operand[11] = (((p->frequency / 4000) >> 16) & 0xff) | (2 << 6);
451 c->operand[12] = ((p->frequency / 4000) >> 8) & 0xff;
452 c->operand[13] = (p->frequency / 4000) & 0xff;
453 c->operand[14] = ((p->symbol_rate / 1000) >> 12) & 0xff;
454 c->operand[15] = ((p->symbol_rate / 1000) >> 4) & 0xff;
455 c->operand[16] = ((p->symbol_rate / 1000) << 4) & 0xf0;
Stefan Richter15490792009-02-23 14:21:10 +0100456 c->operand[17] = 0x00;
457
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300458 switch (p->fec_inner) {
Stefan Richter15490792009-02-23 14:21:10 +0100459 case FEC_1_2: c->operand[18] = 0x1; break;
460 case FEC_2_3: c->operand[18] = 0x2; break;
461 case FEC_3_4: c->operand[18] = 0x3; break;
462 case FEC_5_6: c->operand[18] = 0x4; break;
463 case FEC_7_8: c->operand[18] = 0x5; break;
464 case FEC_8_9: c->operand[18] = 0x6; break;
465 case FEC_4_5: c->operand[18] = 0x8; break;
466 case FEC_AUTO:
467 default: c->operand[18] = 0x0;
468 }
469
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300470 switch (p->modulation) {
Stefan Richter15490792009-02-23 14:21:10 +0100471 case QAM_16: c->operand[19] = 0x08; break;
472 case QAM_32: c->operand[19] = 0x10; break;
473 case QAM_64: c->operand[19] = 0x18; break;
474 case QAM_128: c->operand[19] = 0x20; break;
475 case QAM_256: c->operand[19] = 0x28; break;
476 case QAM_AUTO:
477 default: c->operand[19] = 0x00;
478 }
479
480 c->operand[20] = 0x00;
481 c->operand[21] = 0x00;
Stefan Richter15490792009-02-23 14:21:10 +0100482
Stefan Richter1e4348c2009-11-18 16:03:31 -0300483 return 22 + add_pid_filter(fdtv, &c->operand[22]);
Stefan Richter15490792009-02-23 14:21:10 +0100484}
485
Stefan Richter1e4348c2009-11-18 16:03:31 -0300486static int avc_tuner_dsd_dvb_t(struct firedtv *fdtv,
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300487 struct dtv_frontend_properties *p)
Stefan Richter15490792009-02-23 14:21:10 +0100488{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300489 struct avc_command_frame *c = (void *)fdtv->avc_data;
Stefan Richter15490792009-02-23 14:21:10 +0100490
491 c->opcode = AVC_OPCODE_DSD;
492
493 c->operand[0] = 0; /* source plug */
494 c->operand[1] = 0xd2; /* subfunction replace */
495 c->operand[2] = 0x20; /* system id = DVB */
496 c->operand[3] = 0x00; /* antenna number */
497 c->operand[4] = 0x0c; /* system_specific_multiplex selection_length */
498
499 /* multiplex_valid_flags, high byte */
500 c->operand[5] =
501 0 << 7 /* reserved */
502 | 1 << 6 /* CenterFrequency */
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300503 | (p->bandwidth_hz != 0 ? 1 << 5 : 0)
504 | (p->modulation != QAM_AUTO ? 1 << 4 : 0)
505 | (p->hierarchy != HIERARCHY_AUTO ? 1 << 3 : 0)
506 | (p->code_rate_HP != FEC_AUTO ? 1 << 2 : 0)
507 | (p->code_rate_LP != FEC_AUTO ? 1 << 1 : 0)
508 | (p->guard_interval != GUARD_INTERVAL_AUTO ? 1 << 0 : 0);
Stefan Richter15490792009-02-23 14:21:10 +0100509
510 /* multiplex_valid_flags, low byte */
511 c->operand[6] =
512 0 << 7 /* NetworkID */
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300513 | (p->transmission_mode != TRANSMISSION_MODE_AUTO ? 1 << 6 : 0)
Stefan Richter15490792009-02-23 14:21:10 +0100514 | 0 << 5 /* OtherFrequencyFlag */
515 | 0 << 0 /* reserved */ ;
516
517 c->operand[7] = 0x0;
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300518 c->operand[8] = (p->frequency / 10) >> 24;
519 c->operand[9] = ((p->frequency / 10) >> 16) & 0xff;
520 c->operand[10] = ((p->frequency / 10) >> 8) & 0xff;
521 c->operand[11] = (p->frequency / 10) & 0xff;
Stefan Richter15490792009-02-23 14:21:10 +0100522
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300523 switch (p->bandwidth_hz) {
524 case 7000000: c->operand[12] = 0x20; break;
525 case 8000000:
526 case 6000000: /* not defined by AVC spec */
527 case 0:
Stefan Richter15490792009-02-23 14:21:10 +0100528 default: c->operand[12] = 0x00;
529 }
530
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300531 switch (p->modulation) {
Stefan Richter15490792009-02-23 14:21:10 +0100532 case QAM_16: c->operand[13] = 1 << 6; break;
533 case QAM_64: c->operand[13] = 2 << 6; break;
534 case QPSK:
535 default: c->operand[13] = 0x00;
536 }
537
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300538 switch (p->hierarchy) {
Stefan Richter15490792009-02-23 14:21:10 +0100539 case HIERARCHY_1: c->operand[13] |= 1 << 3; break;
540 case HIERARCHY_2: c->operand[13] |= 2 << 3; break;
541 case HIERARCHY_4: c->operand[13] |= 3 << 3; break;
542 case HIERARCHY_AUTO:
543 case HIERARCHY_NONE:
544 default: break;
545 }
546
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300547 switch (p->code_rate_HP) {
Stefan Richter15490792009-02-23 14:21:10 +0100548 case FEC_2_3: c->operand[13] |= 1; break;
549 case FEC_3_4: c->operand[13] |= 2; break;
550 case FEC_5_6: c->operand[13] |= 3; break;
551 case FEC_7_8: c->operand[13] |= 4; break;
552 case FEC_1_2:
553 default: break;
554 }
555
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300556 switch (p->code_rate_LP) {
Stefan Richter15490792009-02-23 14:21:10 +0100557 case FEC_2_3: c->operand[14] = 1 << 5; break;
558 case FEC_3_4: c->operand[14] = 2 << 5; break;
559 case FEC_5_6: c->operand[14] = 3 << 5; break;
560 case FEC_7_8: c->operand[14] = 4 << 5; break;
561 case FEC_1_2:
562 default: c->operand[14] = 0x00; break;
563 }
564
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300565 switch (p->guard_interval) {
Stefan Richter15490792009-02-23 14:21:10 +0100566 case GUARD_INTERVAL_1_16: c->operand[14] |= 1 << 3; break;
567 case GUARD_INTERVAL_1_8: c->operand[14] |= 2 << 3; break;
568 case GUARD_INTERVAL_1_4: c->operand[14] |= 3 << 3; break;
569 case GUARD_INTERVAL_1_32:
570 case GUARD_INTERVAL_AUTO:
571 default: break;
572 }
573
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300574 switch (p->transmission_mode) {
Stefan Richter15490792009-02-23 14:21:10 +0100575 case TRANSMISSION_MODE_8K: c->operand[14] |= 1 << 1; break;
576 case TRANSMISSION_MODE_2K:
577 case TRANSMISSION_MODE_AUTO:
578 default: break;
579 }
580
581 c->operand[15] = 0x00; /* network_ID[0] */
582 c->operand[16] = 0x00; /* network_ID[1] */
Stefan Richter15490792009-02-23 14:21:10 +0100583
Stefan Richter1e4348c2009-11-18 16:03:31 -0300584 return 17 + add_pid_filter(fdtv, &c->operand[17]);
Stefan Richter15490792009-02-23 14:21:10 +0100585}
586
587int avc_tuner_dsd(struct firedtv *fdtv,
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300588 struct dtv_frontend_properties *p)
Stefan Richter15490792009-02-23 14:21:10 +0100589{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300590 struct avc_command_frame *c = (void *)fdtv->avc_data;
Stefan Richter1e4348c2009-11-18 16:03:31 -0300591 int pos, ret;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300592
Stefan Richter6385c5b2009-11-18 16:03:03 -0300593 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100594
Stefan Richter15490792009-02-23 14:21:10 +0100595 c->ctype = AVC_CTYPE_CONTROL;
596 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
597
598 switch (fdtv->type) {
599 case FIREDTV_DVB_S:
Mauro Carvalho Chehabe11eb282011-12-26 16:12:40 -0300600 case FIREDTV_DVB_S2: pos = avc_tuner_tuneqpsk(fdtv, p); break;
601 case FIREDTV_DVB_C: pos = avc_tuner_dsd_dvb_c(fdtv, p); break;
602 case FIREDTV_DVB_T: pos = avc_tuner_dsd_dvb_t(fdtv, p); break;
Stefan Richter15490792009-02-23 14:21:10 +0100603 default:
604 BUG();
605 }
Stefan Richter1e4348c2009-11-18 16:03:31 -0300606 pad_operands(c, pos);
607
608 fdtv->avc_data_length = ALIGN(3 + pos, 4);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300609 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100610#if 0
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300611 /*
612 * FIXME:
613 * u8 *status was an out-parameter of avc_tuner_dsd, unused by caller.
614 * Check for AVC_RESPONSE_ACCEPTED here instead?
615 */
Stefan Richter15490792009-02-23 14:21:10 +0100616 if (status)
617 *status = r->operand[2];
618#endif
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300619 mutex_unlock(&fdtv->avc_mutex);
620
621 if (ret == 0)
622 msleep(500);
623
624 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100625}
626
627int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
628{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300629 struct avc_command_frame *c = (void *)fdtv->avc_data;
630 int ret, pos, k;
Stefan Richter15490792009-02-23 14:21:10 +0100631
632 if (pidc > 16 && pidc != 0xff)
633 return -EINVAL;
634
Stefan Richter6385c5b2009-11-18 16:03:03 -0300635 mutex_lock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300636
Stefan Richter15490792009-02-23 14:21:10 +0100637 c->ctype = AVC_CTYPE_CONTROL;
638 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
639 c->opcode = AVC_OPCODE_DSD;
640
641 c->operand[0] = 0; /* source plug */
642 c->operand[1] = 0xd2; /* subfunction replace */
643 c->operand[2] = 0x20; /* system id = DVB */
644 c->operand[3] = 0x00; /* antenna number */
645 c->operand[4] = 0x00; /* system_specific_multiplex selection_length */
646 c->operand[5] = pidc; /* Nr_of_dsd_sel_specs */
647
648 pos = 6;
649 if (pidc != 0xff)
650 for (k = 0; k < pidc; k++) {
651 c->operand[pos++] = 0x13; /* flowfunction relay */
652 c->operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
653 c->operand[pos++] = (pid[k] >> 8) & 0x1f;
654 c->operand[pos++] = pid[k] & 0xff;
655 c->operand[pos++] = 0x00; /* tableID */
656 c->operand[pos++] = 0x00; /* filter_length */
657 }
Stefan Richter1e4348c2009-11-18 16:03:31 -0300658 pad_operands(c, pos);
Stefan Richter15490792009-02-23 14:21:10 +0100659
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300660 fdtv->avc_data_length = ALIGN(3 + pos, 4);
661 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100662
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300663 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +0100664
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300665 mutex_unlock(&fdtv->avc_mutex);
666
667 if (ret == 0)
668 msleep(50);
669
670 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100671}
672
673int avc_tuner_get_ts(struct firedtv *fdtv)
674{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300675 struct avc_command_frame *c = (void *)fdtv->avc_data;
676 int ret, sl;
677
Stefan Richter6385c5b2009-11-18 16:03:03 -0300678 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100679
Stefan Richter15490792009-02-23 14:21:10 +0100680 c->ctype = AVC_CTYPE_CONTROL;
681 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
682 c->opcode = AVC_OPCODE_DSIT;
683
684 sl = fdtv->type == FIREDTV_DVB_T ? 0x0c : 0x11;
685
686 c->operand[0] = 0; /* source plug */
687 c->operand[1] = 0xd2; /* subfunction replace */
688 c->operand[2] = 0xff; /* status */
689 c->operand[3] = 0x20; /* system id = DVB */
690 c->operand[4] = 0x00; /* antenna number */
691 c->operand[5] = 0x0; /* system_specific_search_flags */
692 c->operand[6] = sl; /* system_specific_multiplex selection_length */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300693 /*
694 * operand[7]: valid_flags[0]
695 * operand[8]: valid_flags[1]
696 * operand[7 + sl]: nr_of_dsit_sel_specs (always 0)
697 */
698 clear_operands(c, 7, 24);
Stefan Richter15490792009-02-23 14:21:10 +0100699
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300700 fdtv->avc_data_length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
701 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100702
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300703 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +0100704
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300705 mutex_unlock(&fdtv->avc_mutex);
706
707 if (ret == 0)
708 msleep(250);
709
710 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100711}
712
713int avc_identify_subunit(struct firedtv *fdtv)
714{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300715 struct avc_command_frame *c = (void *)fdtv->avc_data;
716 struct avc_response_frame *r = (void *)fdtv->avc_data;
717 int ret;
718
Stefan Richter6385c5b2009-11-18 16:03:03 -0300719 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100720
Stefan Richter15490792009-02-23 14:21:10 +0100721 c->ctype = AVC_CTYPE_CONTROL;
722 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
723 c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
724
725 c->operand[0] = DESCRIPTOR_SUBUNIT_IDENTIFIER;
726 c->operand[1] = 0xff;
727 c->operand[2] = 0x00;
728 c->operand[3] = 0x00; /* length highbyte */
729 c->operand[4] = 0x08; /* length lowbyte */
730 c->operand[5] = 0x00; /* offset highbyte */
731 c->operand[6] = 0x0d; /* offset lowbyte */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300732 clear_operands(c, 7, 8); /* padding */
Stefan Richter15490792009-02-23 14:21:10 +0100733
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300734 fdtv->avc_data_length = 12;
735 ret = avc_write(fdtv);
736 if (ret < 0)
737 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100738
739 if ((r->response != AVC_RESPONSE_STABLE &&
740 r->response != AVC_RESPONSE_ACCEPTED) ||
741 (r->operand[3] << 8) + r->operand[4] != 8) {
742 dev_err(fdtv->device, "cannot read subunit identifier\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300743 ret = -EINVAL;
Stefan Richter15490792009-02-23 14:21:10 +0100744 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300745out:
746 mutex_unlock(&fdtv->avc_mutex);
747
748 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100749}
750
751#define SIZEOF_ANTENNA_INPUT_INFO 22
752
753int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
754{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300755 struct avc_command_frame *c = (void *)fdtv->avc_data;
756 struct avc_response_frame *r = (void *)fdtv->avc_data;
757 int length, ret;
758
Stefan Richter6385c5b2009-11-18 16:03:03 -0300759 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100760
Stefan Richter15490792009-02-23 14:21:10 +0100761 c->ctype = AVC_CTYPE_CONTROL;
762 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
763 c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
764
765 c->operand[0] = DESCRIPTOR_TUNER_STATUS;
766 c->operand[1] = 0xff; /* read_result_status */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300767 /*
768 * operand[2]: reserved
769 * operand[3]: SIZEOF_ANTENNA_INPUT_INFO >> 8
770 * operand[4]: SIZEOF_ANTENNA_INPUT_INFO & 0xff
771 */
772 clear_operands(c, 2, 31);
Stefan Richter15490792009-02-23 14:21:10 +0100773
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300774 fdtv->avc_data_length = 12;
775 ret = avc_write(fdtv);
776 if (ret < 0)
777 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100778
779 if (r->response != AVC_RESPONSE_STABLE &&
780 r->response != AVC_RESPONSE_ACCEPTED) {
781 dev_err(fdtv->device, "cannot read tuner status\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300782 ret = -EINVAL;
783 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100784 }
785
786 length = r->operand[9];
787 if (r->operand[1] != 0x10 || length != SIZEOF_ANTENNA_INPUT_INFO) {
788 dev_err(fdtv->device, "got invalid tuner status\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300789 ret = -EINVAL;
790 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100791 }
792
793 stat->active_system = r->operand[10];
794 stat->searching = r->operand[11] >> 7 & 1;
795 stat->moving = r->operand[11] >> 6 & 1;
796 stat->no_rf = r->operand[11] >> 5 & 1;
797 stat->input = r->operand[12] >> 7 & 1;
798 stat->selected_antenna = r->operand[12] & 0x7f;
799 stat->ber = r->operand[13] << 24 |
800 r->operand[14] << 16 |
801 r->operand[15] << 8 |
802 r->operand[16];
803 stat->signal_strength = r->operand[17];
804 stat->raster_frequency = r->operand[18] >> 6 & 2;
805 stat->rf_frequency = (r->operand[18] & 0x3f) << 16 |
806 r->operand[19] << 8 |
807 r->operand[20];
808 stat->man_dep_info_length = r->operand[21];
809 stat->front_end_error = r->operand[22] >> 4 & 1;
810 stat->antenna_error = r->operand[22] >> 3 & 1;
811 stat->front_end_power_status = r->operand[22] >> 1 & 1;
812 stat->power_supply = r->operand[22] & 1;
813 stat->carrier_noise_ratio = r->operand[23] << 8 |
814 r->operand[24];
815 stat->power_supply_voltage = r->operand[27];
816 stat->antenna_voltage = r->operand[28];
817 stat->firewire_bus_voltage = r->operand[29];
818 stat->ca_mmi = r->operand[30] & 1;
819 stat->ca_pmt_reply = r->operand[31] >> 7 & 1;
820 stat->ca_date_time_request = r->operand[31] >> 6 & 1;
821 stat->ca_application_info = r->operand[31] >> 5 & 1;
822 stat->ca_module_present_status = r->operand[31] >> 4 & 1;
823 stat->ca_dvb_flag = r->operand[31] >> 3 & 1;
824 stat->ca_error_flag = r->operand[31] >> 2 & 1;
825 stat->ca_initialization_status = r->operand[31] >> 1 & 1;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300826out:
827 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100828
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300829 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100830}
831
832int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
833 char conttone, char nrdiseq,
834 struct dvb_diseqc_master_cmd *diseqcmd)
835{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300836 struct avc_command_frame *c = (void *)fdtv->avc_data;
837 struct avc_response_frame *r = (void *)fdtv->avc_data;
Stefan Richter1e4348c2009-11-18 16:03:31 -0300838 int pos, j, k, ret;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300839
Stefan Richter6385c5b2009-11-18 16:03:03 -0300840 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100841
Stefan Richter15490792009-02-23 14:21:10 +0100842 c->ctype = AVC_CTYPE_CONTROL;
843 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
844 c->opcode = AVC_OPCODE_VENDOR;
845
846 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
847 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
848 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
849 c->operand[3] = SFE_VENDOR_OPCODE_LNB_CONTROL;
Stefan Richter15490792009-02-23 14:21:10 +0100850 c->operand[4] = voltage;
851 c->operand[5] = nrdiseq;
852
Stefan Richter1e4348c2009-11-18 16:03:31 -0300853 pos = 6;
Stefan Richter15490792009-02-23 14:21:10 +0100854 for (j = 0; j < nrdiseq; j++) {
Stefan Richter1e4348c2009-11-18 16:03:31 -0300855 c->operand[pos++] = diseqcmd[j].msg_len;
Stefan Richter15490792009-02-23 14:21:10 +0100856
857 for (k = 0; k < diseqcmd[j].msg_len; k++)
Stefan Richter1e4348c2009-11-18 16:03:31 -0300858 c->operand[pos++] = diseqcmd[j].msg[k];
Stefan Richter15490792009-02-23 14:21:10 +0100859 }
Stefan Richter1e4348c2009-11-18 16:03:31 -0300860 c->operand[pos++] = burst;
861 c->operand[pos++] = conttone;
862 pad_operands(c, pos);
Stefan Richter15490792009-02-23 14:21:10 +0100863
Stefan Richter1e4348c2009-11-18 16:03:31 -0300864 fdtv->avc_data_length = ALIGN(3 + pos, 4);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300865 ret = avc_write(fdtv);
866 if (ret < 0)
867 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100868
869 if (r->response != AVC_RESPONSE_ACCEPTED) {
870 dev_err(fdtv->device, "LNB control failed\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300871 ret = -EINVAL;
Stefan Richter15490792009-02-23 14:21:10 +0100872 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300873out:
874 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100875
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300876 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100877}
878
879int avc_register_remote_control(struct firedtv *fdtv)
880{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300881 struct avc_command_frame *c = (void *)fdtv->avc_data;
882 int ret;
883
Stefan Richter6385c5b2009-11-18 16:03:03 -0300884 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100885
Stefan Richter15490792009-02-23 14:21:10 +0100886 c->ctype = AVC_CTYPE_NOTIFY;
887 c->subunit = AVC_SUBUNIT_TYPE_UNIT | 7;
888 c->opcode = AVC_OPCODE_VENDOR;
889
890 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
891 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
892 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
893 c->operand[3] = SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
Stefan Richter1e4348c2009-11-18 16:03:31 -0300894 c->operand[4] = 0; /* padding */
Stefan Richter15490792009-02-23 14:21:10 +0100895
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300896 fdtv->avc_data_length = 8;
897 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100898
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300899 /* FIXME: check response code? */
900
901 mutex_unlock(&fdtv->avc_mutex);
902
903 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100904}
905
906void avc_remote_ctrl_work(struct work_struct *work)
907{
908 struct firedtv *fdtv =
909 container_of(work, struct firedtv, remote_ctrl_work);
910
911 /* Should it be rescheduled in failure cases? */
912 avc_register_remote_control(fdtv);
913}
914
915#if 0 /* FIXME: unused */
916int avc_tuner_host2ca(struct firedtv *fdtv)
917{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300918 struct avc_command_frame *c = (void *)fdtv->avc_data;
919 int ret;
920
Stefan Richter6385c5b2009-11-18 16:03:03 -0300921 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100922
Stefan Richter15490792009-02-23 14:21:10 +0100923 c->ctype = AVC_CTYPE_CONTROL;
924 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
925 c->opcode = AVC_OPCODE_VENDOR;
926
927 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
928 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
929 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
930 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
931 c->operand[4] = 0; /* slot */
932 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300933 clear_operands(c, 6, 8);
Stefan Richter15490792009-02-23 14:21:10 +0100934
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300935 fdtv->avc_data_length = 12;
936 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100937
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300938 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +0100939
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300940 mutex_unlock(&fdtv->avc_mutex);
941
942 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100943}
944#endif
945
946static int get_ca_object_pos(struct avc_response_frame *r)
947{
948 int length = 1;
949
950 /* Check length of length field */
951 if (r->operand[7] & 0x80)
952 length = (r->operand[7] & 0x7f) + 1;
953 return length + 7;
954}
955
956static int get_ca_object_length(struct avc_response_frame *r)
957{
958#if 0 /* FIXME: unused */
959 int size = 0;
960 int i;
961
962 if (r->operand[7] & 0x80)
963 for (i = 0; i < (r->operand[7] & 0x7f); i++) {
964 size <<= 8;
965 size += r->operand[8 + i];
966 }
967#endif
968 return r->operand[7];
969}
970
Nathan Chancellorbb457a42018-10-18 16:03:06 -0400971int avc_ca_app_info(struct firedtv *fdtv, unsigned char *app_info,
972 unsigned int *len)
Stefan Richter15490792009-02-23 14:21:10 +0100973{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300974 struct avc_command_frame *c = (void *)fdtv->avc_data;
975 struct avc_response_frame *r = (void *)fdtv->avc_data;
976 int pos, ret;
977
Stefan Richter6385c5b2009-11-18 16:03:03 -0300978 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100979
Stefan Richter15490792009-02-23 14:21:10 +0100980 c->ctype = AVC_CTYPE_STATUS;
981 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
982 c->opcode = AVC_OPCODE_VENDOR;
983
984 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
985 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
986 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
987 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
988 c->operand[4] = 0; /* slot */
989 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300990 clear_operands(c, 6, LAST_OPERAND);
Stefan Richter15490792009-02-23 14:21:10 +0100991
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300992 fdtv->avc_data_length = 12;
993 ret = avc_write(fdtv);
994 if (ret < 0)
995 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100996
997 /* FIXME: check response code and validate response data */
998
999 pos = get_ca_object_pos(r);
1000 app_info[0] = (EN50221_TAG_APP_INFO >> 16) & 0xff;
1001 app_info[1] = (EN50221_TAG_APP_INFO >> 8) & 0xff;
1002 app_info[2] = (EN50221_TAG_APP_INFO >> 0) & 0xff;
1003 app_info[3] = 6 + r->operand[pos + 4];
1004 app_info[4] = 0x01;
1005 memcpy(&app_info[5], &r->operand[pos], 5 + r->operand[pos + 4]);
1006 *len = app_info[3] + 4;
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001007out:
1008 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001009
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001010 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001011}
1012
Nathan Chancellorbb457a42018-10-18 16:03:06 -04001013int avc_ca_info(struct firedtv *fdtv, unsigned char *app_info,
1014 unsigned int *len)
Stefan Richter15490792009-02-23 14:21:10 +01001015{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001016 struct avc_command_frame *c = (void *)fdtv->avc_data;
1017 struct avc_response_frame *r = (void *)fdtv->avc_data;
Henrik Kurelid52065c52010-03-01 08:56:42 -03001018 int i, pos, ret;
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001019
Stefan Richter6385c5b2009-11-18 16:03:03 -03001020 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001021
Stefan Richter15490792009-02-23 14:21:10 +01001022 c->ctype = AVC_CTYPE_STATUS;
1023 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1024 c->opcode = AVC_OPCODE_VENDOR;
1025
1026 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1027 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1028 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1029 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1030 c->operand[4] = 0; /* slot */
1031 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
Stefan Richter1e4348c2009-11-18 16:03:31 -03001032 clear_operands(c, 6, LAST_OPERAND);
Stefan Richter15490792009-02-23 14:21:10 +01001033
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001034 fdtv->avc_data_length = 12;
1035 ret = avc_write(fdtv);
1036 if (ret < 0)
1037 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001038
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001039 /* FIXME: check response code and validate response data */
Stefan Richter15490792009-02-23 14:21:10 +01001040
1041 pos = get_ca_object_pos(r);
1042 app_info[0] = (EN50221_TAG_CA_INFO >> 16) & 0xff;
1043 app_info[1] = (EN50221_TAG_CA_INFO >> 8) & 0xff;
1044 app_info[2] = (EN50221_TAG_CA_INFO >> 0) & 0xff;
Henrik Kurelid52065c52010-03-01 08:56:42 -03001045 if (num_fake_ca_system_ids == 0) {
1046 app_info[3] = 2;
1047 app_info[4] = r->operand[pos + 0];
1048 app_info[5] = r->operand[pos + 1];
1049 } else {
1050 app_info[3] = num_fake_ca_system_ids * 2;
1051 for (i = 0; i < num_fake_ca_system_ids; i++) {
1052 app_info[4 + i * 2] =
1053 (fake_ca_system_ids[i] >> 8) & 0xff;
1054 app_info[5 + i * 2] = fake_ca_system_ids[i] & 0xff;
1055 }
1056 }
Stefan Richter15490792009-02-23 14:21:10 +01001057 *len = app_info[3] + 4;
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001058out:
1059 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001060
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001061 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001062}
1063
1064int avc_ca_reset(struct firedtv *fdtv)
1065{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001066 struct avc_command_frame *c = (void *)fdtv->avc_data;
1067 int ret;
1068
Stefan Richter6385c5b2009-11-18 16:03:03 -03001069 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001070
Stefan Richter15490792009-02-23 14:21:10 +01001071 c->ctype = AVC_CTYPE_CONTROL;
1072 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1073 c->opcode = AVC_OPCODE_VENDOR;
1074
1075 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1076 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1077 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1078 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1079 c->operand[4] = 0; /* slot */
1080 c->operand[5] = SFE_VENDOR_TAG_CA_RESET; /* ca tag */
1081 c->operand[6] = 0; /* more/last */
1082 c->operand[7] = 1; /* length */
1083 c->operand[8] = 0; /* force hardware reset */
1084
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001085 fdtv->avc_data_length = 12;
1086 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +01001087
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001088 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +01001089
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001090 mutex_unlock(&fdtv->avc_mutex);
1091
1092 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001093}
1094
1095int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
1096{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001097 struct avc_command_frame *c = (void *)fdtv->avc_data;
1098 struct avc_response_frame *r = (void *)fdtv->avc_data;
Stefan Richter15490792009-02-23 14:21:10 +01001099 int list_management;
1100 int program_info_length;
1101 int pmt_cmd_id;
1102 int read_pos;
1103 int write_pos;
1104 int es_info_length;
1105 int crc32_csum;
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001106 int ret;
Stefan Richter15490792009-02-23 14:21:10 +01001107
Henrik Kurelid15344772009-08-01 08:04:06 -03001108 if (unlikely(avc_debug & AVC_DEBUG_APPLICATION_PMT))
1109 debug_pmt(msg, length);
1110
Stefan Richter6385c5b2009-11-18 16:03:03 -03001111 mutex_lock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001112
Stefan Richter15490792009-02-23 14:21:10 +01001113 c->ctype = AVC_CTYPE_CONTROL;
1114 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1115 c->opcode = AVC_OPCODE_VENDOR;
1116
1117 if (msg[0] != EN50221_LIST_MANAGEMENT_ONLY) {
1118 dev_info(fdtv->device, "forcing list_management to ONLY\n");
1119 msg[0] = EN50221_LIST_MANAGEMENT_ONLY;
1120 }
1121 /* We take the cmd_id from the programme level only! */
1122 list_management = msg[0];
1123 program_info_length = ((msg[4] & 0x0f) << 8) + msg[5];
1124 if (program_info_length > 0)
1125 program_info_length--; /* Remove pmt_cmd_id */
1126 pmt_cmd_id = msg[6];
1127
1128 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1129 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1130 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1131 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1132 c->operand[4] = 0; /* slot */
1133 c->operand[5] = SFE_VENDOR_TAG_CA_PMT; /* ca tag */
1134 c->operand[6] = 0; /* more/last */
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001135 /* Use three bytes for length field in case length > 127 */
1136 c->operand[10] = list_management;
1137 c->operand[11] = 0x01; /* pmt_cmd=OK_descramble */
Stefan Richter15490792009-02-23 14:21:10 +01001138
1139 /* TS program map table */
1140
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001141 c->operand[12] = 0x02; /* Table id=2 */
1142 c->operand[13] = 0x80; /* Section syntax + length */
Stefan Richter1e4348c2009-11-18 16:03:31 -03001143
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001144 c->operand[15] = msg[1]; /* Program number */
1145 c->operand[16] = msg[2];
Henrik Kurelidad5e9b92009-07-21 13:45:50 -03001146 c->operand[17] = msg[3]; /* Version number and current/next */
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001147 c->operand[18] = 0x00; /* Section number=0 */
1148 c->operand[19] = 0x00; /* Last section number=0 */
1149 c->operand[20] = 0x1f; /* PCR_PID=1FFF */
1150 c->operand[21] = 0xff;
1151 c->operand[22] = (program_info_length >> 8); /* Program info length */
1152 c->operand[23] = (program_info_length & 0xff);
Stefan Richter15490792009-02-23 14:21:10 +01001153
1154 /* CA descriptors at programme level */
1155 read_pos = 6;
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001156 write_pos = 24;
Stefan Richter15490792009-02-23 14:21:10 +01001157 if (program_info_length > 0) {
1158 pmt_cmd_id = msg[read_pos++];
1159 if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1160 dev_err(fdtv->device,
1161 "invalid pmt_cmd_id %d\n", pmt_cmd_id);
Dan Carpenter7ac95cf2014-09-09 09:11:23 -03001162 if (program_info_length > sizeof(c->operand) - 4 - write_pos) {
Dan Carpenter3011e5e2014-09-08 08:18:43 -03001163 ret = -EINVAL;
1164 goto out;
1165 }
Stefan Richter15490792009-02-23 14:21:10 +01001166
1167 memcpy(&c->operand[write_pos], &msg[read_pos],
1168 program_info_length);
1169 read_pos += program_info_length;
1170 write_pos += program_info_length;
1171 }
1172 while (read_pos < length) {
1173 c->operand[write_pos++] = msg[read_pos++];
1174 c->operand[write_pos++] = msg[read_pos++];
1175 c->operand[write_pos++] = msg[read_pos++];
1176 es_info_length =
1177 ((msg[read_pos] & 0x0f) << 8) + msg[read_pos + 1];
1178 read_pos += 2;
1179 if (es_info_length > 0)
1180 es_info_length--; /* Remove pmt_cmd_id */
1181 c->operand[write_pos++] = es_info_length >> 8;
1182 c->operand[write_pos++] = es_info_length & 0xff;
1183 if (es_info_length > 0) {
1184 pmt_cmd_id = msg[read_pos++];
1185 if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1186 dev_err(fdtv->device, "invalid pmt_cmd_id %d "
1187 "at stream level\n", pmt_cmd_id);
1188
Dan Carpenter7ac95cf2014-09-09 09:11:23 -03001189 if (es_info_length > sizeof(c->operand) - 4 -
1190 write_pos) {
Dan Carpenter3011e5e2014-09-08 08:18:43 -03001191 ret = -EINVAL;
1192 goto out;
1193 }
1194
Stefan Richter15490792009-02-23 14:21:10 +01001195 memcpy(&c->operand[write_pos], &msg[read_pos],
1196 es_info_length);
1197 read_pos += es_info_length;
1198 write_pos += es_info_length;
1199 }
1200 }
Stefan Richter1e4348c2009-11-18 16:03:31 -03001201 write_pos += 4; /* CRC */
Stefan Richter15490792009-02-23 14:21:10 +01001202
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001203 c->operand[7] = 0x82;
1204 c->operand[8] = (write_pos - 10) >> 8;
1205 c->operand[9] = (write_pos - 10) & 0xff;
1206 c->operand[14] = write_pos - 15;
Stefan Richter15490792009-02-23 14:21:10 +01001207
1208 crc32_csum = crc32_be(0, &c->operand[10], c->operand[12] - 1);
1209 c->operand[write_pos - 4] = (crc32_csum >> 24) & 0xff;
1210 c->operand[write_pos - 3] = (crc32_csum >> 16) & 0xff;
1211 c->operand[write_pos - 2] = (crc32_csum >> 8) & 0xff;
1212 c->operand[write_pos - 1] = (crc32_csum >> 0) & 0xff;
Stefan Richter1e4348c2009-11-18 16:03:31 -03001213 pad_operands(c, write_pos);
Stefan Richter15490792009-02-23 14:21:10 +01001214
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001215 fdtv->avc_data_length = ALIGN(3 + write_pos, 4);
1216 ret = avc_write(fdtv);
1217 if (ret < 0)
1218 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001219
1220 if (r->response != AVC_RESPONSE_ACCEPTED) {
1221 dev_err(fdtv->device,
1222 "CA PMT failed with response 0x%x\n", r->response);
Stefan Richter686a9482011-07-06 15:54:48 -03001223 ret = -EACCES;
Stefan Richter15490792009-02-23 14:21:10 +01001224 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001225out:
1226 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001227
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001228 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001229}
1230
1231int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
1232{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001233 struct avc_command_frame *c = (void *)fdtv->avc_data;
1234 struct avc_response_frame *r = (void *)fdtv->avc_data;
1235 int ret;
1236
Stefan Richter6385c5b2009-11-18 16:03:03 -03001237 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001238
Stefan Richter15490792009-02-23 14:21:10 +01001239 c->ctype = AVC_CTYPE_STATUS;
1240 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1241 c->opcode = AVC_OPCODE_VENDOR;
1242
1243 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1244 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1245 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1246 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1247 c->operand[4] = 0; /* slot */
1248 c->operand[5] = SFE_VENDOR_TAG_CA_DATE_TIME; /* ca tag */
Stefan Richter1e4348c2009-11-18 16:03:31 -03001249 clear_operands(c, 6, LAST_OPERAND);
Stefan Richter15490792009-02-23 14:21:10 +01001250
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001251 fdtv->avc_data_length = 12;
1252 ret = avc_write(fdtv);
1253 if (ret < 0)
1254 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001255
1256 /* FIXME: check response code and validate response data */
1257
1258 *interval = r->operand[get_ca_object_pos(r)];
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001259out:
1260 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001261
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001262 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001263}
1264
1265int avc_ca_enter_menu(struct firedtv *fdtv)
1266{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001267 struct avc_command_frame *c = (void *)fdtv->avc_data;
1268 int ret;
1269
Stefan Richter6385c5b2009-11-18 16:03:03 -03001270 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001271
Stefan Richter15490792009-02-23 14:21:10 +01001272 c->ctype = AVC_CTYPE_STATUS;
1273 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1274 c->opcode = AVC_OPCODE_VENDOR;
1275
1276 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1277 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1278 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1279 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1280 c->operand[4] = 0; /* slot */
1281 c->operand[5] = SFE_VENDOR_TAG_CA_ENTER_MENU;
Stefan Richter1e4348c2009-11-18 16:03:31 -03001282 clear_operands(c, 6, 8);
Stefan Richter15490792009-02-23 14:21:10 +01001283
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001284 fdtv->avc_data_length = 12;
1285 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +01001286
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001287 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +01001288
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001289 mutex_unlock(&fdtv->avc_mutex);
1290
1291 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001292}
1293
1294int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
1295{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001296 struct avc_command_frame *c = (void *)fdtv->avc_data;
1297 struct avc_response_frame *r = (void *)fdtv->avc_data;
1298 int ret;
1299
Stefan Richter6385c5b2009-11-18 16:03:03 -03001300 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001301
Stefan Richter15490792009-02-23 14:21:10 +01001302 c->ctype = AVC_CTYPE_STATUS;
1303 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1304 c->opcode = AVC_OPCODE_VENDOR;
1305
1306 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1307 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1308 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1309 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1310 c->operand[4] = 0; /* slot */
1311 c->operand[5] = SFE_VENDOR_TAG_CA_MMI;
Stefan Richter1e4348c2009-11-18 16:03:31 -03001312 clear_operands(c, 6, LAST_OPERAND);
Stefan Richter15490792009-02-23 14:21:10 +01001313
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001314 fdtv->avc_data_length = 12;
1315 ret = avc_write(fdtv);
1316 if (ret < 0)
1317 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001318
1319 /* FIXME: check response code and validate response data */
1320
1321 *len = get_ca_object_length(r);
1322 memcpy(mmi_object, &r->operand[get_ca_object_pos(r)], *len);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001323out:
1324 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001325
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001326 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001327}
1328
1329#define CMP_OUTPUT_PLUG_CONTROL_REG_0 0xfffff0000904ULL
1330
Stefan Richter53756592009-11-18 16:01:34 -03001331static int cmp_read(struct firedtv *fdtv, u64 addr, __be32 *data)
Stefan Richter15490792009-02-23 14:21:10 +01001332{
1333 int ret;
1334
Stefan Richter92374e82011-02-06 11:41:44 -03001335 ret = fdtv_read(fdtv, addr, data);
Stefan Richter15490792009-02-23 14:21:10 +01001336 if (ret < 0)
1337 dev_err(fdtv->device, "CMP: read I/O error\n");
1338
Stefan Richter15490792009-02-23 14:21:10 +01001339 return ret;
1340}
1341
Stefan Richter054286b2009-11-08 18:29:08 -03001342static int cmp_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
Stefan Richter15490792009-02-23 14:21:10 +01001343{
1344 int ret;
1345
Stefan Richterf30e6d32011-04-22 15:13:54 +02001346 ret = fdtv_lock(fdtv, addr, data);
Stefan Richter15490792009-02-23 14:21:10 +01001347 if (ret < 0)
1348 dev_err(fdtv->device, "CMP: lock I/O error\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001349
Stefan Richter15490792009-02-23 14:21:10 +01001350 return ret;
1351}
1352
1353static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
1354{
1355 return (be32_to_cpu(opcr) >> shift) & mask;
1356}
1357
1358static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
1359{
1360 *opcr &= ~cpu_to_be32(mask << shift);
1361 *opcr |= cpu_to_be32((value & mask) << shift);
1362}
1363
1364#define get_opcr_online(v) get_opcr((v), 0x1, 31)
1365#define get_opcr_p2p_connections(v) get_opcr((v), 0x3f, 24)
1366#define get_opcr_channel(v) get_opcr((v), 0x3f, 16)
1367
1368#define set_opcr_p2p_connections(p, v) set_opcr((p), (v), 0x3f, 24)
1369#define set_opcr_channel(p, v) set_opcr((p), (v), 0x3f, 16)
1370#define set_opcr_data_rate(p, v) set_opcr((p), (v), 0x3, 14)
1371#define set_opcr_overhead_id(p, v) set_opcr((p), (v), 0xf, 10)
1372
1373int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
1374{
Stefan Richter054286b2009-11-08 18:29:08 -03001375 __be32 old_opcr, opcr[2];
Stefan Richter15490792009-02-23 14:21:10 +01001376 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1377 int attempts = 0;
1378 int ret;
1379
Stefan Richter53756592009-11-18 16:01:34 -03001380 ret = cmp_read(fdtv, opcr_address, opcr);
Stefan Richter15490792009-02-23 14:21:10 +01001381 if (ret < 0)
1382 return ret;
1383
1384repeat:
Stefan Richter054286b2009-11-08 18:29:08 -03001385 if (!get_opcr_online(*opcr)) {
Stefan Richter15490792009-02-23 14:21:10 +01001386 dev_err(fdtv->device, "CMP: output offline\n");
1387 return -EBUSY;
1388 }
1389
Stefan Richter054286b2009-11-08 18:29:08 -03001390 old_opcr = *opcr;
Stefan Richter15490792009-02-23 14:21:10 +01001391
Stefan Richter054286b2009-11-08 18:29:08 -03001392 if (get_opcr_p2p_connections(*opcr)) {
1393 if (get_opcr_channel(*opcr) != channel) {
Stefan Richter15490792009-02-23 14:21:10 +01001394 dev_err(fdtv->device, "CMP: cannot change channel\n");
1395 return -EBUSY;
1396 }
1397 dev_info(fdtv->device, "CMP: overlaying connection\n");
1398
1399 /* We don't allocate isochronous resources. */
1400 } else {
Stefan Richter054286b2009-11-08 18:29:08 -03001401 set_opcr_channel(opcr, channel);
1402 set_opcr_data_rate(opcr, 2); /* S400 */
Stefan Richter15490792009-02-23 14:21:10 +01001403
1404 /* FIXME: this is for the worst case - optimize */
Stefan Richter054286b2009-11-08 18:29:08 -03001405 set_opcr_overhead_id(opcr, 0);
Stefan Richter15490792009-02-23 14:21:10 +01001406
Stefan Richter92374e82011-02-06 11:41:44 -03001407 /* FIXME: allocate isochronous channel and bandwidth at IRM */
Stefan Richter15490792009-02-23 14:21:10 +01001408 }
1409
Stefan Richter054286b2009-11-08 18:29:08 -03001410 set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) + 1);
Stefan Richter15490792009-02-23 14:21:10 +01001411
Stefan Richter054286b2009-11-08 18:29:08 -03001412 opcr[1] = *opcr;
1413 opcr[0] = old_opcr;
1414
1415 ret = cmp_lock(fdtv, opcr_address, opcr);
Stefan Richter15490792009-02-23 14:21:10 +01001416 if (ret < 0)
1417 return ret;
1418
Stefan Richter054286b2009-11-08 18:29:08 -03001419 if (old_opcr != *opcr) {
Stefan Richter15490792009-02-23 14:21:10 +01001420 /*
1421 * FIXME: if old_opcr.P2P_Connections > 0,
1422 * deallocate isochronous channel and bandwidth at IRM
Stefan Richter15490792009-02-23 14:21:10 +01001423 */
1424
1425 if (++attempts < 6) /* arbitrary limit */
1426 goto repeat;
1427 return -EBUSY;
1428 }
1429
1430 return 0;
1431}
1432
1433void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
1434{
Stefan Richter054286b2009-11-08 18:29:08 -03001435 __be32 old_opcr, opcr[2];
Stefan Richter15490792009-02-23 14:21:10 +01001436 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1437 int attempts = 0;
1438
Stefan Richter53756592009-11-18 16:01:34 -03001439 if (cmp_read(fdtv, opcr_address, opcr) < 0)
Stefan Richter15490792009-02-23 14:21:10 +01001440 return;
1441
1442repeat:
Stefan Richter054286b2009-11-08 18:29:08 -03001443 if (!get_opcr_online(*opcr) || !get_opcr_p2p_connections(*opcr) ||
1444 get_opcr_channel(*opcr) != channel) {
Stefan Richter15490792009-02-23 14:21:10 +01001445 dev_err(fdtv->device, "CMP: no connection to break\n");
1446 return;
1447 }
1448
Stefan Richter054286b2009-11-08 18:29:08 -03001449 old_opcr = *opcr;
1450 set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) - 1);
Stefan Richter15490792009-02-23 14:21:10 +01001451
Stefan Richter054286b2009-11-08 18:29:08 -03001452 opcr[1] = *opcr;
1453 opcr[0] = old_opcr;
1454
1455 if (cmp_lock(fdtv, opcr_address, opcr) < 0)
Stefan Richter15490792009-02-23 14:21:10 +01001456 return;
1457
Stefan Richter054286b2009-11-08 18:29:08 -03001458 if (old_opcr != *opcr) {
Stefan Richter15490792009-02-23 14:21:10 +01001459 /*
1460 * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
1461 * owner, deallocate isochronous channel and bandwidth at IRM
1462 * if (...)
1463 * fdtv->backend->dealloc_resources(fdtv, channel, bw);
1464 */
1465
1466 if (++attempts < 6) /* arbitrary limit */
1467 goto repeat;
1468 }
1469}