1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Driver for Realtek USB card reader
4 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
7 * Roger Tseng <rogerable@realtek.com>
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>
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)");
21 static const struct mfd_cell rtsx_usb_cells
[] = {
22 [RTSX_USB_SD_CARD
] = {
23 .name
= "rtsx_usb_sdmmc",
26 [RTSX_USB_MS_CARD
] = {
27 .name
= "rtsx_usb_ms",
32 static void rtsx_usb_sg_timed_out(struct timer_list
*t
)
34 struct rtsx_ucr
*ucr
= from_timer(ucr
, t
, sg_timer
);
36 dev_dbg(&ucr
->pusb_intf
->dev
, "%s: sg transfer timed out", __func__
);
37 usb_sg_cancel(&ucr
->current_sg
);
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
)
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
);
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
))
59 ret
= ucr
->current_sg
.status
;
62 *act_len
= ucr
->current_sg
.bytes
;
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
)
75 return rtsx_usb_bulk_transfer_sglist(ucr
, pipe
,
76 (struct scatterlist
*)buf
, num_sg
, len
, act_len
,
79 return usb_bulk_msg(ucr
->pusb_dev
, pipe
, buf
, len
, act_len
,
82 EXPORT_SYMBOL_GPL(rtsx_usb_transfer_data
);
84 static inline void rtsx_usb_seq_cmd_hdr(struct rtsx_ucr
*ucr
,
85 u16 addr
, u16 len
, u8 seq_type
)
87 rtsx_usb_cmd_hdr_tag(ucr
);
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
;
95 if (seq_type
== SEQ_WRITE
)
96 ucr
->cmd_buf
[STAGE_FLAG
] = 0;
98 ucr
->cmd_buf
[STAGE_FLAG
] = STAGE_R
;
101 static int rtsx_usb_seq_write_register(struct rtsx_ucr
*ucr
,
102 u16 addr
, u16 len
, u8
*data
)
104 u16 cmd_len
= ALIGN(SEQ_WRITE_DATA_OFFSET
+ len
, 4);
109 if (cmd_len
> IOBUF_SIZE
)
112 rtsx_usb_seq_cmd_hdr(ucr
, addr
, len
, SEQ_WRITE
);
113 memcpy(ucr
->cmd_buf
+ SEQ_WRITE_DATA_OFFSET
, data
, len
);
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);
120 static int rtsx_usb_seq_read_register(struct rtsx_ucr
*ucr
,
121 u16 addr
, u16 len
, u8
*data
)
124 u16 rsp_len
= round_down(len
, 4);
125 u16 res_len
= len
- rsp_len
;
130 /* 4-byte aligned part */
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);
139 ret
= rtsx_usb_transfer_data(ucr
,
140 usb_rcvbulkpipe(ucr
->pusb_dev
, EP_BULK_IN
),
141 data
, rsp_len
, 0, NULL
, 100);
147 for (i
= 0; i
< res_len
; i
++) {
148 ret
= rtsx_usb_read_register(ucr
, addr
+ rsp_len
+ i
,
157 int rtsx_usb_read_ppbuf(struct rtsx_ucr
*ucr
, u8
*buf
, int buf_len
)
159 return rtsx_usb_seq_read_register(ucr
, PPBUF_BASE2
, (u16
)buf_len
, buf
);
161 EXPORT_SYMBOL_GPL(rtsx_usb_read_ppbuf
);
163 int rtsx_usb_write_ppbuf(struct rtsx_ucr
*ucr
, u8
*buf
, int buf_len
)
165 return rtsx_usb_seq_write_register(ucr
, PPBUF_BASE2
, (u16
)buf_len
, buf
);
167 EXPORT_SYMBOL_GPL(rtsx_usb_write_ppbuf
);
169 int rtsx_usb_ep0_write_register(struct rtsx_ucr
*ucr
, u16 addr
,
174 addr
|= EP0_WRITE_REG_CMD
<< EP0_OP_SHIFT
;
175 value
= swab16(addr
);
176 index
= mask
| data
<< 8;
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);
183 EXPORT_SYMBOL_GPL(rtsx_usb_ep0_write_register
);
185 int rtsx_usb_ep0_read_register(struct rtsx_ucr
*ucr
, u16 addr
, u8
*data
)
194 buf
= kzalloc(sizeof(u8
), GFP_KERNEL
);
198 addr
|= EP0_READ_REG_CMD
<< EP0_OP_SHIFT
;
199 value
= swab16(addr
);
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);
210 EXPORT_SYMBOL_GPL(rtsx_usb_ep0_read_register
);
212 void rtsx_usb_add_cmd(struct rtsx_ucr
*ucr
, u8 cmd_type
, u16 reg_addr
,
217 if (ucr
->cmd_idx
< (IOBUF_SIZE
- CMD_OFFSET
) / 4) {
218 i
= CMD_OFFSET
+ ucr
->cmd_idx
* 4;
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
;
229 EXPORT_SYMBOL_GPL(rtsx_usb_add_cmd
);
231 int rtsx_usb_send_cmd(struct rtsx_ucr
*ucr
, u8 flag
, int timeout
)
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
;
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
,
244 rtsx_usb_clear_fsm_err(ucr
);
250 EXPORT_SYMBOL_GPL(rtsx_usb_send_cmd
);
252 int rtsx_usb_get_rsp(struct rtsx_ucr
*ucr
, int rsp_len
, int timeout
)
257 rsp_len
= ALIGN(rsp_len
, 4);
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
);
263 EXPORT_SYMBOL_GPL(rtsx_usb_get_rsp
);
265 static int rtsx_usb_get_status_with_bulk(struct rtsx_ucr
*ucr
, u16
*status
)
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);
276 ret
= rtsx_usb_get_rsp(ucr
, 2, 100);
280 *status
= ((ucr
->rsp_buf
[0] >> 2) & 0x0f) |
281 ((ucr
->rsp_buf
[1] & 0x03) << 4);
286 int rtsx_usb_get_card_status(struct rtsx_ucr
*ucr
, u16
*status
)
294 if (polling_pipe
== 0) {
295 buf
= kzalloc(sizeof(u16
), GFP_KERNEL
);
299 ret
= usb_control_msg(ucr
->pusb_dev
,
300 usb_rcvctrlpipe(ucr
->pusb_dev
, 0),
302 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
308 ret
= rtsx_usb_get_status_with_bulk(ucr
, status
);
311 /* usb_control_msg may return positive when success */
317 EXPORT_SYMBOL_GPL(rtsx_usb_get_card_status
);
319 static int rtsx_usb_write_phy_register(struct rtsx_ucr
*ucr
, u8 addr
, u8 val
)
321 dev_dbg(&ucr
->pusb_intf
->dev
, "Write 0x%x to phy register 0x%x\n",
324 rtsx_usb_init_cmd(ucr
);
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);
337 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
340 int rtsx_usb_write_register(struct rtsx_ucr
*ucr
, u16 addr
, u8 mask
, u8 data
)
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);
346 EXPORT_SYMBOL_GPL(rtsx_usb_write_register
);
348 int rtsx_usb_read_register(struct rtsx_ucr
*ucr
, u16 addr
, u8
*data
)
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);
361 ret
= rtsx_usb_get_rsp(ucr
, 1, 100);
366 *data
= ucr
->rsp_buf
[0];
370 EXPORT_SYMBOL_GPL(rtsx_usb_read_register
);
372 static inline u8
double_ssc_depth(u8 depth
)
374 return (depth
> 1) ? (depth
- 1) : depth
;
377 static u8
revise_ssc_depth(u8 ssc_depth
, u8 div
)
379 if (div
> CLK_DIV_1
) {
380 if (ssc_depth
> div
- 1)
381 ssc_depth
-= (div
- 1);
383 ssc_depth
= SSC_DEPTH_2M
;
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
)
393 u8 n
, clk_divider
, mcu_cnt
, div
;
401 /* We use 250k(around) here, in initial stage */
402 clk_divider
= SD_CLK_DIVIDE_128
;
403 card_clock
= 30000000;
405 clk_divider
= SD_CLK_DIVIDE_0
;
408 ret
= rtsx_usb_write_register(ucr
, SD_CFG1
,
409 SD_CLK_DIVIDE_MASK
, clk_divider
);
413 card_clock
/= 1000000;
414 dev_dbg(&ucr
->pusb_intf
->dev
,
415 "Switch card clock to %dMHz\n", card_clock
);
417 if (!initial_mode
&& double_clk
)
419 dev_dbg(&ucr
->pusb_intf
->dev
,
420 "Internal SSC clock: %dMHz (cur_clk = %d)\n",
421 card_clock
, ucr
->cur_clk
);
423 if (card_clock
== ucr
->cur_clk
)
426 /* Converting clock value into internal settings: n and div */
428 if ((card_clock
<= 2) || (n
> MAX_DIV_N
))
431 mcu_cnt
= 60/card_clock
+ 3;
435 /* Make sure that the SSC clock div_n is not less than MIN_DIV_N */
438 while (n
< MIN_DIV_N
&& div
< CLK_DIV_4
) {
442 dev_dbg(&ucr
->pusb_intf
->dev
, "n = %d, div = %d\n", n
, div
);
445 ssc_depth
= double_ssc_depth(ssc_depth
);
447 ssc_depth
= revise_ssc_depth(ssc_depth
, div
);
448 dev_dbg(&ucr
->pusb_intf
->dev
, "ssc_depth = %d\n", ssc_depth
);
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
);
460 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
462 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
463 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
466 ret
= rtsx_usb_send_cmd(ucr
, MODE_C
, 2000);
470 ret
= rtsx_usb_write_register(ucr
, SSC_CTL1
, 0xff,
471 SSC_RSTB
| SSC_8X_EN
| SSC_SEL_4M
);
475 /* Wait SSC clock stable */
476 usleep_range(100, 1000);
478 ret
= rtsx_usb_write_register(ucr
, CLK_DIV
, CLK_CHANGE
, 0);
482 ucr
->cur_clk
= card_clock
;
486 EXPORT_SYMBOL_GPL(rtsx_usb_switch_clock
);
488 int rtsx_usb_card_exclusive_check(struct rtsx_ucr
*ucr
, int card
)
493 [RTSX_USB_SD_CARD
] = (CD_MASK
& ~SD_CD
),
494 [RTSX_USB_MS_CARD
] = (CD_MASK
& ~MS_CD
)
497 ret
= rtsx_usb_get_card_status(ucr
, &val
);
499 * If get status fails, return 0 (ok) for the exclusive check
500 * and let the flow fail at somewhere else.
505 if (val
& cd_mask
[card
])
510 EXPORT_SYMBOL_GPL(rtsx_usb_card_exclusive_check
);
512 static int rtsx_usb_reset_chip(struct rtsx_ucr
*ucr
)
517 rtsx_usb_init_cmd(ucr
);
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
,
526 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
,
528 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
,
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);
543 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
544 CARD_PULL_CTL5
, 0x03, 0x01);
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
);
552 ret
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
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);
567 static int rtsx_usb_init_chip(struct rtsx_ucr
*ucr
)
572 rtsx_usb_clear_fsm_err(ucr
);
575 ret
= rtsx_usb_write_register(ucr
,
576 FPDCTL
, SSC_POWER_MASK
, SSC_POWER_ON
);
580 usleep_range(100, 1000);
581 ret
= rtsx_usb_write_register(ucr
, CLK_DIV
, CLK_CHANGE
, 0x00);
585 /* determine IC version */
586 ret
= rtsx_usb_read_register(ucr
, HW_VERSION
, &val
);
590 ucr
->ic_version
= val
& HW_VER_MASK
;
592 /* determine package */
593 ret
= rtsx_usb_read_register(ucr
, CARD_SHARE_MODE
, &val
);
597 if (val
& CARD_SHARE_LQFP_SEL
) {
598 ucr
->package
= LQFP48
;
599 dev_dbg(&ucr
->pusb_intf
->dev
, "Package: LQFP48\n");
601 ucr
->package
= QFN24
;
602 dev_dbg(&ucr
->pusb_intf
->dev
, "Package: QFN24\n");
605 /* determine IC variations */
606 rtsx_usb_read_register(ucr
, CFG_MODE_1
, &val
);
608 ucr
->is_rts5179
= true;
609 dev_dbg(&ucr
->pusb_intf
->dev
, "Device is rts5179\n");
611 ucr
->is_rts5179
= false;
614 return rtsx_usb_reset_chip(ucr
);
617 static int rtsx_usb_probe(struct usb_interface
*intf
,
618 const struct usb_device_id
*id
)
620 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
621 struct rtsx_ucr
*ucr
;
625 ": Realtek USB Card Reader found at bus %03d address %03d\n",
626 usb_dev
->bus
->busnum
, usb_dev
->devnum
);
628 ucr
= devm_kzalloc(&intf
->dev
, sizeof(*ucr
), GFP_KERNEL
);
632 ucr
->pusb_dev
= usb_dev
;
634 ucr
->iobuf
= usb_alloc_coherent(ucr
->pusb_dev
, IOBUF_SIZE
,
635 GFP_KERNEL
, &ucr
->iobuf_dma
);
639 usb_set_intfdata(intf
, ucr
);
641 ucr
->vendor_id
= id
->idVendor
;
642 ucr
->product_id
= id
->idProduct
;
643 ucr
->cmd_buf
= ucr
->rsp_buf
= ucr
->iobuf
;
645 mutex_init(&ucr
->dev_mutex
);
647 ucr
->pusb_intf
= intf
;
650 ret
= rtsx_usb_init_chip(ucr
);
654 /* initialize USB SG transfer timer */
655 timer_setup(&ucr
->sg_timer
, rtsx_usb_sg_timed_out
, 0);
657 ret
= mfd_add_hotplug_devices(&intf
->dev
, rtsx_usb_cells
,
658 ARRAY_SIZE(rtsx_usb_cells
));
663 intf
->needs_remote_wakeup
= 1;
664 usb_enable_autosuspend(usb_dev
);
670 usb_free_coherent(ucr
->pusb_dev
, IOBUF_SIZE
, ucr
->iobuf
,
675 static void rtsx_usb_disconnect(struct usb_interface
*intf
)
677 struct rtsx_ucr
*ucr
= (struct rtsx_ucr
*)usb_get_intfdata(intf
);
679 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
681 mfd_remove_devices(&intf
->dev
);
683 usb_set_intfdata(ucr
->pusb_intf
, NULL
);
684 usb_free_coherent(ucr
->pusb_dev
, IOBUF_SIZE
, ucr
->iobuf
,
689 static int rtsx_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
691 struct rtsx_ucr
*ucr
=
692 (struct rtsx_ucr
*)usb_get_intfdata(intf
);
695 dev_dbg(&intf
->dev
, "%s called with pm message 0x%04x\n",
696 __func__
, message
.event
);
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
);
703 /* Defer the autosuspend if card exists */
704 if (val
& (SD_CD
| MS_CD
))
707 /* There is an ongoing operation*/
715 static int rtsx_usb_resume_child(struct device
*dev
, void *data
)
717 pm_request_resume(dev
);
721 static int rtsx_usb_resume(struct usb_interface
*intf
)
723 device_for_each_child(&intf
->dev
, NULL
, rtsx_usb_resume_child
);
727 static int rtsx_usb_reset_resume(struct usb_interface
*intf
)
729 struct rtsx_ucr
*ucr
=
730 (struct rtsx_ucr
*)usb_get_intfdata(intf
);
732 rtsx_usb_reset_chip(ucr
);
733 device_for_each_child(&intf
->dev
, NULL
, rtsx_usb_resume_child
);
737 #else /* CONFIG_PM */
739 #define rtsx_usb_suspend NULL
740 #define rtsx_usb_resume NULL
741 #define rtsx_usb_reset_resume NULL
743 #endif /* CONFIG_PM */
746 static int rtsx_usb_pre_reset(struct usb_interface
*intf
)
748 struct rtsx_ucr
*ucr
= (struct rtsx_ucr
*)usb_get_intfdata(intf
);
750 mutex_lock(&ucr
->dev_mutex
);
754 static int rtsx_usb_post_reset(struct usb_interface
*intf
)
756 struct rtsx_ucr
*ucr
= (struct rtsx_ucr
*)usb_get_intfdata(intf
);
758 mutex_unlock(&ucr
->dev_mutex
);
762 static const struct usb_device_id rtsx_usb_usb_ids
[] = {
763 { USB_DEVICE(0x0BDA, 0x0129) },
764 { USB_DEVICE(0x0BDA, 0x0139) },
765 { USB_DEVICE(0x0BDA, 0x0140) },
768 MODULE_DEVICE_TABLE(usb
, rtsx_usb_usb_ids
);
770 static struct usb_driver rtsx_usb_driver
= {
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,
784 module_usb_driver(rtsx_usb_driver
);
786 MODULE_LICENSE("GPL v2");
787 MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
788 MODULE_DESCRIPTION("Realtek USB Card Reader Driver");