In a conventional microscope you can tell when the sample you have under the lens is in focus simply by looking at the projected 2D image. In a confocal microscope we only have the one beam of light and so we need a tool to tell us where we are with respect to the focal plane.
One way to achieve this is to introduce an aperture (a pinhole) and monitor the light intensity as a function of focus offset. This will work, but it's not very efficient and it doesn't tell us whether we're too near or too far away.
CD/DVD/BluRay drives use a different, more sophisticated method to adjust the focus of the incident laser: A cylindrical lens is placed before the sensor, which is a 4-quadrant photodiode in the CD/DVD/BluRay case. The lens distorts the beam, from a diagonal ellipsoid, to a point (focus), to an ellipsoid in the opposite diagonal. This is called astigmatic focusing.
Using the four outputs from the quad-photodiode we can then tell when we're focused on the sample.
Here's what on of those 4-quadrant photodiodes looks like (you may have to squint a little to see the division in 4 quadrants). The two outer ones are dual-photodiodes used for tracking.
OK, so this sounds pretty straight forward... but of course in reality it's not quite as easy (surprise!).
Here are some pictures of the red laser output for different voice coil positions. We've removed the quad-photodiode and placed a little screen in its place.
BIG SAFETY REMINDER:
The lasers in an optical drive can cause serious damage to your eyes!
Never (and I mean NEVER) look straight at them without proper protection googles or a diffusing screen.
The third picture probably shows the beam in focus. We aren't getting the other ellipsoid though, so more work is needed to figure that out...