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_PCU_B_DEVICE_ID 5
56 #define IMS_PCU_BUF_SIZE 128
59 struct usb_device
*udev
;
60 struct device
*dev
; /* control interface's device, used for logging */
62 unsigned int device_no
;
66 char part_number
[IMS_PCU_PART_NUMBER_LEN
];
67 char serial_number
[IMS_PCU_SERIAL_NUMBER_LEN
];
68 char date_of_manufacturing
[IMS_PCU_DOM_LEN
];
69 char fw_version
[IMS_PCU_FW_VERSION_LEN
];
70 char bl_version
[IMS_PCU_BL_VERSION_LEN
];
71 char reset_reason
[IMS_PCU_BL_RESET_REASON_LEN
];
72 int update_firmware_status
;
77 struct usb_interface
*ctrl_intf
;
79 struct usb_endpoint_descriptor
*ep_ctrl
;
85 struct usb_interface
*data_intf
;
87 struct usb_endpoint_descriptor
*ep_in
;
93 struct usb_endpoint_descriptor
*ep_out
;
97 u8 read_buf
[IMS_PCU_BUF_SIZE
];
103 u8 cmd_buf
[IMS_PCU_BUF_SIZE
];
105 u8 expected_response
;
107 struct completion cmd_done
;
108 struct mutex cmd_mutex
;
112 struct completion async_firmware_done
;
114 struct ims_pcu_buttons buttons
;
115 struct ims_pcu_gamepad
*gamepad
;
116 struct ims_pcu_backlight backlight
;
118 bool setup_complete
; /* Input and LED devices have been created */
122 /*********************************************************************
123 * Buttons Input device support *
124 *********************************************************************/
126 static const unsigned short ims_pcu_keymap_1
[] = {
127 [1] = KEY_ATTENDANT_OFF
,
128 [2] = KEY_ATTENDANT_ON
,
129 [3] = KEY_LIGHTS_TOGGLE
,
131 [5] = KEY_VOLUMEDOWN
,
135 static const unsigned short ims_pcu_keymap_2
[] = {
137 [5] = KEY_VOLUMEDOWN
,
141 static const unsigned short ims_pcu_keymap_3
[] = {
143 [2] = KEY_ATTENDANT_TOGGLE
,
144 [3] = KEY_LIGHTS_TOGGLE
,
146 [5] = KEY_VOLUMEDOWN
,
147 [6] = KEY_DISPLAYTOGGLE
,
148 [18] = KEY_PLAYPAUSE
,
151 static const unsigned short ims_pcu_keymap_4
[] = {
152 [1] = KEY_ATTENDANT_OFF
,
153 [2] = KEY_ATTENDANT_ON
,
154 [3] = KEY_LIGHTS_TOGGLE
,
156 [5] = KEY_VOLUMEDOWN
,
158 [18] = KEY_PLAYPAUSE
,
161 static const unsigned short ims_pcu_keymap_5
[] = {
162 [1] = KEY_ATTENDANT_OFF
,
163 [2] = KEY_ATTENDANT_ON
,
164 [3] = KEY_LIGHTS_TOGGLE
,
167 struct ims_pcu_device_info
{
168 const unsigned short *keymap
;
173 #define IMS_PCU_DEVINFO(_n, _gamepad) \
175 .keymap = ims_pcu_keymap_##_n, \
176 .keymap_len = ARRAY_SIZE(ims_pcu_keymap_##_n), \
177 .has_gamepad = _gamepad, \
180 static const struct ims_pcu_device_info ims_pcu_device_info
[] = {
181 IMS_PCU_DEVINFO(1, true),
182 IMS_PCU_DEVINFO(2, true),
183 IMS_PCU_DEVINFO(3, true),
184 IMS_PCU_DEVINFO(4, true),
185 IMS_PCU_DEVINFO(5, false),
188 static void ims_pcu_buttons_report(struct ims_pcu
*pcu
, u32 data
)
190 struct ims_pcu_buttons
*buttons
= &pcu
->buttons
;
191 struct input_dev
*input
= buttons
->input
;
194 for (i
= 0; i
< 32; i
++) {
195 unsigned short keycode
= buttons
->keymap
[i
];
197 if (keycode
!= KEY_RESERVED
)
198 input_report_key(input
, keycode
, data
& (1UL << i
));
204 static int ims_pcu_setup_buttons(struct ims_pcu
*pcu
,
205 const unsigned short *keymap
,
208 struct ims_pcu_buttons
*buttons
= &pcu
->buttons
;
209 struct input_dev
*input
;
213 input
= input_allocate_device();
216 "Not enough memory for input input device\n");
220 snprintf(buttons
->name
, sizeof(buttons
->name
),
221 "IMS PCU#%d Button Interface", pcu
->device_no
);
223 usb_make_path(pcu
->udev
, buttons
->phys
, sizeof(buttons
->phys
));
224 strlcat(buttons
->phys
, "/input0", sizeof(buttons
->phys
));
226 memcpy(buttons
->keymap
, keymap
, sizeof(*keymap
) * keymap_len
);
228 input
->name
= buttons
->name
;
229 input
->phys
= buttons
->phys
;
230 usb_to_input_id(pcu
->udev
, &input
->id
);
231 input
->dev
.parent
= &pcu
->ctrl_intf
->dev
;
233 input
->keycode
= buttons
->keymap
;
234 input
->keycodemax
= ARRAY_SIZE(buttons
->keymap
);
235 input
->keycodesize
= sizeof(buttons
->keymap
[0]);
237 __set_bit(EV_KEY
, input
->evbit
);
238 for (i
= 0; i
< IMS_PCU_KEYMAP_LEN
; i
++)
239 __set_bit(buttons
->keymap
[i
], input
->keybit
);
240 __clear_bit(KEY_RESERVED
, input
->keybit
);
242 error
= input_register_device(input
);
245 "Failed to register buttons input device: %d\n",
247 input_free_device(input
);
251 buttons
->input
= input
;
255 static void ims_pcu_destroy_buttons(struct ims_pcu
*pcu
)
257 struct ims_pcu_buttons
*buttons
= &pcu
->buttons
;
259 input_unregister_device(buttons
->input
);
263 /*********************************************************************
264 * Gamepad Input device support *
265 *********************************************************************/
267 static void ims_pcu_gamepad_report(struct ims_pcu
*pcu
, u32 data
)
269 struct ims_pcu_gamepad
*gamepad
= pcu
->gamepad
;
270 struct input_dev
*input
= gamepad
->input
;
273 x
= !!(data
& (1 << 14)) - !!(data
& (1 << 13));
274 y
= !!(data
& (1 << 12)) - !!(data
& (1 << 11));
276 input_report_abs(input
, ABS_X
, x
);
277 input_report_abs(input
, ABS_Y
, y
);
279 input_report_key(input
, BTN_A
, data
& (1 << 7));
280 input_report_key(input
, BTN_B
, data
& (1 << 8));
281 input_report_key(input
, BTN_X
, data
& (1 << 9));
282 input_report_key(input
, BTN_Y
, data
& (1 << 10));
283 input_report_key(input
, BTN_START
, data
& (1 << 15));
284 input_report_key(input
, BTN_SELECT
, data
& (1 << 16));
289 static int ims_pcu_setup_gamepad(struct ims_pcu
*pcu
)
291 struct ims_pcu_gamepad
*gamepad
;
292 struct input_dev
*input
;
295 gamepad
= kzalloc(sizeof(struct ims_pcu_gamepad
), GFP_KERNEL
);
296 input
= input_allocate_device();
297 if (!gamepad
|| !input
) {
299 "Not enough memory for gamepad device\n");
304 gamepad
->input
= input
;
306 snprintf(gamepad
->name
, sizeof(gamepad
->name
),
307 "IMS PCU#%d Gamepad Interface", pcu
->device_no
);
309 usb_make_path(pcu
->udev
, gamepad
->phys
, sizeof(gamepad
->phys
));
310 strlcat(gamepad
->phys
, "/input1", sizeof(gamepad
->phys
));
312 input
->name
= gamepad
->name
;
313 input
->phys
= gamepad
->phys
;
314 usb_to_input_id(pcu
->udev
, &input
->id
);
315 input
->dev
.parent
= &pcu
->ctrl_intf
->dev
;
317 __set_bit(EV_KEY
, input
->evbit
);
318 __set_bit(BTN_A
, input
->keybit
);
319 __set_bit(BTN_B
, input
->keybit
);
320 __set_bit(BTN_X
, input
->keybit
);
321 __set_bit(BTN_Y
, input
->keybit
);
322 __set_bit(BTN_START
, input
->keybit
);
323 __set_bit(BTN_SELECT
, input
->keybit
);
325 __set_bit(EV_ABS
, input
->evbit
);
326 input_set_abs_params(input
, ABS_X
, -1, 1, 0, 0);
327 input_set_abs_params(input
, ABS_Y
, -1, 1, 0, 0);
329 error
= input_register_device(input
);
332 "Failed to register gamepad input device: %d\n",
337 pcu
->gamepad
= gamepad
;
341 input_free_device(input
);
346 static void ims_pcu_destroy_gamepad(struct ims_pcu
*pcu
)
348 struct ims_pcu_gamepad
*gamepad
= pcu
->gamepad
;
350 input_unregister_device(gamepad
->input
);
355 /*********************************************************************
356 * PCU Communication protocol handling *
357 *********************************************************************/
359 #define IMS_PCU_PROTOCOL_STX 0x02
360 #define IMS_PCU_PROTOCOL_ETX 0x03
361 #define IMS_PCU_PROTOCOL_DLE 0x10
364 #define IMS_PCU_CMD_STATUS 0xa0
365 #define IMS_PCU_CMD_PCU_RESET 0xa1
366 #define IMS_PCU_CMD_RESET_REASON 0xa2
367 #define IMS_PCU_CMD_SEND_BUTTONS 0xa3
368 #define IMS_PCU_CMD_JUMP_TO_BTLDR 0xa4
369 #define IMS_PCU_CMD_GET_INFO 0xa5
370 #define IMS_PCU_CMD_SET_BRIGHTNESS 0xa6
371 #define IMS_PCU_CMD_EEPROM 0xa7
372 #define IMS_PCU_CMD_GET_FW_VERSION 0xa8
373 #define IMS_PCU_CMD_GET_BL_VERSION 0xa9
374 #define IMS_PCU_CMD_SET_INFO 0xab
375 #define IMS_PCU_CMD_GET_BRIGHTNESS 0xac
376 #define IMS_PCU_CMD_GET_DEVICE_ID 0xae
377 #define IMS_PCU_CMD_SPECIAL_INFO 0xb0
378 #define IMS_PCU_CMD_BOOTLOADER 0xb1 /* Pass data to bootloader */
379 #define IMS_PCU_CMD_OFN_SET_CONFIG 0xb3
380 #define IMS_PCU_CMD_OFN_GET_CONFIG 0xb4
383 #define IMS_PCU_RSP_STATUS 0xc0
384 #define IMS_PCU_RSP_PCU_RESET 0 /* Originally 0xc1 */
385 #define IMS_PCU_RSP_RESET_REASON 0xc2
386 #define IMS_PCU_RSP_SEND_BUTTONS 0xc3
387 #define IMS_PCU_RSP_JUMP_TO_BTLDR 0 /* Originally 0xc4 */
388 #define IMS_PCU_RSP_GET_INFO 0xc5
389 #define IMS_PCU_RSP_SET_BRIGHTNESS 0xc6
390 #define IMS_PCU_RSP_EEPROM 0xc7
391 #define IMS_PCU_RSP_GET_FW_VERSION 0xc8
392 #define IMS_PCU_RSP_GET_BL_VERSION 0xc9
393 #define IMS_PCU_RSP_SET_INFO 0xcb
394 #define IMS_PCU_RSP_GET_BRIGHTNESS 0xcc
395 #define IMS_PCU_RSP_CMD_INVALID 0xcd
396 #define IMS_PCU_RSP_GET_DEVICE_ID 0xce
397 #define IMS_PCU_RSP_SPECIAL_INFO 0xd0
398 #define IMS_PCU_RSP_BOOTLOADER 0xd1 /* Bootloader response */
399 #define IMS_PCU_RSP_OFN_SET_CONFIG 0xd2
400 #define IMS_PCU_RSP_OFN_GET_CONFIG 0xd3
403 #define IMS_PCU_RSP_EVNT_BUTTONS 0xe0 /* Unsolicited, button state */
404 #define IMS_PCU_GAMEPAD_MASK 0x0001ff80UL /* Bits 7 through 16 */
407 #define IMS_PCU_MIN_PACKET_LEN 3
408 #define IMS_PCU_DATA_OFFSET 2
410 #define IMS_PCU_CMD_WRITE_TIMEOUT 100 /* msec */
411 #define IMS_PCU_CMD_RESPONSE_TIMEOUT 500 /* msec */
413 static void ims_pcu_report_events(struct ims_pcu
*pcu
)
415 u32 data
= get_unaligned_be32(&pcu
->read_buf
[3]);
417 ims_pcu_buttons_report(pcu
, data
& ~IMS_PCU_GAMEPAD_MASK
);
419 ims_pcu_gamepad_report(pcu
, data
);
422 static void ims_pcu_handle_response(struct ims_pcu
*pcu
)
424 switch (pcu
->read_buf
[0]) {
425 case IMS_PCU_RSP_EVNT_BUTTONS
:
426 if (likely(pcu
->setup_complete
))
427 ims_pcu_report_events(pcu
);
432 * See if we got command completion.
433 * If both the sequence and response code match save
434 * the data and signal completion.
436 if (pcu
->read_buf
[0] == pcu
->expected_response
&&
437 pcu
->read_buf
[1] == pcu
->ack_id
- 1) {
439 memcpy(pcu
->cmd_buf
, pcu
->read_buf
, pcu
->read_pos
);
440 pcu
->cmd_buf_len
= pcu
->read_pos
;
441 complete(&pcu
->cmd_done
);
447 static void ims_pcu_process_data(struct ims_pcu
*pcu
, struct urb
*urb
)
451 for (i
= 0; i
< urb
->actual_length
; i
++) {
452 u8 data
= pcu
->urb_in_buf
[i
];
454 /* Skip everything until we get Start Xmit */
455 if (!pcu
->have_stx
&& data
!= IMS_PCU_PROTOCOL_STX
)
459 pcu
->have_dle
= false;
460 pcu
->read_buf
[pcu
->read_pos
++] = data
;
461 pcu
->check_sum
+= data
;
466 case IMS_PCU_PROTOCOL_STX
:
469 "Unexpected STX at byte %d, discarding old data\n",
471 pcu
->have_stx
= true;
472 pcu
->have_dle
= false;
477 case IMS_PCU_PROTOCOL_DLE
:
478 pcu
->have_dle
= true;
481 case IMS_PCU_PROTOCOL_ETX
:
482 if (pcu
->read_pos
< IMS_PCU_MIN_PACKET_LEN
) {
484 "Short packet received (%d bytes), ignoring\n",
486 } else if (pcu
->check_sum
!= 0) {
488 "Invalid checksum in packet (%d bytes), ignoring\n",
491 ims_pcu_handle_response(pcu
);
494 pcu
->have_stx
= false;
495 pcu
->have_dle
= false;
500 pcu
->read_buf
[pcu
->read_pos
++] = data
;
501 pcu
->check_sum
+= data
;
507 static bool ims_pcu_byte_needs_escape(u8 byte
)
509 return byte
== IMS_PCU_PROTOCOL_STX
||
510 byte
== IMS_PCU_PROTOCOL_ETX
||
511 byte
== IMS_PCU_PROTOCOL_DLE
;
514 static int ims_pcu_send_cmd_chunk(struct ims_pcu
*pcu
,
515 u8 command
, int chunk
, int len
)
519 error
= usb_bulk_msg(pcu
->udev
,
520 usb_sndbulkpipe(pcu
->udev
,
521 pcu
->ep_out
->bEndpointAddress
),
522 pcu
->urb_out_buf
, len
,
523 NULL
, IMS_PCU_CMD_WRITE_TIMEOUT
);
526 "Sending 0x%02x command failed at chunk %d: %d\n",
527 command
, chunk
, error
);
534 static int ims_pcu_send_command(struct ims_pcu
*pcu
,
535 u8 command
, const u8
*data
, int len
)
545 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_STX
;
547 /* We know the command need not be escaped */
548 pcu
->urb_out_buf
[count
++] = command
;
551 ack_id
= pcu
->ack_id
++;
553 ack_id
= pcu
->ack_id
++;
555 if (ims_pcu_byte_needs_escape(ack_id
))
556 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_DLE
;
558 pcu
->urb_out_buf
[count
++] = ack_id
;
561 for (i
= 0; i
< len
; i
++) {
563 delta
= ims_pcu_byte_needs_escape(data
[i
]) ? 2 : 1;
564 if (count
+ delta
>= pcu
->max_out_size
) {
565 error
= ims_pcu_send_cmd_chunk(pcu
, command
,
574 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_DLE
;
576 pcu
->urb_out_buf
[count
++] = data
[i
];
582 delta
= ims_pcu_byte_needs_escape(csum
) ? 3 : 2;
583 if (count
+ delta
>= pcu
->max_out_size
) {
584 error
= ims_pcu_send_cmd_chunk(pcu
, command
, ++chunk
, count
);
592 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_DLE
;
594 pcu
->urb_out_buf
[count
++] = csum
;
595 pcu
->urb_out_buf
[count
++] = IMS_PCU_PROTOCOL_ETX
;
597 return ims_pcu_send_cmd_chunk(pcu
, command
, ++chunk
, count
);
600 static int __ims_pcu_execute_command(struct ims_pcu
*pcu
,
601 u8 command
, const void *data
, size_t len
,
602 u8 expected_response
, int response_time
)
606 pcu
->expected_response
= expected_response
;
607 init_completion(&pcu
->cmd_done
);
609 error
= ims_pcu_send_command(pcu
, command
, data
, len
);
613 if (expected_response
&&
614 !wait_for_completion_timeout(&pcu
->cmd_done
,
615 msecs_to_jiffies(response_time
))) {
616 dev_dbg(pcu
->dev
, "Command 0x%02x timed out\n", command
);
623 #define ims_pcu_execute_command(pcu, code, data, len) \
624 __ims_pcu_execute_command(pcu, \
625 IMS_PCU_CMD_##code, data, len, \
626 IMS_PCU_RSP_##code, \
627 IMS_PCU_CMD_RESPONSE_TIMEOUT)
629 #define ims_pcu_execute_query(pcu, code) \
630 ims_pcu_execute_command(pcu, code, NULL, 0)
632 /* Bootloader commands */
633 #define IMS_PCU_BL_CMD_QUERY_DEVICE 0xa1
634 #define IMS_PCU_BL_CMD_UNLOCK_CONFIG 0xa2
635 #define IMS_PCU_BL_CMD_ERASE_APP 0xa3
636 #define IMS_PCU_BL_CMD_PROGRAM_DEVICE 0xa4
637 #define IMS_PCU_BL_CMD_PROGRAM_COMPLETE 0xa5
638 #define IMS_PCU_BL_CMD_READ_APP 0xa6
639 #define IMS_PCU_BL_CMD_RESET_DEVICE 0xa7
640 #define IMS_PCU_BL_CMD_LAUNCH_APP 0xa8
642 /* Bootloader commands */
643 #define IMS_PCU_BL_RSP_QUERY_DEVICE 0xc1
644 #define IMS_PCU_BL_RSP_UNLOCK_CONFIG 0xc2
645 #define IMS_PCU_BL_RSP_ERASE_APP 0xc3
646 #define IMS_PCU_BL_RSP_PROGRAM_DEVICE 0xc4
647 #define IMS_PCU_BL_RSP_PROGRAM_COMPLETE 0xc5
648 #define IMS_PCU_BL_RSP_READ_APP 0xc6
649 #define IMS_PCU_BL_RSP_RESET_DEVICE 0 /* originally 0xa7 */
650 #define IMS_PCU_BL_RSP_LAUNCH_APP 0 /* originally 0xa8 */
652 #define IMS_PCU_BL_DATA_OFFSET 3
654 static int __ims_pcu_execute_bl_command(struct ims_pcu
*pcu
,
655 u8 command
, const void *data
, size_t len
,
656 u8 expected_response
, int response_time
)
660 pcu
->cmd_buf
[0] = command
;
662 memcpy(&pcu
->cmd_buf
[1], data
, len
);
664 error
= __ims_pcu_execute_command(pcu
,
665 IMS_PCU_CMD_BOOTLOADER
, pcu
->cmd_buf
, len
+ 1,
666 expected_response
? IMS_PCU_RSP_BOOTLOADER
: 0,
670 "Failure when sending 0x%02x command to bootloader, error: %d\n",
671 pcu
->cmd_buf
[0], error
);
675 if (expected_response
&& pcu
->cmd_buf
[2] != expected_response
) {
677 "Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n",
678 pcu
->cmd_buf
[2], expected_response
);
685 #define ims_pcu_execute_bl_command(pcu, code, data, len, timeout) \
686 __ims_pcu_execute_bl_command(pcu, \
687 IMS_PCU_BL_CMD_##code, data, len, \
688 IMS_PCU_BL_RSP_##code, timeout) \
690 #define IMS_PCU_INFO_PART_OFFSET 2
691 #define IMS_PCU_INFO_DOM_OFFSET 17
692 #define IMS_PCU_INFO_SERIAL_OFFSET 25
694 #define IMS_PCU_SET_INFO_SIZE 31
696 static int ims_pcu_get_info(struct ims_pcu
*pcu
)
700 error
= ims_pcu_execute_query(pcu
, GET_INFO
);
703 "GET_INFO command failed, error: %d\n", error
);
707 memcpy(pcu
->part_number
,
708 &pcu
->cmd_buf
[IMS_PCU_INFO_PART_OFFSET
],
709 sizeof(pcu
->part_number
));
710 memcpy(pcu
->date_of_manufacturing
,
711 &pcu
->cmd_buf
[IMS_PCU_INFO_DOM_OFFSET
],
712 sizeof(pcu
->date_of_manufacturing
));
713 memcpy(pcu
->serial_number
,
714 &pcu
->cmd_buf
[IMS_PCU_INFO_SERIAL_OFFSET
],
715 sizeof(pcu
->serial_number
));
720 static int ims_pcu_set_info(struct ims_pcu
*pcu
)
724 memcpy(&pcu
->cmd_buf
[IMS_PCU_INFO_PART_OFFSET
],
725 pcu
->part_number
, sizeof(pcu
->part_number
));
726 memcpy(&pcu
->cmd_buf
[IMS_PCU_INFO_DOM_OFFSET
],
727 pcu
->date_of_manufacturing
, sizeof(pcu
->date_of_manufacturing
));
728 memcpy(&pcu
->cmd_buf
[IMS_PCU_INFO_SERIAL_OFFSET
],
729 pcu
->serial_number
, sizeof(pcu
->serial_number
));
731 error
= ims_pcu_execute_command(pcu
, SET_INFO
,
732 &pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
],
733 IMS_PCU_SET_INFO_SIZE
);
736 "Failed to update device information, error: %d\n",
744 static int ims_pcu_switch_to_bootloader(struct ims_pcu
*pcu
)
748 /* Execute jump to the bootoloader */
749 error
= ims_pcu_execute_command(pcu
, JUMP_TO_BTLDR
, NULL
, 0);
752 "Failure when sending JUMP TO BOOLTLOADER command, error: %d\n",
760 /*********************************************************************
761 * Firmware Update handling *
762 *********************************************************************/
764 #define IMS_PCU_FIRMWARE_NAME "imspcu.fw"
766 struct ims_pcu_flash_fmt
{
772 static unsigned int ims_pcu_count_fw_records(const struct firmware
*fw
)
774 const struct ihex_binrec
*rec
= (const struct ihex_binrec
*)fw
->data
;
775 unsigned int count
= 0;
779 rec
= ihex_next_binrec(rec
);
785 static int ims_pcu_verify_block(struct ims_pcu
*pcu
,
786 u32 addr
, u8 len
, const u8
*data
)
788 struct ims_pcu_flash_fmt
*fragment
;
791 fragment
= (void *)&pcu
->cmd_buf
[1];
792 put_unaligned_le32(addr
, &fragment
->addr
);
795 error
= ims_pcu_execute_bl_command(pcu
, READ_APP
, NULL
, 5,
796 IMS_PCU_CMD_RESPONSE_TIMEOUT
);
799 "Failed to retrieve block at 0x%08x, len %d, error: %d\n",
804 fragment
= (void *)&pcu
->cmd_buf
[IMS_PCU_BL_DATA_OFFSET
];
805 if (get_unaligned_le32(&fragment
->addr
) != addr
||
806 fragment
->len
!= len
) {
808 "Wrong block when retrieving 0x%08x (0x%08x), len %d (%d)\n",
809 addr
, get_unaligned_le32(&fragment
->addr
),
814 if (memcmp(fragment
->data
, data
, len
)) {
816 "Mismatch in block at 0x%08x, len %d\n",
824 static int ims_pcu_flash_firmware(struct ims_pcu
*pcu
,
825 const struct firmware
*fw
,
826 unsigned int n_fw_records
)
828 const struct ihex_binrec
*rec
= (const struct ihex_binrec
*)fw
->data
;
829 struct ims_pcu_flash_fmt
*fragment
;
830 unsigned int count
= 0;
835 error
= ims_pcu_execute_bl_command(pcu
, ERASE_APP
, NULL
, 0, 2000);
838 "Failed to erase application image, error: %d\n",
845 * The firmware format is messed up for some reason.
846 * The address twice that of what is needed for some
847 * reason and we end up overwriting half of the data
848 * with the next record.
850 addr
= be32_to_cpu(rec
->addr
) / 2;
851 len
= be16_to_cpu(rec
->len
);
853 fragment
= (void *)&pcu
->cmd_buf
[1];
854 put_unaligned_le32(addr
, &fragment
->addr
);
856 memcpy(fragment
->data
, rec
->data
, len
);
858 error
= ims_pcu_execute_bl_command(pcu
, PROGRAM_DEVICE
,
860 IMS_PCU_CMD_RESPONSE_TIMEOUT
);
863 "Failed to write block at 0x%08x, len %d, error: %d\n",
868 if (addr
>= pcu
->fw_start_addr
&& addr
< pcu
->fw_end_addr
) {
869 error
= ims_pcu_verify_block(pcu
, addr
, len
, rec
->data
);
875 pcu
->update_firmware_status
= (count
* 100) / n_fw_records
;
877 rec
= ihex_next_binrec(rec
);
880 error
= ims_pcu_execute_bl_command(pcu
, PROGRAM_COMPLETE
,
884 "Failed to send PROGRAM_COMPLETE, error: %d\n",
890 static int ims_pcu_handle_firmware_update(struct ims_pcu
*pcu
,
891 const struct firmware
*fw
)
893 unsigned int n_fw_records
;
896 dev_info(pcu
->dev
, "Updating firmware %s, size: %zu\n",
897 IMS_PCU_FIRMWARE_NAME
, fw
->size
);
899 n_fw_records
= ims_pcu_count_fw_records(fw
);
901 retval
= ims_pcu_flash_firmware(pcu
, fw
, n_fw_records
);
905 retval
= ims_pcu_execute_bl_command(pcu
, LAUNCH_APP
, NULL
, 0, 0);
908 "Failed to start application image, error: %d\n",
912 pcu
->update_firmware_status
= retval
;
913 sysfs_notify(&pcu
->dev
->kobj
, NULL
, "update_firmware_status");
917 static void ims_pcu_process_async_firmware(const struct firmware
*fw
,
920 struct ims_pcu
*pcu
= context
;
924 dev_err(pcu
->dev
, "Failed to get firmware %s\n",
925 IMS_PCU_FIRMWARE_NAME
);
929 error
= ihex_validate_fw(fw
);
931 dev_err(pcu
->dev
, "Firmware %s is invalid\n",
932 IMS_PCU_FIRMWARE_NAME
);
936 mutex_lock(&pcu
->cmd_mutex
);
937 ims_pcu_handle_firmware_update(pcu
, fw
);
938 mutex_unlock(&pcu
->cmd_mutex
);
940 release_firmware(fw
);
943 complete(&pcu
->async_firmware_done
);
946 /*********************************************************************
947 * Backlight LED device support *
948 *********************************************************************/
950 #define IMS_PCU_MAX_BRIGHTNESS 31998
952 static void ims_pcu_backlight_work(struct work_struct
*work
)
954 struct ims_pcu_backlight
*backlight
=
955 container_of(work
, struct ims_pcu_backlight
, work
);
956 struct ims_pcu
*pcu
=
957 container_of(backlight
, struct ims_pcu
, backlight
);
958 int desired_brightness
= backlight
->desired_brightness
;
959 __le16 br_val
= cpu_to_le16(desired_brightness
);
962 mutex_lock(&pcu
->cmd_mutex
);
964 error
= ims_pcu_execute_command(pcu
, SET_BRIGHTNESS
,
965 &br_val
, sizeof(br_val
));
966 if (error
&& error
!= -ENODEV
)
968 "Failed to set desired brightness %u, error: %d\n",
969 desired_brightness
, error
);
971 mutex_unlock(&pcu
->cmd_mutex
);
974 static void ims_pcu_backlight_set_brightness(struct led_classdev
*cdev
,
975 enum led_brightness value
)
977 struct ims_pcu_backlight
*backlight
=
978 container_of(cdev
, struct ims_pcu_backlight
, cdev
);
980 backlight
->desired_brightness
= value
;
981 schedule_work(&backlight
->work
);
984 static enum led_brightness
985 ims_pcu_backlight_get_brightness(struct led_classdev
*cdev
)
987 struct ims_pcu_backlight
*backlight
=
988 container_of(cdev
, struct ims_pcu_backlight
, cdev
);
989 struct ims_pcu
*pcu
=
990 container_of(backlight
, struct ims_pcu
, backlight
);
994 mutex_lock(&pcu
->cmd_mutex
);
996 error
= ims_pcu_execute_query(pcu
, GET_BRIGHTNESS
);
999 "Failed to get current brightness, error: %d\n",
1001 /* Assume the LED is OFF */
1002 brightness
= LED_OFF
;
1005 get_unaligned_le16(&pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
]);
1008 mutex_unlock(&pcu
->cmd_mutex
);
1013 static int ims_pcu_setup_backlight(struct ims_pcu
*pcu
)
1015 struct ims_pcu_backlight
*backlight
= &pcu
->backlight
;
1018 INIT_WORK(&backlight
->work
, ims_pcu_backlight_work
);
1019 snprintf(backlight
->name
, sizeof(backlight
->name
),
1020 "pcu%d::kbd_backlight", pcu
->device_no
);
1022 backlight
->cdev
.name
= backlight
->name
;
1023 backlight
->cdev
.max_brightness
= IMS_PCU_MAX_BRIGHTNESS
;
1024 backlight
->cdev
.brightness_get
= ims_pcu_backlight_get_brightness
;
1025 backlight
->cdev
.brightness_set
= ims_pcu_backlight_set_brightness
;
1027 error
= led_classdev_register(pcu
->dev
, &backlight
->cdev
);
1030 "Failed to register backlight LED device, error: %d\n",
1038 static void ims_pcu_destroy_backlight(struct ims_pcu
*pcu
)
1040 struct ims_pcu_backlight
*backlight
= &pcu
->backlight
;
1042 led_classdev_unregister(&backlight
->cdev
);
1043 cancel_work_sync(&backlight
->work
);
1047 /*********************************************************************
1048 * Sysfs attributes handling *
1049 *********************************************************************/
1051 struct ims_pcu_attribute
{
1052 struct device_attribute dattr
;
1053 size_t field_offset
;
1057 static ssize_t
ims_pcu_attribute_show(struct device
*dev
,
1058 struct device_attribute
*dattr
,
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
;
1067 return scnprintf(buf
, PAGE_SIZE
, "%.*s\n", attr
->field_length
, field
);
1070 static ssize_t
ims_pcu_attribute_store(struct device
*dev
,
1071 struct device_attribute
*dattr
,
1072 const char *buf
, size_t count
)
1075 struct usb_interface
*intf
= to_usb_interface(dev
);
1076 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1077 struct ims_pcu_attribute
*attr
=
1078 container_of(dattr
, struct ims_pcu_attribute
, dattr
);
1079 char *field
= (char *)pcu
+ attr
->field_offset
;
1083 if (count
> attr
->field_length
)
1086 data_len
= strnlen(buf
, attr
->field_length
);
1087 if (data_len
> attr
->field_length
)
1090 error
= mutex_lock_interruptible(&pcu
->cmd_mutex
);
1094 memset(field
, 0, attr
->field_length
);
1095 memcpy(field
, buf
, data_len
);
1097 error
= ims_pcu_set_info(pcu
);
1100 * Even if update failed, let's fetch the info again as we just
1101 * clobbered one of the fields.
1103 ims_pcu_get_info(pcu
);
1105 mutex_unlock(&pcu
->cmd_mutex
);
1107 return error
< 0 ? error
: count
;
1110 #define IMS_PCU_ATTR(_field, _mode) \
1111 struct ims_pcu_attribute ims_pcu_attr_##_field = { \
1112 .dattr = __ATTR(_field, _mode, \
1113 ims_pcu_attribute_show, \
1114 ims_pcu_attribute_store), \
1115 .field_offset = offsetof(struct ims_pcu, _field), \
1116 .field_length = sizeof(((struct ims_pcu *)NULL)->_field), \
1119 #define IMS_PCU_RO_ATTR(_field) \
1120 IMS_PCU_ATTR(_field, S_IRUGO)
1121 #define IMS_PCU_RW_ATTR(_field) \
1122 IMS_PCU_ATTR(_field, S_IRUGO | S_IWUSR)
1124 static IMS_PCU_RW_ATTR(part_number
);
1125 static IMS_PCU_RW_ATTR(serial_number
);
1126 static IMS_PCU_RW_ATTR(date_of_manufacturing
);
1128 static IMS_PCU_RO_ATTR(fw_version
);
1129 static IMS_PCU_RO_ATTR(bl_version
);
1130 static IMS_PCU_RO_ATTR(reset_reason
);
1132 static ssize_t
ims_pcu_reset_device(struct device
*dev
,
1133 struct device_attribute
*dattr
,
1134 const char *buf
, size_t count
)
1136 static const u8 reset_byte
= 1;
1137 struct usb_interface
*intf
= to_usb_interface(dev
);
1138 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1142 error
= kstrtoint(buf
, 0, &value
);
1149 dev_info(pcu
->dev
, "Attempting to reset device\n");
1151 error
= ims_pcu_execute_command(pcu
, PCU_RESET
, &reset_byte
, 1);
1154 "Failed to reset device, error: %d\n",
1162 static DEVICE_ATTR(reset_device
, S_IWUSR
, NULL
, ims_pcu_reset_device
);
1164 static ssize_t
ims_pcu_update_firmware_store(struct device
*dev
,
1165 struct device_attribute
*dattr
,
1166 const char *buf
, size_t count
)
1168 struct usb_interface
*intf
= to_usb_interface(dev
);
1169 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1170 const struct firmware
*fw
= NULL
;
1174 error
= kstrtoint(buf
, 0, &value
);
1181 error
= mutex_lock_interruptible(&pcu
->cmd_mutex
);
1185 error
= request_ihex_firmware(&fw
, IMS_PCU_FIRMWARE_NAME
, pcu
->dev
);
1187 dev_err(pcu
->dev
, "Failed to request firmware %s, error: %d\n",
1188 IMS_PCU_FIRMWARE_NAME
, error
);
1193 * If we are already in bootloader mode we can proceed with
1194 * flashing the firmware.
1196 * If we are in application mode, then we need to switch into
1197 * bootloader mode, which will cause the device to disconnect
1198 * and reconnect as different device.
1200 if (pcu
->bootloader_mode
)
1201 error
= ims_pcu_handle_firmware_update(pcu
, fw
);
1203 error
= ims_pcu_switch_to_bootloader(pcu
);
1205 release_firmware(fw
);
1208 mutex_unlock(&pcu
->cmd_mutex
);
1209 return error
?: count
;
1212 static DEVICE_ATTR(update_firmware
, S_IWUSR
,
1213 NULL
, ims_pcu_update_firmware_store
);
1216 ims_pcu_update_firmware_status_show(struct device
*dev
,
1217 struct device_attribute
*dattr
,
1220 struct usb_interface
*intf
= to_usb_interface(dev
);
1221 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1223 return scnprintf(buf
, PAGE_SIZE
, "%d\n", pcu
->update_firmware_status
);
1226 static DEVICE_ATTR(update_firmware_status
, S_IRUGO
,
1227 ims_pcu_update_firmware_status_show
, NULL
);
1229 static struct attribute
*ims_pcu_attrs
[] = {
1230 &ims_pcu_attr_part_number
.dattr
.attr
,
1231 &ims_pcu_attr_serial_number
.dattr
.attr
,
1232 &ims_pcu_attr_date_of_manufacturing
.dattr
.attr
,
1233 &ims_pcu_attr_fw_version
.dattr
.attr
,
1234 &ims_pcu_attr_bl_version
.dattr
.attr
,
1235 &ims_pcu_attr_reset_reason
.dattr
.attr
,
1236 &dev_attr_reset_device
.attr
,
1237 &dev_attr_update_firmware
.attr
,
1238 &dev_attr_update_firmware_status
.attr
,
1242 static umode_t
ims_pcu_is_attr_visible(struct kobject
*kobj
,
1243 struct attribute
*attr
, int n
)
1245 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1246 struct usb_interface
*intf
= to_usb_interface(dev
);
1247 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1248 umode_t mode
= attr
->mode
;
1250 if (pcu
->bootloader_mode
) {
1251 if (attr
!= &dev_attr_update_firmware_status
.attr
&&
1252 attr
!= &dev_attr_update_firmware
.attr
&&
1253 attr
!= &dev_attr_reset_device
.attr
) {
1257 if (attr
== &dev_attr_update_firmware_status
.attr
)
1264 static struct attribute_group ims_pcu_attr_group
= {
1265 .is_visible
= ims_pcu_is_attr_visible
,
1266 .attrs
= ims_pcu_attrs
,
1269 /* Support for a separate OFN attribute group */
1271 #define OFN_REG_RESULT_OFFSET 2
1273 static int ims_pcu_read_ofn_config(struct ims_pcu
*pcu
, u8 addr
, u8
*data
)
1278 error
= ims_pcu_execute_command(pcu
, OFN_GET_CONFIG
,
1279 &addr
, sizeof(addr
));
1283 result
= (s16
)get_unaligned_le16(pcu
->cmd_buf
+ OFN_REG_RESULT_OFFSET
);
1287 /* We only need LSB */
1288 *data
= pcu
->cmd_buf
[OFN_REG_RESULT_OFFSET
];
1292 static int ims_pcu_write_ofn_config(struct ims_pcu
*pcu
, u8 addr
, u8 data
)
1294 u8 buffer
[] = { addr
, data
};
1298 error
= ims_pcu_execute_command(pcu
, OFN_SET_CONFIG
,
1299 &buffer
, sizeof(buffer
));
1303 result
= (s16
)get_unaligned_le16(pcu
->cmd_buf
+ OFN_REG_RESULT_OFFSET
);
1310 static ssize_t
ims_pcu_ofn_reg_data_show(struct device
*dev
,
1311 struct device_attribute
*dattr
,
1314 struct usb_interface
*intf
= to_usb_interface(dev
);
1315 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1319 mutex_lock(&pcu
->cmd_mutex
);
1320 error
= ims_pcu_read_ofn_config(pcu
, pcu
->ofn_reg_addr
, &data
);
1321 mutex_unlock(&pcu
->cmd_mutex
);
1326 return scnprintf(buf
, PAGE_SIZE
, "%x\n", data
);
1329 static ssize_t
ims_pcu_ofn_reg_data_store(struct device
*dev
,
1330 struct device_attribute
*dattr
,
1331 const char *buf
, size_t count
)
1333 struct usb_interface
*intf
= to_usb_interface(dev
);
1334 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1338 error
= kstrtou8(buf
, 0, &value
);
1342 mutex_lock(&pcu
->cmd_mutex
);
1343 error
= ims_pcu_write_ofn_config(pcu
, pcu
->ofn_reg_addr
, value
);
1344 mutex_unlock(&pcu
->cmd_mutex
);
1346 return error
?: count
;
1349 static DEVICE_ATTR(reg_data
, S_IRUGO
| S_IWUSR
,
1350 ims_pcu_ofn_reg_data_show
, ims_pcu_ofn_reg_data_store
);
1352 static ssize_t
ims_pcu_ofn_reg_addr_show(struct device
*dev
,
1353 struct device_attribute
*dattr
,
1356 struct usb_interface
*intf
= to_usb_interface(dev
);
1357 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1360 mutex_lock(&pcu
->cmd_mutex
);
1361 error
= scnprintf(buf
, PAGE_SIZE
, "%x\n", pcu
->ofn_reg_addr
);
1362 mutex_unlock(&pcu
->cmd_mutex
);
1367 static ssize_t
ims_pcu_ofn_reg_addr_store(struct device
*dev
,
1368 struct device_attribute
*dattr
,
1369 const char *buf
, size_t count
)
1371 struct usb_interface
*intf
= to_usb_interface(dev
);
1372 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1376 error
= kstrtou8(buf
, 0, &value
);
1380 mutex_lock(&pcu
->cmd_mutex
);
1381 pcu
->ofn_reg_addr
= value
;
1382 mutex_unlock(&pcu
->cmd_mutex
);
1387 static DEVICE_ATTR(reg_addr
, S_IRUGO
| S_IWUSR
,
1388 ims_pcu_ofn_reg_addr_show
, ims_pcu_ofn_reg_addr_store
);
1390 struct ims_pcu_ofn_bit_attribute
{
1391 struct device_attribute dattr
;
1396 static ssize_t
ims_pcu_ofn_bit_show(struct device
*dev
,
1397 struct device_attribute
*dattr
,
1400 struct usb_interface
*intf
= to_usb_interface(dev
);
1401 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1402 struct ims_pcu_ofn_bit_attribute
*attr
=
1403 container_of(dattr
, struct ims_pcu_ofn_bit_attribute
, dattr
);
1407 mutex_lock(&pcu
->cmd_mutex
);
1408 error
= ims_pcu_read_ofn_config(pcu
, attr
->addr
, &data
);
1409 mutex_unlock(&pcu
->cmd_mutex
);
1414 return scnprintf(buf
, PAGE_SIZE
, "%d\n", !!(data
& (1 << attr
->nr
)));
1417 static ssize_t
ims_pcu_ofn_bit_store(struct device
*dev
,
1418 struct device_attribute
*dattr
,
1419 const char *buf
, size_t count
)
1421 struct usb_interface
*intf
= to_usb_interface(dev
);
1422 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
1423 struct ims_pcu_ofn_bit_attribute
*attr
=
1424 container_of(dattr
, struct ims_pcu_ofn_bit_attribute
, dattr
);
1429 error
= kstrtoint(buf
, 0, &value
);
1436 mutex_lock(&pcu
->cmd_mutex
);
1438 error
= ims_pcu_read_ofn_config(pcu
, attr
->addr
, &data
);
1441 data
|= 1U << attr
->nr
;
1443 data
&= ~(1U << attr
->nr
);
1445 error
= ims_pcu_write_ofn_config(pcu
, attr
->addr
, data
);
1448 mutex_unlock(&pcu
->cmd_mutex
);
1450 return error
?: count
;
1453 #define IMS_PCU_OFN_BIT_ATTR(_field, _addr, _nr) \
1454 struct ims_pcu_ofn_bit_attribute ims_pcu_ofn_attr_##_field = { \
1455 .dattr = __ATTR(_field, S_IWUSR | S_IRUGO, \
1456 ims_pcu_ofn_bit_show, ims_pcu_ofn_bit_store), \
1461 static IMS_PCU_OFN_BIT_ATTR(engine_enable
, 0x60, 7);
1462 static IMS_PCU_OFN_BIT_ATTR(speed_enable
, 0x60, 6);
1463 static IMS_PCU_OFN_BIT_ATTR(assert_enable
, 0x60, 5);
1464 static IMS_PCU_OFN_BIT_ATTR(xyquant_enable
, 0x60, 4);
1465 static IMS_PCU_OFN_BIT_ATTR(xyscale_enable
, 0x60, 1);
1467 static IMS_PCU_OFN_BIT_ATTR(scale_x2
, 0x63, 6);
1468 static IMS_PCU_OFN_BIT_ATTR(scale_y2
, 0x63, 7);
1470 static struct attribute
*ims_pcu_ofn_attrs
[] = {
1471 &dev_attr_reg_data
.attr
,
1472 &dev_attr_reg_addr
.attr
,
1473 &ims_pcu_ofn_attr_engine_enable
.dattr
.attr
,
1474 &ims_pcu_ofn_attr_speed_enable
.dattr
.attr
,
1475 &ims_pcu_ofn_attr_assert_enable
.dattr
.attr
,
1476 &ims_pcu_ofn_attr_xyquant_enable
.dattr
.attr
,
1477 &ims_pcu_ofn_attr_xyscale_enable
.dattr
.attr
,
1478 &ims_pcu_ofn_attr_scale_x2
.dattr
.attr
,
1479 &ims_pcu_ofn_attr_scale_y2
.dattr
.attr
,
1483 static struct attribute_group ims_pcu_ofn_attr_group
= {
1485 .attrs
= ims_pcu_ofn_attrs
,
1488 static void ims_pcu_irq(struct urb
*urb
)
1490 struct ims_pcu
*pcu
= urb
->context
;
1493 status
= urb
->status
;
1502 /* this urb is terminated, clean up */
1503 dev_dbg(pcu
->dev
, "%s - urb shutting down with status: %d\n",
1507 dev_dbg(pcu
->dev
, "%s - nonzero urb status received: %d\n",
1512 dev_dbg(pcu
->dev
, "%s: received %d: %*ph\n", __func__
,
1513 urb
->actual_length
, urb
->actual_length
, pcu
->urb_in_buf
);
1515 if (urb
== pcu
->urb_in
)
1516 ims_pcu_process_data(pcu
, urb
);
1519 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1520 if (retval
&& retval
!= -ENODEV
)
1521 dev_err(pcu
->dev
, "%s - usb_submit_urb failed with result %d\n",
1525 static int ims_pcu_buffers_alloc(struct ims_pcu
*pcu
)
1529 pcu
->urb_in_buf
= usb_alloc_coherent(pcu
->udev
, pcu
->max_in_size
,
1530 GFP_KERNEL
, &pcu
->read_dma
);
1531 if (!pcu
->urb_in_buf
) {
1533 "Failed to allocate memory for read buffer\n");
1537 pcu
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
1539 dev_err(pcu
->dev
, "Failed to allocate input URB\n");
1541 goto err_free_urb_in_buf
;
1544 pcu
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1545 pcu
->urb_in
->transfer_dma
= pcu
->read_dma
;
1547 usb_fill_bulk_urb(pcu
->urb_in
, pcu
->udev
,
1548 usb_rcvbulkpipe(pcu
->udev
,
1549 pcu
->ep_in
->bEndpointAddress
),
1550 pcu
->urb_in_buf
, pcu
->max_in_size
,
1554 * We are using usb_bulk_msg() for sending so there is no point
1555 * in allocating memory with usb_alloc_coherent().
1557 pcu
->urb_out_buf
= kmalloc(pcu
->max_out_size
, GFP_KERNEL
);
1558 if (!pcu
->urb_out_buf
) {
1559 dev_err(pcu
->dev
, "Failed to allocate memory for write buffer\n");
1561 goto err_free_in_urb
;
1564 pcu
->urb_ctrl_buf
= usb_alloc_coherent(pcu
->udev
, pcu
->max_ctrl_size
,
1565 GFP_KERNEL
, &pcu
->ctrl_dma
);
1566 if (!pcu
->urb_ctrl_buf
) {
1568 "Failed to allocate memory for read buffer\n");
1570 goto err_free_urb_out_buf
;
1573 pcu
->urb_ctrl
= usb_alloc_urb(0, GFP_KERNEL
);
1574 if (!pcu
->urb_ctrl
) {
1575 dev_err(pcu
->dev
, "Failed to allocate input URB\n");
1577 goto err_free_urb_ctrl_buf
;
1580 pcu
->urb_ctrl
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1581 pcu
->urb_ctrl
->transfer_dma
= pcu
->ctrl_dma
;
1583 usb_fill_int_urb(pcu
->urb_ctrl
, pcu
->udev
,
1584 usb_rcvintpipe(pcu
->udev
,
1585 pcu
->ep_ctrl
->bEndpointAddress
),
1586 pcu
->urb_ctrl_buf
, pcu
->max_ctrl_size
,
1587 ims_pcu_irq
, pcu
, pcu
->ep_ctrl
->bInterval
);
1591 err_free_urb_ctrl_buf
:
1592 usb_free_coherent(pcu
->udev
, pcu
->max_ctrl_size
,
1593 pcu
->urb_ctrl_buf
, pcu
->ctrl_dma
);
1594 err_free_urb_out_buf
:
1595 kfree(pcu
->urb_out_buf
);
1597 usb_free_urb(pcu
->urb_in
);
1598 err_free_urb_in_buf
:
1599 usb_free_coherent(pcu
->udev
, pcu
->max_in_size
,
1600 pcu
->urb_in_buf
, pcu
->read_dma
);
1604 static void ims_pcu_buffers_free(struct ims_pcu
*pcu
)
1606 usb_kill_urb(pcu
->urb_in
);
1607 usb_free_urb(pcu
->urb_in
);
1609 usb_free_coherent(pcu
->udev
, pcu
->max_out_size
,
1610 pcu
->urb_in_buf
, pcu
->read_dma
);
1612 kfree(pcu
->urb_out_buf
);
1614 usb_kill_urb(pcu
->urb_ctrl
);
1615 usb_free_urb(pcu
->urb_ctrl
);
1617 usb_free_coherent(pcu
->udev
, pcu
->max_ctrl_size
,
1618 pcu
->urb_ctrl_buf
, pcu
->ctrl_dma
);
1621 static const struct usb_cdc_union_desc
*
1622 ims_pcu_get_cdc_union_desc(struct usb_interface
*intf
)
1624 const void *buf
= intf
->altsetting
->extra
;
1625 size_t buflen
= intf
->altsetting
->extralen
;
1626 struct usb_cdc_union_desc
*union_desc
;
1629 dev_err(&intf
->dev
, "Missing descriptor data\n");
1634 dev_err(&intf
->dev
, "Zero length descriptor\n");
1638 while (buflen
> 0) {
1639 union_desc
= (struct usb_cdc_union_desc
*)buf
;
1641 if (union_desc
->bDescriptorType
== USB_DT_CS_INTERFACE
&&
1642 union_desc
->bDescriptorSubType
== USB_CDC_UNION_TYPE
) {
1643 dev_dbg(&intf
->dev
, "Found union header\n");
1647 buflen
-= union_desc
->bLength
;
1648 buf
+= union_desc
->bLength
;
1651 dev_err(&intf
->dev
, "Missing CDC union descriptor\n");
1655 static int ims_pcu_parse_cdc_data(struct usb_interface
*intf
, struct ims_pcu
*pcu
)
1657 const struct usb_cdc_union_desc
*union_desc
;
1658 struct usb_host_interface
*alt
;
1660 union_desc
= ims_pcu_get_cdc_union_desc(intf
);
1664 pcu
->ctrl_intf
= usb_ifnum_to_if(pcu
->udev
,
1665 union_desc
->bMasterInterface0
);
1666 if (!pcu
->ctrl_intf
)
1669 alt
= pcu
->ctrl_intf
->cur_altsetting
;
1670 pcu
->ep_ctrl
= &alt
->endpoint
[0].desc
;
1671 pcu
->max_ctrl_size
= usb_endpoint_maxp(pcu
->ep_ctrl
);
1673 pcu
->data_intf
= usb_ifnum_to_if(pcu
->udev
,
1674 union_desc
->bSlaveInterface0
);
1675 if (!pcu
->data_intf
)
1678 alt
= pcu
->data_intf
->cur_altsetting
;
1679 if (alt
->desc
.bNumEndpoints
!= 2) {
1681 "Incorrect number of endpoints on data interface (%d)\n",
1682 alt
->desc
.bNumEndpoints
);
1686 pcu
->ep_out
= &alt
->endpoint
[0].desc
;
1687 if (!usb_endpoint_is_bulk_out(pcu
->ep_out
)) {
1689 "First endpoint on data interface is not BULK OUT\n");
1693 pcu
->max_out_size
= usb_endpoint_maxp(pcu
->ep_out
);
1694 if (pcu
->max_out_size
< 8) {
1696 "Max OUT packet size is too small (%zd)\n",
1701 pcu
->ep_in
= &alt
->endpoint
[1].desc
;
1702 if (!usb_endpoint_is_bulk_in(pcu
->ep_in
)) {
1704 "Second endpoint on data interface is not BULK IN\n");
1708 pcu
->max_in_size
= usb_endpoint_maxp(pcu
->ep_in
);
1709 if (pcu
->max_in_size
< 8) {
1711 "Max IN packet size is too small (%zd)\n",
1719 static int ims_pcu_start_io(struct ims_pcu
*pcu
)
1723 error
= usb_submit_urb(pcu
->urb_ctrl
, GFP_KERNEL
);
1726 "Failed to start control IO - usb_submit_urb failed with result: %d\n",
1731 error
= usb_submit_urb(pcu
->urb_in
, GFP_KERNEL
);
1734 "Failed to start IO - usb_submit_urb failed with result: %d\n",
1736 usb_kill_urb(pcu
->urb_ctrl
);
1743 static void ims_pcu_stop_io(struct ims_pcu
*pcu
)
1745 usb_kill_urb(pcu
->urb_in
);
1746 usb_kill_urb(pcu
->urb_ctrl
);
1749 static int ims_pcu_line_setup(struct ims_pcu
*pcu
)
1751 struct usb_host_interface
*interface
= pcu
->ctrl_intf
->cur_altsetting
;
1752 struct usb_cdc_line_coding
*line
= (void *)pcu
->cmd_buf
;
1755 memset(line
, 0, sizeof(*line
));
1756 line
->dwDTERate
= cpu_to_le32(57600);
1757 line
->bDataBits
= 8;
1759 error
= usb_control_msg(pcu
->udev
, usb_sndctrlpipe(pcu
->udev
, 0),
1760 USB_CDC_REQ_SET_LINE_CODING
,
1761 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1762 0, interface
->desc
.bInterfaceNumber
,
1763 line
, sizeof(struct usb_cdc_line_coding
),
1766 dev_err(pcu
->dev
, "Failed to set line coding, error: %d\n",
1771 error
= usb_control_msg(pcu
->udev
, usb_sndctrlpipe(pcu
->udev
, 0),
1772 USB_CDC_REQ_SET_CONTROL_LINE_STATE
,
1773 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1774 0x03, interface
->desc
.bInterfaceNumber
,
1777 dev_err(pcu
->dev
, "Failed to set line state, error: %d\n",
1785 static int ims_pcu_get_device_info(struct ims_pcu
*pcu
)
1789 error
= ims_pcu_get_info(pcu
);
1793 error
= ims_pcu_execute_query(pcu
, GET_FW_VERSION
);
1796 "GET_FW_VERSION command failed, error: %d\n", error
);
1800 snprintf(pcu
->fw_version
, sizeof(pcu
->fw_version
),
1801 "%02d%02d%02d%02d.%c%c",
1802 pcu
->cmd_buf
[2], pcu
->cmd_buf
[3], pcu
->cmd_buf
[4], pcu
->cmd_buf
[5],
1803 pcu
->cmd_buf
[6], pcu
->cmd_buf
[7]);
1805 error
= ims_pcu_execute_query(pcu
, GET_BL_VERSION
);
1808 "GET_BL_VERSION command failed, error: %d\n", error
);
1812 snprintf(pcu
->bl_version
, sizeof(pcu
->bl_version
),
1813 "%02d%02d%02d%02d.%c%c",
1814 pcu
->cmd_buf
[2], pcu
->cmd_buf
[3], pcu
->cmd_buf
[4], pcu
->cmd_buf
[5],
1815 pcu
->cmd_buf
[6], pcu
->cmd_buf
[7]);
1817 error
= ims_pcu_execute_query(pcu
, RESET_REASON
);
1820 "RESET_REASON command failed, error: %d\n", error
);
1824 snprintf(pcu
->reset_reason
, sizeof(pcu
->reset_reason
),
1825 "%02x", pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
]);
1828 "P/N: %s, MD: %s, S/N: %s, FW: %s, BL: %s, RR: %s\n",
1830 pcu
->date_of_manufacturing
,
1839 static int ims_pcu_identify_type(struct ims_pcu
*pcu
, u8
*device_id
)
1843 error
= ims_pcu_execute_query(pcu
, GET_DEVICE_ID
);
1846 "GET_DEVICE_ID command failed, error: %d\n", error
);
1850 *device_id
= pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
];
1851 dev_dbg(pcu
->dev
, "Detected device ID: %d\n", *device_id
);
1856 static int ims_pcu_init_application_mode(struct ims_pcu
*pcu
)
1858 static atomic_t device_no
= ATOMIC_INIT(-1);
1860 const struct ims_pcu_device_info
*info
;
1863 error
= ims_pcu_get_device_info(pcu
);
1865 /* Device does not respond to basic queries, hopeless */
1869 error
= ims_pcu_identify_type(pcu
, &pcu
->device_id
);
1872 "Failed to identify device, error: %d\n", error
);
1874 * Do not signal error, but do not create input nor
1875 * backlight devices either, let userspace figure this
1876 * out (flash a new firmware?).
1881 if (pcu
->device_id
>= ARRAY_SIZE(ims_pcu_device_info
) ||
1882 !ims_pcu_device_info
[pcu
->device_id
].keymap
) {
1883 dev_err(pcu
->dev
, "Device ID %d is not valid\n", pcu
->device_id
);
1884 /* Same as above, punt to userspace */
1888 /* Device appears to be operable, complete initialization */
1889 pcu
->device_no
= atomic_inc_return(&device_no
);
1892 * PCU-B devices, both GEN_1 and GEN_2 do not have OFN sensor
1894 if (pcu
->device_id
!= IMS_PCU_PCU_B_DEVICE_ID
) {
1895 error
= sysfs_create_group(&pcu
->dev
->kobj
,
1896 &ims_pcu_ofn_attr_group
);
1901 error
= ims_pcu_setup_backlight(pcu
);
1905 info
= &ims_pcu_device_info
[pcu
->device_id
];
1906 error
= ims_pcu_setup_buttons(pcu
, info
->keymap
, info
->keymap_len
);
1908 goto err_destroy_backlight
;
1910 if (info
->has_gamepad
) {
1911 error
= ims_pcu_setup_gamepad(pcu
);
1913 goto err_destroy_buttons
;
1916 pcu
->setup_complete
= true;
1920 err_destroy_buttons
:
1921 ims_pcu_destroy_buttons(pcu
);
1922 err_destroy_backlight
:
1923 ims_pcu_destroy_backlight(pcu
);
1927 static void ims_pcu_destroy_application_mode(struct ims_pcu
*pcu
)
1929 if (pcu
->setup_complete
) {
1930 pcu
->setup_complete
= false;
1931 mb(); /* make sure flag setting is not reordered */
1934 ims_pcu_destroy_gamepad(pcu
);
1935 ims_pcu_destroy_buttons(pcu
);
1936 ims_pcu_destroy_backlight(pcu
);
1938 if (pcu
->device_id
!= IMS_PCU_PCU_B_DEVICE_ID
)
1939 sysfs_remove_group(&pcu
->dev
->kobj
,
1940 &ims_pcu_ofn_attr_group
);
1944 static int ims_pcu_init_bootloader_mode(struct ims_pcu
*pcu
)
1948 error
= ims_pcu_execute_bl_command(pcu
, QUERY_DEVICE
, NULL
, 0,
1949 IMS_PCU_CMD_RESPONSE_TIMEOUT
);
1951 dev_err(pcu
->dev
, "Bootloader does not respond, aborting\n");
1955 pcu
->fw_start_addr
=
1956 get_unaligned_le32(&pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
+ 11]);
1958 get_unaligned_le32(&pcu
->cmd_buf
[IMS_PCU_DATA_OFFSET
+ 15]);
1961 "Device is in bootloader mode (addr 0x%08x-0x%08x), requesting firmware\n",
1962 pcu
->fw_start_addr
, pcu
->fw_end_addr
);
1964 error
= request_firmware_nowait(THIS_MODULE
, true,
1965 IMS_PCU_FIRMWARE_NAME
,
1966 pcu
->dev
, GFP_KERNEL
, pcu
,
1967 ims_pcu_process_async_firmware
);
1969 /* This error is not fatal, let userspace have another chance */
1970 complete(&pcu
->async_firmware_done
);
1976 static void ims_pcu_destroy_bootloader_mode(struct ims_pcu
*pcu
)
1978 /* Make sure our initial firmware request has completed */
1979 wait_for_completion(&pcu
->async_firmware_done
);
1982 #define IMS_PCU_APPLICATION_MODE 0
1983 #define IMS_PCU_BOOTLOADER_MODE 1
1985 static struct usb_driver ims_pcu_driver
;
1987 static int ims_pcu_probe(struct usb_interface
*intf
,
1988 const struct usb_device_id
*id
)
1990 struct usb_device
*udev
= interface_to_usbdev(intf
);
1991 struct ims_pcu
*pcu
;
1994 pcu
= kzalloc(sizeof(struct ims_pcu
), GFP_KERNEL
);
1998 pcu
->dev
= &intf
->dev
;
2000 pcu
->bootloader_mode
= id
->driver_info
== IMS_PCU_BOOTLOADER_MODE
;
2001 mutex_init(&pcu
->cmd_mutex
);
2002 init_completion(&pcu
->cmd_done
);
2003 init_completion(&pcu
->async_firmware_done
);
2005 error
= ims_pcu_parse_cdc_data(intf
, pcu
);
2009 error
= usb_driver_claim_interface(&ims_pcu_driver
,
2010 pcu
->data_intf
, pcu
);
2013 "Unable to claim corresponding data interface: %d\n",
2018 usb_set_intfdata(pcu
->ctrl_intf
, pcu
);
2019 usb_set_intfdata(pcu
->data_intf
, pcu
);
2021 error
= ims_pcu_buffers_alloc(pcu
);
2023 goto err_unclaim_intf
;
2025 error
= ims_pcu_start_io(pcu
);
2027 goto err_free_buffers
;
2029 error
= ims_pcu_line_setup(pcu
);
2033 error
= sysfs_create_group(&intf
->dev
.kobj
, &ims_pcu_attr_group
);
2037 error
= pcu
->bootloader_mode
?
2038 ims_pcu_init_bootloader_mode(pcu
) :
2039 ims_pcu_init_application_mode(pcu
);
2041 goto err_remove_sysfs
;
2046 sysfs_remove_group(&intf
->dev
.kobj
, &ims_pcu_attr_group
);
2048 ims_pcu_stop_io(pcu
);
2050 ims_pcu_buffers_free(pcu
);
2052 usb_driver_release_interface(&ims_pcu_driver
, pcu
->data_intf
);
2058 static void ims_pcu_disconnect(struct usb_interface
*intf
)
2060 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
2061 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
2063 usb_set_intfdata(intf
, NULL
);
2066 * See if we are dealing with control or data interface. The cleanup
2067 * happens when we unbind primary (control) interface.
2069 if (alt
->desc
.bInterfaceClass
!= USB_CLASS_COMM
)
2072 sysfs_remove_group(&intf
->dev
.kobj
, &ims_pcu_attr_group
);
2074 ims_pcu_stop_io(pcu
);
2076 if (pcu
->bootloader_mode
)
2077 ims_pcu_destroy_bootloader_mode(pcu
);
2079 ims_pcu_destroy_application_mode(pcu
);
2081 ims_pcu_buffers_free(pcu
);
2086 static int ims_pcu_suspend(struct usb_interface
*intf
,
2087 pm_message_t message
)
2089 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
2090 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
2092 if (alt
->desc
.bInterfaceClass
== USB_CLASS_COMM
)
2093 ims_pcu_stop_io(pcu
);
2098 static int ims_pcu_resume(struct usb_interface
*intf
)
2100 struct ims_pcu
*pcu
= usb_get_intfdata(intf
);
2101 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
2104 if (alt
->desc
.bInterfaceClass
== USB_CLASS_COMM
) {
2105 retval
= ims_pcu_start_io(pcu
);
2107 retval
= ims_pcu_line_setup(pcu
);
2114 static const struct usb_device_id ims_pcu_id_table
[] = {
2116 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0082,
2118 USB_CDC_SUBCLASS_ACM
,
2119 USB_CDC_ACM_PROTO_AT_V25TER
),
2120 .driver_info
= IMS_PCU_APPLICATION_MODE
,
2123 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0083,
2125 USB_CDC_SUBCLASS_ACM
,
2126 USB_CDC_ACM_PROTO_AT_V25TER
),
2127 .driver_info
= IMS_PCU_BOOTLOADER_MODE
,
2132 static struct usb_driver ims_pcu_driver
= {
2134 .id_table
= ims_pcu_id_table
,
2135 .probe
= ims_pcu_probe
,
2136 .disconnect
= ims_pcu_disconnect
,
2138 .suspend
= ims_pcu_suspend
,
2139 .resume
= ims_pcu_resume
,
2140 .reset_resume
= ims_pcu_resume
,
2144 module_usb_driver(ims_pcu_driver
);
2146 MODULE_DESCRIPTION("IMS Passenger Control Unit driver");
2147 MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
2148 MODULE_LICENSE("GPL");