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");
299 gamepad
->input
= input
;
301 snprintf(gamepad
->name
, sizeof(gamepad
->name
),
302 "IMS PCU#%d Gamepad Interface", pcu
->device_no
);
304 usb_make_path(pcu
->udev
, gamepad
->phys
, sizeof(gamepad
->phys
));
305 strlcat(gamepad
->phys
, "/input1", sizeof(gamepad
->phys
));
307 input
->name
= gamepad
->name
;
308 input
->phys
= gamepad
->phys
;
309 usb_to_input_id(pcu
->udev
, &input
->id
);
310 input
->dev
.parent
= &pcu
->ctrl_intf
->dev
;
312 __set_bit(EV_KEY
, input
->evbit
);
313 __set_bit(BTN_A
, input
->keybit
);
314 __set_bit(BTN_B
, input
->keybit
);
315 __set_bit(BTN_X
, input
->keybit
);
316 __set_bit(BTN_Y
, input
->keybit
);
317 __set_bit(BTN_START
, input
->keybit
);
318 __set_bit(BTN_SELECT
, input
->keybit
);
320 __set_bit(EV_ABS
, input
->evbit
);
321 input_set_abs_params(input
, ABS_X
, -1, 1, 0, 0);
322 input_set_abs_params(input
, ABS_Y
, -1, 1, 0, 0);
324 error
= input_register_device(input
);
327 "Failed to register gamepad input device: %d\n",
332 pcu
->gamepad
= gamepad
;
336 input_free_device(input
);
341 static void ims_pcu_destroy_gamepad(struct ims_pcu
*pcu
)
343 struct ims_pcu_gamepad
*gamepad
= pcu
->gamepad
;
345 input_unregister_device(gamepad
->input
);
350 /*********************************************************************
351 * PCU Communication protocol handling *
352 *********************************************************************/
354 #define IMS_PCU_PROTOCOL_STX 0x02
355 #define IMS_PCU_PROTOCOL_ETX 0x03
356 #define IMS_PCU_PROTOCOL_DLE 0x10
359 #define IMS_PCU_CMD_STATUS 0xa0
360 #define IMS_PCU_CMD_PCU_RESET 0xa1
361 #define IMS_PCU_CMD_RESET_REASON 0xa2
362 #define IMS_PCU_CMD_SEND_BUTTONS 0xa3
363 #define IMS_PCU_CMD_JUMP_TO_BTLDR 0xa4
364 #define IMS_PCU_CMD_GET_INFO 0xa5
365 #define IMS_PCU_CMD_SET_BRIGHTNESS 0xa6
366 #define IMS_PCU_CMD_EEPROM 0xa7
367 #define IMS_PCU_CMD_GET_FW_VERSION 0xa8
368 #define IMS_PCU_CMD_GET_BL_VERSION 0xa9
369 #define IMS_PCU_CMD_SET_INFO 0xab
370 #define IMS_PCU_CMD_GET_BRIGHTNESS 0xac
371 #define IMS_PCU_CMD_GET_DEVICE_ID 0xae
372 #define IMS_PCU_CMD_SPECIAL_INFO 0xb0
373 #define IMS_PCU_CMD_BOOTLOADER 0xb1 /* Pass data to bootloader */
376 #define IMS_PCU_RSP_STATUS 0xc0
377 #define IMS_PCU_RSP_PCU_RESET 0 /* Originally 0xc1 */
378 #define IMS_PCU_RSP_RESET_REASON 0xc2
379 #define IMS_PCU_RSP_SEND_BUTTONS 0xc3
380 #define IMS_PCU_RSP_JUMP_TO_BTLDR 0 /* Originally 0xc4 */
381 #define IMS_PCU_RSP_GET_INFO 0xc5
382 #define IMS_PCU_RSP_SET_BRIGHTNESS 0xc6
383 #define IMS_PCU_RSP_EEPROM 0xc7
384 #define IMS_PCU_RSP_GET_FW_VERSION 0xc8
385 #define IMS_PCU_RSP_GET_BL_VERSION 0xc9
386 #define IMS_PCU_RSP_SET_INFO 0xcb
387 #define IMS_PCU_RSP_GET_BRIGHTNESS 0xcc
388 #define IMS_PCU_RSP_CMD_INVALID 0xcd
389 #define IMS_PCU_RSP_GET_DEVICE_ID 0xce
390 #define IMS_PCU_RSP_SPECIAL_INFO 0xd0
391 #define IMS_PCU_RSP_BOOTLOADER 0xd1 /* Bootloader response */
393 #define IMS_PCU_RSP_EVNT_BUTTONS 0xe0 /* Unsolicited, button state */
394 #define IMS_PCU_GAMEPAD_MASK 0x0001ff80UL /* Bits 7 through 16 */
397 #define IMS_PCU_MIN_PACKET_LEN 3
398 #define IMS_PCU_DATA_OFFSET 2
400 #define IMS_PCU_CMD_WRITE_TIMEOUT 100 /* msec */
401 #define IMS_PCU_CMD_RESPONSE_TIMEOUT 500 /* msec */
403 static void ims_pcu_report_events(struct ims_pcu
*pcu
)
405 u32 data
= get_unaligned_be32(&pcu
->read_buf
[3]);
407 ims_pcu_buttons_report(pcu
, data
& ~IMS_PCU_GAMEPAD_MASK
);
409 ims_pcu_gamepad_report(pcu
, data
);
412 static void ims_pcu_handle_response(struct ims_pcu
*pcu
)
414 switch (pcu
->read_buf
[0]) {
415 case IMS_PCU_RSP_EVNT_BUTTONS
:
416 if (likely(pcu
->setup_complete
))
417 ims_pcu_report_events(pcu
);
422 * See if we got command completion.
423 * If both the sequence and response code match save
424 * the data and signal completion.
426 if (pcu
->read_buf
[0] == pcu
->expected_response
&&
427 pcu
->read_buf
[1] == pcu
->ack_id
- 1) {
429 memcpy(pcu
->cmd_buf
, pcu
->read_buf
, pcu
->read_pos
);
430 pcu
->cmd_buf_len
= pcu
->read_pos
;
431 complete(&pcu
->cmd_done
);
437 static void ims_pcu_process_data(struct ims_pcu
*pcu
, struct urb
*urb
)
441 for (i
= 0; i
< urb
->actual_length
; i
++) {
442 u8 data
= pcu
->urb_in_buf
[i
];
444 /* Skip everything until we get Start Xmit */
445 if (!pcu
->have_stx
&& data
!= IMS_PCU_PROTOCOL_STX
)
449 pcu
->have_dle
= false;
450 pcu
->read_buf
[pcu
->read_pos
++] = data
;
451 pcu
->check_sum
+= data
;
456 case IMS_PCU_PROTOCOL_STX
:
459 "Unexpected STX at byte %d, discarding old data\n",
461 pcu
->have_stx
= true;
462 pcu
->have_dle
= false;
467 case IMS_PCU_PROTOCOL_DLE
:
468 pcu
->have_dle
= true;
471 case IMS_PCU_PROTOCOL_ETX
:
472 if (pcu
->read_pos
< IMS_PCU_MIN_PACKET_LEN
) {
474 "Short packet received (%d bytes), ignoring\n",
476 } else if (pcu
->check_sum
!= 0) {
478 "Invalid checksum in packet (%d bytes), ignoring\n",
481 ims_pcu_handle_response(pcu
);
484 pcu
->have_stx
= false;
485 pcu
->have_dle
= false;
490 pcu
->read_buf
[pcu
->read_pos
++] = data
;
491 pcu
->check_sum
+= data
;
497 static bool ims_pcu_byte_needs_escape(u8 byte
)
499 return byte
== IMS_PCU_PROTOCOL_STX
||
500 byte
== IMS_PCU_PROTOCOL_ETX
||
501 byte
== IMS_PCU_PROTOCOL_DLE
;
504 static int ims_pcu_send_cmd_chunk(struct ims_pcu
*pcu
,
505 u8 command
, int chunk
, int len
)
509 error
= usb_bulk_msg(pcu
->udev
,
510 usb_sndbulkpipe(pcu
->udev
,
511 pcu
->ep_out
->bEndpointAddress
),
512 pcu
->urb_out_buf
, len
,
513 NULL
, IMS_PCU_CMD_WRITE_TIMEOUT
);
516 "Sending 0x%02x command failed at chunk %d: %d\n",
517 command
, chunk
, error
);
524 static int ims_pcu_send_command(struct ims_pcu
*pcu
,
525 u8 command
, const u8
*data
, int len
)
535 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_STX
;
537 /* We know the command need not be escaped */
538 pcu
->urb_out_buf
[count
++] = command
;
541 ack_id
= pcu
->ack_id
++;
543 ack_id
= pcu
->ack_id
++;
545 if (ims_pcu_byte_needs_escape(ack_id
))
546 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_DLE
;
548 pcu
->urb_out_buf
[count
++] = ack_id
;
551 for (i
= 0; i
< len
; i
++) {
553 delta
= ims_pcu_byte_needs_escape(data
[i
]) ? 2 : 1;
554 if (count
+ delta
>= pcu
->max_out_size
) {
555 error
= ims_pcu_send_cmd_chunk(pcu
, command
,
564 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_DLE
;
566 pcu
->urb_out_buf
[count
++] = data
[i
];
572 delta
= ims_pcu_byte_needs_escape(csum
) ? 3 : 2;
573 if (count
+ delta
>= pcu
->max_out_size
) {
574 error
= ims_pcu_send_cmd_chunk(pcu
, command
, ++chunk
, count
);
582 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_DLE
;
584 pcu
->urb_out_buf
[count
++] = csum
;
585 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_ETX
;
587 return ims_pcu_send_cmd_chunk(pcu
, command
, ++chunk
, count
);
590 static int __ims_pcu_execute_command(struct ims_pcu
*pcu
,
591 u8 command
, const void *data
, size_t len
,
592 u8 expected_response
, int response_time
)
596 pcu
->expected_response
= expected_response
;
597 init_completion(&pcu
->cmd_done
);
599 error
= ims_pcu_send_command(pcu
, command
, data
, len
);
603 if (expected_response
&&
604 !wait_for_completion_timeout(&pcu
->cmd_done
,
605 msecs_to_jiffies(response_time
))) {
606 dev_dbg(pcu
->dev
, "Command 0x%02x timed out\n", command
);
613 #define ims_pcu_execute_command(pcu, code, data, len) \
614 __ims_pcu_execute_command(pcu, \
615 IMS_PCU_CMD_##code, data, len, \
616 IMS_PCU_RSP_##code, \
617 IMS_PCU_CMD_RESPONSE_TIMEOUT)
619 #define ims_pcu_execute_query(pcu, code) \
620 ims_pcu_execute_command(pcu, code, NULL, 0)
622 /* Bootloader commands */
623 #define IMS_PCU_BL_CMD_QUERY_DEVICE 0xa1
624 #define IMS_PCU_BL_CMD_UNLOCK_CONFIG 0xa2
625 #define IMS_PCU_BL_CMD_ERASE_APP 0xa3
626 #define IMS_PCU_BL_CMD_PROGRAM_DEVICE 0xa4
627 #define IMS_PCU_BL_CMD_PROGRAM_COMPLETE 0xa5
628 #define IMS_PCU_BL_CMD_READ_APP 0xa6
629 #define IMS_PCU_BL_CMD_RESET_DEVICE 0xa7
630 #define IMS_PCU_BL_CMD_LAUNCH_APP 0xa8
632 /* Bootloader commands */
633 #define IMS_PCU_BL_RSP_QUERY_DEVICE 0xc1
634 #define IMS_PCU_BL_RSP_UNLOCK_CONFIG 0xc2
635 #define IMS_PCU_BL_RSP_ERASE_APP 0xc3
636 #define IMS_PCU_BL_RSP_PROGRAM_DEVICE 0xc4
637 #define IMS_PCU_BL_RSP_PROGRAM_COMPLETE 0xc5
638 #define IMS_PCU_BL_RSP_READ_APP 0xc6
639 #define IMS_PCU_BL_RSP_RESET_DEVICE 0 /* originally 0xa7 */
640 #define IMS_PCU_BL_RSP_LAUNCH_APP 0 /* originally 0xa8 */
642 #define IMS_PCU_BL_DATA_OFFSET 3
644 static int __ims_pcu_execute_bl_command(struct ims_pcu
*pcu
,
645 u8 command
, const void *data
, size_t len
,
646 u8 expected_response
, int response_time
)
650 pcu
->cmd_buf
[0] = command
;
652 memcpy(&pcu
->cmd_buf
[1], data
, len
);
654 error
= __ims_pcu_execute_command(pcu
,
655 IMS_PCU_CMD_BOOTLOADER
, pcu
->cmd_buf
, len
+ 1,
656 expected_response
? IMS_PCU_RSP_BOOTLOADER
: 0,
660 "Failure when sending 0x%02x command to bootloader, error: %d\n",
661 pcu
->cmd_buf
[0], error
);
665 if (expected_response
&& pcu
->cmd_buf
[2] != expected_response
) {
667 "Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n",
668 pcu
->cmd_buf
[2], expected_response
);
675 #define ims_pcu_execute_bl_command(pcu, code, data, len, timeout) \
676 __ims_pcu_execute_bl_command(pcu, \
677 IMS_PCU_BL_CMD_##code, data, len, \
678 IMS_PCU_BL_RSP_##code, timeout) \
680 #define IMS_PCU_INFO_PART_OFFSET 2
681 #define IMS_PCU_INFO_DOM_OFFSET 17
682 #define IMS_PCU_INFO_SERIAL_OFFSET 25
684 #define IMS_PCU_SET_INFO_SIZE 31
686 static int ims_pcu_get_info(struct ims_pcu
*pcu
)
690 error
= ims_pcu_execute_query(pcu
, GET_INFO
);
693 "GET_INFO command failed, error: %d\n", error
);
697 memcpy(pcu
->part_number
,
698 &pcu
->cmd_buf
[IMS_PCU_INFO_PART_OFFSET
],
699 sizeof(pcu
->part_number
));
700 memcpy(pcu
->date_of_manufacturing
,
701 &pcu
->cmd_buf
[IMS_PCU_INFO_DOM_OFFSET
],
702 sizeof(pcu
->date_of_manufacturing
));
703 memcpy(pcu
->serial_number
,
704 &pcu
->cmd_buf
[IMS_PCU_INFO_SERIAL_OFFSET
],
705 sizeof(pcu
->serial_number
));
710 static int ims_pcu_set_info(struct ims_pcu
*pcu
)
714 memcpy(&pcu
->cmd_buf
[IMS_PCU_INFO_PART_OFFSET
],
715 pcu
->part_number
, sizeof(pcu
->part_number
));
716 memcpy(&pcu
->cmd_buf
[IMS_PCU_INFO_DOM_OFFSET
],
717 pcu
->date_of_manufacturing
, sizeof(pcu
->date_of_manufacturing
));
718 memcpy(&pcu
->cmd_buf
[IMS_PCU_INFO_SERIAL_OFFSET
],
719 pcu
->serial_number
, sizeof(pcu
->serial_number
));
721 error
= ims_pcu_execute_command(pcu
, SET_INFO
,
722 &pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
],
723 IMS_PCU_SET_INFO_SIZE
);
726 "Failed to update device information, error: %d\n",
734 static int ims_pcu_switch_to_bootloader(struct ims_pcu
*pcu
)
738 /* Execute jump to the bootoloader */
739 error
= ims_pcu_execute_command(pcu
, JUMP_TO_BTLDR
, NULL
, 0);
742 "Failure when sending JUMP TO BOOLTLOADER command, error: %d\n",
750 /*********************************************************************
751 * Firmware Update handling *
752 *********************************************************************/
754 #define IMS_PCU_FIRMWARE_NAME "imspcu.fw"
756 struct ims_pcu_flash_fmt
{
762 static unsigned int ims_pcu_count_fw_records(const struct firmware
*fw
)
764 const struct ihex_binrec
*rec
= (const struct ihex_binrec
*)fw
->data
;
765 unsigned int count
= 0;
769 rec
= ihex_next_binrec(rec
);
775 static int ims_pcu_verify_block(struct ims_pcu
*pcu
,
776 u32 addr
, u8 len
, const u8
*data
)
778 struct ims_pcu_flash_fmt
*fragment
;
781 fragment
= (void *)&pcu
->cmd_buf
[1];
782 put_unaligned_le32(addr
, &fragment
->addr
);
785 error
= ims_pcu_execute_bl_command(pcu
, READ_APP
, NULL
, 5,
786 IMS_PCU_CMD_RESPONSE_TIMEOUT
);
789 "Failed to retrieve block at 0x%08x, len %d, error: %d\n",
794 fragment
= (void *)&pcu
->cmd_buf
[IMS_PCU_BL_DATA_OFFSET
];
795 if (get_unaligned_le32(&fragment
->addr
) != addr
||
796 fragment
->len
!= len
) {
798 "Wrong block when retrieving 0x%08x (0x%08x), len %d (%d)\n",
799 addr
, get_unaligned_le32(&fragment
->addr
),
804 if (memcmp(fragment
->data
, data
, len
)) {
806 "Mismatch in block at 0x%08x, len %d\n",
814 static int ims_pcu_flash_firmware(struct ims_pcu
*pcu
,
815 const struct firmware
*fw
,
816 unsigned int n_fw_records
)
818 const struct ihex_binrec
*rec
= (const struct ihex_binrec
*)fw
->data
;
819 struct ims_pcu_flash_fmt
*fragment
;
820 unsigned int count
= 0;
825 error
= ims_pcu_execute_bl_command(pcu
, ERASE_APP
, NULL
, 0, 2000);
828 "Failed to erase application image, error: %d\n",
835 * The firmware format is messed up for some reason.
836 * The address twice that of what is needed for some
837 * reason and we end up overwriting half of the data
838 * with the next record.
840 addr
= be32_to_cpu(rec
->addr
) / 2;
841 len
= be16_to_cpu(rec
->len
);
843 fragment
= (void *)&pcu
->cmd_buf
[1];
844 put_unaligned_le32(addr
, &fragment
->addr
);
846 memcpy(fragment
->data
, rec
->data
, len
);
848 error
= ims_pcu_execute_bl_command(pcu
, PROGRAM_DEVICE
,
850 IMS_PCU_CMD_RESPONSE_TIMEOUT
);
853 "Failed to write block at 0x%08x, len %d, error: %d\n",
858 if (addr
>= pcu
->fw_start_addr
&& addr
< pcu
->fw_end_addr
) {
859 error
= ims_pcu_verify_block(pcu
, addr
, len
, rec
->data
);
865 pcu
->update_firmware_status
= (count
* 100) / n_fw_records
;
867 rec
= ihex_next_binrec(rec
);
870 error
= ims_pcu_execute_bl_command(pcu
, PROGRAM_COMPLETE
,
874 "Failed to send PROGRAM_COMPLETE, error: %d\n",
880 static int ims_pcu_handle_firmware_update(struct ims_pcu
*pcu
,
881 const struct firmware
*fw
)
883 unsigned int n_fw_records
;
886 dev_info(pcu
->dev
, "Updating firmware %s, size: %zu\n",
887 IMS_PCU_FIRMWARE_NAME
, fw
->size
);
889 n_fw_records
= ims_pcu_count_fw_records(fw
);
891 retval
= ims_pcu_flash_firmware(pcu
, fw
, n_fw_records
);
895 retval
= ims_pcu_execute_bl_command(pcu
, LAUNCH_APP
, NULL
, 0, 0);
898 "Failed to start application image, error: %d\n",
902 pcu
->update_firmware_status
= retval
;
903 sysfs_notify(&pcu
->dev
->kobj
, NULL
, "update_firmware_status");
907 static void ims_pcu_process_async_firmware(const struct firmware
*fw
,
910 struct ims_pcu
*pcu
= context
;
914 dev_err(pcu
->dev
, "Failed to get firmware %s\n",
915 IMS_PCU_FIRMWARE_NAME
);
919 error
= ihex_validate_fw(fw
);
921 dev_err(pcu
->dev
, "Firmware %s is invalid\n",
922 IMS_PCU_FIRMWARE_NAME
);
926 mutex_lock(&pcu
->cmd_mutex
);
927 ims_pcu_handle_firmware_update(pcu
, fw
);
928 mutex_unlock(&pcu
->cmd_mutex
);
930 release_firmware(fw
);
933 complete(&pcu
->async_firmware_done
);
936 /*********************************************************************
937 * Backlight LED device support *
938 *********************************************************************/
940 #define IMS_PCU_MAX_BRIGHTNESS 31998
942 static void ims_pcu_backlight_work(struct work_struct
*work
)
944 struct ims_pcu_backlight
*backlight
=
945 container_of(work
, struct ims_pcu_backlight
, work
);
946 struct ims_pcu
*pcu
=
947 container_of(backlight
, struct ims_pcu
, backlight
);
948 int desired_brightness
= backlight
->desired_brightness
;
949 __le16 br_val
= cpu_to_le16(desired_brightness
);
952 mutex_lock(&pcu
->cmd_mutex
);
954 error
= ims_pcu_execute_command(pcu
, SET_BRIGHTNESS
,
955 &br_val
, sizeof(br_val
));
956 if (error
&& error
!= -ENODEV
)
958 "Failed to set desired brightness %u, error: %d\n",
959 desired_brightness
, error
);
961 mutex_unlock(&pcu
->cmd_mutex
);
964 static void ims_pcu_backlight_set_brightness(struct led_classdev
*cdev
,
965 enum led_brightness value
)
967 struct ims_pcu_backlight
*backlight
=
968 container_of(cdev
, struct ims_pcu_backlight
, cdev
);
970 backlight
->desired_brightness
= value
;
971 schedule_work(&backlight
->work
);
974 static enum led_brightness
975 ims_pcu_backlight_get_brightness(struct led_classdev
*cdev
)
977 struct ims_pcu_backlight
*backlight
=
978 container_of(cdev
, struct ims_pcu_backlight
, cdev
);
979 struct ims_pcu
*pcu
=
980 container_of(backlight
, struct ims_pcu
, backlight
);
984 mutex_lock(&pcu
->cmd_mutex
);
986 error
= ims_pcu_execute_query(pcu
, GET_BRIGHTNESS
);
989 "Failed to get current brightness, error: %d\n",
991 /* Assume the LED is OFF */
992 brightness
= LED_OFF
;
995 get_unaligned_le16(&pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
]);
998 mutex_unlock(&pcu
->cmd_mutex
);
1003 static int ims_pcu_setup_backlight(struct ims_pcu
*pcu
)
1005 struct ims_pcu_backlight
*backlight
= &pcu
->backlight
;
1008 INIT_WORK(&backlight
->work
, ims_pcu_backlight_work
);
1009 snprintf(backlight
->name
, sizeof(backlight
->name
),
1010 "pcu%d::kbd_backlight", pcu
->device_no
);
1012 backlight
->cdev
.name
= backlight
->name
;
1013 backlight
->cdev
.max_brightness
= IMS_PCU_MAX_BRIGHTNESS
;
1014 backlight
->cdev
.brightness_get
= ims_pcu_backlight_get_brightness
;
1015 backlight
->cdev
.brightness_set
= ims_pcu_backlight_set_brightness
;
1017 error
= led_classdev_register(pcu
->dev
, &backlight
->cdev
);
1020 "Failed to register backlight LED device, error: %d\n",
1028 static void ims_pcu_destroy_backlight(struct ims_pcu
*pcu
)
1030 struct ims_pcu_backlight
*backlight
= &pcu
->backlight
;
1032 led_classdev_unregister(&backlight
->cdev
);
1033 cancel_work_sync(&backlight
->work
);
1037 /*********************************************************************
1038 * Sysfs attributes handling *
1039 *********************************************************************/
1041 struct ims_pcu_attribute
{
1042 struct device_attribute dattr
;
1043 size_t field_offset
;
1047 static ssize_t
ims_pcu_attribute_show(struct device
*dev
,
1048 struct device_attribute
*dattr
,
1051 struct usb_interface
*intf
= to_usb_interface(dev
);
1052 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1053 struct ims_pcu_attribute
*attr
=
1054 container_of(dattr
, struct ims_pcu_attribute
, dattr
);
1055 char *field
= (char *)pcu
+ attr
->field_offset
;
1057 return scnprintf(buf
, PAGE_SIZE
, "%.*s\n", attr
->field_length
, field
);
1060 static ssize_t
ims_pcu_attribute_store(struct device
*dev
,
1061 struct device_attribute
*dattr
,
1062 const char *buf
, size_t count
)
1065 struct usb_interface
*intf
= to_usb_interface(dev
);
1066 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1067 struct ims_pcu_attribute
*attr
=
1068 container_of(dattr
, struct ims_pcu_attribute
, dattr
);
1069 char *field
= (char *)pcu
+ attr
->field_offset
;
1073 if (count
> attr
->field_length
)
1076 data_len
= strnlen(buf
, attr
->field_length
);
1077 if (data_len
> attr
->field_length
)
1080 error
= mutex_lock_interruptible(&pcu
->cmd_mutex
);
1084 memset(field
, 0, attr
->field_length
);
1085 memcpy(field
, buf
, data_len
);
1087 error
= ims_pcu_set_info(pcu
);
1090 * Even if update failed, let's fetch the info again as we just
1091 * clobbered one of the fields.
1093 ims_pcu_get_info(pcu
);
1095 mutex_unlock(&pcu
->cmd_mutex
);
1097 return error
< 0 ? error
: count
;
1100 #define IMS_PCU_ATTR(_field, _mode) \
1101 struct ims_pcu_attribute ims_pcu_attr_##_field = { \
1102 .dattr = __ATTR(_field, _mode, \
1103 ims_pcu_attribute_show, \
1104 ims_pcu_attribute_store), \
1105 .field_offset = offsetof(struct ims_pcu, _field), \
1106 .field_length = sizeof(((struct ims_pcu *)NULL)->_field), \
1109 #define IMS_PCU_RO_ATTR(_field) \
1110 IMS_PCU_ATTR(_field, S_IRUGO)
1111 #define IMS_PCU_RW_ATTR(_field) \
1112 IMS_PCU_ATTR(_field, S_IRUGO | S_IWUSR)
1114 static IMS_PCU_RW_ATTR(part_number
);
1115 static IMS_PCU_RW_ATTR(serial_number
);
1116 static IMS_PCU_RW_ATTR(date_of_manufacturing
);
1118 static IMS_PCU_RO_ATTR(fw_version
);
1119 static IMS_PCU_RO_ATTR(bl_version
);
1120 static IMS_PCU_RO_ATTR(reset_reason
);
1122 static ssize_t
ims_pcu_reset_device(struct device
*dev
,
1123 struct device_attribute
*dattr
,
1124 const char *buf
, size_t count
)
1126 static const u8 reset_byte
= 1;
1127 struct usb_interface
*intf
= to_usb_interface(dev
);
1128 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1132 error
= kstrtoint(buf
, 0, &value
);
1139 dev_info(pcu
->dev
, "Attempting to reset device\n");
1141 error
= ims_pcu_execute_command(pcu
, PCU_RESET
, &reset_byte
, 1);
1144 "Failed to reset device, error: %d\n",
1152 static DEVICE_ATTR(reset_device
, S_IWUSR
, NULL
, ims_pcu_reset_device
);
1154 static ssize_t
ims_pcu_update_firmware_store(struct device
*dev
,
1155 struct device_attribute
*dattr
,
1156 const char *buf
, size_t count
)
1158 struct usb_interface
*intf
= to_usb_interface(dev
);
1159 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1160 const struct firmware
*fw
= NULL
;
1164 error
= kstrtoint(buf
, 0, &value
);
1171 error
= mutex_lock_interruptible(&pcu
->cmd_mutex
);
1175 error
= request_ihex_firmware(&fw
, IMS_PCU_FIRMWARE_NAME
, pcu
->dev
);
1177 dev_err(pcu
->dev
, "Failed to request firmware %s, error: %d\n",
1178 IMS_PCU_FIRMWARE_NAME
, error
);
1183 * If we are already in bootloader mode we can proceed with
1184 * flashing the firmware.
1186 * If we are in application mode, then we need to switch into
1187 * bootloader mode, which will cause the device to disconnect
1188 * and reconnect as different device.
1190 if (pcu
->bootloader_mode
)
1191 error
= ims_pcu_handle_firmware_update(pcu
, fw
);
1193 error
= ims_pcu_switch_to_bootloader(pcu
);
1195 release_firmware(fw
);
1198 mutex_unlock(&pcu
->cmd_mutex
);
1199 return error
?: count
;
1202 static DEVICE_ATTR(update_firmware
, S_IWUSR
,
1203 NULL
, ims_pcu_update_firmware_store
);
1206 ims_pcu_update_firmware_status_show(struct device
*dev
,
1207 struct device_attribute
*dattr
,
1210 struct usb_interface
*intf
= to_usb_interface(dev
);
1211 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1213 return scnprintf(buf
, PAGE_SIZE
, "%d\n", pcu
->update_firmware_status
);
1216 static DEVICE_ATTR(update_firmware_status
, S_IRUGO
,
1217 ims_pcu_update_firmware_status_show
, NULL
);
1219 static struct attribute
*ims_pcu_attrs
[] = {
1220 &ims_pcu_attr_part_number
.dattr
.attr
,
1221 &ims_pcu_attr_serial_number
.dattr
.attr
,
1222 &ims_pcu_attr_date_of_manufacturing
.dattr
.attr
,
1223 &ims_pcu_attr_fw_version
.dattr
.attr
,
1224 &ims_pcu_attr_bl_version
.dattr
.attr
,
1225 &ims_pcu_attr_reset_reason
.dattr
.attr
,
1226 &dev_attr_reset_device
.attr
,
1227 &dev_attr_update_firmware
.attr
,
1228 &dev_attr_update_firmware_status
.attr
,
1232 static umode_t
ims_pcu_is_attr_visible(struct kobject
*kobj
,
1233 struct attribute
*attr
, int n
)
1235 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1236 struct usb_interface
*intf
= to_usb_interface(dev
);
1237 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1238 umode_t mode
= attr
->mode
;
1240 if (pcu
->bootloader_mode
) {
1241 if (attr
!= &dev_attr_update_firmware_status
.attr
&&
1242 attr
!= &dev_attr_update_firmware
.attr
&&
1243 attr
!= &dev_attr_reset_device
.attr
) {
1247 if (attr
== &dev_attr_update_firmware_status
.attr
)
1254 static struct attribute_group ims_pcu_attr_group
= {
1255 .is_visible
= ims_pcu_is_attr_visible
,
1256 .attrs
= ims_pcu_attrs
,
1259 static void ims_pcu_irq(struct urb
*urb
)
1261 struct ims_pcu
*pcu
= urb
->context
;
1264 status
= urb
->status
;
1273 /* this urb is terminated, clean up */
1274 dev_dbg(pcu
->dev
, "%s - urb shutting down with status: %d\n",
1278 dev_dbg(pcu
->dev
, "%s - nonzero urb status received: %d\n",
1283 dev_dbg(pcu
->dev
, "%s: received %d: %*ph\n", __func__
,
1284 urb
->actual_length
, urb
->actual_length
, pcu
->urb_in_buf
);
1286 if (urb
== pcu
->urb_in
)
1287 ims_pcu_process_data(pcu
, urb
);
1290 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1291 if (retval
&& retval
!= -ENODEV
)
1292 dev_err(pcu
->dev
, "%s - usb_submit_urb failed with result %d\n",
1296 static int ims_pcu_buffers_alloc(struct ims_pcu
*pcu
)
1300 pcu
->urb_in_buf
= usb_alloc_coherent(pcu
->udev
, pcu
->max_in_size
,
1301 GFP_KERNEL
, &pcu
->read_dma
);
1302 if (!pcu
->urb_in_buf
) {
1304 "Failed to allocate memory for read buffer\n");
1308 pcu
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
1310 dev_err(pcu
->dev
, "Failed to allocate input URB\n");
1312 goto err_free_urb_in_buf
;
1315 pcu
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1316 pcu
->urb_in
->transfer_dma
= pcu
->read_dma
;
1318 usb_fill_bulk_urb(pcu
->urb_in
, pcu
->udev
,
1319 usb_rcvbulkpipe(pcu
->udev
,
1320 pcu
->ep_in
->bEndpointAddress
),
1321 pcu
->urb_in_buf
, pcu
->max_in_size
,
1325 * We are using usb_bulk_msg() for sending so there is no point
1326 * in allocating memory with usb_alloc_coherent().
1328 pcu
->urb_out_buf
= kmalloc(pcu
->max_out_size
, GFP_KERNEL
);
1329 if (!pcu
->urb_out_buf
) {
1330 dev_err(pcu
->dev
, "Failed to allocate memory for write buffer\n");
1332 goto err_free_in_urb
;
1335 pcu
->urb_ctrl_buf
= usb_alloc_coherent(pcu
->udev
, pcu
->max_ctrl_size
,
1336 GFP_KERNEL
, &pcu
->ctrl_dma
);
1337 if (!pcu
->urb_ctrl_buf
) {
1339 "Failed to allocate memory for read buffer\n");
1340 goto err_free_urb_out_buf
;
1343 pcu
->urb_ctrl
= usb_alloc_urb(0, GFP_KERNEL
);
1344 if (!pcu
->urb_ctrl
) {
1345 dev_err(pcu
->dev
, "Failed to allocate input URB\n");
1347 goto err_free_urb_ctrl_buf
;
1350 pcu
->urb_ctrl
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1351 pcu
->urb_ctrl
->transfer_dma
= pcu
->ctrl_dma
;
1353 usb_fill_int_urb(pcu
->urb_ctrl
, pcu
->udev
,
1354 usb_rcvintpipe(pcu
->udev
,
1355 pcu
->ep_ctrl
->bEndpointAddress
),
1356 pcu
->urb_ctrl_buf
, pcu
->max_ctrl_size
,
1357 ims_pcu_irq
, pcu
, pcu
->ep_ctrl
->bInterval
);
1361 err_free_urb_ctrl_buf
:
1362 usb_free_coherent(pcu
->udev
, pcu
->max_ctrl_size
,
1363 pcu
->urb_ctrl_buf
, pcu
->ctrl_dma
);
1364 err_free_urb_out_buf
:
1365 kfree(pcu
->urb_out_buf
);
1367 usb_free_urb(pcu
->urb_in
);
1368 err_free_urb_in_buf
:
1369 usb_free_coherent(pcu
->udev
, pcu
->max_in_size
,
1370 pcu
->urb_in_buf
, pcu
->read_dma
);
1374 static void ims_pcu_buffers_free(struct ims_pcu
*pcu
)
1376 usb_kill_urb(pcu
->urb_in
);
1377 usb_free_urb(pcu
->urb_in
);
1379 usb_free_coherent(pcu
->udev
, pcu
->max_out_size
,
1380 pcu
->urb_in_buf
, pcu
->read_dma
);
1382 kfree(pcu
->urb_out_buf
);
1384 usb_kill_urb(pcu
->urb_ctrl
);
1385 usb_free_urb(pcu
->urb_ctrl
);
1387 usb_free_coherent(pcu
->udev
, pcu
->max_ctrl_size
,
1388 pcu
->urb_ctrl_buf
, pcu
->ctrl_dma
);
1391 static const struct usb_cdc_union_desc
*
1392 ims_pcu_get_cdc_union_desc(struct usb_interface
*intf
)
1394 const void *buf
= intf
->altsetting
->extra
;
1395 size_t buflen
= intf
->altsetting
->extralen
;
1396 struct usb_cdc_union_desc
*union_desc
;
1399 dev_err(&intf
->dev
, "Missing descriptor data\n");
1404 dev_err(&intf
->dev
, "Zero length descriptor\n");
1408 while (buflen
> 0) {
1409 union_desc
= (struct usb_cdc_union_desc
*)buf
;
1411 if (union_desc
->bDescriptorType
== USB_DT_CS_INTERFACE
&&
1412 union_desc
->bDescriptorSubType
== USB_CDC_UNION_TYPE
) {
1413 dev_dbg(&intf
->dev
, "Found union header\n");
1417 buflen
-= union_desc
->bLength
;
1418 buf
+= union_desc
->bLength
;
1421 dev_err(&intf
->dev
, "Missing CDC union descriptor\n");
1425 static int ims_pcu_parse_cdc_data(struct usb_interface
*intf
, struct ims_pcu
*pcu
)
1427 const struct usb_cdc_union_desc
*union_desc
;
1428 struct usb_host_interface
*alt
;
1430 union_desc
= ims_pcu_get_cdc_union_desc(intf
);
1434 pcu
->ctrl_intf
= usb_ifnum_to_if(pcu
->udev
,
1435 union_desc
->bMasterInterface0
);
1437 alt
= pcu
->ctrl_intf
->cur_altsetting
;
1438 pcu
->ep_ctrl
= &alt
->endpoint
[0].desc
;
1439 pcu
->max_ctrl_size
= usb_endpoint_maxp(pcu
->ep_ctrl
);
1441 pcu
->data_intf
= usb_ifnum_to_if(pcu
->udev
,
1442 union_desc
->bSlaveInterface0
);
1444 alt
= pcu
->data_intf
->cur_altsetting
;
1445 if (alt
->desc
.bNumEndpoints
!= 2) {
1447 "Incorrect number of endpoints on data interface (%d)\n",
1448 alt
->desc
.bNumEndpoints
);
1452 pcu
->ep_out
= &alt
->endpoint
[0].desc
;
1453 if (!usb_endpoint_is_bulk_out(pcu
->ep_out
)) {
1455 "First endpoint on data interface is not BULK OUT\n");
1459 pcu
->max_out_size
= usb_endpoint_maxp(pcu
->ep_out
);
1460 if (pcu
->max_out_size
< 8) {
1462 "Max OUT packet size is too small (%zd)\n",
1467 pcu
->ep_in
= &alt
->endpoint
[1].desc
;
1468 if (!usb_endpoint_is_bulk_in(pcu
->ep_in
)) {
1470 "Second endpoint on data interface is not BULK IN\n");
1474 pcu
->max_in_size
= usb_endpoint_maxp(pcu
->ep_in
);
1475 if (pcu
->max_in_size
< 8) {
1477 "Max IN packet size is too small (%zd)\n",
1485 static int ims_pcu_start_io(struct ims_pcu
*pcu
)
1489 error
= usb_submit_urb(pcu
->urb_ctrl
, GFP_KERNEL
);
1492 "Failed to start control IO - usb_submit_urb failed with result: %d\n",
1497 error
= usb_submit_urb(pcu
->urb_in
, GFP_KERNEL
);
1500 "Failed to start IO - usb_submit_urb failed with result: %d\n",
1502 usb_kill_urb(pcu
->urb_ctrl
);
1509 static void ims_pcu_stop_io(struct ims_pcu
*pcu
)
1511 usb_kill_urb(pcu
->urb_in
);
1512 usb_kill_urb(pcu
->urb_ctrl
);
1515 static int ims_pcu_line_setup(struct ims_pcu
*pcu
)
1517 struct usb_host_interface
*interface
= pcu
->ctrl_intf
->cur_altsetting
;
1518 struct usb_cdc_line_coding
*line
= (void *)pcu
->cmd_buf
;
1521 memset(line
, 0, sizeof(*line
));
1522 line
->dwDTERate
= cpu_to_le32(57600);
1523 line
->bDataBits
= 8;
1525 error
= usb_control_msg(pcu
->udev
, usb_sndctrlpipe(pcu
->udev
, 0),
1526 USB_CDC_REQ_SET_LINE_CODING
,
1527 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1528 0, interface
->desc
.bInterfaceNumber
,
1529 line
, sizeof(struct usb_cdc_line_coding
),
1532 dev_err(pcu
->dev
, "Failed to set line coding, error: %d\n",
1537 error
= usb_control_msg(pcu
->udev
, usb_sndctrlpipe(pcu
->udev
, 0),
1538 USB_CDC_REQ_SET_CONTROL_LINE_STATE
,
1539 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1540 0x03, interface
->desc
.bInterfaceNumber
,
1543 dev_err(pcu
->dev
, "Failed to set line state, error: %d\n",
1551 static int ims_pcu_get_device_info(struct ims_pcu
*pcu
)
1555 error
= ims_pcu_get_info(pcu
);
1559 error
= ims_pcu_execute_query(pcu
, GET_FW_VERSION
);
1562 "GET_FW_VERSION command failed, error: %d\n", error
);
1566 snprintf(pcu
->fw_version
, sizeof(pcu
->fw_version
),
1567 "%02d%02d%02d%02d.%c%c",
1568 pcu
->cmd_buf
[2], pcu
->cmd_buf
[3], pcu
->cmd_buf
[4], pcu
->cmd_buf
[5],
1569 pcu
->cmd_buf
[6], pcu
->cmd_buf
[7]);
1571 error
= ims_pcu_execute_query(pcu
, GET_BL_VERSION
);
1574 "GET_BL_VERSION command failed, error: %d\n", error
);
1578 snprintf(pcu
->bl_version
, sizeof(pcu
->bl_version
),
1579 "%02d%02d%02d%02d.%c%c",
1580 pcu
->cmd_buf
[2], pcu
->cmd_buf
[3], pcu
->cmd_buf
[4], pcu
->cmd_buf
[5],
1581 pcu
->cmd_buf
[6], pcu
->cmd_buf
[7]);
1583 error
= ims_pcu_execute_query(pcu
, RESET_REASON
);
1586 "RESET_REASON command failed, error: %d\n", error
);
1590 snprintf(pcu
->reset_reason
, sizeof(pcu
->reset_reason
),
1591 "%02x", pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
]);
1594 "P/N: %s, MD: %s, S/N: %s, FW: %s, BL: %s, RR: %s\n",
1596 pcu
->date_of_manufacturing
,
1605 static int ims_pcu_identify_type(struct ims_pcu
*pcu
, u8
*device_id
)
1609 error
= ims_pcu_execute_query(pcu
, GET_DEVICE_ID
);
1612 "GET_DEVICE_ID command failed, error: %d\n", error
);
1616 *device_id
= pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
];
1617 dev_dbg(pcu
->dev
, "Detected device ID: %d\n", *device_id
);
1622 static int ims_pcu_init_application_mode(struct ims_pcu
*pcu
)
1624 static atomic_t device_no
= ATOMIC_INIT(0);
1626 const struct ims_pcu_device_info
*info
;
1630 error
= ims_pcu_get_device_info(pcu
);
1632 /* Device does not respond to basic queries, hopeless */
1636 error
= ims_pcu_identify_type(pcu
, &device_id
);
1639 "Failed to identify device, error: %d\n", error
);
1641 * Do not signal error, but do not create input nor
1642 * backlight devices either, let userspace figure this
1643 * out (flash a new firmware?).
1648 if (device_id
>= ARRAY_SIZE(ims_pcu_device_info
) ||
1649 !ims_pcu_device_info
[device_id
].keymap
) {
1650 dev_err(pcu
->dev
, "Device ID %d is not valid\n", device_id
);
1651 /* Same as above, punt to userspace */
1655 /* Device appears to be operable, complete initialization */
1656 pcu
->device_no
= atomic_inc_return(&device_no
) - 1;
1658 error
= ims_pcu_setup_backlight(pcu
);
1662 info
= &ims_pcu_device_info
[device_id
];
1663 error
= ims_pcu_setup_buttons(pcu
, info
->keymap
, info
->keymap_len
);
1665 goto err_destroy_backlight
;
1667 if (info
->has_gamepad
) {
1668 error
= ims_pcu_setup_gamepad(pcu
);
1670 goto err_destroy_buttons
;
1673 pcu
->setup_complete
= true;
1677 err_destroy_backlight
:
1678 ims_pcu_destroy_backlight(pcu
);
1679 err_destroy_buttons
:
1680 ims_pcu_destroy_buttons(pcu
);
1684 static void ims_pcu_destroy_application_mode(struct ims_pcu
*pcu
)
1686 if (pcu
->setup_complete
) {
1687 pcu
->setup_complete
= false;
1688 mb(); /* make sure flag setting is not reordered */
1691 ims_pcu_destroy_gamepad(pcu
);
1692 ims_pcu_destroy_buttons(pcu
);
1693 ims_pcu_destroy_backlight(pcu
);
1697 static int ims_pcu_init_bootloader_mode(struct ims_pcu
*pcu
)
1701 error
= ims_pcu_execute_bl_command(pcu
, QUERY_DEVICE
, NULL
, 0,
1702 IMS_PCU_CMD_RESPONSE_TIMEOUT
);
1704 dev_err(pcu
->dev
, "Bootloader does not respond, aborting\n");
1708 pcu
->fw_start_addr
=
1709 get_unaligned_le32(&pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
+ 11]);
1711 get_unaligned_le32(&pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
+ 15]);
1714 "Device is in bootloader mode (addr 0x%08x-0x%08x), requesting firmware\n",
1715 pcu
->fw_start_addr
, pcu
->fw_end_addr
);
1717 error
= request_firmware_nowait(THIS_MODULE
, true,
1718 IMS_PCU_FIRMWARE_NAME
,
1719 pcu
->dev
, GFP_KERNEL
, pcu
,
1720 ims_pcu_process_async_firmware
);
1722 /* This error is not fatal, let userspace have another chance */
1723 complete(&pcu
->async_firmware_done
);
1729 static void ims_pcu_destroy_bootloader_mode(struct ims_pcu
*pcu
)
1731 /* Make sure our initial firmware request has completed */
1732 wait_for_completion(&pcu
->async_firmware_done
);
1735 #define IMS_PCU_APPLICATION_MODE 0
1736 #define IMS_PCU_BOOTLOADER_MODE 1
1738 static struct usb_driver ims_pcu_driver
;
1740 static int ims_pcu_probe(struct usb_interface
*intf
,
1741 const struct usb_device_id
*id
)
1743 struct usb_device
*udev
= interface_to_usbdev(intf
);
1744 struct ims_pcu
*pcu
;
1747 pcu
= kzalloc(sizeof(struct ims_pcu
), GFP_KERNEL
);
1751 pcu
->dev
= &intf
->dev
;
1753 pcu
->bootloader_mode
= id
->driver_info
== IMS_PCU_BOOTLOADER_MODE
;
1754 mutex_init(&pcu
->cmd_mutex
);
1755 init_completion(&pcu
->cmd_done
);
1756 init_completion(&pcu
->async_firmware_done
);
1758 error
= ims_pcu_parse_cdc_data(intf
, pcu
);
1762 error
= usb_driver_claim_interface(&ims_pcu_driver
,
1763 pcu
->data_intf
, pcu
);
1766 "Unable to claim corresponding data interface: %d\n",
1771 usb_set_intfdata(pcu
->ctrl_intf
, pcu
);
1772 usb_set_intfdata(pcu
->data_intf
, pcu
);
1774 error
= ims_pcu_buffers_alloc(pcu
);
1776 goto err_unclaim_intf
;
1778 error
= ims_pcu_start_io(pcu
);
1780 goto err_free_buffers
;
1782 error
= ims_pcu_line_setup(pcu
);
1786 error
= sysfs_create_group(&intf
->dev
.kobj
, &ims_pcu_attr_group
);
1790 error
= pcu
->bootloader_mode
?
1791 ims_pcu_init_bootloader_mode(pcu
) :
1792 ims_pcu_init_application_mode(pcu
);
1794 goto err_remove_sysfs
;
1799 sysfs_remove_group(&intf
->dev
.kobj
, &ims_pcu_attr_group
);
1801 ims_pcu_stop_io(pcu
);
1803 ims_pcu_buffers_free(pcu
);
1805 usb_driver_release_interface(&ims_pcu_driver
, pcu
->data_intf
);
1811 static void ims_pcu_disconnect(struct usb_interface
*intf
)
1813 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1814 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
1816 usb_set_intfdata(intf
, NULL
);
1819 * See if we are dealing with control or data interface. The cleanup
1820 * happens when we unbind primary (control) interface.
1822 if (alt
->desc
.bInterfaceClass
!= USB_CLASS_COMM
)
1825 sysfs_remove_group(&intf
->dev
.kobj
, &ims_pcu_attr_group
);
1827 ims_pcu_stop_io(pcu
);
1829 if (pcu
->bootloader_mode
)
1830 ims_pcu_destroy_bootloader_mode(pcu
);
1832 ims_pcu_destroy_application_mode(pcu
);
1834 ims_pcu_buffers_free(pcu
);
1839 static int ims_pcu_suspend(struct usb_interface
*intf
,
1840 pm_message_t message
)
1842 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1843 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
1845 if (alt
->desc
.bInterfaceClass
== USB_CLASS_COMM
)
1846 ims_pcu_stop_io(pcu
);
1851 static int ims_pcu_resume(struct usb_interface
*intf
)
1853 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1854 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
1857 if (alt
->desc
.bInterfaceClass
== USB_CLASS_COMM
) {
1858 retval
= ims_pcu_start_io(pcu
);
1860 retval
= ims_pcu_line_setup(pcu
);
1867 static const struct usb_device_id ims_pcu_id_table
[] = {
1869 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0082,
1871 USB_CDC_SUBCLASS_ACM
,
1872 USB_CDC_ACM_PROTO_AT_V25TER
),
1873 .driver_info
= IMS_PCU_APPLICATION_MODE
,
1876 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0083,
1878 USB_CDC_SUBCLASS_ACM
,
1879 USB_CDC_ACM_PROTO_AT_V25TER
),
1880 .driver_info
= IMS_PCU_BOOTLOADER_MODE
,
1885 static struct usb_driver ims_pcu_driver
= {
1887 .id_table
= ims_pcu_id_table
,
1888 .probe
= ims_pcu_probe
,
1889 .disconnect
= ims_pcu_disconnect
,
1891 .suspend
= ims_pcu_suspend
,
1892 .resume
= ims_pcu_resume
,
1893 .reset_resume
= ims_pcu_resume
,
1897 module_usb_driver(ims_pcu_driver
);
1899 MODULE_DESCRIPTION("IMS Passenger Control Unit driver");
1900 MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
1901 MODULE_LICENSE("GPL");