msm: adsprpc: Incorporate coding style comments

Remove goto statements to bail out from inside the macro
and unnecessary defines. Cleanup on module init failure.

Change-Id: I01885a9fa37283d77e1b6735decd2f639ae4d4b3
Acked-by: Sathish Ambley <sambley@qualcomm.com>
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
diff --git a/arch/arm/mach-msm/qdsp6v2/adsprpc.c b/arch/arm/mach-msm/qdsp6v2/adsprpc.c
index 6e6f8e8..ea31769 100644
--- a/arch/arm/mach-msm/qdsp6v2/adsprpc.c
+++ b/arch/arm/mach-msm/qdsp6v2/adsprpc.c
@@ -11,6 +11,7 @@
  * GNU General Public License for more details.
  *
  */
+#include <linux/scatterlist.h>
 #include "adsprpc.h"
 
 struct smq_invoke_ctx {
@@ -70,15 +71,25 @@
 static int alloc_mem(struct fastrpc_buf *buf)
 {
 	struct ion_client *clnt = gfa.iclient;
+	struct sg_table *sg;
 	int err = 0;
 
 	buf->handle = ion_alloc(clnt, buf->size, SZ_4K,
-				ION_HEAP(ION_AUDIO_HEAP_ID));
-	VERIFY(0 == IS_ERR_OR_NULL(buf->handle));
+				ION_HEAP(ION_AUDIO_HEAP_ID), 0);
+	VERIFY(err, 0 == IS_ERR_OR_NULL(buf->handle));
+	if (err)
+		goto bail;
 	buf->virt = 0;
-	VERIFY(0 != (buf->virt = ion_map_kernel(clnt, buf->handle,
-						ION_SET_CACHE(CACHED))));
-	VERIFY(0 == ion_phys(clnt, buf->handle, &buf->phys, &buf->size));
+	VERIFY(err, 0 != (buf->virt = ion_map_kernel(clnt, buf->handle)));
+	if (err)
+		goto bail;
+	VERIFY(err, 0 != (sg = ion_sg_table(clnt, buf->handle)));
+	if (err)
+		goto bail;
+	VERIFY(err, 1 == sg->nents);
+	if (err)
+		goto bail;
+	buf->phys = sg_dma_address(sg->sgl);
  bail:
 	if (err && !IS_ERR_OR_NULL(buf->handle))
 		free_mem(buf);
@@ -88,7 +99,9 @@
 static int context_list_ctor(struct smq_context_list *me, int size)
 {
 	int err = 0;
-	VERIFY(0 != (me->ls = kzalloc(size, GFP_KERNEL)));
+	VERIFY(err, 0 != (me->ls = kzalloc(size, GFP_KERNEL)));
+	if (err)
+		goto bail;
 	me->size = size / sizeof(*me->ls);
 	me->last = 0;
  bail:
@@ -104,18 +117,18 @@
 static void context_list_alloc_ctx(struct smq_context_list *me,
 					struct smq_invoke_ctx **po)
 {
-	int ii = me->last;
+	int i = me->last;
 	struct smq_invoke_ctx *ctx;
 
 	for (;;) {
-		ii = ii % me->size;
-		ctx = &me->ls[ii];
+		i = i % me->size;
+		ctx = &me->ls[i];
 		if (atomic_read(&ctx->free) == 0)
-			if (0 == atomic_cmpxchg(&ctx->free, 0, 1))
+			if (atomic_cmpxchg(&ctx->free, 0, 1) == 0)
 				break;
-		ii++;
+		i++;
 	}
-	me->last = ii;
+	me->last = i;
 	ctx->retval = -1;
 	init_completion(&ctx->work);
 	*po = ctx;
@@ -135,13 +148,13 @@
 
 static void context_notify_all_users(struct smq_context_list *me)
 {
-	int ii;
+	int i;
 
 	if (!me->ls)
 		return;
-	for (ii = 0; ii < me->size; ++ii) {
-		if (atomic_read(&me->ls[ii].free) != 0)
-			complete(&me->ls[ii].work);
+	for (i = 0; i < me->size; ++i) {
+		if (atomic_read(&me->ls[i].free) != 0)
+			complete(&me->ls[i].work);
 	}
 }
 
@@ -150,11 +163,10 @@
 {
 	struct smq_phy_page *pgstart, *pages;
 	struct smq_invoke_buf *list;
-	int ii, rlen, err = 0;
+	int i, rlen, err = 0;
 	int inbufs = REMOTE_SCALARS_INBUFS(sc);
 	int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
 
-	VERIFY(0 != try_module_get(THIS_MODULE));
 	LOCK_MMAP(kernel);
 	*obuf = *ibuf;
  retry:
@@ -166,38 +178,44 @@
 		rlen = ((uint32_t)pages - (uint32_t)obuf->virt) - obuf->size;
 		obuf->size += buf_page_size(rlen);
 		obuf->handle = 0;
-		VERIFY(0 == alloc_mem(obuf));
+		VERIFY(err, 0 == alloc_mem(obuf));
+		if (err)
+			goto bail;
 		goto retry;
 	}
 	pgstart->addr = obuf->phys;
 	pgstart->size = obuf->size;
-	for (ii = 0; ii < inbufs + outbufs; ++ii) {
+	for (i = 0; i < inbufs + outbufs; ++i) {
 		void *buf;
 		int len, num;
 
-		len = pra[ii].buf.len;
+		len = pra[i].buf.len;
 		if (!len)
 			continue;
-		buf = pra[ii].buf.pv;
+		buf = pra[i].buf.pv;
 		num = buf_num_pages(buf, len);
 		if (!kernel)
-			list[ii].num = buf_get_pages(buf, len, num,
-				ii >= inbufs, pages, rlen / sizeof(*pages));
+			list[i].num = buf_get_pages(buf, len, num,
+				i >= inbufs, pages, rlen / sizeof(*pages));
 		else
-			list[ii].num = 0;
-		VERIFY(list[ii].num >= 0);
-		if (list[ii].num) {
-			list[ii].pgidx = pages - pgstart;
-			pages = pages + list[ii].num;
+			list[i].num = 0;
+		VERIFY(err, list[i].num >= 0);
+		if (err)
+			goto bail;
+		if (list[i].num) {
+			list[i].pgidx = pages - pgstart;
+			pages = pages + list[i].num;
 		} else if (rlen > sizeof(*pages)) {
-			list[ii].pgidx = pages - pgstart;
+			list[i].pgidx = pages - pgstart;
 			pages = pages + 1;
 		} else {
 			if (obuf->handle != ibuf->handle)
 				free_mem(obuf);
 			obuf->size += buf_page_size(sizeof(*pages));
 			obuf->handle = 0;
-			VERIFY(0 == alloc_mem(obuf));
+			VERIFY(err, 0 == alloc_mem(obuf));
+			if (err)
+				goto bail;
 			goto retry;
 		}
 		rlen = obuf->size - ((uint32_t) pages - (uint32_t) obuf->virt);
@@ -207,7 +225,6 @@
 	if (err && (obuf->handle != ibuf->handle))
 		free_mem(obuf);
 	UNLOCK_MMAP(kernel);
-	module_put(THIS_MODULE);
 	return err;
 }
 
@@ -220,74 +237,84 @@
 	struct fastrpc_buf *pbuf = ibuf, *obufs = 0;
 	struct smq_phy_page *pages;
 	void *args;
-	int ii, rlen, size, used, inh, bufs = 0, err = 0;
+	int i, rlen, size, used, inh, bufs = 0, err = 0;
 	int inbufs = REMOTE_SCALARS_INBUFS(sc);
 	int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
 
 	list = smq_invoke_buf_start(rpra, sc);
 	pages = smq_phy_page_start(sc, list);
-	used = ALIGN_8(pbuf->used);
+	used = ALIGN(pbuf->used, BALIGN);
 	args = (void *)((char *)pbuf->virt + used);
 	rlen = pbuf->size - used;
-	for (ii = 0; ii < inbufs + outbufs; ++ii) {
+	for (i = 0; i < inbufs + outbufs; ++i) {
 		int num;
 
-		rpra[ii].buf.len = pra[ii].buf.len;
-		if (list[ii].num) {
-			rpra[ii].buf.pv = pra[ii].buf.pv;
+		rpra[i].buf.len = pra[i].buf.len;
+		if (list[i].num) {
+			rpra[i].buf.pv = pra[i].buf.pv;
 			continue;
 		}
-		if (rlen < pra[ii].buf.len) {
+		if (rlen < pra[i].buf.len) {
 			struct fastrpc_buf *b;
 			pbuf->used = pbuf->size - rlen;
-			VERIFY(0 != (b = krealloc(obufs,
+			VERIFY(err, 0 != (b = krealloc(obufs,
 				 (bufs + 1) * sizeof(*obufs), GFP_KERNEL)));
+			if (err)
+				goto bail;
 			obufs = b;
 			pbuf = obufs + bufs;
-			pbuf->size = buf_num_pages(0, pra[ii].buf.len) *
+			pbuf->size = buf_num_pages(0, pra[i].buf.len) *
 								PAGE_SIZE;
-			VERIFY(0 == alloc_mem(pbuf));
+			VERIFY(err, 0 == alloc_mem(pbuf));
+			if (err)
+				goto bail;
 			bufs++;
 			args = pbuf->virt;
 			rlen = pbuf->size;
 		}
-		num = buf_num_pages(args, pra[ii].buf.len);
+		num = buf_num_pages(args, pra[i].buf.len);
 		if (pbuf == ibuf) {
-			list[ii].num = num;
-			list[ii].pgidx = 0;
+			list[i].num = num;
+			list[i].pgidx = 0;
 		} else {
-			list[ii].num = 1;
-			pages[list[ii].pgidx].addr =
+			list[i].num = 1;
+			pages[list[i].pgidx].addr =
 				buf_page_start((void *)(pbuf->phys +
 							 (pbuf->size - rlen)));
-			pages[list[ii].pgidx].size =
-				buf_page_size(pra[ii].buf.len);
+			pages[list[i].pgidx].size =
+				buf_page_size(pra[i].buf.len);
 		}
-		if (ii < inbufs) {
-			if (!kernel)
-				VERIFY(0 == copy_from_user(args, pra[ii].buf.pv,
-							pra[ii].buf.len));
-			else
-				memmove(args, pra[ii].buf.pv, pra[ii].buf.len);
+		if (i < inbufs) {
+			if (!kernel) {
+				VERIFY(err, 0 == copy_from_user(args,
+						pra[i].buf.pv, pra[i].buf.len));
+				if (err)
+					goto bail;
+			} else {
+				memmove(args, pra[i].buf.pv, pra[i].buf.len);
+			}
 		}
-		rpra[ii].buf.pv = args;
-		args = (void *)((char *)args + ALIGN_8(pra[ii].buf.len));
-		rlen -= ALIGN_8(pra[ii].buf.len);
+		rpra[i].buf.pv = args;
+		args = (void *)((char *)args + ALIGN(pra[i].buf.len, BALIGN));
+		rlen -= ALIGN(pra[i].buf.len, BALIGN);
 	}
-	for (ii = 0; ii < inbufs; ++ii) {
-		if (rpra[ii].buf.len)
-			dmac_flush_range(rpra[ii].buf.pv,
-				  (char *)rpra[ii].buf.pv + rpra[ii].buf.len);
+	for (i = 0; i < inbufs; ++i) {
+		if (rpra[i].buf.len)
+			dmac_flush_range(rpra[i].buf.pv,
+				  (char *)rpra[i].buf.pv + rpra[i].buf.len);
 	}
 	pbuf->used = pbuf->size - rlen;
 	size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc);
 	if (size) {
 		inh = inbufs + outbufs;
-		if (!kernel)
-			VERIFY(0 == copy_from_user(&rpra[inh], &upra[inh],
+		if (!kernel) {
+			VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh],
 							size));
-		else
+			if (err)
+				goto bail;
+		} else {
 			memmove(&rpra[inh], &upra[inh], size);
+		}
 	}
 	dmac_flush_range(rpra, (char *)rpra + used);
  bail:
@@ -299,24 +326,30 @@
 static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
 			remote_arg_t *rpra, remote_arg_t *upra)
 {
-	int ii, inbufs, outbufs, outh, size;
+	int i, inbufs, outbufs, outh, size;
 	int err = 0;
 
 	inbufs = REMOTE_SCALARS_INBUFS(sc);
 	outbufs = REMOTE_SCALARS_OUTBUFS(sc);
-	for (ii = inbufs; ii < inbufs + outbufs; ++ii) {
-		if (rpra[ii].buf.pv != pra[ii].buf.pv)
-			VERIFY(0 == copy_to_user(pra[ii].buf.pv,
-					rpra[ii].buf.pv, rpra[ii].buf.len));
+	for (i = inbufs; i < inbufs + outbufs; ++i) {
+		if (rpra[i].buf.pv != pra[i].buf.pv) {
+			VERIFY(err, 0 == copy_to_user(pra[i].buf.pv,
+					rpra[i].buf.pv, rpra[i].buf.len));
+			if (err)
+				goto bail;
+		}
 	}
 	size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc);
 	if (size) {
 		outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc);
-		if (!kernel)
-			VERIFY(0 == copy_to_user(&upra[outh], &rpra[outh],
+		if (!kernel) {
+			VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh],
 						size));
-		else
+			if (err)
+				goto bail;
+		} else {
 			memmove(&upra[outh], &rpra[outh], size);
+		}
 	}
  bail:
 	return err;
@@ -324,24 +357,24 @@
 
 static void inv_args(uint32_t sc, remote_arg_t *rpra, int used)
 {
-	int ii, inbufs, outbufs;
+	int i, inbufs, outbufs;
 	int inv = 0;
 
 	inbufs = REMOTE_SCALARS_INBUFS(sc);
 	outbufs = REMOTE_SCALARS_OUTBUFS(sc);
-	for (ii = inbufs; ii < inbufs + outbufs; ++ii) {
-		if (buf_page_start(rpra) == buf_page_start(rpra[ii].buf.pv))
+	for (i = inbufs; i < inbufs + outbufs; ++i) {
+		if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv))
 			inv = 1;
 		else
-			dmac_inv_range(rpra[ii].buf.pv,
-				(char *)rpra[ii].buf.pv + rpra[ii].buf.len);
+			dmac_inv_range(rpra[i].buf.pv,
+				(char *)rpra[i].buf.pv + rpra[i].buf.len);
 	}
 
 	if (inv || REMOTE_SCALARS_OUTHANDLES(sc))
 		dmac_inv_range(rpra, (char *)rpra + used);
 }
 
-static int fastrpc_invoke_send(struct fastrpc_apps *me, remote_handle_t handle,
+static int fastrpc_invoke_send(struct fastrpc_apps *me, uint32_t handle,
 				 uint32_t sc, struct smq_invoke_ctx *ctx,
 				 struct fastrpc_buf *buf)
 {
@@ -358,8 +391,7 @@
 	spin_lock(&me->wrlock);
 	len = smd_write(me->chan, &msg, sizeof(msg));
 	spin_unlock(&me->wrlock);
-	VERIFY(len == sizeof(msg));
- bail:
+	VERIFY(err, len == sizeof(msg));
 	return err;
 }
 
@@ -370,7 +402,8 @@
 	if (me->chan)
 		(void)smd_close(me->chan);
 	context_list_dtor(&me->clst);
-	ion_client_destroy(me->iclient);
+	if (me->iclient)
+		ion_client_destroy(me->iclient);
 	me->iclient = 0;
 	me->chan = 0;
 }
@@ -382,8 +415,10 @@
 	int err = 0;
 
 	do {
-		VERIFY(sizeof(rsp) ==
+		VERIFY(err, sizeof(rsp) ==
 				 smd_read_from_cb(me->chan, &rsp, sizeof(rsp)));
+		if (err)
+			goto bail;
 		context_notify_user(rsp.ctx, rsp.retval);
 	} while (!err);
  bail:
@@ -413,21 +448,29 @@
 	struct fastrpc_apps *me = &gfa;
 
 	if (me->chan == 0) {
-		int ii;
+		int i;
 		spin_lock_init(&me->hlock);
 		spin_lock_init(&me->wrlock);
 		init_completion(&me->work);
-		for (ii = 0; ii < RPC_HASH_SZ; ++ii)
-			INIT_HLIST_HEAD(&me->htbl[ii]);
-		VERIFY(0 == context_list_ctor(&me->clst, SZ_4K));
+		for (i = 0; i < RPC_HASH_SZ; ++i)
+			INIT_HLIST_HEAD(&me->htbl[i]);
+		VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K));
+		if (err)
+			goto bail;
 		me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK,
 							DEVICE_NAME);
-		VERIFY(0 == IS_ERR_OR_NULL(me->iclient));
-		VERIFY(0 == smd_named_open_on_edge(FASTRPC_SMD_GUID,
+		VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient));
+		if (err)
+			goto bail;
+		VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID,
 						SMD_APPS_QDSP, &me->chan,
 						me, smd_event_handler));
-		VERIFY(0 != wait_for_completion_timeout(&me->work,
+		if (err)
+			goto bail;
+		VERIFY(err, 0 != wait_for_completion_timeout(&me->work,
 							RPC_TIMEOUT));
+		if (err)
+			goto bail;
 	}
  bail:
 	if (err)
@@ -449,10 +492,16 @@
 	int err = 0;
 	struct fastrpc_device *fd = 0;
 
-	VERIFY(0 != try_module_get(THIS_MODULE));
-	VERIFY(0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL)));
+	VERIFY(err, 0 != try_module_get(THIS_MODULE));
+	if (err)
+		goto bail;
+	VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL)));
+	if (err)
+		goto bail;
 	fd->buf.size = PAGE_SIZE;
-	VERIFY(0 == alloc_mem(&fd->buf));
+	VERIFY(err, 0 == alloc_mem(&fd->buf));
+	if (err)
+		goto bail;
 	fd->tgid = current->tgid;
 	INIT_HLIST_NODE(&fd->hn);
 	*dev = fd;
@@ -479,7 +528,9 @@
 		}
 	}
 	spin_unlock(&me->hlock);
-	VERIFY(dev != 0);
+	VERIFY(err, dev != 0);
+	if (err)
+		goto bail;
 	*rdev = dev;
  bail:
 	if (err) {
@@ -512,25 +563,41 @@
 	struct fastrpc_buf obuf, *abufs = 0, *b;
 	int interrupted = 0;
 	uint32_t sc;
-	int ii, nbufs = 0, err = 0;
+	int i, nbufs = 0, err = 0;
 
 	sc = invoke->sc;
 	obuf.handle = 0;
 	if (REMOTE_SCALARS_LENGTH(sc)) {
-		VERIFY(0 == get_dev(me, &dev));
-		VERIFY(0 == get_page_list(kernel, sc, pra, &dev->buf, &obuf));
+		VERIFY(err, 0 == get_dev(me, &dev));
+		if (err)
+			goto bail;
+		VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf,
+						&obuf));
+		if (err)
+			goto bail;
 		rpra = (remote_arg_t *)obuf.virt;
-		VERIFY(0 == get_args(kernel, sc, pra, rpra, invoke->pra, &obuf,
-					&abufs, &nbufs));
+		VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra,
+					&obuf, &abufs, &nbufs));
+		if (err)
+			goto bail;
 	}
 
 	context_list_alloc_ctx(&me->clst, &ctx);
-	VERIFY(0 == fastrpc_invoke_send(me, invoke->handle, sc, ctx, &obuf));
+	VERIFY(err, 0 == fastrpc_invoke_send(me, invoke->handle, sc, ctx,
+						&obuf));
+	if (err)
+		goto bail;
 	inv_args(sc, rpra, obuf.used);
-	VERIFY(0 == (interrupted =
+	VERIFY(err, 0 == (interrupted =
 			wait_for_completion_interruptible(&ctx->work)));
-	VERIFY(0 == (err = ctx->retval));
-	VERIFY(0 == put_args(kernel, sc, pra, rpra, invoke->pra));
+	if (err)
+		goto bail;
+	VERIFY(err, 0 == (err = ctx->retval));
+	if (err)
+		goto bail;
+	VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra));
+	if (err)
+		goto bail;
  bail:
 	if (interrupted) {
 		init_completion(&ctx->work);
@@ -539,7 +606,7 @@
 		wait_for_completion(&ctx->work);
 	}
 	context_free(ctx);
-	for (ii = 0, b = abufs; ii < nbufs; ++ii, ++b)
+	for (i = 0, b = abufs; i < nbufs; ++i, ++b)
 		free_mem(b);
 	kfree(abufs);
 	if (dev) {
@@ -564,8 +631,7 @@
 	ioctl.handle = 1;
 	ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
 	ioctl.pra = ra;
-	VERIFY(0 == fastrpc_internal_invoke(me, 1, &ioctl, ra));
- bail:
+	VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra));
 	return err;
 }
 
@@ -583,8 +649,7 @@
 	ioctl.handle = 1;
 	ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
 	ioctl.pra = ra;
-	VERIFY(0 == fastrpc_internal_invoke(me, 1, &ioctl, ra));
- bail:
+	VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra));
 	return err;
 }
 
@@ -629,8 +694,7 @@
 		/* This call will cause a dev to be created
 		 * which will addref this module
 		 */
-		VERIFY(0 == fastrpc_create_current_dsp_process());
- bail:
+		VERIFY(err, 0 == fastrpc_create_current_dsp_process());
 		if (err)
 			cleanup_current_dev();
 		module_put(THIS_MODULE);
@@ -650,19 +714,28 @@
 
 	switch (ioctl_num) {
 	case FASTRPC_IOCTL_INVOKE:
-		VERIFY(0 == copy_from_user(&invoke, param, sizeof(invoke)));
+		VERIFY(err, 0 == copy_from_user(&invoke, param,
+						sizeof(invoke)));
+		if (err)
+			goto bail;
 		bufs = REMOTE_SCALARS_INBUFS(invoke.sc) +
 			REMOTE_SCALARS_OUTBUFS(invoke.sc);
 		if (bufs) {
 			bufs = bufs * sizeof(*pra);
-			VERIFY(0 != (pra = kmalloc(bufs, GFP_KERNEL)));
+			VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL)));
+			if (err)
+				goto bail;
 		}
-		VERIFY(0 == copy_from_user(pra, invoke.pra, bufs));
-		VERIFY(0 == (err = fastrpc_internal_invoke(me, 0, &invoke,
+		VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs));
+		if (err)
+			goto bail;
+		VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke,
 								pra)));
+		if (err)
+			goto bail;
 		break;
 	default:
-		err = -EINVAL;
+		err = -ENOTTY;
 		break;
 	}
  bail:
@@ -681,13 +754,24 @@
 	struct fastrpc_apps *me = &gfa;
 	int err = 0;
 
-	VERIFY(0 == fastrpc_init());
-	VERIFY(0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME));
+	VERIFY(err, 0 == fastrpc_init());
+	if (err)
+		goto bail;
+	VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME));
+	if (err)
+		goto bail;
 	cdev_init(&me->cdev, &fops);
 	me->cdev.owner = THIS_MODULE;
-	VERIFY(0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1));
+	VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1));
+	if (err)
+		goto bail;
 	pr_info("'mknod /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no));
  bail:
+	if (err) {
+		if (me->dev_no)
+			unregister_chrdev_region(me->dev_no, 1);
+		fastrpc_deinit();
+	}
 	return err;
 }