Commit | Line | Data |
---|---|---|
3dff3106 HV |
1 | /* |
2 | * Pulse Eight HDMI CEC driver | |
3 | * | |
4 | * Copyright 2016 Hans Verkuil <hverkuil@xs4all.nl | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version of 2 of the License, or (at your | |
9 | * option) any later version. See the file COPYING in the main directory of | |
10 | * this archive for more details. | |
11 | */ | |
12 | ||
89c2e228 JF |
13 | /* |
14 | * Notes: | |
15 | * | |
16 | * - Devices with firmware version < 2 do not store their configuration in | |
17 | * EEPROM. | |
18 | * | |
19 | * - In autonomous mode, only messages from a TV will be acknowledged, even | |
20 | * polling messages. Upon receiving a message from a TV, the dongle will | |
21 | * respond to messages from any logical address. | |
22 | * | |
23 | * - In autonomous mode, the dongle will by default reply Feature Abort | |
24 | * [Unrecognized Opcode] when it receives Give Device Vendor ID. It will | |
25 | * however observe vendor ID's reported by other devices and possibly | |
26 | * alter this behavior. When TV's (and TV's only) report that their vendor ID | |
27 | * is LG (0x00e091), the dongle will itself reply that it has the same vendor | |
28 | * ID, and it will respond to at least one vendor specific command. | |
29 | * | |
30 | * - In autonomous mode, the dongle is known to attempt wakeup if it receives | |
31 | * <User Control Pressed> ["Power On"], ["Power] or ["Power Toggle"], or if it | |
32 | * receives <Set Stream Path> with its own physical address. It also does this | |
33 | * if it receives <Vendor Specific Command> [0x03 0x00] from an LG TV. | |
34 | */ | |
35 | ||
3dff3106 HV |
36 | #include <linux/completion.h> |
37 | #include <linux/init.h> | |
38 | #include <linux/interrupt.h> | |
39 | #include <linux/kernel.h> | |
40 | #include <linux/module.h> | |
41 | #include <linux/workqueue.h> | |
42 | #include <linux/serio.h> | |
43 | #include <linux/slab.h> | |
44 | #include <linux/time.h> | |
45 | #include <linux/delay.h> | |
46 | ||
47 | #include <media/cec.h> | |
48 | ||
49 | MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>"); | |
50 | MODULE_DESCRIPTION("Pulse Eight HDMI CEC driver"); | |
51 | MODULE_LICENSE("GPL"); | |
52 | ||
53 | static int debug; | |
e28a6c8b | 54 | static int persistent_config = 1; |
3dff3106 | 55 | module_param(debug, int, 0644); |
e28a6c8b | 56 | module_param(persistent_config, int, 0644); |
3dff3106 | 57 | MODULE_PARM_DESC(debug, "debug level (0-1)"); |
e28a6c8b | 58 | MODULE_PARM_DESC(persistent_config, "read config from persistent memory (0-1)"); |
3dff3106 HV |
59 | |
60 | enum pulse8_msgcodes { | |
61 | MSGCODE_NOTHING = 0, | |
62 | MSGCODE_PING, | |
63 | MSGCODE_TIMEOUT_ERROR, | |
64 | MSGCODE_HIGH_ERROR, | |
65 | MSGCODE_LOW_ERROR, | |
66 | MSGCODE_FRAME_START, | |
67 | MSGCODE_FRAME_DATA, | |
68 | MSGCODE_RECEIVE_FAILED, | |
69 | MSGCODE_COMMAND_ACCEPTED, /* 0x08 */ | |
70 | MSGCODE_COMMAND_REJECTED, | |
71 | MSGCODE_SET_ACK_MASK, | |
72 | MSGCODE_TRANSMIT, | |
73 | MSGCODE_TRANSMIT_EOM, | |
74 | MSGCODE_TRANSMIT_IDLETIME, | |
75 | MSGCODE_TRANSMIT_ACK_POLARITY, | |
76 | MSGCODE_TRANSMIT_LINE_TIMEOUT, | |
77 | MSGCODE_TRANSMIT_SUCCEEDED, /* 0x10 */ | |
78 | MSGCODE_TRANSMIT_FAILED_LINE, | |
79 | MSGCODE_TRANSMIT_FAILED_ACK, | |
80 | MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA, | |
81 | MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE, | |
82 | MSGCODE_FIRMWARE_VERSION, | |
83 | MSGCODE_START_BOOTLOADER, | |
84 | MSGCODE_GET_BUILDDATE, | |
85 | MSGCODE_SET_CONTROLLED, /* 0x18 */ | |
86 | MSGCODE_GET_AUTO_ENABLED, | |
87 | MSGCODE_SET_AUTO_ENABLED, | |
88 | MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS, | |
89 | MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS, | |
90 | MSGCODE_GET_LOGICAL_ADDRESS_MASK, | |
91 | MSGCODE_SET_LOGICAL_ADDRESS_MASK, | |
92 | MSGCODE_GET_PHYSICAL_ADDRESS, | |
93 | MSGCODE_SET_PHYSICAL_ADDRESS, /* 0x20 */ | |
94 | MSGCODE_GET_DEVICE_TYPE, | |
95 | MSGCODE_SET_DEVICE_TYPE, | |
96 | MSGCODE_GET_HDMI_VERSION, | |
97 | MSGCODE_SET_HDMI_VERSION, | |
98 | MSGCODE_GET_OSD_NAME, | |
99 | MSGCODE_SET_OSD_NAME, | |
100 | MSGCODE_WRITE_EEPROM, | |
101 | MSGCODE_GET_ADAPTER_TYPE, /* 0x28 */ | |
102 | MSGCODE_SET_ACTIVE_SOURCE, | |
103 | ||
104 | MSGCODE_FRAME_EOM = 0x80, | |
105 | MSGCODE_FRAME_ACK = 0x40, | |
106 | }; | |
107 | ||
108 | #define MSGSTART 0xff | |
109 | #define MSGEND 0xfe | |
110 | #define MSGESC 0xfd | |
111 | #define MSGOFFSET 3 | |
112 | ||
113 | #define DATA_SIZE 256 | |
114 | ||
e28a6c8b JF |
115 | #define PING_PERIOD (15 * HZ) |
116 | ||
3dff3106 HV |
117 | struct pulse8 { |
118 | struct device *dev; | |
119 | struct serio *serio; | |
120 | struct cec_adapter *adap; | |
e28a6c8b | 121 | unsigned int vers; |
3dff3106 HV |
122 | struct completion cmd_done; |
123 | struct work_struct work; | |
e28a6c8b | 124 | struct delayed_work ping_eeprom_work; |
3dff3106 HV |
125 | struct cec_msg rx_msg; |
126 | u8 data[DATA_SIZE]; | |
127 | unsigned int len; | |
128 | u8 buf[DATA_SIZE]; | |
129 | unsigned int idx; | |
130 | bool escape; | |
131 | bool started; | |
e28a6c8b | 132 | struct mutex config_lock; |
05f3e58b | 133 | struct mutex write_lock; |
e28a6c8b JF |
134 | bool config_pending; |
135 | bool restoring_config; | |
136 | bool autonomous; | |
3dff3106 HV |
137 | }; |
138 | ||
e28a6c8b JF |
139 | static void pulse8_ping_eeprom_work_handler(struct work_struct *work); |
140 | ||
9d01315d | 141 | static void pulse8_irq_work_handler(struct work_struct *work) |
3dff3106 HV |
142 | { |
143 | struct pulse8 *pulse8 = | |
144 | container_of(work, struct pulse8, work); | |
145 | ||
146 | switch (pulse8->data[0] & 0x3f) { | |
147 | case MSGCODE_FRAME_DATA: | |
148 | cec_received_msg(pulse8->adap, &pulse8->rx_msg); | |
149 | break; | |
150 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
151 | cec_transmit_done(pulse8->adap, CEC_TX_STATUS_OK, | |
152 | 0, 0, 0, 0); | |
153 | break; | |
3dff3106 HV |
154 | case MSGCODE_TRANSMIT_FAILED_ACK: |
155 | cec_transmit_done(pulse8->adap, CEC_TX_STATUS_NACK, | |
156 | 0, 1, 0, 0); | |
157 | break; | |
31f58e31 | 158 | case MSGCODE_TRANSMIT_FAILED_LINE: |
3dff3106 HV |
159 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: |
160 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: | |
161 | cec_transmit_done(pulse8->adap, CEC_TX_STATUS_ERROR, | |
162 | 0, 0, 0, 1); | |
163 | break; | |
164 | } | |
165 | } | |
166 | ||
167 | static irqreturn_t pulse8_interrupt(struct serio *serio, unsigned char data, | |
168 | unsigned int flags) | |
169 | { | |
170 | struct pulse8 *pulse8 = serio_get_drvdata(serio); | |
171 | ||
172 | if (!pulse8->started && data != MSGSTART) | |
173 | return IRQ_HANDLED; | |
174 | if (data == MSGESC) { | |
175 | pulse8->escape = true; | |
176 | return IRQ_HANDLED; | |
177 | } | |
178 | if (pulse8->escape) { | |
179 | data += MSGOFFSET; | |
180 | pulse8->escape = false; | |
181 | } else if (data == MSGEND) { | |
182 | struct cec_msg *msg = &pulse8->rx_msg; | |
183 | ||
184 | if (debug) | |
185 | dev_info(pulse8->dev, "received: %*ph\n", | |
186 | pulse8->idx, pulse8->buf); | |
187 | pulse8->data[0] = pulse8->buf[0]; | |
188 | switch (pulse8->buf[0] & 0x3f) { | |
189 | case MSGCODE_FRAME_START: | |
190 | msg->len = 1; | |
191 | msg->msg[0] = pulse8->buf[1]; | |
192 | break; | |
193 | case MSGCODE_FRAME_DATA: | |
194 | if (msg->len == CEC_MAX_MSG_SIZE) | |
195 | break; | |
196 | msg->msg[msg->len++] = pulse8->buf[1]; | |
197 | if (pulse8->buf[0] & MSGCODE_FRAME_EOM) | |
198 | schedule_work(&pulse8->work); | |
199 | break; | |
200 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
201 | case MSGCODE_TRANSMIT_FAILED_LINE: | |
202 | case MSGCODE_TRANSMIT_FAILED_ACK: | |
203 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: | |
204 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: | |
205 | schedule_work(&pulse8->work); | |
206 | break; | |
31f58e31 HV |
207 | case MSGCODE_HIGH_ERROR: |
208 | case MSGCODE_LOW_ERROR: | |
209 | case MSGCODE_RECEIVE_FAILED: | |
3dff3106 HV |
210 | case MSGCODE_TIMEOUT_ERROR: |
211 | break; | |
212 | case MSGCODE_COMMAND_ACCEPTED: | |
213 | case MSGCODE_COMMAND_REJECTED: | |
214 | default: | |
215 | if (pulse8->idx == 0) | |
216 | break; | |
217 | memcpy(pulse8->data, pulse8->buf, pulse8->idx); | |
218 | pulse8->len = pulse8->idx; | |
219 | complete(&pulse8->cmd_done); | |
220 | break; | |
221 | } | |
222 | pulse8->idx = 0; | |
223 | pulse8->started = false; | |
224 | return IRQ_HANDLED; | |
225 | } else if (data == MSGSTART) { | |
226 | pulse8->idx = 0; | |
227 | pulse8->started = true; | |
228 | return IRQ_HANDLED; | |
229 | } | |
230 | ||
231 | if (pulse8->idx >= DATA_SIZE) { | |
232 | dev_dbg(pulse8->dev, | |
233 | "throwing away %d bytes of garbage\n", pulse8->idx); | |
234 | pulse8->idx = 0; | |
235 | } | |
236 | pulse8->buf[pulse8->idx++] = data; | |
237 | return IRQ_HANDLED; | |
238 | } | |
239 | ||
240 | static void pulse8_disconnect(struct serio *serio) | |
241 | { | |
242 | struct pulse8 *pulse8 = serio_get_drvdata(serio); | |
243 | ||
244 | cec_unregister_adapter(pulse8->adap); | |
e28a6c8b | 245 | cancel_delayed_work_sync(&pulse8->ping_eeprom_work); |
3dff3106 HV |
246 | dev_info(&serio->dev, "disconnected\n"); |
247 | serio_close(serio); | |
248 | serio_set_drvdata(serio, NULL); | |
249 | kfree(pulse8); | |
250 | } | |
251 | ||
252 | static int pulse8_send(struct serio *serio, const u8 *command, u8 cmd_len) | |
253 | { | |
254 | int err = 0; | |
255 | ||
256 | err = serio_write(serio, MSGSTART); | |
257 | if (err) | |
258 | return err; | |
259 | for (; !err && cmd_len; command++, cmd_len--) { | |
260 | if (*command >= MSGESC) { | |
261 | err = serio_write(serio, MSGESC); | |
262 | if (!err) | |
263 | err = serio_write(serio, *command - MSGOFFSET); | |
264 | } else { | |
265 | err = serio_write(serio, *command); | |
266 | } | |
267 | } | |
268 | if (!err) | |
9d29327d | 269 | err = serio_write(serio, MSGEND); |
3dff3106 HV |
270 | |
271 | return err; | |
272 | } | |
273 | ||
05f3e58b JF |
274 | static int pulse8_send_and_wait_once(struct pulse8 *pulse8, |
275 | const u8 *cmd, u8 cmd_len, | |
276 | u8 response, u8 size) | |
3dff3106 HV |
277 | { |
278 | int err; | |
279 | ||
280 | /*dev_info(pulse8->dev, "transmit: %*ph\n", cmd_len, cmd);*/ | |
281 | init_completion(&pulse8->cmd_done); | |
282 | ||
283 | err = pulse8_send(pulse8->serio, cmd, cmd_len); | |
284 | if (err) | |
285 | return err; | |
286 | ||
287 | if (!wait_for_completion_timeout(&pulse8->cmd_done, HZ)) | |
288 | return -ETIMEDOUT; | |
289 | if ((pulse8->data[0] & 0x3f) == MSGCODE_COMMAND_REJECTED && | |
290 | cmd[0] != MSGCODE_SET_CONTROLLED && | |
291 | cmd[0] != MSGCODE_SET_AUTO_ENABLED && | |
05f3e58b JF |
292 | cmd[0] != MSGCODE_GET_BUILDDATE) |
293 | return -ENOTTY; | |
3dff3106 HV |
294 | if (response && |
295 | ((pulse8->data[0] & 0x3f) != response || pulse8->len < size + 1)) { | |
296 | dev_info(pulse8->dev, "transmit: failed %02x\n", | |
297 | pulse8->data[0] & 0x3f); | |
298 | return -EIO; | |
299 | } | |
300 | return 0; | |
301 | } | |
302 | ||
05f3e58b JF |
303 | static int pulse8_send_and_wait(struct pulse8 *pulse8, |
304 | const u8 *cmd, u8 cmd_len, u8 response, u8 size) | |
305 | { | |
306 | u8 cmd_sc[2]; | |
307 | int err; | |
308 | ||
309 | mutex_lock(&pulse8->write_lock); | |
310 | err = pulse8_send_and_wait_once(pulse8, cmd, cmd_len, response, size); | |
311 | ||
312 | if (err == -ENOTTY) { | |
313 | cmd_sc[0] = MSGCODE_SET_CONTROLLED; | |
314 | cmd_sc[1] = 1; | |
315 | err = pulse8_send_and_wait_once(pulse8, cmd_sc, 2, | |
316 | MSGCODE_COMMAND_ACCEPTED, 1); | |
317 | if (err) | |
318 | goto unlock; | |
319 | err = pulse8_send_and_wait_once(pulse8, cmd, cmd_len, | |
320 | response, size); | |
321 | } | |
322 | ||
323 | unlock: | |
324 | mutex_unlock(&pulse8->write_lock); | |
325 | return err == -ENOTTY ? -EIO : err; | |
326 | } | |
327 | ||
e28a6c8b JF |
328 | static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio, |
329 | struct cec_log_addrs *log_addrs, u16 *pa) | |
3dff3106 HV |
330 | { |
331 | u8 *data = pulse8->data + 1; | |
3dff3106 HV |
332 | u8 cmd[2]; |
333 | int err; | |
9d29327d JF |
334 | struct tm tm; |
335 | time_t date; | |
3dff3106 | 336 | |
e28a6c8b | 337 | pulse8->vers = 0; |
3dff3106 | 338 | |
3dff3106 | 339 | cmd[0] = MSGCODE_FIRMWARE_VERSION; |
9d29327d | 340 | err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2); |
3dff3106 HV |
341 | if (err) |
342 | return err; | |
e28a6c8b | 343 | pulse8->vers = (data[0] << 8) | data[1]; |
e28a6c8b JF |
344 | dev_info(pulse8->dev, "Firmware version %04x\n", pulse8->vers); |
345 | if (pulse8->vers < 2) | |
3dff3106 HV |
346 | return 0; |
347 | ||
348 | cmd[0] = MSGCODE_GET_BUILDDATE; | |
9d29327d | 349 | err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 4); |
e28a6c8b JF |
350 | if (err) |
351 | return err; | |
9d29327d JF |
352 | date = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; |
353 | time_to_tm(date, 0, &tm); | |
354 | dev_info(pulse8->dev, "Firmware build date %04ld.%02d.%02d %02d:%02d:%02d\n", | |
355 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, | |
356 | tm.tm_hour, tm.tm_min, tm.tm_sec); | |
3dff3106 | 357 | |
e28a6c8b JF |
358 | dev_dbg(pulse8->dev, "Persistent config:\n"); |
359 | cmd[0] = MSGCODE_GET_AUTO_ENABLED; | |
360 | err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1); | |
361 | if (err) | |
362 | return err; | |
363 | pulse8->autonomous = data[0]; | |
364 | dev_dbg(pulse8->dev, "Autonomous mode: %s", | |
365 | data[0] ? "on" : "off"); | |
3dff3106 | 366 | |
e28a6c8b JF |
367 | cmd[0] = MSGCODE_GET_DEVICE_TYPE; |
368 | err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1); | |
369 | if (err) | |
370 | return err; | |
371 | log_addrs->primary_device_type[0] = data[0]; | |
372 | dev_dbg(pulse8->dev, "Primary device type: %d\n", data[0]); | |
373 | switch (log_addrs->primary_device_type[0]) { | |
374 | case CEC_OP_PRIM_DEVTYPE_TV: | |
375 | log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TV; | |
376 | break; | |
377 | case CEC_OP_PRIM_DEVTYPE_RECORD: | |
378 | log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_RECORD; | |
379 | break; | |
380 | case CEC_OP_PRIM_DEVTYPE_TUNER: | |
381 | log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TUNER; | |
382 | break; | |
383 | case CEC_OP_PRIM_DEVTYPE_PLAYBACK: | |
384 | log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK; | |
385 | break; | |
386 | case CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM: | |
387 | log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK; | |
388 | break; | |
389 | case CEC_OP_PRIM_DEVTYPE_SWITCH: | |
390 | log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED; | |
391 | break; | |
392 | case CEC_OP_PRIM_DEVTYPE_PROCESSOR: | |
393 | log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_SPECIFIC; | |
394 | break; | |
395 | default: | |
396 | log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED; | |
397 | dev_info(pulse8->dev, "Unknown Primary Device Type: %d\n", | |
398 | log_addrs->primary_device_type[0]); | |
399 | break; | |
3dff3106 HV |
400 | } |
401 | ||
e28a6c8b JF |
402 | cmd[0] = MSGCODE_GET_LOGICAL_ADDRESS_MASK; |
403 | err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2); | |
404 | if (err) | |
405 | return err; | |
406 | log_addrs->log_addr_mask = (data[0] << 8) | data[1]; | |
407 | dev_dbg(pulse8->dev, "Logical address ACK mask: %x\n", | |
408 | log_addrs->log_addr_mask); | |
409 | if (log_addrs->log_addr_mask) | |
410 | log_addrs->num_log_addrs = 1; | |
411 | ||
412 | cmd[0] = MSGCODE_GET_PHYSICAL_ADDRESS; | |
413 | err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1); | |
414 | if (err) | |
415 | return err; | |
416 | *pa = (data[0] << 8) | data[1]; | |
417 | dev_dbg(pulse8->dev, "Physical address: %x.%x.%x.%x\n", | |
418 | cec_phys_addr_exp(*pa)); | |
3dff3106 | 419 | |
e28a6c8b JF |
420 | cmd[0] = MSGCODE_GET_HDMI_VERSION; |
421 | err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1); | |
422 | if (err) | |
423 | return err; | |
424 | log_addrs->cec_version = data[0]; | |
425 | dev_dbg(pulse8->dev, "CEC version: %d\n", log_addrs->cec_version); | |
3dff3106 | 426 | |
e28a6c8b JF |
427 | cmd[0] = MSGCODE_GET_OSD_NAME; |
428 | err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 0); | |
429 | if (err) | |
430 | return err; | |
431 | strncpy(log_addrs->osd_name, data, 13); | |
432 | dev_dbg(pulse8->dev, "OSD name: %s\n", log_addrs->osd_name); | |
3dff3106 | 433 | |
e28a6c8b JF |
434 | return 0; |
435 | } | |
436 | ||
437 | static int pulse8_apply_persistent_config(struct pulse8 *pulse8, | |
438 | struct cec_log_addrs *log_addrs, | |
439 | u16 pa) | |
440 | { | |
441 | int err; | |
442 | ||
443 | err = cec_s_log_addrs(pulse8->adap, log_addrs, false); | |
444 | if (err) | |
445 | return err; | |
446 | ||
447 | cec_s_phys_addr(pulse8->adap, pa, false); | |
e28a6c8b JF |
448 | |
449 | return 0; | |
3dff3106 HV |
450 | } |
451 | ||
452 | static int pulse8_cec_adap_enable(struct cec_adapter *adap, bool enable) | |
453 | { | |
454 | struct pulse8 *pulse8 = adap->priv; | |
455 | u8 cmd[16]; | |
456 | int err; | |
457 | ||
458 | cmd[0] = MSGCODE_SET_CONTROLLED; | |
459 | cmd[1] = enable; | |
460 | err = pulse8_send_and_wait(pulse8, cmd, 2, | |
461 | MSGCODE_COMMAND_ACCEPTED, 1); | |
462 | return enable ? err : 0; | |
463 | } | |
464 | ||
465 | static int pulse8_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr) | |
466 | { | |
467 | struct pulse8 *pulse8 = adap->priv; | |
468 | u16 mask = 0; | |
e28a6c8b JF |
469 | u16 pa = adap->phys_addr; |
470 | u8 cmd[16]; | |
471 | int err = 0; | |
3dff3106 | 472 | |
e28a6c8b | 473 | mutex_lock(&pulse8->config_lock); |
3dff3106 HV |
474 | if (log_addr != CEC_LOG_ADDR_INVALID) |
475 | mask = 1 << log_addr; | |
476 | cmd[0] = MSGCODE_SET_ACK_MASK; | |
477 | cmd[1] = mask >> 8; | |
478 | cmd[2] = mask & 0xff; | |
479 | err = pulse8_send_and_wait(pulse8, cmd, 3, | |
480 | MSGCODE_COMMAND_ACCEPTED, 0); | |
e28a6c8b JF |
481 | if ((err && mask != 0) || pulse8->restoring_config) |
482 | goto unlock; | |
483 | ||
484 | cmd[0] = MSGCODE_SET_AUTO_ENABLED; | |
485 | cmd[1] = log_addr == CEC_LOG_ADDR_INVALID ? 0 : 1; | |
486 | err = pulse8_send_and_wait(pulse8, cmd, 2, | |
487 | MSGCODE_COMMAND_ACCEPTED, 0); | |
488 | if (err) | |
489 | goto unlock; | |
490 | pulse8->autonomous = cmd[1]; | |
491 | if (log_addr == CEC_LOG_ADDR_INVALID) | |
492 | goto unlock; | |
493 | ||
494 | cmd[0] = MSGCODE_SET_DEVICE_TYPE; | |
495 | cmd[1] = adap->log_addrs.primary_device_type[0]; | |
496 | err = pulse8_send_and_wait(pulse8, cmd, 2, | |
497 | MSGCODE_COMMAND_ACCEPTED, 0); | |
498 | if (err) | |
499 | goto unlock; | |
500 | ||
501 | cmd[0] = MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS; | |
502 | cmd[1] = log_addr; | |
503 | err = pulse8_send_and_wait(pulse8, cmd, 2, | |
504 | MSGCODE_COMMAND_ACCEPTED, 0); | |
505 | if (err) | |
506 | goto unlock; | |
507 | ||
508 | cmd[0] = MSGCODE_SET_PHYSICAL_ADDRESS; | |
509 | cmd[1] = pa >> 8; | |
510 | cmd[2] = pa & 0xff; | |
511 | err = pulse8_send_and_wait(pulse8, cmd, 3, | |
512 | MSGCODE_COMMAND_ACCEPTED, 0); | |
513 | if (err) | |
514 | goto unlock; | |
515 | ||
516 | cmd[0] = MSGCODE_SET_HDMI_VERSION; | |
517 | cmd[1] = adap->log_addrs.cec_version; | |
518 | err = pulse8_send_and_wait(pulse8, cmd, 2, | |
519 | MSGCODE_COMMAND_ACCEPTED, 0); | |
520 | if (err) | |
521 | goto unlock; | |
522 | ||
523 | if (adap->log_addrs.osd_name[0]) { | |
524 | size_t osd_len = strlen(adap->log_addrs.osd_name); | |
525 | char *osd_str = cmd + 1; | |
526 | ||
527 | cmd[0] = MSGCODE_SET_OSD_NAME; | |
528 | strncpy(cmd + 1, adap->log_addrs.osd_name, 13); | |
529 | if (osd_len < 4) { | |
530 | memset(osd_str + osd_len, ' ', 4 - osd_len); | |
531 | osd_len = 4; | |
532 | osd_str[osd_len] = '\0'; | |
533 | strcpy(adap->log_addrs.osd_name, osd_str); | |
534 | } | |
535 | err = pulse8_send_and_wait(pulse8, cmd, 1 + osd_len, | |
536 | MSGCODE_COMMAND_ACCEPTED, 0); | |
537 | if (err) | |
538 | goto unlock; | |
539 | } | |
540 | ||
541 | unlock: | |
542 | if (pulse8->restoring_config) | |
543 | pulse8->restoring_config = false; | |
544 | else | |
545 | pulse8->config_pending = true; | |
546 | mutex_unlock(&pulse8->config_lock); | |
3dff3106 HV |
547 | return err; |
548 | } | |
549 | ||
550 | static int pulse8_cec_adap_transmit(struct cec_adapter *adap, u8 attempts, | |
551 | u32 signal_free_time, struct cec_msg *msg) | |
552 | { | |
553 | struct pulse8 *pulse8 = adap->priv; | |
554 | u8 cmd[2]; | |
555 | unsigned int i; | |
556 | int err; | |
557 | ||
558 | cmd[0] = MSGCODE_TRANSMIT_IDLETIME; | |
1e6e9754 | 559 | cmd[1] = signal_free_time; |
3dff3106 HV |
560 | err = pulse8_send_and_wait(pulse8, cmd, 2, |
561 | MSGCODE_COMMAND_ACCEPTED, 1); | |
562 | cmd[0] = MSGCODE_TRANSMIT_ACK_POLARITY; | |
563 | cmd[1] = cec_msg_is_broadcast(msg); | |
564 | if (!err) | |
565 | err = pulse8_send_and_wait(pulse8, cmd, 2, | |
566 | MSGCODE_COMMAND_ACCEPTED, 1); | |
567 | cmd[0] = msg->len == 1 ? MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT; | |
568 | cmd[1] = msg->msg[0]; | |
569 | if (!err) | |
570 | err = pulse8_send_and_wait(pulse8, cmd, 2, | |
571 | MSGCODE_COMMAND_ACCEPTED, 1); | |
572 | if (!err && msg->len > 1) { | |
573 | cmd[0] = msg->len == 2 ? MSGCODE_TRANSMIT_EOM : | |
574 | MSGCODE_TRANSMIT; | |
575 | cmd[1] = msg->msg[1]; | |
576 | err = pulse8_send_and_wait(pulse8, cmd, 2, | |
577 | MSGCODE_COMMAND_ACCEPTED, 1); | |
578 | for (i = 0; !err && i + 2 < msg->len; i++) { | |
579 | cmd[0] = (i + 2 == msg->len - 1) ? | |
580 | MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT; | |
581 | cmd[1] = msg->msg[i + 2]; | |
582 | err = pulse8_send_and_wait(pulse8, cmd, 2, | |
583 | MSGCODE_COMMAND_ACCEPTED, 1); | |
584 | } | |
585 | } | |
586 | ||
587 | return err; | |
588 | } | |
589 | ||
590 | static int pulse8_received(struct cec_adapter *adap, struct cec_msg *msg) | |
591 | { | |
592 | return -ENOMSG; | |
593 | } | |
594 | ||
babbf091 | 595 | static const struct cec_adap_ops pulse8_cec_adap_ops = { |
3dff3106 HV |
596 | .adap_enable = pulse8_cec_adap_enable, |
597 | .adap_log_addr = pulse8_cec_adap_log_addr, | |
598 | .adap_transmit = pulse8_cec_adap_transmit, | |
599 | .received = pulse8_received, | |
600 | }; | |
601 | ||
602 | static int pulse8_connect(struct serio *serio, struct serio_driver *drv) | |
603 | { | |
604 | u32 caps = CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS | CEC_CAP_PHYS_ADDR | | |
605 | CEC_CAP_PASSTHROUGH | CEC_CAP_RC | CEC_CAP_MONITOR_ALL; | |
606 | struct pulse8 *pulse8; | |
607 | int err = -ENOMEM; | |
e28a6c8b | 608 | struct cec_log_addrs log_addrs = {}; |
c3b80983 | 609 | u16 pa = CEC_PHYS_ADDR_INVALID; |
3dff3106 HV |
610 | |
611 | pulse8 = kzalloc(sizeof(*pulse8), GFP_KERNEL); | |
612 | ||
613 | if (!pulse8) | |
614 | return -ENOMEM; | |
615 | ||
616 | pulse8->serio = serio; | |
617 | pulse8->adap = cec_allocate_adapter(&pulse8_cec_adap_ops, pulse8, | |
618 | "HDMI CEC", caps, 1, &serio->dev); | |
619 | err = PTR_ERR_OR_ZERO(pulse8->adap); | |
620 | if (err < 0) | |
621 | goto free_device; | |
622 | ||
623 | pulse8->dev = &serio->dev; | |
624 | serio_set_drvdata(serio, pulse8); | |
625 | INIT_WORK(&pulse8->work, pulse8_irq_work_handler); | |
05f3e58b | 626 | mutex_init(&pulse8->write_lock); |
e28a6c8b JF |
627 | mutex_init(&pulse8->config_lock); |
628 | pulse8->config_pending = false; | |
3dff3106 HV |
629 | |
630 | err = serio_open(serio, drv); | |
631 | if (err) | |
632 | goto delete_adap; | |
633 | ||
e28a6c8b | 634 | err = pulse8_setup(pulse8, serio, &log_addrs, &pa); |
3dff3106 HV |
635 | if (err) |
636 | goto close_serio; | |
637 | ||
638 | err = cec_register_adapter(pulse8->adap); | |
639 | if (err < 0) | |
640 | goto close_serio; | |
641 | ||
642 | pulse8->dev = &pulse8->adap->devnode.dev; | |
e28a6c8b JF |
643 | |
644 | if (persistent_config && pulse8->autonomous) { | |
645 | err = pulse8_apply_persistent_config(pulse8, &log_addrs, pa); | |
646 | if (err) | |
647 | goto close_serio; | |
648 | pulse8->restoring_config = true; | |
649 | } | |
650 | ||
651 | INIT_DELAYED_WORK(&pulse8->ping_eeprom_work, | |
652 | pulse8_ping_eeprom_work_handler); | |
653 | schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD); | |
654 | ||
3dff3106 HV |
655 | return 0; |
656 | ||
657 | close_serio: | |
658 | serio_close(serio); | |
659 | delete_adap: | |
660 | cec_delete_adapter(pulse8->adap); | |
661 | serio_set_drvdata(serio, NULL); | |
662 | free_device: | |
663 | kfree(pulse8); | |
664 | return err; | |
665 | } | |
666 | ||
e28a6c8b JF |
667 | static void pulse8_ping_eeprom_work_handler(struct work_struct *work) |
668 | { | |
669 | struct pulse8 *pulse8 = | |
670 | container_of(work, struct pulse8, ping_eeprom_work.work); | |
671 | u8 cmd; | |
672 | ||
673 | schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD); | |
674 | cmd = MSGCODE_PING; | |
675 | pulse8_send_and_wait(pulse8, &cmd, 1, | |
676 | MSGCODE_COMMAND_ACCEPTED, 0); | |
677 | ||
678 | if (pulse8->vers < 2) | |
679 | return; | |
680 | ||
681 | mutex_lock(&pulse8->config_lock); | |
682 | if (pulse8->config_pending && persistent_config) { | |
683 | dev_dbg(pulse8->dev, "writing pending config to EEPROM\n"); | |
684 | cmd = MSGCODE_WRITE_EEPROM; | |
685 | if (pulse8_send_and_wait(pulse8, &cmd, 1, | |
686 | MSGCODE_COMMAND_ACCEPTED, 0)) | |
687 | dev_info(pulse8->dev, "failed to write pending config to EEPROM\n"); | |
688 | else | |
689 | pulse8->config_pending = false; | |
690 | } | |
691 | mutex_unlock(&pulse8->config_lock); | |
692 | } | |
693 | ||
3dff3106 HV |
694 | static struct serio_device_id pulse8_serio_ids[] = { |
695 | { | |
696 | .type = SERIO_RS232, | |
697 | .proto = SERIO_PULSE8_CEC, | |
698 | .id = SERIO_ANY, | |
699 | .extra = SERIO_ANY, | |
700 | }, | |
701 | { 0 } | |
702 | }; | |
703 | ||
704 | MODULE_DEVICE_TABLE(serio, pulse8_serio_ids); | |
705 | ||
706 | static struct serio_driver pulse8_drv = { | |
707 | .driver = { | |
708 | .name = "pulse8-cec", | |
709 | }, | |
710 | .description = "Pulse Eight HDMI CEC driver", | |
711 | .id_table = pulse8_serio_ids, | |
712 | .interrupt = pulse8_interrupt, | |
713 | .connect = pulse8_connect, | |
714 | .disconnect = pulse8_disconnect, | |
715 | }; | |
716 | ||
717 | module_serio_driver(pulse8_drv); |