Venkat Chinta | 686c9e5 | 2018-01-20 14:33:25 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved. |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "CAM-REQ-MGR_UTIL %s:%d " fmt, __func__, __LINE__ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of_platform.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/random.h> |
| 21 | #include <media/cam_req_mgr.h> |
| 22 | #include "cam_req_mgr_util.h" |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 23 | #include "cam_debug_util.h" |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 24 | |
| 25 | static struct cam_req_mgr_util_hdl_tbl *hdl_tbl; |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 26 | static DEFINE_SPINLOCK(hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 27 | |
| 28 | int cam_req_mgr_util_init(void) |
| 29 | { |
| 30 | int rc = 0; |
| 31 | int bitmap_size; |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 32 | static struct cam_req_mgr_util_hdl_tbl *hdl_tbl_local; |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 33 | |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 34 | if (hdl_tbl) { |
| 35 | rc = -EINVAL; |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 36 | CAM_ERR(CAM_CRM, "Hdl_tbl is already present"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 37 | goto hdl_tbl_check_failed; |
| 38 | } |
| 39 | |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 40 | hdl_tbl_local = kzalloc(sizeof(*hdl_tbl), GFP_KERNEL); |
| 41 | if (!hdl_tbl_local) { |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 42 | rc = -ENOMEM; |
| 43 | goto hdl_tbl_alloc_failed; |
| 44 | } |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 45 | spin_lock_bh(&hdl_tbl_lock); |
| 46 | if (hdl_tbl) { |
| 47 | spin_unlock_bh(&hdl_tbl_lock); |
| 48 | rc = -EEXIST; |
| 49 | kfree(hdl_tbl_local); |
| 50 | goto hdl_tbl_check_failed; |
| 51 | } |
| 52 | hdl_tbl = hdl_tbl_local; |
| 53 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 54 | |
| 55 | bitmap_size = BITS_TO_LONGS(CAM_REQ_MGR_MAX_HANDLES) * sizeof(long); |
Lakshmi Narayana Kalavala | aae1e6b | 2017-06-14 09:58:40 -0700 | [diff] [blame] | 56 | hdl_tbl->bitmap = kzalloc(bitmap_size, GFP_KERNEL); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 57 | if (!hdl_tbl->bitmap) { |
| 58 | rc = -ENOMEM; |
| 59 | goto bitmap_alloc_fail; |
| 60 | } |
| 61 | hdl_tbl->bits = bitmap_size * BITS_PER_BYTE; |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 62 | |
| 63 | return rc; |
| 64 | |
| 65 | bitmap_alloc_fail: |
| 66 | kfree(hdl_tbl); |
| 67 | hdl_tbl = NULL; |
| 68 | hdl_tbl_alloc_failed: |
| 69 | hdl_tbl_check_failed: |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 70 | return rc; |
| 71 | } |
| 72 | |
| 73 | int cam_req_mgr_util_deinit(void) |
| 74 | { |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 75 | spin_lock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 76 | if (!hdl_tbl) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 77 | CAM_ERR(CAM_CRM, "Hdl tbl is NULL"); |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 78 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 79 | return -EINVAL; |
| 80 | } |
| 81 | |
| 82 | kfree(hdl_tbl->bitmap); |
| 83 | hdl_tbl->bitmap = NULL; |
| 84 | kfree(hdl_tbl); |
| 85 | hdl_tbl = NULL; |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 86 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int cam_req_mgr_util_free_hdls(void) |
| 92 | { |
| 93 | int i = 0; |
| 94 | |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 95 | spin_lock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 96 | if (!hdl_tbl) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 97 | CAM_ERR(CAM_CRM, "Hdl tbl is NULL"); |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 98 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | for (i = 0; i < CAM_REQ_MGR_MAX_HANDLES; i++) { |
| 103 | if (hdl_tbl->hdl[i].state == HDL_ACTIVE) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 104 | CAM_ERR(CAM_CRM, "Dev handle = %x session_handle = %x", |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 105 | hdl_tbl->hdl[i].hdl_value, |
| 106 | hdl_tbl->hdl[i].session_hdl); |
| 107 | hdl_tbl->hdl[i].state = HDL_FREE; |
| 108 | clear_bit(i, hdl_tbl->bitmap); |
| 109 | } |
| 110 | } |
| 111 | bitmap_zero(hdl_tbl->bitmap, CAM_REQ_MGR_MAX_HANDLES); |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 112 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int32_t cam_get_free_handle_index(void) |
| 118 | { |
| 119 | int idx; |
| 120 | |
| 121 | idx = find_first_zero_bit(hdl_tbl->bitmap, hdl_tbl->bits); |
| 122 | |
| 123 | if (idx >= CAM_REQ_MGR_MAX_HANDLES || idx < 0) |
| 124 | return -ENOSR; |
| 125 | |
| 126 | set_bit(idx, hdl_tbl->bitmap); |
| 127 | |
| 128 | return idx; |
| 129 | } |
| 130 | |
| 131 | int32_t cam_create_session_hdl(void *priv) |
| 132 | { |
| 133 | int idx; |
| 134 | int rand = 0; |
| 135 | int32_t handle = 0; |
| 136 | |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 137 | spin_lock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 138 | if (!hdl_tbl) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 139 | CAM_ERR(CAM_CRM, "Hdl tbl is NULL"); |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 140 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | idx = cam_get_free_handle_index(); |
| 145 | if (idx < 0) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 146 | CAM_ERR(CAM_CRM, "Unable to create session handle"); |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 147 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 148 | return idx; |
| 149 | } |
| 150 | |
| 151 | get_random_bytes(&rand, CAM_REQ_MGR_RND1_BYTES); |
| 152 | handle = GET_DEV_HANDLE(rand, HDL_TYPE_SESSION, idx); |
| 153 | hdl_tbl->hdl[idx].session_hdl = handle; |
| 154 | hdl_tbl->hdl[idx].hdl_value = handle; |
| 155 | hdl_tbl->hdl[idx].type = HDL_TYPE_SESSION; |
| 156 | hdl_tbl->hdl[idx].state = HDL_ACTIVE; |
| 157 | hdl_tbl->hdl[idx].priv = priv; |
| 158 | hdl_tbl->hdl[idx].ops = NULL; |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 159 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 160 | |
| 161 | return handle; |
| 162 | } |
| 163 | |
| 164 | int32_t cam_create_device_hdl(struct cam_create_dev_hdl *hdl_data) |
| 165 | { |
| 166 | int idx; |
| 167 | int rand = 0; |
| 168 | int32_t handle; |
| 169 | |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 170 | spin_lock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 171 | if (!hdl_tbl) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 172 | CAM_ERR(CAM_CRM, "Hdl tbl is NULL"); |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 173 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | idx = cam_get_free_handle_index(); |
| 178 | if (idx < 0) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 179 | CAM_ERR(CAM_CRM, "Unable to create device handle"); |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 180 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 181 | return idx; |
| 182 | } |
| 183 | |
| 184 | get_random_bytes(&rand, CAM_REQ_MGR_RND1_BYTES); |
| 185 | handle = GET_DEV_HANDLE(rand, HDL_TYPE_DEV, idx); |
| 186 | hdl_tbl->hdl[idx].session_hdl = hdl_data->session_hdl; |
| 187 | hdl_tbl->hdl[idx].hdl_value = handle; |
| 188 | hdl_tbl->hdl[idx].type = HDL_TYPE_DEV; |
| 189 | hdl_tbl->hdl[idx].state = HDL_ACTIVE; |
| 190 | hdl_tbl->hdl[idx].priv = hdl_data->priv; |
| 191 | hdl_tbl->hdl[idx].ops = hdl_data->ops; |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 192 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 193 | |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 194 | pr_debug("%s: handle = %x", __func__, handle); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 195 | return handle; |
| 196 | } |
| 197 | |
| 198 | void *cam_get_device_priv(int32_t dev_hdl) |
| 199 | { |
| 200 | int idx; |
| 201 | int type; |
| 202 | void *priv; |
| 203 | |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 204 | spin_lock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 205 | if (!hdl_tbl) { |
Harsh Shah | cb0072c | 2017-09-26 19:22:10 -0700 | [diff] [blame] | 206 | CAM_ERR_RATE_LIMIT(CAM_CRM, "Hdl tbl is NULL"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 207 | goto device_priv_fail; |
| 208 | } |
| 209 | |
| 210 | idx = CAM_REQ_MGR_GET_HDL_IDX(dev_hdl); |
| 211 | if (idx >= CAM_REQ_MGR_MAX_HANDLES) { |
Harsh Shah | cb0072c | 2017-09-26 19:22:10 -0700 | [diff] [blame] | 212 | CAM_ERR_RATE_LIMIT(CAM_CRM, "Invalid idx"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 213 | goto device_priv_fail; |
| 214 | } |
| 215 | |
| 216 | if (hdl_tbl->hdl[idx].state != HDL_ACTIVE) { |
Harsh Shah | cb0072c | 2017-09-26 19:22:10 -0700 | [diff] [blame] | 217 | CAM_ERR_RATE_LIMIT(CAM_CRM, "Invalid state"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 218 | goto device_priv_fail; |
| 219 | } |
| 220 | |
| 221 | type = CAM_REQ_MGR_GET_HDL_TYPE(dev_hdl); |
| 222 | if (HDL_TYPE_DEV != type && HDL_TYPE_SESSION != type) { |
Harsh Shah | cb0072c | 2017-09-26 19:22:10 -0700 | [diff] [blame] | 223 | CAM_ERR_RATE_LIMIT(CAM_CRM, "Invalid type"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 224 | goto device_priv_fail; |
| 225 | } |
| 226 | |
| 227 | if (hdl_tbl->hdl[idx].hdl_value != dev_hdl) { |
Harsh Shah | cb0072c | 2017-09-26 19:22:10 -0700 | [diff] [blame] | 228 | CAM_ERR_RATE_LIMIT(CAM_CRM, "Invalid hdl"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 229 | goto device_priv_fail; |
| 230 | } |
| 231 | |
| 232 | priv = hdl_tbl->hdl[idx].priv; |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 233 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 234 | |
| 235 | return priv; |
| 236 | |
| 237 | device_priv_fail: |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 238 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | void *cam_get_device_ops(int32_t dev_hdl) |
| 243 | { |
| 244 | int idx; |
| 245 | int type; |
| 246 | void *ops; |
| 247 | |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 248 | spin_lock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 249 | if (!hdl_tbl) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 250 | CAM_ERR(CAM_CRM, "Hdl tbl is NULL"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 251 | goto device_ops_fail; |
| 252 | } |
| 253 | |
| 254 | idx = CAM_REQ_MGR_GET_HDL_IDX(dev_hdl); |
| 255 | if (idx >= CAM_REQ_MGR_MAX_HANDLES) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 256 | CAM_ERR(CAM_CRM, "Invalid idx"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 257 | goto device_ops_fail; |
| 258 | } |
| 259 | |
| 260 | if (hdl_tbl->hdl[idx].state != HDL_ACTIVE) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 261 | CAM_ERR(CAM_CRM, "Invalid state"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 262 | goto device_ops_fail; |
| 263 | } |
| 264 | |
| 265 | type = CAM_REQ_MGR_GET_HDL_TYPE(dev_hdl); |
| 266 | if (HDL_TYPE_DEV != type && HDL_TYPE_SESSION != type) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 267 | CAM_ERR(CAM_CRM, "Invalid type"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 268 | goto device_ops_fail; |
| 269 | } |
| 270 | |
| 271 | if (hdl_tbl->hdl[idx].hdl_value != dev_hdl) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 272 | CAM_ERR(CAM_CRM, "Invalid hdl"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 273 | goto device_ops_fail; |
| 274 | } |
| 275 | |
| 276 | ops = hdl_tbl->hdl[idx].ops; |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 277 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 278 | |
| 279 | return ops; |
| 280 | |
| 281 | device_ops_fail: |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 282 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | static int cam_destroy_hdl(int32_t dev_hdl, int dev_hdl_type) |
| 287 | { |
| 288 | int idx; |
| 289 | int type; |
| 290 | |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 291 | spin_lock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 292 | if (!hdl_tbl) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 293 | CAM_ERR(CAM_CRM, "Hdl tbl is NULL"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 294 | goto destroy_hdl_fail; |
| 295 | } |
| 296 | |
| 297 | idx = CAM_REQ_MGR_GET_HDL_IDX(dev_hdl); |
| 298 | if (idx >= CAM_REQ_MGR_MAX_HANDLES) { |
Venkat Chinta | 686c9e5 | 2018-01-20 14:33:25 -0800 | [diff] [blame] | 299 | CAM_ERR(CAM_CRM, "Invalid idx %d", idx); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 300 | goto destroy_hdl_fail; |
| 301 | } |
| 302 | |
| 303 | if (hdl_tbl->hdl[idx].state != HDL_ACTIVE) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 304 | CAM_ERR(CAM_CRM, "Invalid state"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 305 | goto destroy_hdl_fail; |
| 306 | } |
| 307 | |
| 308 | type = CAM_REQ_MGR_GET_HDL_TYPE(dev_hdl); |
| 309 | if (type != dev_hdl_type) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 310 | CAM_ERR(CAM_CRM, "Invalid type %d, %d", type, dev_hdl_type); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 311 | goto destroy_hdl_fail; |
| 312 | } |
| 313 | |
| 314 | if (hdl_tbl->hdl[idx].hdl_value != dev_hdl) { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 315 | CAM_ERR(CAM_CRM, "Invalid hdl"); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 316 | goto destroy_hdl_fail; |
| 317 | } |
| 318 | |
| 319 | hdl_tbl->hdl[idx].state = HDL_FREE; |
Harsh Shah | a606d18 | 2017-11-10 00:15:58 -0800 | [diff] [blame] | 320 | hdl_tbl->hdl[idx].ops = NULL; |
| 321 | hdl_tbl->hdl[idx].priv = NULL; |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 322 | clear_bit(idx, hdl_tbl->bitmap); |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 323 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 324 | |
| 325 | return 0; |
| 326 | |
| 327 | destroy_hdl_fail: |
Jing Zhou | efe94f5 | 2017-04-26 12:04:14 -0700 | [diff] [blame] | 328 | spin_unlock_bh(&hdl_tbl_lock); |
Sagar Gore | 8d91a62 | 2017-02-23 14:57:18 -0800 | [diff] [blame] | 329 | return -EINVAL; |
| 330 | } |
| 331 | |
| 332 | int cam_destroy_device_hdl(int32_t dev_hdl) |
| 333 | { |
| 334 | return cam_destroy_hdl(dev_hdl, HDL_TYPE_DEV); |
| 335 | } |
| 336 | |
| 337 | int cam_destroy_session_hdl(int32_t dev_hdl) |
| 338 | { |
| 339 | return cam_destroy_hdl(dev_hdl, HDL_TYPE_SESSION); |
| 340 | } |