Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003-2008 Takahiro Hirofuchi |
| 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 20 | #include <linux/kthread.h> |
Paul Gortmaker | 8e336a7 | 2011-07-10 13:09:12 -0400 | [diff] [blame] | 21 | #include <linux/export.h> |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 22 | |
matt mooney | 0f79847 | 2011-05-06 03:47:47 -0700 | [diff] [blame] | 23 | #include "usbip_common.h" |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 24 | |
| 25 | static int event_handler(struct usbip_device *ud) |
| 26 | { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 27 | usbip_dbg_eh("enter\n"); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Events are handled by only this thread. |
| 31 | */ |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 32 | while (usbip_event_happened(ud)) { |
| 33 | usbip_dbg_eh("pending event %lx\n", ud->event); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * NOTE: shutdown must come first. |
| 37 | * Shutdown the device. |
| 38 | */ |
| 39 | if (ud->event & USBIP_EH_SHUTDOWN) { |
| 40 | ud->eh_ops.shutdown(ud); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 41 | ud->event &= ~USBIP_EH_SHUTDOWN; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 42 | } |
| 43 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 44 | /* Reset the device. */ |
| 45 | if (ud->event & USBIP_EH_RESET) { |
| 46 | ud->eh_ops.reset(ud); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 47 | ud->event &= ~USBIP_EH_RESET; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* Mark the device as unusable. */ |
| 51 | if (ud->event & USBIP_EH_UNUSABLE) { |
| 52 | ud->eh_ops.unusable(ud); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 53 | ud->event &= ~USBIP_EH_UNUSABLE; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 54 | } |
| 55 | |
Max Vozeler | 584c5b7 | 2010-09-21 17:43:30 +0200 | [diff] [blame] | 56 | /* Stop the error handler. */ |
| 57 | if (ud->event & USBIP_EH_BYE) |
| 58 | return -1; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 64 | static int event_handler_loop(void *data) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 65 | { |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 66 | struct usbip_device *ud = data; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 67 | |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 68 | while (!kthread_should_stop()) { |
| 69 | wait_event_interruptible(ud->eh_waitq, |
matt mooney | 0f79847 | 2011-05-06 03:47:47 -0700 | [diff] [blame] | 70 | usbip_event_happened(ud) || |
| 71 | kthread_should_stop()); |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 72 | usbip_dbg_eh("wakeup\n"); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 73 | |
| 74 | if (event_handler(ud) < 0) |
| 75 | break; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 76 | } |
matt mooney | 0f79847 | 2011-05-06 03:47:47 -0700 | [diff] [blame] | 77 | |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 78 | return 0; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 81 | int usbip_start_eh(struct usbip_device *ud) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 82 | { |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 83 | init_waitqueue_head(&ud->eh_waitq); |
| 84 | ud->event = 0; |
| 85 | |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 86 | ud->eh = kthread_run(event_handler_loop, ud, "usbip_eh"); |
| 87 | if (IS_ERR(ud->eh)) { |
Lisa Nguyen | ff45137 | 2013-05-05 16:28:37 -0700 | [diff] [blame] | 88 | pr_warn("Unable to start control thread\n"); |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 89 | return PTR_ERR(ud->eh); |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 90 | } |
matt mooney | 0f79847 | 2011-05-06 03:47:47 -0700 | [diff] [blame] | 91 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 92 | return 0; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 93 | } |
| 94 | EXPORT_SYMBOL_GPL(usbip_start_eh); |
| 95 | |
| 96 | void usbip_stop_eh(struct usbip_device *ud) |
| 97 | { |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 98 | if (ud->eh == current) |
Eric Lescouet | d01f42a | 2010-04-24 02:55:24 +0200 | [diff] [blame] | 99 | return; /* do not wait for myself */ |
| 100 | |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 101 | kthread_stop(ud->eh); |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 102 | usbip_dbg_eh("usbip_eh has finished\n"); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 103 | } |
| 104 | EXPORT_SYMBOL_GPL(usbip_stop_eh); |
| 105 | |
| 106 | void usbip_event_add(struct usbip_device *ud, unsigned long event) |
| 107 | { |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 108 | unsigned long flags; |
| 109 | |
| 110 | spin_lock_irqsave(&ud->lock, flags); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 111 | ud->event |= event; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 112 | wake_up(&ud->eh_waitq); |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 113 | spin_unlock_irqrestore(&ud->lock, flags); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 114 | } |
| 115 | EXPORT_SYMBOL_GPL(usbip_event_add); |
| 116 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 117 | int usbip_event_happened(struct usbip_device *ud) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 118 | { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 119 | int happened = 0; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 120 | |
| 121 | spin_lock(&ud->lock); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 122 | if (ud->event != 0) |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 123 | happened = 1; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 124 | spin_unlock(&ud->lock); |
| 125 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 126 | return happened; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 127 | } |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 128 | EXPORT_SYMBOL_GPL(usbip_event_happened); |