I implemented a version of this in trunk
changeset 2971set_record in both ubasic and lua should do the right thing whether or not a USB connection is present.
I haven't changed switch_mode_usb in lua, in case someone wants to call it directly for some reason.
I added "get_usb_bit_physw" in C code, to return (effectively) the value of the USB bit *after* CHDK is done masking. It would be better to actually get this from the bit in kbd.c, but I didn't feeling like touching >100 files right now. This could be useful elsewhere, for example in some of the PTP related code as proxy for whether a USB connection could be present. It might also be useful in script.
edit:
Slightly related, I completely removed the return value from switch_mode_usb in lua, in both the trunk and stable branches, because it didn't actually work.
Returning values from C to lua is done by pushing the values on the stack. The return value of the C function simply indicates the *number* of values returned. The old switch_mode_usb code returned 0 or 1 depending on the status of (C) switch_mode_usb, so it would either return nothing or one nil or possibly garbage? value.