radio-cadet.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 const struct v4l2_frequency_band bands[] = {
  85. {
  86. .index = 0,
  87. .type = V4L2_TUNER_RADIO,
  88. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
  89. .rangelow = 8320, /* 520 kHz */
  90. .rangehigh = 26400, /* 1650 kHz */
  91. .modulation = V4L2_BAND_MODULATION_AM,
  92. }, {
  93. .index = 1,
  94. .type = V4L2_TUNER_RADIO,
  95. .capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
  96. V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW |
  97. V4L2_TUNER_CAP_FREQ_BANDS,
  98. .rangelow = 1400000, /* 87.5 MHz */
  99. .rangehigh = 1728000, /* 108.0 MHz */
  100. .modulation = V4L2_BAND_MODULATION_FM,
  101. },
  102. };
  103. static int cadet_getstereo(struct cadet *dev)
  104. {
  105. int ret = V4L2_TUNER_SUB_MONO;
  106. if (!dev->is_fm_band) /* Only FM has stereo capability! */
  107. return V4L2_TUNER_SUB_MONO;
  108. outb(7, dev->io); /* Select tuner control */
  109. if ((inb(dev->io + 1) & 0x40) == 0)
  110. ret = V4L2_TUNER_SUB_STEREO;
  111. return ret;
  112. }
  113. static unsigned cadet_gettune(struct cadet *dev)
  114. {
  115. int curvol, i;
  116. unsigned fifo = 0;
  117. /*
  118. * Prepare for read
  119. */
  120. outb(7, dev->io); /* Select tuner control */
  121. curvol = inb(dev->io + 1); /* Save current volume/mute setting */
  122. outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
  123. dev->tunestat = 0xffff;
  124. /*
  125. * Read the shift register
  126. */
  127. for (i = 0; i < 25; i++) {
  128. fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
  129. if (i < 24) {
  130. outb(0x01, dev->io + 1);
  131. dev->tunestat &= inb(dev->io + 1);
  132. outb(0x00, dev->io + 1);
  133. }
  134. }
  135. /*
  136. * Restore volume/mute setting
  137. */
  138. outb(curvol, dev->io + 1);
  139. return fifo;
  140. }
  141. static unsigned cadet_getfreq(struct cadet *dev)
  142. {
  143. int i;
  144. unsigned freq = 0, test, fifo = 0;
  145. /*
  146. * Read current tuning
  147. */
  148. fifo = cadet_gettune(dev);
  149. /*
  150. * Convert to actual frequency
  151. */
  152. if (!dev->is_fm_band) /* AM */
  153. return ((fifo & 0x7fff) - 450) * 16;
  154. test = 12500;
  155. for (i = 0; i < 14; i++) {
  156. if ((fifo & 0x01) != 0)
  157. freq += test;
  158. test = test << 1;
  159. fifo = fifo >> 1;
  160. }
  161. freq -= 10700000; /* IF frequency is 10.7 MHz */
  162. freq = (freq * 16) / 1000; /* Make it 1/16 kHz */
  163. return freq;
  164. }
  165. static void cadet_settune(struct cadet *dev, unsigned fifo)
  166. {
  167. int i;
  168. unsigned test;
  169. outb(7, dev->io); /* Select tuner control */
  170. /*
  171. * Write the shift register
  172. */
  173. test = 0;
  174. test = (fifo >> 23) & 0x02; /* Align data for SDO */
  175. test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
  176. outb(7, dev->io); /* Select tuner control */
  177. outb(test, dev->io + 1); /* Initialize for write */
  178. for (i = 0; i < 25; i++) {
  179. test |= 0x01; /* Toggle SCK High */
  180. outb(test, dev->io + 1);
  181. test &= 0xfe; /* Toggle SCK Low */
  182. outb(test, dev->io + 1);
  183. fifo = fifo << 1; /* Prepare the next bit */
  184. test = 0x1c | ((fifo >> 23) & 0x02);
  185. outb(test, dev->io + 1);
  186. }
  187. }
  188. static void cadet_setfreq(struct cadet *dev, unsigned freq)
  189. {
  190. unsigned fifo;
  191. int i, j, test;
  192. int curvol;
  193. freq = clamp(freq, bands[dev->is_fm_band].rangelow,
  194. bands[dev->is_fm_band].rangehigh);
  195. dev->curfreq = freq;
  196. /*
  197. * Formulate a fifo command
  198. */
  199. fifo = 0;
  200. if (dev->is_fm_band) { /* FM */
  201. test = 102400;
  202. freq = freq / 16; /* Make it kHz */
  203. freq += 10700; /* IF is 10700 kHz */
  204. for (i = 0; i < 14; i++) {
  205. fifo = fifo << 1;
  206. if (freq >= test) {
  207. fifo |= 0x01;
  208. freq -= test;
  209. }
  210. test = test >> 1;
  211. }
  212. } else { /* AM */
  213. fifo = (freq / 16) + 450; /* Make it kHz */
  214. fifo |= 0x100000; /* Select AM Band */
  215. }
  216. /*
  217. * Save current volume/mute setting
  218. */
  219. outb(7, dev->io); /* Select tuner control */
  220. curvol = inb(dev->io + 1);
  221. /*
  222. * Tune the card
  223. */
  224. for (j = 3; j > -1; j--) {
  225. cadet_settune(dev, fifo | (j << 16));
  226. outb(7, dev->io); /* Select tuner control */
  227. outb(curvol, dev->io + 1);
  228. msleep(100);
  229. cadet_gettune(dev);
  230. if ((dev->tunestat & 0x40) == 0) { /* Tuned */
  231. dev->sigstrength = sigtable[dev->is_fm_band][j];
  232. goto reset_rds;
  233. }
  234. }
  235. dev->sigstrength = 0;
  236. reset_rds:
  237. outb(3, dev->io);
  238. outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
  239. }
  240. static bool cadet_has_rds_data(struct cadet *dev)
  241. {
  242. bool result;
  243. mutex_lock(&dev->lock);
  244. result = dev->rdsin != dev->rdsout;
  245. mutex_unlock(&dev->lock);
  246. return result;
  247. }
  248. static void cadet_handler(struct timer_list *t)
  249. {
  250. struct cadet *dev = from_timer(dev, t, readtimer);
  251. /* Service the RDS fifo */
  252. if (mutex_trylock(&dev->lock)) {
  253. outb(0x3, dev->io); /* Select RDS Decoder Control */
  254. if ((inb(dev->io + 1) & 0x20) != 0)
  255. pr_err("cadet: RDS fifo overflow\n");
  256. outb(0x80, dev->io); /* Select RDS fifo */
  257. while ((inb(dev->io) & 0x80) != 0) {
  258. dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
  259. if (dev->rdsin + 1 != dev->rdsout)
  260. dev->rdsin++;
  261. }
  262. mutex_unlock(&dev->lock);
  263. }
  264. /*
  265. * Service pending read
  266. */
  267. if (cadet_has_rds_data(dev))
  268. wake_up_interruptible(&dev->read_queue);
  269. /*
  270. * Clean up and exit
  271. */
  272. dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
  273. add_timer(&dev->readtimer);
  274. }
  275. static void cadet_start_rds(struct cadet *dev)
  276. {
  277. dev->rdsstat = 1;
  278. outb(0x80, dev->io); /* Select RDS fifo */
  279. timer_setup(&dev->readtimer, cadet_handler, 0);
  280. dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
  281. add_timer(&dev->readtimer);
  282. }
  283. static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  284. {
  285. struct cadet *dev = video_drvdata(file);
  286. unsigned char readbuf[RDS_BUFFER];
  287. int i = 0;
  288. mutex_lock(&dev->lock);
  289. if (dev->rdsstat == 0)
  290. cadet_start_rds(dev);
  291. mutex_unlock(&dev->lock);
  292. if (!cadet_has_rds_data(dev) && (file->f_flags & O_NONBLOCK))
  293. return -EWOULDBLOCK;
  294. i = wait_event_interruptible(dev->read_queue, cadet_has_rds_data(dev));
  295. if (i)
  296. return i;
  297. mutex_lock(&dev->lock);
  298. while (i < count && dev->rdsin != dev->rdsout)
  299. readbuf[i++] = dev->rdsbuf[dev->rdsout++];
  300. mutex_unlock(&dev->lock);
  301. if (i && copy_to_user(data, readbuf, i))
  302. return -EFAULT;
  303. return i;
  304. }
  305. static int vidioc_querycap(struct file *file, void *priv,
  306. struct v4l2_capability *v)
  307. {
  308. strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
  309. strlcpy(v->card, "ADS Cadet", sizeof(v->card));
  310. strlcpy(v->bus_info, "ISA:radio-cadet", sizeof(v->bus_info));
  311. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
  312. V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
  313. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  314. return 0;
  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. const 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. const 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. cadet_setfreq(dev, f->frequency);
  378. return 0;
  379. }
  380. static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
  381. {
  382. struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
  383. switch (ctrl->id) {
  384. case V4L2_CID_AUDIO_MUTE:
  385. outb(7, dev->io); /* Select tuner control */
  386. if (ctrl->val)
  387. outb(0x00, dev->io + 1);
  388. else
  389. outb(0x20, dev->io + 1);
  390. return 0;
  391. }
  392. return -EINVAL;
  393. }
  394. static int cadet_open(struct file *file)
  395. {
  396. struct cadet *dev = video_drvdata(file);
  397. int err;
  398. mutex_lock(&dev->lock);
  399. err = v4l2_fh_open(file);
  400. if (err)
  401. goto fail;
  402. if (v4l2_fh_is_singular_file(file))
  403. init_waitqueue_head(&dev->read_queue);
  404. fail:
  405. mutex_unlock(&dev->lock);
  406. return err;
  407. }
  408. static int cadet_release(struct file *file)
  409. {
  410. struct cadet *dev = video_drvdata(file);
  411. mutex_lock(&dev->lock);
  412. if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
  413. del_timer_sync(&dev->readtimer);
  414. dev->rdsstat = 0;
  415. }
  416. v4l2_fh_release(file);
  417. mutex_unlock(&dev->lock);
  418. return 0;
  419. }
  420. static __poll_t cadet_poll(struct file *file, struct poll_table_struct *wait)
  421. {
  422. struct cadet *dev = video_drvdata(file);
  423. __poll_t req_events = poll_requested_events(wait);
  424. __poll_t res = v4l2_ctrl_poll(file, wait);
  425. poll_wait(file, &dev->read_queue, wait);
  426. if (dev->rdsstat == 0 && (req_events & (EPOLLIN | EPOLLRDNORM))) {
  427. mutex_lock(&dev->lock);
  428. if (dev->rdsstat == 0)
  429. cadet_start_rds(dev);
  430. mutex_unlock(&dev->lock);
  431. }
  432. if (cadet_has_rds_data(dev))
  433. res |= EPOLLIN | EPOLLRDNORM;
  434. return res;
  435. }
  436. static const struct v4l2_file_operations cadet_fops = {
  437. .owner = THIS_MODULE,
  438. .open = cadet_open,
  439. .release = cadet_release,
  440. .read = cadet_read,
  441. .unlocked_ioctl = video_ioctl2,
  442. .poll = cadet_poll,
  443. };
  444. static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
  445. .vidioc_querycap = vidioc_querycap,
  446. .vidioc_g_tuner = vidioc_g_tuner,
  447. .vidioc_s_tuner = vidioc_s_tuner,
  448. .vidioc_g_frequency = vidioc_g_frequency,
  449. .vidioc_s_frequency = vidioc_s_frequency,
  450. .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
  451. .vidioc_log_status = v4l2_ctrl_log_status,
  452. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  453. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  454. };
  455. static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
  456. .s_ctrl = cadet_s_ctrl,
  457. };
  458. #ifdef CONFIG_PNP
  459. static const struct pnp_device_id cadet_pnp_devices[] = {
  460. /* ADS Cadet AM/FM Radio Card */
  461. {.id = "MSM0c24", .driver_data = 0},
  462. {.id = ""}
  463. };
  464. MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
  465. static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
  466. {
  467. if (!dev)
  468. return -ENODEV;
  469. /* only support one device */
  470. if (io > 0)
  471. return -EBUSY;
  472. if (!pnp_port_valid(dev, 0))
  473. return -ENODEV;
  474. io = pnp_port_start(dev, 0);
  475. printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
  476. return io;
  477. }
  478. static struct pnp_driver cadet_pnp_driver = {
  479. .name = "radio-cadet",
  480. .id_table = cadet_pnp_devices,
  481. .probe = cadet_pnp_probe,
  482. .remove = NULL,
  483. };
  484. #else
  485. static struct pnp_driver cadet_pnp_driver;
  486. #endif
  487. static void cadet_probe(struct cadet *dev)
  488. {
  489. static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
  490. int i;
  491. for (i = 0; i < 8; i++) {
  492. dev->io = iovals[i];
  493. if (request_region(dev->io, 2, "cadet-probe")) {
  494. cadet_setfreq(dev, bands[1].rangelow);
  495. if (cadet_getfreq(dev) == bands[1].rangelow) {
  496. release_region(dev->io, 2);
  497. return;
  498. }
  499. release_region(dev->io, 2);
  500. }
  501. }
  502. dev->io = -1;
  503. }
  504. /*
  505. * io should only be set if the user has used something like
  506. * isapnp (the userspace program) to initialize this card for us
  507. */
  508. static int __init cadet_init(void)
  509. {
  510. struct cadet *dev = &cadet_card;
  511. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  512. struct v4l2_ctrl_handler *hdl;
  513. int res = -ENODEV;
  514. strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
  515. mutex_init(&dev->lock);
  516. /* If a probe was requested then probe ISAPnP first (safest) */
  517. if (io < 0)
  518. pnp_register_driver(&cadet_pnp_driver);
  519. dev->io = io;
  520. /* If that fails then probe unsafely if probe is requested */
  521. if (dev->io < 0)
  522. cadet_probe(dev);
  523. /* Else we bail out */
  524. if (dev->io < 0) {
  525. #ifdef MODULE
  526. v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
  527. v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
  528. #endif
  529. goto fail;
  530. }
  531. if (!request_region(dev->io, 2, "cadet"))
  532. goto fail;
  533. res = v4l2_device_register(NULL, v4l2_dev);
  534. if (res < 0) {
  535. release_region(dev->io, 2);
  536. v4l2_err(v4l2_dev, "could not register v4l2_device\n");
  537. goto fail;
  538. }
  539. hdl = &dev->ctrl_handler;
  540. v4l2_ctrl_handler_init(hdl, 2);
  541. v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
  542. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  543. v4l2_dev->ctrl_handler = hdl;
  544. if (hdl->error) {
  545. res = hdl->error;
  546. v4l2_err(v4l2_dev, "Could not register controls\n");
  547. goto err_hdl;
  548. }
  549. dev->is_fm_band = true;
  550. dev->curfreq = bands[dev->is_fm_band].rangelow;
  551. cadet_setfreq(dev, dev->curfreq);
  552. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  553. dev->vdev.v4l2_dev = v4l2_dev;
  554. dev->vdev.fops = &cadet_fops;
  555. dev->vdev.ioctl_ops = &cadet_ioctl_ops;
  556. dev->vdev.release = video_device_release_empty;
  557. dev->vdev.lock = &dev->lock;
  558. video_set_drvdata(&dev->vdev, dev);
  559. res = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
  560. if (res < 0)
  561. goto err_hdl;
  562. v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
  563. return 0;
  564. err_hdl:
  565. v4l2_ctrl_handler_free(hdl);
  566. v4l2_device_unregister(v4l2_dev);
  567. release_region(dev->io, 2);
  568. fail:
  569. pnp_unregister_driver(&cadet_pnp_driver);
  570. return res;
  571. }
  572. static void __exit cadet_exit(void)
  573. {
  574. struct cadet *dev = &cadet_card;
  575. video_unregister_device(&dev->vdev);
  576. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  577. v4l2_device_unregister(&dev->v4l2_dev);
  578. outb(7, dev->io); /* Mute */
  579. outb(0x00, dev->io + 1);
  580. release_region(dev->io, 2);
  581. pnp_unregister_driver(&cadet_pnp_driver);
  582. }
  583. module_init(cadet_init);
  584. module_exit(cadet_exit);