// called from kernel to initialize translate module. taken from scull static int translate_init(void) { int result = EXIT_SUCCESS, i; dev_t dev; DEBUG(printk(KERN_NOTICE "translate_init()\n")); DEBUG(printk(KERN_NOTICE "translate_init: param subst = %s \n",translate_subst)); DEBUG(printk(KERN_NOTICE "translate_init: param bufsize = %d \n",translate_bufsize)); result = alloc_chrdev_region(&dev, MINOR_BEGINNING, NO_OF_DEVICES,"translate\n"); translate_major = MAJOR(dev); if (result != EXIT_SUCCESS) { DEBUG(printk(KERN_ALERT "translate_init: error(%d) getting major %d \n", result, translate_major)); return result; } // allocate memory for the devices translate_devs = kmalloc(NO_OF_DEVICES * sizeof(struct translate_dev),GFP_KERNEL); if (!translate_devs) { result = -ENOMEM; goto fail; } // reset contents of device memset(translate_devs, 0, NO_OF_DEVICES * sizeof(struct translate_dev)); // initialize each device (in translate its only two) for (i = 0; i < NO_OF_DEVICES; i++) { struct translate_dev *dev = &translate_devs[i]; // allocate buffer (just like with the device memory) dev->buffer = kmalloc(translate_bufsize, GFP_KERNEL); if (!(dev->buffer)) { result = -ENOMEM; goto fail; } dev->items = 0; dev->read_pos = dev->buffer; dev->write_pos = dev->buffer; // init semaphores sema_init(&dev->reader_open_lock, NUM_SIMULT_ACCESS_USERS); sema_init(&dev->writer_open_lock, NUM_SIMULT_ACCESS_USERS); translate_setup_cdev(&translate_devs[i], i); DEBUG(printk(KERN_NOTICE "translate_init: translate dev %d initialized", i)); } DEBUG(printk(KERN_NOTICE "translate_init: translate initialized")); return EXIT_SUCCESS; fail: translate_cleanup(); return result; }
//regestriert jede vom Modul bereitgestellte Fähigkeiten static int translate_init(void) { int result; dev_t dev; int i; #ifdef DEBUG_MESSAGES printk(KERN_NOTICE "--- translate_init called ---\n"); #endif #ifdef DEBUG_MESSAGES printk(KERN_NOTICE "translate_init: subst = %s \n",translate_subst); #endif #ifdef DEBUG_MESSAGES printk(KERN_NOTICE "translate_init: bufsize = %d \n",translate_bufsize); #endif //Majonummer holen result = alloc_chrdev_region(&dev, TRANSLATE_FIRST_MINOR, TRANSLATE_NUMBER_OF_DEVS,"translate\n"); translate_major = MAJOR(dev); if (result < 0) { #ifdef DEBUG_MESSAGES printk(KERN_ALERT "translate: can`t get major %d \n",translate_major); #endif return result; } /*allocate memory for both structs an get the pointer*/ translate_devs = kmalloc(TRANSLATE_NUMBER_OF_DEVS * sizeof(struct translate_dev),GFP_KERNEL); if (!translate_devs) { result = -ENOMEM; goto fail; } /*overwrite the device memory with 0*/ memset(translate_devs, 0, TRANSLATE_NUMBER_OF_DEVS * sizeof(struct translate_dev)); /*init the member of both devices (translate0,translate1)*/ for (i = 0; i < TRANSLATE_NUMBER_OF_DEVS; i++) { /*init the semaphores*/ sema_init(&translate_devs[i].reader_open_lock, 1); sema_init(&translate_devs[i].writer_open_lock, 1); sema_init(&translate_devs[i].itemsInBuffer, 0); sema_init(&translate_devs[i].freeBufferSpace, translate_bufsize); /*allocate memory for the buffer an set the pointer*/ translate_devs[i].buffer = kmalloc(translate_bufsize, GFP_KERNEL); if (!translate_devs[i].buffer) { result = -ENOMEM; goto fail; } /*init the other member of the struct*/ translate_devs[i].items = 0; translate_devs[i].read_pos = translate_devs[i].buffer; translate_devs[i].write_pos = translate_devs[i].buffer; translate_setup_cdev(&translate_devs[i], i); } return 0; fail: translate_cleanup(); return result; }
// Kernel initialisierung des Translate Modules. Aus Scull kopiert static int translate_init(void) { int result = EXIT_SUCCESS, i; dev_t dev; #ifdef DEBUG_MESSAGES printk(KERN_NOTICE "translate_init()\n"); printk(KERN_NOTICE "translate_init: param subst = %s \n",translate_subst); printk(KERN_NOTICE "translate_init: param bufsize = %d \n",translate_bufsize); #endif result = alloc_chrdev_region(&dev, MINOR_START_IDX, NO_OF_DEVICES,"translate\n"); translate_major = MAJOR(dev); if (result != EXIT_SUCCESS) { #ifdef DEBUG_MESSAGES printk(KERN_ALERT "translate_init: error(%d) getting major %d \n", result, translate_major); #endif return result; } // Speicher fuer die Devices allozieren translate_devs = kmalloc(NO_OF_DEVICES * sizeof(struct translate_dev),GFP_KERNEL); if (!translate_devs) { result = -ENOMEM; goto fail; } // Inhalt der Device zuruecksetzen memset(translate_devs, 0, NO_OF_DEVICES * sizeof(struct translate_dev)); // Initialisieren der Devices for (i = 0; i < NO_OF_DEVICES; i++) { struct translate_dev *dev = &translate_devs[i]; // Allocate Buffer dev->buffer = kmalloc(translate_bufsize, GFP_KERNEL); if (!(dev->buffer)) { result = -ENOMEM; goto fail; } dev->items = 0; dev->read_pos = dev->buffer; dev->write_pos = dev->buffer; // Initialisieren der Semaphore sema_init(&dev->reader_open_lock, 1); sema_init(&dev->writer_open_lock, 1); translate_setup_cdev(&translate_devs[i], i); #ifdef DEBUG_MESSAGES printk(KERN_NOTICE "translate_init: translate dev %d initialized", i); #endif } #ifdef DEBUG_MESSAGES printk(KERN_NOTICE "translate_init: translate initialized"); #endif return EXIT_SUCCESS; fail: translate_cleanup(); return result; }