1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * TechnoTrend USB IR Receiver
5 * Copyright (C) 2012 Sean Young <sean@mess.org>
8 #include <linux/module.h>
10 #include <linux/usb/input.h>
11 #include <linux/slab.h>
12 #include <linux/leds.h>
13 #include <media/rc-core.h>
15 #define DRIVER_NAME "ttusbir"
16 #define DRIVER_DESC "TechnoTrend USB IR Receiver"
18 * The Windows driver uses 8 URBS, the original lirc drivers has a
19 * configurable amount (2 default, 4 max). This device generates about 125
20 * messages per second (!), whether IR is idle or not.
23 #define US_PER_BYTE 62
24 #define US_PER_BIT (US_PER_BYTE / 8)
29 struct usb_device
*udev
;
31 struct urb
*urb
[NUM_URBS
];
33 struct led_classdev led
;
35 uint8_t bulk_buffer
[5];
36 int bulk_out_endp
, iso_in_endp
;
37 bool led_on
, is_led_on
;
38 atomic_t led_complete
;
43 static enum led_brightness
ttusbir_brightness_get(struct led_classdev
*led_dev
)
45 struct ttusbir
*tt
= container_of(led_dev
, struct ttusbir
, led
);
47 return tt
->led_on
? LED_FULL
: LED_OFF
;
50 static void ttusbir_set_led(struct ttusbir
*tt
)
56 if (tt
->led_on
!= tt
->is_led_on
&& tt
->udev
&&
57 atomic_add_unless(&tt
->led_complete
, 1, 1)) {
58 tt
->bulk_buffer
[4] = tt
->is_led_on
= tt
->led_on
;
59 ret
= usb_submit_urb(tt
->bulk_urb
, GFP_ATOMIC
);
61 dev_warn(tt
->dev
, "failed to submit bulk urb: %d\n",
63 atomic_dec(&tt
->led_complete
);
68 static void ttusbir_brightness_set(struct led_classdev
*led_dev
, enum
69 led_brightness brightness
)
71 struct ttusbir
*tt
= container_of(led_dev
, struct ttusbir
, led
);
73 tt
->led_on
= brightness
!= LED_OFF
;
79 * The urb cannot be reused until the urb completes
81 static void ttusbir_bulk_complete(struct urb
*urb
)
83 struct ttusbir
*tt
= urb
->context
;
85 atomic_dec(&tt
->led_complete
);
87 switch (urb
->status
) {
96 dev_dbg(tt
->dev
, "Error: urb status = %d\n", urb
->status
);
104 * The data is one bit per sample, a set bit signifying silence and samples
105 * being MSB first. Bit 0 can contain garbage so take it to be whatever
106 * bit 1 is, so we don't have unexpected edges.
108 static void ttusbir_process_ir_data(struct ttusbir
*tt
, uint8_t *buf
)
110 struct ir_raw_event rawir
= {};
114 for (i
= 0; i
< 128; i
++) {
119 rawir
.duration
= US_PER_BYTE
;
120 if (ir_raw_event_store_with_filter(tt
->rc
, &rawir
))
125 rawir
.duration
= US_PER_BYTE
;
126 if (ir_raw_event_store_with_filter(tt
->rc
, &rawir
))
130 /* one edge per byte */
139 rawir
.duration
= US_PER_BIT
* (8 - b
);
140 if (ir_raw_event_store_with_filter(tt
->rc
, &rawir
))
143 rawir
.pulse
= !rawir
.pulse
;
144 rawir
.duration
= US_PER_BIT
* b
;
145 if (ir_raw_event_store_with_filter(tt
->rc
, &rawir
))
151 /* don't wakeup when there's nothing to do */
153 ir_raw_event_handle(tt
->rc
);
156 static void ttusbir_urb_complete(struct urb
*urb
)
158 struct ttusbir
*tt
= urb
->context
;
161 switch (urb
->status
) {
163 ttusbir_process_ir_data(tt
, urb
->transfer_buffer
);
171 dev_dbg(tt
->dev
, "Error: urb status = %d\n", urb
->status
);
175 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
176 if (rc
&& rc
!= -ENODEV
)
177 dev_warn(tt
->dev
, "failed to resubmit urb: %d\n", rc
);
180 static int ttusbir_probe(struct usb_interface
*intf
,
181 const struct usb_device_id
*id
)
184 struct usb_interface_descriptor
*idesc
;
185 struct usb_endpoint_descriptor
*desc
;
190 tt
= kzalloc(sizeof(*tt
), GFP_KERNEL
);
191 rc
= rc_allocate_device(RC_DRIVER_IR_RAW
);
197 /* find the correct alt setting */
198 for (i
= 0; i
< intf
->num_altsetting
&& altsetting
== -1; i
++) {
199 int max_packet
, bulk_out_endp
= -1, iso_in_endp
= -1;
201 idesc
= &intf
->altsetting
[i
].desc
;
203 for (j
= 0; j
< idesc
->bNumEndpoints
; j
++) {
204 desc
= &intf
->altsetting
[i
].endpoint
[j
].desc
;
205 max_packet
= le16_to_cpu(desc
->wMaxPacketSize
);
206 if (usb_endpoint_dir_in(desc
) &&
207 usb_endpoint_xfer_isoc(desc
) &&
210 else if (usb_endpoint_dir_out(desc
) &&
211 usb_endpoint_xfer_bulk(desc
) &&
215 if (bulk_out_endp
!= -1 && iso_in_endp
!= -1) {
216 tt
->bulk_out_endp
= bulk_out_endp
;
217 tt
->iso_in_endp
= iso_in_endp
;
224 if (altsetting
== -1) {
225 dev_err(&intf
->dev
, "cannot find expected altsetting\n");
230 tt
->dev
= &intf
->dev
;
231 tt
->udev
= interface_to_usbdev(intf
);
234 ret
= usb_set_interface(tt
->udev
, 0, altsetting
);
238 for (i
= 0; i
< NUM_URBS
; i
++) {
239 struct urb
*urb
= usb_alloc_urb(8, GFP_KERNEL
);
249 urb
->pipe
= usb_rcvisocpipe(tt
->udev
, tt
->iso_in_endp
);
251 buffer
= usb_alloc_coherent(tt
->udev
, 128, GFP_KERNEL
,
258 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
| URB_ISO_ASAP
;
259 urb
->transfer_buffer
= buffer
;
260 urb
->complete
= ttusbir_urb_complete
;
261 urb
->number_of_packets
= 8;
262 urb
->transfer_buffer_length
= 128;
264 for (j
= 0; j
< 8; j
++) {
265 urb
->iso_frame_desc
[j
].offset
= j
* 16;
266 urb
->iso_frame_desc
[j
].length
= 16;
272 tt
->bulk_urb
= usb_alloc_urb(0, GFP_KERNEL
);
278 tt
->bulk_buffer
[0] = 0xaa;
279 tt
->bulk_buffer
[1] = 0x01;
280 tt
->bulk_buffer
[2] = 0x05;
281 tt
->bulk_buffer
[3] = 0x01;
283 usb_fill_bulk_urb(tt
->bulk_urb
, tt
->udev
, usb_sndbulkpipe(tt
->udev
,
284 tt
->bulk_out_endp
), tt
->bulk_buffer
, sizeof(tt
->bulk_buffer
),
285 ttusbir_bulk_complete
, tt
);
287 tt
->led
.name
= "ttusbir:green:power";
288 tt
->led
.default_trigger
= "rc-feedback";
289 tt
->led
.brightness_set
= ttusbir_brightness_set
;
290 tt
->led
.brightness_get
= ttusbir_brightness_get
;
291 tt
->is_led_on
= tt
->led_on
= true;
292 atomic_set(&tt
->led_complete
, 0);
293 ret
= led_classdev_register(&intf
->dev
, &tt
->led
);
297 usb_make_path(tt
->udev
, tt
->phys
, sizeof(tt
->phys
));
299 rc
->device_name
= DRIVER_DESC
;
300 rc
->input_phys
= tt
->phys
;
301 usb_to_input_id(tt
->udev
, &rc
->input_id
);
302 rc
->dev
.parent
= &intf
->dev
;
303 rc
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
305 rc
->driver_name
= DRIVER_NAME
;
306 rc
->map_name
= RC_MAP_TT_1500
;
308 rc
->timeout
= IR_DEFAULT_TIMEOUT
;
309 rc
->max_timeout
= 10 * IR_DEFAULT_TIMEOUT
;
312 * The precision is US_PER_BIT, but since every 8th bit can be
313 * overwritten with garbage the accuracy is at best 2 * US_PER_BIT.
315 rc
->rx_resolution
= 2 * US_PER_BIT
;
317 ret
= rc_register_device(rc
);
319 dev_err(&intf
->dev
, "failed to register rc device %d\n", ret
);
323 usb_set_intfdata(intf
, tt
);
325 for (i
= 0; i
< NUM_URBS
; i
++) {
326 ret
= usb_submit_urb(tt
->urb
[i
], GFP_KERNEL
);
328 dev_err(tt
->dev
, "failed to submit urb %d\n", ret
);
335 rc_unregister_device(rc
);
338 led_classdev_unregister(&tt
->led
);
341 for (i
= 0; i
< NUM_URBS
&& tt
->urb
[i
]; i
++) {
342 struct urb
*urb
= tt
->urb
[i
];
345 usb_free_coherent(tt
->udev
, 128, urb
->transfer_buffer
,
349 usb_kill_urb(tt
->bulk_urb
);
350 usb_free_urb(tt
->bulk_urb
);
358 static void ttusbir_disconnect(struct usb_interface
*intf
)
360 struct ttusbir
*tt
= usb_get_intfdata(intf
);
361 struct usb_device
*udev
= tt
->udev
;
366 rc_unregister_device(tt
->rc
);
367 led_classdev_unregister(&tt
->led
);
368 for (i
= 0; i
< NUM_URBS
; i
++) {
369 usb_kill_urb(tt
->urb
[i
]);
370 usb_free_coherent(udev
, 128, tt
->urb
[i
]->transfer_buffer
,
371 tt
->urb
[i
]->transfer_dma
);
372 usb_free_urb(tt
->urb
[i
]);
374 usb_kill_urb(tt
->bulk_urb
);
375 usb_free_urb(tt
->bulk_urb
);
376 usb_set_intfdata(intf
, NULL
);
380 static int ttusbir_suspend(struct usb_interface
*intf
, pm_message_t message
)
382 struct ttusbir
*tt
= usb_get_intfdata(intf
);
385 for (i
= 0; i
< NUM_URBS
; i
++)
386 usb_kill_urb(tt
->urb
[i
]);
388 led_classdev_suspend(&tt
->led
);
389 usb_kill_urb(tt
->bulk_urb
);
394 static int ttusbir_resume(struct usb_interface
*intf
)
396 struct ttusbir
*tt
= usb_get_intfdata(intf
);
399 tt
->is_led_on
= true;
400 led_classdev_resume(&tt
->led
);
402 for (i
= 0; i
< NUM_URBS
; i
++) {
403 rc
= usb_submit_urb(tt
->urb
[i
], GFP_NOIO
);
405 dev_warn(tt
->dev
, "failed to submit urb: %d\n", rc
);
413 static const struct usb_device_id ttusbir_table
[] = {
414 { USB_DEVICE(0x0b48, 0x2003) },
418 static struct usb_driver ttusbir_driver
= {
420 .id_table
= ttusbir_table
,
421 .probe
= ttusbir_probe
,
422 .suspend
= ttusbir_suspend
,
423 .resume
= ttusbir_resume
,
424 .reset_resume
= ttusbir_resume
,
425 .disconnect
= ttusbir_disconnect
,
428 module_usb_driver(ttusbir_driver
);
430 MODULE_DESCRIPTION(DRIVER_DESC
);
431 MODULE_AUTHOR("Sean Young <sean@mess.org>");
432 MODULE_LICENSE("GPL");
433 MODULE_DEVICE_TABLE(usb
, ttusbir_table
);