Skip to contentSkip to conversation

TPM: STMicroelectronics st33 driver SPI

PATCH In review Matthias Leblanc 2 participants 4 messages May 17, 2013 – May 23, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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 causing compilation errors Initalise data and temp variable
Change some variable type
Fix warnings and errors from smatch tools
Change for loop to while loop

 drivers/char/tpm/Kconfig            |   10 +
 drivers/char/tpm/Makefile           |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c |  943 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h |   61 +++
 4 files changed, 1015 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
index dbfd564..2fc1254 100644
@@ -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_stm_st33_spi.
+
 endif # TCG_TPM
drivers/char/tpm/Makefile +1 -0
index a3736c9..a1db3dd 100644
@@ -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 +943 -0

943 lines not on this page · raw message

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.10.4
#1 lore raw
Quoted 1 line · Matthias Leblanc

From: Mathias Leblanc <[email protected]>

Which name is correct? You have not to have this From: line if submitter and author is the same person.

drivers/char/tpm/tpm_spi_stm_st33.c line 3

Quoted 5 lines

+++ b/drivers/char/tpm/tpm_spi_stm_st33.c

drivers/char/tpm/tpm_spi_stm_st33.c new file +3 -0
@@ -0,0 +1,943 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics

2013 as well?

drivers/char/tpm/tpm_spi_stm_st33.c line 30

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+ *     09/15/2010:     First shot driver tpm_tis driver for lpc is
+ *                     used as model.

I beleive it could fit one line.

Review line 7

Quoted 2 lines
(quoted diff) +2 -0
+#include "tpm.h"
+

Seems redundant empty line.

drivers/char/tpm/tpm_spi_stm_st33.c line 67

Quoted 5 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+#include "tpm_spi_stm_st33.h"
 
+enum stm33zp24_int_flags {
+       TPM_GLOBAL_INT_ENABLE = 0x80,
+       TPM_INTF_CMD_READY_INT = 0x080,

What the difference? It looks like first constant is not belong to this enum.

drivers/char/tpm/tpm_spi_stm_st33.c line 99

Quoted 10 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +10 -0
+static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+                     u8 *tpm_data, u16 tpm_size)
+{
+       u8 data = 0;
+       int total_length = 0, nbr_dummy_bytes;
+       int value = 0;
+       struct spi_device *dev =
+                (struct spi_device __force *)tpm->vendor.iobase;
+       struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+       u8 *data_buffer = platform_data->tpm_spi_buffer[0];

It seems a bad idea to have buffers in platform_data. I bet the buffers should be part of other struct. What did I miss?

drivers/char/tpm/tpm_spi_stm_st33.c line 103

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+       struct spi_transfer xfer = {
+               .tx_buf  = data_buffer,
+               .rx_buf  = platform_data->tpm_spi_buffer[1],
+       };

... even this entire structure.
Can you consider to use spi_message API ?

Quoted 1 line

+

Redundant empty line.

Quoted 1 line

+ data = (tpm_size >> 8) & 0x00ff;

No need to do & 0xff. You have u8 type anyway.

drivers/char/tpm/tpm_spi_stm_st33.c line 119

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+                       data_buffer[total_length++] = data;
+                       data = tpm_size & 0x00ff;

Ditto.

drivers/char/tpm/tpm_spi_stm_st33.c line 235

Quoted 8 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +8 -0
+static unsigned long wait_for_serirq_timeout(struct tpm_chip *chip,
+        bool condition, unsigned long timeout)
+{
+       long status = 0;
+       struct spi_device *client;
+       struct st33zp24_platform_data *pin_infos;
+
+       client = (struct spi_device __force *)chip->vendor.iobase;

Is there any better storage for this pointer? It seems an abuse of iobase member.

Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +7 -0
+       pin_infos = client->dev.platform_data;
+
+       status = wait_for_completion_interruptible_timeout(
+                               &pin_infos->irq_detection, timeout);
+       if (status > 0)
+               enable_irq(gpio_to_irq(pin_infos->io_serirq));
+       gpio_direction_input(pin_infos->io_serirq);

drivers/char/tpm/tpm_spi_stm_st33.c line 252

Quoted 10 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +10 -0
+
+       if (!status) {
+               status = -EBUSY;
+               goto wait_end;
+       }
+       clear_interruption(chip);
+       if (condition)
+               status = 1;
+
+wait_end:

Redundant label. Use direct return wherever it applies.

drivers/char/tpm/tpm_spi_stm_st33.c line 260

Quoted 6 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +5 -0
+       return status;
 
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.

Just mention the member and struct names here, no need to refer to entire file.

drivers/char/tpm/tpm_spi_stm_st33.c line 268

Quoted 8 lines

+ */

drivers/char/tpm/tpm_spi_stm_st33.c new file +7 -0
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+       u8 data = TPM_STS_COMMAND_READY;
+
+       /* this causes the current command to be aborted */
+       spi_write8_reg(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_cancel() */

This comment is redundant.

Quoted 1 line

+} /* tpm_stm_spi_status() */

Ditto.
Here and anywhere in the file.

drivers/char/tpm/tpm_spi_stm_st33.c line 47

Quoted 3 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +3 -0
+
+
+

Couple of redundant empty lines.

drivers/char/tpm/tpm_spi_stm_st33.c line 313

Quoted 6 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +5 -0
+static int request_locality(struct tpm_chip *chip)
+{
 
+       unsigned long stop;
+       long rc;
+       u8 data = 0;

Redundant assignment. Please, check entire file for such assignments.

Quoted 1 line

+end:

Redundant label.

drivers/char/tpm/tpm_spi_stm_st33.c line 377

Quoted 6 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+       return -EACCES;
 
+static int get_burstcount(struct tpm_chip *chip)
+{
 
+               tpm_reg = tpm_reg + 1;

tpm_reg++;

Quoted 1 line

+end:

Redundant label. Please, clean up entire file from such useless labels.

drivers/char/tpm/tpm_spi_stm_st33.c line 455

Quoted 12 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +10 -0
+       return -EBUSY;
 
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
 
+               burstcnt = get_burstcount(chip);
+               len = min_t(int, burstcnt, count - size);
+               status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
+               if (status < 0)
+                       return status;
+
+

Useless empty line(s).

drivers/char/tpm/tpm_spi_stm_st33.c line 497

Quoted 9 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +9 -0
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+                           size_t len)
+{
+       u32 burstcnt = 0, i, size = 0;
+       u8 data = 0;
+       long status = 0, ret = 0;
+
+       if (chip == NULL)
+               return -EBUSY;

-EINVAL, btw.

drivers/char/tpm/tpm_spi_stm_st33.c line 499

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+       if (len < TPM_HEADER_SIZE)
+               return -EBUSY;

Same.

drivers/char/tpm/tpm_spi_stm_st33.c line 567

Quoted 8 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +8 -0
+static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
+                           size_t count)
+{
+       int size = 0;
+       int expected;
+
+       if (chip == NULL)
+               return -EBUSY;

-EINVAL. Check entire code.

drivers/char/tpm/tpm_spi_stm_st33.c line 571

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+       if (count < TPM_HEADER_SIZE) {
+               size = -EIO;
+               goto out;
+       }

You will perform asymmetric actions here. At least it requires some explanations in the header of fuction.

drivers/char/tpm/tpm_spi_stm_st33.c line 667

Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +7 -0
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");

Move this section to the top/bottom of the file.

drivers/char/tpm/tpm_spi_stm_st33.c line 686

Quoted 11 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +11 -0
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+       long err = 0;
+       u8 intmask;
+       struct tpm_chip *chip;
+       struct st33zp24_platform_data *platform_data;
+
+       /* Check SPI platform functionnalities */
+       if (dev == NULL) {
+               pr_info("dev is NULL. exiting.\n");

Looks like debug print. Should be remove

Review

Quoted 2 lines
(quoted diff) +2 -0
+               err = -ENODEV;
+               goto end;

return -ENODEV;

drivers/char/tpm/tpm_spi_stm_st33.c line 694

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+       chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+       if (!chip) {
+               err = -ENODEV;
+               goto end;

Ditto.

Quoted 5 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +5 -0
+       /* Allocation of SPI buffers MISO and MOSI              */
+       /* Size is as follow:                                   */
+       /* Request burstcount value  = 0x800 = 2048             */
+       /* +                                                    */
+       /* Response burstcount value = 0x400 = 1024             */

drivers/char/tpm/tpm_spi_stm_st33.c line 711

Quoted 10 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +10 -0
+       /* +                                                    */
+       /* At least:                                            */
+       /* 1 byte for direction/locality                        */
+       /* 1 byte tpm tis register                              */
+       /* 2 bytes spi data length (for request only)           */
+       /* 2 latency bytes                                      */
+       /* 1 status byte                                        */
+       /* = 2048 + 1024 + 7                                    */
+       /* We reserved 2048 + 1024 + 20 in case latency byte    */
+       /* change                                               */

Looks like a candidate to *.h file in the struct description.

Quoted 1 line

+ platform_data = dev->dev.platform_data;

And as I said already, it's not a platform_data.

drivers/char/tpm/tpm_spi_stm_st33.c line 714

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+
+       if (platform_data)

if (!platform_data)
return -ENODEV;

drivers/char/tpm/tpm_spi_stm_st33.c line 717

Quoted 3 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +3 -0
+               platform_data->tpm_spi_buffer[0] =
+               kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+                                TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);

This magic calc may gone when you will use dedicated constant with mentioned explanations.

drivers/char/tpm/tpm_spi_stm_st33.c line 719

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+       else
+               goto end;

Remove those two.

Quoted 1 line

+ chip->vendor.iobase = (void __iomem *)dev;

Don't like this one. Try to find better way to drag pointer.

Quoted 1 line

+ pr_info("TPM SPI Initialized\n");

Something like
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt at the very top of file could be helpful.

Quoted 1 line

+ if (platform_data->tpm_spi_buffer[1] != NULL) {

Redundant check.

drivers/char/tpm/tpm_spi_stm_st33.c line 833

Quoted 3 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+               kfree(platform_data->tpm_spi_buffer[1]);
 
+       if (platform_data->tpm_spi_buffer[0] != NULL) {

Ditto.

drivers/char/tpm/tpm_spi_stm_st33.c line 847

Quoted 5 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
 
+               clear_bit(0, &chip->is_open);

Leftover?

Quoted 5 lines

+ * @return: 0 in case of success.
+ */

drivers/char/tpm/tpm_spi_stm_st33.c new file +3 -0
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+       struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);

drivers/char/tpm/tpm_spi_stm_st33.c line 862

Quoted 10 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +10 -0
+       struct st33zp24_platform_data *pin_infos =
+        ((struct spi_device __force *)chip->vendor.iobase)->dev.platform_data;
+
+       if (pin_infos != NULL) {
+               gpio_free(pin_infos->io_lpcpd);
+
+               /* Check if chip has been previously clean */
+               if (pin_infos->bchipf != true)
+                       tpm_remove_hardware(chip->dev);
+               if (pin_infos->tpm_spi_buffer[1] != NULL) {

Redundant check.

drivers/char/tpm/tpm_spi_stm_st33.c line 866

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+                       kfree(pin_infos->tpm_spi_buffer[1]);
+                       pin_infos->tpm_spi_buffer[1] = NULL;
+               }
+               if (pin_infos->tpm_spi_buffer[0] != NULL) {

Ditto.

drivers/char/tpm/tpm_spi_stm_st33.h line 3

Quoted 5 lines

+++ b/drivers/char/tpm/tpm_spi_stm_st33.h

drivers/char/tpm/tpm_spi_stm_st33.h new file +3 -0
@@ -0,0 +1,61 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics

2013 as well?

drivers/char/tpm/tpm_spi_stm_st33.h line 39

Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.h new file +7 -0
+#define TPM_ACCESS                     (0x0)
+#define TPM_STS                                (0x18)
+#define TPM_DATA_FIFO                  (0x24)
+#define TPM_HASH_DATA                  (0x24)
+#define TPM_INTF_CAPABILITY            (0x14)
+#define TPM_INT_STATUS                 (0x10)
+#define TPM_INT_ENABLE                 (0x08)

What the point to embrace those constants?

--
With Best Regards,
Andy Shevchenko

#2 lore raw

Hello Andy,

Thanks for your support, I will fix these code style problem.

However in a first time, can we publish this SPI driver?
I think that it will be preferable to submit it and apply some patch if it's only coding style error. I have fix errors in this patch that has been discovered in the I2C patch, so I don't know what's stop this submission. I think that's driver is more criticized than the I2C driver although it's the same base.
I know that's important to have good code in the kernel source and I'm agree about that,
I propose it be published as a first release and I fix coding style problem in a second time.

Thanks,

Mathias Leblanc

-----Original Message-----
From: Andy Shevchenko [mailto:[email protected]]
Sent: 19 May, 2013 12:36
To: Mathias LEBLANC
Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; [email protected]; [email protected]; Jean-Luc BLANC
Subject: Re: [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI

Quoted 1 line · Matthias Leblanc

From: Mathias Leblanc <[email protected]>

Which name is correct? You have not to have this From: line if submitter and author is the same person.

drivers/char/tpm/tpm_spi_stm_st33.c line 3

Quoted 5 lines

+++ b/drivers/char/tpm/tpm_spi_stm_st33.c

drivers/char/tpm/tpm_spi_stm_st33.c new file +3 -0
@@ -0,0 +1,943 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics

2013 as well?

drivers/char/tpm/tpm_spi_stm_st33.c line 30

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+ *     09/15/2010:     First shot driver tpm_tis driver for lpc is
+ *                     used as model.

I beleive it could fit one line.

Review line 7

Quoted 2 lines
(quoted diff) +2 -0
+#include "tpm.h"
+

Seems redundant empty line.

drivers/char/tpm/tpm_spi_stm_st33.c line 67

Quoted 5 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+#include "tpm_spi_stm_st33.h"
 
+enum stm33zp24_int_flags {
+       TPM_GLOBAL_INT_ENABLE = 0x80,
+       TPM_INTF_CMD_READY_INT = 0x080,

What the difference? It looks like first constant is not belong to this enum.

drivers/char/tpm/tpm_spi_stm_st33.c line 99

Quoted 9 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +9 -0
+static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+                     u8 *tpm_data, u16 tpm_size) {
+       u8 data = 0;
+       int total_length = 0, nbr_dummy_bytes;
+       int value = 0;
+       struct spi_device *dev =
+                (struct spi_device __force *)tpm->vendor.iobase;
+       struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+       u8 *data_buffer = platform_data->tpm_spi_buffer[0];

It seems a bad idea to have buffers in platform_data. I bet the buffers should be part of other struct. What did I miss?

drivers/char/tpm/tpm_spi_stm_st33.c line 103

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+       struct spi_transfer xfer = {
+               .tx_buf  = data_buffer,
+               .rx_buf  = platform_data->tpm_spi_buffer[1],
+       };

... even this entire structure.
Can you consider to use spi_message API ?

Quoted 1 line

+

Redundant empty line.

Quoted 1 line

+ data = (tpm_size >> 8) & 0x00ff;

No need to do & 0xff. You have u8 type anyway.

drivers/char/tpm/tpm_spi_stm_st33.c line 119

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+                       data_buffer[total_length++] = data;
+                       data = tpm_size & 0x00ff;

Ditto.

drivers/char/tpm/tpm_spi_stm_st33.c line 235

Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +7 -0
+static unsigned long wait_for_serirq_timeout(struct tpm_chip *chip,
+        bool condition, unsigned long timeout) {
+       long status = 0;
+       struct spi_device *client;
+       struct st33zp24_platform_data *pin_infos;
+
+       client = (struct spi_device __force *)chip->vendor.iobase;

Is there any better storage for this pointer? It seems an abuse of iobase member.

Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +7 -0
+       pin_infos = client->dev.platform_data;
+
+       status = wait_for_completion_interruptible_timeout(
+                               &pin_infos->irq_detection, timeout);
+       if (status > 0)
+               enable_irq(gpio_to_irq(pin_infos->io_serirq));
+       gpio_direction_input(pin_infos->io_serirq);

drivers/char/tpm/tpm_spi_stm_st33.c line 252

Quoted 10 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +10 -0
+
+       if (!status) {
+               status = -EBUSY;
+               goto wait_end;
+       }
+       clear_interruption(chip);
+       if (condition)
+               status = 1;
+
+wait_end:

Redundant label. Use direct return wherever it applies.

drivers/char/tpm/tpm_spi_stm_st33.c line 260

Quoted 6 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +5 -0
+       return status;
 
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.

Just mention the member and struct names here, no need to refer to entire file.

drivers/char/tpm/tpm_spi_stm_st33.c

Quoted 7 lines

+ */

drivers/char/tpm/tpm_spi_stm_st33.c new file +6 -0
+static void tpm_stm_spi_cancel(struct tpm_chip *chip) {
+       u8 data = TPM_STS_COMMAND_READY;
+
+       /* this causes the current command to be aborted */
+       spi_write8_reg(chip, TPM_STS, &data, 1); } /* 
+tpm_stm_spi_cancel() */

This comment is redundant.

Quoted 1 line

+} /* tpm_stm_spi_status() */

Ditto.
Here and anywhere in the file.

drivers/char/tpm/tpm_spi_stm_st33.c line 47

Quoted 3 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +3 -0
+
+
+

Couple of redundant empty lines.

drivers/char/tpm/tpm_spi_stm_st33.c line 313

Quoted 5 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+static int request_locality(struct tpm_chip *chip) {
 
+       unsigned long stop;
+       long rc;
+       u8 data = 0;

Redundant assignment. Please, check entire file for such assignments.

Quoted 1 line

+end:

Redundant label.

Review

Quoted 5 lines
(quoted diff) +3 -0
+       return -EACCES;
 
+static int get_burstcount(struct tpm_chip *chip) {
 
+               tpm_reg = tpm_reg + 1;

tpm_reg++;

Quoted 1 line

+end:

Redundant label. Please, clean up entire file from such useless labels.

drivers/char/tpm/tpm_spi_stm_st33.c line 455

Quoted 11 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +9 -0
+       return -EBUSY;
 
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) {
 
+               burstcnt = get_burstcount(chip);
+               len = min_t(int, burstcnt, count - size);
+               status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
+               if (status < 0)
+                       return status;
+
+

Useless empty line(s).

drivers/char/tpm/tpm_spi_stm_st33.c line 497

Quoted 8 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +8 -0
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+                           size_t len) {
+       u32 burstcnt = 0, i, size = 0;
+       u8 data = 0;
+       long status = 0, ret = 0;
+
+       if (chip == NULL)
+               return -EBUSY;

-EINVAL, btw.

drivers/char/tpm/tpm_spi_stm_st33.c line 499

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+       if (len < TPM_HEADER_SIZE)
+               return -EBUSY;

Same.

drivers/char/tpm/tpm_spi_stm_st33.c line 567

Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +7 -0
+static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
+                           size_t count) {
+       int size = 0;
+       int expected;
+
+       if (chip == NULL)
+               return -EBUSY;

-EINVAL. Check entire code.

drivers/char/tpm/tpm_spi_stm_st33.c line 571

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+       if (count < TPM_HEADER_SIZE) {
+               size = -EIO;
+               goto out;
+       }

You will perform asymmetric actions here. At least it requires some explanations in the header of fuction.

drivers/char/tpm/tpm_spi_stm_st33.c line 667

Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +7 -0
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");

Move this section to the top/bottom of the file.

drivers/char/tpm/tpm_spi_stm_st33.c line 686

Quoted 10 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +10 -0
+static int
+tpm_st33_spi_probe(struct spi_device *dev) {
+       long err = 0;
+       u8 intmask;
+       struct tpm_chip *chip;
+       struct st33zp24_platform_data *platform_data;
+
+       /* Check SPI platform functionnalities */
+       if (dev == NULL) {
+               pr_info("dev is NULL. exiting.\n");

Looks like debug print. Should be remove

Review

Quoted 2 lines
(quoted diff) +2 -0
+               err = -ENODEV;
+               goto end;

return -ENODEV;

drivers/char/tpm/tpm_spi_stm_st33.c line 694

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+       chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+       if (!chip) {
+               err = -ENODEV;
+               goto end;

Ditto.

Quoted 5 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +5 -0
+       /* Allocation of SPI buffers MISO and MOSI              */
+       /* Size is as follow:                                   */
+       /* Request burstcount value  = 0x800 = 2048             */
+       /* +                                                    */
+       /* Response burstcount value = 0x400 = 1024             */

drivers/char/tpm/tpm_spi_stm_st33.c line 711

Quoted 10 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +10 -0
+       /* +                                                    */
+       /* At least:                                            */
+       /* 1 byte for direction/locality                        */
+       /* 1 byte tpm tis register                              */
+       /* 2 bytes spi data length (for request only)           */
+       /* 2 latency bytes                                      */
+       /* 1 status byte                                        */
+       /* = 2048 + 1024 + 7                                    */
+       /* We reserved 2048 + 1024 + 20 in case latency byte    */
+       /* change                                               */

Looks like a candidate to *.h file in the struct description.

Quoted 1 line

+ platform_data = dev->dev.platform_data;

And as I said already, it's not a platform_data.

drivers/char/tpm/tpm_spi_stm_st33.c line 714

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+
+       if (platform_data)

if (!platform_data)
return -ENODEV;

drivers/char/tpm/tpm_spi_stm_st33.c

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+               platform_data->tpm_spi_buffer[0] =
+               kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+                                TPM_DIGEST_SIZE) * sizeof(u8), 
+ GFP_KERNEL);

This magic calc may gone when you will use dedicated constant with mentioned explanations.

drivers/char/tpm/tpm_spi_stm_st33.c line 719

Quoted 2 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+       else
+               goto end;

Remove those two.

Quoted 1 line

+ chip->vendor.iobase = (void __iomem *)dev;

Don't like this one. Try to find better way to drag pointer.

Quoted 1 line

+ pr_info("TPM SPI Initialized\n");

Something like
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt at the very top of file could be helpful.

Quoted 1 line

+ if (platform_data->tpm_spi_buffer[1] != NULL) {

Redundant check.

drivers/char/tpm/tpm_spi_stm_st33.c line 833

Quoted 3 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +2 -0
+               kfree(platform_data->tpm_spi_buffer[1]);
 
+       if (platform_data->tpm_spi_buffer[0] != NULL) {

Ditto.

drivers/char/tpm/tpm_spi_stm_st33.c line 847

Quoted 5 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
 
+               clear_bit(0, &chip->is_open);

Leftover?

Quoted 5 lines

+ * @return: 0 in case of success.
+ */

drivers/char/tpm/tpm_spi_stm_st33.c new file +3 -0
+static int tpm_st33_spi_remove(struct spi_device *client) {
+       struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+       struct st33zp24_platform_data *pin_infos =

drivers/char/tpm/tpm_spi_stm_st33.c line 862

Quoted 10 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +10 -0
+        ((struct spi_device __force 
+*)chip->vendor.iobase)->dev.platform_data;
+
+       if (pin_infos != NULL) {
+               gpio_free(pin_infos->io_lpcpd);
+
+               /* Check if chip has been previously clean */
+               if (pin_infos->bchipf != true)
+                       tpm_remove_hardware(chip->dev);
+               if (pin_infos->tpm_spi_buffer[1] != NULL) {

Redundant check.

drivers/char/tpm/tpm_spi_stm_st33.c line 866

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+                       kfree(pin_infos->tpm_spi_buffer[1]);
+                       pin_infos->tpm_spi_buffer[1] = NULL;
+               }
+               if (pin_infos->tpm_spi_buffer[0] != NULL) {

Ditto.

drivers/char/tpm/tpm_spi_stm_st33.h line 3

Quoted 5 lines

+++ b/drivers/char/tpm/tpm_spi_stm_st33.h

drivers/char/tpm/tpm_spi_stm_st33.h new file +3 -0
@@ -0,0 +1,61 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics

2013 as well?

drivers/char/tpm/tpm_spi_stm_st33.h line 39

Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.h new file +7 -0
+#define TPM_ACCESS                     (0x0)
+#define TPM_STS                                (0x18)
+#define TPM_DATA_FIFO                  (0x24)
+#define TPM_HASH_DATA                  (0x24)
+#define TPM_INTF_CAPABILITY            (0x14)
+#define TPM_INT_STATUS                 (0x10)
+#define TPM_INT_ENABLE                 (0x08)

What the point to embrace those constants?

--
With Best Regards,
Andy Shevchenko ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#3 lore raw
Quoted 1 line · Mathias LEBLANC

Thanks for your support, I will fix these code style problem.

I left below the comments I think should be addressed besides style. Please, comment what you think about them.

Quoted 1 line

However in a first time, can we publish this SPI driver?

It's up to SPI subsystem maintainer, though I couldn't consider the quality of the driver is enough to include to upstream. You may try to ask Greg to go to staging if you have real demand of this.

Quoted 1 line

I think that it will be preferable to submit it and apply some patch if it's only coding style error.

I don't support the way of submitting patch on top of submiting something that must be just fixed.

Quoted 2 lines

I have fix errors in this patch that has been discovered in the I2C patch, so I don't know what's stop this submission. I think that's driver is more criticized than the I2C driver although it's the same base.

I hope you understand it's not an exuce.

Quoted 1 line

I know that's important to have good code in the kernel source and I'm agree about that,

Good.

Quoted 1 line

I propose it be published as a first release and I fix coding style problem in a second time.

See above.

Quoted 38 lines

drivers/char/tpm/tpm_spi_stm_st33.c line 67

Quoted 5 lines

+++ b/drivers/char/tpm/tpm_spi_stm_st33.c

drivers/char/tpm/tpm_spi_stm_st33.c new file +3 -0
+enum stm33zp24_int_flags {
+       TPM_GLOBAL_INT_ENABLE = 0x80,
+       TPM_INTF_CMD_READY_INT = 0x080,

What the difference? It looks like first constant is not belong to this enum.

drivers/char/tpm/tpm_spi_stm_st33.c line 99

Quoted 9 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +9 -0
+static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+                     u8 *tpm_data, u16 tpm_size) {
+       u8 data = 0;
+       int total_length = 0, nbr_dummy_bytes;
+       int value = 0;
+       struct spi_device *dev =
+                (struct spi_device __force *)tpm->vendor.iobase;
+       struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+       u8 *data_buffer = platform_data->tpm_spi_buffer[0];

It seems a bad idea to have buffers in platform_data. I bet the buffers should be part of other struct. What did I miss?

drivers/char/tpm/tpm_spi_stm_st33.c line 103

Quoted 4 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +4 -0
+       struct spi_transfer xfer = {
+               .tx_buf  = data_buffer,
+               .rx_buf  = platform_data->tpm_spi_buffer[1],
+       };

... even this entire structure.
Can you consider to use spi_message API ?

drivers/char/tpm/tpm_spi_stm_st33.c line 235

Quoted 7 lines
drivers/char/tpm/tpm_spi_stm_st33.c new file +7 -0
+static unsigned long wait_for_serirq_timeout(struct tpm_chip *chip,
+        bool condition, unsigned long timeout) {
+       long status = 0;
+       struct spi_device *client;
+       struct st33zp24_platform_data *pin_infos;
+
+       client = (struct spi_device __force *)chip->vendor.iobase;

Is there any better storage for this pointer? It seems an abuse of iobase member.

Quoted 3 lines
Quoted 1 line

+ chip->vendor.iobase = (void __iomem *)dev;

Don't like this one. Try to find better way to drag pointer.

--
With Best Regards,
Andy Shevchenko