The invention is substituted 4,5-dihydro- and 4,5,6,7-tetrahydro-pyrazolo[1,5-α]pyrimidine compounds of formula (I),and salts thereof, compositions thereof, and methods of use therefor, such as inhibiting protein kinase, including Bruton's tyrosine kinase (Btk), and for treating disorders mediated thereby.