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 (9 + 1)
46 #define IMS_PCU_BL_VERSION_LEN (9 + 1)
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();
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 */
374 #define IMS_PCU_CMD_OFN_SET_CONFIG 0xb3
375 #define IMS_PCU_CMD_OFN_GET_CONFIG 0xb4
378 #define IMS_PCU_RSP_STATUS 0xc0
379 #define IMS_PCU_RSP_PCU_RESET 0 /* Originally 0xc1 */
380 #define IMS_PCU_RSP_RESET_REASON 0xc2
381 #define IMS_PCU_RSP_SEND_BUTTONS 0xc3
382 #define IMS_PCU_RSP_JUMP_TO_BTLDR 0 /* Originally 0xc4 */
383 #define IMS_PCU_RSP_GET_INFO 0xc5
384 #define IMS_PCU_RSP_SET_BRIGHTNESS 0xc6
385 #define IMS_PCU_RSP_EEPROM 0xc7
386 #define IMS_PCU_RSP_GET_FW_VERSION 0xc8
387 #define IMS_PCU_RSP_GET_BL_VERSION 0xc9
388 #define IMS_PCU_RSP_SET_INFO 0xcb
389 #define IMS_PCU_RSP_GET_BRIGHTNESS 0xcc
390 #define IMS_PCU_RSP_CMD_INVALID 0xcd
391 #define IMS_PCU_RSP_GET_DEVICE_ID 0xce
392 #define IMS_PCU_RSP_SPECIAL_INFO 0xd0
393 #define IMS_PCU_RSP_BOOTLOADER 0xd1 /* Bootloader response */
394 #define IMS_PCU_RSP_OFN_SET_CONFIG 0xd2
395 #define IMS_PCU_RSP_OFN_GET_CONFIG 0xd3
398 #define IMS_PCU_RSP_EVNT_BUTTONS 0xe0 /* Unsolicited, button state */
399 #define IMS_PCU_GAMEPAD_MASK 0x0001ff80UL /* Bits 7 through 16 */
402 #define IMS_PCU_MIN_PACKET_LEN 3
403 #define IMS_PCU_DATA_OFFSET 2
405 #define IMS_PCU_CMD_WRITE_TIMEOUT 100 /* msec */
406 #define IMS_PCU_CMD_RESPONSE_TIMEOUT 500 /* msec */
408 static void ims_pcu_report_events(struct ims_pcu
*pcu
)
410 u32 data
= get_unaligned_be32(&pcu
->read_buf
[3]);
412 ims_pcu_buttons_report(pcu
, data
& ~IMS_PCU_GAMEPAD_MASK
);
414 ims_pcu_gamepad_report(pcu
, data
);
417 static void ims_pcu_handle_response(struct ims_pcu
*pcu
)
419 switch (pcu
->read_buf
[0]) {
420 case IMS_PCU_RSP_EVNT_BUTTONS
:
421 if (likely(pcu
->setup_complete
))
422 ims_pcu_report_events(pcu
);
427 * See if we got command completion.
428 * If both the sequence and response code match save
429 * the data and signal completion.
431 if (pcu
->read_buf
[0] == pcu
->expected_response
&&
432 pcu
->read_buf
[1] == pcu
->ack_id
- 1) {
434 memcpy(pcu
->cmd_buf
, pcu
->read_buf
, pcu
->read_pos
);
435 pcu
->cmd_buf_len
= pcu
->read_pos
;
436 complete(&pcu
->cmd_done
);
442 static void ims_pcu_process_data(struct ims_pcu
*pcu
, struct urb
*urb
)
446 for (i
= 0; i
< urb
->actual_length
; i
++) {
447 u8 data
= pcu
->urb_in_buf
[i
];
449 /* Skip everything until we get Start Xmit */
450 if (!pcu
->have_stx
&& data
!= IMS_PCU_PROTOCOL_STX
)
454 pcu
->have_dle
= false;
455 pcu
->read_buf
[pcu
->read_pos
++] = data
;
456 pcu
->check_sum
+= data
;
461 case IMS_PCU_PROTOCOL_STX
:
464 "Unexpected STX at byte %d, discarding old data\n",
466 pcu
->have_stx
= true;
467 pcu
->have_dle
= false;
472 case IMS_PCU_PROTOCOL_DLE
:
473 pcu
->have_dle
= true;
476 case IMS_PCU_PROTOCOL_ETX
:
477 if (pcu
->read_pos
< IMS_PCU_MIN_PACKET_LEN
) {
479 "Short packet received (%d bytes), ignoring\n",
481 } else if (pcu
->check_sum
!= 0) {
483 "Invalid checksum in packet (%d bytes), ignoring\n",
486 ims_pcu_handle_response(pcu
);
489 pcu
->have_stx
= false;
490 pcu
->have_dle
= false;
495 pcu
->read_buf
[pcu
->read_pos
++] = data
;
496 pcu
->check_sum
+= data
;
502 static bool ims_pcu_byte_needs_escape(u8 byte
)
504 return byte
== IMS_PCU_PROTOCOL_STX
||
505 byte
== IMS_PCU_PROTOCOL_ETX
||
506 byte
== IMS_PCU_PROTOCOL_DLE
;
509 static int ims_pcu_send_cmd_chunk(struct ims_pcu
*pcu
,
510 u8 command
, int chunk
, int len
)
514 error
= usb_bulk_msg(pcu
->udev
,
515 usb_sndbulkpipe(pcu
->udev
,
516 pcu
->ep_out
->bEndpointAddress
),
517 pcu
->urb_out_buf
, len
,
518 NULL
, IMS_PCU_CMD_WRITE_TIMEOUT
);
521 "Sending 0x%02x command failed at chunk %d: %d\n",
522 command
, chunk
, error
);
529 static int ims_pcu_send_command(struct ims_pcu
*pcu
,
530 u8 command
, const u8
*data
, int len
)
540 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_STX
;
542 /* We know the command need not be escaped */
543 pcu
->urb_out_buf
[count
++] = command
;
546 ack_id
= pcu
->ack_id
++;
548 ack_id
= pcu
->ack_id
++;
550 if (ims_pcu_byte_needs_escape(ack_id
))
551 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_DLE
;
553 pcu
->urb_out_buf
[count
++] = ack_id
;
556 for (i
= 0; i
< len
; i
++) {
558 delta
= ims_pcu_byte_needs_escape(data
[i
]) ? 2 : 1;
559 if (count
+ delta
>= pcu
->max_out_size
) {
560 error
= ims_pcu_send_cmd_chunk(pcu
, command
,
569 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_DLE
;
571 pcu
->urb_out_buf
[count
++] = data
[i
];
577 delta
= ims_pcu_byte_needs_escape(csum
) ? 3 : 2;
578 if (count
+ delta
>= pcu
->max_out_size
) {
579 error
= ims_pcu_send_cmd_chunk(pcu
, command
, ++chunk
, count
);
587 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_DLE
;
589 pcu
->urb_out_buf
[count
++] = csum
;
590 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_ETX
;
592 return ims_pcu_send_cmd_chunk(pcu
, command
, ++chunk
, count
);
595 static int __ims_pcu_execute_command(struct ims_pcu
*pcu
,
596 u8 command
, const void *data
, size_t len
,
597 u8 expected_response
, int response_time
)
601 pcu
->expected_response
= expected_response
;
602 init_completion(&pcu
->cmd_done
);
604 error
= ims_pcu_send_command(pcu
, command
, data
, len
);
608 if (expected_response
&&
609 !wait_for_completion_timeout(&pcu
->cmd_done
,
610 msecs_to_jiffies(response_time
))) {
611 dev_dbg(pcu
->dev
, "Command 0x%02x timed out\n", command
);
618 #define ims_pcu_execute_command(pcu, code, data, len) \
619 __ims_pcu_execute_command(pcu, \
620 IMS_PCU_CMD_##code, data, len, \
621 IMS_PCU_RSP_##code, \
622 IMS_PCU_CMD_RESPONSE_TIMEOUT)
624 #define ims_pcu_execute_query(pcu, code) \
625 ims_pcu_execute_command(pcu, code, NULL, 0)
627 /* Bootloader commands */
628 #define IMS_PCU_BL_CMD_QUERY_DEVICE 0xa1
629 #define IMS_PCU_BL_CMD_UNLOCK_CONFIG 0xa2
630 #define IMS_PCU_BL_CMD_ERASE_APP 0xa3
631 #define IMS_PCU_BL_CMD_PROGRAM_DEVICE 0xa4
632 #define IMS_PCU_BL_CMD_PROGRAM_COMPLETE 0xa5
633 #define IMS_PCU_BL_CMD_READ_APP 0xa6
634 #define IMS_PCU_BL_CMD_RESET_DEVICE 0xa7
635 #define IMS_PCU_BL_CMD_LAUNCH_APP 0xa8
637 /* Bootloader commands */
638 #define IMS_PCU_BL_RSP_QUERY_DEVICE 0xc1
639 #define IMS_PCU_BL_RSP_UNLOCK_CONFIG 0xc2
640 #define IMS_PCU_BL_RSP_ERASE_APP 0xc3
641 #define IMS_PCU_BL_RSP_PROGRAM_DEVICE 0xc4
642 #define IMS_PCU_BL_RSP_PROGRAM_COMPLETE 0xc5
643 #define IMS_PCU_BL_RSP_READ_APP 0xc6
644 #define IMS_PCU_BL_RSP_RESET_DEVICE 0 /* originally 0xa7 */
645 #define IMS_PCU_BL_RSP_LAUNCH_APP 0 /* originally 0xa8 */
647 #define IMS_PCU_BL_DATA_OFFSET 3
649 static int __ims_pcu_execute_bl_command(struct ims_pcu
*pcu
,
650 u8 command
, const void *data
, size_t len
,
651 u8 expected_response
, int response_time
)
655 pcu
->cmd_buf
[0] = command
;
657 memcpy(&pcu
->cmd_buf
[1], data
, len
);
659 error
= __ims_pcu_execute_command(pcu
,
660 IMS_PCU_CMD_BOOTLOADER
, pcu
->cmd_buf
, len
+ 1,
661 expected_response
? IMS_PCU_RSP_BOOTLOADER
: 0,
665 "Failure when sending 0x%02x command to bootloader, error: %d\n",
666 pcu
->cmd_buf
[0], error
);
670 if (expected_response
&& pcu
->cmd_buf
[2] != expected_response
) {
672 "Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n",
673 pcu
->cmd_buf
[2], expected_response
);
680 #define ims_pcu_execute_bl_command(pcu, code, data, len, timeout) \
681 __ims_pcu_execute_bl_command(pcu, \
682 IMS_PCU_BL_CMD_##code, data, len, \
683 IMS_PCU_BL_RSP_##code, timeout) \
685 #define IMS_PCU_INFO_PART_OFFSET 2
686 #define IMS_PCU_INFO_DOM_OFFSET 17
687 #define IMS_PCU_INFO_SERIAL_OFFSET 25
689 #define IMS_PCU_SET_INFO_SIZE 31
691 static int ims_pcu_get_info(struct ims_pcu
*pcu
)
695 error
= ims_pcu_execute_query(pcu
, GET_INFO
);
698 "GET_INFO command failed, error: %d\n", error
);
702 memcpy(pcu
->part_number
,
703 &pcu
->cmd_buf
[IMS_PCU_INFO_PART_OFFSET
],
704 sizeof(pcu
->part_number
));
705 memcpy(pcu
->date_of_manufacturing
,
706 &pcu
->cmd_buf
[IMS_PCU_INFO_DOM_OFFSET
],
707 sizeof(pcu
->date_of_manufacturing
));
708 memcpy(pcu
->serial_number
,
709 &pcu
->cmd_buf
[IMS_PCU_INFO_SERIAL_OFFSET
],
710 sizeof(pcu
->serial_number
));
715 static int ims_pcu_set_info(struct ims_pcu
*pcu
)
719 memcpy(&pcu
->cmd_buf
[IMS_PCU_INFO_PART_OFFSET
],
720 pcu
->part_number
, sizeof(pcu
->part_number
));
721 memcpy(&pcu
->cmd_buf
[IMS_PCU_INFO_DOM_OFFSET
],
722 pcu
->date_of_manufacturing
, sizeof(pcu
->date_of_manufacturing
));
723 memcpy(&pcu
->cmd_buf
[IMS_PCU_INFO_SERIAL_OFFSET
],
724 pcu
->serial_number
, sizeof(pcu
->serial_number
));
726 error
= ims_pcu_execute_command(pcu
, SET_INFO
,
727 &pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
],
728 IMS_PCU_SET_INFO_SIZE
);
731 "Failed to update device information, error: %d\n",
739 static int ims_pcu_switch_to_bootloader(struct ims_pcu
*pcu
)
743 /* Execute jump to the bootoloader */
744 error
= ims_pcu_execute_command(pcu
, JUMP_TO_BTLDR
, NULL
, 0);
747 "Failure when sending JUMP TO BOOLTLOADER command, error: %d\n",
755 /*********************************************************************
756 * Firmware Update handling *
757 *********************************************************************/
759 #define IMS_PCU_FIRMWARE_NAME "imspcu.fw"
761 struct ims_pcu_flash_fmt
{
767 static unsigned int ims_pcu_count_fw_records(const struct firmware
*fw
)
769 const struct ihex_binrec
*rec
= (const struct ihex_binrec
*)fw
->data
;
770 unsigned int count
= 0;
774 rec
= ihex_next_binrec(rec
);
780 static int ims_pcu_verify_block(struct ims_pcu
*pcu
,
781 u32 addr
, u8 len
, const u8
*data
)
783 struct ims_pcu_flash_fmt
*fragment
;
786 fragment
= (void *)&pcu
->cmd_buf
[1];
787 put_unaligned_le32(addr
, &fragment
->addr
);
790 error
= ims_pcu_execute_bl_command(pcu
, READ_APP
, NULL
, 5,
791 IMS_PCU_CMD_RESPONSE_TIMEOUT
);
794 "Failed to retrieve block at 0x%08x, len %d, error: %d\n",
799 fragment
= (void *)&pcu
->cmd_buf
[IMS_PCU_BL_DATA_OFFSET
];
800 if (get_unaligned_le32(&fragment
->addr
) != addr
||
801 fragment
->len
!= len
) {
803 "Wrong block when retrieving 0x%08x (0x%08x), len %d (%d)\n",
804 addr
, get_unaligned_le32(&fragment
->addr
),
809 if (memcmp(fragment
->data
, data
, len
)) {
811 "Mismatch in block at 0x%08x, len %d\n",
819 static int ims_pcu_flash_firmware(struct ims_pcu
*pcu
,
820 const struct firmware
*fw
,
821 unsigned int n_fw_records
)
823 const struct ihex_binrec
*rec
= (const struct ihex_binrec
*)fw
->data
;
824 struct ims_pcu_flash_fmt
*fragment
;
825 unsigned int count
= 0;
830 error
= ims_pcu_execute_bl_command(pcu
, ERASE_APP
, NULL
, 0, 2000);
833 "Failed to erase application image, error: %d\n",
840 * The firmware format is messed up for some reason.
841 * The address twice that of what is needed for some
842 * reason and we end up overwriting half of the data
843 * with the next record.
845 addr
= be32_to_cpu(rec
->addr
) / 2;
846 len
= be16_to_cpu(rec
->len
);
848 fragment
= (void *)&pcu
->cmd_buf
[1];
849 put_unaligned_le32(addr
, &fragment
->addr
);
851 memcpy(fragment
->data
, rec
->data
, len
);
853 error
= ims_pcu_execute_bl_command(pcu
, PROGRAM_DEVICE
,
855 IMS_PCU_CMD_RESPONSE_TIMEOUT
);
858 "Failed to write block at 0x%08x, len %d, error: %d\n",
863 if (addr
>= pcu
->fw_start_addr
&& addr
< pcu
->fw_end_addr
) {
864 error
= ims_pcu_verify_block(pcu
, addr
, len
, rec
->data
);
870 pcu
->update_firmware_status
= (count
* 100) / n_fw_records
;
872 rec
= ihex_next_binrec(rec
);
875 error
= ims_pcu_execute_bl_command(pcu
, PROGRAM_COMPLETE
,
879 "Failed to send PROGRAM_COMPLETE, error: %d\n",
885 static int ims_pcu_handle_firmware_update(struct ims_pcu
*pcu
,
886 const struct firmware
*fw
)
888 unsigned int n_fw_records
;
891 dev_info(pcu
->dev
, "Updating firmware %s, size: %zu\n",
892 IMS_PCU_FIRMWARE_NAME
, fw
->size
);
894 n_fw_records
= ims_pcu_count_fw_records(fw
);
896 retval
= ims_pcu_flash_firmware(pcu
, fw
, n_fw_records
);
900 retval
= ims_pcu_execute_bl_command(pcu
, LAUNCH_APP
, NULL
, 0, 0);
903 "Failed to start application image, error: %d\n",
907 pcu
->update_firmware_status
= retval
;
908 sysfs_notify(&pcu
->dev
->kobj
, NULL
, "update_firmware_status");
912 static void ims_pcu_process_async_firmware(const struct firmware
*fw
,
915 struct ims_pcu
*pcu
= context
;
919 dev_err(pcu
->dev
, "Failed to get firmware %s\n",
920 IMS_PCU_FIRMWARE_NAME
);
924 error
= ihex_validate_fw(fw
);
926 dev_err(pcu
->dev
, "Firmware %s is invalid\n",
927 IMS_PCU_FIRMWARE_NAME
);
931 mutex_lock(&pcu
->cmd_mutex
);
932 ims_pcu_handle_firmware_update(pcu
, fw
);
933 mutex_unlock(&pcu
->cmd_mutex
);
935 release_firmware(fw
);
938 complete(&pcu
->async_firmware_done
);
941 /*********************************************************************
942 * Backlight LED device support *
943 *********************************************************************/
945 #define IMS_PCU_MAX_BRIGHTNESS 31998
947 static int ims_pcu_backlight_set_brightness(struct led_classdev
*cdev
,
948 enum led_brightness value
)
950 struct ims_pcu_backlight
*backlight
=
951 container_of(cdev
, struct ims_pcu_backlight
, cdev
);
952 struct ims_pcu
*pcu
=
953 container_of(backlight
, struct ims_pcu
, backlight
);
954 __le16 br_val
= cpu_to_le16(value
);
957 mutex_lock(&pcu
->cmd_mutex
);
959 error
= ims_pcu_execute_command(pcu
, SET_BRIGHTNESS
,
960 &br_val
, sizeof(br_val
));
961 if (error
&& error
!= -ENODEV
)
963 "Failed to set desired brightness %u, error: %d\n",
966 mutex_unlock(&pcu
->cmd_mutex
);
971 static enum led_brightness
972 ims_pcu_backlight_get_brightness(struct led_classdev
*cdev
)
974 struct ims_pcu_backlight
*backlight
=
975 container_of(cdev
, struct ims_pcu_backlight
, cdev
);
976 struct ims_pcu
*pcu
=
977 container_of(backlight
, struct ims_pcu
, backlight
);
981 mutex_lock(&pcu
->cmd_mutex
);
983 error
= ims_pcu_execute_query(pcu
, GET_BRIGHTNESS
);
986 "Failed to get current brightness, error: %d\n",
988 /* Assume the LED is OFF */
989 brightness
= LED_OFF
;
992 get_unaligned_le16(&pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
]);
995 mutex_unlock(&pcu
->cmd_mutex
);
1000 static int ims_pcu_setup_backlight(struct ims_pcu
*pcu
)
1002 struct ims_pcu_backlight
*backlight
= &pcu
->backlight
;
1005 snprintf(backlight
->name
, sizeof(backlight
->name
),
1006 "pcu%d::kbd_backlight", pcu
->device_no
);
1008 backlight
->cdev
.name
= backlight
->name
;
1009 backlight
->cdev
.max_brightness
= IMS_PCU_MAX_BRIGHTNESS
;
1010 backlight
->cdev
.brightness_get
= ims_pcu_backlight_get_brightness
;
1011 backlight
->cdev
.brightness_set_blocking
=
1012 ims_pcu_backlight_set_brightness
;
1014 error
= led_classdev_register(pcu
->dev
, &backlight
->cdev
);
1017 "Failed to register backlight LED device, error: %d\n",
1025 static void ims_pcu_destroy_backlight(struct ims_pcu
*pcu
)
1027 struct ims_pcu_backlight
*backlight
= &pcu
->backlight
;
1029 led_classdev_unregister(&backlight
->cdev
);
1033 /*********************************************************************
1034 * Sysfs attributes handling *
1035 *********************************************************************/
1037 struct ims_pcu_attribute
{
1038 struct device_attribute dattr
;
1039 size_t field_offset
;
1043 static ssize_t
ims_pcu_attribute_show(struct device
*dev
,
1044 struct device_attribute
*dattr
,
1047 struct usb_interface
*intf
= to_usb_interface(dev
);
1048 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1049 struct ims_pcu_attribute
*attr
=
1050 container_of(dattr
, struct ims_pcu_attribute
, dattr
);
1051 char *field
= (char *)pcu
+ attr
->field_offset
;
1053 return scnprintf(buf
, PAGE_SIZE
, "%.*s\n", attr
->field_length
, field
);
1056 static ssize_t
ims_pcu_attribute_store(struct device
*dev
,
1057 struct device_attribute
*dattr
,
1058 const char *buf
, size_t count
)
1061 struct usb_interface
*intf
= to_usb_interface(dev
);
1062 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1063 struct ims_pcu_attribute
*attr
=
1064 container_of(dattr
, struct ims_pcu_attribute
, dattr
);
1065 char *field
= (char *)pcu
+ attr
->field_offset
;
1069 if (count
> attr
->field_length
)
1072 data_len
= strnlen(buf
, attr
->field_length
);
1073 if (data_len
> attr
->field_length
)
1076 error
= mutex_lock_interruptible(&pcu
->cmd_mutex
);
1080 memset(field
, 0, attr
->field_length
);
1081 memcpy(field
, buf
, data_len
);
1083 error
= ims_pcu_set_info(pcu
);
1086 * Even if update failed, let's fetch the info again as we just
1087 * clobbered one of the fields.
1089 ims_pcu_get_info(pcu
);
1091 mutex_unlock(&pcu
->cmd_mutex
);
1093 return error
< 0 ? error
: count
;
1096 #define IMS_PCU_ATTR(_field, _mode) \
1097 struct ims_pcu_attribute ims_pcu_attr_##_field = { \
1098 .dattr = __ATTR(_field, _mode, \
1099 ims_pcu_attribute_show, \
1100 ims_pcu_attribute_store), \
1101 .field_offset = offsetof(struct ims_pcu, _field), \
1102 .field_length = sizeof(((struct ims_pcu *)NULL)->_field), \
1105 #define IMS_PCU_RO_ATTR(_field) \
1106 IMS_PCU_ATTR(_field, S_IRUGO)
1107 #define IMS_PCU_RW_ATTR(_field) \
1108 IMS_PCU_ATTR(_field, S_IRUGO | S_IWUSR)
1110 static IMS_PCU_RW_ATTR(part_number
);
1111 static IMS_PCU_RW_ATTR(serial_number
);
1112 static IMS_PCU_RW_ATTR(date_of_manufacturing
);
1114 static IMS_PCU_RO_ATTR(fw_version
);
1115 static IMS_PCU_RO_ATTR(bl_version
);
1116 static IMS_PCU_RO_ATTR(reset_reason
);
1118 static ssize_t
ims_pcu_reset_device(struct device
*dev
,
1119 struct device_attribute
*dattr
,
1120 const char *buf
, size_t count
)
1122 static const u8 reset_byte
= 1;
1123 struct usb_interface
*intf
= to_usb_interface(dev
);
1124 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1128 error
= kstrtoint(buf
, 0, &value
);
1135 dev_info(pcu
->dev
, "Attempting to reset device\n");
1137 error
= ims_pcu_execute_command(pcu
, PCU_RESET
, &reset_byte
, 1);
1140 "Failed to reset device, error: %d\n",
1148 static DEVICE_ATTR(reset_device
, S_IWUSR
, NULL
, ims_pcu_reset_device
);
1150 static ssize_t
ims_pcu_update_firmware_store(struct device
*dev
,
1151 struct device_attribute
*dattr
,
1152 const char *buf
, size_t count
)
1154 struct usb_interface
*intf
= to_usb_interface(dev
);
1155 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1156 const struct firmware
*fw
= NULL
;
1160 error
= kstrtoint(buf
, 0, &value
);
1167 error
= mutex_lock_interruptible(&pcu
->cmd_mutex
);
1171 error
= request_ihex_firmware(&fw
, IMS_PCU_FIRMWARE_NAME
, pcu
->dev
);
1173 dev_err(pcu
->dev
, "Failed to request firmware %s, error: %d\n",
1174 IMS_PCU_FIRMWARE_NAME
, error
);
1179 * If we are already in bootloader mode we can proceed with
1180 * flashing the firmware.
1182 * If we are in application mode, then we need to switch into
1183 * bootloader mode, which will cause the device to disconnect
1184 * and reconnect as different device.
1186 if (pcu
->bootloader_mode
)
1187 error
= ims_pcu_handle_firmware_update(pcu
, fw
);
1189 error
= ims_pcu_switch_to_bootloader(pcu
);
1191 release_firmware(fw
);
1194 mutex_unlock(&pcu
->cmd_mutex
);
1195 return error
?: count
;
1198 static DEVICE_ATTR(update_firmware
, S_IWUSR
,
1199 NULL
, ims_pcu_update_firmware_store
);
1202 ims_pcu_update_firmware_status_show(struct device
*dev
,
1203 struct device_attribute
*dattr
,
1206 struct usb_interface
*intf
= to_usb_interface(dev
);
1207 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1209 return scnprintf(buf
, PAGE_SIZE
, "%d\n", pcu
->update_firmware_status
);
1212 static DEVICE_ATTR(update_firmware_status
, S_IRUGO
,
1213 ims_pcu_update_firmware_status_show
, NULL
);
1215 static struct attribute
*ims_pcu_attrs
[] = {
1216 &ims_pcu_attr_part_number
.dattr
.attr
,
1217 &ims_pcu_attr_serial_number
.dattr
.attr
,
1218 &ims_pcu_attr_date_of_manufacturing
.dattr
.attr
,
1219 &ims_pcu_attr_fw_version
.dattr
.attr
,
1220 &ims_pcu_attr_bl_version
.dattr
.attr
,
1221 &ims_pcu_attr_reset_reason
.dattr
.attr
,
1222 &dev_attr_reset_device
.attr
,
1223 &dev_attr_update_firmware
.attr
,
1224 &dev_attr_update_firmware_status
.attr
,
1228 static umode_t
ims_pcu_is_attr_visible(struct kobject
*kobj
,
1229 struct attribute
*attr
, int n
)
1231 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1232 struct usb_interface
*intf
= to_usb_interface(dev
);
1233 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1234 umode_t mode
= attr
->mode
;
1236 if (pcu
->bootloader_mode
) {
1237 if (attr
!= &dev_attr_update_firmware_status
.attr
&&
1238 attr
!= &dev_attr_update_firmware
.attr
&&
1239 attr
!= &dev_attr_reset_device
.attr
) {
1243 if (attr
== &dev_attr_update_firmware_status
.attr
)
1250 static const struct attribute_group ims_pcu_attr_group
= {
1251 .is_visible
= ims_pcu_is_attr_visible
,
1252 .attrs
= ims_pcu_attrs
,
1255 /* Support for a separate OFN attribute group */
1257 #define OFN_REG_RESULT_OFFSET 2
1259 static int ims_pcu_read_ofn_config(struct ims_pcu
*pcu
, u8 addr
, u8
*data
)
1264 error
= ims_pcu_execute_command(pcu
, OFN_GET_CONFIG
,
1265 &addr
, sizeof(addr
));
1269 result
= (s16
)get_unaligned_le16(pcu
->cmd_buf
+ OFN_REG_RESULT_OFFSET
);
1273 /* We only need LSB */
1274 *data
= pcu
->cmd_buf
[OFN_REG_RESULT_OFFSET
];
1278 static int ims_pcu_write_ofn_config(struct ims_pcu
*pcu
, u8 addr
, u8 data
)
1280 u8 buffer
[] = { addr
, data
};
1284 error
= ims_pcu_execute_command(pcu
, OFN_SET_CONFIG
,
1285 &buffer
, sizeof(buffer
));
1289 result
= (s16
)get_unaligned_le16(pcu
->cmd_buf
+ OFN_REG_RESULT_OFFSET
);
1296 static ssize_t
ims_pcu_ofn_reg_data_show(struct device
*dev
,
1297 struct device_attribute
*dattr
,
1300 struct usb_interface
*intf
= to_usb_interface(dev
);
1301 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1305 mutex_lock(&pcu
->cmd_mutex
);
1306 error
= ims_pcu_read_ofn_config(pcu
, pcu
->ofn_reg_addr
, &data
);
1307 mutex_unlock(&pcu
->cmd_mutex
);
1312 return scnprintf(buf
, PAGE_SIZE
, "%x\n", data
);
1315 static ssize_t
ims_pcu_ofn_reg_data_store(struct device
*dev
,
1316 struct device_attribute
*dattr
,
1317 const char *buf
, size_t count
)
1319 struct usb_interface
*intf
= to_usb_interface(dev
);
1320 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1324 error
= kstrtou8(buf
, 0, &value
);
1328 mutex_lock(&pcu
->cmd_mutex
);
1329 error
= ims_pcu_write_ofn_config(pcu
, pcu
->ofn_reg_addr
, value
);
1330 mutex_unlock(&pcu
->cmd_mutex
);
1332 return error
?: count
;
1335 static DEVICE_ATTR(reg_data
, S_IRUGO
| S_IWUSR
,
1336 ims_pcu_ofn_reg_data_show
, ims_pcu_ofn_reg_data_store
);
1338 static ssize_t
ims_pcu_ofn_reg_addr_show(struct device
*dev
,
1339 struct device_attribute
*dattr
,
1342 struct usb_interface
*intf
= to_usb_interface(dev
);
1343 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1346 mutex_lock(&pcu
->cmd_mutex
);
1347 error
= scnprintf(buf
, PAGE_SIZE
, "%x\n", pcu
->ofn_reg_addr
);
1348 mutex_unlock(&pcu
->cmd_mutex
);
1353 static ssize_t
ims_pcu_ofn_reg_addr_store(struct device
*dev
,
1354 struct device_attribute
*dattr
,
1355 const char *buf
, size_t count
)
1357 struct usb_interface
*intf
= to_usb_interface(dev
);
1358 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1362 error
= kstrtou8(buf
, 0, &value
);
1366 mutex_lock(&pcu
->cmd_mutex
);
1367 pcu
->ofn_reg_addr
= value
;
1368 mutex_unlock(&pcu
->cmd_mutex
);
1373 static DEVICE_ATTR(reg_addr
, S_IRUGO
| S_IWUSR
,
1374 ims_pcu_ofn_reg_addr_show
, ims_pcu_ofn_reg_addr_store
);
1376 struct ims_pcu_ofn_bit_attribute
{
1377 struct device_attribute dattr
;
1382 static ssize_t
ims_pcu_ofn_bit_show(struct device
*dev
,
1383 struct device_attribute
*dattr
,
1386 struct usb_interface
*intf
= to_usb_interface(dev
);
1387 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1388 struct ims_pcu_ofn_bit_attribute
*attr
=
1389 container_of(dattr
, struct ims_pcu_ofn_bit_attribute
, dattr
);
1393 mutex_lock(&pcu
->cmd_mutex
);
1394 error
= ims_pcu_read_ofn_config(pcu
, attr
->addr
, &data
);
1395 mutex_unlock(&pcu
->cmd_mutex
);
1400 return scnprintf(buf
, PAGE_SIZE
, "%d\n", !!(data
& (1 << attr
->nr
)));
1403 static ssize_t
ims_pcu_ofn_bit_store(struct device
*dev
,
1404 struct device_attribute
*dattr
,
1405 const char *buf
, size_t count
)
1407 struct usb_interface
*intf
= to_usb_interface(dev
);
1408 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1409 struct ims_pcu_ofn_bit_attribute
*attr
=
1410 container_of(dattr
, struct ims_pcu_ofn_bit_attribute
, dattr
);
1415 error
= kstrtoint(buf
, 0, &value
);
1422 mutex_lock(&pcu
->cmd_mutex
);
1424 error
= ims_pcu_read_ofn_config(pcu
, attr
->addr
, &data
);
1427 data
|= 1U << attr
->nr
;
1429 data
&= ~(1U << attr
->nr
);
1431 error
= ims_pcu_write_ofn_config(pcu
, attr
->addr
, data
);
1434 mutex_unlock(&pcu
->cmd_mutex
);
1436 return error
?: count
;
1439 #define IMS_PCU_OFN_BIT_ATTR(_field, _addr, _nr) \
1440 struct ims_pcu_ofn_bit_attribute ims_pcu_ofn_attr_##_field = { \
1441 .dattr = __ATTR(_field, S_IWUSR | S_IRUGO, \
1442 ims_pcu_ofn_bit_show, ims_pcu_ofn_bit_store), \
1447 static IMS_PCU_OFN_BIT_ATTR(engine_enable
, 0x60, 7);
1448 static IMS_PCU_OFN_BIT_ATTR(speed_enable
, 0x60, 6);
1449 static IMS_PCU_OFN_BIT_ATTR(assert_enable
, 0x60, 5);
1450 static IMS_PCU_OFN_BIT_ATTR(xyquant_enable
, 0x60, 4);
1451 static IMS_PCU_OFN_BIT_ATTR(xyscale_enable
, 0x60, 1);
1453 static IMS_PCU_OFN_BIT_ATTR(scale_x2
, 0x63, 6);
1454 static IMS_PCU_OFN_BIT_ATTR(scale_y2
, 0x63, 7);
1456 static struct attribute
*ims_pcu_ofn_attrs
[] = {
1457 &dev_attr_reg_data
.attr
,
1458 &dev_attr_reg_addr
.attr
,
1459 &ims_pcu_ofn_attr_engine_enable
.dattr
.attr
,
1460 &ims_pcu_ofn_attr_speed_enable
.dattr
.attr
,
1461 &ims_pcu_ofn_attr_assert_enable
.dattr
.attr
,
1462 &ims_pcu_ofn_attr_xyquant_enable
.dattr
.attr
,
1463 &ims_pcu_ofn_attr_xyscale_enable
.dattr
.attr
,
1464 &ims_pcu_ofn_attr_scale_x2
.dattr
.attr
,
1465 &ims_pcu_ofn_attr_scale_y2
.dattr
.attr
,
1469 static const struct attribute_group ims_pcu_ofn_attr_group
= {
1471 .attrs
= ims_pcu_ofn_attrs
,
1474 static void ims_pcu_irq(struct urb
*urb
)
1476 struct ims_pcu
*pcu
= urb
->context
;
1479 status
= urb
->status
;
1488 /* this urb is terminated, clean up */
1489 dev_dbg(pcu
->dev
, "%s - urb shutting down with status: %d\n",
1493 dev_dbg(pcu
->dev
, "%s - nonzero urb status received: %d\n",
1498 dev_dbg(pcu
->dev
, "%s: received %d: %*ph\n", __func__
,
1499 urb
->actual_length
, urb
->actual_length
, pcu
->urb_in_buf
);
1501 if (urb
== pcu
->urb_in
)
1502 ims_pcu_process_data(pcu
, urb
);
1505 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1506 if (retval
&& retval
!= -ENODEV
)
1507 dev_err(pcu
->dev
, "%s - usb_submit_urb failed with result %d\n",
1511 static int ims_pcu_buffers_alloc(struct ims_pcu
*pcu
)
1515 pcu
->urb_in_buf
= usb_alloc_coherent(pcu
->udev
, pcu
->max_in_size
,
1516 GFP_KERNEL
, &pcu
->read_dma
);
1517 if (!pcu
->urb_in_buf
) {
1519 "Failed to allocate memory for read buffer\n");
1523 pcu
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
1525 dev_err(pcu
->dev
, "Failed to allocate input URB\n");
1527 goto err_free_urb_in_buf
;
1530 pcu
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1531 pcu
->urb_in
->transfer_dma
= pcu
->read_dma
;
1533 usb_fill_bulk_urb(pcu
->urb_in
, pcu
->udev
,
1534 usb_rcvbulkpipe(pcu
->udev
,
1535 pcu
->ep_in
->bEndpointAddress
),
1536 pcu
->urb_in_buf
, pcu
->max_in_size
,
1540 * We are using usb_bulk_msg() for sending so there is no point
1541 * in allocating memory with usb_alloc_coherent().
1543 pcu
->urb_out_buf
= kmalloc(pcu
->max_out_size
, GFP_KERNEL
);
1544 if (!pcu
->urb_out_buf
) {
1545 dev_err(pcu
->dev
, "Failed to allocate memory for write buffer\n");
1547 goto err_free_in_urb
;
1550 pcu
->urb_ctrl_buf
= usb_alloc_coherent(pcu
->udev
, pcu
->max_ctrl_size
,
1551 GFP_KERNEL
, &pcu
->ctrl_dma
);
1552 if (!pcu
->urb_ctrl_buf
) {
1554 "Failed to allocate memory for read buffer\n");
1556 goto err_free_urb_out_buf
;
1559 pcu
->urb_ctrl
= usb_alloc_urb(0, GFP_KERNEL
);
1560 if (!pcu
->urb_ctrl
) {
1561 dev_err(pcu
->dev
, "Failed to allocate input URB\n");
1563 goto err_free_urb_ctrl_buf
;
1566 pcu
->urb_ctrl
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1567 pcu
->urb_ctrl
->transfer_dma
= pcu
->ctrl_dma
;
1569 usb_fill_int_urb(pcu
->urb_ctrl
, pcu
->udev
,
1570 usb_rcvintpipe(pcu
->udev
,
1571 pcu
->ep_ctrl
->bEndpointAddress
),
1572 pcu
->urb_ctrl_buf
, pcu
->max_ctrl_size
,
1573 ims_pcu_irq
, pcu
, pcu
->ep_ctrl
->bInterval
);
1577 err_free_urb_ctrl_buf
:
1578 usb_free_coherent(pcu
->udev
, pcu
->max_ctrl_size
,
1579 pcu
->urb_ctrl_buf
, pcu
->ctrl_dma
);
1580 err_free_urb_out_buf
:
1581 kfree(pcu
->urb_out_buf
);
1583 usb_free_urb(pcu
->urb_in
);
1584 err_free_urb_in_buf
:
1585 usb_free_coherent(pcu
->udev
, pcu
->max_in_size
,
1586 pcu
->urb_in_buf
, pcu
->read_dma
);
1590 static void ims_pcu_buffers_free(struct ims_pcu
*pcu
)
1592 usb_kill_urb(pcu
->urb_in
);
1593 usb_free_urb(pcu
->urb_in
);
1595 usb_free_coherent(pcu
->udev
, pcu
->max_out_size
,
1596 pcu
->urb_in_buf
, pcu
->read_dma
);
1598 kfree(pcu
->urb_out_buf
);
1600 usb_kill_urb(pcu
->urb_ctrl
);
1601 usb_free_urb(pcu
->urb_ctrl
);
1603 usb_free_coherent(pcu
->udev
, pcu
->max_ctrl_size
,
1604 pcu
->urb_ctrl_buf
, pcu
->ctrl_dma
);
1607 static const struct usb_cdc_union_desc
*
1608 ims_pcu_get_cdc_union_desc(struct usb_interface
*intf
)
1610 const void *buf
= intf
->altsetting
->extra
;
1611 size_t buflen
= intf
->altsetting
->extralen
;
1612 struct usb_cdc_union_desc
*union_desc
;
1615 dev_err(&intf
->dev
, "Missing descriptor data\n");
1620 dev_err(&intf
->dev
, "Zero length descriptor\n");
1624 while (buflen
>= sizeof(*union_desc
)) {
1625 union_desc
= (struct usb_cdc_union_desc
*)buf
;
1627 if (union_desc
->bLength
> buflen
) {
1628 dev_err(&intf
->dev
, "Too large descriptor\n");
1632 if (union_desc
->bDescriptorType
== USB_DT_CS_INTERFACE
&&
1633 union_desc
->bDescriptorSubType
== USB_CDC_UNION_TYPE
) {
1634 dev_dbg(&intf
->dev
, "Found union header\n");
1636 if (union_desc
->bLength
>= sizeof(*union_desc
))
1640 "Union descriptor too short (%d vs %zd)\n",
1641 union_desc
->bLength
, sizeof(*union_desc
));
1645 buflen
-= union_desc
->bLength
;
1646 buf
+= union_desc
->bLength
;
1649 dev_err(&intf
->dev
, "Missing CDC union descriptor\n");
1653 static int ims_pcu_parse_cdc_data(struct usb_interface
*intf
, struct ims_pcu
*pcu
)
1655 const struct usb_cdc_union_desc
*union_desc
;
1656 struct usb_host_interface
*alt
;
1658 union_desc
= ims_pcu_get_cdc_union_desc(intf
);
1662 pcu
->ctrl_intf
= usb_ifnum_to_if(pcu
->udev
,
1663 union_desc
->bMasterInterface0
);
1664 if (!pcu
->ctrl_intf
)
1667 alt
= pcu
->ctrl_intf
->cur_altsetting
;
1669 if (alt
->desc
.bNumEndpoints
< 1)
1672 pcu
->ep_ctrl
= &alt
->endpoint
[0].desc
;
1673 pcu
->max_ctrl_size
= usb_endpoint_maxp(pcu
->ep_ctrl
);
1675 pcu
->data_intf
= usb_ifnum_to_if(pcu
->udev
,
1676 union_desc
->bSlaveInterface0
);
1677 if (!pcu
->data_intf
)
1680 alt
= pcu
->data_intf
->cur_altsetting
;
1681 if (alt
->desc
.bNumEndpoints
!= 2) {
1683 "Incorrect number of endpoints on data interface (%d)\n",
1684 alt
->desc
.bNumEndpoints
);
1688 pcu
->ep_out
= &alt
->endpoint
[0].desc
;
1689 if (!usb_endpoint_is_bulk_out(pcu
->ep_out
)) {
1691 "First endpoint on data interface is not BULK OUT\n");
1695 pcu
->max_out_size
= usb_endpoint_maxp(pcu
->ep_out
);
1696 if (pcu
->max_out_size
< 8) {
1698 "Max OUT packet size is too small (%zd)\n",
1703 pcu
->ep_in
= &alt
->endpoint
[1].desc
;
1704 if (!usb_endpoint_is_bulk_in(pcu
->ep_in
)) {
1706 "Second endpoint on data interface is not BULK IN\n");
1710 pcu
->max_in_size
= usb_endpoint_maxp(pcu
->ep_in
);
1711 if (pcu
->max_in_size
< 8) {
1713 "Max IN packet size is too small (%zd)\n",
1721 static int ims_pcu_start_io(struct ims_pcu
*pcu
)
1725 error
= usb_submit_urb(pcu
->urb_ctrl
, GFP_KERNEL
);
1728 "Failed to start control IO - usb_submit_urb failed with result: %d\n",
1733 error
= usb_submit_urb(pcu
->urb_in
, GFP_KERNEL
);
1736 "Failed to start IO - usb_submit_urb failed with result: %d\n",
1738 usb_kill_urb(pcu
->urb_ctrl
);
1745 static void ims_pcu_stop_io(struct ims_pcu
*pcu
)
1747 usb_kill_urb(pcu
->urb_in
);
1748 usb_kill_urb(pcu
->urb_ctrl
);
1751 static int ims_pcu_line_setup(struct ims_pcu
*pcu
)
1753 struct usb_host_interface
*interface
= pcu
->ctrl_intf
->cur_altsetting
;
1754 struct usb_cdc_line_coding
*line
= (void *)pcu
->cmd_buf
;
1757 memset(line
, 0, sizeof(*line
));
1758 line
->dwDTERate
= cpu_to_le32(57600);
1759 line
->bDataBits
= 8;
1761 error
= usb_control_msg(pcu
->udev
, usb_sndctrlpipe(pcu
->udev
, 0),
1762 USB_CDC_REQ_SET_LINE_CODING
,
1763 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1764 0, interface
->desc
.bInterfaceNumber
,
1765 line
, sizeof(struct usb_cdc_line_coding
),
1768 dev_err(pcu
->dev
, "Failed to set line coding, error: %d\n",
1773 error
= usb_control_msg(pcu
->udev
, usb_sndctrlpipe(pcu
->udev
, 0),
1774 USB_CDC_REQ_SET_CONTROL_LINE_STATE
,
1775 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1776 0x03, interface
->desc
.bInterfaceNumber
,
1779 dev_err(pcu
->dev
, "Failed to set line state, error: %d\n",
1787 static int ims_pcu_get_device_info(struct ims_pcu
*pcu
)
1791 error
= ims_pcu_get_info(pcu
);
1795 error
= ims_pcu_execute_query(pcu
, GET_FW_VERSION
);
1798 "GET_FW_VERSION command failed, error: %d\n", error
);
1802 snprintf(pcu
->fw_version
, sizeof(pcu
->fw_version
),
1803 "%02d%02d%02d%02d.%c%c",
1804 pcu
->cmd_buf
[2], pcu
->cmd_buf
[3], pcu
->cmd_buf
[4], pcu
->cmd_buf
[5],
1805 pcu
->cmd_buf
[6], pcu
->cmd_buf
[7]);
1807 error
= ims_pcu_execute_query(pcu
, GET_BL_VERSION
);
1810 "GET_BL_VERSION command failed, error: %d\n", error
);
1814 snprintf(pcu
->bl_version
, sizeof(pcu
->bl_version
),
1815 "%02d%02d%02d%02d.%c%c",
1816 pcu
->cmd_buf
[2], pcu
->cmd_buf
[3], pcu
->cmd_buf
[4], pcu
->cmd_buf
[5],
1817 pcu
->cmd_buf
[6], pcu
->cmd_buf
[7]);
1819 error
= ims_pcu_execute_query(pcu
, RESET_REASON
);
1822 "RESET_REASON command failed, error: %d\n", error
);
1826 snprintf(pcu
->reset_reason
, sizeof(pcu
->reset_reason
),
1827 "%02x", pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
]);
1830 "P/N: %s, MD: %s, S/N: %s, FW: %s, BL: %s, RR: %s\n",
1832 pcu
->date_of_manufacturing
,
1841 static int ims_pcu_identify_type(struct ims_pcu
*pcu
, u8
*device_id
)
1845 error
= ims_pcu_execute_query(pcu
, GET_DEVICE_ID
);
1848 "GET_DEVICE_ID command failed, error: %d\n", error
);
1852 *device_id
= pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
];
1853 dev_dbg(pcu
->dev
, "Detected device ID: %d\n", *device_id
);
1858 static int ims_pcu_init_application_mode(struct ims_pcu
*pcu
)
1860 static atomic_t device_no
= ATOMIC_INIT(-1);
1862 const struct ims_pcu_device_info
*info
;
1865 error
= ims_pcu_get_device_info(pcu
);
1867 /* Device does not respond to basic queries, hopeless */
1871 error
= ims_pcu_identify_type(pcu
, &pcu
->device_id
);
1874 "Failed to identify device, error: %d\n", error
);
1876 * Do not signal error, but do not create input nor
1877 * backlight devices either, let userspace figure this
1878 * out (flash a new firmware?).
1883 if (pcu
->device_id
>= ARRAY_SIZE(ims_pcu_device_info
) ||
1884 !ims_pcu_device_info
[pcu
->device_id
].keymap
) {
1885 dev_err(pcu
->dev
, "Device ID %d is not valid\n", pcu
->device_id
);
1886 /* Same as above, punt to userspace */
1890 /* Device appears to be operable, complete initialization */
1891 pcu
->device_no
= atomic_inc_return(&device_no
);
1894 * PCU-B devices, both GEN_1 and GEN_2 do not have OFN sensor
1896 if (pcu
->device_id
!= IMS_PCU_PCU_B_DEVICE_ID
) {
1897 error
= sysfs_create_group(&pcu
->dev
->kobj
,
1898 &ims_pcu_ofn_attr_group
);
1903 error
= ims_pcu_setup_backlight(pcu
);
1907 info
= &ims_pcu_device_info
[pcu
->device_id
];
1908 error
= ims_pcu_setup_buttons(pcu
, info
->keymap
, info
->keymap_len
);
1910 goto err_destroy_backlight
;
1912 if (info
->has_gamepad
) {
1913 error
= ims_pcu_setup_gamepad(pcu
);
1915 goto err_destroy_buttons
;
1918 pcu
->setup_complete
= true;
1922 err_destroy_buttons
:
1923 ims_pcu_destroy_buttons(pcu
);
1924 err_destroy_backlight
:
1925 ims_pcu_destroy_backlight(pcu
);
1929 static void ims_pcu_destroy_application_mode(struct ims_pcu
*pcu
)
1931 if (pcu
->setup_complete
) {
1932 pcu
->setup_complete
= false;
1933 mb(); /* make sure flag setting is not reordered */
1936 ims_pcu_destroy_gamepad(pcu
);
1937 ims_pcu_destroy_buttons(pcu
);
1938 ims_pcu_destroy_backlight(pcu
);
1940 if (pcu
->device_id
!= IMS_PCU_PCU_B_DEVICE_ID
)
1941 sysfs_remove_group(&pcu
->dev
->kobj
,
1942 &ims_pcu_ofn_attr_group
);
1946 static int ims_pcu_init_bootloader_mode(struct ims_pcu
*pcu
)
1950 error
= ims_pcu_execute_bl_command(pcu
, QUERY_DEVICE
, NULL
, 0,
1951 IMS_PCU_CMD_RESPONSE_TIMEOUT
);
1953 dev_err(pcu
->dev
, "Bootloader does not respond, aborting\n");
1957 pcu
->fw_start_addr
=
1958 get_unaligned_le32(&pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
+ 11]);
1960 get_unaligned_le32(&pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
+ 15]);
1963 "Device is in bootloader mode (addr 0x%08x-0x%08x), requesting firmware\n",
1964 pcu
->fw_start_addr
, pcu
->fw_end_addr
);
1966 error
= request_firmware_nowait(THIS_MODULE
, true,
1967 IMS_PCU_FIRMWARE_NAME
,
1968 pcu
->dev
, GFP_KERNEL
, pcu
,
1969 ims_pcu_process_async_firmware
);
1971 /* This error is not fatal, let userspace have another chance */
1972 complete(&pcu
->async_firmware_done
);
1978 static void ims_pcu_destroy_bootloader_mode(struct ims_pcu
*pcu
)
1980 /* Make sure our initial firmware request has completed */
1981 wait_for_completion(&pcu
->async_firmware_done
);
1984 #define IMS_PCU_APPLICATION_MODE 0
1985 #define IMS_PCU_BOOTLOADER_MODE 1
1987 static struct usb_driver ims_pcu_driver
;
1989 static int ims_pcu_probe(struct usb_interface
*intf
,
1990 const struct usb_device_id
*id
)
1992 struct usb_device
*udev
= interface_to_usbdev(intf
);
1993 struct ims_pcu
*pcu
;
1996 pcu
= kzalloc(sizeof(struct ims_pcu
), GFP_KERNEL
);
2000 pcu
->dev
= &intf
->dev
;
2002 pcu
->bootloader_mode
= id
->driver_info
== IMS_PCU_BOOTLOADER_MODE
;
2003 mutex_init(&pcu
->cmd_mutex
);
2004 init_completion(&pcu
->cmd_done
);
2005 init_completion(&pcu
->async_firmware_done
);
2007 error
= ims_pcu_parse_cdc_data(intf
, pcu
);
2011 error
= usb_driver_claim_interface(&ims_pcu_driver
,
2012 pcu
->data_intf
, pcu
);
2015 "Unable to claim corresponding data interface: %d\n",
2020 usb_set_intfdata(pcu
->ctrl_intf
, pcu
);
2021 usb_set_intfdata(pcu
->data_intf
, pcu
);
2023 error
= ims_pcu_buffers_alloc(pcu
);
2025 goto err_unclaim_intf
;
2027 error
= ims_pcu_start_io(pcu
);
2029 goto err_free_buffers
;
2031 error
= ims_pcu_line_setup(pcu
);
2035 error
= sysfs_create_group(&intf
->dev
.kobj
, &ims_pcu_attr_group
);
2039 error
= pcu
->bootloader_mode
?
2040 ims_pcu_init_bootloader_mode(pcu
) :
2041 ims_pcu_init_application_mode(pcu
);
2043 goto err_remove_sysfs
;
2048 sysfs_remove_group(&intf
->dev
.kobj
, &ims_pcu_attr_group
);
2050 ims_pcu_stop_io(pcu
);
2052 ims_pcu_buffers_free(pcu
);
2054 usb_driver_release_interface(&ims_pcu_driver
, pcu
->data_intf
);
2060 static void ims_pcu_disconnect(struct usb_interface
*intf
)
2062 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
2063 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
2065 usb_set_intfdata(intf
, NULL
);
2068 * See if we are dealing with control or data interface. The cleanup
2069 * happens when we unbind primary (control) interface.
2071 if (alt
->desc
.bInterfaceClass
!= USB_CLASS_COMM
)
2074 sysfs_remove_group(&intf
->dev
.kobj
, &ims_pcu_attr_group
);
2076 ims_pcu_stop_io(pcu
);
2078 if (pcu
->bootloader_mode
)
2079 ims_pcu_destroy_bootloader_mode(pcu
);
2081 ims_pcu_destroy_application_mode(pcu
);
2083 ims_pcu_buffers_free(pcu
);
2088 static int ims_pcu_suspend(struct usb_interface
*intf
,
2089 pm_message_t message
)
2091 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
2092 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
2094 if (alt
->desc
.bInterfaceClass
== USB_CLASS_COMM
)
2095 ims_pcu_stop_io(pcu
);
2100 static int ims_pcu_resume(struct usb_interface
*intf
)
2102 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
2103 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
2106 if (alt
->desc
.bInterfaceClass
== USB_CLASS_COMM
) {
2107 retval
= ims_pcu_start_io(pcu
);
2109 retval
= ims_pcu_line_setup(pcu
);
2116 static const struct usb_device_id ims_pcu_id_table
[] = {
2118 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0082,
2120 USB_CDC_SUBCLASS_ACM
,
2121 USB_CDC_ACM_PROTO_AT_V25TER
),
2122 .driver_info
= IMS_PCU_APPLICATION_MODE
,
2125 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0083,
2127 USB_CDC_SUBCLASS_ACM
,
2128 USB_CDC_ACM_PROTO_AT_V25TER
),
2129 .driver_info
= IMS_PCU_BOOTLOADER_MODE
,
2134 static struct usb_driver ims_pcu_driver
= {
2136 .id_table
= ims_pcu_id_table
,
2137 .probe
= ims_pcu_probe
,
2138 .disconnect
= ims_pcu_disconnect
,
2140 .suspend
= ims_pcu_suspend
,
2141 .resume
= ims_pcu_resume
,
2142 .reset_resume
= ims_pcu_resume
,
2146 module_usb_driver(ims_pcu_driver
);
2148 MODULE_DESCRIPTION("IMS Passenger Control Unit driver");
2149 MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
2150 MODULE_LICENSE("GPL");