smsdvb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /****************************************************************
  2. Siano Mobile Silicon, Inc.
  3. MDTV receiver kernel modules.
  4. Copyright (C) 2006-2008, Uri Shkolnik
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ****************************************************************/
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include "smscoreapi.h"
  19. #include "smsendian.h"
  20. #include "sms-cards.h"
  21. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  22. struct smsdvb_client_t {
  23. struct list_head entry;
  24. struct smscore_device_t *coredev;
  25. struct smscore_client_t *smsclient;
  26. struct dvb_adapter adapter;
  27. struct dvb_demux demux;
  28. struct dmxdev dmxdev;
  29. struct dvb_frontend frontend;
  30. fe_status_t fe_status;
  31. int fe_ber, fe_snr, fe_unc, fe_signal_strength;
  32. struct completion tune_done, stat_done;
  33. /* todo: save freq/band instead whole struct */
  34. struct dvb_frontend_parameters fe_params;
  35. };
  36. static struct list_head g_smsdvb_clients;
  37. static struct mutex g_smsdvb_clientslock;
  38. static int sms_dbg;
  39. module_param_named(debug, sms_dbg, int, 0644);
  40. MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
  41. static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
  42. {
  43. struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
  44. struct SmsMsgHdr_ST *phdr =
  45. (struct SmsMsgHdr_ST *)(((u8 *) cb->p) + cb->offset);
  46. smsendian_handle_rx_message((struct SmsMsgData_ST *) phdr);
  47. switch (phdr->msgType) {
  48. case MSG_SMS_DVBT_BDA_DATA:
  49. dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
  50. cb->size - sizeof(struct SmsMsgHdr_ST));
  51. break;
  52. case MSG_SMS_RF_TUNE_RES:
  53. complete(&client->tune_done);
  54. break;
  55. case MSG_SMS_GET_STATISTICS_RES:
  56. {
  57. struct SmsMsgStatisticsInfo_ST *p =
  58. (struct SmsMsgStatisticsInfo_ST *)(phdr + 1);
  59. if (p->Stat.IsDemodLocked) {
  60. client->fe_status = FE_HAS_SIGNAL |
  61. FE_HAS_CARRIER |
  62. FE_HAS_VITERBI |
  63. FE_HAS_SYNC |
  64. FE_HAS_LOCK;
  65. client->fe_snr = p->Stat.SNR;
  66. client->fe_ber = p->Stat.BER;
  67. client->fe_unc = p->Stat.BERErrorCount;
  68. if (p->Stat.InBandPwr < -95)
  69. client->fe_signal_strength = 0;
  70. else if (p->Stat.InBandPwr > -29)
  71. client->fe_signal_strength = 100;
  72. else
  73. client->fe_signal_strength =
  74. (p->Stat.InBandPwr + 95) * 3 / 2;
  75. } else {
  76. client->fe_status = 0;
  77. client->fe_snr =
  78. client->fe_ber =
  79. client->fe_unc =
  80. client->fe_signal_strength = 0;
  81. }
  82. complete(&client->stat_done);
  83. break;
  84. } }
  85. smscore_putbuffer(client->coredev, cb);
  86. return 0;
  87. }
  88. static void smsdvb_unregister_client(struct smsdvb_client_t *client)
  89. {
  90. /* must be called under clientslock */
  91. list_del(&client->entry);
  92. smscore_unregister_client(client->smsclient);
  93. dvb_unregister_frontend(&client->frontend);
  94. dvb_dmxdev_release(&client->dmxdev);
  95. dvb_dmx_release(&client->demux);
  96. dvb_unregister_adapter(&client->adapter);
  97. kfree(client);
  98. }
  99. static void smsdvb_onremove(void *context)
  100. {
  101. kmutex_lock(&g_smsdvb_clientslock);
  102. smsdvb_unregister_client((struct smsdvb_client_t *) context);
  103. kmutex_unlock(&g_smsdvb_clientslock);
  104. }
  105. static int smsdvb_start_feed(struct dvb_demux_feed *feed)
  106. {
  107. struct smsdvb_client_t *client =
  108. container_of(feed->demux, struct smsdvb_client_t, demux);
  109. struct SmsMsgData_ST PidMsg;
  110. sms_debug("add pid %d(%x)",
  111. feed->pid, feed->pid);
  112. PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
  113. PidMsg.xMsgHeader.msgDstId = HIF_TASK;
  114. PidMsg.xMsgHeader.msgFlags = 0;
  115. PidMsg.xMsgHeader.msgType = MSG_SMS_ADD_PID_FILTER_REQ;
  116. PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
  117. PidMsg.msgData[0] = feed->pid;
  118. smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
  119. return smsclient_sendrequest(client->smsclient,
  120. &PidMsg, sizeof(PidMsg));
  121. }
  122. static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
  123. {
  124. struct smsdvb_client_t *client =
  125. container_of(feed->demux, struct smsdvb_client_t, demux);
  126. struct SmsMsgData_ST PidMsg;
  127. sms_debug("remove pid %d(%x)",
  128. feed->pid, feed->pid);
  129. PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
  130. PidMsg.xMsgHeader.msgDstId = HIF_TASK;
  131. PidMsg.xMsgHeader.msgFlags = 0;
  132. PidMsg.xMsgHeader.msgType = MSG_SMS_REMOVE_PID_FILTER_REQ;
  133. PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
  134. PidMsg.msgData[0] = feed->pid;
  135. smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
  136. return smsclient_sendrequest(client->smsclient,
  137. &PidMsg, sizeof(PidMsg));
  138. }
  139. static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
  140. void *buffer, size_t size,
  141. struct completion *completion)
  142. {
  143. int rc;
  144. smsendian_handle_tx_message((struct SmsMsgHdr_ST *)buffer);
  145. rc = smsclient_sendrequest(client->smsclient, buffer, size);
  146. if (rc < 0)
  147. return rc;
  148. return wait_for_completion_timeout(completion,
  149. msecs_to_jiffies(2000)) ?
  150. 0 : -ETIME;
  151. }
  152. static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
  153. {
  154. struct SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ,
  155. DVBT_BDA_CONTROL_MSG_ID,
  156. HIF_TASK, sizeof(struct SmsMsgHdr_ST), 0 };
  157. int ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
  158. &client->stat_done);
  159. if (ret < 0)
  160. return ret;
  161. if (client->fe_status & FE_HAS_LOCK)
  162. sms_board_led_feedback(client->coredev,
  163. (client->fe_unc == 0) ?
  164. SMS_LED_HI : SMS_LED_LO);
  165. else
  166. sms_board_led_feedback(client->coredev, SMS_LED_OFF);
  167. return ret;
  168. }
  169. static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
  170. {
  171. struct smsdvb_client_t *client =
  172. container_of(fe, struct smsdvb_client_t, frontend);
  173. int rc = smsdvb_send_statistics_request(client);
  174. if (!rc)
  175. *stat = client->fe_status;
  176. return rc;
  177. }
  178. static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
  179. {
  180. struct smsdvb_client_t *client =
  181. container_of(fe, struct smsdvb_client_t, frontend);
  182. int rc = smsdvb_send_statistics_request(client);
  183. if (!rc)
  184. *ber = client->fe_ber;
  185. return rc;
  186. }
  187. static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  188. {
  189. struct smsdvb_client_t *client =
  190. container_of(fe, struct smsdvb_client_t, frontend);
  191. int rc = smsdvb_send_statistics_request(client);
  192. if (!rc)
  193. *strength = client->fe_signal_strength;
  194. return rc;
  195. }
  196. static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
  197. {
  198. struct smsdvb_client_t *client =
  199. container_of(fe, struct smsdvb_client_t, frontend);
  200. int rc = smsdvb_send_statistics_request(client);
  201. if (!rc)
  202. *snr = client->fe_snr;
  203. return rc;
  204. }
  205. static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  206. {
  207. struct smsdvb_client_t *client =
  208. container_of(fe, struct smsdvb_client_t, frontend);
  209. int rc = smsdvb_send_statistics_request(client);
  210. if (!rc)
  211. *ucblocks = client->fe_unc;
  212. return rc;
  213. }
  214. static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
  215. struct dvb_frontend_tune_settings *tune)
  216. {
  217. sms_debug("");
  218. tune->min_delay_ms = 400;
  219. tune->step_size = 250000;
  220. tune->max_drift = 0;
  221. return 0;
  222. }
  223. static int smsdvb_set_frontend(struct dvb_frontend *fe,
  224. struct dvb_frontend_parameters *fep)
  225. {
  226. struct smsdvb_client_t *client =
  227. container_of(fe, struct smsdvb_client_t, frontend);
  228. struct {
  229. struct SmsMsgHdr_ST Msg;
  230. u32 Data[3];
  231. } Msg;
  232. int ret;
  233. Msg.Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
  234. Msg.Msg.msgDstId = HIF_TASK;
  235. Msg.Msg.msgFlags = 0;
  236. Msg.Msg.msgType = MSG_SMS_RF_TUNE_REQ;
  237. Msg.Msg.msgLength = sizeof(Msg);
  238. Msg.Data[0] = fep->frequency;
  239. Msg.Data[2] = 12000000;
  240. sms_debug("freq %d band %d",
  241. fep->frequency, fep->u.ofdm.bandwidth);
  242. switch (fep->u.ofdm.bandwidth) {
  243. case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
  244. case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
  245. case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
  246. case BANDWIDTH_AUTO: return -EOPNOTSUPP;
  247. default: return -EINVAL;
  248. }
  249. /* Disable LNA, if any. An error is returned if no LNA is present */
  250. ret = sms_board_lna_control(client->coredev, 0);
  251. if (ret == 0) {
  252. fe_status_t status;
  253. /* tune with LNA off at first */
  254. ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
  255. &client->tune_done);
  256. smsdvb_read_status(fe, &status);
  257. if (status & FE_HAS_LOCK)
  258. return ret;
  259. /* previous tune didnt lock - enable LNA and tune again */
  260. sms_board_lna_control(client->coredev, 1);
  261. }
  262. return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
  263. &client->tune_done);
  264. }
  265. static int smsdvb_get_frontend(struct dvb_frontend *fe,
  266. struct dvb_frontend_parameters *fep)
  267. {
  268. struct smsdvb_client_t *client =
  269. container_of(fe, struct smsdvb_client_t, frontend);
  270. sms_debug("");
  271. /* todo: */
  272. memcpy(fep, &client->fe_params,
  273. sizeof(struct dvb_frontend_parameters));
  274. return 0;
  275. }
  276. static int smsdvb_init(struct dvb_frontend *fe)
  277. {
  278. struct smsdvb_client_t *client =
  279. container_of(fe, struct smsdvb_client_t, frontend);
  280. sms_board_power(client->coredev, 1);
  281. return 0;
  282. }
  283. static int smsdvb_sleep(struct dvb_frontend *fe)
  284. {
  285. struct smsdvb_client_t *client =
  286. container_of(fe, struct smsdvb_client_t, frontend);
  287. sms_board_led_feedback(client->coredev, SMS_LED_OFF);
  288. sms_board_power(client->coredev, 0);
  289. return 0;
  290. }
  291. static void smsdvb_release(struct dvb_frontend *fe)
  292. {
  293. /* do nothing */
  294. }
  295. static struct dvb_frontend_ops smsdvb_fe_ops = {
  296. .info = {
  297. .name = "Siano Mobile Digital MDTV Receiver",
  298. .type = FE_OFDM,
  299. .frequency_min = 44250000,
  300. .frequency_max = 867250000,
  301. .frequency_stepsize = 250000,
  302. .caps = FE_CAN_INVERSION_AUTO |
  303. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  304. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  305. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  306. FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
  307. FE_CAN_GUARD_INTERVAL_AUTO |
  308. FE_CAN_RECOVER |
  309. FE_CAN_HIERARCHY_AUTO,
  310. },
  311. .release = smsdvb_release,
  312. .set_frontend = smsdvb_set_frontend,
  313. .get_frontend = smsdvb_get_frontend,
  314. .get_tune_settings = smsdvb_get_tune_settings,
  315. .read_status = smsdvb_read_status,
  316. .read_ber = smsdvb_read_ber,
  317. .read_signal_strength = smsdvb_read_signal_strength,
  318. .read_snr = smsdvb_read_snr,
  319. .read_ucblocks = smsdvb_read_ucblocks,
  320. .init = smsdvb_init,
  321. .sleep = smsdvb_sleep,
  322. };
  323. static int smsdvb_hotplug(struct smscore_device_t *coredev,
  324. struct device *device, int arrival)
  325. {
  326. struct smsclient_params_t params;
  327. struct smsdvb_client_t *client;
  328. int rc;
  329. /* device removal handled by onremove callback */
  330. if (!arrival)
  331. return 0;
  332. if (smscore_get_device_mode(coredev) != DEVICE_MODE_DVBT_BDA) {
  333. sms_err("SMS Device mode is not set for "
  334. "DVB operation.");
  335. return 0;
  336. }
  337. client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
  338. if (!client) {
  339. sms_err("kmalloc() failed");
  340. return -ENOMEM;
  341. }
  342. /* register dvb adapter */
  343. rc = dvb_register_adapter(&client->adapter,
  344. sms_get_board(
  345. smscore_get_board_id(coredev))->name,
  346. THIS_MODULE, device, adapter_nr);
  347. if (rc < 0) {
  348. sms_err("dvb_register_adapter() failed %d", rc);
  349. goto adapter_error;
  350. }
  351. /* init dvb demux */
  352. client->demux.dmx.capabilities = DMX_TS_FILTERING;
  353. client->demux.filternum = 32; /* todo: nova ??? */
  354. client->demux.feednum = 32;
  355. client->demux.start_feed = smsdvb_start_feed;
  356. client->demux.stop_feed = smsdvb_stop_feed;
  357. rc = dvb_dmx_init(&client->demux);
  358. if (rc < 0) {
  359. sms_err("dvb_dmx_init failed %d", rc);
  360. goto dvbdmx_error;
  361. }
  362. /* init dmxdev */
  363. client->dmxdev.filternum = 32;
  364. client->dmxdev.demux = &client->demux.dmx;
  365. client->dmxdev.capabilities = 0;
  366. rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
  367. if (rc < 0) {
  368. sms_err("dvb_dmxdev_init failed %d", rc);
  369. goto dmxdev_error;
  370. }
  371. /* init and register frontend */
  372. memcpy(&client->frontend.ops, &smsdvb_fe_ops,
  373. sizeof(struct dvb_frontend_ops));
  374. rc = dvb_register_frontend(&client->adapter, &client->frontend);
  375. if (rc < 0) {
  376. sms_err("frontend registration failed %d", rc);
  377. goto frontend_error;
  378. }
  379. params.initial_id = 1;
  380. params.data_type = MSG_SMS_DVBT_BDA_DATA;
  381. params.onresponse_handler = smsdvb_onresponse;
  382. params.onremove_handler = smsdvb_onremove;
  383. params.context = client;
  384. rc = smscore_register_client(coredev, &params, &client->smsclient);
  385. if (rc < 0) {
  386. sms_err("smscore_register_client() failed %d", rc);
  387. goto client_error;
  388. }
  389. client->coredev = coredev;
  390. init_completion(&client->tune_done);
  391. kmutex_lock(&g_smsdvb_clientslock);
  392. list_add(&client->entry, &g_smsdvb_clients);
  393. kmutex_unlock(&g_smsdvb_clientslock);
  394. sms_info("success");
  395. sms_board_setup(coredev);
  396. return 0;
  397. client_error:
  398. dvb_unregister_frontend(&client->frontend);
  399. frontend_error:
  400. dvb_dmxdev_release(&client->dmxdev);
  401. dmxdev_error:
  402. dvb_dmx_release(&client->demux);
  403. dvbdmx_error:
  404. dvb_unregister_adapter(&client->adapter);
  405. adapter_error:
  406. kfree(client);
  407. return rc;
  408. }
  409. int smsdvb_module_init(void)
  410. {
  411. int rc;
  412. INIT_LIST_HEAD(&g_smsdvb_clients);
  413. kmutex_init(&g_smsdvb_clientslock);
  414. rc = smscore_register_hotplug(smsdvb_hotplug);
  415. sms_debug("");
  416. return rc;
  417. }
  418. void smsdvb_module_exit(void)
  419. {
  420. smscore_unregister_hotplug(smsdvb_hotplug);
  421. kmutex_lock(&g_smsdvb_clientslock);
  422. while (!list_empty(&g_smsdvb_clients))
  423. smsdvb_unregister_client(
  424. (struct smsdvb_client_t *) g_smsdvb_clients.next);
  425. kmutex_unlock(&g_smsdvb_clientslock);
  426. }
  427. module_init(smsdvb_module_init);
  428. module_exit(smsdvb_module_exit);
  429. MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
  430. MODULE_AUTHOR("Siano Mobile Silicon, INC. (uris@siano-ms.com)");
  431. MODULE_LICENSE("GPL");