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