firedtv-avc.c 39 KB

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