Fix to install pppoecd (in router/Makefile)
[tomato.git] / release / src / router / mmc / mmc.c
blobf588dc07d031b3f4889312c23a989cffb1c30019
1 /*==============================================================================
2 * mmc.c - Linksys WRT54G/WRT54GS/WRT54GL hardware mod - MMHC/SDHC card driver
3 *
4 * Version: 2.0.1
6 * Authors:
8 * Madsuk/Rohde/Cyril CATTIAUX/Marc DENTY/rcichielo KRUSCH/Chris
10 * Description:
12 * Rework of the 1.3.5 optimized driver posted on the opewnwrt forum.
13 * See the Release notes for a description of changes and new features.
15 * Usage:
17 * The /etc/init.d/mmc script is used to start and stop mmc services.
18 * This script sets the appropriate gpiomask for /proc/diag/gpiomask,
19 * loads the kernel module and mounts the first partition under /mmc.
20 * Modify the script if you want different behaviour:
22 * /etc/init.d/mmc start - Start mmc services - mount card if detected
23 * /etc/init.d/mmc stop - Unmount card and stop mmc services
24 * /etc/init.d/mmc status - Print status of mmc service and card details
26 * Module Parameters:
28 * major - Major number to be assigned to the mmc device (default 0 - assign dynamically).
30 * cs - GPIO line connect to the card CS (chip select) pin (default 7).
32 * clk - GPIO line connected to the card CLK (clock) pin (default 3).
34 * din - GPIO line connected to the card DI (data in) pin (default 2).
36 * dout - GPIO line connected to the card DO (data out) pin (default 4).
38 * rahead - Maximum number of blocks kernel can read ahead (default 2).
40 * maxsec - Maximum number of sectors that can be clustered into one request (default 32).
41 * Increasing this number can improve read and/or write throughput for large files.
42 * Keep it smaller if you expect frequent concurrent IO to the card (reading/writing
43 * of multiple files at the same time). Experiment with the setting to see what
44 * works best for your system.
46 * dbg - Only valid if you load the debug version of the kernel module (default 0).
47 * Bit flags that specify what debugging to product to the system log:
49 * 1 - Card initialization trace
50 * 2 - Calls to module "open" function
51 * 3 - Calls to module "release" function
52 * 4 - Calls to module "check_change" function
53 * 5 - Calls to module "revalidate" function
54 * 6 - Calls to module "request" function
55 * 7 - Calls to module "ioctl" function
56 * 8 - Print "busy wait" information
58 * gpio_input - Set the gpio register memory locations.
59 * gpio_output Allows defaults to be overridden when testing driver
60 * gpio_enable on other broadcom based devices.
61 * gpio_control
64 * Release Notes:
66 * Version 2.0.0 - Mar 9, 2008
68 * - Rework of code base:
70 * 1) Rework of functions that must honour max clock frequency. These functions
71 * were generalized and condensed. Max clock frequency now managed through 2
72 * global vars - no need to pass timing arguments.
74 * 2) Logging functions replaced/simplified by variadic macros.
76 * 3) Document and comment. Standardize layout, variables, style, etc. Split
77 * card initialization function into separate source file.
79 * - Switch so module uses a dynamically assigned major number by default. Implement "major="
80 * module parameter to allow a specific major number to be assigned.
82 * - Implement module parameters "cs=", "clk=", "din=", "dout=" for specifying GPIO to card mapping.
83 * Alter read/write algorithms to be more efficient with mappings in variables.
85 * - Implement module parameters "gpio_input=", "gpio_output=", "gpio_enable=", "gpio_control=" for
86 * specifying GPIO register addresses. May be useful if you want to try using this module on other
87 * broadcom based platforms where the gpio registers are located at different locations.
89 * - Debugging improvements. Implement "dbg=" module parameter to allow selective enabling of
90 * debugging output by function. Only available when module compiled with debugging (-DDEBUG)
92 * - Initialize max_segments array so requests are clustered. "maxsec=" module parameter
93 * sets the maximum number of sectors that can be clustered per request (default is 32).
95 * - Implement clustering support in the module request method. Improves speed by allowing more
96 * clusters to be read/written per single invocation of a multi block read/write command.
98 * - Implement Support for high density (> 2GB) SDHC and MMHC cards.
100 * - Implement /proc/mmc/status for obtaining information about the detected card.
102 * - Maximum number of supported partitions reduced from 64 to 8 (memory use reduction).
104 * - Build using buildroot-ng environment. Generate ipkg file for installation.
105 * With so little difference in speed, and only a 4k memory savings, compile debug enabled version
107 * Version 2.0.1 - Feb 8, 2009
109 * - Fix integer overflow when calculating size of SDHC or MMHC cards greater than 4GB. Testers have
110 * confirmed that 8GB and 16GB cards now functioning correctly.
112 * Supported Linksys devices:
114 * Developed and tested on WRT54GL V1.1. Should work on the majority of WRT54G/GS/GL devices.
115 * Reported working on:
117 * - WRT54GL V1.1
119 * Supported mc/sd cards.
121 * Tested using the folowing cards:
123 * - Kingston MMC Plus - 1GB
124 * - PNY Technologies SD - 1GB
125 * - SanDisk Mini SD - 128MB
126 * - Lexar SDHC - 4GB
128 * Module users have reported success with the following cards:
130 * - Kingston SD - 2GB
131 * - Kingston microSD - 2GB
132 * - Toshiba SDHC class 6 - 4GB
133 * - Sandisk SDHC class 4 - 4GB
134 * - Lexar SDHC - 16GB
136 * No MMHC card tested - suspect possible problems at initialization.
137 * If you test one, please provide feedback on the results.
139 *============================================================================*/
140 #include <linux/module.h>
141 #include <linux/delay.h>
142 #include <linux/errno.h>
143 #include <linux/init.h>
144 #include <linux/hdreg.h>
145 #include <linux/fs.h>
146 #include <linux/blkpg.h>
147 #include <linux/slab.h>
148 #include <linux/proc_fs.h>
149 #include <asm/uaccess.h>
150 #include <linux/unistd.h>
152 // Block driver header setup - use variable for MAJOR_NR to allow dynamic assignment
153 // These must be defined before blk.h is included.
154 static unsigned int major = 0; // Device major number (mod parm - dynamic if 0)
155 #define MAJOR_NR major // Major num - map to major var to allow dynamic assignment
156 #define PSHIFT 3 // Max 8 partitions
157 #define NUMDEV 1 // We only support a single device
158 #define DEVICE_NR(device) (MINOR(device) >> PSHIFT) // Same device num for all partitions
159 #define DEVICE_NAME "mmc" // Device name (messaging and in /dev).
160 #define DEVICE_NO_RANDOM // No entropy to contribute
162 #include <linux/blk.h>
165 // Assign default values for modules parameters - can override by passing a module parameter.
166 #define READ_AHEAD 2 // Block read ahead max
167 #define DI 2 // Card DI to GPIO pin mapping
168 #define DO 4 // Card DO to GPIO pin mapping
169 #define CLK 3 // Card CLK to GPIP pin mapping
170 #define CS 7 // Card CS to GPIO pin mapping
171 #define MSEC 32 // Maximum sectors per request
173 #define VERSION "2.0.1" // Module Version Number
174 #define DEBUG // Compile in debugging. Module is very slightly
175 // faster, and 4k smaller with no debugging
177 #ifdef DEBUG // Defined via compiler argument
178 // Debug flag - bit symbols
179 #define DBG_INIT (1 << 0) // 1
180 #define DBG_OPEN (1 << 1) // 2
181 #define DBG_RLSE (1 << 2) // 4
182 #define DBG_CHG (1 << 3) // 8
183 #define DBG_REVAL (1 << 4) // 16
184 #define DBG_REQ (1 << 5) // 32
185 #define DBG_IOCTL (1 << 6) // 64
186 #define DBG_BUSY (1 << 7) // 128
187 #endif
189 // R1 response - bit symbols
190 #define R1_IDLE_STATE (1 << 0)
191 #define R1_ERASE_RESET (1 << 1)
192 #define R1_ILL_CMD (1 << 2)
193 #define R1_CRC_ERR (1 << 3)
194 #define R1_ERASE_ERR (1 << 4)
195 #define R1_ADDR_ERR (1 << 5)
196 #define R1_PARM_ERR (1 << 6)
199 // Macros for logging via printk.
200 #define LOG_INFO(...) printk(KERN_INFO "[INF] mmc: " __VA_ARGS__)
201 #define LOG_NOTICE(...) printk(KERN_NOTICE "[NOT] mmc: " __VA_ARGS__)
202 #define LOG_WARN(...) printk(KERN_WARNING "[WRN] mmc: " __VA_ARGS__)
203 #define LOG_ERR(...) printk(KERN_ERR "[ERR] mmc: " __VA_ARGS__)
204 #ifdef DEBUG
205 #define LOG_DEBUG(flag, ...) if (dbg & flag) printk(KERN_INFO "[DBG] mmc: " __VA_ARGS__)
206 #else
207 #define LOG_DEBUG(flag, ...)
208 #endif
211 // Function prototypes
212 int mmc_open(struct inode *inode, struct file *filp);
213 int mmc_release(struct inode *inode, struct file *filp);
214 int mmc_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
215 int mmc_check_change(kdev_t);
216 int mmc_revalidate(kdev_t);
217 static void mmc_request(request_queue_t *);
218 static ssize_t mmc_proc_read(struct file *, char *, size_t, loff_t *);
219 int mmc_init(void);
220 void mmc_exit(void);
223 // Module global variable declarations and setup.
224 // Items marked with '(parm)' can be overridden via a module parameter.
225 //static unsigned int major; // Defined earlier - device major number (parm)
226 static int rahead = READ_AHEAD; // Number of device blocks to read ahead (parm)
227 static int din = DI; // GPIO attached to card DI pin (parm)
228 static int dout = DO; // GPIO attached to card DO pin (parm)
229 static int clk = CLK; // GPIO attached to card CLK pin (parm)
230 static int cs = CS; // GPIO attached to card CS pin (parm)
231 static int maxsec = MSEC; // Maximum number of sectors per request
233 static volatile unsigned char *gpio_input = (unsigned char *)0xb8000060;
234 static volatile unsigned char *gpio_output = (unsigned char *)0xb8000064;
235 static volatile unsigned char *gpio_enable = (unsigned char *)0xb8000068;
236 static volatile unsigned char *gpio_control = (unsigned char *)0xb800006c;
238 #ifdef DEBUG
239 static int dbg = 0; // Flag bits indicating what debugging to produce
240 #endif
243 // Set up module parameters
244 MODULE_PARM(major, "i");
245 MODULE_PARM_DESC(major, "Major device number to use");
246 MODULE_PARM (rahead, "i");
247 MODULE_PARM_DESC(rahead, "Device read ahead blocks");
248 MODULE_PARM (din, "i");
249 MODULE_PARM_DESC(din, "Gpio to assign to DI pin");
250 MODULE_PARM (dout, "i");
251 MODULE_PARM_DESC(dout, "Gpio to assign to DO pin");
252 MODULE_PARM (clk, "i");
253 MODULE_PARM_DESC(clk, "Gpio to assign to CLK pin");
254 MODULE_PARM (cs, "i");
255 MODULE_PARM_DESC(cs, "Gpio to assign to CS pin");
256 MODULE_PARM (maxsec, "i");
257 MODULE_PARM_DESC(maxsec, "Max sectors per request");
258 MODULE_PARM (gpio_input, "i");
259 MODULE_PARM_DESC(gpio_input, "Address of gpio input register");
260 MODULE_PARM (gpio_output, "i");
261 MODULE_PARM_DESC(gpio_output, "Address of gpio output register");
262 MODULE_PARM (gpio_enable, "i");
263 MODULE_PARM_DESC(gpio_enable, "Address of gpio enable register");
264 MODULE_PARM (gpio_control, "i");
265 MODULE_PARM_DESC(gpio_control, "Address of gpio control register");
266 #ifdef DEBUG
267 MODULE_PARM (dbg, "i");
268 MODULE_PARM_DESC(dbg, "Control debugging output");
269 #endif
272 // Block driver global arrays - indexed by major and minor numbers
273 static int mmc_sizes[NUMDEV << PSHIFT]; // size of each device in kb
274 static int mmc_blocksizes[NUMDEV << PSHIFT]; // size of block used by device
275 static int mmc_hardsectsizes[NUMDEV << PSHIFT]; // size of sectors on device
276 static int mmc_maxsect[NUMDEV << PSHIFT]; // max size of a single request
277 static struct hd_struct mmc_part[NUMDEV << PSHIFT]; // decoded device partition table
280 // Block device operations structure
281 static struct block_device_operations mmc_bdops = {
282 open: mmc_open,
283 release: mmc_release,
284 ioctl: mmc_ioctl,
285 // check_media_change: mmc_check_change,
286 // revalidate: mmc_revalidate,
290 // Generic disk support control structure
291 struct gendisk mmc_gendisk = {
292 major: 0, // Major number assigned later
293 major_name: DEVICE_NAME, // Name of the major device
294 minor_shift: PSHIFT, // Shift to get device number
295 nr_real: NUMDEV, // Number of real devices
296 max_p: 1 << PSHIFT, // Number of partitions
297 part: mmc_part, // Pointer to partition global array
298 sizes: mmc_sizes, // Pointer to sizes global array
299 fops: &mmc_bdops, // Block dev operations
302 // Methods for driving the spi bus
303 #include "spi.c"
305 // Methods for initializing the cards
306 #include "init.c"
308 static struct card_info *card = NULL; // Card information structure (after init.c
309 // to avoid creation of init.h header file.)
311 // Support for /proc filesystem
312 static struct proc_dir_entry *mmcd;
313 static struct file_operations mmc_proc_fops = {
314 read: mmc_proc_read,
315 write: NULL
318 //=================================================================================================
320 /*-----------------------------------------------------------------------------
321 * Module open function
322 *-----------------------------------------------------------------------------*/
323 int mmc_open(struct inode *inode, struct file *filp) {
324 (void)filp; // Supress compiler warning
325 (void)inode; // Supress compiler warning
327 if ((card->state & CARD_STATE_DET) == 0) {
328 LOG_DEBUG(DBG_OPEN, "open: no device\n");
329 return -ENODEV;
331 LOG_DEBUG(DBG_OPEN, "open: use_count: old=%d new=%d\n", MOD_IN_USE, MOD_IN_USE + 1);
332 MOD_INC_USE_COUNT;
334 return 0;
338 /*-----------------------------------------------------------------------------
339 * Module release function
340 *-----------------------------------------------------------------------------*/
341 int mmc_release(struct inode *inode, struct file *filp) {
342 (void)filp; // Supress compiler warning
344 LOG_DEBUG(DBG_RLSE, "release: use_count: old=%d new=%d\n", MOD_IN_USE, MOD_IN_USE - 1);
345 fsync_dev(inode->i_rdev);
346 invalidate_buffers(inode->i_rdev);
347 MOD_DEC_USE_COUNT;
348 return 0;
352 /*-----------------------------------------------------------------------------
353 * Module ioctl function
354 *-----------------------------------------------------------------------------*/
355 int mmc_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) {
356 (void)filp; // supress compiler warning
358 LOG_DEBUG(DBG_IOCTL, "ioctl: cmd=%02x arg=%08lx\n", cmd, arg);
360 if (!inode || !inode->i_rdev) return -EINVAL;
362 switch(cmd) {
363 case BLKGETSIZE:
364 return put_user(mmc_part[MINOR(inode->i_rdev)].nr_sects, (unsigned long *)arg);
365 case BLKGETSIZE64:
366 return put_user((u64)mmc_part[MINOR(inode->i_rdev)].nr_sects, (u64 *) arg);
367 case BLKRRPART:
368 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
369 return mmc_revalidate(inode->i_rdev);
370 case HDIO_GETGEO:
372 struct hd_geometry *loc, g;
373 loc = (struct hd_geometry *) arg;
374 if (!loc) return -EINVAL;
375 g.heads = 4;
376 g.sectors = 16;
377 g.cylinders = mmc_part[0].nr_sects / (4 * 16);
378 g.start = mmc_part[MINOR(inode->i_rdev)].start_sect;
379 return copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0;
381 default:
382 return blk_ioctl(inode->i_rdev, cmd, arg);
387 /*-----------------------------------------------------------------------------
388 * Module check change function
389 *-----------------------------------------------------------------------------*/
390 int mmc_check_change(kdev_t i_rdev) {
391 (void)i_rdev; // Supress compiler warning
393 LOG_DEBUG(DBG_CHG, "check_change: called\n");
394 return 0;
398 /*-----------------------------------------------------------------------------
399 * Module revalidate function
400 *-----------------------------------------------------------------------------*/
401 int mmc_revalidate(kdev_t i_rdev) {
402 int i, minor;
404 if ((card->state & CARD_STATE_DET) == 0) {
405 LOG_DEBUG(DBG_REVAL, "revalidate: no device\n");
406 return -ENODEV;
409 LOG_DEBUG(DBG_REVAL, "revalidate: device=%d\n", DEVICE_NR(i_rdev));
411 // Invalidate all partitions for device
412 for (i = (1 << PSHIFT) - 1; i >= 0; i--) {
413 minor = (DEVICE_NR(i_rdev) << PSHIFT) + i;
414 invalidate_device(MKDEV(MAJOR_NR, minor), 1);
415 mmc_gendisk.part[minor].start_sect = 0;
416 mmc_gendisk.part[minor].nr_sects = 0;
419 // Try to re-read partition table
420 grok_partitions(&mmc_gendisk, DEVICE_NR(i_rdev), (1 << PSHIFT), mmc_part[0].nr_sects);
422 return 0;
426 /*-----------------------------------------------------------------------------
427 * Module request function
428 *-----------------------------------------------------------------------------*/
429 static void mmc_request(request_queue_t *q) {
430 unsigned int card_address;
431 unsigned char *buffer_address;
432 unsigned char first, last;
433 unsigned int sector;
434 unsigned int nr_sectors;
435 unsigned int part_sector;
436 struct request * req;
437 struct buffer_head * buf_head;
438 int cmd;
439 int r, code;
440 (void)q; // Supress compiler warning
442 while (1) {
443 INIT_REQUEST; // More requests on queue? Return if not...
445 // Process all the sectors clustered in the current request
446 first = 1; // About to process first buffer in request.
447 while (1) {
448 code = 1; // Default return code is success
450 // Extract current request information
451 req = CURRENT; // Pointer to current request
452 cmd = req->cmd; // Command
453 sector = req->sector; // Starting sector of request
454 nr_sectors = req->current_nr_sectors; // Number of sectors in request
455 buffer_address = req->buffer; // Buffer address
456 part_sector = mmc_part[MINOR(req->rq_dev)].start_sect; // Starting sector of requested partition
457 buf_head = req->bh; // Buffer head pointer
458 last = (buf_head->b_reqnext == NULL) ? 1 : 0; // Determine if last buffer
460 // Calculate card address - byte addressing for standard density, block for high density.
461 card_address = (unsigned int)(part_sector + sector); // Assume block addressing
462 if (!(card->type & CARD_TYPE_HC)) card_address <<= 9; // Multiply by 512 if byte addressing
464 LOG_DEBUG(DBG_REQ, "Request: cmd=%02x sector=%08x nr_sectors=%08x minor=%02x start_sect=%08x card_address=%08x\n", cmd, sector, nr_sectors, MINOR(req->rq_dev), part_sector, card_address);
466 if (!(card->state & CARD_STATE_DET)) {
467 LOG_ERR("Request: IO request but no MM/SD card detected!\n");
468 code = 0;
469 } else if ((part_sector + sector + nr_sectors) > mmc_part[0].nr_sects) {
470 LOG_ERR("Request: Sector out of range!\n");
471 code = 0;
472 } else if (cmd == READ) {
473 spin_unlock_irq(&io_request_lock);
474 r = spi_mmc_read_multi_o(card_address, buffer_address, nr_sectors, first, last);
475 if (r != 0) {
476 LOG_ERR("Request: protocol error - cmd=read rc=%02x\n", r);
477 code = 0;
479 spin_lock_irq(&io_request_lock);
481 } else if (cmd == WRITE) {
482 spin_unlock_irq(&io_request_lock);
483 r = spi_mmc_write_multi_o(card_address, buffer_address, nr_sectors, first, last);
484 if (r != 0) {
485 LOG_ERR("Request: protocol error - cmd=write rc=%02x\n", r);
486 code = 0;
488 spin_lock_irq(&io_request_lock);
489 } else {
490 code = 0; // Kernel passing an unknown cmd - no way!
493 // Buffer transfered. End the request and break out of loop if no more
494 // buffers clustered in the rquest.
495 if (end_that_request_first(req, code, DEVICE_NAME) == 0) break;
497 first = 0;
500 // All clusters in this request have been processed - tell the kernel.
501 blkdev_dequeue_request(req);
502 end_that_request_last(req);
507 /*-----------------------------------------------------------------------------
508 * Module /proc/mmc/status read function
509 *-----------------------------------------------------------------------------*/
510 static ssize_t mmc_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos) {
511 (void)file; // supress compiler warning
512 char *page;
513 int len = 0;
515 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
516 return -ENOBUFS;
518 // Following are used for mapping bit patterns to strings for printing
519 char * typ_to_str[] = { "", "SD", "MM" }; // SD/MM
520 char * cap_to_str[] = { "", "HC" }; // std/high capacity
521 char * sdv_to_str[] = { "1.00 - 1.01", "1.10", "2.0" }; // SD version
522 char * mmv_to_str[] = { "1.0 - 1.2", "1.4", "2.0 - 2.2", "3.0 - 3.3", "4.1 - 4.3" }; // MM version
523 char * mon_to_str[] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
525 // calc min/max voltage (times 10) from the OCR voltage window bits.
526 unsigned int voltmin = 0;
527 unsigned int voltmax = 0;
528 unsigned int j;
529 for (j = 4; j < 24 ; j++) {
530 if ( (0x00000001 << j) & card->volt ) {
531 voltmax = j + 13;
532 voltmin = (voltmin == 0) ? j + 12 : voltmin;
536 len = sprintf(page,
537 "Card Type : %s%s\n"
538 "Spec Version : %s\n"
539 "Card Size : %d MB\n"
540 "Block Size : 512 bytes\n"
541 "Num. of Blocks : %d\n"
542 "Voltage Range : %d.%d-%d.%d\n"
543 "Manufacture ID : %02x\n"
544 "Application ID : %.2s\n"
545 "Product Name : %.6s\n"
546 "Revision : %d.%d\n"
547 "Serial Number : %08x\n"
548 "Manu. Date : %s %d\n",
549 typ_to_str[card->type >> 4], cap_to_str[card->type & 0x0f],
550 (card->type & CARD_TYPE_SD) ? sdv_to_str[card->version] : mmv_to_str[card->version],
551 card->blocks / 2 / 1024,
552 card->blocks,
553 voltmin/10, voltmin%10, voltmax/10, voltmax%10,
554 card->manid,
555 card->appid,
556 card->name,
557 (card->rev >> 4), (card->rev & 0x0f),
558 card->serial,
559 mon_to_str[card->month],card->year
562 len += 1;
563 if (*ppos < len) {
564 len = min_t(int, len - *ppos, count);
565 if (copy_to_user(buf, (page + *ppos), len)) {
566 kfree(page);
567 return -EFAULT;
569 *ppos += len;
570 } else {
571 len = 0;
574 kfree(page);
575 return len;
579 /*-----------------------------------------------------------------------------
580 * Module initialization function
581 *-----------------------------------------------------------------------------*/
582 int mmc_init(void) {
583 int i, r;
584 static struct proc_dir_entry *p;
586 // Log the module version and parameters
587 #ifdef DEBUG
588 LOG_INFO("Version: " VERSION " Parms: major=%d din=%d dout=%d clk=%d cs=%d maxsec=%d rahead=%d dbg=%04x\n", major, din, dout, clk, cs, maxsec, rahead, dbg);
589 #else
590 LOG_INFO("Version: " VERSION " Parms: major=%d din=%d dout=%d clk=%d cs=%d maxsec=%d rahead=%d\n", major, din, dout, clk, cs, maxsec, rahead);
591 #endif
593 // Initialize spi module - set gpio pins used
594 spi_init();
596 // Try and initialize card - returns a card info structure on success.
597 if ((r = init_card(&card))) {
598 // Give it one more shot!
599 if (card) kfree(card);
600 if ((r = init_card(&card))) goto err1;
603 // Initialize the /proc entries for this module
604 if (!(mmcd = proc_mkdir("mmc", NULL))) {
605 LOG_ERR("Failure creating /proc/mmc\n");
606 r = -EPROTO;
607 goto err1;
609 if (!(p = create_proc_entry("status", S_IRUSR, mmcd))) {
610 LOG_ERR("Failure creating /proc/mmc/status\n");
611 r = -EPROTO;
612 goto err1;
614 p->proc_fops = &mmc_proc_fops;
616 // Register block device. Creates entry in /proc/devices and assigns device major num
617 r = register_blkdev(MAJOR_NR, DEVICE_NAME, &mmc_bdops);
618 if (r < 0) {
619 LOG_ERR("Failure requesting major %d - rc=%d\n", MAJOR_NR, r);
620 goto err1;
622 if (MAJOR_NR == 0) {
623 MAJOR_NR = r; // If requested major was 0, assign dynamic major num
624 LOG_INFO("Assigned dynamic major number %d\n", major);
626 mmc_gendisk.major = MAJOR_NR; // Can now set major number in gendisk structure
628 // initialize device structures
629 memset(mmc_sizes, 0, sizeof(mmc_sizes));
630 memset(mmc_part, 0, sizeof(mmc_part));
631 for (i = 0; i < (1 << PSHIFT); i++) mmc_maxsect[i] = maxsec;
632 mmc_sizes[0] = card->blocks / 2; // number of 1024 byte blocks
633 mmc_part[0].nr_sects = card->blocks; // number of 512 byte sectors
634 read_ahead[MAJOR_NR] = rahead;
635 blk_size[MAJOR_NR] = mmc_sizes;
636 blksize_size[MAJOR_NR] = mmc_blocksizes;
637 hardsect_size[MAJOR_NR] = mmc_hardsectsizes;
638 max_sectors[MAJOR_NR] = mmc_maxsect;
640 // Initialize the block request queue
641 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), mmc_request);
643 // Put gendisk structure on the head of the list
644 add_gendisk(&mmc_gendisk);
646 // Register the disk - system will now read the partition table using mmc_request
647 register_disk(&mmc_gendisk, MKDEV(MAJOR_NR,0), (1 << PSHIFT), &mmc_bdops, mmc_part[0].nr_sects);
649 LOG_INFO("Module loaded\n");
651 return 0; //success
653 /* ----- Error handling ----- */
654 err1:
655 if (card) kfree(card);
656 LOG_INFO("Module unloaded due to error\n");
657 return r;
660 /*-----------------------------------------------------------------------------
661 * Module exit function
662 *-----------------------------------------------------------------------------*/
663 void mmc_exit(void) {
664 unregister_blkdev(MAJOR_NR, DEVICE_NAME);
665 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
666 if (card) kfree(card);
667 remove_proc_entry("status", mmcd);
668 remove_proc_entry("mmc", NULL);
669 LOG_INFO("Module unloaded\n");
673 /*-----------------------------------------------------------------------------
674 * Specify the functions to call for module initialization and exit
675 *-----------------------------------------------------------------------------*/
676 module_init(mmc_init);
677 module_exit(mmc_exit);
679 MODULE_LICENSE("GPL");
680 MODULE_AUTHOR("madsuk/Rohde/Cyril CATTIAUX/Marc DENTY/rcichielo KRUSCH/Chris");
681 MODULE_DESCRIPTION("MMHC/SDHC Card Block Driver - " VERSION);
682 MODULE_SUPPORTED_DEVICE("WRT54GL");