blob: 125929c9048f0be47b4e59543d79a5b62a0e0ff6 [file] [log] [blame]
Abhay Salunke6c54c282005-09-06 15:17:14 -07001/*
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 */
37#include <linux/version.h>
38#include <linux/config.h>
39#include <linux/init.h>
40#include <linux/module.h>
41#include <linux/string.h>
42#include <linux/errno.h>
43#include <linux/blkdev.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010044#include <linux/platform_device.h>
Abhay Salunke6c54c282005-09-06 15:17:14 -070045#include <linux/spinlock.h>
46#include <linux/moduleparam.h>
47#include <linux/firmware.h>
48#include <linux/dma-mapping.h>
49
50MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
51MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
52MODULE_LICENSE("GPL");
Abhay Salunkead6ce872005-10-11 08:29:02 -070053MODULE_VERSION("3.0");
Abhay Salunke6c54c282005-09-06 15:17:14 -070054
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;
Abhay Salunke6c54c282005-09-06 15:17:14 -070065 unsigned long num_packets;
66 unsigned long packetsize;
Abhay Salunkead6ce872005-10-11 08:29:02 -070067 unsigned long imagesize;
Abhay Salunkee61c0e32005-09-16 19:28:04 -070068 int entry_created;
Abhay Salunke6c54c282005-09-06 15:17:14 -070069} rbu_data;
70
Abhay Salunkee61c0e32005-09-16 19:28:04 -070071static char image_type[MAX_IMAGE_LENGTH + 1] = "mono";
72module_param_string(image_type, image_type, sizeof (image_type), 0);
Abhay Salunkead6ce872005-10-11 08:29:02 -070073MODULE_PARM_DESC(image_type,
74 "BIOS image type. choose- mono or packet or init");
Abhay Salunke6c54c282005-09-06 15:17:14 -070075
76struct packet_data {
77 struct list_head list;
78 size_t length;
79 void *data;
80 int ordernum;
81};
82
83static struct packet_data packet_data_head;
84
85static struct platform_device *rbu_device;
86static int context;
87static dma_addr_t dell_rbu_dmaaddr;
88
Andrew Mortondda85772005-09-16 19:28:05 -070089static void init_packet_head(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -070090{
91 INIT_LIST_HEAD(&packet_data_head.list);
Abhay Salunke6c54c282005-09-06 15:17:14 -070092 rbu_data.packet_read_count = 0;
93 rbu_data.num_packets = 0;
94 rbu_data.packetsize = 0;
Abhay Salunkead6ce872005-10-11 08:29:02 -070095 rbu_data.imagesize = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -070096}
97
Abhay Salunkead6ce872005-10-11 08:29:02 -070098static int create_packet(void *data, size_t length)
Abhay Salunke6c54c282005-09-06 15:17:14 -070099{
100 struct packet_data *newpacket;
101 int ordernum = 0;
102
103 pr_debug("create_packet: entry \n");
104
105 if (!rbu_data.packetsize) {
106 pr_debug("create_packet: packetsize not specified\n");
107 return -EINVAL;
108 }
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700109 spin_unlock(&rbu_data.lock);
110 newpacket = kmalloc(sizeof (struct packet_data), GFP_KERNEL);
111 spin_lock(&rbu_data.lock);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700112
Abhay Salunke6c54c282005-09-06 15:17:14 -0700113 if (!newpacket) {
114 printk(KERN_WARNING
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700115 "dell_rbu:%s: failed to allocate new "
116 "packet\n", __FUNCTION__);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700117 return -ENOMEM;
118 }
119
120 ordernum = get_order(length);
121 /*
122 * there is no upper limit on memory
123 * address for packetized mechanism
124 */
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700125 spin_unlock(&rbu_data.lock);
126 newpacket->data = (unsigned char *) __get_free_pages(GFP_KERNEL,
127 ordernum);
128 spin_lock(&rbu_data.lock);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700129
130 pr_debug("create_packet: newpacket %p\n", newpacket->data);
131
132 if (!newpacket->data) {
133 printk(KERN_WARNING
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700134 "dell_rbu:%s: failed to allocate new "
135 "packet\n", __FUNCTION__);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700136 kfree(newpacket);
137 return -ENOMEM;
138 }
139
140 newpacket->ordernum = ordernum;
141 ++rbu_data.num_packets;
142 /*
143 * initialize the newly created packet headers
144 */
145 INIT_LIST_HEAD(&newpacket->list);
146 list_add_tail(&newpacket->list, &packet_data_head.list);
147 /*
Abhay Salunkead6ce872005-10-11 08:29:02 -0700148 * packets may not have fixed size
Abhay Salunke6c54c282005-09-06 15:17:14 -0700149 */
Abhay Salunkead6ce872005-10-11 08:29:02 -0700150 newpacket->length = length;
151
152 memcpy(newpacket->data, data, length);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700153
154 pr_debug("create_packet: exit \n");
155
156 return 0;
157}
158
Andrew Mortondda85772005-09-16 19:28:05 -0700159static int packetize_data(void *data, size_t length)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700160{
161 int rc = 0;
Abhay Salunkead6ce872005-10-11 08:29:02 -0700162 int done = 0;
163 int packet_length;
164 u8 *temp;
165 u8 *end = (u8 *) data + length;
166 pr_debug("packetize_data: data length %d\n", length);
167 if (!rbu_data.packetsize) {
168 printk(KERN_WARNING
169 "dell_rbu: packetsize not specified\n");
170 return -EIO;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700171 }
Abhay Salunkead6ce872005-10-11 08:29:02 -0700172
173 temp = (u8 *) data;
174
175 /* packetize the hunk */
176 while (!done) {
177 if ((temp + rbu_data.packetsize) < end)
178 packet_length = rbu_data.packetsize;
179 else {
180 /* this is the last packet */
181 packet_length = end - temp;
182 done = 1;
183 }
184
185 if ((rc = create_packet(temp, packet_length)))
186 return rc;
187
188 pr_debug("%lu:%lu\n", temp, (end - temp));
189 temp += packet_length;
190 }
191
192 rbu_data.imagesize = length;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700193
194 return rc;
195}
196
Andrew Mortondda85772005-09-16 19:28:05 -0700197static int do_packet_read(char *data, struct list_head *ptemp_list,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700198 int length, int bytes_read, int *list_read_count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700199{
200 void *ptemp_buf;
201 struct packet_data *newpacket = NULL;
202 int bytes_copied = 0;
203 int j = 0;
204
205 newpacket = list_entry(ptemp_list, struct packet_data, list);
206 *list_read_count += newpacket->length;
207
208 if (*list_read_count > bytes_read) {
209 /* point to the start of unread data */
210 j = newpacket->length - (*list_read_count - bytes_read);
211 /* point to the offset in the packet buffer */
212 ptemp_buf = (u8 *) newpacket->data + j;
213 /*
214 * check if there is enough room in
215 * * the incoming buffer
216 */
217 if (length > (*list_read_count - bytes_read))
218 /*
219 * copy what ever is there in this
220 * packet and move on
221 */
222 bytes_copied = (*list_read_count - bytes_read);
223 else
224 /* copy the remaining */
225 bytes_copied = length;
226 memcpy(data, ptemp_buf, bytes_copied);
227 }
228 return bytes_copied;
229}
230
Abhay Salunkead6ce872005-10-11 08:29:02 -0700231static int packet_read_list(char *data, size_t * pread_length)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700232{
233 struct list_head *ptemp_list;
234 int temp_count = 0;
235 int bytes_copied = 0;
236 int bytes_read = 0;
237 int remaining_bytes = 0;
238 char *pdest = data;
239
240 /* check if we have any packets */
241 if (0 == rbu_data.num_packets)
242 return -ENOMEM;
243
244 remaining_bytes = *pread_length;
245 bytes_read = rbu_data.packet_read_count;
246
247 ptemp_list = (&packet_data_head.list)->next;
248 while (!list_empty(ptemp_list)) {
249 bytes_copied = do_packet_read(pdest, ptemp_list,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700250 remaining_bytes, bytes_read, &temp_count);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700251 remaining_bytes -= bytes_copied;
252 bytes_read += bytes_copied;
253 pdest += bytes_copied;
254 /*
255 * check if we reached end of buffer before reaching the
256 * last packet
257 */
258 if (remaining_bytes == 0)
259 break;
260
261 ptemp_list = ptemp_list->next;
262 }
263 /*finally set the bytes read */
264 *pread_length = bytes_read - rbu_data.packet_read_count;
265 rbu_data.packet_read_count = bytes_read;
266 return 0;
267}
268
Andrew Mortondda85772005-09-16 19:28:05 -0700269static void packet_empty_list(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700270{
271 struct list_head *ptemp_list;
272 struct list_head *pnext_list;
273 struct packet_data *newpacket;
274
275 ptemp_list = (&packet_data_head.list)->next;
276 while (!list_empty(ptemp_list)) {
277 newpacket =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700278 list_entry(ptemp_list, struct packet_data, list);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700279 pnext_list = ptemp_list->next;
280 list_del(ptemp_list);
281 ptemp_list = pnext_list;
282 /*
283 * zero out the RBU packet memory before freeing
284 * to make sure there are no stale RBU packets left in memory
285 */
286 memset(newpacket->data, 0, rbu_data.packetsize);
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700287 free_pages((unsigned long) newpacket->data,
288 newpacket->ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700289 kfree(newpacket);
290 }
Abhay Salunke6c54c282005-09-06 15:17:14 -0700291 rbu_data.packet_read_count = 0;
292 rbu_data.num_packets = 0;
Abhay Salunkead6ce872005-10-11 08:29:02 -0700293 rbu_data.imagesize = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700294}
295
296/*
297 * img_update_free: Frees the buffer allocated for storing BIOS image
298 * Always called with lock held and returned with lock held
299 */
Andrew Mortondda85772005-09-16 19:28:05 -0700300static void img_update_free(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700301{
302 if (!rbu_data.image_update_buffer)
303 return;
304 /*
305 * zero out this buffer before freeing it to get rid of any stale
306 * BIOS image copied in memory.
307 */
308 memset(rbu_data.image_update_buffer, 0,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700309 rbu_data.image_update_buffer_size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700310 if (rbu_data.dma_alloc == 1)
311 dma_free_coherent(NULL, rbu_data.bios_image_size,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700312 rbu_data.image_update_buffer, dell_rbu_dmaaddr);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700313 else
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700314 free_pages((unsigned long) rbu_data.image_update_buffer,
315 rbu_data.image_update_ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700316
317 /*
318 * Re-initialize the rbu_data variables after a free
319 */
320 rbu_data.image_update_ordernum = -1;
321 rbu_data.image_update_buffer = NULL;
322 rbu_data.image_update_buffer_size = 0;
323 rbu_data.bios_image_size = 0;
324 rbu_data.dma_alloc = 0;
325}
326
327/*
328 * img_update_realloc: This function allocates the contiguous pages to
329 * accommodate the requested size of data. The memory address and size
330 * values are stored globally and on every call to this function the new
331 * size is checked to see if more data is required than the existing size.
332 * If true the previous memory is freed and new allocation is done to
333 * accommodate the new size. If the incoming size is less then than the
334 * already allocated size, then that memory is reused. This function is
335 * called with lock held and returns with lock held.
336 */
Andrew Mortondda85772005-09-16 19:28:05 -0700337static int img_update_realloc(unsigned long size)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700338{
339 unsigned char *image_update_buffer = NULL;
340 unsigned long rc;
341 unsigned long img_buf_phys_addr;
342 int ordernum;
343 int dma_alloc = 0;
344
345 /*
346 * check if the buffer of sufficient size has been
347 * already allocated
348 */
349 if (rbu_data.image_update_buffer_size >= size) {
350 /*
351 * check for corruption
352 */
353 if ((size != 0) && (rbu_data.image_update_buffer == NULL)) {
354 printk(KERN_ERR "dell_rbu:%s: corruption "
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700355 "check failed\n", __FUNCTION__);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700356 return -EINVAL;
357 }
358 /*
359 * we have a valid pre-allocated buffer with
360 * sufficient size
361 */
362 return 0;
363 }
364
365 /*
366 * free any previously allocated buffer
367 */
368 img_update_free();
369
370 spin_unlock(&rbu_data.lock);
371
372 ordernum = get_order(size);
373 image_update_buffer =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700374 (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700375
376 img_buf_phys_addr =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700377 (unsigned long) virt_to_phys(image_update_buffer);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700378
379 if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700380 free_pages((unsigned long) image_update_buffer, ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700381 ordernum = -1;
382 image_update_buffer = dma_alloc_coherent(NULL, size,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700383 &dell_rbu_dmaaddr, GFP_KERNEL);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700384 dma_alloc = 1;
385 }
386
387 spin_lock(&rbu_data.lock);
388
389 if (image_update_buffer != NULL) {
390 rbu_data.image_update_buffer = image_update_buffer;
391 rbu_data.image_update_buffer_size = size;
392 rbu_data.bios_image_size =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700393 rbu_data.image_update_buffer_size;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700394 rbu_data.image_update_ordernum = ordernum;
395 rbu_data.dma_alloc = dma_alloc;
396 rc = 0;
397 } else {
398 pr_debug("Not enough memory for image update:"
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700399 "size = %ld\n", size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700400 rc = -ENOMEM;
401 }
402
403 return rc;
404}
405
Andrew Mortondda85772005-09-16 19:28:05 -0700406static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700407{
408 int retval;
409 size_t bytes_left;
410 size_t data_length;
411 char *ptempBuf = buffer;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700412
413 /* check to see if we have something to return */
414 if (rbu_data.num_packets == 0) {
415 pr_debug("read_packet_data: no packets written\n");
416 retval = -ENOMEM;
417 goto read_rbu_data_exit;
418 }
419
Abhay Salunkead6ce872005-10-11 08:29:02 -0700420 if (pos > rbu_data.imagesize) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700421 retval = 0;
422 printk(KERN_WARNING "dell_rbu:read_packet_data: "
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700423 "data underrun\n");
Abhay Salunke6c54c282005-09-06 15:17:14 -0700424 goto read_rbu_data_exit;
425 }
426
Abhay Salunkead6ce872005-10-11 08:29:02 -0700427 bytes_left = rbu_data.imagesize - pos;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700428 data_length = min(bytes_left, count);
429
430 if ((retval = packet_read_list(ptempBuf, &data_length)) < 0)
431 goto read_rbu_data_exit;
432
Abhay Salunkead6ce872005-10-11 08:29:02 -0700433 if ((pos + count) > rbu_data.imagesize) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700434 rbu_data.packet_read_count = 0;
435 /* this was the last copy */
436 retval = bytes_left;
437 } else
438 retval = count;
439
440 read_rbu_data_exit:
441 return retval;
442}
443
Andrew Mortondda85772005-09-16 19:28:05 -0700444static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700445{
446 unsigned char *ptemp = NULL;
447 size_t bytes_left = 0;
448 size_t data_length = 0;
449 ssize_t ret_count = 0;
450
451 /* check to see if we have something to return */
452 if ((rbu_data.image_update_buffer == NULL) ||
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700453 (rbu_data.bios_image_size == 0)) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700454 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700455 "bios_image_size %lu\n",
456 rbu_data.image_update_buffer,
457 rbu_data.bios_image_size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700458 ret_count = -ENOMEM;
459 goto read_rbu_data_exit;
460 }
461
462 if (pos > rbu_data.bios_image_size) {
463 ret_count = 0;
464 goto read_rbu_data_exit;
465 }
466
467 bytes_left = rbu_data.bios_image_size - pos;
468 data_length = min(bytes_left, count);
469
470 ptemp = rbu_data.image_update_buffer;
471 memcpy(buffer, (ptemp + pos), data_length);
472
473 if ((pos + count) > rbu_data.bios_image_size)
474 /* this was the last copy */
475 ret_count = bytes_left;
476 else
477 ret_count = count;
478 read_rbu_data_exit:
479 return ret_count;
480}
481
Andrew Mortondda85772005-09-16 19:28:05 -0700482static ssize_t read_rbu_data(struct kobject *kobj, char *buffer,
Abhay Salunkead6ce872005-10-11 08:29:02 -0700483 loff_t pos, size_t count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700484{
485 ssize_t ret_count = 0;
486
487 spin_lock(&rbu_data.lock);
488
489 if (!strcmp(image_type, "mono"))
490 ret_count = read_rbu_mono_data(buffer, pos, count);
491 else if (!strcmp(image_type, "packet"))
492 ret_count = read_packet_data(buffer, pos, count);
493 else
494 pr_debug("read_rbu_data: invalid image type specified\n");
495
496 spin_unlock(&rbu_data.lock);
497 return ret_count;
498}
499
Andrew Mortondda85772005-09-16 19:28:05 -0700500static void callbackfn_rbu(const struct firmware *fw, void *context)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700501{
502 int rc = 0;
503
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700504 if (!fw || !fw->size) {
505 rbu_data.entry_created = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700506 return;
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700507 }
Abhay Salunke6c54c282005-09-06 15:17:14 -0700508
509 spin_lock(&rbu_data.lock);
510 if (!strcmp(image_type, "mono")) {
511 if (!img_update_realloc(fw->size))
512 memcpy(rbu_data.image_update_buffer,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700513 fw->data, fw->size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700514 } else if (!strcmp(image_type, "packet")) {
Abhay Salunkead6ce872005-10-11 08:29:02 -0700515 /*
516 * we need to free previous packets if a
517 * new hunk of packets needs to be downloaded
518 */
519 packet_empty_list();
520 if (packetize_data(fw->data, fw->size))
521 /* Incase something goes wrong when we are
522 * in middle of packetizing the data, we
523 * need to free up whatever packets might
524 * have been created before we quit.
525 */
Abhay Salunke6c54c282005-09-06 15:17:14 -0700526 packet_empty_list();
Abhay Salunke6c54c282005-09-06 15:17:14 -0700527 } else
528 pr_debug("invalid image type specified.\n");
529 spin_unlock(&rbu_data.lock);
530
531 rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700532 "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700533 if (rc)
534 printk(KERN_ERR
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700535 "dell_rbu:%s request_firmware_nowait failed"
536 " %d\n", __FUNCTION__, rc);
537 else
538 rbu_data.entry_created = 1;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700539}
540
Andrew Mortondda85772005-09-16 19:28:05 -0700541static ssize_t read_rbu_image_type(struct kobject *kobj, char *buffer,
Abhay Salunkead6ce872005-10-11 08:29:02 -0700542 loff_t pos, size_t count)
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700543{
544 int size = 0;
545 if (!pos)
546 size = sprintf(buffer, "%s\n", image_type);
547 return size;
548}
549
Andrew Mortondda85772005-09-16 19:28:05 -0700550static ssize_t write_rbu_image_type(struct kobject *kobj, char *buffer,
Abhay Salunkead6ce872005-10-11 08:29:02 -0700551 loff_t pos, size_t count)
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700552{
553 int rc = count;
554 int req_firm_rc = 0;
555 int i;
556 spin_lock(&rbu_data.lock);
557 /*
558 * Find the first newline or space
559 */
560 for (i = 0; i < count; ++i)
561 if (buffer[i] == '\n' || buffer[i] == ' ') {
562 buffer[i] = '\0';
563 break;
564 }
565 if (i == count)
566 buffer[count] = '\0';
567
568 if (strstr(buffer, "mono"))
569 strcpy(image_type, "mono");
570 else if (strstr(buffer, "packet"))
571 strcpy(image_type, "packet");
572 else if (strstr(buffer, "init")) {
573 /*
574 * If due to the user error the driver gets in a bad
575 * state where even though it is loaded , the
576 * /sys/class/firmware/dell_rbu entries are missing.
577 * to cover this situation the user can recreate entries
578 * by writing init to image_type.
579 */
580 if (!rbu_data.entry_created) {
581 spin_unlock(&rbu_data.lock);
582 req_firm_rc = request_firmware_nowait(THIS_MODULE,
583 FW_ACTION_NOHOTPLUG, "dell_rbu",
584 &rbu_device->dev, &context,
585 callbackfn_rbu);
586 if (req_firm_rc) {
587 printk(KERN_ERR
588 "dell_rbu:%s request_firmware_nowait"
589 " failed %d\n", __FUNCTION__, rc);
590 rc = -EIO;
591 } else
592 rbu_data.entry_created = 1;
593
594 spin_lock(&rbu_data.lock);
595 }
596 } else {
597 printk(KERN_WARNING "dell_rbu: image_type is invalid\n");
598 spin_unlock(&rbu_data.lock);
599 return -EINVAL;
600 }
601
602 /* we must free all previous allocations */
603 packet_empty_list();
604 img_update_free();
605 spin_unlock(&rbu_data.lock);
606
607 return rc;
608}
609
Abhay Salunkead6ce872005-10-11 08:29:02 -0700610static ssize_t read_rbu_packet_size(struct kobject *kobj, char *buffer,
611 loff_t pos, size_t count)
612{
613 int size = 0;
614 if (!pos) {
615 spin_lock(&rbu_data.lock);
616 size = sprintf(buffer, "%lu\n", rbu_data.packetsize);
617 spin_unlock(&rbu_data.lock);
618 }
619 return size;
620}
621
622static ssize_t write_rbu_packet_size(struct kobject *kobj, char *buffer,
623 loff_t pos, size_t count)
624{
625 unsigned long temp;
626 spin_lock(&rbu_data.lock);
627 packet_empty_list();
628 sscanf(buffer, "%lu", &temp);
629 if (temp < 0xffffffff)
630 rbu_data.packetsize = temp;
631
632 spin_unlock(&rbu_data.lock);
633 return count;
634}
635
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700636static struct bin_attribute rbu_data_attr = {
Abhay Salunkead6ce872005-10-11 08:29:02 -0700637 .attr = {.name = "data",.owner = THIS_MODULE,.mode = 0444},
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700638 .read = read_rbu_data,
639};
640
641static struct bin_attribute rbu_image_type_attr = {
Abhay Salunkead6ce872005-10-11 08:29:02 -0700642 .attr = {.name = "image_type",.owner = THIS_MODULE,.mode = 0644},
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700643 .read = read_rbu_image_type,
644 .write = write_rbu_image_type,
645};
646
Abhay Salunkead6ce872005-10-11 08:29:02 -0700647static struct bin_attribute rbu_packet_size_attr = {
648 .attr = {.name = "packet_size",.owner = THIS_MODULE,.mode = 0644},
649 .read = read_rbu_packet_size,
650 .write = write_rbu_packet_size,
651};
652
Andrew Mortondda85772005-09-16 19:28:05 -0700653static int __init dcdrbu_init(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700654{
655 int rc = 0;
656 spin_lock_init(&rbu_data.lock);
657
658 init_packet_head();
659 rbu_device =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700660 platform_device_register_simple("dell_rbu", -1, NULL, 0);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700661 if (!rbu_device) {
662 printk(KERN_ERR
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700663 "dell_rbu:%s:platform_device_register_simple "
664 "failed\n", __FUNCTION__);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700665 return -EIO;
666 }
667
668 sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
669 sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
Abhay Salunkead6ce872005-10-11 08:29:02 -0700670 sysfs_create_bin_file(&rbu_device->dev.kobj,
671 &rbu_packet_size_attr);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700672
673 rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700674 "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700675 if (rc)
676 printk(KERN_ERR "dell_rbu:%s:request_firmware_nowait"
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700677 " failed %d\n", __FUNCTION__, rc);
678 else
679 rbu_data.entry_created = 1;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700680
681 return rc;
682
683}
684
Andrew Mortondda85772005-09-16 19:28:05 -0700685static __exit void dcdrbu_exit(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700686{
687 spin_lock(&rbu_data.lock);
688 packet_empty_list();
689 img_update_free();
690 spin_unlock(&rbu_data.lock);
691 platform_device_unregister(rbu_device);
692}
693
694module_exit(dcdrbu_exit);
695module_init(dcdrbu_init);