PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/input/misc/ims-pcu.c

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