1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel La Jolla Cove Adapter USB driver
5 * Copyright (c) 2021, Intel Corporation.
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>
20 enum ljca_acpi_match_adr
{
27 static char *gpio_hids
[] = {
31 static struct mfd_cell_acpi_match ljca_acpi_match_gpio
;
33 static char *i2c_hids
[] = {
37 static struct mfd_cell_acpi_match ljca_acpi_match_i2cs
[2];
39 static char *spi_hids
[] = {
43 static struct mfd_cell_acpi_match ljca_acpi_match_spis
[1];
70 #define ACK_FLAG BIT(0)
71 #define RESP_FLAG BIT(1)
72 #define CMPL_FLAG BIT(2)
74 /* MNG stub commands */
81 MNG_POWER_STATE_CHANGE
,
97 DIAG_GET_SECURE_STATE
,
100 struct ljca_i2c_ctr_info
{
106 struct ljca_i2c_descriptor
{
108 struct ljca_i2c_ctr_info info
[];
111 struct ljca_spi_ctr_info
{
116 struct ljca_spi_descriptor
{
118 struct ljca_spi_ctr_info info
[];
121 struct ljca_bank_descriptor
{
125 /* 1 bit for each gpio, 1 means valid */
129 struct ljca_gpio_descriptor
{
132 struct ljca_bank_descriptor bank_desc
[];
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
140 struct ljca_event_cb_entry
{
141 struct platform_device
*pdev
;
142 ljca_event_cb_t notify
;
145 struct ljca_stub_packet
{
151 struct list_head list
;
153 struct usb_interface
*intf
;
154 spinlock_t event_cb_lock
;
156 struct ljca_stub_packet ipacket
;
158 /* for identify ack */
162 struct ljca_event_cb_entry event_entry
;
165 static inline void *ljca_priv(const struct ljca_stub
*stub
)
167 return (char *)stub
+ sizeof(struct ljca_stub
);
173 LJCA_RESET_HANDSHAKE
,
175 LJCA_ENUM_GPIO_COMPLETE
,
176 LJCA_ENUM_I2C_COMPLETE
,
177 LJCA_ENUM_SPI_COMPLETE
,
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 */
189 /* the urb/buffer for read */
196 struct list_head stubs_list
;
198 /* to wait for an ongoing write ack */
199 wait_queue_head_t ack_wq
;
201 struct mfd_cell
*cells
;
206 static int try_match_acpi_hid(struct acpi_device
*child
,
207 struct mfd_cell_acpi_match
*match
, char **hids
,
210 struct acpi_device_id ids
[2] = {};
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
];
224 static int precheck_acpi_hid(struct usb_interface
*intf
)
226 struct acpi_device
*parent
, *child
;
228 parent
= ACPI_COMPANION(&intf
->dev
);
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
));
250 static bool ljca_validate(void *data
, u32 data_len
)
252 struct ljca_msg
*header
= (struct ljca_msg
*)data
;
254 return (header
->len
+ sizeof(*header
) == data_len
);
258 void ljca_dump(struct ljca_dev
*ljca
, void *buf
, int len
)
267 for (i
= 0; i
< len
; i
++)
268 n
+= scnprintf(tmp
+ n
, sizeof(tmp
) - n
- 1, "%02x ", ((u8
*)buf
)[i
]);
270 dev_dbg(&ljca
->intf
->dev
, "%s\n", tmp
);
273 static struct ljca_stub
*ljca_stub_alloc(struct ljca_dev
*ljca
, int priv_size
)
275 struct ljca_stub
*stub
;
277 stub
= kzalloc(sizeof(*stub
) + priv_size
, GFP_KERNEL
);
279 return ERR_PTR(-ENOMEM
);
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");
288 static struct ljca_stub
*ljca_stub_find(struct ljca_dev
*ljca
, u8 type
)
290 struct ljca_stub
*stub
;
292 list_for_each_entry (stub
, &ljca
->stubs_list
, list
) {
293 if (stub
->type
== type
)
297 dev_err(&ljca
->intf
->dev
, "usb stub not find, type: %d", type
);
298 return ERR_PTR(-ENODEV
);
301 static void ljca_stub_notify(struct ljca_stub
*stub
, u8 cmd
,
302 const void *evt_data
, int len
)
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
,
309 spin_unlock_irqrestore(&stub
->event_cb_lock
, flags
);
312 static int ljca_parse(struct ljca_dev
*ljca
, struct ljca_msg
*header
)
314 struct ljca_stub
*stub
;
316 stub
= ljca_stub_find(ljca
, header
->type
);
318 return PTR_ERR(stub
);
320 if (!(header
->flags
& ACK_FLAG
)) {
321 ljca_stub_notify(stub
, header
->cmd
, header
->data
, header
->len
);
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
);
331 stub
->ipacket
.ibuf_len
= header
->len
;
332 if (stub
->ipacket
.ibuf
)
333 memcpy(stub
->ipacket
.ibuf
, header
->data
, header
->len
);
336 wake_up(&ljca
->ack_wq
);
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
)
344 struct ljca_msg
*header
;
345 struct ljca_dev
*ljca
= usb_get_intfdata(stub
->intf
);
347 u8 flags
= CMPL_FLAG
;
350 if (ljca
->state
== LJCA_STOPPED
)
353 if (obuf_len
> MAX_PAYLOAD_SIZE
)
359 stub
->ipacket
.ibuf_len
= 0;
360 header
= kmalloc(sizeof(*header
) + obuf_len
, GFP_KERNEL
);
364 header
->type
= stub
->type
;
366 header
->flags
= flags
;
367 header
->len
= obuf_len
;
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
);
374 mutex_lock(&ljca
->mutex
);
376 stub
->ipacket
.ibuf
= ibuf
;
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
,
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
,
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
);
405 *ibuf_len
= stub
->ipacket
.ibuf_len
;
407 stub
->ipacket
.ibuf
= NULL
;
408 stub
->ipacket
.ibuf_len
= 0;
411 usb_autopm_put_interface(ljca
->intf
);
412 mutex_unlock(&ljca
->mutex
);
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
)
419 struct ljca_platform_data
*ljca_pdata
;
420 struct ljca_dev
*ljca
;
421 struct ljca_stub
*stub
;
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
);
430 return PTR_ERR(stub
);
432 return ljca_stub_write(stub
, cmd
, obuf
, obuf_len
, ibuf
, ibuf_len
, wait_ack
);
435 int ljca_transfer(struct platform_device
*pdev
, u8 cmd
, const void *obuf
,
436 int obuf_len
, void *ibuf
, int *ibuf_len
)
438 return ljca_transfer_internal(pdev
, cmd
, obuf
, obuf_len
, ibuf
, ibuf_len
,
441 EXPORT_SYMBOL_GPL(ljca_transfer
);
443 int ljca_transfer_noack(struct platform_device
*pdev
, u8 cmd
, const void *obuf
,
446 return ljca_transfer_internal(pdev
, cmd
, obuf
, obuf_len
, NULL
, NULL
,
449 EXPORT_SYMBOL_GPL(ljca_transfer_noack
);
451 int ljca_register_event_cb(struct platform_device
*pdev
,
452 ljca_event_cb_t event_cb
)
454 struct ljca_platform_data
*ljca_pdata
;
455 struct ljca_dev
*ljca
;
456 struct ljca_stub
*stub
;
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
);
466 return PTR_ERR(stub
);
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
);
475 EXPORT_SYMBOL_GPL(ljca_register_event_cb
);
477 void ljca_unregister_event_cb(struct platform_device
*pdev
)
479 struct ljca_platform_data
*ljca_pdata
;
480 struct ljca_dev
*ljca
;
481 struct ljca_stub
*stub
;
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
);
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
);
495 EXPORT_SYMBOL_GPL(ljca_unregister_event_cb
);
497 static void ljca_stub_cleanup(struct ljca_dev
*ljca
)
499 struct ljca_stub
*stub
;
500 struct ljca_stub
*next
;
502 list_for_each_entry_safe (stub
, next
, &ljca
->stubs_list
, list
) {
503 list_del_init(&stub
->list
);
508 static void ljca_read_complete(struct urb
*urb
)
510 struct ljca_dev
*ljca
= urb
->context
;
511 struct ljca_msg
*header
= urb
->transfer_buffer
;
512 int len
= urb
->actual_length
;
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
);
520 /* sync/async unlink faults aren't errors */
521 if (urb
->status
== -ENOENT
|| urb
->status
== -ECONNRESET
||
522 urb
->status
== -ESHUTDOWN
)
525 dev_err(&ljca
->intf
->dev
, "read bulk urb transfer failed: %d\n",
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
);
534 if (!ljca_validate(header
, len
)) {
535 dev_err(&ljca
->intf
->dev
,
536 "data not correct header->len:%d payload_len:%d\n ",
541 ret
= ljca_parse(ljca
, header
);
543 dev_err(&ljca
->intf
->dev
,
544 "failed to parse data: ret:%d type:%d len: %d", ret
,
545 header
->type
, header
->len
);
548 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
550 dev_err(&ljca
->intf
->dev
,
551 "failed submitting read urb, error %d\n", ret
);
554 static int ljca_start(struct ljca_dev
*ljca
)
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
);
562 ret
= usb_submit_urb(ljca
->in_urb
, GFP_KERNEL
);
564 dev_err(&ljca
->intf
->dev
,
565 "failed submitting read urb, error %d\n", ret
);
570 struct ljca_mng_priv
{
574 static int ljca_mng_reset_handshake(struct ljca_stub
*stub
)
577 struct ljca_mng_priv
*priv
;
579 __le32 reset_id_ret
= 0;
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
);
596 static inline int ljca_mng_reset(struct ljca_stub
*stub
)
598 return ljca_stub_write(stub
, MNG_RESET
, NULL
, 0, NULL
, NULL
, true);
601 static int ljca_add_mfd_cell(struct ljca_dev
*ljca
, struct mfd_cell
*cell
)
603 struct mfd_cell
*new_cells
;
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",
611 new_cells
= krealloc_array(ljca
->cells
, (ljca
->cell_count
+ 1),
612 sizeof(struct mfd_cell
), GFP_KERNEL
);
616 memcpy(&new_cells
[ljca
->cell_count
], cell
, sizeof(*cell
));
617 ljca
->cells
= new_cells
;
623 static int ljca_gpio_stub_init(struct ljca_dev
*ljca
,
624 struct ljca_gpio_descriptor
*desc
)
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
;
631 u32 valid_pin
[MAX_GPIO_NUM
/ (sizeof(u32
) * BITS_PER_BYTE
)];
633 if (gpio_num
> MAX_GPIO_NUM
)
636 stub
= ljca_stub_alloc(ljca
, sizeof(*pdata
));
638 return PTR_ERR(stub
);
640 stub
->type
= GPIO_STUB
;
641 stub
->intf
= ljca
->intf
;
643 pdata
= ljca_priv(stub
);
644 pdata
->type
= stub
->type
;
645 pdata
->gpio_info
.num
= gpio_num
;
647 for (i
= 0; i
< desc
->bank_num
; i
++)
648 valid_pin
[i
] = desc
->bank_desc
[i
].valid_pins
;
650 bitmap_from_arr32(pdata
->gpio_info
.valid_pin_map
, valid_pin
, gpio_num
);
652 cell
.name
= "ljca-gpio";
653 cell
.platform_data
= pdata
;
654 cell
.pdata_size
= sizeof(*pdata
);
655 cell
.acpi_match
= &ljca_acpi_match_gpio
;
657 return ljca_add_mfd_cell(ljca
, &cell
);
660 static int ljca_mng_enum_gpio(struct ljca_stub
*stub
)
662 struct ljca_dev
*ljca
= usb_get_intfdata(stub
->intf
);
663 struct ljca_gpio_descriptor
*desc
;
667 desc
= kzalloc(MAX_PAYLOAD_SIZE
, GFP_KERNEL
);
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
);
679 ret
= ljca_gpio_stub_init(ljca
, desc
);
684 static int ljca_i2c_stub_init(struct ljca_dev
*ljca
,
685 struct ljca_i2c_descriptor
*desc
)
687 struct ljca_stub
*stub
;
688 struct ljca_platform_data
*pdata
;
692 stub
= ljca_stub_alloc(ljca
, desc
->num
* sizeof(*pdata
));
694 return PTR_ERR(stub
);
696 stub
->type
= I2C_STUB
;
697 stub
->intf
= ljca
->intf
;
698 pdata
= ljca_priv(stub
);
700 for (i
= 0; i
< desc
->num
; i
++) {
701 struct mfd_cell cell
= { 0 };
702 pdata
[i
].type
= stub
->type
;
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
;
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
];
714 ret
= ljca_add_mfd_cell(ljca
, &cell
);
722 static int ljca_mng_enum_i2c(struct ljca_stub
*stub
)
724 struct ljca_dev
*ljca
= usb_get_intfdata(stub
->intf
);
725 struct ljca_i2c_descriptor
*desc
;
729 desc
= kzalloc(MAX_PAYLOAD_SIZE
, GFP_KERNEL
);
733 ret
= ljca_stub_write(stub
, MNG_ENUM_I2C
, NULL
, 0, desc
, &len
, true);
735 dev_err(&stub
->intf
->dev
,
736 "MNG_ENUM_I2C failed ret:%d len:%d num:%d\n", ret
, len
,
742 ret
= ljca_i2c_stub_init(ljca
, desc
);
747 static int ljca_spi_stub_init(struct ljca_dev
*ljca
,
748 struct ljca_spi_descriptor
*desc
)
750 struct ljca_stub
*stub
;
751 struct ljca_platform_data
*pdata
;
755 stub
= ljca_stub_alloc(ljca
, desc
->num
* sizeof(*pdata
));
757 return PTR_ERR(stub
);
759 stub
->type
= SPI_STUB
;
760 stub
->intf
= ljca
->intf
;
761 pdata
= ljca_priv(stub
);
763 for (i
= 0; i
< desc
->num
; i
++) {
764 struct mfd_cell cell
= { 0 };
765 pdata
[i
].type
= stub
->type
;
767 pdata
[i
].spi_info
.id
= desc
->info
[i
].id
;
768 pdata
[i
].spi_info
.capacity
= desc
->info
[i
].capacity
;
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
];
776 ret
= ljca_add_mfd_cell(ljca
, &cell
);
784 static int ljca_mng_enum_spi(struct ljca_stub
*stub
)
786 struct ljca_dev
*ljca
= usb_get_intfdata(stub
->intf
);
787 struct ljca_spi_descriptor
*desc
;
791 desc
= kzalloc(MAX_PAYLOAD_SIZE
, GFP_KERNEL
);
795 ret
= ljca_stub_write(stub
, MNG_ENUM_SPI
, NULL
, 0, desc
, &len
, true);
797 dev_err(&stub
->intf
->dev
,
798 "MNG_ENUM_SPI failed ret:%d len:%d num:%d\n", ret
, len
,
804 ret
= ljca_spi_stub_init(ljca
, desc
);
809 static int ljca_mng_get_version(struct ljca_stub
*stub
, char *buf
)
811 struct fw_version version
= {0};
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
);
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
));
829 static inline int ljca_mng_set_dfu_mode(struct ljca_stub
*stub
)
831 return ljca_stub_write(stub
, MNG_SET_DFU_MODE
, NULL
, 0, NULL
, NULL
, true);
834 static int ljca_mng_link(struct ljca_dev
*ljca
, struct ljca_stub
*stub
)
838 ret
= ljca_mng_reset_handshake(stub
);
842 ljca
->state
= LJCA_RESET_SYNCED
;
844 ret
= ljca_mng_enum_gpio(stub
);
848 ljca
->state
= LJCA_ENUM_GPIO_COMPLETE
;
850 ret
= ljca_mng_enum_i2c(stub
);
854 ljca
->state
= LJCA_ENUM_I2C_COMPLETE
;
856 ret
= ljca_mng_enum_spi(stub
);
860 ljca
->state
= LJCA_ENUM_SPI_COMPLETE
;
864 static int ljca_mng_init(struct ljca_dev
*ljca
)
866 struct ljca_stub
*stub
;
867 struct ljca_mng_priv
*priv
;
870 stub
= ljca_stub_alloc(ljca
, sizeof(*priv
));
872 return PTR_ERR(stub
);
874 priv
= ljca_priv(stub
);
879 stub
->type
= MNG_STUB
;
880 stub
->intf
= ljca
->intf
;
882 ret
= ljca_mng_link(ljca
, stub
);
884 dev_err(&ljca
->intf
->dev
,
885 "mng stub link done ret:%d state:%d\n", ret
,
891 static inline int ljca_diag_get_fw_log(struct ljca_stub
*stub
, void *buf
)
899 ret
= ljca_stub_write(stub
, DIAG_GET_FW_LOG
, NULL
, 0, buf
, &len
, true);
906 static inline int ljca_diag_get_coredump(struct ljca_stub
*stub
, void *buf
)
914 ret
= ljca_stub_write(stub
, DIAG_GET_FW_COREDUMP
, NULL
, 0, buf
, &len
, true);
921 static inline int ljca_diag_set_trace_level(struct ljca_stub
*stub
, u8 level
)
923 return ljca_stub_write(stub
, DIAG_SET_TRACE_LEVEL
, &level
,
924 sizeof(level
), NULL
, NULL
, true);
927 static int ljca_diag_init(struct ljca_dev
*ljca
)
929 struct ljca_stub
*stub
;
931 stub
= ljca_stub_alloc(ljca
, 0);
933 return PTR_ERR(stub
);
935 stub
->type
= DIAG_STUB
;
936 stub
->intf
= ljca
->intf
;
940 static void ljca_delete(struct ljca_dev
*ljca
)
942 mutex_destroy(&ljca
->mutex
);
943 usb_free_urb(ljca
->in_urb
);
944 usb_put_intf(ljca
->intf
);
945 usb_put_dev(ljca
->udev
);
951 static int ljca_init(struct ljca_dev
*ljca
)
953 mutex_init(&ljca
->mutex
);
954 init_waitqueue_head(&ljca
->ack_wq
);
955 INIT_LIST_HEAD(&ljca
->stubs_list
);
957 ljca
->state
= LJCA_INITED
;
962 static void ljca_stop(struct ljca_dev
*ljca
)
964 usb_kill_urb(ljca
->in_urb
);
967 static ssize_t
cmd_store(struct device
*dev
, struct device_attribute
*attr
,
968 const char *buf
, size_t count
)
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
);
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);
985 static ssize_t
cmd_show(struct device
*dev
, struct device_attribute
*attr
,
988 return sysfs_emit(buf
, "%s\n", "supported cmd: [dfu, reset, debug]");
990 static DEVICE_ATTR_RW(cmd
);
992 static ssize_t
version_show(struct device
*dev
, struct device_attribute
*attr
,
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
);
999 return ljca_mng_get_version(stub
, buf
);
1001 static DEVICE_ATTR_RO(version
);
1003 static ssize_t
log_show(struct device
*dev
, struct device_attribute
*attr
,
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
);
1010 return ljca_diag_get_fw_log(diag_stub
, buf
);
1012 static DEVICE_ATTR_RO(log
);
1014 static ssize_t
coredump_show(struct device
*dev
, struct device_attribute
*attr
,
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
);
1021 return ljca_diag_get_coredump(diag_stub
, buf
);
1023 static DEVICE_ATTR_RO(coredump
);
1025 static struct attribute
*ljca_attrs
[] = {
1026 &dev_attr_version
.attr
,
1029 &dev_attr_coredump
.attr
,
1032 ATTRIBUTE_GROUPS(ljca
);
1034 static int ljca_probe(struct usb_interface
*intf
,
1035 const struct usb_device_id
*id
)
1037 struct ljca_dev
*ljca
;
1038 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
1041 ret
= precheck_acpi_hid(intf
);
1046 /* allocate memory for our device state and initialize it */
1047 ljca
= kzalloc(sizeof(*ljca
), GFP_KERNEL
);
1052 ljca
->udev
= usb_get_dev(interface_to_usbdev(intf
));
1053 ljca
->intf
= usb_get_intf(intf
);
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
);
1060 "Could not find both bulk-in and bulk-out endpoints\n");
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
);
1072 ljca
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1073 if (!ljca
->in_urb
) {
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
);
1082 /* save our data pointer in this intf device */
1083 usb_set_intfdata(intf
, ljca
);
1084 ret
= ljca_start(ljca
);
1086 dev_err(&intf
->dev
, "bridge read start failed ret %d\n", ret
);
1090 ret
= ljca_mng_init(ljca
);
1092 dev_err(&intf
->dev
, "register mng stub failed ret %d\n", ret
);
1096 ret
= ljca_diag_init(ljca
);
1098 dev_err(&intf
->dev
, "register diag stub failed ret %d\n", ret
);
1102 ret
= mfd_add_hotplug_devices(&intf
->dev
, ljca
->cells
,
1105 dev_err(&intf
->dev
, "failed to add mfd devices to core %d\n",
1110 ljca
->state
= LJCA_STARTED
;
1111 dev_info(&intf
->dev
, "LJCA USB device init success\n");
1115 dev_err(&intf
->dev
, "LJCA USB device init failed\n");
1116 /* this frees allocated memory */
1117 ljca_stub_cleanup(ljca
);
1122 static void ljca_disconnect(struct usb_interface
*intf
)
1124 struct ljca_dev
*ljca
;
1126 ljca
= usb_get_intfdata(intf
);
1129 ljca
->state
= LJCA_STOPPED
;
1130 mfd_remove_devices(&intf
->dev
);
1131 ljca_stub_cleanup(ljca
);
1132 usb_set_intfdata(intf
, NULL
);
1134 dev_info(&intf
->dev
, "LJCA disconnected\n");
1137 static int ljca_suspend(struct usb_interface
*intf
, pm_message_t message
)
1139 struct ljca_dev
*ljca
= usb_get_intfdata(intf
);
1142 ljca
->state
= LJCA_SUSPEND
;
1144 dev_dbg(&intf
->dev
, "LJCA suspend\n");
1148 static int ljca_resume(struct usb_interface
*intf
)
1150 struct ljca_dev
*ljca
= usb_get_intfdata(intf
);
1152 ljca
->state
= LJCA_STARTED
;
1153 dev_dbg(&intf
->dev
, "LJCA resume\n");
1154 return ljca_start(ljca
);
1157 static const struct usb_device_id ljca_table
[] = {
1158 {USB_DEVICE(0x8086, 0x0b63)},
1161 MODULE_DEVICE_TABLE(usb
, ljca_table
);
1163 static struct usb_driver ljca_driver
= {
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,
1174 module_usb_driver(ljca_driver
);
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");