firedtv-avc.c 40 KB

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