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