@@ -117,6 +117,27 @@ static int mbox_init(const struct device *instance)
117117 return mbox_set_enabled (& conf -> mbox_rx , 1 );
118118}
119119
120+ static int mbox_deinit (const struct device * instance )
121+ {
122+ const struct backend_config_t * conf = instance -> config ;
123+ struct backend_data_t * dev_data = instance -> data ;
124+ int err ;
125+
126+ err = mbox_set_enabled (& conf -> mbox_rx , 0 );
127+ if (err != 0 ) {
128+ return err ;
129+ }
130+
131+ err = mbox_register_callback (& conf -> mbox_rx , NULL , NULL );
132+ if (err != 0 ) {
133+ return err ;
134+ }
135+
136+ (void )k_work_cancel (& dev_data -> mbox_work );
137+
138+ return 0 ;
139+ }
140+
120141static int register_ept (const struct device * instance , void * * token ,
121142 const struct ipc_ept_cfg * cfg )
122143{
@@ -138,8 +159,18 @@ static int register_ept(const struct device *instance, void **token,
138159 return ret ;
139160 }
140161
162+ dev_data -> tx_ib = spsc_pbuf_init ((void * )conf -> tx_shm_addr ,
163+ conf -> tx_shm_size ,
164+ SPSC_PBUF_CACHE );
165+ dev_data -> rx_ib = (void * )conf -> rx_shm_addr ;
166+
141167 ret = spsc_pbuf_write (dev_data -> tx_ib , magic , sizeof (magic ));
142- if (ret < sizeof (magic )) {
168+ if (ret < 0 ) {
169+ __ASSERT_NO_MSG (false);
170+ return ret ;
171+ }
172+
173+ if (ret < (int )sizeof (magic )) {
143174 __ASSERT_NO_MSG (ret == sizeof (magic ));
144175 return ret ;
145176 }
@@ -157,6 +188,23 @@ static int register_ept(const struct device *instance, void **token,
157188 return 0 ;
158189}
159190
191+ static int deregister_ept (const struct device * instance , void * token )
192+ {
193+ struct backend_data_t * dev_data = instance -> data ;
194+ int ret ;
195+
196+ ret = mbox_deinit (instance );
197+ if (ret ) {
198+ return ret ;
199+ }
200+
201+ dev_data -> cfg = NULL ;
202+
203+ atomic_set (& dev_data -> state , ICMSG_STATE_OFF );
204+
205+ return 0 ;
206+ }
207+
160208static int send (const struct device * instance , void * token ,
161209 const void * msg , size_t len )
162210{
@@ -187,21 +235,12 @@ static int send(const struct device *instance, void *token,
187235
188236const static struct ipc_service_backend backend_ops = {
189237 .register_endpoint = register_ept ,
238+ .deregister_endpoint = deregister_ept ,
190239 .send = send ,
191240};
192241
193242static int backend_init (const struct device * instance )
194243{
195- const struct backend_config_t * conf = instance -> config ;
196- struct backend_data_t * dev_data = instance -> data ;
197-
198- __ASSERT_NO_MSG (conf -> tx_shm_size > sizeof (struct spsc_pbuf ));
199-
200- dev_data -> tx_ib = spsc_pbuf_init ((void * )conf -> tx_shm_addr ,
201- conf -> tx_shm_size ,
202- SPSC_PBUF_CACHE );
203- dev_data -> rx_ib = (void * )conf -> rx_shm_addr ;
204-
205244 return 0 ;
206245}
207246
@@ -215,6 +254,8 @@ static int backend_init(const struct device *instance)
215254 .mbox_rx = MBOX_DT_CHANNEL_GET(DT_DRV_INST(i), rx), \
216255 }; \
217256 \
257+ BUILD_ASSERT(DT_REG_SIZE(DT_INST_PHANDLE(i, tx_region)) > \
258+ sizeof(struct spsc_pbuf)); \
218259 static struct backend_data_t backend_data_##i; \
219260 \
220261 DEVICE_DT_INST_DEFINE(i, \
0 commit comments