firedtv-avc.c 39 KB

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