PERFORCE change 166069 for review

Hans Petter Selasky hselasky at FreeBSD.org
Tue Jul 14 08:28:51 UTC 2009


http://perforce.freebsd.org/chv.cgi?CH=166069

Change 166069 by hselasky at hselasky_laptop001 on 2009/07/14 08:28:28

	
	LibUSB:
	 - cleanup casting between struct libusb20_device and libusb_device_handle .
	 - cleanup and optimise debug macro
	 - fix one or two compiler warnings
	 - rename usb_pollfd structure

Affected files ...

.. //depot/projects/usb/src/lib/libusb/libusb10.c#8 edit
.. //depot/projects/usb/src/lib/libusb/libusb10.h#5 edit

Differences ...

==== //depot/projects/usb/src/lib/libusb/libusb10.c#8 (text+ko) ====

@@ -44,14 +44,14 @@
 
 /* Prototypes */
 
-static struct libusb20_transfer *libusb10_get_transfer(libusb_device_handle *devh, uint8_t endpoint, uint8_t index);
+static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *pdev, uint8_t endpoint, uint8_t index);
 static int libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer);
 static int libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer);
 static int libusb10_convert_error(uint8_t status);
 static void libusb10_isoc_proxy(struct libusb20_transfer *pxfer);
 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer);
 static void libusb10_ctrl_proxy(struct libusb20_transfer *pxfer);
-static void libusb10_submit_transfer_sub(struct libusb_device_handle *devh, uint8_t endpoint);
+static void libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint);
 
 /*  Library initialisation / deinitialisation */
 
@@ -340,7 +340,7 @@
 		/* ignore error, if any */
 		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
 	}
-	*devh = (libusb_device_handle *)pdev;
+	*devh = pdev;
 
 	return (0);
 }
@@ -350,7 +350,6 @@
     uint16_t product_id)
 {
 	struct libusb_device **devs;
-	struct libusb_device_handle *devh;
 	struct libusb20_device *pdev;
 	struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
 	int i;
@@ -365,7 +364,7 @@
 	if ((i = libusb_get_device_list(ctx, &devs)) < 0)
 		return (NULL);
 
-	devh = NULL;
+	pdev = NULL;
 
 	for (j = 0; j < i; j++) {
 		pdev = devs[j]->os_priv;
@@ -377,32 +376,29 @@
 		 */
 		if (pdesc->idVendor == vendor_id &&
 		    pdesc->idProduct == product_id) {
-			if (libusb_open(devs[j], &devh) < 0)
-				devh = NULL;
+			if (libusb_open(devs[j], &pdev) < 0)
+				pdev = NULL;
 			break;
 		}
 	}
 
 	libusb_free_device_list(devs, 1);
 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
-	return (devh);
+	return (pdev);
 }
 
 void
-libusb_close(libusb_device_handle *devh)
+libusb_close(struct libusb20_device *pdev)
 {
 	libusb_context *ctx;
-	struct libusb20_device *pdev;
 	struct libusb_device *dev;
-	struct usb_ep_tr *eptr;
 	uint8_t dummy;
 	int err;
 
-	if (devh == NULL)
+	if (pdev == NULL)
 		return;			/* be NULL safe */
 
-	pdev = (struct libusb20_device *)devh;
-	dev = pdev->privLuData;
+	dev = libusb_get_device(pdev);
 	ctx = dev->ctx;
 
 	libusb10_remove_pollfd(ctx, &dev->dev_poll);
@@ -420,25 +416,21 @@
 }
 
 libusb_device *
-libusb_get_device(libusb_device_handle *devh)
+libusb_get_device(struct libusb20_device *pdev)
 {
-	if (devh == NULL)
+	if (pdev == NULL)
 		return (NULL);
-	return ((libusb_device *)(((struct libusb20_device *)devh)->privLuData));
+	return ((libusb_device *)pdev->privLuData);
 }
 
 int
-libusb_get_configuration(libusb_device_handle *devh, int *config)
+libusb_get_configuration(struct libusb20_device *pdev, int *config)
 {
 	struct libusb20_config *pconf;
-	struct libusb20_device *pdev;
-	int err;
 
-	if (devh == NULL || config == NULL)
+	if (pdev == NULL || config == NULL)
 		return (LIBUSB_ERROR_INVALID_PARAM);
 
-	pdev = (struct libusb20_device *)devh;
-
 	pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
 	if (pconf == NULL)
 		return (LIBUSB_ERROR_NO_MEM);
@@ -451,21 +443,18 @@
 }
 
 int
-libusb_set_configuration(libusb_device_handle *devh, int configuration)
+libusb_set_configuration(struct libusb20_device *pdev, int configuration)
 {
 	struct libusb20_config *pconf;
-	struct libusb20_device *pdev;
 	struct libusb_device *dev;
 	int err;
 	uint8_t i;
 
-	dev = libusb_get_device(devh);
+	dev = libusb_get_device(pdev);
 
 	if (dev == NULL)
 		return (LIBUSB_ERROR_INVALID_PARAM);
 
-	pdev = (struct libusb20_device *)devh;
-
 	if (configuration < 1) {
 		/* unconfigure */
 		i = 255;
@@ -501,12 +490,12 @@
 }
 
 int
-libusb_claim_interface(libusb_device_handle *devh, int interface_number)
+libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
 {
 	libusb_device *dev;
 	int err = 0;
 
-	dev = libusb_get_device(devh);
+	dev = libusb_get_device(pdev);
 	if (dev == NULL)
 		return (LIBUSB_ERROR_INVALID_PARAM);
 
@@ -524,12 +513,12 @@
 }
 
 int
-libusb_release_interface(libusb_device_handle *devh, int interface_number)
+libusb_release_interface(struct libusb20_device *pdev, int interface_number)
 {
 	libusb_device *dev;
 	int err = 0;
 
-	dev = libusb_get_device(devh);
+	dev = libusb_get_device(pdev);
 	if (dev == NULL)
 		return (LIBUSB_ERROR_INVALID_PARAM);
 
@@ -547,14 +536,13 @@
 }
 
 int
-libusb_set_interface_alt_setting(libusb_device_handle *devh,
+libusb_set_interface_alt_setting(struct libusb20_device *pdev,
     int interface_number, int alternate_setting)
 {
-	struct libusb20_device *pdev;
 	libusb_device *dev;
 	int err = 0;
 
-	dev = libusb_get_device(devh);
+	dev = libusb_get_device(pdev);
 	if (dev == NULL)
 		return (LIBUSB_ERROR_INVALID_PARAM);
 
@@ -573,8 +561,6 @@
 
 	usb_cancel_all_dev(dev);
 
-	pdev = (struct libusb20_device *)devh;
-
 	err = libusb20_dev_set_alt_index(pdev,
 	    interface_number, alternate_setting);
 
@@ -585,9 +571,8 @@
 	return (err ? LIBUSB_ERROR_OTHER : 0);
 }
 
-
-UNEXPORTED static struct libusb20_transfer *
-libusb10_get_transfer(libusb_device_handle *devh,
+static struct libusb20_transfer *
+libusb10_get_transfer(struct libusb20_device *pdev,
     uint8_t endpoint, uint8_t index)
 {
 	index &= 1;			/* double buffering */
@@ -598,22 +583,21 @@
 		/* this is an IN endpoint */
 		index |= 2;
 	}
-	return (libusb20_tr_get_pointer((struct libusb20_device *)devh, index));
+	return (libusb20_tr_get_pointer(pdev, index));
 }
 
 int
-libusb_clear_halt(libusb_device_handle *devh, uint8_t endpoint)
+libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
 {
 	struct libusb20_transfer *xfer;
-	struct libusb20_device *pdev;
 	struct libusb_device *dev;
 	int err;
 
-	xfer = libusb10_get_transfer(devh, endpoint, 0);
+	xfer = libusb10_get_transfer(pdev, endpoint, 0);
 	if (xfer == NULL)
 		return (LIBUSB_ERROR_INVALID_PARAM);
 
-	dev = pdev->privLuData;
+	dev = libusb_get_device(pdev);
 
 	CTX_LOCK(dev->ctx);
 	err = libusb20_tr_open(xfer, 0, 0, endpoint);
@@ -634,13 +618,12 @@
 }
 
 int
-libusb_reset_device(libusb_device_handle *devh)
+libusb_reset_device(struct libusb20_device *pdev)
 {
-	struct libusb20_device *pdev;
 	libusb_device *dev;
 	int err;
 
-	dev = libusb_get_device(devh);
+	dev = libusb_get_device(pdev);
 	if (dev == NULL)
 		return (LIBUSB20_ERROR_INVALID_PARAM);
 
@@ -648,9 +631,7 @@
 
 	usb_cancel_all_dev(dev);
 
-	err = libusb20_dev_reset((struct libusb20_device *)devh);
-
-	pdev = (struct libusb20_device *)devh;
+	err = libusb20_dev_reset(pdev);
 
 	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
 	    pdev, libusb20_dev_get_fd(pdev),
@@ -660,33 +641,33 @@
 }
 
 int
-libusb_kernel_driver_active(libusb_device_handle *devh, int interface)
+libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
 {
-	if (devh == NULL)
+	if (pdev == NULL)
 		return (LIBUSB_ERROR_INVALID_PARAM);
 
 	return (libusb20_dev_kernel_driver_active(
-	    (struct libusb20_device *)devh, interface));
+	    pdev, interface));
 }
 
 int
-libusb_detach_kernel_driver(libusb_device_handle *devh, int interface)
+libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
 {
 	int err;
 
-	if (devh == NULL)
+	if (pdev == NULL)
 		return (LIBUSB_ERROR_INVALID_PARAM);
 
 	err = libusb20_dev_detach_kernel_driver(
-	    (struct libusb20_device *)devh, interface);
+	    pdev, interface);
 
 	return (err ? LIBUSB20_ERROR_OTHER : 0);
 }
 
 int
-libusb_attach_kernel_driver(libusb_device_handle *devh, int interface)
+libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
 {
-	if (devh == NULL)
+	if (pdev == NULL)
 		return (LIBUSB_ERROR_INVALID_PARAM);
 	/* stub - currently not supported by libusb20 */
 	return (0);
@@ -870,7 +851,6 @@
 		/* setup length(s) */
 		actlen = 0;
 		for (i = 0; i != iso_packets; i++) {
-			uxfer->iso_packet_desc[i].length;
 			libusb20_tr_setup_isoc(pxfer,
 			    &uxfer->buffer[actlen],
 			    uxfer->iso_packet_desc[i].length, i);
@@ -883,6 +863,7 @@
 		libusb20_tr_set_total_frames(pxfer, iso_packets);
 		libusb20_tr_submit(pxfer);
 
+		/* fork another USB transfer, if any */
 		libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
 		break;
 
@@ -1052,7 +1033,7 @@
 /* The following function must be called locked */
 
 static void
-libusb10_submit_transfer_sub(struct libusb_device_handle *devh, uint8_t endpoint)
+libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
 {
 	struct libusb20_transfer *pxfer0;
 	struct libusb20_transfer *pxfer1;
@@ -1064,10 +1045,10 @@
 	int maxframe;
 	int temp;
 
-	dev = libusb_get_device(devh);
+	dev = libusb_get_device(pdev);
 
-	pxfer0 = libusb10_get_transfer(devh, endpoint, 0);
-	pxfer1 = libusb10_get_transfer(devh, endpoint, 1);
+	pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
+	pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
 
 	if (pxfer0 == NULL || pxfer1 == NULL)
 		return;			/* shouldn't happen */
@@ -1116,7 +1097,7 @@
 
 found:
 
-	libusb20_tr_set_priv_sc0(pxfer0, devh);
+	libusb20_tr_set_priv_sc0(pxfer0, pdev);
 	libusb20_tr_set_priv_sc1(pxfer0, sxfer);
 
 	/* reset super transfer state */
@@ -1147,8 +1128,8 @@
 		return;
 	}
 
-	buffsize = libusb10_get_buffsize((struct libusb20_device *)devh, uxfer);
-	maxframe = libusb10_get_maxframe((struct libusb20_device *)devh, uxfer);
+	buffsize = libusb10_get_buffsize(pdev, uxfer);
+	maxframe = libusb10_get_maxframe(pdev, uxfer);
 
 	/* make sure the transfer is opened */
 	err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
@@ -1162,13 +1143,13 @@
 /* The following function must be called locked */
 
 UNEXPORTED void
-libusb10_complete_transfer_sub(struct libusb_device_handle *devh)
+libusb10_complete_transfer_sub(struct libusb20_device *pdev)
 {
 	struct libusb_device *dev;
 	struct libusb_super_transfer *sxfer;
 	struct libusb_transfer *uxfer;
 
-	dev = libusb_get_device(devh);
+	dev = libusb_get_device(pdev);
 
 	while ((sxfer = TAILQ_FIRST(&dev->tr_done))) {
 		TAILQ_REMOVE(&dev->tr_done, sxfer, entry);

==== //depot/projects/usb/src/lib/libusb/libusb10.h#5 (text+ko) ====

@@ -24,45 +24,46 @@
  */
 
 #ifndef __LIBUSB10_H__
-#define __LIBUSB10_H__
+#define	__LIBUSB10_H__
 
 #include <sys/queue.h>
 
-#define GET_CONTEXT(ctx) (((ctx) == NULL) ? usbi_default_context : (ctx))
-#define MAX(a,b) (((a)>(b))?(a):(b))
-#define USB_TIMED_OUT (1<<0)
-#define UNEXPORTED __attribute__((__visibility__("hidden")))
+#define	GET_CONTEXT(ctx) (((ctx) == NULL) ? usbi_default_context : (ctx))
+#define	UNEXPORTED __attribute__((__visibility__("hidden")))
 #define	CTX_LOCK(ctx) pthread_mutex_lock(&(ctx)->ctx_lock)
 #define	CTX_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->ctx_lock)
 
-#define DPRINTF(ctx, dbg, format, args...)	\
-if (ctx->debug == dbg) {			\
-	printf("LIBUSB_%s : ", (ctx->debug == LIBUSB_DEBUG_FUNCTION) ? "FUNCTION" : "TRANSFER");	\
-	switch(ctx->debug) {			\
-		case LIBUSB_DEBUG_FUNCTION:	\
-			printf(format, ## args);\
-			break ;			\
-		case LIBUSB_DEBUG_TRANSFER:	\
-			printf(format, ## args);\
-			break ;			\
+#define	DPRINTF(ctx, dbg, format, args...) do {	\
+    if ((ctx)->debug == dbg) {			\
+	switch (dbg) {				\
+	case LIBUSB_DEBUG_FUNCTION:		\
+		printf("LIBUSB_FUNCTION: "	\
+		    format "\n", ## args);	\
+		break;				\
+	case LIBUSB_DEBUG_TRANSFER:		\
+		printf("LIBUSB_TRANSFER: "	\
+		    format "\n", ## args);	\
+		break;				\
+	default:				\
+		break;				\
 	}					\
-	printf("\n");				\
-}
+    }						\
+} while(0)
 
 /* internal structures */
 
-struct usb_pollfd {
+struct libusb_super_pollfd {
 	TAILQ_ENTRY(usb_pollfd) list;
 	struct libusb20_device *pdev;
 	struct libusb_pollfd pollfd;
 };
 
 struct libusb_super_transfer {
-	TAILQ_ENTRY(libusb_super_transfer) entry;
+	TAILQ_ENTRY(libusb_super_transfer)entry;
 	uint8_t *curr_data;
 	uint32_t rem_len;
 	uint32_t last_len;
-	uint8_t flags;
+	uint8_t	flags;
 };
 
 struct libusb_context {
@@ -75,7 +76,7 @@
 
 	TAILQ_HEAD(pollfds_list, usb_pollfd) pollfds;
 
-	struct usb_pollfd ctx_poll;
+	struct libusb_super_pollfd ctx_poll;
 
 	libusb_pollfd_added_cb fd_added_cb;
 	libusb_pollfd_removed_cb fd_removed_cb;
@@ -89,20 +90,20 @@
 
 	uint32_t claimed_interfaces;
 
-	struct usb_pollfd dev_poll;
+	struct libusb_super_pollfd dev_poll;
 
 	struct libusb_context *ctx;
 
-	TAILQ_HEAD(,libusb_super_transfer) tr_head;
-	TAILQ_HEAD(,libusb_super_transfer) tr_done;
+	TAILQ_HEAD(, libusb_super_transfer)tr_head;
+	TAILQ_HEAD(, libusb_super_transfer)tr_done;
 
 	struct libusb20_device *os_priv;
 };
 
 extern struct libusb_context *usbi_default_context;
 
-void libusb10_add_pollfd(libusb_context *ctx, struct usb_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events);
-void libusb10_remove_pollfd(libusb_context *ctx, struct usb_pollfd *pollfd);
-void libusb10_complete_transfer_sub(struct libusb_device_handle *devh);
+void	libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events);
+void	libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd);
+void	libusb10_complete_transfer_sub(libusb_device_handle *devh);
 
-#endif /*__LIBUSB10_H__*/
+#endif					/* __LIBUSB10_H__ */


More information about the p4-projects mailing list