1
/* Driver for Realtek RTS51xx USB card reader
3
* Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License as published by the
7
* Free Software Foundation; either version 2, or (at your option) any
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, see <http://www.gnu.org/licenses/>.
19
* wwang (wei_wang@realsil.com.cn)
20
* No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
23
#include <linux/module.h>
24
#include <linux/blkdev.h>
25
#include <linux/kthread.h>
26
#include <linux/sched.h>
27
#include <linux/workqueue.h>
28
#include <linux/kernel.h>
29
#include <linux/version.h>
31
#include <scsi/scsi.h>
32
#include <scsi/scsi_cmnd.h>
33
#include <scsi/scsi_device.h>
34
#include <linux/cdrom.h>
36
#include <linux/usb.h>
37
#include <linux/slab.h>
38
#include <linux/usb_usual.h>
41
#include "transport.h"
45
MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
46
MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
47
MODULE_LICENSE("GPL");
48
MODULE_VERSION("1.03");
50
static int auto_delink_en = 1;
51
module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
52
MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
54
struct rts51x_status {
77
struct rts51x_status *status;
84
#define FLIDX_AUTO_DELINK 0x01
86
#define SCSI_LUN(srb) ((srb)->device->lun)
89
#define SET_BIT(data, idx) ((data) |= 1 << (idx))
90
#define CLR_BIT(data, idx) ((data) &= ~(1 << (idx)))
91
#define CHK_BIT(data, idx) ((data) & (1 << (idx)))
93
#define SET_AUTO_DELINK(chip) ((chip)->flag |= FLIDX_AUTO_DELINK)
94
#define CLR_AUTO_DELINK(chip) ((chip)->flag &= ~FLIDX_AUTO_DELINK)
95
#define CHK_AUTO_DELINK(chip) ((chip)->flag & FLIDX_AUTO_DELINK)
97
#define RTS51X_GET_VID(chip) ((chip)->vendor_id)
98
#define RTS51X_GET_PID(chip) ((chip)->product_id)
100
#define FW_VERSION(chip) ((chip)->status[0].fw_ver)
101
#define STATUS_LEN(chip) ((chip)->status_len)
103
/* Check card reader function */
104
#define SUPPORT_DETAILED_TYPE1(chip) \
105
CHK_BIT((chip)->status[0].function[0], 1)
106
#define SUPPORT_OT(chip) \
107
CHK_BIT((chip)->status[0].function[0], 2)
108
#define SUPPORT_OC(chip) \
109
CHK_BIT((chip)->status[0].function[0], 3)
110
#define SUPPORT_AUTO_DELINK(chip) \
111
CHK_BIT((chip)->status[0].function[0], 4)
112
#define SUPPORT_SDIO(chip) \
113
CHK_BIT((chip)->status[0].function[1], 0)
114
#define SUPPORT_DETAILED_TYPE2(chip) \
115
CHK_BIT((chip)->status[0].function[1], 1)
117
#define CHECK_PID(chip, pid) (RTS51X_GET_PID(chip) == (pid))
118
#define CHECK_FW_VER(chip, fw_ver) (FW_VERSION(chip) == (fw_ver))
119
#define CHECK_ID(chip, pid, fw_ver) \
120
(CHECK_PID((chip), (pid)) && CHECK_FW_VER((chip), (fw_ver)))
122
#define wait_timeout_x(task_state, msecs) \
124
set_current_state((task_state)); \
125
schedule_timeout((msecs) * HZ / 1000); \
128
#define wait_timeout(msecs) \
129
wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
131
static int init_realtek_cr(struct us_data *us);
134
* The table of devices
136
#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
137
vendorName, productName, useProtocol, useTransport, \
138
initFunction, flags) \
140
USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
141
.driver_info = (flags)|(USB_US_TYPE_STOR<<24)\
144
static const struct usb_device_id realtek_cr_ids[] = {
145
# include "unusual_realtek.h"
146
{ } /* Terminating entry */
148
MODULE_DEVICE_TABLE(usb, realtek_cr_ids);
155
#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
156
vendor_name, product_name, use_protocol, use_transport, \
157
init_function, Flags) \
159
.vendorName = vendor_name, \
160
.productName = product_name, \
161
.useProtocol = use_protocol, \
162
.useTransport = use_transport, \
163
.initFunction = init_function, \
166
static struct us_unusual_dev realtek_cr_unusual_dev_list[] = {
167
# include "unusual_realtek.h"
168
{ } /* Terminating entry */
173
static int rts51x_bulk_transport(struct us_data *us, u8 lun,
174
u8 *cmd, int cmd_len, u8 *buf, int buf_len,
175
enum dma_data_direction dir, int *act_len)
177
struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
178
struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
180
unsigned int residue;
182
unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
184
/* set up the command wrapper */
185
bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
186
bcb->DataTransferLength = cpu_to_le32(buf_len);
187
bcb->Flags = (dir == DMA_FROM_DEVICE) ? 1 << 7 : 0;
188
bcb->Tag = ++us->tag;
190
bcb->Length = cmd_len;
192
/* copy the command payload */
193
memset(bcb->CDB, 0, sizeof(bcb->CDB));
194
memcpy(bcb->CDB, cmd, bcb->Length);
196
/* send it to out endpoint */
197
result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
199
if (result != USB_STOR_XFER_GOOD)
200
return USB_STOR_TRANSPORT_ERROR;
203
/* send/receive data payload, if there is any */
205
if (buf && buf_len) {
206
unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
207
us->recv_bulk_pipe : us->send_bulk_pipe;
208
result = usb_stor_bulk_transfer_buf(us, pipe,
210
if (result == USB_STOR_XFER_ERROR)
211
return USB_STOR_TRANSPORT_ERROR;
214
/* get CSW for device status */
215
result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
216
bcs, US_BULK_CS_WRAP_LEN, &cswlen);
217
if (result != USB_STOR_XFER_GOOD)
218
return USB_STOR_TRANSPORT_ERROR;
220
/* check bulk status */
221
if (bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN)) {
222
US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
223
le32_to_cpu(bcs->Signature),
225
return USB_STOR_TRANSPORT_ERROR;
228
residue = bcs->Residue;
229
if (bcs->Tag != us->tag)
230
return USB_STOR_TRANSPORT_ERROR;
232
/* try to compute the actual residue, based on how much data
233
* was really transferred and what the device tells us */
235
residue = residue < buf_len ? residue : buf_len;
238
*act_len = buf_len - residue;
240
/* based on the status code, we report good or bad */
241
switch (bcs->Status) {
242
case US_BULK_STAT_OK:
243
/* command good -- note that data could be short */
244
return USB_STOR_TRANSPORT_GOOD;
246
case US_BULK_STAT_FAIL:
248
return USB_STOR_TRANSPORT_FAILED;
250
case US_BULK_STAT_PHASE:
251
/* phase error -- note that a transport reset will be
252
* invoked by the invoke_transport() function
254
return USB_STOR_TRANSPORT_ERROR;
257
/* we should never get here, but if we do, we're in trouble */
258
return USB_STOR_TRANSPORT_ERROR;
261
/* Determine what the maximum LUN supported is */
262
static int rts51x_get_max_lun(struct us_data *us)
266
/* issue the command */
268
result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
270
USB_DIR_IN | USB_TYPE_CLASS |
272
0, us->ifnum, us->iobuf, 1, 10*HZ);
274
US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
275
result, us->iobuf[0]);
277
/* if we have a successful request, return the result */
284
static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
289
US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
293
cmnd[2] = (u8)(addr >> 8);
295
cmnd[4] = (u8)(len >> 8);
298
retval = rts51x_bulk_transport(us, 0, cmnd, 12,
299
data, len, DMA_FROM_DEVICE, NULL);
300
if (retval != USB_STOR_TRANSPORT_GOOD)
306
static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
311
US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
315
cmnd[2] = (u8)(addr >> 8);
317
cmnd[4] = (u8)(len >> 8);
320
retval = rts51x_bulk_transport(us, 0, cmnd, 12,
321
data, len, DMA_TO_DEVICE, NULL);
322
if (retval != USB_STOR_TRANSPORT_GOOD)
328
static int rts51x_read_status(struct us_data *us,
329
u8 lun, u8 *status, int len, int *actlen)
334
US_DEBUGP("%s, lun = %d\n", __func__, lun);
339
retval = rts51x_bulk_transport(us, lun, cmnd, 12,
340
status, len, DMA_FROM_DEVICE, actlen);
341
if (retval != USB_STOR_TRANSPORT_GOOD)
347
static int rts51x_check_status(struct us_data *us, u8 lun)
349
struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
353
retval = rts51x_read_status(us, lun, buf, 16, &(chip->status_len));
357
US_DEBUGP("chip->status_len = %d\n", chip->status_len);
359
chip->status[lun].vid = ((u16)buf[0] << 8) | buf[1];
360
chip->status[lun].pid = ((u16)buf[2] << 8) | buf[3];
361
chip->status[lun].cur_lun = buf[4];
362
chip->status[lun].card_type = buf[5];
363
chip->status[lun].total_lun = buf[6];
364
chip->status[lun].fw_ver = ((u16)buf[7] << 8) | buf[8];
365
chip->status[lun].phy_exist = buf[9];
366
chip->status[lun].multi_flag = buf[10];
367
chip->status[lun].multi_card = buf[11];
368
chip->status[lun].log_exist = buf[12];
369
if (chip->status_len == 16) {
370
chip->status[lun].detailed_type.detailed_type1 = buf[13];
371
chip->status[lun].function[0] = buf[14];
372
chip->status[lun].function[1] = buf[15];
378
static int enable_oscillator(struct us_data *us)
383
retval = rts51x_read_mem(us, 0xFE77, &value, 1);
388
retval = rts51x_write_mem(us, 0xFE77, &value, 1);
392
retval = rts51x_read_mem(us, 0xFE77, &value, 1);
402
static int do_config_autodelink(struct us_data *us, int enable, int force)
407
retval = rts51x_read_mem(us, 0xFE47, &value, 1);
420
US_DEBUGP("In %s,set 0xfe47 to 0x%x\n", __func__, value);
422
retval = rts51x_write_mem(us, 0xFE47, &value, 1);
429
static int config_autodelink_after_power_on(struct us_data *us)
431
struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
435
if (!CHK_AUTO_DELINK(chip))
438
retval = rts51x_read_mem(us, 0xFE47, &value, 1);
442
if (auto_delink_en) {
447
if (CHECK_ID(chip, 0x0138, 0x3882))
452
retval = rts51x_write_mem(us, 0xFE47, &value, 1);
456
retval = enable_oscillator(us);
458
(void)do_config_autodelink(us, 1, 0);
460
/* Autodelink controlled by firmware */
464
if (CHECK_ID(chip, 0x0138, 0x3882))
467
if (CHECK_ID(chip, 0x0159, 0x5889) ||
468
CHECK_ID(chip, 0x0138, 0x3880)) {
473
retval = rts51x_write_mem(us, 0xFE47, &value, 1);
477
if (CHECK_ID(chip, 0x0159, 0x5888)) {
479
retval = rts51x_write_mem(us, 0xFE79, &value, 1);
484
retval = rts51x_write_mem(us, 0x48, &value, 1);
493
static int config_autodelink_before_power_down(struct us_data *us)
495
struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
499
if (!CHK_AUTO_DELINK(chip))
502
if (auto_delink_en) {
503
retval = rts51x_read_mem(us, 0xFE77, &value, 1);
508
retval = rts51x_write_mem(us, 0xFE77, &value, 1);
512
if (CHECK_ID(chip, 0x0159, 0x5888)) {
514
retval = rts51x_write_mem(us, 0x48, &value, 1);
519
retval = rts51x_read_mem(us, 0xFE47, &value, 1);
524
if (CHECK_ID(chip, 0x0138, 0x3882))
526
retval = rts51x_write_mem(us, 0xFE77, &value, 1);
530
if (CHECK_ID(chip, 0x0159, 0x5889) ||
531
CHECK_ID(chip, 0x0138, 0x3880) ||
532
CHECK_ID(chip, 0x0138, 0x3882)) {
533
retval = rts51x_read_mem(us, 0xFE47, &value, 1);
537
if (CHECK_ID(chip, 0x0159, 0x5889) ||
538
CHECK_ID(chip, 0x0138, 0x3880)) {
543
if (CHECK_ID(chip, 0x0138, 0x3882))
546
retval = rts51x_write_mem(us, 0xFE47, &value, 1);
551
if (CHECK_ID(chip, 0x0159, 0x5888)) {
553
retval = rts51x_write_mem(us, 0x48, &value, 1);
562
static void realtek_cr_destructor(void *extra)
564
struct rts51x_chip *chip = (struct rts51x_chip *)extra;
573
static void realtek_pm_hook(struct us_data *us, int pm_state)
575
if (pm_state == US_SUSPEND)
576
(void)config_autodelink_before_power_down(us);
580
static int init_realtek_cr(struct us_data *us)
582
struct rts51x_chip *chip;
585
chip = kzalloc(sizeof(struct rts51x_chip), GFP_KERNEL);
590
us->extra_destructor = realtek_cr_destructor;
592
us->suspend_resume_hook = realtek_pm_hook;
595
us->max_lun = chip->max_lun = rts51x_get_max_lun(us);
597
US_DEBUGP("chip->max_lun = %d\n", chip->max_lun);
599
size = (chip->max_lun + 1) * sizeof(struct rts51x_status);
600
chip->status = kzalloc(size, GFP_KERNEL);
604
for (i = 0; i <= (int)(chip->max_lun); i++) {
605
retval = rts51x_check_status(us, (u8)i);
610
if (CHECK_FW_VER(chip, 0x5888) || CHECK_FW_VER(chip, 0x5889) ||
611
CHECK_FW_VER(chip, 0x5901))
612
SET_AUTO_DELINK(chip);
613
if (STATUS_LEN(chip) == 16) {
614
if (SUPPORT_AUTO_DELINK(chip))
615
SET_AUTO_DELINK(chip);
618
US_DEBUGP("chip->flag = 0x%x\n", chip->flag);
620
(void)config_autodelink_after_power_on(us);
634
static int realtek_cr_probe(struct usb_interface *intf,
635
const struct usb_device_id *id)
640
US_DEBUGP("Probe Realtek Card Reader!\n");
642
result = usb_stor_probe1(&us, intf, id,
643
(id - realtek_cr_ids) + realtek_cr_unusual_dev_list);
647
result = usb_stor_probe2(us);
651
static struct usb_driver realtek_cr_driver = {
652
.name = "ums-realtek",
653
.probe = realtek_cr_probe,
654
.disconnect = usb_stor_disconnect,
655
.suspend = usb_stor_suspend,
656
.resume = usb_stor_resume,
657
.reset_resume = usb_stor_reset_resume,
658
.pre_reset = usb_stor_pre_reset,
659
.post_reset = usb_stor_post_reset,
660
.id_table = realtek_cr_ids,
664
static int __init realtek_cr_init(void)
666
return usb_register(&realtek_cr_driver);
669
static void __exit realtek_cr_exit(void)
671
usb_deregister(&realtek_cr_driver);
674
module_init(realtek_cr_init);
675
module_exit(realtek_cr_exit);