Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* SF16FMR2 radio driver for Linux radio support |
| 2 | * heavily based on fmi driver... |
| 3 | * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com |
| 4 | * |
| 5 | * Notes on the hardware |
| 6 | * |
| 7 | * Frequency control is done digitally -- ie out(port,encodefreq(95.8)); |
| 8 | * No volume control - only mute/unmute - you have to use line volume |
| 9 | * |
| 10 | * For read stereo/mono you must wait 0.1 sec after set frequency and |
| 11 | * card unmuted so I set frequency on unmute |
| 12 | * Signal handling seem to work only on autoscanning (not implemented) |
Mauro Carvalho Chehab | acda0e7 | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 13 | * |
| 14 | * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> /* Modules */ |
| 18 | #include <linux/init.h> /* Initdata */ |
Peter Osterlund | fb911ee | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 19 | #include <linux/ioport.h> /* request_region */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/delay.h> /* udelay */ |
Mauro Carvalho Chehab | acda0e7 | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 21 | #include <linux/videodev2.h> /* kernel radio structs */ |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Mauro Carvalho Chehab | 1ddf5bc | 2006-08-08 09:10:06 -0300 | [diff] [blame] | 23 | #include <linux/version.h> /* for KERNEL_VERSION MACRO */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 24 | #include <linux/io.h> /* outb, outb_p */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 25 | #include <media/v4l2-device.h> |
| 26 | #include <media/v4l2-ioctl.h> |
| 27 | |
| 28 | MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com"); |
| 29 | MODULE_DESCRIPTION("A driver for the SF16FMR2 radio."); |
| 30 | MODULE_LICENSE("GPL"); |
| 31 | |
| 32 | static int io = 0x384; |
| 33 | static int radio_nr = -1; |
| 34 | |
| 35 | module_param(io, int, 0); |
| 36 | MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)"); |
| 37 | module_param(radio_nr, int, 0); |
| 38 | |
Mauro Carvalho Chehab | acda0e7 | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 39 | #define RADIO_VERSION KERNEL_VERSION(0,0,2) |
| 40 | |
Mauro Carvalho Chehab | b2cb200 | 2008-04-22 14:46:03 -0300 | [diff] [blame] | 41 | #define AUD_VOL_INDEX 1 |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #undef DEBUG |
| 44 | //#define DEBUG 1 |
| 45 | |
| 46 | #ifdef DEBUG |
| 47 | # define debug_print(s) printk s |
| 48 | #else |
| 49 | # define debug_print(s) |
| 50 | #endif |
| 51 | |
| 52 | /* this should be static vars for module size */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 53 | struct fmr2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 55 | struct v4l2_device v4l2_dev; |
| 56 | struct video_device vdev; |
| 57 | struct mutex lock; |
| 58 | int io; |
Mauro Carvalho Chehab | b2cb200 | 2008-04-22 14:46:03 -0300 | [diff] [blame] | 59 | int curvol; /* 0-15 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | int mute; |
| 61 | int stereo; /* card is producing stereo audio */ |
| 62 | unsigned long curfreq; /* freq in kHz */ |
| 63 | int card_type; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 64 | u32 flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 67 | static struct fmr2 fmr2_card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | /* hw precision is 12.5 kHz |
Adrian Bunk | a58a414 | 2006-01-10 00:08:17 +0100 | [diff] [blame] | 70 | * It is only useful to give freq in intervall of 200 (=0.0125Mhz), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | * other bits will be truncated |
| 72 | */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 73 | #define RSF16_ENCODE(x) ((x) / 200 + 856) |
| 74 | #define RSF16_MINFREQ (87 * 16000) |
| 75 | #define RSF16_MAXFREQ (108 * 16000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 77 | static inline void wait(int n, int io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 79 | for (; n; --n) |
| 80 | inb(io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 83 | static void outbits(int bits, unsigned int data, int nWait, int io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | int bit; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 86 | |
| 87 | for (; --bits >= 0;) { |
| 88 | bit = (data >> bits) & 1; |
| 89 | outb(bit, io); |
| 90 | wait(nWait, io); |
| 91 | outb(bit | 2, io); |
| 92 | wait(nWait, io); |
| 93 | outb(bit, io); |
| 94 | wait(nWait, io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 98 | static inline void fmr2_mute(int io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 100 | outb(0x00, io); |
| 101 | wait(4, io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 104 | static inline void fmr2_unmute(int io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 106 | outb(0x04, io); |
| 107 | wait(4, io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 110 | static inline int fmr2_stereo_mode(int io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 112 | int n = inb(io); |
| 113 | |
| 114 | outb(6, io); |
| 115 | inb(io); |
| 116 | n = ((n >> 3) & 1) ^ 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | debug_print((KERN_DEBUG "stereo: %d\n", n)); |
| 118 | return n; |
| 119 | } |
| 120 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 121 | static int fmr2_product_info(struct fmr2 *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 123 | int n = inb(dev->io); |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | n &= 0xC1; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 126 | if (n == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* this should support volume set */ |
| 128 | dev->card_type = 12; |
| 129 | return 0; |
| 130 | } |
| 131 | /* not volume (mine is 11) */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 132 | dev->card_type = (n == 128) ? 11 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return n; |
| 134 | } |
| 135 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 136 | static inline int fmr2_getsigstr(struct fmr2 *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 138 | /* !!! works only if scanning freq */ |
| 139 | int res = 0xffff; |
| 140 | |
| 141 | outb(5, dev->io); |
| 142 | wait(4, dev->io); |
| 143 | if (!(inb(dev->io) & 1)) |
| 144 | res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | debug_print((KERN_DEBUG "signal: %d\n", res)); |
| 146 | return res; |
| 147 | } |
| 148 | |
| 149 | /* set frequency and unmute card */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 150 | static int fmr2_setfreq(struct fmr2 *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | unsigned long freq = dev->curfreq; |
| 153 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 154 | fmr2_mute(dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | /* 0x42 for mono output |
| 157 | * 0x102 forward scanning |
| 158 | * 0x182 scansione avanti |
| 159 | */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 160 | outbits(9, 0x2, 3, dev->io); |
| 161 | outbits(16, RSF16_ENCODE(freq), 2, dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 163 | fmr2_unmute(dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* wait 0.11 sec */ |
| 166 | msleep(110); |
| 167 | |
| 168 | /* NOTE if mute this stop radio |
| 169 | you must set freq on unmute */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 170 | dev->stereo = fmr2_stereo_mode(dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* !!! not tested, in my card this does't work !!! */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 175 | static int fmr2_setvolume(struct fmr2 *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
Mauro Carvalho Chehab | b2cb200 | 2008-04-22 14:46:03 -0300 | [diff] [blame] | 177 | int vol[16] = { 0x021, 0x084, 0x090, 0x104, |
| 178 | 0x110, 0x204, 0x210, 0x402, |
| 179 | 0x404, 0x408, 0x410, 0x801, |
| 180 | 0x802, 0x804, 0x808, 0x810 }; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 181 | int i, a; |
Mauro Carvalho Chehab | b2cb200 | 2008-04-22 14:46:03 -0300 | [diff] [blame] | 182 | int n = vol[dev->curvol & 0x0f]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Mauro Carvalho Chehab | b2cb200 | 2008-04-22 14:46:03 -0300 | [diff] [blame] | 184 | if (dev->card_type != 11) |
| 185 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Mauro Carvalho Chehab | b2cb200 | 2008-04-22 14:46:03 -0300 | [diff] [blame] | 187 | for (i = 12; --i >= 0; ) { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 188 | a = ((n >> i) & 1) << 6; /* if (a==0) a = 0; else a = 0x40; */ |
| 189 | outb(a | 4, dev->io); |
| 190 | wait(4, dev->io); |
| 191 | outb(a | 0x24, dev->io); |
| 192 | wait(4, dev->io); |
| 193 | outb(a | 4, dev->io); |
| 194 | wait(4, dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
Mauro Carvalho Chehab | b2cb200 | 2008-04-22 14:46:03 -0300 | [diff] [blame] | 196 | for (i = 6; --i >= 0; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | a = ((0x18 >> i) & 1) << 6; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 198 | outb(a | 4, dev->io); |
| 199 | wait(4, dev->io); |
| 200 | outb(a | 0x24, dev->io); |
| 201 | wait(4, dev->io); |
| 202 | outb(a | 4, dev->io); |
| 203 | wait(4, dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 205 | wait(4, dev->io); |
| 206 | outb(0x14, dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 210 | static int vidioc_querycap(struct file *file, void *priv, |
| 211 | struct v4l2_capability *v) |
| 212 | { |
| 213 | strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver)); |
| 214 | strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card)); |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 215 | strlcpy(v->bus_info, "ISA", sizeof(v->bus_info)); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 216 | v->version = RADIO_VERSION; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 217 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 222 | struct v4l2_tuner *v) |
| 223 | { |
| 224 | int mult; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 225 | struct fmr2 *fmr2 = video_drvdata(file); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 226 | |
| 227 | if (v->index > 0) |
| 228 | return -EINVAL; |
| 229 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 230 | strlcpy(v->name, "FM", sizeof(v->name)); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 231 | v->type = V4L2_TUNER_RADIO; |
| 232 | |
| 233 | mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 234 | v->rangelow = RSF16_MINFREQ / mult; |
| 235 | v->rangehigh = RSF16_MAXFREQ / mult; |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 236 | v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO; |
| 237 | v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW; |
| 238 | v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO: |
| 239 | V4L2_TUNER_MODE_MONO; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 240 | mutex_lock(&fmr2->lock); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 241 | v->signal = fmr2_getsigstr(fmr2); |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 242 | mutex_unlock(&fmr2->lock); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 247 | struct v4l2_tuner *v) |
| 248 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 249 | return v->index ? -EINVAL : 0; |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 253 | struct v4l2_frequency *f) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 255 | struct fmr2 *fmr2 = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 257 | if (!(fmr2->flags & V4L2_TUNER_CAP_LOW)) |
| 258 | f->frequency *= 1000; |
| 259 | if (f->frequency < RSF16_MINFREQ || |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 260 | f->frequency > RSF16_MAXFREQ) |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 261 | return -EINVAL; |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 262 | /* rounding in steps of 200 to match the freq |
| 263 | that will be used */ |
| 264 | fmr2->curfreq = (f->frequency / 200) * 200; |
Mauro Carvalho Chehab | acda0e7 | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 265 | |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 266 | /* set card freq (if not muted) */ |
| 267 | if (fmr2->curvol && !fmr2->mute) { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 268 | mutex_lock(&fmr2->lock); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 269 | fmr2_setfreq(fmr2); |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 270 | mutex_unlock(&fmr2->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 272 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 275 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 276 | struct v4l2_frequency *f) |
| 277 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 278 | struct fmr2 *fmr2 = video_drvdata(file); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 279 | |
| 280 | f->type = V4L2_TUNER_RADIO; |
| 281 | f->frequency = fmr2->curfreq; |
| 282 | if (!(fmr2->flags & V4L2_TUNER_CAP_LOW)) |
| 283 | f->frequency /= 1000; |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 288 | struct v4l2_queryctrl *qc) |
| 289 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 290 | struct fmr2 *fmr2 = video_drvdata(file); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 291 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 292 | switch (qc->id) { |
| 293 | case V4L2_CID_AUDIO_MUTE: |
| 294 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); |
| 295 | case V4L2_CID_AUDIO_VOLUME: |
| 296 | /* Only card_type == 11 implements volume */ |
| 297 | if (fmr2->card_type == 11) |
| 298 | return v4l2_ctrl_query_fill(qc, 0, 15, 1, 0); |
| 299 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 300 | } |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | |
| 304 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 305 | struct v4l2_control *ctrl) |
| 306 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 307 | struct fmr2 *fmr2 = video_drvdata(file); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 308 | |
| 309 | switch (ctrl->id) { |
| 310 | case V4L2_CID_AUDIO_MUTE: |
| 311 | ctrl->value = fmr2->mute; |
| 312 | return 0; |
| 313 | case V4L2_CID_AUDIO_VOLUME: |
| 314 | ctrl->value = fmr2->curvol; |
| 315 | return 0; |
| 316 | } |
| 317 | return -EINVAL; |
| 318 | } |
| 319 | |
| 320 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 321 | struct v4l2_control *ctrl) |
| 322 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 323 | struct fmr2 *fmr2 = video_drvdata(file); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 324 | |
| 325 | switch (ctrl->id) { |
| 326 | case V4L2_CID_AUDIO_MUTE: |
| 327 | fmr2->mute = ctrl->value; |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 328 | break; |
| 329 | case V4L2_CID_AUDIO_VOLUME: |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 330 | fmr2->curvol = ctrl->value; |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 331 | break; |
| 332 | default: |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | |
| 336 | #ifdef DEBUG |
| 337 | if (fmr2->curvol && !fmr2->mute) |
| 338 | printk(KERN_DEBUG "unmute\n"); |
| 339 | else |
| 340 | printk(KERN_DEBUG "mute\n"); |
| 341 | #endif |
| 342 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 343 | mutex_lock(&fmr2->lock); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 344 | if (fmr2->curvol && !fmr2->mute) { |
| 345 | fmr2_setvolume(fmr2); |
Mauro Carvalho Chehab | b2cb200 | 2008-04-22 14:46:03 -0300 | [diff] [blame] | 346 | /* Set frequency and unmute card */ |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 347 | fmr2_setfreq(fmr2); |
| 348 | } else |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 349 | fmr2_mute(fmr2->io); |
| 350 | mutex_unlock(&fmr2->lock); |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
| 355 | { |
| 356 | *i = 0; |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
| 361 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 362 | return i ? -EINVAL : 0; |
| 363 | } |
| 364 | |
| 365 | static int vidioc_g_audio(struct file *file, void *priv, |
| 366 | struct v4l2_audio *a) |
| 367 | { |
| 368 | a->index = 0; |
| 369 | strlcpy(a->name, "Radio", sizeof(a->name)); |
| 370 | a->capability = V4L2_AUDCAP_STEREO; |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int vidioc_s_audio(struct file *file, void *priv, |
| 375 | struct v4l2_audio *a) |
| 376 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 377 | return a->index ? -EINVAL : 0; |
| 378 | } |
| 379 | |
| 380 | static int fmr2_open(struct file *file) |
| 381 | { |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 382 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 385 | static int fmr2_release(struct file *file) |
Hans Verkuil | 3ca685a | 2008-08-23 04:49:13 -0300 | [diff] [blame] | 386 | { |
Hans Verkuil | 3ca685a | 2008-08-23 04:49:13 -0300 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 390 | static const struct v4l2_file_operations fmr2_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | .owner = THIS_MODULE, |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 392 | .open = fmr2_open, |
| 393 | .release = fmr2_release, |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 394 | .ioctl = video_ioctl2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | }; |
| 396 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 397 | static const struct v4l2_ioctl_ops fmr2_ioctl_ops = { |
Douglas Landgraf | 34ab962 | 2007-04-23 17:51:37 -0300 | [diff] [blame] | 398 | .vidioc_querycap = vidioc_querycap, |
| 399 | .vidioc_g_tuner = vidioc_g_tuner, |
| 400 | .vidioc_s_tuner = vidioc_s_tuner, |
| 401 | .vidioc_g_audio = vidioc_g_audio, |
| 402 | .vidioc_s_audio = vidioc_s_audio, |
| 403 | .vidioc_g_input = vidioc_g_input, |
| 404 | .vidioc_s_input = vidioc_s_input, |
| 405 | .vidioc_g_frequency = vidioc_g_frequency, |
| 406 | .vidioc_s_frequency = vidioc_s_frequency, |
| 407 | .vidioc_queryctrl = vidioc_queryctrl, |
| 408 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 409 | .vidioc_s_ctrl = vidioc_s_ctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | static int __init fmr2_init(void) |
| 413 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 414 | struct fmr2 *fmr2 = &fmr2_card; |
| 415 | struct v4l2_device *v4l2_dev = &fmr2->v4l2_dev; |
| 416 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 418 | strlcpy(v4l2_dev->name, "sf16fmr2", sizeof(v4l2_dev->name)); |
| 419 | fmr2->io = io; |
| 420 | fmr2->stereo = 1; |
| 421 | fmr2->flags = V4L2_TUNER_CAP_LOW; |
| 422 | mutex_init(&fmr2->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 424 | if (!request_region(fmr2->io, 2, "sf16fmr2")) { |
| 425 | v4l2_err(v4l2_dev, "request_region failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return -EBUSY; |
| 427 | } |
| 428 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 429 | res = v4l2_device_register(NULL, v4l2_dev); |
| 430 | if (res < 0) { |
| 431 | release_region(fmr2->io, 2); |
| 432 | v4l2_err(v4l2_dev, "Could not register v4l2_device\n"); |
| 433 | return res; |
| 434 | } |
| 435 | |
| 436 | strlcpy(fmr2->vdev.name, v4l2_dev->name, sizeof(fmr2->vdev.name)); |
| 437 | fmr2->vdev.v4l2_dev = v4l2_dev; |
| 438 | fmr2->vdev.fops = &fmr2_fops; |
| 439 | fmr2->vdev.ioctl_ops = &fmr2_ioctl_ops; |
| 440 | fmr2->vdev.release = video_device_release_empty; |
| 441 | video_set_drvdata(&fmr2->vdev, fmr2); |
| 442 | |
| 443 | if (video_register_device(&fmr2->vdev, VFL_TYPE_RADIO, radio_nr) < 0) { |
| 444 | v4l2_device_unregister(v4l2_dev); |
| 445 | release_region(fmr2->io, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | return -EINVAL; |
| 447 | } |
| 448 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 449 | v4l2_info(v4l2_dev, "SF16FMR2 radio card driver at 0x%x.\n", fmr2->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | /* mute card - prevents noisy bootups */ |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 451 | mutex_lock(&fmr2->lock); |
| 452 | fmr2_mute(fmr2->io); |
| 453 | fmr2_product_info(fmr2); |
| 454 | mutex_unlock(&fmr2->lock); |
| 455 | debug_print((KERN_DEBUG "card_type %d\n", fmr2->card_type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 459 | static void __exit fmr2_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 461 | struct fmr2 *fmr2 = &fmr2_card; |
| 462 | |
| 463 | video_unregister_device(&fmr2->vdev); |
| 464 | v4l2_device_unregister(&fmr2->v4l2_dev); |
| 465 | release_region(fmr2->io, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | module_init(fmr2_init); |
Hans Verkuil | c32a9d7 | 2009-03-06 13:53:26 -0300 | [diff] [blame] | 469 | module_exit(fmr2_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | #ifndef MODULE |
| 472 | |
| 473 | static int __init fmr2_setup_io(char *str) |
| 474 | { |
| 475 | get_option(&str, &io); |
| 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | __setup("sf16fmr2=", fmr2_setup_io); |
| 480 | |
| 481 | #endif |