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