Commit 579b6f18 authored by Dave Penkler's avatar Dave Penkler Committed by Greg Kroah-Hartman
Browse files

staging: gpib: Agilent usb code cleanup



Remove useless #ifdef RESET_USB_CONFIG code.

Change kalloc / memset to kzalloc

The attach function was not freeing the private data on error
returns. Separate the releasing of urbs and private data and
add a common error exit for attach failure.

Set the board private data pointer to NULL after freeing
the private data.

Reduce console spam by emitting only one attach message.

Change last pr_err in attach to dev_err

Signed-off-by: default avatarDave Penkler <dpenkler@gmail.com>
Link: https://lore.kernel.org/r/20250118145046.12181-3-dpenkler@gmail.com


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 6a6c1535
Loading
Loading
Loading
Loading
+36 −48
Original line number Diff line number Diff line
@@ -1146,25 +1146,6 @@ static int agilent_82357a_setup_urbs(gpib_board_t *board)
	return retval;
}

#ifdef RESET_USB_CONFIG
static int agilent_82357a_reset_usb_configuration(gpib_board_t *board)
{
	struct agilent_82357a_priv *a_priv = board->private_data;
	struct usb_device *usb_dev = interface_to_usbdev(a_priv->bus_interface);
	struct usb_device *usb_dev;
	int retval;

	if (!a_priv->bus_interface)
		return -ENODEV;
	usb_dev = interface_to_usbdev(a_priv->bus_interface);
	retval = usb_reset_configuration(usb_dev);
	if (retval)
		dev_err(&usb_dev->dev, "%s: usb_reset_configuration() returned %i\n",
			__func__, retval);
	return retval;
}
#endif

static void agilent_82357a_cleanup_urbs(struct agilent_82357a_priv *a_priv)
{
	if (a_priv && a_priv->bus_interface) {
@@ -1175,15 +1156,23 @@ static void agilent_82357a_cleanup_urbs(struct agilent_82357a_priv *a_priv)
	}
};

static void agilent_82357a_release_urbs(struct agilent_82357a_priv *a_priv)
{
	if (a_priv) {
		usb_free_urb(a_priv->interrupt_urb);
		a_priv->interrupt_urb = NULL;
		kfree(a_priv->interrupt_buffer);
	}
}

static int agilent_82357a_allocate_private(gpib_board_t *board)
{
	struct agilent_82357a_priv *a_priv;

	board->private_data = kmalloc(sizeof(struct agilent_82357a_priv), GFP_KERNEL);
	board->private_data = kzalloc(sizeof(struct agilent_82357a_priv), GFP_KERNEL);
	if (!board->private_data)
		return -ENOMEM;
	a_priv = board->private_data;
	memset(a_priv, 0, sizeof(struct agilent_82357a_priv));
	mutex_init(&a_priv->bulk_transfer_lock);
	mutex_init(&a_priv->bulk_alloc_lock);
	mutex_init(&a_priv->control_alloc_lock);
@@ -1191,11 +1180,11 @@ static int agilent_82357a_allocate_private(gpib_board_t *board)
	return 0;
}

static void agilent_82357a_free_private(struct agilent_82357a_priv *a_priv)
static void agilent_82357a_free_private(gpib_board_t *board)
{
	usb_free_urb(a_priv->interrupt_urb);
	kfree(a_priv->interrupt_buffer);
	kfree(a_priv);
	kfree(board->private_data);
	board->private_data = NULL;

}

static int agilent_82357a_init(gpib_board_t *board)
@@ -1342,16 +1331,14 @@ static int agilent_82357a_attach(gpib_board_t *board, const gpib_board_config_t
			a_priv->bus_interface = agilent_82357a_driver_interfaces[i];
			usb_set_intfdata(agilent_82357a_driver_interfaces[i], board);
			usb_dev = interface_to_usbdev(a_priv->bus_interface);
			dev_info(&usb_dev->dev,
				 "bus %d dev num %d attached to gpib minor %d, agilent usb interface %i\n",
				 usb_dev->bus->busnum, usb_dev->devnum, board->minor, i);
			break;
		}
	}
	if (i == MAX_NUM_82357A_INTERFACES) {
		mutex_unlock(&agilent_82357a_hotplug_lock);
		pr_err("No Agilent 82357 gpib adapters found, have you loaded its firmware?\n");
		return -ENODEV;
		dev_err(board->gpib_dev,
			"No Agilent 82357 gpib adapters found, have you loaded its firmware?\n");
		retval = -ENODEV;
		goto attach_fail;
	}
	product_id = le16_to_cpu(interface_to_usbdev(a_priv->bus_interface)->descriptor.idProduct);
	switch (product_id) {
@@ -1365,21 +1352,13 @@ static int agilent_82357a_attach(gpib_board_t *board, const gpib_board_config_t
		break;
	default:
		dev_err(&usb_dev->dev, "bug, unhandled product_id in switch?\n");
		mutex_unlock(&agilent_82357a_hotplug_lock);
		return -EIO;
	}
#ifdef RESET_USB_CONFIG
	retval = agilent_82357a_reset_usb_configuration(board);
	if (retval < 0)	{
		mutex_unlock(&agilent_82357a_hotplug_lock);
		return retval;
		retval = -EIO;
		goto attach_fail;
	}
#endif

	retval = agilent_82357a_setup_urbs(board);
	if (retval < 0)	{
		mutex_unlock(&agilent_82357a_hotplug_lock);
		return retval;
	}
	if (retval < 0)
		goto attach_fail;

	timer_setup(&a_priv->bulk_timer, agilent_82357a_timeout_handler, 0);

@@ -1388,11 +1367,19 @@ static int agilent_82357a_attach(gpib_board_t *board, const gpib_board_config_t
	retval = agilent_82357a_init(board);

	if (retval < 0)	{
		agilent_82357a_cleanup_urbs(a_priv);
		agilent_82357a_release_urbs(a_priv);
		goto attach_fail;
	}

	dev_info(&usb_dev->dev,
		 "bus %d dev num %d attached to gpib minor %d, agilent usb interface %i\n",
		 usb_dev->bus->busnum, usb_dev->devnum, board->minor, i);
	mutex_unlock(&agilent_82357a_hotplug_lock);
	return retval;
	}

	dev_info(&usb_dev->dev, "%s: attached\n", __func__);
attach_fail:
	agilent_82357a_free_private(board);
	mutex_unlock(&agilent_82357a_hotplug_lock);
	return retval;
}
@@ -1455,7 +1442,8 @@ static void agilent_82357a_detach(gpib_board_t *board)
		mutex_lock(&a_priv->bulk_alloc_lock);
		mutex_lock(&a_priv->interrupt_alloc_lock);
		agilent_82357a_cleanup_urbs(a_priv);
		agilent_82357a_free_private(a_priv);
		agilent_82357a_release_urbs(a_priv);
		agilent_82357a_free_private(board);
	}
	dev_info(board->gpib_dev, "%s: detached\n", __func__);
	mutex_unlock(&agilent_82357a_hotplug_lock);