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