2 * FireDTV driver (formerly known as FireSAT)
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>
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.
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>
29 #define FCP_COMMAND_REGISTER 0xfffff0000b00ULL
31 #define AVC_CTYPE_CONTROL 0x0
32 #define AVC_CTYPE_STATUS 0x1
33 #define AVC_CTYPE_NOTIFY 0x3
35 #define AVC_RESPONSE_ACCEPTED 0x9
36 #define AVC_RESPONSE_STABLE 0xc
37 #define AVC_RESPONSE_CHANGED 0xd
38 #define AVC_RESPONSE_INTERIM 0xf
40 #define AVC_SUBUNIT_TYPE_TUNER (0x05 << 3)
41 #define AVC_SUBUNIT_TYPE_UNIT (0x1f << 3)
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
48 #define DESCRIPTOR_TUNER_STATUS 0x80
49 #define DESCRIPTOR_SUBUNIT_IDENTIFIER 0x00
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
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 */
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 */
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
72 #define EN50221_LIST_MANAGEMENT_ONLY 0x03
73 #define EN50221_TAG_APP_INFO 0x9f8021
74 #define EN50221_TAG_CA_INFO 0x9f8031
76 struct avc_command_frame {
83 struct avc_response_frame {
90 #define AVC_DEBUG_READ_DESCRIPTOR 0x0001
91 #define AVC_DEBUG_DSIT 0x0002
92 #define AVC_DEBUG_DSD 0x0004
93 #define AVC_DEBUG_REGISTER_REMOTE_CONTROL 0x0008
94 #define AVC_DEBUG_LNB_CONTROL 0x0010
95 #define AVC_DEBUG_TUNE_QPSK 0x0020
96 #define AVC_DEBUG_TUNE_QPSK2 0x0040
97 #define AVC_DEBUG_HOST2CA 0x0080
98 #define AVC_DEBUG_CA2HOST 0x0100
99 #define AVC_DEBUG_APPLICATION_PMT 0x4000
100 #define AVC_DEBUG_FCP_PAYLOADS 0x8000
102 static int avc_debug;
103 module_param_named(debug, avc_debug, int, 0644);
104 MODULE_PARM_DESC(debug, "Verbose logging (none = 0"
106 ": READ DESCRIPTOR = " __stringify(AVC_DEBUG_READ_DESCRIPTOR)
107 ", DSIT = " __stringify(AVC_DEBUG_DSIT)
108 ", REGISTER_REMOTE_CONTROL = " __stringify(AVC_DEBUG_REGISTER_REMOTE_CONTROL)
109 ", LNB CONTROL = " __stringify(AVC_DEBUG_LNB_CONTROL)
110 ", TUNE QPSK = " __stringify(AVC_DEBUG_TUNE_QPSK)
111 ", TUNE QPSK2 = " __stringify(AVC_DEBUG_TUNE_QPSK2)
112 ", HOST2CA = " __stringify(AVC_DEBUG_HOST2CA)
113 ", CA2HOST = " __stringify(AVC_DEBUG_CA2HOST)
114 "; Application sent PMT = " __stringify(AVC_DEBUG_APPLICATION_PMT)
115 ", FCP payloads = " __stringify(AVC_DEBUG_FCP_PAYLOADS)
116 ", or a combination, or all = -1)");
118 static const char *debug_fcp_ctype(unsigned int ctype)
120 static const char *ctypes[] = {
121 [0x0] = "CONTROL", [0x1] = "STATUS",
122 [0x2] = "SPECIFIC INQUIRY", [0x3] = "NOTIFY",
123 [0x4] = "GENERAL INQUIRY", [0x8] = "NOT IMPLEMENTED",
124 [0x9] = "ACCEPTED", [0xa] = "REJECTED",
125 [0xb] = "IN TRANSITION", [0xc] = "IMPLEMENTED/STABLE",
126 [0xd] = "CHANGED", [0xf] = "INTERIM",
128 const char *ret = ctype < ARRAY_SIZE(ctypes) ? ctypes[ctype] : NULL;
130 return ret ? ret : "?";
133 static const char *debug_fcp_opcode(unsigned int opcode,
134 const u8 *data, int length)
137 case AVC_OPCODE_VENDOR:
139 case AVC_OPCODE_READ_DESCRIPTOR:
140 return avc_debug & AVC_DEBUG_READ_DESCRIPTOR ?
141 "ReadDescriptor" : NULL;
142 case AVC_OPCODE_DSIT:
143 return avc_debug & AVC_DEBUG_DSIT ?
144 "DirectSelectInfo.Type" : NULL;
146 return avc_debug & AVC_DEBUG_DSD ? "DirectSelectData" : NULL;
152 data[3] != SFE_VENDOR_DE_COMPANYID_0 ||
153 data[4] != SFE_VENDOR_DE_COMPANYID_1 ||
154 data[5] != SFE_VENDOR_DE_COMPANYID_2)
155 return "Vendor/Unknown";
158 case SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL:
159 return avc_debug & AVC_DEBUG_REGISTER_REMOTE_CONTROL ?
161 case SFE_VENDOR_OPCODE_LNB_CONTROL:
162 return avc_debug & AVC_DEBUG_LNB_CONTROL ? "LNBControl" : NULL;
163 case SFE_VENDOR_OPCODE_TUNE_QPSK:
164 return avc_debug & AVC_DEBUG_TUNE_QPSK ? "TuneQPSK" : NULL;
165 case SFE_VENDOR_OPCODE_TUNE_QPSK2:
166 return avc_debug & AVC_DEBUG_TUNE_QPSK2 ? "TuneQPSK2" : NULL;
167 case SFE_VENDOR_OPCODE_HOST2CA:
168 return avc_debug & AVC_DEBUG_HOST2CA ? "Host2CA" : NULL;
169 case SFE_VENDOR_OPCODE_CA2HOST:
170 return avc_debug & AVC_DEBUG_CA2HOST ? "CA2Host" : NULL;
172 return "Vendor/Unknown";
175 static void debug_fcp(const u8 *data, int length)
177 unsigned int subunit_type, subunit_id, opcode;
178 const char *op, *prefix;
180 prefix = data[0] > 7 ? "FCP <- " : "FCP -> ";
181 subunit_type = data[1] >> 3;
182 subunit_id = data[1] & 7;
183 opcode = subunit_type == 0x1e || subunit_id == 5 ? ~0 : data[2];
184 op = debug_fcp_opcode(opcode, data, length);
187 printk(KERN_INFO "%ssu=%x.%x l=%d: %-8s - %s\n",
188 prefix, subunit_type, subunit_id, length,
189 debug_fcp_ctype(data[0]), op);
190 if (avc_debug & AVC_DEBUG_FCP_PAYLOADS)
191 print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_NONE,
192 16, 1, data, length, false);
196 static void debug_pmt(char *msg, int length)
198 printk(KERN_INFO "APP PMT -> l=%d\n", length);
199 print_hex_dump(KERN_INFO, "APP PMT -> ", DUMP_PREFIX_NONE,
200 16, 1, msg, length, false);
203 static int avc_write(struct firedtv *fdtv)
207 fdtv->avc_reply_received = false;
209 for (retry = 0; retry < 6; retry++) {
210 if (unlikely(avc_debug))
211 debug_fcp(fdtv->avc_data, fdtv->avc_data_length);
213 err = fdtv->backend->write(fdtv, FCP_COMMAND_REGISTER,
214 fdtv->avc_data, fdtv->avc_data_length);
216 dev_err(fdtv->device, "FCP command write failed\n");
222 * AV/C specs say that answers should be sent within 150 ms.
223 * Time out after 200 ms.
225 if (wait_event_timeout(fdtv->avc_wait,
226 fdtv->avc_reply_received,
227 msecs_to_jiffies(200)) != 0)
230 dev_err(fdtv->device, "FCP response timed out\n");
235 static bool is_register_rc(struct avc_response_frame *r)
237 return r->opcode == AVC_OPCODE_VENDOR &&
238 r->operand[0] == SFE_VENDOR_DE_COMPANYID_0 &&
239 r->operand[1] == SFE_VENDOR_DE_COMPANYID_1 &&
240 r->operand[2] == SFE_VENDOR_DE_COMPANYID_2 &&
241 r->operand[3] == SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
244 int avc_recv(struct firedtv *fdtv, void *data, size_t length)
246 struct avc_response_frame *r = data;
248 if (unlikely(avc_debug))
249 debug_fcp(data, length);
251 if (length >= 8 && is_register_rc(r)) {
252 switch (r->response) {
253 case AVC_RESPONSE_CHANGED:
254 fdtv_handle_rc(fdtv, r->operand[4] << 8 | r->operand[5]);
255 schedule_work(&fdtv->remote_ctrl_work);
257 case AVC_RESPONSE_INTERIM:
258 if (is_register_rc((void *)fdtv->avc_data))
262 dev_info(fdtv->device,
263 "remote control result = %d\n", r->response);
268 if (fdtv->avc_reply_received) {
269 dev_err(fdtv->device, "out-of-order AVC response, ignored\n");
273 memcpy(fdtv->avc_data, data, length);
274 fdtv->avc_data_length = length;
276 fdtv->avc_reply_received = true;
277 wake_up(&fdtv->avc_wait);
282 static int add_pid_filter(struct firedtv *fdtv, u8 *operand)
286 for (i = 0, n = 0; i < 16; i++) {
287 if (test_bit(i, &fdtv->channel_active)) {
288 operand[pos++] = 0x13; /* flowfunction relay */
289 operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
290 operand[pos++] = (fdtv->channel_pid[i] >> 8) & 0x1f;
291 operand[pos++] = fdtv->channel_pid[i] & 0xff;
292 operand[pos++] = 0x00; /* tableID */
293 operand[pos++] = 0x00; /* filter_length */
303 * tuning command for setting the relative LNB frequency
304 * (not supported by the AVC standard)
306 static void avc_tuner_tuneqpsk(struct firedtv *fdtv,
307 struct dvb_frontend_parameters *params)
309 struct avc_command_frame *c = (void *)fdtv->avc_data;
311 c->opcode = AVC_OPCODE_VENDOR;
313 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
314 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
315 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
316 if (fdtv->type == FIREDTV_DVB_S2)
317 c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK2;
319 c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK;
321 c->operand[4] = (params->frequency >> 24) & 0xff;
322 c->operand[5] = (params->frequency >> 16) & 0xff;
323 c->operand[6] = (params->frequency >> 8) & 0xff;
324 c->operand[7] = params->frequency & 0xff;
326 c->operand[8] = ((params->u.qpsk.symbol_rate / 1000) >> 8) & 0xff;
327 c->operand[9] = (params->u.qpsk.symbol_rate / 1000) & 0xff;
329 switch (params->u.qpsk.fec_inner) {
330 case FEC_1_2: c->operand[10] = 0x1; break;
331 case FEC_2_3: c->operand[10] = 0x2; break;
332 case FEC_3_4: c->operand[10] = 0x3; break;
333 case FEC_5_6: c->operand[10] = 0x4; break;
334 case FEC_7_8: c->operand[10] = 0x5; break;
338 default: c->operand[10] = 0x0;
341 if (fdtv->voltage == 0xff)
342 c->operand[11] = 0xff;
343 else if (fdtv->voltage == SEC_VOLTAGE_18) /* polarisation */
348 if (fdtv->tone == 0xff)
349 c->operand[12] = 0xff;
350 else if (fdtv->tone == SEC_TONE_ON) /* band */
355 if (fdtv->type == FIREDTV_DVB_S2) {
356 c->operand[13] = 0x1;
357 c->operand[14] = 0xff;
358 c->operand[15] = 0xff;
359 fdtv->avc_data_length = 20;
361 fdtv->avc_data_length = 16;
365 static void avc_tuner_dsd_dvb_c(struct firedtv *fdtv,
366 struct dvb_frontend_parameters *params)
368 struct avc_command_frame *c = (void *)fdtv->avc_data;
370 c->opcode = AVC_OPCODE_DSD;
372 c->operand[0] = 0; /* source plug */
373 c->operand[1] = 0xd2; /* subfunction replace */
374 c->operand[2] = 0x20; /* system id = DVB */
375 c->operand[3] = 0x00; /* antenna number */
376 c->operand[4] = 0x11; /* system_specific_multiplex selection_length */
378 /* multiplex_valid_flags, high byte */
379 c->operand[5] = 0 << 7 /* reserved */
380 | 0 << 6 /* Polarisation */
381 | 0 << 5 /* Orbital_Pos */
382 | 1 << 4 /* Frequency */
383 | 1 << 3 /* Symbol_Rate */
384 | 0 << 2 /* FEC_outer */
385 | (params->u.qam.fec_inner != FEC_AUTO ? 1 << 1 : 0)
386 | (params->u.qam.modulation != QAM_AUTO ? 1 << 0 : 0);
388 /* multiplex_valid_flags, low byte */
389 c->operand[6] = 0 << 7 /* NetworkID */
390 | 0 << 0 /* reserved */ ;
392 c->operand[7] = 0x00;
393 c->operand[8] = 0x00;
394 c->operand[9] = 0x00;
395 c->operand[10] = 0x00;
397 c->operand[11] = (((params->frequency / 4000) >> 16) & 0xff) | (2 << 6);
398 c->operand[12] = ((params->frequency / 4000) >> 8) & 0xff;
399 c->operand[13] = (params->frequency / 4000) & 0xff;
400 c->operand[14] = ((params->u.qpsk.symbol_rate / 1000) >> 12) & 0xff;
401 c->operand[15] = ((params->u.qpsk.symbol_rate / 1000) >> 4) & 0xff;
402 c->operand[16] = ((params->u.qpsk.symbol_rate / 1000) << 4) & 0xf0;
403 c->operand[17] = 0x00;
405 switch (params->u.qpsk.fec_inner) {
406 case FEC_1_2: c->operand[18] = 0x1; break;
407 case FEC_2_3: c->operand[18] = 0x2; break;
408 case FEC_3_4: c->operand[18] = 0x3; break;
409 case FEC_5_6: c->operand[18] = 0x4; break;
410 case FEC_7_8: c->operand[18] = 0x5; break;
411 case FEC_8_9: c->operand[18] = 0x6; break;
412 case FEC_4_5: c->operand[18] = 0x8; break;
414 default: c->operand[18] = 0x0;
417 switch (params->u.qam.modulation) {
418 case QAM_16: c->operand[19] = 0x08; break;
419 case QAM_32: c->operand[19] = 0x10; break;
420 case QAM_64: c->operand[19] = 0x18; break;
421 case QAM_128: c->operand[19] = 0x20; break;
422 case QAM_256: c->operand[19] = 0x28; break;
424 default: c->operand[19] = 0x00;
427 c->operand[20] = 0x00;
428 c->operand[21] = 0x00;
430 /* Add PIDs to filter */
431 fdtv->avc_data_length =
432 ALIGN(22 + add_pid_filter(fdtv, &c->operand[22]) + 3, 4);
435 static void avc_tuner_dsd_dvb_t(struct firedtv *fdtv,
436 struct dvb_frontend_parameters *params)
438 struct dvb_ofdm_parameters *ofdm = ¶ms->u.ofdm;
439 struct avc_command_frame *c = (void *)fdtv->avc_data;
441 c->opcode = AVC_OPCODE_DSD;
443 c->operand[0] = 0; /* source plug */
444 c->operand[1] = 0xd2; /* subfunction replace */
445 c->operand[2] = 0x20; /* system id = DVB */
446 c->operand[3] = 0x00; /* antenna number */
447 c->operand[4] = 0x0c; /* system_specific_multiplex selection_length */
449 /* multiplex_valid_flags, high byte */
451 0 << 7 /* reserved */
452 | 1 << 6 /* CenterFrequency */
453 | (ofdm->bandwidth != BANDWIDTH_AUTO ? 1 << 5 : 0)
454 | (ofdm->constellation != QAM_AUTO ? 1 << 4 : 0)
455 | (ofdm->hierarchy_information != HIERARCHY_AUTO ? 1 << 3 : 0)
456 | (ofdm->code_rate_HP != FEC_AUTO ? 1 << 2 : 0)
457 | (ofdm->code_rate_LP != FEC_AUTO ? 1 << 1 : 0)
458 | (ofdm->guard_interval != GUARD_INTERVAL_AUTO ? 1 << 0 : 0);
460 /* multiplex_valid_flags, low byte */
462 0 << 7 /* NetworkID */
463 | (ofdm->transmission_mode != TRANSMISSION_MODE_AUTO ? 1 << 6 : 0)
464 | 0 << 5 /* OtherFrequencyFlag */
465 | 0 << 0 /* reserved */ ;
468 c->operand[8] = (params->frequency / 10) >> 24;
469 c->operand[9] = ((params->frequency / 10) >> 16) & 0xff;
470 c->operand[10] = ((params->frequency / 10) >> 8) & 0xff;
471 c->operand[11] = (params->frequency / 10) & 0xff;
473 switch (ofdm->bandwidth) {
474 case BANDWIDTH_7_MHZ: c->operand[12] = 0x20; break;
475 case BANDWIDTH_8_MHZ:
476 case BANDWIDTH_6_MHZ: /* not defined by AVC spec */
478 default: c->operand[12] = 0x00;
481 switch (ofdm->constellation) {
482 case QAM_16: c->operand[13] = 1 << 6; break;
483 case QAM_64: c->operand[13] = 2 << 6; break;
485 default: c->operand[13] = 0x00;
488 switch (ofdm->hierarchy_information) {
489 case HIERARCHY_1: c->operand[13] |= 1 << 3; break;
490 case HIERARCHY_2: c->operand[13] |= 2 << 3; break;
491 case HIERARCHY_4: c->operand[13] |= 3 << 3; break;
497 switch (ofdm->code_rate_HP) {
498 case FEC_2_3: c->operand[13] |= 1; break;
499 case FEC_3_4: c->operand[13] |= 2; break;
500 case FEC_5_6: c->operand[13] |= 3; break;
501 case FEC_7_8: c->operand[13] |= 4; break;
506 switch (ofdm->code_rate_LP) {
507 case FEC_2_3: c->operand[14] = 1 << 5; break;
508 case FEC_3_4: c->operand[14] = 2 << 5; break;
509 case FEC_5_6: c->operand[14] = 3 << 5; break;
510 case FEC_7_8: c->operand[14] = 4 << 5; break;
512 default: c->operand[14] = 0x00; break;
515 switch (ofdm->guard_interval) {
516 case GUARD_INTERVAL_1_16: c->operand[14] |= 1 << 3; break;
517 case GUARD_INTERVAL_1_8: c->operand[14] |= 2 << 3; break;
518 case GUARD_INTERVAL_1_4: c->operand[14] |= 3 << 3; break;
519 case GUARD_INTERVAL_1_32:
520 case GUARD_INTERVAL_AUTO:
524 switch (ofdm->transmission_mode) {
525 case TRANSMISSION_MODE_8K: c->operand[14] |= 1 << 1; break;
526 case TRANSMISSION_MODE_2K:
527 case TRANSMISSION_MODE_AUTO:
531 c->operand[15] = 0x00; /* network_ID[0] */
532 c->operand[16] = 0x00; /* network_ID[1] */
534 /* Add PIDs to filter */
535 fdtv->avc_data_length =
536 ALIGN(17 + add_pid_filter(fdtv, &c->operand[17]) + 3, 4);
539 int avc_tuner_dsd(struct firedtv *fdtv,
540 struct dvb_frontend_parameters *params)
542 struct avc_command_frame *c = (void *)fdtv->avc_data;
545 mutex_lock(&fdtv->avc_mutex);
547 memset(c, 0, sizeof(*c));
549 c->ctype = AVC_CTYPE_CONTROL;
550 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
552 switch (fdtv->type) {
554 case FIREDTV_DVB_S2: avc_tuner_tuneqpsk(fdtv, params); break;
555 case FIREDTV_DVB_C: avc_tuner_dsd_dvb_c(fdtv, params); break;
556 case FIREDTV_DVB_T: avc_tuner_dsd_dvb_t(fdtv, params); break;
560 ret = avc_write(fdtv);
564 * u8 *status was an out-parameter of avc_tuner_dsd, unused by caller.
565 * Check for AVC_RESPONSE_ACCEPTED here instead?
568 *status = r->operand[2];
570 mutex_unlock(&fdtv->avc_mutex);
578 int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
580 struct avc_command_frame *c = (void *)fdtv->avc_data;
583 if (pidc > 16 && pidc != 0xff)
586 mutex_lock(&fdtv->avc_mutex);
588 memset(c, 0, sizeof(*c));
590 c->ctype = AVC_CTYPE_CONTROL;
591 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
592 c->opcode = AVC_OPCODE_DSD;
594 c->operand[0] = 0; /* source plug */
595 c->operand[1] = 0xd2; /* subfunction replace */
596 c->operand[2] = 0x20; /* system id = DVB */
597 c->operand[3] = 0x00; /* antenna number */
598 c->operand[4] = 0x00; /* system_specific_multiplex selection_length */
599 c->operand[5] = pidc; /* Nr_of_dsd_sel_specs */
603 for (k = 0; k < pidc; k++) {
604 c->operand[pos++] = 0x13; /* flowfunction relay */
605 c->operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
606 c->operand[pos++] = (pid[k] >> 8) & 0x1f;
607 c->operand[pos++] = pid[k] & 0xff;
608 c->operand[pos++] = 0x00; /* tableID */
609 c->operand[pos++] = 0x00; /* filter_length */
612 fdtv->avc_data_length = ALIGN(3 + pos, 4);
613 ret = avc_write(fdtv);
615 /* FIXME: check response code? */
617 mutex_unlock(&fdtv->avc_mutex);
625 int avc_tuner_get_ts(struct firedtv *fdtv)
627 struct avc_command_frame *c = (void *)fdtv->avc_data;
630 mutex_lock(&fdtv->avc_mutex);
632 memset(c, 0, sizeof(*c));
634 c->ctype = AVC_CTYPE_CONTROL;
635 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
636 c->opcode = AVC_OPCODE_DSIT;
638 sl = fdtv->type == FIREDTV_DVB_T ? 0x0c : 0x11;
640 c->operand[0] = 0; /* source plug */
641 c->operand[1] = 0xd2; /* subfunction replace */
642 c->operand[2] = 0xff; /* status */
643 c->operand[3] = 0x20; /* system id = DVB */
644 c->operand[4] = 0x00; /* antenna number */
645 c->operand[5] = 0x0; /* system_specific_search_flags */
646 c->operand[6] = sl; /* system_specific_multiplex selection_length */
647 c->operand[7] = 0x00; /* valid_flags [0] */
648 c->operand[8] = 0x00; /* valid_flags [1] */
649 c->operand[7 + sl] = 0x00; /* nr_of_dsit_sel_specs (always 0) */
651 fdtv->avc_data_length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
652 ret = avc_write(fdtv);
654 /* FIXME: check response code? */
656 mutex_unlock(&fdtv->avc_mutex);
664 int avc_identify_subunit(struct firedtv *fdtv)
666 struct avc_command_frame *c = (void *)fdtv->avc_data;
667 struct avc_response_frame *r = (void *)fdtv->avc_data;
670 mutex_lock(&fdtv->avc_mutex);
672 memset(c, 0, sizeof(*c));
674 c->ctype = AVC_CTYPE_CONTROL;
675 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
676 c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
678 c->operand[0] = DESCRIPTOR_SUBUNIT_IDENTIFIER;
679 c->operand[1] = 0xff;
680 c->operand[2] = 0x00;
681 c->operand[3] = 0x00; /* length highbyte */
682 c->operand[4] = 0x08; /* length lowbyte */
683 c->operand[5] = 0x00; /* offset highbyte */
684 c->operand[6] = 0x0d; /* offset lowbyte */
686 fdtv->avc_data_length = 12;
687 ret = avc_write(fdtv);
691 if ((r->response != AVC_RESPONSE_STABLE &&
692 r->response != AVC_RESPONSE_ACCEPTED) ||
693 (r->operand[3] << 8) + r->operand[4] != 8) {
694 dev_err(fdtv->device, "cannot read subunit identifier\n");
698 mutex_unlock(&fdtv->avc_mutex);
703 #define SIZEOF_ANTENNA_INPUT_INFO 22
705 int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
707 struct avc_command_frame *c = (void *)fdtv->avc_data;
708 struct avc_response_frame *r = (void *)fdtv->avc_data;
711 mutex_lock(&fdtv->avc_mutex);
713 memset(c, 0, sizeof(*c));
715 c->ctype = AVC_CTYPE_CONTROL;
716 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
717 c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
719 c->operand[0] = DESCRIPTOR_TUNER_STATUS;
720 c->operand[1] = 0xff; /* read_result_status */
721 c->operand[2] = 0x00; /* reserved */
722 c->operand[3] = 0; /* SIZEOF_ANTENNA_INPUT_INFO >> 8; */
723 c->operand[4] = 0; /* SIZEOF_ANTENNA_INPUT_INFO & 0xff; */
724 c->operand[5] = 0x00;
725 c->operand[6] = 0x00;
727 fdtv->avc_data_length = 12;
728 ret = avc_write(fdtv);
732 if (r->response != AVC_RESPONSE_STABLE &&
733 r->response != AVC_RESPONSE_ACCEPTED) {
734 dev_err(fdtv->device, "cannot read tuner status\n");
739 length = r->operand[9];
740 if (r->operand[1] != 0x10 || length != SIZEOF_ANTENNA_INPUT_INFO) {
741 dev_err(fdtv->device, "got invalid tuner status\n");
746 stat->active_system = r->operand[10];
747 stat->searching = r->operand[11] >> 7 & 1;
748 stat->moving = r->operand[11] >> 6 & 1;
749 stat->no_rf = r->operand[11] >> 5 & 1;
750 stat->input = r->operand[12] >> 7 & 1;
751 stat->selected_antenna = r->operand[12] & 0x7f;
752 stat->ber = r->operand[13] << 24 |
753 r->operand[14] << 16 |
754 r->operand[15] << 8 |
756 stat->signal_strength = r->operand[17];
757 stat->raster_frequency = r->operand[18] >> 6 & 2;
758 stat->rf_frequency = (r->operand[18] & 0x3f) << 16 |
759 r->operand[19] << 8 |
761 stat->man_dep_info_length = r->operand[21];
762 stat->front_end_error = r->operand[22] >> 4 & 1;
763 stat->antenna_error = r->operand[22] >> 3 & 1;
764 stat->front_end_power_status = r->operand[22] >> 1 & 1;
765 stat->power_supply = r->operand[22] & 1;
766 stat->carrier_noise_ratio = r->operand[23] << 8 |
768 stat->power_supply_voltage = r->operand[27];
769 stat->antenna_voltage = r->operand[28];
770 stat->firewire_bus_voltage = r->operand[29];
771 stat->ca_mmi = r->operand[30] & 1;
772 stat->ca_pmt_reply = r->operand[31] >> 7 & 1;
773 stat->ca_date_time_request = r->operand[31] >> 6 & 1;
774 stat->ca_application_info = r->operand[31] >> 5 & 1;
775 stat->ca_module_present_status = r->operand[31] >> 4 & 1;
776 stat->ca_dvb_flag = r->operand[31] >> 3 & 1;
777 stat->ca_error_flag = r->operand[31] >> 2 & 1;
778 stat->ca_initialization_status = r->operand[31] >> 1 & 1;
780 mutex_unlock(&fdtv->avc_mutex);
785 int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
786 char conttone, char nrdiseq,
787 struct dvb_diseqc_master_cmd *diseqcmd)
789 struct avc_command_frame *c = (void *)fdtv->avc_data;
790 struct avc_response_frame *r = (void *)fdtv->avc_data;
793 mutex_lock(&fdtv->avc_mutex);
795 memset(c, 0, sizeof(*c));
797 c->ctype = AVC_CTYPE_CONTROL;
798 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
799 c->opcode = AVC_OPCODE_VENDOR;
801 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
802 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
803 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
804 c->operand[3] = SFE_VENDOR_OPCODE_LNB_CONTROL;
806 c->operand[4] = voltage;
807 c->operand[5] = nrdiseq;
811 for (j = 0; j < nrdiseq; j++) {
812 c->operand[i++] = diseqcmd[j].msg_len;
814 for (k = 0; k < diseqcmd[j].msg_len; k++)
815 c->operand[i++] = diseqcmd[j].msg[k];
818 c->operand[i++] = burst;
819 c->operand[i++] = conttone;
821 fdtv->avc_data_length = ALIGN(3 + i, 4);
822 ret = avc_write(fdtv);
826 if (r->response != AVC_RESPONSE_ACCEPTED) {
827 dev_err(fdtv->device, "LNB control failed\n");
831 mutex_unlock(&fdtv->avc_mutex);
836 int avc_register_remote_control(struct firedtv *fdtv)
838 struct avc_command_frame *c = (void *)fdtv->avc_data;
841 mutex_lock(&fdtv->avc_mutex);
843 memset(c, 0, sizeof(*c));
845 c->ctype = AVC_CTYPE_NOTIFY;
846 c->subunit = AVC_SUBUNIT_TYPE_UNIT | 7;
847 c->opcode = AVC_OPCODE_VENDOR;
849 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
850 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
851 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
852 c->operand[3] = SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
854 fdtv->avc_data_length = 8;
855 ret = avc_write(fdtv);
857 /* FIXME: check response code? */
859 mutex_unlock(&fdtv->avc_mutex);
864 void avc_remote_ctrl_work(struct work_struct *work)
866 struct firedtv *fdtv =
867 container_of(work, struct firedtv, remote_ctrl_work);
869 /* Should it be rescheduled in failure cases? */
870 avc_register_remote_control(fdtv);
873 #if 0 /* FIXME: unused */
874 int avc_tuner_host2ca(struct firedtv *fdtv)
876 struct avc_command_frame *c = (void *)fdtv->avc_data;
879 mutex_lock(&fdtv->avc_mutex);
881 memset(c, 0, sizeof(*c));
883 c->ctype = AVC_CTYPE_CONTROL;
884 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
885 c->opcode = AVC_OPCODE_VENDOR;
887 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
888 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
889 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
890 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
891 c->operand[4] = 0; /* slot */
892 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
893 c->operand[6] = 0; /* more/last */
894 c->operand[7] = 0; /* length */
896 fdtv->avc_data_length = 12;
897 ret = avc_write(fdtv);
899 /* FIXME: check response code? */
901 mutex_unlock(&fdtv->avc_mutex);
907 static int get_ca_object_pos(struct avc_response_frame *r)
911 /* Check length of length field */
912 if (r->operand[7] & 0x80)
913 length = (r->operand[7] & 0x7f) + 1;
917 static int get_ca_object_length(struct avc_response_frame *r)
919 #if 0 /* FIXME: unused */
923 if (r->operand[7] & 0x80)
924 for (i = 0; i < (r->operand[7] & 0x7f); i++) {
926 size += r->operand[8 + i];
929 return r->operand[7];
932 int avc_ca_app_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
934 struct avc_command_frame *c = (void *)fdtv->avc_data;
935 struct avc_response_frame *r = (void *)fdtv->avc_data;
938 mutex_lock(&fdtv->avc_mutex);
940 memset(c, 0, sizeof(*c));
942 c->ctype = AVC_CTYPE_STATUS;
943 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
944 c->opcode = AVC_OPCODE_VENDOR;
946 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
947 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
948 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
949 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
950 c->operand[4] = 0; /* slot */
951 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
953 fdtv->avc_data_length = 12;
954 ret = avc_write(fdtv);
958 /* FIXME: check response code and validate response data */
960 pos = get_ca_object_pos(r);
961 app_info[0] = (EN50221_TAG_APP_INFO >> 16) & 0xff;
962 app_info[1] = (EN50221_TAG_APP_INFO >> 8) & 0xff;
963 app_info[2] = (EN50221_TAG_APP_INFO >> 0) & 0xff;
964 app_info[3] = 6 + r->operand[pos + 4];
966 memcpy(&app_info[5], &r->operand[pos], 5 + r->operand[pos + 4]);
967 *len = app_info[3] + 4;
969 mutex_unlock(&fdtv->avc_mutex);
974 int avc_ca_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
976 struct avc_command_frame *c = (void *)fdtv->avc_data;
977 struct avc_response_frame *r = (void *)fdtv->avc_data;
980 mutex_lock(&fdtv->avc_mutex);
982 memset(c, 0, sizeof(*c));
984 c->ctype = AVC_CTYPE_STATUS;
985 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
986 c->opcode = AVC_OPCODE_VENDOR;
988 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
989 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
990 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
991 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
992 c->operand[4] = 0; /* slot */
993 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
995 fdtv->avc_data_length = 12;
996 ret = avc_write(fdtv);
1000 /* FIXME: check response code and validate response data */
1002 pos = get_ca_object_pos(r);
1003 app_info[0] = (EN50221_TAG_CA_INFO >> 16) & 0xff;
1004 app_info[1] = (EN50221_TAG_CA_INFO >> 8) & 0xff;
1005 app_info[2] = (EN50221_TAG_CA_INFO >> 0) & 0xff;
1007 app_info[4] = r->operand[pos + 0];
1008 app_info[5] = r->operand[pos + 1];
1009 *len = app_info[3] + 4;
1011 mutex_unlock(&fdtv->avc_mutex);
1016 int avc_ca_reset(struct firedtv *fdtv)
1018 struct avc_command_frame *c = (void *)fdtv->avc_data;
1021 mutex_lock(&fdtv->avc_mutex);
1023 memset(c, 0, sizeof(*c));
1025 c->ctype = AVC_CTYPE_CONTROL;
1026 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1027 c->opcode = AVC_OPCODE_VENDOR;
1029 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1030 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1031 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1032 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1033 c->operand[4] = 0; /* slot */
1034 c->operand[5] = SFE_VENDOR_TAG_CA_RESET; /* ca tag */
1035 c->operand[6] = 0; /* more/last */
1036 c->operand[7] = 1; /* length */
1037 c->operand[8] = 0; /* force hardware reset */
1039 fdtv->avc_data_length = 12;
1040 ret = avc_write(fdtv);
1042 /* FIXME: check response code? */
1044 mutex_unlock(&fdtv->avc_mutex);
1049 int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
1051 struct avc_command_frame *c = (void *)fdtv->avc_data;
1052 struct avc_response_frame *r = (void *)fdtv->avc_data;
1053 int list_management;
1054 int program_info_length;
1062 if (unlikely(avc_debug & AVC_DEBUG_APPLICATION_PMT))
1063 debug_pmt(msg, length);
1065 mutex_lock(&fdtv->avc_mutex);
1067 memset(c, 0, sizeof(*c));
1069 c->ctype = AVC_CTYPE_CONTROL;
1070 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1071 c->opcode = AVC_OPCODE_VENDOR;
1073 if (msg[0] != EN50221_LIST_MANAGEMENT_ONLY) {
1074 dev_info(fdtv->device, "forcing list_management to ONLY\n");
1075 msg[0] = EN50221_LIST_MANAGEMENT_ONLY;
1077 /* We take the cmd_id from the programme level only! */
1078 list_management = msg[0];
1079 program_info_length = ((msg[4] & 0x0f) << 8) + msg[5];
1080 if (program_info_length > 0)
1081 program_info_length--; /* Remove pmt_cmd_id */
1082 pmt_cmd_id = msg[6];
1084 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1085 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1086 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1087 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1088 c->operand[4] = 0; /* slot */
1089 c->operand[5] = SFE_VENDOR_TAG_CA_PMT; /* ca tag */
1090 c->operand[6] = 0; /* more/last */
1091 /* Use three bytes for length field in case length > 127 */
1092 c->operand[10] = list_management;
1093 c->operand[11] = 0x01; /* pmt_cmd=OK_descramble */
1095 /* TS program map table */
1097 c->operand[12] = 0x02; /* Table id=2 */
1098 c->operand[13] = 0x80; /* Section syntax + length */
1099 /* c->operand[14] = XXXprogram_info_length + 12; */
1100 c->operand[15] = msg[1]; /* Program number */
1101 c->operand[16] = msg[2];
1102 c->operand[17] = 0x01; /* Version number=0 + current/next=1 */
1103 c->operand[18] = 0x00; /* Section number=0 */
1104 c->operand[19] = 0x00; /* Last section number=0 */
1105 c->operand[20] = 0x1f; /* PCR_PID=1FFF */
1106 c->operand[21] = 0xff;
1107 c->operand[22] = (program_info_length >> 8); /* Program info length */
1108 c->operand[23] = (program_info_length & 0xff);
1110 /* CA descriptors at programme level */
1113 if (program_info_length > 0) {
1114 pmt_cmd_id = msg[read_pos++];
1115 if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1116 dev_err(fdtv->device,
1117 "invalid pmt_cmd_id %d\n", pmt_cmd_id);
1119 memcpy(&c->operand[write_pos], &msg[read_pos],
1120 program_info_length);
1121 read_pos += program_info_length;
1122 write_pos += program_info_length;
1124 while (read_pos < length) {
1125 c->operand[write_pos++] = msg[read_pos++];
1126 c->operand[write_pos++] = msg[read_pos++];
1127 c->operand[write_pos++] = msg[read_pos++];
1129 ((msg[read_pos] & 0x0f) << 8) + msg[read_pos + 1];
1131 if (es_info_length > 0)
1132 es_info_length--; /* Remove pmt_cmd_id */
1133 c->operand[write_pos++] = es_info_length >> 8;
1134 c->operand[write_pos++] = es_info_length & 0xff;
1135 if (es_info_length > 0) {
1136 pmt_cmd_id = msg[read_pos++];
1137 if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1138 dev_err(fdtv->device, "invalid pmt_cmd_id %d "
1139 "at stream level\n", pmt_cmd_id);
1141 memcpy(&c->operand[write_pos], &msg[read_pos],
1143 read_pos += es_info_length;
1144 write_pos += es_info_length;
1149 c->operand[write_pos++] = 0x00;
1150 c->operand[write_pos++] = 0x00;
1151 c->operand[write_pos++] = 0x00;
1152 c->operand[write_pos++] = 0x00;
1154 c->operand[7] = 0x82;
1155 c->operand[8] = (write_pos - 10) >> 8;
1156 c->operand[9] = (write_pos - 10) & 0xff;
1157 c->operand[14] = write_pos - 15;
1159 crc32_csum = crc32_be(0, &c->operand[10], c->operand[12] - 1);
1160 c->operand[write_pos - 4] = (crc32_csum >> 24) & 0xff;
1161 c->operand[write_pos - 3] = (crc32_csum >> 16) & 0xff;
1162 c->operand[write_pos - 2] = (crc32_csum >> 8) & 0xff;
1163 c->operand[write_pos - 1] = (crc32_csum >> 0) & 0xff;
1165 fdtv->avc_data_length = ALIGN(3 + write_pos, 4);
1166 ret = avc_write(fdtv);
1170 if (r->response != AVC_RESPONSE_ACCEPTED) {
1171 dev_err(fdtv->device,
1172 "CA PMT failed with response 0x%x\n", r->response);
1176 mutex_unlock(&fdtv->avc_mutex);
1181 int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
1183 struct avc_command_frame *c = (void *)fdtv->avc_data;
1184 struct avc_response_frame *r = (void *)fdtv->avc_data;
1187 mutex_lock(&fdtv->avc_mutex);
1189 memset(c, 0, sizeof(*c));
1191 c->ctype = AVC_CTYPE_STATUS;
1192 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1193 c->opcode = AVC_OPCODE_VENDOR;
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_CA2HOST;
1199 c->operand[4] = 0; /* slot */
1200 c->operand[5] = SFE_VENDOR_TAG_CA_DATE_TIME; /* ca tag */
1201 c->operand[6] = 0; /* more/last */
1202 c->operand[7] = 0; /* length */
1204 fdtv->avc_data_length = 12;
1205 ret = avc_write(fdtv);
1209 /* FIXME: check response code and validate response data */
1211 *interval = r->operand[get_ca_object_pos(r)];
1213 mutex_unlock(&fdtv->avc_mutex);
1218 int avc_ca_enter_menu(struct firedtv *fdtv)
1220 struct avc_command_frame *c = (void *)fdtv->avc_data;
1223 mutex_lock(&fdtv->avc_mutex);
1225 memset(c, 0, sizeof(*c));
1227 c->ctype = AVC_CTYPE_STATUS;
1228 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1229 c->opcode = AVC_OPCODE_VENDOR;
1231 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1232 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1233 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1234 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1235 c->operand[4] = 0; /* slot */
1236 c->operand[5] = SFE_VENDOR_TAG_CA_ENTER_MENU;
1237 c->operand[6] = 0; /* more/last */
1238 c->operand[7] = 0; /* length */
1240 fdtv->avc_data_length = 12;
1241 ret = avc_write(fdtv);
1243 /* FIXME: check response code? */
1245 mutex_unlock(&fdtv->avc_mutex);
1250 int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
1252 struct avc_command_frame *c = (void *)fdtv->avc_data;
1253 struct avc_response_frame *r = (void *)fdtv->avc_data;
1256 mutex_lock(&fdtv->avc_mutex);
1258 memset(c, 0, sizeof(*c));
1260 c->ctype = AVC_CTYPE_STATUS;
1261 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1262 c->opcode = AVC_OPCODE_VENDOR;
1264 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1265 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1266 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1267 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1268 c->operand[4] = 0; /* slot */
1269 c->operand[5] = SFE_VENDOR_TAG_CA_MMI;
1270 c->operand[6] = 0; /* more/last */
1271 c->operand[7] = 0; /* length */
1273 fdtv->avc_data_length = 12;
1274 ret = avc_write(fdtv);
1278 /* FIXME: check response code and validate response data */
1280 *len = get_ca_object_length(r);
1281 memcpy(mmi_object, &r->operand[get_ca_object_pos(r)], *len);
1283 mutex_unlock(&fdtv->avc_mutex);
1288 #define CMP_OUTPUT_PLUG_CONTROL_REG_0 0xfffff0000904ULL
1290 static int cmp_read(struct firedtv *fdtv, u64 addr, __be32 *data)
1294 mutex_lock(&fdtv->avc_mutex);
1296 ret = fdtv->backend->read(fdtv, addr, data);
1298 dev_err(fdtv->device, "CMP: read I/O error\n");
1300 mutex_unlock(&fdtv->avc_mutex);
1305 static int cmp_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
1309 mutex_lock(&fdtv->avc_mutex);
1311 /* data[] is stack-allocated and should not be DMA-mapped. */
1312 memcpy(fdtv->avc_data, data, 8);
1314 ret = fdtv->backend->lock(fdtv, addr, fdtv->avc_data);
1316 dev_err(fdtv->device, "CMP: lock I/O error\n");
1318 memcpy(data, fdtv->avc_data, 8);
1320 mutex_unlock(&fdtv->avc_mutex);
1325 static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
1327 return (be32_to_cpu(opcr) >> shift) & mask;
1330 static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
1332 *opcr &= ~cpu_to_be32(mask << shift);
1333 *opcr |= cpu_to_be32((value & mask) << shift);
1336 #define get_opcr_online(v) get_opcr((v), 0x1, 31)
1337 #define get_opcr_p2p_connections(v) get_opcr((v), 0x3f, 24)
1338 #define get_opcr_channel(v) get_opcr((v), 0x3f, 16)
1340 #define set_opcr_p2p_connections(p, v) set_opcr((p), (v), 0x3f, 24)
1341 #define set_opcr_channel(p, v) set_opcr((p), (v), 0x3f, 16)
1342 #define set_opcr_data_rate(p, v) set_opcr((p), (v), 0x3, 14)
1343 #define set_opcr_overhead_id(p, v) set_opcr((p), (v), 0xf, 10)
1345 int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
1347 __be32 old_opcr, opcr[2];
1348 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1352 ret = cmp_read(fdtv, opcr_address, opcr);
1357 if (!get_opcr_online(*opcr)) {
1358 dev_err(fdtv->device, "CMP: output offline\n");
1364 if (get_opcr_p2p_connections(*opcr)) {
1365 if (get_opcr_channel(*opcr) != channel) {
1366 dev_err(fdtv->device, "CMP: cannot change channel\n");
1369 dev_info(fdtv->device, "CMP: overlaying connection\n");
1371 /* We don't allocate isochronous resources. */
1373 set_opcr_channel(opcr, channel);
1374 set_opcr_data_rate(opcr, 2); /* S400 */
1376 /* FIXME: this is for the worst case - optimize */
1377 set_opcr_overhead_id(opcr, 0);
1380 * FIXME: allocate isochronous channel and bandwidth at IRM
1381 * fdtv->backend->alloc_resources(fdtv, channels_mask, bw);
1385 set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) + 1);
1390 ret = cmp_lock(fdtv, opcr_address, opcr);
1394 if (old_opcr != *opcr) {
1396 * FIXME: if old_opcr.P2P_Connections > 0,
1397 * deallocate isochronous channel and bandwidth at IRM
1399 * fdtv->backend->dealloc_resources(fdtv, channel, bw);
1402 if (++attempts < 6) /* arbitrary limit */
1410 void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
1412 __be32 old_opcr, opcr[2];
1413 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1416 if (cmp_read(fdtv, opcr_address, opcr) < 0)
1420 if (!get_opcr_online(*opcr) || !get_opcr_p2p_connections(*opcr) ||
1421 get_opcr_channel(*opcr) != channel) {
1422 dev_err(fdtv->device, "CMP: no connection to break\n");
1427 set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) - 1);
1432 if (cmp_lock(fdtv, opcr_address, opcr) < 0)
1435 if (old_opcr != *opcr) {
1437 * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
1438 * owner, deallocate isochronous channel and bandwidth at IRM
1440 * fdtv->backend->dealloc_resources(fdtv, channel, bw);
1443 if (++attempts < 6) /* arbitrary limit */