Alexey Klimov | fc55bcb | 2008-12-27 22:33:54 -0300 | [diff] [blame] | 1 | /* A driver for the D-Link DSB-R100 USB radio and Gemtek USB Radio 21. |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 2 | * The device plugs into both the USB and an analog audio input, so this thing |
| 3 | * only deals with initialisation and frequency setting, the |
| 4 | * audio data has to be handled by a sound driver. |
| 5 | * |
| 6 | * Major issue: I can't find out where the device reports the signal |
| 7 | * strength, and indeed the windows software appearantly just looks |
| 8 | * at the stereo indicator as well. So, scanning will only find |
| 9 | * stereo stations. Sad, but I can't help it. |
| 10 | * |
| 11 | * Also, the windows program sends oodles of messages over to the |
| 12 | * device, and I couldn't figure out their meaning. My suspicion |
| 13 | * is that they don't have any:-) |
| 14 | * |
| 15 | * You might find some interesting stuff about this module at |
| 16 | * http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr |
| 17 | * |
| 18 | * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool. |
| 19 | * |
| 20 | * Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de> |
| 21 | * |
| 22 | * This program is free software; you can redistribute it and/or modify |
| 23 | * it under the terms of the GNU General Public License as published by |
| 24 | * the Free Software Foundation; either version 2 of the License, or |
| 25 | * (at your option) any later version. |
| 26 | * |
| 27 | * This program is distributed in the hope that it will be useful, |
| 28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 30 | * GNU General Public License for more details. |
| 31 | * |
| 32 | * You should have received a copy of the GNU General Public License |
| 33 | * along with this program; if not, write to the Free Software |
| 34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/kernel.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/input.h> |
Alan Cox | 5aff308 | 2006-08-08 15:47:50 -0300 | [diff] [blame] | 42 | #include <linux/videodev2.h> |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 43 | #include <linux/usb.h> |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 44 | #include <media/v4l2-device.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 45 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 46 | #include <media/v4l2-ctrls.h> |
| 47 | #include <media/v4l2-event.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Version Information |
| 51 | */ |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 52 | MODULE_AUTHOR("Markus Demleitner <msdemlei@tucana.harvard.edu>"); |
| 53 | MODULE_DESCRIPTION("D-Link DSB-R100 USB FM radio driver"); |
| 54 | MODULE_LICENSE("GPL"); |
| 55 | MODULE_VERSION("1.1.0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #define DSB100_VENDOR 0x04b4 |
| 58 | #define DSB100_PRODUCT 0x1002 |
| 59 | |
| 60 | /* Commands the device appears to understand */ |
| 61 | #define DSB100_TUNE 1 |
| 62 | #define DSB100_ONOFF 2 |
| 63 | |
| 64 | #define TB_LEN 16 |
| 65 | |
| 66 | /* Frequency limits in MHz -- these are European values. For Japanese |
| 67 | devices, that would be 76 and 91. */ |
| 68 | #define FREQ_MIN 87.5 |
| 69 | #define FREQ_MAX 108.0 |
| 70 | #define FREQ_MUL 16000 |
| 71 | |
Hans Verkuil | 1309929 | 2011-01-17 20:25:11 -0300 | [diff] [blame] | 72 | #define v4l2_dev_to_radio(d) container_of(d, struct dsbr100_device, v4l2_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | static int radio_nr = -1; |
| 75 | module_param(radio_nr, int, 0); |
| 76 | |
| 77 | /* Data for one (physical) device */ |
Alan Cox | 5aff308 | 2006-08-08 15:47:50 -0300 | [diff] [blame] | 78 | struct dsbr100_device { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct usb_device *usbdev; |
Alexey Klimov | 3a0efc3 | 2008-12-27 21:32:49 -0300 | [diff] [blame] | 80 | struct video_device videodev; |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 81 | struct v4l2_device v4l2_dev; |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 82 | struct v4l2_ctrl_handler hdl; |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 83 | |
Oliver Neukum | 863c86d | 2007-12-03 06:48:43 -0300 | [diff] [blame] | 84 | u8 *transfer_buffer; |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 85 | struct mutex v4l2_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | int curfreq; |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 87 | bool stereo; |
| 88 | bool muted; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /* Low-level device interface begins here */ |
| 92 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 93 | /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */ |
| 94 | static int dsbr100_setfreq(struct dsbr100_device *radio, unsigned freq) |
| 95 | { |
| 96 | unsigned f = (freq / 16 * 80) / 1000 + 856; |
| 97 | int retval = 0; |
| 98 | |
| 99 | if (!radio->muted) { |
| 100 | retval = usb_control_msg(radio->usbdev, |
| 101 | usb_rcvctrlpipe(radio->usbdev, 0), |
| 102 | DSB100_TUNE, |
| 103 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 104 | (f >> 8) & 0x00ff, f & 0xff, |
| 105 | radio->transfer_buffer, 8, 300); |
| 106 | if (retval >= 0) |
| 107 | mdelay(1); |
| 108 | } |
| 109 | |
| 110 | if (retval >= 0) { |
| 111 | radio->curfreq = freq; |
| 112 | return 0; |
| 113 | } |
| 114 | dev_err(&radio->usbdev->dev, |
| 115 | "%s - usb_control_msg returned %i, request %i\n", |
| 116 | __func__, retval, DSB100_TUNE); |
| 117 | return retval; |
| 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* switch on radio */ |
Alan Cox | 5aff308 | 2006-08-08 15:47:50 -0300 | [diff] [blame] | 121 | static int dsbr100_start(struct dsbr100_device *radio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 123 | int retval = usb_control_msg(radio->usbdev, |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 124 | usb_rcvctrlpipe(radio->usbdev, 0), |
| 125 | DSB100_ONOFF, |
| 126 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 127 | 0x01, 0x00, radio->transfer_buffer, 8, 300); |
| 128 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 129 | if (retval >= 0) |
| 130 | return dsbr100_setfreq(radio, radio->curfreq); |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 131 | dev_err(&radio->usbdev->dev, |
| 132 | "%s - usb_control_msg returned %i, request %i\n", |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 133 | __func__, retval, DSB100_ONOFF); |
Alexey Klimov | d25cb64 | 2008-12-27 22:40:57 -0300 | [diff] [blame] | 134 | return retval; |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* switch off radio */ |
Alan Cox | 5aff308 | 2006-08-08 15:47:50 -0300 | [diff] [blame] | 139 | static int dsbr100_stop(struct dsbr100_device *radio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 141 | int retval = usb_control_msg(radio->usbdev, |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 142 | usb_rcvctrlpipe(radio->usbdev, 0), |
| 143 | DSB100_ONOFF, |
| 144 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 145 | 0x00, 0x00, radio->transfer_buffer, 8, 300); |
| 146 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 147 | if (retval >= 0) |
| 148 | return 0; |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 149 | dev_err(&radio->usbdev->dev, |
| 150 | "%s - usb_control_msg returned %i, request %i\n", |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 151 | __func__, retval, DSB100_ONOFF); |
Alexey Klimov | d25cb64 | 2008-12-27 22:40:57 -0300 | [diff] [blame] | 152 | return retval; |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* return the device status. This is, in effect, just whether it |
| 157 | sees a stereo signal or not. Pity. */ |
Alan Cox | 5aff308 | 2006-08-08 15:47:50 -0300 | [diff] [blame] | 158 | static void dsbr100_getstat(struct dsbr100_device *radio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 160 | int retval = usb_control_msg(radio->usbdev, |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 161 | usb_rcvctrlpipe(radio->usbdev, 0), |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 162 | USB_REQ_GET_STATUS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 164 | 0x00, 0x24, radio->transfer_buffer, 8, 300); |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 165 | |
| 166 | if (retval < 0) { |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 167 | radio->stereo = false; |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 168 | dev_err(&radio->usbdev->dev, |
| 169 | "%s - usb_control_msg returned %i, request %i\n", |
| 170 | __func__, retval, USB_REQ_GET_STATUS); |
| 171 | } else { |
Alexey Klimov | 223377e | 2008-10-19 23:50:27 -0300 | [diff] [blame] | 172 | radio->stereo = !(radio->transfer_buffer[0] & 0x01); |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 176 | static int vidioc_querycap(struct file *file, void *priv, |
| 177 | struct v4l2_capability *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Alexey Klimov | c7181cf | 2009-01-25 20:05:58 -0300 | [diff] [blame] | 179 | struct dsbr100_device *radio = video_drvdata(file); |
| 180 | |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 181 | strlcpy(v->driver, "dsbr100", sizeof(v->driver)); |
| 182 | strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card)); |
Alexey Klimov | c7181cf | 2009-01-25 20:05:58 -0300 | [diff] [blame] | 183 | usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info)); |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 184 | v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER; |
| 185 | v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 186 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 189 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 190 | struct v4l2_tuner *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 192 | struct dsbr100_device *radio = video_drvdata(file); |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 193 | |
| 194 | if (v->index > 0) |
| 195 | return -EINVAL; |
| 196 | |
| 197 | dsbr100_getstat(radio); |
| 198 | strcpy(v->name, "FM"); |
| 199 | v->type = V4L2_TUNER_RADIO; |
Alexey Klimov | 223377e | 2008-10-19 23:50:27 -0300 | [diff] [blame] | 200 | v->rangelow = FREQ_MIN * FREQ_MUL; |
| 201 | v->rangehigh = FREQ_MAX * FREQ_MUL; |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 202 | v->rxsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO : |
| 203 | V4L2_TUNER_SUB_MONO; |
| 204 | v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; |
| 205 | v->audmode = V4L2_TUNER_MODE_STEREO; |
| 206 | v->signal = radio->stereo ? 0xffff : 0; /* We can't get the signal strength */ |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 211 | struct v4l2_tuner *v) |
| 212 | { |
Hans Verkuil | 1309929 | 2011-01-17 20:25:11 -0300 | [diff] [blame] | 213 | return v->index ? -EINVAL : 0; |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 217 | struct v4l2_frequency *f) |
| 218 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 219 | struct dsbr100_device *radio = video_drvdata(file); |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 220 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 221 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
| 222 | return -EINVAL; |
Alexey Klimov | fdf9c99 | 2009-02-08 02:00:14 -0300 | [diff] [blame] | 223 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 224 | return dsbr100_setfreq(radio, clamp_t(unsigned, f->frequency, |
| 225 | FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL)); |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 229 | struct v4l2_frequency *f) |
| 230 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 231 | struct dsbr100_device *radio = video_drvdata(file); |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 232 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 233 | if (f->tuner) |
| 234 | return -EINVAL; |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 235 | f->type = V4L2_TUNER_RADIO; |
| 236 | f->frequency = radio->curfreq; |
| 237 | return 0; |
| 238 | } |
| 239 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 240 | static int usb_dsbr100_s_ctrl(struct v4l2_ctrl *ctrl) |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 241 | { |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 242 | struct dsbr100_device *radio = |
| 243 | container_of(ctrl->handler, struct dsbr100_device, hdl); |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 244 | |
| 245 | switch (ctrl->id) { |
| 246 | case V4L2_CID_AUDIO_MUTE: |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 247 | radio->muted = ctrl->val; |
| 248 | return radio->muted ? dsbr100_stop(radio) : dsbr100_start(radio); |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 249 | } |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 254 | /* USB subsystem interface begins here */ |
| 255 | |
| 256 | /* |
| 257 | * Handle unplugging of the device. |
| 258 | * We call video_unregister_device in any case. |
| 259 | * The last function called in this procedure is |
| 260 | * usb_dsbr100_video_device_release |
| 261 | */ |
| 262 | static void usb_dsbr100_disconnect(struct usb_interface *intf) |
| 263 | { |
| 264 | struct dsbr100_device *radio = usb_get_intfdata(intf); |
| 265 | |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 266 | mutex_lock(&radio->v4l2_lock); |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 267 | /* |
| 268 | * Disconnect is also called on unload, and in that case we need to |
| 269 | * mute the device. This call will silently fail if it is called |
| 270 | * after a physical disconnect. |
| 271 | */ |
| 272 | usb_control_msg(radio->usbdev, |
| 273 | usb_rcvctrlpipe(radio->usbdev, 0), |
| 274 | DSB100_ONOFF, |
| 275 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 276 | 0x00, 0x00, radio->transfer_buffer, 8, 300); |
Hans Verkuil | 1309929 | 2011-01-17 20:25:11 -0300 | [diff] [blame] | 277 | usb_set_intfdata(intf, NULL); |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 278 | video_unregister_device(&radio->videodev); |
| 279 | v4l2_device_disconnect(&radio->v4l2_dev); |
Hans Verkuil | 1309929 | 2011-01-17 20:25:11 -0300 | [diff] [blame] | 280 | mutex_unlock(&radio->v4l2_lock); |
| 281 | v4l2_device_put(&radio->v4l2_dev); |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
Alexey Klimov | 04e0ffb | 2008-11-08 00:40:46 -0300 | [diff] [blame] | 285 | /* Suspend device - stop device. */ |
| 286 | static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message) |
| 287 | { |
| 288 | struct dsbr100_device *radio = usb_get_intfdata(intf); |
Alexey Klimov | 04e0ffb | 2008-11-08 00:40:46 -0300 | [diff] [blame] | 289 | |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 290 | mutex_lock(&radio->v4l2_lock); |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 291 | if (!radio->muted && dsbr100_stop(radio) < 0) |
| 292 | dev_warn(&intf->dev, "dsbr100_stop failed\n"); |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 293 | mutex_unlock(&radio->v4l2_lock); |
Alexey Klimov | 04e0ffb | 2008-11-08 00:40:46 -0300 | [diff] [blame] | 294 | |
| 295 | dev_info(&intf->dev, "going into suspend..\n"); |
Alexey Klimov | 04e0ffb | 2008-11-08 00:40:46 -0300 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /* Resume device - start device. */ |
| 300 | static int usb_dsbr100_resume(struct usb_interface *intf) |
| 301 | { |
| 302 | struct dsbr100_device *radio = usb_get_intfdata(intf); |
Alexey Klimov | 04e0ffb | 2008-11-08 00:40:46 -0300 | [diff] [blame] | 303 | |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 304 | mutex_lock(&radio->v4l2_lock); |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 305 | if (!radio->muted && dsbr100_start(radio) < 0) |
| 306 | dev_warn(&intf->dev, "dsbr100_start failed\n"); |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 307 | mutex_unlock(&radio->v4l2_lock); |
Alexey Klimov | 04e0ffb | 2008-11-08 00:40:46 -0300 | [diff] [blame] | 308 | |
| 309 | dev_info(&intf->dev, "coming out of suspend..\n"); |
Alexey Klimov | 04e0ffb | 2008-11-08 00:40:46 -0300 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
Alexey Klimov | fc55bcb | 2008-12-27 22:33:54 -0300 | [diff] [blame] | 313 | /* free data structures */ |
Hans Verkuil | 1309929 | 2011-01-17 20:25:11 -0300 | [diff] [blame] | 314 | static void usb_dsbr100_release(struct v4l2_device *v4l2_dev) |
Alexey Klimov | 3a0efc3 | 2008-12-27 21:32:49 -0300 | [diff] [blame] | 315 | { |
Hans Verkuil | 1309929 | 2011-01-17 20:25:11 -0300 | [diff] [blame] | 316 | struct dsbr100_device *radio = v4l2_dev_to_radio(v4l2_dev); |
Alexey Klimov | 3a0efc3 | 2008-12-27 21:32:49 -0300 | [diff] [blame] | 317 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 318 | v4l2_ctrl_handler_free(&radio->hdl); |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 319 | v4l2_device_unregister(&radio->v4l2_dev); |
Alexey Klimov | 3a0efc3 | 2008-12-27 21:32:49 -0300 | [diff] [blame] | 320 | kfree(radio->transfer_buffer); |
| 321 | kfree(radio); |
| 322 | } |
| 323 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 324 | static const struct v4l2_ctrl_ops usb_dsbr100_ctrl_ops = { |
| 325 | .s_ctrl = usb_dsbr100_s_ctrl, |
| 326 | }; |
| 327 | |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 328 | /* File system interface */ |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 329 | static const struct v4l2_file_operations usb_dsbr100_fops = { |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 330 | .owner = THIS_MODULE, |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 331 | .unlocked_ioctl = video_ioctl2, |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 332 | .open = v4l2_fh_open, |
| 333 | .release = v4l2_fh_release, |
| 334 | .poll = v4l2_ctrl_poll, |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 335 | }; |
| 336 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 337 | static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = { |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 338 | .vidioc_querycap = vidioc_querycap, |
| 339 | .vidioc_g_tuner = vidioc_g_tuner, |
| 340 | .vidioc_s_tuner = vidioc_s_tuner, |
| 341 | .vidioc_g_frequency = vidioc_g_frequency, |
| 342 | .vidioc_s_frequency = vidioc_s_frequency, |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 343 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 344 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 345 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 346 | }; |
| 347 | |
Alexey Klimov | fc55bcb | 2008-12-27 22:33:54 -0300 | [diff] [blame] | 348 | /* check if the device is present and register with v4l and usb if it is */ |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 349 | static int usb_dsbr100_probe(struct usb_interface *intf, |
| 350 | const struct usb_device_id *id) |
| 351 | { |
| 352 | struct dsbr100_device *radio; |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 353 | struct v4l2_device *v4l2_dev; |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 354 | int retval; |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 355 | |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 356 | radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL); |
Alexey Klimov | 223377e | 2008-10-19 23:50:27 -0300 | [diff] [blame] | 357 | |
| 358 | if (!radio) |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 359 | return -ENOMEM; |
Alexey Klimov | 223377e | 2008-10-19 23:50:27 -0300 | [diff] [blame] | 360 | |
| 361 | radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL); |
| 362 | |
| 363 | if (!(radio->transfer_buffer)) { |
Oliver Neukum | 863c86d | 2007-12-03 06:48:43 -0300 | [diff] [blame] | 364 | kfree(radio); |
| 365 | return -ENOMEM; |
| 366 | } |
Alexey Klimov | 223377e | 2008-10-19 23:50:27 -0300 | [diff] [blame] | 367 | |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 368 | v4l2_dev = &radio->v4l2_dev; |
Hans Verkuil | 1309929 | 2011-01-17 20:25:11 -0300 | [diff] [blame] | 369 | v4l2_dev->release = usb_dsbr100_release; |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 370 | |
| 371 | retval = v4l2_device_register(&intf->dev, v4l2_dev); |
| 372 | if (retval < 0) { |
| 373 | v4l2_err(v4l2_dev, "couldn't register v4l2_device\n"); |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 374 | goto err_reg_dev; |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 375 | } |
| 376 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 377 | v4l2_ctrl_handler_init(&radio->hdl, 1); |
| 378 | v4l2_ctrl_new_std(&radio->hdl, &usb_dsbr100_ctrl_ops, |
| 379 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); |
| 380 | if (radio->hdl.error) { |
| 381 | retval = radio->hdl.error; |
| 382 | v4l2_err(v4l2_dev, "couldn't register control\n"); |
| 383 | goto err_reg_ctrl; |
| 384 | } |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 385 | mutex_init(&radio->v4l2_lock); |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 386 | strlcpy(radio->videodev.name, v4l2_dev->name, sizeof(radio->videodev.name)); |
| 387 | radio->videodev.v4l2_dev = v4l2_dev; |
| 388 | radio->videodev.fops = &usb_dsbr100_fops; |
| 389 | radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops; |
Hans Verkuil | 1309929 | 2011-01-17 20:25:11 -0300 | [diff] [blame] | 390 | radio->videodev.release = video_device_release_empty; |
Hans Verkuil | e64d07c | 2010-12-19 18:54:08 -0300 | [diff] [blame] | 391 | radio->videodev.lock = &radio->v4l2_lock; |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 392 | radio->videodev.ctrl_handler = &radio->hdl; |
| 393 | set_bit(V4L2_FL_USE_FH_PRIO, &radio->videodev.flags); |
Alexey Klimov | 3a0efc3 | 2008-12-27 21:32:49 -0300 | [diff] [blame] | 394 | |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 395 | radio->usbdev = interface_to_usbdev(intf); |
Alexey Klimov | 223377e | 2008-10-19 23:50:27 -0300 | [diff] [blame] | 396 | radio->curfreq = FREQ_MIN * FREQ_MUL; |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 397 | radio->muted = true; |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 398 | |
Alexey Klimov | 3a0efc3 | 2008-12-27 21:32:49 -0300 | [diff] [blame] | 399 | video_set_drvdata(&radio->videodev, radio); |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 400 | usb_set_intfdata(intf, radio); |
Alexey Klimov | 406827c | 2009-04-03 18:45:17 -0300 | [diff] [blame] | 401 | |
Alexey Klimov | 417b795 | 2008-12-27 22:30:29 -0300 | [diff] [blame] | 402 | retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr); |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 403 | if (retval == 0) |
| 404 | return 0; |
| 405 | v4l2_err(v4l2_dev, "couldn't register video device\n"); |
| 406 | |
| 407 | err_reg_ctrl: |
| 408 | v4l2_ctrl_handler_free(&radio->hdl); |
| 409 | v4l2_device_unregister(v4l2_dev); |
| 410 | err_reg_dev: |
| 411 | kfree(radio->transfer_buffer); |
| 412 | kfree(radio); |
| 413 | return retval; |
Douglas Landgraf | 7002a4f | 2007-05-07 16:43:01 -0300 | [diff] [blame] | 414 | } |
| 415 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 416 | static struct usb_device_id usb_dsbr100_device_table[] = { |
| 417 | { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) }, |
| 418 | { } /* Terminating entry */ |
| 419 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Hans Verkuil | 5d77864 | 2012-04-27 08:13:26 -0300 | [diff] [blame] | 421 | MODULE_DEVICE_TABLE(usb, usb_dsbr100_device_table); |
| 422 | |
| 423 | /* USB subsystem interface */ |
| 424 | static struct usb_driver usb_dsbr100_driver = { |
| 425 | .name = "dsbr100", |
| 426 | .probe = usb_dsbr100_probe, |
| 427 | .disconnect = usb_dsbr100_disconnect, |
| 428 | .id_table = usb_dsbr100_device_table, |
| 429 | .suspend = usb_dsbr100_suspend, |
| 430 | .resume = usb_dsbr100_resume, |
| 431 | .reset_resume = usb_dsbr100_resume, |
| 432 | }; |
| 433 | |
| 434 | module_usb_driver(usb_dsbr100_driver); |