Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Linux V4L2 radio driver for the Griffin radioSHARK2 USB radio receiver
0003  *
0004  * Note the radioSHARK2 offers the audio through a regular USB audio device,
0005  * this driver only handles the tuning.
0006  *
0007  * The info necessary to drive the shark2 was taken from the small userspace
0008  * shark2.c program by Hisaaki Shibata, which he kindly placed in the Public
0009  * Domain.
0010  *
0011  * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
0012  *
0013  * This program is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * This program is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  */
0023 
0024 #include <linux/init.h>
0025 #include <linux/kernel.h>
0026 #include <linux/leds.h>
0027 #include <linux/module.h>
0028 #include <linux/slab.h>
0029 #include <linux/usb.h>
0030 #include <linux/workqueue.h>
0031 #include <media/v4l2-device.h>
0032 #include "radio-tea5777.h"
0033 
0034 #if defined(CONFIG_LEDS_CLASS) || \
0035     (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK2_MODULE))
0036 #define SHARK_USE_LEDS 1
0037 #endif
0038 
0039 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0040 MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver");
0041 MODULE_LICENSE("GPL");
0042 
0043 static int debug;
0044 module_param(debug, int, 0);
0045 MODULE_PARM_DESC(debug, "Debug level (0-1)");
0046 
0047 #define SHARK_IN_EP     0x83
0048 #define SHARK_OUT_EP        0x05
0049 
0050 #define TB_LEN 7
0051 #define DRV_NAME "radioshark2"
0052 
0053 #define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)
0054 
0055 enum { BLUE_LED, RED_LED, NO_LEDS };
0056 
0057 struct shark_device {
0058     struct usb_device *usbdev;
0059     struct v4l2_device v4l2_dev;
0060     struct radio_tea5777 tea;
0061 
0062 #ifdef SHARK_USE_LEDS
0063     struct work_struct led_work;
0064     struct led_classdev leds[NO_LEDS];
0065     char led_names[NO_LEDS][32];
0066     atomic_t brightness[NO_LEDS];
0067     unsigned long brightness_new;
0068 #endif
0069 
0070     u8 *transfer_buffer;
0071 };
0072 
0073 static atomic_t shark_instance = ATOMIC_INIT(0);
0074 
0075 static int shark_write_reg(struct radio_tea5777 *tea, u64 reg)
0076 {
0077     struct shark_device *shark = tea->private_data;
0078     int i, res, actual_len;
0079 
0080     memset(shark->transfer_buffer, 0, TB_LEN);
0081     shark->transfer_buffer[0] = 0x81; /* Write register command */
0082     for (i = 0; i < 6; i++)
0083         shark->transfer_buffer[i + 1] = (reg >> (40 - i * 8)) & 0xff;
0084 
0085     v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-write: %*ph\n",
0086          7, shark->transfer_buffer);
0087 
0088     res = usb_interrupt_msg(shark->usbdev,
0089                 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
0090                 shark->transfer_buffer, TB_LEN,
0091                 &actual_len, 1000);
0092     if (res < 0) {
0093         v4l2_err(tea->v4l2_dev, "write error: %d\n", res);
0094         return res;
0095     }
0096 
0097     return 0;
0098 }
0099 
0100 static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret)
0101 {
0102     struct shark_device *shark = tea->private_data;
0103     int i, res, actual_len;
0104     u32 reg = 0;
0105 
0106     memset(shark->transfer_buffer, 0, TB_LEN);
0107     shark->transfer_buffer[0] = 0x82;
0108     res = usb_interrupt_msg(shark->usbdev,
0109                 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
0110                 shark->transfer_buffer, TB_LEN,
0111                 &actual_len, 1000);
0112     if (res < 0) {
0113         v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res);
0114         return res;
0115     }
0116 
0117     res = usb_interrupt_msg(shark->usbdev,
0118                 usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
0119                 shark->transfer_buffer, TB_LEN,
0120                 &actual_len, 1000);
0121     if (res < 0) {
0122         v4l2_err(tea->v4l2_dev, "read error: %d\n", res);
0123         return res;
0124     }
0125 
0126     for (i = 0; i < 3; i++)
0127         reg |= shark->transfer_buffer[i] << (16 - i * 8);
0128 
0129     v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-read: %*ph\n",
0130          3, shark->transfer_buffer);
0131 
0132     *reg_ret = reg;
0133     return 0;
0134 }
0135 
0136 static const struct radio_tea5777_ops shark_tea_ops = {
0137     .write_reg = shark_write_reg,
0138     .read_reg  = shark_read_reg,
0139 };
0140 
0141 #ifdef SHARK_USE_LEDS
0142 static void shark_led_work(struct work_struct *work)
0143 {
0144     struct shark_device *shark =
0145         container_of(work, struct shark_device, led_work);
0146     int i, res, brightness, actual_len;
0147 
0148     for (i = 0; i < 2; i++) {
0149         if (!test_and_clear_bit(i, &shark->brightness_new))
0150             continue;
0151 
0152         brightness = atomic_read(&shark->brightness[i]);
0153         memset(shark->transfer_buffer, 0, TB_LEN);
0154         shark->transfer_buffer[0] = 0x83 + i;
0155         shark->transfer_buffer[1] = brightness;
0156         res = usb_interrupt_msg(shark->usbdev,
0157                     usb_sndintpipe(shark->usbdev,
0158                                SHARK_OUT_EP),
0159                     shark->transfer_buffer, TB_LEN,
0160                     &actual_len, 1000);
0161         if (res < 0)
0162             v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
0163                  shark->led_names[i], res);
0164     }
0165 }
0166 
0167 static void shark_led_set_blue(struct led_classdev *led_cdev,
0168                    enum led_brightness value)
0169 {
0170     struct shark_device *shark =
0171         container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
0172 
0173     atomic_set(&shark->brightness[BLUE_LED], value);
0174     set_bit(BLUE_LED, &shark->brightness_new);
0175     schedule_work(&shark->led_work);
0176 }
0177 
0178 static void shark_led_set_red(struct led_classdev *led_cdev,
0179                   enum led_brightness value)
0180 {
0181     struct shark_device *shark =
0182         container_of(led_cdev, struct shark_device, leds[RED_LED]);
0183 
0184     atomic_set(&shark->brightness[RED_LED], value);
0185     set_bit(RED_LED, &shark->brightness_new);
0186     schedule_work(&shark->led_work);
0187 }
0188 
0189 static const struct led_classdev shark_led_templates[NO_LEDS] = {
0190     [BLUE_LED] = {
0191         .name       = "%s:blue:",
0192         .brightness = LED_OFF,
0193         .max_brightness = 127,
0194         .brightness_set = shark_led_set_blue,
0195     },
0196     [RED_LED] = {
0197         .name       = "%s:red:",
0198         .brightness = LED_OFF,
0199         .max_brightness = 1,
0200         .brightness_set = shark_led_set_red,
0201     },
0202 };
0203 
0204 static int shark_register_leds(struct shark_device *shark, struct device *dev)
0205 {
0206     int i, retval;
0207 
0208     atomic_set(&shark->brightness[BLUE_LED], 127);
0209     INIT_WORK(&shark->led_work, shark_led_work);
0210     for (i = 0; i < NO_LEDS; i++) {
0211         shark->leds[i] = shark_led_templates[i];
0212         snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
0213              shark->leds[i].name, shark->v4l2_dev.name);
0214         shark->leds[i].name = shark->led_names[i];
0215         retval = led_classdev_register(dev, &shark->leds[i]);
0216         if (retval) {
0217             v4l2_err(&shark->v4l2_dev,
0218                  "couldn't register led: %s\n",
0219                  shark->led_names[i]);
0220             return retval;
0221         }
0222     }
0223     return 0;
0224 }
0225 
0226 static void shark_unregister_leds(struct shark_device *shark)
0227 {
0228     int i;
0229 
0230     for (i = 0; i < NO_LEDS; i++)
0231         led_classdev_unregister(&shark->leds[i]);
0232 
0233     cancel_work_sync(&shark->led_work);
0234 }
0235 
0236 static inline void shark_resume_leds(struct shark_device *shark)
0237 {
0238     int i;
0239 
0240     for (i = 0; i < NO_LEDS; i++)
0241         set_bit(i, &shark->brightness_new);
0242 
0243     schedule_work(&shark->led_work);
0244 }
0245 #else
0246 static int shark_register_leds(struct shark_device *shark, struct device *dev)
0247 {
0248     v4l2_warn(&shark->v4l2_dev,
0249           "CONFIG_LEDS_CLASS not enabled, LED support disabled\n");
0250     return 0;
0251 }
0252 static inline void shark_unregister_leds(struct shark_device *shark) { }
0253 static inline void shark_resume_leds(struct shark_device *shark) { }
0254 #endif
0255 
0256 static void usb_shark_disconnect(struct usb_interface *intf)
0257 {
0258     struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
0259     struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
0260 
0261     mutex_lock(&shark->tea.mutex);
0262     v4l2_device_disconnect(&shark->v4l2_dev);
0263     radio_tea5777_exit(&shark->tea);
0264     mutex_unlock(&shark->tea.mutex);
0265 
0266     shark_unregister_leds(shark);
0267 
0268     v4l2_device_put(&shark->v4l2_dev);
0269 }
0270 
0271 static void usb_shark_release(struct v4l2_device *v4l2_dev)
0272 {
0273     struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
0274 
0275     v4l2_device_unregister(&shark->v4l2_dev);
0276     kfree(shark->transfer_buffer);
0277     kfree(shark);
0278 }
0279 
0280 static int usb_shark_probe(struct usb_interface *intf,
0281                const struct usb_device_id *id)
0282 {
0283     struct shark_device *shark;
0284     int retval = -ENOMEM;
0285 
0286     shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
0287     if (!shark)
0288         return retval;
0289 
0290     shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
0291     if (!shark->transfer_buffer)
0292         goto err_alloc_buffer;
0293 
0294     v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
0295 
0296     retval = shark_register_leds(shark, &intf->dev);
0297     if (retval)
0298         goto err_reg_leds;
0299 
0300     shark->v4l2_dev.release = usb_shark_release;
0301     retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
0302     if (retval) {
0303         v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
0304         goto err_reg_dev;
0305     }
0306 
0307     shark->usbdev = interface_to_usbdev(intf);
0308     shark->tea.v4l2_dev = &shark->v4l2_dev;
0309     shark->tea.private_data = shark;
0310     shark->tea.ops = &shark_tea_ops;
0311     shark->tea.has_am = true;
0312     shark->tea.write_before_read = true;
0313     strscpy(shark->tea.card, "Griffin radioSHARK2",
0314         sizeof(shark->tea.card));
0315     usb_make_path(shark->usbdev, shark->tea.bus_info,
0316         sizeof(shark->tea.bus_info));
0317 
0318     retval = radio_tea5777_init(&shark->tea, THIS_MODULE);
0319     if (retval) {
0320         v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n");
0321         goto err_init_tea;
0322     }
0323 
0324     return 0;
0325 
0326 err_init_tea:
0327     v4l2_device_unregister(&shark->v4l2_dev);
0328 err_reg_dev:
0329     shark_unregister_leds(shark);
0330 err_reg_leds:
0331     kfree(shark->transfer_buffer);
0332 err_alloc_buffer:
0333     kfree(shark);
0334 
0335     return retval;
0336 }
0337 
0338 #ifdef CONFIG_PM
0339 static int usb_shark_suspend(struct usb_interface *intf, pm_message_t message)
0340 {
0341     return 0;
0342 }
0343 
0344 static int usb_shark_resume(struct usb_interface *intf)
0345 {
0346     struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
0347     struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
0348     int ret;
0349 
0350     mutex_lock(&shark->tea.mutex);
0351     ret = radio_tea5777_set_freq(&shark->tea);
0352     mutex_unlock(&shark->tea.mutex);
0353 
0354     shark_resume_leds(shark);
0355 
0356     return ret;
0357 }
0358 #endif
0359 
0360 /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
0361 static const struct usb_device_id usb_shark_device_table[] = {
0362     { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
0363              USB_DEVICE_ID_MATCH_INT_CLASS,
0364       .idVendor     = 0x077d,
0365       .idProduct    = 0x627a,
0366       .bcdDevice_lo = 0x0010,
0367       .bcdDevice_hi = 0x0010,
0368       .bInterfaceClass = 3,
0369     },
0370     { }
0371 };
0372 MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
0373 
0374 static struct usb_driver usb_shark_driver = {
0375     .name           = DRV_NAME,
0376     .probe          = usb_shark_probe,
0377     .disconnect     = usb_shark_disconnect,
0378     .id_table       = usb_shark_device_table,
0379 #ifdef CONFIG_PM
0380     .suspend        = usb_shark_suspend,
0381     .resume         = usb_shark_resume,
0382     .reset_resume       = usb_shark_resume,
0383 #endif
0384 };
0385 module_usb_driver(usb_shark_driver);