1 /* 2 * Copyright (c) 2024 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #include <internal/nrfs_backend.h> 8 #include <internal/nrfs_callbacks.h> 9 #include <nrfs_usb.h> 10 11 typedef struct { 12 nrfs_usb_evt_handler_t handler; 13 bool is_initialized; 14 } nrfs_usb_cb_t; 15 static nrfs_usb_cb_t m_cb; 16 nrfs_usb_service_notify(void * p_notification,size_t size)17void nrfs_usb_service_notify(void *p_notification, size_t size) 18 { 19 if (!m_cb.handler || !m_cb.is_initialized) { 20 return; 21 } 22 23 nrfs_usb_evt_t evt; 24 nrfs_generic_t *p_data = (nrfs_generic_t *)p_notification; 25 if (NRFS_HDR_FILTER_ERR_GET(&p_data->hdr)) { 26 evt.type = NRFS_USB_EVT_REJECT; 27 m_cb.handler(&evt, (void *)p_data->ctx.ctx); 28 return; 29 } 30 31 nrfs_usb_rsp_t *p_rsp = (nrfs_usb_rsp_t *)p_notification; 32 switch (p_data->hdr.req) { 33 case NRFS_USB_REQ_ENABLE: 34 evt.type = NRFS_USB_EVT_VBUS_STATUS_CHANGE; 35 evt.usbhspll_ok = p_rsp->data.pll_ok; 36 evt.vregusb_ok = p_rsp->data.vreg_ok; 37 evt.vbus_detected = p_rsp->data.vbus_detected; 38 m_cb.handler(&evt, (void *)p_rsp->ctx.ctx); 39 break; 40 41 default: 42 break; 43 } 44 } 45 nrfs_usb_init(nrfs_usb_evt_handler_t handler)46nrfs_err_t nrfs_usb_init(nrfs_usb_evt_handler_t handler) 47 { 48 if (m_cb.is_initialized) { 49 return NRFS_ERR_INVALID_STATE; 50 } 51 52 m_cb.handler = handler; 53 m_cb.is_initialized = true; 54 return NRFS_SUCCESS; 55 } 56 nrfs_usb_uninit(void)57void nrfs_usb_uninit(void) 58 { 59 m_cb.is_initialized = false; 60 } 61 nrfs_usb_enable_request(void * p_context)62nrfs_err_t nrfs_usb_enable_request(void *p_context) 63 { 64 if (!m_cb.is_initialized) { 65 return NRFS_ERR_INVALID_STATE; 66 } 67 68 nrfs_usb_enable_req_t req; 69 70 NRFS_SERVICE_HDR_FILL(&req, NRFS_USB_REQ_ENABLE); 71 req.ctx.ctx = (uint32_t)p_context; 72 73 return nrfs_backend_send(&req, sizeof(req)); 74 } 75 nrfs_usb_disable_request(void * p_context)76nrfs_err_t nrfs_usb_disable_request(void *p_context) 77 { 78 if (!m_cb.is_initialized) { 79 return NRFS_ERR_INVALID_STATE; 80 } 81 82 nrfs_usb_disable_req_t req; 83 84 NRFS_SERVICE_HDR_FILL(&req, NRFS_USB_REQ_DISABLE); 85 req.ctx.ctx = (uint32_t)p_context; 86 87 return nrfs_backend_send(&req, sizeof(req)); 88 } 89