Measurement of the proton and deuteron spin structure functions g2 and asymmetry A2