Constraints on millicharged particles from cosmic-ray production