Unformatted text preview:

Systems Programming Linux Device Drivers V CMPE 3101 (May 11, 2000 2:08 pm)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Hardware ManagementWe will develop code that is designed to control the parallel port.First we will look at the kernel functions that allow communication to 8-bit,16-bit and 32-bit I/O ports.unsigned inb(unsigned port);void outb(unsigned char byte, unsigned port);Read and write 8-bit wide ports.The port argument is defined as unsigned long on some andunsigned short on other platforms.unsigned inw(unsigned port);void outw(unsigned short word, unsigned port);The 16-bit versions.unsigned inl(unsigned port);void outl(unsigned doubleword, unsigned port);The 32-bit versions in which doubleword is defined as unsigned longor unsigned int, according to the platform.Systems Programming Linux Device Drivers V CMPE 3102 (May 11, 2000 2:08 pm)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Parallel PortOn the 80x86, it is possible that the processor tries to transfer data too quicklyto and from the bus, particularly if the I/O instructions are back-to-back.There are inb_p and outb_p (pause) versions of the above functions.You can use SLOW_DOWN_IO macro to add the delay also.There are also string functions defi ned as follows:void insb(unsigned port, void *addr, unsigned longcnt);void outsb(unsigned port, void *addr, unsigned longcnt);Along with the w and l versions for 16-bit and 32-bit transfers.Parallel PortThe parallel port, in its minimal confi guration, is made up of a few 8-bitports.Data written to the output port shows up on the output pins of the 25-pinconnector at standard TTL levels (0 and 5 volts w/ threshold of 1.2 V).Systems Programming Linux Device Drivers V CMPE 3103 (May 11, 2000 2:08 pm)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Parallel PortThe parallel port specifi cation.13121110987625242322212019185432117161514Control Port: base_addr+201234567irq enable01234567Status Port: base_addr+101234567Data Port: base_addr+0234567898101213 15Output ControlInput Status1117 16141Input DataInvertedDB25Systems Programming Linux Device Drivers V CMPE 3104 (May 11, 2000 2:08 pm)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Interrupt HandlingControl Port bit 4 is not connected to the DB25 connector, but rather it is usedto enable interrupts.outb is used at module initialization time to do this.The device generates an interrupt by raising pin 10 (the “ACK” bit).Of course, nothing will happen until an interrupt service routine has beeninstalled.Linux acknowledges and ignores any unexpected interrupts.There are only 15 or 16 interrupt lines and the kernel keeps a registry of inter-rupt lines (similar to the I/O port registry).Systems Programming Linux Device Drivers V CMPE 3105 (May 11, 2000 2:08 pm)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Obtaining Interrupt Request NumbersA module requests and frees an interrupt channel (IRQ) using functionsdeclared in <linux/sched.h>:extern int request_irq(unsigned int irq, void (*handler)(int, void *, struct pt_regs *), unsigned long flags, const char *device, void *dev_id);extern void free_irq(unsigned int irq, void *dev_id);• irq is the requested IRQ (which may not correspond to the hardware num-ber).• handler is a pointer to the ISR.• flags is a bitmask of options related to interrupt management.• device is the string used in /proc/interrupts to show the owner of the inter-rupt.• dev_id is a unique identifi er used for shared interrupt lines and is usuallyset to NULL.Systems Programming Linux Device Drivers V CMPE 3106 (May 11, 2000 2:08 pm)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Obtaining Interrupt Request NumbersThe bits that can be set in flags are: SA_INTERRUPTWhen set, this indicates a “fast” interrupt handler (cli issued), otherwiseit is “slow” (sti enables processor to handle other types of interrupts). SA_SHIRQIndicates that the interrupt can be shared between devices. SA_SAMPLE_RANDOMIndicates that the generated interrupts contribute to the entropy poolused by application software to choose secure keys for encryption.A 0 return value from request_irq indicates success while a negative numberindicates an error.e.g., -EBUSY indicates another driver is using the IRQ requested.The interrupt handler is usually not installed from within init_module, sincethe device may never use it.Because interrupt lines are a limited resource, the installation is usuallydone on the fi rst open call.Systems Programming Linux Device Drivers V CMPE 3107 (May 11, 2000 2:08 pm)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Obtaining Interrupt Request NumbersThe request_irq should appear in the open call before the hardware isinstructed to generate interrupts.The short module (Simple Hardware Operations and Raw Tests) actuallyinstalls the ISR in init_module for simplicity.if (short_irq >= 0) { result = request_irq(short_irq, short_interrupt, SA_INTERRUPT, "short", NULL); if (result) { printk(KERN_INFO "short: can’t get assigned \ irq %i\n",short_irq); short_irq = -1; } else /* Enable interrupts(assume parallel port) */ { outb(0x10, short_base+2); } }Systems Programming Linux Device Drivers V CMPE 3108 (May 11, 2000 2:08 pm)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Obtaining Interrupt Request NumbersThis code shows the handler being installed as a “fast” handler , without sup-port for interrupt sharing or contributing to system entropy.Hardware interrupts are counted, and reported in /proc/interrupts fi le.A sample of mine looks like: CPU0 0: 93487704 XT-PIC timer 1: 239645 XT-PIC keyboard 2: 0 XT-PIC cascade 9: 4961908 XT-PIC SMC EPIC/100 12: 1668143 XT-PIC PS/2 Mouse 14: 1550375 XT-PIC ide0NMI: 0The fi rst column indicates the interrupt number.The second the number of times the interrupt occurred.The third is the chip.The fourth is the identifi cation string.Systems Programming Linux Device Drivers V CMPE 3109 (May 11, 2000 2:08 pm)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Obtaining Interrupt Request NumbersA second fi le that reports this (and other) data in a different format is /proc/stat.A sample of mine looks like:intr 101942156 93507692 240219 0 2 6 0 3 0 1 4963072 00


View Full Document

UNM CMPE 310 - CMPE 310 LECTURE NOTES

Download CMPE 310 LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CMPE 310 LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CMPE 310 LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?