Several ioctl calls were returning -1 instead of -errno,
which may confuse upper layers, especially when the error
is -ENOMEM. Also move some existing "err = -errno" calls
to the top of if statements so that errno cannot change
by libc calls in the error handling code.
(cherry picked from commit a8eda532452651eb1fbae419319455de2a078ef0)
Change-Id: I181f98d5a261e8e3e1b3f6ecd3ba288e7b4b5607