pd-dvb.c 14 KB

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