PERFORCE change 161836 for review

Sylvestre Gallon syl at FreeBSD.org
Sat May 9 16:26:09 UTC 2009


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

Change 161836 by syl at syl_atuin on 2009/05/09 16:25:44

	- fix event_waiters_cond in libusb.h
	- Fix transfer bug reported by Hans Petter Selasky.
	- Add GET_XFER macro to simplify the acquirement of an xfer.
	- Implement libusb_try_lock_events.
	- Implement libusb_lock_events.
	- Implement libusb_unlock_events.
	- Implement libusb_event_handling_ok.
	- Implement libusb_event_handler_active.
	- Implement libusb_lock_event_waiters.
	- Implement libusb_unlock_event_waiters.
	- Implement libusb_set_pollfd_notifiers.	

Affected files ...

.. //depot/projects/soc2009/syl_usb/src/lib/libusb/libusb.h#7 edit
.. //depot/projects/soc2009/syl_usb/src/lib/libusb/libusb10.c#8 edit

Differences ...

==== //depot/projects/soc2009/syl_usb/src/lib/libusb/libusb.h#7 (text+ko) ====

@@ -207,7 +207,7 @@
 	int	event_handler_active;
 
 	pthread_mutex_t event_waiters_lock;
-	pthread_mutex_t event_waiters_cond;
+	pthread_cond_t event_waiters_cond;
 }	libusb_context;
 
 typedef struct libusb_device {

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

@@ -54,6 +54,13 @@
 	(entry)->next->prev = (entry)->prev; \
 	(entry)->prev->next = (entry)->next;
 
+/* fetxh libusb20_transfer from libusb20_device */
+#define GET_XFER(xfer, endpoint, pdev)\
+	xfer = libusb20_tr_get_pointer(pdev, \
+	    (2 *endpoint)|(endpoint/0x80)); \
+	if (xfer == NULL) \
+		return (LIBUSB_ERROR_OTHER);
+
 /*  Library initialisation / deinitialisation */
 
 void
@@ -442,12 +449,20 @@
 }
 
 int
-libusb_clear_halt(libusb_device_handle * dev, unsigned char endpoint)
+libusb_clear_halt(libusb_device_handle * devh, unsigned char endpoint)
 {
-	struct libusb20_transfer xfer;
+	struct libusb20_transfer *xfer;
+	int ret; 
+
+	GET_XFER(xfer, endpoint, devh->os_priv);
+
+	ret = libusb20_tr_open(xfer, 0, 0, endpoint);
+	if (ret != 0 && ret != LIBUSB20_ERROR_BUSY)
+		return (LIBUSB_ERROR_OTHER);
 
-	libusb20_tr_open(&xfer, 0, 0, endpoint);
-	libusb20_tr_clear_stall_sync(&xfer);
+	libusb20_tr_clear_stall_sync(xfer);
+	if (ret == 0) /* check if we have open the device */
+		libusb20_tr_close(xfer);
 	return (0);
 }
 
@@ -484,7 +499,6 @@
 	return (0);
 }
 
-
 /* 
  * stub function.
  * libusb20 doesn't support this feature.
@@ -542,12 +556,21 @@
 libusb_submit_transfer(struct libusb_transfer *xfer)
 {
 	struct libusb20_transfer *usb20_xfer;
+	struct libusb20_device *pdev;
 	int ret;
 
-	usb20_xfer = malloc(sizeof(*usb20_xfer));
+	pdev = xfer->dev_handle->os_priv;
+	GET_XFER(usb20_xfer, xfer->endpoint, pdev);
 	xfer->os_priv = usb20_xfer;
 
-	libusb20_tr_open(usb20_xfer, xfer->length, xfer->num_iso_packets, xfer->endpoint);
+	ret = libusb20_tr_open(usb20_xfer, xfer->length, xfer->num_iso_packets, 
+	    xfer->endpoint);
+       
+	if (ret	== LIBUSB20_ERROR_BUSY)
+		return (LIBUSB_ERROR_BUSY);
+	if (ret != 0)
+		return (LIBUSB_ERROR_OTHER);
+
 	libusb20_tr_set_timeout(usb20_xfer, xfer->timeout);
 	libusb20_tr_set_buffer(usb20_xfer, xfer->buffer, xfer->num_iso_packets);
 	libusb20_tr_set_length(usb20_xfer, xfer->length, xfer->num_iso_packets);
@@ -601,42 +624,79 @@
 int
 libusb_try_lock_events(libusb_context * ctx)
 {
+	int ret;
+
+	pthread_mutex_lock(&ctx->pollfd_modify_lock);
+	ret = ctx->pollfd_modify;
+	pthread_mutex_unlock(&ctx->pollfd_modify_lock);
+
+	if (ret != 0)
+		return (1);
+
+	ret = pthread_mutex_trylock(&ctx->events_lock);
+	
+	if (ret != 0)
+		return (1);
+	
+	ctx->event_handler_active = 1;
 	return (0);
 }
 
 void
 libusb_lock_events(libusb_context * ctx)
 {
-	return;
+	pthread_mutex_lock(&ctx->events_lock);
+	ctx->event_handler_active = 1;
 }
 
 void
 libusb_unlock_events(libusb_context * ctx)
 {
-	return;
+	ctx->event_handler_active = 0;
+	pthread_mutex_unlock(&ctx->events_lock);
+
+	pthread_mutex_lock(&ctx->event_waiters_lock);
+	pthread_cond_broadcast(&ctx->event_waiters_cond);
+	pthread_mutex_unlock(&ctx->event_waiters_lock);
 }
 
 int
 libusb_event_handling_ok(libusb_context * ctx)
 {
-	return (0);
+	int ret;
+
+	pthread_mutex_lock(&ctx->pollfd_modify_lock);
+	ret = ctx->pollfd_modify;
+	pthread_mutex_unlock(&ctx->pollfd_modify_lock);
+	if (ret)
+		return (0);
+	return (1);
 }
 
 int
 libusb_event_handler_active(libusb_context * ctx)
 {
-	return (0);
+	int ret;
+
+	pthread_mutex_lock(&ctx->pollfd_modify_lock);
+	ret = ctx->pollfd_modify;
+	pthread_mutex_unlock(&ctx->pollfd_modify_lock);
+	if (ret)
+		return (1);
+	return (ctx->event_handler_active);
 }
 
 void
 libusb_lock_event_waiters(libusb_context * ctx)
 {
+	pthread_mutex_lock(&ctx->event_waiters_lock);
 	return;
 }
 
 void
 libusb_unlock_event_waiters(libusb_context * ctx)
 {
+	pthread_mutex_unlock(&ctx->event_waiters_lock);
 	return;
 }
 
@@ -679,7 +739,9 @@
     libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
     void *user_data)
 {
-	return;
+	ctx->fd_added_cb = added_cb;
+	ctx->fd_removed_cb = removed_cb;
+	ctx->fd_cb_user_data = user_data;
 }
 
 struct libusb_pollfd **


More information about the p4-projects mailing list