radio-cadet.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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/version.h>
  33. #include <linux/module.h> /* Modules */
  34. #include <linux/init.h> /* Initdata */
  35. #include <linux/ioport.h> /* request_region */
  36. #include <linux/delay.h> /* udelay */
  37. #include <linux/videodev2.h> /* V4L2 API defs */
  38. #include <linux/param.h>
  39. #include <linux/pnp.h>
  40. #include <linux/sched.h>
  41. #include <linux/io.h> /* outb, outb_p */
  42. #include <media/v4l2-device.h>
  43. #include <media/v4l2-ioctl.h>
  44. MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  45. MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
  46. MODULE_LICENSE("GPL");
  47. static int io = -1; /* default to isapnp activation */
  48. static int radio_nr = -1;
  49. module_param(io, int, 0);
  50. MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
  51. module_param(radio_nr, int, 0);
  52. #define CADET_VERSION KERNEL_VERSION(0, 3, 3)
  53. #define RDS_BUFFER 256
  54. #define RDS_RX_FLAG 1
  55. #define MBS_RX_FLAG 2
  56. struct cadet {
  57. struct v4l2_device v4l2_dev;
  58. struct video_device vdev;
  59. int io;
  60. int users;
  61. int curtuner;
  62. int tunestat;
  63. int sigstrength;
  64. wait_queue_head_t read_queue;
  65. struct timer_list readtimer;
  66. __u8 rdsin, rdsout, rdsstat;
  67. unsigned char rdsbuf[RDS_BUFFER];
  68. struct mutex lock;
  69. int reading;
  70. };
  71. static struct cadet cadet_card;
  72. /*
  73. * Signal Strength Threshold Values
  74. * The V4L API spec does not define any particular unit for the signal
  75. * strength value. These values are in microvolts of RF at the tuner's input.
  76. */
  77. static __u16 sigtable[2][4] = {
  78. { 5, 10, 30, 150 },
  79. { 28, 40, 63, 1000 }
  80. };
  81. static int cadet_getstereo(struct cadet *dev)
  82. {
  83. int ret = V4L2_TUNER_SUB_MONO;
  84. if (dev->curtuner != 0) /* Only FM has stereo capability! */
  85. return V4L2_TUNER_SUB_MONO;
  86. mutex_lock(&dev->lock);
  87. outb(7, dev->io); /* Select tuner control */
  88. if ((inb(dev->io + 1) & 0x40) == 0)
  89. ret = V4L2_TUNER_SUB_STEREO;
  90. mutex_unlock(&dev->lock);
  91. return ret;
  92. }
  93. static unsigned cadet_gettune(struct cadet *dev)
  94. {
  95. int curvol, i;
  96. unsigned fifo = 0;
  97. /*
  98. * Prepare for read
  99. */
  100. mutex_lock(&dev->lock);
  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. mutex_unlock(&dev->lock);
  121. return fifo;
  122. }
  123. static unsigned cadet_getfreq(struct cadet *dev)
  124. {
  125. int i;
  126. unsigned freq = 0, test, fifo = 0;
  127. /*
  128. * Read current tuning
  129. */
  130. fifo = cadet_gettune(dev);
  131. /*
  132. * Convert to actual frequency
  133. */
  134. if (dev->curtuner == 0) { /* FM */
  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) / 1000000; /* Make it 1/16 MHz */
  144. }
  145. if (dev->curtuner == 1) /* AM */
  146. freq = ((fifo & 0x7fff) - 2010) * 16;
  147. return freq;
  148. }
  149. static void cadet_settune(struct cadet *dev, unsigned fifo)
  150. {
  151. int i;
  152. unsigned test;
  153. mutex_lock(&dev->lock);
  154. outb(7, dev->io); /* Select tuner control */
  155. /*
  156. * Write the shift register
  157. */
  158. test = 0;
  159. test = (fifo >> 23) & 0x02; /* Align data for SDO */
  160. test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
  161. outb(7, dev->io); /* Select tuner control */
  162. outb(test, dev->io + 1); /* Initialize for write */
  163. for (i = 0; i < 25; i++) {
  164. test |= 0x01; /* Toggle SCK High */
  165. outb(test, dev->io + 1);
  166. test &= 0xfe; /* Toggle SCK Low */
  167. outb(test, dev->io + 1);
  168. fifo = fifo << 1; /* Prepare the next bit */
  169. test = 0x1c | ((fifo >> 23) & 0x02);
  170. outb(test, dev->io + 1);
  171. }
  172. mutex_unlock(&dev->lock);
  173. }
  174. static void cadet_setfreq(struct cadet *dev, unsigned freq)
  175. {
  176. unsigned fifo;
  177. int i, j, test;
  178. int curvol;
  179. /*
  180. * Formulate a fifo command
  181. */
  182. fifo = 0;
  183. if (dev->curtuner == 0) { /* FM */
  184. test = 102400;
  185. freq = (freq * 1000) / 16; /* Make it kHz */
  186. freq += 10700; /* IF is 10700 kHz */
  187. for (i = 0; i < 14; i++) {
  188. fifo = fifo << 1;
  189. if (freq >= test) {
  190. fifo |= 0x01;
  191. freq -= test;
  192. }
  193. test = test >> 1;
  194. }
  195. }
  196. if (dev->curtuner == 1) { /* AM */
  197. fifo = (freq / 16) + 2010; /* Make it kHz */
  198. fifo |= 0x100000; /* Select AM Band */
  199. }
  200. /*
  201. * Save current volume/mute setting
  202. */
  203. mutex_lock(&dev->lock);
  204. outb(7, dev->io); /* Select tuner control */
  205. curvol = inb(dev->io + 1);
  206. mutex_unlock(&dev->lock);
  207. /*
  208. * Tune the card
  209. */
  210. for (j = 3; j > -1; j--) {
  211. cadet_settune(dev, fifo | (j << 16));
  212. mutex_lock(&dev->lock);
  213. outb(7, dev->io); /* Select tuner control */
  214. outb(curvol, dev->io + 1);
  215. mutex_unlock(&dev->lock);
  216. msleep(100);
  217. cadet_gettune(dev);
  218. if ((dev->tunestat & 0x40) == 0) { /* Tuned */
  219. dev->sigstrength = sigtable[dev->curtuner][j];
  220. return;
  221. }
  222. }
  223. dev->sigstrength = 0;
  224. }
  225. static int cadet_getvol(struct cadet *dev)
  226. {
  227. int ret = 0;
  228. mutex_lock(&dev->lock);
  229. outb(7, dev->io); /* Select tuner control */
  230. if ((inb(dev->io + 1) & 0x20) != 0)
  231. ret = 0xffff;
  232. mutex_unlock(&dev->lock);
  233. return ret;
  234. }
  235. static void cadet_setvol(struct cadet *dev, int vol)
  236. {
  237. mutex_lock(&dev->lock);
  238. outb(7, dev->io); /* Select tuner control */
  239. if (vol > 0)
  240. outb(0x20, dev->io + 1);
  241. else
  242. outb(0x00, dev->io + 1);
  243. mutex_unlock(&dev->lock);
  244. }
  245. static void cadet_handler(unsigned long data)
  246. {
  247. struct cadet *dev = (void *)data;
  248. /* Service the RDS fifo */
  249. if (mutex_trylock(&dev->lock)) {
  250. outb(0x3, dev->io); /* Select RDS Decoder Control */
  251. if ((inb(dev->io + 1) & 0x20) != 0)
  252. printk(KERN_CRIT "cadet: RDS fifo overflow\n");
  253. outb(0x80, dev->io); /* Select RDS fifo */
  254. while ((inb(dev->io) & 0x80) != 0) {
  255. dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
  256. if (dev->rdsin == dev->rdsout)
  257. printk(KERN_WARNING "cadet: RDS buffer overflow\n");
  258. else
  259. dev->rdsin++;
  260. }
  261. mutex_unlock(&dev->lock);
  262. }
  263. /*
  264. * Service pending read
  265. */
  266. if (dev->rdsin != dev->rdsout)
  267. wake_up_interruptible(&dev->read_queue);
  268. /*
  269. * Clean up and exit
  270. */
  271. init_timer(&dev->readtimer);
  272. dev->readtimer.function = cadet_handler;
  273. dev->readtimer.data = (unsigned long)0;
  274. dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
  275. add_timer(&dev->readtimer);
  276. }
  277. static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  278. {
  279. struct cadet *dev = video_drvdata(file);
  280. unsigned char readbuf[RDS_BUFFER];
  281. int i = 0;
  282. mutex_lock(&dev->lock);
  283. if (dev->rdsstat == 0) {
  284. dev->rdsstat = 1;
  285. outb(0x80, dev->io); /* Select RDS fifo */
  286. init_timer(&dev->readtimer);
  287. dev->readtimer.function = cadet_handler;
  288. dev->readtimer.data = (unsigned long)dev;
  289. dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
  290. add_timer(&dev->readtimer);
  291. }
  292. if (dev->rdsin == dev->rdsout) {
  293. mutex_unlock(&dev->lock);
  294. if (file->f_flags & O_NONBLOCK)
  295. return -EWOULDBLOCK;
  296. interruptible_sleep_on(&dev->read_queue);
  297. mutex_lock(&dev->lock);
  298. }
  299. while (i < count && dev->rdsin != dev->rdsout)
  300. readbuf[i++] = dev->rdsbuf[dev->rdsout++];
  301. mutex_unlock(&dev->lock);
  302. if (copy_to_user(data, readbuf, i))
  303. return -EFAULT;
  304. return i;
  305. }
  306. static int vidioc_querycap(struct file *file, void *priv,
  307. struct v4l2_capability *v)
  308. {
  309. strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
  310. strlcpy(v->card, "ADS Cadet", sizeof(v->card));
  311. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  312. v->version = CADET_VERSION;
  313. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
  314. V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
  315. return 0;
  316. }
  317. static int vidioc_g_tuner(struct file *file, void *priv,
  318. struct v4l2_tuner *v)
  319. {
  320. struct cadet *dev = video_drvdata(file);
  321. v->type = V4L2_TUNER_RADIO;
  322. switch (v->index) {
  323. case 0:
  324. strlcpy(v->name, "FM", sizeof(v->name));
  325. v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
  326. V4L2_TUNER_CAP_RDS_BLOCK_IO;
  327. v->rangelow = 1400; /* 87.5 MHz */
  328. v->rangehigh = 1728; /* 108.0 MHz */
  329. v->rxsubchans = cadet_getstereo(dev);
  330. switch (v->rxsubchans) {
  331. case V4L2_TUNER_SUB_MONO:
  332. v->audmode = V4L2_TUNER_MODE_MONO;
  333. break;
  334. case V4L2_TUNER_SUB_STEREO:
  335. v->audmode = V4L2_TUNER_MODE_STEREO;
  336. break;
  337. default:
  338. break;
  339. }
  340. v->rxsubchans |= V4L2_TUNER_SUB_RDS;
  341. break;
  342. case 1:
  343. strlcpy(v->name, "AM", sizeof(v->name));
  344. v->capability = V4L2_TUNER_CAP_LOW;
  345. v->rangelow = 8320; /* 520 kHz */
  346. v->rangehigh = 26400; /* 1650 kHz */
  347. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  348. v->audmode = V4L2_TUNER_MODE_MONO;
  349. break;
  350. default:
  351. return -EINVAL;
  352. }
  353. v->signal = dev->sigstrength; /* We might need to modify scaling of this */
  354. return 0;
  355. }
  356. static int vidioc_s_tuner(struct file *file, void *priv,
  357. struct v4l2_tuner *v)
  358. {
  359. struct cadet *dev = video_drvdata(file);
  360. if (v->index != 0 && v->index != 1)
  361. return -EINVAL;
  362. dev->curtuner = v->index;
  363. return 0;
  364. }
  365. static int vidioc_g_frequency(struct file *file, void *priv,
  366. struct v4l2_frequency *f)
  367. {
  368. struct cadet *dev = video_drvdata(file);
  369. f->tuner = dev->curtuner;
  370. f->type = V4L2_TUNER_RADIO;
  371. f->frequency = cadet_getfreq(dev);
  372. return 0;
  373. }
  374. static int vidioc_s_frequency(struct file *file, void *priv,
  375. struct v4l2_frequency *f)
  376. {
  377. struct cadet *dev = video_drvdata(file);
  378. if (f->type != V4L2_TUNER_RADIO)
  379. return -EINVAL;
  380. if (dev->curtuner == 0 && (f->frequency < 1400 || f->frequency > 1728))
  381. return -EINVAL;
  382. if (dev->curtuner == 1 && (f->frequency < 8320 || f->frequency > 26400))
  383. return -EINVAL;
  384. cadet_setfreq(dev, f->frequency);
  385. return 0;
  386. }
  387. static int vidioc_queryctrl(struct file *file, void *priv,
  388. struct v4l2_queryctrl *qc)
  389. {
  390. switch (qc->id) {
  391. case V4L2_CID_AUDIO_MUTE:
  392. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  393. case V4L2_CID_AUDIO_VOLUME:
  394. return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
  395. }
  396. return -EINVAL;
  397. }
  398. static int vidioc_g_ctrl(struct file *file, void *priv,
  399. struct v4l2_control *ctrl)
  400. {
  401. struct cadet *dev = video_drvdata(file);
  402. switch (ctrl->id) {
  403. case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
  404. ctrl->value = (cadet_getvol(dev) == 0);
  405. break;
  406. case V4L2_CID_AUDIO_VOLUME:
  407. ctrl->value = cadet_getvol(dev);
  408. break;
  409. default:
  410. return -EINVAL;
  411. }
  412. return 0;
  413. }
  414. static int vidioc_s_ctrl(struct file *file, void *priv,
  415. struct v4l2_control *ctrl)
  416. {
  417. struct cadet *dev = video_drvdata(file);
  418. switch (ctrl->id){
  419. case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
  420. if (ctrl->value)
  421. cadet_setvol(dev, 0);
  422. else
  423. cadet_setvol(dev, 0xffff);
  424. break;
  425. case V4L2_CID_AUDIO_VOLUME:
  426. cadet_setvol(dev, ctrl->value);
  427. break;
  428. default:
  429. return -EINVAL;
  430. }
  431. return 0;
  432. }
  433. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  434. {
  435. *i = 0;
  436. return 0;
  437. }
  438. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  439. {
  440. return i ? -EINVAL : 0;
  441. }
  442. static int vidioc_g_audio(struct file *file, void *priv,
  443. struct v4l2_audio *a)
  444. {
  445. a->index = 0;
  446. strlcpy(a->name, "Radio", sizeof(a->name));
  447. a->capability = V4L2_AUDCAP_STEREO;
  448. return 0;
  449. }
  450. static int vidioc_s_audio(struct file *file, void *priv,
  451. struct v4l2_audio *a)
  452. {
  453. return a->index ? -EINVAL : 0;
  454. }
  455. static int cadet_open(struct file *file)
  456. {
  457. struct cadet *dev = video_drvdata(file);
  458. mutex_lock(&dev->lock);
  459. dev->users++;
  460. if (1 == dev->users)
  461. init_waitqueue_head(&dev->read_queue);
  462. mutex_unlock(&dev->lock);
  463. return 0;
  464. }
  465. static int cadet_release(struct file *file)
  466. {
  467. struct cadet *dev = video_drvdata(file);
  468. mutex_lock(&dev->lock);
  469. dev->users--;
  470. if (0 == dev->users) {
  471. del_timer_sync(&dev->readtimer);
  472. dev->rdsstat = 0;
  473. }
  474. mutex_unlock(&dev->lock);
  475. return 0;
  476. }
  477. static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
  478. {
  479. struct cadet *dev = video_drvdata(file);
  480. poll_wait(file, &dev->read_queue, wait);
  481. if (dev->rdsin != dev->rdsout)
  482. return POLLIN | POLLRDNORM;
  483. return 0;
  484. }
  485. static const struct v4l2_file_operations cadet_fops = {
  486. .owner = THIS_MODULE,
  487. .open = cadet_open,
  488. .release = cadet_release,
  489. .read = cadet_read,
  490. .unlocked_ioctl = video_ioctl2,
  491. .poll = cadet_poll,
  492. };
  493. static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
  494. .vidioc_querycap = vidioc_querycap,
  495. .vidioc_g_tuner = vidioc_g_tuner,
  496. .vidioc_s_tuner = vidioc_s_tuner,
  497. .vidioc_g_frequency = vidioc_g_frequency,
  498. .vidioc_s_frequency = vidioc_s_frequency,
  499. .vidioc_queryctrl = vidioc_queryctrl,
  500. .vidioc_g_ctrl = vidioc_g_ctrl,
  501. .vidioc_s_ctrl = vidioc_s_ctrl,
  502. .vidioc_g_audio = vidioc_g_audio,
  503. .vidioc_s_audio = vidioc_s_audio,
  504. .vidioc_g_input = vidioc_g_input,
  505. .vidioc_s_input = vidioc_s_input,
  506. };
  507. #ifdef CONFIG_PNP
  508. static struct pnp_device_id cadet_pnp_devices[] = {
  509. /* ADS Cadet AM/FM Radio Card */
  510. {.id = "MSM0c24", .driver_data = 0},
  511. {.id = ""}
  512. };
  513. MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
  514. static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
  515. {
  516. if (!dev)
  517. return -ENODEV;
  518. /* only support one device */
  519. if (io > 0)
  520. return -EBUSY;
  521. if (!pnp_port_valid(dev, 0))
  522. return -ENODEV;
  523. io = pnp_port_start(dev, 0);
  524. printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
  525. return io;
  526. }
  527. static struct pnp_driver cadet_pnp_driver = {
  528. .name = "radio-cadet",
  529. .id_table = cadet_pnp_devices,
  530. .probe = cadet_pnp_probe,
  531. .remove = NULL,
  532. };
  533. #else
  534. static struct pnp_driver cadet_pnp_driver;
  535. #endif
  536. static void cadet_probe(struct cadet *dev)
  537. {
  538. static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
  539. int i;
  540. for (i = 0; i < 8; i++) {
  541. dev->io = iovals[i];
  542. if (request_region(dev->io, 2, "cadet-probe")) {
  543. cadet_setfreq(dev, 1410);
  544. if (cadet_getfreq(dev) == 1410) {
  545. release_region(dev->io, 2);
  546. return;
  547. }
  548. release_region(dev->io, 2);
  549. }
  550. }
  551. dev->io = -1;
  552. }
  553. /*
  554. * io should only be set if the user has used something like
  555. * isapnp (the userspace program) to initialize this card for us
  556. */
  557. static int __init cadet_init(void)
  558. {
  559. struct cadet *dev = &cadet_card;
  560. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  561. int res;
  562. strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
  563. mutex_init(&dev->lock);
  564. /* If a probe was requested then probe ISAPnP first (safest) */
  565. if (io < 0)
  566. pnp_register_driver(&cadet_pnp_driver);
  567. dev->io = io;
  568. /* If that fails then probe unsafely if probe is requested */
  569. if (dev->io < 0)
  570. cadet_probe(dev);
  571. /* Else we bail out */
  572. if (dev->io < 0) {
  573. #ifdef MODULE
  574. v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
  575. v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
  576. #endif
  577. goto fail;
  578. }
  579. if (!request_region(dev->io, 2, "cadet"))
  580. goto fail;
  581. res = v4l2_device_register(NULL, v4l2_dev);
  582. if (res < 0) {
  583. release_region(dev->io, 2);
  584. v4l2_err(v4l2_dev, "could not register v4l2_device\n");
  585. goto fail;
  586. }
  587. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  588. dev->vdev.v4l2_dev = v4l2_dev;
  589. dev->vdev.fops = &cadet_fops;
  590. dev->vdev.ioctl_ops = &cadet_ioctl_ops;
  591. dev->vdev.release = video_device_release_empty;
  592. video_set_drvdata(&dev->vdev, dev);
  593. if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  594. v4l2_device_unregister(v4l2_dev);
  595. release_region(dev->io, 2);
  596. goto fail;
  597. }
  598. v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
  599. return 0;
  600. fail:
  601. pnp_unregister_driver(&cadet_pnp_driver);
  602. return -ENODEV;
  603. }
  604. static void __exit cadet_exit(void)
  605. {
  606. struct cadet *dev = &cadet_card;
  607. video_unregister_device(&dev->vdev);
  608. v4l2_device_unregister(&dev->v4l2_dev);
  609. release_region(dev->io, 2);
  610. pnp_unregister_driver(&cadet_pnp_driver);
  611. }
  612. module_init(cadet_init);
  613. module_exit(cadet_exit);