From 5f858a61a8b4f5fec7b3c0618646ed6f7bfcd3a7 Mon Sep 17 00:00:00 2001 From: Christian Gromm Date: Tue, 22 Dec 2015 10:53:01 +0100 Subject: staging: most: encapsulate shared code This patch encapsulates shared code. It therefore creates the new functions stop_channel and destroy_cdev. Signed-off-by: Christian Gromm Signed-off-by: Greg Kroah-Hartman diff --git a/drivers/staging/most/aim-cdev/cdev.c b/drivers/staging/most/aim-cdev/cdev.c index fd2ac89..533e290 100644 --- a/drivers/staging/most/aim-cdev/cdev.c +++ b/drivers/staging/most/aim-cdev/cdev.c @@ -69,6 +69,30 @@ static struct aim_channel *get_channel(struct most_interface *iface, int id) return channel; } +static void stop_channel(struct aim_channel *c) +{ + struct mbo *mbo; + + while (kfifo_out((struct kfifo *)&c->fifo, &mbo, 1)) + most_put_mbo(mbo); + if (c->stacked_mbo) + most_put_mbo(c->stacked_mbo); + most_stop_channel(c->iface, c->channel_id, &cdev_aim); +} + +static void destroy_cdev(struct aim_channel *c) +{ + unsigned long flags; + + device_destroy(aim_class, c->devno); + cdev_del(&c->cdev); + kfifo_free(&c->fifo); + spin_lock_irqsave(&ch_list_lock, flags); + list_del(&c->list); + spin_unlock_irqrestore(&ch_list_lock, flags); + ida_simple_remove(&minor_id, MINOR(c->devno)); +} + /** * aim_open - implements the syscall to open the device * @inode: inode pointer @@ -114,31 +138,21 @@ static int aim_open(struct inode *inode, struct file *filp) */ static int aim_close(struct inode *inode, struct file *filp) { - int ret; - struct mbo *mbo; struct aim_channel *channel = to_channel(inode->i_cdev); mutex_lock(&channel->io_mutex); if (!channel->dev) { mutex_unlock(&channel->io_mutex); atomic_dec(&channel->access_ref); - device_destroy(aim_class, channel->devno); - cdev_del(&channel->cdev); - kfifo_free(&channel->fifo); - list_del(&channel->list); - ida_simple_remove(&minor_id, MINOR(channel->devno)); + destroy_cdev(channel); kfree(channel); return 0; } mutex_unlock(&channel->io_mutex); - while (kfifo_out((struct kfifo *)&channel->fifo, &mbo, 1)) - most_put_mbo(mbo); - if (channel->stacked_mbo) - most_put_mbo(channel->stacked_mbo); - ret = most_stop_channel(channel->iface, channel->channel_id, &cdev_aim); + stop_channel(channel); atomic_dec(&channel->access_ref); - return ret; + return 0; } /** @@ -310,7 +324,6 @@ static const struct file_operations channel_fops = { static int aim_disconnect_channel(struct most_interface *iface, int channel_id) { struct aim_channel *channel; - unsigned long flags; if (!iface) { pr_info("Bad interface pointer\n"); @@ -326,13 +339,7 @@ static int aim_disconnect_channel(struct most_interface *iface, int channel_id) mutex_unlock(&channel->io_mutex); if (atomic_read(&channel->access_ref)) { - device_destroy(aim_class, channel->devno); - cdev_del(&channel->cdev); - kfifo_free(&channel->fifo); - ida_simple_remove(&minor_id, MINOR(channel->devno)); - spin_lock_irqsave(&ch_list_lock, flags); - list_del(&channel->list); - spin_unlock_irqrestore(&ch_list_lock, flags); + destroy_cdev(channel); kfree(channel); } else { wake_up_interruptible(&channel->wq); @@ -526,11 +533,7 @@ static void __exit mod_exit(void) most_deregister_aim(&cdev_aim); list_for_each_entry_safe(channel, tmp, &channel_list, list) { - device_destroy(aim_class, channel->devno); - cdev_del(&channel->cdev); - kfifo_free(&channel->fifo); - list_del(&channel->list); - ida_simple_remove(&minor_id, MINOR(channel->devno)); + destroy_cdev(channel); kfree(channel); } class_destroy(aim_class); -- cgit v0.10.2