treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / cardreader / rtsx_usb.c
bloba328cab11014326c685db2f889a4afe228e74f46
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.
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>
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",
24 .pdata_size = 0,
26 [RTSX_USB_MS_CARD] = {
27 .name = "rtsx_usb_ms",
28 .pdata_size = 0,
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)
44 int ret;
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;
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;
61 if (act_len)
62 *act_len = ucr->current_sg.bytes;
64 return ret;
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)
71 if (timeout < 600)
72 timeout = 600;
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);
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;
97 else
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);
106 if (!data)
107 return -EINVAL;
109 if (cmd_len > IOBUF_SIZE)
110 return -EINVAL;
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)
123 int i, ret;
124 u16 rsp_len = round_down(len, 4);
125 u16 res_len = len - rsp_len;
127 if (!data)
128 return -EINVAL;
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;
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;
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;
154 return 0;
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,
170 u8 mask, u8 data)
172 u16 value, index;
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)
187 u16 value;
188 u8 *buf;
189 int ret;
191 if (!data)
192 return -EINVAL;
194 buf = kzalloc(sizeof(u8), GFP_KERNEL);
195 if (!buf)
196 return -ENOMEM;
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);
205 *data = *buf;
207 kfree(buf);
208 return ret;
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,
213 u8 mask, u8 data)
215 int i;
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;
226 ucr->cmd_idx++;
229 EXPORT_SYMBOL_GPL(rtsx_usb_add_cmd);
231 int rtsx_usb_send_cmd(struct rtsx_ucr *ucr, u8 flag, int timeout)
233 int ret;
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,
242 0, NULL, timeout);
243 if (ret) {
244 rtsx_usb_clear_fsm_err(ucr);
245 return ret;
248 return 0;
250 EXPORT_SYMBOL_GPL(rtsx_usb_send_cmd);
252 int rtsx_usb_get_rsp(struct rtsx_ucr *ucr, int rsp_len, int timeout)
254 if (rsp_len <= 0)
255 return -EINVAL;
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)
267 int ret;
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;
276 ret = rtsx_usb_get_rsp(ucr, 2, 100);
277 if (ret)
278 return ret;
280 *status = ((ucr->rsp_buf[0] >> 2) & 0x0f) |
281 ((ucr->rsp_buf[1] & 0x03) << 4);
283 return 0;
286 int rtsx_usb_get_card_status(struct rtsx_ucr *ucr, u16 *status)
288 int ret;
289 u16 *buf;
291 if (!status)
292 return -EINVAL;
294 if (polling_pipe == 0) {
295 buf = kzalloc(sizeof(u16), GFP_KERNEL);
296 if (!buf)
297 return -ENOMEM;
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;
306 kfree(buf);
307 } else {
308 ret = rtsx_usb_get_status_with_bulk(ucr, status);
311 /* usb_control_msg may return positive when success */
312 if (ret < 0)
313 return ret;
315 return 0;
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",
322 val, addr);
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)
350 int ret;
352 if (data != NULL)
353 *data = 0;
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;
361 ret = rtsx_usb_get_rsp(ucr, 1, 100);
362 if (ret)
363 return ret;
365 if (data != NULL)
366 *data = ucr->rsp_buf[0];
368 return 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);
382 else
383 ssc_depth = SSC_DEPTH_2M;
386 return ssc_depth;
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)
392 int ret;
393 u8 n, clk_divider, mcu_cnt, div;
395 if (!card_clock) {
396 ucr->cur_clk = 0;
397 return 0;
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;
408 ret = rtsx_usb_write_register(ucr, SD_CFG1,
409 SD_CLK_DIVIDE_MASK, clk_divider);
410 if (ret < 0)
411 return ret;
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)
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);
423 if (card_clock == ucr->cur_clk)
424 return 0;
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;
431 mcu_cnt = 60/card_clock + 3;
432 if (mcu_cnt > 15)
433 mcu_cnt = 15;
435 /* Make sure that the SSC clock div_n is not less than MIN_DIV_N */
437 div = CLK_DIV_1;
438 while (n < MIN_DIV_N && div < CLK_DIV_4) {
439 n = (n + 2) * 2 - 2;
440 div++;
442 dev_dbg(&ucr->pusb_intf->dev, "n = %d, div = %d\n", n, div);
444 if (double_clk)
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);
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);
466 ret = rtsx_usb_send_cmd(ucr, MODE_C, 2000);
467 if (ret < 0)
468 return ret;
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;
475 /* Wait SSC clock stable */
476 usleep_range(100, 1000);
478 ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0);
479 if (ret < 0)
480 return ret;
482 ucr->cur_clk = card_clock;
484 return 0;
486 EXPORT_SYMBOL_GPL(rtsx_usb_switch_clock);
488 int rtsx_usb_card_exclusive_check(struct rtsx_ucr *ucr, int card)
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)
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.
502 if (ret)
503 return 0;
505 if (val & cd_mask[card])
506 return -EIO;
508 return 0;
510 EXPORT_SYMBOL_GPL(rtsx_usb_card_exclusive_check);
512 static int rtsx_usb_reset_chip(struct rtsx_ucr *ucr)
514 int ret;
515 u8 val;
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,
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);
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);
542 if (ucr->is_rts5179)
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);
553 if (ret)
554 return ret;
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;
564 return 0;
567 static int rtsx_usb_init_chip(struct rtsx_ucr *ucr)
569 int ret;
570 u8 val;
572 rtsx_usb_clear_fsm_err(ucr);
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;
580 usleep_range(100, 1000);
581 ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0x00);
582 if (ret)
583 return ret;
585 /* determine IC version */
586 ret = rtsx_usb_read_register(ucr, HW_VERSION, &val);
587 if (ret)
588 return ret;
590 ucr->ic_version = val & HW_VER_MASK;
592 /* determine package */
593 ret = rtsx_usb_read_register(ucr, CARD_SHARE_MODE, &val);
594 if (ret)
595 return ret;
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");
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;
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;
622 int ret;
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);
628 ucr = devm_kzalloc(&intf->dev, sizeof(*ucr), GFP_KERNEL);
629 if (!ucr)
630 return -ENOMEM;
632 ucr->pusb_dev = usb_dev;
634 ucr->iobuf = usb_alloc_coherent(ucr->pusb_dev, IOBUF_SIZE,
635 GFP_KERNEL, &ucr->iobuf_dma);
636 if (!ucr->iobuf)
637 return -ENOMEM;
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;
649 /* initialize */
650 ret = rtsx_usb_init_chip(ucr);
651 if (ret)
652 goto out_init_fail;
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));
659 if (ret)
660 goto out_init_fail;
662 #ifdef CONFIG_PM
663 intf->needs_remote_wakeup = 1;
664 usb_enable_autosuspend(usb_dev);
665 #endif
667 return 0;
669 out_init_fail:
670 usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
671 ucr->iobuf_dma);
672 return ret;
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,
685 ucr->iobuf_dma);
688 #ifdef CONFIG_PM
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);
693 u16 val = 0;
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))
705 return -EAGAIN;
706 } else {
707 /* There is an ongoing operation*/
708 return -EAGAIN;
712 return 0;
715 static int rtsx_usb_resume_child(struct device *dev, void *data)
717 pm_request_resume(dev);
718 return 0;
721 static int rtsx_usb_resume(struct usb_interface *intf)
723 device_for_each_child(&intf->dev, NULL, rtsx_usb_resume_child);
724 return 0;
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);
734 return 0;
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);
751 return 0;
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);
759 return 0;
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) },
768 MODULE_DEVICE_TABLE(usb, rtsx_usb_usb_ids);
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,
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");