1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * MCP2221A - Microchip USB to I2C Host Protocol Bridge 4 * 5 * Copyright (c) 2020, Rishi Gupta <gupt21@gmail.com> 6 * 7 * Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/20005565B.pdf 8 */ 9 10 #include <linux/module.h> 11 #include <linux/err.h> 12 #include <linux/mutex.h> 13 #include <linux/completion.h> 14 #include <linux/delay.h> 15 #include <linux/hid.h> 16 #include <linux/hidraw.h> 17 #include <linux/i2c.h> 18 #include "hid-ids.h" 19 20 /* Commands codes in a raw output report */ 21 enum { 22 MCP2221_I2C_WR_DATA = 0x90, 23 MCP2221_I2C_WR_NO_STOP = 0x94, 24 MCP2221_I2C_RD_DATA = 0x91, 25 MCP2221_I2C_RD_RPT_START = 0x93, 26 MCP2221_I2C_GET_DATA = 0x40, 27 MCP2221_I2C_PARAM_OR_STATUS = 0x10, 28 MCP2221_I2C_SET_SPEED = 0x20, 29 MCP2221_I2C_CANCEL = 0x10, 30 }; 31 32 /* Response codes in a raw input report */ 33 enum { 34 MCP2221_SUCCESS = 0x00, 35 MCP2221_I2C_ENG_BUSY = 0x01, 36 MCP2221_I2C_START_TOUT = 0x12, 37 MCP2221_I2C_STOP_TOUT = 0x62, 38 MCP2221_I2C_WRADDRL_TOUT = 0x23, 39 MCP2221_I2C_WRDATA_TOUT = 0x44, 40 MCP2221_I2C_WRADDRL_NACK = 0x25, 41 MCP2221_I2C_MASK_ADDR_NACK = 0x40, 42 MCP2221_I2C_WRADDRL_SEND = 0x21, 43 MCP2221_I2C_ADDR_NACK = 0x25, 44 MCP2221_I2C_READ_COMPL = 0x55, 45 }; 46 47 /* 48 * There is no way to distinguish responses. Therefore next command 49 * is sent only after response to previous has been received. Mutex 50 * lock is used for this purpose mainly. 51 */ 52 struct mcp2221 { 53 struct hid_device *hdev; 54 struct i2c_adapter adapter; 55 struct mutex lock; 56 struct completion wait_in_report; 57 u8 *rxbuf; 58 u8 txbuf[64]; 59 int rxbuf_idx; 60 int status; 61 u8 cur_i2c_clk_div; 62 }; 63 64 /* 65 * Default i2c bus clock frequency 400 kHz. Modify this if you 66 * want to set some other frequency (min 50 kHz - max 400 kHz). 67 */ 68 static uint i2c_clk_freq = 400; 69 70 /* Synchronously send output report to the device */ 71 static int mcp_send_report(struct mcp2221 *mcp, 72 u8 *out_report, size_t len) 73 { 74 u8 *buf; 75 int ret; 76 77 buf = kmemdup(out_report, len, GFP_KERNEL); 78 if (!buf) 79 return -ENOMEM; 80 81 /* mcp2221 uses interrupt endpoint for out reports */ 82 ret = hid_hw_output_report(mcp->hdev, buf, len); 83 kfree(buf); 84 85 if (ret < 0) 86 return ret; 87 return 0; 88 } 89 90 /* 91 * Send o/p report to the device and wait for i/p report to be 92 * received from the device. If the device does not respond, 93 * we timeout. 94 */ 95 static int mcp_send_data_req_status(struct mcp2221 *mcp, 96 u8 *out_report, int len) 97 { 98 int ret; 99 unsigned long t; 100 101 reinit_completion(&mcp->wait_in_report); 102 103 ret = mcp_send_report(mcp, out_report, len); 104 if (ret) 105 return ret; 106 107 t = wait_for_completion_timeout(&mcp->wait_in_report, 108 msecs_to_jiffies(4000)); 109 if (!t) 110 return -ETIMEDOUT; 111 112 return mcp->status; 113 } 114 115 /* Check pass/fail for actual communication with i2c slave */ 116 static int mcp_chk_last_cmd_status(struct mcp2221 *mcp) 117 { 118 memset(mcp->txbuf, 0, 8); 119 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 120 121 return mcp_send_data_req_status(mcp, mcp->txbuf, 8); 122 } 123 124 /* Cancels last command releasing i2c bus just in case occupied */ 125 static int mcp_cancel_last_cmd(struct mcp2221 *mcp) 126 { 127 memset(mcp->txbuf, 0, 8); 128 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 129 mcp->txbuf[2] = MCP2221_I2C_CANCEL; 130 131 return mcp_send_data_req_status(mcp, mcp->txbuf, 8); 132 } 133 134 static int mcp_set_i2c_speed(struct mcp2221 *mcp) 135 { 136 int ret; 137 138 memset(mcp->txbuf, 0, 8); 139 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 140 mcp->txbuf[3] = MCP2221_I2C_SET_SPEED; 141 mcp->txbuf[4] = mcp->cur_i2c_clk_div; 142 143 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8); 144 if (ret) { 145 /* Small delay is needed here */ 146 usleep_range(980, 1000); 147 mcp_cancel_last_cmd(mcp); 148 } 149 150 return 0; 151 } 152 153 /* 154 * An output report can contain minimum 1 and maximum 60 user data 155 * bytes. If the number of data bytes is more then 60, we send it 156 * in chunks of 60 bytes. Last chunk may contain exactly 60 or less 157 * bytes. Total number of bytes is informed in very first report to 158 * mcp2221, from that point onwards it first collect all the data 159 * from host and then send to i2c slave device. 160 */ 161 static int mcp_i2c_write(struct mcp2221 *mcp, 162 struct i2c_msg *msg, int type, u8 last_status) 163 { 164 int ret, len, idx, sent; 165 166 idx = 0; 167 sent = 0; 168 if (msg->len < 60) 169 len = msg->len; 170 else 171 len = 60; 172 173 do { 174 mcp->txbuf[0] = type; 175 mcp->txbuf[1] = msg->len & 0xff; 176 mcp->txbuf[2] = msg->len >> 8; 177 mcp->txbuf[3] = (u8)(msg->addr << 1); 178 179 memcpy(&mcp->txbuf[4], &msg->buf[idx], len); 180 181 ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4); 182 if (ret) 183 return ret; 184 185 usleep_range(980, 1000); 186 187 if (last_status) { 188 ret = mcp_chk_last_cmd_status(mcp); 189 if (ret) 190 return ret; 191 } 192 193 sent = sent + len; 194 if (sent >= msg->len) 195 break; 196 197 idx = idx + len; 198 if ((msg->len - sent) < 60) 199 len = msg->len - sent; 200 else 201 len = 60; 202 203 /* 204 * Testing shows delay is needed between successive writes 205 * otherwise next write fails on first-try from i2c core. 206 * This value is obtained through automated stress testing. 207 */ 208 usleep_range(980, 1000); 209 } while (len > 0); 210 211 return ret; 212 } 213 214 /* 215 * Device reads all data (0 - 65535 bytes) from i2c slave device and 216 * stores it in device itself. This data is read back from device to 217 * host in multiples of 60 bytes using input reports. 218 */ 219 static int mcp_i2c_smbus_read(struct mcp2221 *mcp, 220 struct i2c_msg *msg, int type, u16 smbus_addr, 221 u8 smbus_len, u8 *smbus_buf) 222 { 223 int ret; 224 u16 total_len; 225 226 mcp->txbuf[0] = type; 227 if (msg) { 228 mcp->txbuf[1] = msg->len & 0xff; 229 mcp->txbuf[2] = msg->len >> 8; 230 mcp->txbuf[3] = (u8)(msg->addr << 1); 231 total_len = msg->len; 232 mcp->rxbuf = msg->buf; 233 } else { 234 mcp->txbuf[1] = smbus_len; 235 mcp->txbuf[2] = 0; 236 mcp->txbuf[3] = (u8)(smbus_addr << 1); 237 total_len = smbus_len; 238 mcp->rxbuf = smbus_buf; 239 } 240 241 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4); 242 if (ret) 243 return ret; 244 245 mcp->rxbuf_idx = 0; 246 247 do { 248 memset(mcp->txbuf, 0, 4); 249 mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 250 251 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 252 if (ret) 253 return ret; 254 255 ret = mcp_chk_last_cmd_status(mcp); 256 if (ret) 257 return ret; 258 259 usleep_range(980, 1000); 260 } while (mcp->rxbuf_idx < total_len); 261 262 return ret; 263 } 264 265 static int mcp_i2c_xfer(struct i2c_adapter *adapter, 266 struct i2c_msg msgs[], int num) 267 { 268 int ret; 269 struct mcp2221 *mcp = i2c_get_adapdata(adapter); 270 271 hid_hw_power(mcp->hdev, PM_HINT_FULLON); 272 273 mutex_lock(&mcp->lock); 274 275 /* Setting speed before every transaction is required for mcp2221 */ 276 ret = mcp_set_i2c_speed(mcp); 277 if (ret) 278 goto exit; 279 280 if (num == 1) { 281 if (msgs->flags & I2C_M_RD) { 282 ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA, 283 0, 0, NULL); 284 } else { 285 ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1); 286 } 287 if (ret) 288 goto exit; 289 ret = num; 290 } else if (num == 2) { 291 /* Ex transaction; send reg address and read its contents */ 292 if (msgs[0].addr == msgs[1].addr && 293 !(msgs[0].flags & I2C_M_RD) && 294 (msgs[1].flags & I2C_M_RD)) { 295 296 ret = mcp_i2c_write(mcp, &msgs[0], 297 MCP2221_I2C_WR_NO_STOP, 0); 298 if (ret) 299 goto exit; 300 301 ret = mcp_i2c_smbus_read(mcp, &msgs[1], 302 MCP2221_I2C_RD_RPT_START, 303 0, 0, NULL); 304 if (ret) 305 goto exit; 306 ret = num; 307 } else { 308 dev_err(&adapter->dev, 309 "unsupported multi-msg i2c transaction\n"); 310 ret = -EOPNOTSUPP; 311 } 312 } else { 313 dev_err(&adapter->dev, 314 "unsupported multi-msg i2c transaction\n"); 315 ret = -EOPNOTSUPP; 316 } 317 318 exit: 319 hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 320 mutex_unlock(&mcp->lock); 321 return ret; 322 } 323 324 static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr, 325 u8 command, u8 *buf, u8 len, int type, 326 u8 last_status) 327 { 328 int data_len, ret; 329 330 mcp->txbuf[0] = type; 331 mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */ 332 mcp->txbuf[2] = 0; 333 mcp->txbuf[3] = (u8)(addr << 1); 334 mcp->txbuf[4] = command; 335 336 switch (len) { 337 case 0: 338 data_len = 5; 339 break; 340 case 1: 341 mcp->txbuf[5] = buf[0]; 342 data_len = 6; 343 break; 344 case 2: 345 mcp->txbuf[5] = buf[0]; 346 mcp->txbuf[6] = buf[1]; 347 data_len = 7; 348 break; 349 default: 350 memcpy(&mcp->txbuf[5], buf, len); 351 data_len = len + 5; 352 } 353 354 ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len); 355 if (ret) 356 return ret; 357 358 if (last_status) { 359 usleep_range(980, 1000); 360 361 ret = mcp_chk_last_cmd_status(mcp); 362 if (ret) 363 return ret; 364 } 365 366 return ret; 367 } 368 369 static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, 370 unsigned short flags, char read_write, 371 u8 command, int size, 372 union i2c_smbus_data *data) 373 { 374 int ret; 375 struct mcp2221 *mcp = i2c_get_adapdata(adapter); 376 377 hid_hw_power(mcp->hdev, PM_HINT_FULLON); 378 379 mutex_lock(&mcp->lock); 380 381 ret = mcp_set_i2c_speed(mcp); 382 if (ret) 383 goto exit; 384 385 switch (size) { 386 387 case I2C_SMBUS_QUICK: 388 if (read_write == I2C_SMBUS_READ) 389 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA, 390 addr, 0, &data->byte); 391 else 392 ret = mcp_smbus_write(mcp, addr, command, NULL, 393 0, MCP2221_I2C_WR_DATA, 1); 394 break; 395 case I2C_SMBUS_BYTE: 396 if (read_write == I2C_SMBUS_READ) 397 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA, 398 addr, 1, &data->byte); 399 else 400 ret = mcp_smbus_write(mcp, addr, command, NULL, 401 0, MCP2221_I2C_WR_DATA, 1); 402 break; 403 case I2C_SMBUS_BYTE_DATA: 404 if (read_write == I2C_SMBUS_READ) { 405 ret = mcp_smbus_write(mcp, addr, command, NULL, 406 0, MCP2221_I2C_WR_NO_STOP, 0); 407 if (ret) 408 goto exit; 409 410 ret = mcp_i2c_smbus_read(mcp, NULL, 411 MCP2221_I2C_RD_RPT_START, 412 addr, 1, &data->byte); 413 } else { 414 ret = mcp_smbus_write(mcp, addr, command, &data->byte, 415 1, MCP2221_I2C_WR_DATA, 1); 416 } 417 break; 418 case I2C_SMBUS_WORD_DATA: 419 if (read_write == I2C_SMBUS_READ) { 420 ret = mcp_smbus_write(mcp, addr, command, NULL, 421 0, MCP2221_I2C_WR_NO_STOP, 0); 422 if (ret) 423 goto exit; 424 425 ret = mcp_i2c_smbus_read(mcp, NULL, 426 MCP2221_I2C_RD_RPT_START, 427 addr, 2, (u8 *)&data->word); 428 } else { 429 ret = mcp_smbus_write(mcp, addr, command, 430 (u8 *)&data->word, 2, 431 MCP2221_I2C_WR_DATA, 1); 432 } 433 break; 434 case I2C_SMBUS_BLOCK_DATA: 435 if (read_write == I2C_SMBUS_READ) { 436 ret = mcp_smbus_write(mcp, addr, command, NULL, 437 0, MCP2221_I2C_WR_NO_STOP, 1); 438 if (ret) 439 goto exit; 440 441 mcp->rxbuf_idx = 0; 442 mcp->rxbuf = data->block; 443 mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 444 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 445 if (ret) 446 goto exit; 447 } else { 448 if (!data->block[0]) { 449 ret = -EINVAL; 450 goto exit; 451 } 452 ret = mcp_smbus_write(mcp, addr, command, data->block, 453 data->block[0] + 1, 454 MCP2221_I2C_WR_DATA, 1); 455 } 456 break; 457 case I2C_SMBUS_I2C_BLOCK_DATA: 458 if (read_write == I2C_SMBUS_READ) { 459 ret = mcp_smbus_write(mcp, addr, command, NULL, 460 0, MCP2221_I2C_WR_NO_STOP, 1); 461 if (ret) 462 goto exit; 463 464 mcp->rxbuf_idx = 0; 465 mcp->rxbuf = data->block; 466 mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 467 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 468 if (ret) 469 goto exit; 470 } else { 471 if (!data->block[0]) { 472 ret = -EINVAL; 473 goto exit; 474 } 475 ret = mcp_smbus_write(mcp, addr, command, 476 &data->block[1], data->block[0], 477 MCP2221_I2C_WR_DATA, 1); 478 } 479 break; 480 case I2C_SMBUS_PROC_CALL: 481 ret = mcp_smbus_write(mcp, addr, command, 482 (u8 *)&data->word, 483 2, MCP2221_I2C_WR_NO_STOP, 0); 484 if (ret) 485 goto exit; 486 487 ret = mcp_i2c_smbus_read(mcp, NULL, 488 MCP2221_I2C_RD_RPT_START, 489 addr, 2, (u8 *)&data->word); 490 break; 491 case I2C_SMBUS_BLOCK_PROC_CALL: 492 ret = mcp_smbus_write(mcp, addr, command, data->block, 493 data->block[0] + 1, 494 MCP2221_I2C_WR_NO_STOP, 0); 495 if (ret) 496 goto exit; 497 498 ret = mcp_i2c_smbus_read(mcp, NULL, 499 MCP2221_I2C_RD_RPT_START, 500 addr, I2C_SMBUS_BLOCK_MAX, 501 data->block); 502 break; 503 default: 504 dev_err(&mcp->adapter.dev, 505 "unsupported smbus transaction size:%d\n", size); 506 ret = -EOPNOTSUPP; 507 } 508 509 exit: 510 hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 511 mutex_unlock(&mcp->lock); 512 return ret; 513 } 514 515 static u32 mcp_i2c_func(struct i2c_adapter *adapter) 516 { 517 return I2C_FUNC_I2C | 518 I2C_FUNC_SMBUS_READ_BLOCK_DATA | 519 I2C_FUNC_SMBUS_BLOCK_PROC_CALL | 520 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC); 521 } 522 523 static const struct i2c_algorithm mcp_i2c_algo = { 524 .master_xfer = mcp_i2c_xfer, 525 .smbus_xfer = mcp_smbus_xfer, 526 .functionality = mcp_i2c_func, 527 }; 528 529 /* Gives current state of i2c engine inside mcp2221 */ 530 static int mcp_get_i2c_eng_state(struct mcp2221 *mcp, 531 u8 *data, u8 idx) 532 { 533 int ret; 534 535 switch (data[idx]) { 536 case MCP2221_I2C_WRADDRL_NACK: 537 case MCP2221_I2C_WRADDRL_SEND: 538 ret = -ENXIO; 539 break; 540 case MCP2221_I2C_START_TOUT: 541 case MCP2221_I2C_STOP_TOUT: 542 case MCP2221_I2C_WRADDRL_TOUT: 543 case MCP2221_I2C_WRDATA_TOUT: 544 ret = -ETIMEDOUT; 545 break; 546 case MCP2221_I2C_ENG_BUSY: 547 ret = -EAGAIN; 548 break; 549 case MCP2221_SUCCESS: 550 ret = 0x00; 551 break; 552 default: 553 ret = -EIO; 554 } 555 556 return ret; 557 } 558 559 /* 560 * MCP2221 uses interrupt endpoint for input reports. This function 561 * is called by HID layer when it receives i/p report from mcp2221, 562 * which is actually a response to the previously sent command. 563 * 564 * MCP2221A firmware specific return codes are parsed and 0 or 565 * appropriate negative error code is returned. Delayed response 566 * results in timeout error and stray reponses results in -EIO. 567 */ 568 static int mcp2221_raw_event(struct hid_device *hdev, 569 struct hid_report *report, u8 *data, int size) 570 { 571 u8 *buf; 572 struct mcp2221 *mcp = hid_get_drvdata(hdev); 573 574 switch (data[0]) { 575 576 case MCP2221_I2C_WR_DATA: 577 case MCP2221_I2C_WR_NO_STOP: 578 case MCP2221_I2C_RD_DATA: 579 case MCP2221_I2C_RD_RPT_START: 580 switch (data[1]) { 581 case MCP2221_SUCCESS: 582 mcp->status = 0; 583 break; 584 default: 585 mcp->status = mcp_get_i2c_eng_state(mcp, data, 2); 586 } 587 complete(&mcp->wait_in_report); 588 break; 589 590 case MCP2221_I2C_PARAM_OR_STATUS: 591 switch (data[1]) { 592 case MCP2221_SUCCESS: 593 if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) && 594 (data[3] != MCP2221_I2C_SET_SPEED)) { 595 mcp->status = -EAGAIN; 596 break; 597 } 598 if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) { 599 mcp->status = -ENXIO; 600 break; 601 } 602 mcp->status = mcp_get_i2c_eng_state(mcp, data, 8); 603 break; 604 default: 605 mcp->status = -EIO; 606 } 607 complete(&mcp->wait_in_report); 608 break; 609 610 case MCP2221_I2C_GET_DATA: 611 switch (data[1]) { 612 case MCP2221_SUCCESS: 613 if (data[2] == MCP2221_I2C_ADDR_NACK) { 614 mcp->status = -ENXIO; 615 break; 616 } 617 if (!mcp_get_i2c_eng_state(mcp, data, 2) 618 && (data[3] == 0)) { 619 mcp->status = 0; 620 break; 621 } 622 if (data[3] == 127) { 623 mcp->status = -EIO; 624 break; 625 } 626 if (data[2] == MCP2221_I2C_READ_COMPL) { 627 buf = mcp->rxbuf; 628 memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]); 629 mcp->rxbuf_idx = mcp->rxbuf_idx + data[3]; 630 mcp->status = 0; 631 break; 632 } 633 mcp->status = -EIO; 634 break; 635 default: 636 mcp->status = -EIO; 637 } 638 complete(&mcp->wait_in_report); 639 break; 640 641 default: 642 mcp->status = -EIO; 643 complete(&mcp->wait_in_report); 644 } 645 646 return 1; 647 } 648 649 static int mcp2221_probe(struct hid_device *hdev, 650 const struct hid_device_id *id) 651 { 652 int ret; 653 struct mcp2221 *mcp; 654 655 mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL); 656 if (!mcp) 657 return -ENOMEM; 658 659 ret = hid_parse(hdev); 660 if (ret) { 661 hid_err(hdev, "can't parse reports\n"); 662 return ret; 663 } 664 665 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); 666 if (ret) { 667 hid_err(hdev, "can't start hardware\n"); 668 return ret; 669 } 670 671 ret = hid_hw_open(hdev); 672 if (ret) { 673 hid_err(hdev, "can't open device\n"); 674 goto err_hstop; 675 } 676 677 mutex_init(&mcp->lock); 678 init_completion(&mcp->wait_in_report); 679 hid_set_drvdata(hdev, mcp); 680 mcp->hdev = hdev; 681 682 /* Set I2C bus clock diviser */ 683 if (i2c_clk_freq > 400) 684 i2c_clk_freq = 400; 685 if (i2c_clk_freq < 50) 686 i2c_clk_freq = 50; 687 mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3; 688 689 mcp->adapter.owner = THIS_MODULE; 690 mcp->adapter.class = I2C_CLASS_HWMON; 691 mcp->adapter.algo = &mcp_i2c_algo; 692 mcp->adapter.retries = 1; 693 mcp->adapter.dev.parent = &hdev->dev; 694 snprintf(mcp->adapter.name, sizeof(mcp->adapter.name), 695 "MCP2221 usb-i2c bridge on hidraw%d", 696 ((struct hidraw *)hdev->hidraw)->minor); 697 698 ret = i2c_add_adapter(&mcp->adapter); 699 if (ret) { 700 hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret); 701 goto err_i2c; 702 } 703 i2c_set_adapdata(&mcp->adapter, mcp); 704 705 return 0; 706 707 err_i2c: 708 hid_hw_close(mcp->hdev); 709 err_hstop: 710 hid_hw_stop(mcp->hdev); 711 return ret; 712 } 713 714 static void mcp2221_remove(struct hid_device *hdev) 715 { 716 struct mcp2221 *mcp = hid_get_drvdata(hdev); 717 718 i2c_del_adapter(&mcp->adapter); 719 hid_hw_close(mcp->hdev); 720 hid_hw_stop(mcp->hdev); 721 } 722 723 static const struct hid_device_id mcp2221_devices[] = { 724 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) }, 725 { } 726 }; 727 MODULE_DEVICE_TABLE(hid, mcp2221_devices); 728 729 static struct hid_driver mcp2221_driver = { 730 .name = "mcp2221", 731 .id_table = mcp2221_devices, 732 .probe = mcp2221_probe, 733 .remove = mcp2221_remove, 734 .raw_event = mcp2221_raw_event, 735 }; 736 737 /* Register with HID core */ 738 module_hid_driver(mcp2221_driver); 739 740 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>"); 741 MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge"); 742 MODULE_LICENSE("GPL v2"); 743