1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for IMS Passenger Control Unit Devices 4 * 5 * Copyright (C) 2013 The IMS Company 6 */ 7 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 <linux/unaligned.h> 21 22 #define IMS_PCU_KEYMAP_LEN 32 23 24 struct ims_pcu_buttons { 25 struct input_dev *input; 26 char name[32]; 27 char phys[32]; 28 unsigned short keymap[IMS_PCU_KEYMAP_LEN]; 29 }; 30 31 struct ims_pcu_gamepad { 32 struct input_dev *input; 33 char name[32]; 34 char phys[32]; 35 }; 36 37 struct ims_pcu_backlight { 38 struct led_classdev cdev; 39 char name[32]; 40 }; 41 42 #define IMS_PCU_PART_NUMBER_LEN 15 43 #define IMS_PCU_SERIAL_NUMBER_LEN 8 44 #define IMS_PCU_DOM_LEN 8 45 #define IMS_PCU_FW_VERSION_LEN 16 46 #define IMS_PCU_BL_VERSION_LEN 16 47 #define IMS_PCU_BL_RESET_REASON_LEN (2 + 1) 48 49 #define IMS_PCU_PCU_B_DEVICE_ID 5 50 51 #define IMS_PCU_BUF_SIZE 128 52 53 struct ims_pcu { 54 struct usb_device *udev; 55 struct device *dev; /* control interface's device, used for logging */ 56 57 unsigned int device_no; 58 59 bool bootloader_mode; 60 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; 68 u8 device_id; 69 70 u8 ofn_reg_addr; 71 72 struct usb_interface *ctrl_intf; 73 74 struct usb_endpoint_descriptor *ep_ctrl; 75 struct urb *urb_ctrl; 76 u8 *urb_ctrl_buf; 77 dma_addr_t ctrl_dma; 78 size_t max_ctrl_size; 79 80 struct usb_interface *data_intf; 81 82 struct usb_endpoint_descriptor *ep_in; 83 struct urb *urb_in; 84 u8 *urb_in_buf; 85 dma_addr_t read_dma; 86 size_t max_in_size; 87 88 struct usb_endpoint_descriptor *ep_out; 89 u8 *urb_out_buf; 90 size_t max_out_size; 91 92 u8 read_buf[IMS_PCU_BUF_SIZE]; 93 u8 read_pos; 94 u8 check_sum; 95 bool have_stx; 96 bool have_dle; 97 98 u8 cmd_buf[IMS_PCU_BUF_SIZE]; 99 u8 ack_id; 100 u8 expected_response; 101 u8 cmd_buf_len; 102 struct completion cmd_done; 103 struct mutex cmd_mutex; 104 105 u32 fw_start_addr; 106 u32 fw_end_addr; 107 struct completion async_firmware_done; 108 109 struct ims_pcu_buttons buttons; 110 struct ims_pcu_gamepad *gamepad; 111 struct ims_pcu_backlight backlight; 112 113 bool setup_complete; /* Input and LED devices have been created */ 114 }; 115 116 117 /********************************************************************* 118 * Buttons Input device support * 119 *********************************************************************/ 120 121 static const unsigned short ims_pcu_keymap_1[] = { 122 [1] = KEY_ATTENDANT_OFF, 123 [2] = KEY_ATTENDANT_ON, 124 [3] = KEY_LIGHTS_TOGGLE, 125 [4] = KEY_VOLUMEUP, 126 [5] = KEY_VOLUMEDOWN, 127 [6] = KEY_INFO, 128 }; 129 130 static const unsigned short ims_pcu_keymap_2[] = { 131 [4] = KEY_VOLUMEUP, 132 [5] = KEY_VOLUMEDOWN, 133 [6] = KEY_INFO, 134 }; 135 136 static const unsigned short ims_pcu_keymap_3[] = { 137 [1] = KEY_HOMEPAGE, 138 [2] = KEY_ATTENDANT_TOGGLE, 139 [3] = KEY_LIGHTS_TOGGLE, 140 [4] = KEY_VOLUMEUP, 141 [5] = KEY_VOLUMEDOWN, 142 [6] = KEY_DISPLAYTOGGLE, 143 [18] = KEY_PLAYPAUSE, 144 }; 145 146 static const unsigned short ims_pcu_keymap_4[] = { 147 [1] = KEY_ATTENDANT_OFF, 148 [2] = KEY_ATTENDANT_ON, 149 [3] = KEY_LIGHTS_TOGGLE, 150 [4] = KEY_VOLUMEUP, 151 [5] = KEY_VOLUMEDOWN, 152 [6] = KEY_INFO, 153 [18] = KEY_PLAYPAUSE, 154 }; 155 156 static const unsigned short ims_pcu_keymap_5[] = { 157 [1] = KEY_ATTENDANT_OFF, 158 [2] = KEY_ATTENDANT_ON, 159 [3] = KEY_LIGHTS_TOGGLE, 160 }; 161 162 struct ims_pcu_device_info { 163 const unsigned short *keymap; 164 size_t keymap_len; 165 bool has_gamepad; 166 }; 167 168 #define IMS_PCU_DEVINFO(_n, _gamepad) \ 169 [_n] = { \ 170 .keymap = ims_pcu_keymap_##_n, \ 171 .keymap_len = ARRAY_SIZE(ims_pcu_keymap_##_n), \ 172 .has_gamepad = _gamepad, \ 173 } 174 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), 181 }; 182 183 static void ims_pcu_buttons_report(struct ims_pcu *pcu, u32 data) 184 { 185 struct ims_pcu_buttons *buttons = &pcu->buttons; 186 struct input_dev *input = buttons->input; 187 int i; 188 189 for (i = 0; i < 32; i++) { 190 unsigned short keycode = buttons->keymap[i]; 191 192 if (keycode != KEY_RESERVED) 193 input_report_key(input, keycode, data & (1UL << i)); 194 } 195 196 input_sync(input); 197 } 198 199 static int ims_pcu_setup_buttons(struct ims_pcu *pcu, 200 const unsigned short *keymap, 201 size_t keymap_len) 202 { 203 struct ims_pcu_buttons *buttons = &pcu->buttons; 204 struct input_dev *input; 205 int i; 206 int error; 207 208 input = input_allocate_device(); 209 if (!input) { 210 dev_err(pcu->dev, "Not enough memory for input device\n"); 211 return -ENOMEM; 212 } 213 214 snprintf(buttons->name, sizeof(buttons->name), 215 "IMS PCU#%d Button Interface", pcu->device_no); 216 217 usb_make_path(pcu->udev, buttons->phys, sizeof(buttons->phys)); 218 strlcat(buttons->phys, "/input0", sizeof(buttons->phys)); 219 220 memcpy(buttons->keymap, keymap, sizeof(*keymap) * keymap_len); 221 222 input->name = buttons->name; 223 input->phys = buttons->phys; 224 usb_to_input_id(pcu->udev, &input->id); 225 input->dev.parent = &pcu->ctrl_intf->dev; 226 227 input->keycode = buttons->keymap; 228 input->keycodemax = ARRAY_SIZE(buttons->keymap); 229 input->keycodesize = sizeof(buttons->keymap[0]); 230 231 __set_bit(EV_KEY, input->evbit); 232 for (i = 0; i < IMS_PCU_KEYMAP_LEN; i++) 233 __set_bit(buttons->keymap[i], input->keybit); 234 __clear_bit(KEY_RESERVED, input->keybit); 235 236 error = input_register_device(input); 237 if (error) { 238 dev_err(pcu->dev, 239 "Failed to register buttons input device: %d\n", 240 error); 241 input_free_device(input); 242 return error; 243 } 244 245 buttons->input = input; 246 return 0; 247 } 248 249 static void ims_pcu_destroy_buttons(struct ims_pcu *pcu) 250 { 251 struct ims_pcu_buttons *buttons = &pcu->buttons; 252 253 input_unregister_device(buttons->input); 254 } 255 256 257 /********************************************************************* 258 * Gamepad Input device support * 259 *********************************************************************/ 260 261 static void ims_pcu_gamepad_report(struct ims_pcu *pcu, u32 data) 262 { 263 struct ims_pcu_gamepad *gamepad = pcu->gamepad; 264 struct input_dev *input = gamepad->input; 265 int x, y; 266 267 x = !!(data & (1 << 14)) - !!(data & (1 << 13)); 268 y = !!(data & (1 << 12)) - !!(data & (1 << 11)); 269 270 input_report_abs(input, ABS_X, x); 271 input_report_abs(input, ABS_Y, y); 272 273 input_report_key(input, BTN_A, data & (1 << 7)); 274 input_report_key(input, BTN_B, data & (1 << 8)); 275 input_report_key(input, BTN_X, data & (1 << 9)); 276 input_report_key(input, BTN_Y, data & (1 << 10)); 277 input_report_key(input, BTN_START, data & (1 << 15)); 278 input_report_key(input, BTN_SELECT, data & (1 << 16)); 279 280 input_sync(input); 281 } 282 283 static int ims_pcu_setup_gamepad(struct ims_pcu *pcu) 284 { 285 struct ims_pcu_gamepad *gamepad; 286 struct input_dev *input; 287 int error; 288 289 gamepad = kzalloc(sizeof(*gamepad), GFP_KERNEL); 290 input = input_allocate_device(); 291 if (!gamepad || !input) { 292 dev_err(pcu->dev, 293 "Not enough memory for gamepad device\n"); 294 error = -ENOMEM; 295 goto err_free_mem; 296 } 297 298 gamepad->input = input; 299 300 snprintf(gamepad->name, sizeof(gamepad->name), 301 "IMS PCU#%d Gamepad Interface", pcu->device_no); 302 303 usb_make_path(pcu->udev, gamepad->phys, sizeof(gamepad->phys)); 304 strlcat(gamepad->phys, "/input1", sizeof(gamepad->phys)); 305 306 input->name = gamepad->name; 307 input->phys = gamepad->phys; 308 usb_to_input_id(pcu->udev, &input->id); 309 input->dev.parent = &pcu->ctrl_intf->dev; 310 311 __set_bit(EV_KEY, input->evbit); 312 __set_bit(BTN_A, input->keybit); 313 __set_bit(BTN_B, input->keybit); 314 __set_bit(BTN_X, input->keybit); 315 __set_bit(BTN_Y, input->keybit); 316 __set_bit(BTN_START, input->keybit); 317 __set_bit(BTN_SELECT, input->keybit); 318 319 __set_bit(EV_ABS, input->evbit); 320 input_set_abs_params(input, ABS_X, -1, 1, 0, 0); 321 input_set_abs_params(input, ABS_Y, -1, 1, 0, 0); 322 323 error = input_register_device(input); 324 if (error) { 325 dev_err(pcu->dev, 326 "Failed to register gamepad input device: %d\n", 327 error); 328 goto err_free_mem; 329 } 330 331 pcu->gamepad = gamepad; 332 return 0; 333 334 err_free_mem: 335 input_free_device(input); 336 kfree(gamepad); 337 return error; 338 } 339 340 static void ims_pcu_destroy_gamepad(struct ims_pcu *pcu) 341 { 342 struct ims_pcu_gamepad *gamepad = pcu->gamepad; 343 344 input_unregister_device(gamepad->input); 345 kfree(gamepad); 346 } 347 348 349 /********************************************************************* 350 * PCU Communication protocol handling * 351 *********************************************************************/ 352 353 #define IMS_PCU_PROTOCOL_STX 0x02 354 #define IMS_PCU_PROTOCOL_ETX 0x03 355 #define IMS_PCU_PROTOCOL_DLE 0x10 356 357 /* PCU commands */ 358 #define IMS_PCU_CMD_STATUS 0xa0 359 #define IMS_PCU_CMD_PCU_RESET 0xa1 360 #define IMS_PCU_CMD_RESET_REASON 0xa2 361 #define IMS_PCU_CMD_SEND_BUTTONS 0xa3 362 #define IMS_PCU_CMD_JUMP_TO_BTLDR 0xa4 363 #define IMS_PCU_CMD_GET_INFO 0xa5 364 #define IMS_PCU_CMD_SET_BRIGHTNESS 0xa6 365 #define IMS_PCU_CMD_EEPROM 0xa7 366 #define IMS_PCU_CMD_GET_FW_VERSION 0xa8 367 #define IMS_PCU_CMD_GET_BL_VERSION 0xa9 368 #define IMS_PCU_CMD_SET_INFO 0xab 369 #define IMS_PCU_CMD_GET_BRIGHTNESS 0xac 370 #define IMS_PCU_CMD_GET_DEVICE_ID 0xae 371 #define IMS_PCU_CMD_SPECIAL_INFO 0xb0 372 #define IMS_PCU_CMD_BOOTLOADER 0xb1 /* Pass data to bootloader */ 373 #define IMS_PCU_CMD_OFN_SET_CONFIG 0xb3 374 #define IMS_PCU_CMD_OFN_GET_CONFIG 0xb4 375 376 /* PCU responses */ 377 #define IMS_PCU_RSP_STATUS 0xc0 378 #define IMS_PCU_RSP_PCU_RESET 0 /* Originally 0xc1 */ 379 #define IMS_PCU_RSP_RESET_REASON 0xc2 380 #define IMS_PCU_RSP_SEND_BUTTONS 0xc3 381 #define IMS_PCU_RSP_JUMP_TO_BTLDR 0 /* Originally 0xc4 */ 382 #define IMS_PCU_RSP_GET_INFO 0xc5 383 #define IMS_PCU_RSP_SET_BRIGHTNESS 0xc6 384 #define IMS_PCU_RSP_EEPROM 0xc7 385 #define IMS_PCU_RSP_GET_FW_VERSION 0xc8 386 #define IMS_PCU_RSP_GET_BL_VERSION 0xc9 387 #define IMS_PCU_RSP_SET_INFO 0xcb 388 #define IMS_PCU_RSP_GET_BRIGHTNESS 0xcc 389 #define IMS_PCU_RSP_CMD_INVALID 0xcd 390 #define IMS_PCU_RSP_GET_DEVICE_ID 0xce 391 #define IMS_PCU_RSP_SPECIAL_INFO 0xd0 392 #define IMS_PCU_RSP_BOOTLOADER 0xd1 /* Bootloader response */ 393 #define IMS_PCU_RSP_OFN_SET_CONFIG 0xd2 394 #define IMS_PCU_RSP_OFN_GET_CONFIG 0xd3 395 396 397 #define IMS_PCU_RSP_EVNT_BUTTONS 0xe0 /* Unsolicited, button state */ 398 #define IMS_PCU_GAMEPAD_MASK 0x0001ff80UL /* Bits 7 through 16 */ 399 400 401 #define IMS_PCU_MIN_PACKET_LEN 3 402 #define IMS_PCU_DATA_OFFSET 2 403 404 #define IMS_PCU_CMD_WRITE_TIMEOUT 100 /* msec */ 405 #define IMS_PCU_CMD_RESPONSE_TIMEOUT 500 /* msec */ 406 407 static void ims_pcu_report_events(struct ims_pcu *pcu) 408 { 409 u32 data = get_unaligned_be32(&pcu->read_buf[3]); 410 411 ims_pcu_buttons_report(pcu, data & ~IMS_PCU_GAMEPAD_MASK); 412 if (pcu->gamepad) 413 ims_pcu_gamepad_report(pcu, data); 414 } 415 416 static void ims_pcu_handle_response(struct ims_pcu *pcu) 417 { 418 switch (pcu->read_buf[0]) { 419 case IMS_PCU_RSP_EVNT_BUTTONS: 420 if (likely(pcu->setup_complete)) 421 ims_pcu_report_events(pcu); 422 break; 423 424 default: 425 /* 426 * See if we got command completion. 427 * If both the sequence and response code match save 428 * the data and signal completion. 429 */ 430 if (pcu->read_buf[0] == pcu->expected_response && 431 pcu->read_buf[1] == pcu->ack_id - 1) { 432 433 memcpy(pcu->cmd_buf, pcu->read_buf, pcu->read_pos); 434 pcu->cmd_buf_len = pcu->read_pos; 435 complete(&pcu->cmd_done); 436 } 437 break; 438 } 439 } 440 441 static void ims_pcu_process_data(struct ims_pcu *pcu, struct urb *urb) 442 { 443 int i; 444 445 for (i = 0; i < urb->actual_length; i++) { 446 u8 data = pcu->urb_in_buf[i]; 447 448 /* Skip everything until we get Start Xmit */ 449 if (!pcu->have_stx && data != IMS_PCU_PROTOCOL_STX) 450 continue; 451 452 if (pcu->have_dle) { 453 pcu->have_dle = false; 454 pcu->read_buf[pcu->read_pos++] = data; 455 pcu->check_sum += data; 456 continue; 457 } 458 459 switch (data) { 460 case IMS_PCU_PROTOCOL_STX: 461 if (pcu->have_stx) 462 dev_warn(pcu->dev, 463 "Unexpected STX at byte %d, discarding old data\n", 464 pcu->read_pos); 465 pcu->have_stx = true; 466 pcu->have_dle = false; 467 pcu->read_pos = 0; 468 pcu->check_sum = 0; 469 break; 470 471 case IMS_PCU_PROTOCOL_DLE: 472 pcu->have_dle = true; 473 break; 474 475 case IMS_PCU_PROTOCOL_ETX: 476 if (pcu->read_pos < IMS_PCU_MIN_PACKET_LEN) { 477 dev_warn(pcu->dev, 478 "Short packet received (%d bytes), ignoring\n", 479 pcu->read_pos); 480 } else if (pcu->check_sum != 0) { 481 dev_warn(pcu->dev, 482 "Invalid checksum in packet (%d bytes), ignoring\n", 483 pcu->read_pos); 484 } else { 485 ims_pcu_handle_response(pcu); 486 } 487 488 pcu->have_stx = false; 489 pcu->have_dle = false; 490 pcu->read_pos = 0; 491 break; 492 493 default: 494 pcu->read_buf[pcu->read_pos++] = data; 495 pcu->check_sum += data; 496 break; 497 } 498 } 499 } 500 501 static bool ims_pcu_byte_needs_escape(u8 byte) 502 { 503 return byte == IMS_PCU_PROTOCOL_STX || 504 byte == IMS_PCU_PROTOCOL_ETX || 505 byte == IMS_PCU_PROTOCOL_DLE; 506 } 507 508 static int ims_pcu_send_cmd_chunk(struct ims_pcu *pcu, 509 u8 command, int chunk, int len) 510 { 511 int error; 512 513 error = usb_bulk_msg(pcu->udev, 514 usb_sndbulkpipe(pcu->udev, 515 pcu->ep_out->bEndpointAddress), 516 pcu->urb_out_buf, len, 517 NULL, IMS_PCU_CMD_WRITE_TIMEOUT); 518 if (error < 0) { 519 dev_dbg(pcu->dev, 520 "Sending 0x%02x command failed at chunk %d: %d\n", 521 command, chunk, error); 522 return error; 523 } 524 525 return 0; 526 } 527 528 static int ims_pcu_send_command(struct ims_pcu *pcu, 529 u8 command, const u8 *data, int len) 530 { 531 int count = 0; 532 int chunk = 0; 533 int delta; 534 int i; 535 int error; 536 u8 csum = 0; 537 u8 ack_id; 538 539 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_STX; 540 541 /* We know the command need not be escaped */ 542 pcu->urb_out_buf[count++] = command; 543 csum += command; 544 545 ack_id = pcu->ack_id++; 546 if (ack_id == 0xff) 547 ack_id = pcu->ack_id++; 548 549 if (ims_pcu_byte_needs_escape(ack_id)) 550 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE; 551 552 pcu->urb_out_buf[count++] = ack_id; 553 csum += ack_id; 554 555 for (i = 0; i < len; i++) { 556 557 delta = ims_pcu_byte_needs_escape(data[i]) ? 2 : 1; 558 if (count + delta >= pcu->max_out_size) { 559 error = ims_pcu_send_cmd_chunk(pcu, command, 560 ++chunk, count); 561 if (error) 562 return error; 563 564 count = 0; 565 } 566 567 if (delta == 2) 568 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE; 569 570 pcu->urb_out_buf[count++] = data[i]; 571 csum += data[i]; 572 } 573 574 csum = 1 + ~csum; 575 576 delta = ims_pcu_byte_needs_escape(csum) ? 3 : 2; 577 if (count + delta >= pcu->max_out_size) { 578 error = ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count); 579 if (error) 580 return error; 581 582 count = 0; 583 } 584 585 if (delta == 3) 586 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_DLE; 587 588 pcu->urb_out_buf[count++] = csum; 589 pcu->urb_out_buf[count++] = IMS_PCU_PROTOCOL_ETX; 590 591 return ims_pcu_send_cmd_chunk(pcu, command, ++chunk, count); 592 } 593 594 static int __ims_pcu_execute_command(struct ims_pcu *pcu, 595 u8 command, const void *data, size_t len, 596 u8 expected_response, int response_time) 597 { 598 int error; 599 600 pcu->expected_response = expected_response; 601 init_completion(&pcu->cmd_done); 602 603 error = ims_pcu_send_command(pcu, command, data, len); 604 if (error) 605 return error; 606 607 if (expected_response && 608 !wait_for_completion_timeout(&pcu->cmd_done, 609 msecs_to_jiffies(response_time))) { 610 dev_dbg(pcu->dev, "Command 0x%02x timed out\n", command); 611 return -ETIMEDOUT; 612 } 613 614 return 0; 615 } 616 617 #define ims_pcu_execute_command(pcu, code, data, len) \ 618 __ims_pcu_execute_command(pcu, \ 619 IMS_PCU_CMD_##code, data, len, \ 620 IMS_PCU_RSP_##code, \ 621 IMS_PCU_CMD_RESPONSE_TIMEOUT) 622 623 #define ims_pcu_execute_query(pcu, code) \ 624 ims_pcu_execute_command(pcu, code, NULL, 0) 625 626 /* Bootloader commands */ 627 #define IMS_PCU_BL_CMD_QUERY_DEVICE 0xa1 628 #define IMS_PCU_BL_CMD_UNLOCK_CONFIG 0xa2 629 #define IMS_PCU_BL_CMD_ERASE_APP 0xa3 630 #define IMS_PCU_BL_CMD_PROGRAM_DEVICE 0xa4 631 #define IMS_PCU_BL_CMD_PROGRAM_COMPLETE 0xa5 632 #define IMS_PCU_BL_CMD_READ_APP 0xa6 633 #define IMS_PCU_BL_CMD_RESET_DEVICE 0xa7 634 #define IMS_PCU_BL_CMD_LAUNCH_APP 0xa8 635 636 /* Bootloader commands */ 637 #define IMS_PCU_BL_RSP_QUERY_DEVICE 0xc1 638 #define IMS_PCU_BL_RSP_UNLOCK_CONFIG 0xc2 639 #define IMS_PCU_BL_RSP_ERASE_APP 0xc3 640 #define IMS_PCU_BL_RSP_PROGRAM_DEVICE 0xc4 641 #define IMS_PCU_BL_RSP_PROGRAM_COMPLETE 0xc5 642 #define IMS_PCU_BL_RSP_READ_APP 0xc6 643 #define IMS_PCU_BL_RSP_RESET_DEVICE 0 /* originally 0xa7 */ 644 #define IMS_PCU_BL_RSP_LAUNCH_APP 0 /* originally 0xa8 */ 645 646 #define IMS_PCU_BL_DATA_OFFSET 3 647 648 static int __ims_pcu_execute_bl_command(struct ims_pcu *pcu, 649 u8 command, const void *data, size_t len, 650 u8 expected_response, int response_time) 651 { 652 int error; 653 654 pcu->cmd_buf[0] = command; 655 if (data) 656 memcpy(&pcu->cmd_buf[1], data, len); 657 658 error = __ims_pcu_execute_command(pcu, 659 IMS_PCU_CMD_BOOTLOADER, pcu->cmd_buf, len + 1, 660 expected_response ? IMS_PCU_RSP_BOOTLOADER : 0, 661 response_time); 662 if (error) { 663 dev_err(pcu->dev, 664 "Failure when sending 0x%02x command to bootloader, error: %d\n", 665 pcu->cmd_buf[0], error); 666 return error; 667 } 668 669 if (expected_response && pcu->cmd_buf[2] != expected_response) { 670 dev_err(pcu->dev, 671 "Unexpected response from bootloader: 0x%02x, wanted 0x%02x\n", 672 pcu->cmd_buf[2], expected_response); 673 return -EINVAL; 674 } 675 676 return 0; 677 } 678 679 #define ims_pcu_execute_bl_command(pcu, code, data, len, timeout) \ 680 __ims_pcu_execute_bl_command(pcu, \ 681 IMS_PCU_BL_CMD_##code, data, len, \ 682 IMS_PCU_BL_RSP_##code, timeout) \ 683 684 #define IMS_PCU_INFO_PART_OFFSET 2 685 #define IMS_PCU_INFO_DOM_OFFSET 17 686 #define IMS_PCU_INFO_SERIAL_OFFSET 25 687 688 #define IMS_PCU_SET_INFO_SIZE 31 689 690 static int ims_pcu_get_info(struct ims_pcu *pcu) 691 { 692 int error; 693 694 error = ims_pcu_execute_query(pcu, GET_INFO); 695 if (error) { 696 dev_err(pcu->dev, 697 "GET_INFO command failed, error: %d\n", error); 698 return error; 699 } 700 701 memcpy(pcu->part_number, 702 &pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET], 703 sizeof(pcu->part_number)); 704 memcpy(pcu->date_of_manufacturing, 705 &pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET], 706 sizeof(pcu->date_of_manufacturing)); 707 memcpy(pcu->serial_number, 708 &pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET], 709 sizeof(pcu->serial_number)); 710 711 return 0; 712 } 713 714 static int ims_pcu_set_info(struct ims_pcu *pcu) 715 { 716 int error; 717 718 memcpy(&pcu->cmd_buf[IMS_PCU_INFO_PART_OFFSET], 719 pcu->part_number, sizeof(pcu->part_number)); 720 memcpy(&pcu->cmd_buf[IMS_PCU_INFO_DOM_OFFSET], 721 pcu->date_of_manufacturing, sizeof(pcu->date_of_manufacturing)); 722 memcpy(&pcu->cmd_buf[IMS_PCU_INFO_SERIAL_OFFSET], 723 pcu->serial_number, sizeof(pcu->serial_number)); 724 725 error = ims_pcu_execute_command(pcu, SET_INFO, 726 &pcu->cmd_buf[IMS_PCU_DATA_OFFSET], 727 IMS_PCU_SET_INFO_SIZE); 728 if (error) { 729 dev_err(pcu->dev, 730 "Failed to update device information, error: %d\n", 731 error); 732 return error; 733 } 734 735 return 0; 736 } 737 738 static int ims_pcu_switch_to_bootloader(struct ims_pcu *pcu) 739 { 740 int error; 741 742 /* Execute jump to the bootloader */ 743 error = ims_pcu_execute_command(pcu, JUMP_TO_BTLDR, NULL, 0); 744 if (error) { 745 dev_err(pcu->dev, 746 "Failure when sending JUMP TO BOOTLOADER command, error: %d\n", 747 error); 748 return error; 749 } 750 751 return 0; 752 } 753 754 /********************************************************************* 755 * Firmware Update handling * 756 *********************************************************************/ 757 758 #define IMS_PCU_FIRMWARE_NAME "imspcu.fw" 759 760 struct ims_pcu_flash_fmt { 761 __le32 addr; 762 u8 len; 763 u8 data[] __counted_by(len); 764 }; 765 766 static unsigned int ims_pcu_count_fw_records(const struct firmware *fw) 767 { 768 const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data; 769 unsigned int count = 0; 770 771 while (rec) { 772 count++; 773 rec = ihex_next_binrec(rec); 774 } 775 776 return count; 777 } 778 779 static int ims_pcu_verify_block(struct ims_pcu *pcu, 780 u32 addr, u8 len, const u8 *data) 781 { 782 struct ims_pcu_flash_fmt *fragment; 783 int error; 784 785 fragment = (void *)&pcu->cmd_buf[1]; 786 put_unaligned_le32(addr, &fragment->addr); 787 fragment->len = len; 788 789 error = ims_pcu_execute_bl_command(pcu, READ_APP, NULL, 5, 790 IMS_PCU_CMD_RESPONSE_TIMEOUT); 791 if (error) { 792 dev_err(pcu->dev, 793 "Failed to retrieve block at 0x%08x, len %d, error: %d\n", 794 addr, len, error); 795 return error; 796 } 797 798 fragment = (void *)&pcu->cmd_buf[IMS_PCU_BL_DATA_OFFSET]; 799 if (get_unaligned_le32(&fragment->addr) != addr || 800 fragment->len != len) { 801 dev_err(pcu->dev, 802 "Wrong block when retrieving 0x%08x (0x%08x), len %d (%d)\n", 803 addr, get_unaligned_le32(&fragment->addr), 804 len, fragment->len); 805 return -EINVAL; 806 } 807 808 if (memcmp(fragment->data, data, len)) { 809 dev_err(pcu->dev, 810 "Mismatch in block at 0x%08x, len %d\n", 811 addr, len); 812 return -EINVAL; 813 } 814 815 return 0; 816 } 817 818 static int ims_pcu_flash_firmware(struct ims_pcu *pcu, 819 const struct firmware *fw, 820 unsigned int n_fw_records) 821 { 822 const struct ihex_binrec *rec = (const struct ihex_binrec *)fw->data; 823 struct ims_pcu_flash_fmt *fragment; 824 unsigned int count = 0; 825 u32 addr; 826 u8 len; 827 int error; 828 829 error = ims_pcu_execute_bl_command(pcu, ERASE_APP, NULL, 0, 2000); 830 if (error) { 831 dev_err(pcu->dev, 832 "Failed to erase application image, error: %d\n", 833 error); 834 return error; 835 } 836 837 while (rec) { 838 /* 839 * The firmware format is messed up for some reason. 840 * The address twice that of what is needed for some 841 * reason and we end up overwriting half of the data 842 * with the next record. 843 */ 844 addr = be32_to_cpu(rec->addr) / 2; 845 len = be16_to_cpu(rec->len); 846 847 if (len > sizeof(pcu->cmd_buf) - 1 - sizeof(*fragment)) { 848 dev_err(pcu->dev, 849 "Invalid record length in firmware: %d\n", len); 850 return -EINVAL; 851 } 852 853 fragment = (void *)&pcu->cmd_buf[1]; 854 put_unaligned_le32(addr, &fragment->addr); 855 fragment->len = len; 856 memcpy(fragment->data, rec->data, len); 857 858 error = ims_pcu_execute_bl_command(pcu, PROGRAM_DEVICE, 859 NULL, len + 5, 860 IMS_PCU_CMD_RESPONSE_TIMEOUT); 861 if (error) { 862 dev_err(pcu->dev, 863 "Failed to write block at 0x%08x, len %d, error: %d\n", 864 addr, len, error); 865 return error; 866 } 867 868 if (addr >= pcu->fw_start_addr && addr < pcu->fw_end_addr) { 869 error = ims_pcu_verify_block(pcu, addr, len, rec->data); 870 if (error) 871 return error; 872 } 873 874 count++; 875 pcu->update_firmware_status = (count * 100) / n_fw_records; 876 877 rec = ihex_next_binrec(rec); 878 } 879 880 error = ims_pcu_execute_bl_command(pcu, PROGRAM_COMPLETE, 881 NULL, 0, 2000); 882 if (error) 883 dev_err(pcu->dev, 884 "Failed to send PROGRAM_COMPLETE, error: %d\n", 885 error); 886 887 return 0; 888 } 889 890 static int ims_pcu_handle_firmware_update(struct ims_pcu *pcu, 891 const struct firmware *fw) 892 { 893 unsigned int n_fw_records; 894 int retval; 895 896 dev_info(pcu->dev, "Updating firmware %s, size: %zu\n", 897 IMS_PCU_FIRMWARE_NAME, fw->size); 898 899 n_fw_records = ims_pcu_count_fw_records(fw); 900 901 retval = ims_pcu_flash_firmware(pcu, fw, n_fw_records); 902 if (retval) 903 goto out; 904 905 retval = ims_pcu_execute_bl_command(pcu, LAUNCH_APP, NULL, 0, 0); 906 if (retval) 907 dev_err(pcu->dev, 908 "Failed to start application image, error: %d\n", 909 retval); 910 911 out: 912 pcu->update_firmware_status = retval; 913 sysfs_notify(&pcu->dev->kobj, NULL, "update_firmware_status"); 914 return retval; 915 } 916 917 static void ims_pcu_process_async_firmware(const struct firmware *fw, 918 void *context) 919 { 920 struct ims_pcu *pcu = context; 921 int error; 922 923 if (!fw) { 924 dev_err(pcu->dev, "Failed to get firmware %s\n", 925 IMS_PCU_FIRMWARE_NAME); 926 goto out; 927 } 928 929 error = ihex_validate_fw(fw); 930 if (error) { 931 dev_err(pcu->dev, "Firmware %s is invalid\n", 932 IMS_PCU_FIRMWARE_NAME); 933 goto out; 934 } 935 936 scoped_guard(mutex, &pcu->cmd_mutex) 937 ims_pcu_handle_firmware_update(pcu, fw); 938 939 release_firmware(fw); 940 941 out: 942 complete(&pcu->async_firmware_done); 943 } 944 945 /********************************************************************* 946 * Backlight LED device support * 947 *********************************************************************/ 948 949 #define IMS_PCU_MAX_BRIGHTNESS 31998 950 951 static int ims_pcu_backlight_set_brightness(struct led_classdev *cdev, 952 enum led_brightness value) 953 { 954 struct ims_pcu_backlight *backlight = 955 container_of(cdev, struct ims_pcu_backlight, cdev); 956 struct ims_pcu *pcu = 957 container_of(backlight, struct ims_pcu, backlight); 958 __le16 br_val = cpu_to_le16(value); 959 int error; 960 961 guard(mutex)(&pcu->cmd_mutex); 962 963 error = ims_pcu_execute_command(pcu, SET_BRIGHTNESS, 964 &br_val, sizeof(br_val)); 965 if (error && error != -ENODEV) 966 dev_warn(pcu->dev, 967 "Failed to set desired brightness %u, error: %d\n", 968 value, error); 969 970 return error; 971 } 972 973 static enum led_brightness 974 ims_pcu_backlight_get_brightness(struct led_classdev *cdev) 975 { 976 struct ims_pcu_backlight *backlight = 977 container_of(cdev, struct ims_pcu_backlight, cdev); 978 struct ims_pcu *pcu = 979 container_of(backlight, struct ims_pcu, backlight); 980 int brightness; 981 int error; 982 983 guard(mutex)(&pcu->cmd_mutex); 984 985 error = ims_pcu_execute_query(pcu, GET_BRIGHTNESS); 986 if (error) { 987 dev_warn(pcu->dev, 988 "Failed to get current brightness, error: %d\n", 989 error); 990 /* Assume the LED is OFF */ 991 brightness = LED_OFF; 992 } else { 993 brightness = 994 get_unaligned_le16(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET]); 995 } 996 997 return brightness; 998 } 999 1000 static int ims_pcu_setup_backlight(struct ims_pcu *pcu) 1001 { 1002 struct ims_pcu_backlight *backlight = &pcu->backlight; 1003 int error; 1004 1005 snprintf(backlight->name, sizeof(backlight->name), 1006 "pcu%d::kbd_backlight", pcu->device_no); 1007 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; 1013 1014 error = led_classdev_register(pcu->dev, &backlight->cdev); 1015 if (error) { 1016 dev_err(pcu->dev, 1017 "Failed to register backlight LED device, error: %d\n", 1018 error); 1019 return error; 1020 } 1021 1022 return 0; 1023 } 1024 1025 static void ims_pcu_destroy_backlight(struct ims_pcu *pcu) 1026 { 1027 struct ims_pcu_backlight *backlight = &pcu->backlight; 1028 1029 led_classdev_unregister(&backlight->cdev); 1030 } 1031 1032 1033 /********************************************************************* 1034 * Sysfs attributes handling * 1035 *********************************************************************/ 1036 1037 struct ims_pcu_attribute { 1038 struct device_attribute dattr; 1039 size_t field_offset; 1040 int field_length; 1041 }; 1042 1043 static ssize_t ims_pcu_attribute_show(struct device *dev, 1044 struct device_attribute *dattr, 1045 char *buf) 1046 { 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; 1052 1053 return sysfs_emit(buf, "%.*s\n", attr->field_length, field); 1054 } 1055 1056 static ssize_t ims_pcu_attribute_store(struct device *dev, 1057 struct device_attribute *dattr, 1058 const char *buf, size_t count) 1059 { 1060 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; 1066 size_t data_len; 1067 int error; 1068 1069 if (count > attr->field_length) 1070 return -EINVAL; 1071 1072 data_len = strnlen(buf, attr->field_length); 1073 if (data_len > attr->field_length) 1074 return -EINVAL; 1075 1076 scoped_cond_guard(mutex_intr, return -EINTR, &pcu->cmd_mutex) { 1077 memset(field, 0, attr->field_length); 1078 memcpy(field, buf, data_len); 1079 1080 error = ims_pcu_set_info(pcu); 1081 1082 /* 1083 * Even if update failed, let's fetch the info again as we just 1084 * clobbered one of the fields. 1085 */ 1086 ims_pcu_get_info(pcu); 1087 1088 if (error) 1089 return error; 1090 } 1091 1092 return count; 1093 } 1094 1095 #define IMS_PCU_ATTR(_field, _mode) \ 1096 struct ims_pcu_attribute ims_pcu_attr_##_field = { \ 1097 .dattr = __ATTR(_field, _mode, \ 1098 ims_pcu_attribute_show, \ 1099 ims_pcu_attribute_store), \ 1100 .field_offset = offsetof(struct ims_pcu, _field), \ 1101 .field_length = sizeof(((struct ims_pcu *)NULL)->_field), \ 1102 } 1103 1104 #define IMS_PCU_RO_ATTR(_field) \ 1105 IMS_PCU_ATTR(_field, S_IRUGO) 1106 #define IMS_PCU_RW_ATTR(_field) \ 1107 IMS_PCU_ATTR(_field, S_IRUGO | S_IWUSR) 1108 1109 static IMS_PCU_RW_ATTR(part_number); 1110 static IMS_PCU_RW_ATTR(serial_number); 1111 static IMS_PCU_RW_ATTR(date_of_manufacturing); 1112 1113 static IMS_PCU_RO_ATTR(fw_version); 1114 static IMS_PCU_RO_ATTR(bl_version); 1115 static IMS_PCU_RO_ATTR(reset_reason); 1116 1117 static ssize_t ims_pcu_reset_device(struct device *dev, 1118 struct device_attribute *dattr, 1119 const char *buf, size_t count) 1120 { 1121 static const u8 reset_byte = 1; 1122 struct usb_interface *intf = to_usb_interface(dev); 1123 struct ims_pcu *pcu = usb_get_intfdata(intf); 1124 int value; 1125 int error; 1126 1127 error = kstrtoint(buf, 0, &value); 1128 if (error) 1129 return error; 1130 1131 if (value != 1) 1132 return -EINVAL; 1133 1134 dev_info(pcu->dev, "Attempting to reset device\n"); 1135 1136 error = ims_pcu_execute_command(pcu, PCU_RESET, &reset_byte, 1); 1137 if (error) { 1138 dev_info(pcu->dev, 1139 "Failed to reset device, error: %d\n", 1140 error); 1141 return error; 1142 } 1143 1144 return count; 1145 } 1146 1147 static DEVICE_ATTR(reset_device, S_IWUSR, NULL, ims_pcu_reset_device); 1148 1149 static ssize_t ims_pcu_update_firmware_store(struct device *dev, 1150 struct device_attribute *dattr, 1151 const char *buf, size_t count) 1152 { 1153 struct usb_interface *intf = to_usb_interface(dev); 1154 struct ims_pcu *pcu = usb_get_intfdata(intf); 1155 int value; 1156 int error; 1157 1158 error = kstrtoint(buf, 0, &value); 1159 if (error) 1160 return error; 1161 1162 if (value != 1) 1163 return -EINVAL; 1164 1165 const struct firmware *fw __free(firmware) = NULL; 1166 error = request_ihex_firmware(&fw, IMS_PCU_FIRMWARE_NAME, pcu->dev); 1167 if (error) { 1168 dev_err(pcu->dev, "Failed to request firmware %s, error: %d\n", 1169 IMS_PCU_FIRMWARE_NAME, error); 1170 return error; 1171 } 1172 1173 scoped_cond_guard(mutex_intr, return -EINTR, &pcu->cmd_mutex) { 1174 /* 1175 * If we are already in bootloader mode we can proceed with 1176 * flashing the firmware. 1177 * 1178 * If we are in application mode, then we need to switch into 1179 * bootloader mode, which will cause the device to disconnect 1180 * and reconnect as different device. 1181 */ 1182 if (pcu->bootloader_mode) 1183 error = ims_pcu_handle_firmware_update(pcu, fw); 1184 else 1185 error = ims_pcu_switch_to_bootloader(pcu); 1186 1187 if (error) 1188 return error; 1189 } 1190 1191 return count; 1192 } 1193 1194 static DEVICE_ATTR(update_firmware, S_IWUSR, 1195 NULL, ims_pcu_update_firmware_store); 1196 1197 static ssize_t 1198 ims_pcu_update_firmware_status_show(struct device *dev, 1199 struct device_attribute *dattr, 1200 char *buf) 1201 { 1202 struct usb_interface *intf = to_usb_interface(dev); 1203 struct ims_pcu *pcu = usb_get_intfdata(intf); 1204 1205 return sysfs_emit(buf, "%d\n", pcu->update_firmware_status); 1206 } 1207 1208 static DEVICE_ATTR(update_firmware_status, S_IRUGO, 1209 ims_pcu_update_firmware_status_show, NULL); 1210 1211 static struct attribute *ims_pcu_attrs[] = { 1212 &ims_pcu_attr_part_number.dattr.attr, 1213 &ims_pcu_attr_serial_number.dattr.attr, 1214 &ims_pcu_attr_date_of_manufacturing.dattr.attr, 1215 &ims_pcu_attr_fw_version.dattr.attr, 1216 &ims_pcu_attr_bl_version.dattr.attr, 1217 &ims_pcu_attr_reset_reason.dattr.attr, 1218 &dev_attr_reset_device.attr, 1219 &dev_attr_update_firmware.attr, 1220 &dev_attr_update_firmware_status.attr, 1221 NULL 1222 }; 1223 1224 static umode_t ims_pcu_is_attr_visible(struct kobject *kobj, 1225 struct attribute *attr, int n) 1226 { 1227 struct device *dev = kobj_to_dev(kobj); 1228 struct usb_interface *intf = to_usb_interface(dev); 1229 struct ims_pcu *pcu = usb_get_intfdata(intf); 1230 umode_t mode = attr->mode; 1231 1232 if (pcu->bootloader_mode) { 1233 if (attr != &dev_attr_update_firmware_status.attr && 1234 attr != &dev_attr_update_firmware.attr && 1235 attr != &dev_attr_reset_device.attr) { 1236 mode = 0; 1237 } 1238 } else { 1239 if (attr == &dev_attr_update_firmware_status.attr) 1240 mode = 0; 1241 } 1242 1243 return mode; 1244 } 1245 1246 static const struct attribute_group ims_pcu_attr_group = { 1247 .is_visible = ims_pcu_is_attr_visible, 1248 .attrs = ims_pcu_attrs, 1249 }; 1250 1251 /* Support for a separate OFN attribute group */ 1252 1253 #define OFN_REG_RESULT_OFFSET 2 1254 1255 static int ims_pcu_read_ofn_config(struct ims_pcu *pcu, u8 addr, u8 *data) 1256 { 1257 int error; 1258 s16 result; 1259 1260 error = ims_pcu_execute_command(pcu, OFN_GET_CONFIG, 1261 &addr, sizeof(addr)); 1262 if (error) 1263 return error; 1264 1265 result = (s16)get_unaligned_le16(pcu->cmd_buf + OFN_REG_RESULT_OFFSET); 1266 if (result < 0) 1267 return -EIO; 1268 1269 /* We only need LSB */ 1270 *data = pcu->cmd_buf[OFN_REG_RESULT_OFFSET]; 1271 return 0; 1272 } 1273 1274 static int ims_pcu_write_ofn_config(struct ims_pcu *pcu, u8 addr, u8 data) 1275 { 1276 u8 buffer[] = { addr, data }; 1277 int error; 1278 s16 result; 1279 1280 error = ims_pcu_execute_command(pcu, OFN_SET_CONFIG, 1281 &buffer, sizeof(buffer)); 1282 if (error) 1283 return error; 1284 1285 result = (s16)get_unaligned_le16(pcu->cmd_buf + OFN_REG_RESULT_OFFSET); 1286 if (result < 0) 1287 return -EIO; 1288 1289 return 0; 1290 } 1291 1292 static ssize_t ims_pcu_ofn_reg_data_show(struct device *dev, 1293 struct device_attribute *dattr, 1294 char *buf) 1295 { 1296 struct usb_interface *intf = to_usb_interface(dev); 1297 struct ims_pcu *pcu = usb_get_intfdata(intf); 1298 int error; 1299 u8 data; 1300 1301 scoped_guard(mutex, &pcu->cmd_mutex) { 1302 error = ims_pcu_read_ofn_config(pcu, pcu->ofn_reg_addr, &data); 1303 if (error) 1304 return error; 1305 } 1306 1307 return sysfs_emit(buf, "%x\n", data); 1308 } 1309 1310 static ssize_t ims_pcu_ofn_reg_data_store(struct device *dev, 1311 struct device_attribute *dattr, 1312 const char *buf, size_t count) 1313 { 1314 struct usb_interface *intf = to_usb_interface(dev); 1315 struct ims_pcu *pcu = usb_get_intfdata(intf); 1316 int error; 1317 u8 value; 1318 1319 error = kstrtou8(buf, 0, &value); 1320 if (error) 1321 return error; 1322 1323 guard(mutex)(&pcu->cmd_mutex); 1324 1325 error = ims_pcu_write_ofn_config(pcu, pcu->ofn_reg_addr, value); 1326 if (error) 1327 return error; 1328 1329 return count; 1330 } 1331 1332 static DEVICE_ATTR(reg_data, S_IRUGO | S_IWUSR, 1333 ims_pcu_ofn_reg_data_show, ims_pcu_ofn_reg_data_store); 1334 1335 static ssize_t ims_pcu_ofn_reg_addr_show(struct device *dev, 1336 struct device_attribute *dattr, 1337 char *buf) 1338 { 1339 struct usb_interface *intf = to_usb_interface(dev); 1340 struct ims_pcu *pcu = usb_get_intfdata(intf); 1341 1342 guard(mutex)(&pcu->cmd_mutex); 1343 1344 return sysfs_emit(buf, "%x\n", pcu->ofn_reg_addr); 1345 } 1346 1347 static ssize_t ims_pcu_ofn_reg_addr_store(struct device *dev, 1348 struct device_attribute *dattr, 1349 const char *buf, size_t count) 1350 { 1351 struct usb_interface *intf = to_usb_interface(dev); 1352 struct ims_pcu *pcu = usb_get_intfdata(intf); 1353 int error; 1354 u8 value; 1355 1356 error = kstrtou8(buf, 0, &value); 1357 if (error) 1358 return error; 1359 1360 guard(mutex)(&pcu->cmd_mutex); 1361 1362 pcu->ofn_reg_addr = value; 1363 1364 return count; 1365 } 1366 1367 static DEVICE_ATTR(reg_addr, S_IRUGO | S_IWUSR, 1368 ims_pcu_ofn_reg_addr_show, ims_pcu_ofn_reg_addr_store); 1369 1370 struct ims_pcu_ofn_bit_attribute { 1371 struct device_attribute dattr; 1372 u8 addr; 1373 u8 nr; 1374 }; 1375 1376 static ssize_t ims_pcu_ofn_bit_show(struct device *dev, 1377 struct device_attribute *dattr, 1378 char *buf) 1379 { 1380 struct usb_interface *intf = to_usb_interface(dev); 1381 struct ims_pcu *pcu = usb_get_intfdata(intf); 1382 struct ims_pcu_ofn_bit_attribute *attr = 1383 container_of(dattr, struct ims_pcu_ofn_bit_attribute, dattr); 1384 int error; 1385 u8 data; 1386 1387 scoped_guard(mutex, &pcu->cmd_mutex) { 1388 error = ims_pcu_read_ofn_config(pcu, attr->addr, &data); 1389 if (error) 1390 return error; 1391 } 1392 1393 return sysfs_emit(buf, "%d\n", !!(data & (1 << attr->nr))); 1394 } 1395 1396 static ssize_t ims_pcu_ofn_bit_store(struct device *dev, 1397 struct device_attribute *dattr, 1398 const char *buf, size_t count) 1399 { 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); 1404 int error; 1405 int value; 1406 u8 data; 1407 1408 error = kstrtoint(buf, 0, &value); 1409 if (error) 1410 return error; 1411 1412 if (value > 1) 1413 return -EINVAL; 1414 1415 scoped_guard(mutex, &pcu->cmd_mutex) { 1416 error = ims_pcu_read_ofn_config(pcu, attr->addr, &data); 1417 if (error) 1418 return error; 1419 1420 if (value) 1421 data |= 1U << attr->nr; 1422 else 1423 data &= ~(1U << attr->nr); 1424 1425 error = ims_pcu_write_ofn_config(pcu, attr->addr, data); 1426 if (error) 1427 return error; 1428 } 1429 1430 return count; 1431 } 1432 1433 #define IMS_PCU_OFN_BIT_ATTR(_field, _addr, _nr) \ 1434 struct ims_pcu_ofn_bit_attribute ims_pcu_ofn_attr_##_field = { \ 1435 .dattr = __ATTR(_field, S_IWUSR | S_IRUGO, \ 1436 ims_pcu_ofn_bit_show, ims_pcu_ofn_bit_store), \ 1437 .addr = _addr, \ 1438 .nr = _nr, \ 1439 } 1440 1441 static IMS_PCU_OFN_BIT_ATTR(engine_enable, 0x60, 7); 1442 static IMS_PCU_OFN_BIT_ATTR(speed_enable, 0x60, 6); 1443 static IMS_PCU_OFN_BIT_ATTR(assert_enable, 0x60, 5); 1444 static IMS_PCU_OFN_BIT_ATTR(xyquant_enable, 0x60, 4); 1445 static IMS_PCU_OFN_BIT_ATTR(xyscale_enable, 0x60, 1); 1446 1447 static IMS_PCU_OFN_BIT_ATTR(scale_x2, 0x63, 6); 1448 static IMS_PCU_OFN_BIT_ATTR(scale_y2, 0x63, 7); 1449 1450 static struct attribute *ims_pcu_ofn_attrs[] = { 1451 &dev_attr_reg_data.attr, 1452 &dev_attr_reg_addr.attr, 1453 &ims_pcu_ofn_attr_engine_enable.dattr.attr, 1454 &ims_pcu_ofn_attr_speed_enable.dattr.attr, 1455 &ims_pcu_ofn_attr_assert_enable.dattr.attr, 1456 &ims_pcu_ofn_attr_xyquant_enable.dattr.attr, 1457 &ims_pcu_ofn_attr_xyscale_enable.dattr.attr, 1458 &ims_pcu_ofn_attr_scale_x2.dattr.attr, 1459 &ims_pcu_ofn_attr_scale_y2.dattr.attr, 1460 NULL 1461 }; 1462 1463 static umode_t ims_pcu_ofn_is_attr_visible(struct kobject *kobj, 1464 struct attribute *attr, int n) 1465 { 1466 struct device *dev = kobj_to_dev(kobj); 1467 struct usb_interface *intf = to_usb_interface(dev); 1468 struct ims_pcu *pcu = usb_get_intfdata(intf); 1469 umode_t mode = attr->mode; 1470 1471 /* 1472 * PCU-B devices, both GEN_1 and GEN_2 do not have OFN sensor. 1473 */ 1474 if (pcu->bootloader_mode || pcu->device_id == IMS_PCU_PCU_B_DEVICE_ID) 1475 mode = 0; 1476 1477 return mode; 1478 } 1479 1480 static const struct attribute_group ims_pcu_ofn_attr_group = { 1481 .name = "ofn", 1482 .is_visible = ims_pcu_ofn_is_attr_visible, 1483 .attrs = ims_pcu_ofn_attrs, 1484 }; 1485 1486 static void ims_pcu_irq(struct urb *urb) 1487 { 1488 struct ims_pcu *pcu = urb->context; 1489 int retval, status; 1490 1491 status = urb->status; 1492 1493 switch (status) { 1494 case 0: 1495 /* success */ 1496 break; 1497 case -ECONNRESET: 1498 case -ENOENT: 1499 case -ESHUTDOWN: 1500 /* this urb is terminated, clean up */ 1501 dev_dbg(pcu->dev, "%s - urb shutting down with status: %d\n", 1502 __func__, status); 1503 return; 1504 default: 1505 dev_dbg(pcu->dev, "%s - nonzero urb status received: %d\n", 1506 __func__, status); 1507 goto exit; 1508 } 1509 1510 dev_dbg(pcu->dev, "%s: received %d: %*ph\n", __func__, 1511 urb->actual_length, urb->actual_length, pcu->urb_in_buf); 1512 1513 if (urb == pcu->urb_in) 1514 ims_pcu_process_data(pcu, urb); 1515 1516 exit: 1517 retval = usb_submit_urb(urb, GFP_ATOMIC); 1518 if (retval && retval != -ENODEV) 1519 dev_err(pcu->dev, "%s - usb_submit_urb failed with result %d\n", 1520 __func__, retval); 1521 } 1522 1523 static int ims_pcu_buffers_alloc(struct ims_pcu *pcu) 1524 { 1525 int error; 1526 1527 pcu->urb_in_buf = usb_alloc_coherent(pcu->udev, pcu->max_in_size, 1528 GFP_KERNEL, &pcu->read_dma); 1529 if (!pcu->urb_in_buf) { 1530 dev_err(pcu->dev, 1531 "Failed to allocate memory for read buffer\n"); 1532 return -ENOMEM; 1533 } 1534 1535 pcu->urb_in = usb_alloc_urb(0, GFP_KERNEL); 1536 if (!pcu->urb_in) { 1537 dev_err(pcu->dev, "Failed to allocate input URB\n"); 1538 error = -ENOMEM; 1539 goto err_free_urb_in_buf; 1540 } 1541 1542 pcu->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1543 pcu->urb_in->transfer_dma = pcu->read_dma; 1544 1545 usb_fill_bulk_urb(pcu->urb_in, pcu->udev, 1546 usb_rcvbulkpipe(pcu->udev, 1547 pcu->ep_in->bEndpointAddress), 1548 pcu->urb_in_buf, pcu->max_in_size, 1549 ims_pcu_irq, pcu); 1550 1551 /* 1552 * We are using usb_bulk_msg() for sending so there is no point 1553 * in allocating memory with usb_alloc_coherent(). 1554 */ 1555 pcu->urb_out_buf = kmalloc(pcu->max_out_size, GFP_KERNEL); 1556 if (!pcu->urb_out_buf) { 1557 dev_err(pcu->dev, "Failed to allocate memory for write buffer\n"); 1558 error = -ENOMEM; 1559 goto err_free_in_urb; 1560 } 1561 1562 pcu->urb_ctrl_buf = usb_alloc_coherent(pcu->udev, pcu->max_ctrl_size, 1563 GFP_KERNEL, &pcu->ctrl_dma); 1564 if (!pcu->urb_ctrl_buf) { 1565 dev_err(pcu->dev, 1566 "Failed to allocate memory for read buffer\n"); 1567 error = -ENOMEM; 1568 goto err_free_urb_out_buf; 1569 } 1570 1571 pcu->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL); 1572 if (!pcu->urb_ctrl) { 1573 dev_err(pcu->dev, "Failed to allocate input URB\n"); 1574 error = -ENOMEM; 1575 goto err_free_urb_ctrl_buf; 1576 } 1577 1578 pcu->urb_ctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1579 pcu->urb_ctrl->transfer_dma = pcu->ctrl_dma; 1580 1581 usb_fill_int_urb(pcu->urb_ctrl, pcu->udev, 1582 usb_rcvintpipe(pcu->udev, 1583 pcu->ep_ctrl->bEndpointAddress), 1584 pcu->urb_ctrl_buf, pcu->max_ctrl_size, 1585 ims_pcu_irq, pcu, pcu->ep_ctrl->bInterval); 1586 1587 return 0; 1588 1589 err_free_urb_ctrl_buf: 1590 usb_free_coherent(pcu->udev, pcu->max_ctrl_size, 1591 pcu->urb_ctrl_buf, pcu->ctrl_dma); 1592 err_free_urb_out_buf: 1593 kfree(pcu->urb_out_buf); 1594 err_free_in_urb: 1595 usb_free_urb(pcu->urb_in); 1596 err_free_urb_in_buf: 1597 usb_free_coherent(pcu->udev, pcu->max_in_size, 1598 pcu->urb_in_buf, pcu->read_dma); 1599 return error; 1600 } 1601 1602 static void ims_pcu_buffers_free(struct ims_pcu *pcu) 1603 { 1604 usb_kill_urb(pcu->urb_in); 1605 usb_free_urb(pcu->urb_in); 1606 1607 usb_free_coherent(pcu->udev, pcu->max_out_size, 1608 pcu->urb_in_buf, pcu->read_dma); 1609 1610 kfree(pcu->urb_out_buf); 1611 1612 usb_kill_urb(pcu->urb_ctrl); 1613 usb_free_urb(pcu->urb_ctrl); 1614 1615 usb_free_coherent(pcu->udev, pcu->max_ctrl_size, 1616 pcu->urb_ctrl_buf, pcu->ctrl_dma); 1617 } 1618 1619 static const struct usb_cdc_union_desc * 1620 ims_pcu_get_cdc_union_desc(struct usb_interface *intf) 1621 { 1622 const void *buf = intf->altsetting->extra; 1623 size_t buflen = intf->altsetting->extralen; 1624 struct usb_cdc_union_desc *union_desc; 1625 1626 if (!buf) { 1627 dev_err(&intf->dev, "Missing descriptor data\n"); 1628 return NULL; 1629 } 1630 1631 if (!buflen) { 1632 dev_err(&intf->dev, "Zero length descriptor\n"); 1633 return NULL; 1634 } 1635 1636 while (buflen >= sizeof(*union_desc)) { 1637 union_desc = (struct usb_cdc_union_desc *)buf; 1638 1639 if (union_desc->bLength > buflen) { 1640 dev_err(&intf->dev, "Too large descriptor\n"); 1641 return NULL; 1642 } 1643 1644 if (union_desc->bDescriptorType == USB_DT_CS_INTERFACE && 1645 union_desc->bDescriptorSubType == USB_CDC_UNION_TYPE) { 1646 dev_dbg(&intf->dev, "Found union header\n"); 1647 1648 if (union_desc->bLength >= sizeof(*union_desc)) 1649 return union_desc; 1650 1651 dev_err(&intf->dev, 1652 "Union descriptor too short (%d vs %zd)\n", 1653 union_desc->bLength, sizeof(*union_desc)); 1654 return NULL; 1655 } 1656 1657 buflen -= union_desc->bLength; 1658 buf += union_desc->bLength; 1659 } 1660 1661 dev_err(&intf->dev, "Missing CDC union descriptor\n"); 1662 return NULL; 1663 } 1664 1665 static int ims_pcu_parse_cdc_data(struct usb_interface *intf, struct ims_pcu *pcu) 1666 { 1667 const struct usb_cdc_union_desc *union_desc; 1668 struct usb_host_interface *alt; 1669 1670 union_desc = ims_pcu_get_cdc_union_desc(intf); 1671 if (!union_desc) 1672 return -EINVAL; 1673 1674 pcu->ctrl_intf = usb_ifnum_to_if(pcu->udev, 1675 union_desc->bMasterInterface0); 1676 if (!pcu->ctrl_intf) 1677 return -EINVAL; 1678 1679 alt = pcu->ctrl_intf->cur_altsetting; 1680 1681 if (alt->desc.bNumEndpoints < 1) 1682 return -ENODEV; 1683 1684 pcu->ep_ctrl = &alt->endpoint[0].desc; 1685 pcu->max_ctrl_size = usb_endpoint_maxp(pcu->ep_ctrl); 1686 1687 pcu->data_intf = usb_ifnum_to_if(pcu->udev, 1688 union_desc->bSlaveInterface0); 1689 if (!pcu->data_intf) 1690 return -EINVAL; 1691 1692 alt = pcu->data_intf->cur_altsetting; 1693 if (alt->desc.bNumEndpoints != 2) { 1694 dev_err(pcu->dev, 1695 "Incorrect number of endpoints on data interface (%d)\n", 1696 alt->desc.bNumEndpoints); 1697 return -EINVAL; 1698 } 1699 1700 pcu->ep_out = &alt->endpoint[0].desc; 1701 if (!usb_endpoint_is_bulk_out(pcu->ep_out)) { 1702 dev_err(pcu->dev, 1703 "First endpoint on data interface is not BULK OUT\n"); 1704 return -EINVAL; 1705 } 1706 1707 pcu->max_out_size = usb_endpoint_maxp(pcu->ep_out); 1708 if (pcu->max_out_size < 8) { 1709 dev_err(pcu->dev, 1710 "Max OUT packet size is too small (%zd)\n", 1711 pcu->max_out_size); 1712 return -EINVAL; 1713 } 1714 1715 pcu->ep_in = &alt->endpoint[1].desc; 1716 if (!usb_endpoint_is_bulk_in(pcu->ep_in)) { 1717 dev_err(pcu->dev, 1718 "Second endpoint on data interface is not BULK IN\n"); 1719 return -EINVAL; 1720 } 1721 1722 pcu->max_in_size = usb_endpoint_maxp(pcu->ep_in); 1723 if (pcu->max_in_size < 8) { 1724 dev_err(pcu->dev, 1725 "Max IN packet size is too small (%zd)\n", 1726 pcu->max_in_size); 1727 return -EINVAL; 1728 } 1729 1730 return 0; 1731 } 1732 1733 static int ims_pcu_start_io(struct ims_pcu *pcu) 1734 { 1735 int error; 1736 1737 error = usb_submit_urb(pcu->urb_ctrl, GFP_KERNEL); 1738 if (error) { 1739 dev_err(pcu->dev, 1740 "Failed to start control IO - usb_submit_urb failed with result: %d\n", 1741 error); 1742 return -EIO; 1743 } 1744 1745 error = usb_submit_urb(pcu->urb_in, GFP_KERNEL); 1746 if (error) { 1747 dev_err(pcu->dev, 1748 "Failed to start IO - usb_submit_urb failed with result: %d\n", 1749 error); 1750 usb_kill_urb(pcu->urb_ctrl); 1751 return -EIO; 1752 } 1753 1754 return 0; 1755 } 1756 1757 static void ims_pcu_stop_io(struct ims_pcu *pcu) 1758 { 1759 usb_kill_urb(pcu->urb_in); 1760 usb_kill_urb(pcu->urb_ctrl); 1761 } 1762 1763 static int ims_pcu_line_setup(struct ims_pcu *pcu) 1764 { 1765 struct usb_host_interface *interface = pcu->ctrl_intf->cur_altsetting; 1766 struct usb_cdc_line_coding *line = (void *)pcu->cmd_buf; 1767 int error; 1768 1769 memset(line, 0, sizeof(*line)); 1770 line->dwDTERate = cpu_to_le32(57600); 1771 line->bDataBits = 8; 1772 1773 error = usb_control_msg(pcu->udev, usb_sndctrlpipe(pcu->udev, 0), 1774 USB_CDC_REQ_SET_LINE_CODING, 1775 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1776 0, interface->desc.bInterfaceNumber, 1777 line, sizeof(struct usb_cdc_line_coding), 1778 5000); 1779 if (error < 0) { 1780 dev_err(pcu->dev, "Failed to set line coding, error: %d\n", 1781 error); 1782 return error; 1783 } 1784 1785 error = usb_control_msg(pcu->udev, usb_sndctrlpipe(pcu->udev, 0), 1786 USB_CDC_REQ_SET_CONTROL_LINE_STATE, 1787 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1788 0x03, interface->desc.bInterfaceNumber, 1789 NULL, 0, 5000); 1790 if (error < 0) { 1791 dev_err(pcu->dev, "Failed to set line state, error: %d\n", 1792 error); 1793 return error; 1794 } 1795 1796 return 0; 1797 } 1798 1799 static int ims_pcu_get_device_info(struct ims_pcu *pcu) 1800 { 1801 int error; 1802 1803 error = ims_pcu_get_info(pcu); 1804 if (error) 1805 return error; 1806 1807 error = ims_pcu_execute_query(pcu, GET_FW_VERSION); 1808 if (error) { 1809 dev_err(pcu->dev, 1810 "GET_FW_VERSION command failed, error: %d\n", error); 1811 return error; 1812 } 1813 1814 snprintf(pcu->fw_version, sizeof(pcu->fw_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]); 1818 1819 error = ims_pcu_execute_query(pcu, GET_BL_VERSION); 1820 if (error) { 1821 dev_err(pcu->dev, 1822 "GET_BL_VERSION command failed, error: %d\n", error); 1823 return error; 1824 } 1825 1826 snprintf(pcu->bl_version, sizeof(pcu->bl_version), 1827 "%02d%02d%02d%02d.%c%c", 1828 pcu->cmd_buf[2], pcu->cmd_buf[3], pcu->cmd_buf[4], pcu->cmd_buf[5], 1829 pcu->cmd_buf[6], pcu->cmd_buf[7]); 1830 1831 error = ims_pcu_execute_query(pcu, RESET_REASON); 1832 if (error) { 1833 dev_err(pcu->dev, 1834 "RESET_REASON command failed, error: %d\n", error); 1835 return error; 1836 } 1837 1838 snprintf(pcu->reset_reason, sizeof(pcu->reset_reason), 1839 "%02x", pcu->cmd_buf[IMS_PCU_DATA_OFFSET]); 1840 1841 dev_dbg(pcu->dev, 1842 "P/N: %s, MD: %s, S/N: %s, FW: %s, BL: %s, RR: %s\n", 1843 pcu->part_number, 1844 pcu->date_of_manufacturing, 1845 pcu->serial_number, 1846 pcu->fw_version, 1847 pcu->bl_version, 1848 pcu->reset_reason); 1849 1850 return 0; 1851 } 1852 1853 static int ims_pcu_identify_type(struct ims_pcu *pcu, u8 *device_id) 1854 { 1855 int error; 1856 1857 error = ims_pcu_execute_query(pcu, GET_DEVICE_ID); 1858 if (error) { 1859 dev_err(pcu->dev, 1860 "GET_DEVICE_ID command failed, error: %d\n", error); 1861 return error; 1862 } 1863 1864 *device_id = pcu->cmd_buf[IMS_PCU_DATA_OFFSET]; 1865 dev_dbg(pcu->dev, "Detected device ID: %d\n", *device_id); 1866 1867 return 0; 1868 } 1869 1870 static int ims_pcu_init_application_mode(struct ims_pcu *pcu) 1871 { 1872 static atomic_t device_no = ATOMIC_INIT(-1); 1873 1874 const struct ims_pcu_device_info *info; 1875 int error; 1876 1877 error = ims_pcu_get_device_info(pcu); 1878 if (error) { 1879 /* Device does not respond to basic queries, hopeless */ 1880 return error; 1881 } 1882 1883 error = ims_pcu_identify_type(pcu, &pcu->device_id); 1884 if (error) { 1885 dev_err(pcu->dev, 1886 "Failed to identify device, error: %d\n", error); 1887 /* 1888 * Do not signal error, but do not create input nor 1889 * backlight devices either, let userspace figure this 1890 * out (flash a new firmware?). 1891 */ 1892 return 0; 1893 } 1894 1895 if (pcu->device_id >= ARRAY_SIZE(ims_pcu_device_info) || 1896 !ims_pcu_device_info[pcu->device_id].keymap) { 1897 dev_err(pcu->dev, "Device ID %d is not valid\n", pcu->device_id); 1898 /* Same as above, punt to userspace */ 1899 return 0; 1900 } 1901 1902 /* Device appears to be operable, complete initialization */ 1903 pcu->device_no = atomic_inc_return(&device_no); 1904 1905 error = ims_pcu_setup_backlight(pcu); 1906 if (error) 1907 return error; 1908 1909 info = &ims_pcu_device_info[pcu->device_id]; 1910 error = ims_pcu_setup_buttons(pcu, info->keymap, info->keymap_len); 1911 if (error) 1912 goto err_destroy_backlight; 1913 1914 if (info->has_gamepad) { 1915 error = ims_pcu_setup_gamepad(pcu); 1916 if (error) 1917 goto err_destroy_buttons; 1918 } 1919 1920 pcu->setup_complete = true; 1921 1922 return 0; 1923 1924 err_destroy_buttons: 1925 ims_pcu_destroy_buttons(pcu); 1926 err_destroy_backlight: 1927 ims_pcu_destroy_backlight(pcu); 1928 return error; 1929 } 1930 1931 static void ims_pcu_destroy_application_mode(struct ims_pcu *pcu) 1932 { 1933 if (pcu->setup_complete) { 1934 pcu->setup_complete = false; 1935 mb(); /* make sure flag setting is not reordered */ 1936 1937 if (pcu->gamepad) 1938 ims_pcu_destroy_gamepad(pcu); 1939 ims_pcu_destroy_buttons(pcu); 1940 ims_pcu_destroy_backlight(pcu); 1941 } 1942 } 1943 1944 static int ims_pcu_init_bootloader_mode(struct ims_pcu *pcu) 1945 { 1946 int error; 1947 1948 error = ims_pcu_execute_bl_command(pcu, QUERY_DEVICE, NULL, 0, 1949 IMS_PCU_CMD_RESPONSE_TIMEOUT); 1950 if (error) { 1951 dev_err(pcu->dev, "Bootloader does not respond, aborting\n"); 1952 return error; 1953 } 1954 1955 pcu->fw_start_addr = 1956 get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 11]); 1957 pcu->fw_end_addr = 1958 get_unaligned_le32(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET + 15]); 1959 1960 dev_info(pcu->dev, 1961 "Device is in bootloader mode (addr 0x%08x-0x%08x), requesting firmware\n", 1962 pcu->fw_start_addr, pcu->fw_end_addr); 1963 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); 1968 if (error) { 1969 /* This error is not fatal, let userspace have another chance */ 1970 complete(&pcu->async_firmware_done); 1971 } 1972 1973 return 0; 1974 } 1975 1976 static void ims_pcu_destroy_bootloader_mode(struct ims_pcu *pcu) 1977 { 1978 /* Make sure our initial firmware request has completed */ 1979 wait_for_completion(&pcu->async_firmware_done); 1980 } 1981 1982 #define IMS_PCU_APPLICATION_MODE 0 1983 #define IMS_PCU_BOOTLOADER_MODE 1 1984 1985 static struct usb_driver ims_pcu_driver; 1986 1987 static int ims_pcu_probe(struct usb_interface *intf, 1988 const struct usb_device_id *id) 1989 { 1990 struct usb_device *udev = interface_to_usbdev(intf); 1991 struct ims_pcu *pcu; 1992 int error; 1993 1994 pcu = kzalloc(sizeof(*pcu), GFP_KERNEL); 1995 if (!pcu) 1996 return -ENOMEM; 1997 1998 pcu->dev = &intf->dev; 1999 pcu->udev = udev; 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); 2004 2005 error = ims_pcu_parse_cdc_data(intf, pcu); 2006 if (error) 2007 goto err_free_mem; 2008 2009 error = usb_driver_claim_interface(&ims_pcu_driver, 2010 pcu->data_intf, pcu); 2011 if (error) { 2012 dev_err(&intf->dev, 2013 "Unable to claim corresponding data interface: %d\n", 2014 error); 2015 goto err_free_mem; 2016 } 2017 2018 usb_set_intfdata(pcu->ctrl_intf, pcu); 2019 2020 error = ims_pcu_buffers_alloc(pcu); 2021 if (error) 2022 goto err_unclaim_intf; 2023 2024 error = ims_pcu_start_io(pcu); 2025 if (error) 2026 goto err_free_buffers; 2027 2028 error = ims_pcu_line_setup(pcu); 2029 if (error) 2030 goto err_stop_io; 2031 2032 error = pcu->bootloader_mode ? 2033 ims_pcu_init_bootloader_mode(pcu) : 2034 ims_pcu_init_application_mode(pcu); 2035 if (error) 2036 goto err_stop_io; 2037 2038 return 0; 2039 2040 err_stop_io: 2041 ims_pcu_stop_io(pcu); 2042 err_free_buffers: 2043 ims_pcu_buffers_free(pcu); 2044 err_unclaim_intf: 2045 usb_driver_release_interface(&ims_pcu_driver, pcu->data_intf); 2046 err_free_mem: 2047 kfree(pcu); 2048 return error; 2049 } 2050 2051 static void ims_pcu_disconnect(struct usb_interface *intf) 2052 { 2053 struct ims_pcu *pcu = usb_get_intfdata(intf); 2054 struct usb_host_interface *alt = intf->cur_altsetting; 2055 2056 usb_set_intfdata(intf, NULL); 2057 2058 /* 2059 * See if we are dealing with control or data interface. The cleanup 2060 * happens when we unbind primary (control) interface. 2061 */ 2062 if (alt->desc.bInterfaceClass != USB_CLASS_COMM) 2063 return; 2064 2065 ims_pcu_stop_io(pcu); 2066 2067 if (pcu->bootloader_mode) 2068 ims_pcu_destroy_bootloader_mode(pcu); 2069 else 2070 ims_pcu_destroy_application_mode(pcu); 2071 2072 ims_pcu_buffers_free(pcu); 2073 kfree(pcu); 2074 } 2075 2076 #ifdef CONFIG_PM 2077 static int ims_pcu_suspend(struct usb_interface *intf, 2078 pm_message_t message) 2079 { 2080 struct ims_pcu *pcu = usb_get_intfdata(intf); 2081 struct usb_host_interface *alt = intf->cur_altsetting; 2082 2083 if (alt->desc.bInterfaceClass == USB_CLASS_COMM) 2084 ims_pcu_stop_io(pcu); 2085 2086 return 0; 2087 } 2088 2089 static int ims_pcu_resume(struct usb_interface *intf) 2090 { 2091 struct ims_pcu *pcu = usb_get_intfdata(intf); 2092 struct usb_host_interface *alt = intf->cur_altsetting; 2093 int retval = 0; 2094 2095 if (alt->desc.bInterfaceClass == USB_CLASS_COMM) { 2096 retval = ims_pcu_start_io(pcu); 2097 if (retval == 0) 2098 retval = ims_pcu_line_setup(pcu); 2099 } 2100 2101 return retval; 2102 } 2103 #endif 2104 2105 static const struct usb_device_id ims_pcu_id_table[] = { 2106 { 2107 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0082, 2108 USB_CLASS_COMM, 2109 USB_CDC_SUBCLASS_ACM, 2110 USB_CDC_ACM_PROTO_AT_V25TER), 2111 .driver_info = IMS_PCU_APPLICATION_MODE, 2112 }, 2113 { 2114 USB_DEVICE_AND_INTERFACE_INFO(0x04d8, 0x0083, 2115 USB_CLASS_COMM, 2116 USB_CDC_SUBCLASS_ACM, 2117 USB_CDC_ACM_PROTO_AT_V25TER), 2118 .driver_info = IMS_PCU_BOOTLOADER_MODE, 2119 }, 2120 { } 2121 }; 2122 2123 static const struct attribute_group *ims_pcu_sysfs_groups[] = { 2124 &ims_pcu_attr_group, 2125 &ims_pcu_ofn_attr_group, 2126 NULL 2127 }; 2128 2129 static struct usb_driver ims_pcu_driver = { 2130 .name = "ims_pcu", 2131 .id_table = ims_pcu_id_table, 2132 .dev_groups = ims_pcu_sysfs_groups, 2133 .probe = ims_pcu_probe, 2134 .disconnect = ims_pcu_disconnect, 2135 #ifdef CONFIG_PM 2136 .suspend = ims_pcu_suspend, 2137 .resume = ims_pcu_resume, 2138 .reset_resume = ims_pcu_resume, 2139 #endif 2140 }; 2141 2142 module_usb_driver(ims_pcu_driver); 2143 2144 MODULE_DESCRIPTION("IMS Passenger Control Unit driver"); 2145 MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>"); 2146 MODULE_LICENSE("GPL"); 2147