]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/dvb/firewire/firedtv-avc.c
e706428112170eee41981b92d6574cce5209ebec
[net-next-2.6.git] / drivers / media / dvb / firewire / firedtv-avc.c
1 /*
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
76 struct avc_command_frame {
77         u8 ctype;
78         u8 subunit;
79         u8 opcode;
80         u8 operand[509];
81 };
82
83 struct avc_response_frame {
84         u8 response;
85         u8 subunit;
86         u8 opcode;
87         u8 operand[509];
88 };
89
90 #define AVC_DEBUG_READ_DESCRIPTOR              0x0001
91 #define AVC_DEBUG_DSIT                         0x0002
92 #define AVC_DEBUG_DSD                          0x0004
93 #define AVC_DEBUG_REGISTER_REMOTE_CONTROL      0x0008
94 #define AVC_DEBUG_LNB_CONTROL                  0x0010
95 #define AVC_DEBUG_TUNE_QPSK                    0x0020
96 #define AVC_DEBUG_TUNE_QPSK2                   0x0040
97 #define AVC_DEBUG_HOST2CA                      0x0080
98 #define AVC_DEBUG_CA2HOST                      0x0100
99 #define AVC_DEBUG_APPLICATION_PMT              0x4000
100 #define AVC_DEBUG_FCP_PAYLOADS                 0x8000
101
102 static int avc_debug;
103 module_param_named(debug, avc_debug, int, 0644);
104 MODULE_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)");
117
118 static 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
133 static const char *debug_fcp_opcode(unsigned int opcode,
134                                     const u8 *data, int length)
135 {
136         switch (opcode) {
137         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";
149         }
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)
155                 return "Vendor/Unknown";
156
157         switch (data[6]) {
158         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;
171         }
172         return "Vendor/Unknown";
173 }
174
175 static void debug_fcp(const u8 *data, int length)
176 {
177         unsigned int subunit_type, subunit_id, opcode;
178         const char *op, *prefix;
179
180         prefix       = data[0] > 7 ? "FCP <- " : "FCP -> ";
181         subunit_type = data[1] >> 3;
182         subunit_id   = data[1] & 7;
183         opcode       = subunit_type == 0x1e || subunit_id == 5 ? ~0 : data[2];
184         op           = debug_fcp_opcode(opcode, data, length);
185
186         if (op) {
187                 printk(KERN_INFO "%ssu=%x.%x l=%d: %-8s - %s\n",
188                        prefix, subunit_type, subunit_id, length,
189                        debug_fcp_ctype(data[0]), op);
190                 if (avc_debug & AVC_DEBUG_FCP_PAYLOADS)
191                         print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_NONE,
192                                        16, 1, data, length, false);
193         }
194 }
195
196 static 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);
201 }
202
203 static int avc_write(struct firedtv *fdtv)
204 {
205         int err, retry;
206
207         fdtv->avc_reply_received = false;
208
209         for (retry = 0; retry < 6; retry++) {
210                 if (unlikely(avc_debug))
211                         debug_fcp(fdtv->avc_data, fdtv->avc_data_length);
212
213                 err = fdtv->backend->write(fdtv, FCP_COMMAND_REGISTER,
214                                 fdtv->avc_data, fdtv->avc_data_length);
215                 if (err) {
216                         dev_err(fdtv->device, "FCP command write failed\n");
217
218                         return err;
219                 }
220
221                 /*
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,
227                                        msecs_to_jiffies(200)) != 0)
228                         return 0;
229         }
230         dev_err(fdtv->device, "FCP response timed out\n");
231
232         return -ETIMEDOUT;
233 }
234
235 static bool is_register_rc(struct avc_response_frame *r)
236 {
237         return r->opcode     == AVC_OPCODE_VENDOR &&
238                r->operand[0] == SFE_VENDOR_DE_COMPANYID_0 &&
239                r->operand[1] == SFE_VENDOR_DE_COMPANYID_1 &&
240                r->operand[2] == SFE_VENDOR_DE_COMPANYID_2 &&
241                r->operand[3] == SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
242 }
243
244 int avc_recv(struct firedtv *fdtv, void *data, size_t length)
245 {
246         struct avc_response_frame *r = data;
247
248         if (unlikely(avc_debug))
249                 debug_fcp(data, length);
250
251         if (length >= 8 && is_register_rc(r)) {
252                 switch (r->response) {
253                 case AVC_RESPONSE_CHANGED:
254                         fdtv_handle_rc(fdtv, r->operand[4] << 8 | r->operand[5]);
255                         schedule_work(&fdtv->remote_ctrl_work);
256                         break;
257                 case AVC_RESPONSE_INTERIM:
258                         if (is_register_rc((void *)fdtv->avc_data))
259                                 goto wake;
260                         break;
261                 default:
262                         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
273         memcpy(fdtv->avc_data, data, length);
274         fdtv->avc_data_length = length;
275 wake:
276         fdtv->avc_reply_received = true;
277         wake_up(&fdtv->avc_wait);
278
279         return 0;
280 }
281
282 static 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
302 /*
303  * tuning command for setting the relative LNB frequency
304  * (not supported by the AVC standard)
305  */
306 static void avc_tuner_tuneqpsk(struct firedtv *fdtv,
307                                struct dvb_frontend_parameters *params)
308 {
309         struct avc_command_frame *c = (void *)fdtv->avc_data;
310
311         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;
316         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;
320
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;
359                 fdtv->avc_data_length = 20;
360         } else {
361                 fdtv->avc_data_length = 16;
362         }
363 }
364
365 static void avc_tuner_dsd_dvb_c(struct firedtv *fdtv,
366                                 struct dvb_frontend_parameters *params)
367 {
368         struct avc_command_frame *c = (void *)fdtv->avc_data;
369
370         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;
429
430         /* Add PIDs to filter */
431         fdtv->avc_data_length =
432                 ALIGN(22 + add_pid_filter(fdtv, &c->operand[22]) + 3, 4);
433 }
434
435 static void avc_tuner_dsd_dvb_t(struct firedtv *fdtv,
436                                 struct dvb_frontend_parameters *params)
437 {
438         struct dvb_ofdm_parameters *ofdm = &params->u.ofdm;
439         struct avc_command_frame *c = (void *)fdtv->avc_data;
440
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] */
533
534         /* Add PIDs to filter */
535         fdtv->avc_data_length =
536                 ALIGN(17 + add_pid_filter(fdtv, &c->operand[17]) + 3, 4);
537 }
538
539 int avc_tuner_dsd(struct firedtv *fdtv,
540                   struct dvb_frontend_parameters *params)
541 {
542         struct avc_command_frame *c = (void *)fdtv->avc_data;
543         int ret;
544
545         mutex_lock(&fdtv->avc_mutex);
546
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:
554         case FIREDTV_DVB_S2: avc_tuner_tuneqpsk(fdtv, params); break;
555         case FIREDTV_DVB_C: avc_tuner_dsd_dvb_c(fdtv, params); break;
556         case FIREDTV_DVB_T: avc_tuner_dsd_dvb_t(fdtv, params); break;
557         default:
558                 BUG();
559         }
560         ret = avc_write(fdtv);
561 #if 0
562         /*
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          */
567         if (status)
568                 *status = r->operand[2];
569 #endif
570         mutex_unlock(&fdtv->avc_mutex);
571
572         if (ret == 0)
573                 msleep(500);
574
575         return ret;
576 }
577
578 int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
579 {
580         struct avc_command_frame *c = (void *)fdtv->avc_data;
581         int ret, pos, k;
582
583         if (pidc > 16 && pidc != 0xff)
584                 return -EINVAL;
585
586         mutex_lock(&fdtv->avc_mutex);
587
588         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
612         fdtv->avc_data_length = ALIGN(3 + pos, 4);
613         ret = avc_write(fdtv);
614
615         /* FIXME: check response code? */
616
617         mutex_unlock(&fdtv->avc_mutex);
618
619         if (ret == 0)
620                 msleep(50);
621
622         return ret;
623 }
624
625 int avc_tuner_get_ts(struct firedtv *fdtv)
626 {
627         struct avc_command_frame *c = (void *)fdtv->avc_data;
628         int ret, sl;
629
630         mutex_lock(&fdtv->avc_mutex);
631
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
651         fdtv->avc_data_length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
652         ret = avc_write(fdtv);
653
654         /* FIXME: check response code? */
655
656         mutex_unlock(&fdtv->avc_mutex);
657
658         if (ret == 0)
659                 msleep(250);
660
661         return ret;
662 }
663
664 int avc_identify_subunit(struct firedtv *fdtv)
665 {
666         struct avc_command_frame *c = (void *)fdtv->avc_data;
667         struct avc_response_frame *r = (void *)fdtv->avc_data;
668         int ret;
669
670         mutex_lock(&fdtv->avc_mutex);
671
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
686         fdtv->avc_data_length = 12;
687         ret = avc_write(fdtv);
688         if (ret < 0)
689                 goto out;
690
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");
695                 ret = -EINVAL;
696         }
697 out:
698         mutex_unlock(&fdtv->avc_mutex);
699
700         return ret;
701 }
702
703 #define SIZEOF_ANTENNA_INPUT_INFO 22
704
705 int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
706 {
707         struct avc_command_frame *c = (void *)fdtv->avc_data;
708         struct avc_response_frame *r = (void *)fdtv->avc_data;
709         int length, ret;
710
711         mutex_lock(&fdtv->avc_mutex);
712
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
727         fdtv->avc_data_length = 12;
728         ret = avc_write(fdtv);
729         if (ret < 0)
730                 goto out;
731
732         if (r->response != AVC_RESPONSE_STABLE &&
733             r->response != AVC_RESPONSE_ACCEPTED) {
734                 dev_err(fdtv->device, "cannot read tuner status\n");
735                 ret = -EINVAL;
736                 goto out;
737         }
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");
742                 ret = -EINVAL;
743                 goto out;
744         }
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;
779 out:
780         mutex_unlock(&fdtv->avc_mutex);
781
782         return ret;
783 }
784
785 int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
786                     char conttone, char nrdiseq,
787                     struct dvb_diseqc_master_cmd *diseqcmd)
788 {
789         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
793         mutex_lock(&fdtv->avc_mutex);
794
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
821         fdtv->avc_data_length = ALIGN(3 + i, 4);
822         ret = avc_write(fdtv);
823         if (ret < 0)
824                 goto out;
825
826         if (r->response != AVC_RESPONSE_ACCEPTED) {
827                 dev_err(fdtv->device, "LNB control failed\n");
828                 ret = -EINVAL;
829         }
830 out:
831         mutex_unlock(&fdtv->avc_mutex);
832
833         return ret;
834 }
835
836 int avc_register_remote_control(struct firedtv *fdtv)
837 {
838         struct avc_command_frame *c = (void *)fdtv->avc_data;
839         int ret;
840
841         mutex_lock(&fdtv->avc_mutex);
842
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
854         fdtv->avc_data_length = 8;
855         ret = avc_write(fdtv);
856
857         /* FIXME: check response code? */
858
859         mutex_unlock(&fdtv->avc_mutex);
860
861         return ret;
862 }
863
864 void 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 */
874 int avc_tuner_host2ca(struct firedtv *fdtv)
875 {
876         struct avc_command_frame *c = (void *)fdtv->avc_data;
877         int ret;
878
879         mutex_lock(&fdtv->avc_mutex);
880
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
896         fdtv->avc_data_length = 12;
897         ret = avc_write(fdtv);
898
899         /* FIXME: check response code? */
900
901         mutex_unlock(&fdtv->avc_mutex);
902
903         return ret;
904 }
905 #endif
906
907 static 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
917 static 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
932 int avc_ca_app_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
933 {
934         struct avc_command_frame *c = (void *)fdtv->avc_data;
935         struct avc_response_frame *r = (void *)fdtv->avc_data;
936         int pos, ret;
937
938         mutex_lock(&fdtv->avc_mutex);
939
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
953         fdtv->avc_data_length = 12;
954         ret = avc_write(fdtv);
955         if (ret < 0)
956                 goto out;
957
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;
968 out:
969         mutex_unlock(&fdtv->avc_mutex);
970
971         return ret;
972 }
973
974 int avc_ca_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
975 {
976         struct avc_command_frame *c = (void *)fdtv->avc_data;
977         struct avc_response_frame *r = (void *)fdtv->avc_data;
978         int pos, ret;
979
980         mutex_lock(&fdtv->avc_mutex);
981
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
995         fdtv->avc_data_length = 12;
996         ret = avc_write(fdtv);
997         if (ret < 0)
998                 goto out;
999
1000         /* FIXME: check response code and validate response data */
1001
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;
1010 out:
1011         mutex_unlock(&fdtv->avc_mutex);
1012
1013         return ret;
1014 }
1015
1016 int avc_ca_reset(struct firedtv *fdtv)
1017 {
1018         struct avc_command_frame *c = (void *)fdtv->avc_data;
1019         int ret;
1020
1021         mutex_lock(&fdtv->avc_mutex);
1022
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
1039         fdtv->avc_data_length = 12;
1040         ret = avc_write(fdtv);
1041
1042         /* FIXME: check response code? */
1043
1044         mutex_unlock(&fdtv->avc_mutex);
1045
1046         return ret;
1047 }
1048
1049 int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
1050 {
1051         struct avc_command_frame *c = (void *)fdtv->avc_data;
1052         struct avc_response_frame *r = (void *)fdtv->avc_data;
1053         int list_management;
1054         int program_info_length;
1055         int pmt_cmd_id;
1056         int read_pos;
1057         int write_pos;
1058         int es_info_length;
1059         int crc32_csum;
1060         int ret;
1061
1062         if (unlikely(avc_debug & AVC_DEBUG_APPLICATION_PMT))
1063                 debug_pmt(msg, length);
1064
1065         mutex_lock(&fdtv->avc_mutex);
1066
1067         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 */
1091         /* Use three bytes for length field in case length > 127 */
1092         c->operand[10] = list_management;
1093         c->operand[11] = 0x01; /* pmt_cmd=OK_descramble */
1094
1095         /* TS program map table */
1096
1097         c->operand[12] = 0x02; /* Table id=2 */
1098         c->operand[13] = 0x80; /* Section syntax + length */
1099         /* c->operand[14] = XXXprogram_info_length + 12; */
1100         c->operand[15] = msg[1]; /* Program number */
1101         c->operand[16] = msg[2];
1102         c->operand[17] = 0x01; /* Version number=0 + current/next=1 */
1103         c->operand[18] = 0x00; /* Section number=0 */
1104         c->operand[19] = 0x00; /* Last section number=0 */
1105         c->operand[20] = 0x1f; /* PCR_PID=1FFF */
1106         c->operand[21] = 0xff;
1107         c->operand[22] = (program_info_length >> 8); /* Program info length */
1108         c->operand[23] = (program_info_length & 0xff);
1109
1110         /* CA descriptors at programme level */
1111         read_pos = 6;
1112         write_pos = 24;
1113         if (program_info_length > 0) {
1114                 pmt_cmd_id = msg[read_pos++];
1115                 if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
1116                         dev_err(fdtv->device,
1117                                 "invalid pmt_cmd_id %d\n", pmt_cmd_id);
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
1154         c->operand[7] = 0x82;
1155         c->operand[8] = (write_pos - 10) >> 8;
1156         c->operand[9] = (write_pos - 10) & 0xff;
1157         c->operand[14] = write_pos - 15;
1158
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
1165         fdtv->avc_data_length = ALIGN(3 + write_pos, 4);
1166         ret = avc_write(fdtv);
1167         if (ret < 0)
1168                 goto out;
1169
1170         if (r->response != AVC_RESPONSE_ACCEPTED) {
1171                 dev_err(fdtv->device,
1172                         "CA PMT failed with response 0x%x\n", r->response);
1173                 ret = -EFAULT;
1174         }
1175 out:
1176         mutex_unlock(&fdtv->avc_mutex);
1177
1178         return ret;
1179 }
1180
1181 int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
1182 {
1183         struct avc_command_frame *c = (void *)fdtv->avc_data;
1184         struct avc_response_frame *r = (void *)fdtv->avc_data;
1185         int ret;
1186
1187         mutex_lock(&fdtv->avc_mutex);
1188
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
1204         fdtv->avc_data_length = 12;
1205         ret = avc_write(fdtv);
1206         if (ret < 0)
1207                 goto out;
1208
1209         /* FIXME: check response code and validate response data */
1210
1211         *interval = r->operand[get_ca_object_pos(r)];
1212 out:
1213         mutex_unlock(&fdtv->avc_mutex);
1214
1215         return ret;
1216 }
1217
1218 int avc_ca_enter_menu(struct firedtv *fdtv)
1219 {
1220         struct avc_command_frame *c = (void *)fdtv->avc_data;
1221         int ret;
1222
1223         mutex_lock(&fdtv->avc_mutex);
1224
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
1240         fdtv->avc_data_length = 12;
1241         ret = avc_write(fdtv);
1242
1243         /* FIXME: check response code? */
1244
1245         mutex_unlock(&fdtv->avc_mutex);
1246
1247         return ret;
1248 }
1249
1250 int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
1251 {
1252         struct avc_command_frame *c = (void *)fdtv->avc_data;
1253         struct avc_response_frame *r = (void *)fdtv->avc_data;
1254         int ret;
1255
1256         mutex_lock(&fdtv->avc_mutex);
1257
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
1273         fdtv->avc_data_length = 12;
1274         ret = avc_write(fdtv);
1275         if (ret < 0)
1276                 goto out;
1277
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);
1282 out:
1283         mutex_unlock(&fdtv->avc_mutex);
1284
1285         return ret;
1286 }
1287
1288 #define CMP_OUTPUT_PLUG_CONTROL_REG_0   0xfffff0000904ULL
1289
1290 static int cmp_read(struct firedtv *fdtv, u64 addr, __be32 *data)
1291 {
1292         int ret;
1293
1294         mutex_lock(&fdtv->avc_mutex);
1295
1296         ret = fdtv->backend->read(fdtv, addr, data);
1297         if (ret < 0)
1298                 dev_err(fdtv->device, "CMP: read I/O error\n");
1299
1300         mutex_unlock(&fdtv->avc_mutex);
1301
1302         return ret;
1303 }
1304
1305 static int cmp_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
1306 {
1307         int ret;
1308
1309         mutex_lock(&fdtv->avc_mutex);
1310
1311         /* 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);
1315         if (ret < 0)
1316                 dev_err(fdtv->device, "CMP: lock I/O error\n");
1317         else
1318                 memcpy(data, fdtv->avc_data, 8);
1319
1320         mutex_unlock(&fdtv->avc_mutex);
1321
1322         return ret;
1323 }
1324
1325 static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
1326 {
1327         return (be32_to_cpu(opcr) >> shift) & mask;
1328 }
1329
1330 static 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
1345 int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
1346 {
1347         __be32 old_opcr, opcr[2];
1348         u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1349         int attempts = 0;
1350         int ret;
1351
1352         ret = cmp_read(fdtv, opcr_address, opcr);
1353         if (ret < 0)
1354                 return ret;
1355
1356 repeat:
1357         if (!get_opcr_online(*opcr)) {
1358                 dev_err(fdtv->device, "CMP: output offline\n");
1359                 return -EBUSY;
1360         }
1361
1362         old_opcr = *opcr;
1363
1364         if (get_opcr_p2p_connections(*opcr)) {
1365                 if (get_opcr_channel(*opcr) != channel) {
1366                         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 {
1373                 set_opcr_channel(opcr, channel);
1374                 set_opcr_data_rate(opcr, 2); /* S400 */
1375
1376                 /* FIXME: this is for the worst case - optimize */
1377                 set_opcr_overhead_id(opcr, 0);
1378
1379                 /*
1380                  * FIXME: allocate isochronous channel and bandwidth at IRM
1381                  * fdtv->backend->alloc_resources(fdtv, channels_mask, bw);
1382                  */
1383         }
1384
1385         set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) + 1);
1386
1387         opcr[1] = *opcr;
1388         opcr[0] = old_opcr;
1389
1390         ret = cmp_lock(fdtv, opcr_address, opcr);
1391         if (ret < 0)
1392                 return ret;
1393
1394         if (old_opcr != *opcr) {
1395                 /*
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
1410 void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
1411 {
1412         __be32 old_opcr, opcr[2];
1413         u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
1414         int attempts = 0;
1415
1416         if (cmp_read(fdtv, opcr_address, opcr) < 0)
1417                 return;
1418
1419 repeat:
1420         if (!get_opcr_online(*opcr) || !get_opcr_p2p_connections(*opcr) ||
1421             get_opcr_channel(*opcr) != channel) {
1422                 dev_err(fdtv->device, "CMP: no connection to break\n");
1423                 return;
1424         }
1425
1426         old_opcr = *opcr;
1427         set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) - 1);
1428
1429         opcr[1] = *opcr;
1430         opcr[0] = old_opcr;
1431
1432         if (cmp_lock(fdtv, opcr_address, opcr) < 0)
1433                 return;
1434
1435         if (old_opcr != *opcr) {
1436                 /*
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 }