Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * (C) 2001 Dimitromanolakis Apostolos <apdim@grecian.net> |
| 4 | * |
| 5 | * Based in the radio Maestro PCI driver. Actually it uses the same chip |
| 6 | * for radio but different pci controller. |
| 7 | * |
| 8 | * I didn't have any specs I reversed engineered the protocol from |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 9 | * the windows driver (radio.dll). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * The card uses the TEA5757 chip that includes a search function but it |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 12 | * is useless as I haven't found any way to read back the frequency. If |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * anybody does please mail me. |
| 14 | * |
| 15 | * For the pdf file see: |
| 16 | * http://www.semiconductors.philips.com/pip/TEA5757H/V1 |
| 17 | * |
| 18 | * |
| 19 | * CHANGES: |
| 20 | * 0.75b |
| 21 | * - better pci interface thanks to Francois Romieu <romieu@cogenit.fr> |
| 22 | * |
Mauro Carvalho Chehab | e84fef6 | 2006-08-08 09:10:05 -0300 | [diff] [blame] | 23 | * 0.75 Sun Feb 4 22:51:27 EET 2001 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * - tiding up |
| 25 | * - removed support for multiple devices as it didn't work anyway |
| 26 | * |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 27 | * BUGS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * - card unmutes if you change frequency |
| 29 | * |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 30 | * (c) 2006, 2007 by Mauro Carvalho Chehab <mchehab@infradead.org>: |
| 31 | * - Conversion to V4L2 API |
| 32 | * - Uses video_ioctl2 for parsing and to add debug support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <linux/ioport.h> |
| 39 | #include <linux/delay.h> |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 40 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/pci.h> |
Mauro Carvalho Chehab | e84fef6 | 2006-08-08 09:10:05 -0300 | [diff] [blame] | 42 | #include <linux/videodev2.h> |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 43 | #include <linux/version.h> /* for KERNEL_VERSION MACRO */ |
| 44 | #include <linux/io.h> |
| 45 | #include <linux/uaccess.h> |
| 46 | #include <media/v4l2-device.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 47 | #include <media/v4l2-ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 49 | MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net"); |
| 50 | MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio."); |
| 51 | MODULE_LICENSE("GPL"); |
| 52 | |
| 53 | static int radio_nr = -1; |
| 54 | module_param(radio_nr, int, 0); |
| 55 | |
| 56 | static int debug; |
| 57 | |
| 58 | module_param(debug, int, 0644); |
| 59 | MODULE_PARM_DESC(debug, "activates debug info"); |
| 60 | |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 61 | #define DRIVER_VERSION "0.77" |
Mauro Carvalho Chehab | e84fef6 | 2006-08-08 09:10:05 -0300 | [diff] [blame] | 62 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 63 | #define RADIO_VERSION KERNEL_VERSION(0, 7, 7) |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 64 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 65 | #define dprintk(dev, num, fmt, arg...) \ |
| 66 | v4l2_dbg(num, debug, &dev->v4l2_dev, fmt, ## arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | #ifndef PCI_VENDOR_ID_GUILLEMOT |
| 69 | #define PCI_VENDOR_ID_GUILLEMOT 0x5046 |
| 70 | #endif |
| 71 | |
| 72 | #ifndef PCI_DEVICE_ID_GUILLEMOT |
| 73 | #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001 |
| 74 | #endif |
| 75 | |
| 76 | |
| 77 | /* TEA5757 pin mappings */ |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 78 | static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 80 | #define FREQ_LO (50 * 16000) |
| 81 | #define FREQ_HI (150 * 16000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | #define FREQ_IF 171200 /* 10.7*16000 */ |
| 84 | #define FREQ_STEP 200 /* 12.5*16 */ |
| 85 | |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 86 | /* (x==fmhz*16*1000) -> bits */ |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 87 | #define FREQ2BITS(x) \ |
| 88 | ((((unsigned int)(x) + FREQ_IF + (FREQ_STEP << 1)) / (FREQ_STEP << 2)) << 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF) |
| 91 | |
| 92 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 93 | struct maxiradio |
Hans Verkuil | 3ca685a | 2008-08-23 04:49:13 -0300 | [diff] [blame] | 94 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 95 | struct v4l2_device v4l2_dev; |
| 96 | struct video_device vdev; |
| 97 | struct pci_dev *pdev; |
Hans Verkuil | 3ca685a | 2008-08-23 04:49:13 -0300 | [diff] [blame] | 98 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 99 | u16 io; /* base of radio io */ |
| 100 | u16 muted; /* VIDEO_AUDIO_MUTE */ |
| 101 | u16 stereo; /* VIDEO_TUNER_STEREO_ON */ |
| 102 | u16 tuned; /* signal strength (0 or 0xffff) */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | unsigned long freq; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 105 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 106 | struct mutex lock; |
Mauro Carvalho Chehab | 712642b | 2007-01-26 07:33:07 -0300 | [diff] [blame] | 107 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 109 | static inline struct maxiradio *to_maxiradio(struct v4l2_device *v4l2_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 111 | return container_of(v4l2_dev, struct maxiradio, v4l2_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 114 | static void outbit(unsigned long bit, u16 io) |
| 115 | { |
| 116 | int val = power | wren | (bit ? data : 0); |
| 117 | |
| 118 | outb(val, io); |
| 119 | udelay(4); |
| 120 | outb(val | clk, io); |
| 121 | udelay(4); |
| 122 | outb(val, io); |
| 123 | udelay(4); |
| 124 | } |
| 125 | |
| 126 | static void turn_power(struct maxiradio *dev, int p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 128 | if (p != 0) { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 129 | dprintk(dev, 1, "Radio powered on\n"); |
| 130 | outb(power, dev->io); |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 131 | } else { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 132 | dprintk(dev, 1, "Radio powered off\n"); |
| 133 | outb(0, dev->io); |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 137 | static void set_freq(struct maxiradio *dev, u32 freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | unsigned long int si; |
| 140 | int bl; |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 141 | int io = dev->io; |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 142 | int val = FREQ2BITS(freq); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | /* TEA5757 shift register bits (see pdf) */ |
| 145 | |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 146 | outbit(0, io); /* 24 search */ |
| 147 | outbit(1, io); /* 23 search up/down */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 148 | |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 149 | outbit(0, io); /* 22 stereo/mono */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 151 | outbit(0, io); /* 21 band */ |
| 152 | outbit(0, io); /* 20 band (only 00=FM works I think) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 154 | outbit(0, io); /* 19 port ? */ |
| 155 | outbit(0, io); /* 18 port ? */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 156 | |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 157 | outbit(0, io); /* 17 search level */ |
| 158 | outbit(0, io); /* 16 search level */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | si = 0x8000; |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 161 | for (bl = 1; bl <= 16; bl++) { |
| 162 | outbit(val & si, io); |
| 163 | si >>= 1; |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 164 | } |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 165 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 166 | dprintk(dev, 1, "Radio freq set to %d.%02d MHz\n", |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 167 | freq / 16000, |
| 168 | freq % 16000 * 100 / 16000); |
| 169 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 170 | turn_power(dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 173 | static int get_stereo(u16 io) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 174 | { |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 175 | outb(power,io); |
| 176 | udelay(4); |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | return !(inb(io) & mo_st); |
| 179 | } |
| 180 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 181 | static int get_tune(u16 io) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 182 | { |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 183 | outb(power+clk,io); |
| 184 | udelay(4); |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return !(inb(io) & mo_st); |
| 187 | } |
| 188 | |
| 189 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 190 | static int vidioc_querycap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 191 | struct v4l2_capability *v) |
| 192 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 193 | struct maxiradio *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 194 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 195 | strlcpy(v->driver, "radio-maxiradio", sizeof(v->driver)); |
| 196 | strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof(v->card)); |
| 197 | snprintf(v->bus_info, sizeof(v->bus_info), "PCI:%s", pci_name(dev->pdev)); |
| 198 | v->version = RADIO_VERSION; |
| 199 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 203 | static int vidioc_g_tuner(struct file *file, void *priv, |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 204 | struct v4l2_tuner *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 206 | struct maxiradio *dev = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 208 | if (v->index > 0) |
| 209 | return -EINVAL; |
Mauro Carvalho Chehab | e84fef6 | 2006-08-08 09:10:05 -0300 | [diff] [blame] | 210 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 211 | mutex_lock(&dev->lock); |
| 212 | strlcpy(v->name, "FM", sizeof(v->name)); |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 213 | v->type = V4L2_TUNER_RADIO; |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 214 | v->rangelow = FREQ_LO; |
| 215 | v->rangehigh = FREQ_HI; |
| 216 | v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; |
| 217 | v->capability = V4L2_TUNER_CAP_LOW; |
| 218 | if (get_stereo(dev->io)) |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 219 | v->audmode = V4L2_TUNER_MODE_STEREO; |
| 220 | else |
| 221 | v->audmode = V4L2_TUNER_MODE_MONO; |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 222 | v->signal = 0xffff * get_tune(dev->io); |
| 223 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 228 | static int vidioc_s_tuner(struct file *file, void *priv, |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 229 | struct v4l2_tuner *v) |
| 230 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 231 | return v->index ? -EINVAL : 0; |
Mauro Carvalho Chehab | 140dcc4 | 2007-01-25 15:00:45 -0300 | [diff] [blame] | 232 | } |
| 233 | |
Mauro Carvalho Chehab | a0c05ab | 2007-01-25 16:48:13 -0300 | [diff] [blame] | 234 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
| 235 | { |
| 236 | *i = 0; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
| 241 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 242 | return i ? -EINVAL : 0; |
Mauro Carvalho Chehab | a0c05ab | 2007-01-25 16:48:13 -0300 | [diff] [blame] | 243 | } |
| 244 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 245 | static int vidioc_g_audio(struct file *file, void *priv, |
Mauro Carvalho Chehab | 140dcc4 | 2007-01-25 15:00:45 -0300 | [diff] [blame] | 246 | struct v4l2_audio *a) |
| 247 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 248 | a->index = 0; |
| 249 | strlcpy(a->name, "Radio", sizeof(a->name)); |
| 250 | a->capability = V4L2_AUDCAP_STEREO; |
Mauro Carvalho Chehab | 140dcc4 | 2007-01-25 15:00:45 -0300 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 254 | |
| 255 | static int vidioc_s_audio(struct file *file, void *priv, |
| 256 | struct v4l2_audio *a) |
| 257 | { |
| 258 | return a->index ? -EINVAL : 0; |
| 259 | } |
| 260 | |
| 261 | static int vidioc_s_frequency(struct file *file, void *priv, |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 262 | struct v4l2_frequency *f) |
| 263 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 264 | struct maxiradio *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 265 | |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 266 | if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 267 | dprintk(dev, 1, "radio freq (%d.%02d MHz) out of range (%d-%d)\n", |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 268 | f->frequency / 16000, |
| 269 | f->frequency % 16000 * 100 / 16000, |
| 270 | FREQ_LO / 16000, FREQ_HI / 16000); |
| 271 | |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 272 | return -EINVAL; |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 273 | } |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 274 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 275 | mutex_lock(&dev->lock); |
| 276 | dev->freq = f->frequency; |
| 277 | set_freq(dev, dev->freq); |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 278 | msleep(125); |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 279 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 284 | static int vidioc_g_frequency(struct file *file, void *priv, |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 285 | struct v4l2_frequency *f) |
| 286 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 287 | struct maxiradio *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 288 | |
| 289 | f->type = V4L2_TUNER_RADIO; |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 290 | f->frequency = dev->freq; |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 291 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 292 | dprintk(dev, 4, "radio freq is %d.%02d MHz", |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 293 | f->frequency / 16000, |
| 294 | f->frequency % 16000 * 100 / 16000); |
| 295 | |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 299 | static int vidioc_queryctrl(struct file *file, void *priv, |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 300 | struct v4l2_queryctrl *qc) |
| 301 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 302 | switch (qc->id) { |
| 303 | case V4L2_CID_AUDIO_MUTE: |
| 304 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 306 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 309 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 310 | struct v4l2_control *ctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 312 | struct maxiradio *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 313 | |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 314 | switch (ctrl->id) { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 315 | case V4L2_CID_AUDIO_MUTE: |
| 316 | ctrl->value = dev->muted; |
| 317 | return 0; |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 318 | } |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 319 | |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 320 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 323 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 324 | struct v4l2_control *ctrl) |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 325 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 326 | struct maxiradio *dev = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 328 | switch (ctrl->id) { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 329 | case V4L2_CID_AUDIO_MUTE: |
| 330 | mutex_lock(&dev->lock); |
| 331 | dev->muted = ctrl->value; |
| 332 | if (dev->muted) |
| 333 | turn_power(dev, 0); |
| 334 | else |
| 335 | set_freq(dev, dev->freq); |
| 336 | mutex_unlock(&dev->lock); |
| 337 | return 0; |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 338 | } |
Mauro Carvalho Chehab | f1557ce | 2007-01-26 07:23:44 -0300 | [diff] [blame] | 339 | |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 340 | return -EINVAL; |
| 341 | } |
| 342 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 343 | static int maxiradio_open(struct file *file) |
| 344 | { |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int maxiradio_release(struct file *file) |
| 349 | { |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static const struct v4l2_file_operations maxiradio_fops = { |
| 354 | .owner = THIS_MODULE, |
| 355 | .open = maxiradio_open, |
| 356 | .release = maxiradio_release, |
| 357 | .ioctl = video_ioctl2, |
| 358 | }; |
| 359 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 360 | static const struct v4l2_ioctl_ops maxiradio_ioctl_ops = { |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 361 | .vidioc_querycap = vidioc_querycap, |
| 362 | .vidioc_g_tuner = vidioc_g_tuner, |
| 363 | .vidioc_s_tuner = vidioc_s_tuner, |
Mauro Carvalho Chehab | 140dcc4 | 2007-01-25 15:00:45 -0300 | [diff] [blame] | 364 | .vidioc_g_audio = vidioc_g_audio, |
| 365 | .vidioc_s_audio = vidioc_s_audio, |
Mauro Carvalho Chehab | a0c05ab | 2007-01-25 16:48:13 -0300 | [diff] [blame] | 366 | .vidioc_g_input = vidioc_g_input, |
| 367 | .vidioc_s_input = vidioc_s_input, |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 368 | .vidioc_g_frequency = vidioc_g_frequency, |
| 369 | .vidioc_s_frequency = vidioc_s_frequency, |
| 370 | .vidioc_queryctrl = vidioc_queryctrl, |
| 371 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 372 | .vidioc_s_ctrl = vidioc_s_ctrl, |
Mauro Carvalho Chehab | 06470ed | 2007-01-25 09:04:34 -0300 | [diff] [blame] | 373 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 376 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 377 | struct maxiradio *dev; |
| 378 | struct v4l2_device *v4l2_dev; |
| 379 | int retval = -ENOMEM; |
| 380 | |
| 381 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 382 | if (dev == NULL) { |
| 383 | dev_err(&pdev->dev, "not enough memory\n"); |
| 384 | return -ENOMEM; |
| 385 | } |
| 386 | |
| 387 | v4l2_dev = &dev->v4l2_dev; |
| 388 | mutex_init(&dev->lock); |
| 389 | dev->pdev = pdev; |
| 390 | dev->muted = 1; |
| 391 | dev->freq = FREQ_LO; |
| 392 | |
| 393 | strlcpy(v4l2_dev->name, "maxiradio", sizeof(v4l2_dev->name)); |
| 394 | |
| 395 | retval = v4l2_device_register(&pdev->dev, v4l2_dev); |
| 396 | if (retval < 0) { |
| 397 | v4l2_err(v4l2_dev, "Could not register v4l2_device\n"); |
| 398 | goto errfr; |
| 399 | } |
| 400 | |
| 401 | if (!request_region(pci_resource_start(pdev, 0), |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 402 | pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 403 | v4l2_err(v4l2_dev, "can't reserve I/O ports\n"); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 404 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | if (pci_enable_device(pdev)) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 408 | goto err_out_free_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 410 | dev->io = pci_resource_start(pdev, 0); |
| 411 | strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); |
| 412 | dev->vdev.v4l2_dev = v4l2_dev; |
| 413 | dev->vdev.fops = &maxiradio_fops; |
| 414 | dev->vdev.ioctl_ops = &maxiradio_ioctl_ops; |
| 415 | dev->vdev.release = video_device_release_empty; |
| 416 | video_set_drvdata(&dev->vdev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 418 | if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) { |
| 419 | v4l2_err(v4l2_dev, "can't register device!"); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 420 | goto err_out_free_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 423 | v4l2_info(v4l2_dev, "version " DRIVER_VERSION |
| 424 | " time " __TIME__ " " __DATE__ "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 426 | v4l2_info(v4l2_dev, "found Guillemot MAXI Radio device (io = 0x%x)\n", |
| 427 | dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | return 0; |
| 429 | |
| 430 | err_out_free_region: |
| 431 | release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0)); |
| 432 | err_out: |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 433 | v4l2_device_unregister(v4l2_dev); |
| 434 | errfr: |
| 435 | kfree(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | return -ENODEV; |
| 437 | } |
| 438 | |
| 439 | static void __devexit maxiradio_remove_one(struct pci_dev *pdev) |
| 440 | { |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 441 | struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev); |
| 442 | struct maxiradio *dev = to_maxiradio(v4l2_dev); |
| 443 | |
| 444 | video_unregister_device(&dev->vdev); |
| 445 | v4l2_device_unregister(&dev->v4l2_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0)); |
| 447 | } |
| 448 | |
| 449 | static struct pci_device_id maxiradio_pci_tbl[] = { |
| 450 | { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO, |
| 451 | PCI_ANY_ID, PCI_ANY_ID, }, |
Hans Verkuil | 2710e6a | 2009-03-06 13:51:33 -0300 | [diff] [blame^] | 452 | { 0 } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl); |
| 456 | |
| 457 | static struct pci_driver maxiradio_driver = { |
| 458 | .name = "radio-maxiradio", |
| 459 | .id_table = maxiradio_pci_tbl, |
| 460 | .probe = maxiradio_init_one, |
| 461 | .remove = __devexit_p(maxiradio_remove_one), |
| 462 | }; |
| 463 | |
| 464 | static int __init maxiradio_radio_init(void) |
| 465 | { |
Richard Knutsson | 9bfab8c | 2005-11-30 00:59:34 +0100 | [diff] [blame] | 466 | return pci_register_driver(&maxiradio_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static void __exit maxiradio_radio_exit(void) |
| 470 | { |
| 471 | pci_unregister_driver(&maxiradio_driver); |
| 472 | } |
| 473 | |
| 474 | module_init(maxiradio_radio_init); |
| 475 | module_exit(maxiradio_radio_exit); |