firedtv-avc.c 39 KB

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