blob: ba17292eb2902b5c7acb676beeb7fb840813414a [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 Salunke274b6932005-11-07 00:59:26 -080053MODULE_VERSION("3.1");
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
Abhay Salunke274b6932005-11-07 00:59:26 -080076static 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
Abhay Salunke6c54c282005-09-06 15:17:14 -070081struct 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
Andrew Mortondda85772005-09-16 19:28:05 -070094static void init_packet_head(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -070095{
96 INIT_LIST_HEAD(&packet_data_head.list);
Abhay Salunke6c54c282005-09-06 15:17:14 -070097 rbu_data.packet_read_count = 0;
98 rbu_data.num_packets = 0;
99 rbu_data.packetsize = 0;
Abhay Salunkead6ce872005-10-11 08:29:02 -0700100 rbu_data.imagesize = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700101}
102
Abhay Salunkead6ce872005-10-11 08:29:02 -0700103static int create_packet(void *data, size_t length)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700104{
105 struct packet_data *newpacket;
106 int ordernum = 0;
Abhay Salunke274b6932005-11-07 00:59:26 -0800107 int retval = 0;
108 unsigned int packet_array_size = 0;
109 void **invalid_addr_packet_array = 0;
110 void *packet_data_temp_buf = 0;
111 unsigned int idx = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700112
113 pr_debug("create_packet: entry \n");
114
115 if (!rbu_data.packetsize) {
116 pr_debug("create_packet: packetsize not specified\n");
Abhay Salunke274b6932005-11-07 00:59:26 -0800117 retval = -EINVAL;
118 goto out_noalloc;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700119 }
Abhay Salunke274b6932005-11-07 00:59:26 -0800120
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700121 spin_unlock(&rbu_data.lock);
Abhay Salunke274b6932005-11-07 00:59:26 -0800122
123 newpacket = kzalloc(sizeof (struct packet_data), GFP_KERNEL);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700124
Abhay Salunke6c54c282005-09-06 15:17:14 -0700125 if (!newpacket) {
126 printk(KERN_WARNING
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700127 "dell_rbu:%s: failed to allocate new "
128 "packet\n", __FUNCTION__);
Abhay Salunke274b6932005-11-07 00:59:26 -0800129 retval = -ENOMEM;
130 spin_lock(&rbu_data.lock);
131 goto out_noalloc;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700132 }
133
134 ordernum = get_order(length);
Abhay Salunke274b6932005-11-07 00:59:26 -0800135
Abhay Salunke6c54c282005-09-06 15:17:14 -0700136 /*
Abhay Salunke274b6932005-11-07 00:59:26 -0800137 * 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.
Abhay Salunke6c54c282005-09-06 15:17:14 -0700146 */
Abhay Salunke274b6932005-11-07 00:59:26 -0800147 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);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700152
Abhay Salunke274b6932005-11-07 00:59:26 -0800153 if (!invalid_addr_packet_array) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700154 printk(KERN_WARNING
Abhay Salunke274b6932005-11-07 00:59:26 -0800155 "dell_rbu:%s: failed to allocate "
156 "invalid_addr_packet_array \n",
157 __FUNCTION__);
158 retval = -ENOMEM;
159 spin_lock(&rbu_data.lock);
160 goto out_alloc_packet;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700161 }
162
Abhay Salunke274b6932005-11-07 00:59:26 -0800163 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 "
169 "packet\n", __FUNCTION__);
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;
182 packet_data_temp_buf = 0;
183 }
184 }
185 spin_lock(&rbu_data.lock);
186
187 newpacket->data = packet_data_temp_buf;
188
189 pr_debug("create_packet: newpacket at physical addr %lx\n",
190 (unsigned long)virt_to_phys(newpacket->data));
191
192 /* packets may not have fixed size */
193 newpacket->length = length;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700194 newpacket->ordernum = ordernum;
195 ++rbu_data.num_packets;
Abhay Salunke274b6932005-11-07 00:59:26 -0800196
197 /* initialize the newly created packet headers */
Abhay Salunke6c54c282005-09-06 15:17:14 -0700198 INIT_LIST_HEAD(&newpacket->list);
199 list_add_tail(&newpacket->list, &packet_data_head.list);
Abhay Salunkead6ce872005-10-11 08:29:02 -0700200
201 memcpy(newpacket->data, data, length);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700202
203 pr_debug("create_packet: exit \n");
204
Abhay Salunke274b6932005-11-07 00:59:26 -0800205out_alloc_packet_array:
206 /* always free packet array */
207 for (;idx>0;idx--) {
208 pr_debug("freeing unused packet below floor 0x%lx.\n",
209 (unsigned long)virt_to_phys(
210 invalid_addr_packet_array[idx-1]));
211 free_pages((unsigned long)invalid_addr_packet_array[idx-1],
212 ordernum);
213 }
214 kfree(invalid_addr_packet_array);
215
216out_alloc_packet:
217 /* if error, free data */
218 if (retval)
219 kfree(newpacket);
220
221out_noalloc:
222 return retval;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700223}
224
Andrew Mortondda85772005-09-16 19:28:05 -0700225static int packetize_data(void *data, size_t length)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700226{
227 int rc = 0;
Abhay Salunkead6ce872005-10-11 08:29:02 -0700228 int done = 0;
229 int packet_length;
230 u8 *temp;
231 u8 *end = (u8 *) data + length;
232 pr_debug("packetize_data: data length %d\n", length);
233 if (!rbu_data.packetsize) {
234 printk(KERN_WARNING
235 "dell_rbu: packetsize not specified\n");
236 return -EIO;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700237 }
Abhay Salunkead6ce872005-10-11 08:29:02 -0700238
239 temp = (u8 *) data;
240
241 /* packetize the hunk */
242 while (!done) {
243 if ((temp + rbu_data.packetsize) < end)
244 packet_length = rbu_data.packetsize;
245 else {
246 /* this is the last packet */
247 packet_length = end - temp;
248 done = 1;
249 }
250
251 if ((rc = create_packet(temp, packet_length)))
252 return rc;
253
254 pr_debug("%lu:%lu\n", temp, (end - temp));
255 temp += packet_length;
256 }
257
258 rbu_data.imagesize = length;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700259
260 return rc;
261}
262
Andrew Mortondda85772005-09-16 19:28:05 -0700263static int do_packet_read(char *data, struct list_head *ptemp_list,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700264 int length, int bytes_read, int *list_read_count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700265{
266 void *ptemp_buf;
267 struct packet_data *newpacket = NULL;
268 int bytes_copied = 0;
269 int j = 0;
270
271 newpacket = list_entry(ptemp_list, struct packet_data, list);
272 *list_read_count += newpacket->length;
273
274 if (*list_read_count > bytes_read) {
275 /* point to the start of unread data */
276 j = newpacket->length - (*list_read_count - bytes_read);
277 /* point to the offset in the packet buffer */
278 ptemp_buf = (u8 *) newpacket->data + j;
279 /*
280 * check if there is enough room in
281 * * the incoming buffer
282 */
283 if (length > (*list_read_count - bytes_read))
284 /*
285 * copy what ever is there in this
286 * packet and move on
287 */
288 bytes_copied = (*list_read_count - bytes_read);
289 else
290 /* copy the remaining */
291 bytes_copied = length;
292 memcpy(data, ptemp_buf, bytes_copied);
293 }
294 return bytes_copied;
295}
296
Abhay Salunkead6ce872005-10-11 08:29:02 -0700297static int packet_read_list(char *data, size_t * pread_length)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700298{
299 struct list_head *ptemp_list;
300 int temp_count = 0;
301 int bytes_copied = 0;
302 int bytes_read = 0;
303 int remaining_bytes = 0;
304 char *pdest = data;
305
306 /* check if we have any packets */
307 if (0 == rbu_data.num_packets)
308 return -ENOMEM;
309
310 remaining_bytes = *pread_length;
311 bytes_read = rbu_data.packet_read_count;
312
313 ptemp_list = (&packet_data_head.list)->next;
314 while (!list_empty(ptemp_list)) {
315 bytes_copied = do_packet_read(pdest, ptemp_list,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700316 remaining_bytes, bytes_read, &temp_count);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700317 remaining_bytes -= bytes_copied;
318 bytes_read += bytes_copied;
319 pdest += bytes_copied;
320 /*
321 * check if we reached end of buffer before reaching the
322 * last packet
323 */
324 if (remaining_bytes == 0)
325 break;
326
327 ptemp_list = ptemp_list->next;
328 }
329 /*finally set the bytes read */
330 *pread_length = bytes_read - rbu_data.packet_read_count;
331 rbu_data.packet_read_count = bytes_read;
332 return 0;
333}
334
Andrew Mortondda85772005-09-16 19:28:05 -0700335static void packet_empty_list(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700336{
337 struct list_head *ptemp_list;
338 struct list_head *pnext_list;
339 struct packet_data *newpacket;
340
341 ptemp_list = (&packet_data_head.list)->next;
342 while (!list_empty(ptemp_list)) {
343 newpacket =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700344 list_entry(ptemp_list, struct packet_data, list);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700345 pnext_list = ptemp_list->next;
346 list_del(ptemp_list);
347 ptemp_list = pnext_list;
348 /*
349 * zero out the RBU packet memory before freeing
350 * to make sure there are no stale RBU packets left in memory
351 */
352 memset(newpacket->data, 0, rbu_data.packetsize);
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700353 free_pages((unsigned long) newpacket->data,
354 newpacket->ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700355 kfree(newpacket);
356 }
Abhay Salunke6c54c282005-09-06 15:17:14 -0700357 rbu_data.packet_read_count = 0;
358 rbu_data.num_packets = 0;
Abhay Salunkead6ce872005-10-11 08:29:02 -0700359 rbu_data.imagesize = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700360}
361
362/*
363 * img_update_free: Frees the buffer allocated for storing BIOS image
364 * Always called with lock held and returned with lock held
365 */
Andrew Mortondda85772005-09-16 19:28:05 -0700366static void img_update_free(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700367{
368 if (!rbu_data.image_update_buffer)
369 return;
370 /*
371 * zero out this buffer before freeing it to get rid of any stale
372 * BIOS image copied in memory.
373 */
374 memset(rbu_data.image_update_buffer, 0,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700375 rbu_data.image_update_buffer_size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700376 if (rbu_data.dma_alloc == 1)
377 dma_free_coherent(NULL, rbu_data.bios_image_size,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700378 rbu_data.image_update_buffer, dell_rbu_dmaaddr);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700379 else
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700380 free_pages((unsigned long) rbu_data.image_update_buffer,
381 rbu_data.image_update_ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700382
383 /*
384 * Re-initialize the rbu_data variables after a free
385 */
386 rbu_data.image_update_ordernum = -1;
387 rbu_data.image_update_buffer = NULL;
388 rbu_data.image_update_buffer_size = 0;
389 rbu_data.bios_image_size = 0;
390 rbu_data.dma_alloc = 0;
391}
392
393/*
394 * img_update_realloc: This function allocates the contiguous pages to
395 * accommodate the requested size of data. The memory address and size
396 * values are stored globally and on every call to this function the new
397 * size is checked to see if more data is required than the existing size.
398 * If true the previous memory is freed and new allocation is done to
399 * accommodate the new size. If the incoming size is less then than the
400 * already allocated size, then that memory is reused. This function is
401 * called with lock held and returns with lock held.
402 */
Andrew Mortondda85772005-09-16 19:28:05 -0700403static int img_update_realloc(unsigned long size)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700404{
405 unsigned char *image_update_buffer = NULL;
406 unsigned long rc;
407 unsigned long img_buf_phys_addr;
408 int ordernum;
409 int dma_alloc = 0;
410
411 /*
412 * check if the buffer of sufficient size has been
413 * already allocated
414 */
415 if (rbu_data.image_update_buffer_size >= size) {
416 /*
417 * check for corruption
418 */
419 if ((size != 0) && (rbu_data.image_update_buffer == NULL)) {
420 printk(KERN_ERR "dell_rbu:%s: corruption "
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700421 "check failed\n", __FUNCTION__);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700422 return -EINVAL;
423 }
424 /*
425 * we have a valid pre-allocated buffer with
426 * sufficient size
427 */
428 return 0;
429 }
430
431 /*
432 * free any previously allocated buffer
433 */
434 img_update_free();
435
436 spin_unlock(&rbu_data.lock);
437
438 ordernum = get_order(size);
439 image_update_buffer =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700440 (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700441
442 img_buf_phys_addr =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700443 (unsigned long) virt_to_phys(image_update_buffer);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700444
445 if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700446 free_pages((unsigned long) image_update_buffer, ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700447 ordernum = -1;
448 image_update_buffer = dma_alloc_coherent(NULL, size,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700449 &dell_rbu_dmaaddr, GFP_KERNEL);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700450 dma_alloc = 1;
451 }
452
453 spin_lock(&rbu_data.lock);
454
455 if (image_update_buffer != NULL) {
456 rbu_data.image_update_buffer = image_update_buffer;
457 rbu_data.image_update_buffer_size = size;
458 rbu_data.bios_image_size =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700459 rbu_data.image_update_buffer_size;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700460 rbu_data.image_update_ordernum = ordernum;
461 rbu_data.dma_alloc = dma_alloc;
462 rc = 0;
463 } else {
464 pr_debug("Not enough memory for image update:"
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700465 "size = %ld\n", size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700466 rc = -ENOMEM;
467 }
468
469 return rc;
470}
471
Andrew Mortondda85772005-09-16 19:28:05 -0700472static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700473{
474 int retval;
475 size_t bytes_left;
476 size_t data_length;
477 char *ptempBuf = buffer;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700478
479 /* check to see if we have something to return */
480 if (rbu_data.num_packets == 0) {
481 pr_debug("read_packet_data: no packets written\n");
482 retval = -ENOMEM;
483 goto read_rbu_data_exit;
484 }
485
Abhay Salunkead6ce872005-10-11 08:29:02 -0700486 if (pos > rbu_data.imagesize) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700487 retval = 0;
488 printk(KERN_WARNING "dell_rbu:read_packet_data: "
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700489 "data underrun\n");
Abhay Salunke6c54c282005-09-06 15:17:14 -0700490 goto read_rbu_data_exit;
491 }
492
Abhay Salunkead6ce872005-10-11 08:29:02 -0700493 bytes_left = rbu_data.imagesize - pos;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700494 data_length = min(bytes_left, count);
495
496 if ((retval = packet_read_list(ptempBuf, &data_length)) < 0)
497 goto read_rbu_data_exit;
498
Abhay Salunkead6ce872005-10-11 08:29:02 -0700499 if ((pos + count) > rbu_data.imagesize) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700500 rbu_data.packet_read_count = 0;
501 /* this was the last copy */
502 retval = bytes_left;
503 } else
504 retval = count;
505
506 read_rbu_data_exit:
507 return retval;
508}
509
Andrew Mortondda85772005-09-16 19:28:05 -0700510static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700511{
512 unsigned char *ptemp = NULL;
513 size_t bytes_left = 0;
514 size_t data_length = 0;
515 ssize_t ret_count = 0;
516
517 /* check to see if we have something to return */
518 if ((rbu_data.image_update_buffer == NULL) ||
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700519 (rbu_data.bios_image_size == 0)) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700520 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700521 "bios_image_size %lu\n",
522 rbu_data.image_update_buffer,
523 rbu_data.bios_image_size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700524 ret_count = -ENOMEM;
525 goto read_rbu_data_exit;
526 }
527
528 if (pos > rbu_data.bios_image_size) {
529 ret_count = 0;
530 goto read_rbu_data_exit;
531 }
532
533 bytes_left = rbu_data.bios_image_size - pos;
534 data_length = min(bytes_left, count);
535
536 ptemp = rbu_data.image_update_buffer;
537 memcpy(buffer, (ptemp + pos), data_length);
538
539 if ((pos + count) > rbu_data.bios_image_size)
540 /* this was the last copy */
541 ret_count = bytes_left;
542 else
543 ret_count = count;
544 read_rbu_data_exit:
545 return ret_count;
546}
547
Andrew Mortondda85772005-09-16 19:28:05 -0700548static ssize_t read_rbu_data(struct kobject *kobj, char *buffer,
Abhay Salunkead6ce872005-10-11 08:29:02 -0700549 loff_t pos, size_t count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700550{
551 ssize_t ret_count = 0;
552
553 spin_lock(&rbu_data.lock);
554
555 if (!strcmp(image_type, "mono"))
556 ret_count = read_rbu_mono_data(buffer, pos, count);
557 else if (!strcmp(image_type, "packet"))
558 ret_count = read_packet_data(buffer, pos, count);
559 else
560 pr_debug("read_rbu_data: invalid image type specified\n");
561
562 spin_unlock(&rbu_data.lock);
563 return ret_count;
564}
565
Andrew Mortondda85772005-09-16 19:28:05 -0700566static void callbackfn_rbu(const struct firmware *fw, void *context)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700567{
568 int rc = 0;
569
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700570 if (!fw || !fw->size) {
571 rbu_data.entry_created = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700572 return;
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700573 }
Abhay Salunke6c54c282005-09-06 15:17:14 -0700574
575 spin_lock(&rbu_data.lock);
576 if (!strcmp(image_type, "mono")) {
577 if (!img_update_realloc(fw->size))
578 memcpy(rbu_data.image_update_buffer,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700579 fw->data, fw->size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700580 } else if (!strcmp(image_type, "packet")) {
Abhay Salunkead6ce872005-10-11 08:29:02 -0700581 /*
582 * we need to free previous packets if a
583 * new hunk of packets needs to be downloaded
584 */
585 packet_empty_list();
586 if (packetize_data(fw->data, fw->size))
587 /* Incase something goes wrong when we are
588 * in middle of packetizing the data, we
589 * need to free up whatever packets might
590 * have been created before we quit.
591 */
Abhay Salunke6c54c282005-09-06 15:17:14 -0700592 packet_empty_list();
Abhay Salunke6c54c282005-09-06 15:17:14 -0700593 } else
594 pr_debug("invalid image type specified.\n");
595 spin_unlock(&rbu_data.lock);
596
597 rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700598 "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700599 if (rc)
600 printk(KERN_ERR
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700601 "dell_rbu:%s request_firmware_nowait failed"
602 " %d\n", __FUNCTION__, rc);
603 else
604 rbu_data.entry_created = 1;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700605}
606
Andrew Mortondda85772005-09-16 19:28:05 -0700607static ssize_t read_rbu_image_type(struct kobject *kobj, char *buffer,
Abhay Salunkead6ce872005-10-11 08:29:02 -0700608 loff_t pos, size_t count)
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700609{
610 int size = 0;
611 if (!pos)
612 size = sprintf(buffer, "%s\n", image_type);
613 return size;
614}
615
Andrew Mortondda85772005-09-16 19:28:05 -0700616static ssize_t write_rbu_image_type(struct kobject *kobj, char *buffer,
Abhay Salunkead6ce872005-10-11 08:29:02 -0700617 loff_t pos, size_t count)
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700618{
619 int rc = count;
620 int req_firm_rc = 0;
621 int i;
622 spin_lock(&rbu_data.lock);
623 /*
624 * Find the first newline or space
625 */
626 for (i = 0; i < count; ++i)
627 if (buffer[i] == '\n' || buffer[i] == ' ') {
628 buffer[i] = '\0';
629 break;
630 }
631 if (i == count)
632 buffer[count] = '\0';
633
634 if (strstr(buffer, "mono"))
635 strcpy(image_type, "mono");
636 else if (strstr(buffer, "packet"))
637 strcpy(image_type, "packet");
638 else if (strstr(buffer, "init")) {
639 /*
640 * If due to the user error the driver gets in a bad
641 * state where even though it is loaded , the
642 * /sys/class/firmware/dell_rbu entries are missing.
643 * to cover this situation the user can recreate entries
644 * by writing init to image_type.
645 */
646 if (!rbu_data.entry_created) {
647 spin_unlock(&rbu_data.lock);
648 req_firm_rc = request_firmware_nowait(THIS_MODULE,
649 FW_ACTION_NOHOTPLUG, "dell_rbu",
650 &rbu_device->dev, &context,
651 callbackfn_rbu);
652 if (req_firm_rc) {
653 printk(KERN_ERR
654 "dell_rbu:%s request_firmware_nowait"
655 " failed %d\n", __FUNCTION__, rc);
656 rc = -EIO;
657 } else
658 rbu_data.entry_created = 1;
659
660 spin_lock(&rbu_data.lock);
661 }
662 } else {
663 printk(KERN_WARNING "dell_rbu: image_type is invalid\n");
664 spin_unlock(&rbu_data.lock);
665 return -EINVAL;
666 }
667
668 /* we must free all previous allocations */
669 packet_empty_list();
670 img_update_free();
671 spin_unlock(&rbu_data.lock);
672
673 return rc;
674}
675
Abhay Salunkead6ce872005-10-11 08:29:02 -0700676static ssize_t read_rbu_packet_size(struct kobject *kobj, char *buffer,
677 loff_t pos, size_t count)
678{
679 int size = 0;
680 if (!pos) {
681 spin_lock(&rbu_data.lock);
682 size = sprintf(buffer, "%lu\n", rbu_data.packetsize);
683 spin_unlock(&rbu_data.lock);
684 }
685 return size;
686}
687
688static ssize_t write_rbu_packet_size(struct kobject *kobj, char *buffer,
689 loff_t pos, size_t count)
690{
691 unsigned long temp;
692 spin_lock(&rbu_data.lock);
693 packet_empty_list();
694 sscanf(buffer, "%lu", &temp);
695 if (temp < 0xffffffff)
696 rbu_data.packetsize = temp;
697
698 spin_unlock(&rbu_data.lock);
699 return count;
700}
701
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700702static struct bin_attribute rbu_data_attr = {
Abhay Salunkead6ce872005-10-11 08:29:02 -0700703 .attr = {.name = "data",.owner = THIS_MODULE,.mode = 0444},
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700704 .read = read_rbu_data,
705};
706
707static struct bin_attribute rbu_image_type_attr = {
Abhay Salunkead6ce872005-10-11 08:29:02 -0700708 .attr = {.name = "image_type",.owner = THIS_MODULE,.mode = 0644},
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700709 .read = read_rbu_image_type,
710 .write = write_rbu_image_type,
711};
712
Abhay Salunkead6ce872005-10-11 08:29:02 -0700713static struct bin_attribute rbu_packet_size_attr = {
714 .attr = {.name = "packet_size",.owner = THIS_MODULE,.mode = 0644},
715 .read = read_rbu_packet_size,
716 .write = write_rbu_packet_size,
717};
718
Andrew Mortondda85772005-09-16 19:28:05 -0700719static int __init dcdrbu_init(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700720{
721 int rc = 0;
722 spin_lock_init(&rbu_data.lock);
723
724 init_packet_head();
725 rbu_device =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700726 platform_device_register_simple("dell_rbu", -1, NULL, 0);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700727 if (!rbu_device) {
728 printk(KERN_ERR
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700729 "dell_rbu:%s:platform_device_register_simple "
730 "failed\n", __FUNCTION__);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700731 return -EIO;
732 }
733
734 sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
735 sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
Abhay Salunkead6ce872005-10-11 08:29:02 -0700736 sysfs_create_bin_file(&rbu_device->dev.kobj,
737 &rbu_packet_size_attr);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700738
739 rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700740 "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700741 if (rc)
742 printk(KERN_ERR "dell_rbu:%s:request_firmware_nowait"
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700743 " failed %d\n", __FUNCTION__, rc);
744 else
745 rbu_data.entry_created = 1;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700746
747 return rc;
748
749}
750
Andrew Mortondda85772005-09-16 19:28:05 -0700751static __exit void dcdrbu_exit(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700752{
753 spin_lock(&rbu_data.lock);
754 packet_empty_list();
755 img_update_free();
756 spin_unlock(&rbu_data.lock);
757 platform_device_unregister(rbu_device);
758}
759
760module_exit(dcdrbu_exit);
761module_init(dcdrbu_init);
Abhay Salunke274b6932005-11-07 00:59:26 -0800762
763/* vim:noet:ts=8:sw=8
764*/