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