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/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>
19 enum ljca_acpi_match_adr
{
26 static struct mfd_cell_acpi_match ljca_acpi_match_gpio
= {
30 static struct mfd_cell_acpi_match ljca_acpi_match_i2cs
[] = {
39 static struct mfd_cell_acpi_match ljca_acpi_match_spis
[] = {
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
;
155 spinlock_t event_cb_lock
;
157 struct ljca_stub_packet ipacket
;
159 /* for identify ack */
163 struct ljca_event_cb_entry event_entry
;
166 static inline void *ljca_priv(const struct ljca_stub
*stub
)
168 return (char *)stub
+ sizeof(struct ljca_stub
);
174 LJCA_RESET_HANDSHAKE
,
176 LJCA_ENUM_GPIO_COMPLETE
,
177 LJCA_ENUM_I2C_COMPLETE
,
178 LJCA_ENUM_SPI_COMPLETE
,
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 */
190 /* the urb/buffer for read */
197 atomic_t active_transfers
;
198 wait_queue_head_t disconnect_wq
;
200 struct list_head stubs_list
;
202 /* to wait for an ongoing write ack */
203 wait_queue_head_t ack_wq
;
205 struct mfd_cell
*cells
;
209 static bool ljca_validate(void *data
, u32 data_len
)
211 struct ljca_msg
*header
= (struct ljca_msg
*)data
;
213 return (header
->len
+ sizeof(*header
) == data_len
);
217 void ljca_dump(struct ljca_dev
*ljca
, void *buf
, int len
)
226 for (i
= 0; i
< len
; i
++)
227 n
+= scnprintf(tmp
+ n
, sizeof(tmp
) - n
- 1, "%02x ", ((u8
*)buf
)[i
]);
229 dev_dbg(&ljca
->intf
->dev
, "%s\n", tmp
);
232 static struct ljca_stub
*ljca_stub_alloc(struct ljca_dev
*ljca
, int priv_size
)
234 struct ljca_stub
*stub
;
236 stub
= kzalloc(sizeof(*stub
) + priv_size
, GFP_KERNEL
);
238 return ERR_PTR(-ENOMEM
);
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");
248 static struct ljca_stub
*ljca_stub_find(struct ljca_dev
*ljca
, u8 type
)
250 struct ljca_stub
*stub
;
252 list_for_each_entry (stub
, &ljca
->stubs_list
, list
) {
253 if (stub
->type
== type
)
257 dev_err(&ljca
->intf
->dev
, "usb stub not find, type: %d", type
);
258 return ERR_PTR(-ENODEV
);
261 static void ljca_stub_notify(struct ljca_stub
*stub
, u8 cmd
,
262 const void *evt_data
, int len
)
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
,
269 spin_unlock_irqrestore(&stub
->event_cb_lock
, flags
);
272 static int ljca_parse(struct ljca_dev
*ljca
, struct ljca_msg
*header
)
274 struct ljca_stub
*stub
;
276 stub
= ljca_stub_find(ljca
, header
->type
);
278 return PTR_ERR(stub
);
280 if (!(header
->flags
& ACK_FLAG
)) {
281 ljca_stub_notify(stub
, header
->cmd
, header
->data
, header
->len
);
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
);
291 stub
->ipacket
.ibuf_len
= header
->len
;
292 if (stub
->ipacket
.ibuf
)
293 memcpy(stub
->ipacket
.ibuf
, header
->data
, header
->len
);
296 wake_up(&ljca
->ack_wq
);
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
)
304 struct ljca_msg
*header
;
305 struct ljca_dev
*ljca
= usb_get_intfdata(stub
->intf
);
307 u8 flags
= CMPL_FLAG
;
310 atomic_inc(&ljca
->active_transfers
);
311 if (ljca
->state
== LJCA_STOPPED
)
314 if (obuf_len
> MAX_PAYLOAD_SIZE
)
320 stub
->ipacket
.ibuf_len
= 0;
321 header
= kmalloc(sizeof(*header
) + obuf_len
, GFP_KERNEL
);
325 header
->type
= stub
->type
;
327 header
->flags
= flags
;
328 header
->len
= obuf_len
;
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
);
335 mutex_lock(&stub
->mutex
);
337 stub
->ipacket
.ibuf
= ibuf
;
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
,
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
,
352 usb_autopm_put_interface(ljca
->intf
);
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
);
368 *ibuf_len
= stub
->ipacket
.ibuf_len
;
370 stub
->ipacket
.ibuf
= NULL
;
371 stub
->ipacket
.ibuf_len
= 0;
374 atomic_dec(&ljca
->active_transfers
);
375 if (ljca
->state
== LJCA_STOPPED
)
376 wake_up(&ljca
->disconnect_wq
);
378 mutex_unlock(&stub
->mutex
);
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
)
385 struct ljca_platform_data
*ljca_pdata
;
386 struct ljca_dev
*ljca
;
387 struct ljca_stub
*stub
;
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
);
396 return PTR_ERR(stub
);
398 return ljca_stub_write(stub
, cmd
, obuf
, obuf_len
, ibuf
, ibuf_len
, wait_ack
);
401 int ljca_transfer(struct platform_device
*pdev
, u8 cmd
, const void *obuf
,
402 int obuf_len
, void *ibuf
, int *ibuf_len
)
404 return ljca_transfer_internal(pdev
, cmd
, obuf
, obuf_len
, ibuf
, ibuf_len
,
407 EXPORT_SYMBOL_GPL(ljca_transfer
);
409 int ljca_transfer_noack(struct platform_device
*pdev
, u8 cmd
, const void *obuf
,
412 return ljca_transfer_internal(pdev
, cmd
, obuf
, obuf_len
, NULL
, NULL
,
415 EXPORT_SYMBOL_GPL(ljca_transfer_noack
);
417 int ljca_register_event_cb(struct platform_device
*pdev
,
418 ljca_event_cb_t event_cb
)
420 struct ljca_platform_data
*ljca_pdata
;
421 struct ljca_dev
*ljca
;
422 struct ljca_stub
*stub
;
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
);
432 return PTR_ERR(stub
);
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
);
441 EXPORT_SYMBOL_GPL(ljca_register_event_cb
);
443 void ljca_unregister_event_cb(struct platform_device
*pdev
)
445 struct ljca_platform_data
*ljca_pdata
;
446 struct ljca_dev
*ljca
;
447 struct ljca_stub
*stub
;
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
);
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
);
461 EXPORT_SYMBOL_GPL(ljca_unregister_event_cb
);
463 static void ljca_stub_cleanup(struct ljca_dev
*ljca
)
465 struct ljca_stub
*stub
;
466 struct ljca_stub
*next
;
468 list_for_each_entry_safe (stub
, next
, &ljca
->stubs_list
, list
) {
469 list_del_init(&stub
->list
);
470 mutex_destroy(&stub
->mutex
);
475 static void ljca_read_complete(struct urb
*urb
)
477 struct ljca_dev
*ljca
= urb
->context
;
478 struct ljca_msg
*header
= urb
->transfer_buffer
;
479 int len
= urb
->actual_length
;
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
);
487 /* sync/async unlink faults aren't errors */
488 if (urb
->status
== -ENOENT
|| urb
->status
== -ECONNRESET
||
489 urb
->status
== -ESHUTDOWN
)
492 dev_err(&ljca
->intf
->dev
, "read bulk urb transfer failed: %d\n",
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
);
501 if (!ljca_validate(header
, len
)) {
502 dev_err(&ljca
->intf
->dev
,
503 "data not correct header->len:%d payload_len:%d\n ",
508 ret
= ljca_parse(ljca
, header
);
510 dev_err(&ljca
->intf
->dev
,
511 "failed to parse data: ret:%d type:%d len: %d", ret
,
512 header
->type
, header
->len
);
515 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
517 dev_err(&ljca
->intf
->dev
,
518 "failed submitting read urb, error %d\n", ret
);
521 static int ljca_start(struct ljca_dev
*ljca
)
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
);
529 ret
= usb_submit_urb(ljca
->in_urb
, GFP_KERNEL
);
531 dev_err(&ljca
->intf
->dev
,
532 "failed submitting read urb, error %d\n", ret
);
537 struct ljca_mng_priv
{
541 static int ljca_mng_reset_handshake(struct ljca_stub
*stub
)
544 struct ljca_mng_priv
*priv
;
546 __le32 reset_id_ret
= 0;
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
);
563 static inline int ljca_mng_reset(struct ljca_stub
*stub
)
565 return ljca_stub_write(stub
, MNG_RESET
, NULL
, 0, NULL
, NULL
, true);
568 static int ljca_add_mfd_cell(struct ljca_dev
*ljca
, struct mfd_cell
*cell
)
570 struct mfd_cell
*new_cells
;
571 new_cells
= krealloc_array(ljca
->cells
, (ljca
->cell_count
+ 1),
572 sizeof(struct mfd_cell
), GFP_KERNEL
);
576 memcpy(&new_cells
[ljca
->cell_count
], cell
, sizeof(*cell
));
577 ljca
->cells
= new_cells
;
583 static int ljca_gpio_stub_init(struct ljca_dev
*ljca
,
584 struct ljca_gpio_descriptor
*desc
)
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
;
591 u32 valid_pin
[MAX_GPIO_NUM
/ (sizeof(u32
) * BITS_PER_BYTE
)];
593 if (gpio_num
> MAX_GPIO_NUM
)
596 stub
= ljca_stub_alloc(ljca
, sizeof(*pdata
));
598 return PTR_ERR(stub
);
600 stub
->type
= GPIO_STUB
;
601 stub
->intf
= ljca
->intf
;
603 pdata
= ljca_priv(stub
);
604 pdata
->type
= stub
->type
;
605 pdata
->gpio_info
.num
= gpio_num
;
607 for (i
= 0; i
< desc
->bank_num
; i
++)
608 valid_pin
[i
] = desc
->bank_desc
[i
].valid_pins
;
610 bitmap_from_arr32(pdata
->gpio_info
.valid_pin_map
, valid_pin
, gpio_num
);
612 cell
.name
= "ljca-gpio";
613 cell
.platform_data
= pdata
;
614 cell
.pdata_size
= sizeof(*pdata
);
615 cell
.acpi_match
= &ljca_acpi_match_gpio
;
617 return ljca_add_mfd_cell(ljca
, &cell
);
620 static int ljca_mng_enum_gpio(struct ljca_stub
*stub
)
622 struct ljca_dev
*ljca
= usb_get_intfdata(stub
->intf
);
623 struct ljca_gpio_descriptor
*desc
;
627 desc
= kzalloc(MAX_PAYLOAD_SIZE
, GFP_KERNEL
);
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
);
639 ret
= ljca_gpio_stub_init(ljca
, desc
);
644 static int ljca_i2c_stub_init(struct ljca_dev
*ljca
,
645 struct ljca_i2c_descriptor
*desc
)
647 struct ljca_stub
*stub
;
648 struct ljca_platform_data
*pdata
;
652 stub
= ljca_stub_alloc(ljca
, desc
->num
* sizeof(*pdata
));
654 return PTR_ERR(stub
);
656 stub
->type
= I2C_STUB
;
657 stub
->intf
= ljca
->intf
;
658 pdata
= ljca_priv(stub
);
660 for (i
= 0; i
< desc
->num
; i
++) {
661 struct mfd_cell cell
= { 0 };
662 pdata
[i
].type
= stub
->type
;
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
;
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
];
674 ret
= ljca_add_mfd_cell(ljca
, &cell
);
682 static int ljca_mng_enum_i2c(struct ljca_stub
*stub
)
684 struct ljca_dev
*ljca
= usb_get_intfdata(stub
->intf
);
685 struct ljca_i2c_descriptor
*desc
;
689 desc
= kzalloc(MAX_PAYLOAD_SIZE
, GFP_KERNEL
);
693 ret
= ljca_stub_write(stub
, MNG_ENUM_I2C
, NULL
, 0, desc
, &len
, true);
695 dev_err(&stub
->intf
->dev
,
696 "MNG_ENUM_I2C failed ret:%d len:%d num:%d\n", ret
, len
,
702 ret
= ljca_i2c_stub_init(ljca
, desc
);
707 static int ljca_spi_stub_init(struct ljca_dev
*ljca
,
708 struct ljca_spi_descriptor
*desc
)
710 struct ljca_stub
*stub
;
711 struct ljca_platform_data
*pdata
;
715 stub
= ljca_stub_alloc(ljca
, desc
->num
* sizeof(*pdata
));
717 return PTR_ERR(stub
);
719 stub
->type
= SPI_STUB
;
720 stub
->intf
= ljca
->intf
;
721 pdata
= ljca_priv(stub
);
723 for (i
= 0; i
< desc
->num
; i
++) {
724 struct mfd_cell cell
= { 0 };
725 pdata
[i
].type
= stub
->type
;
727 pdata
[i
].spi_info
.id
= desc
->info
[i
].id
;
728 pdata
[i
].spi_info
.capacity
= desc
->info
[i
].capacity
;
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
];
736 ret
= ljca_add_mfd_cell(ljca
, &cell
);
744 static int ljca_mng_enum_spi(struct ljca_stub
*stub
)
746 struct ljca_dev
*ljca
= usb_get_intfdata(stub
->intf
);
747 struct ljca_spi_descriptor
*desc
;
751 desc
= kzalloc(MAX_PAYLOAD_SIZE
, GFP_KERNEL
);
755 ret
= ljca_stub_write(stub
, MNG_ENUM_SPI
, NULL
, 0, desc
, &len
, true);
757 dev_err(&stub
->intf
->dev
,
758 "MNG_ENUM_I2C failed ret:%d len:%d num:%d\n", ret
, len
,
764 ret
= ljca_spi_stub_init(ljca
, desc
);
769 static int ljca_mng_get_version(struct ljca_stub
*stub
, char *buf
)
771 struct fw_version version
= {0};
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
);
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
));
789 static inline int ljca_mng_set_dfu_mode(struct ljca_stub
*stub
)
791 return ljca_stub_write(stub
, MNG_SET_DFU_MODE
, NULL
, 0, NULL
, NULL
, true);
794 static int ljca_mng_link(struct ljca_dev
*ljca
, struct ljca_stub
*stub
)
798 ret
= ljca_mng_reset_handshake(stub
);
802 ljca
->state
= LJCA_RESET_SYNCED
;
804 ret
= ljca_mng_enum_gpio(stub
);
808 ljca
->state
= LJCA_ENUM_GPIO_COMPLETE
;
810 ret
= ljca_mng_enum_i2c(stub
);
814 ljca
->state
= LJCA_ENUM_I2C_COMPLETE
;
816 ret
= ljca_mng_enum_spi(stub
);
820 ljca
->state
= LJCA_ENUM_SPI_COMPLETE
;
824 static int ljca_mng_init(struct ljca_dev
*ljca
)
826 struct ljca_stub
*stub
;
827 struct ljca_mng_priv
*priv
;
830 stub
= ljca_stub_alloc(ljca
, sizeof(*priv
));
832 return PTR_ERR(stub
);
834 priv
= ljca_priv(stub
);
839 stub
->type
= MNG_STUB
;
840 stub
->intf
= ljca
->intf
;
842 ret
= ljca_mng_link(ljca
, stub
);
844 dev_err(&ljca
->intf
->dev
,
845 "mng stub link done ret:%d state:%d\n", ret
,
851 static inline int ljca_diag_get_fw_log(struct ljca_stub
*stub
, void *buf
)
859 ret
= ljca_stub_write(stub
, DIAG_GET_FW_LOG
, NULL
, 0, buf
, &len
, true);
866 static inline int ljca_diag_get_coredump(struct ljca_stub
*stub
, void *buf
)
874 ret
= ljca_stub_write(stub
, DIAG_GET_FW_COREDUMP
, NULL
, 0, buf
, &len
, true);
881 static inline int ljca_diag_set_trace_level(struct ljca_stub
*stub
, u8 level
)
883 return ljca_stub_write(stub
, DIAG_SET_TRACE_LEVEL
, &level
,
884 sizeof(level
), NULL
, NULL
, true);
887 static int ljca_diag_init(struct ljca_dev
*ljca
)
889 struct ljca_stub
*stub
;
891 stub
= ljca_stub_alloc(ljca
, 0);
893 return PTR_ERR(stub
);
895 stub
->type
= DIAG_STUB
;
896 stub
->intf
= ljca
->intf
;
900 static void ljca_delete(struct ljca_dev
*ljca
)
902 usb_free_urb(ljca
->in_urb
);
903 usb_put_intf(ljca
->intf
);
904 usb_put_dev(ljca
->udev
);
910 static int ljca_init(struct ljca_dev
*ljca
)
912 init_waitqueue_head(&ljca
->ack_wq
);
913 init_waitqueue_head(&ljca
->disconnect_wq
);
914 INIT_LIST_HEAD(&ljca
->stubs_list
);
916 ljca
->state
= LJCA_INITED
;
921 static void ljca_stop(struct ljca_dev
*ljca
)
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
);
929 static ssize_t
cmd_store(struct device
*dev
, struct device_attribute
*attr
,
930 const char *buf
, size_t count
)
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
);
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);
947 static ssize_t
cmd_show(struct device
*dev
, struct device_attribute
*attr
,
950 return sysfs_emit(buf
, "%s\n", "supported cmd: [dfu, reset, debug]");
952 static DEVICE_ATTR_RW(cmd
);
954 static ssize_t
version_show(struct device
*dev
, struct device_attribute
*attr
,
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
);
961 return ljca_mng_get_version(stub
, buf
);
963 static DEVICE_ATTR_RO(version
);
965 static ssize_t
log_show(struct device
*dev
, struct device_attribute
*attr
,
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
);
972 return ljca_diag_get_fw_log(diag_stub
, buf
);
974 static DEVICE_ATTR_RO(log
);
976 static ssize_t
coredump_show(struct device
*dev
, struct device_attribute
*attr
,
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
);
983 return ljca_diag_get_coredump(diag_stub
, buf
);
985 static DEVICE_ATTR_RO(coredump
);
987 static struct attribute
*ljca_attrs
[] = {
988 &dev_attr_version
.attr
,
991 &dev_attr_coredump
.attr
,
994 ATTRIBUTE_GROUPS(ljca
);
996 static int ljca_probe(struct usb_interface
*intf
,
997 const struct usb_device_id
*id
)
999 struct ljca_dev
*ljca
;
1000 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
1003 /* allocate memory for our device state and initialize it */
1004 ljca
= kzalloc(sizeof(*ljca
), GFP_KERNEL
);
1009 ljca
->udev
= usb_get_dev(interface_to_usbdev(intf
));
1010 ljca
->intf
= usb_get_intf(intf
);
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
);
1017 "Could not find both bulk-in and bulk-out endpoints\n");
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
);
1029 ljca
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1030 if (!ljca
->in_urb
) {
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
);
1039 /* save our data pointer in this intf device */
1040 usb_set_intfdata(intf
, ljca
);
1041 ret
= ljca_start(ljca
);
1043 dev_err(&intf
->dev
, "bridge read start failed ret %d\n", ret
);
1047 ret
= ljca_mng_init(ljca
);
1049 dev_err(&intf
->dev
, "register mng stub failed ret %d\n", ret
);
1053 ret
= ljca_diag_init(ljca
);
1055 dev_err(&intf
->dev
, "register diag stub failed ret %d\n", ret
);
1059 ret
= mfd_add_hotplug_devices(&intf
->dev
, ljca
->cells
,
1062 dev_err(&intf
->dev
, "failed to add mfd devices to core %d\n",
1067 usb_enable_autosuspend(ljca
->udev
);
1068 ljca
->state
= LJCA_STARTED
;
1069 dev_info(&intf
->dev
, "LJCA USB device init success\n");
1073 dev_err(&intf
->dev
, "LJCA USB device init failed\n");
1074 /* this frees allocated memory */
1075 ljca_stub_cleanup(ljca
);
1080 static void ljca_disconnect(struct usb_interface
*intf
)
1082 struct ljca_dev
*ljca
;
1084 ljca
= usb_get_intfdata(intf
);
1087 mfd_remove_devices(&intf
->dev
);
1088 ljca_stub_cleanup(ljca
);
1089 usb_set_intfdata(intf
, NULL
);
1091 dev_dbg(&intf
->dev
, "LJCA disconnected\n");
1094 static int ljca_suspend(struct usb_interface
*intf
, pm_message_t message
)
1096 struct ljca_dev
*ljca
= usb_get_intfdata(intf
);
1099 ljca
->state
= LJCA_SUSPEND
;
1101 dev_dbg(&intf
->dev
, "LJCA suspend\n");
1105 static int ljca_resume(struct usb_interface
*intf
)
1107 struct ljca_dev
*ljca
= usb_get_intfdata(intf
);
1109 ljca
->state
= LJCA_STARTED
;
1110 dev_dbg(&intf
->dev
, "LJCA resume\n");
1111 return ljca_start(ljca
);
1114 static const struct usb_device_id ljca_table
[] = {
1115 {USB_DEVICE(0x8086, 0x0b63)},
1118 MODULE_DEVICE_TABLE(usb
, ljca_table
);
1120 static struct usb_driver ljca_driver
= {
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,
1131 module_usb_driver(ljca_driver
);
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");