| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (C) 2003-2008 Takahiro Hirofuchi | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software | 
					
						
							|  |  |  |  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | 
					
						
							|  |  |  |  * USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | #include <linux/kthread.h>
 | 
					
						
							| 
									
										
										
										
											2011-07-10 13:09:12 -04:00
										 |  |  | #include <linux/export.h>
 | 
					
						
							| 
									
										
										
										
											2011-05-11 22:33:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-06 03:47:47 -07:00
										 |  |  | #include "usbip_common.h"
 | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int event_handler(struct usbip_device *ud) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | 	usbip_dbg_eh("enter\n"); | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Events are handled by only this thread. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | 	while (usbip_event_happened(ud)) { | 
					
						
							|  |  |  | 		usbip_dbg_eh("pending event %lx\n", ud->event); | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * NOTE: shutdown must come first. | 
					
						
							|  |  |  | 		 * Shutdown the device. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		if (ud->event & USBIP_EH_SHUTDOWN) { | 
					
						
							|  |  |  | 			ud->eh_ops.shutdown(ud); | 
					
						
							|  |  |  | 			ud->event &= ~USBIP_EH_SHUTDOWN; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Reset the device. */ | 
					
						
							|  |  |  | 		if (ud->event & USBIP_EH_RESET) { | 
					
						
							|  |  |  | 			ud->eh_ops.reset(ud); | 
					
						
							|  |  |  | 			ud->event &= ~USBIP_EH_RESET; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Mark the device as unusable. */ | 
					
						
							|  |  |  | 		if (ud->event & USBIP_EH_UNUSABLE) { | 
					
						
							|  |  |  | 			ud->eh_ops.unusable(ud); | 
					
						
							|  |  |  | 			ud->event &= ~USBIP_EH_UNUSABLE; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-21 17:43:30 +02:00
										 |  |  | 		/* Stop the error handler. */ | 
					
						
							|  |  |  | 		if (ud->event & USBIP_EH_BYE) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 00:13:05 +01:00
										 |  |  | static int event_handler_loop(void *data) | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-02 00:13:05 +01:00
										 |  |  | 	struct usbip_device *ud = data; | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 00:13:05 +01:00
										 |  |  | 	while (!kthread_should_stop()) { | 
					
						
							|  |  |  | 		wait_event_interruptible(ud->eh_waitq, | 
					
						
							| 
									
										
										
										
											2011-05-06 03:47:47 -07:00
										 |  |  | 					 usbip_event_happened(ud) || | 
					
						
							|  |  |  | 					 kthread_should_stop()); | 
					
						
							| 
									
										
										
										
											2011-03-02 00:13:05 +01:00
										 |  |  | 		usbip_dbg_eh("wakeup\n"); | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (event_handler(ud) < 0) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-05-06 03:47:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 00:13:05 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | int usbip_start_eh(struct usbip_device *ud) | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | { | 
					
						
							|  |  |  | 	init_waitqueue_head(&ud->eh_waitq); | 
					
						
							|  |  |  | 	ud->event = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 00:13:05 +01:00
										 |  |  | 	ud->eh = kthread_run(event_handler_loop, ud, "usbip_eh"); | 
					
						
							|  |  |  | 	if (IS_ERR(ud->eh)) { | 
					
						
							| 
									
										
										
										
											2013-05-05 16:28:37 -07:00
										 |  |  | 		pr_warn("Unable to start control thread\n"); | 
					
						
							| 
									
										
										
										
											2011-03-02 00:13:05 +01:00
										 |  |  | 		return PTR_ERR(ud->eh); | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-05-06 03:47:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(usbip_start_eh); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void usbip_stop_eh(struct usbip_device *ud) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-02 00:13:05 +01:00
										 |  |  | 	if (ud->eh == current) | 
					
						
							| 
									
										
										
										
											2010-04-24 02:55:24 +02:00
										 |  |  | 		return; /* do not wait for myself */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 00:13:05 +01:00
										 |  |  | 	kthread_stop(ud->eh); | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | 	usbip_dbg_eh("usbip_eh has finished\n"); | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(usbip_stop_eh); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void usbip_event_add(struct usbip_device *ud, unsigned long event) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-01-22 13:31:30 +08:00
										 |  |  | 	unsigned long flags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock_irqsave(&ud->lock, flags); | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | 	ud->event |= event; | 
					
						
							|  |  |  | 	wake_up(&ud->eh_waitq); | 
					
						
							| 
									
										
										
										
											2013-01-22 13:31:30 +08:00
										 |  |  | 	spin_unlock_irqrestore(&ud->lock, flags); | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(usbip_event_add); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | int usbip_event_happened(struct usbip_device *ud) | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | 	int happened = 0; | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock(&ud->lock); | 
					
						
							|  |  |  | 	if (ud->event != 0) | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | 		happened = 1; | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | 	spin_unlock(&ud->lock); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | 	return happened; | 
					
						
							| 
									
										
										
										
											2008-07-09 14:56:51 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-07-21 00:46:13 -06:00
										 |  |  | EXPORT_SYMBOL_GPL(usbip_event_happened); |