1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Driver for Realtek USB card reader 3 * 4 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved. 5 * 6 * Author: 7 * Roger Tseng <rogerable@realtek.com> 8 */ 9 #include <linux/module.h> 10 #include <linux/slab.h> 11 #include <linux/mutex.h> 12 #include <linux/usb.h> 13 #include <linux/platform_device.h> 14 #include <linux/mfd/core.h> 15 #include <linux/rtsx_usb.h> 16 17 static int polling_pipe = 1; 18 module_param(polling_pipe, int, S_IRUGO | S_IWUSR); 19 MODULE_PARM_DESC(polling_pipe, "polling pipe (0: ctl, 1: bulk)"); 20 21 static const struct mfd_cell rtsx_usb_cells[] = { 22 [RTSX_USB_SD_CARD] = { 23 .name = "rtsx_usb_sdmmc", 24 .pdata_size = 0, 25 }, 26 [RTSX_USB_MS_CARD] = { 27 .name = "rtsx_usb_ms", 28 .pdata_size = 0, 29 }, 30 }; 31 32 static void rtsx_usb_sg_timed_out(struct timer_list *t) 33 { 34 struct rtsx_ucr *ucr = from_timer(ucr, t, sg_timer); 35 36 dev_dbg(&ucr->pusb_intf->dev, "%s: sg transfer timed out", __func__); 37 usb_sg_cancel(&ucr->current_sg); 38 } 39 40 static int rtsx_usb_bulk_transfer_sglist(struct rtsx_ucr *ucr, 41 unsigned int pipe, struct scatterlist *sg, int num_sg, 42 unsigned int length, unsigned int *act_len, int timeout) 43 { 44 int ret; 45 46 dev_dbg(&ucr->pusb_intf->dev, "%s: xfer %u bytes, %d entries\n", 47 __func__, length, num_sg); 48 ret = usb_sg_init(&ucr->current_sg, ucr->pusb_dev, pipe, 0, 49 sg, num_sg, length, GFP_NOIO); 50 if (ret) 51 return ret; 52 53 ucr->sg_timer.expires = jiffies + msecs_to_jiffies(timeout); 54 add_timer(&ucr->sg_timer); 55 usb_sg_wait(&ucr->current_sg); 56 if (!del_timer_sync(&ucr->sg_timer)) 57 ret = -ETIMEDOUT; 58 else 59 ret = ucr->current_sg.status; 60 61 if (act_len) 62 *act_len = ucr->current_sg.bytes; 63 64 return ret; 65 } 66 67 int rtsx_usb_transfer_data(struct rtsx_ucr *ucr, unsigned int pipe, 68 void *buf, unsigned int len, int num_sg, 69 unsigned int *act_len, int timeout) 70 { 71 if (timeout < 600) 72 timeout = 600; 73 74 if (num_sg) 75 return rtsx_usb_bulk_transfer_sglist(ucr, pipe, 76 (struct scatterlist *)buf, num_sg, len, act_len, 77 timeout); 78 else 79 return usb_bulk_msg(ucr->pusb_dev, pipe, buf, len, act_len, 80 timeout); 81 } 82 EXPORT_SYMBOL_GPL(rtsx_usb_transfer_data); 83 84 static inline void rtsx_usb_seq_cmd_hdr(struct rtsx_ucr *ucr, 85 u16 addr, u16 len, u8 seq_type) 86 { 87 rtsx_usb_cmd_hdr_tag(ucr); 88 89 ucr->cmd_buf[PACKET_TYPE] = seq_type; 90 ucr->cmd_buf[5] = (u8)(len >> 8); 91 ucr->cmd_buf[6] = (u8)len; 92 ucr->cmd_buf[8] = (u8)(addr >> 8); 93 ucr->cmd_buf[9] = (u8)addr; 94 95 if (seq_type == SEQ_WRITE) 96 ucr->cmd_buf[STAGE_FLAG] = 0; 97 else 98 ucr->cmd_buf[STAGE_FLAG] = STAGE_R; 99 } 100 101 static int rtsx_usb_seq_write_register(struct rtsx_ucr *ucr, 102 u16 addr, u16 len, u8 *data) 103 { 104 u16 cmd_len = ALIGN(SEQ_WRITE_DATA_OFFSET + len, 4); 105 106 if (!data) 107 return -EINVAL; 108 109 if (cmd_len > IOBUF_SIZE) 110 return -EINVAL; 111 112 rtsx_usb_seq_cmd_hdr(ucr, addr, len, SEQ_WRITE); 113 memcpy(ucr->cmd_buf + SEQ_WRITE_DATA_OFFSET, data, len); 114 115 return rtsx_usb_transfer_data(ucr, 116 usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT), 117 ucr->cmd_buf, cmd_len, 0, NULL, 100); 118 } 119 120 static int rtsx_usb_seq_read_register(struct rtsx_ucr *ucr, 121 u16 addr, u16 len, u8 *data) 122 { 123 int i, ret; 124 u16 rsp_len = round_down(len, 4); 125 u16 res_len = len - rsp_len; 126 127 if (!data) 128 return -EINVAL; 129 130 /* 4-byte aligned part */ 131 if (rsp_len) { 132 rtsx_usb_seq_cmd_hdr(ucr, addr, len, SEQ_READ); 133 ret = rtsx_usb_transfer_data(ucr, 134 usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT), 135 ucr->cmd_buf, 12, 0, NULL, 100); 136 if (ret) 137 return ret; 138 139 ret = rtsx_usb_transfer_data(ucr, 140 usb_rcvbulkpipe(ucr->pusb_dev, EP_BULK_IN), 141 data, rsp_len, 0, NULL, 100); 142 if (ret) 143 return ret; 144 } 145 146 /* unaligned part */ 147 for (i = 0; i < res_len; i++) { 148 ret = rtsx_usb_read_register(ucr, addr + rsp_len + i, 149 data + rsp_len + i); 150 if (ret) 151 return ret; 152 } 153 154 return 0; 155 } 156 157 int rtsx_usb_read_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len) 158 { 159 return rtsx_usb_seq_read_register(ucr, PPBUF_BASE2, (u16)buf_len, buf); 160 } 161 EXPORT_SYMBOL_GPL(rtsx_usb_read_ppbuf); 162 163 int rtsx_usb_write_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len) 164 { 165 return rtsx_usb_seq_write_register(ucr, PPBUF_BASE2, (u16)buf_len, buf); 166 } 167 EXPORT_SYMBOL_GPL(rtsx_usb_write_ppbuf); 168 169 int rtsx_usb_ep0_write_register(struct rtsx_ucr *ucr, u16 addr, 170 u8 mask, u8 data) 171 { 172 u16 value, index; 173 174 addr |= EP0_WRITE_REG_CMD << EP0_OP_SHIFT; 175 value = swab16(addr); 176 index = mask | data << 8; 177 178 return usb_control_msg(ucr->pusb_dev, 179 usb_sndctrlpipe(ucr->pusb_dev, 0), RTSX_USB_REQ_REG_OP, 180 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 181 value, index, NULL, 0, 100); 182 } 183 EXPORT_SYMBOL_GPL(rtsx_usb_ep0_write_register); 184 185 int rtsx_usb_ep0_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data) 186 { 187 u16 value; 188 u8 *buf; 189 int ret; 190 191 if (!data) 192 return -EINVAL; 193 194 buf = kzalloc(sizeof(u8), GFP_KERNEL); 195 if (!buf) 196 return -ENOMEM; 197 198 addr |= EP0_READ_REG_CMD << EP0_OP_SHIFT; 199 value = swab16(addr); 200 201 ret = usb_control_msg(ucr->pusb_dev, 202 usb_rcvctrlpipe(ucr->pusb_dev, 0), RTSX_USB_REQ_REG_OP, 203 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 204 value, 0, buf, 1, 100); 205 *data = *buf; 206 207 kfree(buf); 208 return ret; 209 } 210 EXPORT_SYMBOL_GPL(rtsx_usb_ep0_read_register); 211 212 void rtsx_usb_add_cmd(struct rtsx_ucr *ucr, u8 cmd_type, u16 reg_addr, 213 u8 mask, u8 data) 214 { 215 int i; 216 217 if (ucr->cmd_idx < (IOBUF_SIZE - CMD_OFFSET) / 4) { 218 i = CMD_OFFSET + ucr->cmd_idx * 4; 219 220 ucr->cmd_buf[i++] = ((cmd_type & 0x03) << 6) | 221 (u8)((reg_addr >> 8) & 0x3F); 222 ucr->cmd_buf[i++] = (u8)reg_addr; 223 ucr->cmd_buf[i++] = mask; 224 ucr->cmd_buf[i++] = data; 225 226 ucr->cmd_idx++; 227 } 228 } 229 EXPORT_SYMBOL_GPL(rtsx_usb_add_cmd); 230 231 int rtsx_usb_send_cmd(struct rtsx_ucr *ucr, u8 flag, int timeout) 232 { 233 int ret; 234 235 ucr->cmd_buf[CNT_H] = (u8)(ucr->cmd_idx >> 8); 236 ucr->cmd_buf[CNT_L] = (u8)(ucr->cmd_idx); 237 ucr->cmd_buf[STAGE_FLAG] = flag; 238 239 ret = rtsx_usb_transfer_data(ucr, 240 usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT), 241 ucr->cmd_buf, ucr->cmd_idx * 4 + CMD_OFFSET, 242 0, NULL, timeout); 243 if (ret) { 244 rtsx_usb_clear_fsm_err(ucr); 245 return ret; 246 } 247 248 return 0; 249 } 250 EXPORT_SYMBOL_GPL(rtsx_usb_send_cmd); 251 252 int rtsx_usb_get_rsp(struct rtsx_ucr *ucr, int rsp_len, int timeout) 253 { 254 if (rsp_len <= 0) 255 return -EINVAL; 256 257 rsp_len = ALIGN(rsp_len, 4); 258 259 return rtsx_usb_transfer_data(ucr, 260 usb_rcvbulkpipe(ucr->pusb_dev, EP_BULK_IN), 261 ucr->rsp_buf, rsp_len, 0, NULL, timeout); 262 } 263 EXPORT_SYMBOL_GPL(rtsx_usb_get_rsp); 264 265 static int rtsx_usb_get_status_with_bulk(struct rtsx_ucr *ucr, u16 *status) 266 { 267 int ret; 268 269 rtsx_usb_init_cmd(ucr); 270 rtsx_usb_add_cmd(ucr, READ_REG_CMD, CARD_EXIST, 0x00, 0x00); 271 rtsx_usb_add_cmd(ucr, READ_REG_CMD, OCPSTAT, 0x00, 0x00); 272 ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100); 273 if (ret) 274 return ret; 275 276 ret = rtsx_usb_get_rsp(ucr, 2, 100); 277 if (ret) 278 return ret; 279 280 *status = ((ucr->rsp_buf[0] >> 2) & 0x0f) | 281 ((ucr->rsp_buf[1] & 0x03) << 4); 282 283 return 0; 284 } 285 286 int rtsx_usb_get_card_status(struct rtsx_ucr *ucr, u16 *status) 287 { 288 int ret; 289 u16 *buf; 290 291 if (!status) 292 return -EINVAL; 293 294 if (polling_pipe == 0) { 295 buf = kzalloc(sizeof(u16), GFP_KERNEL); 296 if (!buf) 297 return -ENOMEM; 298 299 ret = usb_control_msg(ucr->pusb_dev, 300 usb_rcvctrlpipe(ucr->pusb_dev, 0), 301 RTSX_USB_REQ_POLL, 302 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 303 0, 0, buf, 2, 100); 304 *status = *buf; 305 306 kfree(buf); 307 } else { 308 ret = rtsx_usb_get_status_with_bulk(ucr, status); 309 } 310 311 /* usb_control_msg may return positive when success */ 312 if (ret < 0) 313 return ret; 314 315 return 0; 316 } 317 EXPORT_SYMBOL_GPL(rtsx_usb_get_card_status); 318 319 static int rtsx_usb_write_phy_register(struct rtsx_ucr *ucr, u8 addr, u8 val) 320 { 321 dev_dbg(&ucr->pusb_intf->dev, "Write 0x%x to phy register 0x%x\n", 322 val, addr); 323 324 rtsx_usb_init_cmd(ucr); 325 326 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VSTAIN, 0xFF, val); 327 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL, 0xFF, addr & 0x0F); 328 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00); 329 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00); 330 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01); 331 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL, 332 0xFF, (addr >> 4) & 0x0F); 333 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00); 334 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00); 335 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01); 336 337 return rtsx_usb_send_cmd(ucr, MODE_C, 100); 338 } 339 340 int rtsx_usb_write_register(struct rtsx_ucr *ucr, u16 addr, u8 mask, u8 data) 341 { 342 rtsx_usb_init_cmd(ucr); 343 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, addr, mask, data); 344 return rtsx_usb_send_cmd(ucr, MODE_C, 100); 345 } 346 EXPORT_SYMBOL_GPL(rtsx_usb_write_register); 347 348 int rtsx_usb_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data) 349 { 350 int ret; 351 352 if (data != NULL) 353 *data = 0; 354 355 rtsx_usb_init_cmd(ucr); 356 rtsx_usb_add_cmd(ucr, READ_REG_CMD, addr, 0, 0); 357 ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100); 358 if (ret) 359 return ret; 360 361 ret = rtsx_usb_get_rsp(ucr, 1, 100); 362 if (ret) 363 return ret; 364 365 if (data != NULL) 366 *data = ucr->rsp_buf[0]; 367 368 return 0; 369 } 370 EXPORT_SYMBOL_GPL(rtsx_usb_read_register); 371 372 static inline u8 double_ssc_depth(u8 depth) 373 { 374 return (depth > 1) ? (depth - 1) : depth; 375 } 376 377 static u8 revise_ssc_depth(u8 ssc_depth, u8 div) 378 { 379 if (div > CLK_DIV_1) { 380 if (ssc_depth > div - 1) 381 ssc_depth -= (div - 1); 382 else 383 ssc_depth = SSC_DEPTH_2M; 384 } 385 386 return ssc_depth; 387 } 388 389 int rtsx_usb_switch_clock(struct rtsx_ucr *ucr, unsigned int card_clock, 390 u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk) 391 { 392 int ret; 393 u8 n, clk_divider, mcu_cnt, div; 394 395 if (!card_clock) { 396 ucr->cur_clk = 0; 397 return 0; 398 } 399 400 if (initial_mode) { 401 /* We use 250k(around) here, in initial stage */ 402 clk_divider = SD_CLK_DIVIDE_128; 403 card_clock = 30000000; 404 } else { 405 clk_divider = SD_CLK_DIVIDE_0; 406 } 407 408 ret = rtsx_usb_write_register(ucr, SD_CFG1, 409 SD_CLK_DIVIDE_MASK, clk_divider); 410 if (ret < 0) 411 return ret; 412 413 card_clock /= 1000000; 414 dev_dbg(&ucr->pusb_intf->dev, 415 "Switch card clock to %dMHz\n", card_clock); 416 417 if (!initial_mode && double_clk) 418 card_clock *= 2; 419 dev_dbg(&ucr->pusb_intf->dev, 420 "Internal SSC clock: %dMHz (cur_clk = %d)\n", 421 card_clock, ucr->cur_clk); 422 423 if (card_clock == ucr->cur_clk) 424 return 0; 425 426 /* Converting clock value into internal settings: n and div */ 427 n = card_clock - 2; 428 if ((card_clock <= 2) || (n > MAX_DIV_N)) 429 return -EINVAL; 430 431 mcu_cnt = 60/card_clock + 3; 432 if (mcu_cnt > 15) 433 mcu_cnt = 15; 434 435 /* Make sure that the SSC clock div_n is not less than MIN_DIV_N */ 436 437 div = CLK_DIV_1; 438 while (n < MIN_DIV_N && div < CLK_DIV_4) { 439 n = (n + 2) * 2 - 2; 440 div++; 441 } 442 dev_dbg(&ucr->pusb_intf->dev, "n = %d, div = %d\n", n, div); 443 444 if (double_clk) 445 ssc_depth = double_ssc_depth(ssc_depth); 446 447 ssc_depth = revise_ssc_depth(ssc_depth, div); 448 dev_dbg(&ucr->pusb_intf->dev, "ssc_depth = %d\n", ssc_depth); 449 450 rtsx_usb_init_cmd(ucr); 451 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV, CLK_CHANGE, CLK_CHANGE); 452 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV, 453 0x3F, (div << 4) | mcu_cnt); 454 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0); 455 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL2, 456 SSC_DEPTH_MASK, ssc_depth); 457 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n); 458 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB); 459 if (vpclk) { 460 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL, 461 PHASE_NOT_RESET, 0); 462 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL, 463 PHASE_NOT_RESET, PHASE_NOT_RESET); 464 } 465 466 ret = rtsx_usb_send_cmd(ucr, MODE_C, 2000); 467 if (ret < 0) 468 return ret; 469 470 ret = rtsx_usb_write_register(ucr, SSC_CTL1, 0xff, 471 SSC_RSTB | SSC_8X_EN | SSC_SEL_4M); 472 if (ret < 0) 473 return ret; 474 475 /* Wait SSC clock stable */ 476 usleep_range(100, 1000); 477 478 ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0); 479 if (ret < 0) 480 return ret; 481 482 ucr->cur_clk = card_clock; 483 484 return 0; 485 } 486 EXPORT_SYMBOL_GPL(rtsx_usb_switch_clock); 487 488 int rtsx_usb_card_exclusive_check(struct rtsx_ucr *ucr, int card) 489 { 490 int ret; 491 u16 val; 492 u16 cd_mask[] = { 493 [RTSX_USB_SD_CARD] = (CD_MASK & ~SD_CD), 494 [RTSX_USB_MS_CARD] = (CD_MASK & ~MS_CD) 495 }; 496 497 ret = rtsx_usb_get_card_status(ucr, &val); 498 /* 499 * If get status fails, return 0 (ok) for the exclusive check 500 * and let the flow fail at somewhere else. 501 */ 502 if (ret) 503 return 0; 504 505 if (val & cd_mask[card]) 506 return -EIO; 507 508 return 0; 509 } 510 EXPORT_SYMBOL_GPL(rtsx_usb_card_exclusive_check); 511 512 static int rtsx_usb_reset_chip(struct rtsx_ucr *ucr) 513 { 514 int ret; 515 u8 val; 516 517 rtsx_usb_init_cmd(ucr); 518 519 if (CHECK_PKG(ucr, LQFP48)) { 520 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL, 521 LDO3318_PWR_MASK, LDO_SUSPEND); 522 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL, 523 FORCE_LDO_POWERB, FORCE_LDO_POWERB); 524 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL1, 525 0x30, 0x10); 526 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL5, 527 0x03, 0x01); 528 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL6, 529 0x0C, 0x04); 530 } 531 532 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SYS_DUMMY0, NYET_MSAK, NYET_EN); 533 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CD_DEGLITCH_WIDTH, 0xFF, 0x08); 534 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, 535 CD_DEGLITCH_EN, XD_CD_DEGLITCH_EN, 0x0); 536 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD30_DRIVE_SEL, 537 SD30_DRIVE_MASK, DRIVER_TYPE_D); 538 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, 539 CARD_DRIVE_SEL, SD20_DRIVE_MASK, 0x0); 540 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, LDO_POWER_CFG, 0xE0, 0x0); 541 542 if (ucr->is_rts5179) 543 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, 544 CARD_PULL_CTL5, 0x03, 0x01); 545 546 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_DMA1_CTL, 547 EXTEND_DMA1_ASYNC_SIGNAL, EXTEND_DMA1_ASYNC_SIGNAL); 548 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_INT_PEND, 549 XD_INT | MS_INT | SD_INT, 550 XD_INT | MS_INT | SD_INT); 551 552 ret = rtsx_usb_send_cmd(ucr, MODE_C, 100); 553 if (ret) 554 return ret; 555 556 /* config non-crystal mode */ 557 rtsx_usb_read_register(ucr, CFG_MODE, &val); 558 if ((val & XTAL_FREE) || ((val & CLK_MODE_MASK) == CLK_MODE_NON_XTAL)) { 559 ret = rtsx_usb_write_phy_register(ucr, 0xC2, 0x7C); 560 if (ret) 561 return ret; 562 } 563 564 return 0; 565 } 566 567 static int rtsx_usb_init_chip(struct rtsx_ucr *ucr) 568 { 569 int ret; 570 u8 val; 571 572 rtsx_usb_clear_fsm_err(ucr); 573 574 /* power on SSC */ 575 ret = rtsx_usb_write_register(ucr, 576 FPDCTL, SSC_POWER_MASK, SSC_POWER_ON); 577 if (ret) 578 return ret; 579 580 usleep_range(100, 1000); 581 ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0x00); 582 if (ret) 583 return ret; 584 585 /* determine IC version */ 586 ret = rtsx_usb_read_register(ucr, HW_VERSION, &val); 587 if (ret) 588 return ret; 589 590 ucr->ic_version = val & HW_VER_MASK; 591 592 /* determine package */ 593 ret = rtsx_usb_read_register(ucr, CARD_SHARE_MODE, &val); 594 if (ret) 595 return ret; 596 597 if (val & CARD_SHARE_LQFP_SEL) { 598 ucr->package = LQFP48; 599 dev_dbg(&ucr->pusb_intf->dev, "Package: LQFP48\n"); 600 } else { 601 ucr->package = QFN24; 602 dev_dbg(&ucr->pusb_intf->dev, "Package: QFN24\n"); 603 } 604 605 /* determine IC variations */ 606 rtsx_usb_read_register(ucr, CFG_MODE_1, &val); 607 if (val & RTS5179) { 608 ucr->is_rts5179 = true; 609 dev_dbg(&ucr->pusb_intf->dev, "Device is rts5179\n"); 610 } else { 611 ucr->is_rts5179 = false; 612 } 613 614 return rtsx_usb_reset_chip(ucr); 615 } 616 617 static int rtsx_usb_probe(struct usb_interface *intf, 618 const struct usb_device_id *id) 619 { 620 struct usb_device *usb_dev = interface_to_usbdev(intf); 621 struct rtsx_ucr *ucr; 622 int ret; 623 624 dev_dbg(&intf->dev, 625 ": Realtek USB Card Reader found at bus %03d address %03d\n", 626 usb_dev->bus->busnum, usb_dev->devnum); 627 628 ucr = devm_kzalloc(&intf->dev, sizeof(*ucr), GFP_KERNEL); 629 if (!ucr) 630 return -ENOMEM; 631 632 ucr->pusb_dev = usb_dev; 633 634 ucr->iobuf = usb_alloc_coherent(ucr->pusb_dev, IOBUF_SIZE, 635 GFP_KERNEL, &ucr->iobuf_dma); 636 if (!ucr->iobuf) 637 return -ENOMEM; 638 639 usb_set_intfdata(intf, ucr); 640 641 ucr->vendor_id = id->idVendor; 642 ucr->product_id = id->idProduct; 643 ucr->cmd_buf = ucr->rsp_buf = ucr->iobuf; 644 645 mutex_init(&ucr->dev_mutex); 646 647 ucr->pusb_intf = intf; 648 649 /* initialize */ 650 ret = rtsx_usb_init_chip(ucr); 651 if (ret) 652 goto out_init_fail; 653 654 /* initialize USB SG transfer timer */ 655 timer_setup(&ucr->sg_timer, rtsx_usb_sg_timed_out, 0); 656 657 ret = mfd_add_hotplug_devices(&intf->dev, rtsx_usb_cells, 658 ARRAY_SIZE(rtsx_usb_cells)); 659 if (ret) 660 goto out_init_fail; 661 662 #ifdef CONFIG_PM 663 intf->needs_remote_wakeup = 1; 664 usb_enable_autosuspend(usb_dev); 665 #endif 666 667 return 0; 668 669 out_init_fail: 670 usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf, 671 ucr->iobuf_dma); 672 return ret; 673 } 674 675 static void rtsx_usb_disconnect(struct usb_interface *intf) 676 { 677 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf); 678 679 dev_dbg(&intf->dev, "%s called\n", __func__); 680 681 mfd_remove_devices(&intf->dev); 682 683 usb_set_intfdata(ucr->pusb_intf, NULL); 684 usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf, 685 ucr->iobuf_dma); 686 } 687 688 #ifdef CONFIG_PM 689 static int rtsx_usb_suspend(struct usb_interface *intf, pm_message_t message) 690 { 691 struct rtsx_ucr *ucr = 692 (struct rtsx_ucr *)usb_get_intfdata(intf); 693 u16 val = 0; 694 695 dev_dbg(&intf->dev, "%s called with pm message 0x%04x\n", 696 __func__, message.event); 697 698 if (PMSG_IS_AUTO(message)) { 699 if (mutex_trylock(&ucr->dev_mutex)) { 700 rtsx_usb_get_card_status(ucr, &val); 701 mutex_unlock(&ucr->dev_mutex); 702 703 /* Defer the autosuspend if card exists */ 704 if (val & (SD_CD | MS_CD)) 705 return -EAGAIN; 706 } else { 707 /* There is an ongoing operation*/ 708 return -EAGAIN; 709 } 710 } 711 712 return 0; 713 } 714 715 static int rtsx_usb_resume_child(struct device *dev, void *data) 716 { 717 pm_request_resume(dev); 718 return 0; 719 } 720 721 static int rtsx_usb_resume(struct usb_interface *intf) 722 { 723 device_for_each_child(&intf->dev, NULL, rtsx_usb_resume_child); 724 return 0; 725 } 726 727 static int rtsx_usb_reset_resume(struct usb_interface *intf) 728 { 729 struct rtsx_ucr *ucr = 730 (struct rtsx_ucr *)usb_get_intfdata(intf); 731 732 rtsx_usb_reset_chip(ucr); 733 device_for_each_child(&intf->dev, NULL, rtsx_usb_resume_child); 734 return 0; 735 } 736 737 #else /* CONFIG_PM */ 738 739 #define rtsx_usb_suspend NULL 740 #define rtsx_usb_resume NULL 741 #define rtsx_usb_reset_resume NULL 742 743 #endif /* CONFIG_PM */ 744 745 746 static int rtsx_usb_pre_reset(struct usb_interface *intf) 747 { 748 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf); 749 750 mutex_lock(&ucr->dev_mutex); 751 return 0; 752 } 753 754 static int rtsx_usb_post_reset(struct usb_interface *intf) 755 { 756 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf); 757 758 mutex_unlock(&ucr->dev_mutex); 759 return 0; 760 } 761 762 static struct usb_device_id rtsx_usb_usb_ids[] = { 763 { USB_DEVICE(0x0BDA, 0x0129) }, 764 { USB_DEVICE(0x0BDA, 0x0139) }, 765 { USB_DEVICE(0x0BDA, 0x0140) }, 766 { } 767 }; 768 MODULE_DEVICE_TABLE(usb, rtsx_usb_usb_ids); 769 770 static struct usb_driver rtsx_usb_driver = { 771 .name = "rtsx_usb", 772 .probe = rtsx_usb_probe, 773 .disconnect = rtsx_usb_disconnect, 774 .suspend = rtsx_usb_suspend, 775 .resume = rtsx_usb_resume, 776 .reset_resume = rtsx_usb_reset_resume, 777 .pre_reset = rtsx_usb_pre_reset, 778 .post_reset = rtsx_usb_post_reset, 779 .id_table = rtsx_usb_usb_ids, 780 .supports_autosuspend = 1, 781 .soft_unbind = 1, 782 }; 783 784 module_usb_driver(rtsx_usb_driver); 785 786 MODULE_LICENSE("GPL v2"); 787 MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>"); 788 MODULE_DESCRIPTION("Realtek USB Card Reader Driver"); 789