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