ia64/xen-unstable
changeset 14222:bc265a79dd32
tpm: Code style cleanups.
Signed-off-by: Keir Fraser <keir@xensource.com>
Signed-off-by: Keir Fraser <keir@xensource.com>
author | kfraser@localhost.localdomain |
---|---|
date | Fri Mar 02 16:15:28 2007 +0000 (2007-03-02) |
parents | cc18ea7309b3 |
children | 36b923615369 |
files | linux-2.6-xen-sparse/drivers/char/tpm/tpm_xen.c |
line diff
1.1 --- a/linux-2.6-xen-sparse/drivers/char/tpm/tpm_xen.c Fri Mar 02 16:03:21 2007 +0000 1.2 +++ b/linux-2.6-xen-sparse/drivers/char/tpm/tpm_xen.c Fri Mar 02 16:15:28 2007 +0000 1.3 @@ -113,14 +113,13 @@ void __exit tpmif_exit(void); 1.4 1.5 1.6 static inline int 1.7 -tx_buffer_copy(struct tx_buffer *txb, const u8 * src, int len, 1.8 +tx_buffer_copy(struct tx_buffer *txb, const u8 *src, int len, 1.9 int isuserbuffer) 1.10 { 1.11 int copied = len; 1.12 1.13 - if (len > txb->size) { 1.14 + if (len > txb->size) 1.15 copied = txb->size; 1.16 - } 1.17 if (isuserbuffer) { 1.18 if (copy_from_user(txb->data, src, copied)) 1.19 return -EFAULT; 1.20 @@ -133,18 +132,20 @@ tx_buffer_copy(struct tx_buffer *txb, co 1.21 1.22 static inline struct tx_buffer *tx_buffer_alloc(void) 1.23 { 1.24 - struct tx_buffer *txb = kzalloc(sizeof (struct tx_buffer), 1.25 - GFP_KERNEL); 1.26 + struct tx_buffer *txb; 1.27 + 1.28 + txb = kzalloc(sizeof(struct tx_buffer), GFP_KERNEL); 1.29 + if (!txb) 1.30 + return NULL; 1.31 1.32 - if (txb) { 1.33 - txb->len = 0; 1.34 - txb->size = PAGE_SIZE; 1.35 - txb->data = (unsigned char *)__get_free_page(GFP_KERNEL); 1.36 - if (txb->data == NULL) { 1.37 - kfree(txb); 1.38 - txb = NULL; 1.39 - } 1.40 + txb->len = 0; 1.41 + txb->size = PAGE_SIZE; 1.42 + txb->data = (unsigned char *)__get_free_page(GFP_KERNEL); 1.43 + if (txb->data == NULL) { 1.44 + kfree(txb); 1.45 + txb = NULL; 1.46 } 1.47 + 1.48 return txb; 1.49 } 1.50 1.51 @@ -160,37 +161,41 @@ static inline void tx_buffer_free(struct 1.52 /************************************************************** 1.53 Utility function for the tpm_private structure 1.54 **************************************************************/ 1.55 -static inline void tpm_private_init(struct tpm_private *tp) 1.56 +static void tpm_private_init(struct tpm_private *tp) 1.57 { 1.58 spin_lock_init(&tp->tx_lock); 1.59 init_waitqueue_head(&tp->wait_q); 1.60 atomic_set(&tp->refcnt, 1); 1.61 } 1.62 1.63 -static inline void tpm_private_put(void) 1.64 +static void tpm_private_put(void) 1.65 { 1.66 - if ( atomic_dec_and_test(&my_priv->refcnt)) { 1.67 - tpmif_free_tx_buffers(my_priv); 1.68 - kfree(my_priv); 1.69 - my_priv = NULL; 1.70 - } 1.71 + if (!atomic_dec_and_test(&my_priv->refcnt)) 1.72 + return; 1.73 + 1.74 + tpmif_free_tx_buffers(my_priv); 1.75 + kfree(my_priv); 1.76 + my_priv = NULL; 1.77 } 1.78 1.79 static struct tpm_private *tpm_private_get(void) 1.80 { 1.81 int err; 1.82 - if (!my_priv) { 1.83 - my_priv = kzalloc(sizeof(struct tpm_private), GFP_KERNEL); 1.84 - if (my_priv) { 1.85 - tpm_private_init(my_priv); 1.86 - err = tpmif_allocate_tx_buffers(my_priv); 1.87 - if (err < 0) { 1.88 - tpm_private_put(); 1.89 - } 1.90 - } 1.91 - } else { 1.92 + 1.93 + if (my_priv) { 1.94 atomic_inc(&my_priv->refcnt); 1.95 + return my_priv; 1.96 } 1.97 + 1.98 + my_priv = kzalloc(sizeof(struct tpm_private), GFP_KERNEL); 1.99 + if (!my_priv) 1.100 + return NULL; 1.101 + 1.102 + tpm_private_init(my_priv); 1.103 + err = tpmif_allocate_tx_buffers(my_priv); 1.104 + if (err < 0) 1.105 + tpm_private_put(); 1.106 + 1.107 return my_priv; 1.108 } 1.109 1.110 @@ -379,10 +384,8 @@ static int tpmfront_probe(struct xenbus_ 1.111 return -ENOMEM; 1.112 1.113 tp->chip = init_vtpm(&dev->dev, &tvd, tp); 1.114 - 1.115 - if (IS_ERR(tp->chip)) { 1.116 + if (IS_ERR(tp->chip)) 1.117 return PTR_ERR(tp->chip); 1.118 - } 1.119 1.120 err = xenbus_scanf(XBT_NIL, dev->nodename, 1.121 "handle", "%i", &handle); 1.122 @@ -401,6 +404,7 @@ static int tpmfront_probe(struct xenbus_ 1.123 tpm_private_put(); 1.124 return err; 1.125 } 1.126 + 1.127 return 0; 1.128 } 1.129 1.130 @@ -417,7 +421,8 @@ static int tpmfront_suspend(struct xenbu 1.131 { 1.132 struct tpm_private *tp = tpm_private_from_dev(&dev->dev); 1.133 u32 ctr; 1.134 - /* lock, so no app can send */ 1.135 + 1.136 + /* Take the lock, preventing any application from sending. */ 1.137 mutex_lock(&suspend_lock); 1.138 tp->is_suspended = 1; 1.139 1.140 @@ -425,19 +430,17 @@ static int tpmfront_suspend(struct xenbu 1.141 if ((ctr % 10) == 0) 1.142 printk("TPM-FE [INFO]: Waiting for outstanding " 1.143 "request.\n"); 1.144 - /* 1.145 - * Wait for a request to be responded to. 1.146 - */ 1.147 + /* Wait for a request to be responded to. */ 1.148 interruptible_sleep_on_timeout(&tp->wait_q, 100); 1.149 } 1.150 1.151 return 0; 1.152 } 1.153 1.154 -static int __tpmfront_suspend_cancel(struct tpm_private *tp) 1.155 +static int tpmfront_resume(struct tpm_private *tp) 1.156 { 1.157 tp->is_suspended = 0; 1.158 - /* unlock, so apps can send again */ 1.159 + /* Allow applications to send again. */ 1.160 mutex_unlock(&suspend_lock); 1.161 return 0; 1.162 } 1.163 @@ -445,7 +448,7 @@ static int __tpmfront_suspend_cancel(str 1.164 static int tpmfront_suspend_cancel(struct xenbus_device *dev) 1.165 { 1.166 struct tpm_private *tp = tpm_private_from_dev(&dev->dev); 1.167 - return __tpmfront_suspend_cancel(tp); 1.168 + return tpmfront_resume(tp); 1.169 } 1.170 1.171 static int tpmfront_resume(struct xenbus_device *dev) 1.172 @@ -520,9 +523,8 @@ static void tpmif_free_tx_buffers(struct 1.173 { 1.174 unsigned int i; 1.175 1.176 - for (i = 0; i < TPMIF_TX_RING_SIZE; i++) { 1.177 + for (i = 0; i < TPMIF_TX_RING_SIZE; i++) 1.178 tx_buffer_free(tp->tx_buffers[i]); 1.179 - } 1.180 } 1.181 1.182 static void tpmif_rx_action(unsigned long priv) 1.183 @@ -542,9 +544,8 @@ static void tpmif_rx_action(unsigned lon 1.184 received = tx->size; 1.185 1.186 buffer = kmalloc(received, GFP_ATOMIC); 1.187 - if (NULL == buffer) { 1.188 + if (!buffer) 1.189 goto exit; 1.190 - } 1.191 1.192 for (i = 0; i < TPMIF_TX_RING_SIZE && offset < received; i++) { 1.193 struct tx_buffer *txb = tp->tx_buffers[i]; 1.194 @@ -553,9 +554,8 @@ static void tpmif_rx_action(unsigned lon 1.195 1.196 tx = &tp->tx->ring[i].req; 1.197 tocopy = tx->size; 1.198 - if (tocopy > PAGE_SIZE) { 1.199 + if (tocopy > PAGE_SIZE) 1.200 tocopy = PAGE_SIZE; 1.201 - } 1.202 1.203 memcpy(&buffer[offset], txb->data, tocopy); 1.204 1.205 @@ -613,12 +613,13 @@ static int tpm_xmit(struct tpm_private * 1.206 struct tx_buffer *txb = tp->tx_buffers[i]; 1.207 int copied; 1.208 1.209 - if (NULL == txb) { 1.210 + if (!txb) { 1.211 DPRINTK("txb (i=%d) is NULL. buffers initilized?\n" 1.212 "Not transmitting anything!\n", i); 1.213 spin_unlock_irq(&tp->tx_lock); 1.214 return -EFAULT; 1.215 } 1.216 + 1.217 copied = tx_buffer_copy(txb, &buf[offset], count, 1.218 isuserbuffer); 1.219 if (copied < 0) { 1.220 @@ -630,25 +631,26 @@ static int tpm_xmit(struct tpm_private * 1.221 offset += copied; 1.222 1.223 tx = &tp->tx->ring[i].req; 1.224 - 1.225 tx->addr = virt_to_machine(txb->data); 1.226 tx->size = txb->len; 1.227 1.228 - DPRINTK("First 4 characters sent by TPM-FE are 0x%02x 0x%02x 0x%02x 0x%02x\n", 1.229 + DPRINTK("First 4 characters sent by TPM-FE are " 1.230 + "0x%02x 0x%02x 0x%02x 0x%02x\n", 1.231 txb->data[0],txb->data[1],txb->data[2],txb->data[3]); 1.232 1.233 - /* get the granttable reference for this page */ 1.234 + /* Get the granttable reference for this page. */ 1.235 tx->ref = gnttab_claim_grant_reference(&gref_head); 1.236 - 1.237 - if (-ENOSPC == tx->ref) { 1.238 + if (tx->ref == -ENOSPC) { 1.239 spin_unlock_irq(&tp->tx_lock); 1.240 - DPRINTK(" Grant table claim reference failed in func:%s line:%d file:%s\n", __FUNCTION__, __LINE__, __FILE__); 1.241 + DPRINTK("Grant table claim reference failed in " 1.242 + "func:%s line:%d file:%s\n", 1.243 + __FUNCTION__, __LINE__, __FILE__); 1.244 return -ENOSPC; 1.245 } 1.246 - gnttab_grant_foreign_access_ref( tx->ref, 1.247 - tp->backend_id, 1.248 - virt_to_mfn(txb->data), 1.249 - 0 /*RW*/); 1.250 + gnttab_grant_foreign_access_ref(tx->ref, 1.251 + tp->backend_id, 1.252 + virt_to_mfn(txb->data), 1.253 + 0 /*RW*/); 1.254 wmb(); 1.255 } 1.256 1.257 @@ -666,15 +668,10 @@ static int tpm_xmit(struct tpm_private * 1.258 1.259 static void tpmif_notify_upperlayer(struct tpm_private *tp) 1.260 { 1.261 - /* 1.262 - * Notify upper layer about the state of the connection 1.263 - * to the BE. 1.264 - */ 1.265 - if (tp->is_connected) { 1.266 - vtpm_vd_status(tp->chip, TPM_VD_STATUS_CONNECTED); 1.267 - } else { 1.268 - vtpm_vd_status(tp->chip, TPM_VD_STATUS_DISCONNECTED); 1.269 - } 1.270 + /* Notify upper layer about the state of the connection to the BE. */ 1.271 + vtpm_vd_status(tp->chip, (tp->is_connected 1.272 + ? TPM_VD_STATUS_CONNECTED 1.273 + : TPM_VD_STATUS_DISCONNECTED)); 1.274 } 1.275 1.276 1.277 @@ -685,18 +682,16 @@ static void tpmif_set_connected_state(st 1.278 * should disconnect - assumption is that we will resume 1.279 * The mutex keeps apps from sending. 1.280 */ 1.281 - if (is_connected == 0 && tp->is_suspended == 1) { 1.282 + if (is_connected == 0 && tp->is_suspended == 1) 1.283 return; 1.284 - } 1.285 1.286 /* 1.287 * Unlock the mutex if we are connected again 1.288 * after being suspended - now resuming. 1.289 * This also removes the suspend state. 1.290 */ 1.291 - if (is_connected == 1 && tp->is_suspended == 1) { 1.292 - __tpmfront_suspend_cancel(tp); 1.293 - } 1.294 + if (is_connected == 1 && tp->is_suspended == 1) 1.295 + tpmfront_resume(tp); 1.296 1.297 if (is_connected != tp->is_connected) { 1.298 tp->is_connected = is_connected; 1.299 @@ -714,33 +709,24 @@ static void tpmif_set_connected_state(st 1.300 1.301 static int __init tpmif_init(void) 1.302 { 1.303 - long rc = 0; 1.304 struct tpm_private *tp; 1.305 1.306 if (is_initial_xendomain()) 1.307 return -EPERM; 1.308 1.309 tp = tpm_private_get(); 1.310 - if (!tp) { 1.311 - rc = -ENOMEM; 1.312 - goto failexit; 1.313 - } 1.314 + if (!tp) 1.315 + return -ENOMEM; 1.316 1.317 IPRINTK("Initialising the vTPM driver.\n"); 1.318 - if ( gnttab_alloc_grant_references ( TPMIF_TX_RING_SIZE, 1.319 - &gref_head ) < 0) { 1.320 - rc = -EFAULT; 1.321 - goto gnttab_alloc_failed; 1.322 + if (gnttab_alloc_grant_references(TPMIF_TX_RING_SIZE, 1.323 + &gref_head) < 0) { 1.324 + tpm_private_put(); 1.325 + return -EFAULT; 1.326 } 1.327 1.328 init_tpm_xenbus(); 1.329 return 0; 1.330 - 1.331 -gnttab_alloc_failed: 1.332 - tpm_private_put(); 1.333 -failexit: 1.334 - 1.335 - return (int)rc; 1.336 } 1.337 1.338