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