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
17static int polling_pipe = 1;
18module_param(polling_pipe, int, S_IRUGO | S_IWUSR);
19MODULE_PARM_DESC(polling_pipe, "polling pipe (0: ctl, 1: bulk)");
20
21static const struct mfd_cell rtsx_usb_cells[] = {
22 [RTSX_USB_SD_CARD] = {
23 .name = DRV_NAME_RTSX_USB_SDMMC,
24 .pdata_size = 0,
25 },
26 [RTSX_USB_MS_CARD] = {
27 .name = DRV_NAME_RTSX_USB_MS,
28 .pdata_size = 0,
29 },
30};
31
32static void rtsx_usb_sg_timed_out(struct timer_list *t)
33{
34 struct rtsx_ucr *ucr = timer_container_of(ucr, t, sg_timer);
35
36 dev_dbg(&ucr->pusb_intf->dev, "%s: sg transfer timed out", __func__);
37 usb_sg_cancel(io: &ucr->current_sg);
38}
39
40static 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(io: &ucr->current_sg, dev: ucr->pusb_dev, pipe, period: 0,
49 sg, nents: num_sg, length, GFP_NOIO);
50 if (ret)
51 return ret;
52
53 ucr->sg_timer.expires = jiffies + msecs_to_jiffies(m: timeout);
54 add_timer(timer: &ucr->sg_timer);
55 usb_sg_wait(io: &ucr->current_sg);
56 if (!timer_delete_sync(timer: &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
67int 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 sg: (struct scatterlist *)buf, num_sg, length: len, act_len,
77 timeout);
78 else
79 return usb_bulk_msg(usb_dev: ucr->pusb_dev, pipe, data: buf, len, actual_length: act_len,
80 timeout);
81}
82EXPORT_SYMBOL_GPL(rtsx_usb_transfer_data);
83
84static 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
101static 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
120static 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: addr + rsp_len + i,
149 data: data + rsp_len + i);
150 if (ret)
151 return ret;
152 }
153
154 return 0;
155}
156
157int rtsx_usb_read_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len)
158{
159 return rtsx_usb_seq_read_register(ucr, PPBUF_BASE2, len: (u16)buf_len, data: buf);
160}
161EXPORT_SYMBOL_GPL(rtsx_usb_read_ppbuf);
162
163int rtsx_usb_write_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len)
164{
165 return rtsx_usb_seq_write_register(ucr, PPBUF_BASE2, len: (u16)buf_len, data: buf);
166}
167EXPORT_SYMBOL_GPL(rtsx_usb_write_ppbuf);
168
169int 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(dev: 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, size: 0, timeout: 100);
182}
183EXPORT_SYMBOL_GPL(rtsx_usb_ep0_write_register);
184
185int 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(dev: 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, index: 0, data: buf, size: 1, timeout: 100);
205 *data = *buf;
206
207 kfree(objp: buf);
208 return ret;
209}
210EXPORT_SYMBOL_GPL(rtsx_usb_ep0_read_register);
211
212void 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}
229EXPORT_SYMBOL_GPL(rtsx_usb_add_cmd);
230
231int 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}
250EXPORT_SYMBOL_GPL(rtsx_usb_send_cmd);
251
252int 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}
263EXPORT_SYMBOL_GPL(rtsx_usb_get_rsp);
264
265static 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
286int 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(dev: 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 value: 0, index: 0, data: buf, size: 2, timeout: 100);
304 *status = *buf;
305
306 kfree(objp: 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}
317EXPORT_SYMBOL_GPL(rtsx_usb_get_card_status);
318
319static 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
340int 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}
346EXPORT_SYMBOL_GPL(rtsx_usb_write_register);
347
348int 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}
370EXPORT_SYMBOL_GPL(rtsx_usb_read_register);
371
372static inline u8 double_ssc_depth(u8 depth)
373{
374 return (depth > 1) ? (depth - 1) : depth;
375}
376
377static 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
389int 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(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(min: 100, max: 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}
486EXPORT_SYMBOL_GPL(rtsx_usb_switch_clock);
487
488int 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}
510EXPORT_SYMBOL_GPL(rtsx_usb_card_exclusive_check);
511
512static 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 /* config OCP */
556 rtsx_usb_write_register(ucr, OCPCTL, MS_OCP_DETECT_EN, MS_OCP_DETECT_EN);
557 rtsx_usb_write_register(ucr, OCPPARA1, 0xF0, 0x50);
558 rtsx_usb_write_register(ucr, OCPPARA2, 0x7, 0x3);
559
560 /* config non-crystal mode */
561 rtsx_usb_read_register(ucr, CFG_MODE, &val);
562 if ((val & XTAL_FREE) || ((val & CLK_MODE_MASK) == CLK_MODE_NON_XTAL)) {
563 ret = rtsx_usb_write_phy_register(ucr, addr: 0xC2, val: 0x7C);
564 if (ret)
565 return ret;
566 }
567
568 return 0;
569}
570
571static int rtsx_usb_init_chip(struct rtsx_ucr *ucr)
572{
573 int ret;
574 u8 val;
575
576 rtsx_usb_clear_fsm_err(ucr);
577
578 /* power on SSC */
579 ret = rtsx_usb_write_register(ucr,
580 FPDCTL, SSC_POWER_MASK, SSC_POWER_ON);
581 if (ret)
582 return ret;
583
584 usleep_range(min: 100, max: 1000);
585 ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0x00);
586 if (ret)
587 return ret;
588
589 /* determine IC version */
590 ret = rtsx_usb_read_register(ucr, HW_VERSION, &val);
591 if (ret)
592 return ret;
593
594 ucr->ic_version = val & HW_VER_MASK;
595
596 /* determine package */
597 ret = rtsx_usb_read_register(ucr, CARD_SHARE_MODE, &val);
598 if (ret)
599 return ret;
600
601 if (val & CARD_SHARE_LQFP_SEL) {
602 ucr->package = LQFP48;
603 dev_dbg(&ucr->pusb_intf->dev, "Package: LQFP48\n");
604 } else {
605 ucr->package = QFN24;
606 dev_dbg(&ucr->pusb_intf->dev, "Package: QFN24\n");
607 }
608
609 /* determine IC variations */
610 rtsx_usb_read_register(ucr, CFG_MODE_1, &val);
611 if (val & RTS5179) {
612 ucr->is_rts5179 = true;
613 dev_dbg(&ucr->pusb_intf->dev, "Device is rts5179\n");
614 } else {
615 ucr->is_rts5179 = false;
616 }
617
618 return rtsx_usb_reset_chip(ucr);
619}
620
621static int rtsx_usb_probe(struct usb_interface *intf,
622 const struct usb_device_id *id)
623{
624 struct usb_device *usb_dev = interface_to_usbdev(intf);
625 struct rtsx_ucr *ucr;
626 int ret;
627
628 dev_dbg(&intf->dev,
629 ": Realtek USB Card Reader found at bus %03d address %03d\n",
630 usb_dev->bus->busnum, usb_dev->devnum);
631
632 ucr = devm_kzalloc(dev: &intf->dev, size: sizeof(*ucr), GFP_KERNEL);
633 if (!ucr)
634 return -ENOMEM;
635
636 ucr->pusb_dev = usb_dev;
637
638 ucr->cmd_buf = kmalloc(IOBUF_SIZE, GFP_KERNEL);
639 if (!ucr->cmd_buf)
640 return -ENOMEM;
641
642 ucr->rsp_buf = kmalloc(IOBUF_SIZE, GFP_KERNEL);
643 if (!ucr->rsp_buf) {
644 ret = -ENOMEM;
645 goto out_free_cmd_buf;
646 }
647
648 usb_set_intfdata(intf, data: ucr);
649
650 ucr->vendor_id = id->idVendor;
651 ucr->product_id = id->idProduct;
652
653 mutex_init(&ucr->dev_mutex);
654
655 ucr->pusb_intf = intf;
656
657 /* initialize */
658 ret = rtsx_usb_init_chip(ucr);
659 if (ret)
660 goto out_init_fail;
661
662 /* initialize USB SG transfer timer */
663 timer_setup(&ucr->sg_timer, rtsx_usb_sg_timed_out, 0);
664
665 ret = mfd_add_hotplug_devices(parent: &intf->dev, cells: rtsx_usb_cells,
666 ARRAY_SIZE(rtsx_usb_cells));
667 if (ret)
668 goto out_init_fail;
669
670#ifdef CONFIG_PM
671 intf->needs_remote_wakeup = 1;
672 usb_enable_autosuspend(udev: usb_dev);
673#endif
674
675 return 0;
676
677out_init_fail:
678 usb_set_intfdata(intf: ucr->pusb_intf, NULL);
679 kfree(objp: ucr->rsp_buf);
680 ucr->rsp_buf = NULL;
681out_free_cmd_buf:
682 kfree(objp: ucr->cmd_buf);
683 ucr->cmd_buf = NULL;
684 return ret;
685}
686
687static void rtsx_usb_disconnect(struct usb_interface *intf)
688{
689 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
690
691 dev_dbg(&intf->dev, "%s called\n", __func__);
692
693 mfd_remove_devices(parent: &intf->dev);
694
695 usb_set_intfdata(intf: ucr->pusb_intf, NULL);
696
697 kfree(objp: ucr->cmd_buf);
698 ucr->cmd_buf = NULL;
699
700 kfree(objp: ucr->rsp_buf);
701 ucr->rsp_buf = NULL;
702}
703
704#ifdef CONFIG_PM
705static int rtsx_usb_resume_child(struct device *dev, void *data)
706{
707 pm_request_resume(dev);
708 return 0;
709}
710
711static int rtsx_usb_suspend(struct usb_interface *intf, pm_message_t message)
712{
713 struct rtsx_ucr *ucr =
714 (struct rtsx_ucr *)usb_get_intfdata(intf);
715 u16 val = 0;
716
717 dev_dbg(&intf->dev, "%s called with pm message 0x%04x\n",
718 __func__, message.event);
719
720 if (PMSG_IS_AUTO(message)) {
721 if (mutex_trylock(&ucr->dev_mutex)) {
722 rtsx_usb_get_card_status(ucr, &val);
723 mutex_unlock(lock: &ucr->dev_mutex);
724
725 /* Defer the autosuspend if card exists */
726 if (val & (SD_CD | MS_CD)) {
727 device_for_each_child(parent: &intf->dev, NULL, fn: rtsx_usb_resume_child);
728 return -EAGAIN;
729 } else {
730 /* if the card does not exists, clear OCP status */
731 rtsx_usb_write_register(ucr, OCPCTL, MS_OCP_CLEAR, MS_OCP_CLEAR);
732 }
733 } else {
734 /* There is an ongoing operation*/
735 return -EAGAIN;
736 }
737 }
738
739 return 0;
740}
741
742static int rtsx_usb_resume(struct usb_interface *intf)
743{
744 device_for_each_child(parent: &intf->dev, NULL, fn: rtsx_usb_resume_child);
745 return 0;
746}
747
748static int rtsx_usb_reset_resume(struct usb_interface *intf)
749{
750 struct rtsx_ucr *ucr =
751 (struct rtsx_ucr *)usb_get_intfdata(intf);
752
753 rtsx_usb_reset_chip(ucr);
754 device_for_each_child(parent: &intf->dev, NULL, fn: rtsx_usb_resume_child);
755 return 0;
756}
757
758#else /* CONFIG_PM */
759
760#define rtsx_usb_suspend NULL
761#define rtsx_usb_resume NULL
762#define rtsx_usb_reset_resume NULL
763
764#endif /* CONFIG_PM */
765
766
767static int rtsx_usb_pre_reset(struct usb_interface *intf)
768{
769 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
770
771 mutex_lock(&ucr->dev_mutex);
772 return 0;
773}
774
775static int rtsx_usb_post_reset(struct usb_interface *intf)
776{
777 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
778
779 mutex_unlock(lock: &ucr->dev_mutex);
780 return 0;
781}
782
783static const struct usb_device_id rtsx_usb_usb_ids[] = {
784 { USB_DEVICE(0x0BDA, 0x0129) },
785 { USB_DEVICE(0x0BDA, 0x0139) },
786 { USB_DEVICE(0x0BDA, 0x0140) },
787 { }
788};
789MODULE_DEVICE_TABLE(usb, rtsx_usb_usb_ids);
790
791static struct usb_driver rtsx_usb_driver = {
792 .name = DRV_NAME_RTSX_USB,
793 .probe = rtsx_usb_probe,
794 .disconnect = rtsx_usb_disconnect,
795 .suspend = rtsx_usb_suspend,
796 .resume = rtsx_usb_resume,
797 .reset_resume = rtsx_usb_reset_resume,
798 .pre_reset = rtsx_usb_pre_reset,
799 .post_reset = rtsx_usb_post_reset,
800 .id_table = rtsx_usb_usb_ids,
801 .supports_autosuspend = 1,
802 .soft_unbind = 1,
803};
804
805module_usb_driver(rtsx_usb_driver);
806
807MODULE_LICENSE("GPL v2");
808MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
809MODULE_DESCRIPTION("Realtek USB Card Reader Driver");
810

source code of linux/drivers/misc/cardreader/rtsx_usb.c