]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/mfd/ljca.c
UBUNTU: SAUCE: mei_vsc: add ACPI HID for ADL
[mirror_ubuntu-jammy-kernel.git] / drivers / mfd / ljca.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Intel La Jolla Cove Adapter USB driver
4 *
5 * Copyright (c) 2021, Intel Corporation.
6 */
7
8 #include <linux/kernel.h>
9 #include <linux/mfd/core.h>
10 #include <linux/mfd/ljca.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/uaccess.h>
17 #include <linux/usb.h>
18
19 enum ljca_acpi_match_adr {
20 LJCA_ACPI_MATCH_GPIO,
21 LJCA_ACPI_MATCH_I2C1,
22 LJCA_ACPI_MATCH_I2C2,
23 LJCA_ACPI_MATCH_SPI1,
24 };
25
26 static struct mfd_cell_acpi_match ljca_acpi_match_gpio = {
27 .pnpid = "INTC1074",
28 };
29
30 static struct mfd_cell_acpi_match ljca_acpi_match_i2cs[] = {
31 {
32 .pnpid = "INTC1075",
33 },
34 {
35 .pnpid = "INTC1076",
36 },
37 };
38
39 static struct mfd_cell_acpi_match ljca_acpi_match_spis[] = {
40 {
41 .pnpid = "INTC1091",
42 },
43 };
44
45 struct ljca_msg {
46 u8 type;
47 u8 cmd;
48 u8 flags;
49 u8 len;
50 u8 data[];
51 } __packed;
52
53 struct fw_version {
54 u8 major;
55 u8 minor;
56 u16 patch;
57 u16 build;
58 } __packed;
59
60 /* stub types */
61 enum stub_type {
62 MNG_STUB = 1,
63 DIAG_STUB,
64 GPIO_STUB,
65 I2C_STUB,
66 SPI_STUB,
67 };
68
69 /* command Flags */
70 #define ACK_FLAG BIT(0)
71 #define RESP_FLAG BIT(1)
72 #define CMPL_FLAG BIT(2)
73
74 /* MNG stub commands */
75 enum ljca_mng_cmd {
76 MNG_GET_VERSION = 1,
77 MNG_RESET_NOTIFY,
78 MNG_RESET,
79 MNG_ENUM_GPIO,
80 MNG_ENUM_I2C,
81 MNG_POWER_STATE_CHANGE,
82 MNG_SET_DFU_MODE,
83 MNG_ENUM_SPI,
84 };
85
86 /* DIAG commands */
87 enum diag_cmd {
88 DIAG_GET_STATE = 1,
89 DIAG_GET_STATISTIC,
90 DIAG_SET_TRACE_LEVEL,
91 DIAG_SET_ECHO_MODE,
92 DIAG_GET_FW_LOG,
93 DIAG_GET_FW_COREDUMP,
94 DIAG_TRIGGER_WDT,
95 DIAG_TRIGGER_FAULT,
96 DIAG_FEED_WDT,
97 DIAG_GET_SECURE_STATE,
98 };
99
100 struct ljca_i2c_ctr_info {
101 u8 id;
102 u8 capacity;
103 u8 intr_pin;
104 } __packed;
105
106 struct ljca_i2c_descriptor {
107 u8 num;
108 struct ljca_i2c_ctr_info info[];
109 } __packed;
110
111 struct ljca_spi_ctr_info {
112 u8 id;
113 u8 capacity;
114 } __packed;
115
116 struct ljca_spi_descriptor {
117 u8 num;
118 struct ljca_spi_ctr_info info[];
119 } __packed;
120
121 struct ljca_bank_descriptor {
122 u8 bank_id;
123 u8 pin_num;
124
125 /* 1 bit for each gpio, 1 means valid */
126 u32 valid_pins;
127 } __packed;
128
129 struct ljca_gpio_descriptor {
130 u8 pins_per_bank;
131 u8 bank_num;
132 struct ljca_bank_descriptor bank_desc[];
133 } __packed;
134
135 #define MAX_PACKET_SIZE 64
136 #define MAX_PAYLOAD_SIZE (MAX_PACKET_SIZE - sizeof(struct ljca_msg))
137 #define USB_WRITE_TIMEOUT 200
138 #define USB_WRITE_ACK_TIMEOUT 500
139
140 struct ljca_event_cb_entry {
141 struct platform_device *pdev;
142 ljca_event_cb_t notify;
143 };
144
145 struct ljca_stub_packet {
146 u8 *ibuf;
147 u32 ibuf_len;
148 };
149
150 struct ljca_stub {
151 struct list_head list;
152 u8 type;
153 struct usb_interface *intf;
154 struct mutex mutex;
155 spinlock_t event_cb_lock;
156
157 struct ljca_stub_packet ipacket;
158
159 /* for identify ack */
160 bool acked;
161 int cur_cmd;
162
163 struct ljca_event_cb_entry event_entry;
164 };
165
166 static inline void *ljca_priv(const struct ljca_stub *stub)
167 {
168 return (char *)stub + sizeof(struct ljca_stub);
169 }
170
171 enum ljca_state {
172 LJCA_STOPPED,
173 LJCA_INITED,
174 LJCA_RESET_HANDSHAKE,
175 LJCA_RESET_SYNCED,
176 LJCA_ENUM_GPIO_COMPLETE,
177 LJCA_ENUM_I2C_COMPLETE,
178 LJCA_ENUM_SPI_COMPLETE,
179 LJCA_SUSPEND,
180 LJCA_STARTED,
181 LJCA_FAILED,
182 };
183
184 struct ljca_dev {
185 struct usb_device *udev;
186 struct usb_interface *intf;
187 u8 in_ep; /* the address of the bulk in endpoint */
188 u8 out_ep; /* the address of the bulk out endpoint */
189
190 /* the urb/buffer for read */
191 struct urb *in_urb;
192 unsigned char *ibuf;
193 size_t ibuf_len;
194
195 int state;
196
197 atomic_t active_transfers;
198 wait_queue_head_t disconnect_wq;
199
200 struct list_head stubs_list;
201
202 /* to wait for an ongoing write ack */
203 wait_queue_head_t ack_wq;
204
205 struct mfd_cell *cells;
206 int cell_count;
207 };
208
209 static bool ljca_validate(void *data, u32 data_len)
210 {
211 struct ljca_msg *header = (struct ljca_msg *)data;
212
213 return (header->len + sizeof(*header) == data_len);
214 }
215
216
217 void ljca_dump(struct ljca_dev *ljca, void *buf, int len)
218 {
219 int i;
220 u8 tmp[256] = { 0 };
221 int n = 0;
222
223 if (!len)
224 return;
225
226 for (i = 0; i < len; i++)
227 n += scnprintf(tmp + n, sizeof(tmp) - n - 1, "%02x ", ((u8*)buf)[i]);
228
229 dev_dbg(&ljca->intf->dev, "%s\n", tmp);
230 }
231
232 static struct ljca_stub *ljca_stub_alloc(struct ljca_dev *ljca, int priv_size)
233 {
234 struct ljca_stub *stub;
235
236 stub = kzalloc(sizeof(*stub) + priv_size, GFP_KERNEL);
237 if (!stub)
238 return ERR_PTR(-ENOMEM);
239
240 mutex_init(&stub->mutex);
241 spin_lock_init(&stub->event_cb_lock);
242 INIT_LIST_HEAD(&stub->list);
243 list_add_tail(&stub->list, &ljca->stubs_list);
244 dev_dbg(&ljca->intf->dev, "enuming a stub success\n");
245 return stub;
246 }
247
248 static struct ljca_stub *ljca_stub_find(struct ljca_dev *ljca, u8 type)
249 {
250 struct ljca_stub *stub;
251
252 list_for_each_entry (stub, &ljca->stubs_list, list) {
253 if (stub->type == type)
254 return stub;
255 }
256
257 dev_err(&ljca->intf->dev, "usb stub not find, type: %d", type);
258 return ERR_PTR(-ENODEV);
259 }
260
261 static void ljca_stub_notify(struct ljca_stub *stub, u8 cmd,
262 const void *evt_data, int len)
263 {
264 unsigned long flags;
265 spin_lock_irqsave(&stub->event_cb_lock, flags);
266 if (stub->event_entry.notify && stub->event_entry.pdev)
267 stub->event_entry.notify(stub->event_entry.pdev, cmd, evt_data,
268 len);
269 spin_unlock_irqrestore(&stub->event_cb_lock, flags);
270 }
271
272 static int ljca_parse(struct ljca_dev *ljca, struct ljca_msg *header)
273 {
274 struct ljca_stub *stub;
275
276 stub = ljca_stub_find(ljca, header->type);
277 if (IS_ERR(stub))
278 return PTR_ERR(stub);
279
280 if (!(header->flags & ACK_FLAG)) {
281 ljca_stub_notify(stub, header->cmd, header->data, header->len);
282 return 0;
283 }
284
285 if (stub->cur_cmd != header->cmd) {
286 dev_err(&ljca->intf->dev, "header->cmd:%x != stub->cur_cmd:%x",
287 header->cmd, stub->cur_cmd);
288 return -EINVAL;
289 }
290
291 stub->ipacket.ibuf_len = header->len;
292 if (stub->ipacket.ibuf)
293 memcpy(stub->ipacket.ibuf, header->data, header->len);
294
295 stub->acked = true;
296 wake_up(&ljca->ack_wq);
297
298 return 0;
299 }
300
301 static int ljca_stub_write(struct ljca_stub *stub, u8 cmd, const void *obuf,
302 int obuf_len, void *ibuf, int *ibuf_len, bool wait_ack)
303 {
304 struct ljca_msg *header;
305 struct ljca_dev *ljca = usb_get_intfdata(stub->intf);
306 int ret;
307 u8 flags = CMPL_FLAG;
308 int actual;
309
310 atomic_inc(&ljca->active_transfers);
311 if (ljca->state == LJCA_STOPPED)
312 return -ENODEV;
313
314 if (obuf_len > MAX_PAYLOAD_SIZE)
315 return -EINVAL;
316
317 if (wait_ack)
318 flags |= ACK_FLAG;
319
320 stub->ipacket.ibuf_len = 0;
321 header = kmalloc(sizeof(*header) + obuf_len, GFP_KERNEL);
322 if (!header)
323 return -ENOMEM;
324
325 header->type = stub->type;
326 header->cmd = cmd;
327 header->flags = flags;
328 header->len = obuf_len;
329
330 memcpy(header->data, obuf, obuf_len);
331 dev_dbg(&ljca->intf->dev, "send: type:%d cmd:%d flags:%d len:%d\n",
332 header->type, header->cmd, header->flags, header->len);
333 ljca_dump(ljca, header->data, header->len);
334
335 mutex_lock(&stub->mutex);
336 stub->cur_cmd = cmd;
337 stub->ipacket.ibuf = ibuf;
338 stub->acked = false;
339 usb_autopm_get_interface(ljca->intf);
340 ret = usb_bulk_msg(ljca->udev,
341 usb_sndbulkpipe(ljca->udev, ljca->out_ep), header,
342 sizeof(struct ljca_msg) + obuf_len, &actual,
343 USB_WRITE_TIMEOUT);
344 kfree(header);
345 if (ret || actual != sizeof(struct ljca_msg) + obuf_len) {
346 dev_err(&ljca->intf->dev,
347 "bridge write failed ret:%d total_len:%d\n ", ret,
348 actual);
349 goto error;
350 }
351
352 usb_autopm_put_interface(ljca->intf);
353
354 if (wait_ack) {
355 ret = wait_event_timeout(
356 ljca->ack_wq, stub->acked,
357 msecs_to_jiffies(USB_WRITE_ACK_TIMEOUT));
358 if (!ret || !stub->acked) {
359 dev_err(&ljca->intf->dev,
360 "acked sem wait timed out ret:%d timeout:%d ack:%d\n",
361 ret, USB_WRITE_ACK_TIMEOUT, stub->acked);
362 ret = -ETIMEDOUT;
363 goto error;
364 }
365 }
366
367 if (ibuf_len)
368 *ibuf_len = stub->ipacket.ibuf_len;
369
370 stub->ipacket.ibuf = NULL;
371 stub->ipacket.ibuf_len = 0;
372 ret = 0;
373 error:
374 atomic_dec(&ljca->active_transfers);
375 if (ljca->state == LJCA_STOPPED)
376 wake_up(&ljca->disconnect_wq);
377
378 mutex_unlock(&stub->mutex);
379 return ret;
380 }
381
382 static int ljca_transfer_internal(struct platform_device *pdev, u8 cmd, const void *obuf,
383 int obuf_len, void *ibuf, int *ibuf_len, bool wait_ack)
384 {
385 struct ljca_platform_data *ljca_pdata;
386 struct ljca_dev *ljca;
387 struct ljca_stub *stub;
388
389 if (!pdev)
390 return -EINVAL;
391
392 ljca = dev_get_drvdata(pdev->dev.parent);
393 ljca_pdata = dev_get_platdata(&pdev->dev);
394 stub = ljca_stub_find(ljca, ljca_pdata->type);
395 if (IS_ERR(stub))
396 return PTR_ERR(stub);
397
398 return ljca_stub_write(stub, cmd, obuf, obuf_len, ibuf, ibuf_len, wait_ack);
399 }
400
401 int ljca_transfer(struct platform_device *pdev, u8 cmd, const void *obuf,
402 int obuf_len, void *ibuf, int *ibuf_len)
403 {
404 return ljca_transfer_internal(pdev, cmd, obuf, obuf_len, ibuf, ibuf_len,
405 true);
406 }
407 EXPORT_SYMBOL_GPL(ljca_transfer);
408
409 int ljca_transfer_noack(struct platform_device *pdev, u8 cmd, const void *obuf,
410 int obuf_len)
411 {
412 return ljca_transfer_internal(pdev, cmd, obuf, obuf_len, NULL, NULL,
413 false);
414 }
415 EXPORT_SYMBOL_GPL(ljca_transfer_noack);
416
417 int ljca_register_event_cb(struct platform_device *pdev,
418 ljca_event_cb_t event_cb)
419 {
420 struct ljca_platform_data *ljca_pdata;
421 struct ljca_dev *ljca;
422 struct ljca_stub *stub;
423 unsigned long flags;
424
425 if (!pdev)
426 return -EINVAL;
427
428 ljca = dev_get_drvdata(pdev->dev.parent);
429 ljca_pdata = dev_get_platdata(&pdev->dev);
430 stub = ljca_stub_find(ljca, ljca_pdata->type);
431 if (IS_ERR(stub))
432 return PTR_ERR(stub);
433
434 spin_lock_irqsave(&stub->event_cb_lock, flags);
435 stub->event_entry.notify = event_cb;
436 stub->event_entry.pdev = pdev;
437 spin_unlock_irqrestore(&stub->event_cb_lock, flags);
438
439 return 0;
440 }
441 EXPORT_SYMBOL_GPL(ljca_register_event_cb);
442
443 void ljca_unregister_event_cb(struct platform_device *pdev)
444 {
445 struct ljca_platform_data *ljca_pdata;
446 struct ljca_dev *ljca;
447 struct ljca_stub *stub;
448 unsigned long flags;
449
450 ljca = dev_get_drvdata(pdev->dev.parent);
451 ljca_pdata = dev_get_platdata(&pdev->dev);
452 stub = ljca_stub_find(ljca, ljca_pdata->type);
453 if (IS_ERR(stub))
454 return;
455
456 spin_lock_irqsave(&stub->event_cb_lock, flags);
457 stub->event_entry.notify = NULL;
458 stub->event_entry.pdev = NULL;
459 spin_unlock_irqrestore(&stub->event_cb_lock, flags);
460 }
461 EXPORT_SYMBOL_GPL(ljca_unregister_event_cb);
462
463 static void ljca_stub_cleanup(struct ljca_dev *ljca)
464 {
465 struct ljca_stub *stub;
466 struct ljca_stub *next;
467
468 list_for_each_entry_safe (stub, next, &ljca->stubs_list, list) {
469 list_del_init(&stub->list);
470 mutex_destroy(&stub->mutex);
471 kfree(stub);
472 }
473 }
474
475 static void ljca_read_complete(struct urb *urb)
476 {
477 struct ljca_dev *ljca = urb->context;
478 struct ljca_msg *header = urb->transfer_buffer;
479 int len = urb->actual_length;
480 int ret;
481
482 dev_dbg(&ljca->intf->dev,
483 "bulk read urb got message from fw, status:%d data_len:%d\n",
484 urb->status, urb->actual_length);
485
486 if (urb->status) {
487 /* sync/async unlink faults aren't errors */
488 if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
489 urb->status == -ESHUTDOWN)
490 return;
491
492 dev_err(&ljca->intf->dev, "read bulk urb transfer failed: %d\n",
493 urb->status);
494 goto resubmit;
495 }
496
497 dev_dbg(&ljca->intf->dev, "receive: type:%d cmd:%d flags:%d len:%d\n",
498 header->type, header->cmd, header->flags, header->len);
499 ljca_dump(ljca, header->data, header->len);
500
501 if (!ljca_validate(header, len)) {
502 dev_err(&ljca->intf->dev,
503 "data not correct header->len:%d payload_len:%d\n ",
504 header->len, len);
505 goto resubmit;
506 }
507
508 ret = ljca_parse(ljca, header);
509 if (ret)
510 dev_err(&ljca->intf->dev,
511 "failed to parse data: ret:%d type:%d len: %d", ret,
512 header->type, header->len);
513
514 resubmit:
515 ret = usb_submit_urb(urb, GFP_KERNEL);
516 if (ret)
517 dev_err(&ljca->intf->dev,
518 "failed submitting read urb, error %d\n", ret);
519 }
520
521 static int ljca_start(struct ljca_dev *ljca)
522 {
523 int ret;
524
525 usb_fill_bulk_urb(ljca->in_urb, ljca->udev,
526 usb_rcvbulkpipe(ljca->udev, ljca->in_ep), ljca->ibuf,
527 ljca->ibuf_len, ljca_read_complete, ljca);
528
529 ret = usb_submit_urb(ljca->in_urb, GFP_KERNEL);
530 if (ret) {
531 dev_err(&ljca->intf->dev,
532 "failed submitting read urb, error %d\n", ret);
533 }
534 return ret;
535 }
536
537 struct ljca_mng_priv {
538 long reset_id;
539 };
540
541 static int ljca_mng_reset_handshake(struct ljca_stub *stub)
542 {
543 int ret;
544 struct ljca_mng_priv *priv;
545 __le32 reset_id;
546 __le32 reset_id_ret = 0;
547 int ilen;
548
549 priv = ljca_priv(stub);
550 reset_id = cpu_to_le32(priv->reset_id++);
551 ret = ljca_stub_write(stub, MNG_RESET_NOTIFY, &reset_id,
552 sizeof(reset_id), &reset_id_ret, &ilen, true);
553 if (ret || ilen != sizeof(reset_id_ret) || reset_id_ret != reset_id) {
554 dev_err(&stub->intf->dev,
555 "MNG_RESET_NOTIFY failed reset_id:%d/%d ret:%d\n",
556 le32_to_cpu(reset_id_ret), le32_to_cpu(reset_id), ret);
557 return -EIO;
558 }
559
560 return 0;
561 }
562
563 static inline int ljca_mng_reset(struct ljca_stub *stub)
564 {
565 return ljca_stub_write(stub, MNG_RESET, NULL, 0, NULL, NULL, true);
566 }
567
568 static int ljca_add_mfd_cell(struct ljca_dev *ljca, struct mfd_cell *cell)
569 {
570 struct mfd_cell *new_cells;
571 new_cells = krealloc_array(ljca->cells, (ljca->cell_count + 1),
572 sizeof(struct mfd_cell), GFP_KERNEL);
573 if (!new_cells)
574 return -ENOMEM;
575
576 memcpy(&new_cells[ljca->cell_count], cell, sizeof(*cell));
577 ljca->cells = new_cells;
578 ljca->cell_count++;
579
580 return 0;
581 }
582
583 static int ljca_gpio_stub_init(struct ljca_dev *ljca,
584 struct ljca_gpio_descriptor *desc)
585 {
586 struct ljca_stub *stub;
587 struct mfd_cell cell = { 0 };
588 struct ljca_platform_data *pdata;
589 int gpio_num = desc->pins_per_bank * desc->bank_num;
590 int i;
591 u32 valid_pin[MAX_GPIO_NUM / (sizeof(u32) * BITS_PER_BYTE)];
592
593 if (gpio_num > MAX_GPIO_NUM)
594 return -EINVAL;
595
596 stub = ljca_stub_alloc(ljca, sizeof(*pdata));
597 if (IS_ERR(stub))
598 return PTR_ERR(stub);
599
600 stub->type = GPIO_STUB;
601 stub->intf = ljca->intf;
602
603 pdata = ljca_priv(stub);
604 pdata->type = stub->type;
605 pdata->gpio_info.num = gpio_num;
606
607 for (i = 0; i < desc->bank_num; i++)
608 valid_pin[i] = desc->bank_desc[i].valid_pins;
609
610 bitmap_from_arr32(pdata->gpio_info.valid_pin_map, valid_pin, gpio_num);
611
612 cell.name = "ljca-gpio";
613 cell.platform_data = pdata;
614 cell.pdata_size = sizeof(*pdata);
615 cell.acpi_match = &ljca_acpi_match_gpio;
616
617 return ljca_add_mfd_cell(ljca, &cell);
618 }
619
620 static int ljca_mng_enum_gpio(struct ljca_stub *stub)
621 {
622 struct ljca_dev *ljca = usb_get_intfdata(stub->intf);
623 struct ljca_gpio_descriptor *desc;
624 int ret;
625 int len;
626
627 desc = kzalloc(MAX_PAYLOAD_SIZE, GFP_KERNEL);
628 if (!desc)
629 return -ENOMEM;
630
631 ret = ljca_stub_write(stub, MNG_ENUM_GPIO, NULL, 0, desc, &len, true);
632 if (ret || len != sizeof(*desc) + desc->bank_num *
633 sizeof(desc->bank_desc[0])) {
634 dev_err(&stub->intf->dev,
635 "enum gpio failed ret:%d len:%d bank_num:%d\n", ret,
636 len, desc->bank_num);
637 }
638
639 ret = ljca_gpio_stub_init(ljca, desc);
640 kfree(desc);
641 return ret;
642 }
643
644 static int ljca_i2c_stub_init(struct ljca_dev *ljca,
645 struct ljca_i2c_descriptor *desc)
646 {
647 struct ljca_stub *stub;
648 struct ljca_platform_data *pdata;
649 int i;
650 int ret;
651
652 stub = ljca_stub_alloc(ljca, desc->num * sizeof(*pdata));
653 if (IS_ERR(stub))
654 return PTR_ERR(stub);
655
656 stub->type = I2C_STUB;
657 stub->intf = ljca->intf;
658 pdata = ljca_priv(stub);
659
660 for (i = 0; i < desc->num; i++) {
661 struct mfd_cell cell = { 0 };
662 pdata[i].type = stub->type;
663
664 pdata[i].i2c_info.id = desc->info[i].id;
665 pdata[i].i2c_info.capacity = desc->info[i].capacity;
666 pdata[i].i2c_info.intr_pin = desc->info[i].intr_pin;
667
668 cell.name = "ljca-i2c";
669 cell.platform_data = &pdata[i];
670 cell.pdata_size = sizeof(pdata[i]);
671 if (i < ARRAY_SIZE(ljca_acpi_match_i2cs))
672 cell.acpi_match = &ljca_acpi_match_i2cs[i];
673
674 ret = ljca_add_mfd_cell(ljca, &cell);
675 if (ret)
676 return ret;
677 }
678
679 return 0;
680 }
681
682 static int ljca_mng_enum_i2c(struct ljca_stub *stub)
683 {
684 struct ljca_dev *ljca = usb_get_intfdata(stub->intf);
685 struct ljca_i2c_descriptor *desc;
686 int ret;
687 int len;
688
689 desc = kzalloc(MAX_PAYLOAD_SIZE, GFP_KERNEL);
690 if (!desc)
691 return -ENOMEM;
692
693 ret = ljca_stub_write(stub, MNG_ENUM_I2C, NULL, 0, desc, &len, true);
694 if (ret) {
695 dev_err(&stub->intf->dev,
696 "MNG_ENUM_I2C failed ret:%d len:%d num:%d\n", ret, len,
697 desc->num);
698 kfree(desc);
699 return -EIO;
700 }
701
702 ret = ljca_i2c_stub_init(ljca, desc);
703 kfree(desc);
704 return ret;
705 }
706
707 static int ljca_spi_stub_init(struct ljca_dev *ljca,
708 struct ljca_spi_descriptor *desc)
709 {
710 struct ljca_stub *stub;
711 struct ljca_platform_data *pdata;
712 int i;
713 int ret;
714
715 stub = ljca_stub_alloc(ljca, desc->num * sizeof(*pdata));
716 if (IS_ERR(stub))
717 return PTR_ERR(stub);
718
719 stub->type = SPI_STUB;
720 stub->intf = ljca->intf;
721 pdata = ljca_priv(stub);
722
723 for (i = 0; i < desc->num; i++) {
724 struct mfd_cell cell = { 0 };
725 pdata[i].type = stub->type;
726
727 pdata[i].spi_info.id = desc->info[i].id;
728 pdata[i].spi_info.capacity = desc->info[i].capacity;
729
730 cell.name = "ljca-spi";
731 cell.platform_data = &pdata[i];
732 cell.pdata_size = sizeof(pdata[i]);
733 if (i < ARRAY_SIZE(ljca_acpi_match_spis))
734 cell.acpi_match = &ljca_acpi_match_spis[i];
735
736 ret = ljca_add_mfd_cell(ljca, &cell);
737 if (ret)
738 return ret;
739 }
740
741 return 0;
742 }
743
744 static int ljca_mng_enum_spi(struct ljca_stub *stub)
745 {
746 struct ljca_dev *ljca = usb_get_intfdata(stub->intf);
747 struct ljca_spi_descriptor *desc;
748 int ret;
749 int len;
750
751 desc = kzalloc(MAX_PAYLOAD_SIZE, GFP_KERNEL);
752 if (!desc)
753 return -ENOMEM;
754
755 ret = ljca_stub_write(stub, MNG_ENUM_SPI, NULL, 0, desc, &len, true);
756 if (ret) {
757 dev_err(&stub->intf->dev,
758 "MNG_ENUM_I2C failed ret:%d len:%d num:%d\n", ret, len,
759 desc->num);
760 kfree(desc);
761 return -EIO;
762 }
763
764 ret = ljca_spi_stub_init(ljca, desc);
765 kfree(desc);
766 return ret;
767 }
768
769 static int ljca_mng_get_version(struct ljca_stub *stub, char *buf)
770 {
771 struct fw_version version = {0};
772 int ret;
773 int len;
774
775 if (!buf)
776 return -EINVAL;
777
778 ret = ljca_stub_write(stub, MNG_GET_VERSION, NULL, 0, &version, &len, true);
779 if (ret || len < sizeof(struct fw_version)) {
780 dev_err(&stub->intf->dev,
781 "MNG_GET_VERSION failed ret:%d len:%d\n", ret, len);
782 return ret;
783 }
784
785 return sysfs_emit(buf, "%d.%d.%d.%d\n", version.major, version.minor,
786 le16_to_cpu(version.patch), le16_to_cpu(version.build));
787 }
788
789 static inline int ljca_mng_set_dfu_mode(struct ljca_stub *stub)
790 {
791 return ljca_stub_write(stub, MNG_SET_DFU_MODE, NULL, 0, NULL, NULL, true);
792 }
793
794 static int ljca_mng_link(struct ljca_dev *ljca, struct ljca_stub *stub)
795 {
796 int ret;
797
798 ret = ljca_mng_reset_handshake(stub);
799 if (ret)
800 return ret;
801
802 ljca->state = LJCA_RESET_SYNCED;
803
804 ret = ljca_mng_enum_gpio(stub);
805 if (ret)
806 return ret;
807
808 ljca->state = LJCA_ENUM_GPIO_COMPLETE;
809
810 ret = ljca_mng_enum_i2c(stub);
811 if (ret)
812 return ret;
813
814 ljca->state = LJCA_ENUM_I2C_COMPLETE;
815
816 ret = ljca_mng_enum_spi(stub);
817 if (ret)
818 return ret;
819
820 ljca->state = LJCA_ENUM_SPI_COMPLETE;
821 return ret;
822 }
823
824 static int ljca_mng_init(struct ljca_dev *ljca)
825 {
826 struct ljca_stub *stub;
827 struct ljca_mng_priv *priv;
828 int ret;
829
830 stub = ljca_stub_alloc(ljca, sizeof(*priv));
831 if (IS_ERR(stub))
832 return PTR_ERR(stub);
833
834 priv = ljca_priv(stub);
835 if (!priv)
836 return -ENOMEM;
837
838 priv->reset_id = 0;
839 stub->type = MNG_STUB;
840 stub->intf = ljca->intf;
841
842 ret = ljca_mng_link(ljca, stub);
843 if (ret)
844 dev_err(&ljca->intf->dev,
845 "mng stub link done ret:%d state:%d\n", ret,
846 ljca->state);
847
848 return ret;
849 }
850
851 static inline int ljca_diag_get_fw_log(struct ljca_stub *stub, void *buf)
852 {
853 int ret;
854 int len;
855
856 if (!buf)
857 return -EINVAL;
858
859 ret = ljca_stub_write(stub, DIAG_GET_FW_LOG, NULL, 0, buf, &len, true);
860 if (ret)
861 return ret;
862
863 return len;
864 }
865
866 static inline int ljca_diag_get_coredump(struct ljca_stub *stub, void *buf)
867 {
868 int ret;
869 int len;
870
871 if (!buf)
872 return -EINVAL;
873
874 ret = ljca_stub_write(stub, DIAG_GET_FW_COREDUMP, NULL, 0, buf, &len, true);
875 if (ret)
876 return ret;
877
878 return len;
879 }
880
881 static inline int ljca_diag_set_trace_level(struct ljca_stub *stub, u8 level)
882 {
883 return ljca_stub_write(stub, DIAG_SET_TRACE_LEVEL, &level,
884 sizeof(level), NULL, NULL, true);
885 }
886
887 static int ljca_diag_init(struct ljca_dev *ljca)
888 {
889 struct ljca_stub *stub;
890
891 stub = ljca_stub_alloc(ljca, 0);
892 if (IS_ERR(stub))
893 return PTR_ERR(stub);
894
895 stub->type = DIAG_STUB;
896 stub->intf = ljca->intf;
897 return 0;
898 }
899
900 static void ljca_delete(struct ljca_dev *ljca)
901 {
902 usb_free_urb(ljca->in_urb);
903 usb_put_intf(ljca->intf);
904 usb_put_dev(ljca->udev);
905 kfree(ljca->ibuf);
906 kfree(ljca->cells);
907 kfree(ljca);
908 }
909
910 static int ljca_init(struct ljca_dev *ljca)
911 {
912 init_waitqueue_head(&ljca->ack_wq);
913 init_waitqueue_head(&ljca->disconnect_wq);
914 INIT_LIST_HEAD(&ljca->stubs_list);
915
916 ljca->state = LJCA_INITED;
917
918 return 0;
919 }
920
921 static void ljca_stop(struct ljca_dev *ljca)
922 {
923 ljca->state = LJCA_STOPPED;
924 wait_event_interruptible(ljca->disconnect_wq,
925 !atomic_read(&ljca->active_transfers));
926 usb_kill_urb(ljca->in_urb);
927 }
928
929 static ssize_t cmd_store(struct device *dev, struct device_attribute *attr,
930 const char *buf, size_t count)
931 {
932 struct usb_interface *intf = to_usb_interface(dev);
933 struct ljca_dev *ljca = usb_get_intfdata(intf);
934 struct ljca_stub *mng_stub = ljca_stub_find(ljca, MNG_STUB);
935 struct ljca_stub *diag_stub = ljca_stub_find(ljca, DIAG_STUB);
936
937 if (sysfs_streq(buf, "dfu"))
938 ljca_mng_set_dfu_mode(mng_stub);
939 else if (sysfs_streq(buf, "reset"))
940 ljca_mng_reset(mng_stub);
941 else if (sysfs_streq(buf, "debug"))
942 ljca_diag_set_trace_level(diag_stub, 3);
943
944 return count;
945 }
946
947 static ssize_t cmd_show(struct device *dev, struct device_attribute *attr,
948 char *buf)
949 {
950 return sysfs_emit(buf, "%s\n", "supported cmd: [dfu, reset, debug]");
951 }
952 static DEVICE_ATTR_RW(cmd);
953
954 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
955 char *buf)
956 {
957 struct usb_interface *intf = to_usb_interface(dev);
958 struct ljca_dev *ljca = usb_get_intfdata(intf);
959 struct ljca_stub *stub = ljca_stub_find(ljca, MNG_STUB);
960
961 return ljca_mng_get_version(stub, buf);
962 }
963 static DEVICE_ATTR_RO(version);
964
965 static ssize_t log_show(struct device *dev, struct device_attribute *attr,
966 char *buf)
967 {
968 struct usb_interface *intf = to_usb_interface(dev);
969 struct ljca_dev *ljca = usb_get_intfdata(intf);
970 struct ljca_stub *diag_stub = ljca_stub_find(ljca, DIAG_STUB);
971
972 return ljca_diag_get_fw_log(diag_stub, buf);
973 }
974 static DEVICE_ATTR_RO(log);
975
976 static ssize_t coredump_show(struct device *dev, struct device_attribute *attr,
977 char *buf)
978 {
979 struct usb_interface *intf = to_usb_interface(dev);
980 struct ljca_dev *ljca = usb_get_intfdata(intf);
981 struct ljca_stub *diag_stub = ljca_stub_find(ljca, DIAG_STUB);
982
983 return ljca_diag_get_coredump(diag_stub, buf);
984 }
985 static DEVICE_ATTR_RO(coredump);
986
987 static struct attribute *ljca_attrs[] = {
988 &dev_attr_version.attr,
989 &dev_attr_cmd.attr,
990 &dev_attr_log.attr,
991 &dev_attr_coredump.attr,
992 NULL,
993 };
994 ATTRIBUTE_GROUPS(ljca);
995
996 static int ljca_probe(struct usb_interface *intf,
997 const struct usb_device_id *id)
998 {
999 struct ljca_dev *ljca;
1000 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
1001 int ret;
1002
1003 /* allocate memory for our device state and initialize it */
1004 ljca = kzalloc(sizeof(*ljca), GFP_KERNEL);
1005 if (!ljca)
1006 return -ENOMEM;
1007
1008 ljca_init(ljca);
1009 ljca->udev = usb_get_dev(interface_to_usbdev(intf));
1010 ljca->intf = usb_get_intf(intf);
1011
1012 /* set up the endpoint information use only the first bulk-in and bulk-out endpoints */
1013 ret = usb_find_common_endpoints(intf->cur_altsetting, &bulk_in,
1014 &bulk_out, NULL, NULL);
1015 if (ret) {
1016 dev_err(&intf->dev,
1017 "Could not find both bulk-in and bulk-out endpoints\n");
1018 goto error;
1019 }
1020
1021 ljca->ibuf_len = usb_endpoint_maxp(bulk_in);
1022 ljca->in_ep = bulk_in->bEndpointAddress;
1023 ljca->ibuf = kzalloc(ljca->ibuf_len, GFP_KERNEL);
1024 if (!ljca->ibuf) {
1025 ret = -ENOMEM;
1026 goto error;
1027 }
1028
1029 ljca->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1030 if (!ljca->in_urb) {
1031 ret = -ENOMEM;
1032 goto error;
1033 }
1034
1035 ljca->out_ep = bulk_out->bEndpointAddress;
1036 dev_dbg(&intf->dev, "bulk_in size:%zu addr:%d bulk_out addr:%d\n",
1037 ljca->ibuf_len, ljca->in_ep, ljca->out_ep);
1038
1039 /* save our data pointer in this intf device */
1040 usb_set_intfdata(intf, ljca);
1041 ret = ljca_start(ljca);
1042 if (ret) {
1043 dev_err(&intf->dev, "bridge read start failed ret %d\n", ret);
1044 goto error;
1045 }
1046
1047 ret = ljca_mng_init(ljca);
1048 if (ret) {
1049 dev_err(&intf->dev, "register mng stub failed ret %d\n", ret);
1050 goto error;
1051 }
1052
1053 ret = ljca_diag_init(ljca);
1054 if (ret) {
1055 dev_err(&intf->dev, "register diag stub failed ret %d\n", ret);
1056 goto error;
1057 }
1058
1059 ret = mfd_add_hotplug_devices(&intf->dev, ljca->cells,
1060 ljca->cell_count);
1061 if (ret) {
1062 dev_err(&intf->dev, "failed to add mfd devices to core %d\n",
1063 ljca->cell_count);
1064 goto error;
1065 }
1066
1067 usb_enable_autosuspend(ljca->udev);
1068 ljca->state = LJCA_STARTED;
1069 dev_info(&intf->dev, "LJCA USB device init success\n");
1070 return 0;
1071
1072 error:
1073 dev_err(&intf->dev, "LJCA USB device init failed\n");
1074 /* this frees allocated memory */
1075 ljca_stub_cleanup(ljca);
1076 ljca_delete(ljca);
1077 return ret;
1078 }
1079
1080 static void ljca_disconnect(struct usb_interface *intf)
1081 {
1082 struct ljca_dev *ljca;
1083
1084 ljca = usb_get_intfdata(intf);
1085
1086 ljca_stop(ljca);
1087 mfd_remove_devices(&intf->dev);
1088 ljca_stub_cleanup(ljca);
1089 usb_set_intfdata(intf, NULL);
1090 ljca_delete(ljca);
1091 dev_dbg(&intf->dev, "LJCA disconnected\n");
1092 }
1093
1094 static int ljca_suspend(struct usb_interface *intf, pm_message_t message)
1095 {
1096 struct ljca_dev *ljca = usb_get_intfdata(intf);
1097
1098 ljca_stop(ljca);
1099 ljca->state = LJCA_SUSPEND;
1100
1101 dev_dbg(&intf->dev, "LJCA suspend\n");
1102 return 0;
1103 }
1104
1105 static int ljca_resume(struct usb_interface *intf)
1106 {
1107 struct ljca_dev *ljca = usb_get_intfdata(intf);
1108
1109 ljca->state = LJCA_STARTED;
1110 dev_dbg(&intf->dev, "LJCA resume\n");
1111 return ljca_start(ljca);
1112 }
1113
1114 static const struct usb_device_id ljca_table[] = {
1115 {USB_DEVICE(0x8086, 0x0b63)},
1116 {}
1117 };
1118 MODULE_DEVICE_TABLE(usb, ljca_table);
1119
1120 static struct usb_driver ljca_driver = {
1121 .name = "ljca",
1122 .probe = ljca_probe,
1123 .disconnect = ljca_disconnect,
1124 .suspend = ljca_suspend,
1125 .resume = ljca_resume,
1126 .id_table = ljca_table,
1127 .dev_groups = ljca_groups,
1128 .supports_autosuspend = 1,
1129 };
1130
1131 module_usb_driver(ljca_driver);
1132
1133 MODULE_AUTHOR("Ye Xiang <xiang.ye@intel.com>");
1134 MODULE_AUTHOR("Zhang Lixu <lixu.zhang@intel.com>");
1135 MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB driver");
1136 MODULE_LICENSE("GPL v2");