I am sure you already know the easy answer--God can quote Himself any way He wants. However, let try to give a more complete explanation of what is happening here.
I believe that the opening of the ear in Psalm 40:6 refers to a practice found in Deuteronomy 15:16-17. This passage refers to a Hebrew servant who has served for seven years and has come to the time when the law states that he is to be released from his servitude. However, in this passage, the servant who is free by law desires to remain as a servant because of his love for his master. Consider the verses:
Deuteronomy 15:16 And it shall be, if he say unto thee, I will not go away from thee; because he loveth thee and thine house, because he is well with thee;
17 Then thou shalt take an aul, and thrust it through his ear unto the door, and he shall be thy servant for ever. And also unto thy maidservant thou shalt do likewise.
As you can see, he chooses to serve though he is not liable under the law to do so. As a sign of his servitude, he is to have an aul thrust through his ear into the door. This "piercing" of the ear makes him a servant of his master forever. He no longer belongs to himself but has surrendered control of his body to his master.
When you think of it, this is much what Jesus Christ did when He surrendered to be born in a body of human flesh. He was not required to do so by law. However, because of His love for the Father and His desire to be of service to Him, He surrendered to be made in the likeness of men and come in the form of a servant. Philippians 2:7 states, "But made himself of no reputation, and took upon him the form of a servant, and was made in the likeness of men." This verse shows that the two things--becoming a servant and being born as a man--are one and the same for Jesus Christ.
Just as the servant in Deuteronomy gave himself bodily to his master by the opening of the ear, so the Lord Jesus Christ did the same by taking the body prepared for Him by God--that is, by being born as a baby in the manger. In John 6:38, Jesus testified, "For I came down from heaven, not to do mine own will, but the will of him that sent me." The coming down from heaven was His incarnation (or birth as a human). He came to do the will of the Father--that is the work of a servant. Again, the two are the same event. When He took on a body of flesh, He was also placing His ear against the door so that the aul of eternal service could be hammered through His ear. Praise the Lord!