@@ -2204,95 +2204,99 @@ static int bladerf2_device_reset(struct bladerf *dev)
22042204/* Tuning mode */
22052205/******************************************************************************/
22062206
2207- extern struct controller_fns const rfic_host_control ;
2208- extern struct controller_fns const rfic_fpga_control ;
2207+ static inline bool _supports_fpga_tuning (struct bladerf * dev )
2208+ {
2209+ extern struct controller_fns const rfic_fpga_control ;
22092210
2210- struct controller_fns const * rfic_control_fns [] = {
2211- & rfic_host_control ,
2212- & rfic_fpga_control ,
2213- };
2211+ struct bladerf2_board_data * board_data = dev -> board_data ;
2212+
2213+ return (have_cap (board_data -> capabilities , BLADERF_CAP_FPGA_TUNING ) &&
2214+ rfic_fpga_control .is_present (dev ));
2215+ }
22142216
22152217static int bladerf2_set_tuning_mode (struct bladerf * dev ,
22162218 bladerf_tuning_mode mode )
22172219{
22182220 CHECK_BOARD_STATE (STATE_FPGA_LOADED );
22192221
2220- struct bladerf2_board_data * board_data = dev -> board_data ;
2221- bool has_cap = have_cap (board_data -> capabilities , BLADERF_CAP_FPGA_TUNING );
2222- int status = 0 ;
2222+ extern struct controller_fns const rfic_host_control ;
2223+ extern struct controller_fns const rfic_fpga_control ;
22232224
2224- if (!has_cap && BLADERF_TUNING_MODE_FPGA == mode ) {
2225- log_debug ("The loaded FPGA version (%u.%u.%u) does not support the "
2226- "provided tuning mode (%d)\n" ,
2227- board_data -> fpga_version .major ,
2228- board_data -> fpga_version .minor ,
2229- board_data -> fpga_version .patch , mode );
2230- return BLADERF_ERR_UNSUPPORTED ;
2231- }
2225+ struct bladerf2_board_data * board_data = dev -> board_data ;
2226+ struct controller_fns const * rfic_new = NULL ;
2227+ struct controller_fns const * rfic_other = NULL ;
22322228
2233- /* Test to make sure this FPGA actually will do FPGA-based tuning */
2234- if (BLADERF_TUNING_MODE_FPGA == mode ) {
2235- if (!rfic_fpga_control .is_present (dev )) {
2236- log_debug ("FPGA does not have RFIC control target, bailing out.\n" );
2237- return BLADERF_ERR_UNSUPPORTED ;
2238- }
2239- }
2229+ bladerf_tuning_mode mode_other ;
2230+ bladerf_rfic_init_state init_state ;
22402231
2241- log_debug ("%s: Tuning mode: %s\n" , __FUNCTION__ , tuningmode2str (mode ));
2232+ log_debug ("%s: New tuning mode: %s\n" , __FUNCTION__ , tuningmode2str (mode ));
22422233
2243- /* Do the tuning mode change */
22442234 switch (mode ) {
22452235 case BLADERF_TUNING_MODE_HOST :
2246- board_data -> rfic = & rfic_host_control ;
2236+ rfic_new = & rfic_host_control ;
2237+ rfic_other = _supports_fpga_tuning (dev ) ? & rfic_fpga_control : NULL ;
2238+ mode_other = BLADERF_TUNING_MODE_FPGA ;
22472239 break ;
22482240
22492241 case BLADERF_TUNING_MODE_FPGA :
2250- board_data -> rfic = & rfic_fpga_control ;
2242+ /* Test capability */
2243+ if (!_supports_fpga_tuning (dev )) {
2244+ log_debug ("%s: The loaded FPGA version (%u.%u.%u) does not "
2245+ "support FPGA RFIC control\n" ,
2246+ __FUNCTION__ , board_data -> fpga_version .major ,
2247+ board_data -> fpga_version .minor ,
2248+ board_data -> fpga_version .patch );
2249+ return BLADERF_ERR_UNSUPPORTED ;
2250+ }
2251+
2252+ rfic_new = & rfic_fpga_control ;
2253+ rfic_other = & rfic_host_control ;
2254+ mode_other = BLADERF_TUNING_MODE_HOST ;
22512255 break ;
22522256
22532257 default :
2254- assert (! "Invalid tuning mode." );
2258+ log_error ( "%s: invalid tuning mode (%d)\n" , mode );
22552259 return BLADERF_ERR_INVAL ;
22562260 }
22572261
2262+ /* De-initialize RFIC if it's initialized by another tuning mode */
2263+ if (NULL != rfic_other ) {
2264+ CHECK_STATUS (rfic_other -> get_init_state (dev , & init_state ));
2265+
2266+ if (init_state != BLADERF_RFIC_INIT_STATE_OFF ) {
2267+ log_debug ("%s: %s %s RFIC control\n" , __FUNCTION__ , "Releasing" ,
2268+ tuningmode2str (mode_other ));
2269+ CHECK_STATUS (rfic_other -> deinitialize (dev ));
2270+ }
2271+ }
2272+
2273+ /* Set board data */
2274+ board_data -> rfic = rfic_new ;
22582275 board_data -> tuning_mode = mode ;
22592276
2260- /* De-initialize RFIC if it's initialized by another tuning mode */
2261- switch (board_data -> rfic -> command_mode ) {
2262- case RFIC_COMMAND_HOST :
2263- if (has_cap && (rfic_fpga_control .is_initialized (dev ) ||
2264- rfic_fpga_control .is_standby (dev ))) {
2265- log_info ("%s: Releasing FPGA RFIC control\n" , __FUNCTION__ );
2266- status = rfic_fpga_control .deinitialize (dev );
2267- }
2268- break ;
2277+ /* Bring RFIC to initialized state */
2278+ CHECK_STATUS (rfic_new -> get_init_state (dev , & init_state ));
22692279
2270- case RFIC_COMMAND_FPGA :
2271- if (rfic_host_control .is_initialized (dev ) ||
2272- rfic_host_control .is_standby (dev )) {
2273- log_info ("%s: Releasing Host RFIC control\n" , __FUNCTION__ );
2274- status = rfic_host_control .deinitialize (dev );
2275- }
2276- break ;
2280+ switch (init_state ) {
2281+ case BLADERF_RFIC_INIT_STATE_OFF :
2282+ log_debug ("%s: %s %s RFIC control\n" , __FUNCTION__ , "Initializing" ,
2283+ tuningmode2str (mode ));
2284+ return rfic_new -> initialize (dev );
22772285
2278- default :
2279- assert (! "Invalid RFIC control mode." );
2280- return BLADERF_ERR_INVAL ;
2281- }
2286+ case BLADERF_RFIC_INIT_STATE_STANDBY :
2287+ log_debug ( "%s: %s %s RFIC control\n" , __FUNCTION__ , "Restoring" ,
2288+ tuningmode2str ( mode )) ;
2289+ return rfic_new -> initialize ( dev );
22822290
2283- if (status < 0 ) {
2284- RETURN_ERROR_STATUS ("failed to release RFIC control" , status );
2285- }
2291+ case BLADERF_RFIC_INIT_STATE_ON :
2292+ log_debug ("%s: %s %s RFIC control\n" , __FUNCTION__ , "Maintaining" ,
2293+ tuningmode2str (mode ));
2294+ return 0 ;
22862295
2287- /* Initialize RFIC, if it hasn't yet been initialized */
2288- if (!board_data -> rfic -> is_initialized (dev )) {
2289- log_info ("%s: Initializing %s RFIC control\n" , __FUNCTION__ ,
2290- tuningmode2str (mode ));
2291- return board_data -> rfic -> initialize (dev );
2292- } else {
2293- log_debug ("%s: Maintaining %s RFIC control\n" , __FUNCTION__ ,
2294- tuningmode2str (mode ));
2295- return 0 ;
2296+ default :
2297+ log_error ("%s: invalid RFIC initialization state (%d)\n" ,
2298+ init_state );
2299+ return BLADERF_ERR_UNEXPECTED ;
22962300 }
22972301}
22982302
0 commit comments