569 * |
567 * |
570 * Result: |
568 * Result: |
571 * On success the return value is 0 else failure. |
569 * On success the return value is 0 else failure. |
572 *-------------------------------------------------------------------------- |
570 *-------------------------------------------------------------------------- |
573 * Author: GG |
571 * Author: GG |
574 * Modification: |
|
575 * 01.10.04 Guard board_in_use with spin_lock, cause of race condition |
|
576 * when compiling for SMP. |
|
577 */ |
572 */ |
578 static int me8100_open(struct inode *inode_ptr, struct file *file_ptr){ |
573 static int me8100_open(struct inode *inode_ptr, struct file *file_ptr){ |
579 int device,subdevice; |
574 int device,subdevice; |
580 me8100_info_type *info; |
575 me8100_info_type *info; |
581 struct me8100_subinfo *subinfo; |
576 struct me8100_subinfo *subinfo; |
582 |
577 |
583 PDEBUG("*** me8100_open() is executed\n"); |
578 PDEBUG("*** me8100_open() is executed\n"); |
584 |
579 |
585 device = DEVICE(MINOR(inode_ptr->i_rdev)); |
580 device = DEVICE(MINOR(inode_ptr->i_rdev)); |
586 subdevice = SUBDEVICE(MINOR(inode_ptr->i_rdev)); |
581 subdevice = SUBDEVICE(MINOR(inode_ptr->i_rdev)); |
|
582 PDEBUG("*** device: %d, subdevice %d\n", device, subdevice); |
|
583 |
587 info = &info_vec[device]; |
584 info = &info_vec[device]; |
588 subinfo = &info->subinfo[subdevice]; |
585 subinfo = &info->subinfo[subdevice]; |
589 |
586 |
590 PDEBUG("*** device: %d, subdevice %d\n", device, subdevice); |
|
591 |
587 |
592 if(device >= me8100_board_count){ |
588 if(device >= me8100_board_count){ |
593 printk(KERN_ERR"ME8100:me8100_open():Board %d doesn't exist\n", device); |
589 printk(KERN_ERR"ME8100:me8100_open():Board %d doesn't exist\n", device); |
594 return -ENODEV; |
590 return -ENODEV; |
595 } |
591 } |
596 |
592 |
597 spin_lock(&info->use_lock); |
593 MOD_INC_USE_COUNT; |
598 if(info->board_in_use){ |
|
599 printk(KERN_ERR "WARNING: ME8100:me8100_open():Board %d already in use\n", device); |
|
600 } |
|
601 ++info->board_in_use; |
|
602 spin_unlock(&info->use_lock); |
|
603 |
594 |
604 if (file_ptr->f_mode & FMODE_WRITE) { |
595 if (file_ptr->f_mode & FMODE_WRITE) { |
|
596 /* If we're the first write, the control register has to be |
|
597 * setup properly. */ |
|
598 |
605 PDEBUG("*** open for writing\n"); |
599 PDEBUG("*** open for writing\n"); |
606 |
600 |
607 if (0 == subinfo->num_writer++) { |
601 if (0 == subinfo->num_writer++) { |
608 subinfo->ctrl_reg |= ME8100_CTRL_ENIO | ME8100_CTRL_SOURCE; |
602 subinfo->ctrl_reg |= ME8100_CTRL_ENIO | ME8100_CTRL_SOURCE; |
609 PDEBUG("*** setting ENIO+SOURCE mode: ctrl: 0x%x\n", subinfo->ctrl_reg); |
603 PDEBUG("*** adding ENIO+SOURCE mode: ctrl: 0x%x\n", subinfo->ctrl_reg); |
610 outw(subinfo->ctrl_reg, subinfo->regbase + ME8100_CTRL_REG); |
604 outw(subinfo->ctrl_reg, subinfo->regbase + ME8100_CTRL_REG); |
611 } |
605 } |
612 } |
606 } |
613 |
607 |
|
608 |
614 if (file_ptr->f_mode & FMODE_READ) { |
609 if (file_ptr->f_mode & FMODE_READ) { |
|
610 /* If we're a reader, the IRQ should be setup -- if not done already |
|
611 * (might be done by some ioctl()s. The file_ptr get a private data |
|
612 * pointer passed. We need this to keep track of timestamps for |
|
613 * read data. */ |
|
614 |
|
615 struct me8100_private_data *priv; |
|
616 |
615 PDEBUG("*** open for reading\n"); |
617 PDEBUG("*** open for reading\n"); |
|
618 |
|
619 if (!(priv = kmalloc(sizeof(*priv), GFP_KERNEL))) { |
|
620 printk(KERN_ERR"ME8100:me8100_open: kmalloc() failed.\n"); |
|
621 me8100_release(inode_ptr, file_ptr); |
|
622 return -EIO; |
|
623 } |
|
624 |
|
625 priv->last_read = 0; |
|
626 file_ptr->private_data = priv; |
|
627 |
|
628 /* Now we've to setup the IRQ line. We suppose that it should be done |
|
629 * in "mask" manner. If somebody wishes to do it the otherway or if somebody |
|
630 * wants to change the mask, ioctl() should be used. */ |
|
631 { |
|
632 int mask = 0xffff; |
|
633 unsigned short icsr = PCI_INT_EN | ((LOCAL_INT_EN | LOCAL_INT_POL) << (3 * subdevice)); |
|
634 |
|
635 |
|
636 /* 1) setup the PLX register */ |
|
637 PDEBUG("*** plx = 0x%0x\n", icsr); |
|
638 outb(icsr, info->plx_regbase + PLX_ICSR); |
|
639 |
|
640 /* 2) setup the irq flags in regbase */ |
|
641 subinfo->ctrl_reg |= ME8100_CTRL_IRQ_MASK; |
|
642 PDEBUG("*** ctrl = 0x%04x\n", subinfo->ctrl_reg); |
|
643 outw(subinfo->ctrl_reg, subinfo->regbase + ME8100_CTRL_REG); |
|
644 |
|
645 /* 3) setup the interrupt mask */ |
|
646 PDEBUG("*** irqmask = 0x%04x\n", mask); |
|
647 outw(mask, subinfo->regbase + ME8100_MASK_REG); |
|
648 |
|
649 } |
616 } |
650 } |
617 |
651 |
618 MOD_INC_USE_COUNT; |
|
619 |
652 |
620 return 0; |
653 return 0; |
621 } |
654 } |
622 |
655 |
623 |
656 |
2111 if(err) |
2146 if(err) |
2112 printk(KERN_WARNING"ME8100:cleanup_module():cannot unregister major\n"); |
2147 printk(KERN_WARNING"ME8100:cleanup_module():cannot unregister major\n"); |
2113 } |
2148 } |
2114 } |
2149 } |
2115 |
2150 |
|
2151 /* Reading: we do only read (at most) one word (an usigned short) and return immediatly, |
|
2152 * thus passing the responsibility for reading a bunch of words back to the caller. |
|
2153 * This seems to be ok, since it's not expected to read more than one word at once |
|
2154 * (the port is one word wide only!) |
|
2155 * |
|
2156 * If a a reader (identified by the file_ptr) calls this functions the first time, |
|
2157 * the current input is returned. Every succsessive read is then put to sleep until |
|
2158 * the status of the input changed. (Unless the IRQ behaviour if the device is changed by |
|
2159 * some ioctl()s.) |
|
2160 */ |
2116 ssize_t me8100_read(struct file * file_ptr, char *buffer, size_t len, loff_t *offset) |
2161 ssize_t me8100_read(struct file * file_ptr, char *buffer, size_t len, loff_t *offset) |
2117 { |
2162 { |
2118 int err; |
2163 int err; |
2119 unsigned short val; |
2164 unsigned short val; |
|
2165 struct me8100_private_data *priv = file_ptr->private_data; |
2120 int minor; |
2166 int minor; |
|
2167 struct me8100_subinfo *subinfo; |
2121 |
2168 |
2122 PDEBUG("me8100_read(%d) called\n", len); |
2169 PDEBUG("me8100_read(%d) called\n", len); |
2123 |
2170 |
2124 minor = MINOR(file_ptr->f_dentry->d_inode->i_rdev); |
2171 minor = MINOR(file_ptr->f_dentry->d_inode->i_rdev); |
|
2172 subinfo = &info_vec[DEVICE(minor)].subinfo[SUBDEVICE(minor)]; |
2125 |
2173 |
2126 if (len == 0) return 0; /* do we really need this check? */ |
2174 if (len == 0) return 0; /* do we really need this check? */ |
2127 if (len < 0) return -EINVAL; /* do we really need this check? */ |
2175 if (len < 0) return -EINVAL; /* do we really need this check? */ |
2128 |
2176 if (priv->last_read >= subinfo->last_change) return 0; /* nothing has changed */ |
2129 val = inw(info_vec[DEVICE(minor)].subinfo[SUBDEVICE(minor)].regbase + ME8100_DI_REG); |
2177 |
2130 PDEBUG("me8100_read: val=0x%0x\n", val); |
2178 val = inw(subinfo->regbase + ME8100_DI_REG); |
|
2179 PDEBUG("me8100_read: val=0x%04x\n", val); |
2131 |
2180 |
2132 if (len >= sizeof(val)) { |
2181 if (len >= sizeof(val)) { |
2133 err = put_user(val, (unsigned short*) buffer); |
2182 err = put_user(val, (unsigned short*) buffer); |
2134 len = sizeof(val); |
2183 len = sizeof(val); |
2135 } else { |
2184 } else { |
2136 err = put_user(val, (char*) buffer); |
2185 err = put_user(val, (char*) buffer); |
2137 } |
2186 } |
2138 |
2187 |
2139 return len; |
2188 return len; |
2140 |
|
2141 } |
2189 } |
2142 |
2190 |
2143 /* Writing: we do only write one word (an unsigned short) and return immediatly. Yes, |
2191 /* Writing: we do only write one word (an unsigned short) and return immediatly. Yes, |
2144 * we could loop over the complete buffer, but it's not expected to get more data |
2192 * we could loop over the complete buffer, but it's not expected to get more data |
2145 * than one word. If there's more output, the responsibility is transferred back |
2193 * than one word. If there's more output, the responsibility is transferred back |