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

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 423 lines · 354 code · 67 blank · 2 comment · 55 complexity · 35b2dfd1cda24fb86815c71f9e2b72d2 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. static int tlg_fm_vidioc_g_tuner(struct file *file, void *priv,
  141. struct v4l2_tuner *vt)
  142. {
  143. struct tuner_fm_sig_stat_s fm_stat = {};
  144. int ret, status, count = 5;
  145. struct poseidon *p = file->private_data;
  146. if (vt->index != 0)
  147. return -EINVAL;
  148. vt->type = V4L2_TUNER_RADIO;
  149. vt->capability = V4L2_TUNER_CAP_STEREO;
  150. vt->rangelow = TUNER_FREQ_MIN_FM / 62500;
  151. vt->rangehigh = TUNER_FREQ_MAX_FM / 62500;
  152. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  153. vt->audmode = V4L2_TUNER_MODE_STEREO;
  154. vt->signal = 0;
  155. vt->afc = 0;
  156. mutex_lock(&p->lock);
  157. ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
  158. &fm_stat, &status, sizeof(fm_stat));
  159. while (fm_stat.sig_lock_busy && count-- && !ret) {
  160. set_current_state(TASK_INTERRUPTIBLE);
  161. schedule_timeout(HZ);
  162. ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
  163. &fm_stat, &status, sizeof(fm_stat));
  164. }
  165. mutex_unlock(&p->lock);
  166. if (ret || status) {
  167. vt->signal = 0;
  168. } else if ((fm_stat.sig_present || fm_stat.sig_locked)
  169. && fm_stat.sig_strength == 0) {
  170. vt->signal = 0xffff;
  171. } else
  172. vt->signal = (fm_stat.sig_strength * 255 / 10) << 8;
  173. return 0;
  174. }
  175. static int fm_get_freq(struct file *file, void *priv,
  176. struct v4l2_frequency *argp)
  177. {
  178. struct poseidon *p = file->private_data;
  179. argp->frequency = p->radio_data.fm_freq;
  180. return 0;
  181. }
  182. static int set_frequency(struct poseidon *p, __u32 frequency)
  183. {
  184. __u32 freq ;
  185. int ret, status;
  186. mutex_lock(&p->lock);
  187. ret = send_set_req(p, TUNER_AUD_ANA_STD,
  188. p->radio_data.pre_emphasis, &status);
  189. freq = (frequency * 125) * 500 / 1000;/* kHZ */
  190. if (freq < TUNER_FREQ_MIN_FM/1000 || freq > TUNER_FREQ_MAX_FM/1000) {
  191. ret = -EINVAL;
  192. goto error;
  193. }
  194. ret = send_set_req(p, TUNE_FREQ_SELECT, freq, &status);
  195. if (ret < 0)
  196. goto error ;
  197. ret = send_set_req(p, TAKE_REQUEST, 0, &status);
  198. set_current_state(TASK_INTERRUPTIBLE);
  199. schedule_timeout(HZ/4);
  200. if (!p->radio_data.is_radio_streaming) {
  201. ret = send_set_req(p, TAKE_REQUEST, 0, &status);
  202. ret = send_set_req(p, PLAY_SERVICE,
  203. TLG_TUNE_PLAY_SVC_START, &status);
  204. p->radio_data.is_radio_streaming = 1;
  205. }
  206. p->radio_data.fm_freq = frequency;
  207. error:
  208. mutex_unlock(&p->lock);
  209. return ret;
  210. }
  211. static int fm_set_freq(struct file *file, void *priv,
  212. struct v4l2_frequency *argp)
  213. {
  214. struct poseidon *p = file->private_data;
  215. p->file_for_stream = file;
  216. #ifdef CONFIG_PM
  217. p->pm_suspend = pm_fm_suspend;
  218. p->pm_resume = pm_fm_resume;
  219. #endif
  220. return set_frequency(p, argp->frequency);
  221. }
  222. static int tlg_fm_vidioc_g_ctrl(struct file *file, void *priv,
  223. struct v4l2_control *arg)
  224. {
  225. return 0;
  226. }
  227. static int tlg_fm_vidioc_g_exts_ctrl(struct file *file, void *fh,
  228. struct v4l2_ext_controls *ctrls)
  229. {
  230. struct poseidon *p = file->private_data;
  231. int i;
  232. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  233. return -EINVAL;
  234. for (i = 0; i < ctrls->count; i++) {
  235. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  236. if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
  237. continue;
  238. if (i < MAX_PREEMPHASIS)
  239. ctrl->value = p->radio_data.pre_emphasis;
  240. }
  241. return 0;
  242. }
  243. static int tlg_fm_vidioc_s_exts_ctrl(struct file *file, void *fh,
  244. struct v4l2_ext_controls *ctrls)
  245. {
  246. int i;
  247. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  248. return -EINVAL;
  249. for (i = 0; i < ctrls->count; i++) {
  250. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  251. if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
  252. continue;
  253. if (ctrl->value >= 0 && ctrl->value < MAX_PREEMPHASIS) {
  254. struct poseidon *p = file->private_data;
  255. int pre_emphasis = preemphasis[ctrl->value];
  256. u32 status;
  257. send_set_req(p, TUNER_AUD_ANA_STD,
  258. pre_emphasis, &status);
  259. p->radio_data.pre_emphasis = pre_emphasis;
  260. }
  261. }
  262. return 0;
  263. }
  264. static int tlg_fm_vidioc_s_ctrl(struct file *file, void *priv,
  265. struct v4l2_control *ctrl)
  266. {
  267. return 0;
  268. }
  269. static int tlg_fm_vidioc_queryctrl(struct file *file, void *priv,
  270. struct v4l2_queryctrl *ctrl)
  271. {
  272. if (!(ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL))
  273. return -EINVAL;
  274. ctrl->id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
  275. if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS) {
  276. /* return the next supported control */
  277. ctrl->id = V4L2_CID_TUNE_PREEMPHASIS;
  278. v4l2_ctrl_query_fill(ctrl, V4L2_PREEMPHASIS_DISABLED,
  279. V4L2_PREEMPHASIS_75_uS, 1,
  280. V4L2_PREEMPHASIS_50_uS);
  281. ctrl->flags = V4L2_CTRL_FLAG_UPDATE;
  282. return 0;
  283. }
  284. return -EINVAL;
  285. }
  286. static int tlg_fm_vidioc_querymenu(struct file *file, void *fh,
  287. struct v4l2_querymenu *qmenu)
  288. {
  289. return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
  290. }
  291. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
  292. {
  293. return vt->index > 0 ? -EINVAL : 0;
  294. }
  295. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *va)
  296. {
  297. return (va->index != 0) ? -EINVAL : 0;
  298. }
  299. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  300. {
  301. a->index = 0;
  302. a->mode = 0;
  303. a->capability = V4L2_AUDCAP_STEREO;
  304. strcpy(a->name, "Radio");
  305. return 0;
  306. }
  307. static int vidioc_s_input(struct file *filp, void *priv, u32 i)
  308. {
  309. return (i != 0) ? -EINVAL : 0;
  310. }
  311. static int vidioc_g_input(struct file *filp, void *priv, u32 *i)
  312. {
  313. return (*i != 0) ? -EINVAL : 0;
  314. }
  315. static const struct v4l2_ioctl_ops poseidon_fm_ioctl_ops = {
  316. .vidioc_querycap = vidioc_querycap,
  317. .vidioc_g_audio = vidioc_g_audio,
  318. .vidioc_s_audio = vidioc_s_audio,
  319. .vidioc_g_input = vidioc_g_input,
  320. .vidioc_s_input = vidioc_s_input,
  321. .vidioc_queryctrl = tlg_fm_vidioc_queryctrl,
  322. .vidioc_querymenu = tlg_fm_vidioc_querymenu,
  323. .vidioc_g_ctrl = tlg_fm_vidioc_g_ctrl,
  324. .vidioc_s_ctrl = tlg_fm_vidioc_s_ctrl,
  325. .vidioc_s_ext_ctrls = tlg_fm_vidioc_s_exts_ctrl,
  326. .vidioc_g_ext_ctrls = tlg_fm_vidioc_g_exts_ctrl,
  327. .vidioc_s_tuner = vidioc_s_tuner,
  328. .vidioc_g_tuner = tlg_fm_vidioc_g_tuner,
  329. .vidioc_g_frequency = fm_get_freq,
  330. .vidioc_s_frequency = fm_set_freq,
  331. };
  332. static struct video_device poseidon_fm_template = {
  333. .name = "Telegent-Radio",
  334. .fops = &poseidon_fm_fops,
  335. .minor = -1,
  336. .release = video_device_release,
  337. .ioctl_ops = &poseidon_fm_ioctl_ops,
  338. };
  339. int poseidon_fm_init(struct poseidon *p)
  340. {
  341. struct video_device *fm_dev;
  342. fm_dev = vdev_init(p, &poseidon_fm_template);
  343. if (fm_dev == NULL)
  344. return -1;
  345. if (video_register_device(fm_dev, VFL_TYPE_RADIO, -1) < 0) {
  346. video_device_release(fm_dev);
  347. return -1;
  348. }
  349. p->radio_data.fm_dev = fm_dev;
  350. return 0;
  351. }
  352. int poseidon_fm_exit(struct poseidon *p)
  353. {
  354. destroy_video_device(&p->radio_data.fm_dev);
  355. return 0;
  356. }