usb: storage: Mark various arrays as const
Subsystems (2)
v2 cover usb: storage: Mark various arrays as const
While reading code, I noticed that some arrays in USB mass storage drivers are declared static but not const, even though they are not modified. This patchset marks them const.
All patches were compile-tested.
---
Changes in v2:
- Add new patches 2-9
- Use consistent authorship information
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Jonathan Neuschäfer (9):
usb: storage: jumpshot: Use const for constant arrays
usb: storage: transport: Use const for constant array
usb: storage: alauda: Use const for card ID array
usb: storage: datafab: Use const for constant arrays
usb: storage: initializers: Use const for constant array
usb: storage: realtek_cr: Use const for constant arrays
usb: storage: sddr09: Use const for constant arrays
usb: storage: sddr55: Use const for constant arrays
usb: storage: shuttle_usbat: Use const for constant array
drivers/usb/storage/alauda.c | 8 ++++---- drivers/usb/storage/datafab.c | 14 +++++++------- drivers/usb/storage/initializers.c | 2 +- drivers/usb/storage/jumpshot.c | 10 +++++----- drivers/usb/storage/realtek_cr.c | 6 +++--- drivers/usb/storage/sddr09.c | 14 +++++++------- drivers/usb/storage/sddr55.c | 4 ++-- drivers/usb/storage/shuttle_usbat.c | 2 +- drivers/usb/storage/transport.c | 2 +- 9 files changed, 31 insertions(+), 31 deletions(-)
---
base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b
change-id: 20240401-misc-const-e7b4cf20d5f9
Best regards,
Signature · 1 line
Jonathan Neuschäfer <[email protected]>
PATCH v2 1/9 usb: storage: jumpshot: Use const for constant arrays
These arrays are not modified. Make them const.
---
V2:
- use consistent authorship information
--- drivers/usb/storage/jumpshot.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/usb/storage/jumpshot.c +5 -5
@@ -367,16 +367,16 @@ static int jumpshot_handle_mode_sense(struct us_data *us, struct scsi_cmnd * srb, int sense_6) { -static unsigned char rw_err_page[12 ] = { +static const unsigned char rw_err_page[12 ] = {0x1 ,0xA ,0x21 ,1 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 }; -static unsigned char cache_page[12 ] = { +static const unsigned char cache_page[12 ] = {0x8 ,0xA ,0x1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; -static unsigned char rbac_page[12 ] = { +static const unsigned char rbac_page[12 ] = {0x1B ,0xA ,0 ,0x81 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; -static unsigned char timer_page[8 ] = { +static const unsigned char timer_page[8 ] = {0x1C ,0x6 ,0 ,0 ,0 ,0 };unsigned char pc, page_code;
@@ -477,7 +477,7 @@ static int jumpshot_transport(struct scsi_cmnd *srb, struct us_data *us)int rc;unsigned long block, blocks;unsigned char *ptr = us->iobuf; -static unsigned char inquiry_response[8 ] = { +static const unsigned char inquiry_response[8 ] = {0x00 ,0x80 ,0x00 ,0x01 ,0x1F ,0x00 ,0x00 ,0x00 };
Signature · 1 line
2.48.0.rc1.219.gb6b6757d772
PATCH v2 2/9 usb: storage: transport: Use const for constant array
In reply to Jonathan NeuschäferThis array is only read, not modified
---
V2:
- new patch
--- drivers/usb/storage/transport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/transport.c +1 -1
@@ -528,7 +528,7 @@ static void last_sector_hacks(struct us_data *us, struct scsi_cmnd *srb)u32 sector;/* To Report "Medium Error: Record Not Found */ -static unsigned char record_not_found[18 ] = { +static const unsigned char record_not_found[18 ] = { [0 ] =0x70 ,/* current error */ [2 ] =MEDIUM_ERROR ,/* = 0x03 */ [7 ] =0x0a ,/* additional length */
Signature · 1 line
2.48.0.rc1.219.gb6b6757d772
PATCH v2 3/9 usb: storage: alauda: Use const for card ID array
In reply to Jonathan NeuschäferThe alauda_card_ids array is only read, and not modified.
---
V2:
- new patch
--- drivers/usb/storage/alauda.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/usb/storage/alauda.c +4 -4
@@ -174,7 +174,7 @@ struct alauda_card_info {unsigned char zoneshift;/* 1<<zs blocks per zone */ }; -static struct alauda_card_info alauda_card_ids[] = { +static const struct alauda_card_info alauda_card_ids[] = {/* NAND flash */ {0x6e ,20 ,8 ,4 ,8 },/* 1 MB */ {0xe8 ,20 ,8 ,4 ,8 },/* 1 MB */
@@ -200,7 +200,7 @@ static struct alauda_card_info alauda_card_ids[] = { { 0,} }; -static struct alauda_card_info *alauda_card_find_id (unsigned char id) +static const struct alauda_card_info *alauda_card_find_id (unsigned char id) {int i;
@@ -383,7 +383,7 @@ static int alauda_init_media(struct us_data *us) {unsigned char *data = us->iobuf;int ready =0 ; -struct alauda_card_info *media_info; +const struct alauda_card_info *media_info;unsigned int num_zones;while (ready ==0 ) {
@@ -1132,7 +1132,7 @@ static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)int rc;struct alauda_info *info = (struct alauda_info *) us->extra;unsigned char *ptr = us->iobuf; -static unsigned char inquiry_response[36 ] = { +static const unsigned char inquiry_response[36 ] = {0x00 ,0x80 ,0x00 ,0x01 ,0x1F ,0x00 ,0x00 ,0x00 };
Signature · 1 line
2.48.0.rc1.219.gb6b6757d772
PATCH v2 4/9 usb: storage: datafab: Use const for constant arrays
In reply to Jonathan NeuschäferThese arrays are only read, not modified.
---
V2:
- new patch
--- drivers/usb/storage/datafab.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/usb/storage/datafab.c +7 -7
@@ -319,7 +319,7 @@ static int datafab_determine_lun(struct us_data *us,// // There might be a better way of doing this? -static unsigned char scommand[8 ] = {0 ,1 ,0 ,0 ,0 ,0xa0 ,0xec ,1 }; +static const unsigned char scommand[8 ] = {0 ,1 ,0 ,0 ,0 ,0xa0 ,0xec ,1 };unsigned char *command = us->iobuf;unsigned char *buf;int count =0 , rc;
@@ -384,7 +384,7 @@ static int datafab_id_device(struct us_data *us,// to the ATA spec, 'Sector Count' isn't used but the Windows driver // sets this bit so we do too... // -static unsigned char scommand[8 ] = {0 ,1 ,0 ,0 ,0 ,0xa0 ,0xec ,1 }; +static const unsigned char scommand[8 ] = {0 ,1 ,0 ,0 ,0 ,0xa0 ,0xec ,1 };unsigned char *command = us->iobuf;unsigned char *reply;int rc;
@@ -437,16 +437,16 @@ static int datafab_handle_mode_sense(struct us_data *us, struct scsi_cmnd * srb, int sense_6) { -static unsigned char rw_err_page[12 ] = { +static const unsigned char rw_err_page[12 ] = {0x1 ,0xA ,0x21 ,1 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 }; -static unsigned char cache_page[12 ] = { +static const unsigned char cache_page[12 ] = {0x8 ,0xA ,0x1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; -static unsigned char rbac_page[12 ] = { +static const unsigned char rbac_page[12 ] = {0x1B ,0xA ,0 ,0x81 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; -static unsigned char timer_page[8 ] = { +static const unsigned char timer_page[8 ] = {0x1C ,0x6 ,0 ,0 ,0 ,0 };unsigned char pc, page_code;
@@ -550,7 +550,7 @@ static int datafab_transport(struct scsi_cmnd *srb, struct us_data *us)int rc;unsigned long block, blocks;unsigned char *ptr = us->iobuf; -static unsigned char inquiry_reply[8 ] = { +static const unsigned char inquiry_reply[8 ] = {0x00 ,0x80 ,0x00 ,0x01 ,0x1F ,0x00 ,0x00 ,0x00 };
Signature · 1 line
2.48.0.rc1.219.gb6b6757d772
PATCH v2 5/9 usb: storage: initializers: Use const for constant array
In reply to Jonathan Neuschäferinit_string is only read, not modified.
---
V2:
- new patch
--- drivers/usb/storage/initializers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/initializers.c +1 -1
@@ -54,7 +54,7 @@ int usb_stor_ucr61s2b_init(struct us_data *us)struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;int res;unsigned int partial; -static char init_string[] ="\xec\x0a\x06\x00$PCCHIPS" ; +static const char init_string[] ="\xec\x0a\x06\x00$PCCHIPS" ;usb_stor_dbg (us,"Sending UCR-61S2B initialization packet...\n" );
Signature · 1 line
2.48.0.rc1.219.gb6b6757d772
PATCH v2 6/9 usb: storage: realtek_cr: Use const for constant arrays
In reply to Jonathan NeuschäferThese arrays are only read, never modified.
---
V2:
- new patch
--- drivers/usb/storage/realtek_cr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/usb/storage/realtek_cr.c +3 -3
@@ -191,7 +191,7 @@ MODULE_DEVICE_TABLE(usb, realtek_cr_ids); .initFunction = init_function, \ } -static struct us_unusual_dev realtek_cr_unusual_dev_list[] = { +static const struct us_unusual_dev realtek_cr_unusual_dev_list[] = { # include "unusual_realtek.h" {}/* Terminating entry */ };
@@ -797,10 +797,10 @@ static void rts51x_invoke_transport(struct scsi_cmnd *srb, struct us_data *us) {struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);static int card_first_show =1 ; -static u8 media_not_present[] = {0x70 ,0 ,0x02 ,0 ,0 ,0 ,0 , +static const u8 media_not_present[] = {0x70 ,0 ,0x02 ,0 ,0 ,0 ,0 ,10 ,0 ,0 ,0 ,0 ,0x3A ,0 ,0 ,0 ,0 ,0 }; -static u8 invalid_cmd_field[] = {0x70 ,0 ,0x05 ,0 ,0 ,0 ,0 , +static const u8 invalid_cmd_field[] = {0x70 ,0 ,0x05 ,0 ,0 ,0 ,0 ,10 ,0 ,0 ,0 ,0 ,0x24 ,0 ,0 ,0 ,0 ,0 };int ret;
Signature · 1 line
2.48.0.rc1.219.gb6b6757d772
PATCH v2 7/9 usb: storage: sddr09: Use const for constant arrays
In reply to Jonathan NeuschäferThe nand_flash_ids, inquiry_response, and mode_page_01 arrays are only read, not modified.
---
V2:
- new patch
--- drivers/usb/storage/sddr09.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/usb/storage/sddr09.c +7 -7
@@ -144,7 +144,7 @@ static inline char *nand_flash_manufacturer(int manuf_id) {* 256 MB NAND flash has a 5-byte ID with 2nd byte 0xaa, 0xba, 0xca or 0xda. */ -static struct nand_flash_dev nand_flash_ids[] = { +static const struct nand_flash_dev nand_flash_ids[] = {/* NAND flash */ {0x6e ,20 ,8 ,4 ,8 ,2 },/* 1 MB */ {0xe8 ,20 ,8 ,4 ,8 ,2 },/* 1 MB */
@@ -169,7 +169,7 @@ static struct nand_flash_dev nand_flash_ids[] = { { 0,} }; -static struct nand_flash_dev * +static const struct nand_flash_dev *nand_find_id (unsigned char id) {int i;
@@ -1133,9 +1133,9 @@ sddr09_reset(struct us_data *us) { }#endif -static struct nand_flash_dev * +static const struct nand_flash_dev *sddr09_get_cardinfo (struct us_data *us,unsigned char flags) { -struct nand_flash_dev *cardinfo; +const struct nand_flash_dev *cardinfo;unsigned char deviceID[4 ];char blurbtxt[256 ];int result;
@@ -1545,12 +1545,12 @@ static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us)struct sddr09_card_info *info; -static unsigned char inquiry_response[8 ] = { +static const unsigned char inquiry_response[8 ] = {0x00 ,0x80 ,0x00 ,0x02 ,0x1F ,0x00 ,0x00 ,0x00 };/* note: no block descriptor support */ -static unsigned char mode_page_01[19 ] = { +static const unsigned char mode_page_01[19 ] = {0x00 ,0x0F ,0x00 ,0x0 ,0x0 ,0x0 ,0x00 ,0x01 ,0x0A ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
@@ -1584,7 +1584,7 @@ static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us) }if (srb->cmnd[0 ] ==READ_CAPACITY ) { -struct nand_flash_dev *cardinfo; +const struct nand_flash_dev *cardinfo;sddr09_get_wp (us, info);/* read WP bit */
Signature · 1 line
2.48.0.rc1.219.gb6b6757d772
PATCH v2 8/9 usb: storage: sddr55: Use const for constant arrays
In reply to Jonathan NeuschäferThese arrays are only read, not modified.
---
V2:
- new patch
--- drivers/usb/storage/sddr55.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/storage/sddr55.c +2 -2
@@ -775,11 +775,11 @@ static void sddr55_card_info_destructor(void *extra) {static int sddr55_transport (struct scsi_cmnd *srb,struct us_data *us) {int result; -static unsigned char inquiry_response[8 ] = { +static const unsigned char inquiry_response[8 ] = {0x00 ,0x80 ,0x00 ,0x02 ,0x1F ,0x00 ,0x00 ,0x00 };// write-protected for now, no block descriptor support -static unsigned char mode_page_01[20 ] = { +static const unsigned char mode_page_01[20 ] = {0x0 ,0x12 ,0x00 ,0x80 ,0x0 ,0x0 ,0x0 ,0x0 ,0x01 ,0x0A ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
Signature · 1 line
2.48.0.rc1.219.gb6b6757d772
PATCH v2 9/9 usb: storage: shuttle_usbat: Use const for constant array
In reply to Jonathan NeuschäferThis array is only read, not modified.
---
V2:
- new patch
--- drivers/usb/storage/shuttle_usbat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/shuttle_usbat.c +1 -1
@@ -1683,7 +1683,7 @@ static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)struct usbat_info *info = (struct usbat_info *) (us->extra);unsigned long block, blocks;unsigned char *ptr = us->iobuf; -static unsigned char inquiry_response[36 ] = { +static const unsigned char inquiry_response[36 ] = {0x00 ,0x80 ,0x00 ,0x01 ,0x1F ,0x00 ,0x00 ,0x00 };
Signature · 1 line
2.48.0.rc1.219.gb6b6757d772
Alan Stern
maintainerQuoted 4 lines · On Wed, Feb 26, 2025 at 03:17:22PM +0100, 'Jonathan Neuschäfer via B4 Relay'…
While reading code, I noticed that some arrays in USB mass storage drivers are declared static but not const, even though they are not modified. This patchset marks them const.
Quoted 8 lines
All patches were compile-tested.
Signed-off-by Jonathan Neuschäfer <[email protected]>---
Changes in v2:
- Add new patches 2-9
- Use consistent authorship information
- Link to v1: https://lore.kernel.org/r/[email protected]
The patches themselves look good, but I still think you should explain in the patch descriptions why declaring these arrays const is worth doing.
Merely saying _what_ you are doing isn't good enough. We can tell what a patch does just by reading it. What we can't always tell is _why_ you would want to do it. That is what needs to be explained.
The explanation doesn't have to be terribly long or detailed, but you should not omit it entirely.
Alan Stern
Quoted 18 lines · Alan Stern
Quoted 4 lines · On Wed, Feb 26, 2025 at 03:17:22PM +0100, 'Jonathan Neuschäfer via B4 Relay'…
While reading code, I noticed that some arrays in USB mass storage drivers are declared static but not const, even though they are not modified. This patchset marks them const.
Quoted 8 lines
All patches were compile-tested.
Signed-off-by Jonathan Neuschäfer <[email protected]>---
Changes in v2:
- Add new patches 2-9
- Use consistent authorship information
- Link to v1: https://lore.kernel.org/r/[email protected]The patches themselves look good, but I still think you should explain in the patch descriptions why declaring these arrays const is worth doing.
Quoted 6 lines
Merely saying _what_ you are doing isn't good enough. We can tell what a patch does just by reading it. What we can't always tell is _why_ you would want to do it. That is what needs to be explained.
The explanation doesn't have to be terribly long or detailed, but you should not omit it entirely.
Fair enough, I'll add such explanations to the patches.
Roughly, my motivations are:
- Moving data to read-only memory can prevent unintended modifications and the hard-to-debug issue that might follow
- Const makes it easier for human readers to know what to expect
Best regards