pd-radio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 <linux/sched.h>
  13. #include "pd-common.h"
  14. #include "vendorcmds.h"
  15. static int set_frequency(struct poseidon *p, __u32 frequency);
  16. static int poseidon_fm_close(struct file *filp);
  17. static int poseidon_fm_open(struct file *filp);
  18. #define TUNER_FREQ_MIN_FM 76000000U
  19. #define TUNER_FREQ_MAX_FM 108000000U
  20. #define MAX_PREEMPHASIS (V4L2_PREEMPHASIS_75_uS + 1)
  21. static int preemphasis[MAX_PREEMPHASIS] = {
  22. TLG_TUNE_ASTD_NONE, /* V4L2_PREEMPHASIS_DISABLED */
  23. TLG_TUNE_ASTD_FM_EUR, /* V4L2_PREEMPHASIS_50_uS */
  24. TLG_TUNE_ASTD_FM_US, /* V4L2_PREEMPHASIS_75_uS */
  25. };
  26. static int poseidon_check_mode_radio(struct poseidon *p)
  27. {
  28. int ret;
  29. u32 status;
  30. set_current_state(TASK_INTERRUPTIBLE);
  31. schedule_timeout(HZ/2);
  32. ret = usb_set_interface(p->udev, 0, BULK_ALTERNATE_IFACE);
  33. if (ret < 0)
  34. goto out;
  35. ret = set_tuner_mode(p, TLG_MODE_FM_RADIO);
  36. if (ret != 0)
  37. goto out;
  38. ret = send_set_req(p, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &status);
  39. ret = send_set_req(p, TUNER_AUD_ANA_STD,
  40. p->radio_data.pre_emphasis, &status);
  41. ret |= send_set_req(p, TUNER_AUD_MODE,
  42. TLG_TUNE_TVAUDIO_MODE_STEREO, &status);
  43. ret |= send_set_req(p, AUDIO_SAMPLE_RATE_SEL,
  44. ATV_AUDIO_RATE_48K, &status);
  45. ret |= send_set_req(p, TUNE_FREQ_SELECT, TUNER_FREQ_MIN_FM, &status);
  46. out:
  47. return ret;
  48. }
  49. #ifdef CONFIG_PM
  50. static int pm_fm_suspend(struct poseidon *p)
  51. {
  52. logpm(p);
  53. pm_alsa_suspend(p);
  54. usb_set_interface(p->udev, 0, 0);
  55. msleep(300);
  56. return 0;
  57. }
  58. static int pm_fm_resume(struct poseidon *p)
  59. {
  60. logpm(p);
  61. poseidon_check_mode_radio(p);
  62. set_frequency(p, p->radio_data.fm_freq);
  63. pm_alsa_resume(p);
  64. return 0;
  65. }
  66. #endif
  67. static int poseidon_fm_open(struct file *filp)
  68. {
  69. struct video_device *vfd = video_devdata(filp);
  70. struct poseidon *p = video_get_drvdata(vfd);
  71. int ret = 0;
  72. if (!p)
  73. return -1;
  74. mutex_lock(&p->lock);
  75. if (p->state & POSEIDON_STATE_DISCONNECT) {
  76. ret = -ENODEV;
  77. goto out;
  78. }
  79. if (p->state && !(p->state & POSEIDON_STATE_FM)) {
  80. ret = -EBUSY;
  81. goto out;
  82. }
  83. usb_autopm_get_interface(p->interface);
  84. if (0 == p->state) {
  85. /* default pre-emphasis */
  86. if (p->radio_data.pre_emphasis == 0)
  87. p->radio_data.pre_emphasis = TLG_TUNE_ASTD_FM_EUR;
  88. set_debug_mode(vfd, debug_mode);
  89. ret = poseidon_check_mode_radio(p);
  90. if (ret < 0) {
  91. usb_autopm_put_interface(p->interface);
  92. goto out;
  93. }
  94. p->state |= POSEIDON_STATE_FM;
  95. }
  96. p->radio_data.users++;
  97. kref_get(&p->kref);
  98. filp->private_data = p;
  99. out:
  100. mutex_unlock(&p->lock);
  101. return ret;
  102. }
  103. static int poseidon_fm_close(struct file *filp)
  104. {
  105. struct poseidon *p = filp->private_data;
  106. struct radio_data *fm = &p->radio_data;
  107. uint32_t status;
  108. mutex_lock(&p->lock);
  109. fm->users--;
  110. if (0 == fm->users)
  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. filp->private_data = NULL;
  120. return 0;
  121. }
  122. static int vidioc_querycap(struct file *file, void *priv,
  123. struct v4l2_capability *v)
  124. {
  125. struct poseidon *p = file->private_data;
  126. strlcpy(v->driver, "tele-radio", sizeof(v->driver));
  127. strlcpy(v->card, "Telegent Poseidon", sizeof(v->card));
  128. usb_make_path(p->udev, v->bus_info, sizeof(v->bus_info));
  129. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  130. return 0;
  131. }
  132. static const struct v4l2_file_operations poseidon_fm_fops = {
  133. .owner = THIS_MODULE,
  134. .open = poseidon_fm_open,
  135. .release = poseidon_fm_close,
  136. .unlocked_ioctl = video_ioctl2,
  137. };
  138. static int tlg_fm_vidioc_g_tuner(struct file *file, void *priv,
  139. struct v4l2_tuner *vt)
  140. {
  141. struct tuner_fm_sig_stat_s fm_stat = {};
  142. int ret, status, count = 5;
  143. struct poseidon *p = file->private_data;
  144. if (vt->index != 0)
  145. return -EINVAL;
  146. vt->type = V4L2_TUNER_RADIO;
  147. vt->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
  148. vt->rangelow = TUNER_FREQ_MIN_FM * 2 / 125;
  149. vt->rangehigh = TUNER_FREQ_MAX_FM * 2 / 125;
  150. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  151. vt->audmode = V4L2_TUNER_MODE_STEREO;
  152. vt->signal = 0;
  153. vt->afc = 0;
  154. strlcpy(vt->name, "Radio", sizeof(vt->name));
  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. static int fm_get_freq(struct file *file, void *priv,
  175. struct v4l2_frequency *argp)
  176. {
  177. struct poseidon *p = file->private_data;
  178. if (argp->tuner)
  179. return -EINVAL;
  180. argp->frequency = p->radio_data.fm_freq;
  181. return 0;
  182. }
  183. static int set_frequency(struct poseidon *p, __u32 frequency)
  184. {
  185. __u32 freq ;
  186. int ret, status;
  187. mutex_lock(&p->lock);
  188. ret = send_set_req(p, TUNER_AUD_ANA_STD,
  189. p->radio_data.pre_emphasis, &status);
  190. freq = (frequency * 125) / 2; /* Hz */
  191. freq = clamp(freq, TUNER_FREQ_MIN_FM, TUNER_FREQ_MAX_FM);
  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 = freq * 2 / 125;
  205. error:
  206. mutex_unlock(&p->lock);
  207. return ret;
  208. }
  209. static int fm_set_freq(struct file *file, void *priv,
  210. struct v4l2_frequency *argp)
  211. {
  212. struct poseidon *p = file->private_data;
  213. if (argp->tuner)
  214. return -EINVAL;
  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 const struct v4l2_ioctl_ops poseidon_fm_ioctl_ops = {
  296. .vidioc_querycap = vidioc_querycap,
  297. .vidioc_queryctrl = tlg_fm_vidioc_queryctrl,
  298. .vidioc_querymenu = tlg_fm_vidioc_querymenu,
  299. .vidioc_g_ctrl = tlg_fm_vidioc_g_ctrl,
  300. .vidioc_s_ctrl = tlg_fm_vidioc_s_ctrl,
  301. .vidioc_s_ext_ctrls = tlg_fm_vidioc_s_exts_ctrl,
  302. .vidioc_g_ext_ctrls = tlg_fm_vidioc_g_exts_ctrl,
  303. .vidioc_s_tuner = vidioc_s_tuner,
  304. .vidioc_g_tuner = tlg_fm_vidioc_g_tuner,
  305. .vidioc_g_frequency = fm_get_freq,
  306. .vidioc_s_frequency = fm_set_freq,
  307. };
  308. static struct video_device poseidon_fm_template = {
  309. .name = "Telegent-Radio",
  310. .fops = &poseidon_fm_fops,
  311. .minor = -1,
  312. .release = video_device_release,
  313. .ioctl_ops = &poseidon_fm_ioctl_ops,
  314. };
  315. int poseidon_fm_init(struct poseidon *p)
  316. {
  317. struct video_device *fm_dev;
  318. int err;
  319. fm_dev = vdev_init(p, &poseidon_fm_template);
  320. if (fm_dev == NULL)
  321. return -ENOMEM;
  322. p->radio_data.fm_dev = fm_dev;
  323. set_frequency(p, TUNER_FREQ_MIN_FM);
  324. err = video_register_device(fm_dev, VFL_TYPE_RADIO, -1);
  325. if (err < 0) {
  326. video_device_release(fm_dev);
  327. return err;
  328. }
  329. return 0;
  330. }
  331. int poseidon_fm_exit(struct poseidon *p)
  332. {
  333. destroy_video_device(&p->radio_data.fm_dev);
  334. return 0;
  335. }