blob: e706428112170eee41981b92d6574cce5209ebec [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
Henrik Kurelid15344772009-08-01 08:04:06 -030090#define AVC_DEBUG_READ_DESCRIPTOR 0x0001
91#define AVC_DEBUG_DSIT 0x0002
92#define AVC_DEBUG_DSD 0x0004
93#define AVC_DEBUG_REGISTER_REMOTE_CONTROL 0x0008
94#define AVC_DEBUG_LNB_CONTROL 0x0010
95#define AVC_DEBUG_TUNE_QPSK 0x0020
96#define AVC_DEBUG_TUNE_QPSK2 0x0040
97#define AVC_DEBUG_HOST2CA 0x0080
98#define AVC_DEBUG_CA2HOST 0x0100
99#define AVC_DEBUG_APPLICATION_PMT 0x4000
100#define AVC_DEBUG_FCP_PAYLOADS 0x8000
Stefan Richter15490792009-02-23 14:21:10 +0100101
102static int avc_debug;
103module_param_named(debug, avc_debug, int, 0644);
Stefan Richter96e242a2009-08-01 08:05:16 -0300104MODULE_PARM_DESC(debug, "Verbose logging (none = 0"
105 ", FCP subactions"
106 ": READ DESCRIPTOR = " __stringify(AVC_DEBUG_READ_DESCRIPTOR)
107 ", DSIT = " __stringify(AVC_DEBUG_DSIT)
108 ", REGISTER_REMOTE_CONTROL = " __stringify(AVC_DEBUG_REGISTER_REMOTE_CONTROL)
109 ", LNB CONTROL = " __stringify(AVC_DEBUG_LNB_CONTROL)
110 ", TUNE QPSK = " __stringify(AVC_DEBUG_TUNE_QPSK)
111 ", TUNE QPSK2 = " __stringify(AVC_DEBUG_TUNE_QPSK2)
112 ", HOST2CA = " __stringify(AVC_DEBUG_HOST2CA)
113 ", CA2HOST = " __stringify(AVC_DEBUG_CA2HOST)
114 "; Application sent PMT = " __stringify(AVC_DEBUG_APPLICATION_PMT)
115 ", FCP payloads = " __stringify(AVC_DEBUG_FCP_PAYLOADS)
116 ", or a combination, or all = -1)");
Stefan Richter15490792009-02-23 14:21:10 +0100117
118static const char *debug_fcp_ctype(unsigned int ctype)
119{
120 static const char *ctypes[] = {
121 [0x0] = "CONTROL", [0x1] = "STATUS",
122 [0x2] = "SPECIFIC INQUIRY", [0x3] = "NOTIFY",
123 [0x4] = "GENERAL INQUIRY", [0x8] = "NOT IMPLEMENTED",
124 [0x9] = "ACCEPTED", [0xa] = "REJECTED",
125 [0xb] = "IN TRANSITION", [0xc] = "IMPLEMENTED/STABLE",
126 [0xd] = "CHANGED", [0xf] = "INTERIM",
127 };
128 const char *ret = ctype < ARRAY_SIZE(ctypes) ? ctypes[ctype] : NULL;
129
130 return ret ? ret : "?";
131}
132
133static const char *debug_fcp_opcode(unsigned int opcode,
Stefan Richter40cf65d2009-03-05 19:13:43 +0100134 const u8 *data, int length)
Stefan Richter15490792009-02-23 14:21:10 +0100135{
136 switch (opcode) {
Stefan Richter96e242a2009-08-01 08:05:16 -0300137 case AVC_OPCODE_VENDOR:
138 break;
139 case AVC_OPCODE_READ_DESCRIPTOR:
140 return avc_debug & AVC_DEBUG_READ_DESCRIPTOR ?
141 "ReadDescriptor" : NULL;
142 case AVC_OPCODE_DSIT:
143 return avc_debug & AVC_DEBUG_DSIT ?
144 "DirectSelectInfo.Type" : NULL;
145 case AVC_OPCODE_DSD:
146 return avc_debug & AVC_DEBUG_DSD ? "DirectSelectData" : NULL;
147 default:
148 return "Unknown";
Stefan Richter15490792009-02-23 14:21:10 +0100149 }
150
151 if (length < 7 ||
152 data[3] != SFE_VENDOR_DE_COMPANYID_0 ||
153 data[4] != SFE_VENDOR_DE_COMPANYID_1 ||
154 data[5] != SFE_VENDOR_DE_COMPANYID_2)
Stefan Richter96e242a2009-08-01 08:05:16 -0300155 return "Vendor/Unknown";
Stefan Richter15490792009-02-23 14:21:10 +0100156
157 switch (data[6]) {
Stefan Richter96e242a2009-08-01 08:05:16 -0300158 case SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL:
159 return avc_debug & AVC_DEBUG_REGISTER_REMOTE_CONTROL ?
160 "RegisterRC" : NULL;
161 case SFE_VENDOR_OPCODE_LNB_CONTROL:
162 return avc_debug & AVC_DEBUG_LNB_CONTROL ? "LNBControl" : NULL;
163 case SFE_VENDOR_OPCODE_TUNE_QPSK:
164 return avc_debug & AVC_DEBUG_TUNE_QPSK ? "TuneQPSK" : NULL;
165 case SFE_VENDOR_OPCODE_TUNE_QPSK2:
166 return avc_debug & AVC_DEBUG_TUNE_QPSK2 ? "TuneQPSK2" : NULL;
167 case SFE_VENDOR_OPCODE_HOST2CA:
168 return avc_debug & AVC_DEBUG_HOST2CA ? "Host2CA" : NULL;
169 case SFE_VENDOR_OPCODE_CA2HOST:
170 return avc_debug & AVC_DEBUG_CA2HOST ? "CA2Host" : NULL;
Stefan Richter15490792009-02-23 14:21:10 +0100171 }
Stefan Richter96e242a2009-08-01 08:05:16 -0300172 return "Vendor/Unknown";
Henrik Kurelid15344772009-08-01 08:04:06 -0300173}
174
Stefan Richter40cf65d2009-03-05 19:13:43 +0100175static void debug_fcp(const u8 *data, int length)
Stefan Richter15490792009-02-23 14:21:10 +0100176{
Stefan Richter96e242a2009-08-01 08:05:16 -0300177 unsigned int subunit_type, subunit_id, opcode;
178 const char *op, *prefix;
Stefan Richter15490792009-02-23 14:21:10 +0100179
Stefan Richter96e242a2009-08-01 08:05:16 -0300180 prefix = data[0] > 7 ? "FCP <- " : "FCP -> ";
Henrik Kurelid15344772009-08-01 08:04:06 -0300181 subunit_type = data[1] >> 3;
Stefan Richter96e242a2009-08-01 08:05:16 -0300182 subunit_id = data[1] & 7;
183 opcode = subunit_type == 0x1e || subunit_id == 5 ? ~0 : data[2];
184 op = debug_fcp_opcode(opcode, data, length);
185
186 if (op) {
Stefan Richter14edcd52009-04-01 17:25:00 -0300187 printk(KERN_INFO "%ssu=%x.%x l=%d: %-8s - %s\n",
Stefan Richter15490792009-02-23 14:21:10 +0100188 prefix, subunit_type, subunit_id, length,
Stefan Richter96e242a2009-08-01 08:05:16 -0300189 debug_fcp_ctype(data[0]), op);
Henrik Kurelid15344772009-08-01 08:04:06 -0300190 if (avc_debug & AVC_DEBUG_FCP_PAYLOADS)
191 print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_NONE,
192 16, 1, data, length, false);
Stefan Richter15490792009-02-23 14:21:10 +0100193 }
Henrik Kurelid15344772009-08-01 08:04:06 -0300194}
Stefan Richter15490792009-02-23 14:21:10 +0100195
Henrik Kurelid15344772009-08-01 08:04:06 -0300196static void debug_pmt(char *msg, int length)
197{
198 printk(KERN_INFO "APP PMT -> l=%d\n", length);
199 print_hex_dump(KERN_INFO, "APP PMT -> ", DUMP_PREFIX_NONE,
200 16, 1, msg, length, false);
Stefan Richter15490792009-02-23 14:21:10 +0100201}
202
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300203static int avc_write(struct firedtv *fdtv)
Stefan Richter15490792009-02-23 14:21:10 +0100204{
205 int err, retry;
206
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300207 fdtv->avc_reply_received = false;
Stefan Richter15490792009-02-23 14:21:10 +0100208
209 for (retry = 0; retry < 6; retry++) {
210 if (unlikely(avc_debug))
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300211 debug_fcp(fdtv->avc_data, fdtv->avc_data_length);
Stefan Richter15490792009-02-23 14:21:10 +0100212
213 err = fdtv->backend->write(fdtv, FCP_COMMAND_REGISTER,
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300214 fdtv->avc_data, fdtv->avc_data_length);
Stefan Richter15490792009-02-23 14:21:10 +0100215 if (err) {
Stefan Richter15490792009-02-23 14:21:10 +0100216 dev_err(fdtv->device, "FCP command write failed\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300217
Stefan Richter15490792009-02-23 14:21:10 +0100218 return err;
219 }
220
Stefan Richter15490792009-02-23 14:21:10 +0100221 /*
222 * AV/C specs say that answers should be sent within 150 ms.
223 * Time out after 200 ms.
224 */
225 if (wait_event_timeout(fdtv->avc_wait,
226 fdtv->avc_reply_received,
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300227 msecs_to_jiffies(200)) != 0)
Stefan Richter15490792009-02-23 14:21:10 +0100228 return 0;
Stefan Richter15490792009-02-23 14:21:10 +0100229 }
230 dev_err(fdtv->device, "FCP response timed out\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300231
Stefan Richter15490792009-02-23 14:21:10 +0100232 return -ETIMEDOUT;
233}
234
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300235static bool is_register_rc(struct avc_response_frame *r)
Stefan Richter15490792009-02-23 14:21:10 +0100236{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300237 return r->opcode == AVC_OPCODE_VENDOR &&
238 r->operand[0] == SFE_VENDOR_DE_COMPANYID_0 &&
239 r->operand[1] == SFE_VENDOR_DE_COMPANYID_1 &&
240 r->operand[2] == SFE_VENDOR_DE_COMPANYID_2 &&
241 r->operand[3] == SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
Stefan Richter15490792009-02-23 14:21:10 +0100242}
243
244int avc_recv(struct firedtv *fdtv, void *data, size_t length)
245{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300246 struct avc_response_frame *r = data;
Stefan Richter15490792009-02-23 14:21:10 +0100247
248 if (unlikely(avc_debug))
249 debug_fcp(data, length);
250
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300251 if (length >= 8 && is_register_rc(r)) {
252 switch (r->response) {
253 case AVC_RESPONSE_CHANGED:
254 fdtv_handle_rc(fdtv, r->operand[4] << 8 | r->operand[5]);
Stefan Richter15490792009-02-23 14:21:10 +0100255 schedule_work(&fdtv->remote_ctrl_work);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300256 break;
257 case AVC_RESPONSE_INTERIM:
258 if (is_register_rc((void *)fdtv->avc_data))
259 goto wake;
260 break;
261 default:
Stefan Richter15490792009-02-23 14:21:10 +0100262 dev_info(fdtv->device,
263 "remote control result = %d\n", r->response);
264 }
265 return 0;
266 }
267
268 if (fdtv->avc_reply_received) {
269 dev_err(fdtv->device, "out-of-order AVC response, ignored\n");
270 return -EIO;
271 }
272
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300273 memcpy(fdtv->avc_data, data, length);
274 fdtv->avc_data_length = length;
275wake:
Stefan Richter15490792009-02-23 14:21:10 +0100276 fdtv->avc_reply_received = true;
277 wake_up(&fdtv->avc_wait);
278
279 return 0;
280}
281
Henrik Kurelid166987c2009-08-01 08:02:38 -0300282static int add_pid_filter(struct firedtv *fdtv, u8 *operand)
283{
284 int i, n, pos = 1;
285
286 for (i = 0, n = 0; i < 16; i++) {
287 if (test_bit(i, &fdtv->channel_active)) {
288 operand[pos++] = 0x13; /* flowfunction relay */
289 operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
290 operand[pos++] = (fdtv->channel_pid[i] >> 8) & 0x1f;
291 operand[pos++] = fdtv->channel_pid[i] & 0xff;
292 operand[pos++] = 0x00; /* tableID */
293 operand[pos++] = 0x00; /* filter_length */
294 n++;
295 }
296 }
297 operand[0] = n;
298
299 return pos;
300}
301
Stefan Richter15490792009-02-23 14:21:10 +0100302/*
303 * tuning command for setting the relative LNB frequency
304 * (not supported by the AVC standard)
305 */
306static void avc_tuner_tuneqpsk(struct firedtv *fdtv,
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300307 struct dvb_frontend_parameters *params)
Stefan Richter15490792009-02-23 14:21:10 +0100308{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300309 struct avc_command_frame *c = (void *)fdtv->avc_data;
310
Stefan Richter15490792009-02-23 14:21:10 +0100311 c->opcode = AVC_OPCODE_VENDOR;
312
313 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
314 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
315 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
Beat Michel Liechti32a0f482009-03-26 22:36:52 +0100316 if (fdtv->type == FIREDTV_DVB_S2)
317 c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK2;
318 else
319 c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK;
Stefan Richter15490792009-02-23 14:21:10 +0100320
321 c->operand[4] = (params->frequency >> 24) & 0xff;
322 c->operand[5] = (params->frequency >> 16) & 0xff;
323 c->operand[6] = (params->frequency >> 8) & 0xff;
324 c->operand[7] = params->frequency & 0xff;
325
326 c->operand[8] = ((params->u.qpsk.symbol_rate / 1000) >> 8) & 0xff;
327 c->operand[9] = (params->u.qpsk.symbol_rate / 1000) & 0xff;
328
329 switch (params->u.qpsk.fec_inner) {
330 case FEC_1_2: c->operand[10] = 0x1; break;
331 case FEC_2_3: c->operand[10] = 0x2; break;
332 case FEC_3_4: c->operand[10] = 0x3; break;
333 case FEC_5_6: c->operand[10] = 0x4; break;
334 case FEC_7_8: c->operand[10] = 0x5; break;
335 case FEC_4_5:
336 case FEC_8_9:
337 case FEC_AUTO:
338 default: c->operand[10] = 0x0;
339 }
340
341 if (fdtv->voltage == 0xff)
342 c->operand[11] = 0xff;
343 else if (fdtv->voltage == SEC_VOLTAGE_18) /* polarisation */
344 c->operand[11] = 0;
345 else
346 c->operand[11] = 1;
347
348 if (fdtv->tone == 0xff)
349 c->operand[12] = 0xff;
350 else if (fdtv->tone == SEC_TONE_ON) /* band */
351 c->operand[12] = 1;
352 else
353 c->operand[12] = 0;
354
355 if (fdtv->type == FIREDTV_DVB_S2) {
356 c->operand[13] = 0x1;
357 c->operand[14] = 0xff;
358 c->operand[15] = 0xff;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300359 fdtv->avc_data_length = 20;
Stefan Richter15490792009-02-23 14:21:10 +0100360 } else {
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300361 fdtv->avc_data_length = 16;
Stefan Richter15490792009-02-23 14:21:10 +0100362 }
363}
364
Henrik Kurelid166987c2009-08-01 08:02:38 -0300365static void avc_tuner_dsd_dvb_c(struct firedtv *fdtv,
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300366 struct dvb_frontend_parameters *params)
Stefan Richter15490792009-02-23 14:21:10 +0100367{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300368 struct avc_command_frame *c = (void *)fdtv->avc_data;
369
Stefan Richter15490792009-02-23 14:21:10 +0100370 c->opcode = AVC_OPCODE_DSD;
371
372 c->operand[0] = 0; /* source plug */
373 c->operand[1] = 0xd2; /* subfunction replace */
374 c->operand[2] = 0x20; /* system id = DVB */
375 c->operand[3] = 0x00; /* antenna number */
376 c->operand[4] = 0x11; /* system_specific_multiplex selection_length */
377
378 /* multiplex_valid_flags, high byte */
379 c->operand[5] = 0 << 7 /* reserved */
380 | 0 << 6 /* Polarisation */
381 | 0 << 5 /* Orbital_Pos */
382 | 1 << 4 /* Frequency */
383 | 1 << 3 /* Symbol_Rate */
384 | 0 << 2 /* FEC_outer */
385 | (params->u.qam.fec_inner != FEC_AUTO ? 1 << 1 : 0)
386 | (params->u.qam.modulation != QAM_AUTO ? 1 << 0 : 0);
387
388 /* multiplex_valid_flags, low byte */
389 c->operand[6] = 0 << 7 /* NetworkID */
390 | 0 << 0 /* reserved */ ;
391
392 c->operand[7] = 0x00;
393 c->operand[8] = 0x00;
394 c->operand[9] = 0x00;
395 c->operand[10] = 0x00;
396
397 c->operand[11] = (((params->frequency / 4000) >> 16) & 0xff) | (2 << 6);
398 c->operand[12] = ((params->frequency / 4000) >> 8) & 0xff;
399 c->operand[13] = (params->frequency / 4000) & 0xff;
400 c->operand[14] = ((params->u.qpsk.symbol_rate / 1000) >> 12) & 0xff;
401 c->operand[15] = ((params->u.qpsk.symbol_rate / 1000) >> 4) & 0xff;
402 c->operand[16] = ((params->u.qpsk.symbol_rate / 1000) << 4) & 0xf0;
403 c->operand[17] = 0x00;
404
405 switch (params->u.qpsk.fec_inner) {
406 case FEC_1_2: c->operand[18] = 0x1; break;
407 case FEC_2_3: c->operand[18] = 0x2; break;
408 case FEC_3_4: c->operand[18] = 0x3; break;
409 case FEC_5_6: c->operand[18] = 0x4; break;
410 case FEC_7_8: c->operand[18] = 0x5; break;
411 case FEC_8_9: c->operand[18] = 0x6; break;
412 case FEC_4_5: c->operand[18] = 0x8; break;
413 case FEC_AUTO:
414 default: c->operand[18] = 0x0;
415 }
416
417 switch (params->u.qam.modulation) {
418 case QAM_16: c->operand[19] = 0x08; break;
419 case QAM_32: c->operand[19] = 0x10; break;
420 case QAM_64: c->operand[19] = 0x18; break;
421 case QAM_128: c->operand[19] = 0x20; break;
422 case QAM_256: c->operand[19] = 0x28; break;
423 case QAM_AUTO:
424 default: c->operand[19] = 0x00;
425 }
426
427 c->operand[20] = 0x00;
428 c->operand[21] = 0x00;
Stefan Richter15490792009-02-23 14:21:10 +0100429
Henrik Kurelid166987c2009-08-01 08:02:38 -0300430 /* Add PIDs to filter */
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300431 fdtv->avc_data_length =
432 ALIGN(22 + add_pid_filter(fdtv, &c->operand[22]) + 3, 4);
Stefan Richter15490792009-02-23 14:21:10 +0100433}
434
Henrik Kurelid166987c2009-08-01 08:02:38 -0300435static void avc_tuner_dsd_dvb_t(struct firedtv *fdtv,
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300436 struct dvb_frontend_parameters *params)
Stefan Richter15490792009-02-23 14:21:10 +0100437{
438 struct dvb_ofdm_parameters *ofdm = &params->u.ofdm;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300439 struct avc_command_frame *c = (void *)fdtv->avc_data;
Stefan Richter15490792009-02-23 14:21:10 +0100440
441 c->opcode = AVC_OPCODE_DSD;
442
443 c->operand[0] = 0; /* source plug */
444 c->operand[1] = 0xd2; /* subfunction replace */
445 c->operand[2] = 0x20; /* system id = DVB */
446 c->operand[3] = 0x00; /* antenna number */
447 c->operand[4] = 0x0c; /* system_specific_multiplex selection_length */
448
449 /* multiplex_valid_flags, high byte */
450 c->operand[5] =
451 0 << 7 /* reserved */
452 | 1 << 6 /* CenterFrequency */
453 | (ofdm->bandwidth != BANDWIDTH_AUTO ? 1 << 5 : 0)
454 | (ofdm->constellation != QAM_AUTO ? 1 << 4 : 0)
455 | (ofdm->hierarchy_information != HIERARCHY_AUTO ? 1 << 3 : 0)
456 | (ofdm->code_rate_HP != FEC_AUTO ? 1 << 2 : 0)
457 | (ofdm->code_rate_LP != FEC_AUTO ? 1 << 1 : 0)
458 | (ofdm->guard_interval != GUARD_INTERVAL_AUTO ? 1 << 0 : 0);
459
460 /* multiplex_valid_flags, low byte */
461 c->operand[6] =
462 0 << 7 /* NetworkID */
463 | (ofdm->transmission_mode != TRANSMISSION_MODE_AUTO ? 1 << 6 : 0)
464 | 0 << 5 /* OtherFrequencyFlag */
465 | 0 << 0 /* reserved */ ;
466
467 c->operand[7] = 0x0;
468 c->operand[8] = (params->frequency / 10) >> 24;
469 c->operand[9] = ((params->frequency / 10) >> 16) & 0xff;
470 c->operand[10] = ((params->frequency / 10) >> 8) & 0xff;
471 c->operand[11] = (params->frequency / 10) & 0xff;
472
473 switch (ofdm->bandwidth) {
474 case BANDWIDTH_7_MHZ: c->operand[12] = 0x20; break;
475 case BANDWIDTH_8_MHZ:
476 case BANDWIDTH_6_MHZ: /* not defined by AVC spec */
477 case BANDWIDTH_AUTO:
478 default: c->operand[12] = 0x00;
479 }
480
481 switch (ofdm->constellation) {
482 case QAM_16: c->operand[13] = 1 << 6; break;
483 case QAM_64: c->operand[13] = 2 << 6; break;
484 case QPSK:
485 default: c->operand[13] = 0x00;
486 }
487
488 switch (ofdm->hierarchy_information) {
489 case HIERARCHY_1: c->operand[13] |= 1 << 3; break;
490 case HIERARCHY_2: c->operand[13] |= 2 << 3; break;
491 case HIERARCHY_4: c->operand[13] |= 3 << 3; break;
492 case HIERARCHY_AUTO:
493 case HIERARCHY_NONE:
494 default: break;
495 }
496
497 switch (ofdm->code_rate_HP) {
498 case FEC_2_3: c->operand[13] |= 1; break;
499 case FEC_3_4: c->operand[13] |= 2; break;
500 case FEC_5_6: c->operand[13] |= 3; break;
501 case FEC_7_8: c->operand[13] |= 4; break;
502 case FEC_1_2:
503 default: break;
504 }
505
506 switch (ofdm->code_rate_LP) {
507 case FEC_2_3: c->operand[14] = 1 << 5; break;
508 case FEC_3_4: c->operand[14] = 2 << 5; break;
509 case FEC_5_6: c->operand[14] = 3 << 5; break;
510 case FEC_7_8: c->operand[14] = 4 << 5; break;
511 case FEC_1_2:
512 default: c->operand[14] = 0x00; break;
513 }
514
515 switch (ofdm->guard_interval) {
516 case GUARD_INTERVAL_1_16: c->operand[14] |= 1 << 3; break;
517 case GUARD_INTERVAL_1_8: c->operand[14] |= 2 << 3; break;
518 case GUARD_INTERVAL_1_4: c->operand[14] |= 3 << 3; break;
519 case GUARD_INTERVAL_1_32:
520 case GUARD_INTERVAL_AUTO:
521 default: break;
522 }
523
524 switch (ofdm->transmission_mode) {
525 case TRANSMISSION_MODE_8K: c->operand[14] |= 1 << 1; break;
526 case TRANSMISSION_MODE_2K:
527 case TRANSMISSION_MODE_AUTO:
528 default: break;
529 }
530
531 c->operand[15] = 0x00; /* network_ID[0] */
532 c->operand[16] = 0x00; /* network_ID[1] */
Stefan Richter15490792009-02-23 14:21:10 +0100533
Henrik Kurelid166987c2009-08-01 08:02:38 -0300534 /* Add PIDs to filter */
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300535 fdtv->avc_data_length =
536 ALIGN(17 + add_pid_filter(fdtv, &c->operand[17]) + 3, 4);
Stefan Richter15490792009-02-23 14:21:10 +0100537}
538
539int avc_tuner_dsd(struct firedtv *fdtv,
540 struct dvb_frontend_parameters *params)
541{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300542 struct avc_command_frame *c = (void *)fdtv->avc_data;
543 int ret;
544
Stefan Richter6385c5b2009-11-18 16:03:03 -0300545 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100546
547 memset(c, 0, sizeof(*c));
548
549 c->ctype = AVC_CTYPE_CONTROL;
550 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
551
552 switch (fdtv->type) {
553 case FIREDTV_DVB_S:
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300554 case FIREDTV_DVB_S2: avc_tuner_tuneqpsk(fdtv, params); break;
555 case FIREDTV_DVB_C: avc_tuner_dsd_dvb_c(fdtv, params); break;
556 case FIREDTV_DVB_T: avc_tuner_dsd_dvb_t(fdtv, params); break;
Stefan Richter15490792009-02-23 14:21:10 +0100557 default:
558 BUG();
559 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300560 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100561#if 0
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300562 /*
563 * FIXME:
564 * u8 *status was an out-parameter of avc_tuner_dsd, unused by caller.
565 * Check for AVC_RESPONSE_ACCEPTED here instead?
566 */
Stefan Richter15490792009-02-23 14:21:10 +0100567 if (status)
568 *status = r->operand[2];
569#endif
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300570 mutex_unlock(&fdtv->avc_mutex);
571
572 if (ret == 0)
573 msleep(500);
574
575 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100576}
577
578int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
579{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300580 struct avc_command_frame *c = (void *)fdtv->avc_data;
581 int ret, pos, k;
Stefan Richter15490792009-02-23 14:21:10 +0100582
583 if (pidc > 16 && pidc != 0xff)
584 return -EINVAL;
585
Stefan Richter6385c5b2009-11-18 16:03:03 -0300586 mutex_lock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300587
Stefan Richter15490792009-02-23 14:21:10 +0100588 memset(c, 0, sizeof(*c));
589
590 c->ctype = AVC_CTYPE_CONTROL;
591 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
592 c->opcode = AVC_OPCODE_DSD;
593
594 c->operand[0] = 0; /* source plug */
595 c->operand[1] = 0xd2; /* subfunction replace */
596 c->operand[2] = 0x20; /* system id = DVB */
597 c->operand[3] = 0x00; /* antenna number */
598 c->operand[4] = 0x00; /* system_specific_multiplex selection_length */
599 c->operand[5] = pidc; /* Nr_of_dsd_sel_specs */
600
601 pos = 6;
602 if (pidc != 0xff)
603 for (k = 0; k < pidc; k++) {
604 c->operand[pos++] = 0x13; /* flowfunction relay */
605 c->operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
606 c->operand[pos++] = (pid[k] >> 8) & 0x1f;
607 c->operand[pos++] = pid[k] & 0xff;
608 c->operand[pos++] = 0x00; /* tableID */
609 c->operand[pos++] = 0x00; /* filter_length */
610 }
611
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300612 fdtv->avc_data_length = ALIGN(3 + pos, 4);
613 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100614
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300615 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +0100616
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300617 mutex_unlock(&fdtv->avc_mutex);
618
619 if (ret == 0)
620 msleep(50);
621
622 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100623}
624
625int avc_tuner_get_ts(struct firedtv *fdtv)
626{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300627 struct avc_command_frame *c = (void *)fdtv->avc_data;
628 int ret, sl;
629
Stefan Richter6385c5b2009-11-18 16:03:03 -0300630 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100631
632 memset(c, 0, sizeof(*c));
633
634 c->ctype = AVC_CTYPE_CONTROL;
635 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
636 c->opcode = AVC_OPCODE_DSIT;
637
638 sl = fdtv->type == FIREDTV_DVB_T ? 0x0c : 0x11;
639
640 c->operand[0] = 0; /* source plug */
641 c->operand[1] = 0xd2; /* subfunction replace */
642 c->operand[2] = 0xff; /* status */
643 c->operand[3] = 0x20; /* system id = DVB */
644 c->operand[4] = 0x00; /* antenna number */
645 c->operand[5] = 0x0; /* system_specific_search_flags */
646 c->operand[6] = sl; /* system_specific_multiplex selection_length */
647 c->operand[7] = 0x00; /* valid_flags [0] */
648 c->operand[8] = 0x00; /* valid_flags [1] */
649 c->operand[7 + sl] = 0x00; /* nr_of_dsit_sel_specs (always 0) */
650
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300651 fdtv->avc_data_length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
652 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100653
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300654 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +0100655
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300656 mutex_unlock(&fdtv->avc_mutex);
657
658 if (ret == 0)
659 msleep(250);
660
661 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100662}
663
664int avc_identify_subunit(struct firedtv *fdtv)
665{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300666 struct avc_command_frame *c = (void *)fdtv->avc_data;
667 struct avc_response_frame *r = (void *)fdtv->avc_data;
668 int ret;
669
Stefan Richter6385c5b2009-11-18 16:03:03 -0300670 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100671
672 memset(c, 0, sizeof(*c));
673
674 c->ctype = AVC_CTYPE_CONTROL;
675 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
676 c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
677
678 c->operand[0] = DESCRIPTOR_SUBUNIT_IDENTIFIER;
679 c->operand[1] = 0xff;
680 c->operand[2] = 0x00;
681 c->operand[3] = 0x00; /* length highbyte */
682 c->operand[4] = 0x08; /* length lowbyte */
683 c->operand[5] = 0x00; /* offset highbyte */
684 c->operand[6] = 0x0d; /* offset lowbyte */
685
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300686 fdtv->avc_data_length = 12;
687 ret = avc_write(fdtv);
688 if (ret < 0)
689 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100690
691 if ((r->response != AVC_RESPONSE_STABLE &&
692 r->response != AVC_RESPONSE_ACCEPTED) ||
693 (r->operand[3] << 8) + r->operand[4] != 8) {
694 dev_err(fdtv->device, "cannot read subunit identifier\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300695 ret = -EINVAL;
Stefan Richter15490792009-02-23 14:21:10 +0100696 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300697out:
698 mutex_unlock(&fdtv->avc_mutex);
699
700 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100701}
702
703#define SIZEOF_ANTENNA_INPUT_INFO 22
704
705int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
706{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300707 struct avc_command_frame *c = (void *)fdtv->avc_data;
708 struct avc_response_frame *r = (void *)fdtv->avc_data;
709 int length, ret;
710
Stefan Richter6385c5b2009-11-18 16:03:03 -0300711 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100712
713 memset(c, 0, sizeof(*c));
714
715 c->ctype = AVC_CTYPE_CONTROL;
716 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
717 c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
718
719 c->operand[0] = DESCRIPTOR_TUNER_STATUS;
720 c->operand[1] = 0xff; /* read_result_status */
721 c->operand[2] = 0x00; /* reserved */
722 c->operand[3] = 0; /* SIZEOF_ANTENNA_INPUT_INFO >> 8; */
723 c->operand[4] = 0; /* SIZEOF_ANTENNA_INPUT_INFO & 0xff; */
724 c->operand[5] = 0x00;
725 c->operand[6] = 0x00;
726
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300727 fdtv->avc_data_length = 12;
728 ret = avc_write(fdtv);
729 if (ret < 0)
730 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100731
732 if (r->response != AVC_RESPONSE_STABLE &&
733 r->response != AVC_RESPONSE_ACCEPTED) {
734 dev_err(fdtv->device, "cannot read tuner status\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300735 ret = -EINVAL;
736 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100737 }
738
739 length = r->operand[9];
740 if (r->operand[1] != 0x10 || length != SIZEOF_ANTENNA_INPUT_INFO) {
741 dev_err(fdtv->device, "got invalid tuner status\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300742 ret = -EINVAL;
743 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100744 }
745
746 stat->active_system = r->operand[10];
747 stat->searching = r->operand[11] >> 7 & 1;
748 stat->moving = r->operand[11] >> 6 & 1;
749 stat->no_rf = r->operand[11] >> 5 & 1;
750 stat->input = r->operand[12] >> 7 & 1;
751 stat->selected_antenna = r->operand[12] & 0x7f;
752 stat->ber = r->operand[13] << 24 |
753 r->operand[14] << 16 |
754 r->operand[15] << 8 |
755 r->operand[16];
756 stat->signal_strength = r->operand[17];
757 stat->raster_frequency = r->operand[18] >> 6 & 2;
758 stat->rf_frequency = (r->operand[18] & 0x3f) << 16 |
759 r->operand[19] << 8 |
760 r->operand[20];
761 stat->man_dep_info_length = r->operand[21];
762 stat->front_end_error = r->operand[22] >> 4 & 1;
763 stat->antenna_error = r->operand[22] >> 3 & 1;
764 stat->front_end_power_status = r->operand[22] >> 1 & 1;
765 stat->power_supply = r->operand[22] & 1;
766 stat->carrier_noise_ratio = r->operand[23] << 8 |
767 r->operand[24];
768 stat->power_supply_voltage = r->operand[27];
769 stat->antenna_voltage = r->operand[28];
770 stat->firewire_bus_voltage = r->operand[29];
771 stat->ca_mmi = r->operand[30] & 1;
772 stat->ca_pmt_reply = r->operand[31] >> 7 & 1;
773 stat->ca_date_time_request = r->operand[31] >> 6 & 1;
774 stat->ca_application_info = r->operand[31] >> 5 & 1;
775 stat->ca_module_present_status = r->operand[31] >> 4 & 1;
776 stat->ca_dvb_flag = r->operand[31] >> 3 & 1;
777 stat->ca_error_flag = r->operand[31] >> 2 & 1;
778 stat->ca_initialization_status = r->operand[31] >> 1 & 1;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300779out:
780 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100781
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300782 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100783}
784
785int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
786 char conttone, char nrdiseq,
787 struct dvb_diseqc_master_cmd *diseqcmd)
788{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300789 struct avc_command_frame *c = (void *)fdtv->avc_data;
790 struct avc_response_frame *r = (void *)fdtv->avc_data;
791 int i, j, k, ret;
792
Stefan Richter6385c5b2009-11-18 16:03:03 -0300793 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100794
795 memset(c, 0, sizeof(*c));
796
797 c->ctype = AVC_CTYPE_CONTROL;
798 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
799 c->opcode = AVC_OPCODE_VENDOR;
800
801 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
802 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
803 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
804 c->operand[3] = SFE_VENDOR_OPCODE_LNB_CONTROL;
805
806 c->operand[4] = voltage;
807 c->operand[5] = nrdiseq;
808
809 i = 6;
810
811 for (j = 0; j < nrdiseq; j++) {
812 c->operand[i++] = diseqcmd[j].msg_len;
813
814 for (k = 0; k < diseqcmd[j].msg_len; k++)
815 c->operand[i++] = diseqcmd[j].msg[k];
816 }
817
818 c->operand[i++] = burst;
819 c->operand[i++] = conttone;
820
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300821 fdtv->avc_data_length = ALIGN(3 + i, 4);
822 ret = avc_write(fdtv);
823 if (ret < 0)
824 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100825
826 if (r->response != AVC_RESPONSE_ACCEPTED) {
827 dev_err(fdtv->device, "LNB control failed\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300828 ret = -EINVAL;
Stefan Richter15490792009-02-23 14:21:10 +0100829 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300830out:
831 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100832
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300833 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100834}
835
836int avc_register_remote_control(struct firedtv *fdtv)
837{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300838 struct avc_command_frame *c = (void *)fdtv->avc_data;
839 int ret;
840
Stefan Richter6385c5b2009-11-18 16:03:03 -0300841 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100842
843 memset(c, 0, sizeof(*c));
844
845 c->ctype = AVC_CTYPE_NOTIFY;
846 c->subunit = AVC_SUBUNIT_TYPE_UNIT | 7;
847 c->opcode = AVC_OPCODE_VENDOR;
848
849 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
850 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
851 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
852 c->operand[3] = SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
853
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300854 fdtv->avc_data_length = 8;
855 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100856
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300857 /* FIXME: check response code? */
858
859 mutex_unlock(&fdtv->avc_mutex);
860
861 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100862}
863
864void avc_remote_ctrl_work(struct work_struct *work)
865{
866 struct firedtv *fdtv =
867 container_of(work, struct firedtv, remote_ctrl_work);
868
869 /* Should it be rescheduled in failure cases? */
870 avc_register_remote_control(fdtv);
871}
872
873#if 0 /* FIXME: unused */
874int avc_tuner_host2ca(struct firedtv *fdtv)
875{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300876 struct avc_command_frame *c = (void *)fdtv->avc_data;
877 int ret;
878
Stefan Richter6385c5b2009-11-18 16:03:03 -0300879 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100880
881 memset(c, 0, sizeof(*c));
882
883 c->ctype = AVC_CTYPE_CONTROL;
884 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
885 c->opcode = AVC_OPCODE_VENDOR;
886
887 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
888 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
889 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
890 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
891 c->operand[4] = 0; /* slot */
892 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
893 c->operand[6] = 0; /* more/last */
894 c->operand[7] = 0; /* length */
895
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300896 fdtv->avc_data_length = 12;
897 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +0100898
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300899 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +0100900
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300901 mutex_unlock(&fdtv->avc_mutex);
902
903 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100904}
905#endif
906
907static int get_ca_object_pos(struct avc_response_frame *r)
908{
909 int length = 1;
910
911 /* Check length of length field */
912 if (r->operand[7] & 0x80)
913 length = (r->operand[7] & 0x7f) + 1;
914 return length + 7;
915}
916
917static int get_ca_object_length(struct avc_response_frame *r)
918{
919#if 0 /* FIXME: unused */
920 int size = 0;
921 int i;
922
923 if (r->operand[7] & 0x80)
924 for (i = 0; i < (r->operand[7] & 0x7f); i++) {
925 size <<= 8;
926 size += r->operand[8 + i];
927 }
928#endif
929 return r->operand[7];
930}
931
932int avc_ca_app_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
933{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300934 struct avc_command_frame *c = (void *)fdtv->avc_data;
935 struct avc_response_frame *r = (void *)fdtv->avc_data;
936 int pos, ret;
937
Stefan Richter6385c5b2009-11-18 16:03:03 -0300938 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100939
940 memset(c, 0, sizeof(*c));
941
942 c->ctype = AVC_CTYPE_STATUS;
943 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
944 c->opcode = AVC_OPCODE_VENDOR;
945
946 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
947 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
948 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
949 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
950 c->operand[4] = 0; /* slot */
951 c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
952
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300953 fdtv->avc_data_length = 12;
954 ret = avc_write(fdtv);
955 if (ret < 0)
956 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100957
958 /* FIXME: check response code and validate response data */
959
960 pos = get_ca_object_pos(r);
961 app_info[0] = (EN50221_TAG_APP_INFO >> 16) & 0xff;
962 app_info[1] = (EN50221_TAG_APP_INFO >> 8) & 0xff;
963 app_info[2] = (EN50221_TAG_APP_INFO >> 0) & 0xff;
964 app_info[3] = 6 + r->operand[pos + 4];
965 app_info[4] = 0x01;
966 memcpy(&app_info[5], &r->operand[pos], 5 + r->operand[pos + 4]);
967 *len = app_info[3] + 4;
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300968out:
969 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100970
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300971 return ret;
Stefan Richter15490792009-02-23 14:21:10 +0100972}
973
974int avc_ca_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
975{
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300976 struct avc_command_frame *c = (void *)fdtv->avc_data;
977 struct avc_response_frame *r = (void *)fdtv->avc_data;
978 int pos, ret;
979
Stefan Richter6385c5b2009-11-18 16:03:03 -0300980 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100981
982 memset(c, 0, sizeof(*c));
983
984 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 */
994
Stefan Richter3fb80ef2009-11-18 16:02:01 -0300995 fdtv->avc_data_length = 12;
996 ret = avc_write(fdtv);
997 if (ret < 0)
998 goto out;
Stefan Richter15490792009-02-23 14:21:10 +0100999
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001000 /* FIXME: check response code and validate response data */
Stefan Richter15490792009-02-23 14:21:10 +01001001
1002 pos = get_ca_object_pos(r);
1003 app_info[0] = (EN50221_TAG_CA_INFO >> 16) & 0xff;
1004 app_info[1] = (EN50221_TAG_CA_INFO >> 8) & 0xff;
1005 app_info[2] = (EN50221_TAG_CA_INFO >> 0) & 0xff;
1006 app_info[3] = 2;
1007 app_info[4] = r->operand[pos + 0];
1008 app_info[5] = r->operand[pos + 1];
1009 *len = app_info[3] + 4;
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001010out:
1011 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001012
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001013 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001014}
1015
1016int avc_ca_reset(struct firedtv *fdtv)
1017{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001018 struct avc_command_frame *c = (void *)fdtv->avc_data;
1019 int ret;
1020
Stefan Richter6385c5b2009-11-18 16:03:03 -03001021 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001022
1023 memset(c, 0, sizeof(*c));
1024
1025 c->ctype = AVC_CTYPE_CONTROL;
1026 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1027 c->opcode = AVC_OPCODE_VENDOR;
1028
1029 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1030 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1031 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1032 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1033 c->operand[4] = 0; /* slot */
1034 c->operand[5] = SFE_VENDOR_TAG_CA_RESET; /* ca tag */
1035 c->operand[6] = 0; /* more/last */
1036 c->operand[7] = 1; /* length */
1037 c->operand[8] = 0; /* force hardware reset */
1038
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001039 fdtv->avc_data_length = 12;
1040 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +01001041
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001042 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +01001043
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001044 mutex_unlock(&fdtv->avc_mutex);
1045
1046 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001047}
1048
1049int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
1050{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001051 struct avc_command_frame *c = (void *)fdtv->avc_data;
1052 struct avc_response_frame *r = (void *)fdtv->avc_data;
Stefan Richter15490792009-02-23 14:21:10 +01001053 int list_management;
1054 int program_info_length;
1055 int pmt_cmd_id;
1056 int read_pos;
1057 int write_pos;
1058 int es_info_length;
1059 int crc32_csum;
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001060 int ret;
Stefan Richter15490792009-02-23 14:21:10 +01001061
Henrik Kurelid15344772009-08-01 08:04:06 -03001062 if (unlikely(avc_debug & AVC_DEBUG_APPLICATION_PMT))
1063 debug_pmt(msg, length);
1064
Stefan Richter6385c5b2009-11-18 16:03:03 -03001065 mutex_lock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001066
Stefan Richter15490792009-02-23 14:21:10 +01001067 memset(c, 0, sizeof(*c));
1068
1069 c->ctype = AVC_CTYPE_CONTROL;
1070 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1071 c->opcode = AVC_OPCODE_VENDOR;
1072
1073 if (msg[0] != EN50221_LIST_MANAGEMENT_ONLY) {
1074 dev_info(fdtv->device, "forcing list_management to ONLY\n");
1075 msg[0] = EN50221_LIST_MANAGEMENT_ONLY;
1076 }
1077 /* We take the cmd_id from the programme level only! */
1078 list_management = msg[0];
1079 program_info_length = ((msg[4] & 0x0f) << 8) + msg[5];
1080 if (program_info_length > 0)
1081 program_info_length--; /* Remove pmt_cmd_id */
1082 pmt_cmd_id = msg[6];
1083
1084 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1085 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1086 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1087 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1088 c->operand[4] = 0; /* slot */
1089 c->operand[5] = SFE_VENDOR_TAG_CA_PMT; /* ca tag */
1090 c->operand[6] = 0; /* more/last */
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001091 /* Use three bytes for length field in case length > 127 */
1092 c->operand[10] = list_management;
1093 c->operand[11] = 0x01; /* pmt_cmd=OK_descramble */
Stefan Richter15490792009-02-23 14:21:10 +01001094
1095 /* TS program map table */
1096
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001097 c->operand[12] = 0x02; /* Table id=2 */
1098 c->operand[13] = 0x80; /* Section syntax + length */
1099 /* c->operand[14] = XXXprogram_info_length + 12; */
1100 c->operand[15] = msg[1]; /* Program number */
1101 c->operand[16] = msg[2];
1102 c->operand[17] = 0x01; /* Version number=0 + current/next=1 */
1103 c->operand[18] = 0x00; /* Section number=0 */
1104 c->operand[19] = 0x00; /* Last section number=0 */
1105 c->operand[20] = 0x1f; /* PCR_PID=1FFF */
1106 c->operand[21] = 0xff;
1107 c->operand[22] = (program_info_length >> 8); /* Program info length */
1108 c->operand[23] = (program_info_length & 0xff);
Stefan Richter15490792009-02-23 14:21:10 +01001109
1110 /* CA descriptors at programme level */
1111 read_pos = 6;
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001112 write_pos = 24;
Stefan Richter15490792009-02-23 14:21:10 +01001113 if (program_info_length > 0) {
1114 pmt_cmd_id = msg[read_pos++];
1115 if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1116 dev_err(fdtv->device,
1117 "invalid pmt_cmd_id %d\n", pmt_cmd_id);
1118
1119 memcpy(&c->operand[write_pos], &msg[read_pos],
1120 program_info_length);
1121 read_pos += program_info_length;
1122 write_pos += program_info_length;
1123 }
1124 while (read_pos < length) {
1125 c->operand[write_pos++] = msg[read_pos++];
1126 c->operand[write_pos++] = msg[read_pos++];
1127 c->operand[write_pos++] = msg[read_pos++];
1128 es_info_length =
1129 ((msg[read_pos] & 0x0f) << 8) + msg[read_pos + 1];
1130 read_pos += 2;
1131 if (es_info_length > 0)
1132 es_info_length--; /* Remove pmt_cmd_id */
1133 c->operand[write_pos++] = es_info_length >> 8;
1134 c->operand[write_pos++] = es_info_length & 0xff;
1135 if (es_info_length > 0) {
1136 pmt_cmd_id = msg[read_pos++];
1137 if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1138 dev_err(fdtv->device, "invalid pmt_cmd_id %d "
1139 "at stream level\n", pmt_cmd_id);
1140
1141 memcpy(&c->operand[write_pos], &msg[read_pos],
1142 es_info_length);
1143 read_pos += es_info_length;
1144 write_pos += es_info_length;
1145 }
1146 }
1147
1148 /* CRC */
1149 c->operand[write_pos++] = 0x00;
1150 c->operand[write_pos++] = 0x00;
1151 c->operand[write_pos++] = 0x00;
1152 c->operand[write_pos++] = 0x00;
1153
Henrik Kurelidc94115f2009-10-03 05:37:58 -03001154 c->operand[7] = 0x82;
1155 c->operand[8] = (write_pos - 10) >> 8;
1156 c->operand[9] = (write_pos - 10) & 0xff;
1157 c->operand[14] = write_pos - 15;
Stefan Richter15490792009-02-23 14:21:10 +01001158
1159 crc32_csum = crc32_be(0, &c->operand[10], c->operand[12] - 1);
1160 c->operand[write_pos - 4] = (crc32_csum >> 24) & 0xff;
1161 c->operand[write_pos - 3] = (crc32_csum >> 16) & 0xff;
1162 c->operand[write_pos - 2] = (crc32_csum >> 8) & 0xff;
1163 c->operand[write_pos - 1] = (crc32_csum >> 0) & 0xff;
1164
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001165 fdtv->avc_data_length = ALIGN(3 + write_pos, 4);
1166 ret = avc_write(fdtv);
1167 if (ret < 0)
1168 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001169
1170 if (r->response != AVC_RESPONSE_ACCEPTED) {
1171 dev_err(fdtv->device,
1172 "CA PMT failed with response 0x%x\n", r->response);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001173 ret = -EFAULT;
Stefan Richter15490792009-02-23 14:21:10 +01001174 }
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001175out:
1176 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001177
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001178 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001179}
1180
1181int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
1182{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001183 struct avc_command_frame *c = (void *)fdtv->avc_data;
1184 struct avc_response_frame *r = (void *)fdtv->avc_data;
1185 int ret;
1186
Stefan Richter6385c5b2009-11-18 16:03:03 -03001187 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001188
1189 memset(c, 0, sizeof(*c));
1190
1191 c->ctype = AVC_CTYPE_STATUS;
1192 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1193 c->opcode = AVC_OPCODE_VENDOR;
1194
1195 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1196 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1197 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1198 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1199 c->operand[4] = 0; /* slot */
1200 c->operand[5] = SFE_VENDOR_TAG_CA_DATE_TIME; /* ca tag */
1201 c->operand[6] = 0; /* more/last */
1202 c->operand[7] = 0; /* length */
1203
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001204 fdtv->avc_data_length = 12;
1205 ret = avc_write(fdtv);
1206 if (ret < 0)
1207 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001208
1209 /* FIXME: check response code and validate response data */
1210
1211 *interval = r->operand[get_ca_object_pos(r)];
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001212out:
1213 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001214
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001215 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001216}
1217
1218int avc_ca_enter_menu(struct firedtv *fdtv)
1219{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001220 struct avc_command_frame *c = (void *)fdtv->avc_data;
1221 int ret;
1222
Stefan Richter6385c5b2009-11-18 16:03:03 -03001223 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001224
1225 memset(c, 0, sizeof(*c));
1226
1227 c->ctype = AVC_CTYPE_STATUS;
1228 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1229 c->opcode = AVC_OPCODE_VENDOR;
1230
1231 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1232 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1233 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1234 c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
1235 c->operand[4] = 0; /* slot */
1236 c->operand[5] = SFE_VENDOR_TAG_CA_ENTER_MENU;
1237 c->operand[6] = 0; /* more/last */
1238 c->operand[7] = 0; /* length */
1239
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001240 fdtv->avc_data_length = 12;
1241 ret = avc_write(fdtv);
Stefan Richter15490792009-02-23 14:21:10 +01001242
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001243 /* FIXME: check response code? */
Stefan Richter15490792009-02-23 14:21:10 +01001244
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001245 mutex_unlock(&fdtv->avc_mutex);
1246
1247 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001248}
1249
1250int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
1251{
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001252 struct avc_command_frame *c = (void *)fdtv->avc_data;
1253 struct avc_response_frame *r = (void *)fdtv->avc_data;
1254 int ret;
1255
Stefan Richter6385c5b2009-11-18 16:03:03 -03001256 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001257
1258 memset(c, 0, sizeof(*c));
1259
1260 c->ctype = AVC_CTYPE_STATUS;
1261 c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
1262 c->opcode = AVC_OPCODE_VENDOR;
1263
1264 c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
1265 c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
1266 c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
1267 c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
1268 c->operand[4] = 0; /* slot */
1269 c->operand[5] = SFE_VENDOR_TAG_CA_MMI;
1270 c->operand[6] = 0; /* more/last */
1271 c->operand[7] = 0; /* length */
1272
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001273 fdtv->avc_data_length = 12;
1274 ret = avc_write(fdtv);
1275 if (ret < 0)
1276 goto out;
Stefan Richter15490792009-02-23 14:21:10 +01001277
1278 /* FIXME: check response code and validate response data */
1279
1280 *len = get_ca_object_length(r);
1281 memcpy(mmi_object, &r->operand[get_ca_object_pos(r)], *len);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001282out:
1283 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001284
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001285 return ret;
Stefan Richter15490792009-02-23 14:21:10 +01001286}
1287
1288#define CMP_OUTPUT_PLUG_CONTROL_REG_0 0xfffff0000904ULL
1289
Stefan Richter53756592009-11-18 16:01:34 -03001290static int cmp_read(struct firedtv *fdtv, u64 addr, __be32 *data)
Stefan Richter15490792009-02-23 14:21:10 +01001291{
1292 int ret;
1293
Stefan Richter6385c5b2009-11-18 16:03:03 -03001294 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001295
Stefan Richter53756592009-11-18 16:01:34 -03001296 ret = fdtv->backend->read(fdtv, addr, data);
Stefan Richter15490792009-02-23 14:21:10 +01001297 if (ret < 0)
1298 dev_err(fdtv->device, "CMP: read I/O error\n");
1299
1300 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001301
Stefan Richter15490792009-02-23 14:21:10 +01001302 return ret;
1303}
1304
Stefan Richter054286b2009-11-08 18:29:08 -03001305static int cmp_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
Stefan Richter15490792009-02-23 14:21:10 +01001306{
1307 int ret;
1308
Stefan Richter6385c5b2009-11-18 16:03:03 -03001309 mutex_lock(&fdtv->avc_mutex);
Stefan Richter15490792009-02-23 14:21:10 +01001310
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001311 /* data[] is stack-allocated and should not be DMA-mapped. */
1312 memcpy(fdtv->avc_data, data, 8);
1313
1314 ret = fdtv->backend->lock(fdtv, addr, fdtv->avc_data);
Stefan Richter15490792009-02-23 14:21:10 +01001315 if (ret < 0)
1316 dev_err(fdtv->device, "CMP: lock I/O error\n");
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001317 else
1318 memcpy(data, fdtv->avc_data, 8);
Stefan Richter15490792009-02-23 14:21:10 +01001319
1320 mutex_unlock(&fdtv->avc_mutex);
Stefan Richter3fb80ef2009-11-18 16:02:01 -03001321
Stefan Richter15490792009-02-23 14:21:10 +01001322 return ret;
1323}
1324
1325static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
1326{
1327 return (be32_to_cpu(opcr) >> shift) & mask;
1328}
1329
1330static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
1331{
1332 *opcr &= ~cpu_to_be32(mask << shift);
1333 *opcr |= cpu_to_be32((value & mask) << shift);
1334}
1335
1336#define get_opcr_online(v) get_opcr((v), 0x1, 31)
1337#define get_opcr_p2p_connections(v) get_opcr((v), 0x3f, 24)
1338#define get_opcr_channel(v) get_opcr((v), 0x3f, 16)
1339
1340#define set_opcr_p2p_connections(p, v) set_opcr((p), (v), 0x3f, 24)
1341#define set_opcr_channel(p, v) set_opcr((p), (v), 0x3f, 16)
1342#define set_opcr_data_rate(p, v) set_opcr((p), (v), 0x3, 14)
1343#define set_opcr_overhead_id(p, v) set_opcr((p), (v), 0xf, 10)
1344
1345int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
1346{
Stefan Richter054286b2009-11-08 18:29:08 -03001347 __be32 old_opcr, opcr[2];
Stefan Richter15490792009-02-23 14:21:10 +01001348 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1349 int attempts = 0;
1350 int ret;
1351
Stefan Richter53756592009-11-18 16:01:34 -03001352 ret = cmp_read(fdtv, opcr_address, opcr);
Stefan Richter15490792009-02-23 14:21:10 +01001353 if (ret < 0)
1354 return ret;
1355
1356repeat:
Stefan Richter054286b2009-11-08 18:29:08 -03001357 if (!get_opcr_online(*opcr)) {
Stefan Richter15490792009-02-23 14:21:10 +01001358 dev_err(fdtv->device, "CMP: output offline\n");
1359 return -EBUSY;
1360 }
1361
Stefan Richter054286b2009-11-08 18:29:08 -03001362 old_opcr = *opcr;
Stefan Richter15490792009-02-23 14:21:10 +01001363
Stefan Richter054286b2009-11-08 18:29:08 -03001364 if (get_opcr_p2p_connections(*opcr)) {
1365 if (get_opcr_channel(*opcr) != channel) {
Stefan Richter15490792009-02-23 14:21:10 +01001366 dev_err(fdtv->device, "CMP: cannot change channel\n");
1367 return -EBUSY;
1368 }
1369 dev_info(fdtv->device, "CMP: overlaying connection\n");
1370
1371 /* We don't allocate isochronous resources. */
1372 } else {
Stefan Richter054286b2009-11-08 18:29:08 -03001373 set_opcr_channel(opcr, channel);
1374 set_opcr_data_rate(opcr, 2); /* S400 */
Stefan Richter15490792009-02-23 14:21:10 +01001375
1376 /* FIXME: this is for the worst case - optimize */
Stefan Richter054286b2009-11-08 18:29:08 -03001377 set_opcr_overhead_id(opcr, 0);
Stefan Richter15490792009-02-23 14:21:10 +01001378
1379 /*
1380 * FIXME: allocate isochronous channel and bandwidth at IRM
1381 * fdtv->backend->alloc_resources(fdtv, channels_mask, bw);
1382 */
1383 }
1384
Stefan Richter054286b2009-11-08 18:29:08 -03001385 set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) + 1);
Stefan Richter15490792009-02-23 14:21:10 +01001386
Stefan Richter054286b2009-11-08 18:29:08 -03001387 opcr[1] = *opcr;
1388 opcr[0] = old_opcr;
1389
1390 ret = cmp_lock(fdtv, opcr_address, opcr);
Stefan Richter15490792009-02-23 14:21:10 +01001391 if (ret < 0)
1392 return ret;
1393
Stefan Richter054286b2009-11-08 18:29:08 -03001394 if (old_opcr != *opcr) {
Stefan Richter15490792009-02-23 14:21:10 +01001395 /*
1396 * FIXME: if old_opcr.P2P_Connections > 0,
1397 * deallocate isochronous channel and bandwidth at IRM
1398 * if (...)
1399 * fdtv->backend->dealloc_resources(fdtv, channel, bw);
1400 */
1401
1402 if (++attempts < 6) /* arbitrary limit */
1403 goto repeat;
1404 return -EBUSY;
1405 }
1406
1407 return 0;
1408}
1409
1410void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
1411{
Stefan Richter054286b2009-11-08 18:29:08 -03001412 __be32 old_opcr, opcr[2];
Stefan Richter15490792009-02-23 14:21:10 +01001413 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1414 int attempts = 0;
1415
Stefan Richter53756592009-11-18 16:01:34 -03001416 if (cmp_read(fdtv, opcr_address, opcr) < 0)
Stefan Richter15490792009-02-23 14:21:10 +01001417 return;
1418
1419repeat:
Stefan Richter054286b2009-11-08 18:29:08 -03001420 if (!get_opcr_online(*opcr) || !get_opcr_p2p_connections(*opcr) ||
1421 get_opcr_channel(*opcr) != channel) {
Stefan Richter15490792009-02-23 14:21:10 +01001422 dev_err(fdtv->device, "CMP: no connection to break\n");
1423 return;
1424 }
1425
Stefan Richter054286b2009-11-08 18:29:08 -03001426 old_opcr = *opcr;
1427 set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) - 1);
Stefan Richter15490792009-02-23 14:21:10 +01001428
Stefan Richter054286b2009-11-08 18:29:08 -03001429 opcr[1] = *opcr;
1430 opcr[0] = old_opcr;
1431
1432 if (cmp_lock(fdtv, opcr_address, opcr) < 0)
Stefan Richter15490792009-02-23 14:21:10 +01001433 return;
1434
Stefan Richter054286b2009-11-08 18:29:08 -03001435 if (old_opcr != *opcr) {
Stefan Richter15490792009-02-23 14:21:10 +01001436 /*
1437 * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
1438 * owner, deallocate isochronous channel and bandwidth at IRM
1439 * if (...)
1440 * fdtv->backend->dealloc_resources(fdtv, channel, bw);
1441 */
1442
1443 if (++attempts < 6) /* arbitrary limit */
1444 goto repeat;
1445 }
1446}