firedtv-avc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. #include <linux/bug.h>
  14. #include <linux/crc32.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/kernel.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/mutex.h>
  21. #include <linux/string.h>
  22. #include <linux/stringify.h>
  23. #include <linux/wait.h>
  24. #include <linux/workqueue.h>
  25. #include "firedtv.h"
  26. #define FCP_COMMAND_REGISTER 0xfffff0000b00ULL
  27. #define AVC_CTYPE_CONTROL 0x0
  28. #define AVC_CTYPE_STATUS 0x1
  29. #define AVC_CTYPE_NOTIFY 0x3
  30. #define AVC_RESPONSE_ACCEPTED 0x9
  31. #define AVC_RESPONSE_STABLE 0xc
  32. #define AVC_RESPONSE_CHANGED 0xd
  33. #define AVC_RESPONSE_INTERIM 0xf
  34. #define AVC_SUBUNIT_TYPE_TUNER (0x05 << 3)
  35. #define AVC_SUBUNIT_TYPE_UNIT (0x1f << 3)
  36. #define AVC_OPCODE_VENDOR 0x00
  37. #define AVC_OPCODE_READ_DESCRIPTOR 0x09
  38. #define AVC_OPCODE_DSIT 0xc8
  39. #define AVC_OPCODE_DSD 0xcb
  40. #define DESCRIPTOR_TUNER_STATUS 0x80
  41. #define DESCRIPTOR_SUBUNIT_IDENTIFIER 0x00
  42. #define SFE_VENDOR_DE_COMPANYID_0 0x00 /* OUI of Digital Everywhere */
  43. #define SFE_VENDOR_DE_COMPANYID_1 0x12
  44. #define SFE_VENDOR_DE_COMPANYID_2 0x87
  45. #define SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL 0x0a
  46. #define SFE_VENDOR_OPCODE_LNB_CONTROL 0x52
  47. #define SFE_VENDOR_OPCODE_TUNE_QPSK 0x58 /* for DVB-S */
  48. #define SFE_VENDOR_OPCODE_GET_FIRMWARE_VERSION 0x00
  49. #define SFE_VENDOR_OPCODE_HOST2CA 0x56
  50. #define SFE_VENDOR_OPCODE_CA2HOST 0x57
  51. #define SFE_VENDOR_OPCODE_CISTATUS 0x59
  52. #define SFE_VENDOR_OPCODE_TUNE_QPSK2 0x60 /* for DVB-S2 */
  53. #define SFE_VENDOR_TAG_CA_RESET 0x00
  54. #define SFE_VENDOR_TAG_CA_APPLICATION_INFO 0x01
  55. #define SFE_VENDOR_TAG_CA_PMT 0x02
  56. #define SFE_VENDOR_TAG_CA_DATE_TIME 0x04
  57. #define SFE_VENDOR_TAG_CA_MMI 0x05
  58. #define SFE_VENDOR_TAG_CA_ENTER_MENU 0x07
  59. #define EN50221_LIST_MANAGEMENT_ONLY 0x03
  60. #define EN50221_TAG_APP_INFO 0x9f8021
  61. #define EN50221_TAG_CA_INFO 0x9f8031
  62. struct avc_command_frame {
  63. int length;
  64. u8 ctype;
  65. u8 subunit;
  66. u8 opcode;
  67. u8 operand[509];
  68. };
  69. struct avc_response_frame {
  70. int length;
  71. u8 response;
  72. u8 subunit;
  73. u8 opcode;
  74. u8 operand[509];
  75. };
  76. #define AVC_DEBUG_FCP_SUBACTIONS 1
  77. #define AVC_DEBUG_FCP_PAYLOADS 2
  78. static int avc_debug;
  79. module_param_named(debug, avc_debug, int, 0644);
  80. MODULE_PARM_DESC(debug, "Verbose logging (default = 0"
  81. ", FCP subactions = " __stringify(AVC_DEBUG_FCP_SUBACTIONS)
  82. ", FCP payloads = " __stringify(AVC_DEBUG_FCP_PAYLOADS)
  83. ", or all = -1)");
  84. static const char *debug_fcp_ctype(unsigned int ctype)
  85. {
  86. static const char *ctypes[] = {
  87. [0x0] = "CONTROL", [0x1] = "STATUS",
  88. [0x2] = "SPECIFIC INQUIRY", [0x3] = "NOTIFY",
  89. [0x4] = "GENERAL INQUIRY", [0x8] = "NOT IMPLEMENTED",
  90. [0x9] = "ACCEPTED", [0xa] = "REJECTED",
  91. [0xb] = "IN TRANSITION", [0xc] = "IMPLEMENTED/STABLE",
  92. [0xd] = "CHANGED", [0xf] = "INTERIM",
  93. };
  94. const char *ret = ctype < ARRAY_SIZE(ctypes) ? ctypes[ctype] : NULL;
  95. return ret ? ret : "?";
  96. }
  97. static const char *debug_fcp_opcode(unsigned int opcode,
  98. const u8 *data, int length)
  99. {
  100. switch (opcode) {
  101. case AVC_OPCODE_VENDOR: break;
  102. case AVC_OPCODE_READ_DESCRIPTOR: return "ReadDescriptor";
  103. case AVC_OPCODE_DSIT: return "DirectSelectInfo.Type";
  104. case AVC_OPCODE_DSD: return "DirectSelectData";
  105. default: return "?";
  106. }
  107. if (length < 7 ||
  108. data[3] != SFE_VENDOR_DE_COMPANYID_0 ||
  109. data[4] != SFE_VENDOR_DE_COMPANYID_1 ||
  110. data[5] != SFE_VENDOR_DE_COMPANYID_2)
  111. return "Vendor";
  112. switch (data[6]) {
  113. case SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL: return "RegisterRC";
  114. case SFE_VENDOR_OPCODE_LNB_CONTROL: return "LNBControl";
  115. case SFE_VENDOR_OPCODE_TUNE_QPSK: return "TuneQPSK";
  116. case SFE_VENDOR_OPCODE_TUNE_QPSK2: return "TuneQPSK2";
  117. case SFE_VENDOR_OPCODE_HOST2CA: return "Host2CA";
  118. case SFE_VENDOR_OPCODE_CA2HOST: return "CA2Host";
  119. }
  120. return "Vendor";
  121. }
  122. static void debug_fcp(const u8 *data, int length)
  123. {
  124. unsigned int subunit_type, subunit_id, op;
  125. const char *prefix = data[0] > 7 ? "FCP <- " : "FCP -> ";
  126. if (avc_debug & AVC_DEBUG_FCP_SUBACTIONS) {
  127. subunit_type = data[1] >> 3;
  128. subunit_id = data[1] & 7;
  129. op = subunit_type == 0x1e || subunit_id == 5 ? ~0 : data[2];
  130. printk(KERN_INFO "%ssu=%x.%x l=%d: %-8s - %s\n",
  131. prefix, subunit_type, subunit_id, length,
  132. debug_fcp_ctype(data[0]),
  133. debug_fcp_opcode(op, data, length));
  134. }
  135. if (avc_debug & AVC_DEBUG_FCP_PAYLOADS)
  136. print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_NONE, 16, 1,
  137. data, length, false);
  138. }
  139. static int __avc_write(struct firedtv *fdtv,
  140. const struct avc_command_frame *c, struct avc_response_frame *r)
  141. {
  142. int err, retry;
  143. if (r)
  144. fdtv->avc_reply_received = false;
  145. for (retry = 0; retry < 6; retry++) {
  146. if (unlikely(avc_debug))
  147. debug_fcp(&c->ctype, c->length);
  148. err = fdtv->backend->write(fdtv, FCP_COMMAND_REGISTER,
  149. (void *)&c->ctype, c->length);
  150. if (err) {
  151. fdtv->avc_reply_received = true;
  152. dev_err(fdtv->device, "FCP command write failed\n");
  153. return err;
  154. }
  155. if (!r)
  156. return 0;
  157. /*
  158. * AV/C specs say that answers should be sent within 150 ms.
  159. * Time out after 200 ms.
  160. */
  161. if (wait_event_timeout(fdtv->avc_wait,
  162. fdtv->avc_reply_received,
  163. msecs_to_jiffies(200)) != 0) {
  164. r->length = fdtv->response_length;
  165. memcpy(&r->response, fdtv->response, r->length);
  166. return 0;
  167. }
  168. }
  169. dev_err(fdtv->device, "FCP response timed out\n");
  170. return -ETIMEDOUT;
  171. }
  172. static int avc_write(struct firedtv *fdtv,
  173. const struct avc_command_frame *c, struct avc_response_frame *r)
  174. {
  175. int ret;
  176. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  177. return -EINTR;
  178. ret = __avc_write(fdtv, c, r);
  179. mutex_unlock(&fdtv->avc_mutex);
  180. return ret;
  181. }
  182. int avc_recv(struct firedtv *fdtv, void *data, size_t length)
  183. {
  184. struct avc_response_frame *r =
  185. data - offsetof(struct avc_response_frame, response);
  186. if (unlikely(avc_debug))
  187. debug_fcp(data, length);
  188. if (length >= 8 &&
  189. r->operand[0] == SFE_VENDOR_DE_COMPANYID_0 &&
  190. r->operand[1] == SFE_VENDOR_DE_COMPANYID_1 &&
  191. r->operand[2] == SFE_VENDOR_DE_COMPANYID_2 &&
  192. r->operand[3] == SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL) {
  193. if (r->response == AVC_RESPONSE_CHANGED) {
  194. fdtv_handle_rc(fdtv,
  195. r->operand[4] << 8 | r->operand[5]);
  196. schedule_work(&fdtv->remote_ctrl_work);
  197. } else if (r->response != AVC_RESPONSE_INTERIM) {
  198. dev_info(fdtv->device,
  199. "remote control result = %d\n", r->response);
  200. }
  201. return 0;
  202. }
  203. if (fdtv->avc_reply_received) {
  204. dev_err(fdtv->device, "out-of-order AVC response, ignored\n");
  205. return -EIO;
  206. }
  207. memcpy(fdtv->response, data, length);
  208. fdtv->response_length = length;
  209. fdtv->avc_reply_received = true;
  210. wake_up(&fdtv->avc_wait);
  211. return 0;
  212. }
  213. static int add_pid_filter(struct firedtv *fdtv, u8 *operand)
  214. {
  215. int i, n, pos = 1;
  216. for (i = 0, n = 0; i < 16; i++) {
  217. if (test_bit(i, &fdtv->channel_active)) {
  218. operand[pos++] = 0x13; /* flowfunction relay */
  219. operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
  220. operand[pos++] = (fdtv->channel_pid[i] >> 8) & 0x1f;
  221. operand[pos++] = fdtv->channel_pid[i] & 0xff;
  222. operand[pos++] = 0x00; /* tableID */
  223. operand[pos++] = 0x00; /* filter_length */
  224. n++;
  225. }
  226. }
  227. operand[0] = n;
  228. return pos;
  229. }
  230. /*
  231. * tuning command for setting the relative LNB frequency
  232. * (not supported by the AVC standard)
  233. */
  234. static void avc_tuner_tuneqpsk(struct firedtv *fdtv,
  235. struct dvb_frontend_parameters *params,
  236. struct avc_command_frame *c)
  237. {
  238. c->opcode = AVC_OPCODE_VENDOR;
  239. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  240. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  241. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  242. if (fdtv->type == FIREDTV_DVB_S2)
  243. c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK2;
  244. else
  245. c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK;
  246. c->operand[4] = (params->frequency >> 24) & 0xff;
  247. c->operand[5] = (params->frequency >> 16) & 0xff;
  248. c->operand[6] = (params->frequency >> 8) & 0xff;
  249. c->operand[7] = params->frequency & 0xff;
  250. c->operand[8] = ((params->u.qpsk.symbol_rate / 1000) >> 8) & 0xff;
  251. c->operand[9] = (params->u.qpsk.symbol_rate / 1000) & 0xff;
  252. switch (params->u.qpsk.fec_inner) {
  253. case FEC_1_2: c->operand[10] = 0x1; break;
  254. case FEC_2_3: c->operand[10] = 0x2; break;
  255. case FEC_3_4: c->operand[10] = 0x3; break;
  256. case FEC_5_6: c->operand[10] = 0x4; break;
  257. case FEC_7_8: c->operand[10] = 0x5; break;
  258. case FEC_4_5:
  259. case FEC_8_9:
  260. case FEC_AUTO:
  261. default: c->operand[10] = 0x0;
  262. }
  263. if (fdtv->voltage == 0xff)
  264. c->operand[11] = 0xff;
  265. else if (fdtv->voltage == SEC_VOLTAGE_18) /* polarisation */
  266. c->operand[11] = 0;
  267. else
  268. c->operand[11] = 1;
  269. if (fdtv->tone == 0xff)
  270. c->operand[12] = 0xff;
  271. else if (fdtv->tone == SEC_TONE_ON) /* band */
  272. c->operand[12] = 1;
  273. else
  274. c->operand[12] = 0;
  275. if (fdtv->type == FIREDTV_DVB_S2) {
  276. c->operand[13] = 0x1;
  277. c->operand[14] = 0xff;
  278. c->operand[15] = 0xff;
  279. c->length = 20;
  280. } else {
  281. c->length = 16;
  282. }
  283. }
  284. static void avc_tuner_dsd_dvb_c(struct firedtv *fdtv,
  285. struct dvb_frontend_parameters *params,
  286. struct avc_command_frame *c)
  287. {
  288. c->opcode = AVC_OPCODE_DSD;
  289. c->operand[0] = 0; /* source plug */
  290. c->operand[1] = 0xd2; /* subfunction replace */
  291. c->operand[2] = 0x20; /* system id = DVB */
  292. c->operand[3] = 0x00; /* antenna number */
  293. c->operand[4] = 0x11; /* system_specific_multiplex selection_length */
  294. /* multiplex_valid_flags, high byte */
  295. c->operand[5] = 0 << 7 /* reserved */
  296. | 0 << 6 /* Polarisation */
  297. | 0 << 5 /* Orbital_Pos */
  298. | 1 << 4 /* Frequency */
  299. | 1 << 3 /* Symbol_Rate */
  300. | 0 << 2 /* FEC_outer */
  301. | (params->u.qam.fec_inner != FEC_AUTO ? 1 << 1 : 0)
  302. | (params->u.qam.modulation != QAM_AUTO ? 1 << 0 : 0);
  303. /* multiplex_valid_flags, low byte */
  304. c->operand[6] = 0 << 7 /* NetworkID */
  305. | 0 << 0 /* reserved */ ;
  306. c->operand[7] = 0x00;
  307. c->operand[8] = 0x00;
  308. c->operand[9] = 0x00;
  309. c->operand[10] = 0x00;
  310. c->operand[11] = (((params->frequency / 4000) >> 16) & 0xff) | (2 << 6);
  311. c->operand[12] = ((params->frequency / 4000) >> 8) & 0xff;
  312. c->operand[13] = (params->frequency / 4000) & 0xff;
  313. c->operand[14] = ((params->u.qpsk.symbol_rate / 1000) >> 12) & 0xff;
  314. c->operand[15] = ((params->u.qpsk.symbol_rate / 1000) >> 4) & 0xff;
  315. c->operand[16] = ((params->u.qpsk.symbol_rate / 1000) << 4) & 0xf0;
  316. c->operand[17] = 0x00;
  317. switch (params->u.qpsk.fec_inner) {
  318. case FEC_1_2: c->operand[18] = 0x1; break;
  319. case FEC_2_3: c->operand[18] = 0x2; break;
  320. case FEC_3_4: c->operand[18] = 0x3; break;
  321. case FEC_5_6: c->operand[18] = 0x4; break;
  322. case FEC_7_8: c->operand[18] = 0x5; break;
  323. case FEC_8_9: c->operand[18] = 0x6; break;
  324. case FEC_4_5: c->operand[18] = 0x8; break;
  325. case FEC_AUTO:
  326. default: c->operand[18] = 0x0;
  327. }
  328. switch (params->u.qam.modulation) {
  329. case QAM_16: c->operand[19] = 0x08; break;
  330. case QAM_32: c->operand[19] = 0x10; break;
  331. case QAM_64: c->operand[19] = 0x18; break;
  332. case QAM_128: c->operand[19] = 0x20; break;
  333. case QAM_256: c->operand[19] = 0x28; break;
  334. case QAM_AUTO:
  335. default: c->operand[19] = 0x00;
  336. }
  337. c->operand[20] = 0x00;
  338. c->operand[21] = 0x00;
  339. /* Add PIDs to filter */
  340. c->length = ALIGN(22 + add_pid_filter(fdtv, &c->operand[22]) + 3, 4);
  341. }
  342. static void avc_tuner_dsd_dvb_t(struct firedtv *fdtv,
  343. struct dvb_frontend_parameters *params,
  344. struct avc_command_frame *c)
  345. {
  346. struct dvb_ofdm_parameters *ofdm = &params->u.ofdm;
  347. c->opcode = AVC_OPCODE_DSD;
  348. c->operand[0] = 0; /* source plug */
  349. c->operand[1] = 0xd2; /* subfunction replace */
  350. c->operand[2] = 0x20; /* system id = DVB */
  351. c->operand[3] = 0x00; /* antenna number */
  352. c->operand[4] = 0x0c; /* system_specific_multiplex selection_length */
  353. /* multiplex_valid_flags, high byte */
  354. c->operand[5] =
  355. 0 << 7 /* reserved */
  356. | 1 << 6 /* CenterFrequency */
  357. | (ofdm->bandwidth != BANDWIDTH_AUTO ? 1 << 5 : 0)
  358. | (ofdm->constellation != QAM_AUTO ? 1 << 4 : 0)
  359. | (ofdm->hierarchy_information != HIERARCHY_AUTO ? 1 << 3 : 0)
  360. | (ofdm->code_rate_HP != FEC_AUTO ? 1 << 2 : 0)
  361. | (ofdm->code_rate_LP != FEC_AUTO ? 1 << 1 : 0)
  362. | (ofdm->guard_interval != GUARD_INTERVAL_AUTO ? 1 << 0 : 0);
  363. /* multiplex_valid_flags, low byte */
  364. c->operand[6] =
  365. 0 << 7 /* NetworkID */
  366. | (ofdm->transmission_mode != TRANSMISSION_MODE_AUTO ? 1 << 6 : 0)
  367. | 0 << 5 /* OtherFrequencyFlag */
  368. | 0 << 0 /* reserved */ ;
  369. c->operand[7] = 0x0;
  370. c->operand[8] = (params->frequency / 10) >> 24;
  371. c->operand[9] = ((params->frequency / 10) >> 16) & 0xff;
  372. c->operand[10] = ((params->frequency / 10) >> 8) & 0xff;
  373. c->operand[11] = (params->frequency / 10) & 0xff;
  374. switch (ofdm->bandwidth) {
  375. case BANDWIDTH_7_MHZ: c->operand[12] = 0x20; break;
  376. case BANDWIDTH_8_MHZ:
  377. case BANDWIDTH_6_MHZ: /* not defined by AVC spec */
  378. case BANDWIDTH_AUTO:
  379. default: c->operand[12] = 0x00;
  380. }
  381. switch (ofdm->constellation) {
  382. case QAM_16: c->operand[13] = 1 << 6; break;
  383. case QAM_64: c->operand[13] = 2 << 6; break;
  384. case QPSK:
  385. default: c->operand[13] = 0x00;
  386. }
  387. switch (ofdm->hierarchy_information) {
  388. case HIERARCHY_1: c->operand[13] |= 1 << 3; break;
  389. case HIERARCHY_2: c->operand[13] |= 2 << 3; break;
  390. case HIERARCHY_4: c->operand[13] |= 3 << 3; break;
  391. case HIERARCHY_AUTO:
  392. case HIERARCHY_NONE:
  393. default: break;
  394. }
  395. switch (ofdm->code_rate_HP) {
  396. case FEC_2_3: c->operand[13] |= 1; break;
  397. case FEC_3_4: c->operand[13] |= 2; break;
  398. case FEC_5_6: c->operand[13] |= 3; break;
  399. case FEC_7_8: c->operand[13] |= 4; break;
  400. case FEC_1_2:
  401. default: break;
  402. }
  403. switch (ofdm->code_rate_LP) {
  404. case FEC_2_3: c->operand[14] = 1 << 5; break;
  405. case FEC_3_4: c->operand[14] = 2 << 5; break;
  406. case FEC_5_6: c->operand[14] = 3 << 5; break;
  407. case FEC_7_8: c->operand[14] = 4 << 5; break;
  408. case FEC_1_2:
  409. default: c->operand[14] = 0x00; break;
  410. }
  411. switch (ofdm->guard_interval) {
  412. case GUARD_INTERVAL_1_16: c->operand[14] |= 1 << 3; break;
  413. case GUARD_INTERVAL_1_8: c->operand[14] |= 2 << 3; break;
  414. case GUARD_INTERVAL_1_4: c->operand[14] |= 3 << 3; break;
  415. case GUARD_INTERVAL_1_32:
  416. case GUARD_INTERVAL_AUTO:
  417. default: break;
  418. }
  419. switch (ofdm->transmission_mode) {
  420. case TRANSMISSION_MODE_8K: c->operand[14] |= 1 << 1; break;
  421. case TRANSMISSION_MODE_2K:
  422. case TRANSMISSION_MODE_AUTO:
  423. default: break;
  424. }
  425. c->operand[15] = 0x00; /* network_ID[0] */
  426. c->operand[16] = 0x00; /* network_ID[1] */
  427. /* Add PIDs to filter */
  428. c->length = ALIGN(17 + add_pid_filter(fdtv, &c->operand[17]) + 3, 4);
  429. }
  430. int avc_tuner_dsd(struct firedtv *fdtv,
  431. struct dvb_frontend_parameters *params)
  432. {
  433. char buffer[sizeof(struct avc_command_frame)];
  434. struct avc_command_frame *c = (void *)buffer;
  435. struct avc_response_frame *r = (void *)buffer; /* FIXME: unused */
  436. memset(c, 0, sizeof(*c));
  437. c->ctype = AVC_CTYPE_CONTROL;
  438. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  439. switch (fdtv->type) {
  440. case FIREDTV_DVB_S:
  441. case FIREDTV_DVB_S2: avc_tuner_tuneqpsk(fdtv, params, c); break;
  442. case FIREDTV_DVB_C: avc_tuner_dsd_dvb_c(fdtv, params, c); break;
  443. case FIREDTV_DVB_T: avc_tuner_dsd_dvb_t(fdtv, params, c); break;
  444. default:
  445. BUG();
  446. }
  447. if (avc_write(fdtv, c, r) < 0)
  448. return -EIO;
  449. msleep(500);
  450. #if 0
  451. /* FIXME: */
  452. /* u8 *status was an out-parameter of avc_tuner_dsd, unused by caller */
  453. if (status)
  454. *status = r->operand[2];
  455. #endif
  456. return 0;
  457. }
  458. int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
  459. {
  460. char buffer[sizeof(struct avc_command_frame)];
  461. struct avc_command_frame *c = (void *)buffer;
  462. struct avc_response_frame *r = (void *)buffer; /* FIXME: unused */
  463. int pos, k;
  464. if (pidc > 16 && pidc != 0xff)
  465. return -EINVAL;
  466. memset(c, 0, sizeof(*c));
  467. c->ctype = AVC_CTYPE_CONTROL;
  468. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  469. c->opcode = AVC_OPCODE_DSD;
  470. c->operand[0] = 0; /* source plug */
  471. c->operand[1] = 0xd2; /* subfunction replace */
  472. c->operand[2] = 0x20; /* system id = DVB */
  473. c->operand[3] = 0x00; /* antenna number */
  474. c->operand[4] = 0x00; /* system_specific_multiplex selection_length */
  475. c->operand[5] = pidc; /* Nr_of_dsd_sel_specs */
  476. pos = 6;
  477. if (pidc != 0xff)
  478. for (k = 0; k < pidc; k++) {
  479. c->operand[pos++] = 0x13; /* flowfunction relay */
  480. c->operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
  481. c->operand[pos++] = (pid[k] >> 8) & 0x1f;
  482. c->operand[pos++] = pid[k] & 0xff;
  483. c->operand[pos++] = 0x00; /* tableID */
  484. c->operand[pos++] = 0x00; /* filter_length */
  485. }
  486. c->length = ALIGN(3 + pos, 4);
  487. if (avc_write(fdtv, c, r) < 0)
  488. return -EIO;
  489. msleep(50);
  490. return 0;
  491. }
  492. int avc_tuner_get_ts(struct firedtv *fdtv)
  493. {
  494. char buffer[sizeof(struct avc_command_frame)];
  495. struct avc_command_frame *c = (void *)buffer;
  496. struct avc_response_frame *r = (void *)buffer; /* FIXME: unused */
  497. int sl;
  498. memset(c, 0, sizeof(*c));
  499. c->ctype = AVC_CTYPE_CONTROL;
  500. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  501. c->opcode = AVC_OPCODE_DSIT;
  502. sl = fdtv->type == FIREDTV_DVB_T ? 0x0c : 0x11;
  503. c->operand[0] = 0; /* source plug */
  504. c->operand[1] = 0xd2; /* subfunction replace */
  505. c->operand[2] = 0xff; /* status */
  506. c->operand[3] = 0x20; /* system id = DVB */
  507. c->operand[4] = 0x00; /* antenna number */
  508. c->operand[5] = 0x0; /* system_specific_search_flags */
  509. c->operand[6] = sl; /* system_specific_multiplex selection_length */
  510. c->operand[7] = 0x00; /* valid_flags [0] */
  511. c->operand[8] = 0x00; /* valid_flags [1] */
  512. c->operand[7 + sl] = 0x00; /* nr_of_dsit_sel_specs (always 0) */
  513. c->length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
  514. if (avc_write(fdtv, c, r) < 0)
  515. return -EIO;
  516. msleep(250);
  517. return 0;
  518. }
  519. int avc_identify_subunit(struct firedtv *fdtv)
  520. {
  521. char buffer[sizeof(struct avc_command_frame)];
  522. struct avc_command_frame *c = (void *)buffer;
  523. struct avc_response_frame *r = (void *)buffer;
  524. memset(c, 0, sizeof(*c));
  525. c->ctype = AVC_CTYPE_CONTROL;
  526. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  527. c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
  528. c->operand[0] = DESCRIPTOR_SUBUNIT_IDENTIFIER;
  529. c->operand[1] = 0xff;
  530. c->operand[2] = 0x00;
  531. c->operand[3] = 0x00; /* length highbyte */
  532. c->operand[4] = 0x08; /* length lowbyte */
  533. c->operand[5] = 0x00; /* offset highbyte */
  534. c->operand[6] = 0x0d; /* offset lowbyte */
  535. c->length = 12;
  536. if (avc_write(fdtv, c, r) < 0)
  537. return -EIO;
  538. if ((r->response != AVC_RESPONSE_STABLE &&
  539. r->response != AVC_RESPONSE_ACCEPTED) ||
  540. (r->operand[3] << 8) + r->operand[4] != 8) {
  541. dev_err(fdtv->device, "cannot read subunit identifier\n");
  542. return -EINVAL;
  543. }
  544. return 0;
  545. }
  546. #define SIZEOF_ANTENNA_INPUT_INFO 22
  547. int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
  548. {
  549. char buffer[sizeof(struct avc_command_frame)];
  550. struct avc_command_frame *c = (void *)buffer;
  551. struct avc_response_frame *r = (void *)buffer;
  552. int length;
  553. memset(c, 0, sizeof(*c));
  554. c->ctype = AVC_CTYPE_CONTROL;
  555. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  556. c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
  557. c->operand[0] = DESCRIPTOR_TUNER_STATUS;
  558. c->operand[1] = 0xff; /* read_result_status */
  559. c->operand[2] = 0x00; /* reserved */
  560. c->operand[3] = 0; /* SIZEOF_ANTENNA_INPUT_INFO >> 8; */
  561. c->operand[4] = 0; /* SIZEOF_ANTENNA_INPUT_INFO & 0xff; */
  562. c->operand[5] = 0x00;
  563. c->operand[6] = 0x00;
  564. c->length = 12;
  565. if (avc_write(fdtv, c, r) < 0)
  566. return -EIO;
  567. if (r->response != AVC_RESPONSE_STABLE &&
  568. r->response != AVC_RESPONSE_ACCEPTED) {
  569. dev_err(fdtv->device, "cannot read tuner status\n");
  570. return -EINVAL;
  571. }
  572. length = r->operand[9];
  573. if (r->operand[1] != 0x10 || length != SIZEOF_ANTENNA_INPUT_INFO) {
  574. dev_err(fdtv->device, "got invalid tuner status\n");
  575. return -EINVAL;
  576. }
  577. stat->active_system = r->operand[10];
  578. stat->searching = r->operand[11] >> 7 & 1;
  579. stat->moving = r->operand[11] >> 6 & 1;
  580. stat->no_rf = r->operand[11] >> 5 & 1;
  581. stat->input = r->operand[12] >> 7 & 1;
  582. stat->selected_antenna = r->operand[12] & 0x7f;
  583. stat->ber = r->operand[13] << 24 |
  584. r->operand[14] << 16 |
  585. r->operand[15] << 8 |
  586. r->operand[16];
  587. stat->signal_strength = r->operand[17];
  588. stat->raster_frequency = r->operand[18] >> 6 & 2;
  589. stat->rf_frequency = (r->operand[18] & 0x3f) << 16 |
  590. r->operand[19] << 8 |
  591. r->operand[20];
  592. stat->man_dep_info_length = r->operand[21];
  593. stat->front_end_error = r->operand[22] >> 4 & 1;
  594. stat->antenna_error = r->operand[22] >> 3 & 1;
  595. stat->front_end_power_status = r->operand[22] >> 1 & 1;
  596. stat->power_supply = r->operand[22] & 1;
  597. stat->carrier_noise_ratio = r->operand[23] << 8 |
  598. r->operand[24];
  599. stat->power_supply_voltage = r->operand[27];
  600. stat->antenna_voltage = r->operand[28];
  601. stat->firewire_bus_voltage = r->operand[29];
  602. stat->ca_mmi = r->operand[30] & 1;
  603. stat->ca_pmt_reply = r->operand[31] >> 7 & 1;
  604. stat->ca_date_time_request = r->operand[31] >> 6 & 1;
  605. stat->ca_application_info = r->operand[31] >> 5 & 1;
  606. stat->ca_module_present_status = r->operand[31] >> 4 & 1;
  607. stat->ca_dvb_flag = r->operand[31] >> 3 & 1;
  608. stat->ca_error_flag = r->operand[31] >> 2 & 1;
  609. stat->ca_initialization_status = r->operand[31] >> 1 & 1;
  610. return 0;
  611. }
  612. int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
  613. char conttone, char nrdiseq,
  614. struct dvb_diseqc_master_cmd *diseqcmd)
  615. {
  616. char buffer[sizeof(struct avc_command_frame)];
  617. struct avc_command_frame *c = (void *)buffer;
  618. struct avc_response_frame *r = (void *)buffer;
  619. int i, j, k;
  620. memset(c, 0, sizeof(*c));
  621. c->ctype = AVC_CTYPE_CONTROL;
  622. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  623. c->opcode = AVC_OPCODE_VENDOR;
  624. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  625. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  626. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  627. c->operand[3] = SFE_VENDOR_OPCODE_LNB_CONTROL;
  628. c->operand[4] = voltage;
  629. c->operand[5] = nrdiseq;
  630. i = 6;
  631. for (j = 0; j < nrdiseq; j++) {
  632. c->operand[i++] = diseqcmd[j].msg_len;
  633. for (k = 0; k < diseqcmd[j].msg_len; k++)
  634. c->operand[i++] = diseqcmd[j].msg[k];
  635. }
  636. c->operand[i++] = burst;
  637. c->operand[i++] = conttone;
  638. c->length = ALIGN(3 + i, 4);
  639. if (avc_write(fdtv, c, r) < 0)
  640. return -EIO;
  641. if (r->response != AVC_RESPONSE_ACCEPTED) {
  642. dev_err(fdtv->device, "LNB control failed\n");
  643. return -EINVAL;
  644. }
  645. return 0;
  646. }
  647. int avc_register_remote_control(struct firedtv *fdtv)
  648. {
  649. char buffer[sizeof(struct avc_command_frame)];
  650. struct avc_command_frame *c = (void *)buffer;
  651. memset(c, 0, sizeof(*c));
  652. c->ctype = AVC_CTYPE_NOTIFY;
  653. c->subunit = AVC_SUBUNIT_TYPE_UNIT | 7;
  654. c->opcode = AVC_OPCODE_VENDOR;
  655. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  656. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  657. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  658. c->operand[3] = SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
  659. c->length = 8;
  660. return avc_write(fdtv, c, NULL);
  661. }
  662. void avc_remote_ctrl_work(struct work_struct *work)
  663. {
  664. struct firedtv *fdtv =
  665. container_of(work, struct firedtv, remote_ctrl_work);
  666. /* Should it be rescheduled in failure cases? */
  667. avc_register_remote_control(fdtv);
  668. }
  669. #if 0 /* FIXME: unused */
  670. int avc_tuner_host2ca(struct firedtv *fdtv)
  671. {
  672. char buffer[sizeof(struct avc_command_frame)];
  673. struct avc_command_frame *c = (void *)buffer;
  674. struct avc_response_frame *r = (void *)buffer; /* FIXME: unused */
  675. memset(c, 0, sizeof(*c));
  676. c->ctype = AVC_CTYPE_CONTROL;
  677. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  678. c->opcode = AVC_OPCODE_VENDOR;
  679. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  680. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  681. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  682. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  683. c->operand[4] = 0; /* slot */
  684. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  685. c->operand[6] = 0; /* more/last */
  686. c->operand[7] = 0; /* length */
  687. c->length = 12;
  688. if (avc_write(fdtv, c, r) < 0)
  689. return -EIO;
  690. return 0;
  691. }
  692. #endif
  693. static int get_ca_object_pos(struct avc_response_frame *r)
  694. {
  695. int length = 1;
  696. /* Check length of length field */
  697. if (r->operand[7] & 0x80)
  698. length = (r->operand[7] & 0x7f) + 1;
  699. return length + 7;
  700. }
  701. static int get_ca_object_length(struct avc_response_frame *r)
  702. {
  703. #if 0 /* FIXME: unused */
  704. int size = 0;
  705. int i;
  706. if (r->operand[7] & 0x80)
  707. for (i = 0; i < (r->operand[7] & 0x7f); i++) {
  708. size <<= 8;
  709. size += r->operand[8 + i];
  710. }
  711. #endif
  712. return r->operand[7];
  713. }
  714. int avc_ca_app_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
  715. {
  716. char buffer[sizeof(struct avc_command_frame)];
  717. struct avc_command_frame *c = (void *)buffer;
  718. struct avc_response_frame *r = (void *)buffer;
  719. int pos;
  720. memset(c, 0, sizeof(*c));
  721. c->ctype = AVC_CTYPE_STATUS;
  722. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  723. c->opcode = AVC_OPCODE_VENDOR;
  724. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  725. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  726. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  727. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  728. c->operand[4] = 0; /* slot */
  729. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  730. c->length = 12;
  731. if (avc_write(fdtv, c, r) < 0)
  732. return -EIO;
  733. /* FIXME: check response code and validate response data */
  734. pos = get_ca_object_pos(r);
  735. app_info[0] = (EN50221_TAG_APP_INFO >> 16) & 0xff;
  736. app_info[1] = (EN50221_TAG_APP_INFO >> 8) & 0xff;
  737. app_info[2] = (EN50221_TAG_APP_INFO >> 0) & 0xff;
  738. app_info[3] = 6 + r->operand[pos + 4];
  739. app_info[4] = 0x01;
  740. memcpy(&app_info[5], &r->operand[pos], 5 + r->operand[pos + 4]);
  741. *len = app_info[3] + 4;
  742. return 0;
  743. }
  744. int avc_ca_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
  745. {
  746. char buffer[sizeof(struct avc_command_frame)];
  747. struct avc_command_frame *c = (void *)buffer;
  748. struct avc_response_frame *r = (void *)buffer;
  749. int pos;
  750. memset(c, 0, sizeof(*c));
  751. c->ctype = AVC_CTYPE_STATUS;
  752. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  753. c->opcode = AVC_OPCODE_VENDOR;
  754. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  755. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  756. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  757. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  758. c->operand[4] = 0; /* slot */
  759. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  760. c->length = 12;
  761. if (avc_write(fdtv, c, r) < 0)
  762. return -EIO;
  763. pos = get_ca_object_pos(r);
  764. app_info[0] = (EN50221_TAG_CA_INFO >> 16) & 0xff;
  765. app_info[1] = (EN50221_TAG_CA_INFO >> 8) & 0xff;
  766. app_info[2] = (EN50221_TAG_CA_INFO >> 0) & 0xff;
  767. app_info[3] = 2;
  768. app_info[4] = r->operand[pos + 0];
  769. app_info[5] = r->operand[pos + 1];
  770. *len = app_info[3] + 4;
  771. return 0;
  772. }
  773. int avc_ca_reset(struct firedtv *fdtv)
  774. {
  775. char buffer[sizeof(struct avc_command_frame)];
  776. struct avc_command_frame *c = (void *)buffer;
  777. struct avc_response_frame *r = (void *)buffer; /* FIXME: unused */
  778. memset(c, 0, sizeof(*c));
  779. c->ctype = AVC_CTYPE_CONTROL;
  780. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  781. c->opcode = AVC_OPCODE_VENDOR;
  782. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  783. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  784. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  785. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  786. c->operand[4] = 0; /* slot */
  787. c->operand[5] = SFE_VENDOR_TAG_CA_RESET; /* ca tag */
  788. c->operand[6] = 0; /* more/last */
  789. c->operand[7] = 1; /* length */
  790. c->operand[8] = 0; /* force hardware reset */
  791. c->length = 12;
  792. if (avc_write(fdtv, c, r) < 0)
  793. return -EIO;
  794. return 0;
  795. }
  796. int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
  797. {
  798. char buffer[sizeof(struct avc_command_frame)];
  799. struct avc_command_frame *c = (void *)buffer;
  800. struct avc_response_frame *r = (void *)buffer;
  801. int list_management;
  802. int program_info_length;
  803. int pmt_cmd_id;
  804. int read_pos;
  805. int write_pos;
  806. int es_info_length;
  807. int crc32_csum;
  808. memset(c, 0, sizeof(*c));
  809. c->ctype = AVC_CTYPE_CONTROL;
  810. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  811. c->opcode = AVC_OPCODE_VENDOR;
  812. if (msg[0] != EN50221_LIST_MANAGEMENT_ONLY) {
  813. dev_info(fdtv->device, "forcing list_management to ONLY\n");
  814. msg[0] = EN50221_LIST_MANAGEMENT_ONLY;
  815. }
  816. /* We take the cmd_id from the programme level only! */
  817. list_management = msg[0];
  818. program_info_length = ((msg[4] & 0x0f) << 8) + msg[5];
  819. if (program_info_length > 0)
  820. program_info_length--; /* Remove pmt_cmd_id */
  821. pmt_cmd_id = msg[6];
  822. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  823. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  824. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  825. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  826. c->operand[4] = 0; /* slot */
  827. c->operand[5] = SFE_VENDOR_TAG_CA_PMT; /* ca tag */
  828. c->operand[6] = 0; /* more/last */
  829. /* c->operand[7] = XXXprogram_info_length + 17; */ /* length */
  830. c->operand[8] = list_management;
  831. c->operand[9] = 0x01; /* pmt_cmd=OK_descramble */
  832. /* TS program map table */
  833. c->operand[10] = 0x02; /* Table id=2 */
  834. c->operand[11] = 0x80; /* Section syntax + length */
  835. /* c->operand[12] = XXXprogram_info_length + 12; */
  836. c->operand[13] = msg[1]; /* Program number */
  837. c->operand[14] = msg[2];
  838. c->operand[15] = 0x01; /* Version number=0 + current/next=1 */
  839. c->operand[16] = 0x00; /* Section number=0 */
  840. c->operand[17] = 0x00; /* Last section number=0 */
  841. c->operand[18] = 0x1f; /* PCR_PID=1FFF */
  842. c->operand[19] = 0xff;
  843. c->operand[20] = (program_info_length >> 8); /* Program info length */
  844. c->operand[21] = (program_info_length & 0xff);
  845. /* CA descriptors at programme level */
  846. read_pos = 6;
  847. write_pos = 22;
  848. if (program_info_length > 0) {
  849. pmt_cmd_id = msg[read_pos++];
  850. if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
  851. dev_err(fdtv->device,
  852. "invalid pmt_cmd_id %d\n", pmt_cmd_id);
  853. memcpy(&c->operand[write_pos], &msg[read_pos],
  854. program_info_length);
  855. read_pos += program_info_length;
  856. write_pos += program_info_length;
  857. }
  858. while (read_pos < length) {
  859. c->operand[write_pos++] = msg[read_pos++];
  860. c->operand[write_pos++] = msg[read_pos++];
  861. c->operand[write_pos++] = msg[read_pos++];
  862. es_info_length =
  863. ((msg[read_pos] & 0x0f) << 8) + msg[read_pos + 1];
  864. read_pos += 2;
  865. if (es_info_length > 0)
  866. es_info_length--; /* Remove pmt_cmd_id */
  867. c->operand[write_pos++] = es_info_length >> 8;
  868. c->operand[write_pos++] = es_info_length & 0xff;
  869. if (es_info_length > 0) {
  870. pmt_cmd_id = msg[read_pos++];
  871. if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
  872. dev_err(fdtv->device, "invalid pmt_cmd_id %d "
  873. "at stream level\n", pmt_cmd_id);
  874. memcpy(&c->operand[write_pos], &msg[read_pos],
  875. es_info_length);
  876. read_pos += es_info_length;
  877. write_pos += es_info_length;
  878. }
  879. }
  880. /* CRC */
  881. c->operand[write_pos++] = 0x00;
  882. c->operand[write_pos++] = 0x00;
  883. c->operand[write_pos++] = 0x00;
  884. c->operand[write_pos++] = 0x00;
  885. c->operand[7] = write_pos - 8;
  886. c->operand[12] = write_pos - 13;
  887. crc32_csum = crc32_be(0, &c->operand[10], c->operand[12] - 1);
  888. c->operand[write_pos - 4] = (crc32_csum >> 24) & 0xff;
  889. c->operand[write_pos - 3] = (crc32_csum >> 16) & 0xff;
  890. c->operand[write_pos - 2] = (crc32_csum >> 8) & 0xff;
  891. c->operand[write_pos - 1] = (crc32_csum >> 0) & 0xff;
  892. c->length = ALIGN(3 + write_pos, 4);
  893. if (avc_write(fdtv, c, r) < 0)
  894. return -EIO;
  895. if (r->response != AVC_RESPONSE_ACCEPTED) {
  896. dev_err(fdtv->device,
  897. "CA PMT failed with response 0x%x\n", r->response);
  898. return -EFAULT;
  899. }
  900. return 0;
  901. }
  902. int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
  903. {
  904. char buffer[sizeof(struct avc_command_frame)];
  905. struct avc_command_frame *c = (void *)buffer;
  906. struct avc_response_frame *r = (void *)buffer;
  907. memset(c, 0, sizeof(*c));
  908. c->ctype = AVC_CTYPE_STATUS;
  909. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  910. c->opcode = AVC_OPCODE_VENDOR;
  911. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  912. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  913. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  914. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  915. c->operand[4] = 0; /* slot */
  916. c->operand[5] = SFE_VENDOR_TAG_CA_DATE_TIME; /* ca tag */
  917. c->operand[6] = 0; /* more/last */
  918. c->operand[7] = 0; /* length */
  919. c->length = 12;
  920. if (avc_write(fdtv, c, r) < 0)
  921. return -EIO;
  922. /* FIXME: check response code and validate response data */
  923. *interval = r->operand[get_ca_object_pos(r)];
  924. return 0;
  925. }
  926. int avc_ca_enter_menu(struct firedtv *fdtv)
  927. {
  928. char buffer[sizeof(struct avc_command_frame)];
  929. struct avc_command_frame *c = (void *)buffer;
  930. struct avc_response_frame *r = (void *)buffer; /* FIXME: unused */
  931. memset(c, 0, sizeof(*c));
  932. c->ctype = AVC_CTYPE_STATUS;
  933. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  934. c->opcode = AVC_OPCODE_VENDOR;
  935. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  936. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  937. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  938. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  939. c->operand[4] = 0; /* slot */
  940. c->operand[5] = SFE_VENDOR_TAG_CA_ENTER_MENU;
  941. c->operand[6] = 0; /* more/last */
  942. c->operand[7] = 0; /* length */
  943. c->length = 12;
  944. if (avc_write(fdtv, c, r) < 0)
  945. return -EIO;
  946. return 0;
  947. }
  948. int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
  949. {
  950. char buffer[sizeof(struct avc_command_frame)];
  951. struct avc_command_frame *c = (void *)buffer;
  952. struct avc_response_frame *r = (void *)buffer;
  953. memset(c, 0, sizeof(*c));
  954. c->ctype = AVC_CTYPE_STATUS;
  955. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  956. c->opcode = AVC_OPCODE_VENDOR;
  957. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  958. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  959. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  960. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  961. c->operand[4] = 0; /* slot */
  962. c->operand[5] = SFE_VENDOR_TAG_CA_MMI;
  963. c->operand[6] = 0; /* more/last */
  964. c->operand[7] = 0; /* length */
  965. c->length = 12;
  966. if (avc_write(fdtv, c, r) < 0)
  967. return -EIO;
  968. /* FIXME: check response code and validate response data */
  969. *len = get_ca_object_length(r);
  970. memcpy(mmi_object, &r->operand[get_ca_object_pos(r)], *len);
  971. return 0;
  972. }
  973. #define CMP_OUTPUT_PLUG_CONTROL_REG_0 0xfffff0000904ULL
  974. static int cmp_read(struct firedtv *fdtv, void *buf, u64 addr, size_t len)
  975. {
  976. int ret;
  977. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  978. return -EINTR;
  979. ret = fdtv->backend->read(fdtv, addr, buf, len);
  980. if (ret < 0)
  981. dev_err(fdtv->device, "CMP: read I/O error\n");
  982. mutex_unlock(&fdtv->avc_mutex);
  983. return ret;
  984. }
  985. static int cmp_lock(struct firedtv *fdtv, void *data, u64 addr, __be32 arg)
  986. {
  987. int ret;
  988. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  989. return -EINTR;
  990. ret = fdtv->backend->lock(fdtv, addr, data, arg);
  991. if (ret < 0)
  992. dev_err(fdtv->device, "CMP: lock I/O error\n");
  993. mutex_unlock(&fdtv->avc_mutex);
  994. return ret;
  995. }
  996. static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
  997. {
  998. return (be32_to_cpu(opcr) >> shift) & mask;
  999. }
  1000. static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
  1001. {
  1002. *opcr &= ~cpu_to_be32(mask << shift);
  1003. *opcr |= cpu_to_be32((value & mask) << shift);
  1004. }
  1005. #define get_opcr_online(v) get_opcr((v), 0x1, 31)
  1006. #define get_opcr_p2p_connections(v) get_opcr((v), 0x3f, 24)
  1007. #define get_opcr_channel(v) get_opcr((v), 0x3f, 16)
  1008. #define set_opcr_p2p_connections(p, v) set_opcr((p), (v), 0x3f, 24)
  1009. #define set_opcr_channel(p, v) set_opcr((p), (v), 0x3f, 16)
  1010. #define set_opcr_data_rate(p, v) set_opcr((p), (v), 0x3, 14)
  1011. #define set_opcr_overhead_id(p, v) set_opcr((p), (v), 0xf, 10)
  1012. int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
  1013. {
  1014. __be32 old_opcr, opcr;
  1015. u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
  1016. int attempts = 0;
  1017. int ret;
  1018. ret = cmp_read(fdtv, &opcr, opcr_address, 4);
  1019. if (ret < 0)
  1020. return ret;
  1021. repeat:
  1022. if (!get_opcr_online(opcr)) {
  1023. dev_err(fdtv->device, "CMP: output offline\n");
  1024. return -EBUSY;
  1025. }
  1026. old_opcr = opcr;
  1027. if (get_opcr_p2p_connections(opcr)) {
  1028. if (get_opcr_channel(opcr) != channel) {
  1029. dev_err(fdtv->device, "CMP: cannot change channel\n");
  1030. return -EBUSY;
  1031. }
  1032. dev_info(fdtv->device, "CMP: overlaying connection\n");
  1033. /* We don't allocate isochronous resources. */
  1034. } else {
  1035. set_opcr_channel(&opcr, channel);
  1036. set_opcr_data_rate(&opcr, 2); /* S400 */
  1037. /* FIXME: this is for the worst case - optimize */
  1038. set_opcr_overhead_id(&opcr, 0);
  1039. /*
  1040. * FIXME: allocate isochronous channel and bandwidth at IRM
  1041. * fdtv->backend->alloc_resources(fdtv, channels_mask, bw);
  1042. */
  1043. }
  1044. set_opcr_p2p_connections(&opcr, get_opcr_p2p_connections(opcr) + 1);
  1045. ret = cmp_lock(fdtv, &opcr, opcr_address, old_opcr);
  1046. if (ret < 0)
  1047. return ret;
  1048. if (old_opcr != opcr) {
  1049. /*
  1050. * FIXME: if old_opcr.P2P_Connections > 0,
  1051. * deallocate isochronous channel and bandwidth at IRM
  1052. * if (...)
  1053. * fdtv->backend->dealloc_resources(fdtv, channel, bw);
  1054. */
  1055. if (++attempts < 6) /* arbitrary limit */
  1056. goto repeat;
  1057. return -EBUSY;
  1058. }
  1059. return 0;
  1060. }
  1061. void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
  1062. {
  1063. __be32 old_opcr, opcr;
  1064. u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
  1065. int attempts = 0;
  1066. if (cmp_read(fdtv, &opcr, opcr_address, 4) < 0)
  1067. return;
  1068. repeat:
  1069. if (!get_opcr_online(opcr) || !get_opcr_p2p_connections(opcr) ||
  1070. get_opcr_channel(opcr) != channel) {
  1071. dev_err(fdtv->device, "CMP: no connection to break\n");
  1072. return;
  1073. }
  1074. old_opcr = opcr;
  1075. set_opcr_p2p_connections(&opcr, get_opcr_p2p_connections(opcr) - 1);
  1076. if (cmp_lock(fdtv, &opcr, opcr_address, old_opcr) < 0)
  1077. return;
  1078. if (old_opcr != opcr) {
  1079. /*
  1080. * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
  1081. * owner, deallocate isochronous channel and bandwidth at IRM
  1082. * if (...)
  1083. * fdtv->backend->dealloc_resources(fdtv, channel, bw);
  1084. */
  1085. if (++attempts < 6) /* arbitrary limit */
  1086. goto repeat;
  1087. }
  1088. }