2 * Driver for IMS Passenger Control Unit Devices
4 * Copyright (C) 2013 The IMS Company
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
11 #include <linux/completion.h>
12 #include <linux/device.h>
13 #include <linux/firmware.h>
14 #include <linux/ihex.h>
15 #include <linux/input.h>
16 #include <linux/kernel.h>
17 #include <linux/leds.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/usb/input.h>
22 #include <linux/usb/cdc.h>
23 #include <asm/unaligned.h>
25 #define IMS_PCU_KEYMAP_LEN 32
27 struct ims_pcu_buttons {
28 struct input_dev *input;
31 unsigned short keymap[IMS_PCU_KEYMAP_LEN];
34 struct ims_pcu_gamepad {
35 struct input_dev *input;
40 struct ims_pcu_backlight {
41 struct led_classdev cdev;
42 struct work_struct work;
43 enum led_brightness desired_brightness;
47 #define IMS_PCU_PART_NUMBER_LEN 15
48 #define IMS_PCU_SERIAL_NUMBER_LEN 8
49 #define IMS_PCU_DOM_LEN 8
50 #define IMS_PCU_FW_VERSION_LEN (9 + 1)
51 #define IMS_PCU_BL_VERSION_LEN (9 + 1)
52 #define IMS_PCU_BL_RESET_REASON_LEN (2 + 1)
54 #define IMS_PCU_BUF_SIZE 128
57 struct usb_device *udev;
58 struct device *dev; /* control interface's device, used for logging */
60 unsigned int device_no;
64 char part_number[IMS_PCU_PART_NUMBER_LEN];
65 char serial_number[IMS_PCU_SERIAL_NUMBER_LEN];
66 char date_of_manufacturing[IMS_PCU_DOM_LEN];
67 char fw_version[IMS_PCU_FW_VERSION_LEN];
68 char bl_version[IMS_PCU_BL_VERSION_LEN];
69 char reset_reason[IMS_PCU_BL_RESET_REASON_LEN];
70 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();
211 "Not enough memory for input input device\n");
215 snprintf(buttons->name, sizeof(buttons->name),
216 "IMS PCU#%d Button Interface", pcu->device_no);
218 usb_make_path(pcu->udev, buttons->phys, sizeof(buttons->phys));
219 strlcat(buttons->phys, "/input0", sizeof(buttons->phys));
221 memcpy(buttons->keymap, keymap, sizeof(*keymap) * keymap_len);
223 input->name = buttons->name;
224 input->phys = buttons->phys;
225 usb_to_input_id(pcu->udev, &input->id);
226 input->dev.parent = &pcu->ctrl_intf->dev;
228 input->keycode = buttons->keymap;
229 input->keycodemax = ARRAY_SIZE(buttons->keymap);
230 input->keycodesize = sizeof(buttons->keymap[0]);
232 __set_bit(EV_KEY, input->evbit);
233 for (i = 0; i < IMS_PCU_KEYMAP_LEN; i++)
234 __set_bit(buttons->keymap[i], input->keybit);
235 __clear_bit(KEY_RESERVED, input->keybit);
237 error = input_register_device(input);
240 "Failed to register buttons input device: %d\n",
242 input_free_device(input);
246 buttons->input = input;
250 static void ims_pcu_destroy_buttons(struct ims_pcu *pcu)
252 struct ims_pcu_buttons *buttons = &pcu->buttons;
254 input_unregister_device(buttons->input);
258 /*********************************************************************
259 * Gamepad Input device support *
260 *********************************************************************/
262 static void ims_pcu_gamepad_report(struct ims_pcu *pcu, u32 data)
264 struct ims_pcu_gamepad *gamepad = pcu->gamepad;
265 struct input_dev *input = gamepad->input;
268 x = !!(data & (1 << 14)) - !!(data & (1 << 13));
269 y = !!(data & (1 << 12)) - !!(data & (1 << 11));
271 input_report_abs(input, ABS_X, x);
272 input_report_abs(input, ABS_Y, y);
274 input_report_key(input, BTN_A, data & (1 << 7));
275 input_report_key(input, BTN_B, data & (1 << 8));
276 input_report_key(input, BTN_X, data & (1 << 9));
277 input_report_key(input, BTN_Y, data & (1 << 10));
278 input_report_key(input, BTN_START, data & (1 << 15));
279 input_report_key(input, BTN_SELECT, data & (1 << 16));
284 static int ims_pcu_setup_gamepad(struct ims_pcu *pcu)
286 struct ims_pcu_gamepad *gamepad;
287 struct input_dev *input;
290 gamepad = kzalloc(sizeof(struct ims_pcu_gamepad), GFP_KERNEL);
291 input = input_allocate_device();
292 if (!gamepad || !input) {
294 "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 */
375 #define IMS_PCU_RSP_STATUS 0xc0
376 #define IMS_PCU_RSP_PCU_RESET 0 /* Originally 0xc1 */
377 #define IMS_PCU_RSP_RESET_REASON 0xc2
378 #define IMS_PCU_RSP_SEND_BUTTONS 0xc3
379 #define IMS_PCU_RSP_JUMP_TO_BTLDR 0 /* Originally 0xc4 */
380 #define IMS_PCU_RSP_GET_INFO 0xc5
381 #define IMS_PCU_RSP_SET_BRIGHTNESS 0xc6
382 #define IMS_PCU_RSP_EEPROM 0xc7
383 #define IMS_PCU_RSP_GET_FW_VERSION 0xc8
384 #define IMS_PCU_RSP_GET_BL_VERSION 0xc9
385 #define IMS_PCU_RSP_SET_INFO 0xcb
386 #define IMS_PCU_RSP_GET_BRIGHTNESS 0xcc
387 #define IMS_PCU_RSP_CMD_INVALID 0xcd
388 #define IMS_PCU_RSP_GET_DEVICE_ID 0xce
389 #define IMS_PCU_RSP_SPECIAL_INFO 0xd0
390 #define IMS_PCU_RSP_BOOTLOADER 0xd1 /* Bootloader response */
392 #define IMS_PCU_RSP_EVNT_BUTTONS 0xe0 /* Unsolicited, button state */
393 #define IMS_PCU_GAMEPAD_MASK 0x0001ff80UL /* Bits 7 through 16 */
396 #define IMS_PCU_MIN_PACKET_LEN 3
397 #define IMS_PCU_DATA_OFFSET 2
399 #define IMS_PCU_CMD_WRITE_TIMEOUT 100 /* msec */
400 #define IMS_PCU_CMD_RESPONSE_TIMEOUT 500 /* msec */
402 static void ims_pcu_report_events(struct ims_pcu *pcu)
404 u32 data = get_unaligned_be32(&pcu->read_buf[3]);
406 ims_pcu_buttons_report(pcu, data & ~IMS_PCU_GAMEPAD_MASK);
408 ims_pcu_gamepad_report(pcu, data);
411 static void ims_pcu_handle_response(struct ims_pcu *pcu)
413 switch (pcu->read_buf[0]) {
414 case IMS_PCU_RSP_EVNT_BUTTONS:
415 if (likely(pcu->setup_complete))
416 ims_pcu_report_events(pcu);
421 * See if we got command completion.
422 * If both the sequence and response code match save
423 * the data and signal completion.
425 if (pcu->read_buf[0] == pcu->expected_response &&
426 pcu->read_buf[1] == pcu->ack_id - 1) {
428 memcpy(pcu->cmd_buf, pcu->read_buf, pcu->read_pos);
429 pcu->cmd_buf_len = pcu->read_pos;
430 complete(&pcu->cmd_done);
436 static void ims_pcu_process_data(struct ims_pcu *pcu, struct urb *urb)
440 for (i = 0; i < urb->actual_length; i++) {
441 u8 data = pcu->urb_in_buf[i];
443 /* Skip everything until we get Start Xmit */
444 if (!pcu->have_stx && data != IMS_PCU_PROTOCOL_STX)
448 pcu->have_dle = false;
449 pcu->read_buf[pcu->read_pos++] = data;
450 pcu->check_sum += data;
455 case IMS_PCU_PROTOCOL_STX:
458 "Unexpected STX at byte %d, discarding old data\n",
460 pcu->have_stx = true;
461 pcu->have_dle = false;
466 case IMS_PCU_PROTOCOL_DLE:
467 pcu->have_dle = true;
470 case IMS_PCU_PROTOCOL_ETX:
471 if (pcu->read_pos < IMS_PCU_MIN_PACKET_LEN) {
473 "Short packet received (%d bytes), ignoring\n",
475 } else if (pcu->check_sum != 0) {
477 "Invalid checksum in packet (%d bytes), ignoring\n",
480 ims_pcu_handle_response(pcu);
483 pcu->have_stx = false;
484 pcu->have_dle = false;
489 pcu->read_buf[pcu->read_pos++] = data;
490 pcu->check_sum += data;
496 static bool ims_pcu_byte_needs_escape(u8 byte)
498 return byte == IMS_PCU_PROTOCOL_STX ||
499 byte == IMS_PCU_PROTOCOL_ETX ||
500 byte == IMS_PCU_PROTOCOL_DLE;
503 static int ims_pcu_send_cmd_chunk(struct ims_pcu *pcu,
504 u8 command, int chunk, int len)
508 error = usb_bulk_msg(pcu->udev,
509 usb_sndbulkpipe(pcu->udev,
510 pcu->ep_out->bEndpointAddress),
511 pcu->urb_out_buf, len,
512 NULL, IMS_PCU_CMD_WRITE_TIMEOUT);
515 "Sending 0x%02x command failed at chunk %d: %d\n",
516 command, chunk, error);
523 static int ims_pcu_send_command(struct ims_pcu *pcu,
524 u8 command, const u8 *data, int len)
534 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_STX;
536 /* We know the command need not be escaped */
537 pcu->urb_out_buf[count++] = command;
540 ack_id = pcu->ack_id++;
542 ack_id = pcu->ack_id++;
544 if (ims_pcu_byte_needs_escape(ack_id))
545 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
547 pcu->urb_out_buf[count++] = ack_id;
550 for (i = 0; i < len; i++) {
552 delta = ims_pcu_byte_needs_escape(data[i]) ? 2 : 1;
553 if (count + delta >= pcu->max_out_size) {
554 error = ims_pcu_send_cmd_chunk(pcu, command,
563 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
565 pcu->urb_out_buf[count++] = data[i];
571 delta = ims_pcu_byte_needs_escape(csum) ? 3 : 2;
572 if (count + delta >= pcu->max_out_size) {
573 error = ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count);
581 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE;
583 pcu->urb_out_buf[count++] = csum;
584 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_ETX;
586 return ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count);
589 static int __ims_pcu_execute_command(struct ims_pcu *pcu,
590 u8 command, const void *data, size_t len,
591 u8 expected_response, int response_time)
595 pcu->expected_response = expected_response;
596 init_completion(&pcu->cmd_done);
598 error = ims_pcu_send_command(pcu, command, data, len);
602 if (expected_response &&
603 !wait_for_completion_timeout(&pcu->cmd_done,
604 msecs_to_jiffies(response_time))) {
605 dev_dbg(pcu->dev, "Command 0x%02x timed out\n", command);
612 #define ims_pcu_execute_command(pcu, code, data, len) \
613 __ims_pcu_execute_command(pcu, \
614 IMS_PCU_CMD_##code, data, len, \
615 IMS_PCU_RSP_##code, \
616 IMS_PCU_CMD_RESPONSE_TIMEOUT)
618 #define ims_pcu_execute_query(pcu, code) \
619 ims_pcu_execute_command(pcu, code, NULL, 0)
621 /* Bootloader commands */
622 #define IMS_PCU_BL_CMD_QUERY_DEVICE 0xa1
623 #define IMS_PCU_BL_CMD_UNLOCK_CONFIG 0xa2
624 #define IMS_PCU_BL_CMD_ERASE_APP 0xa3
625 #define IMS_PCU_BL_CMD_PROGRAM_DEVICE 0xa4
626 #define IMS_PCU_BL_CMD_PROGRAM_COMPLETE 0xa5
627 #define IMS_PCU_BL_CMD_READ_APP 0xa6
628 #define IMS_PCU_BL_CMD_RESET_DEVICE 0xa7
629 #define IMS_PCU_BL_CMD_LAUNCH_APP 0xa8
631 /* Bootloader commands */
632 #define IMS_PCU_BL_RSP_QUERY_DEVICE 0xc1
633 #define IMS_PCU_BL_RSP_UNLOCK_CONFIG 0xc2
634 #define IMS_PCU_BL_RSP_ERASE_APP 0xc3
635 #define IMS_PCU_BL_RSP_PROGRAM_DEVICE 0xc4
636 #define IMS_PCU_BL_RSP_PROGRAM_COMPLETE 0xc5
637 #define IMS_PCU_BL_RSP_READ_APP 0xc6
638 #define IMS_PCU_BL_RSP_RESET_DEVICE 0 /* originally 0xa7 */
639 #define IMS_PCU_BL_RSP_LAUNCH_APP 0 /* originally 0xa8 */
641 #define IMS_PCU_BL_DATA_OFFSET 3
643 static int __ims_pcu_execute_bl_command(struct ims_pcu *pcu,
644 u8 command, const void *data, size_t len,
645 u8 expected_response, int response_time)
649 pcu->cmd_buf[0] = command;
651 memcpy(&pcu->cmd_buf[1], data, len);
653 error = __ims_pcu_execute_command(pcu,
654 IMS_PCU_CMD_BOOTLOADER, pcu->cmd_buf, len + 1,
655 expected_response ? IMS_PCU_RSP_BOOTLOADER : 0,
659 "Failure when sending 0x%02x command to bootloader, error: %d\n",
660 pcu->cmd_buf[0], error);
664 if (expected_response && pcu->cmd_buf[2] != expected_response) {
666 "Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n",
667 pcu->cmd_buf[2], expected_response);
674 #define ims_pcu_execute_bl_command(pcu, code, data, len, timeout) \
675 __ims_pcu_execute_bl_command(pcu, \
676 IMS_PCU_BL_CMD_##code, data, len, \
677 IMS_PCU_BL_RSP_##code, timeout) \
679 #define IMS_PCU_INFO_PART_OFFSET 2
680 #define IMS_PCU_INFO_DOM_OFFSET 17
681 #define IMS_PCU_INFO_SERIAL_OFFSET 25
683 #define IMS_PCU_SET_INFO_SIZE 31
685 static int ims_pcu_get_info(struct ims_pcu *pcu)
689 error = ims_pcu_execute_query(pcu, GET_INFO);
692 "GET_INFO command failed, error: %d\n", error);
696 memcpy(pcu->part_number,
697 &pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET],
698 sizeof(pcu->part_number));
699 memcpy(pcu->date_of_manufacturing,
700 &pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET],
701 sizeof(pcu->date_of_manufacturing));
702 memcpy(pcu->serial_number,
703 &pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET],
704 sizeof(pcu->serial_number));
709 static int ims_pcu_set_info(struct ims_pcu *pcu)
713 memcpy(&pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET],
714 pcu->part_number, sizeof(pcu->part_number));
715 memcpy(&pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET],
716 pcu->date_of_manufacturing, sizeof(pcu->date_of_manufacturing));
717 memcpy(&pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET],
718 pcu->serial_number, sizeof(pcu->serial_number));
720 error = ims_pcu_execute_command(pcu, SET_INFO,
721 &pcu->cmd_buf[IMS_PCU_DATA_OFFSET],
722 IMS_PCU_SET_INFO_SIZE);
725 "Failed to update device information, error: %d\n",
733 static int ims_pcu_switch_to_bootloader(struct ims_pcu *pcu)
737 /* Execute jump to the bootoloader */
738 error = ims_pcu_execute_command(pcu, JUMP_TO_BTLDR, NULL, 0);
741 "Failure when sending JUMP TO BOOLTLOADER command, error: %d\n",
749 /*********************************************************************
750 * Firmware Update handling *
751 *********************************************************************/
753 #define IMS_PCU_FIRMWARE_NAME "imspcu.fw"
755 struct ims_pcu_flash_fmt {
761 static unsigned int ims_pcu_count_fw_records(const struct firmware *fw)
763 const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data;
764 unsigned int count = 0;
768 rec = ihex_next_binrec(rec);
774 static int ims_pcu_verify_block(struct ims_pcu *pcu,
775 u32 addr, u8 len, const u8 *data)
777 struct ims_pcu_flash_fmt *fragment;
780 fragment = (void *)&pcu->cmd_buf[1];
781 put_unaligned_le32(addr, &fragment->addr);
784 error = ims_pcu_execute_bl_command(pcu, READ_APP, NULL, 5,
785 IMS_PCU_CMD_RESPONSE_TIMEOUT);
788 "Failed to retrieve block at 0x%08x, len %d, error: %d\n",
793 fragment = (void *)&pcu->cmd_buf[IMS_PCU_BL_DATA_OFFSET];
794 if (get_unaligned_le32(&fragment->addr) != addr ||
795 fragment->len != len) {
797 "Wrong block when retrieving 0x%08x (0x%08x), len %d (%d)\n",
798 addr, get_unaligned_le32(&fragment->addr),
803 if (memcmp(fragment->data, data, len)) {
805 "Mismatch in block at 0x%08x, len %d\n",
813 static int ims_pcu_flash_firmware(struct ims_pcu *pcu,
814 const struct firmware *fw,
815 unsigned int n_fw_records)
817 const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data;
818 struct ims_pcu_flash_fmt *fragment;
819 unsigned int count = 0;
824 error = ims_pcu_execute_bl_command(pcu, ERASE_APP, NULL, 0, 2000);
827 "Failed to erase application image, error: %d\n",
834 * The firmware format is messed up for some reason.
835 * The address twice that of what is needed for some
836 * reason and we end up overwriting half of the data
837 * with the next record.
839 addr = be32_to_cpu(rec->addr) / 2;
840 len = be16_to_cpu(rec->len);
842 fragment = (void *)&pcu->cmd_buf[1];
843 put_unaligned_le32(addr, &fragment->addr);
845 memcpy(fragment->data, rec->data, len);
847 error = ims_pcu_execute_bl_command(pcu, PROGRAM_DEVICE,
849 IMS_PCU_CMD_RESPONSE_TIMEOUT);
852 "Failed to write block at 0x%08x, len %d, error: %d\n",
857 if (addr >= pcu->fw_start_addr && addr < pcu->fw_end_addr) {
858 error = ims_pcu_verify_block(pcu, addr, len, rec->data);
864 pcu->update_firmware_status = (count * 100) / n_fw_records;
866 rec = ihex_next_binrec(rec);
869 error = ims_pcu_execute_bl_command(pcu, PROGRAM_COMPLETE,
873 "Failed to send PROGRAM_COMPLETE, error: %d\n",
879 static int ims_pcu_handle_firmware_update(struct ims_pcu *pcu,
880 const struct firmware *fw)
882 unsigned int n_fw_records;
885 dev_info(pcu->dev, "Updating firmware %s, size: %zu\n",
886 IMS_PCU_FIRMWARE_NAME, fw->size);
888 n_fw_records = ims_pcu_count_fw_records(fw);
890 retval = ims_pcu_flash_firmware(pcu, fw, n_fw_records);
894 retval = ims_pcu_execute_bl_command(pcu, LAUNCH_APP, NULL, 0, 0);
897 "Failed to start application image, error: %d\n",
901 pcu->update_firmware_status = retval;
902 sysfs_notify(&pcu->dev->kobj, NULL, "update_firmware_status");
906 static void ims_pcu_process_async_firmware(const struct firmware *fw,
909 struct ims_pcu *pcu = context;
913 dev_err(pcu->dev, "Failed to get firmware %s\n",
914 IMS_PCU_FIRMWARE_NAME);
918 error = ihex_validate_fw(fw);
920 dev_err(pcu->dev, "Firmware %s is invalid\n",
921 IMS_PCU_FIRMWARE_NAME);
925 mutex_lock(&pcu->cmd_mutex);
926 ims_pcu_handle_firmware_update(pcu, fw);
927 mutex_unlock(&pcu->cmd_mutex);
929 release_firmware(fw);
932 complete(&pcu->async_firmware_done);
935 /*********************************************************************
936 * Backlight LED device support *
937 *********************************************************************/
939 #define IMS_PCU_MAX_BRIGHTNESS 31998
941 static void ims_pcu_backlight_work(struct work_struct *work)
943 struct ims_pcu_backlight *backlight =
944 container_of(work, struct ims_pcu_backlight, work);
945 struct ims_pcu *pcu =
946 container_of(backlight, struct ims_pcu, backlight);
947 int desired_brightness = backlight->desired_brightness;
948 __le16 br_val = cpu_to_le16(desired_brightness);
951 mutex_lock(&pcu->cmd_mutex);
953 error = ims_pcu_execute_command(pcu, SET_BRIGHTNESS,
954 &br_val, sizeof(br_val));
955 if (error && error != -ENODEV)
957 "Failed to set desired brightness %u, error: %d\n",
958 desired_brightness, error);
960 mutex_unlock(&pcu->cmd_mutex);
963 static void ims_pcu_backlight_set_brightness(struct led_classdev *cdev,
964 enum led_brightness value)
966 struct ims_pcu_backlight *backlight =
967 container_of(cdev, struct ims_pcu_backlight, cdev);
969 backlight->desired_brightness = value;
970 schedule_work(&backlight->work);
973 static enum led_brightness
974 ims_pcu_backlight_get_brightness(struct led_classdev *cdev)
976 struct ims_pcu_backlight *backlight =
977 container_of(cdev, struct ims_pcu_backlight, cdev);
978 struct ims_pcu *pcu =
979 container_of(backlight, struct ims_pcu, backlight);
983 mutex_lock(&pcu->cmd_mutex);
985 error = ims_pcu_execute_query(pcu, GET_BRIGHTNESS);
988 "Failed to get current brightness, error: %d\n",
990 /* Assume the LED is OFF */
991 brightness = LED_OFF;
994 get_unaligned_le16(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET]);
997 mutex_unlock(&pcu->cmd_mutex);
1002 static int ims_pcu_setup_backlight(struct ims_pcu *pcu)
1004 struct ims_pcu_backlight *backlight = &pcu->backlight;
1007 INIT_WORK(&backlight->work, ims_pcu_backlight_work);
1008 snprintf(backlight->name, sizeof(backlight->name),
1009 "pcu%d::kbd_backlight", pcu->device_no);
1011 backlight->cdev.name = backlight->name;
1012 backlight->cdev.max_brightness = IMS_PCU_MAX_BRIGHTNESS;
1013 backlight->cdev.brightness_get = ims_pcu_backlight_get_brightness;
1014 backlight->cdev.brightness_set = ims_pcu_backlight_set_brightness;
1016 error = led_classdev_register(pcu->dev, &backlight->cdev);
1019 "Failed to register backlight LED device, error: %d\n",
1027 static void ims_pcu_destroy_backlight(struct ims_pcu *pcu)
1029 struct ims_pcu_backlight *backlight = &pcu->backlight;
1031 led_classdev_unregister(&backlight->cdev);
1032 cancel_work_sync(&backlight->work);
1036 /*********************************************************************
1037 * Sysfs attributes handling *
1038 *********************************************************************/
1040 struct ims_pcu_attribute {
1041 struct device_attribute dattr;
1042 size_t field_offset;
1046 static ssize_t ims_pcu_attribute_show(struct device *dev,
1047 struct device_attribute *dattr,
1050 struct usb_interface *intf = to_usb_interface(dev);
1051 struct ims_pcu *pcu = usb_get_intfdata(intf);
1052 struct ims_pcu_attribute *attr =
1053 container_of(dattr, struct ims_pcu_attribute, dattr);
1054 char *field = (char *)pcu + attr->field_offset;
1056 return scnprintf(buf, PAGE_SIZE, "%.*s\n", attr->field_length, field);
1059 static ssize_t ims_pcu_attribute_store(struct device *dev,
1060 struct device_attribute *dattr,
1061 const char *buf, size_t count)
1064 struct usb_interface *intf = to_usb_interface(dev);
1065 struct ims_pcu *pcu = usb_get_intfdata(intf);
1066 struct ims_pcu_attribute *attr =
1067 container_of(dattr, struct ims_pcu_attribute, dattr);
1068 char *field = (char *)pcu + attr->field_offset;
1072 if (count > attr->field_length)
1075 data_len = strnlen(buf, attr->field_length);
1076 if (data_len > attr->field_length)
1079 error = mutex_lock_interruptible(&pcu->cmd_mutex);
1083 memset(field, 0, attr->field_length);
1084 memcpy(field, buf, data_len);
1086 error = ims_pcu_set_info(pcu);
1089 * Even if update failed, let's fetch the info again as we just
1090 * clobbered one of the fields.
1092 ims_pcu_get_info(pcu);
1094 mutex_unlock(&pcu->cmd_mutex);
1096 return error < 0 ? error : count;
1099 #define IMS_PCU_ATTR(_field, _mode) \
1100 struct ims_pcu_attribute ims_pcu_attr_##_field = { \
1101 .dattr = __ATTR(_field, _mode, \
1102 ims_pcu_attribute_show, \
1103 ims_pcu_attribute_store), \
1104 .field_offset = offsetof(struct ims_pcu, _field), \
1105 .field_length = sizeof(((struct ims_pcu *)NULL)->_field), \
1108 #define IMS_PCU_RO_ATTR(_field) \
1109 IMS_PCU_ATTR(_field, S_IRUGO)
1110 #define IMS_PCU_RW_ATTR(_field) \
1111 IMS_PCU_ATTR(_field, S_IRUGO | S_IWUSR)
1113 static IMS_PCU_RW_ATTR(part_number);
1114 static IMS_PCU_RW_ATTR(serial_number);
1115 static IMS_PCU_RW_ATTR(date_of_manufacturing);
1117 static IMS_PCU_RO_ATTR(fw_version);
1118 static IMS_PCU_RO_ATTR(bl_version);
1119 static IMS_PCU_RO_ATTR(reset_reason);
1121 static ssize_t ims_pcu_reset_device(struct device *dev,
1122 struct device_attribute *dattr,
1123 const char *buf, size_t count)
1125 static const u8 reset_byte = 1;
1126 struct usb_interface *intf = to_usb_interface(dev);
1127 struct ims_pcu *pcu = usb_get_intfdata(intf);
1131 error = kstrtoint(buf, 0, &value);
1138 dev_info(pcu->dev, "Attempting to reset device\n");
1140 error = ims_pcu_execute_command(pcu, PCU_RESET, &reset_byte, 1);
1143 "Failed to reset device, error: %d\n",
1151 static DEVICE_ATTR(reset_device, S_IWUSR, NULL, ims_pcu_reset_device);
1153 static ssize_t ims_pcu_update_firmware_store(struct device *dev,
1154 struct device_attribute *dattr,
1155 const char *buf, size_t count)
1157 struct usb_interface *intf = to_usb_interface(dev);
1158 struct ims_pcu *pcu = usb_get_intfdata(intf);
1159 const struct firmware *fw;
1163 error = kstrtoint(buf, 0, &value);
1170 error = mutex_lock_interruptible(&pcu->cmd_mutex);
1174 error = request_ihex_firmware(&fw, IMS_PCU_FIRMWARE_NAME, pcu->dev);
1176 dev_err(pcu->dev, "Failed to request firmware %s, error: %d\n",
1177 IMS_PCU_FIRMWARE_NAME, error);
1182 * If we are already in bootloader mode we can proceed with
1183 * flashing the firmware.
1185 * If we are in application mode, then we need to switch into
1186 * bootloader mode, which will cause the device to disconnect
1187 * and reconnect as different device.
1189 if (pcu->bootloader_mode)
1190 error = ims_pcu_handle_firmware_update(pcu, fw);
1192 error = ims_pcu_switch_to_bootloader(pcu);
1194 release_firmware(fw);
1197 mutex_unlock(&pcu->cmd_mutex);
1198 return error ?: count;
1201 static DEVICE_ATTR(update_firmware, S_IWUSR,
1202 NULL, ims_pcu_update_firmware_store);
1205 ims_pcu_update_firmware_status_show(struct device *dev,
1206 struct device_attribute *dattr,
1209 struct usb_interface *intf = to_usb_interface(dev);
1210 struct ims_pcu *pcu = usb_get_intfdata(intf);
1212 return scnprintf(buf, PAGE_SIZE, "%d\n", pcu->update_firmware_status);
1215 static DEVICE_ATTR(update_firmware_status, S_IRUGO,
1216 ims_pcu_update_firmware_status_show, NULL);
1218 static struct attribute *ims_pcu_attrs[] = {
1219 &ims_pcu_attr_part_number.dattr.attr,
1220 &ims_pcu_attr_serial_number.dattr.attr,
1221 &ims_pcu_attr_date_of_manufacturing.dattr.attr,
1222 &ims_pcu_attr_fw_version.dattr.attr,
1223 &ims_pcu_attr_bl_version.dattr.attr,
1224 &ims_pcu_attr_reset_reason.dattr.attr,
1225 &dev_attr_reset_device.attr,
1226 &dev_attr_update_firmware.attr,
1227 &dev_attr_update_firmware_status.attr,
1231 static umode_t ims_pcu_is_attr_visible(struct kobject *kobj,
1232 struct attribute *attr, int n)
1234 struct device *dev = container_of(kobj, struct device, kobj);
1235 struct usb_interface *intf = to_usb_interface(dev);
1236 struct ims_pcu *pcu = usb_get_intfdata(intf);
1237 umode_t mode = attr->mode;
1239 if (pcu->bootloader_mode) {
1240 if (attr != &dev_attr_update_firmware_status.attr &&
1241 attr != &dev_attr_update_firmware.attr &&
1242 attr != &dev_attr_reset_device.attr) {
1246 if (attr == &dev_attr_update_firmware_status.attr)
1253 static struct attribute_group ims_pcu_attr_group = {
1254 .is_visible = ims_pcu_is_attr_visible,
1255 .attrs = ims_pcu_attrs,
1258 static void ims_pcu_irq(struct urb *urb)
1260 struct ims_pcu *pcu = urb->context;
1263 status = urb->status;
1272 /* this urb is terminated, clean up */
1273 dev_dbg(pcu->dev, "%s - urb shutting down with status: %d\n",
1277 dev_dbg(pcu->dev, "%s - nonzero urb status received: %d\n",
1282 dev_dbg(pcu->dev, "%s: received %d: %*ph\n", __func__,
1283 urb->actual_length, urb->actual_length, pcu->urb_in_buf);
1285 if (urb == pcu->urb_in)
1286 ims_pcu_process_data(pcu, urb);
1289 retval = usb_submit_urb(urb, GFP_ATOMIC);
1290 if (retval && retval != -ENODEV)
1291 dev_err(pcu->dev, "%s - usb_submit_urb failed with result %d\n",
1295 static int ims_pcu_buffers_alloc(struct ims_pcu *pcu)
1299 pcu->urb_in_buf = usb_alloc_coherent(pcu->udev, pcu->max_in_size,
1300 GFP_KERNEL, &pcu->read_dma);
1301 if (!pcu->urb_in_buf) {
1303 "Failed to allocate memory for read buffer\n");
1307 pcu->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1309 dev_err(pcu->dev, "Failed to allocate input URB\n");
1311 goto err_free_urb_in_buf;
1314 pcu->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1315 pcu->urb_in->transfer_dma = pcu->read_dma;
1317 usb_fill_bulk_urb(pcu->urb_in, pcu->udev,
1318 usb_rcvbulkpipe(pcu->udev,
1319 pcu->ep_in->bEndpointAddress),
1320 pcu->urb_in_buf, pcu->max_in_size,
1324 * We are using usb_bulk_msg() for sending so there is no point
1325 * in allocating memory with usb_alloc_coherent().
1327 pcu->urb_out_buf = kmalloc(pcu->max_out_size, GFP_KERNEL);
1328 if (!pcu->urb_out_buf) {
1329 dev_err(pcu->dev, "Failed to allocate memory for write buffer\n");
1331 goto err_free_in_urb;
1334 pcu->urb_ctrl_buf = usb_alloc_coherent(pcu->udev, pcu->max_ctrl_size,
1335 GFP_KERNEL, &pcu->ctrl_dma);
1336 if (!pcu->urb_ctrl_buf) {
1338 "Failed to allocate memory for read buffer\n");
1339 goto err_free_urb_out_buf;
1342 pcu->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL);
1343 if (!pcu->urb_ctrl) {
1344 dev_err(pcu->dev, "Failed to allocate input URB\n");
1346 goto err_free_urb_ctrl_buf;
1349 pcu->urb_ctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1350 pcu->urb_ctrl->transfer_dma = pcu->ctrl_dma;
1352 usb_fill_int_urb(pcu->urb_ctrl, pcu->udev,
1353 usb_rcvintpipe(pcu->udev,
1354 pcu->ep_ctrl->bEndpointAddress),
1355 pcu->urb_ctrl_buf, pcu->max_ctrl_size,
1356 ims_pcu_irq, pcu, pcu->ep_ctrl->bInterval);
1360 err_free_urb_ctrl_buf:
1361 usb_free_coherent(pcu->udev, pcu->max_ctrl_size,
1362 pcu->urb_ctrl_buf, pcu->ctrl_dma);
1363 err_free_urb_out_buf:
1364 kfree(pcu->urb_out_buf);
1366 usb_free_urb(pcu->urb_in);
1367 err_free_urb_in_buf:
1368 usb_free_coherent(pcu->udev, pcu->max_in_size,
1369 pcu->urb_in_buf, pcu->read_dma);
1373 static void ims_pcu_buffers_free(struct ims_pcu *pcu)
1375 usb_kill_urb(pcu->urb_in);
1376 usb_free_urb(pcu->urb_in);
1378 usb_free_coherent(pcu->udev, pcu->max_out_size,
1379 pcu->urb_in_buf, pcu->read_dma);
1381 kfree(pcu->urb_out_buf);
1383 usb_kill_urb(pcu->urb_ctrl);
1384 usb_free_urb(pcu->urb_ctrl);
1386 usb_free_coherent(pcu->udev, pcu->max_ctrl_size,
1387 pcu->urb_ctrl_buf, pcu->ctrl_dma);
1390 static const struct usb_cdc_union_desc *
1391 ims_pcu_get_cdc_union_desc(struct usb_interface *intf)
1393 const void *buf = intf->altsetting->extra;
1394 size_t buflen = intf->altsetting->extralen;
1395 struct usb_cdc_union_desc *union_desc;
1398 dev_err(&intf->dev, "Missing descriptor data\n");
1403 dev_err(&intf->dev, "Zero length descriptor\n");
1407 while (buflen > 0) {
1408 union_desc = (struct usb_cdc_union_desc *)buf;
1410 if (union_desc->bDescriptorType == USB_DT_CS_INTERFACE &&
1411 union_desc->bDescriptorSubType == USB_CDC_UNION_TYPE) {
1412 dev_dbg(&intf->dev, "Found union header\n");
1416 buflen -= union_desc->bLength;
1417 buf += union_desc->bLength;
1420 dev_err(&intf->dev, "Missing CDC union descriptor\n");
1424 static int ims_pcu_parse_cdc_data(struct usb_interface *intf, struct ims_pcu *pcu)
1426 const struct usb_cdc_union_desc *union_desc;
1427 struct usb_host_interface *alt;
1429 union_desc = ims_pcu_get_cdc_union_desc(intf);
1433 pcu->ctrl_intf = usb_ifnum_to_if(pcu->udev,
1434 union_desc->bMasterInterface0);
1436 alt = pcu->ctrl_intf->cur_altsetting;
1437 pcu->ep_ctrl = &alt->endpoint[0].desc;
1438 pcu->max_ctrl_size = usb_endpoint_maxp(pcu->ep_ctrl);
1440 pcu->data_intf = usb_ifnum_to_if(pcu->udev,
1441 union_desc->bSlaveInterface0);
1443 alt = pcu->data_intf->cur_altsetting;
1444 if (alt->desc.bNumEndpoints != 2) {
1446 "Incorrect number of endpoints on data interface (%d)\n",
1447 alt->desc.bNumEndpoints);
1451 pcu->ep_out = &alt->endpoint[0].desc;
1452 if (!usb_endpoint_is_bulk_out(pcu->ep_out)) {
1454 "First endpoint on data interface is not BULK OUT\n");
1458 pcu->max_out_size = usb_endpoint_maxp(pcu->ep_out);
1459 if (pcu->max_out_size < 8) {
1461 "Max OUT packet size is too small (%zd)\n",
1466 pcu->ep_in = &alt->endpoint[1].desc;
1467 if (!usb_endpoint_is_bulk_in(pcu->ep_in)) {
1469 "Second endpoint on data interface is not BULK IN\n");
1473 pcu->max_in_size = usb_endpoint_maxp(pcu->ep_in);
1474 if (pcu->max_in_size < 8) {
1476 "Max IN packet size is too small (%zd)\n",
1484 static int ims_pcu_start_io(struct ims_pcu *pcu)
1488 error = usb_submit_urb(pcu->urb_ctrl, GFP_KERNEL);
1491 "Failed to start control IO - usb_submit_urb failed with result: %d\n",
1496 error = usb_submit_urb(pcu->urb_in, GFP_KERNEL);
1499 "Failed to start IO - usb_submit_urb failed with result: %d\n",
1501 usb_kill_urb(pcu->urb_ctrl);
1508 static void ims_pcu_stop_io(struct ims_pcu *pcu)
1510 usb_kill_urb(pcu->urb_in);
1511 usb_kill_urb(pcu->urb_ctrl);
1514 static int ims_pcu_line_setup(struct ims_pcu *pcu)
1516 struct usb_host_interface *interface = pcu->ctrl_intf->cur_altsetting;
1517 struct usb_cdc_line_coding *line = (void *)pcu->cmd_buf;
1520 memset(line, 0, sizeof(*line));
1521 line->dwDTERate = cpu_to_le32(57600);
1522 line->bDataBits = 8;
1524 error = usb_control_msg(pcu->udev, usb_sndctrlpipe(pcu->udev, 0),
1525 USB_CDC_REQ_SET_LINE_CODING,
1526 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1527 0, interface->desc.bInterfaceNumber,
1528 line, sizeof(struct usb_cdc_line_coding),
1531 dev_err(pcu->dev, "Failed to set line coding, error: %d\n",
1536 error = usb_control_msg(pcu->udev, usb_sndctrlpipe(pcu->udev, 0),
1537 USB_CDC_REQ_SET_CONTROL_LINE_STATE,
1538 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1539 0x03, interface->desc.bInterfaceNumber,
1542 dev_err(pcu->dev, "Failed to set line state, error: %d\n",
1550 static int ims_pcu_get_device_info(struct ims_pcu *pcu)
1554 error = ims_pcu_get_info(pcu);
1558 error = ims_pcu_execute_query(pcu, GET_FW_VERSION);
1561 "GET_FW_VERSION command failed, error: %d\n", error);
1565 snprintf(pcu->fw_version, sizeof(pcu->fw_version),
1566 "%02d%02d%02d%02d.%c%c",
1567 pcu->cmd_buf[2], pcu->cmd_buf[3], pcu->cmd_buf[4], pcu->cmd_buf[5],
1568 pcu->cmd_buf[6], pcu->cmd_buf[7]);
1570 error = ims_pcu_execute_query(pcu, GET_BL_VERSION);
1573 "GET_BL_VERSION command failed, error: %d\n", error);
1577 snprintf(pcu->bl_version, sizeof(pcu->bl_version),
1578 "%02d%02d%02d%02d.%c%c",
1579 pcu->cmd_buf[2], pcu->cmd_buf[3], pcu->cmd_buf[4], pcu->cmd_buf[5],
1580 pcu->cmd_buf[6], pcu->cmd_buf[7]);
1582 error = ims_pcu_execute_query(pcu, RESET_REASON);
1585 "RESET_REASON command failed, error: %d\n", error);
1589 snprintf(pcu->reset_reason, sizeof(pcu->reset_reason),
1590 "%02x", pcu->cmd_buf[IMS_PCU_DATA_OFFSET]);
1593 "P/N: %s, MD: %s, S/N: %s, FW: %s, BL: %s, RR: %s\n",
1595 pcu->date_of_manufacturing,
1604 static int ims_pcu_identify_type(struct ims_pcu *pcu, u8 *device_id)
1608 error = ims_pcu_execute_query(pcu, GET_DEVICE_ID);
1611 "GET_DEVICE_ID command failed, error: %d\n", error);
1615 *device_id = pcu->cmd_buf[IMS_PCU_DATA_OFFSET];
1616 dev_dbg(pcu->dev, "Detected device ID: %d\n", *device_id);
1621 static int ims_pcu_init_application_mode(struct ims_pcu *pcu)
1623 static atomic_t device_no = ATOMIC_INIT(0);
1625 const struct ims_pcu_device_info *info;
1629 error = ims_pcu_get_device_info(pcu);
1631 /* Device does not respond to basic queries, hopeless */
1635 error = ims_pcu_identify_type(pcu, &device_id);
1638 "Failed to identify device, error: %d\n", error);
1640 * Do not signal error, but do not create input nor
1641 * backlight devices either, let userspace figure this
1642 * out (flash a new firmware?).
1647 if (device_id >= ARRAY_SIZE(ims_pcu_device_info) ||
1648 !ims_pcu_device_info[device_id].keymap) {
1649 dev_err(pcu->dev, "Device ID %d is not valid\n", device_id);
1650 /* Same as above, punt to userspace */
1654 /* Device appears to be operable, complete initialization */
1655 pcu->device_no = atomic_inc_return(&device_no) - 1;
1657 error = ims_pcu_setup_backlight(pcu);
1661 info = &ims_pcu_device_info[device_id];
1662 error = ims_pcu_setup_buttons(pcu, info->keymap, info->keymap_len);
1664 goto err_destroy_backlight;
1666 if (info->has_gamepad) {
1667 error = ims_pcu_setup_gamepad(pcu);
1669 goto err_destroy_buttons;
1672 pcu->setup_complete = true;
1676 err_destroy_backlight:
1677 ims_pcu_destroy_backlight(pcu);
1678 err_destroy_buttons:
1679 ims_pcu_destroy_buttons(pcu);
1683 static void ims_pcu_destroy_application_mode(struct ims_pcu *pcu)
1685 if (pcu->setup_complete) {
1686 pcu->setup_complete = false;
1687 mb(); /* make sure flag setting is not reordered */
1690 ims_pcu_destroy_gamepad(pcu);
1691 ims_pcu_destroy_buttons(pcu);
1692 ims_pcu_destroy_backlight(pcu);
1696 static int ims_pcu_init_bootloader_mode(struct ims_pcu *pcu)
1700 error = ims_pcu_execute_bl_command(pcu, QUERY_DEVICE, NULL, 0,
1701 IMS_PCU_CMD_RESPONSE_TIMEOUT);
1703 dev_err(pcu->dev, "Bootloader does not respond, aborting\n");
1707 pcu->fw_start_addr =
1708 get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 11]);
1710 get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 15]);
1713 "Device is in bootloader mode (addr 0x%08x-0x%08x), requesting firmware\n",
1714 pcu->fw_start_addr, pcu->fw_end_addr);
1716 error = request_firmware_nowait(THIS_MODULE, true,
1717 IMS_PCU_FIRMWARE_NAME,
1718 pcu->dev, GFP_KERNEL, pcu,
1719 ims_pcu_process_async_firmware);
1721 /* This error is not fatal, let userspace have another chance */
1722 complete(&pcu->async_firmware_done);
1728 static void ims_pcu_destroy_bootloader_mode(struct ims_pcu *pcu)
1730 /* Make sure our initial firmware request has completed */
1731 wait_for_completion(&pcu->async_firmware_done);
1734 #define IMS_PCU_APPLICATION_MODE 0
1735 #define IMS_PCU_BOOTLOADER_MODE 1
1737 static struct usb_driver ims_pcu_driver;
1739 static int ims_pcu_probe(struct usb_interface *intf,
1740 const struct usb_device_id *id)
1742 struct usb_device *udev = interface_to_usbdev(intf);
1743 struct ims_pcu *pcu;
1746 pcu = kzalloc(sizeof(struct ims_pcu), GFP_KERNEL);
1750 pcu->dev = &intf->dev;
1752 pcu->bootloader_mode = id->driver_info == IMS_PCU_BOOTLOADER_MODE;
1753 mutex_init(&pcu->cmd_mutex);
1754 init_completion(&pcu->cmd_done);
1755 init_completion(&pcu->async_firmware_done);
1757 error = ims_pcu_parse_cdc_data(intf, pcu);
1761 error = usb_driver_claim_interface(&ims_pcu_driver,
1762 pcu->data_intf, pcu);
1765 "Unable to claim corresponding data interface: %d\n",
1770 usb_set_intfdata(pcu->ctrl_intf, pcu);
1771 usb_set_intfdata(pcu->data_intf, pcu);
1773 error = ims_pcu_buffers_alloc(pcu);
1775 goto err_unclaim_intf;
1777 error = ims_pcu_start_io(pcu);
1779 goto err_free_buffers;
1781 error = ims_pcu_line_setup(pcu);
1785 error = sysfs_create_group(&intf->dev.kobj, &ims_pcu_attr_group);
1789 error = pcu->bootloader_mode ?
1790 ims_pcu_init_bootloader_mode(pcu) :
1791 ims_pcu_init_application_mode(pcu);
1793 goto err_remove_sysfs;
1798 sysfs_remove_group(&intf->dev.kobj, &ims_pcu_attr_group);
1800 ims_pcu_stop_io(pcu);
1802 ims_pcu_buffers_free(pcu);
1804 usb_driver_release_interface(&ims_pcu_driver, pcu->data_intf);
1810 static void ims_pcu_disconnect(struct usb_interface *intf)
1812 struct ims_pcu *pcu = usb_get_intfdata(intf);
1813 struct usb_host_interface *alt = intf->cur_altsetting;
1815 usb_set_intfdata(intf, NULL);
1818 * See if we are dealing with control or data interface. The cleanup
1819 * happens when we unbind primary (control) interface.
1821 if (alt->desc.bInterfaceClass != USB_CLASS_COMM)
1824 sysfs_remove_group(&intf->dev.kobj, &ims_pcu_attr_group);
1826 ims_pcu_stop_io(pcu);
1828 if (pcu->bootloader_mode)
1829 ims_pcu_destroy_bootloader_mode(pcu);
1831 ims_pcu_destroy_application_mode(pcu);
1833 ims_pcu_buffers_free(pcu);
1838 static int ims_pcu_suspend(struct usb_interface *intf,
1839 pm_message_t message)
1841 struct ims_pcu *pcu = usb_get_intfdata(intf);
1842 struct usb_host_interface *alt = intf->cur_altsetting;
1844 if (alt->desc.bInterfaceClass == USB_CLASS_COMM)
1845 ims_pcu_stop_io(pcu);
1850 static int ims_pcu_resume(struct usb_interface *intf)
1852 struct ims_pcu *pcu = usb_get_intfdata(intf);
1853 struct usb_host_interface *alt = intf->cur_altsetting;
1856 if (alt->desc.bInterfaceClass == USB_CLASS_COMM) {
1857 retval = ims_pcu_start_io(pcu);
1859 retval = ims_pcu_line_setup(pcu);
1866 static const struct usb_device_id ims_pcu_id_table[] = {
1868 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0082,
1870 USB_CDC_SUBCLASS_ACM,
1871 USB_CDC_ACM_PROTO_AT_V25TER),
1872 .driver_info = IMS_PCU_APPLICATION_MODE,
1875 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0083,
1877 USB_CDC_SUBCLASS_ACM,
1878 USB_CDC_ACM_PROTO_AT_V25TER),
1879 .driver_info = IMS_PCU_BOOTLOADER_MODE,
1884 static struct usb_driver ims_pcu_driver = {
1886 .id_table = ims_pcu_id_table,
1887 .probe = ims_pcu_probe,
1888 .disconnect = ims_pcu_disconnect,
1890 .suspend = ims_pcu_suspend,
1891 .resume = ims_pcu_resume,
1892 .reset_resume = ims_pcu_resume,
1896 module_usb_driver(ims_pcu_driver);
1898 MODULE_DESCRIPTION("IMS Passenger Control Unit driver");
1899 MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
1900 MODULE_LICENSE("GPL");