firedtv-avc.c 36 KB

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