pd-dvb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. #include "pd-common.h"
  2. #include <linux/kernel.h>
  3. #include <linux/usb.h>
  4. #include <linux/dvb/dmx.h>
  5. #include <linux/delay.h>
  6. #include "vendorcmds.h"
  7. #include <linux/sched.h>
  8. #include <asm/atomic.h>
  9. static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb);
  10. static int dvb_bandwidth[][2] = {
  11. { TLG_BW_8, BANDWIDTH_8_MHZ },
  12. { TLG_BW_7, BANDWIDTH_7_MHZ },
  13. { TLG_BW_6, BANDWIDTH_6_MHZ }
  14. };
  15. static int dvb_bandwidth_length = ARRAY_SIZE(dvb_bandwidth);
  16. static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb);
  17. static int poseidon_check_mode_dvbt(struct poseidon *pd)
  18. {
  19. s32 ret = 0, cmd_status = 0;
  20. set_current_state(TASK_INTERRUPTIBLE);
  21. schedule_timeout(HZ/4);
  22. ret = usb_set_interface(pd->udev, 0, BULK_ALTERNATE_IFACE);
  23. if (ret != 0)
  24. return ret;
  25. ret = set_tuner_mode(pd, TLG_MODE_CAPS_DVB_T);
  26. if (ret)
  27. return ret;
  28. /* signal source */
  29. ret = send_set_req(pd, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &cmd_status);
  30. if (ret|cmd_status)
  31. return ret;
  32. return 0;
  33. }
  34. /* acquire :
  35. * 1 == open
  36. * 0 == release
  37. */
  38. static int poseidon_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
  39. {
  40. struct poseidon *pd = fe->demodulator_priv;
  41. struct pd_dvb_adapter *pd_dvb;
  42. int ret = 0;
  43. if (!pd)
  44. return -ENODEV;
  45. pd_dvb = container_of(fe, struct pd_dvb_adapter, dvb_fe);
  46. if (acquire) {
  47. mutex_lock(&pd->lock);
  48. if (pd->state & POSEIDON_STATE_DISCONNECT) {
  49. ret = -ENODEV;
  50. goto open_out;
  51. }
  52. if (pd->state && !(pd->state & POSEIDON_STATE_DVBT)) {
  53. ret = -EBUSY;
  54. goto open_out;
  55. }
  56. usb_autopm_get_interface(pd->interface);
  57. if (0 == pd->state) {
  58. ret = poseidon_check_mode_dvbt(pd);
  59. if (ret < 0) {
  60. usb_autopm_put_interface(pd->interface);
  61. goto open_out;
  62. }
  63. pd->state |= POSEIDON_STATE_DVBT;
  64. pd_dvb->bandwidth = 0;
  65. pd_dvb->prev_freq = 0;
  66. }
  67. atomic_inc(&pd_dvb->users);
  68. kref_get(&pd->kref);
  69. open_out:
  70. mutex_unlock(&pd->lock);
  71. } else {
  72. dvb_stop_streaming(pd_dvb);
  73. if (atomic_dec_and_test(&pd_dvb->users)) {
  74. mutex_lock(&pd->lock);
  75. pd->state &= ~POSEIDON_STATE_DVBT;
  76. mutex_unlock(&pd->lock);
  77. }
  78. kref_put(&pd->kref, poseidon_delete);
  79. usb_autopm_put_interface(pd->interface);
  80. }
  81. return ret;
  82. }
  83. static void poseidon_fe_release(struct dvb_frontend *fe)
  84. {
  85. struct poseidon *pd = fe->demodulator_priv;
  86. #ifdef CONFIG_PM
  87. pd->pm_suspend = NULL;
  88. pd->pm_resume = NULL;
  89. #endif
  90. }
  91. static s32 poseidon_fe_sleep(struct dvb_frontend *fe)
  92. {
  93. return 0;
  94. }
  95. /*
  96. * return true if we can satisfy the conditions, else return false.
  97. */
  98. static bool check_scan_ok(__u32 freq, int bandwidth,
  99. struct pd_dvb_adapter *adapter)
  100. {
  101. if (bandwidth < 0)
  102. return false;
  103. if (adapter->prev_freq == freq
  104. && adapter->bandwidth == bandwidth) {
  105. long nl = jiffies - adapter->last_jiffies;
  106. unsigned int msec ;
  107. msec = jiffies_to_msecs(abs(nl));
  108. return msec > 15000 ? true : false;
  109. }
  110. return true;
  111. }
  112. /*
  113. * Check if the firmware delays too long for an invalid frequency.
  114. */
  115. static int fw_delay_overflow(struct pd_dvb_adapter *adapter)
  116. {
  117. long nl = jiffies - adapter->last_jiffies;
  118. unsigned int msec ;
  119. msec = jiffies_to_msecs(abs(nl));
  120. return msec > 800 ? true : false;
  121. }
  122. static int poseidon_set_fe(struct dvb_frontend *fe,
  123. struct dvb_frontend_parameters *fep)
  124. {
  125. s32 ret = 0, cmd_status = 0;
  126. s32 i, bandwidth = -1;
  127. struct poseidon *pd = fe->demodulator_priv;
  128. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  129. if (in_hibernation(pd))
  130. return -EBUSY;
  131. mutex_lock(&pd->lock);
  132. for (i = 0; i < dvb_bandwidth_length; i++)
  133. if (fep->u.ofdm.bandwidth == dvb_bandwidth[i][1])
  134. bandwidth = dvb_bandwidth[i][0];
  135. if (check_scan_ok(fep->frequency, bandwidth, pd_dvb)) {
  136. ret = send_set_req(pd, TUNE_FREQ_SELECT,
  137. fep->frequency / 1000, &cmd_status);
  138. if (ret | cmd_status) {
  139. log("error line");
  140. goto front_out;
  141. }
  142. ret = send_set_req(pd, DVBT_BANDW_SEL,
  143. bandwidth, &cmd_status);
  144. if (ret | cmd_status) {
  145. log("error line");
  146. goto front_out;
  147. }
  148. ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
  149. if (ret | cmd_status) {
  150. log("error line");
  151. goto front_out;
  152. }
  153. /* save the context for future */
  154. memcpy(&pd_dvb->fe_param, fep, sizeof(*fep));
  155. pd_dvb->bandwidth = bandwidth;
  156. pd_dvb->prev_freq = fep->frequency;
  157. pd_dvb->last_jiffies = jiffies;
  158. }
  159. front_out:
  160. mutex_unlock(&pd->lock);
  161. return ret;
  162. }
  163. #ifdef CONFIG_PM
  164. static int pm_dvb_suspend(struct poseidon *pd)
  165. {
  166. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  167. dvb_stop_streaming(pd_dvb);
  168. dvb_urb_cleanup(pd_dvb);
  169. msleep(500);
  170. return 0;
  171. }
  172. static int pm_dvb_resume(struct poseidon *pd)
  173. {
  174. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  175. poseidon_check_mode_dvbt(pd);
  176. msleep(300);
  177. poseidon_set_fe(&pd_dvb->dvb_fe, &pd_dvb->fe_param);
  178. dvb_start_streaming(pd_dvb);
  179. return 0;
  180. }
  181. #endif
  182. static s32 poseidon_fe_init(struct dvb_frontend *fe)
  183. {
  184. struct poseidon *pd = fe->demodulator_priv;
  185. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  186. #ifdef CONFIG_PM
  187. pd->pm_suspend = pm_dvb_suspend;
  188. pd->pm_resume = pm_dvb_resume;
  189. #endif
  190. memset(&pd_dvb->fe_param, 0,
  191. sizeof(struct dvb_frontend_parameters));
  192. return 0;
  193. }
  194. static int poseidon_get_fe(struct dvb_frontend *fe,
  195. struct dvb_frontend_parameters *fep)
  196. {
  197. struct poseidon *pd = fe->demodulator_priv;
  198. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  199. memcpy(fep, &pd_dvb->fe_param, sizeof(*fep));
  200. return 0;
  201. }
  202. static int poseidon_fe_get_tune_settings(struct dvb_frontend *fe,
  203. struct dvb_frontend_tune_settings *tune)
  204. {
  205. tune->min_delay_ms = 1000;
  206. return 0;
  207. }
  208. static int poseidon_read_status(struct dvb_frontend *fe, fe_status_t *stat)
  209. {
  210. struct poseidon *pd = fe->demodulator_priv;
  211. s32 ret = -1, cmd_status;
  212. struct tuner_dtv_sig_stat_s status = {};
  213. if (in_hibernation(pd))
  214. return -EBUSY;
  215. mutex_lock(&pd->lock);
  216. ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
  217. &status, &cmd_status, sizeof(status));
  218. if (ret | cmd_status) {
  219. log("get tuner status error");
  220. goto out;
  221. }
  222. if (debug_mode)
  223. log("P : %d, L %d, LB :%d", status.sig_present,
  224. status.sig_locked, status.sig_lock_busy);
  225. if (status.sig_lock_busy) {
  226. goto out;
  227. } else if (status.sig_present || status.sig_locked) {
  228. *stat |= FE_HAS_LOCK | FE_HAS_SIGNAL | FE_HAS_CARRIER
  229. | FE_HAS_SYNC | FE_HAS_VITERBI;
  230. } else {
  231. if (fw_delay_overflow(&pd->dvb_data))
  232. *stat |= FE_TIMEDOUT;
  233. }
  234. out:
  235. mutex_unlock(&pd->lock);
  236. return ret;
  237. }
  238. static int poseidon_read_ber(struct dvb_frontend *fe, u32 *ber)
  239. {
  240. struct poseidon *pd = fe->demodulator_priv;
  241. struct tuner_ber_rate_s tlg_ber = {};
  242. s32 ret = -1, cmd_status;
  243. mutex_lock(&pd->lock);
  244. ret = send_get_req(pd, TUNER_BER_RATE, 0,
  245. &tlg_ber, &cmd_status, sizeof(tlg_ber));
  246. if (ret | cmd_status)
  247. goto out;
  248. *ber = tlg_ber.ber_rate;
  249. out:
  250. mutex_unlock(&pd->lock);
  251. return ret;
  252. }
  253. static s32 poseidon_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  254. {
  255. struct poseidon *pd = fe->demodulator_priv;
  256. struct tuner_dtv_sig_stat_s status = {};
  257. s32 ret = 0, cmd_status;
  258. mutex_lock(&pd->lock);
  259. ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
  260. &status, &cmd_status, sizeof(status));
  261. if (ret | cmd_status)
  262. goto out;
  263. if ((status.sig_present || status.sig_locked) && !status.sig_strength)
  264. *strength = 0xFFFF;
  265. else
  266. *strength = status.sig_strength;
  267. out:
  268. mutex_unlock(&pd->lock);
  269. return ret;
  270. }
  271. static int poseidon_read_snr(struct dvb_frontend *fe, u16 *snr)
  272. {
  273. return 0;
  274. }
  275. static int poseidon_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  276. {
  277. *unc = 0;
  278. return 0;
  279. }
  280. static struct dvb_frontend_ops poseidon_frontend_ops = {
  281. .info = {
  282. .name = "Poseidon DVB-T",
  283. .type = FE_OFDM,
  284. .frequency_min = 174000000,
  285. .frequency_max = 862000000,
  286. .frequency_stepsize = 62500,/* FIXME */
  287. .caps = FE_CAN_INVERSION_AUTO |
  288. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  289. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  290. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  291. FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
  292. FE_CAN_GUARD_INTERVAL_AUTO |
  293. FE_CAN_RECOVER |
  294. FE_CAN_HIERARCHY_AUTO,
  295. },
  296. .release = poseidon_fe_release,
  297. .init = poseidon_fe_init,
  298. .sleep = poseidon_fe_sleep,
  299. .set_frontend = poseidon_set_fe,
  300. .get_frontend = poseidon_get_fe,
  301. .get_tune_settings = poseidon_fe_get_tune_settings,
  302. .read_status = poseidon_read_status,
  303. .read_ber = poseidon_read_ber,
  304. .read_signal_strength = poseidon_read_signal_strength,
  305. .read_snr = poseidon_read_snr,
  306. .read_ucblocks = poseidon_read_unc_blocks,
  307. .ts_bus_ctrl = poseidon_ts_bus_ctrl,
  308. };
  309. static void dvb_urb_irq(struct urb *urb)
  310. {
  311. struct pd_dvb_adapter *pd_dvb = urb->context;
  312. int len = urb->transfer_buffer_length;
  313. struct dvb_demux *demux = &pd_dvb->demux;
  314. s32 ret;
  315. if (!pd_dvb->is_streaming || urb->status) {
  316. if (urb->status == -EPROTO)
  317. goto resend;
  318. return;
  319. }
  320. if (urb->actual_length == len)
  321. dvb_dmx_swfilter(demux, urb->transfer_buffer, len);
  322. else if (urb->actual_length == len - 4) {
  323. int offset;
  324. u8 *buf = urb->transfer_buffer;
  325. /*
  326. * The packet size is 512,
  327. * last packet contains 456 bytes tsp data
  328. */
  329. for (offset = 456; offset < len; offset += 512) {
  330. if (!strncmp(buf + offset, "DVHS", 4)) {
  331. dvb_dmx_swfilter(demux, buf, offset);
  332. if (len > offset + 52 + 4) {
  333. /*16 bytes trailer + 36 bytes padding */
  334. buf += offset + 52;
  335. len -= offset + 52 + 4;
  336. dvb_dmx_swfilter(demux, buf, len);
  337. }
  338. break;
  339. }
  340. }
  341. }
  342. resend:
  343. ret = usb_submit_urb(urb, GFP_ATOMIC);
  344. if (ret)
  345. log(" usb_submit_urb failed: error %d", ret);
  346. }
  347. static int dvb_urb_init(struct pd_dvb_adapter *pd_dvb)
  348. {
  349. if (pd_dvb->urb_array[0])
  350. return 0;
  351. alloc_bulk_urbs_generic(pd_dvb->urb_array, DVB_SBUF_NUM,
  352. pd_dvb->pd_device->udev, pd_dvb->ep_addr,
  353. DVB_URB_BUF_SIZE, GFP_KERNEL,
  354. dvb_urb_irq, pd_dvb);
  355. return 0;
  356. }
  357. static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb)
  358. {
  359. free_all_urb_generic(pd_dvb->urb_array, DVB_SBUF_NUM);
  360. }
  361. static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb)
  362. {
  363. struct poseidon *pd = pd_dvb->pd_device;
  364. int ret = 0;
  365. if (pd->state & POSEIDON_STATE_DISCONNECT)
  366. return -ENODEV;
  367. mutex_lock(&pd->lock);
  368. if (!pd_dvb->is_streaming) {
  369. s32 i, cmd_status = 0;
  370. /*
  371. * Once upon a time, there was a difficult bug lying here.
  372. * ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
  373. */
  374. ret = send_set_req(pd, PLAY_SERVICE, 1, &cmd_status);
  375. if (ret | cmd_status)
  376. goto out;
  377. ret = dvb_urb_init(pd_dvb);
  378. if (ret < 0)
  379. goto out;
  380. pd_dvb->is_streaming = 1;
  381. for (i = 0; i < DVB_SBUF_NUM; i++) {
  382. ret = usb_submit_urb(pd_dvb->urb_array[i],
  383. GFP_KERNEL);
  384. if (ret) {
  385. log(" submit urb error %d", ret);
  386. goto out;
  387. }
  388. }
  389. }
  390. out:
  391. mutex_unlock(&pd->lock);
  392. return ret;
  393. }
  394. void dvb_stop_streaming(struct pd_dvb_adapter *pd_dvb)
  395. {
  396. struct poseidon *pd = pd_dvb->pd_device;
  397. mutex_lock(&pd->lock);
  398. if (pd_dvb->is_streaming) {
  399. s32 i, ret, cmd_status = 0;
  400. pd_dvb->is_streaming = 0;
  401. for (i = 0; i < DVB_SBUF_NUM; i++)
  402. if (pd_dvb->urb_array[i])
  403. usb_kill_urb(pd_dvb->urb_array[i]);
  404. ret = send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
  405. &cmd_status);
  406. if (ret | cmd_status)
  407. log("error");
  408. }
  409. mutex_unlock(&pd->lock);
  410. }
  411. static int pd_start_feed(struct dvb_demux_feed *feed)
  412. {
  413. struct pd_dvb_adapter *pd_dvb = feed->demux->priv;
  414. int ret = 0;
  415. if (!pd_dvb)
  416. return -1;
  417. if (atomic_inc_return(&pd_dvb->active_feed) == 1)
  418. ret = dvb_start_streaming(pd_dvb);
  419. return ret;
  420. }
  421. static int pd_stop_feed(struct dvb_demux_feed *feed)
  422. {
  423. struct pd_dvb_adapter *pd_dvb = feed->demux->priv;
  424. if (!pd_dvb)
  425. return -1;
  426. if (atomic_dec_and_test(&pd_dvb->active_feed))
  427. dvb_stop_streaming(pd_dvb);
  428. return 0;
  429. }
  430. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  431. int pd_dvb_usb_device_init(struct poseidon *pd)
  432. {
  433. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  434. struct dvb_demux *dvbdemux;
  435. int ret = 0;
  436. pd_dvb->ep_addr = 0x82;
  437. atomic_set(&pd_dvb->users, 0);
  438. atomic_set(&pd_dvb->active_feed, 0);
  439. pd_dvb->pd_device = pd;
  440. ret = dvb_register_adapter(&pd_dvb->dvb_adap,
  441. "Poseidon dvbt adapter",
  442. THIS_MODULE,
  443. NULL /* for hibernation correctly*/,
  444. adapter_nr);
  445. if (ret < 0)
  446. goto error1;
  447. /* register frontend */
  448. pd_dvb->dvb_fe.demodulator_priv = pd;
  449. memcpy(&pd_dvb->dvb_fe.ops, &poseidon_frontend_ops,
  450. sizeof(struct dvb_frontend_ops));
  451. ret = dvb_register_frontend(&pd_dvb->dvb_adap, &pd_dvb->dvb_fe);
  452. if (ret < 0)
  453. goto error2;
  454. /* register demux device */
  455. dvbdemux = &pd_dvb->demux;
  456. dvbdemux->dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
  457. dvbdemux->priv = pd_dvb;
  458. dvbdemux->feednum = dvbdemux->filternum = 64;
  459. dvbdemux->start_feed = pd_start_feed;
  460. dvbdemux->stop_feed = pd_stop_feed;
  461. dvbdemux->write_to_decoder = NULL;
  462. ret = dvb_dmx_init(dvbdemux);
  463. if (ret < 0)
  464. goto error3;
  465. pd_dvb->dmxdev.filternum = pd_dvb->demux.filternum;
  466. pd_dvb->dmxdev.demux = &pd_dvb->demux.dmx;
  467. pd_dvb->dmxdev.capabilities = 0;
  468. ret = dvb_dmxdev_init(&pd_dvb->dmxdev, &pd_dvb->dvb_adap);
  469. if (ret < 0)
  470. goto error3;
  471. return 0;
  472. error3:
  473. dvb_unregister_frontend(&pd_dvb->dvb_fe);
  474. error2:
  475. dvb_unregister_adapter(&pd_dvb->dvb_adap);
  476. error1:
  477. return ret;
  478. }
  479. void pd_dvb_usb_device_exit(struct poseidon *pd)
  480. {
  481. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  482. while (atomic_read(&pd_dvb->users) != 0
  483. || atomic_read(&pd_dvb->active_feed) != 0) {
  484. set_current_state(TASK_INTERRUPTIBLE);
  485. schedule_timeout(HZ);
  486. }
  487. dvb_dmxdev_release(&pd_dvb->dmxdev);
  488. dvb_unregister_frontend(&pd_dvb->dvb_fe);
  489. dvb_unregister_adapter(&pd_dvb->dvb_adap);
  490. pd_dvb_usb_device_cleanup(pd);
  491. }
  492. void pd_dvb_usb_device_cleanup(struct poseidon *pd)
  493. {
  494. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  495. dvb_urb_cleanup(pd_dvb);
  496. }
  497. int pd_dvb_get_adapter_num(struct pd_dvb_adapter *pd_dvb)
  498. {
  499. return pd_dvb->dvb_adap.num;
  500. }