I think the longest you can go with toslink (optical) cable is around 30 feet. At least that's what I keep seeing as a recomended length. So to get 40 feet you'll need to use a repeater around 20 feet or so to extend your range. The only problem with a repeater is that it's an electrical device, it needs power to re-transmit the signal, so somewhere on the 40ft run, you'll need a power outlet.
Secondly, I don't know if you can get 20 foot long cables, the longest I could find was around 12 feet (a 4 meter long monster cable). Then again I didn't look very hard.
I'm lucky, the longest I need my cable run to be is 20 feet, so I'm going to get another 12ft cable and use a couple to connect the 2 cables and make a long 24 foot cable.
So what you'll need to do is buy a few smaller length cables (unless you can find some around 20 feet), use couplers to make long cables, and then you'll need a repeater to get that signal to go 40 feet.
You might be able to go 40 feet without a repeater, but i really don't know about that, and using couplers to make long cables might take a small hit on the throughput.
You can get repeaters and couplers at radioshack, and look around online for long cables.
a good shopping search engine is google's froogle tool
http://froogle.google.com and just play around with various search terms and min/max prices and narrow it down.
So to recap, assuming you use the same monster cable 12ft cable that i'm using, you'll need
1) (3 or 4) 4meter cables
2) (2 or 3) toslink couplers
3) (1) toslink repeater
pricing on toslink cables is pretty high, but you can get good deals online. I think i found my 4M cable online for around $30 or $35 which is somewhere around half off retail.
-vasu