diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-07 10:49:05 +0200 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-07 10:49:05 +0200 |
commit | 0b8e74c6f44094189dbe78baf4101acc7570c6af (patch) | |
tree | 6440561d09fb71ba5928664604ec92f29940be6b /drivers/media/rc/ttusbir.c | |
parent | Merge tag 'for-v3.7' of git://git.infradead.org/users/cbou/linux-pstore (diff) | |
parent | Merge branch 'staging/for_v3.7' into v4l_for_linus (diff) | |
download | linux-0b8e74c6f44094189dbe78baf4101acc7570c6af.tar.xz linux-0b8e74c6f44094189dbe78baf4101acc7570c6af.zip |
Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-media
Pull media updates from Mauro Carvalho Chehab:
"The first part of the media updates for Kernel 3.7.
This series contain:
- A major tree renaming patch series: now, drivers are organized
internally by their used bus, instead of by V4L2 and/or DVB API,
providing a cleaner driver location for hybrid drivers that
implement both APIs, and allowing to cleanup the Kconfig items and
make them more intuitive for the end user;
- Media Kernel developers are typically very lazy with their duties
of keeping the MAINTAINERS entries for their drivers updated. As
now the tree is more organized, we're doing an effort to add/update
those entries for the drivers that aren't currently orphan;
- Several DVB USB drivers got moved to a new DVB USB v2 core; the new
core fixes several bugs (as the existing one that got bitroted).
Now, suspend/resume finally started to work fine (at least with
some devices - we should expect more work with regards to it);
- added multistream support for DVB-T2, and unified the API for
DVB-S2 and ISDB-S. Backward binary support is preserved;
- as usual, a few new drivers, some V4L2 core improvements and lots
of drivers improvements and fixes.
There are some points to notice on this series:
1) you should expect a trivial merge conflict on your tree, with the
removal of Documentation/feature-removal-schedule.txt: this series
would be adding two additional entries there. I opted to not
rebase it due to this recent change;
2) With regards to the PCTV 520e udev-related breakage, I opted to
fix it in a way that the patches can be backported to 3.5 even
without your firmware fix patch. This way, Greg doesn't need to
rush backporting your patch (as there are still the firmware cache
and firmware path customization issues to be addressed there).
I'll send later a patch (likely after the end of the merge window)
reverting the rest of the DRX-K async firmware request, fully
restoring its original behaviour to allow media drivers to
initialize everything serialized as before for 3.7 and upper.
3) I'm planning to work on this weekend to test the DMABUF patches
for V4L2. The patches are on my queue for several Kernel cycles,
but, up to now, there is/was no way to test the series locally.
I have some concerns about this particular changeset with regards
to security issues, and with regards to the replacement of the old
VIDIOC_OVERLAY ioctl's that is broken on modern systems, due to
GPU drivers change. The Overlay API allows direct PCI2PCI
transfers from a media capture card into the GPU framebuffer, but
its API is crappy. Also, the only existing X11 driver that
implements it requires a XV extension that is not available
anymore on modern drivers. The DMABUF can do the same thing, but
with it is promising to be a properly-designed API. If I can
successfully test this series and be happy with it, I should be
asking you to pull them next week."
* 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-media: (717 commits)
em28xx: regression fix: use DRX-K sync firmware requests on em28xx
drxk: allow loading firmware synchrousnously
em28xx: Make all em28xx extensions to be initialized asynchronously
[media] tda18271: properly report read errors in tda18271_get_id
[media] tda18271: delay IR & RF calibration until init() if delay_cal is set
[media] MAINTAINERS: add Michael Krufky as tda827x maintainer
[media] MAINTAINERS: add Michael Krufky as tda8290 maintainer
[media] MAINTAINERS: add Michael Krufky as cxusb maintainer
[media] MAINTAINERS: add Michael Krufky as lg2160 maintainer
[media] MAINTAINERS: add Michael Krufky as lgdt3305 maintainer
[media] MAINTAINERS: add Michael Krufky as mxl111sf maintainer
[media] MAINTAINERS: add Michael Krufky as mxl5007t maintainer
[media] MAINTAINERS: add Michael Krufky as tda18271 maintainer
[media] s5p-tv: Report only multi-plane capabilities in vidioc_querycap
[media] s5p-mfc: Fix misplaced return statement in s5p_mfc_suspend()
[media] exynos-gsc: Add missing static storage class specifiers
[media] exynos-gsc: Remove <linux/version.h> header file inclusion
[media] s5p-fimc: Fix incorrect condition in fimc_lite_reqbufs()
[media] s5p-tv: Fix potential NULL pointer dereference error
[media] s5k6aa: Fix possible NULL pointer dereference
...
Diffstat (limited to 'drivers/media/rc/ttusbir.c')
-rw-r--r-- | drivers/media/rc/ttusbir.c | 447 |
1 files changed, 447 insertions, 0 deletions
diff --git a/drivers/media/rc/ttusbir.c b/drivers/media/rc/ttusbir.c new file mode 100644 index 000000000000..fef05235234a --- /dev/null +++ b/drivers/media/rc/ttusbir.c @@ -0,0 +1,447 @@ +/* + * TechnoTrend USB IR Receiver + * + * Copyright (C) 2012 Sean Young <sean@mess.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <linux/module.h> +#include <linux/usb.h> +#include <linux/usb/input.h> +#include <linux/slab.h> +#include <linux/leds.h> +#include <media/rc-core.h> + +#define DRIVER_NAME "ttusbir" +#define DRIVER_DESC "TechnoTrend USB IR Receiver" +/* + * The Windows driver uses 8 URBS, the original lirc drivers has a + * configurable amount (2 default, 4 max). This device generates about 125 + * messages per second (!), whether IR is idle or not. + */ +#define NUM_URBS 4 +#define NS_PER_BYTE 62500 +#define NS_PER_BIT (NS_PER_BYTE/8) + +struct ttusbir { + struct rc_dev *rc; + struct device *dev; + struct usb_device *udev; + + struct urb *urb[NUM_URBS]; + + struct led_classdev led; + struct urb *bulk_urb; + uint8_t bulk_buffer[5]; + int bulk_out_endp, iso_in_endp; + bool led_on, is_led_on; + atomic_t led_complete; + + char phys[64]; +}; + +static enum led_brightness ttusbir_brightness_get(struct led_classdev *led_dev) +{ + struct ttusbir *tt = container_of(led_dev, struct ttusbir, led); + + return tt->led_on ? LED_FULL : LED_OFF; +} + +static void ttusbir_set_led(struct ttusbir *tt) +{ + int ret; + + smp_mb(); + + if (tt->led_on != tt->is_led_on && tt->udev && + atomic_add_unless(&tt->led_complete, 1, 1)) { + tt->bulk_buffer[4] = tt->is_led_on = tt->led_on; + ret = usb_submit_urb(tt->bulk_urb, GFP_ATOMIC); + if (ret) { + dev_warn(tt->dev, "failed to submit bulk urb: %d\n", + ret); + atomic_dec(&tt->led_complete); + } + } +} + +static void ttusbir_brightness_set(struct led_classdev *led_dev, enum + led_brightness brightness) +{ + struct ttusbir *tt = container_of(led_dev, struct ttusbir, led); + + tt->led_on = brightness != LED_OFF; + + ttusbir_set_led(tt); +} + +/* + * The urb cannot be reused until the urb completes + */ +static void ttusbir_bulk_complete(struct urb *urb) +{ + struct ttusbir *tt = urb->context; + + atomic_dec(&tt->led_complete); + + switch (urb->status) { + case 0: + break; + case -ECONNRESET: + case -ENOENT: + case -ESHUTDOWN: + usb_unlink_urb(urb); + return; + case -EPIPE: + default: + dev_dbg(tt->dev, "Error: urb status = %d\n", urb->status); + break; + } + + ttusbir_set_led(tt); +} + +/* + * The data is one bit per sample, a set bit signifying silence and samples + * being MSB first. Bit 0 can contain garbage so take it to be whatever + * bit 1 is, so we don't have unexpected edges. + */ +static void ttusbir_process_ir_data(struct ttusbir *tt, uint8_t *buf) +{ + struct ir_raw_event rawir; + unsigned i, v, b; + bool event = false; + + init_ir_raw_event(&rawir); + + for (i = 0; i < 128; i++) { + v = buf[i] & 0xfe; + switch (v) { + case 0xfe: + rawir.pulse = false; + rawir.duration = NS_PER_BYTE; + if (ir_raw_event_store_with_filter(tt->rc, &rawir)) + event = true; + break; + case 0: + rawir.pulse = true; + rawir.duration = NS_PER_BYTE; + if (ir_raw_event_store_with_filter(tt->rc, &rawir)) + event = true; + break; + default: + /* one edge per byte */ + if (v & 2) { + b = ffz(v | 1); + rawir.pulse = true; + } else { + b = ffs(v) - 1; + rawir.pulse = false; + } + + rawir.duration = NS_PER_BIT * (8 - b); + if (ir_raw_event_store_with_filter(tt->rc, &rawir)) + event = true; + + rawir.pulse = !rawir.pulse; + rawir.duration = NS_PER_BIT * b; + if (ir_raw_event_store_with_filter(tt->rc, &rawir)) + event = true; + break; + } + } + + /* don't wakeup when there's nothing to do */ + if (event) + ir_raw_event_handle(tt->rc); +} + +static void ttusbir_urb_complete(struct urb *urb) +{ + struct ttusbir *tt = urb->context; + int rc; + + switch (urb->status) { + case 0: + ttusbir_process_ir_data(tt, urb->transfer_buffer); + break; + case -ECONNRESET: + case -ENOENT: + case -ESHUTDOWN: + usb_unlink_urb(urb); + return; + case -EPIPE: + default: + dev_dbg(tt->dev, "Error: urb status = %d\n", urb->status); + break; + } + + rc = usb_submit_urb(urb, GFP_ATOMIC); + if (rc && rc != -ENODEV) + dev_warn(tt->dev, "failed to resubmit urb: %d\n", rc); +} + +static int __devinit ttusbir_probe(struct usb_interface *intf, + const struct usb_device_id *id) +{ + struct ttusbir *tt; + struct usb_interface_descriptor *idesc; + struct usb_endpoint_descriptor *desc; + struct rc_dev *rc; + int i, j, ret; + int altsetting = -1; + + tt = kzalloc(sizeof(*tt), GFP_KERNEL); + rc = rc_allocate_device(); + if (!tt || !rc) { + ret = -ENOMEM; + goto out; + } + + /* find the correct alt setting */ + for (i = 0; i < intf->num_altsetting && altsetting == -1; i++) { + int bulk_out_endp = -1, iso_in_endp = -1; + + idesc = &intf->altsetting[i].desc; + + for (j = 0; j < idesc->bNumEndpoints; j++) { + desc = &intf->altsetting[i].endpoint[j].desc; + if (usb_endpoint_dir_in(desc) && + usb_endpoint_xfer_isoc(desc) && + desc->wMaxPacketSize == 0x10) + iso_in_endp = j; + else if (usb_endpoint_dir_out(desc) && + usb_endpoint_xfer_bulk(desc) && + desc->wMaxPacketSize == 0x20) + bulk_out_endp = j; + + if (bulk_out_endp != -1 && iso_in_endp != -1) { + tt->bulk_out_endp = bulk_out_endp; + tt->iso_in_endp = iso_in_endp; + altsetting = i; + break; + } + } + } + + if (altsetting == -1) { + dev_err(&intf->dev, "cannot find expected altsetting\n"); + ret = -ENODEV; + goto out; + } + + tt->dev = &intf->dev; + tt->udev = interface_to_usbdev(intf); + tt->rc = rc; + + ret = usb_set_interface(tt->udev, 0, altsetting); + if (ret) + goto out; + + for (i = 0; i < NUM_URBS; i++) { + struct urb *urb = usb_alloc_urb(8, GFP_KERNEL); + void *buffer; + + if (!urb) { + ret = -ENOMEM; + goto out; + } + + urb->dev = tt->udev; + urb->context = tt; + urb->pipe = usb_rcvisocpipe(tt->udev, tt->iso_in_endp); + urb->interval = 1; + buffer = usb_alloc_coherent(tt->udev, 128, GFP_KERNEL, + &urb->transfer_dma); + if (!buffer) { + usb_free_urb(urb); + ret = -ENOMEM; + goto out; + } + urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP | URB_ISO_ASAP; + urb->transfer_buffer = buffer; + urb->complete = ttusbir_urb_complete; + urb->number_of_packets = 8; + urb->transfer_buffer_length = 128; + + for (j = 0; j < 8; j++) { + urb->iso_frame_desc[j].offset = j * 16; + urb->iso_frame_desc[j].length = 16; + } + + tt->urb[i] = urb; + } + + tt->bulk_urb = usb_alloc_urb(0, GFP_KERNEL); + if (!tt->bulk_urb) { + ret = -ENOMEM; + goto out; + } + + tt->bulk_buffer[0] = 0xaa; + tt->bulk_buffer[1] = 0x01; + tt->bulk_buffer[2] = 0x05; + tt->bulk_buffer[3] = 0x01; + + usb_fill_bulk_urb(tt->bulk_urb, tt->udev, usb_sndbulkpipe(tt->udev, + tt->bulk_out_endp), tt->bulk_buffer, sizeof(tt->bulk_buffer), + ttusbir_bulk_complete, tt); + + tt->led.name = "ttusbir:green:power"; + tt->led.brightness_set = ttusbir_brightness_set; + tt->led.brightness_get = ttusbir_brightness_get; + tt->is_led_on = tt->led_on = true; + atomic_set(&tt->led_complete, 0); + ret = led_classdev_register(&intf->dev, &tt->led); + if (ret) + goto out; + + usb_make_path(tt->udev, tt->phys, sizeof(tt->phys)); + + rc->input_name = DRIVER_DESC; + rc->input_phys = tt->phys; + usb_to_input_id(tt->udev, &rc->input_id); + rc->dev.parent = &intf->dev; + rc->driver_type = RC_DRIVER_IR_RAW; + rc->allowed_protos = RC_TYPE_ALL; + rc->priv = tt; + rc->driver_name = DRIVER_NAME; + rc->map_name = RC_MAP_TT_1500; + rc->timeout = MS_TO_NS(100); + /* + * The precision is NS_PER_BIT, but since every 8th bit can be + * overwritten with garbage the accuracy is at best 2 * NS_PER_BIT. + */ + rc->rx_resolution = NS_PER_BIT; + + ret = rc_register_device(rc); + if (ret) { + dev_err(&intf->dev, "failed to register rc device %d\n", ret); + goto out2; + } + + usb_set_intfdata(intf, tt); + + for (i = 0; i < NUM_URBS; i++) { + ret = usb_submit_urb(tt->urb[i], GFP_KERNEL); + if (ret) { + dev_err(tt->dev, "failed to submit urb %d\n", ret); + goto out3; + } + } + + return 0; +out3: + rc_unregister_device(rc); +out2: + led_classdev_unregister(&tt->led); +out: + if (tt) { + for (i = 0; i < NUM_URBS && tt->urb[i]; i++) { + struct urb *urb = tt->urb[i]; + + usb_kill_urb(urb); + usb_free_coherent(tt->udev, 128, urb->transfer_buffer, + urb->transfer_dma); + usb_free_urb(urb); + } + usb_kill_urb(tt->bulk_urb); + usb_free_urb(tt->bulk_urb); + kfree(tt); + } + rc_free_device(rc); + + return ret; +} + +static void __devexit ttusbir_disconnect(struct usb_interface *intf) +{ + struct ttusbir *tt = usb_get_intfdata(intf); + struct usb_device *udev = tt->udev; + int i; + + tt->udev = NULL; + + rc_unregister_device(tt->rc); + led_classdev_unregister(&tt->led); + for (i = 0; i < NUM_URBS; i++) { + usb_kill_urb(tt->urb[i]); + usb_free_coherent(udev, 128, tt->urb[i]->transfer_buffer, + tt->urb[i]->transfer_dma); + usb_free_urb(tt->urb[i]); + } + usb_kill_urb(tt->bulk_urb); + usb_free_urb(tt->bulk_urb); + usb_set_intfdata(intf, NULL); + kfree(tt); +} + +static int ttusbir_suspend(struct usb_interface *intf, pm_message_t message) +{ + struct ttusbir *tt = usb_get_intfdata(intf); + int i; + + for (i = 0; i < NUM_URBS; i++) + usb_kill_urb(tt->urb[i]); + + led_classdev_suspend(&tt->led); + usb_kill_urb(tt->bulk_urb); + + return 0; +} + +static int ttusbir_resume(struct usb_interface *intf) +{ + struct ttusbir *tt = usb_get_intfdata(intf); + int i, rc; + + led_classdev_resume(&tt->led); + tt->is_led_on = true; + ttusbir_set_led(tt); + + for (i = 0; i < NUM_URBS; i++) { + rc = usb_submit_urb(tt->urb[i], GFP_KERNEL); + if (rc) { + dev_warn(tt->dev, "failed to submit urb: %d\n", rc); + break; + } + } + + return rc; +} + +static const struct usb_device_id ttusbir_table[] = { + { USB_DEVICE(0x0b48, 0x2003) }, + { } +}; + +static struct usb_driver ttusbir_driver = { + .name = DRIVER_NAME, + .id_table = ttusbir_table, + .probe = ttusbir_probe, + .suspend = ttusbir_suspend, + .resume = ttusbir_resume, + .reset_resume = ttusbir_resume, + .disconnect = __devexit_p(ttusbir_disconnect) +}; + +module_usb_driver(ttusbir_driver); + +MODULE_DESCRIPTION(DRIVER_DESC); +MODULE_AUTHOR("Sean Young <sean@mess.org>"); +MODULE_LICENSE("GPL"); +MODULE_DEVICE_TABLE(usb, ttusbir_table); + |