xref: /openbmc/linux/drivers/media/rc/iguanair.c (revision f05643a0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * IguanaWorks USB IR Transceiver support
4  *
5  * Copyright (C) 2012 Sean Young <sean@mess.org>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/usb/input.h>
13 #include <linux/slab.h>
14 #include <linux/completion.h>
15 #include <media/rc-core.h>
16 
17 #define BUF_SIZE 152
18 
19 struct iguanair {
20 	struct rc_dev *rc;
21 
22 	struct device *dev;
23 	struct usb_device *udev;
24 
25 	uint16_t version;
26 	uint8_t bufsize;
27 	uint8_t cycle_overhead;
28 
29 	/* receiver support */
30 	bool receiver_on;
31 	dma_addr_t dma_in, dma_out;
32 	uint8_t *buf_in;
33 	struct urb *urb_in, *urb_out;
34 	struct completion completion;
35 
36 	/* transmit support */
37 	bool tx_overflow;
38 	uint32_t carrier;
39 	struct send_packet *packet;
40 
41 	char name[64];
42 	char phys[64];
43 };
44 
45 #define CMD_NOP			0x00
46 #define CMD_GET_VERSION		0x01
47 #define CMD_GET_BUFSIZE		0x11
48 #define CMD_GET_FEATURES	0x10
49 #define CMD_SEND		0x15
50 #define CMD_EXECUTE		0x1f
51 #define CMD_RX_OVERFLOW		0x31
52 #define CMD_TX_OVERFLOW		0x32
53 #define CMD_RECEIVER_ON		0x12
54 #define CMD_RECEIVER_OFF	0x14
55 
56 #define DIR_IN			0xdc
57 #define DIR_OUT			0xcd
58 
59 #define MAX_IN_PACKET		8u
60 #define MAX_OUT_PACKET		(sizeof(struct send_packet) + BUF_SIZE)
61 #define TIMEOUT			1000
62 #define RX_RESOLUTION		21
63 
64 struct packet {
65 	uint16_t start;
66 	uint8_t direction;
67 	uint8_t cmd;
68 };
69 
70 struct send_packet {
71 	struct packet header;
72 	uint8_t length;
73 	uint8_t channels;
74 	uint8_t busy7;
75 	uint8_t busy4;
76 	uint8_t payload[];
77 };
78 
79 static void process_ir_data(struct iguanair *ir, unsigned len)
80 {
81 	if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
82 		switch (ir->buf_in[3]) {
83 		case CMD_GET_VERSION:
84 			if (len == 6) {
85 				ir->version = (ir->buf_in[5] << 8) |
86 							ir->buf_in[4];
87 				complete(&ir->completion);
88 			}
89 			break;
90 		case CMD_GET_BUFSIZE:
91 			if (len >= 5) {
92 				ir->bufsize = ir->buf_in[4];
93 				complete(&ir->completion);
94 			}
95 			break;
96 		case CMD_GET_FEATURES:
97 			if (len > 5) {
98 				ir->cycle_overhead = ir->buf_in[5];
99 				complete(&ir->completion);
100 			}
101 			break;
102 		case CMD_TX_OVERFLOW:
103 			ir->tx_overflow = true;
104 			fallthrough;
105 		case CMD_RECEIVER_OFF:
106 		case CMD_RECEIVER_ON:
107 		case CMD_SEND:
108 			complete(&ir->completion);
109 			break;
110 		case CMD_RX_OVERFLOW:
111 			dev_warn(ir->dev, "receive overflow\n");
112 			ir_raw_event_overflow(ir->rc);
113 			break;
114 		default:
115 			dev_warn(ir->dev, "control code %02x received\n",
116 							ir->buf_in[3]);
117 			break;
118 		}
119 	} else if (len >= 7) {
120 		struct ir_raw_event rawir = {};
121 		unsigned i;
122 		bool event = false;
123 
124 		for (i = 0; i < 7; i++) {
125 			if (ir->buf_in[i] == 0x80) {
126 				rawir.pulse = false;
127 				rawir.duration = 21845;
128 			} else {
129 				rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
130 				rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
131 								 RX_RESOLUTION;
132 			}
133 
134 			if (ir_raw_event_store_with_filter(ir->rc, &rawir))
135 				event = true;
136 		}
137 
138 		if (event)
139 			ir_raw_event_handle(ir->rc);
140 	}
141 }
142 
143 static void iguanair_rx(struct urb *urb)
144 {
145 	struct iguanair *ir;
146 	int rc;
147 
148 	if (!urb)
149 		return;
150 
151 	ir = urb->context;
152 	if (!ir) {
153 		usb_unlink_urb(urb);
154 		return;
155 	}
156 
157 	switch (urb->status) {
158 	case 0:
159 		process_ir_data(ir, urb->actual_length);
160 		break;
161 	case -ECONNRESET:
162 	case -ENOENT:
163 	case -ESHUTDOWN:
164 		usb_unlink_urb(urb);
165 		return;
166 	case -EPIPE:
167 	default:
168 		dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
169 		break;
170 	}
171 
172 	rc = usb_submit_urb(urb, GFP_ATOMIC);
173 	if (rc && rc != -ENODEV)
174 		dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
175 }
176 
177 static void iguanair_irq_out(struct urb *urb)
178 {
179 	struct iguanair *ir = urb->context;
180 
181 	if (urb->status)
182 		dev_dbg(ir->dev, "Error: out urb status = %d\n", urb->status);
183 
184 	/* if we sent an nop packet, do not expect a response */
185 	if (urb->status == 0 && ir->packet->header.cmd == CMD_NOP)
186 		complete(&ir->completion);
187 }
188 
189 static int iguanair_send(struct iguanair *ir, unsigned size)
190 {
191 	int rc;
192 
193 	reinit_completion(&ir->completion);
194 
195 	ir->urb_out->transfer_buffer_length = size;
196 	rc = usb_submit_urb(ir->urb_out, GFP_KERNEL);
197 	if (rc)
198 		return rc;
199 
200 	if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
201 		return -ETIMEDOUT;
202 
203 	return rc;
204 }
205 
206 static int iguanair_get_features(struct iguanair *ir)
207 {
208 	int rc;
209 
210 	/*
211 	 * On cold boot, the iguanair initializes on the first packet
212 	 * received but does not process that packet. Send an empty
213 	 * packet.
214 	 */
215 	ir->packet->header.start = 0;
216 	ir->packet->header.direction = DIR_OUT;
217 	ir->packet->header.cmd = CMD_NOP;
218 	iguanair_send(ir, sizeof(ir->packet->header));
219 
220 	ir->packet->header.cmd = CMD_GET_VERSION;
221 	rc = iguanair_send(ir, sizeof(ir->packet->header));
222 	if (rc) {
223 		dev_info(ir->dev, "failed to get version\n");
224 		goto out;
225 	}
226 
227 	if (ir->version < 0x205) {
228 		dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
229 		rc = -ENODEV;
230 		goto out;
231 	}
232 
233 	ir->bufsize = 150;
234 	ir->cycle_overhead = 65;
235 
236 	ir->packet->header.cmd = CMD_GET_BUFSIZE;
237 
238 	rc = iguanair_send(ir, sizeof(ir->packet->header));
239 	if (rc) {
240 		dev_info(ir->dev, "failed to get buffer size\n");
241 		goto out;
242 	}
243 
244 	if (ir->bufsize > BUF_SIZE) {
245 		dev_info(ir->dev, "buffer size %u larger than expected\n",
246 								ir->bufsize);
247 		ir->bufsize = BUF_SIZE;
248 	}
249 
250 	ir->packet->header.cmd = CMD_GET_FEATURES;
251 
252 	rc = iguanair_send(ir, sizeof(ir->packet->header));
253 	if (rc)
254 		dev_info(ir->dev, "failed to get features\n");
255 out:
256 	return rc;
257 }
258 
259 static int iguanair_receiver(struct iguanair *ir, bool enable)
260 {
261 	ir->packet->header.start = 0;
262 	ir->packet->header.direction = DIR_OUT;
263 	ir->packet->header.cmd = enable ? CMD_RECEIVER_ON : CMD_RECEIVER_OFF;
264 
265 	return iguanair_send(ir, sizeof(ir->packet->header));
266 }
267 
268 /*
269  * The iguanair creates the carrier by busy spinning after each half period.
270  * This is counted in CPU cycles, with the CPU running at 24MHz. It is
271  * broken down into 7-cycles and 4-cyles delays, with a preference for
272  * 4-cycle delays, minus the overhead of the loop itself (cycle_overhead).
273  */
274 static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
275 {
276 	struct iguanair *ir = dev->priv;
277 
278 	if (carrier < 25000 || carrier > 150000)
279 		return -EINVAL;
280 
281 	if (carrier != ir->carrier) {
282 		uint32_t cycles, fours, sevens;
283 
284 		ir->carrier = carrier;
285 
286 		cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
287 							ir->cycle_overhead;
288 
289 		/*
290 		 * Calculate minimum number of 7 cycles needed so
291 		 * we are left with a multiple of 4; so we want to have
292 		 * (sevens * 7) & 3 == cycles & 3
293 		 */
294 		sevens = (4 - cycles) & 3;
295 		fours = (cycles - sevens * 7) / 4;
296 
297 		/*
298 		 * The firmware interprets these values as a relative offset
299 		 * for a branch. Immediately following the branches, there
300 		 * 4 instructions of 7 cycles (2 bytes each) and 110
301 		 * instructions of 4 cycles (1 byte each). A relative branch
302 		 * of 0 will execute all of them, branch further for less
303 		 * cycle burning.
304 		 */
305 		ir->packet->busy7 = (4 - sevens) * 2;
306 		ir->packet->busy4 = 110 - fours;
307 	}
308 
309 	return 0;
310 }
311 
312 static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
313 {
314 	struct iguanair *ir = dev->priv;
315 
316 	if (mask > 15)
317 		return 4;
318 
319 	ir->packet->channels = mask << 4;
320 
321 	return 0;
322 }
323 
324 static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
325 {
326 	struct iguanair *ir = dev->priv;
327 	unsigned int i, size, p, periods;
328 	int rc;
329 
330 	/* convert from us to carrier periods */
331 	for (i = size = 0; i < count; i++) {
332 		periods = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
333 		while (periods) {
334 			p = min(periods, 127u);
335 			if (size >= ir->bufsize) {
336 				rc = -EINVAL;
337 				goto out;
338 			}
339 			ir->packet->payload[size++] = p | ((i & 1) ? 0x80 : 0);
340 			periods -= p;
341 		}
342 	}
343 
344 	ir->packet->header.start = 0;
345 	ir->packet->header.direction = DIR_OUT;
346 	ir->packet->header.cmd = CMD_SEND;
347 	ir->packet->length = size;
348 
349 	ir->tx_overflow = false;
350 
351 	rc = iguanair_send(ir, sizeof(*ir->packet) + size);
352 
353 	if (rc == 0 && ir->tx_overflow)
354 		rc = -EOVERFLOW;
355 
356 out:
357 	return rc ? rc : count;
358 }
359 
360 static int iguanair_open(struct rc_dev *rdev)
361 {
362 	struct iguanair *ir = rdev->priv;
363 	int rc;
364 
365 	rc = iguanair_receiver(ir, true);
366 	if (rc == 0)
367 		ir->receiver_on = true;
368 
369 	return rc;
370 }
371 
372 static void iguanair_close(struct rc_dev *rdev)
373 {
374 	struct iguanair *ir = rdev->priv;
375 	int rc;
376 
377 	rc = iguanair_receiver(ir, false);
378 	ir->receiver_on = false;
379 	if (rc && rc != -ENODEV)
380 		dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
381 }
382 
383 static int iguanair_probe(struct usb_interface *intf,
384 			  const struct usb_device_id *id)
385 {
386 	struct usb_device *udev = interface_to_usbdev(intf);
387 	struct iguanair *ir;
388 	struct rc_dev *rc;
389 	int ret, pipein, pipeout;
390 	struct usb_host_interface *idesc;
391 
392 	idesc = intf->cur_altsetting;
393 	if (idesc->desc.bNumEndpoints < 2)
394 		return -ENODEV;
395 
396 	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
397 	rc = rc_allocate_device(RC_DRIVER_IR_RAW);
398 	if (!ir || !rc) {
399 		ret = -ENOMEM;
400 		goto out;
401 	}
402 
403 	ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL,
404 								&ir->dma_in);
405 	ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL,
406 								&ir->dma_out);
407 	ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
408 	ir->urb_out = usb_alloc_urb(0, GFP_KERNEL);
409 
410 	if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out ||
411 	    !usb_endpoint_is_int_in(&idesc->endpoint[0].desc) ||
412 	    !usb_endpoint_is_int_out(&idesc->endpoint[1].desc)) {
413 		ret = -ENOMEM;
414 		goto out;
415 	}
416 
417 	ir->rc = rc;
418 	ir->dev = &intf->dev;
419 	ir->udev = udev;
420 
421 	init_completion(&ir->completion);
422 	pipeout = usb_sndintpipe(udev,
423 				idesc->endpoint[1].desc.bEndpointAddress);
424 	usb_fill_int_urb(ir->urb_out, udev, pipeout, ir->packet, MAX_OUT_PACKET,
425 						iguanair_irq_out, ir, 1);
426 	ir->urb_out->transfer_dma = ir->dma_out;
427 	ir->urb_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
428 
429 	pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
430 	usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_IN_PACKET,
431 							 iguanair_rx, ir, 1);
432 	ir->urb_in->transfer_dma = ir->dma_in;
433 	ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
434 
435 	ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
436 	if (ret) {
437 		dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
438 		goto out;
439 	}
440 
441 	ret = iguanair_get_features(ir);
442 	if (ret)
443 		goto out2;
444 
445 	snprintf(ir->name, sizeof(ir->name),
446 		"IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
447 
448 	usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
449 
450 	rc->device_name = ir->name;
451 	rc->input_phys = ir->phys;
452 	usb_to_input_id(ir->udev, &rc->input_id);
453 	rc->dev.parent = &intf->dev;
454 	rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
455 	rc->priv = ir;
456 	rc->open = iguanair_open;
457 	rc->close = iguanair_close;
458 	rc->s_tx_mask = iguanair_set_tx_mask;
459 	rc->s_tx_carrier = iguanair_set_tx_carrier;
460 	rc->tx_ir = iguanair_tx;
461 	rc->driver_name = KBUILD_MODNAME;
462 	rc->map_name = RC_MAP_RC6_MCE;
463 	rc->min_timeout = 1;
464 	rc->timeout = IR_DEFAULT_TIMEOUT;
465 	rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
466 	rc->rx_resolution = RX_RESOLUTION;
467 
468 	iguanair_set_tx_carrier(rc, 38000);
469 	iguanair_set_tx_mask(rc, 0);
470 
471 	ret = rc_register_device(rc);
472 	if (ret < 0) {
473 		dev_err(&intf->dev, "failed to register rc device %d", ret);
474 		goto out2;
475 	}
476 
477 	usb_set_intfdata(intf, ir);
478 
479 	return 0;
480 out2:
481 	usb_kill_urb(ir->urb_in);
482 	usb_kill_urb(ir->urb_out);
483 out:
484 	if (ir) {
485 		usb_free_urb(ir->urb_in);
486 		usb_free_urb(ir->urb_out);
487 		usb_free_coherent(udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
488 		usb_free_coherent(udev, MAX_OUT_PACKET, ir->packet,
489 								ir->dma_out);
490 	}
491 	rc_free_device(rc);
492 	kfree(ir);
493 	return ret;
494 }
495 
496 static void iguanair_disconnect(struct usb_interface *intf)
497 {
498 	struct iguanair *ir = usb_get_intfdata(intf);
499 
500 	rc_unregister_device(ir->rc);
501 	usb_set_intfdata(intf, NULL);
502 	usb_kill_urb(ir->urb_in);
503 	usb_kill_urb(ir->urb_out);
504 	usb_free_urb(ir->urb_in);
505 	usb_free_urb(ir->urb_out);
506 	usb_free_coherent(ir->udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
507 	usb_free_coherent(ir->udev, MAX_OUT_PACKET, ir->packet, ir->dma_out);
508 	kfree(ir);
509 }
510 
511 static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
512 {
513 	struct iguanair *ir = usb_get_intfdata(intf);
514 	int rc = 0;
515 
516 	if (ir->receiver_on) {
517 		rc = iguanair_receiver(ir, false);
518 		if (rc)
519 			dev_warn(ir->dev, "failed to disable receiver for suspend\n");
520 	}
521 
522 	usb_kill_urb(ir->urb_in);
523 	usb_kill_urb(ir->urb_out);
524 
525 	return rc;
526 }
527 
528 static int iguanair_resume(struct usb_interface *intf)
529 {
530 	struct iguanair *ir = usb_get_intfdata(intf);
531 	int rc;
532 
533 	rc = usb_submit_urb(ir->urb_in, GFP_KERNEL);
534 	if (rc)
535 		dev_warn(&intf->dev, "failed to submit urb: %d\n", rc);
536 
537 	if (ir->receiver_on) {
538 		rc = iguanair_receiver(ir, true);
539 		if (rc)
540 			dev_warn(ir->dev, "failed to enable receiver after resume\n");
541 	}
542 
543 	return rc;
544 }
545 
546 static const struct usb_device_id iguanair_table[] = {
547 	{ USB_DEVICE(0x1781, 0x0938) },
548 	{ }
549 };
550 
551 static struct usb_driver iguanair_driver = {
552 	.name =	KBUILD_MODNAME,
553 	.probe = iguanair_probe,
554 	.disconnect = iguanair_disconnect,
555 	.suspend = iguanair_suspend,
556 	.resume = iguanair_resume,
557 	.reset_resume = iguanair_resume,
558 	.id_table = iguanair_table,
559 	.soft_unbind = 1	/* we want to disable receiver on unbind */
560 };
561 
562 module_usb_driver(iguanair_driver);
563 
564 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
565 MODULE_AUTHOR("Sean Young <sean@mess.org>");
566 MODULE_LICENSE("GPL");
567 MODULE_DEVICE_TABLE(usb, iguanair_table);
568 
569