blob: 36265de0a9e8acad8ac9e45c5e49ed8c00dd98e9 [file] [log] [blame]
Mike Frysingere359dc22011-03-22 16:34:40 -07001/*
2 * Load Analog Devices SigmaStudio firmware files
3 *
4 * Copyright 2009-2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
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>
Randy Dunlap27c46a22011-07-25 17:13:21 -070014#include <linux/module.h>
Mike Frysingere359dc22011-03-22 16:34:40 -070015#include <linux/sigma.h>
16
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +010017static size_t sigma_action_size(struct sigma_action *sa)
Mike Frysingere359dc22011-03-22 16:34:40 -070018{
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +010019 size_t payload = 0;
20
21 switch (sa->instr) {
22 case SIGMA_ACTION_WRITEXBYTES:
23 case SIGMA_ACTION_WRITESINGLE:
24 case SIGMA_ACTION_WRITESAFELOAD:
25 payload = sigma_action_len(sa);
26 break;
27 default:
28 break;
29 }
30
31 payload = ALIGN(payload, 2);
32
33 return payload + sizeof(struct sigma_action);
34}
35
36/*
37 * Returns a negative error value in case of an error, 0 if processing of
38 * the firmware should be stopped after this action, 1 otherwise.
39 */
40static int
41process_sigma_action(struct i2c_client *client, struct sigma_action *sa)
42{
Mike Frysingere359dc22011-03-22 16:34:40 -070043 size_t len = sigma_action_len(sa);
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +010044 int ret;
Mike Frysingere359dc22011-03-22 16:34:40 -070045
46 pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
47 sa->instr, sa->addr, len);
48
49 switch (sa->instr) {
50 case SIGMA_ACTION_WRITEXBYTES:
51 case SIGMA_ACTION_WRITESINGLE:
52 case SIGMA_ACTION_WRITESAFELOAD:
Mike Frysingere359dc22011-03-22 16:34:40 -070053 ret = i2c_master_send(client, (void *)&sa->addr, len);
54 if (ret < 0)
55 return -EINVAL;
56 break;
Mike Frysingere359dc22011-03-22 16:34:40 -070057 case SIGMA_ACTION_DELAY:
Mike Frysingere359dc22011-03-22 16:34:40 -070058 udelay(len);
59 len = 0;
60 break;
Mike Frysingere359dc22011-03-22 16:34:40 -070061 case SIGMA_ACTION_END:
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +010062 return 0;
Mike Frysingere359dc22011-03-22 16:34:40 -070063 default:
64 return -EINVAL;
65 }
66
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +010067 return 1;
Mike Frysingere359dc22011-03-22 16:34:40 -070068}
69
70static int
71process_sigma_actions(struct i2c_client *client, struct sigma_firmware *ssfw)
72{
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +010073 struct sigma_action *sa;
74 size_t size;
75 int ret;
Mike Frysingere359dc22011-03-22 16:34:40 -070076
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +010077 while (ssfw->pos + sizeof(*sa) <= ssfw->fw->size) {
78 sa = (struct sigma_action *)(ssfw->fw->data + ssfw->pos);
79
80 size = sigma_action_size(sa);
81 ssfw->pos += size;
82 if (ssfw->pos > ssfw->fw->size || size == 0)
83 break;
84
85 ret = process_sigma_action(client, sa);
86
Mike Frysingere359dc22011-03-22 16:34:40 -070087 pr_debug("%s: action returned %i\n", __func__, ret);
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +010088
89 if (ret <= 0)
Mike Frysingere359dc22011-03-22 16:34:40 -070090 return ret;
91 }
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +010092
93 if (ssfw->pos != ssfw->fw->size)
94 return -EINVAL;
95
96 return 0;
Mike Frysingere359dc22011-03-22 16:34:40 -070097}
98
99int process_sigma_firmware(struct i2c_client *client, const char *name)
100{
101 int ret;
102 struct sigma_firmware_header *ssfw_head;
103 struct sigma_firmware ssfw;
104 const struct firmware *fw;
105 u32 crc;
106
107 pr_debug("%s: loading firmware %s\n", __func__, name);
108
109 /* first load the blob */
110 ret = request_firmware(&fw, name, &client->dev);
111 if (ret) {
112 pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
113 return ret;
114 }
115 ssfw.fw = fw;
116
117 /* then verify the header */
118 ret = -EINVAL;
Lars-Peter Clausen4f718a22011-11-28 09:44:14 +0100119
120 /*
121 * Reject too small or unreasonable large files. The upper limit has been
122 * chosen a bit arbitrarily, but it should be enough for all practical
123 * purposes and having the limit makes it easier to avoid integer
124 * overflows later in the loading process.
125 */
126 if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000)
Mike Frysingere359dc22011-03-22 16:34:40 -0700127 goto done;
128
129 ssfw_head = (void *)fw->data;
130 if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic)))
131 goto done;
132
Lars-Peter Clausenc56935b2011-11-28 09:44:15 +0100133 crc = crc32(0, fw->data + sizeof(*ssfw_head),
134 fw->size - sizeof(*ssfw_head));
Mike Frysingere359dc22011-03-22 16:34:40 -0700135 pr_debug("%s: crc=%x\n", __func__, crc);
136 if (crc != ssfw_head->crc)
137 goto done;
138
139 ssfw.pos = sizeof(*ssfw_head);
140
141 /* finally process all of the actions */
142 ret = process_sigma_actions(client, &ssfw);
143
144 done:
145 release_firmware(fw);
146
147 pr_debug("%s: loaded %s\n", __func__, name);
148
149 return ret;
150}
151EXPORT_SYMBOL(process_sigma_firmware);
Randy Dunlap27c46a22011-07-25 17:13:21 -0700152
153MODULE_LICENSE("GPL");