return -EINVAL if the input parameters are wrong.

It was earlier reporting -ENOMEM. Fixed it in this patch.