Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* A driver for the D-Link DSB-R100 USB radio and Gemtek USB Radio 21.
0003  * The device plugs into both the USB and an analog audio input, so this thing
0004  * only deals with initialisation and frequency setting, the
0005  * audio data has to be handled by a sound driver.
0006  *
0007  * Major issue: I can't find out where the device reports the signal
0008  * strength, and indeed the windows software appearantly just looks
0009  * at the stereo indicator as well.  So, scanning will only find
0010  * stereo stations.  Sad, but I can't help it.
0011  *
0012  * Also, the windows program sends oodles of messages over to the
0013  * device, and I couldn't figure out their meaning.  My suspicion
0014  * is that they don't have any:-)
0015  *
0016  * You might find some interesting stuff about this module at
0017  * http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
0018  *
0019  * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
0020  *
0021  * Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
0022 */
0023 
0024 #include <linux/kernel.h>
0025 #include <linux/module.h>
0026 #include <linux/init.h>
0027 #include <linux/slab.h>
0028 #include <linux/input.h>
0029 #include <linux/videodev2.h>
0030 #include <linux/usb.h>
0031 #include <media/v4l2-device.h>
0032 #include <media/v4l2-ioctl.h>
0033 #include <media/v4l2-ctrls.h>
0034 #include <media/v4l2-event.h>
0035 
0036 /*
0037  * Version Information
0038  */
0039 MODULE_AUTHOR("Markus Demleitner <msdemlei@tucana.harvard.edu>");
0040 MODULE_DESCRIPTION("D-Link DSB-R100 USB FM radio driver");
0041 MODULE_LICENSE("GPL");
0042 MODULE_VERSION("1.1.0");
0043 
0044 #define DSB100_VENDOR 0x04b4
0045 #define DSB100_PRODUCT 0x1002
0046 
0047 /* Commands the device appears to understand */
0048 #define DSB100_TUNE 1
0049 #define DSB100_ONOFF 2
0050 
0051 #define TB_LEN 16
0052 
0053 /* Frequency limits in MHz -- these are European values.  For Japanese
0054 devices, that would be 76 and 91.  */
0055 #define FREQ_MIN  87.5
0056 #define FREQ_MAX 108.0
0057 #define FREQ_MUL 16000
0058 
0059 #define v4l2_dev_to_radio(d) container_of(d, struct dsbr100_device, v4l2_dev)
0060 
0061 static int radio_nr = -1;
0062 module_param(radio_nr, int, 0);
0063 
0064 /* Data for one (physical) device */
0065 struct dsbr100_device {
0066     struct usb_device *usbdev;
0067     struct video_device videodev;
0068     struct v4l2_device v4l2_dev;
0069     struct v4l2_ctrl_handler hdl;
0070 
0071     u8 *transfer_buffer;
0072     struct mutex v4l2_lock;
0073     int curfreq;
0074     bool stereo;
0075     bool muted;
0076 };
0077 
0078 /* Low-level device interface begins here */
0079 
0080 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
0081 static int dsbr100_setfreq(struct dsbr100_device *radio, unsigned freq)
0082 {
0083     unsigned f = (freq / 16 * 80) / 1000 + 856;
0084     int retval = 0;
0085 
0086     if (!radio->muted) {
0087         retval = usb_control_msg(radio->usbdev,
0088                 usb_rcvctrlpipe(radio->usbdev, 0),
0089                 DSB100_TUNE,
0090                 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0091                 (f >> 8) & 0x00ff, f & 0xff,
0092                 radio->transfer_buffer, 8, 300);
0093         if (retval >= 0)
0094             mdelay(1);
0095     }
0096 
0097     if (retval >= 0) {
0098         radio->curfreq = freq;
0099         return 0;
0100     }
0101     dev_err(&radio->usbdev->dev,
0102         "%s - usb_control_msg returned %i, request %i\n",
0103             __func__, retval, DSB100_TUNE);
0104     return retval;
0105 }
0106 
0107 /* switch on radio */
0108 static int dsbr100_start(struct dsbr100_device *radio)
0109 {
0110     int retval = usb_control_msg(radio->usbdev,
0111         usb_rcvctrlpipe(radio->usbdev, 0),
0112         DSB100_ONOFF,
0113         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0114         0x01, 0x00, radio->transfer_buffer, 8, 300);
0115 
0116     if (retval >= 0)
0117         return dsbr100_setfreq(radio, radio->curfreq);
0118     dev_err(&radio->usbdev->dev,
0119         "%s - usb_control_msg returned %i, request %i\n",
0120             __func__, retval, DSB100_ONOFF);
0121     return retval;
0122 
0123 }
0124 
0125 /* switch off radio */
0126 static int dsbr100_stop(struct dsbr100_device *radio)
0127 {
0128     int retval = usb_control_msg(radio->usbdev,
0129         usb_rcvctrlpipe(radio->usbdev, 0),
0130         DSB100_ONOFF,
0131         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0132         0x00, 0x00, radio->transfer_buffer, 8, 300);
0133 
0134     if (retval >= 0)
0135         return 0;
0136     dev_err(&radio->usbdev->dev,
0137         "%s - usb_control_msg returned %i, request %i\n",
0138             __func__, retval, DSB100_ONOFF);
0139     return retval;
0140 
0141 }
0142 
0143 /* return the device status.  This is, in effect, just whether it
0144 sees a stereo signal or not.  Pity. */
0145 static void dsbr100_getstat(struct dsbr100_device *radio)
0146 {
0147     int retval = usb_control_msg(radio->usbdev,
0148         usb_rcvctrlpipe(radio->usbdev, 0),
0149         USB_REQ_GET_STATUS,
0150         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0151         0x00, 0x24, radio->transfer_buffer, 8, 300);
0152 
0153     if (retval < 0) {
0154         radio->stereo = false;
0155         dev_err(&radio->usbdev->dev,
0156             "%s - usb_control_msg returned %i, request %i\n",
0157                 __func__, retval, USB_REQ_GET_STATUS);
0158     } else {
0159         radio->stereo = !(radio->transfer_buffer[0] & 0x01);
0160     }
0161 }
0162 
0163 static int vidioc_querycap(struct file *file, void *priv,
0164                     struct v4l2_capability *v)
0165 {
0166     struct dsbr100_device *radio = video_drvdata(file);
0167 
0168     strscpy(v->driver, "dsbr100", sizeof(v->driver));
0169     strscpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
0170     usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
0171     return 0;
0172 }
0173 
0174 static int vidioc_g_tuner(struct file *file, void *priv,
0175                 struct v4l2_tuner *v)
0176 {
0177     struct dsbr100_device *radio = video_drvdata(file);
0178 
0179     if (v->index > 0)
0180         return -EINVAL;
0181 
0182     dsbr100_getstat(radio);
0183     strscpy(v->name, "FM", sizeof(v->name));
0184     v->type = V4L2_TUNER_RADIO;
0185     v->rangelow = FREQ_MIN * FREQ_MUL;
0186     v->rangehigh = FREQ_MAX * FREQ_MUL;
0187     v->rxsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO :
0188         V4L2_TUNER_SUB_MONO;
0189     v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
0190     v->audmode = V4L2_TUNER_MODE_STEREO;
0191     v->signal = radio->stereo ? 0xffff : 0;     /* We can't get the signal strength */
0192     return 0;
0193 }
0194 
0195 static int vidioc_s_tuner(struct file *file, void *priv,
0196                 const struct v4l2_tuner *v)
0197 {
0198     return v->index ? -EINVAL : 0;
0199 }
0200 
0201 static int vidioc_s_frequency(struct file *file, void *priv,
0202                 const struct v4l2_frequency *f)
0203 {
0204     struct dsbr100_device *radio = video_drvdata(file);
0205 
0206     if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
0207         return -EINVAL;
0208 
0209     return dsbr100_setfreq(radio, clamp_t(unsigned, f->frequency,
0210             FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL));
0211 }
0212 
0213 static int vidioc_g_frequency(struct file *file, void *priv,
0214                 struct v4l2_frequency *f)
0215 {
0216     struct dsbr100_device *radio = video_drvdata(file);
0217 
0218     if (f->tuner)
0219         return -EINVAL;
0220     f->type = V4L2_TUNER_RADIO;
0221     f->frequency = radio->curfreq;
0222     return 0;
0223 }
0224 
0225 static int usb_dsbr100_s_ctrl(struct v4l2_ctrl *ctrl)
0226 {
0227     struct dsbr100_device *radio =
0228         container_of(ctrl->handler, struct dsbr100_device, hdl);
0229 
0230     switch (ctrl->id) {
0231     case V4L2_CID_AUDIO_MUTE:
0232         radio->muted = ctrl->val;
0233         return radio->muted ? dsbr100_stop(radio) : dsbr100_start(radio);
0234     }
0235     return -EINVAL;
0236 }
0237 
0238 
0239 /* USB subsystem interface begins here */
0240 
0241 /*
0242  * Handle unplugging of the device.
0243  * We call video_unregister_device in any case.
0244  * The last function called in this procedure is
0245  * usb_dsbr100_video_device_release
0246  */
0247 static void usb_dsbr100_disconnect(struct usb_interface *intf)
0248 {
0249     struct dsbr100_device *radio = usb_get_intfdata(intf);
0250 
0251     mutex_lock(&radio->v4l2_lock);
0252     /*
0253      * Disconnect is also called on unload, and in that case we need to
0254      * mute the device. This call will silently fail if it is called
0255      * after a physical disconnect.
0256      */
0257     usb_control_msg(radio->usbdev,
0258         usb_rcvctrlpipe(radio->usbdev, 0),
0259         DSB100_ONOFF,
0260         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0261         0x00, 0x00, radio->transfer_buffer, 8, 300);
0262     usb_set_intfdata(intf, NULL);
0263     video_unregister_device(&radio->videodev);
0264     v4l2_device_disconnect(&radio->v4l2_dev);
0265     mutex_unlock(&radio->v4l2_lock);
0266     v4l2_device_put(&radio->v4l2_dev);
0267 }
0268 
0269 
0270 /* Suspend device - stop device. */
0271 static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
0272 {
0273     struct dsbr100_device *radio = usb_get_intfdata(intf);
0274 
0275     mutex_lock(&radio->v4l2_lock);
0276     if (!radio->muted && dsbr100_stop(radio) < 0)
0277         dev_warn(&intf->dev, "dsbr100_stop failed\n");
0278     mutex_unlock(&radio->v4l2_lock);
0279 
0280     dev_info(&intf->dev, "going into suspend..\n");
0281     return 0;
0282 }
0283 
0284 /* Resume device - start device. */
0285 static int usb_dsbr100_resume(struct usb_interface *intf)
0286 {
0287     struct dsbr100_device *radio = usb_get_intfdata(intf);
0288 
0289     mutex_lock(&radio->v4l2_lock);
0290     if (!radio->muted && dsbr100_start(radio) < 0)
0291         dev_warn(&intf->dev, "dsbr100_start failed\n");
0292     mutex_unlock(&radio->v4l2_lock);
0293 
0294     dev_info(&intf->dev, "coming out of suspend..\n");
0295     return 0;
0296 }
0297 
0298 /* free data structures */
0299 static void usb_dsbr100_release(struct v4l2_device *v4l2_dev)
0300 {
0301     struct dsbr100_device *radio = v4l2_dev_to_radio(v4l2_dev);
0302 
0303     v4l2_ctrl_handler_free(&radio->hdl);
0304     v4l2_device_unregister(&radio->v4l2_dev);
0305     kfree(radio->transfer_buffer);
0306     kfree(radio);
0307 }
0308 
0309 static const struct v4l2_ctrl_ops usb_dsbr100_ctrl_ops = {
0310     .s_ctrl = usb_dsbr100_s_ctrl,
0311 };
0312 
0313 /* File system interface */
0314 static const struct v4l2_file_operations usb_dsbr100_fops = {
0315     .owner      = THIS_MODULE,
0316     .unlocked_ioctl = video_ioctl2,
0317     .open           = v4l2_fh_open,
0318     .release        = v4l2_fh_release,
0319     .poll       = v4l2_ctrl_poll,
0320 };
0321 
0322 static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
0323     .vidioc_querycap    = vidioc_querycap,
0324     .vidioc_g_tuner     = vidioc_g_tuner,
0325     .vidioc_s_tuner     = vidioc_s_tuner,
0326     .vidioc_g_frequency = vidioc_g_frequency,
0327     .vidioc_s_frequency = vidioc_s_frequency,
0328     .vidioc_log_status  = v4l2_ctrl_log_status,
0329     .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
0330     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
0331 };
0332 
0333 /* check if the device is present and register with v4l and usb if it is */
0334 static int usb_dsbr100_probe(struct usb_interface *intf,
0335                 const struct usb_device_id *id)
0336 {
0337     struct dsbr100_device *radio;
0338     struct v4l2_device *v4l2_dev;
0339     int retval;
0340 
0341     radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
0342 
0343     if (!radio)
0344         return -ENOMEM;
0345 
0346     radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
0347 
0348     if (!(radio->transfer_buffer)) {
0349         kfree(radio);
0350         return -ENOMEM;
0351     }
0352 
0353     v4l2_dev = &radio->v4l2_dev;
0354     v4l2_dev->release = usb_dsbr100_release;
0355 
0356     retval = v4l2_device_register(&intf->dev, v4l2_dev);
0357     if (retval < 0) {
0358         v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
0359         goto err_reg_dev;
0360     }
0361 
0362     v4l2_ctrl_handler_init(&radio->hdl, 1);
0363     v4l2_ctrl_new_std(&radio->hdl, &usb_dsbr100_ctrl_ops,
0364               V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
0365     if (radio->hdl.error) {
0366         retval = radio->hdl.error;
0367         v4l2_err(v4l2_dev, "couldn't register control\n");
0368         goto err_reg_ctrl;
0369     }
0370     mutex_init(&radio->v4l2_lock);
0371     strscpy(radio->videodev.name, v4l2_dev->name,
0372         sizeof(radio->videodev.name));
0373     radio->videodev.v4l2_dev = v4l2_dev;
0374     radio->videodev.fops = &usb_dsbr100_fops;
0375     radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops;
0376     radio->videodev.release = video_device_release_empty;
0377     radio->videodev.lock = &radio->v4l2_lock;
0378     radio->videodev.ctrl_handler = &radio->hdl;
0379     radio->videodev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
0380 
0381     radio->usbdev = interface_to_usbdev(intf);
0382     radio->curfreq = FREQ_MIN * FREQ_MUL;
0383     radio->muted = true;
0384 
0385     video_set_drvdata(&radio->videodev, radio);
0386     usb_set_intfdata(intf, radio);
0387 
0388     retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
0389     if (retval == 0)
0390         return 0;
0391     v4l2_err(v4l2_dev, "couldn't register video device\n");
0392 
0393 err_reg_ctrl:
0394     v4l2_ctrl_handler_free(&radio->hdl);
0395     v4l2_device_unregister(v4l2_dev);
0396 err_reg_dev:
0397     kfree(radio->transfer_buffer);
0398     kfree(radio);
0399     return retval;
0400 }
0401 
0402 static const struct usb_device_id usb_dsbr100_device_table[] = {
0403     { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
0404     { }                     /* Terminating entry */
0405 };
0406 
0407 MODULE_DEVICE_TABLE(usb, usb_dsbr100_device_table);
0408 
0409 /* USB subsystem interface */
0410 static struct usb_driver usb_dsbr100_driver = {
0411     .name           = "dsbr100",
0412     .probe          = usb_dsbr100_probe,
0413     .disconnect     = usb_dsbr100_disconnect,
0414     .id_table       = usb_dsbr100_device_table,
0415     .suspend        = usb_dsbr100_suspend,
0416     .resume         = usb_dsbr100_resume,
0417     .reset_resume       = usb_dsbr100_resume,
0418 };
0419 
0420 module_usb_driver(usb_dsbr100_driver);