ST19NP18 I2C driver submission + possible bug found in the last TPM kernel core
Subsystems (3)
Hi,
Please find in attached file the tpm driver for the ST19NP18 device working on a I2C bus. This driver use the Linux I2C cores, TPM cores and generic gpio interfaces.
It has been tested with the latest stable kernel 2.6.35.4 available on kernel.org.
Let me know of your feedback.
Best Regards
Christophe RICARD.
--------------------- Previous Message --------------------------------------------------
Hi Chris,
Just submit it to LKML with us on CC, so I'll then ack and CC James Morris.
Thanks
---
Rajiv Andrade
Security Development
IBM Linux Technology Center
Documentation/tpm/tpm_stm_st19_i2c.txt +168 -0
@@ -0,0 +1,168 @@ +/* + * STMicroelectronics TPM I2C Linux driver for TPM ST19NP18 + * Copyright (C) 2009, 2010 STMicroelectronics + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * 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. + * + * @Author: Christophe RICARD [email protected] + */ + +PURPOSE OF THE DOCUMENT +------------------------ +This document is intend to describe how to install the TPM driver for +TPM ST19NP18 using I2C protocols. + + +PLATFORM USED FOR TESTING +-------------------------- +During the development, several embedded platforms running ARM CPU have been +used. +Validated platforms listing: +- TI Beagleboard +- STMicroelectronics Spear 300 +- STMicroelectronics Spear 600 + +REQUIREMENTS +------------- +Software +========= +This TPM driver could be install under a kernel which implement at least the +following features: +- Linux GENERIC_GPIO programming interfaces. +- I2C new style programming interface base (with probe & remove functions) +- 1 I2C adapter (I2C Linux controller driver) or use of I2C_GPIO driver for I2C +bitbanging. + + +Hardware +========= +To run a TPM the platform needs at least: +- 2 Power supply (3.3V). +- 1 I2C controller or 2 GPIOs used for SDA & SCL (I2C bit bang method). +- 2 GPIOs for signals accept_command & data_available. + +TPM I2C speed is 100Khz (Maximum) + +All TPM signals work at 3.3V + +HOW TO INSTALL +--------------- +Platform installation file +=========================== +(N.B: platform file in arch/<processor_type>/mach-<platform-name>/ + + +1 - Software integration +========================= +<processor_type> could be: alpha, arm, avr32, blackfin, cris, frv, h8300, ia64, +m32r, m68k, m68knommu, parisc, powerpc, s390, sh, sparc, sparc64, um, x86, +xtensa... +<platform-name> corresponds to your platform + +In the file where the machine_init() function exists, the developer must +declare: +- 1 struct st19np18_platform_data to provide which gpio the driver will use. + * The accept_pin and data_avail_pin gpio are configured as input only. + * This gpio management is under the platform developer responsability. + +Finally in the machine_init() function provided in the same file, the developer +should use the well known function i2c_register_board_info() from the I2C Linux +API Core. + +2- Hardware integration +======================== +- ST recommends connecting VPS1 and VPS2 to board power supply and at least two +GNDs (on each side of TSSOP28 package). (See datasheet for further informations) + +- As the ST19NP18 has no internal pull up, ST recommands to had: + * 2 external pull up on SDA & SCL signals (RpSDA/RpSCL) with value according +to the abacus on page 40 or the "I2C Bus specification", version 2.1 January +2000. + + +Platform integration advises +============================= + +For power management purposes, the kernel will send a TPM_SaveState command in the +suspend tpm driver function. +If the platform generate a TPM Init event on wakeup, the first TPM command that should +be executed before the Linux kernel is back (resume function execution) is +TPM_Startup(ST_STATE). + +Here is an example with beagleboard: +==================================== +Depending on the platform, the developper should specify in +the platform init file the following informations: +- The platform gpio's used to managed the tpm's accept_pin/data_avail_pin +(in a struct st19np18_platform_data declaration). +- The TPM I2C 7 bits address (TPM_I2C_ST19_ADDR_WR) (in a struct i2c_board_info). + +Then the developper should add the TPM slave device to the good i2c adapter with the +i2c_register_board_info function (Assuming that the gpio and the i2c bus are well configured). + +file arch\arm\mach-omap2\board-omap3beagle.c +add the following: +----------------------------------------------------------------------- + +static struct st19np18_platform_data tpm_data = { + .accept_pin = 135, + .data_avail_pin = 143, +}; + +static struct i2c_board_info __initdata tpm_st19_i2c_board_info[] = { + { + I2C_BOARD_INFO(TPM_DRIVER_NAME, TPM_I2C_ST19_ADDR_WR), + .platform_data = &tpm_data, + }, +}; + +------------------------------------------------------------------------ +Then complete the beagleboard init to be like that: +------------------------------------------------------------------------ +static void __init omap3_beagle_init(void) +{ + omap3_mux_init(board_mux, OMAP_PACKAGE_CBB); + omap3_beagle_i2c_init(); + platform_add_devices(omap3_beagle_devices, + ARRAY_SIZE(omap3_beagle_devices)); + omap_serial_init(); + + omap_mux_init_gpio(170, OMAP_PIN_INPUT); + gpio_request(170, "DVI_nPD"); + /* REVISIT leave DVI powered down until it's needed ... */ + gpio_direction_output(170, true); + + usb_musb_init(&musb_board_data); + usb_ehci_init(&ehci_pdata); + omap3beagle_flash_init(); + + beagle_display_init(); + + /* Ensure SDRC pins are mux'd for self-refresh */ + omap_mux_init_signal("sdrc_cke0", OMAP_PIN_OUTPUT); + omap_mux_init_signal("sdrc_cke1", OMAP_PIN_OUTPUT); + omap_mux_init_gpio(((struct st19np18_platform_data *) + tpm_st19_i2c_board_info[0].platform_data)->data_avail_pin, + OMAP_PIN_INPUT); + omap_mux_init_gpio(((struct st19np18_platform_data *) + tpm_st19_i2c_board_info[0].platform_data)->accept_pin, + OMAP_PIN_INPUT); + + i2c_register_board_info(3, tpm_st19_i2c_board_info, ARRAY_SIZE(tpm_st19_i2c_board_info)); +}
drivers/char/tpm/Kconfig +9 -0
@@ -60,4 +60,13 @@ Further information on this driver and the supported hardware can be found at http://www.prosec.rub.de/tpm +config TCG_ST19_I2C + tristate "STMicroelectronics ST19 I2C TPM" + depends on I2C + depends on GPIOLIB + ---help--- + If you have a TPM security chip from STMicroelectronics working with + an I2C 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_st19_i2c. endif # TCG_TPM
drivers/char/tpm/Makefile +1 -0
@@ -9,3 +9,4 @@ obj-$(CONFIG_TCG_NSC) += tpm_nsc.o obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o +obj-$(CONFIG_TCG_ST19_I2C) += tpm_stm_st19_i2c.o
drivers/char/tpm/tpm_stm_st19_i2c.c +822 -0
822 lines not on this page · raw message
drivers/char/tpm/tpm_stm_st19_i2c.h +63 -0
63 lines not on this page · raw message
include/linux/i2c/tpm_stm_st19_i2c.h +45 -0
45 lines not on this page · raw message
cover tpm_stm_st19+i2c: use pr_<level> and cleanups
There appears to be a missing break in wait_until_good_shape. Other than that, just some neatening.
Joe Perches (2):
drivers/char/tpm/tpm_stm_st19_i2c.c: Use pr_fmt, pr_<level> and FUNC_ENTER drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neatening
drivers/char/tpm/tpm_stm_st19_i2c.c | 215 ++++++++++++++++++---------------- 1 files changed, 114 insertions(+), 101 deletions(-)
Signature · 1 line
1.7.2.19.g9a302
PATCH 1/2 drivers/char/tpm/tpm_stm_st19_i2c.c: Use pr_fmt, pr_<level> and FUNC_ENTER
Add FUNC_ENTER to emit pr_info("%s\n", __func_) with #DEBUG
--- drivers/char/tpm/tpm_stm_st19_i2c.c | 57 ++++++++++++++++++----------------- 1 files changed, 29 insertions(+), 28 deletions(-)
drivers/char/tpm/tpm_stm_st19_i2c.c +29 -28
@@ -58,6 +58,8 @@* ---------------------------------------------------------------------- */ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/module.h> #include <linux/kernel.h> #include <linux/delay.h>
@@ -81,6 +83,12 @@#include "tpm_stm_st19_i2c.h" +#ifdef DEBUG +#define FUNC_ENTER () pr_info("%s\n", __func__) +#else +#define FUNC_ENTER () do {} while (0) +#endif +/* * @Comments: tpm_stm_st19_platform_specific.h deliver shows a platform specific * file example.
@@ -260,9 +268,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,u32 ret =0 , i, size, ordinal, pin =0 ;struct i2c_client *client; -#ifdef DEBUG -printk (KERN_INFO "tpm_st19_i2c: tpm_stm_i2c_send\n" ); -#endif +FUNC_ENTER ();if (chip ==NULL )return -EBUSY ;
@@ -337,7 +343,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : count - i : count);if (ret <0 ) { -printk (KERN_INFO "tpm_st19_i2c: Failed to send data\n" ); +pr_info ("Failed to send data\n" ); goto end; }
@@ -351,16 +357,13 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, (TPM_I2C_SHORT));if (pin !=COMMAND_ON ) { -printk (KERN_INFO -"tpm_st19_i2c:" -" Failed to read gpio pin (AcceptCmd)\n" ); +pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ; goto end; } }if (i ==0 ) { -printk (KERN_INFO -"tpm_st19_i2c: Failed to read gpio pin (AcceptCmd)\n" ); +pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ; } end:
@@ -395,9 +398,7 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,int pin =0 ;struct i2c_client *client; -#ifdef DEBUG -printk (KERN_INFO "tpm_st19_i2c: tpm_stm_i2c_recv\n" ); -#endif +FUNC_ENTER ();if (chip ==NULL )return -EBUSY ;
@@ -425,9 +426,7 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : size - i : count));if (ret <0 ) { -printk (KERN_INFO -"tpm_st19_i2c:" -" Failed to read gpio pin (DataAvalaible)\n" ); +pr_info ("Failed to read gpio pin (DataAvailable)\n" ); goto end; }
@@ -444,7 +443,7 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf, size = count; } }else { -printk (KERN_INFO "tpm_st19_i2c: read buffer is NULL\n" ); +pr_info ("read buffer is NULL\n" ); goto end; }
@@ -459,9 +458,7 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf, }if (i ==0 ) { -printk (KERN_INFO -"tpm_st19_i2c: " -"Failed to read gpio pin (DataAvalaible)\n" ); +pr_info ("Failed to read gpio pin (DataAvailable)\n" ); ret = -EIO ; goto end; }
@@ -501,7 +498,8 @@ static void tpm_st19_i2c_dummy(struct device *dev)static void tpm_st19_i2c_release (struct kobject *kobj) {struct tpm_chip *chip; -printk (KERN_INFO "tpm_st19_i2c_release\n" ); + +FUNC_ENTER ();if (_client !=NULL ) { chip = (struct tpm_chip *)i2c_get_clientdata (_client);
@@ -614,19 +612,19 @@ tpm_st19_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)struct tpm_chip *chip;struct st19np18_platform_data *platform_data; -printk (KERN_INFO "tpm_st19_i2c: tpm_st19_i2c_probe\n" ); +FUNC_ENTER (); err =0 ;/* Check I2C platform functionnalities */ if (client ==NULL ) { -printk (KERN_INFO "client is NULL. exiting.\n" ); +pr_info ("client is NULL. exiting.\n" ); err = -ENODEV ; goto end; }if (!i2c_check_functionality (client->adapter,I2C_FUNC_I2C )) { -printk (KERN_INFO "tpm_st19_i2c: client not i2c capable\n" ); +pr_info ("client not i2c capable\n" ); err = -ENODEV ; goto end; }
@@ -680,7 +678,7 @@ tpm_st19_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)i2c_set_clientdata (client, chip); pin_infos->bChipF = false; -printk (KERN_INFO "tpm_st19_i2c: TPM I2C Initialized\n" ); +pr_info ("TPM I2C Initialized\n" );return 0 ; _gpio_set: _gpio_init:
@@ -701,7 +699,7 @@ _tpm_clean_answer: } pin_infos->bChipF = true; end: -printk (KERN_INFO "tpm_st19_i2c: TPM I2C initialisation fail\n" ); +pr_info ("TPM I2C initialisation fail\n" );return err; }
@@ -714,7 +712,8 @@ end: static __devexit int tpm_st19_i2c_remove(struct i2c_client *client) {struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata (client); -printk (KERN_INFO "tpm_st19_i2c: tpm_st19_i2c_remove\n" ); + +FUNC_ENTER ();if (pin_infos !=NULL ) {gpio_free (pin_infos->accept_pin);
@@ -799,7 +798,8 @@ static struct i2c_driver tpm_st19_i2c_driver = {*/ static int __init tpm_st19_i2c_init (void ) { -printk (KERN_INFO "tpm_st19_i2c: tpm_st19_i2c_init\n" ); +FUNC_ENTER (); +return i2c_add_driver (&tpm_st19_i2c_driver); }
@@ -809,7 +809,8 @@ static int __init tpm_st19_i2c_init(void)*/ static void __exit tpm_st19_i2c_exit(void) { -printk (KERN_INFO "tpm_st19_i2c: tpm_st19_i2c_exit\n" ); +FUNC_ENTER (); +i2c_del_driver (&tpm_st19_i2c_driver); }
Signature · 1 line
1.7.2.19.g9a302
PATCH 2/2 drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neatening
In reply to Joe PerchesAdd missing break in wait_until_good_shape
Use init; while (test) loops not for(init;test;) Calculate bytes to transfer using if and min_t not multiple ?:
Miscellaneous function whitespace aliging
--- drivers/char/tpm/tpm_stm_st19_i2c.c | 158 +++++++++++++++++++---------------- 1 files changed, 85 insertions(+), 73 deletions(-)
drivers/char/tpm/tpm_stm_st19_i2c.c +85 -73
@@ -158,8 +158,10 @@ static int wait_until_good_shape(void)else if (!signal_pending (current)) { ret =schedule_timeout (time); wait_time += time; - }else + }else { ret = -ERESTARTSYS ; +break ; + } }while (1 );finish_wait (&queue, &__wait);
@@ -204,10 +206,10 @@ static int wait_event_interruptible_on_gpio(wait_queue_head_t queue,else if (wait_time >= timeout)break ;else if (!signal_pending (current)) { - ret = -schedule_timeout (msecs_to_jiffies - (TICK_GPIO_SPOOLING )); - wait_time +=msecs_to_jiffies (TICK_GPIO_SPOOLING ); +int time =msecs_to_jiffies (TICK_GPIO_SPOOLING ); + + ret =schedule_timeout (time); + wait_time += time; }else { ret = -ERESTARTSYS ;break ;
@@ -286,9 +288,9 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,/* sending data (data_avail_pin hight) */ /* If data are available, we read the data */ init_waitqueue_head (&pin_infos->write_queue); - pin =wait_event_interruptible_on_gpio (pin_infos->write_queue, -tpm_calc_ordinal_duration - (chip, ordinal)); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +tpm_calc_ordinal_duration (chip, ordinal));if (pin <0 ) { ret = pin; goto end;
@@ -297,25 +299,30 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =I2C_M_RD ; size =TPM_HEADER_SIZE ; -for (i =0 ; pin ==DATA_ON && i < size;) { - ret =i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (i ==0 ) ?TPM_HEADER_SIZE : - count - i >TPM_I2C_BLOCK_SIZE ? -TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) goto end; -if (i ==0 ) - size = -responseSize (pin_infos->tpm_i2c_buffer[1 ], count); - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) { + size =responseSize (pin_infos->tpm_i2c_buffer[1 ], + count); + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); } pin = wait_event_interruptible_on_gpio(pin_infos->write_queue,
@@ -325,36 +332,41 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =0 ; size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==COMMAND_ON ;) { -memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, - (i ==0 ) ?TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==COMMAND_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + +memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, bytes);if (i ==0 ) { size =responseSize (buf, count); - size = (size < count ? size : count); + size = size < count ? size : count; } - ret = -i2c_master_send (client, - pin_infos->tpm_i2c_buffer[0 ], - count >=TPM_HEADER_SIZE ? (i == -0 ) ? -TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - count - i : count); + +if (count <TPM_HEADER_SIZE ) + bytes = count; + + ret =i2c_master_send (client, pin_infos->tpm_i2c_buffer[0 ], + bytes);if (ret <0 ) {pr_info ("Failed to send data\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) + i +=TPM_HEADER_SIZE ; +else + i +=TPM_I2C_BLOCK_SIZE ;/* Wait for AcceptCmd signal hight */ if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT ));if (pin !=COMMAND_ON ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" );
@@ -362,6 +374,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, goto end; } } +if (i ==0 ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ;
@@ -412,49 +425,48 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,/* Spool on the good gpio as long as pin GPIO 3 not HIGHT */ init_waitqueue_head (&chip->vendor.read_queue); - pin =wait_event_interruptible_on_gpio (chip->vendor.read_queue, -tpm_calc_ordinal_duration - (chip,TPM_I2C_ORDINAL_LONG )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +tpm_calc_ordinal_duration (chip,TPM_I2C_ORDINAL_LONG )); size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==DATA_ON ;) { - ret = -i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i == -0 ?TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - size - i : count)); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (count <TPM_HEADER_SIZE ) + bytes = count; +else if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, size - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) {pr_info ("Failed to read gpio pin (DataAvailable)\n" ); goto end; } -if (buf !=NULL ) { -memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i ==0 ? -TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : size - - i : count)); - -if (i ==0 ) { - size =responseSize (buf, size); -if (size > count) - size = count; - } - }else { +if (!buf) {pr_info ("read buffer is NULL\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], size); + +if (i ==0 ) { + size =responseSize (buf, size); +if (size > count) + size = count; + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (chip->vendor. - read_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); }if (i ==0 ) {
@@ -532,7 +544,7 @@ static ssize_t tpm_st19_i2c_ioctl(struct inode *inode, struct file *file,return -ENOSYS ;case TPMIOC_TRANSMIT :if (copy_from_user (pin_infos->tpm_i2c_buffer[0 ], - (const char *)arg,TPM_HEADER_SIZE )) + (const char *)arg,TPM_HEADER_SIZE ))return -EFAULT ; in_size =responseSize (pin_infos->tpm_i2c_buffer[0 ],TPM_HEADER_SIZE );
Signature · 1 line
1.7.2.19.g9a302
Re: [PATCH 1/2] drivers/char/tpm/tpm_stm_st19_i2c.c: Use pr_fmt, pr_<level> and FUNC_ENTER
In reply to Joe PerchesQuoted 195 lines · Joe Perches
Add
FUNC_ENTERto emitpr_info("%s\n", __func_)with #DEBUGSigned-off-by Joe Perches <[email protected]>--- drivers/char/tpm/tpm_stm_st19_i2c.c | 57 ++++++++++++++++++----------------- 1 files changed, 29 insertions(+), 28 deletions(-)drivers/char/tpm/tpm_stm_st19_i2c.c +29
-28@@ -58,6 +58,8 @@* ---------------------------------------------------------------------- */ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/module.h> #include <linux/kernel.h> #include <linux/delay.h> @@ -81,6 +83,12 @@#include "tpm_stm_st19_i2c.h" +#ifdef DEBUG +#define FUNC_ENTER () pr_info("%s\n", __func__) +#else +#define FUNC_ENTER () do {} while (0) +#endif +/* * @Comments: tpm_stm_st19_platform_specific.h deliver shows a platform specific * file example. @@ -260,9 +268,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,u32 ret =0 , i, size, ordinal, pin =0 ;struct i2c_client *client; -#ifdef DEBUG -printk (KERN_INFO "tpm_st19_i2c: tpm_stm_i2c_send\n" ); -#endif +FUNC_ENTER ();if (chip ==NULL )return -EBUSY ;@@ -337,7 +343,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : count - i : count);if (ret <0 ) { -printk (KERN_INFO "tpm_st19_i2c: Failed to send data\n" ); +pr_info ("Failed to send data\n" ); goto end; }@@ -351,16 +357,13 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, (TPM_I2C_SHORT));if (pin !=COMMAND_ON ) { -printk (KERN_INFO -"tpm_st19_i2c:" -" Failed to read gpio pin (AcceptCmd)\n" ); +pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ; goto end; } }if (i ==0 ) { -printk (KERN_INFO -"tpm_st19_i2c: Failed to read gpio pin (AcceptCmd)\n" ); +pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ; } end:@@ -395,9 +398,7 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,int pin =0 ;struct i2c_client *client; -#ifdef DEBUG -printk (KERN_INFO "tpm_st19_i2c: tpm_stm_i2c_recv\n" ); -#endif +FUNC_ENTER ();if (chip ==NULL )return -EBUSY ;@@ -425,9 +426,7 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : size - i : count));if (ret <0 ) { -printk (KERN_INFO -"tpm_st19_i2c:" -" Failed to read gpio pin (DataAvalaible)\n" ); +pr_info ("Failed to read gpio pin (DataAvailable)\n" ); goto end; }@@ -444,7 +443,7 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf, size = count; } }else { -printk (KERN_INFO "tpm_st19_i2c: read buffer is NULL\n" ); +pr_info ("read buffer is NULL\n" ); goto end; }@@ -459,9 +458,7 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf, }if (i ==0 ) { -printk (KERN_INFO -"tpm_st19_i2c: " -"Failed to read gpio pin (DataAvalaible)\n" ); +pr_info ("Failed to read gpio pin (DataAvailable)\n" ); ret = -EIO ; goto end; }@@ -501,7 +498,8 @@ static void tpm_st19_i2c_dummy(struct device *dev)static void tpm_st19_i2c_release (struct kobject *kobj) {struct tpm_chip *chip; -printk (KERN_INFO "tpm_st19_i2c_release\n" ); + +FUNC_ENTER ();if (_client !=NULL ) { chip = (struct tpm_chip *)i2c_get_clientdata (_client);@@ -614,19 +612,19 @@ tpm_st19_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)struct tpm_chip *chip;struct st19np18_platform_data *platform_data; -printk (KERN_INFO "tpm_st19_i2c: tpm_st19_i2c_probe\n" ); +FUNC_ENTER (); err =0 ;/* Check I2C platform functionnalities */ if (client ==NULL ) { -printk (KERN_INFO "client is NULL. exiting.\n" ); +pr_info ("client is NULL. exiting.\n" ); err = -ENODEV ; goto end; }if (!i2c_check_functionality (client->adapter,I2C_FUNC_I2C )) { -printk (KERN_INFO "tpm_st19_i2c: client not i2c capable\n" ); +pr_info ("client not i2c capable\n" ); err = -ENODEV ; goto end; }@@ -680,7 +678,7 @@ tpm_st19_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)i2c_set_clientdata (client, chip); pin_infos->bChipF = false; -printk (KERN_INFO "tpm_st19_i2c: TPM I2C Initialized\n" ); +pr_info ("TPM I2C Initialized\n" );return 0 ; _gpio_set: _gpio_init:@@ -701,7 +699,7 @@ _tpm_clean_answer: } pin_infos->bChipF = true; end: -printk (KERN_INFO "tpm_st19_i2c: TPM I2C initialisation fail\n" ); +pr_info ("TPM I2C initialisation fail\n" );return err; }@@ -714,7 +712,8 @@ end: static __devexit int tpm_st19_i2c_remove(struct i2c_client *client) {struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata (client); -printk (KERN_INFO "tpm_st19_i2c: tpm_st19_i2c_remove\n" ); + +FUNC_ENTER ();if (pin_infos !=NULL ) {gpio_free (pin_infos->accept_pin);@@ -799,7 +798,8 @@ static struct i2c_driver tpm_st19_i2c_driver = {*/ static int __init tpm_st19_i2c_init (void ) { -printk (KERN_INFO "tpm_st19_i2c: tpm_st19_i2c_init\n" ); +FUNC_ENTER (); +return i2c_add_driver (&tpm_st19_i2c_driver); }@@ -809,7 +809,8 @@ static int __init tpm_st19_i2c_init(void)*/ static void __exit tpm_st19_i2c_exit(void) { -printk (KERN_INFO "tpm_st19_i2c: tpm_st19_i2c_exit\n" ); +FUNC_ENTER (); +i2c_del_driver (&tpm_st19_i2c_driver); }
Re: [PATCH 2/2] drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neatening
In reply to Joe PerchesChris, did you have the chance to test it against this specific TPM? Despite this, the patch looks pretty good, thanks Joe.
Quoted 257 lines · Joe Perches
Add missing break in
wait_until_good_shape
Use init; while (test) loops not for(init;test;) Calculate bytes to transfer using if andmin_tnot multiple ?:
Miscellaneous function whitespace aligingSigned-off-by Joe Perches <[email protected]>--- drivers/char/tpm/tpm_stm_st19_i2c.c | 158 +++++++++++++++++++---------------- 1 files changed, 85 insertions(+), 73 deletions(-)drivers/char/tpm/tpm_stm_st19_i2c.c +85
-73@@ -158,8 +158,10 @@ static int wait_until_good_shape(void)else if (!signal_pending (current)) { ret =schedule_timeout (time); wait_time += time; - }else + }else { ret = -ERESTARTSYS ; +break ; + } }while (1 );finish_wait (&queue, &__wait);@@ -204,10 +206,10 @@ static int wait_event_interruptible_on_gpio(wait_queue_head_t queue,else if (wait_time >= timeout)break ;else if (!signal_pending (current)) { - ret = -schedule_timeout (msecs_to_jiffies - (TICK_GPIO_SPOOLING )); - wait_time +=msecs_to_jiffies (TICK_GPIO_SPOOLING ); +int time =msecs_to_jiffies (TICK_GPIO_SPOOLING ); + + ret =schedule_timeout (time); + wait_time += time; }else { ret = -ERESTARTSYS ;break ;@@ -286,9 +288,9 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,/* sending data (data_avail_pin hight) */ /* If data are available, we read the data */ init_waitqueue_head (&pin_infos->write_queue); - pin =wait_event_interruptible_on_gpio (pin_infos->write_queue, -tpm_calc_ordinal_duration - (chip, ordinal)); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +tpm_calc_ordinal_duration (chip, ordinal));if (pin <0 ) { ret = pin; goto end;@@ -297,25 +299,30 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =I2C_M_RD ; size =TPM_HEADER_SIZE ; -for (i =0 ; pin ==DATA_ON && i < size;) { - ret =i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (i ==0 ) ?TPM_HEADER_SIZE : - count - i >TPM_I2C_BLOCK_SIZE ? -TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) goto end; -if (i ==0 ) - size = -responseSize (pin_infos->tpm_i2c_buffer[1 ], count); - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) { + size =responseSize (pin_infos->tpm_i2c_buffer[1 ], + count); + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); } pin = wait_event_interruptible_on_gpio(pin_infos->write_queue,@@ -325,36 +332,41 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =0 ; size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==COMMAND_ON ;) { -memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, - (i ==0 ) ?TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==COMMAND_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + +memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, bytes);if (i ==0 ) { size =responseSize (buf, count); - size = (size < count ? size : count); + size = size < count ? size : count; } - ret = -i2c_master_send (client, - pin_infos->tpm_i2c_buffer[0 ], - count >=TPM_HEADER_SIZE ? (i == -0 ) ? -TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - count - i : count); + +if (count <TPM_HEADER_SIZE ) + bytes = count; + + ret =i2c_master_send (client, pin_infos->tpm_i2c_buffer[0 ], + bytes);if (ret <0 ) {pr_info ("Failed to send data\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) + i +=TPM_HEADER_SIZE ; +else + i +=TPM_I2C_BLOCK_SIZE ;/* Wait for AcceptCmd signal hight */ if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT ));if (pin !=COMMAND_ON ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" );@@ -362,6 +374,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, goto end; } } +if (i ==0 ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ;@@ -412,49 +425,48 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,/* Spool on the good gpio as long as pin GPIO 3 not HIGHT */ init_waitqueue_head (&chip->vendor.read_queue); - pin =wait_event_interruptible_on_gpio (chip->vendor.read_queue, -tpm_calc_ordinal_duration - (chip,TPM_I2C_ORDINAL_LONG )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +tpm_calc_ordinal_duration (chip,TPM_I2C_ORDINAL_LONG )); size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==DATA_ON ;) { - ret = -i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i == -0 ?TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - size - i : count)); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (count <TPM_HEADER_SIZE ) + bytes = count; +else if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, size - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) {pr_info ("Failed to read gpio pin (DataAvailable)\n" ); goto end; } -if (buf !=NULL ) { -memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i ==0 ? -TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : size - - i : count)); - -if (i ==0 ) { - size =responseSize (buf, size); -if (size > count) - size = count; - } - }else { +if (!buf) {pr_info ("read buffer is NULL\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], size); + +if (i ==0 ) { + size =responseSize (buf, size); +if (size > count) + size = count; + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (chip->vendor. - read_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); }if (i ==0 ) {@@ -532,7 +544,7 @@ static ssize_t tpm_st19_i2c_ioctl(struct inode *inode, struct file *file,return -ENOSYS ;case TPMIOC_TRANSMIT :if (copy_from_user (pin_infos->tpm_i2c_buffer[0 ], - (const char *)arg,TPM_HEADER_SIZE )) + (const char *)arg,TPM_HEADER_SIZE ))return -EFAULT ; in_size =responseSize (pin_infos->tpm_i2c_buffer[0 ],TPM_HEADER_SIZE );
Christophe Henri RICARD
Acked-by Rajiv AndradeRE: [PATCH 2/2] drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neatening
Rajiv,
Please find in attached file a full patch (driver + Joe Perches observations/patches + comments update). This patch has been tested.
Thanks
Christophe
-----Original Message-----
From: Rajiv Andrade [mailto:[email protected]]
Sent: Tuesday, September 14, 2010 12:11 PM
To: Joe Perches
Cc: Christophe Henri RICARD; Marcel Selhorst; Debora Velarde; James Morris; [email protected]; LKML
Subject: Re: [PATCH 2/2] drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neatening
Chris, did you have the chance to test it against this specific TPM? Despite this, the patch looks pretty good, thanks Joe.
Quoted 249 lines · Joe Perches
Add missing break in
wait_until_good_shape
Use init; while (test) loops not for(init;test;) Calculate bytes to transfer using if andmin_tnot multiple ?:
Miscellaneous function whitespace aligingSigned-off-by Joe Perches <[email protected]>--- drivers/char/tpm/tpm_stm_st19_i2c.c | 158 +++++++++++++++++++---------------- 1 files changed, 85 insertions(+), 73 deletions(-)drivers/char/tpm/tpm_stm_st19_i2c.c +84
-72@@ -158,8 +158,10 @@ static int wait_until_good_shape(void)else if (!signal_pending (current)) { ret =schedule_timeout (time); wait_time += time; - }else + }else { ret = -ERESTARTSYS ; +break ; + } }while (1 );finish_wait (&queue, &__wait);@@ -204,10 +206,10 @@ static int wait_event_interruptible_on_gpio(wait_queue_head_t queue,else if (wait_time >= timeout)break ;else if (!signal_pending (current)) { - ret = -schedule_timeout (msecs_to_jiffies - (TICK_GPIO_SPOOLING )); - wait_time +=msecs_to_jiffies (TICK_GPIO_SPOOLING ); +int time =msecs_to_jiffies (TICK_GPIO_SPOOLING ); + + ret =schedule_timeout (time); + wait_time += time; }else { ret = -ERESTARTSYS ;break ;@@ -286,9 +288,9 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,/* sending data (data_avail_pin hight) */ /* If data are available, we read the data */ init_waitqueue_head (&pin_infos->write_queue); - pin =wait_event_interruptible_on_gpio (pin_infos->write_queue, -tpm_calc_ordinal_duration - (chip, ordinal)); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +tpm_calc_ordinal_duration (chip, ordinal));if (pin <0 ) { ret = pin; goto end;@@ -297,25 +299,30 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =I2C_M_RD ; size =TPM_HEADER_SIZE ; -for (i =0 ; pin ==DATA_ON && i < size;) { - ret =i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (i ==0 ) ?TPM_HEADER_SIZE : - count - i >TPM_I2C_BLOCK_SIZE ? -TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) goto end; -if (i ==0 ) - size = -responseSize (pin_infos->tpm_i2c_buffer[1 ], count); - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) { + size =responseSize (pin_infos->tpm_i2c_buffer[1 ], + count); + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); } pin = wait_event_interruptible_on_gpio(pin_infos->write_queue,@@ -325,36 +332,41 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =0 ; size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==COMMAND_ON ;) { -memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, - (i ==0 ) ?TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==COMMAND_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + +memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, bytes);if (i ==0 ) { size =responseSize (buf, count); - size = (size < count ? size : count); + size = size < count ? size : count; } - ret = -i2c_master_send (client, - pin_infos->tpm_i2c_buffer[0 ], - count >=TPM_HEADER_SIZE ? (i == -0 ) ? -TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - count - i : count); + +if (count <TPM_HEADER_SIZE ) + bytes = count; + + ret =i2c_master_send (client, pin_infos->tpm_i2c_buffer[0 ], + bytes);if (ret <0 ) {pr_info ("Failed to send data\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) + i +=TPM_HEADER_SIZE ; +else + i +=TPM_I2C_BLOCK_SIZE ;/* Wait for AcceptCmd signal hight */ if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT ));if (pin !=COMMAND_ON ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" );@@ -362,6 +374,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, goto end; } } +if (i ==0 ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ;@@ -412,49 +425,48 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,/* Spool on the good gpio as long as pin GPIO 3 not HIGHT */ init_waitqueue_head (&chip->vendor.read_queue); - pin =wait_event_interruptible_on_gpio (chip->vendor.read_queue, -tpm_calc_ordinal_duration - (chip,TPM_I2C_ORDINAL_LONG )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +tpm_calc_ordinal_duration (chip,TPM_I2C_ORDINAL_LONG )); size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==DATA_ON ;) { - ret = -i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i == -0 ?TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - size - i : count)); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (count <TPM_HEADER_SIZE ) + bytes = count; +else if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, size - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) {pr_info ("Failed to read gpio pin (DataAvailable)\n" ); goto end; } -if (buf !=NULL ) { -memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i ==0 ? -TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : size - - i : count)); - -if (i ==0 ) { - size =responseSize (buf, size); -if (size > count) - size = count; - } - }else { +if (!buf) {pr_info ("read buffer is NULL\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], size); + +if (i ==0 ) { + size =responseSize (buf, size); +if (size > count) + size = count; + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (chip->vendor. - read_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); }if (i ==0 ) {
drivers/char/tpm/tpm_stm_st19_i2c.c static ssize_t tpm_st19_i2c_ioctl(struct inode *inode, struct file *file, · line 550
Quoted 8 lines
drivers/char/tpm/tpm_stm_st19_i2c.c +1
-1@@ -532,7 +544,7 @@ static ssize_t tpm_st19_i2c_ioctl(struct inode *inode, struct file *file,return -ENOSYS ;case TPMIOC_TRANSMIT :if (copy_from_user (pin_infos->tpm_i2c_buffer[0 ], - (const char *)arg,TPM_HEADER_SIZE )) + (const char *)arg,TPM_HEADER_SIZE ))return -EFAULT ; in_size =responseSize (pin_infos->tpm_i2c_buffer[0 ],TPM_HEADER_SIZE );
Documentation/tpm/tpm_stm_st19_i2c.txt +170 -0
@@ -0,0 +1,170 @@ +/* + * STMicroelectronics TPM I2C Linux driver for TPM ST19NP18 + * Copyright (C) 2009, 2010 STMicroelectronics + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * 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. + * + * @Author: Christophe RICARD [email protected] + */ + +PURPOSE OF THE DOCUMENT +------------------------ +This document is intend to describe how to install the TPM driver for +TPM ST19NP18 using I2C protocols. + + +PLATFORM USED FOR TESTING +-------------------------- +During the development, several embedded platforms running ARM CPU have been +used. +Validated platforms listing: +- TI Beagleboard +- STMicroelectronics Spear 300 +- STMicroelectronics Spear 600 + +REQUIREMENTS +------------- +Software +========= +This TPM driver could be install under a kernel which implement at least the +following features: +- Linux GENERIC_GPIO programming interfaces. +- I2C new style programming interface base (with probe & remove functions) +- 1 I2C adapter (I2C Linux controller driver) or use of I2C_GPIO driver for I2C +bitbanging. + + +Hardware +========= +To run a TPM the platform needs at least: +- 2 Power supply (3.3V). +- 1 I2C controller or 2 GPIOs used for SDA & SCL (I2C bit bang method). +- 2 GPIOs for signals accept_command & data_available. + +TPM I2C speed is 100Khz (Maximum) + +All TPM signals work at 3.3V + +HOW TO INSTALL +--------------- +Platform installation file +=========================== +(N.B: platform file in arch/<processor_type>/mach-<platform-name>/ + + +1 - Software integration +========================= +<processor_type> could be: alpha, arm, avr32, blackfin, cris, frv, h8300, ia64, +m32r, m68k, m68knommu, parisc, powerpc, s390, sh, sparc, sparc64, um, x86, +xtensa... +<platform-name> corresponds to your platform + +In the file where the machine_init() function exists, the developer must +declare: +- 1 struct st19np18_platform_data to provide which gpio the driver will use. + * The accept_pin and data_avail_pin gpio are configured as input only. + * This gpio management is under the platform developer responsability. + +Finally in the machine_init() function provided in the same file, the developer +should use the well known function i2c_register_board_info() from the I2C Linux +API Core. + +2- Hardware integration +======================== +- ST recommends connecting VPS1 and VPS2 to board power supply and at least two +GNDs (on each side of TSSOP28 package). (See datasheet for further informations) + +- As the ST19NP18 has no internal pull up, ST recommands to had: + * 2 external pull up on SDA & SCL signals (RpSDA/RpSCL) with value according +to the abacus on page 40 or the "I2C Bus specification", version 2.1 January +2000. + + +Platform integration advises +============================= + +For power management purposes, the kernel will send a TPM_SaveState command in the +suspend tpm driver function. +If the platform generate a TPM Init event on wakeup, the first TPM command that should +be executed before the Linux kernel is back (resume function execution) is +TPM_Startup(ST_STATE). + +Here is an example with beagleboard: +==================================== +Depending on the platform, the developper should specify in +the platform init file the following informations: +- The platform gpio's used to managed the tpm's accept_pin/data_avail_pin +(in a struct st19np18_platform_data declaration). +- The TPM I2C 7 bits address (TPM_I2C_ST19_ADDR_WR) (in a struct i2c_board_info). + +Then the developper should add the TPM slave device to the good i2c adapter with the +i2c_register_board_info function (Assuming that the gpio and the i2c bus are well configured). + +Note: For the beagleboard configure your kernel with the following option: CONFIG_OMAP_MUX=y + +file arch\arm\mach-omap2\board-omap3beagle.c +add the following: +----------------------------------------------------------------------- + +static struct st19np18_platform_data tpm_data = { + .accept_pin = 135, + .data_avail_pin = 143, +}; + +static struct i2c_board_info __initdata tpm_st19_i2c_board_info[] = { + { + I2C_BOARD_INFO(TPM_DRIVER_NAME, TPM_I2C_ST19_ADDR_WR), + .platform_data = &tpm_data, + }, +}; + +------------------------------------------------------------------------ +Then complete the beagleboard init to be like that: +------------------------------------------------------------------------ +static void __init omap3_beagle_init(void) +{ + omap3_mux_init(board_mux, OMAP_PACKAGE_CBB); + omap3_beagle_i2c_init(); + platform_add_devices(omap3_beagle_devices, + ARRAY_SIZE(omap3_beagle_devices)); + omap_serial_init(); + + omap_mux_init_gpio(170, OMAP_PIN_INPUT); + gpio_request(170, "DVI_nPD"); + /* REVISIT leave DVI powered down until it's needed ... */ + gpio_direction_output(170, true); + + usb_musb_init(&musb_board_data); + usb_ehci_init(&ehci_pdata); + omap3beagle_flash_init(); + + beagle_display_init(); + + /* Ensure SDRC pins are mux'd for self-refresh */ + omap_mux_init_signal("sdrc_cke0", OMAP_PIN_OUTPUT); + omap_mux_init_signal("sdrc_cke1", OMAP_PIN_OUTPUT); + omap_mux_init_gpio(((struct st19np18_platform_data *) + tpm_st19_i2c_board_info[0].platform_data)->data_avail_pin, + OMAP_PIN_INPUT); + omap_mux_init_gpio(((struct st19np18_platform_data *) + tpm_st19_i2c_board_info[0].platform_data)->accept_pin, + OMAP_PIN_INPUT); + + i2c_register_board_info(3, tpm_st19_i2c_board_info, ARRAY_SIZE(tpm_st19_i2c_board_info)); +}
drivers/char/tpm/Kconfig +9 -0
@@ -60,4 +60,13 @@ Further information on this driver and the supported hardware can be found at http://www.prosec.rub.de/tpm +config TCG_ST19_I2C + tristate "STMicroelectronics ST19 I2C TPM" + depends on I2C + depends on GPIOLIB + ---help--- + If you have a TPM security chip from STMicroelectronics working with + an I2C 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_st19_i2c. endif # TCG_TPM
drivers/char/tpm/Makefile +1 -0
@@ -9,3 +9,4 @@ obj-$(CONFIG_TCG_NSC) += tpm_nsc.o obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o +obj-$(CONFIG_TCG_ST19_I2C) += tpm_stm_st19_i2c.o
drivers/char/tpm/tpm_stm_st19_i2c.c +800 -0
800 lines not on this page · raw message
Binary files linux-2.6.35.4/drivers/char/tpm/.tpm_stm_st19_i2c.c.swp and linux-st19i2c/drivers/char/tpm/.tpm_stm_st19_i2c.c.swp differ
drivers/char/tpm/tpm_stm_st19_i2c.h +63 -0
63 lines not on this page · raw message
include/linux/i2c/tpm_stm_st19_i2c.h +45 -0
45 lines not on this page · raw message
RE: [PATCH 2/2] drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neatening
Which patches need to be applied to the tree?
Rajiv: can you please send them to me as plain text inline, against my -next tree, with your signoffs?
Quoted 280 lines · Christophe Henri RICARD
Rajiv,
Please find in attached file a full patch (driver + Joe Perches observations/patches + comments update). This patch has been tested.Thanks
Christophe-----Original Message-----
From: Rajiv Andrade [mailto:[email protected]]
Sent: Tuesday, September 14, 2010 12:11 PM
To: Joe Perches
Cc: Christophe Henri RICARD; Marcel Selhorst; Debora Velarde; James Morris; [email protected]; LKML
Subject: Re: [PATCH 2/2]drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neateningChris, did you have the chance to test it against this specific TPM? Despite this, the patch looks pretty good, thanks Joe.
Acked-by Rajiv Andrade <[email protected]>Quoted 257 lines · Joe Perches
Add missing break in
wait_until_good_shape
Use init; while (test) loops not for(init;test;) Calculate bytes to transfer using if andmin_tnot multiple ?:
Miscellaneous function whitespace aligingSigned-off-by Joe Perches <[email protected]>--- drivers/char/tpm/tpm_stm_st19_i2c.c | 158 +++++++++++++++++++---------------- 1 files changed, 85 insertions(+), 73 deletions(-)drivers/char/tpm/tpm_stm_st19_i2c.c +85
-73@@ -158,8 +158,10 @@ static int wait_until_good_shape(void)else if (!signal_pending (current)) { ret =schedule_timeout (time); wait_time += time; - }else + }else { ret = -ERESTARTSYS ; +break ; + } }while (1 );finish_wait (&queue, &__wait);@@ -204,10 +206,10 @@ static int wait_event_interruptible_on_gpio(wait_queue_head_t queue,else if (wait_time >= timeout)break ;else if (!signal_pending (current)) { - ret = -schedule_timeout (msecs_to_jiffies - (TICK_GPIO_SPOOLING )); - wait_time +=msecs_to_jiffies (TICK_GPIO_SPOOLING ); +int time =msecs_to_jiffies (TICK_GPIO_SPOOLING ); + + ret =schedule_timeout (time); + wait_time += time; }else { ret = -ERESTARTSYS ;break ;@@ -286,9 +288,9 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,/* sending data (data_avail_pin hight) */ /* If data are available, we read the data */ init_waitqueue_head (&pin_infos->write_queue); - pin =wait_event_interruptible_on_gpio (pin_infos->write_queue, -tpm_calc_ordinal_duration - (chip, ordinal)); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +tpm_calc_ordinal_duration (chip, ordinal));if (pin <0 ) { ret = pin; goto end;@@ -297,25 +299,30 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =I2C_M_RD ; size =TPM_HEADER_SIZE ; -for (i =0 ; pin ==DATA_ON && i < size;) { - ret =i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (i ==0 ) ?TPM_HEADER_SIZE : - count - i >TPM_I2C_BLOCK_SIZE ? -TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) goto end; -if (i ==0 ) - size = -responseSize (pin_infos->tpm_i2c_buffer[1 ], count); - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) { + size =responseSize (pin_infos->tpm_i2c_buffer[1 ], + count); + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); } pin = wait_event_interruptible_on_gpio(pin_infos->write_queue,@@ -325,36 +332,41 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =0 ; size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==COMMAND_ON ;) { -memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, - (i ==0 ) ?TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==COMMAND_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + +memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, bytes);if (i ==0 ) { size =responseSize (buf, count); - size = (size < count ? size : count); + size = size < count ? size : count; } - ret = -i2c_master_send (client, - pin_infos->tpm_i2c_buffer[0 ], - count >=TPM_HEADER_SIZE ? (i == -0 ) ? -TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - count - i : count); + +if (count <TPM_HEADER_SIZE ) + bytes = count; + + ret =i2c_master_send (client, pin_infos->tpm_i2c_buffer[0 ], + bytes);if (ret <0 ) {pr_info ("Failed to send data\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) + i +=TPM_HEADER_SIZE ; +else + i +=TPM_I2C_BLOCK_SIZE ;/* Wait for AcceptCmd signal hight */ if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT ));if (pin !=COMMAND_ON ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" );@@ -362,6 +374,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, goto end; } } +if (i ==0 ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ;@@ -412,49 +425,48 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,/* Spool on the good gpio as long as pin GPIO 3 not HIGHT */ init_waitqueue_head (&chip->vendor.read_queue); - pin =wait_event_interruptible_on_gpio (chip->vendor.read_queue, -tpm_calc_ordinal_duration - (chip,TPM_I2C_ORDINAL_LONG )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +tpm_calc_ordinal_duration (chip,TPM_I2C_ORDINAL_LONG )); size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==DATA_ON ;) { - ret = -i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i == -0 ?TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - size - i : count)); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (count <TPM_HEADER_SIZE ) + bytes = count; +else if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, size - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) {pr_info ("Failed to read gpio pin (DataAvailable)\n" ); goto end; } -if (buf !=NULL ) { -memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i ==0 ? -TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : size - - i : count)); - -if (i ==0 ) { - size =responseSize (buf, size); -if (size > count) - size = count; - } - }else { +if (!buf) {pr_info ("read buffer is NULL\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], size); + +if (i ==0 ) { + size =responseSize (buf, size); +if (size > count) + size = count; + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (chip->vendor. - read_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); }if (i ==0 ) {@@ -532,7 +544,7 @@ static ssize_t tpm_st19_i2c_ioctl(struct inode *inode, struct file *file,return -ENOSYS ;case TPMIOC_TRANSMIT :if (copy_from_user (pin_infos->tpm_i2c_buffer[0 ], - (const char *)arg,TPM_HEADER_SIZE )) + (const char *)arg,TPM_HEADER_SIZE ))return -EFAULT ; in_size =responseSize (pin_infos->tpm_i2c_buffer[0 ],TPM_HEADER_SIZE );
Signature · 2 lines
James Morris <[email protected]>
RE: [PATCH 2/2] drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neatening
Hi James, Rajiv,
The last one includes all the patches and some comments cleaning.
Let me know if you need some information's or have some improvements I have to do.
Thanks
Christophe
-----Original Message-----
From: James Morris [mailto:[email protected]]
Sent: Thursday, September 23, 2010 6:39 PM
To: Christophe Henri RICARD
Cc: Rajiv Andrade; Joe Perches; Marcel Selhorst; Debora Velarde; [email protected]; LKML
Subject: RE: [PATCH 2/2] drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neatening
Which patches need to be applied to the tree?
Rajiv: can you please send them to me as plain text inline, against my -next tree, with your signoffs?
Quoted 280 lines · Christophe Henri RICARD
Rajiv,
Please find in attached file a full patch (driver + Joe Perches observations/patches + comments update). This patch has been tested.Thanks
Christophe-----Original Message-----
From: Rajiv Andrade [mailto:[email protected]]
Sent: Tuesday, September 14, 2010 12:11 PM
To: Joe Perches
Cc: Christophe Henri RICARD; Marcel Selhorst; Debora Velarde; James Morris; [email protected]; LKML
Subject: Re: [PATCH 2/2]drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neateningChris, did you have the chance to test it against this specific TPM? Despite this, the patch looks pretty good, thanks Joe.
Acked-by Rajiv Andrade <[email protected]>Quoted 257 lines · Joe Perches
Add missing break in
wait_until_good_shape
Use init; while (test) loops not for(init;test;) Calculate bytes to transfer using if andmin_tnot multiple ?:
Miscellaneous function whitespace aligingSigned-off-by Joe Perches <[email protected]>--- drivers/char/tpm/tpm_stm_st19_i2c.c | 158 +++++++++++++++++++---------------- 1 files changed, 85 insertions(+), 73 deletions(-)drivers/char/tpm/tpm_stm_st19_i2c.c +85
-73@@ -158,8 +158,10 @@ static int wait_until_good_shape(void)else if (!signal_pending (current)) { ret =schedule_timeout (time); wait_time += time; - }else + }else { ret = -ERESTARTSYS ; +break ; + } }while (1 );finish_wait (&queue, &__wait);@@ -204,10 +206,10 @@ static int wait_event_interruptible_on_gpio(wait_queue_head_t queue,else if (wait_time >= timeout)break ;else if (!signal_pending (current)) { - ret = -schedule_timeout (msecs_to_jiffies - (TICK_GPIO_SPOOLING )); - wait_time +=msecs_to_jiffies (TICK_GPIO_SPOOLING ); +int time =msecs_to_jiffies (TICK_GPIO_SPOOLING ); + + ret =schedule_timeout (time); + wait_time += time; }else { ret = -ERESTARTSYS ;break ;@@ -286,9 +288,9 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,/* sending data (data_avail_pin hight) */ /* If data are available, we read the data */ init_waitqueue_head (&pin_infos->write_queue); - pin =wait_event_interruptible_on_gpio (pin_infos->write_queue, -tpm_calc_ordinal_duration - (chip, ordinal)); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +tpm_calc_ordinal_duration (chip, ordinal));if (pin <0 ) { ret = pin; goto end;@@ -297,25 +299,30 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =I2C_M_RD ; size =TPM_HEADER_SIZE ; -for (i =0 ; pin ==DATA_ON && i < size;) { - ret =i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (i ==0 ) ?TPM_HEADER_SIZE : - count - i >TPM_I2C_BLOCK_SIZE ? -TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) goto end; -if (i ==0 ) - size = -responseSize (pin_infos->tpm_i2c_buffer[1 ], count); - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) { + size =responseSize (pin_infos->tpm_i2c_buffer[1 ], + count); + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); } pin = wait_event_interruptible_on_gpio(pin_infos->write_queue,@@ -325,36 +332,41 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =0 ; size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==COMMAND_ON ;) { -memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, - (i ==0 ) ?TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==COMMAND_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + +memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, bytes);if (i ==0 ) { size =responseSize (buf, count); - size = (size < count ? size : count); + size = size < count ? size : count; } - ret = -i2c_master_send (client, - pin_infos->tpm_i2c_buffer[0 ], - count >=TPM_HEADER_SIZE ? (i == -0 ) ? -TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - count - i : count); + +if (count <TPM_HEADER_SIZE ) + bytes = count; + + ret =i2c_master_send (client, pin_infos->tpm_i2c_buffer[0 ], + bytes);if (ret <0 ) {pr_info ("Failed to send data\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) + i +=TPM_HEADER_SIZE ; +else + i +=TPM_I2C_BLOCK_SIZE ;/* Wait for AcceptCmd signal hight */ if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT ));if (pin !=COMMAND_ON ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" );@@ -362,6 +374,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, goto end; } } +if (i ==0 ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ;@@ -412,49 +425,48 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,/* Spool on the good gpio as long as pin GPIO 3 not HIGHT */ init_waitqueue_head (&chip->vendor.read_queue); - pin =wait_event_interruptible_on_gpio (chip->vendor.read_queue, -tpm_calc_ordinal_duration - (chip,TPM_I2C_ORDINAL_LONG )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +tpm_calc_ordinal_duration (chip,TPM_I2C_ORDINAL_LONG )); size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==DATA_ON ;) { - ret = -i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i == -0 ?TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - size - i : count)); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (count <TPM_HEADER_SIZE ) + bytes = count; +else if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, size - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) {pr_info ("Failed to read gpio pin (DataAvailable)\n" ); goto end; } -if (buf !=NULL ) { -memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i ==0 ? -TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : size - - i : count)); - -if (i ==0 ) { - size =responseSize (buf, size); -if (size > count) - size = count; - } - }else { +if (!buf) {pr_info ("read buffer is NULL\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], size); + +if (i ==0 ) { + size =responseSize (buf, size); +if (size > count) + size = count; + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (chip->vendor. - read_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); }if (i ==0 ) {@@ -532,7 +544,7 @@ static ssize_t tpm_st19_i2c_ioctl(struct inode *inode, struct file *file,return -ENOSYS ;case TPMIOC_TRANSMIT :if (copy_from_user (pin_infos->tpm_i2c_buffer[0 ], - (const char *)arg,TPM_HEADER_SIZE )) + (const char *)arg,TPM_HEADER_SIZE ))return -EFAULT ; in_size =responseSize (pin_infos->tpm_i2c_buffer[0 ],TPM_HEADER_SIZE );
Signature · 2 lines
James Morris <[email protected]>
Re: [PATCH 2/2] drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neatening
Hi James,
I'll send you the complete patchset against your -next tree today.
Thanks,
Rajiv
Quoted 312 lines · Christophe Henri RICARD
Hi James, Rajiv,
The last one includes all the patches and some comments cleaning.
Let me know if you need some information's or have some improvements I have to do.Thanks
Christophe-----Original Message-----
From: James Morris [mailto:[email protected]]
Sent: Thursday, September 23, 2010 6:39 PM
To: Christophe Henri RICARD
Cc: Rajiv Andrade; Joe Perches; Marcel Selhorst; Debora Velarde; [email protected]; LKML
Subject: RE: [PATCH 2/2]drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neateningWhich patches need to be applied to the tree?
Rajiv: can you please send them to me as plain text inline, against my -next tree, with your signoffs?
Quoted 280 lines · Christophe Henri RICARD
Rajiv,
Please find in attached file a full patch (driver + Joe Perches observations/patches + comments update). This patch has been tested.Thanks
Christophe-----Original Message-----
From: Rajiv Andrade [mailto:[email protected]]
Sent: Tuesday, September 14, 2010 12:11 PM
To: Joe Perches
Cc: Christophe Henri RICARD; Marcel Selhorst; Debora Velarde; James Morris; [email protected]; LKML
Subject: Re: [PATCH 2/2]drivers/char/tpm/tpm_stm_st19_i2c.c: Add missing break and neateningChris, did you have the chance to test it against this specific TPM? Despite this, the patch looks pretty good, thanks Joe.
Acked-by Rajiv Andrade <[email protected]>Quoted 257 lines · Joe Perches
Add missing break in
wait_until_good_shape
Use init; while (test) loops not for(init;test;) Calculate bytes to transfer using if andmin_tnot multiple ?:
Miscellaneous function whitespace aligingSigned-off-by Joe Perches <[email protected]>---
drivers/char/tpm/tpm_stm_st19_i2c.c| 158 +++++++++++++++++++----------------1 files changed, 85 insertions(+), 73 deletions(-)drivers/char/tpm/tpm_stm_st19_i2c.c +85
-73@@ -158,8 +158,10 @@ static int wait_until_good_shape(void)else if (!signal_pending (current)) { ret =schedule_timeout (time); wait_time += time; - }else + }else { ret = -ERESTARTSYS ; +break ; + } }while (1 );finish_wait (&queue, &__wait);@@ -204,10 +206,10 @@ static int wait_event_interruptible_on_gpio(wait_queue_head_t queue,else if (wait_time >= timeout)break ;else if (!signal_pending (current)) { - ret = -schedule_timeout (msecs_to_jiffies - (TICK_GPIO_SPOOLING )); - wait_time +=msecs_to_jiffies (TICK_GPIO_SPOOLING ); +int time =msecs_to_jiffies (TICK_GPIO_SPOOLING ); + + ret =schedule_timeout (time); + wait_time += time; }else { ret = -ERESTARTSYS ;break ;@@ -286,9 +288,9 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,/* sending data (data_avail_pin hight) */ /* If data are available, we read the data */ init_waitqueue_head (&pin_infos->write_queue); - pin =wait_event_interruptible_on_gpio (pin_infos->write_queue, -tpm_calc_ordinal_duration - (chip, ordinal)); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +tpm_calc_ordinal_duration (chip, ordinal));if (pin <0 ) { ret = pin; goto end;@@ -297,25 +299,30 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =I2C_M_RD ; size =TPM_HEADER_SIZE ; -for (i =0 ; pin ==DATA_ON && i < size;) { - ret =i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (i ==0 ) ?TPM_HEADER_SIZE : - count - i >TPM_I2C_BLOCK_SIZE ? -TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) goto end; -if (i ==0 ) - size = -responseSize (pin_infos->tpm_i2c_buffer[1 ], count); - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) { + size =responseSize (pin_infos->tpm_i2c_buffer[1 ], + count); + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); } pin = wait_event_interruptible_on_gpio(pin_infos->write_queue,@@ -325,36 +332,41 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, client->flags =0 ; size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==COMMAND_ON ;) { -memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, - (i ==0 ) ?TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : count - i); + i =0 ; +while (i < size && pin ==COMMAND_ON ) { +int bytes; + +if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, count - i,TPM_I2C_BLOCK_SIZE ); + +memcpy (pin_infos->tpm_i2c_buffer[0 ], buf + i, bytes);if (i ==0 ) { size =responseSize (buf, count); - size = (size < count ? size : count); + size = size < count ? size : count; } - ret = -i2c_master_send (client, - pin_infos->tpm_i2c_buffer[0 ], - count >=TPM_HEADER_SIZE ? (i == -0 ) ? -TPM_HEADER_SIZE : count - i > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - count - i : count); + +if (count <TPM_HEADER_SIZE ) + bytes = count; + + ret =i2c_master_send (client, pin_infos->tpm_i2c_buffer[0 ], + bytes);if (ret <0 ) {pr_info ("Failed to send data\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +if (i ==0 ) + i +=TPM_HEADER_SIZE ; +else + i +=TPM_I2C_BLOCK_SIZE ;/* Wait for AcceptCmd signal hight */ if (i < size) - pin = -wait_event_interruptible_on_gpio (pin_infos-> - write_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + pin_infos->write_queue, +msecs_to_jiffies (TPM_I2C_SHORT ));if (pin !=COMMAND_ON ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" );@@ -362,6 +374,7 @@ static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, goto end; } } +if (i ==0 ) {pr_info ("Failed to read gpio pin (AcceptCmd)\n" ); ret = -EIO ;@@ -412,49 +425,48 @@ static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,/* Spool on the good gpio as long as pin GPIO 3 not HIGHT */ init_waitqueue_head (&chip->vendor.read_queue); - pin =wait_event_interruptible_on_gpio (chip->vendor.read_queue, -tpm_calc_ordinal_duration - (chip,TPM_I2C_ORDINAL_LONG )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +tpm_calc_ordinal_duration (chip,TPM_I2C_ORDINAL_LONG )); size =TPM_HEADER_SIZE ; -for (i =0 ; i < size && pin ==DATA_ON ;) { - ret = -i2c_master_recv (client, - pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i == -0 ?TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : - size - i : count)); + i =0 ; +while (i < size && pin ==DATA_ON ) { +int bytes; + +if (count <TPM_HEADER_SIZE ) + bytes = count; +else if (i ==0 ) + bytes =TPM_HEADER_SIZE ; +else + bytes =min_t (int, size - i,TPM_I2C_BLOCK_SIZE ); + + ret =i2c_master_recv (client, pin_infos->tpm_i2c_buffer[1 ], + bytes);if (ret <0 ) {pr_info ("Failed to read gpio pin (DataAvailable)\n" ); goto end; } -if (buf !=NULL ) { -memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], - (count >=TPM_HEADER_SIZE ? i ==0 ? -TPM_HEADER_SIZE : (size - i) > -TPM_I2C_BLOCK_SIZE ?TPM_I2C_BLOCK_SIZE : size - - i : count)); - -if (i ==0 ) { - size =responseSize (buf, size); -if (size > count) - size = count; - } - }else { +if (!buf) {pr_info ("read buffer is NULL\n" ); goto end; } - (i ==0 ) ? (i +=TPM_HEADER_SIZE ) : (i +=TPM_I2C_BLOCK_SIZE ); +memcpy (buf + i, pin_infos->tpm_i2c_buffer[1 ], size); + +if (i ==0 ) { + size =responseSize (buf, size); +if (size > count) + size = count; + i +=TPM_HEADER_SIZE ; + }else + i +=TPM_I2C_BLOCK_SIZE ;if (i < size) - pin = -wait_event_interruptible_on_gpio (chip->vendor. - read_queue, -msecs_to_jiffies - (TPM_I2C_SHORT )); + pin =wait_event_interruptible_on_gpio ( + chip->vendor.read_queue, +msecs_to_jiffies (TPM_I2C_SHORT )); }if (i ==0 ) {@@ -532,7 +544,7 @@ static ssize_t tpm_st19_i2c_ioctl(struct inode *inode, struct file *file,return -ENOSYS ;case TPMIOC_TRANSMIT :if (copy_from_user (pin_infos->tpm_i2c_buffer[0 ], - (const char *)arg,TPM_HEADER_SIZE )) + (const char *)arg,TPM_HEADER_SIZE ))return -EFAULT ; in_size =responseSize (pin_infos->tpm_i2c_buffer[0 ],TPM_HEADER_SIZE );Signature · 7 lines
James Morris <[email protected]> -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/