firedtv-avc.c 39 KB

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