radio-cadet.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
  2. *
  3. * by Fred Gleason <fredg@wava.com>
  4. * Version 0.3.3
  5. *
  6. * (Loosely) based on code for the Aztech radio card by
  7. *
  8. * Russell Kroll (rkroll@exploits.org)
  9. * Quay Ly
  10. * Donald Song
  11. * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
  12. * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
  13. * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
  14. *
  15. * History:
  16. * 2000-04-29 Russell Kroll <rkroll@exploits.org>
  17. * Added ISAPnP detection for Linux 2.3/2.4
  18. *
  19. * 2001-01-10 Russell Kroll <rkroll@exploits.org>
  20. * Removed dead CONFIG_RADIO_CADET_PORT code
  21. * PnP detection on load is now default (no args necessary)
  22. *
  23. * 2002-01-17 Adam Belay <ambx1@neo.rr.com>
  24. * Updated to latest pnp code
  25. *
  26. * 2003-01-31 Alan Cox <alan@lxorguk.ukuu.org.uk>
  27. * Cleaned up locking, delay code, general odds and ends
  28. *
  29. * 2006-07-30 Hans J. Koch <koch@hjk-az.de>
  30. * Changed API to V4L2
  31. */
  32. #include <linux/module.h> /* Modules */
  33. #include <linux/init.h> /* Initdata */
  34. #include <linux/ioport.h> /* request_region */
  35. #include <linux/delay.h> /* udelay */
  36. #include <linux/videodev2.h> /* V4L2 API defs */
  37. #include <linux/param.h>
  38. #include <linux/pnp.h>
  39. #include <linux/sched.h>
  40. #include <linux/io.h> /* outb, outb_p */
  41. #include <media/v4l2-device.h>
  42. #include <media/v4l2-ioctl.h>
  43. #include <media/v4l2-ctrls.h>
  44. #include <media/v4l2-fh.h>
  45. #include <media/v4l2-event.h>
  46. MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  47. MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
  48. MODULE_LICENSE("GPL");
  49. MODULE_VERSION("0.3.4");
  50. static int io = -1; /* default to isapnp activation */
  51. static int radio_nr = -1;
  52. module_param(io, int, 0);
  53. MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
  54. module_param(radio_nr, int, 0);
  55. #define RDS_BUFFER 256
  56. #define RDS_RX_FLAG 1
  57. #define MBS_RX_FLAG 2
  58. struct cadet {
  59. struct v4l2_device v4l2_dev;
  60. struct video_device vdev;
  61. struct v4l2_ctrl_handler ctrl_handler;
  62. int io;
  63. bool is_fm_band;
  64. u32 curfreq;
  65. int tunestat;
  66. int sigstrength;
  67. wait_queue_head_t read_queue;
  68. struct timer_list readtimer;
  69. u8 rdsin, rdsout, rdsstat;
  70. unsigned char rdsbuf[RDS_BUFFER];
  71. struct mutex lock;
  72. int reading;
  73. };
  74. static struct cadet cadet_card;
  75. /*
  76. * Signal Strength Threshold Values
  77. * The V4L API spec does not define any particular unit for the signal
  78. * strength value. These values are in microvolts of RF at the tuner's input.
  79. */
  80. static u16 sigtable[2][4] = {
  81. { 1835, 2621, 4128, 65535 },
  82. { 2185, 4369, 13107, 65535 },
  83. };
  84. static int cadet_getstereo(struct cadet *dev)
  85. {
  86. int ret = V4L2_TUNER_SUB_MONO;
  87. if (!dev->is_fm_band) /* Only FM has stereo capability! */
  88. return V4L2_TUNER_SUB_MONO;
  89. outb(7, dev->io); /* Select tuner control */
  90. if ((inb(dev->io + 1) & 0x40) == 0)
  91. ret = V4L2_TUNER_SUB_STEREO;
  92. return ret;
  93. }
  94. static unsigned cadet_gettune(struct cadet *dev)
  95. {
  96. int curvol, i;
  97. unsigned fifo = 0;
  98. /*
  99. * Prepare for read
  100. */
  101. outb(7, dev->io); /* Select tuner control */
  102. curvol = inb(dev->io + 1); /* Save current volume/mute setting */
  103. outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
  104. dev->tunestat = 0xffff;
  105. /*
  106. * Read the shift register
  107. */
  108. for (i = 0; i < 25; i++) {
  109. fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
  110. if (i < 24) {
  111. outb(0x01, dev->io + 1);
  112. dev->tunestat &= inb(dev->io + 1);
  113. outb(0x00, dev->io + 1);
  114. }
  115. }
  116. /*
  117. * Restore volume/mute setting
  118. */
  119. outb(curvol, dev->io + 1);
  120. return fifo;
  121. }
  122. static unsigned cadet_getfreq(struct cadet *dev)
  123. {
  124. int i;
  125. unsigned freq = 0, test, fifo = 0;
  126. /*
  127. * Read current tuning
  128. */
  129. fifo = cadet_gettune(dev);
  130. /*
  131. * Convert to actual frequency
  132. */
  133. if (!dev->is_fm_band) /* AM */
  134. return ((fifo & 0x7fff) - 450) * 16;
  135. test = 12500;
  136. for (i = 0; i < 14; i++) {
  137. if ((fifo & 0x01) != 0)
  138. freq += test;
  139. test = test << 1;
  140. fifo = fifo >> 1;
  141. }
  142. freq -= 10700000; /* IF frequency is 10.7 MHz */
  143. freq = (freq * 16) / 1000; /* Make it 1/16 kHz */
  144. return freq;
  145. }
  146. static void cadet_settune(struct cadet *dev, unsigned fifo)
  147. {
  148. int i;
  149. unsigned test;
  150. outb(7, dev->io); /* Select tuner control */
  151. /*
  152. * Write the shift register
  153. */
  154. test = 0;
  155. test = (fifo >> 23) & 0x02; /* Align data for SDO */
  156. test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
  157. outb(7, dev->io); /* Select tuner control */
  158. outb(test, dev->io + 1); /* Initialize for write */
  159. for (i = 0; i < 25; i++) {
  160. test |= 0x01; /* Toggle SCK High */
  161. outb(test, dev->io + 1);
  162. test &= 0xfe; /* Toggle SCK Low */
  163. outb(test, dev->io + 1);
  164. fifo = fifo << 1; /* Prepare the next bit */
  165. test = 0x1c | ((fifo >> 23) & 0x02);
  166. outb(test, dev->io + 1);
  167. }
  168. }
  169. static void cadet_setfreq(struct cadet *dev, unsigned freq)
  170. {
  171. unsigned fifo;
  172. int i, j, test;
  173. int curvol;
  174. dev->curfreq = freq;
  175. /*
  176. * Formulate a fifo command
  177. */
  178. fifo = 0;
  179. if (dev->is_fm_band) { /* FM */
  180. test = 102400;
  181. freq = freq / 16; /* Make it kHz */
  182. freq += 10700; /* IF is 10700 kHz */
  183. for (i = 0; i < 14; i++) {
  184. fifo = fifo << 1;
  185. if (freq >= test) {
  186. fifo |= 0x01;
  187. freq -= test;
  188. }
  189. test = test >> 1;
  190. }
  191. } else { /* AM */
  192. fifo = (freq / 16) + 450; /* Make it kHz */
  193. fifo |= 0x100000; /* Select AM Band */
  194. }
  195. /*
  196. * Save current volume/mute setting
  197. */
  198. outb(7, dev->io); /* Select tuner control */
  199. curvol = inb(dev->io + 1);
  200. /*
  201. * Tune the card
  202. */
  203. for (j = 3; j > -1; j--) {
  204. cadet_settune(dev, fifo | (j << 16));
  205. outb(7, dev->io); /* Select tuner control */
  206. outb(curvol, dev->io + 1);
  207. msleep(100);
  208. cadet_gettune(dev);
  209. if ((dev->tunestat & 0x40) == 0) { /* Tuned */
  210. dev->sigstrength = sigtable[dev->is_fm_band][j];
  211. goto reset_rds;
  212. }
  213. }
  214. dev->sigstrength = 0;
  215. reset_rds:
  216. outb(3, dev->io);
  217. outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
  218. }
  219. static void cadet_handler(unsigned long data)
  220. {
  221. struct cadet *dev = (void *)data;
  222. /* Service the RDS fifo */
  223. if (mutex_trylock(&dev->lock)) {
  224. outb(0x3, dev->io); /* Select RDS Decoder Control */
  225. if ((inb(dev->io + 1) & 0x20) != 0)
  226. printk(KERN_CRIT "cadet: RDS fifo overflow\n");
  227. outb(0x80, dev->io); /* Select RDS fifo */
  228. while ((inb(dev->io) & 0x80) != 0) {
  229. dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
  230. if (dev->rdsin + 1 == dev->rdsout)
  231. printk(KERN_WARNING "cadet: RDS buffer overflow\n");
  232. else
  233. dev->rdsin++;
  234. }
  235. mutex_unlock(&dev->lock);
  236. }
  237. /*
  238. * Service pending read
  239. */
  240. if (dev->rdsin != dev->rdsout)
  241. wake_up_interruptible(&dev->read_queue);
  242. /*
  243. * Clean up and exit
  244. */
  245. init_timer(&dev->readtimer);
  246. dev->readtimer.function = cadet_handler;
  247. dev->readtimer.data = data;
  248. dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
  249. add_timer(&dev->readtimer);
  250. }
  251. static void cadet_start_rds(struct cadet *dev)
  252. {
  253. dev->rdsstat = 1;
  254. outb(0x80, dev->io); /* Select RDS fifo */
  255. init_timer(&dev->readtimer);
  256. dev->readtimer.function = cadet_handler;
  257. dev->readtimer.data = (unsigned long)dev;
  258. dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
  259. add_timer(&dev->readtimer);
  260. }
  261. static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  262. {
  263. struct cadet *dev = video_drvdata(file);
  264. unsigned char readbuf[RDS_BUFFER];
  265. int i = 0;
  266. mutex_lock(&dev->lock);
  267. if (dev->rdsstat == 0)
  268. cadet_start_rds(dev);
  269. if (dev->rdsin == dev->rdsout) {
  270. if (file->f_flags & O_NONBLOCK) {
  271. i = -EWOULDBLOCK;
  272. goto unlock;
  273. }
  274. mutex_unlock(&dev->lock);
  275. interruptible_sleep_on(&dev->read_queue);
  276. mutex_lock(&dev->lock);
  277. }
  278. while (i < count && dev->rdsin != dev->rdsout)
  279. readbuf[i++] = dev->rdsbuf[dev->rdsout++];
  280. if (i && copy_to_user(data, readbuf, i))
  281. i = -EFAULT;
  282. unlock:
  283. mutex_unlock(&dev->lock);
  284. return i;
  285. }
  286. static int vidioc_querycap(struct file *file, void *priv,
  287. struct v4l2_capability *v)
  288. {
  289. strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
  290. strlcpy(v->card, "ADS Cadet", sizeof(v->card));
  291. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  292. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
  293. V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
  294. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  295. return 0;
  296. }
  297. static const struct v4l2_frequency_band bands[] = {
  298. {
  299. .index = 0,
  300. .type = V4L2_TUNER_RADIO,
  301. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
  302. .rangelow = 8320, /* 520 kHz */
  303. .rangehigh = 26400, /* 1650 kHz */
  304. .modulation = V4L2_BAND_MODULATION_AM,
  305. }, {
  306. .index = 1,
  307. .type = V4L2_TUNER_RADIO,
  308. .capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
  309. V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW |
  310. V4L2_TUNER_CAP_FREQ_BANDS,
  311. .rangelow = 1400000, /* 87.5 MHz */
  312. .rangehigh = 1728000, /* 108.0 MHz */
  313. .modulation = V4L2_BAND_MODULATION_FM,
  314. },
  315. };
  316. static int vidioc_g_tuner(struct file *file, void *priv,
  317. struct v4l2_tuner *v)
  318. {
  319. struct cadet *dev = video_drvdata(file);
  320. if (v->index)
  321. return -EINVAL;
  322. v->type = V4L2_TUNER_RADIO;
  323. strlcpy(v->name, "Radio", sizeof(v->name));
  324. v->capability = bands[0].capability | bands[1].capability;
  325. v->rangelow = bands[0].rangelow; /* 520 kHz (start of AM band) */
  326. v->rangehigh = bands[1].rangehigh; /* 108.0 MHz (end of FM band) */
  327. if (dev->is_fm_band) {
  328. v->rxsubchans = cadet_getstereo(dev);
  329. outb(3, dev->io);
  330. outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
  331. mdelay(100);
  332. outb(3, dev->io);
  333. if (inb(dev->io + 1) & 0x80)
  334. v->rxsubchans |= V4L2_TUNER_SUB_RDS;
  335. } else {
  336. v->rangelow = 8320; /* 520 kHz */
  337. v->rangehigh = 26400; /* 1650 kHz */
  338. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  339. }
  340. v->audmode = V4L2_TUNER_MODE_STEREO;
  341. v->signal = dev->sigstrength; /* We might need to modify scaling of this */
  342. return 0;
  343. }
  344. static int vidioc_s_tuner(struct file *file, void *priv,
  345. struct v4l2_tuner *v)
  346. {
  347. return v->index ? -EINVAL : 0;
  348. }
  349. static int vidioc_enum_freq_bands(struct file *file, void *priv,
  350. struct v4l2_frequency_band *band)
  351. {
  352. if (band->tuner)
  353. return -EINVAL;
  354. if (band->index >= ARRAY_SIZE(bands))
  355. return -EINVAL;
  356. *band = bands[band->index];
  357. return 0;
  358. }
  359. static int vidioc_g_frequency(struct file *file, void *priv,
  360. struct v4l2_frequency *f)
  361. {
  362. struct cadet *dev = video_drvdata(file);
  363. if (f->tuner)
  364. return -EINVAL;
  365. f->type = V4L2_TUNER_RADIO;
  366. f->frequency = dev->curfreq;
  367. return 0;
  368. }
  369. static int vidioc_s_frequency(struct file *file, void *priv,
  370. struct v4l2_frequency *f)
  371. {
  372. struct cadet *dev = video_drvdata(file);
  373. if (f->tuner)
  374. return -EINVAL;
  375. dev->is_fm_band =
  376. f->frequency >= (bands[0].rangehigh + bands[1].rangelow) / 2;
  377. clamp(f->frequency, bands[dev->is_fm_band].rangelow,
  378. bands[dev->is_fm_band].rangehigh);
  379. cadet_setfreq(dev, f->frequency);
  380. return 0;
  381. }
  382. static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
  383. {
  384. struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
  385. switch (ctrl->id) {
  386. case V4L2_CID_AUDIO_MUTE:
  387. outb(7, dev->io); /* Select tuner control */
  388. if (ctrl->val)
  389. outb(0x00, dev->io + 1);
  390. else
  391. outb(0x20, dev->io + 1);
  392. return 0;
  393. }
  394. return -EINVAL;
  395. }
  396. static int cadet_open(struct file *file)
  397. {
  398. struct cadet *dev = video_drvdata(file);
  399. int err;
  400. mutex_lock(&dev->lock);
  401. err = v4l2_fh_open(file);
  402. if (err)
  403. goto fail;
  404. if (v4l2_fh_is_singular_file(file))
  405. init_waitqueue_head(&dev->read_queue);
  406. fail:
  407. mutex_unlock(&dev->lock);
  408. return err;
  409. }
  410. static int cadet_release(struct file *file)
  411. {
  412. struct cadet *dev = video_drvdata(file);
  413. mutex_lock(&dev->lock);
  414. if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
  415. del_timer_sync(&dev->readtimer);
  416. dev->rdsstat = 0;
  417. }
  418. v4l2_fh_release(file);
  419. mutex_unlock(&dev->lock);
  420. return 0;
  421. }
  422. static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
  423. {
  424. struct cadet *dev = video_drvdata(file);
  425. unsigned long req_events = poll_requested_events(wait);
  426. unsigned int res = v4l2_ctrl_poll(file, wait);
  427. poll_wait(file, &dev->read_queue, wait);
  428. if (dev->rdsstat == 0 && (req_events & (POLLIN | POLLRDNORM))) {
  429. mutex_lock(&dev->lock);
  430. if (dev->rdsstat == 0)
  431. cadet_start_rds(dev);
  432. mutex_unlock(&dev->lock);
  433. }
  434. if (dev->rdsin != dev->rdsout)
  435. res |= POLLIN | POLLRDNORM;
  436. return res;
  437. }
  438. static const struct v4l2_file_operations cadet_fops = {
  439. .owner = THIS_MODULE,
  440. .open = cadet_open,
  441. .release = cadet_release,
  442. .read = cadet_read,
  443. .unlocked_ioctl = video_ioctl2,
  444. .poll = cadet_poll,
  445. };
  446. static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
  447. .vidioc_querycap = vidioc_querycap,
  448. .vidioc_g_tuner = vidioc_g_tuner,
  449. .vidioc_s_tuner = vidioc_s_tuner,
  450. .vidioc_g_frequency = vidioc_g_frequency,
  451. .vidioc_s_frequency = vidioc_s_frequency,
  452. .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
  453. .vidioc_log_status = v4l2_ctrl_log_status,
  454. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  455. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  456. };
  457. static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
  458. .s_ctrl = cadet_s_ctrl,
  459. };
  460. #ifdef CONFIG_PNP
  461. static struct pnp_device_id cadet_pnp_devices[] = {
  462. /* ADS Cadet AM/FM Radio Card */
  463. {.id = "MSM0c24", .driver_data = 0},
  464. {.id = ""}
  465. };
  466. MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
  467. static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
  468. {
  469. if (!dev)
  470. return -ENODEV;
  471. /* only support one device */
  472. if (io > 0)
  473. return -EBUSY;
  474. if (!pnp_port_valid(dev, 0))
  475. return -ENODEV;
  476. io = pnp_port_start(dev, 0);
  477. printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
  478. return io;
  479. }
  480. static struct pnp_driver cadet_pnp_driver = {
  481. .name = "radio-cadet",
  482. .id_table = cadet_pnp_devices,
  483. .probe = cadet_pnp_probe,
  484. .remove = NULL,
  485. };
  486. #else
  487. static struct pnp_driver cadet_pnp_driver;
  488. #endif
  489. static void cadet_probe(struct cadet *dev)
  490. {
  491. static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
  492. int i;
  493. for (i = 0; i < 8; i++) {
  494. dev->io = iovals[i];
  495. if (request_region(dev->io, 2, "cadet-probe")) {
  496. cadet_setfreq(dev, bands[1].rangelow);
  497. if (cadet_getfreq(dev) == bands[1].rangelow) {
  498. release_region(dev->io, 2);
  499. return;
  500. }
  501. release_region(dev->io, 2);
  502. }
  503. }
  504. dev->io = -1;
  505. }
  506. /*
  507. * io should only be set if the user has used something like
  508. * isapnp (the userspace program) to initialize this card for us
  509. */
  510. static int __init cadet_init(void)
  511. {
  512. struct cadet *dev = &cadet_card;
  513. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  514. struct v4l2_ctrl_handler *hdl;
  515. int res = -ENODEV;
  516. strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
  517. mutex_init(&dev->lock);
  518. /* If a probe was requested then probe ISAPnP first (safest) */
  519. if (io < 0)
  520. pnp_register_driver(&cadet_pnp_driver);
  521. dev->io = io;
  522. /* If that fails then probe unsafely if probe is requested */
  523. if (dev->io < 0)
  524. cadet_probe(dev);
  525. /* Else we bail out */
  526. if (dev->io < 0) {
  527. #ifdef MODULE
  528. v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
  529. v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
  530. #endif
  531. goto fail;
  532. }
  533. if (!request_region(dev->io, 2, "cadet"))
  534. goto fail;
  535. res = v4l2_device_register(NULL, v4l2_dev);
  536. if (res < 0) {
  537. release_region(dev->io, 2);
  538. v4l2_err(v4l2_dev, "could not register v4l2_device\n");
  539. goto fail;
  540. }
  541. hdl = &dev->ctrl_handler;
  542. v4l2_ctrl_handler_init(hdl, 2);
  543. v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
  544. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  545. v4l2_dev->ctrl_handler = hdl;
  546. if (hdl->error) {
  547. res = hdl->error;
  548. v4l2_err(v4l2_dev, "Could not register controls\n");
  549. goto err_hdl;
  550. }
  551. dev->is_fm_band = true;
  552. dev->curfreq = bands[dev->is_fm_band].rangelow;
  553. cadet_setfreq(dev, dev->curfreq);
  554. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  555. dev->vdev.v4l2_dev = v4l2_dev;
  556. dev->vdev.fops = &cadet_fops;
  557. dev->vdev.ioctl_ops = &cadet_ioctl_ops;
  558. dev->vdev.release = video_device_release_empty;
  559. dev->vdev.lock = &dev->lock;
  560. set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
  561. video_set_drvdata(&dev->vdev, dev);
  562. res = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
  563. if (res < 0)
  564. goto err_hdl;
  565. v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
  566. return 0;
  567. err_hdl:
  568. v4l2_ctrl_handler_free(hdl);
  569. v4l2_device_unregister(v4l2_dev);
  570. release_region(dev->io, 2);
  571. fail:
  572. pnp_unregister_driver(&cadet_pnp_driver);
  573. return res;
  574. }
  575. static void __exit cadet_exit(void)
  576. {
  577. struct cadet *dev = &cadet_card;
  578. video_unregister_device(&dev->vdev);
  579. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  580. v4l2_device_unregister(&dev->v4l2_dev);
  581. outb(7, dev->io); /* Mute */
  582. outb(0x00, dev->io + 1);
  583. release_region(dev->io, 2);
  584. pnp_unregister_driver(&cadet_pnp_driver);
  585. }
  586. module_init(cadet_init);
  587. module_exit(cadet_exit);