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