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