radio-gemtek-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. ***************************************************************************
  3. *
  4. * radio-gemtek-pci.c - Gemtek PCI Radio driver
  5. * (C) 2001 Vladimir Shebordaev <vshebordaev@mail.ru>
  6. *
  7. ***************************************************************************
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program; if not, write to the Free
  21. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  22. * USA.
  23. *
  24. ***************************************************************************
  25. *
  26. * Gemtek Corp still silently refuses to release any specifications
  27. * of their multimedia devices, so the protocol still has to be
  28. * reverse engineered.
  29. *
  30. * The v4l code was inspired by Jonas Munsin's Gemtek serial line
  31. * radio device driver.
  32. *
  33. * Please, let me know if this piece of code was useful :)
  34. *
  35. * TODO: multiple device support and portability were not tested
  36. *
  37. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  38. *
  39. ***************************************************************************
  40. */
  41. #include <linux/types.h>
  42. #include <linux/list.h>
  43. #include <linux/module.h>
  44. #include <linux/init.h>
  45. #include <linux/pci.h>
  46. #include <linux/videodev2.h>
  47. #include <linux/errno.h>
  48. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  49. #include <linux/io.h>
  50. #include <media/v4l2-device.h>
  51. #include <media/v4l2-ioctl.h>
  52. MODULE_AUTHOR("Vladimir Shebordaev <vshebordaev@mail.ru>");
  53. MODULE_DESCRIPTION("The video4linux driver for the Gemtek PCI Radio Card");
  54. MODULE_LICENSE("GPL");
  55. static int nr_radio = -1;
  56. static int mx = 1;
  57. module_param(mx, bool, 0);
  58. MODULE_PARM_DESC(mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not");
  59. module_param(nr_radio, int, 0);
  60. MODULE_PARM_DESC(nr_radio, "video4linux device number to use");
  61. #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
  62. #ifndef PCI_VENDOR_ID_GEMTEK
  63. #define PCI_VENDOR_ID_GEMTEK 0x5046
  64. #endif
  65. #ifndef PCI_DEVICE_ID_GEMTEK_PR103
  66. #define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
  67. #endif
  68. #ifndef GEMTEK_PCI_RANGE_LOW
  69. #define GEMTEK_PCI_RANGE_LOW (87*16000)
  70. #endif
  71. #ifndef GEMTEK_PCI_RANGE_HIGH
  72. #define GEMTEK_PCI_RANGE_HIGH (108*16000)
  73. #endif
  74. struct gemtek_pci {
  75. struct v4l2_device v4l2_dev;
  76. struct video_device vdev;
  77. struct mutex lock;
  78. struct pci_dev *pdev;
  79. u32 iobase;
  80. u32 length;
  81. u32 current_frequency;
  82. u8 mute;
  83. };
  84. static inline struct gemtek_pci *to_gemtek_pci(struct v4l2_device *v4l2_dev)
  85. {
  86. return container_of(v4l2_dev, struct gemtek_pci, v4l2_dev);
  87. }
  88. static inline u8 gemtek_pci_out(u16 value, u32 port)
  89. {
  90. outw(value, port);
  91. return (u8)value;
  92. }
  93. #define _b0(v) (*((u8 *)&v))
  94. static void __gemtek_pci_cmd(u16 value, u32 port, u8 *last_byte, int keep)
  95. {
  96. u8 byte = *last_byte;
  97. if (!value) {
  98. if (!keep)
  99. value = (u16)port;
  100. byte &= 0xfd;
  101. } else
  102. byte |= 2;
  103. _b0(value) = byte;
  104. outw(value, port);
  105. byte |= 1;
  106. _b0(value) = byte;
  107. outw(value, port);
  108. byte &= 0xfe;
  109. _b0(value) = byte;
  110. outw(value, port);
  111. *last_byte = byte;
  112. }
  113. static inline void gemtek_pci_nil(u32 port, u8 *last_byte)
  114. {
  115. __gemtek_pci_cmd(0x00, port, last_byte, false);
  116. }
  117. static inline void gemtek_pci_cmd(u16 cmd, u32 port, u8 *last_byte)
  118. {
  119. __gemtek_pci_cmd(cmd, port, last_byte, true);
  120. }
  121. static void gemtek_pci_setfrequency(struct gemtek_pci *card, unsigned long frequency)
  122. {
  123. int i;
  124. u32 value = frequency / 200 + 856;
  125. u16 mask = 0x8000;
  126. u8 last_byte;
  127. u32 port = card->iobase;
  128. mutex_lock(&card->lock);
  129. card->current_frequency = frequency;
  130. last_byte = gemtek_pci_out(0x06, port);
  131. i = 0;
  132. do {
  133. gemtek_pci_nil(port, &last_byte);
  134. i++;
  135. } while (i < 9);
  136. i = 0;
  137. do {
  138. gemtek_pci_cmd(value & mask, port, &last_byte);
  139. mask >>= 1;
  140. i++;
  141. } while (i < 16);
  142. outw(0x10, port);
  143. mutex_unlock(&card->lock);
  144. }
  145. static void gemtek_pci_mute(struct gemtek_pci *card)
  146. {
  147. mutex_lock(&card->lock);
  148. outb(0x1f, card->iobase);
  149. card->mute = true;
  150. mutex_unlock(&card->lock);
  151. }
  152. static void gemtek_pci_unmute(struct gemtek_pci *card)
  153. {
  154. if (card->mute) {
  155. gemtek_pci_setfrequency(card, card->current_frequency);
  156. card->mute = false;
  157. }
  158. }
  159. static int gemtek_pci_getsignal(struct gemtek_pci *card)
  160. {
  161. int sig;
  162. mutex_lock(&card->lock);
  163. sig = (inb(card->iobase) & 0x08) ? 0 : 1;
  164. mutex_unlock(&card->lock);
  165. return sig;
  166. }
  167. static int vidioc_querycap(struct file *file, void *priv,
  168. struct v4l2_capability *v)
  169. {
  170. struct gemtek_pci *card = video_drvdata(file);
  171. strlcpy(v->driver, "radio-gemtek-pci", sizeof(v->driver));
  172. strlcpy(v->card, "GemTek PCI Radio", sizeof(v->card));
  173. snprintf(v->bus_info, sizeof(v->bus_info), "PCI:%s", pci_name(card->pdev));
  174. v->version = RADIO_VERSION;
  175. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  176. return 0;
  177. }
  178. static int vidioc_g_tuner(struct file *file, void *priv,
  179. struct v4l2_tuner *v)
  180. {
  181. struct gemtek_pci *card = video_drvdata(file);
  182. if (v->index > 0)
  183. return -EINVAL;
  184. strlcpy(v->name, "FM", sizeof(v->name));
  185. v->type = V4L2_TUNER_RADIO;
  186. v->rangelow = GEMTEK_PCI_RANGE_LOW;
  187. v->rangehigh = GEMTEK_PCI_RANGE_HIGH;
  188. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  189. v->capability = V4L2_TUNER_CAP_LOW;
  190. v->audmode = V4L2_TUNER_MODE_MONO;
  191. v->signal = 0xffff * gemtek_pci_getsignal(card);
  192. return 0;
  193. }
  194. static int vidioc_s_tuner(struct file *file, void *priv,
  195. struct v4l2_tuner *v)
  196. {
  197. return v->index ? -EINVAL : 0;
  198. }
  199. static int vidioc_s_frequency(struct file *file, void *priv,
  200. struct v4l2_frequency *f)
  201. {
  202. struct gemtek_pci *card = video_drvdata(file);
  203. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  204. return -EINVAL;
  205. if (f->frequency < GEMTEK_PCI_RANGE_LOW ||
  206. f->frequency > GEMTEK_PCI_RANGE_HIGH)
  207. return -EINVAL;
  208. gemtek_pci_setfrequency(card, f->frequency);
  209. card->mute = false;
  210. return 0;
  211. }
  212. static int vidioc_g_frequency(struct file *file, void *priv,
  213. struct v4l2_frequency *f)
  214. {
  215. struct gemtek_pci *card = video_drvdata(file);
  216. if (f->tuner != 0)
  217. return -EINVAL;
  218. f->type = V4L2_TUNER_RADIO;
  219. f->frequency = card->current_frequency;
  220. return 0;
  221. }
  222. static int vidioc_queryctrl(struct file *file, void *priv,
  223. struct v4l2_queryctrl *qc)
  224. {
  225. switch (qc->id) {
  226. case V4L2_CID_AUDIO_MUTE:
  227. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  228. case V4L2_CID_AUDIO_VOLUME:
  229. return v4l2_ctrl_query_fill(qc, 0, 65535, 65535, 65535);
  230. }
  231. return -EINVAL;
  232. }
  233. static int vidioc_g_ctrl(struct file *file, void *priv,
  234. struct v4l2_control *ctrl)
  235. {
  236. struct gemtek_pci *card = video_drvdata(file);
  237. switch (ctrl->id) {
  238. case V4L2_CID_AUDIO_MUTE:
  239. ctrl->value = card->mute;
  240. return 0;
  241. case V4L2_CID_AUDIO_VOLUME:
  242. if (card->mute)
  243. ctrl->value = 0;
  244. else
  245. ctrl->value = 65535;
  246. return 0;
  247. }
  248. return -EINVAL;
  249. }
  250. static int vidioc_s_ctrl(struct file *file, void *priv,
  251. struct v4l2_control *ctrl)
  252. {
  253. struct gemtek_pci *card = video_drvdata(file);
  254. switch (ctrl->id) {
  255. case V4L2_CID_AUDIO_MUTE:
  256. if (ctrl->value)
  257. gemtek_pci_mute(card);
  258. else
  259. gemtek_pci_unmute(card);
  260. return 0;
  261. case V4L2_CID_AUDIO_VOLUME:
  262. if (ctrl->value)
  263. gemtek_pci_unmute(card);
  264. else
  265. gemtek_pci_mute(card);
  266. return 0;
  267. }
  268. return -EINVAL;
  269. }
  270. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  271. {
  272. *i = 0;
  273. return 0;
  274. }
  275. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  276. {
  277. return i ? -EINVAL : 0;
  278. }
  279. static int vidioc_g_audio(struct file *file, void *priv,
  280. struct v4l2_audio *a)
  281. {
  282. a->index = 0;
  283. strlcpy(a->name, "Radio", sizeof(a->name));
  284. a->capability = V4L2_AUDCAP_STEREO;
  285. return 0;
  286. }
  287. static int vidioc_s_audio(struct file *file, void *priv,
  288. struct v4l2_audio *a)
  289. {
  290. return a->index ? -EINVAL : 0;
  291. }
  292. enum {
  293. GEMTEK_PR103
  294. };
  295. static char *card_names[] __devinitdata = {
  296. "GEMTEK_PR103"
  297. };
  298. static struct pci_device_id gemtek_pci_id[] =
  299. {
  300. { PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
  301. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
  302. { 0 }
  303. };
  304. MODULE_DEVICE_TABLE(pci, gemtek_pci_id);
  305. static const struct v4l2_file_operations gemtek_pci_fops = {
  306. .owner = THIS_MODULE,
  307. .ioctl = video_ioctl2,
  308. };
  309. static const struct v4l2_ioctl_ops gemtek_pci_ioctl_ops = {
  310. .vidioc_querycap = vidioc_querycap,
  311. .vidioc_g_tuner = vidioc_g_tuner,
  312. .vidioc_s_tuner = vidioc_s_tuner,
  313. .vidioc_g_audio = vidioc_g_audio,
  314. .vidioc_s_audio = vidioc_s_audio,
  315. .vidioc_g_input = vidioc_g_input,
  316. .vidioc_s_input = vidioc_s_input,
  317. .vidioc_g_frequency = vidioc_g_frequency,
  318. .vidioc_s_frequency = vidioc_s_frequency,
  319. .vidioc_queryctrl = vidioc_queryctrl,
  320. .vidioc_g_ctrl = vidioc_g_ctrl,
  321. .vidioc_s_ctrl = vidioc_s_ctrl,
  322. };
  323. static int __devinit gemtek_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
  324. {
  325. struct gemtek_pci *card;
  326. struct v4l2_device *v4l2_dev;
  327. int res;
  328. card = kzalloc(sizeof(struct gemtek_pci), GFP_KERNEL);
  329. if (card == NULL) {
  330. dev_err(&pdev->dev, "out of memory\n");
  331. return -ENOMEM;
  332. }
  333. v4l2_dev = &card->v4l2_dev;
  334. mutex_init(&card->lock);
  335. card->pdev = pdev;
  336. strlcpy(v4l2_dev->name, "gemtek_pci", sizeof(v4l2_dev->name));
  337. res = v4l2_device_register(&pdev->dev, v4l2_dev);
  338. if (res < 0) {
  339. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  340. kfree(card);
  341. return res;
  342. }
  343. if (pci_enable_device(pdev))
  344. goto err_pci;
  345. card->iobase = pci_resource_start(pdev, 0);
  346. card->length = pci_resource_len(pdev, 0);
  347. if (request_region(card->iobase, card->length, card_names[pci_id->driver_data]) == NULL) {
  348. v4l2_err(v4l2_dev, "i/o port already in use\n");
  349. goto err_pci;
  350. }
  351. strlcpy(card->vdev.name, v4l2_dev->name, sizeof(card->vdev.name));
  352. card->vdev.v4l2_dev = v4l2_dev;
  353. card->vdev.fops = &gemtek_pci_fops;
  354. card->vdev.ioctl_ops = &gemtek_pci_ioctl_ops;
  355. card->vdev.release = video_device_release_empty;
  356. video_set_drvdata(&card->vdev, card);
  357. if (video_register_device(&card->vdev, VFL_TYPE_RADIO, nr_radio) < 0)
  358. goto err_video;
  359. gemtek_pci_mute(card);
  360. v4l2_info(v4l2_dev, "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
  361. pdev->revision, card->iobase, card->iobase + card->length - 1);
  362. return 0;
  363. err_video:
  364. release_region(card->iobase, card->length);
  365. err_pci:
  366. v4l2_device_unregister(v4l2_dev);
  367. kfree(card);
  368. return -ENODEV;
  369. }
  370. static void __devexit gemtek_pci_remove(struct pci_dev *pdev)
  371. {
  372. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  373. struct gemtek_pci *card = to_gemtek_pci(v4l2_dev);
  374. video_unregister_device(&card->vdev);
  375. v4l2_device_unregister(v4l2_dev);
  376. release_region(card->iobase, card->length);
  377. if (mx)
  378. gemtek_pci_mute(card);
  379. kfree(card);
  380. }
  381. static struct pci_driver gemtek_pci_driver = {
  382. .name = "gemtek_pci",
  383. .id_table = gemtek_pci_id,
  384. .probe = gemtek_pci_probe,
  385. .remove = __devexit_p(gemtek_pci_remove),
  386. };
  387. static int __init gemtek_pci_init(void)
  388. {
  389. return pci_register_driver(&gemtek_pci_driver);
  390. }
  391. static void __exit gemtek_pci_exit(void)
  392. {
  393. pci_unregister_driver(&gemtek_pci_driver);
  394. }
  395. module_init(gemtek_pci_init);
  396. module_exit(gemtek_pci_exit);