Hi Ivan,
What you are trying to do is very much possible. I have done it with Mass storage host & custom device(connected to PC) on the same port.
By default you need to initialize USB in device mode, so when host(PC) is connected communication is done easily. But when another device(USB pen drive) is connected ID pin goes low & then initialize USB host so as to enumerate & communicate with connected device.
For Switching modes you need:-
1) switch for pull downs on D+ & D- lines(15k pull down)
2) switch for pull up on D+(1.5k) for full speed.
3) switch for VBUS(so that when device is connected +5v supply is provided).
4) ID pin connected to GPIO using 100k pull up.(to detect when device is connected).
Regards,
Priyank.