1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for IMS Passenger Control Unit Devices
5 * Copyright (C) 2013 The IMS Company
8 #include <linux/completion.h>
9 #include <linux/device.h>
10 #include <linux/firmware.h>
11 #include <linux/ihex.h>
12 #include <linux/input.h>
13 #include <linux/kernel.h>
14 #include <linux/leds.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/usb/input.h>
19 #include <linux/usb/cdc.h>
20 #include <asm/unaligned.h>
22 #define IMS_PCU_KEYMAP_LEN 32
24 struct ims_pcu_buttons {
25 struct input_dev *input;
28 unsigned short keymap[IMS_PCU_KEYMAP_LEN];
31 struct ims_pcu_gamepad {
32 struct input_dev *input;
37 struct ims_pcu_backlight {
38 struct led_classdev cdev;
42 #define IMS_PCU_PART_NUMBER_LEN 15
43 #define IMS_PCU_SERIAL_NUMBER_LEN 8
44 #define IMS_PCU_DOM_LEN 8
45 #define IMS_PCU_FW_VERSION_LEN 16
46 #define IMS_PCU_BL_VERSION_LEN 16
47 #define IMS_PCU_BL_RESET_REASON_LEN (2 + 1)
49 #define IMS_PCU_PCU_B_DEVICE_ID 5
51 #define IMS_PCU_BUF_SIZE 128
54 struct usb_device *udev;
55 struct device *dev; /* control interface's device, used for logging */
57 unsigned int device_no;
61 char part_number[IMS_PCU_PART_NUMBER_LEN];
62 char serial_number[IMS_PCU_SERIAL_NUMBER_LEN];
63 char date_of_manufacturing[IMS_PCU_DOM_LEN];
64 char fw_version[IMS_PCU_FW_VERSION_LEN];
65 char bl_version[IMS_PCU_BL_VERSION_LEN];
66 char reset_reason[IMS_PCU_BL_RESET_REASON_LEN];
67 int update_firmware_status;
72 struct usb_interface *ctrl_intf;
74 struct usb_endpoint_descriptor *ep_ctrl;
80 struct usb_interface *data_intf;
82 struct usb_endpoint_descriptor *ep_in;
88 struct usb_endpoint_descriptor *ep_out;
92 u8 read_buf[IMS_PCU_BUF_SIZE];
98 u8 cmd_buf[IMS_PCU_BUF_SIZE];
100 u8 expected_response;
102 struct completion cmd_done;
103 struct mutex cmd_mutex;
107 struct completion async_firmware_done;
109 struct ims_pcu_buttons buttons;
110 struct ims_pcu_gamepad *gamepad;
111 struct ims_pcu_backlight backlight;
113 bool setup_complete; /* Input and LED devices have been created */
117 /*********************************************************************
118 * Buttons Input device support *
119 *********************************************************************/
121 static const unsigned short ims_pcu_keymap_1[] = {
122 [1] = KEY_ATTENDANT_OFF,
123 [2] = KEY_ATTENDANT_ON,
124 [3] = KEY_LIGHTS_TOGGLE,
126 [5] = KEY_VOLUMEDOWN,
130 static const unsigned short ims_pcu_keymap_2[] = {
132 [5] = KEY_VOLUMEDOWN,
136 static const unsigned short ims_pcu_keymap_3[] = {
138 [2] = KEY_ATTENDANT_TOGGLE,
139 [3] = KEY_LIGHTS_TOGGLE,
141 [5] = KEY_VOLUMEDOWN,
142 [6] = KEY_DISPLAYTOGGLE,
143 [18] = KEY_PLAYPAUSE,
146 static const unsigned short ims_pcu_keymap_4[] = {
147 [1] = KEY_ATTENDANT_OFF,
148 [2] = KEY_ATTENDANT_ON,
149 [3] = KEY_LIGHTS_TOGGLE,
151 [5] = KEY_VOLUMEDOWN,
153 [18] = KEY_PLAYPAUSE,
156 static const unsigned short ims_pcu_keymap_5[] = {
157 [1] = KEY_ATTENDANT_OFF,
158 [2] = KEY_ATTENDANT_ON,
159 [3] = KEY_LIGHTS_TOGGLE,
162 struct ims_pcu_device_info {
163 const unsigned short *keymap;
168 #define IMS_PCU_DEVINFO(_n, _gamepad) \
170 .keymap = ims_pcu_keymap_##_n, \
171 .keymap_len = ARRAY_SIZE(ims_pcu_keymap_##_n), \
172 .has_gamepad = _gamepad, \
175 static const struct ims_pcu_device_info ims_pcu_device_info[] = {
176 IMS_PCU_DEVINFO(1, true),
177 IMS_PCU_DEVINFO(2, true),
178 IMS_PCU_DEVINFO(3, true),
179 IMS_PCU_DEVINFO(4, true),
180 IMS_PCU_DEVINFO(5, false),
183 static void ims_pcu_buttons_report(struct ims_pcu *pcu, u32 data)
185 struct ims_pcu_buttons *buttons = &pcu->buttons;
186 struct input_dev *input = buttons->input;
189 for (i = 0; i < 32; i++) {
190 unsigned short keycode = buttons->keymap[i];
192 if (keycode != KEY_RESERVED)
193 input_report_key(input, keycode, data & (1UL << i));
199 static int ims_pcu_setup_buttons(struct ims_pcu *pcu,
200 const unsigned short *keymap,
203 struct ims_pcu_buttons *buttons = &pcu->buttons;
204 struct input_dev *input;
208 input = input_allocate_device();
210 dev_err(pcu->dev, "Not enough memory for input device\n");
214 snprintf(buttons->name, sizeof(buttons->name),
215 "IMS PCU#%d Button Interface", pcu->device_no);
217 usb_make_path(pcu->udev, buttons->phys, sizeof(buttons->phys));
218 strlcat(buttons->phys, "/input0", sizeof(buttons->phys));
220 memcpy(buttons->keymap, keymap, sizeof(*keymap) * keymap_len);
222 input->name = buttons->name;
223 input->phys = buttons->phys;
224 usb_to_input_id(pcu->udev, &input->id);
225 input->dev.parent = &pcu->ctrl_intf->dev;
227 input->keycode = buttons->keymap;
228 input->keycodemax = ARRAY_SIZE(buttons->keymap);
229 input->keycodesize = sizeof(buttons->keymap[0]);
231 __set_bit(EV_KEY, input->evbit);
232 for (i = 0; i < IMS_PCU_KEYMAP_LEN; i++)
233 __set_bit(buttons->keymap[i], input->keybit);
234 __clear_bit(KEY_RESERVED, input->keybit);
236 error = input_register_device(input);
239 "Failed to register buttons input device: %d\n",
241 input_free_device(input);
245 buttons->input = input;
249 static void ims_pcu_destroy_buttons(struct ims_pcu *pcu)
251 struct ims_pcu_buttons *buttons = &pcu->buttons;
253 input_unregister_device(buttons->input);
257 /*********************************************************************
258 * Gamepad Input device support *
259 *********************************************************************/
261 static void ims_pcu_gamepad_report(struct ims_pcu *pcu, u32 data)
263 struct ims_pcu_gamepad *gamepad = pcu->gamepad;
264 struct input_dev *input = gamepad->input;
267 x = !!(data & (1 << 14)) - !!(data & (1 << 13));
268 y = !!(data & (1 << 12)) - !!(data & (1 << 11));
270 input_report_abs(input, ABS_X, x);
271 input_report_abs(input, ABS_Y, y);
273 input_report_key(input, BTN_A, data & (1 << 7));
274 input_report_key(input, BTN_B, data & (1 << 8));
275 input_report_key(input, BTN_X, data & (1 << 9));
276 input_report_key(input, BTN_Y, data & (1 << 10));
277 input_report_key(input, BTN_START, data & (1 << 15));
278 input_report_key(input, BTN_SELECT, data & (1 << 16));
283 static int ims_pcu_setup_gamepad(struct ims_pcu *pcu)
285 struct ims_pcu_gamepad *gamepad;
286 struct input_dev *input;
289 gamepad = kzalloc(sizeof(*gamepad), GFP_KERNEL);
290 input = input_allocate_device();
291 if (!gamepad || !input) {
293 "Not enough memory for gamepad device\n");
298 gamepad->input = input;
300 snprintf(gamepad->name, sizeof(gamepad->name),
301 "IMS PCU#%d Gamepad Interface", pcu->device_no);
303 usb_make_path(pcu->udev, gamepad->phys, sizeof(gamepad->phys));
304 strlcat(gamepad->phys, "/input1", sizeof(gamepad->phys));
306 input->name = gamepad->name;
307 input->phys = gamepad->phys;
308 usb_to_input_id(pcu->udev, &input->id);
309 input->dev.parent = &pcu->ctrl_intf->dev;
311 __set_bit(EV_KEY, input->evbit);
312 __set_bit(BTN_A, input->keybit);
313 __set_bit(BTN_B, input->keybit);
314 __set_bit(BTN_X, input->keybit);
315 __set_bit(BTN_Y, input->keybit);
316 __set_bit(BTN_START, input->keybit);
317 __set_bit(BTN_SELECT, input->keybit);
319 __set_bit(EV_ABS, input->evbit);
320 input_set_abs_params(input, ABS_X, -1, 1, 0, 0);
321 input_set_abs_params(input, ABS_Y, -1, 1, 0, 0);
323 error = input_register_device(input);
326 "Failed to register gamepad input device: %d\n",
331 pcu->gamepad = gamepad;
335 input_free_device(input);
340 static void ims_pcu_destroy_gamepad(struct ims_pcu *pcu)
342 struct ims_pcu_gamepad *gamepad = pcu->gamepad;
344 input_unregister_device(gamepad->input);
349 /*********************************************************************
350 * PCU Communication protocol handling *
351 *********************************************************************/
353 #define IMS_PCU_PROTOCOL_STX 0x02
354 #define IMS_PCU_PROTOCOL_ETX 0x03
355 #define IMS_PCU_PROTOCOL_DLE 0x10
358 #define IMS_PCU_CMD_STATUS 0xa0
359 #define IMS_PCU_CMD_PCU_RESET 0xa1
360 #define IMS_PCU_CMD_RESET_REASON 0xa2
361 #define IMS_PCU_CMD_SEND_BUTTONS 0xa3
362 #define IMS_PCU_CMD_JUMP_TO_BTLDR 0xa4
363 #define IMS_PCU_CMD_GET_INFO 0xa5
364 #define IMS_PCU_CMD_SET_BRIGHTNESS 0xa6
365 #define IMS_PCU_CMD_EEPROM 0xa7
366 #define IMS_PCU_CMD_GET_FW_VERSION 0xa8
367 #define IMS_PCU_CMD_GET_BL_VERSION 0xa9
368 #define IMS_PCU_CMD_SET_INFO 0xab
369 #define IMS_PCU_CMD_GET_BRIGHTNESS 0xac
370 #define IMS_PCU_CMD_GET_DEVICE_ID 0xae
371 #define IMS_PCU_CMD_SPECIAL_INFO 0xb0
372 #define IMS_PCU_CMD_BOOTLOADER 0xb1 /* Pass data to bootloader */
373 #define IMS_PCU_CMD_OFN_SET_CONFIG 0xb3
374 #define IMS_PCU_CMD_OFN_GET_CONFIG 0xb4
377 #define IMS_PCU_RSP_STATUS 0xc0
378 #define IMS_PCU_RSP_PCU_RESET 0 /* Originally 0xc1 */
379 #define IMS_PCU_RSP_RESET_REASON 0xc2
380 #define IMS_PCU_RSP_SEND_BUTTONS 0xc3
381 #define IMS_PCU_RSP_JUMP_TO_BTLDR 0 /* Originally 0xc4 */
382 #define IMS_PCU_RSP_GET_INFO 0xc5
383 #define IMS_PCU_RSP_SET_BRIGHTNESS 0xc6
384 #define IMS_PCU_RSP_EEPROM 0xc7
385 #define IMS_PCU_RSP_GET_FW_VERSION 0xc8
386 #define IMS_PCU_RSP_GET_BL_VERSION 0xc9
387 #define IMS_PCU_RSP_SET_INFO 0xcb
388 #define IMS_PCU_RSP_GET_BRIGHTNESS 0xcc
389 #define IMS_PCU_RSP_CMD_INVALID 0xcd
390 #define IMS_PCU_RSP_GET_DEVICE_ID 0xce
391 #define IMS_PCU_RSP_SPECIAL_INFO 0xd0
392 #define IMS_PCU_RSP_BOOTLOADER 0xd1 /* Bootloader response */
393 #define IMS_PCU_RSP_OFN_SET_CONFIG 0xd2
394 #define IMS_PCU_RSP_OFN_GET_CONFIG 0xd3
397 #define IMS_PCU_RSP_EVNT_BUTTONS 0xe0 /* Unsolicited, button state */
398 #define IMS_PCU_GAMEPAD_MASK 0x0001ff80UL /* Bits 7 through 16 */
401 #define IMS_PCU_MIN_PACKET_LEN 3
402 #define IMS_PCU_DATA_OFFSET 2
404 #define IMS_PCU_CMD_WRITE_TIMEOUT 100 /* msec */
405 #define IMS_PCU_CMD_RESPONSE_TIMEOUT 500 /* msec */
407 static void ims_pcu_report_events(struct ims_pcu *pcu)
409 u32 data = get_unaligned_be32(&pcu->read_buf[3]);
411 ims_pcu_buttons_report(pcu, data & ~IMS_PCU_GAMEPAD_MASK);
413 ims_pcu_gamepad_report(pcu, data);
416 static void ims_pcu_handle_response(struct ims_pcu *pcu)
418 switch (pcu->read_buf[0]) {
419 case IMS_PCU_RSP_EVNT_BUTTONS:
420 if (likely(pcu->setup_complete))
421 ims_pcu_report_events(pcu);
426 * See if we got command completion.
427 * If both the sequence and response code match save
428 * the data and signal completion.
430 if (pcu->read_buf[0] == pcu->expected_response &&
431 pcu->read_buf[1] == pcu->ack_id - 1) {
433 memcpy(pcu->cmd_buf, pcu->read_buf, pcu->read_pos);
434 pcu->cmd_buf_len = pcu->read_pos;
435 complete(&pcu->cmd_done);
441 static void ims_pcu_process_data(struct ims_pcu *pcu, struct urb *urb)
445 for (i = 0; i < urb->actual_length; i++) {
446 u8 data = pcu->urb_in_buf[i];
448 /* Skip everything until we get Start Xmit */
449 if (!pcu->have_stx && data != IMS_PCU_PROTOCOL_STX)
453 pcu->have_dle = false;
454 pcu->read_buf[pcu->read_pos++] = data;
455 pcu->check_sum += data;
460 case IMS_PCU_PROTOCOL_STX:
463 "Unexpected STX at byte %d, discarding old data\n",
465 pcu->have_stx = true;
466 pcu->have_dle = false;
471 case IMS_PCU_PROTOCOL_DLE:
472 pcu->have_dle = true;
475 case IMS_PCU_PROTOCOL_ETX:
476 if (pcu->read_pos < IMS_PCU_MIN_PACKET_LEN) {
478 "Short packet received (%d bytes), ignoring\n",
480 } else if (pcu->check_sum != 0) {
482 "Invalid checksum in packet (%d bytes), ignoring\n",
485 ims_pcu_handle_response(pcu);
488 pcu->have_stx = false;
489 pcu->have_dle = false;
494 pcu->read_buf[pcu->read_pos++] = data;
495 pcu->check_sum += data;
501 static bool ims_pcu_byte_needs_escape(u8 byte)
503 return byte == IMS_PCU_PROTOCOL_STX ||
504 byte == IMS_PCU_PROTOCOL_ETX ||
505 byte == IMS_PCU_PROTOCOL_DLE;
508 static int ims_pcu_send_cmd_chunk(struct ims_pcu *pcu,
509 u8 command, int chunk, int len)
513 error = usb_bulk_msg(pcu->udev,
514 usb_sndbulkpipe(pcu->udev,
515 pcu->ep_out->bEndpointAddress),
516 pcu->urb_out_buf, len,
517 NULL, IMS_PCU_CMD_WRITE_TIMEOUT);
520 "Sending 0x%02x command failed at chunk %d: %d\n",
521 command, chunk, error);
528 static int ims_pcu_send_command(struct ims_pcu *pcu,
529 u8 command, const u8 *data, int len)
539 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_STX;
541 /* We know the command need not be escaped */
542 pcu->urb_out_buf[count++] = command;
545 ack_id = pcu->ack_id++;
547 ack_id = pcu->ack_id++;
549 if (ims_pcu_byte_needs_escape(ack_id))
550 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
552 pcu->urb_out_buf[count++] = ack_id;
555 for (i = 0; i < len; i++) {
557 delta = ims_pcu_byte_needs_escape(data[i]) ? 2 : 1;
558 if (count + delta >= pcu->max_out_size) {
559 error = ims_pcu_send_cmd_chunk(pcu, command,
568 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
570 pcu->urb_out_buf[count++] = data[i];
576 delta = ims_pcu_byte_needs_escape(csum) ? 3 : 2;
577 if (count + delta >= pcu->max_out_size) {
578 error = ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count);
586 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
588 pcu->urb_out_buf[count++] = csum;
589 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_ETX;
591 return ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count);
594 static int __ims_pcu_execute_command(struct ims_pcu *pcu,
595 u8 command, const void *data, size_t len,
596 u8 expected_response, int response_time)
600 pcu->expected_response = expected_response;
601 init_completion(&pcu->cmd_done);
603 error = ims_pcu_send_command(pcu, command, data, len);
607 if (expected_response &&
608 !wait_for_completion_timeout(&pcu->cmd_done,
609 msecs_to_jiffies(response_time))) {
610 dev_dbg(pcu->dev, "Command 0x%02x timed out\n", command);
617 #define ims_pcu_execute_command(pcu, code, data, len) \
618 __ims_pcu_execute_command(pcu, \
619 IMS_PCU_CMD_##code, data, len, \
620 IMS_PCU_RSP_##code, \
621 IMS_PCU_CMD_RESPONSE_TIMEOUT)
623 #define ims_pcu_execute_query(pcu, code) \
624 ims_pcu_execute_command(pcu, code, NULL, 0)
626 /* Bootloader commands */
627 #define IMS_PCU_BL_CMD_QUERY_DEVICE 0xa1
628 #define IMS_PCU_BL_CMD_UNLOCK_CONFIG 0xa2
629 #define IMS_PCU_BL_CMD_ERASE_APP 0xa3
630 #define IMS_PCU_BL_CMD_PROGRAM_DEVICE 0xa4
631 #define IMS_PCU_BL_CMD_PROGRAM_COMPLETE 0xa5
632 #define IMS_PCU_BL_CMD_READ_APP 0xa6
633 #define IMS_PCU_BL_CMD_RESET_DEVICE 0xa7
634 #define IMS_PCU_BL_CMD_LAUNCH_APP 0xa8
636 /* Bootloader commands */
637 #define IMS_PCU_BL_RSP_QUERY_DEVICE 0xc1
638 #define IMS_PCU_BL_RSP_UNLOCK_CONFIG 0xc2
639 #define IMS_PCU_BL_RSP_ERASE_APP 0xc3
640 #define IMS_PCU_BL_RSP_PROGRAM_DEVICE 0xc4
641 #define IMS_PCU_BL_RSP_PROGRAM_COMPLETE 0xc5
642 #define IMS_PCU_BL_RSP_READ_APP 0xc6
643 #define IMS_PCU_BL_RSP_RESET_DEVICE 0 /* originally 0xa7 */
644 #define IMS_PCU_BL_RSP_LAUNCH_APP 0 /* originally 0xa8 */
646 #define IMS_PCU_BL_DATA_OFFSET 3
648 static int __ims_pcu_execute_bl_command(struct ims_pcu *pcu,
649 u8 command, const void *data, size_t len,
650 u8 expected_response, int response_time)
654 pcu->cmd_buf[0] = command;
656 memcpy(&pcu->cmd_buf[1], data, len);
658 error = __ims_pcu_execute_command(pcu,
659 IMS_PCU_CMD_BOOTLOADER, pcu->cmd_buf, len + 1,
660 expected_response ? IMS_PCU_RSP_BOOTLOADER : 0,
664 "Failure when sending 0x%02x command to bootloader, error: %d\n",
665 pcu->cmd_buf[0], error);
669 if (expected_response && pcu->cmd_buf[2] != expected_response) {
671 "Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n",
672 pcu->cmd_buf[2], expected_response);
679 #define ims_pcu_execute_bl_command(pcu, code, data, len, timeout) \
680 __ims_pcu_execute_bl_command(pcu, \
681 IMS_PCU_BL_CMD_##code, data, len, \
682 IMS_PCU_BL_RSP_##code, timeout) \
684 #define IMS_PCU_INFO_PART_OFFSET 2
685 #define IMS_PCU_INFO_DOM_OFFSET 17
686 #define IMS_PCU_INFO_SERIAL_OFFSET 25
688 #define IMS_PCU_SET_INFO_SIZE 31
690 static int ims_pcu_get_info(struct ims_pcu *pcu)
694 error = ims_pcu_execute_query(pcu, GET_INFO);
697 "GET_INFO command failed, error: %d\n", error);
701 memcpy(pcu->part_number,
702 &pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET],
703 sizeof(pcu->part_number));
704 memcpy(pcu->date_of_manufacturing,
705 &pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET],
706 sizeof(pcu->date_of_manufacturing));
707 memcpy(pcu->serial_number,
708 &pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET],
709 sizeof(pcu->serial_number));
714 static int ims_pcu_set_info(struct ims_pcu *pcu)
718 memcpy(&pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET],
719 pcu->part_number, sizeof(pcu->part_number));
720 memcpy(&pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET],
721 pcu->date_of_manufacturing, sizeof(pcu->date_of_manufacturing));
722 memcpy(&pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET],
723 pcu->serial_number, sizeof(pcu->serial_number));
725 error = ims_pcu_execute_command(pcu, SET_INFO,
726 &pcu->cmd_buf[IMS_PCU_DATA_OFFSET],
727 IMS_PCU_SET_INFO_SIZE);
730 "Failed to update device information, error: %d\n",
738 static int ims_pcu_switch_to_bootloader(struct ims_pcu *pcu)
742 /* Execute jump to the bootoloader */
743 error = ims_pcu_execute_command(pcu, JUMP_TO_BTLDR, NULL, 0);
746 "Failure when sending JUMP TO BOOTLOADER command, error: %d\n",
754 /*********************************************************************
755 * Firmware Update handling *
756 *********************************************************************/
758 #define IMS_PCU_FIRMWARE_NAME "imspcu.fw"
760 struct ims_pcu_flash_fmt {
763 u8 data[] __counted_by(len);
766 static unsigned int ims_pcu_count_fw_records(const struct firmware *fw)
768 const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data;
769 unsigned int count = 0;
773 rec = ihex_next_binrec(rec);
779 static int ims_pcu_verify_block(struct ims_pcu *pcu,
780 u32 addr, u8 len, const u8 *data)
782 struct ims_pcu_flash_fmt *fragment;
785 fragment = (void *)&pcu->cmd_buf[1];
786 put_unaligned_le32(addr, &fragment->addr);
789 error = ims_pcu_execute_bl_command(pcu, READ_APP, NULL, 5,
790 IMS_PCU_CMD_RESPONSE_TIMEOUT);
793 "Failed to retrieve block at 0x%08x, len %d, error: %d\n",
798 fragment = (void *)&pcu->cmd_buf[IMS_PCU_BL_DATA_OFFSET];
799 if (get_unaligned_le32(&fragment->addr) != addr ||
800 fragment->len != len) {
802 "Wrong block when retrieving 0x%08x (0x%08x), len %d (%d)\n",
803 addr, get_unaligned_le32(&fragment->addr),
808 if (memcmp(fragment->data, data, len)) {
810 "Mismatch in block at 0x%08x, len %d\n",
818 static int ims_pcu_flash_firmware(struct ims_pcu *pcu,
819 const struct firmware *fw,
820 unsigned int n_fw_records)
822 const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data;
823 struct ims_pcu_flash_fmt *fragment;
824 unsigned int count = 0;
829 error = ims_pcu_execute_bl_command(pcu, ERASE_APP, NULL, 0, 2000);
832 "Failed to erase application image, error: %d\n",
839 * The firmware format is messed up for some reason.
840 * The address twice that of what is needed for some
841 * reason and we end up overwriting half of the data
842 * with the next record.
844 addr = be32_to_cpu(rec->addr) / 2;
845 len = be16_to_cpu(rec->len);
847 fragment = (void *)&pcu->cmd_buf[1];
848 put_unaligned_le32(addr, &fragment->addr);
850 memcpy(fragment->data, rec->data, len);
852 error = ims_pcu_execute_bl_command(pcu, PROGRAM_DEVICE,
854 IMS_PCU_CMD_RESPONSE_TIMEOUT);
857 "Failed to write block at 0x%08x, len %d, error: %d\n",
862 if (addr >= pcu->fw_start_addr && addr < pcu->fw_end_addr) {
863 error = ims_pcu_verify_block(pcu, addr, len, rec->data);
869 pcu->update_firmware_status = (count * 100) / n_fw_records;
871 rec = ihex_next_binrec(rec);
874 error = ims_pcu_execute_bl_command(pcu, PROGRAM_COMPLETE,
878 "Failed to send PROGRAM_COMPLETE, error: %d\n",
884 static int ims_pcu_handle_firmware_update(struct ims_pcu *pcu,
885 const struct firmware *fw)
887 unsigned int n_fw_records;
890 dev_info(pcu->dev, "Updating firmware %s, size: %zu\n",
891 IMS_PCU_FIRMWARE_NAME, fw->size);
893 n_fw_records = ims_pcu_count_fw_records(fw);
895 retval = ims_pcu_flash_firmware(pcu, fw, n_fw_records);
899 retval = ims_pcu_execute_bl_command(pcu, LAUNCH_APP, NULL, 0, 0);
902 "Failed to start application image, error: %d\n",
906 pcu->update_firmware_status = retval;
907 sysfs_notify(&pcu->dev->kobj, NULL, "update_firmware_status");
911 static void ims_pcu_process_async_firmware(const struct firmware *fw,
914 struct ims_pcu *pcu = context;
918 dev_err(pcu->dev, "Failed to get firmware %s\n",
919 IMS_PCU_FIRMWARE_NAME);
923 error = ihex_validate_fw(fw);
925 dev_err(pcu->dev, "Firmware %s is invalid\n",
926 IMS_PCU_FIRMWARE_NAME);
930 scoped_guard(mutex, &pcu->cmd_mutex)
931 ims_pcu_handle_firmware_update(pcu, fw);
933 release_firmware(fw);
936 complete(&pcu->async_firmware_done);
939 /*********************************************************************
940 * Backlight LED device support *
941 *********************************************************************/
943 #define IMS_PCU_MAX_BRIGHTNESS 31998
945 static int ims_pcu_backlight_set_brightness(struct led_classdev *cdev,
946 enum led_brightness value)
948 struct ims_pcu_backlight *backlight =
949 container_of(cdev, struct ims_pcu_backlight, cdev);
950 struct ims_pcu *pcu =
951 container_of(backlight, struct ims_pcu, backlight);
952 __le16 br_val = cpu_to_le16(value);
955 guard(mutex)(&pcu->cmd_mutex);
957 error = ims_pcu_execute_command(pcu, SET_BRIGHTNESS,
958 &br_val, sizeof(br_val));
959 if (error && error != -ENODEV)
961 "Failed to set desired brightness %u, error: %d\n",
967 static enum led_brightness
968 ims_pcu_backlight_get_brightness(struct led_classdev *cdev)
970 struct ims_pcu_backlight *backlight =
971 container_of(cdev, struct ims_pcu_backlight, cdev);
972 struct ims_pcu *pcu =
973 container_of(backlight, struct ims_pcu, backlight);
977 guard(mutex)(&pcu->cmd_mutex);
979 error = ims_pcu_execute_query(pcu, GET_BRIGHTNESS);
982 "Failed to get current brightness, error: %d\n",
984 /* Assume the LED is OFF */
985 brightness = LED_OFF;
988 get_unaligned_le16(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET]);
994 static int ims_pcu_setup_backlight(struct ims_pcu *pcu)
996 struct ims_pcu_backlight *backlight = &pcu->backlight;
999 snprintf(backlight->name, sizeof(backlight->name),
1000 "pcu%d::kbd_backlight", pcu->device_no);
1002 backlight->cdev.name = backlight->name;
1003 backlight->cdev.max_brightness = IMS_PCU_MAX_BRIGHTNESS;
1004 backlight->cdev.brightness_get = ims_pcu_backlight_get_brightness;
1005 backlight->cdev.brightness_set_blocking =
1006 ims_pcu_backlight_set_brightness;
1008 error = led_classdev_register(pcu->dev, &backlight->cdev);
1011 "Failed to register backlight LED device, error: %d\n",
1019 static void ims_pcu_destroy_backlight(struct ims_pcu *pcu)
1021 struct ims_pcu_backlight *backlight = &pcu->backlight;
1023 led_classdev_unregister(&backlight->cdev);
1027 /*********************************************************************
1028 * Sysfs attributes handling *
1029 *********************************************************************/
1031 struct ims_pcu_attribute {
1032 struct device_attribute dattr;
1033 size_t field_offset;
1037 static ssize_t ims_pcu_attribute_show(struct device *dev,
1038 struct device_attribute *dattr,
1041 struct usb_interface *intf = to_usb_interface(dev);
1042 struct ims_pcu *pcu = usb_get_intfdata(intf);
1043 struct ims_pcu_attribute *attr =
1044 container_of(dattr, struct ims_pcu_attribute, dattr);
1045 char *field = (char *)pcu + attr->field_offset;
1047 return sysfs_emit(buf, "%.*s\n", attr->field_length, field);
1050 static ssize_t ims_pcu_attribute_store(struct device *dev,
1051 struct device_attribute *dattr,
1052 const char *buf, size_t count)
1055 struct usb_interface *intf = to_usb_interface(dev);
1056 struct ims_pcu *pcu = usb_get_intfdata(intf);
1057 struct ims_pcu_attribute *attr =
1058 container_of(dattr, struct ims_pcu_attribute, dattr);
1059 char *field = (char *)pcu + attr->field_offset;
1063 if (count > attr->field_length)
1066 data_len = strnlen(buf, attr->field_length);
1067 if (data_len > attr->field_length)
1070 scoped_cond_guard(mutex, return -EINTR, &pcu->cmd_mutex) {
1071 memset(field, 0, attr->field_length);
1072 memcpy(field, buf, data_len);
1074 error = ims_pcu_set_info(pcu);
1077 * Even if update failed, let's fetch the info again as we just
1078 * clobbered one of the fields.
1080 ims_pcu_get_info(pcu);
1089 #define IMS_PCU_ATTR(_field, _mode) \
1090 struct ims_pcu_attribute ims_pcu_attr_##_field = { \
1091 .dattr = __ATTR(_field, _mode, \
1092 ims_pcu_attribute_show, \
1093 ims_pcu_attribute_store), \
1094 .field_offset = offsetof(struct ims_pcu, _field), \
1095 .field_length = sizeof(((struct ims_pcu *)NULL)->_field), \
1098 #define IMS_PCU_RO_ATTR(_field) \
1099 IMS_PCU_ATTR(_field, S_IRUGO)
1100 #define IMS_PCU_RW_ATTR(_field) \
1101 IMS_PCU_ATTR(_field, S_IRUGO | S_IWUSR)
1103 static IMS_PCU_RW_ATTR(part_number);
1104 static IMS_PCU_RW_ATTR(serial_number);
1105 static IMS_PCU_RW_ATTR(date_of_manufacturing);
1107 static IMS_PCU_RO_ATTR(fw_version);
1108 static IMS_PCU_RO_ATTR(bl_version);
1109 static IMS_PCU_RO_ATTR(reset_reason);
1111 static ssize_t ims_pcu_reset_device(struct device *dev,
1112 struct device_attribute *dattr,
1113 const char *buf, size_t count)
1115 static const u8 reset_byte = 1;
1116 struct usb_interface *intf = to_usb_interface(dev);
1117 struct ims_pcu *pcu = usb_get_intfdata(intf);
1121 error = kstrtoint(buf, 0, &value);
1128 dev_info(pcu->dev, "Attempting to reset device\n");
1130 error = ims_pcu_execute_command(pcu, PCU_RESET, &reset_byte, 1);
1133 "Failed to reset device, error: %d\n",
1141 static DEVICE_ATTR(reset_device, S_IWUSR, NULL, ims_pcu_reset_device);
1143 static ssize_t ims_pcu_update_firmware_store(struct device *dev,
1144 struct device_attribute *dattr,
1145 const char *buf, size_t count)
1147 struct usb_interface *intf = to_usb_interface(dev);
1148 struct ims_pcu *pcu = usb_get_intfdata(intf);
1152 error = kstrtoint(buf, 0, &value);
1159 const struct firmware *fw __free(firmware) = NULL;
1160 error = request_ihex_firmware(&fw, IMS_PCU_FIRMWARE_NAME, pcu->dev);
1162 dev_err(pcu->dev, "Failed to request firmware %s, error: %d\n",
1163 IMS_PCU_FIRMWARE_NAME, error);
1167 scoped_cond_guard(mutex_intr, return -EINTR, &pcu->cmd_mutex) {
1169 * If we are already in bootloader mode we can proceed with
1170 * flashing the firmware.
1172 * If we are in application mode, then we need to switch into
1173 * bootloader mode, which will cause the device to disconnect
1174 * and reconnect as different device.
1176 if (pcu->bootloader_mode)
1177 error = ims_pcu_handle_firmware_update(pcu, fw);
1179 error = ims_pcu_switch_to_bootloader(pcu);
1188 static DEVICE_ATTR(update_firmware, S_IWUSR,
1189 NULL, ims_pcu_update_firmware_store);
1192 ims_pcu_update_firmware_status_show(struct device *dev,
1193 struct device_attribute *dattr,
1196 struct usb_interface *intf = to_usb_interface(dev);
1197 struct ims_pcu *pcu = usb_get_intfdata(intf);
1199 return sysfs_emit(buf, "%d\n", pcu->update_firmware_status);
1202 static DEVICE_ATTR(update_firmware_status, S_IRUGO,
1203 ims_pcu_update_firmware_status_show, NULL);
1205 static struct attribute *ims_pcu_attrs[] = {
1206 &ims_pcu_attr_part_number.dattr.attr,
1207 &ims_pcu_attr_serial_number.dattr.attr,
1208 &ims_pcu_attr_date_of_manufacturing.dattr.attr,
1209 &ims_pcu_attr_fw_version.dattr.attr,
1210 &ims_pcu_attr_bl_version.dattr.attr,
1211 &ims_pcu_attr_reset_reason.dattr.attr,
1212 &dev_attr_reset_device.attr,
1213 &dev_attr_update_firmware.attr,
1214 &dev_attr_update_firmware_status.attr,
1218 static umode_t ims_pcu_is_attr_visible(struct kobject *kobj,
1219 struct attribute *attr, int n)
1221 struct device *dev = kobj_to_dev(kobj);
1222 struct usb_interface *intf = to_usb_interface(dev);
1223 struct ims_pcu *pcu = usb_get_intfdata(intf);
1224 umode_t mode = attr->mode;
1226 if (pcu->bootloader_mode) {
1227 if (attr != &dev_attr_update_firmware_status.attr &&
1228 attr != &dev_attr_update_firmware.attr &&
1229 attr != &dev_attr_reset_device.attr) {
1233 if (attr == &dev_attr_update_firmware_status.attr)
1240 static const struct attribute_group ims_pcu_attr_group = {
1241 .is_visible = ims_pcu_is_attr_visible,
1242 .attrs = ims_pcu_attrs,
1245 /* Support for a separate OFN attribute group */
1247 #define OFN_REG_RESULT_OFFSET 2
1249 static int ims_pcu_read_ofn_config(struct ims_pcu *pcu, u8 addr, u8 *data)
1254 error = ims_pcu_execute_command(pcu, OFN_GET_CONFIG,
1255 &addr, sizeof(addr));
1259 result = (s16)get_unaligned_le16(pcu->cmd_buf + OFN_REG_RESULT_OFFSET);
1263 /* We only need LSB */
1264 *data = pcu->cmd_buf[OFN_REG_RESULT_OFFSET];
1268 static int ims_pcu_write_ofn_config(struct ims_pcu *pcu, u8 addr, u8 data)
1270 u8 buffer[] = { addr, data };
1274 error = ims_pcu_execute_command(pcu, OFN_SET_CONFIG,
1275 &buffer, sizeof(buffer));
1279 result = (s16)get_unaligned_le16(pcu->cmd_buf + OFN_REG_RESULT_OFFSET);
1286 static ssize_t ims_pcu_ofn_reg_data_show(struct device *dev,
1287 struct device_attribute *dattr,
1290 struct usb_interface *intf = to_usb_interface(dev);
1291 struct ims_pcu *pcu = usb_get_intfdata(intf);
1295 scoped_guard(mutex, &pcu->cmd_mutex) {
1296 error = ims_pcu_read_ofn_config(pcu, pcu->ofn_reg_addr, &data);
1301 return sysfs_emit(buf, "%x\n", data);
1304 static ssize_t ims_pcu_ofn_reg_data_store(struct device *dev,
1305 struct device_attribute *dattr,
1306 const char *buf, size_t count)
1308 struct usb_interface *intf = to_usb_interface(dev);
1309 struct ims_pcu *pcu = usb_get_intfdata(intf);
1313 error = kstrtou8(buf, 0, &value);
1317 guard(mutex)(&pcu->cmd_mutex);
1319 error = ims_pcu_write_ofn_config(pcu, pcu->ofn_reg_addr, value);
1326 static DEVICE_ATTR(reg_data, S_IRUGO | S_IWUSR,
1327 ims_pcu_ofn_reg_data_show, ims_pcu_ofn_reg_data_store);
1329 static ssize_t ims_pcu_ofn_reg_addr_show(struct device *dev,
1330 struct device_attribute *dattr,
1333 struct usb_interface *intf = to_usb_interface(dev);
1334 struct ims_pcu *pcu = usb_get_intfdata(intf);
1336 guard(mutex)(&pcu->cmd_mutex);
1338 return sysfs_emit(buf, "%x\n", pcu->ofn_reg_addr);
1341 static ssize_t ims_pcu_ofn_reg_addr_store(struct device *dev,
1342 struct device_attribute *dattr,
1343 const char *buf, size_t count)
1345 struct usb_interface *intf = to_usb_interface(dev);
1346 struct ims_pcu *pcu = usb_get_intfdata(intf);
1350 error = kstrtou8(buf, 0, &value);
1354 guard(mutex)(&pcu->cmd_mutex);
1356 pcu->ofn_reg_addr = value;
1361 static DEVICE_ATTR(reg_addr, S_IRUGO | S_IWUSR,
1362 ims_pcu_ofn_reg_addr_show, ims_pcu_ofn_reg_addr_store);
1364 struct ims_pcu_ofn_bit_attribute {
1365 struct device_attribute dattr;
1370 static ssize_t ims_pcu_ofn_bit_show(struct device *dev,
1371 struct device_attribute *dattr,
1374 struct usb_interface *intf = to_usb_interface(dev);
1375 struct ims_pcu *pcu = usb_get_intfdata(intf);
1376 struct ims_pcu_ofn_bit_attribute *attr =
1377 container_of(dattr, struct ims_pcu_ofn_bit_attribute, dattr);
1381 scoped_guard(mutex, &pcu->cmd_mutex) {
1382 error = ims_pcu_read_ofn_config(pcu, attr->addr, &data);
1387 return sysfs_emit(buf, "%d\n", !!(data & (1 << attr->nr)));
1390 static ssize_t ims_pcu_ofn_bit_store(struct device *dev,
1391 struct device_attribute *dattr,
1392 const char *buf, size_t count)
1394 struct usb_interface *intf = to_usb_interface(dev);
1395 struct ims_pcu *pcu = usb_get_intfdata(intf);
1396 struct ims_pcu_ofn_bit_attribute *attr =
1397 container_of(dattr, struct ims_pcu_ofn_bit_attribute, dattr);
1402 error = kstrtoint(buf, 0, &value);
1409 scoped_guard(mutex, &pcu->cmd_mutex) {
1410 error = ims_pcu_read_ofn_config(pcu, attr->addr, &data);
1415 data |= 1U << attr->nr;
1417 data &= ~(1U << attr->nr);
1419 error = ims_pcu_write_ofn_config(pcu, attr->addr, data);
1427 #define IMS_PCU_OFN_BIT_ATTR(_field, _addr, _nr) \
1428 struct ims_pcu_ofn_bit_attribute ims_pcu_ofn_attr_##_field = { \
1429 .dattr = __ATTR(_field, S_IWUSR | S_IRUGO, \
1430 ims_pcu_ofn_bit_show, ims_pcu_ofn_bit_store), \
1435 static IMS_PCU_OFN_BIT_ATTR(engine_enable, 0x60, 7);
1436 static IMS_PCU_OFN_BIT_ATTR(speed_enable, 0x60, 6);
1437 static IMS_PCU_OFN_BIT_ATTR(assert_enable, 0x60, 5);
1438 static IMS_PCU_OFN_BIT_ATTR(xyquant_enable, 0x60, 4);
1439 static IMS_PCU_OFN_BIT_ATTR(xyscale_enable, 0x60, 1);
1441 static IMS_PCU_OFN_BIT_ATTR(scale_x2, 0x63, 6);
1442 static IMS_PCU_OFN_BIT_ATTR(scale_y2, 0x63, 7);
1444 static struct attribute *ims_pcu_ofn_attrs[] = {
1445 &dev_attr_reg_data.attr,
1446 &dev_attr_reg_addr.attr,
1447 &ims_pcu_ofn_attr_engine_enable.dattr.attr,
1448 &ims_pcu_ofn_attr_speed_enable.dattr.attr,
1449 &ims_pcu_ofn_attr_assert_enable.dattr.attr,
1450 &ims_pcu_ofn_attr_xyquant_enable.dattr.attr,
1451 &ims_pcu_ofn_attr_xyscale_enable.dattr.attr,
1452 &ims_pcu_ofn_attr_scale_x2.dattr.attr,
1453 &ims_pcu_ofn_attr_scale_y2.dattr.attr,
1457 static umode_t ims_pcu_ofn_is_attr_visible(struct kobject *kobj,
1458 struct attribute *attr, int n)
1460 struct device *dev = kobj_to_dev(kobj);
1461 struct usb_interface *intf = to_usb_interface(dev);
1462 struct ims_pcu *pcu = usb_get_intfdata(intf);
1463 umode_t mode = attr->mode;
1466 * PCU-B devices, both GEN_1 and GEN_2 do not have OFN sensor.
1468 if (pcu->bootloader_mode || pcu->device_id == IMS_PCU_PCU_B_DEVICE_ID)
1474 static const struct attribute_group ims_pcu_ofn_attr_group = {
1476 .is_visible = ims_pcu_ofn_is_attr_visible,
1477 .attrs = ims_pcu_ofn_attrs,
1480 static void ims_pcu_irq(struct urb *urb)
1482 struct ims_pcu *pcu = urb->context;
1485 status = urb->status;
1494 /* this urb is terminated, clean up */
1495 dev_dbg(pcu->dev, "%s - urb shutting down with status: %d\n",
1499 dev_dbg(pcu->dev, "%s - nonzero urb status received: %d\n",
1504 dev_dbg(pcu->dev, "%s: received %d: %*ph\n", __func__,
1505 urb->actual_length, urb->actual_length, pcu->urb_in_buf);
1507 if (urb == pcu->urb_in)
1508 ims_pcu_process_data(pcu, urb);
1511 retval = usb_submit_urb(urb, GFP_ATOMIC);
1512 if (retval && retval != -ENODEV)
1513 dev_err(pcu->dev, "%s - usb_submit_urb failed with result %d\n",
1517 static int ims_pcu_buffers_alloc(struct ims_pcu *pcu)
1521 pcu->urb_in_buf = usb_alloc_coherent(pcu->udev, pcu->max_in_size,
1522 GFP_KERNEL, &pcu->read_dma);
1523 if (!pcu->urb_in_buf) {
1525 "Failed to allocate memory for read buffer\n");
1529 pcu->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1531 dev_err(pcu->dev, "Failed to allocate input URB\n");
1533 goto err_free_urb_in_buf;
1536 pcu->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1537 pcu->urb_in->transfer_dma = pcu->read_dma;
1539 usb_fill_bulk_urb(pcu->urb_in, pcu->udev,
1540 usb_rcvbulkpipe(pcu->udev,
1541 pcu->ep_in->bEndpointAddress),
1542 pcu->urb_in_buf, pcu->max_in_size,
1546 * We are using usb_bulk_msg() for sending so there is no point
1547 * in allocating memory with usb_alloc_coherent().
1549 pcu->urb_out_buf = kmalloc(pcu->max_out_size, GFP_KERNEL);
1550 if (!pcu->urb_out_buf) {
1551 dev_err(pcu->dev, "Failed to allocate memory for write buffer\n");
1553 goto err_free_in_urb;
1556 pcu->urb_ctrl_buf = usb_alloc_coherent(pcu->udev, pcu->max_ctrl_size,
1557 GFP_KERNEL, &pcu->ctrl_dma);
1558 if (!pcu->urb_ctrl_buf) {
1560 "Failed to allocate memory for read buffer\n");
1562 goto err_free_urb_out_buf;
1565 pcu->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL);
1566 if (!pcu->urb_ctrl) {
1567 dev_err(pcu->dev, "Failed to allocate input URB\n");
1569 goto err_free_urb_ctrl_buf;
1572 pcu->urb_ctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1573 pcu->urb_ctrl->transfer_dma = pcu->ctrl_dma;
1575 usb_fill_int_urb(pcu->urb_ctrl, pcu->udev,
1576 usb_rcvintpipe(pcu->udev,
1577 pcu->ep_ctrl->bEndpointAddress),
1578 pcu->urb_ctrl_buf, pcu->max_ctrl_size,
1579 ims_pcu_irq, pcu, pcu->ep_ctrl->bInterval);
1583 err_free_urb_ctrl_buf:
1584 usb_free_coherent(pcu->udev, pcu->max_ctrl_size,
1585 pcu->urb_ctrl_buf, pcu->ctrl_dma);
1586 err_free_urb_out_buf:
1587 kfree(pcu->urb_out_buf);
1589 usb_free_urb(pcu->urb_in);
1590 err_free_urb_in_buf:
1591 usb_free_coherent(pcu->udev, pcu->max_in_size,
1592 pcu->urb_in_buf, pcu->read_dma);
1596 static void ims_pcu_buffers_free(struct ims_pcu *pcu)
1598 usb_kill_urb(pcu->urb_in);
1599 usb_free_urb(pcu->urb_in);
1601 usb_free_coherent(pcu->udev, pcu->max_out_size,
1602 pcu->urb_in_buf, pcu->read_dma);
1604 kfree(pcu->urb_out_buf);
1606 usb_kill_urb(pcu->urb_ctrl);
1607 usb_free_urb(pcu->urb_ctrl);
1609 usb_free_coherent(pcu->udev, pcu->max_ctrl_size,
1610 pcu->urb_ctrl_buf, pcu->ctrl_dma);
1613 static const struct usb_cdc_union_desc *
1614 ims_pcu_get_cdc_union_desc(struct usb_interface *intf)
1616 const void *buf = intf->altsetting->extra;
1617 size_t buflen = intf->altsetting->extralen;
1618 struct usb_cdc_union_desc *union_desc;
1621 dev_err(&intf->dev, "Missing descriptor data\n");
1626 dev_err(&intf->dev, "Zero length descriptor\n");
1630 while (buflen >= sizeof(*union_desc)) {
1631 union_desc = (struct usb_cdc_union_desc *)buf;
1633 if (union_desc->bLength > buflen) {
1634 dev_err(&intf->dev, "Too large descriptor\n");
1638 if (union_desc->bDescriptorType == USB_DT_CS_INTERFACE &&
1639 union_desc->bDescriptorSubType == USB_CDC_UNION_TYPE) {
1640 dev_dbg(&intf->dev, "Found union header\n");
1642 if (union_desc->bLength >= sizeof(*union_desc))
1646 "Union descriptor too short (%d vs %zd)\n",
1647 union_desc->bLength, sizeof(*union_desc));
1651 buflen -= union_desc->bLength;
1652 buf += union_desc->bLength;
1655 dev_err(&intf->dev, "Missing CDC union descriptor\n");
1659 static int ims_pcu_parse_cdc_data(struct usb_interface *intf, struct ims_pcu *pcu)
1661 const struct usb_cdc_union_desc *union_desc;
1662 struct usb_host_interface *alt;
1664 union_desc = ims_pcu_get_cdc_union_desc(intf);
1668 pcu->ctrl_intf = usb_ifnum_to_if(pcu->udev,
1669 union_desc->bMasterInterface0);
1670 if (!pcu->ctrl_intf)
1673 alt = pcu->ctrl_intf->cur_altsetting;
1675 if (alt->desc.bNumEndpoints < 1)
1678 pcu->ep_ctrl = &alt->endpoint[0].desc;
1679 pcu->max_ctrl_size = usb_endpoint_maxp(pcu->ep_ctrl);
1681 pcu->data_intf = usb_ifnum_to_if(pcu->udev,
1682 union_desc->bSlaveInterface0);
1683 if (!pcu->data_intf)
1686 alt = pcu->data_intf->cur_altsetting;
1687 if (alt->desc.bNumEndpoints != 2) {
1689 "Incorrect number of endpoints on data interface (%d)\n",
1690 alt->desc.bNumEndpoints);
1694 pcu->ep_out = &alt->endpoint[0].desc;
1695 if (!usb_endpoint_is_bulk_out(pcu->ep_out)) {
1697 "First endpoint on data interface is not BULK OUT\n");
1701 pcu->max_out_size = usb_endpoint_maxp(pcu->ep_out);
1702 if (pcu->max_out_size < 8) {
1704 "Max OUT packet size is too small (%zd)\n",
1709 pcu->ep_in = &alt->endpoint[1].desc;
1710 if (!usb_endpoint_is_bulk_in(pcu->ep_in)) {
1712 "Second endpoint on data interface is not BULK IN\n");
1716 pcu->max_in_size = usb_endpoint_maxp(pcu->ep_in);
1717 if (pcu->max_in_size < 8) {
1719 "Max IN packet size is too small (%zd)\n",
1727 static int ims_pcu_start_io(struct ims_pcu *pcu)
1731 error = usb_submit_urb(pcu->urb_ctrl, GFP_KERNEL);
1734 "Failed to start control IO - usb_submit_urb failed with result: %d\n",
1739 error = usb_submit_urb(pcu->urb_in, GFP_KERNEL);
1742 "Failed to start IO - usb_submit_urb failed with result: %d\n",
1744 usb_kill_urb(pcu->urb_ctrl);
1751 static void ims_pcu_stop_io(struct ims_pcu *pcu)
1753 usb_kill_urb(pcu->urb_in);
1754 usb_kill_urb(pcu->urb_ctrl);
1757 static int ims_pcu_line_setup(struct ims_pcu *pcu)
1759 struct usb_host_interface *interface = pcu->ctrl_intf->cur_altsetting;
1760 struct usb_cdc_line_coding *line = (void *)pcu->cmd_buf;
1763 memset(line, 0, sizeof(*line));
1764 line->dwDTERate = cpu_to_le32(57600);
1765 line->bDataBits = 8;
1767 error = usb_control_msg(pcu->udev, usb_sndctrlpipe(pcu->udev, 0),
1768 USB_CDC_REQ_SET_LINE_CODING,
1769 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1770 0, interface->desc.bInterfaceNumber,
1771 line, sizeof(struct usb_cdc_line_coding),
1774 dev_err(pcu->dev, "Failed to set line coding, error: %d\n",
1779 error = usb_control_msg(pcu->udev, usb_sndctrlpipe(pcu->udev, 0),
1780 USB_CDC_REQ_SET_CONTROL_LINE_STATE,
1781 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1782 0x03, interface->desc.bInterfaceNumber,
1785 dev_err(pcu->dev, "Failed to set line state, error: %d\n",
1793 static int ims_pcu_get_device_info(struct ims_pcu *pcu)
1797 error = ims_pcu_get_info(pcu);
1801 error = ims_pcu_execute_query(pcu, GET_FW_VERSION);
1804 "GET_FW_VERSION command failed, error: %d\n", error);
1808 snprintf(pcu->fw_version, sizeof(pcu->fw_version),
1809 "%02d%02d%02d%02d.%c%c",
1810 pcu->cmd_buf[2], pcu->cmd_buf[3], pcu->cmd_buf[4], pcu->cmd_buf[5],
1811 pcu->cmd_buf[6], pcu->cmd_buf[7]);
1813 error = ims_pcu_execute_query(pcu, GET_BL_VERSION);
1816 "GET_BL_VERSION command failed, error: %d\n", error);
1820 snprintf(pcu->bl_version, sizeof(pcu->bl_version),
1821 "%02d%02d%02d%02d.%c%c",
1822 pcu->cmd_buf[2], pcu->cmd_buf[3], pcu->cmd_buf[4], pcu->cmd_buf[5],
1823 pcu->cmd_buf[6], pcu->cmd_buf[7]);
1825 error = ims_pcu_execute_query(pcu, RESET_REASON);
1828 "RESET_REASON command failed, error: %d\n", error);
1832 snprintf(pcu->reset_reason, sizeof(pcu->reset_reason),
1833 "%02x", pcu->cmd_buf[IMS_PCU_DATA_OFFSET]);
1836 "P/N: %s, MD: %s, S/N: %s, FW: %s, BL: %s, RR: %s\n",
1838 pcu->date_of_manufacturing,
1847 static int ims_pcu_identify_type(struct ims_pcu *pcu, u8 *device_id)
1851 error = ims_pcu_execute_query(pcu, GET_DEVICE_ID);
1854 "GET_DEVICE_ID command failed, error: %d\n", error);
1858 *device_id = pcu->cmd_buf[IMS_PCU_DATA_OFFSET];
1859 dev_dbg(pcu->dev, "Detected device ID: %d\n", *device_id);
1864 static int ims_pcu_init_application_mode(struct ims_pcu *pcu)
1866 static atomic_t device_no = ATOMIC_INIT(-1);
1868 const struct ims_pcu_device_info *info;
1871 error = ims_pcu_get_device_info(pcu);
1873 /* Device does not respond to basic queries, hopeless */
1877 error = ims_pcu_identify_type(pcu, &pcu->device_id);
1880 "Failed to identify device, error: %d\n", error);
1882 * Do not signal error, but do not create input nor
1883 * backlight devices either, let userspace figure this
1884 * out (flash a new firmware?).
1889 if (pcu->device_id >= ARRAY_SIZE(ims_pcu_device_info) ||
1890 !ims_pcu_device_info[pcu->device_id].keymap) {
1891 dev_err(pcu->dev, "Device ID %d is not valid\n", pcu->device_id);
1892 /* Same as above, punt to userspace */
1896 /* Device appears to be operable, complete initialization */
1897 pcu->device_no = atomic_inc_return(&device_no);
1899 error = ims_pcu_setup_backlight(pcu);
1903 info = &ims_pcu_device_info[pcu->device_id];
1904 error = ims_pcu_setup_buttons(pcu, info->keymap, info->keymap_len);
1906 goto err_destroy_backlight;
1908 if (info->has_gamepad) {
1909 error = ims_pcu_setup_gamepad(pcu);
1911 goto err_destroy_buttons;
1914 pcu->setup_complete = true;
1918 err_destroy_buttons:
1919 ims_pcu_destroy_buttons(pcu);
1920 err_destroy_backlight:
1921 ims_pcu_destroy_backlight(pcu);
1925 static void ims_pcu_destroy_application_mode(struct ims_pcu *pcu)
1927 if (pcu->setup_complete) {
1928 pcu->setup_complete = false;
1929 mb(); /* make sure flag setting is not reordered */
1932 ims_pcu_destroy_gamepad(pcu);
1933 ims_pcu_destroy_buttons(pcu);
1934 ims_pcu_destroy_backlight(pcu);
1938 static int ims_pcu_init_bootloader_mode(struct ims_pcu *pcu)
1942 error = ims_pcu_execute_bl_command(pcu, QUERY_DEVICE, NULL, 0,
1943 IMS_PCU_CMD_RESPONSE_TIMEOUT);
1945 dev_err(pcu->dev, "Bootloader does not respond, aborting\n");
1949 pcu->fw_start_addr =
1950 get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 11]);
1952 get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 15]);
1955 "Device is in bootloader mode (addr 0x%08x-0x%08x), requesting firmware\n",
1956 pcu->fw_start_addr, pcu->fw_end_addr);
1958 error = request_firmware_nowait(THIS_MODULE, true,
1959 IMS_PCU_FIRMWARE_NAME,
1960 pcu->dev, GFP_KERNEL, pcu,
1961 ims_pcu_process_async_firmware);
1963 /* This error is not fatal, let userspace have another chance */
1964 complete(&pcu->async_firmware_done);
1970 static void ims_pcu_destroy_bootloader_mode(struct ims_pcu *pcu)
1972 /* Make sure our initial firmware request has completed */
1973 wait_for_completion(&pcu->async_firmware_done);
1976 #define IMS_PCU_APPLICATION_MODE 0
1977 #define IMS_PCU_BOOTLOADER_MODE 1
1979 static struct usb_driver ims_pcu_driver;
1981 static int ims_pcu_probe(struct usb_interface *intf,
1982 const struct usb_device_id *id)
1984 struct usb_device *udev = interface_to_usbdev(intf);
1985 struct ims_pcu *pcu;
1988 pcu = kzalloc(sizeof(*pcu), GFP_KERNEL);
1992 pcu->dev = &intf->dev;
1994 pcu->bootloader_mode = id->driver_info == IMS_PCU_BOOTLOADER_MODE;
1995 mutex_init(&pcu->cmd_mutex);
1996 init_completion(&pcu->cmd_done);
1997 init_completion(&pcu->async_firmware_done);
1999 error = ims_pcu_parse_cdc_data(intf, pcu);
2003 error = usb_driver_claim_interface(&ims_pcu_driver,
2004 pcu->data_intf, pcu);
2007 "Unable to claim corresponding data interface: %d\n",
2012 usb_set_intfdata(pcu->ctrl_intf, pcu);
2014 error = ims_pcu_buffers_alloc(pcu);
2016 goto err_unclaim_intf;
2018 error = ims_pcu_start_io(pcu);
2020 goto err_free_buffers;
2022 error = ims_pcu_line_setup(pcu);
2026 error = pcu->bootloader_mode ?
2027 ims_pcu_init_bootloader_mode(pcu) :
2028 ims_pcu_init_application_mode(pcu);
2035 ims_pcu_stop_io(pcu);
2037 ims_pcu_buffers_free(pcu);
2039 usb_driver_release_interface(&ims_pcu_driver, pcu->data_intf);
2045 static void ims_pcu_disconnect(struct usb_interface *intf)
2047 struct ims_pcu *pcu = usb_get_intfdata(intf);
2048 struct usb_host_interface *alt = intf->cur_altsetting;
2050 usb_set_intfdata(intf, NULL);
2053 * See if we are dealing with control or data interface. The cleanup
2054 * happens when we unbind primary (control) interface.
2056 if (alt->desc.bInterfaceClass != USB_CLASS_COMM)
2059 ims_pcu_stop_io(pcu);
2061 if (pcu->bootloader_mode)
2062 ims_pcu_destroy_bootloader_mode(pcu);
2064 ims_pcu_destroy_application_mode(pcu);
2066 ims_pcu_buffers_free(pcu);
2071 static int ims_pcu_suspend(struct usb_interface *intf,
2072 pm_message_t message)
2074 struct ims_pcu *pcu = usb_get_intfdata(intf);
2075 struct usb_host_interface *alt = intf->cur_altsetting;
2077 if (alt->desc.bInterfaceClass == USB_CLASS_COMM)
2078 ims_pcu_stop_io(pcu);
2083 static int ims_pcu_resume(struct usb_interface *intf)
2085 struct ims_pcu *pcu = usb_get_intfdata(intf);
2086 struct usb_host_interface *alt = intf->cur_altsetting;
2089 if (alt->desc.bInterfaceClass == USB_CLASS_COMM) {
2090 retval = ims_pcu_start_io(pcu);
2092 retval = ims_pcu_line_setup(pcu);
2099 static const struct usb_device_id ims_pcu_id_table[] = {
2101 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0082,
2103 USB_CDC_SUBCLASS_ACM,
2104 USB_CDC_ACM_PROTO_AT_V25TER),
2105 .driver_info = IMS_PCU_APPLICATION_MODE,
2108 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0083,
2110 USB_CDC_SUBCLASS_ACM,
2111 USB_CDC_ACM_PROTO_AT_V25TER),
2112 .driver_info = IMS_PCU_BOOTLOADER_MODE,
2117 static const struct attribute_group *ims_pcu_sysfs_groups[] = {
2118 &ims_pcu_attr_group,
2119 &ims_pcu_ofn_attr_group,
2123 static struct usb_driver ims_pcu_driver = {
2125 .id_table = ims_pcu_id_table,
2126 .dev_groups = ims_pcu_sysfs_groups,
2127 .probe = ims_pcu_probe,
2128 .disconnect = ims_pcu_disconnect,
2130 .suspend = ims_pcu_suspend,
2131 .resume = ims_pcu_resume,
2132 .reset_resume = ims_pcu_resume,
2136 module_usb_driver(ims_pcu_driver);
2138 MODULE_DESCRIPTION("IMS Passenger Control Unit driver");
2139 MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
2140 MODULE_LICENSE("GPL");