First the desk at which an official tried cases, then the cases themselves. Chan Buddhists borrowed the word for recorded exchanges between masters and students, studied like legal precedents.
公案小说 are old crime stories about upright judges such as
包公.