blob: 28294af752dbb7aea16ed95fb895d79d11863c3a [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 {
Stefan Richter15490792009-02-23 14:21:10 +010077 u8 ctype;
78 u8 subunit;
79 u8 opcode;
80 u8 operand[509];
81};
82
83struct avc_response_frame {
Stefan Richter15490792009-02-23 14:21:10 +010084 u8 response;
85 u8 subunit;
86 u8 opcode;
87 u8 operand[509];
88};
89
Stefan Richter1e4348c2009-11-18 16:03:31 -030090#define LAST_OPERAND (509 - 1)
91
92static inline void clear_operands(struct avc_command_frame *c, int from, int to)
93{
94 memset(&c->operand[from], 0, to - from + 1);
95}
96
97static void pad_operands(struct avc_command_frame *c, int from)
98{
99 int to = ALIGN(from, 4);
100
101 if (from <= to && to <= LAST_OPERAND)
102 clear_operands(c, from, to);
103}
104
Henrik Kurelid15344772009-08-01 08:04:06 -0300105#define AVC_DEBUG_READ_DESCRIPTOR 0x0001
106#define AVC_DEBUG_DSIT 0x0002
107#define AVC_DEBUG_DSD 0x0004
108#define AVC_DEBUG_REGISTER_REMOTE_CONTROL 0x0008
109#define AVC_DEBUG_LNB_CONTROL 0x0010
110#define AVC_DEBUG_TUNE_QPSK 0x0020
111#define AVC_DEBUG_TUNE_QPSK2 0x0040
112#define AVC_DEBUG_HOST2CA 0x0080
113#define AVC_DEBUG_CA2HOST 0x0100
114#define AVC_DEBUG_APPLICATION_PMT 0x4000
115#define AVC_DEBUG_FCP_PAYLOADS 0x8000
Stefan Richter15490792009-02-23 14:21:10 +0100116
117static int avc_debug;
118module_param_named(debug, avc_debug, int, 0644);
Stefan Richter96e242a2009-08-01 08:05:16 -0300119MODULE_PARM_DESC(debug, "Verbose logging (none = 0"
120 ", FCP subactions"
121 ": READ DESCRIPTOR = " __stringify(AVC_DEBUG_READ_DESCRIPTOR)
122 ", DSIT = " __stringify(AVC_DEBUG_DSIT)
123 ", REGISTER_REMOTE_CONTROL = " __stringify(AVC_DEBUG_REGISTER_REMOTE_CONTROL)
124 ", LNB CONTROL = " __stringify(AVC_DEBUG_LNB_CONTROL)
125 ", TUNE QPSK = " __stringify(AVC_DEBUG_TUNE_QPSK)
126 ", TUNE QPSK2 = " __stringify(AVC_DEBUG_TUNE_QPSK2)
127 ", HOST2CA = " __stringify(AVC_DEBUG_HOST2CA)
128 ", CA2HOST = " __stringify(AVC_DEBUG_CA2HOST)
129 "; Application sent PMT = " __stringify(AVC_DEBUG_APPLICATION_PMT)
130 ", FCP payloads = " __stringify(AVC_DEBUG_FCP_PAYLOADS)
131 ", or a combination, or all = -1)");
Stefan Richter15490792009-02-23 14:21:10 +0100132
133static const char *debug_fcp_ctype(unsigned int ctype)
134{
135 static const char *ctypes[] = {
136 [0x0] = "CONTROL", [0x1] = "STATUS",
137 [0x2] = "SPECIFIC INQUIRY", [0x3] = "NOTIFY",
138 [0x4] = "GENERAL INQUIRY", [0x8] = "NOT IMPLEMENTED",
139 [0x9] = "ACCEPTED", [0xa] = "REJECTED",
140 [0xb] = "IN TRANSITION", [0xc] = "IMPLEMENTED/STABLE",
141 [0xd] = "CHANGED", [0xf] = "INTERIM",
142 };
143 const char *ret = ctype < ARRAY_SIZE(ctypes) ? ctypes[ctype] : NULL;
144
145 return ret ? ret : "?";
146}
147
148static const char *debug_fcp_opcode(unsigned int opcode,
Stefan Richter40cf65d2009-03-05 19:13:43 +0100149 const u8 *data, int length)
Stefan Richter15490792009-02-23 14:21:10 +0100150{
151 switch (opcode) {
Stefan Richter96e242a2009-08-01 08:05:16 -0300152 case AVC_OPCODE_VENDOR:
153 break;
154 case AVC_OPCODE_READ_DESCRIPTOR:
155 return avc_debug & AVC_DEBUG_READ_DESCRIPTOR ?
156 "ReadDescriptor" : NULL;
157 case AVC_OPCODE_DSIT:
158 return avc_debug & AVC_DEBUG_DSIT ?
159 "DirectSelectInfo.Type" : NULL;
160 case AVC_OPCODE_DSD:
161 return avc_debug & AVC_DEBUG_DSD ? "DirectSelectData" : NULL;
162 default:
163 return "Unknown";
Stefan Richter15490792009-02-23 14:21:10 +0100164 }
165
166 if (length < 7 ||
167 data[3] != SFE_VENDOR_DE_COMPANYID_0 ||
168 data[4] != SFE_VENDOR_DE_COMPANYID_1 ||
169 data[5] != SFE_VENDOR_DE_COMPANYID_2)
Stefan Richter96e242a2009-08-01 08:05:16 -0300170 return "Vendor/Unknown";
Stefan Richter15490792009-02-23 14:21:10 +0100171
172 switch (data[6]) {
Stefan Richter96e242a2009-08-01 08:05:16 -0300173 case SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL:
174 return avc_debug & AVC_DEBUG_REGISTER_REMOTE_CONTROL ?
175 "RegisterRC" : NULL;
176 case SFE_VENDOR_OPCODE_LNB_CONTROL:
177 return avc_debug & AVC_DEBUG_LNB_CONTROL ? "LNBControl" : NULL;
178 case SFE_VENDOR_OPCODE_TUNE_QPSK:
179 return avc_debug & AVC_DEBUG_TUNE_QPSK ? "TuneQPSK" : NULL;
180 case SFE_VENDOR_OPCODE_TUNE_QPSK2:
181 return avc_debug & AVC_DEBUG_TUNE_QPSK2 ? "TuneQPSK2" : NULL;
182 case SFE_VENDOR_OPCODE_HOST2CA:
183 return avc_debug & AVC_DEBUG_HOST2CA ? "Host2CA" : NULL;
184 case SFE_VENDOR_OPCODE_CA2HOST:
185 return avc_debug & AVC_DEBUG_CA2HOST ? "CA2Host" : NULL;
Stefan Richter15490792009-02-23 14:21:10 +0100186 }
Stefan Richter96e242a2009-08-01 08:05:16 -0300187 return "Vendor/Unknown";
Henrik Kurelid15344772009-08-01 08:04:06 -0300188}
189
Stefan Richter40cf65d2009-03-05 19:13:43 +0100190static void debug_fcp(const u8 *data, int length)
Stefan Richter15490792009-02-23 14:21:10 +0100191{
Stefan Richter96e242a2009-08-01 08:05:16 -0300192 unsigned int subunit_type, subunit_id, opcode;
193 const char *op, *prefix;
Stefan Richter15490792009-02-23 14:21:10 +0100194
Stefan Richter96e242a2009-08-01 08:05:16 -0300195 prefix = data[0] > 7 ? "FCP <- " : "FCP -> ";
Henrik Kurelid15344772009-08-01 08:04:06 -0300196 subunit_type = data[1] >> 3;
Stefan Richter96e242a2009-08-01 08:05:16 -0300197 subunit_id = data[1] & 7;
198 opcode = subunit_type == 0x1e || subunit_id == 5 ? ~0 : data[2];
199 op = debug_fcp_opcode(opcode, data, length);
200
201 if (op) {
Stefan Richter14edcd52009-04-01 17:25:00 -0300202 printk(KERN_INFO "%ssu=%x.%x l=%d: %-8s - %s\n",
Stefan Richter15490792009-02-23 14:21:10 +0100203 prefix, subunit_type, subunit_id, length,
Stefan Richter96e242a2009-08-01 08:05:16 -0300204 debug_fcp_ctype(data[0]), op);
Henrik Kurelid15344772009-08-01 08:04:06 -0300205 if (avc_debug & AVC_DEBUG_FCP_PAYLOADS)
206 print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_NONE,
207 16, 1, data, length, false);
Stefan Richter15490792009-02-23 14:21:10 +0100208 }
Henrik Kurelid15344772009-08-01 08:04:06 -0300209}
Stefan Richter15490792009-02-23 14:21:10 +0100210
Henrik Kurelid15344772009-08-01 08:04:06 -0300211static void debug_pmt(char *msg, int length)
212{
213 printk(KERN_INFO "APP PMT -> l=%d\n", length);
214 print_hex_dump(KERN_INFO, "APP PMT -> ", DUMP_PREFIX_NONE,
215 16, 1, msg, length, false);
Stefan Richter15490792009-02-23 14:21:10 +0100216}
217
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300218static int avc_write(struct firedtv *fdtv)
Stefan Richter15490792009-02-23 14:21:10 +0100219{
220 int err, retry;
221
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300222 fdtv->avc_reply_received = false;
Stefan Richter15490792009-02-23 14:21:10 +0100223
224 for (retry = 0; retry < 6; retry++) {
225 if (unlikely(avc_debug))
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300226 debug_fcp(fdtv->avc_data, fdtv->avc_data_length);
Stefan Richter15490792009-02-23 14:21:10 +0100227
228 err = fdtv->backend->write(fdtv, FCP_COMMAND_REGISTER,
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300229 fdtv->avc_data, fdtv->avc_data_length);
Stefan Richter15490792009-02-23 14:21:10 +0100230 if (err) {
Stefan Richter15490792009-02-23 14:21:10 +0100231 dev_err(fdtv->device, "FCP command write failed\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300232
Stefan Richter15490792009-02-23 14:21:10 +0100233 return err;
234 }
235
Stefan Richter15490792009-02-23 14:21:10 +0100236 /*
237 * AV/C specs say that answers should be sent within 150 ms.
238 * Time out after 200 ms.
239 */
240 if (wait_event_timeout(fdtv->avc_wait,
241 fdtv->avc_reply_received,
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300242 msecs_to_jiffies(200)) != 0)
Stefan Richter15490792009-02-23 14:21:10 +0100243 return 0;
Stefan Richter15490792009-02-23 14:21:10 +0100244 }
245 dev_err(fdtv->device, "FCP response timed out\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300246
Stefan Richter15490792009-02-23 14:21:10 +0100247 return -ETIMEDOUT;
248}
249
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300250static bool is_register_rc(struct avc_response_frame *r)
Stefan Richter15490792009-02-23 14:21:10 +0100251{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300252 return r->opcode == AVC_OPCODE_VENDOR &&
253 r->operand[0] == SFE_VENDOR_DE_COMPANYID_0 &&
254 r->operand[1] == SFE_VENDOR_DE_COMPANYID_1 &&
255 r->operand[2] == SFE_VENDOR_DE_COMPANYID_2 &&
256 r->operand[3] == SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
Stefan Richter15490792009-02-23 14:21:10 +0100257}
258
259int avc_recv(struct firedtv *fdtv, void *data, size_t length)
260{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300261 struct avc_response_frame *r = data;
Stefan Richter15490792009-02-23 14:21:10 +0100262
263 if (unlikely(avc_debug))
264 debug_fcp(data, length);
265
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300266 if (length >= 8 && is_register_rc(r)) {
267 switch (r->response) {
268 case AVC_RESPONSE_CHANGED:
269 fdtv_handle_rc(fdtv, r->operand[4] << 8 | r->operand[5]);
Stefan Richter15490792009-02-23 14:21:10 +0100270 schedule_work(&fdtv->remote_ctrl_work);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300271 break;
272 case AVC_RESPONSE_INTERIM:
273 if (is_register_rc((void *)fdtv->avc_data))
274 goto wake;
275 break;
276 default:
Stefan Richter15490792009-02-23 14:21:10 +0100277 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
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300288 memcpy(fdtv->avc_data, data, length);
289 fdtv->avc_data_length = length;
290wake:
Stefan Richter15490792009-02-23 14:21:10 +0100291 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 */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300321static int avc_tuner_tuneqpsk(struct firedtv *fdtv,
322 struct dvb_frontend_parameters *params)
Stefan Richter15490792009-02-23 14:21:10 +0100323{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300324 struct avc_command_frame *c = (void *)fdtv->avc_data;
325
Stefan Richter15490792009-02-23 14:21:10 +0100326 c->opcode = AVC_OPCODE_VENDOR;
327
328 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
329 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
330 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
Beat Michel Liechti32a0f482009-03-26 22:36:52 +0100331 if (fdtv->type == FIREDTV_DVB_S2)
332 c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK2;
333 else
334 c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK;
Stefan Richter15490792009-02-23 14:21:10 +0100335
336 c->operand[4] = (params->frequency >> 24) & 0xff;
337 c->operand[5] = (params->frequency >> 16) & 0xff;
338 c->operand[6] = (params->frequency >> 8) & 0xff;
339 c->operand[7] = params->frequency & 0xff;
340
341 c->operand[8] = ((params->u.qpsk.symbol_rate / 1000) >> 8) & 0xff;
342 c->operand[9] = (params->u.qpsk.symbol_rate / 1000) & 0xff;
343
344 switch (params->u.qpsk.fec_inner) {
345 case FEC_1_2: c->operand[10] = 0x1; break;
346 case FEC_2_3: c->operand[10] = 0x2; break;
347 case FEC_3_4: c->operand[10] = 0x3; break;
348 case FEC_5_6: c->operand[10] = 0x4; break;
349 case FEC_7_8: c->operand[10] = 0x5; break;
350 case FEC_4_5:
351 case FEC_8_9:
352 case FEC_AUTO:
353 default: c->operand[10] = 0x0;
354 }
355
356 if (fdtv->voltage == 0xff)
357 c->operand[11] = 0xff;
358 else if (fdtv->voltage == SEC_VOLTAGE_18) /* polarisation */
359 c->operand[11] = 0;
360 else
361 c->operand[11] = 1;
362
363 if (fdtv->tone == 0xff)
364 c->operand[12] = 0xff;
365 else if (fdtv->tone == SEC_TONE_ON) /* band */
366 c->operand[12] = 1;
367 else
368 c->operand[12] = 0;
369
370 if (fdtv->type == FIREDTV_DVB_S2) {
371 c->operand[13] = 0x1;
372 c->operand[14] = 0xff;
373 c->operand[15] = 0xff;
Stefan Richter1e4348c2009-11-18 16:03:31 -0300374
375 return 16;
Stefan Richter15490792009-02-23 14:21:10 +0100376 } else {
Stefan Richter1e4348c2009-11-18 16:03:31 -0300377 return 13;
Stefan Richter15490792009-02-23 14:21:10 +0100378 }
379}
380
Stefan Richter1e4348c2009-11-18 16:03:31 -0300381static int avc_tuner_dsd_dvb_c(struct firedtv *fdtv,
382 struct dvb_frontend_parameters *params)
Stefan Richter15490792009-02-23 14:21:10 +0100383{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300384 struct avc_command_frame *c = (void *)fdtv->avc_data;
385
Stefan Richter15490792009-02-23 14:21:10 +0100386 c->opcode = AVC_OPCODE_DSD;
387
388 c->operand[0] = 0; /* source plug */
389 c->operand[1] = 0xd2; /* subfunction replace */
390 c->operand[2] = 0x20; /* system id = DVB */
391 c->operand[3] = 0x00; /* antenna number */
392 c->operand[4] = 0x11; /* system_specific_multiplex selection_length */
393
394 /* multiplex_valid_flags, high byte */
395 c->operand[5] = 0 << 7 /* reserved */
396 | 0 << 6 /* Polarisation */
397 | 0 << 5 /* Orbital_Pos */
398 | 1 << 4 /* Frequency */
399 | 1 << 3 /* Symbol_Rate */
400 | 0 << 2 /* FEC_outer */
401 | (params->u.qam.fec_inner != FEC_AUTO ? 1 << 1 : 0)
402 | (params->u.qam.modulation != QAM_AUTO ? 1 << 0 : 0);
403
404 /* multiplex_valid_flags, low byte */
405 c->operand[6] = 0 << 7 /* NetworkID */
406 | 0 << 0 /* reserved */ ;
407
408 c->operand[7] = 0x00;
409 c->operand[8] = 0x00;
410 c->operand[9] = 0x00;
411 c->operand[10] = 0x00;
412
413 c->operand[11] = (((params->frequency / 4000) >> 16) & 0xff) | (2 << 6);
414 c->operand[12] = ((params->frequency / 4000) >> 8) & 0xff;
415 c->operand[13] = (params->frequency / 4000) & 0xff;
416 c->operand[14] = ((params->u.qpsk.symbol_rate / 1000) >> 12) & 0xff;
417 c->operand[15] = ((params->u.qpsk.symbol_rate / 1000) >> 4) & 0xff;
418 c->operand[16] = ((params->u.qpsk.symbol_rate / 1000) << 4) & 0xf0;
419 c->operand[17] = 0x00;
420
421 switch (params->u.qpsk.fec_inner) {
422 case FEC_1_2: c->operand[18] = 0x1; break;
423 case FEC_2_3: c->operand[18] = 0x2; break;
424 case FEC_3_4: c->operand[18] = 0x3; break;
425 case FEC_5_6: c->operand[18] = 0x4; break;
426 case FEC_7_8: c->operand[18] = 0x5; break;
427 case FEC_8_9: c->operand[18] = 0x6; break;
428 case FEC_4_5: c->operand[18] = 0x8; break;
429 case FEC_AUTO:
430 default: c->operand[18] = 0x0;
431 }
432
433 switch (params->u.qam.modulation) {
434 case QAM_16: c->operand[19] = 0x08; break;
435 case QAM_32: c->operand[19] = 0x10; break;
436 case QAM_64: c->operand[19] = 0x18; break;
437 case QAM_128: c->operand[19] = 0x20; break;
438 case QAM_256: c->operand[19] = 0x28; break;
439 case QAM_AUTO:
440 default: c->operand[19] = 0x00;
441 }
442
443 c->operand[20] = 0x00;
444 c->operand[21] = 0x00;
Stefan Richter15490792009-02-23 14:21:10 +0100445
Stefan Richter1e4348c2009-11-18 16:03:31 -0300446 return 22 + add_pid_filter(fdtv, &c->operand[22]);
Stefan Richter15490792009-02-23 14:21:10 +0100447}
448
Stefan Richter1e4348c2009-11-18 16:03:31 -0300449static int avc_tuner_dsd_dvb_t(struct firedtv *fdtv,
450 struct dvb_frontend_parameters *params)
Stefan Richter15490792009-02-23 14:21:10 +0100451{
452 struct dvb_ofdm_parameters *ofdm = &params->u.ofdm;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300453 struct avc_command_frame *c = (void *)fdtv->avc_data;
Stefan Richter15490792009-02-23 14:21:10 +0100454
455 c->opcode = AVC_OPCODE_DSD;
456
457 c->operand[0] = 0; /* source plug */
458 c->operand[1] = 0xd2; /* subfunction replace */
459 c->operand[2] = 0x20; /* system id = DVB */
460 c->operand[3] = 0x00; /* antenna number */
461 c->operand[4] = 0x0c; /* system_specific_multiplex selection_length */
462
463 /* multiplex_valid_flags, high byte */
464 c->operand[5] =
465 0 << 7 /* reserved */
466 | 1 << 6 /* CenterFrequency */
467 | (ofdm->bandwidth != BANDWIDTH_AUTO ? 1 << 5 : 0)
468 | (ofdm->constellation != QAM_AUTO ? 1 << 4 : 0)
469 | (ofdm->hierarchy_information != HIERARCHY_AUTO ? 1 << 3 : 0)
470 | (ofdm->code_rate_HP != FEC_AUTO ? 1 << 2 : 0)
471 | (ofdm->code_rate_LP != FEC_AUTO ? 1 << 1 : 0)
472 | (ofdm->guard_interval != GUARD_INTERVAL_AUTO ? 1 << 0 : 0);
473
474 /* multiplex_valid_flags, low byte */
475 c->operand[6] =
476 0 << 7 /* NetworkID */
477 | (ofdm->transmission_mode != TRANSMISSION_MODE_AUTO ? 1 << 6 : 0)
478 | 0 << 5 /* OtherFrequencyFlag */
479 | 0 << 0 /* reserved */ ;
480
481 c->operand[7] = 0x0;
482 c->operand[8] = (params->frequency / 10) >> 24;
483 c->operand[9] = ((params->frequency / 10) >> 16) & 0xff;
484 c->operand[10] = ((params->frequency / 10) >> 8) & 0xff;
485 c->operand[11] = (params->frequency / 10) & 0xff;
486
487 switch (ofdm->bandwidth) {
488 case BANDWIDTH_7_MHZ: c->operand[12] = 0x20; break;
489 case BANDWIDTH_8_MHZ:
490 case BANDWIDTH_6_MHZ: /* not defined by AVC spec */
491 case BANDWIDTH_AUTO:
492 default: c->operand[12] = 0x00;
493 }
494
495 switch (ofdm->constellation) {
496 case QAM_16: c->operand[13] = 1 << 6; break;
497 case QAM_64: c->operand[13] = 2 << 6; break;
498 case QPSK:
499 default: c->operand[13] = 0x00;
500 }
501
502 switch (ofdm->hierarchy_information) {
503 case HIERARCHY_1: c->operand[13] |= 1 << 3; break;
504 case HIERARCHY_2: c->operand[13] |= 2 << 3; break;
505 case HIERARCHY_4: c->operand[13] |= 3 << 3; break;
506 case HIERARCHY_AUTO:
507 case HIERARCHY_NONE:
508 default: break;
509 }
510
511 switch (ofdm->code_rate_HP) {
512 case FEC_2_3: c->operand[13] |= 1; break;
513 case FEC_3_4: c->operand[13] |= 2; break;
514 case FEC_5_6: c->operand[13] |= 3; break;
515 case FEC_7_8: c->operand[13] |= 4; break;
516 case FEC_1_2:
517 default: break;
518 }
519
520 switch (ofdm->code_rate_LP) {
521 case FEC_2_3: c->operand[14] = 1 << 5; break;
522 case FEC_3_4: c->operand[14] = 2 << 5; break;
523 case FEC_5_6: c->operand[14] = 3 << 5; break;
524 case FEC_7_8: c->operand[14] = 4 << 5; break;
525 case FEC_1_2:
526 default: c->operand[14] = 0x00; break;
527 }
528
529 switch (ofdm->guard_interval) {
530 case GUARD_INTERVAL_1_16: c->operand[14] |= 1 << 3; break;
531 case GUARD_INTERVAL_1_8: c->operand[14] |= 2 << 3; break;
532 case GUARD_INTERVAL_1_4: c->operand[14] |= 3 << 3; break;
533 case GUARD_INTERVAL_1_32:
534 case GUARD_INTERVAL_AUTO:
535 default: break;
536 }
537
538 switch (ofdm->transmission_mode) {
539 case TRANSMISSION_MODE_8K: c->operand[14] |= 1 << 1; break;
540 case TRANSMISSION_MODE_2K:
541 case TRANSMISSION_MODE_AUTO:
542 default: break;
543 }
544
545 c->operand[15] = 0x00; /* network_ID[0] */
546 c->operand[16] = 0x00; /* network_ID[1] */
Stefan Richter15490792009-02-23 14:21:10 +0100547
Stefan Richter1e4348c2009-11-18 16:03:31 -0300548 return 17 + add_pid_filter(fdtv, &c->operand[17]);
Stefan Richter15490792009-02-23 14:21:10 +0100549}
550
551int avc_tuner_dsd(struct firedtv *fdtv,
552 struct dvb_frontend_parameters *params)
553{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300554 struct avc_command_frame *c = (void *)fdtv->avc_data;
Stefan Richter1e4348c2009-11-18 16:03:31 -0300555 int pos, ret;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300556
Stefan Richter6385c5b2009-11-18 16:03:03 -0300557 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100558
Stefan Richter15490792009-02-23 14:21:10 +0100559 c->ctype = AVC_CTYPE_CONTROL;
560 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
561
562 switch (fdtv->type) {
563 case FIREDTV_DVB_S:
Stefan Richter1e4348c2009-11-18 16:03:31 -0300564 case FIREDTV_DVB_S2: pos = avc_tuner_tuneqpsk(fdtv, params); break;
565 case FIREDTV_DVB_C: pos = avc_tuner_dsd_dvb_c(fdtv, params); break;
566 case FIREDTV_DVB_T: pos = avc_tuner_dsd_dvb_t(fdtv, params); break;
Stefan Richter15490792009-02-23 14:21:10 +0100567 default:
568 BUG();
569 }
Stefan Richter1e4348c2009-11-18 16:03:31 -0300570 pad_operands(c, pos);
571
572 fdtv->avc_data_length = ALIGN(3 + pos, 4);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300573 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100574#if 0
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300575 /*
576 * FIXME:
577 * u8 *status was an out-parameter of avc_tuner_dsd, unused by caller.
578 * Check for AVC_RESPONSE_ACCEPTED here instead?
579 */
Stefan Richter15490792009-02-23 14:21:10 +0100580 if (status)
581 *status = r->operand[2];
582#endif
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300583 mutex_unlock(&fdtv->avc_mutex);
584
585 if (ret == 0)
586 msleep(500);
587
588 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100589}
590
591int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
592{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300593 struct avc_command_frame *c = (void *)fdtv->avc_data;
594 int ret, pos, k;
Stefan Richter15490792009-02-23 14:21:10 +0100595
596 if (pidc > 16 && pidc != 0xff)
597 return -EINVAL;
598
Stefan Richter6385c5b2009-11-18 16:03:03 -0300599 mutex_lock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300600
Stefan Richter15490792009-02-23 14:21:10 +0100601 c->ctype = AVC_CTYPE_CONTROL;
602 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
603 c->opcode = AVC_OPCODE_DSD;
604
605 c->operand[0] = 0; /* source plug */
606 c->operand[1] = 0xd2; /* subfunction replace */
607 c->operand[2] = 0x20; /* system id = DVB */
608 c->operand[3] = 0x00; /* antenna number */
609 c->operand[4] = 0x00; /* system_specific_multiplex selection_length */
610 c->operand[5] = pidc; /* Nr_of_dsd_sel_specs */
611
612 pos = 6;
613 if (pidc != 0xff)
614 for (k = 0; k < pidc; k++) {
615 c->operand[pos++] = 0x13; /* flowfunction relay */
616 c->operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
617 c->operand[pos++] = (pid[k] >> 8) & 0x1f;
618 c->operand[pos++] = pid[k] & 0xff;
619 c->operand[pos++] = 0x00; /* tableID */
620 c->operand[pos++] = 0x00; /* filter_length */
621 }
Stefan Richter1e4348c2009-11-18 16:03:31 -0300622 pad_operands(c, pos);
Stefan Richter15490792009-02-23 14:21:10 +0100623
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300624 fdtv->avc_data_length = ALIGN(3 + pos, 4);
625 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100626
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300627 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +0100628
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300629 mutex_unlock(&fdtv->avc_mutex);
630
631 if (ret == 0)
632 msleep(50);
633
634 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100635}
636
637int avc_tuner_get_ts(struct firedtv *fdtv)
638{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300639 struct avc_command_frame *c = (void *)fdtv->avc_data;
640 int ret, sl;
641
Stefan Richter6385c5b2009-11-18 16:03:03 -0300642 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100643
Stefan Richter15490792009-02-23 14:21:10 +0100644 c->ctype = AVC_CTYPE_CONTROL;
645 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
646 c->opcode = AVC_OPCODE_DSIT;
647
648 sl = fdtv->type == FIREDTV_DVB_T ? 0x0c : 0x11;
649
650 c->operand[0] = 0; /* source plug */
651 c->operand[1] = 0xd2; /* subfunction replace */
652 c->operand[2] = 0xff; /* status */
653 c->operand[3] = 0x20; /* system id = DVB */
654 c->operand[4] = 0x00; /* antenna number */
655 c->operand[5] = 0x0; /* system_specific_search_flags */
656 c->operand[6] = sl; /* system_specific_multiplex selection_length */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300657 /*
658 * operand[7]: valid_flags[0]
659 * operand[8]: valid_flags[1]
660 * operand[7 + sl]: nr_of_dsit_sel_specs (always 0)
661 */
662 clear_operands(c, 7, 24);
Stefan Richter15490792009-02-23 14:21:10 +0100663
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300664 fdtv->avc_data_length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
665 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100666
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300667 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +0100668
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300669 mutex_unlock(&fdtv->avc_mutex);
670
671 if (ret == 0)
672 msleep(250);
673
674 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100675}
676
677int avc_identify_subunit(struct firedtv *fdtv)
678{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300679 struct avc_command_frame *c = (void *)fdtv->avc_data;
680 struct avc_response_frame *r = (void *)fdtv->avc_data;
681 int ret;
682
Stefan Richter6385c5b2009-11-18 16:03:03 -0300683 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100684
Stefan Richter15490792009-02-23 14:21:10 +0100685 c->ctype = AVC_CTYPE_CONTROL;
686 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
687 c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
688
689 c->operand[0] = DESCRIPTOR_SUBUNIT_IDENTIFIER;
690 c->operand[1] = 0xff;
691 c->operand[2] = 0x00;
692 c->operand[3] = 0x00; /* length highbyte */
693 c->operand[4] = 0x08; /* length lowbyte */
694 c->operand[5] = 0x00; /* offset highbyte */
695 c->operand[6] = 0x0d; /* offset lowbyte */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300696 clear_operands(c, 7, 8); /* padding */
Stefan Richter15490792009-02-23 14:21:10 +0100697
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300698 fdtv->avc_data_length = 12;
699 ret = avc_write(fdtv);
700 if (ret < 0)
701 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100702
703 if ((r->response != AVC_RESPONSE_STABLE &&
704 r->response != AVC_RESPONSE_ACCEPTED) ||
705 (r->operand[3] << 8) + r->operand[4] != 8) {
706 dev_err(fdtv->device, "cannot read subunit identifier\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300707 ret = -EINVAL;
Stefan Richter15490792009-02-23 14:21:10 +0100708 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300709out:
710 mutex_unlock(&fdtv->avc_mutex);
711
712 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100713}
714
715#define SIZEOF_ANTENNA_INPUT_INFO 22
716
717int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
718{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300719 struct avc_command_frame *c = (void *)fdtv->avc_data;
720 struct avc_response_frame *r = (void *)fdtv->avc_data;
721 int length, ret;
722
Stefan Richter6385c5b2009-11-18 16:03:03 -0300723 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100724
Stefan Richter15490792009-02-23 14:21:10 +0100725 c->ctype = AVC_CTYPE_CONTROL;
726 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
727 c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
728
729 c->operand[0] = DESCRIPTOR_TUNER_STATUS;
730 c->operand[1] = 0xff; /* read_result_status */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300731 /*
732 * operand[2]: reserved
733 * operand[3]: SIZEOF_ANTENNA_INPUT_INFO >> 8
734 * operand[4]: SIZEOF_ANTENNA_INPUT_INFO & 0xff
735 */
736 clear_operands(c, 2, 31);
Stefan Richter15490792009-02-23 14:21:10 +0100737
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300738 fdtv->avc_data_length = 12;
739 ret = avc_write(fdtv);
740 if (ret < 0)
741 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100742
743 if (r->response != AVC_RESPONSE_STABLE &&
744 r->response != AVC_RESPONSE_ACCEPTED) {
745 dev_err(fdtv->device, "cannot read tuner status\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300746 ret = -EINVAL;
747 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100748 }
749
750 length = r->operand[9];
751 if (r->operand[1] != 0x10 || length != SIZEOF_ANTENNA_INPUT_INFO) {
752 dev_err(fdtv->device, "got invalid tuner status\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300753 ret = -EINVAL;
754 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100755 }
756
757 stat->active_system = r->operand[10];
758 stat->searching = r->operand[11] >> 7 & 1;
759 stat->moving = r->operand[11] >> 6 & 1;
760 stat->no_rf = r->operand[11] >> 5 & 1;
761 stat->input = r->operand[12] >> 7 & 1;
762 stat->selected_antenna = r->operand[12] & 0x7f;
763 stat->ber = r->operand[13] << 24 |
764 r->operand[14] << 16 |
765 r->operand[15] << 8 |
766 r->operand[16];
767 stat->signal_strength = r->operand[17];
768 stat->raster_frequency = r->operand[18] >> 6 & 2;
769 stat->rf_frequency = (r->operand[18] & 0x3f) << 16 |
770 r->operand[19] << 8 |
771 r->operand[20];
772 stat->man_dep_info_length = r->operand[21];
773 stat->front_end_error = r->operand[22] >> 4 & 1;
774 stat->antenna_error = r->operand[22] >> 3 & 1;
775 stat->front_end_power_status = r->operand[22] >> 1 & 1;
776 stat->power_supply = r->operand[22] & 1;
777 stat->carrier_noise_ratio = r->operand[23] << 8 |
778 r->operand[24];
779 stat->power_supply_voltage = r->operand[27];
780 stat->antenna_voltage = r->operand[28];
781 stat->firewire_bus_voltage = r->operand[29];
782 stat->ca_mmi = r->operand[30] & 1;
783 stat->ca_pmt_reply = r->operand[31] >> 7 & 1;
784 stat->ca_date_time_request = r->operand[31] >> 6 & 1;
785 stat->ca_application_info = r->operand[31] >> 5 & 1;
786 stat->ca_module_present_status = r->operand[31] >> 4 & 1;
787 stat->ca_dvb_flag = r->operand[31] >> 3 & 1;
788 stat->ca_error_flag = r->operand[31] >> 2 & 1;
789 stat->ca_initialization_status = r->operand[31] >> 1 & 1;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300790out:
791 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100792
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300793 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100794}
795
796int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
797 char conttone, char nrdiseq,
798 struct dvb_diseqc_master_cmd *diseqcmd)
799{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300800 struct avc_command_frame *c = (void *)fdtv->avc_data;
801 struct avc_response_frame *r = (void *)fdtv->avc_data;
Stefan Richter1e4348c2009-11-18 16:03:31 -0300802 int pos, j, k, ret;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300803
Stefan Richter6385c5b2009-11-18 16:03:03 -0300804 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100805
Stefan Richter15490792009-02-23 14:21:10 +0100806 c->ctype = AVC_CTYPE_CONTROL;
807 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
808 c->opcode = AVC_OPCODE_VENDOR;
809
810 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
811 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
812 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
813 c->operand[3] = SFE_VENDOR_OPCODE_LNB_CONTROL;
Stefan Richter15490792009-02-23 14:21:10 +0100814 c->operand[4] = voltage;
815 c->operand[5] = nrdiseq;
816
Stefan Richter1e4348c2009-11-18 16:03:31 -0300817 pos = 6;
Stefan Richter15490792009-02-23 14:21:10 +0100818 for (j = 0; j < nrdiseq; j++) {
Stefan Richter1e4348c2009-11-18 16:03:31 -0300819 c->operand[pos++] = diseqcmd[j].msg_len;
Stefan Richter15490792009-02-23 14:21:10 +0100820
821 for (k = 0; k < diseqcmd[j].msg_len; k++)
Stefan Richter1e4348c2009-11-18 16:03:31 -0300822 c->operand[pos++] = diseqcmd[j].msg[k];
Stefan Richter15490792009-02-23 14:21:10 +0100823 }
Stefan Richter1e4348c2009-11-18 16:03:31 -0300824 c->operand[pos++] = burst;
825 c->operand[pos++] = conttone;
826 pad_operands(c, pos);
Stefan Richter15490792009-02-23 14:21:10 +0100827
Stefan Richter1e4348c2009-11-18 16:03:31 -0300828 fdtv->avc_data_length = ALIGN(3 + pos, 4);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300829 ret = avc_write(fdtv);
830 if (ret < 0)
831 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100832
833 if (r->response != AVC_RESPONSE_ACCEPTED) {
834 dev_err(fdtv->device, "LNB control failed\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300835 ret = -EINVAL;
Stefan Richter15490792009-02-23 14:21:10 +0100836 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300837out:
838 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100839
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300840 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100841}
842
843int avc_register_remote_control(struct firedtv *fdtv)
844{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300845 struct avc_command_frame *c = (void *)fdtv->avc_data;
846 int ret;
847
Stefan Richter6385c5b2009-11-18 16:03:03 -0300848 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100849
Stefan Richter15490792009-02-23 14:21:10 +0100850 c->ctype = AVC_CTYPE_NOTIFY;
851 c->subunit = AVC_SUBUNIT_TYPE_UNIT | 7;
852 c->opcode = AVC_OPCODE_VENDOR;
853
854 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
855 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
856 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
857 c->operand[3] = SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
Stefan Richter1e4348c2009-11-18 16:03:31 -0300858 c->operand[4] = 0; /* padding */
Stefan Richter15490792009-02-23 14:21:10 +0100859
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300860 fdtv->avc_data_length = 8;
861 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100862
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300863 /* FIXME: check response code? */
864
865 mutex_unlock(&fdtv->avc_mutex);
866
867 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100868}
869
870void avc_remote_ctrl_work(struct work_struct *work)
871{
872 struct firedtv *fdtv =
873 container_of(work, struct firedtv, remote_ctrl_work);
874
875 /* Should it be rescheduled in failure cases? */
876 avc_register_remote_control(fdtv);
877}
878
879#if 0 /* FIXME: unused */
880int avc_tuner_host2ca(struct firedtv *fdtv)
881{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300882 struct avc_command_frame *c = (void *)fdtv->avc_data;
883 int ret;
884
Stefan Richter6385c5b2009-11-18 16:03:03 -0300885 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100886
Stefan Richter15490792009-02-23 14:21:10 +0100887 c->ctype = AVC_CTYPE_CONTROL;
888 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
889 c->opcode = AVC_OPCODE_VENDOR;
890
891 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
892 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
893 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
894 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
895 c->operand[4] = 0; /* slot */
896 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300897 clear_operands(c, 6, 8);
Stefan Richter15490792009-02-23 14:21:10 +0100898
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300899 fdtv->avc_data_length = 12;
900 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100901
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300902 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +0100903
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300904 mutex_unlock(&fdtv->avc_mutex);
905
906 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100907}
908#endif
909
910static int get_ca_object_pos(struct avc_response_frame *r)
911{
912 int length = 1;
913
914 /* Check length of length field */
915 if (r->operand[7] & 0x80)
916 length = (r->operand[7] & 0x7f) + 1;
917 return length + 7;
918}
919
920static int get_ca_object_length(struct avc_response_frame *r)
921{
922#if 0 /* FIXME: unused */
923 int size = 0;
924 int i;
925
926 if (r->operand[7] & 0x80)
927 for (i = 0; i < (r->operand[7] & 0x7f); i++) {
928 size <<= 8;
929 size += r->operand[8 + i];
930 }
931#endif
932 return r->operand[7];
933}
934
935int avc_ca_app_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
936{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300937 struct avc_command_frame *c = (void *)fdtv->avc_data;
938 struct avc_response_frame *r = (void *)fdtv->avc_data;
939 int pos, ret;
940
Stefan Richter6385c5b2009-11-18 16:03:03 -0300941 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100942
Stefan Richter15490792009-02-23 14:21:10 +0100943 c->ctype = AVC_CTYPE_STATUS;
944 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
945 c->opcode = AVC_OPCODE_VENDOR;
946
947 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
948 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
949 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
950 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
951 c->operand[4] = 0; /* slot */
952 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300953 clear_operands(c, 6, LAST_OPERAND);
Stefan Richter15490792009-02-23 14:21:10 +0100954
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300955 fdtv->avc_data_length = 12;
956 ret = avc_write(fdtv);
957 if (ret < 0)
958 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100959
960 /* FIXME: check response code and validate response data */
961
962 pos = get_ca_object_pos(r);
963 app_info[0] = (EN50221_TAG_APP_INFO >> 16) & 0xff;
964 app_info[1] = (EN50221_TAG_APP_INFO >> 8) & 0xff;
965 app_info[2] = (EN50221_TAG_APP_INFO >> 0) & 0xff;
966 app_info[3] = 6 + r->operand[pos + 4];
967 app_info[4] = 0x01;
968 memcpy(&app_info[5], &r->operand[pos], 5 + r->operand[pos + 4]);
969 *len = app_info[3] + 4;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300970out:
971 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100972
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300973 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100974}
975
976int avc_ca_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
977{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300978 struct avc_command_frame *c = (void *)fdtv->avc_data;
979 struct avc_response_frame *r = (void *)fdtv->avc_data;
980 int pos, ret;
981
Stefan Richter6385c5b2009-11-18 16:03:03 -0300982 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100983
Stefan Richter15490792009-02-23 14:21:10 +0100984 c->ctype = AVC_CTYPE_STATUS;
985 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
986 c->opcode = AVC_OPCODE_VENDOR;
987
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 */
Stefan Richter1e4348c2009-11-18 16:03:31 -0300994 clear_operands(c, 6, LAST_OPERAND);
Stefan Richter15490792009-02-23 14:21:10 +0100995
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300996 fdtv->avc_data_length = 12;
997 ret = avc_write(fdtv);
998 if (ret < 0)
999 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001000
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001001 /* FIXME: check response code and validate response data */
Stefan Richter15490792009-02-23 14:21:10 +01001002
1003 pos = get_ca_object_pos(r);
1004 app_info[0] = (EN50221_TAG_CA_INFO >> 16) & 0xff;
1005 app_info[1] = (EN50221_TAG_CA_INFO >> 8) & 0xff;
1006 app_info[2] = (EN50221_TAG_CA_INFO >> 0) & 0xff;
1007 app_info[3] = 2;
1008 app_info[4] = r->operand[pos + 0];
1009 app_info[5] = r->operand[pos + 1];
1010 *len = app_info[3] + 4;
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001011out:
1012 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001013
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001014 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001015}
1016
1017int avc_ca_reset(struct firedtv *fdtv)
1018{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001019 struct avc_command_frame *c = (void *)fdtv->avc_data;
1020 int ret;
1021
Stefan Richter6385c5b2009-11-18 16:03:03 -03001022 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001023
Stefan Richter15490792009-02-23 14:21:10 +01001024 c->ctype = AVC_CTYPE_CONTROL;
1025 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1026 c->opcode = AVC_OPCODE_VENDOR;
1027
1028 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1029 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1030 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1031 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1032 c->operand[4] = 0; /* slot */
1033 c->operand[5] = SFE_VENDOR_TAG_CA_RESET; /* ca tag */
1034 c->operand[6] = 0; /* more/last */
1035 c->operand[7] = 1; /* length */
1036 c->operand[8] = 0; /* force hardware reset */
1037
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001038 fdtv->avc_data_length = 12;
1039 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +01001040
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001041 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +01001042
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001043 mutex_unlock(&fdtv->avc_mutex);
1044
1045 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001046}
1047
1048int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
1049{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001050 struct avc_command_frame *c = (void *)fdtv->avc_data;
1051 struct avc_response_frame *r = (void *)fdtv->avc_data;
Stefan Richter15490792009-02-23 14:21:10 +01001052 int list_management;
1053 int program_info_length;
1054 int pmt_cmd_id;
1055 int read_pos;
1056 int write_pos;
1057 int es_info_length;
1058 int crc32_csum;
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001059 int ret;
Stefan Richter15490792009-02-23 14:21:10 +01001060
Henrik Kurelid15344772009-08-01 08:04:06 -03001061 if (unlikely(avc_debug & AVC_DEBUG_APPLICATION_PMT))
1062 debug_pmt(msg, length);
1063
Stefan Richter6385c5b2009-11-18 16:03:03 -03001064 mutex_lock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001065
Stefan Richter15490792009-02-23 14:21:10 +01001066 c->ctype = AVC_CTYPE_CONTROL;
1067 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1068 c->opcode = AVC_OPCODE_VENDOR;
1069
1070 if (msg[0] != EN50221_LIST_MANAGEMENT_ONLY) {
1071 dev_info(fdtv->device, "forcing list_management to ONLY\n");
1072 msg[0] = EN50221_LIST_MANAGEMENT_ONLY;
1073 }
1074 /* We take the cmd_id from the programme level only! */
1075 list_management = msg[0];
1076 program_info_length = ((msg[4] & 0x0f) << 8) + msg[5];
1077 if (program_info_length > 0)
1078 program_info_length--; /* Remove pmt_cmd_id */
1079 pmt_cmd_id = msg[6];
1080
1081 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1082 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1083 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1084 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1085 c->operand[4] = 0; /* slot */
1086 c->operand[5] = SFE_VENDOR_TAG_CA_PMT; /* ca tag */
1087 c->operand[6] = 0; /* more/last */
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001088 /* Use three bytes for length field in case length > 127 */
1089 c->operand[10] = list_management;
1090 c->operand[11] = 0x01; /* pmt_cmd=OK_descramble */
Stefan Richter15490792009-02-23 14:21:10 +01001091
1092 /* TS program map table */
1093
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001094 c->operand[12] = 0x02; /* Table id=2 */
1095 c->operand[13] = 0x80; /* Section syntax + length */
Stefan Richter1e4348c2009-11-18 16:03:31 -03001096
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001097 c->operand[15] = msg[1]; /* Program number */
1098 c->operand[16] = msg[2];
Henrik Kurelidad5e9b92009-07-21 13:45:50 -03001099 c->operand[17] = msg[3]; /* Version number and current/next */
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001100 c->operand[18] = 0x00; /* Section number=0 */
1101 c->operand[19] = 0x00; /* Last section number=0 */
1102 c->operand[20] = 0x1f; /* PCR_PID=1FFF */
1103 c->operand[21] = 0xff;
1104 c->operand[22] = (program_info_length >> 8); /* Program info length */
1105 c->operand[23] = (program_info_length & 0xff);
Stefan Richter15490792009-02-23 14:21:10 +01001106
1107 /* CA descriptors at programme level */
1108 read_pos = 6;
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001109 write_pos = 24;
Stefan Richter15490792009-02-23 14:21:10 +01001110 if (program_info_length > 0) {
1111 pmt_cmd_id = msg[read_pos++];
1112 if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1113 dev_err(fdtv->device,
1114 "invalid pmt_cmd_id %d\n", pmt_cmd_id);
1115
1116 memcpy(&c->operand[write_pos], &msg[read_pos],
1117 program_info_length);
1118 read_pos += program_info_length;
1119 write_pos += program_info_length;
1120 }
1121 while (read_pos < length) {
1122 c->operand[write_pos++] = msg[read_pos++];
1123 c->operand[write_pos++] = msg[read_pos++];
1124 c->operand[write_pos++] = msg[read_pos++];
1125 es_info_length =
1126 ((msg[read_pos] & 0x0f) << 8) + msg[read_pos + 1];
1127 read_pos += 2;
1128 if (es_info_length > 0)
1129 es_info_length--; /* Remove pmt_cmd_id */
1130 c->operand[write_pos++] = es_info_length >> 8;
1131 c->operand[write_pos++] = es_info_length & 0xff;
1132 if (es_info_length > 0) {
1133 pmt_cmd_id = msg[read_pos++];
1134 if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1135 dev_err(fdtv->device, "invalid pmt_cmd_id %d "
1136 "at stream level\n", pmt_cmd_id);
1137
1138 memcpy(&c->operand[write_pos], &msg[read_pos],
1139 es_info_length);
1140 read_pos += es_info_length;
1141 write_pos += es_info_length;
1142 }
1143 }
Stefan Richter1e4348c2009-11-18 16:03:31 -03001144 write_pos += 4; /* CRC */
Stefan Richter15490792009-02-23 14:21:10 +01001145
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001146 c->operand[7] = 0x82;
1147 c->operand[8] = (write_pos - 10) >> 8;
1148 c->operand[9] = (write_pos - 10) & 0xff;
1149 c->operand[14] = write_pos - 15;
Stefan Richter15490792009-02-23 14:21:10 +01001150
1151 crc32_csum = crc32_be(0, &c->operand[10], c->operand[12] - 1);
1152 c->operand[write_pos - 4] = (crc32_csum >> 24) & 0xff;
1153 c->operand[write_pos - 3] = (crc32_csum >> 16) & 0xff;
1154 c->operand[write_pos - 2] = (crc32_csum >> 8) & 0xff;
1155 c->operand[write_pos - 1] = (crc32_csum >> 0) & 0xff;
Stefan Richter1e4348c2009-11-18 16:03:31 -03001156 pad_operands(c, write_pos);
Stefan Richter15490792009-02-23 14:21:10 +01001157
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001158 fdtv->avc_data_length = ALIGN(3 + write_pos, 4);
1159 ret = avc_write(fdtv);
1160 if (ret < 0)
1161 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001162
1163 if (r->response != AVC_RESPONSE_ACCEPTED) {
1164 dev_err(fdtv->device,
1165 "CA PMT failed with response 0x%x\n", r->response);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001166 ret = -EFAULT;
Stefan Richter15490792009-02-23 14:21:10 +01001167 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001168out:
1169 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001170
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001171 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001172}
1173
1174int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
1175{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001176 struct avc_command_frame *c = (void *)fdtv->avc_data;
1177 struct avc_response_frame *r = (void *)fdtv->avc_data;
1178 int ret;
1179
Stefan Richter6385c5b2009-11-18 16:03:03 -03001180 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001181
Stefan Richter15490792009-02-23 14:21:10 +01001182 c->ctype = AVC_CTYPE_STATUS;
1183 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1184 c->opcode = AVC_OPCODE_VENDOR;
1185
1186 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1187 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1188 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1189 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1190 c->operand[4] = 0; /* slot */
1191 c->operand[5] = SFE_VENDOR_TAG_CA_DATE_TIME; /* ca tag */
Stefan Richter1e4348c2009-11-18 16:03:31 -03001192 clear_operands(c, 6, LAST_OPERAND);
Stefan Richter15490792009-02-23 14:21:10 +01001193
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001194 fdtv->avc_data_length = 12;
1195 ret = avc_write(fdtv);
1196 if (ret < 0)
1197 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001198
1199 /* FIXME: check response code and validate response data */
1200
1201 *interval = r->operand[get_ca_object_pos(r)];
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001202out:
1203 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001204
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001205 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001206}
1207
1208int avc_ca_enter_menu(struct firedtv *fdtv)
1209{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001210 struct avc_command_frame *c = (void *)fdtv->avc_data;
1211 int ret;
1212
Stefan Richter6385c5b2009-11-18 16:03:03 -03001213 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001214
Stefan Richter15490792009-02-23 14:21:10 +01001215 c->ctype = AVC_CTYPE_STATUS;
1216 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1217 c->opcode = AVC_OPCODE_VENDOR;
1218
1219 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1220 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1221 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1222 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1223 c->operand[4] = 0; /* slot */
1224 c->operand[5] = SFE_VENDOR_TAG_CA_ENTER_MENU;
Stefan Richter1e4348c2009-11-18 16:03:31 -03001225 clear_operands(c, 6, 8);
Stefan Richter15490792009-02-23 14:21:10 +01001226
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001227 fdtv->avc_data_length = 12;
1228 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +01001229
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001230 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +01001231
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001232 mutex_unlock(&fdtv->avc_mutex);
1233
1234 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001235}
1236
1237int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
1238{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001239 struct avc_command_frame *c = (void *)fdtv->avc_data;
1240 struct avc_response_frame *r = (void *)fdtv->avc_data;
1241 int ret;
1242
Stefan Richter6385c5b2009-11-18 16:03:03 -03001243 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001244
Stefan Richter15490792009-02-23 14:21:10 +01001245 c->ctype = AVC_CTYPE_STATUS;
1246 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1247 c->opcode = AVC_OPCODE_VENDOR;
1248
1249 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1250 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1251 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1252 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1253 c->operand[4] = 0; /* slot */
1254 c->operand[5] = SFE_VENDOR_TAG_CA_MMI;
Stefan Richter1e4348c2009-11-18 16:03:31 -03001255 clear_operands(c, 6, LAST_OPERAND);
Stefan Richter15490792009-02-23 14:21:10 +01001256
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001257 fdtv->avc_data_length = 12;
1258 ret = avc_write(fdtv);
1259 if (ret < 0)
1260 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001261
1262 /* FIXME: check response code and validate response data */
1263
1264 *len = get_ca_object_length(r);
1265 memcpy(mmi_object, &r->operand[get_ca_object_pos(r)], *len);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001266out:
1267 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001268
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001269 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001270}
1271
1272#define CMP_OUTPUT_PLUG_CONTROL_REG_0 0xfffff0000904ULL
1273
Stefan Richter53756592009-11-18 16:01:34 -03001274static int cmp_read(struct firedtv *fdtv, u64 addr, __be32 *data)
Stefan Richter15490792009-02-23 14:21:10 +01001275{
1276 int ret;
1277
Stefan Richter6385c5b2009-11-18 16:03:03 -03001278 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001279
Stefan Richter53756592009-11-18 16:01:34 -03001280 ret = fdtv->backend->read(fdtv, addr, data);
Stefan Richter15490792009-02-23 14:21:10 +01001281 if (ret < 0)
1282 dev_err(fdtv->device, "CMP: read I/O error\n");
1283
1284 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001285
Stefan Richter15490792009-02-23 14:21:10 +01001286 return ret;
1287}
1288
Stefan Richter054286b2009-11-08 18:29:08 -03001289static int cmp_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
Stefan Richter15490792009-02-23 14:21:10 +01001290{
1291 int ret;
1292
Stefan Richter6385c5b2009-11-18 16:03:03 -03001293 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001294
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001295 /* data[] is stack-allocated and should not be DMA-mapped. */
1296 memcpy(fdtv->avc_data, data, 8);
1297
1298 ret = fdtv->backend->lock(fdtv, addr, fdtv->avc_data);
Stefan Richter15490792009-02-23 14:21:10 +01001299 if (ret < 0)
1300 dev_err(fdtv->device, "CMP: lock I/O error\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001301 else
1302 memcpy(data, fdtv->avc_data, 8);
Stefan Richter15490792009-02-23 14:21:10 +01001303
1304 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001305
Stefan Richter15490792009-02-23 14:21:10 +01001306 return ret;
1307}
1308
1309static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
1310{
1311 return (be32_to_cpu(opcr) >> shift) & mask;
1312}
1313
1314static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
1315{
1316 *opcr &= ~cpu_to_be32(mask << shift);
1317 *opcr |= cpu_to_be32((value & mask) << shift);
1318}
1319
1320#define get_opcr_online(v) get_opcr((v), 0x1, 31)
1321#define get_opcr_p2p_connections(v) get_opcr((v), 0x3f, 24)
1322#define get_opcr_channel(v) get_opcr((v), 0x3f, 16)
1323
1324#define set_opcr_p2p_connections(p, v) set_opcr((p), (v), 0x3f, 24)
1325#define set_opcr_channel(p, v) set_opcr((p), (v), 0x3f, 16)
1326#define set_opcr_data_rate(p, v) set_opcr((p), (v), 0x3, 14)
1327#define set_opcr_overhead_id(p, v) set_opcr((p), (v), 0xf, 10)
1328
1329int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
1330{
Stefan Richter054286b2009-11-08 18:29:08 -03001331 __be32 old_opcr, opcr[2];
Stefan Richter15490792009-02-23 14:21:10 +01001332 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1333 int attempts = 0;
1334 int ret;
1335
Stefan Richter53756592009-11-18 16:01:34 -03001336 ret = cmp_read(fdtv, opcr_address, opcr);
Stefan Richter15490792009-02-23 14:21:10 +01001337 if (ret < 0)
1338 return ret;
1339
1340repeat:
Stefan Richter054286b2009-11-08 18:29:08 -03001341 if (!get_opcr_online(*opcr)) {
Stefan Richter15490792009-02-23 14:21:10 +01001342 dev_err(fdtv->device, "CMP: output offline\n");
1343 return -EBUSY;
1344 }
1345
Stefan Richter054286b2009-11-08 18:29:08 -03001346 old_opcr = *opcr;
Stefan Richter15490792009-02-23 14:21:10 +01001347
Stefan Richter054286b2009-11-08 18:29:08 -03001348 if (get_opcr_p2p_connections(*opcr)) {
1349 if (get_opcr_channel(*opcr) != channel) {
Stefan Richter15490792009-02-23 14:21:10 +01001350 dev_err(fdtv->device, "CMP: cannot change channel\n");
1351 return -EBUSY;
1352 }
1353 dev_info(fdtv->device, "CMP: overlaying connection\n");
1354
1355 /* We don't allocate isochronous resources. */
1356 } else {
Stefan Richter054286b2009-11-08 18:29:08 -03001357 set_opcr_channel(opcr, channel);
1358 set_opcr_data_rate(opcr, 2); /* S400 */
Stefan Richter15490792009-02-23 14:21:10 +01001359
1360 /* FIXME: this is for the worst case - optimize */
Stefan Richter054286b2009-11-08 18:29:08 -03001361 set_opcr_overhead_id(opcr, 0);
Stefan Richter15490792009-02-23 14:21:10 +01001362
1363 /*
1364 * FIXME: allocate isochronous channel and bandwidth at IRM
1365 * fdtv->backend->alloc_resources(fdtv, channels_mask, bw);
1366 */
1367 }
1368
Stefan Richter054286b2009-11-08 18:29:08 -03001369 set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) + 1);
Stefan Richter15490792009-02-23 14:21:10 +01001370
Stefan Richter054286b2009-11-08 18:29:08 -03001371 opcr[1] = *opcr;
1372 opcr[0] = old_opcr;
1373
1374 ret = cmp_lock(fdtv, opcr_address, opcr);
Stefan Richter15490792009-02-23 14:21:10 +01001375 if (ret < 0)
1376 return ret;
1377
Stefan Richter054286b2009-11-08 18:29:08 -03001378 if (old_opcr != *opcr) {
Stefan Richter15490792009-02-23 14:21:10 +01001379 /*
1380 * FIXME: if old_opcr.P2P_Connections > 0,
1381 * deallocate isochronous channel and bandwidth at IRM
1382 * if (...)
1383 * fdtv->backend->dealloc_resources(fdtv, channel, bw);
1384 */
1385
1386 if (++attempts < 6) /* arbitrary limit */
1387 goto repeat;
1388 return -EBUSY;
1389 }
1390
1391 return 0;
1392}
1393
1394void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
1395{
Stefan Richter054286b2009-11-08 18:29:08 -03001396 __be32 old_opcr, opcr[2];
Stefan Richter15490792009-02-23 14:21:10 +01001397 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1398 int attempts = 0;
1399
Stefan Richter53756592009-11-18 16:01:34 -03001400 if (cmp_read(fdtv, opcr_address, opcr) < 0)
Stefan Richter15490792009-02-23 14:21:10 +01001401 return;
1402
1403repeat:
Stefan Richter054286b2009-11-08 18:29:08 -03001404 if (!get_opcr_online(*opcr) || !get_opcr_p2p_connections(*opcr) ||
1405 get_opcr_channel(*opcr) != channel) {
Stefan Richter15490792009-02-23 14:21:10 +01001406 dev_err(fdtv->device, "CMP: no connection to break\n");
1407 return;
1408 }
1409
Stefan Richter054286b2009-11-08 18:29:08 -03001410 old_opcr = *opcr;
1411 set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) - 1);
Stefan Richter15490792009-02-23 14:21:10 +01001412
Stefan Richter054286b2009-11-08 18:29:08 -03001413 opcr[1] = *opcr;
1414 opcr[0] = old_opcr;
1415
1416 if (cmp_lock(fdtv, opcr_address, opcr) < 0)
Stefan Richter15490792009-02-23 14:21:10 +01001417 return;
1418
Stefan Richter054286b2009-11-08 18:29:08 -03001419 if (old_opcr != *opcr) {
Stefan Richter15490792009-02-23 14:21:10 +01001420 /*
1421 * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
1422 * owner, deallocate isochronous channel and bandwidth at IRM
1423 * if (...)
1424 * fdtv->backend->dealloc_resources(fdtv, channel, bw);
1425 */
1426
1427 if (++attempts < 6) /* arbitrary limit */
1428 goto repeat;
1429 }
1430}