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