pd-radio.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include <linux/init.h>
  2. #include <linux/list.h>
  3. #include <linux/module.h>
  4. #include <linux/kernel.h>
  5. #include <linux/bitmap.h>
  6. #include <linux/usb.h>
  7. #include <linux/i2c.h>
  8. #include <media/v4l2-dev.h>
  9. #include <linux/mm.h>
  10. #include <linux/mutex.h>
  11. #include <media/v4l2-ioctl.h>
  12. #include <media/v4l2-event.h>
  13. #include <media/v4l2-fh.h>
  14. #include <linux/sched.h>
  15. #include "pd-common.h"
  16. #include "vendorcmds.h"
  17. static int set_frequency(struct poseidon *p, __u32 frequency);
  18. static int poseidon_fm_close(struct file *filp);
  19. static int poseidon_fm_open(struct file *filp);
  20. #define TUNER_FREQ_MIN_FM 76000000U
  21. #define TUNER_FREQ_MAX_FM 108000000U
  22. #define MAX_PREEMPHASIS (V4L2_PREEMPHASIS_75_uS + 1)
  23. static int preemphasis[MAX_PREEMPHASIS] = {
  24. TLG_TUNE_ASTD_NONE, /* V4L2_PREEMPHASIS_DISABLED */
  25. TLG_TUNE_ASTD_FM_EUR, /* V4L2_PREEMPHASIS_50_uS */
  26. TLG_TUNE_ASTD_FM_US, /* V4L2_PREEMPHASIS_75_uS */
  27. };
  28. static int poseidon_check_mode_radio(struct poseidon *p)
  29. {
  30. int ret;
  31. u32 status;
  32. set_current_state(TASK_INTERRUPTIBLE);
  33. schedule_timeout(HZ/2);
  34. ret = usb_set_interface(p->udev, 0, BULK_ALTERNATE_IFACE);
  35. if (ret < 0)
  36. goto out;
  37. ret = set_tuner_mode(p, TLG_MODE_FM_RADIO);
  38. if (ret != 0)
  39. goto out;
  40. ret = send_set_req(p, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &status);
  41. ret = send_set_req(p, TUNER_AUD_ANA_STD,
  42. p->radio_data.pre_emphasis, &status);
  43. ret |= send_set_req(p, TUNER_AUD_MODE,
  44. TLG_TUNE_TVAUDIO_MODE_STEREO, &status);
  45. ret |= send_set_req(p, AUDIO_SAMPLE_RATE_SEL,
  46. ATV_AUDIO_RATE_48K, &status);
  47. ret |= send_set_req(p, TUNE_FREQ_SELECT, TUNER_FREQ_MIN_FM, &status);
  48. out:
  49. return ret;
  50. }
  51. #ifdef CONFIG_PM
  52. static int pm_fm_suspend(struct poseidon *p)
  53. {
  54. logpm(p);
  55. pm_alsa_suspend(p);
  56. usb_set_interface(p->udev, 0, 0);
  57. msleep(300);
  58. return 0;
  59. }
  60. static int pm_fm_resume(struct poseidon *p)
  61. {
  62. logpm(p);
  63. poseidon_check_mode_radio(p);
  64. set_frequency(p, p->radio_data.fm_freq);
  65. pm_alsa_resume(p);
  66. return 0;
  67. }
  68. #endif
  69. static int poseidon_fm_open(struct file *filp)
  70. {
  71. struct poseidon *p = video_drvdata(filp);
  72. int ret = 0;
  73. mutex_lock(&p->lock);
  74. if (p->state & POSEIDON_STATE_DISCONNECT) {
  75. ret = -ENODEV;
  76. goto out;
  77. }
  78. if (p->state && !(p->state & POSEIDON_STATE_FM)) {
  79. ret = -EBUSY;
  80. goto out;
  81. }
  82. ret = v4l2_fh_open(filp);
  83. if (ret)
  84. goto out;
  85. usb_autopm_get_interface(p->interface);
  86. if (0 == p->state) {
  87. struct video_device *vfd = &p->radio_data.fm_dev;
  88. /* default pre-emphasis */
  89. if (p->radio_data.pre_emphasis == 0)
  90. p->radio_data.pre_emphasis = TLG_TUNE_ASTD_FM_EUR;
  91. set_debug_mode(vfd, debug_mode);
  92. ret = poseidon_check_mode_radio(p);
  93. if (ret < 0) {
  94. usb_autopm_put_interface(p->interface);
  95. goto out;
  96. }
  97. p->state |= POSEIDON_STATE_FM;
  98. }
  99. kref_get(&p->kref);
  100. out:
  101. mutex_unlock(&p->lock);
  102. return ret;
  103. }
  104. static int poseidon_fm_close(struct file *filp)
  105. {
  106. struct poseidon *p = video_drvdata(filp);
  107. struct radio_data *fm = &p->radio_data;
  108. uint32_t status;
  109. mutex_lock(&p->lock);
  110. if (v4l2_fh_is_singular_file(filp))
  111. p->state &= ~POSEIDON_STATE_FM;
  112. if (fm->is_radio_streaming && filp == p->file_for_stream) {
  113. fm->is_radio_streaming = 0;
  114. send_set_req(p, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP, &status);
  115. }
  116. usb_autopm_put_interface(p->interface);
  117. mutex_unlock(&p->lock);
  118. kref_put(&p->kref, poseidon_delete);
  119. return v4l2_fh_release(filp);
  120. }
  121. static int vidioc_querycap(struct file *file, void *priv,
  122. struct v4l2_capability *v)
  123. {
  124. struct poseidon *p = video_drvdata(file);
  125. strlcpy(v->driver, "tele-radio", sizeof(v->driver));
  126. strlcpy(v->card, "Telegent Poseidon", sizeof(v->card));
  127. usb_make_path(p->udev, v->bus_info, sizeof(v->bus_info));
  128. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  129. /* Report all capabilities of the USB device */
  130. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS |
  131. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
  132. V4L2_CAP_AUDIO | V4L2_CAP_STREAMING |
  133. V4L2_CAP_READWRITE;
  134. return 0;
  135. }
  136. static const struct v4l2_file_operations poseidon_fm_fops = {
  137. .owner = THIS_MODULE,
  138. .open = poseidon_fm_open,
  139. .release = poseidon_fm_close,
  140. .poll = v4l2_ctrl_poll,
  141. .unlocked_ioctl = video_ioctl2,
  142. };
  143. static int tlg_fm_vidioc_g_tuner(struct file *file, void *priv,
  144. struct v4l2_tuner *vt)
  145. {
  146. struct poseidon *p = video_drvdata(file);
  147. struct tuner_fm_sig_stat_s fm_stat = {};
  148. int ret, status, count = 5;
  149. if (vt->index != 0)
  150. return -EINVAL;
  151. vt->type = V4L2_TUNER_RADIO;
  152. vt->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
  153. vt->rangelow = TUNER_FREQ_MIN_FM * 2 / 125;
  154. vt->rangehigh = TUNER_FREQ_MAX_FM * 2 / 125;
  155. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  156. vt->audmode = V4L2_TUNER_MODE_STEREO;
  157. vt->signal = 0;
  158. vt->afc = 0;
  159. strlcpy(vt->name, "Radio", sizeof(vt->name));
  160. mutex_lock(&p->lock);
  161. ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
  162. &fm_stat, &status, sizeof(fm_stat));
  163. while (fm_stat.sig_lock_busy && count-- && !ret) {
  164. set_current_state(TASK_INTERRUPTIBLE);
  165. schedule_timeout(HZ);
  166. ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
  167. &fm_stat, &status, sizeof(fm_stat));
  168. }
  169. mutex_unlock(&p->lock);
  170. if (ret || status) {
  171. vt->signal = 0;
  172. } else if ((fm_stat.sig_present || fm_stat.sig_locked)
  173. && fm_stat.sig_strength == 0) {
  174. vt->signal = 0xffff;
  175. } else
  176. vt->signal = (fm_stat.sig_strength * 255 / 10) << 8;
  177. return 0;
  178. }
  179. static int fm_get_freq(struct file *file, void *priv,
  180. struct v4l2_frequency *argp)
  181. {
  182. struct poseidon *p = video_drvdata(file);
  183. if (argp->tuner)
  184. return -EINVAL;
  185. argp->frequency = p->radio_data.fm_freq;
  186. return 0;
  187. }
  188. static int set_frequency(struct poseidon *p, __u32 frequency)
  189. {
  190. __u32 freq ;
  191. int ret, status;
  192. mutex_lock(&p->lock);
  193. ret = send_set_req(p, TUNER_AUD_ANA_STD,
  194. p->radio_data.pre_emphasis, &status);
  195. freq = (frequency * 125) / 2; /* Hz */
  196. freq = clamp(freq, TUNER_FREQ_MIN_FM, TUNER_FREQ_MAX_FM);
  197. ret = send_set_req(p, TUNE_FREQ_SELECT, freq, &status);
  198. if (ret < 0)
  199. goto error ;
  200. ret = send_set_req(p, TAKE_REQUEST, 0, &status);
  201. set_current_state(TASK_INTERRUPTIBLE);
  202. schedule_timeout(HZ/4);
  203. if (!p->radio_data.is_radio_streaming) {
  204. ret = send_set_req(p, TAKE_REQUEST, 0, &status);
  205. ret = send_set_req(p, PLAY_SERVICE,
  206. TLG_TUNE_PLAY_SVC_START, &status);
  207. p->radio_data.is_radio_streaming = 1;
  208. }
  209. p->radio_data.fm_freq = freq * 2 / 125;
  210. error:
  211. mutex_unlock(&p->lock);
  212. return ret;
  213. }
  214. static int fm_set_freq(struct file *file, void *priv,
  215. const struct v4l2_frequency *argp)
  216. {
  217. struct poseidon *p = video_drvdata(file);
  218. if (argp->tuner)
  219. return -EINVAL;
  220. p->file_for_stream = file;
  221. #ifdef CONFIG_PM
  222. p->pm_suspend = pm_fm_suspend;
  223. p->pm_resume = pm_fm_resume;
  224. #endif
  225. return set_frequency(p, argp->frequency);
  226. }
  227. static int tlg_fm_s_ctrl(struct v4l2_ctrl *ctrl)
  228. {
  229. struct poseidon *p = container_of(ctrl->handler, struct poseidon,
  230. radio_data.ctrl_handler);
  231. int pre_emphasis;
  232. u32 status;
  233. switch (ctrl->id) {
  234. case V4L2_CID_TUNE_PREEMPHASIS:
  235. pre_emphasis = preemphasis[ctrl->val];
  236. send_set_req(p, TUNER_AUD_ANA_STD, pre_emphasis, &status);
  237. p->radio_data.pre_emphasis = pre_emphasis;
  238. return 0;
  239. }
  240. return -EINVAL;
  241. }
  242. static int vidioc_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *vt)
  243. {
  244. return vt->index > 0 ? -EINVAL : 0;
  245. }
  246. static const struct v4l2_ctrl_ops tlg_fm_ctrl_ops = {
  247. .s_ctrl = tlg_fm_s_ctrl,
  248. };
  249. static const struct v4l2_ioctl_ops poseidon_fm_ioctl_ops = {
  250. .vidioc_querycap = vidioc_querycap,
  251. .vidioc_s_tuner = vidioc_s_tuner,
  252. .vidioc_g_tuner = tlg_fm_vidioc_g_tuner,
  253. .vidioc_g_frequency = fm_get_freq,
  254. .vidioc_s_frequency = fm_set_freq,
  255. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  256. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  257. };
  258. static struct video_device poseidon_fm_template = {
  259. .name = "Telegent-Radio",
  260. .fops = &poseidon_fm_fops,
  261. .minor = -1,
  262. .release = video_device_release_empty,
  263. .ioctl_ops = &poseidon_fm_ioctl_ops,
  264. };
  265. int poseidon_fm_init(struct poseidon *p)
  266. {
  267. struct video_device *vfd = &p->radio_data.fm_dev;
  268. struct v4l2_ctrl_handler *hdl = &p->radio_data.ctrl_handler;
  269. *vfd = poseidon_fm_template;
  270. set_frequency(p, TUNER_FREQ_MIN_FM);
  271. v4l2_ctrl_handler_init(hdl, 1);
  272. v4l2_ctrl_new_std_menu(hdl, &tlg_fm_ctrl_ops, V4L2_CID_TUNE_PREEMPHASIS,
  273. V4L2_PREEMPHASIS_75_uS, 0, V4L2_PREEMPHASIS_50_uS);
  274. if (hdl->error) {
  275. v4l2_ctrl_handler_free(hdl);
  276. return hdl->error;
  277. }
  278. vfd->v4l2_dev = &p->v4l2_dev;
  279. vfd->ctrl_handler = hdl;
  280. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  281. video_set_drvdata(vfd, p);
  282. return video_register_device(vfd, VFL_TYPE_RADIO, -1);
  283. }
  284. int poseidon_fm_exit(struct poseidon *p)
  285. {
  286. video_unregister_device(&p->radio_data.fm_dev);
  287. v4l2_ctrl_handler_free(&p->radio_data.ctrl_handler);
  288. return 0;
  289. }