Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Input: adc-joystick - move axes data into the main structure

There is no need to allocate axes information separately from the main
joystick structure so let's fold the allocation and also drop members
(such as range, flat and fuzz) that are only used during initialization
of the device.

Acked-by: Artur Rojek <contact@artur-rojek.eu>
Link: https://lore.kernel.org/r/ZmkrgTlxNwm_oHxv@google.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+61 -53
+61 -53
drivers/input/joystick/adc-joystick.c
··· 15 15 16 16 struct adc_joystick_axis { 17 17 u32 code; 18 - s32 range[2]; 19 - s32 fuzz; 20 - s32 flat; 21 18 bool inverted; 22 19 }; 23 20 24 21 struct adc_joystick { 25 22 struct input_dev *input; 26 23 struct iio_cb_buffer *buffer; 27 - struct adc_joystick_axis *axes; 28 24 struct iio_channel *chans; 29 - int num_chans; 30 - bool polled; 25 + unsigned int num_chans; 26 + struct adc_joystick_axis axes[] __counted_by(num_chans); 31 27 }; 32 28 33 29 static int adc_joystick_invert(struct input_dev *dev, ··· 131 135 132 136 static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy) 133 137 { 134 - struct adc_joystick_axis *axes; 138 + struct adc_joystick_axis *axes = joy->axes; 135 139 struct fwnode_handle *child; 136 - int num_axes, error, i; 140 + s32 range[2], fuzz, flat; 141 + unsigned int num_axes; 142 + int error, i; 137 143 138 144 num_axes = device_get_child_node_count(dev); 139 145 if (!num_axes) { ··· 148 150 num_axes, joy->num_chans); 149 151 return -EINVAL; 150 152 } 151 - 152 - axes = devm_kmalloc_array(dev, num_axes, sizeof(*axes), GFP_KERNEL); 153 - if (!axes) 154 - return -ENOMEM; 155 153 156 154 device_for_each_child_node(dev, child) { 157 155 error = fwnode_property_read_u32(child, "reg", &i); ··· 170 176 } 171 177 172 178 error = fwnode_property_read_u32_array(child, "abs-range", 173 - axes[i].range, 2); 179 + range, 2); 174 180 if (error) { 175 181 dev_err(dev, "abs-range invalid or missing\n"); 176 182 goto err_fwnode_put; 177 183 } 178 184 179 - if (axes[i].range[0] > axes[i].range[1]) { 185 + if (range[0] > range[1]) { 180 186 dev_dbg(dev, "abs-axis %d inverted\n", i); 181 187 axes[i].inverted = true; 182 - swap(axes[i].range[0], axes[i].range[1]); 188 + swap(range[0], range[1]); 183 189 } 184 190 185 - fwnode_property_read_u32(child, "abs-fuzz", &axes[i].fuzz); 186 - fwnode_property_read_u32(child, "abs-flat", &axes[i].flat); 191 + fwnode_property_read_u32(child, "abs-fuzz", &fuzz); 192 + fwnode_property_read_u32(child, "abs-flat", &flat); 187 193 188 194 input_set_abs_params(joy->input, axes[i].code, 189 - axes[i].range[0], axes[i].range[1], 190 - axes[i].fuzz, axes[i].flat); 191 - input_set_capability(joy->input, EV_ABS, axes[i].code); 195 + range[0], range[1], fuzz, flat); 192 196 } 193 - 194 - joy->axes = axes; 195 197 196 198 return 0; 197 199 ··· 196 206 return error; 197 207 } 198 208 209 + 210 + static int adc_joystick_count_channels(struct device *dev, 211 + const struct iio_channel *chans, 212 + bool polled, 213 + unsigned int *num_chans) 214 + { 215 + int bits; 216 + int i; 217 + 218 + /* 219 + * Count how many channels we got. NULL terminated. 220 + * Do not check the storage size if using polling. 221 + */ 222 + for (i = 0; chans[i].indio_dev; i++) { 223 + if (polled) 224 + continue; 225 + bits = chans[i].channel->scan_type.storagebits; 226 + if (!bits || bits > 16) { 227 + dev_err(dev, "Unsupported channel storage size\n"); 228 + return -EINVAL; 229 + } 230 + if (bits != chans[0].channel->scan_type.storagebits) { 231 + dev_err(dev, "Channels must have equal storage size\n"); 232 + return -EINVAL; 233 + } 234 + } 235 + 236 + *num_chans = i; 237 + return 0; 238 + } 239 + 199 240 static int adc_joystick_probe(struct platform_device *pdev) 200 241 { 201 242 struct device *dev = &pdev->dev; 243 + struct iio_channel *chans; 202 244 struct adc_joystick *joy; 203 245 struct input_dev *input; 246 + unsigned int poll_interval = 0; 247 + unsigned int num_chans; 204 248 int error; 205 - int bits; 206 - int i; 207 - unsigned int poll_interval; 208 249 209 - joy = devm_kzalloc(dev, sizeof(*joy), GFP_KERNEL); 210 - if (!joy) 211 - return -ENOMEM; 212 - 213 - joy->chans = devm_iio_channel_get_all(dev); 214 - if (IS_ERR(joy->chans)) { 215 - error = PTR_ERR(joy->chans); 250 + chans = devm_iio_channel_get_all(dev); 251 + error = PTR_ERR_OR_ZERO(chans); 252 + if (error) { 216 253 if (error != -EPROBE_DEFER) 217 254 dev_err(dev, "Unable to get IIO channels"); 218 255 return error; ··· 253 236 } else if (poll_interval == 0) { 254 237 dev_err(dev, "Unable to get poll-interval\n"); 255 238 return -EINVAL; 256 - } else { 257 - joy->polled = true; 258 239 } 259 240 260 - /* 261 - * Count how many channels we got. NULL terminated. 262 - * Do not check the storage size if using polling. 263 - */ 264 - for (i = 0; joy->chans[i].indio_dev; i++) { 265 - if (joy->polled) 266 - continue; 267 - bits = joy->chans[i].channel->scan_type.storagebits; 268 - if (!bits || bits > 16) { 269 - dev_err(dev, "Unsupported channel storage size\n"); 270 - return -EINVAL; 271 - } 272 - if (bits != joy->chans[0].channel->scan_type.storagebits) { 273 - dev_err(dev, "Channels must have equal storage size\n"); 274 - return -EINVAL; 275 - } 276 - } 277 - joy->num_chans = i; 241 + error = adc_joystick_count_channels(dev, chans, poll_interval != 0, 242 + &num_chans); 243 + if (error) 244 + return error; 245 + 246 + joy = devm_kzalloc(dev, struct_size(joy, axes, num_chans), GFP_KERNEL); 247 + if (!joy) 248 + return -ENOMEM; 249 + 250 + joy->chans = chans; 251 + joy->num_chans = num_chans; 278 252 279 253 input = devm_input_allocate_device(dev); 280 254 if (!input) { ··· 281 273 if (error) 282 274 return error; 283 275 284 - if (joy->polled) { 276 + if (poll_interval != 0) { 285 277 input_setup_polling(input, adc_joystick_poll); 286 278 input_set_poll_interval(input, poll_interval); 287 279 } else {