TPM: STMicroelectronics st33 driver SPI
Subsystems (2)
PATCH 1/1 TPM: STMicroelectronics st33 driver SPI
From: Mathias Leblanc <[email protected]>
* STMicroelectronics version 1.2.0, Copyright (C) 2010
* STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
* This is free software, and you are welcome to redistribute it
* under certain conditions.
This is the driver for TPM chip from ST Microelectronics.
If you have a TPM security chip from STMicroelectronics working with an SPI, in menuconfig or .config choose the tpm driver on device --> tpm and activate the protocol of your choice before compiling the kernel.
The driver will be accessible from within Linux.
Tested on linux x86/x64 and beagleboard REV B & XM REV C
---
Remove unused variables
Remove tpm_st33_spi_exit function that cause compilation error Initalise data and temp variable
Change some variable type
Correction of warnings and errors from smatch tools
drivers/char/tpm/Kconfig | 10 + drivers/char/tpm/Makefile | 1 + drivers/char/tpm/tpm_spi_stm_st33.c | 948 +++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm_spi_stm_st33.h | 61 +++ 4 files changed, 1020 insertions(+), 0 deletions(-) create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
drivers/char/tpm/Kconfig +10 -0
@@ -91,4 +91,14 @@ config TCG_ST33_I2C To compile this driver as a module, choose M here; the module will be called tpm_stm_st33_i2c. +config TCG_ST33_SPI + tristate "STMicroelectronics ST33 SPI" + depends on SPI + depends on GPIOLIB + ---help--- + If you have a TPM security chip from STMicroelectronics working with + an SPI bus say Yes and it will be accessible from within Linux. + To compile this driver as a module, choose M here; the module will be + called tpm_spi_stm_st33. + endif # TCG_TPM
drivers/char/tpm/Makefile +1 -0
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o +obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
drivers/char/tpm/tpm_spi_stm_st33.c new file +948 -0
948 lines not on this page · raw message
+MODULE_LICENSE("GPL");
drivers/char/tpm/tpm_spi_stm_st33.h new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Peter Hüwe
maintainerHi Matthias,
Am Mittwoch, 15. Mai 2013, 15:53:19 schrieb Matthias Leblanc:
drivers/char/tpm/tpm_spi_stm_st33.c line 87
Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +7
-0+static inline int spi_read_write (struct spi_device *spi, +struct spi_transfer xfer) { +struct spi_message msg; +spi_message_init (&msg); +spi_message_add_tail (&xfer, &msg); +return spi_sync (spi, &msg); +}
This looks extremely like the predefined spi_sync_transfer in include/linux/spi.h
/**
* spi_sync_transfer - synchronous SPI data transfer
* @spi: device with which data will be exchanged
* @xfers: An array of spi_transfers
* @num_xfers: Number of items in the xfer array
* Context: can sleep
*
* Does a synchronous SPI data transfer of the given spi_transfer array. *
* For more specific semantics see spi_sync().
*
* It returns zero on success, else a negative error code. */
static inline int
spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
unsigned int num_xfers)
{
struct spi_message msg;
spi_message_init_with_transfers(&msg, xfers, num_xfers);
return spi_sync(spi, &msg);
}where spi_message_init_with_transfers is defined as
static inline void spi_message_init_with_transfers(struct spi_message *m, struct spi_transfer *xfers, unsigned int num_xfers)
{
unsigned int i;
spi_message_init(m);
for (i = 0; i < num_xfers; ++i)
spi_message_add_tail(&xfers[i], m);
}If you combine these functions you end up (more or less) with your implementation
So please use spi_sync_transfer
OR as an alternative use spi_write / spi_read as an alternative as mentioned in a previous email
And here is a rather subjective remark:
drivers/char/tpm/tpm_spi_stm_st33.c line 201
Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4
-0+for (; nbr_dummy_bytes < total_length && + ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] ==0 ; + nbr_dummy_bytes++) + ;
Not sure if it would be easier to read using a while and putting the increment into the loop body
drivers/char/tpm/tpm_spi_stm_st33.c line 146
Quoted 3 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +3
-0+while (nbr_dummy_bytes < total_length && + ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] ==0 ) + nbr_dummy_bytes++;
I usually don't like empty loop bodies, as they tend to be overlooked.
drivers/char/tpm/tpm_spi_stm_st33.c
Quoted 6 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +6
-0+module_spi_driver (tpm_st33_spi_driver); + +MODULE_AUTHOR ("Christophe Ricard ([email protected])" ); +MODULE_DESCRIPTION ("STM TPM SPI ST33 Driver" ); +MODULE_VERSION ("1.2.0" ); +MODULE_LICENSE ("GPL" );
Something is strange here - if I apply your patch, the module license gets lost - so I get a compile warning:
WARNING: modpost: missing MODULE_LICENSE() in /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.o
Do you edit the patch by hand before sending?
Steps to reproduce:drivers/char/tpm $ git reset --hard tpmdd/tpmdd-04-26-13
HEAD is now at d224213 tpm_tis: missing platform_driver_unregister() on error in init_tis() drivers/char/tpm $ git am /tmp/\[tpmdd-devel\]\ \[PATCH\ 1_1\]\ TPM_STMicroelectronics\ st33\ driver\ SPI.mbox
Applying: TPM: STMicroelectronics st33 driver SPI
drivers/char/tpm $ tail tpm_spi_stm_st33.c },
.probe = tpm_st33_spi_probe,
.remove = tpm_st33_spi_remove,
};module_spi_driver(tpm_st33_spi_driver);
MODULE_AUTHOR("Christophe Ricard ([email protected])"); MODULE_DESCRIPTION("STM TPM SPI ST33 Driver"); MODULE_VERSION("1.2.0");
And the usual sparse warnings ;)drivers/char/tpm $ make M=pwd -C /data/data-old/linux-2.6/ modules C=1 CHECK=sparse
make: Entering directory `/data/data-old/linux-2.6'
CHECK /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:104:35: warning: cast removes address space of expression /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:170:35: warning: cast removes address space of expression /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:242:19: warning: cast removes address space of expression /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:235:15: warning: symbol 'wait_for_serirq_timeout' was not declared. Should it be static? /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:482:19: warning: cast removes address space of expression
Regards,
Peter
Hi Peter,
Thanks for your feedback.
You are right, this function looks like the spi_sync_transfer; So, I will use it in the next version of the driver.
Regarding the while loop, I don't see how can I check the number of dummy byte differently?
For the module error, Yes I have modified the patch file :s, apparently I haven't correctly modified the number of lines
Finaly, These sparse warning are finaly fix ;).
Best regards,
Mathias Leblanc
________________________________________
From: Peter Hüwe [[email protected]]
Sent: 16 May 2013 00:29
To: [email protected]
Cc: Mathias LEBLANC; Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; Jean-Luc BLANC; [email protected]
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
Hi Matthias,
Am Mittwoch, 15. Mai 2013, 15:53:19 schrieb Matthias Leblanc:
drivers/char/tpm/tpm_spi_stm_st33.c line 87
Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +7
-0+static inline int spi_read_write (struct spi_device *spi, +struct spi_transfer xfer) { +struct spi_message msg; +spi_message_init (&msg); +spi_message_add_tail (&xfer, &msg); +return spi_sync (spi, &msg); +}
This looks extremely like the predefined spi_sync_transfer in include/linux/spi.h
/**
* spi_sync_transfer - synchronous SPI data transfer
* @spi: device with which data will be exchanged
* @xfers: An array of spi_transfers
* @num_xfers: Number of items in the xfer array
* Context: can sleep
*
* Does a synchronous SPI data transfer of the given spi_transfer array. *
* For more specific semantics see spi_sync().
*
* It returns zero on success, else a negative error code. */
static inline int
spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
unsigned int num_xfers)
{
struct spi_message msg;
spi_message_init_with_transfers(&msg, xfers, num_xfers);
return spi_sync(spi, &msg);
}where spi_message_init_with_transfers is defined as
static inline void spi_message_init_with_transfers(struct spi_message *m, struct spi_transfer *xfers, unsigned int num_xfers)
{
unsigned int i;
spi_message_init(m);
for (i = 0; i < num_xfers; ++i)
spi_message_add_tail(&xfers[i], m);
}If you combine these functions you end up (more or less) with your implementation
So please use spi_sync_transfer
OR as an alternative use spi_write / spi_read as an alternative as mentioned in a previous email
And here is a rather subjective remark:
drivers/char/tpm/tpm_spi_stm_st33.c line 201
Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4
-0+for (; nbr_dummy_bytes < total_length && + ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] ==0 ; + nbr_dummy_bytes++) + ;
Not sure if it would be easier to read using a while and putting the increment into the loop body
drivers/char/tpm/tpm_spi_stm_st33.c line 146
Quoted 3 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +3
-0+while (nbr_dummy_bytes < total_length && + ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] ==0 ) + nbr_dummy_bytes++;
I usually don't like empty loop bodies, as they tend to be overlooked.
drivers/char/tpm/tpm_spi_stm_st33.c
Quoted 6 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +6
-0+module_spi_driver (tpm_st33_spi_driver); + +MODULE_AUTHOR ("Christophe Ricard ([email protected])" ); +MODULE_DESCRIPTION ("STM TPM SPI ST33 Driver" ); +MODULE_VERSION ("1.2.0" ); +MODULE_LICENSE ("GPL" );
Something is strange here - if I apply your patch, the module license gets lost - so I get a compile warning:
WARNING: modpost: missing MODULE_LICENSE() in /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.o
Do you edit the patch by hand before sending?
Steps to reproduce:drivers/char/tpm $ git reset --hard tpmdd/tpmdd-04-26-13
HEAD is now at d224213 tpm_tis: missing platform_driver_unregister() on error in init_tis() drivers/char/tpm $ git am /tmp/\[tpmdd-devel\]\ \[PATCH\ 1_1\]\ TPM_STMicroelectronics\ st33\ driver\ SPI.mbox
Applying: TPM: STMicroelectronics st33 driver SPI
drivers/char/tpm $ tail tpm_spi_stm_st33.c },
.probe = tpm_st33_spi_probe,
.remove = tpm_st33_spi_remove,
};module_spi_driver(tpm_st33_spi_driver);
MODULE_AUTHOR("Christophe Ricard ([email protected])"); MODULE_DESCRIPTION("STM TPM SPI ST33 Driver"); MODULE_VERSION("1.2.0");
And the usual sparse warnings ;)drivers/char/tpm $ make M=pwd -C /data/data-old/linux-2.6/ modules C=1 CHECK=sparse
make: Entering directory `/data/data-old/linux-2.6'
CHECK /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:104:35: warning: cast removes address space of expression /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:170:35: warning: cast removes address space of expression /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:242:19: warning: cast removes address space of expression /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:235:15: warning: symbol 'wait_for_serirq_timeout' was not declared. Should it be static? /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:482:19: warning: cast removes address space of expression
Regards,
Peter
Peter Hüwe
maintainerHi Matthias:
Quoted 2 lines
Regarding the while loop, I don't see how can I check the number of dummy byte differently?
Checking the number of dummy bytes is fine -
in line #144 you check it with a while loop which looks fine in line #198 you check it with a for loop with an empty body, which in my opinion looks less nice than the while in #144.
So simply replace the second instance in #198 and I'm happy with that part ;)
Maybe also have a look at memchr_inv
/**
* memchr_inv - Find an unmatching character in an area of memory.
* @start: The memory area
* @c: Find a character other than c
* @bytes: The size of the area.
*
* returns the address of the first character other than @c, or %NULL
* if the whole buffer contains just @c.
*/
void *memchr_inv(const void *start, int c, size_t bytes)
so #198 would look something like:nbr_dummy_bytes = memchr_inv (xfer.rx_buf+nbr_dummy_bytes,
0, total_length-nbr_dummy_bytes) - xfer.rx_buf;
But I'm not sure whether that's more readable, maybe give it a try.
I'm also fine with the while loop.
Peter
Hi Peter,
Thanks for this new function.
I had already changed the for loop but i would to be sure that it's the best way and that you expected.
Don't hesitate if you have any other recommendation or any comments for this driver.
Regards,
Mathias Leblanc
________________________________________
From: Peter Hüwe [[email protected]]
Sent: 16 May 2013 21:03
To: [email protected]
Cc: Mathias LEBLANC; Jean-Luc BLANC; [email protected]; Rajiv Andrade; Sirrix AG
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
Hi Matthias:
Quoted 2 lines
Regarding the while loop, I don't see how can I check the number of dummy byte differently?
Checking the number of dummy bytes is fine -
in line #144 you check it with a while loop which looks fine in line #198 you check it with a for loop with an empty body, which in my opinion looks less nice than the while in #144.
So simply replace the second instance in #198 and I'm happy with that part ;)
Maybe also have a look at memchr_inv
/**
* memchr_inv - Find an unmatching character in an area of memory.
* @start: The memory area
* @c: Find a character other than c
* @bytes: The size of the area.
*
* returns the address of the first character other than @c, or %NULL
* if the whole buffer contains just @c.
*/
void *memchr_inv(const void *start, int c, size_t bytes)
so #198 would look something like:nbr_dummy_bytes = memchr_inv (xfer.rx_buf+nbr_dummy_bytes,
0,
total_length-nbr_dummy_bytes) - xfer.rx_buf;But I'm not sure whether that's more readable, maybe give it a try.
I'm also fine with the while loop.
Peter