/drivers/media/video/tlg2300/pd-radio.c

https://bitbucket.org/abioy/linux · C · 420 lines · 351 code · 67 blank · 2 comment · 55 complexity · 2e2717136b716c90e188a6031e7bc861 MD5 · raw file

  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/version.h>
  10. #include <linux/mm.h>
  11. #include <linux/mutex.h>
  12. #include <media/v4l2-ioctl.h>
  13. #include <linux/sched.h>
  14. #include "pd-common.h"
  15. #include "vendorcmds.h"
  16. static int set_frequency(struct poseidon *p, __u32 frequency);
  17. static int poseidon_fm_close(struct file *filp);
  18. static int poseidon_fm_open(struct file *filp);
  19. #define TUNER_FREQ_MIN_FM 76000000
  20. #define TUNER_FREQ_MAX_FM 108000000
  21. #define MAX_PREEMPHASIS (V4L2_PREEMPHASIS_75_uS + 1)
  22. static int preemphasis[MAX_PREEMPHASIS] = {
  23. TLG_TUNE_ASTD_NONE, /* V4L2_PREEMPHASIS_DISABLED */
  24. TLG_TUNE_ASTD_FM_EUR, /* V4L2_PREEMPHASIS_50_uS */
  25. TLG_TUNE_ASTD_FM_US, /* V4L2_PREEMPHASIS_75_uS */
  26. };
  27. static int poseidon_check_mode_radio(struct poseidon *p)
  28. {
  29. int ret;
  30. u32 status;
  31. set_current_state(TASK_INTERRUPTIBLE);
  32. schedule_timeout(HZ/2);
  33. ret = usb_set_interface(p->udev, 0, BULK_ALTERNATE_IFACE);
  34. if (ret < 0)
  35. goto out;
  36. ret = set_tuner_mode(p, TLG_MODE_FM_RADIO);
  37. if (ret != 0)
  38. goto out;
  39. ret = send_set_req(p, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &status);
  40. ret = send_set_req(p, TUNER_AUD_ANA_STD,
  41. p->radio_data.pre_emphasis, &status);
  42. ret |= send_set_req(p, TUNER_AUD_MODE,
  43. TLG_TUNE_TVAUDIO_MODE_STEREO, &status);
  44. ret |= send_set_req(p, AUDIO_SAMPLE_RATE_SEL,
  45. ATV_AUDIO_RATE_48K, &status);
  46. ret |= send_set_req(p, TUNE_FREQ_SELECT, TUNER_FREQ_MIN_FM, &status);
  47. out:
  48. return ret;
  49. }
  50. #ifdef CONFIG_PM
  51. static int pm_fm_suspend(struct poseidon *p)
  52. {
  53. logpm(p);
  54. pm_alsa_suspend(p);
  55. usb_set_interface(p->udev, 0, 0);
  56. msleep(300);
  57. return 0;
  58. }
  59. static int pm_fm_resume(struct poseidon *p)
  60. {
  61. logpm(p);
  62. poseidon_check_mode_radio(p);
  63. set_frequency(p, p->radio_data.fm_freq);
  64. pm_alsa_resume(p);
  65. return 0;
  66. }
  67. #endif
  68. static int poseidon_fm_open(struct file *filp)
  69. {
  70. struct video_device *vfd = video_devdata(filp);
  71. struct poseidon *p = video_get_drvdata(vfd);
  72. int ret = 0;
  73. if (!p)
  74. return -1;
  75. mutex_lock(&p->lock);
  76. if (p->state & POSEIDON_STATE_DISCONNECT) {
  77. ret = -ENODEV;
  78. goto out;
  79. }
  80. if (p->state && !(p->state & POSEIDON_STATE_FM)) {
  81. ret = -EBUSY;
  82. goto out;
  83. }
  84. usb_autopm_get_interface(p->interface);
  85. if (0 == p->state) {
  86. /* default pre-emphasis */
  87. if (p->radio_data.pre_emphasis == 0)
  88. p->radio_data.pre_emphasis = TLG_TUNE_ASTD_FM_EUR;
  89. set_debug_mode(vfd, debug_mode);
  90. ret = poseidon_check_mode_radio(p);
  91. if (ret < 0) {
  92. usb_autopm_put_interface(p->interface);
  93. goto out;
  94. }
  95. p->state |= POSEIDON_STATE_FM;
  96. }
  97. p->radio_data.users++;
  98. kref_get(&p->kref);
  99. filp->private_data = p;
  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 = filp->private_data;
  107. struct radio_data *fm = &p->radio_data;
  108. uint32_t status;
  109. mutex_lock(&p->lock);
  110. fm->users--;
  111. if (0 == fm->users)
  112. p->state &= ~POSEIDON_STATE_FM;
  113. if (fm->is_radio_streaming && filp == p->file_for_stream) {
  114. fm->is_radio_streaming = 0;
  115. send_set_req(p, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP, &status);
  116. }
  117. usb_autopm_put_interface(p->interface);
  118. mutex_unlock(&p->lock);
  119. kref_put(&p->kref, poseidon_delete);
  120. filp->private_data = NULL;
  121. return 0;
  122. }
  123. static int vidioc_querycap(struct file *file, void *priv,
  124. struct v4l2_capability *v)
  125. {
  126. struct poseidon *p = file->private_data;
  127. strlcpy(v->driver, "tele-radio", sizeof(v->driver));
  128. strlcpy(v->card, "Telegent Poseidon", sizeof(v->card));
  129. usb_make_path(p->udev, v->bus_info, sizeof(v->bus_info));
  130. v->version = KERNEL_VERSION(0, 0, 1);
  131. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  132. return 0;
  133. }
  134. static const struct v4l2_file_operations poseidon_fm_fops = {
  135. .owner = THIS_MODULE,
  136. .open = poseidon_fm_open,
  137. .release = poseidon_fm_close,
  138. .ioctl = video_ioctl2,
  139. };
  140. int tlg_fm_vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
  141. {
  142. struct tuner_fm_sig_stat_s fm_stat = {};
  143. int ret, status, count = 5;
  144. struct poseidon *p = file->private_data;
  145. if (vt->index != 0)
  146. return -EINVAL;
  147. vt->type = V4L2_TUNER_RADIO;
  148. vt->capability = V4L2_TUNER_CAP_STEREO;
  149. vt->rangelow = TUNER_FREQ_MIN_FM / 62500;
  150. vt->rangehigh = TUNER_FREQ_MAX_FM / 62500;
  151. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  152. vt->audmode = V4L2_TUNER_MODE_STEREO;
  153. vt->signal = 0;
  154. vt->afc = 0;
  155. mutex_lock(&p->lock);
  156. ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
  157. &fm_stat, &status, sizeof(fm_stat));
  158. while (fm_stat.sig_lock_busy && count-- && !ret) {
  159. set_current_state(TASK_INTERRUPTIBLE);
  160. schedule_timeout(HZ);
  161. ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
  162. &fm_stat, &status, sizeof(fm_stat));
  163. }
  164. mutex_unlock(&p->lock);
  165. if (ret || status) {
  166. vt->signal = 0;
  167. } else if ((fm_stat.sig_present || fm_stat.sig_locked)
  168. && fm_stat.sig_strength == 0) {
  169. vt->signal = 0xffff;
  170. } else
  171. vt->signal = (fm_stat.sig_strength * 255 / 10) << 8;
  172. return 0;
  173. }
  174. int fm_get_freq(struct file *file, void *priv, struct v4l2_frequency *argp)
  175. {
  176. struct poseidon *p = file->private_data;
  177. argp->frequency = p->radio_data.fm_freq;
  178. return 0;
  179. }
  180. static int set_frequency(struct poseidon *p, __u32 frequency)
  181. {
  182. __u32 freq ;
  183. int ret, status;
  184. mutex_lock(&p->lock);
  185. ret = send_set_req(p, TUNER_AUD_ANA_STD,
  186. p->radio_data.pre_emphasis, &status);
  187. freq = (frequency * 125) * 500 / 1000;/* kHZ */
  188. if (freq < TUNER_FREQ_MIN_FM/1000 || freq > TUNER_FREQ_MAX_FM/1000) {
  189. ret = -EINVAL;
  190. goto error;
  191. }
  192. ret = send_set_req(p, TUNE_FREQ_SELECT, freq, &status);
  193. if (ret < 0)
  194. goto error ;
  195. ret = send_set_req(p, TAKE_REQUEST, 0, &status);
  196. set_current_state(TASK_INTERRUPTIBLE);
  197. schedule_timeout(HZ/4);
  198. if (!p->radio_data.is_radio_streaming) {
  199. ret = send_set_req(p, TAKE_REQUEST, 0, &status);
  200. ret = send_set_req(p, PLAY_SERVICE,
  201. TLG_TUNE_PLAY_SVC_START, &status);
  202. p->radio_data.is_radio_streaming = 1;
  203. }
  204. p->radio_data.fm_freq = frequency;
  205. error:
  206. mutex_unlock(&p->lock);
  207. return ret;
  208. }
  209. int fm_set_freq(struct file *file, void *priv, struct v4l2_frequency *argp)
  210. {
  211. struct poseidon *p = file->private_data;
  212. p->file_for_stream = file;
  213. #ifdef CONFIG_PM
  214. p->pm_suspend = pm_fm_suspend;
  215. p->pm_resume = pm_fm_resume;
  216. #endif
  217. return set_frequency(p, argp->frequency);
  218. }
  219. int tlg_fm_vidioc_g_ctrl(struct file *file, void *priv,
  220. struct v4l2_control *arg)
  221. {
  222. return 0;
  223. }
  224. int tlg_fm_vidioc_g_exts_ctrl(struct file *file, void *fh,
  225. struct v4l2_ext_controls *ctrls)
  226. {
  227. struct poseidon *p = file->private_data;
  228. int i;
  229. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  230. return -EINVAL;
  231. for (i = 0; i < ctrls->count; i++) {
  232. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  233. if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
  234. continue;
  235. if (i < MAX_PREEMPHASIS)
  236. ctrl->value = p->radio_data.pre_emphasis;
  237. }
  238. return 0;
  239. }
  240. int tlg_fm_vidioc_s_exts_ctrl(struct file *file, void *fh,
  241. struct v4l2_ext_controls *ctrls)
  242. {
  243. int i;
  244. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  245. return -EINVAL;
  246. for (i = 0; i < ctrls->count; i++) {
  247. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  248. if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
  249. continue;
  250. if (ctrl->value >= 0 && ctrl->value < MAX_PREEMPHASIS) {
  251. struct poseidon *p = file->private_data;
  252. int pre_emphasis = preemphasis[ctrl->value];
  253. u32 status;
  254. send_set_req(p, TUNER_AUD_ANA_STD,
  255. pre_emphasis, &status);
  256. p->radio_data.pre_emphasis = pre_emphasis;
  257. }
  258. }
  259. return 0;
  260. }
  261. int tlg_fm_vidioc_s_ctrl(struct file *file, void *priv,
  262. struct v4l2_control *ctrl)
  263. {
  264. return 0;
  265. }
  266. int tlg_fm_vidioc_queryctrl(struct file *file, void *priv,
  267. struct v4l2_queryctrl *ctrl)
  268. {
  269. if (!(ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL))
  270. return -EINVAL;
  271. ctrl->id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
  272. if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS) {
  273. /* return the next supported control */
  274. ctrl->id = V4L2_CID_TUNE_PREEMPHASIS;
  275. v4l2_ctrl_query_fill(ctrl, V4L2_PREEMPHASIS_DISABLED,
  276. V4L2_PREEMPHASIS_75_uS, 1,
  277. V4L2_PREEMPHASIS_50_uS);
  278. ctrl->flags = V4L2_CTRL_FLAG_UPDATE;
  279. return 0;
  280. }
  281. return -EINVAL;
  282. }
  283. int tlg_fm_vidioc_querymenu(struct file *file, void *fh,
  284. struct v4l2_querymenu *qmenu)
  285. {
  286. return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
  287. }
  288. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
  289. {
  290. return vt->index > 0 ? -EINVAL : 0;
  291. }
  292. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *va)
  293. {
  294. return (va->index != 0) ? -EINVAL : 0;
  295. }
  296. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  297. {
  298. a->index = 0;
  299. a->mode = 0;
  300. a->capability = V4L2_AUDCAP_STEREO;
  301. strcpy(a->name, "Radio");
  302. return 0;
  303. }
  304. static int vidioc_s_input(struct file *filp, void *priv, u32 i)
  305. {
  306. return (i != 0) ? -EINVAL : 0;
  307. }
  308. static int vidioc_g_input(struct file *filp, void *priv, u32 *i)
  309. {
  310. return (*i != 0) ? -EINVAL : 0;
  311. }
  312. static const struct v4l2_ioctl_ops poseidon_fm_ioctl_ops = {
  313. .vidioc_querycap = vidioc_querycap,
  314. .vidioc_g_audio = vidioc_g_audio,
  315. .vidioc_s_audio = vidioc_s_audio,
  316. .vidioc_g_input = vidioc_g_input,
  317. .vidioc_s_input = vidioc_s_input,
  318. .vidioc_queryctrl = tlg_fm_vidioc_queryctrl,
  319. .vidioc_querymenu = tlg_fm_vidioc_querymenu,
  320. .vidioc_g_ctrl = tlg_fm_vidioc_g_ctrl,
  321. .vidioc_s_ctrl = tlg_fm_vidioc_s_ctrl,
  322. .vidioc_s_ext_ctrls = tlg_fm_vidioc_s_exts_ctrl,
  323. .vidioc_g_ext_ctrls = tlg_fm_vidioc_g_exts_ctrl,
  324. .vidioc_s_tuner = vidioc_s_tuner,
  325. .vidioc_g_tuner = tlg_fm_vidioc_g_tuner,
  326. .vidioc_g_frequency = fm_get_freq,
  327. .vidioc_s_frequency = fm_set_freq,
  328. };
  329. static struct video_device poseidon_fm_template = {
  330. .name = "Telegent-Radio",
  331. .fops = &poseidon_fm_fops,
  332. .minor = -1,
  333. .release = video_device_release,
  334. .ioctl_ops = &poseidon_fm_ioctl_ops,
  335. };
  336. int poseidon_fm_init(struct poseidon *p)
  337. {
  338. struct video_device *fm_dev;
  339. fm_dev = vdev_init(p, &poseidon_fm_template);
  340. if (fm_dev == NULL)
  341. return -1;
  342. if (video_register_device(fm_dev, VFL_TYPE_RADIO, -1) < 0) {
  343. video_device_release(fm_dev);
  344. return -1;
  345. }
  346. p->radio_data.fm_dev = fm_dev;
  347. return 0;
  348. }
  349. int poseidon_fm_exit(struct poseidon *p)
  350. {
  351. destroy_video_device(&p->radio_data.fm_dev);
  352. return 0;
  353. }