2 * Load Analog Devices SigmaStudio firmware files
4 * Copyright 2009-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/crc32.h>
10 #include <linux/delay.h>
11 #include <linux/firmware.h>
12 #include <linux/kernel.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
18 static size_t sigma_action_size(struct sigma_action
*sa
)
23 case SIGMA_ACTION_WRITEXBYTES
:
24 case SIGMA_ACTION_WRITESINGLE
:
25 case SIGMA_ACTION_WRITESAFELOAD
:
26 payload
= sigma_action_len(sa
);
32 payload
= ALIGN(payload
, 2);
34 return payload
+ sizeof(struct sigma_action
);
38 * Returns a negative error value in case of an error, 0 if processing of
39 * the firmware should be stopped after this action, 1 otherwise.
42 process_sigma_action(struct i2c_client
*client
, struct sigma_action
*sa
)
44 size_t len
= sigma_action_len(sa
);
47 pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__
,
48 sa
->instr
, sa
->addr
, len
);
51 case SIGMA_ACTION_WRITEXBYTES
:
52 case SIGMA_ACTION_WRITESINGLE
:
53 case SIGMA_ACTION_WRITESAFELOAD
:
54 ret
= i2c_master_send(client
, (void *)&sa
->addr
, len
);
58 case SIGMA_ACTION_DELAY
:
62 case SIGMA_ACTION_END
:
72 process_sigma_actions(struct i2c_client
*client
, struct sigma_firmware
*ssfw
)
74 struct sigma_action
*sa
;
78 while (ssfw
->pos
+ sizeof(*sa
) <= ssfw
->fw
->size
) {
79 sa
= (struct sigma_action
*)(ssfw
->fw
->data
+ ssfw
->pos
);
81 size
= sigma_action_size(sa
);
83 if (ssfw
->pos
> ssfw
->fw
->size
|| size
== 0)
86 ret
= process_sigma_action(client
, sa
);
88 pr_debug("%s: action returned %i\n", __func__
, ret
);
94 if (ssfw
->pos
!= ssfw
->fw
->size
)
100 int process_sigma_firmware(struct i2c_client
*client
, const char *name
)
103 struct sigma_firmware_header
*ssfw_head
;
104 struct sigma_firmware ssfw
;
105 const struct firmware
*fw
;
108 pr_debug("%s: loading firmware %s\n", __func__
, name
);
110 /* first load the blob */
111 ret
= request_firmware(&fw
, name
, &client
->dev
);
113 pr_debug("%s: request_firmware() failed with %i\n", __func__
, ret
);
118 /* then verify the header */
122 * Reject too small or unreasonable large files. The upper limit has been
123 * chosen a bit arbitrarily, but it should be enough for all practical
124 * purposes and having the limit makes it easier to avoid integer
125 * overflows later in the loading process.
127 if (fw
->size
< sizeof(*ssfw_head
) || fw
->size
>= 0x4000000)
130 ssfw_head
= (void *)fw
->data
;
131 if (memcmp(ssfw_head
->magic
, SIGMA_MAGIC
, ARRAY_SIZE(ssfw_head
->magic
)))
134 crc
= crc32(0, fw
->data
+ sizeof(*ssfw_head
),
135 fw
->size
- sizeof(*ssfw_head
));
136 pr_debug("%s: crc=%x\n", __func__
, crc
);
137 if (crc
!= le32_to_cpu(ssfw_head
->crc
))
140 ssfw
.pos
= sizeof(*ssfw_head
);
142 /* finally process all of the actions */
143 ret
= process_sigma_actions(client
, &ssfw
);
146 release_firmware(fw
);
148 pr_debug("%s: loaded %s\n", __func__
, name
);
152 EXPORT_SYMBOL(process_sigma_firmware
);
154 MODULE_LICENSE("GPL");