smsdvb.c 13 KB

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