firedtv-avc.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  466. return -EINTR;
  467. memset(c, 0, sizeof(*c));
  468. c->ctype = AVC_CTYPE_CONTROL;
  469. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  470. switch (fdtv->type) {
  471. case FIREDTV_DVB_S:
  472. case FIREDTV_DVB_S2: avc_tuner_tuneqpsk(fdtv, params); break;
  473. case FIREDTV_DVB_C: avc_tuner_dsd_dvb_c(fdtv, params); break;
  474. case FIREDTV_DVB_T: avc_tuner_dsd_dvb_t(fdtv, params); break;
  475. default:
  476. BUG();
  477. }
  478. ret = avc_write(fdtv);
  479. #if 0
  480. /*
  481. * FIXME:
  482. * u8 *status was an out-parameter of avc_tuner_dsd, unused by caller.
  483. * Check for AVC_RESPONSE_ACCEPTED here instead?
  484. */
  485. if (status)
  486. *status = r->operand[2];
  487. #endif
  488. mutex_unlock(&fdtv->avc_mutex);
  489. if (ret == 0)
  490. msleep(500);
  491. return ret;
  492. }
  493. int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
  494. {
  495. struct avc_command_frame *c = (void *)fdtv->avc_data;
  496. int ret, pos, k;
  497. if (pidc > 16 && pidc != 0xff)
  498. return -EINVAL;
  499. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  500. return -EINTR;
  501. memset(c, 0, sizeof(*c));
  502. c->ctype = AVC_CTYPE_CONTROL;
  503. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  504. c->opcode = AVC_OPCODE_DSD;
  505. c->operand[0] = 0; /* source plug */
  506. c->operand[1] = 0xd2; /* subfunction replace */
  507. c->operand[2] = 0x20; /* system id = DVB */
  508. c->operand[3] = 0x00; /* antenna number */
  509. c->operand[4] = 0x00; /* system_specific_multiplex selection_length */
  510. c->operand[5] = pidc; /* Nr_of_dsd_sel_specs */
  511. pos = 6;
  512. if (pidc != 0xff)
  513. for (k = 0; k < pidc; k++) {
  514. c->operand[pos++] = 0x13; /* flowfunction relay */
  515. c->operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
  516. c->operand[pos++] = (pid[k] >> 8) & 0x1f;
  517. c->operand[pos++] = pid[k] & 0xff;
  518. c->operand[pos++] = 0x00; /* tableID */
  519. c->operand[pos++] = 0x00; /* filter_length */
  520. }
  521. fdtv->avc_data_length = ALIGN(3 + pos, 4);
  522. ret = avc_write(fdtv);
  523. /* FIXME: check response code? */
  524. mutex_unlock(&fdtv->avc_mutex);
  525. if (ret == 0)
  526. msleep(50);
  527. return ret;
  528. }
  529. int avc_tuner_get_ts(struct firedtv *fdtv)
  530. {
  531. struct avc_command_frame *c = (void *)fdtv->avc_data;
  532. int ret, sl;
  533. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  534. return -EINTR;
  535. memset(c, 0, sizeof(*c));
  536. c->ctype = AVC_CTYPE_CONTROL;
  537. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  538. c->opcode = AVC_OPCODE_DSIT;
  539. sl = fdtv->type == FIREDTV_DVB_T ? 0x0c : 0x11;
  540. c->operand[0] = 0; /* source plug */
  541. c->operand[1] = 0xd2; /* subfunction replace */
  542. c->operand[2] = 0xff; /* status */
  543. c->operand[3] = 0x20; /* system id = DVB */
  544. c->operand[4] = 0x00; /* antenna number */
  545. c->operand[5] = 0x0; /* system_specific_search_flags */
  546. c->operand[6] = sl; /* system_specific_multiplex selection_length */
  547. c->operand[7] = 0x00; /* valid_flags [0] */
  548. c->operand[8] = 0x00; /* valid_flags [1] */
  549. c->operand[7 + sl] = 0x00; /* nr_of_dsit_sel_specs (always 0) */
  550. fdtv->avc_data_length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
  551. ret = avc_write(fdtv);
  552. /* FIXME: check response code? */
  553. mutex_unlock(&fdtv->avc_mutex);
  554. if (ret == 0)
  555. msleep(250);
  556. return ret;
  557. }
  558. int avc_identify_subunit(struct firedtv *fdtv)
  559. {
  560. struct avc_command_frame *c = (void *)fdtv->avc_data;
  561. struct avc_response_frame *r = (void *)fdtv->avc_data;
  562. int ret;
  563. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  564. return -EINTR;
  565. memset(c, 0, sizeof(*c));
  566. c->ctype = AVC_CTYPE_CONTROL;
  567. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  568. c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
  569. c->operand[0] = DESCRIPTOR_SUBUNIT_IDENTIFIER;
  570. c->operand[1] = 0xff;
  571. c->operand[2] = 0x00;
  572. c->operand[3] = 0x00; /* length highbyte */
  573. c->operand[4] = 0x08; /* length lowbyte */
  574. c->operand[5] = 0x00; /* offset highbyte */
  575. c->operand[6] = 0x0d; /* offset lowbyte */
  576. fdtv->avc_data_length = 12;
  577. ret = avc_write(fdtv);
  578. if (ret < 0)
  579. goto out;
  580. if ((r->response != AVC_RESPONSE_STABLE &&
  581. r->response != AVC_RESPONSE_ACCEPTED) ||
  582. (r->operand[3] << 8) + r->operand[4] != 8) {
  583. dev_err(fdtv->device, "cannot read subunit identifier\n");
  584. ret = -EINVAL;
  585. }
  586. out:
  587. mutex_unlock(&fdtv->avc_mutex);
  588. return ret;
  589. }
  590. #define SIZEOF_ANTENNA_INPUT_INFO 22
  591. int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
  592. {
  593. struct avc_command_frame *c = (void *)fdtv->avc_data;
  594. struct avc_response_frame *r = (void *)fdtv->avc_data;
  595. int length, ret;
  596. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  597. return -EINTR;
  598. memset(c, 0, sizeof(*c));
  599. c->ctype = AVC_CTYPE_CONTROL;
  600. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  601. c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
  602. c->operand[0] = DESCRIPTOR_TUNER_STATUS;
  603. c->operand[1] = 0xff; /* read_result_status */
  604. c->operand[2] = 0x00; /* reserved */
  605. c->operand[3] = 0; /* SIZEOF_ANTENNA_INPUT_INFO >> 8; */
  606. c->operand[4] = 0; /* SIZEOF_ANTENNA_INPUT_INFO & 0xff; */
  607. c->operand[5] = 0x00;
  608. c->operand[6] = 0x00;
  609. fdtv->avc_data_length = 12;
  610. ret = avc_write(fdtv);
  611. if (ret < 0)
  612. goto out;
  613. if (r->response != AVC_RESPONSE_STABLE &&
  614. r->response != AVC_RESPONSE_ACCEPTED) {
  615. dev_err(fdtv->device, "cannot read tuner status\n");
  616. ret = -EINVAL;
  617. goto out;
  618. }
  619. length = r->operand[9];
  620. if (r->operand[1] != 0x10 || length != SIZEOF_ANTENNA_INPUT_INFO) {
  621. dev_err(fdtv->device, "got invalid tuner status\n");
  622. ret = -EINVAL;
  623. goto out;
  624. }
  625. stat->active_system = r->operand[10];
  626. stat->searching = r->operand[11] >> 7 & 1;
  627. stat->moving = r->operand[11] >> 6 & 1;
  628. stat->no_rf = r->operand[11] >> 5 & 1;
  629. stat->input = r->operand[12] >> 7 & 1;
  630. stat->selected_antenna = r->operand[12] & 0x7f;
  631. stat->ber = r->operand[13] << 24 |
  632. r->operand[14] << 16 |
  633. r->operand[15] << 8 |
  634. r->operand[16];
  635. stat->signal_strength = r->operand[17];
  636. stat->raster_frequency = r->operand[18] >> 6 & 2;
  637. stat->rf_frequency = (r->operand[18] & 0x3f) << 16 |
  638. r->operand[19] << 8 |
  639. r->operand[20];
  640. stat->man_dep_info_length = r->operand[21];
  641. stat->front_end_error = r->operand[22] >> 4 & 1;
  642. stat->antenna_error = r->operand[22] >> 3 & 1;
  643. stat->front_end_power_status = r->operand[22] >> 1 & 1;
  644. stat->power_supply = r->operand[22] & 1;
  645. stat->carrier_noise_ratio = r->operand[23] << 8 |
  646. r->operand[24];
  647. stat->power_supply_voltage = r->operand[27];
  648. stat->antenna_voltage = r->operand[28];
  649. stat->firewire_bus_voltage = r->operand[29];
  650. stat->ca_mmi = r->operand[30] & 1;
  651. stat->ca_pmt_reply = r->operand[31] >> 7 & 1;
  652. stat->ca_date_time_request = r->operand[31] >> 6 & 1;
  653. stat->ca_application_info = r->operand[31] >> 5 & 1;
  654. stat->ca_module_present_status = r->operand[31] >> 4 & 1;
  655. stat->ca_dvb_flag = r->operand[31] >> 3 & 1;
  656. stat->ca_error_flag = r->operand[31] >> 2 & 1;
  657. stat->ca_initialization_status = r->operand[31] >> 1 & 1;
  658. out:
  659. mutex_unlock(&fdtv->avc_mutex);
  660. return ret;
  661. }
  662. int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
  663. char conttone, char nrdiseq,
  664. struct dvb_diseqc_master_cmd *diseqcmd)
  665. {
  666. struct avc_command_frame *c = (void *)fdtv->avc_data;
  667. struct avc_response_frame *r = (void *)fdtv->avc_data;
  668. int i, j, k, ret;
  669. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  670. return -EINTR;
  671. memset(c, 0, sizeof(*c));
  672. c->ctype = AVC_CTYPE_CONTROL;
  673. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  674. c->opcode = AVC_OPCODE_VENDOR;
  675. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  676. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  677. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  678. c->operand[3] = SFE_VENDOR_OPCODE_LNB_CONTROL;
  679. c->operand[4] = voltage;
  680. c->operand[5] = nrdiseq;
  681. i = 6;
  682. for (j = 0; j < nrdiseq; j++) {
  683. c->operand[i++] = diseqcmd[j].msg_len;
  684. for (k = 0; k < diseqcmd[j].msg_len; k++)
  685. c->operand[i++] = diseqcmd[j].msg[k];
  686. }
  687. c->operand[i++] = burst;
  688. c->operand[i++] = conttone;
  689. fdtv->avc_data_length = ALIGN(3 + i, 4);
  690. ret = avc_write(fdtv);
  691. if (ret < 0)
  692. goto out;
  693. if (r->response != AVC_RESPONSE_ACCEPTED) {
  694. dev_err(fdtv->device, "LNB control failed\n");
  695. ret = -EINVAL;
  696. }
  697. out:
  698. mutex_unlock(&fdtv->avc_mutex);
  699. return ret;
  700. }
  701. int avc_register_remote_control(struct firedtv *fdtv)
  702. {
  703. struct avc_command_frame *c = (void *)fdtv->avc_data;
  704. int ret;
  705. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  706. return -EINTR;
  707. memset(c, 0, sizeof(*c));
  708. c->ctype = AVC_CTYPE_NOTIFY;
  709. c->subunit = AVC_SUBUNIT_TYPE_UNIT | 7;
  710. c->opcode = AVC_OPCODE_VENDOR;
  711. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  712. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  713. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  714. c->operand[3] = SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
  715. fdtv->avc_data_length = 8;
  716. ret = avc_write(fdtv);
  717. /* FIXME: check response code? */
  718. mutex_unlock(&fdtv->avc_mutex);
  719. return ret;
  720. }
  721. void avc_remote_ctrl_work(struct work_struct *work)
  722. {
  723. struct firedtv *fdtv =
  724. container_of(work, struct firedtv, remote_ctrl_work);
  725. /* Should it be rescheduled in failure cases? */
  726. avc_register_remote_control(fdtv);
  727. }
  728. #if 0 /* FIXME: unused */
  729. int avc_tuner_host2ca(struct firedtv *fdtv)
  730. {
  731. struct avc_command_frame *c = (void *)fdtv->avc_data;
  732. int ret;
  733. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  734. return -EINTR;
  735. memset(c, 0, sizeof(*c));
  736. c->ctype = AVC_CTYPE_CONTROL;
  737. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  738. c->opcode = AVC_OPCODE_VENDOR;
  739. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  740. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  741. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  742. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  743. c->operand[4] = 0; /* slot */
  744. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  745. c->operand[6] = 0; /* more/last */
  746. c->operand[7] = 0; /* length */
  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. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  781. return -EINTR;
  782. memset(c, 0, sizeof(*c));
  783. c->ctype = AVC_CTYPE_STATUS;
  784. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  785. c->opcode = AVC_OPCODE_VENDOR;
  786. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  787. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  788. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  789. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  790. c->operand[4] = 0; /* slot */
  791. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  792. fdtv->avc_data_length = 12;
  793. ret = avc_write(fdtv);
  794. if (ret < 0)
  795. goto out;
  796. /* FIXME: check response code and validate response data */
  797. pos = get_ca_object_pos(r);
  798. app_info[0] = (EN50221_TAG_APP_INFO >> 16) & 0xff;
  799. app_info[1] = (EN50221_TAG_APP_INFO >> 8) & 0xff;
  800. app_info[2] = (EN50221_TAG_APP_INFO >> 0) & 0xff;
  801. app_info[3] = 6 + r->operand[pos + 4];
  802. app_info[4] = 0x01;
  803. memcpy(&app_info[5], &r->operand[pos], 5 + r->operand[pos + 4]);
  804. *len = app_info[3] + 4;
  805. out:
  806. mutex_unlock(&fdtv->avc_mutex);
  807. return ret;
  808. }
  809. int avc_ca_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
  810. {
  811. struct avc_command_frame *c = (void *)fdtv->avc_data;
  812. struct avc_response_frame *r = (void *)fdtv->avc_data;
  813. int pos, ret;
  814. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  815. return -EINTR;
  816. memset(c, 0, sizeof(*c));
  817. c->ctype = AVC_CTYPE_STATUS;
  818. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  819. c->opcode = AVC_OPCODE_VENDOR;
  820. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  821. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  822. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  823. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  824. c->operand[4] = 0; /* slot */
  825. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  826. fdtv->avc_data_length = 12;
  827. ret = avc_write(fdtv);
  828. if (ret < 0)
  829. goto out;
  830. /* FIXME: check response code and validate response data */
  831. pos = get_ca_object_pos(r);
  832. app_info[0] = (EN50221_TAG_CA_INFO >> 16) & 0xff;
  833. app_info[1] = (EN50221_TAG_CA_INFO >> 8) & 0xff;
  834. app_info[2] = (EN50221_TAG_CA_INFO >> 0) & 0xff;
  835. app_info[3] = 2;
  836. app_info[4] = r->operand[pos + 0];
  837. app_info[5] = r->operand[pos + 1];
  838. *len = app_info[3] + 4;
  839. out:
  840. mutex_unlock(&fdtv->avc_mutex);
  841. return ret;
  842. }
  843. int avc_ca_reset(struct firedtv *fdtv)
  844. {
  845. struct avc_command_frame *c = (void *)fdtv->avc_data;
  846. int ret;
  847. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  848. return -EINTR;
  849. memset(c, 0, sizeof(*c));
  850. c->ctype = AVC_CTYPE_CONTROL;
  851. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  852. c->opcode = AVC_OPCODE_VENDOR;
  853. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  854. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  855. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  856. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  857. c->operand[4] = 0; /* slot */
  858. c->operand[5] = SFE_VENDOR_TAG_CA_RESET; /* ca tag */
  859. c->operand[6] = 0; /* more/last */
  860. c->operand[7] = 1; /* length */
  861. c->operand[8] = 0; /* force hardware reset */
  862. fdtv->avc_data_length = 12;
  863. ret = avc_write(fdtv);
  864. /* FIXME: check response code? */
  865. mutex_unlock(&fdtv->avc_mutex);
  866. return ret;
  867. }
  868. int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
  869. {
  870. struct avc_command_frame *c = (void *)fdtv->avc_data;
  871. struct avc_response_frame *r = (void *)fdtv->avc_data;
  872. int list_management;
  873. int program_info_length;
  874. int pmt_cmd_id;
  875. int read_pos;
  876. int write_pos;
  877. int es_info_length;
  878. int crc32_csum;
  879. int ret;
  880. if (unlikely(avc_debug & AVC_DEBUG_APPLICATION_PMT))
  881. debug_pmt(msg, length);
  882. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  883. return -EINTR;
  884. memset(c, 0, sizeof(*c));
  885. c->ctype = AVC_CTYPE_CONTROL;
  886. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  887. c->opcode = AVC_OPCODE_VENDOR;
  888. if (msg[0] != EN50221_LIST_MANAGEMENT_ONLY) {
  889. dev_info(fdtv->device, "forcing list_management to ONLY\n");
  890. msg[0] = EN50221_LIST_MANAGEMENT_ONLY;
  891. }
  892. /* We take the cmd_id from the programme level only! */
  893. list_management = msg[0];
  894. program_info_length = ((msg[4] & 0x0f) << 8) + msg[5];
  895. if (program_info_length > 0)
  896. program_info_length--; /* Remove pmt_cmd_id */
  897. pmt_cmd_id = msg[6];
  898. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  899. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  900. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  901. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  902. c->operand[4] = 0; /* slot */
  903. c->operand[5] = SFE_VENDOR_TAG_CA_PMT; /* ca tag */
  904. c->operand[6] = 0; /* more/last */
  905. /* Use three bytes for length field in case length > 127 */
  906. c->operand[10] = list_management;
  907. c->operand[11] = 0x01; /* pmt_cmd=OK_descramble */
  908. /* TS program map table */
  909. c->operand[12] = 0x02; /* Table id=2 */
  910. c->operand[13] = 0x80; /* Section syntax + length */
  911. /* c->operand[14] = XXXprogram_info_length + 12; */
  912. c->operand[15] = msg[1]; /* Program number */
  913. c->operand[16] = msg[2];
  914. c->operand[17] = 0x01; /* Version number=0 + current/next=1 */
  915. c->operand[18] = 0x00; /* Section number=0 */
  916. c->operand[19] = 0x00; /* Last section number=0 */
  917. c->operand[20] = 0x1f; /* PCR_PID=1FFF */
  918. c->operand[21] = 0xff;
  919. c->operand[22] = (program_info_length >> 8); /* Program info length */
  920. c->operand[23] = (program_info_length & 0xff);
  921. /* CA descriptors at programme level */
  922. read_pos = 6;
  923. write_pos = 24;
  924. if (program_info_length > 0) {
  925. pmt_cmd_id = msg[read_pos++];
  926. if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
  927. dev_err(fdtv->device,
  928. "invalid pmt_cmd_id %d\n", pmt_cmd_id);
  929. memcpy(&c->operand[write_pos], &msg[read_pos],
  930. program_info_length);
  931. read_pos += program_info_length;
  932. write_pos += program_info_length;
  933. }
  934. while (read_pos < length) {
  935. c->operand[write_pos++] = msg[read_pos++];
  936. c->operand[write_pos++] = msg[read_pos++];
  937. c->operand[write_pos++] = msg[read_pos++];
  938. es_info_length =
  939. ((msg[read_pos] & 0x0f) << 8) + msg[read_pos + 1];
  940. read_pos += 2;
  941. if (es_info_length > 0)
  942. es_info_length--; /* Remove pmt_cmd_id */
  943. c->operand[write_pos++] = es_info_length >> 8;
  944. c->operand[write_pos++] = es_info_length & 0xff;
  945. if (es_info_length > 0) {
  946. pmt_cmd_id = msg[read_pos++];
  947. if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
  948. dev_err(fdtv->device, "invalid pmt_cmd_id %d "
  949. "at stream level\n", pmt_cmd_id);
  950. memcpy(&c->operand[write_pos], &msg[read_pos],
  951. es_info_length);
  952. read_pos += es_info_length;
  953. write_pos += es_info_length;
  954. }
  955. }
  956. /* CRC */
  957. c->operand[write_pos++] = 0x00;
  958. c->operand[write_pos++] = 0x00;
  959. c->operand[write_pos++] = 0x00;
  960. c->operand[write_pos++] = 0x00;
  961. c->operand[7] = 0x82;
  962. c->operand[8] = (write_pos - 10) >> 8;
  963. c->operand[9] = (write_pos - 10) & 0xff;
  964. c->operand[14] = write_pos - 15;
  965. crc32_csum = crc32_be(0, &c->operand[10], c->operand[12] - 1);
  966. c->operand[write_pos - 4] = (crc32_csum >> 24) & 0xff;
  967. c->operand[write_pos - 3] = (crc32_csum >> 16) & 0xff;
  968. c->operand[write_pos - 2] = (crc32_csum >> 8) & 0xff;
  969. c->operand[write_pos - 1] = (crc32_csum >> 0) & 0xff;
  970. fdtv->avc_data_length = ALIGN(3 + write_pos, 4);
  971. ret = avc_write(fdtv);
  972. if (ret < 0)
  973. goto out;
  974. if (r->response != AVC_RESPONSE_ACCEPTED) {
  975. dev_err(fdtv->device,
  976. "CA PMT failed with response 0x%x\n", r->response);
  977. ret = -EFAULT;
  978. }
  979. out:
  980. mutex_unlock(&fdtv->avc_mutex);
  981. return ret;
  982. }
  983. int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
  984. {
  985. struct avc_command_frame *c = (void *)fdtv->avc_data;
  986. struct avc_response_frame *r = (void *)fdtv->avc_data;
  987. int ret;
  988. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  989. return -EINTR;
  990. memset(c, 0, sizeof(*c));
  991. c->ctype = AVC_CTYPE_STATUS;
  992. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  993. c->opcode = AVC_OPCODE_VENDOR;
  994. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  995. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  996. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  997. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  998. c->operand[4] = 0; /* slot */
  999. c->operand[5] = SFE_VENDOR_TAG_CA_DATE_TIME; /* ca tag */
  1000. c->operand[6] = 0; /* more/last */
  1001. c->operand[7] = 0; /* length */
  1002. fdtv->avc_data_length = 12;
  1003. ret = avc_write(fdtv);
  1004. if (ret < 0)
  1005. goto out;
  1006. /* FIXME: check response code and validate response data */
  1007. *interval = r->operand[get_ca_object_pos(r)];
  1008. out:
  1009. mutex_unlock(&fdtv->avc_mutex);
  1010. return ret;
  1011. }
  1012. int avc_ca_enter_menu(struct firedtv *fdtv)
  1013. {
  1014. struct avc_command_frame *c = (void *)fdtv->avc_data;
  1015. int ret;
  1016. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  1017. return -EINTR;
  1018. memset(c, 0, sizeof(*c));
  1019. c->ctype = AVC_CTYPE_STATUS;
  1020. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  1021. c->opcode = AVC_OPCODE_VENDOR;
  1022. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  1023. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  1024. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  1025. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  1026. c->operand[4] = 0; /* slot */
  1027. c->operand[5] = SFE_VENDOR_TAG_CA_ENTER_MENU;
  1028. c->operand[6] = 0; /* more/last */
  1029. c->operand[7] = 0; /* length */
  1030. fdtv->avc_data_length = 12;
  1031. ret = avc_write(fdtv);
  1032. /* FIXME: check response code? */
  1033. mutex_unlock(&fdtv->avc_mutex);
  1034. return ret;
  1035. }
  1036. int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
  1037. {
  1038. struct avc_command_frame *c = (void *)fdtv->avc_data;
  1039. struct avc_response_frame *r = (void *)fdtv->avc_data;
  1040. int ret;
  1041. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  1042. return -EINTR;
  1043. memset(c, 0, sizeof(*c));
  1044. c->ctype = AVC_CTYPE_STATUS;
  1045. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  1046. c->opcode = AVC_OPCODE_VENDOR;
  1047. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  1048. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  1049. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  1050. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  1051. c->operand[4] = 0; /* slot */
  1052. c->operand[5] = SFE_VENDOR_TAG_CA_MMI;
  1053. c->operand[6] = 0; /* more/last */
  1054. c->operand[7] = 0; /* length */
  1055. fdtv->avc_data_length = 12;
  1056. ret = avc_write(fdtv);
  1057. if (ret < 0)
  1058. goto out;
  1059. /* FIXME: check response code and validate response data */
  1060. *len = get_ca_object_length(r);
  1061. memcpy(mmi_object, &r->operand[get_ca_object_pos(r)], *len);
  1062. out:
  1063. mutex_unlock(&fdtv->avc_mutex);
  1064. return ret;
  1065. }
  1066. #define CMP_OUTPUT_PLUG_CONTROL_REG_0 0xfffff0000904ULL
  1067. static int cmp_read(struct firedtv *fdtv, u64 addr, __be32 *data)
  1068. {
  1069. int ret;
  1070. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  1071. return -EINTR;
  1072. ret = fdtv->backend->read(fdtv, addr, data);
  1073. if (ret < 0)
  1074. dev_err(fdtv->device, "CMP: read I/O error\n");
  1075. mutex_unlock(&fdtv->avc_mutex);
  1076. return ret;
  1077. }
  1078. static int cmp_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
  1079. {
  1080. int ret;
  1081. if (mutex_lock_interruptible(&fdtv->avc_mutex))
  1082. return -EINTR;
  1083. /* data[] is stack-allocated and should not be DMA-mapped. */
  1084. memcpy(fdtv->avc_data, data, 8);
  1085. ret = fdtv->backend->lock(fdtv, addr, fdtv->avc_data);
  1086. if (ret < 0)
  1087. dev_err(fdtv->device, "CMP: lock I/O error\n");
  1088. else
  1089. memcpy(data, fdtv->avc_data, 8);
  1090. mutex_unlock(&fdtv->avc_mutex);
  1091. return ret;
  1092. }
  1093. static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
  1094. {
  1095. return (be32_to_cpu(opcr) >> shift) & mask;
  1096. }
  1097. static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
  1098. {
  1099. *opcr &= ~cpu_to_be32(mask << shift);
  1100. *opcr |= cpu_to_be32((value & mask) << shift);
  1101. }
  1102. #define get_opcr_online(v) get_opcr((v), 0x1, 31)
  1103. #define get_opcr_p2p_connections(v) get_opcr((v), 0x3f, 24)
  1104. #define get_opcr_channel(v) get_opcr((v), 0x3f, 16)
  1105. #define set_opcr_p2p_connections(p, v) set_opcr((p), (v), 0x3f, 24)
  1106. #define set_opcr_channel(p, v) set_opcr((p), (v), 0x3f, 16)
  1107. #define set_opcr_data_rate(p, v) set_opcr((p), (v), 0x3, 14)
  1108. #define set_opcr_overhead_id(p, v) set_opcr((p), (v), 0xf, 10)
  1109. int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
  1110. {
  1111. __be32 old_opcr, opcr[2];
  1112. u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
  1113. int attempts = 0;
  1114. int ret;
  1115. ret = cmp_read(fdtv, opcr_address, opcr);
  1116. if (ret < 0)
  1117. return ret;
  1118. repeat:
  1119. if (!get_opcr_online(*opcr)) {
  1120. dev_err(fdtv->device, "CMP: output offline\n");
  1121. return -EBUSY;
  1122. }
  1123. old_opcr = *opcr;
  1124. if (get_opcr_p2p_connections(*opcr)) {
  1125. if (get_opcr_channel(*opcr) != channel) {
  1126. dev_err(fdtv->device, "CMP: cannot change channel\n");
  1127. return -EBUSY;
  1128. }
  1129. dev_info(fdtv->device, "CMP: overlaying connection\n");
  1130. /* We don't allocate isochronous resources. */
  1131. } else {
  1132. set_opcr_channel(opcr, channel);
  1133. set_opcr_data_rate(opcr, 2); /* S400 */
  1134. /* FIXME: this is for the worst case - optimize */
  1135. set_opcr_overhead_id(opcr, 0);
  1136. /*
  1137. * FIXME: allocate isochronous channel and bandwidth at IRM
  1138. * fdtv->backend->alloc_resources(fdtv, channels_mask, bw);
  1139. */
  1140. }
  1141. set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) + 1);
  1142. opcr[1] = *opcr;
  1143. opcr[0] = old_opcr;
  1144. ret = cmp_lock(fdtv, opcr_address, opcr);
  1145. if (ret < 0)
  1146. return ret;
  1147. if (old_opcr != *opcr) {
  1148. /*
  1149. * FIXME: if old_opcr.P2P_Connections > 0,
  1150. * deallocate isochronous channel and bandwidth at IRM
  1151. * if (...)
  1152. * fdtv->backend->dealloc_resources(fdtv, channel, bw);
  1153. */
  1154. if (++attempts < 6) /* arbitrary limit */
  1155. goto repeat;
  1156. return -EBUSY;
  1157. }
  1158. return 0;
  1159. }
  1160. void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
  1161. {
  1162. __be32 old_opcr, opcr[2];
  1163. u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
  1164. int attempts = 0;
  1165. if (cmp_read(fdtv, opcr_address, opcr) < 0)
  1166. return;
  1167. repeat:
  1168. if (!get_opcr_online(*opcr) || !get_opcr_p2p_connections(*opcr) ||
  1169. get_opcr_channel(*opcr) != channel) {
  1170. dev_err(fdtv->device, "CMP: no connection to break\n");
  1171. return;
  1172. }
  1173. old_opcr = *opcr;
  1174. set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) - 1);
  1175. opcr[1] = *opcr;
  1176. opcr[0] = old_opcr;
  1177. if (cmp_lock(fdtv, opcr_address, opcr) < 0)
  1178. return;
  1179. if (old_opcr != *opcr) {
  1180. /*
  1181. * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
  1182. * owner, deallocate isochronous channel and bandwidth at IRM
  1183. * if (...)
  1184. * fdtv->backend->dealloc_resources(fdtv, channel, bw);
  1185. */
  1186. if (++attempts < 6) /* arbitrary limit */
  1187. goto repeat;
  1188. }
  1189. }