]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/firmware/dell_rbu.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / drivers / firmware / dell_rbu.c
CommitLineData
6c54c28e
AS
1/*
2 * dell_rbu.c
3 * Bios Update driver for Dell systems
4 * Author: Dell Inc
5 * Abhay Salunke <abhay_salunke@dell.com>
6 *
7 * Copyright (C) 2005 Dell Inc.
8 *
9 * Remote BIOS Update (rbu) driver is used for updating DELL BIOS by
10 * creating entries in the /sys file systems on Linux 2.6 and higher
11 * kernels. The driver supports two mechanism to update the BIOS namely
12 * contiguous and packetized. Both these methods still require having some
13 * application to set the CMOS bit indicating the BIOS to update itself
14 * after a reboot.
15 *
16 * Contiguous method:
17 * This driver writes the incoming data in a monolithic image by allocating
18 * contiguous physical pages large enough to accommodate the incoming BIOS
19 * image size.
20 *
21 * Packetized method:
22 * The driver writes the incoming packet image by allocating a new packet
23 * on every time the packet data is written. This driver requires an
24 * application to break the BIOS image in to fixed sized packet chunks.
25 *
26 * See Documentation/dell_rbu.txt for more info.
27 *
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License v2.0 as published by
30 * the Free Software Foundation
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 */
6c54c28e
AS
37#include <linux/init.h>
38#include <linux/module.h>
5a0e3ad6 39#include <linux/slab.h>
6c54c28e
AS
40#include <linux/string.h>
41#include <linux/errno.h>
42#include <linux/blkdev.h>
d052d1be 43#include <linux/platform_device.h>
6c54c28e
AS
44#include <linux/spinlock.h>
45#include <linux/moduleparam.h>
46#include <linux/firmware.h>
47#include <linux/dma-mapping.h>
d87e8465 48#include <asm/set_memory.h>
6c54c28e
AS
49
50MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
51MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
52MODULE_LICENSE("GPL");
2c560840 53MODULE_VERSION("3.2");
6c54c28e
AS
54
55#define BIOS_SCAN_LIMIT 0xffffffff
56#define MAX_IMAGE_LENGTH 16
57static struct _rbu_data {
58 void *image_update_buffer;
59 unsigned long image_update_buffer_size;
60 unsigned long bios_image_size;
61 int image_update_ordernum;
62 int dma_alloc;
63 spinlock_t lock;
64 unsigned long packet_read_count;
6c54c28e
AS
65 unsigned long num_packets;
66 unsigned long packetsize;
ad6ce87e 67 unsigned long imagesize;
e61c0e33 68 int entry_created;
6c54c28e
AS
69} rbu_data;
70
e61c0e33
AS
71static char image_type[MAX_IMAGE_LENGTH + 1] = "mono";
72module_param_string(image_type, image_type, sizeof (image_type), 0);
ad6ce87e
AS
73MODULE_PARM_DESC(image_type,
74 "BIOS image type. choose- mono or packet or init");
6c54c28e 75
274b6933
AS
76static unsigned long allocation_floor = 0x100000;
77module_param(allocation_floor, ulong, 0644);
78MODULE_PARM_DESC(allocation_floor,
79 "Minimum address for allocations when using Packet mode");
80
6c54c28e
AS
81struct packet_data {
82 struct list_head list;
83 size_t length;
84 void *data;
85 int ordernum;
86};
87
88static struct packet_data packet_data_head;
89
90static struct platform_device *rbu_device;
91static int context;
92static dma_addr_t dell_rbu_dmaaddr;
93
dda8577f 94static void init_packet_head(void)
6c54c28e
AS
95{
96 INIT_LIST_HEAD(&packet_data_head.list);
6c54c28e
AS
97 rbu_data.packet_read_count = 0;
98 rbu_data.num_packets = 0;
99 rbu_data.packetsize = 0;
ad6ce87e 100 rbu_data.imagesize = 0;
6c54c28e
AS
101}
102
ad6ce87e 103static int create_packet(void *data, size_t length)
6c54c28e
AS
104{
105 struct packet_data *newpacket;
106 int ordernum = 0;
274b6933
AS
107 int retval = 0;
108 unsigned int packet_array_size = 0;
5ad9201b
AV
109 void **invalid_addr_packet_array = NULL;
110 void *packet_data_temp_buf = NULL;
274b6933 111 unsigned int idx = 0;
6c54c28e
AS
112
113 pr_debug("create_packet: entry \n");
114
115 if (!rbu_data.packetsize) {
116 pr_debug("create_packet: packetsize not specified\n");
274b6933
AS
117 retval = -EINVAL;
118 goto out_noalloc;
6c54c28e 119 }
274b6933 120
e61c0e33 121 spin_unlock(&rbu_data.lock);
274b6933
AS
122
123 newpacket = kzalloc(sizeof (struct packet_data), GFP_KERNEL);
6c54c28e 124
6c54c28e
AS
125 if (!newpacket) {
126 printk(KERN_WARNING
e61c0e33 127 "dell_rbu:%s: failed to allocate new "
eecd5853 128 "packet\n", __func__);
274b6933
AS
129 retval = -ENOMEM;
130 spin_lock(&rbu_data.lock);
131 goto out_noalloc;
6c54c28e
AS
132 }
133
134 ordernum = get_order(length);
274b6933 135
6c54c28e 136 /*
274b6933
AS
137 * BIOS errata mean we cannot allocate packets below 1MB or they will
138 * be overwritten by BIOS.
139 *
140 * array to temporarily hold packets
141 * that are below the allocation floor
142 *
143 * NOTE: very simplistic because we only need the floor to be at 1MB
144 * due to BIOS errata. This shouldn't be used for higher floors
145 * or you will run out of mem trying to allocate the array.
6c54c28e 146 */
274b6933
AS
147 packet_array_size = max(
148 (unsigned int)(allocation_floor / rbu_data.packetsize),
149 (unsigned int)1);
150 invalid_addr_packet_array = kzalloc(packet_array_size * sizeof(void*),
151 GFP_KERNEL);
6c54c28e 152
274b6933 153 if (!invalid_addr_packet_array) {
6c54c28e 154 printk(KERN_WARNING
274b6933
AS
155 "dell_rbu:%s: failed to allocate "
156 "invalid_addr_packet_array \n",
eecd5853 157 __func__);
274b6933
AS
158 retval = -ENOMEM;
159 spin_lock(&rbu_data.lock);
160 goto out_alloc_packet;
6c54c28e
AS
161 }
162
274b6933
AS
163 while (!packet_data_temp_buf) {
164 packet_data_temp_buf = (unsigned char *)
165 __get_free_pages(GFP_KERNEL, ordernum);
166 if (!packet_data_temp_buf) {
167 printk(KERN_WARNING
168 "dell_rbu:%s: failed to allocate new "
eecd5853 169 "packet\n", __func__);
274b6933
AS
170 retval = -ENOMEM;
171 spin_lock(&rbu_data.lock);
172 goto out_alloc_packet_array;
173 }
174
175 if ((unsigned long)virt_to_phys(packet_data_temp_buf)
176 < allocation_floor) {
177 pr_debug("packet 0x%lx below floor at 0x%lx.\n",
178 (unsigned long)virt_to_phys(
179 packet_data_temp_buf),
180 allocation_floor);
181 invalid_addr_packet_array[idx++] = packet_data_temp_buf;
5ad9201b 182 packet_data_temp_buf = NULL;
274b6933
AS
183 }
184 }
d87e8465
SH
185 /*
186 * set to uncachable or it may never get written back before reboot
187 */
188 set_memory_uc((unsigned long)packet_data_temp_buf, 1 << ordernum);
189
274b6933
AS
190 spin_lock(&rbu_data.lock);
191
192 newpacket->data = packet_data_temp_buf;
193
194 pr_debug("create_packet: newpacket at physical addr %lx\n",
195 (unsigned long)virt_to_phys(newpacket->data));
196
197 /* packets may not have fixed size */
198 newpacket->length = length;
6c54c28e
AS
199 newpacket->ordernum = ordernum;
200 ++rbu_data.num_packets;
274b6933
AS
201
202 /* initialize the newly created packet headers */
6c54c28e
AS
203 INIT_LIST_HEAD(&newpacket->list);
204 list_add_tail(&newpacket->list, &packet_data_head.list);
ad6ce87e
AS
205
206 memcpy(newpacket->data, data, length);
6c54c28e
AS
207
208 pr_debug("create_packet: exit \n");
209
274b6933
AS
210out_alloc_packet_array:
211 /* always free packet array */
212 for (;idx>0;idx--) {
213 pr_debug("freeing unused packet below floor 0x%lx.\n",
214 (unsigned long)virt_to_phys(
215 invalid_addr_packet_array[idx-1]));
216 free_pages((unsigned long)invalid_addr_packet_array[idx-1],
217 ordernum);
218 }
219 kfree(invalid_addr_packet_array);
220
221out_alloc_packet:
222 /* if error, free data */
223 if (retval)
224 kfree(newpacket);
225
226out_noalloc:
227 return retval;
6c54c28e
AS
228}
229
c6c1c94e 230static int packetize_data(const u8 *data, size_t length)
6c54c28e
AS
231{
232 int rc = 0;
ad6ce87e
AS
233 int done = 0;
234 int packet_length;
235 u8 *temp;
236 u8 *end = (u8 *) data + length;
cb7cf57a 237 pr_debug("packetize_data: data length %zd\n", length);
ad6ce87e
AS
238 if (!rbu_data.packetsize) {
239 printk(KERN_WARNING
240 "dell_rbu: packetsize not specified\n");
241 return -EIO;
242 }
6c54c28e 243
ad6ce87e
AS
244 temp = (u8 *) data;
245
246 /* packetize the hunk */
247 while (!done) {
248 if ((temp + rbu_data.packetsize) < end)
249 packet_length = rbu_data.packetsize;
250 else {
251 /* this is the last packet */
252 packet_length = end - temp;
253 done = 1;
254 }
255
256 if ((rc = create_packet(temp, packet_length)))
6c54c28e 257 return rc;
ad6ce87e 258
9e42ef77 259 pr_debug("%p:%td\n", temp, (end - temp));
ad6ce87e 260 temp += packet_length;
6c54c28e 261 }
ad6ce87e
AS
262
263 rbu_data.imagesize = length;
6c54c28e
AS
264
265 return rc;
266}
267
dda8577f 268static int do_packet_read(char *data, struct list_head *ptemp_list,
e61c0e33 269 int length, int bytes_read, int *list_read_count)
6c54c28e
AS
270{
271 void *ptemp_buf;
272 struct packet_data *newpacket = NULL;
273 int bytes_copied = 0;
274 int j = 0;
275
276 newpacket = list_entry(ptemp_list, struct packet_data, list);
277 *list_read_count += newpacket->length;
278
279 if (*list_read_count > bytes_read) {
280 /* point to the start of unread data */
281 j = newpacket->length - (*list_read_count - bytes_read);
282 /* point to the offset in the packet buffer */
283 ptemp_buf = (u8 *) newpacket->data + j;
284 /*
285 * check if there is enough room in
286 * * the incoming buffer
287 */
288 if (length > (*list_read_count - bytes_read))
289 /*
290 * copy what ever is there in this
291 * packet and move on
292 */
293 bytes_copied = (*list_read_count - bytes_read);
294 else
295 /* copy the remaining */
296 bytes_copied = length;
297 memcpy(data, ptemp_buf, bytes_copied);
298 }
299 return bytes_copied;
300}
301
ad6ce87e 302static int packet_read_list(char *data, size_t * pread_length)
6c54c28e
AS
303{
304 struct list_head *ptemp_list;
305 int temp_count = 0;
306 int bytes_copied = 0;
307 int bytes_read = 0;
308 int remaining_bytes = 0;
309 char *pdest = data;
310
311 /* check if we have any packets */
312 if (0 == rbu_data.num_packets)
313 return -ENOMEM;
314
315 remaining_bytes = *pread_length;
316 bytes_read = rbu_data.packet_read_count;
317
318 ptemp_list = (&packet_data_head.list)->next;
319 while (!list_empty(ptemp_list)) {
320 bytes_copied = do_packet_read(pdest, ptemp_list,
e61c0e33 321 remaining_bytes, bytes_read, &temp_count);
6c54c28e
AS
322 remaining_bytes -= bytes_copied;
323 bytes_read += bytes_copied;
324 pdest += bytes_copied;
325 /*
326 * check if we reached end of buffer before reaching the
327 * last packet
328 */
329 if (remaining_bytes == 0)
330 break;
331
332 ptemp_list = ptemp_list->next;
333 }
334 /*finally set the bytes read */
335 *pread_length = bytes_read - rbu_data.packet_read_count;
336 rbu_data.packet_read_count = bytes_read;
337 return 0;
338}
339
dda8577f 340static void packet_empty_list(void)
6c54c28e
AS
341{
342 struct list_head *ptemp_list;
343 struct list_head *pnext_list;
344 struct packet_data *newpacket;
345
346 ptemp_list = (&packet_data_head.list)->next;
347 while (!list_empty(ptemp_list)) {
348 newpacket =
e61c0e33 349 list_entry(ptemp_list, struct packet_data, list);
6c54c28e
AS
350 pnext_list = ptemp_list->next;
351 list_del(ptemp_list);
352 ptemp_list = pnext_list;
353 /*
354 * zero out the RBU packet memory before freeing
355 * to make sure there are no stale RBU packets left in memory
356 */
357 memset(newpacket->data, 0, rbu_data.packetsize);
d87e8465
SH
358 set_memory_wb((unsigned long)newpacket->data,
359 1 << newpacket->ordernum);
e61c0e33
AS
360 free_pages((unsigned long) newpacket->data,
361 newpacket->ordernum);
6c54c28e
AS
362 kfree(newpacket);
363 }
6c54c28e
AS
364 rbu_data.packet_read_count = 0;
365 rbu_data.num_packets = 0;
ad6ce87e 366 rbu_data.imagesize = 0;
6c54c28e
AS
367}
368
369/*
370 * img_update_free: Frees the buffer allocated for storing BIOS image
371 * Always called with lock held and returned with lock held
372 */
dda8577f 373static void img_update_free(void)
6c54c28e
AS
374{
375 if (!rbu_data.image_update_buffer)
376 return;
377 /*
378 * zero out this buffer before freeing it to get rid of any stale
379 * BIOS image copied in memory.
380 */
381 memset(rbu_data.image_update_buffer, 0,
e61c0e33 382 rbu_data.image_update_buffer_size);
6c54c28e
AS
383 if (rbu_data.dma_alloc == 1)
384 dma_free_coherent(NULL, rbu_data.bios_image_size,
e61c0e33 385 rbu_data.image_update_buffer, dell_rbu_dmaaddr);
6c54c28e 386 else
e61c0e33
AS
387 free_pages((unsigned long) rbu_data.image_update_buffer,
388 rbu_data.image_update_ordernum);
6c54c28e
AS
389
390 /*
391 * Re-initialize the rbu_data variables after a free
392 */
393 rbu_data.image_update_ordernum = -1;
394 rbu_data.image_update_buffer = NULL;
395 rbu_data.image_update_buffer_size = 0;
396 rbu_data.bios_image_size = 0;
397 rbu_data.dma_alloc = 0;
398}
399
400/*
401 * img_update_realloc: This function allocates the contiguous pages to
402 * accommodate the requested size of data. The memory address and size
403 * values are stored globally and on every call to this function the new
404 * size is checked to see if more data is required than the existing size.
405 * If true the previous memory is freed and new allocation is done to
406 * accommodate the new size. If the incoming size is less then than the
407 * already allocated size, then that memory is reused. This function is
408 * called with lock held and returns with lock held.
409 */
dda8577f 410static int img_update_realloc(unsigned long size)
6c54c28e
AS
411{
412 unsigned char *image_update_buffer = NULL;
413 unsigned long rc;
414 unsigned long img_buf_phys_addr;
415 int ordernum;
416 int dma_alloc = 0;
417
418 /*
419 * check if the buffer of sufficient size has been
420 * already allocated
421 */
422 if (rbu_data.image_update_buffer_size >= size) {
423 /*
424 * check for corruption
425 */
426 if ((size != 0) && (rbu_data.image_update_buffer == NULL)) {
427 printk(KERN_ERR "dell_rbu:%s: corruption "
eecd5853 428 "check failed\n", __func__);
6c54c28e
AS
429 return -EINVAL;
430 }
431 /*
432 * we have a valid pre-allocated buffer with
433 * sufficient size
434 */
435 return 0;
436 }
437
438 /*
439 * free any previously allocated buffer
440 */
441 img_update_free();
442
443 spin_unlock(&rbu_data.lock);
444
445 ordernum = get_order(size);
446 image_update_buffer =
e61c0e33 447 (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum);
6c54c28e
AS
448
449 img_buf_phys_addr =
e61c0e33 450 (unsigned long) virt_to_phys(image_update_buffer);
6c54c28e
AS
451
452 if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
e61c0e33 453 free_pages((unsigned long) image_update_buffer, ordernum);
6c54c28e
AS
454 ordernum = -1;
455 image_update_buffer = dma_alloc_coherent(NULL, size,
e61c0e33 456 &dell_rbu_dmaaddr, GFP_KERNEL);
6c54c28e
AS
457 dma_alloc = 1;
458 }
459
460 spin_lock(&rbu_data.lock);
461
462 if (image_update_buffer != NULL) {
463 rbu_data.image_update_buffer = image_update_buffer;
464 rbu_data.image_update_buffer_size = size;
465 rbu_data.bios_image_size =
e61c0e33 466 rbu_data.image_update_buffer_size;
6c54c28e
AS
467 rbu_data.image_update_ordernum = ordernum;
468 rbu_data.dma_alloc = dma_alloc;
469 rc = 0;
470 } else {
471 pr_debug("Not enough memory for image update:"
e61c0e33 472 "size = %ld\n", size);
6c54c28e
AS
473 rc = -ENOMEM;
474 }
475
476 return rc;
477}
478
dda8577f 479static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
6c54c28e
AS
480{
481 int retval;
482 size_t bytes_left;
483 size_t data_length;
484 char *ptempBuf = buffer;
6c54c28e
AS
485
486 /* check to see if we have something to return */
487 if (rbu_data.num_packets == 0) {
488 pr_debug("read_packet_data: no packets written\n");
489 retval = -ENOMEM;
490 goto read_rbu_data_exit;
491 }
492
ad6ce87e 493 if (pos > rbu_data.imagesize) {
6c54c28e
AS
494 retval = 0;
495 printk(KERN_WARNING "dell_rbu:read_packet_data: "
e61c0e33 496 "data underrun\n");
6c54c28e
AS
497 goto read_rbu_data_exit;
498 }
499
ad6ce87e 500 bytes_left = rbu_data.imagesize - pos;
6c54c28e
AS
501 data_length = min(bytes_left, count);
502
503 if ((retval = packet_read_list(ptempBuf, &data_length)) < 0)
504 goto read_rbu_data_exit;
505
ad6ce87e 506 if ((pos + count) > rbu_data.imagesize) {
6c54c28e
AS
507 rbu_data.packet_read_count = 0;
508 /* this was the last copy */
509 retval = bytes_left;
510 } else
511 retval = count;
512
513 read_rbu_data_exit:
514 return retval;
515}
516
dda8577f 517static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
6c54c28e 518{
6c54c28e
AS
519 /* check to see if we have something to return */
520 if ((rbu_data.image_update_buffer == NULL) ||
e61c0e33 521 (rbu_data.bios_image_size == 0)) {
6c54c28e 522 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
e61c0e33
AS
523 "bios_image_size %lu\n",
524 rbu_data.image_update_buffer,
525 rbu_data.bios_image_size);
25377479 526 return -ENOMEM;
6c54c28e
AS
527 }
528
25377479
AM
529 return memory_read_from_buffer(buffer, count, &pos,
530 rbu_data.image_update_buffer, rbu_data.bios_image_size);
6c54c28e
AS
531}
532
2c3c8bea 533static ssize_t read_rbu_data(struct file *filp, struct kobject *kobj,
91a69029
ZR
534 struct bin_attribute *bin_attr,
535 char *buffer, loff_t pos, size_t count)
6c54c28e
AS
536{
537 ssize_t ret_count = 0;
538
539 spin_lock(&rbu_data.lock);
540
541 if (!strcmp(image_type, "mono"))
542 ret_count = read_rbu_mono_data(buffer, pos, count);
543 else if (!strcmp(image_type, "packet"))
544 ret_count = read_packet_data(buffer, pos, count);
545 else
546 pr_debug("read_rbu_data: invalid image type specified\n");
547
548 spin_unlock(&rbu_data.lock);
549 return ret_count;
550}
551
dda8577f 552static void callbackfn_rbu(const struct firmware *fw, void *context)
e61c0e33 553{
2c560840 554 rbu_data.entry_created = 0;
e61c0e33 555
9ebfbd45 556 if (!fw)
e61c0e33 557 return;
e61c0e33 558
9ebfbd45
JB
559 if (!fw->size)
560 goto out;
561
e61c0e33
AS
562 spin_lock(&rbu_data.lock);
563 if (!strcmp(image_type, "mono")) {
564 if (!img_update_realloc(fw->size))
565 memcpy(rbu_data.image_update_buffer,
566 fw->data, fw->size);
567 } else if (!strcmp(image_type, "packet")) {
ad6ce87e
AS
568 /*
569 * we need to free previous packets if a
570 * new hunk of packets needs to be downloaded
571 */
572 packet_empty_list();
573 if (packetize_data(fw->data, fw->size))
574 /* Incase something goes wrong when we are
575 * in middle of packetizing the data, we
576 * need to free up whatever packets might
577 * have been created before we quit.
578 */
e61c0e33 579 packet_empty_list();
e61c0e33
AS
580 } else
581 pr_debug("invalid image type specified.\n");
582 spin_unlock(&rbu_data.lock);
9ebfbd45
JB
583 out:
584 release_firmware(fw);
e61c0e33
AS
585}
586
2c3c8bea 587static ssize_t read_rbu_image_type(struct file *filp, struct kobject *kobj,
91a69029
ZR
588 struct bin_attribute *bin_attr,
589 char *buffer, loff_t pos, size_t count)
6c54c28e
AS
590{
591 int size = 0;
592 if (!pos)
81156928 593 size = scnprintf(buffer, count, "%s\n", image_type);
6c54c28e
AS
594 return size;
595}
596
2c3c8bea 597static ssize_t write_rbu_image_type(struct file *filp, struct kobject *kobj,
91a69029
ZR
598 struct bin_attribute *bin_attr,
599 char *buffer, loff_t pos, size_t count)
6c54c28e
AS
600{
601 int rc = count;
e61c0e33
AS
602 int req_firm_rc = 0;
603 int i;
6c54c28e 604 spin_lock(&rbu_data.lock);
e61c0e33
AS
605 /*
606 * Find the first newline or space
607 */
608 for (i = 0; i < count; ++i)
609 if (buffer[i] == '\n' || buffer[i] == ' ') {
610 buffer[i] = '\0';
611 break;
612 }
613 if (i == count)
614 buffer[count] = '\0';
615
616 if (strstr(buffer, "mono"))
617 strcpy(image_type, "mono");
618 else if (strstr(buffer, "packet"))
619 strcpy(image_type, "packet");
620 else if (strstr(buffer, "init")) {
621 /*
622 * If due to the user error the driver gets in a bad
623 * state where even though it is loaded , the
624 * /sys/class/firmware/dell_rbu entries are missing.
625 * to cover this situation the user can recreate entries
626 * by writing init to image_type.
627 */
628 if (!rbu_data.entry_created) {
629 spin_unlock(&rbu_data.lock);
630 req_firm_rc = request_firmware_nowait(THIS_MODULE,
631 FW_ACTION_NOHOTPLUG, "dell_rbu",
9ebfbd45 632 &rbu_device->dev, GFP_KERNEL, &context,
e61c0e33
AS
633 callbackfn_rbu);
634 if (req_firm_rc) {
635 printk(KERN_ERR
636 "dell_rbu:%s request_firmware_nowait"
eecd5853 637 " failed %d\n", __func__, rc);
e61c0e33
AS
638 rc = -EIO;
639 } else
640 rbu_data.entry_created = 1;
641
642 spin_lock(&rbu_data.lock);
643 }
644 } else {
645 printk(KERN_WARNING "dell_rbu: image_type is invalid\n");
646 spin_unlock(&rbu_data.lock);
647 return -EINVAL;
648 }
6c54c28e
AS
649
650 /* we must free all previous allocations */
651 packet_empty_list();
652 img_update_free();
6c54c28e 653 spin_unlock(&rbu_data.lock);
6c54c28e 654
e61c0e33 655 return rc;
6c54c28e
AS
656}
657
2c3c8bea 658static ssize_t read_rbu_packet_size(struct file *filp, struct kobject *kobj,
91a69029
ZR
659 struct bin_attribute *bin_attr,
660 char *buffer, loff_t pos, size_t count)
ad6ce87e
AS
661{
662 int size = 0;
663 if (!pos) {
664 spin_lock(&rbu_data.lock);
81156928 665 size = scnprintf(buffer, count, "%lu\n", rbu_data.packetsize);
ad6ce87e
AS
666 spin_unlock(&rbu_data.lock);
667 }
668 return size;
669}
670
2c3c8bea 671static ssize_t write_rbu_packet_size(struct file *filp, struct kobject *kobj,
91a69029
ZR
672 struct bin_attribute *bin_attr,
673 char *buffer, loff_t pos, size_t count)
ad6ce87e
AS
674{
675 unsigned long temp;
676 spin_lock(&rbu_data.lock);
677 packet_empty_list();
678 sscanf(buffer, "%lu", &temp);
679 if (temp < 0xffffffff)
680 rbu_data.packetsize = temp;
681
682 spin_unlock(&rbu_data.lock);
683 return count;
684}
685
6c54c28e 686static struct bin_attribute rbu_data_attr = {
7b595756 687 .attr = {.name = "data", .mode = 0444},
6c54c28e
AS
688 .read = read_rbu_data,
689};
690
691static struct bin_attribute rbu_image_type_attr = {
7b595756 692 .attr = {.name = "image_type", .mode = 0644},
6c54c28e
AS
693 .read = read_rbu_image_type,
694 .write = write_rbu_image_type,
695};
696
ad6ce87e 697static struct bin_attribute rbu_packet_size_attr = {
7b595756 698 .attr = {.name = "packet_size", .mode = 0644},
ad6ce87e
AS
699 .read = read_rbu_packet_size,
700 .write = write_rbu_packet_size,
701};
702
dda8577f 703static int __init dcdrbu_init(void)
6c54c28e 704{
6897083a 705 int rc;
6c54c28e
AS
706 spin_lock_init(&rbu_data.lock);
707
708 init_packet_head();
6897083a
AM
709 rbu_device = platform_device_register_simple("dell_rbu", -1, NULL, 0);
710 if (IS_ERR(rbu_device)) {
6c54c28e 711 printk(KERN_ERR
e61c0e33 712 "dell_rbu:%s:platform_device_register_simple "
eecd5853 713 "failed\n", __func__);
6897083a 714 return PTR_ERR(rbu_device);
6c54c28e
AS
715 }
716
41bfcfd9
JG
717 rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
718 if (rc)
719 goto out_devreg;
720 rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
721 if (rc)
722 goto out_data;
723 rc = sysfs_create_bin_file(&rbu_device->dev.kobj,
ad6ce87e 724 &rbu_packet_size_attr);
41bfcfd9
JG
725 if (rc)
726 goto out_imtype;
6c54c28e 727
2c560840 728 rbu_data.entry_created = 0;
41bfcfd9 729 return 0;
6c54c28e 730
41bfcfd9
JG
731out_imtype:
732 sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
733out_data:
734 sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
735out_devreg:
736 platform_device_unregister(rbu_device);
737 return rc;
6c54c28e
AS
738}
739
dda8577f 740static __exit void dcdrbu_exit(void)
6c54c28e
AS
741{
742 spin_lock(&rbu_data.lock);
743 packet_empty_list();
744 img_update_free();
745 spin_unlock(&rbu_data.lock);
746 platform_device_unregister(rbu_device);
747}
748
749module_exit(dcdrbu_exit);
750module_init(dcdrbu_init);
274b6933
AS
751
752/* vim:noet:ts=8:sw=8
753*/