Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Driver for Realtek RTS51xx USB card reader
0004  *
0005  * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
0006  *
0007  * Author:
0008  *   wwang (wei_wang@realsil.com.cn)
0009  *   No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/blkdev.h>
0014 #include <linux/kthread.h>
0015 #include <linux/sched.h>
0016 #include <linux/kernel.h>
0017 
0018 #include <scsi/scsi.h>
0019 #include <scsi/scsi_cmnd.h>
0020 #include <scsi/scsi_device.h>
0021 #include <linux/cdrom.h>
0022 
0023 #include <linux/usb.h>
0024 #include <linux/slab.h>
0025 #include <linux/usb_usual.h>
0026 
0027 #include "usb.h"
0028 #include "transport.h"
0029 #include "protocol.h"
0030 #include "debug.h"
0031 #include "scsiglue.h"
0032 
0033 #define DRV_NAME "ums-realtek"
0034 
0035 MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
0036 MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
0037 MODULE_LICENSE("GPL");
0038 MODULE_IMPORT_NS(USB_STORAGE);
0039 
0040 static int auto_delink_en = 1;
0041 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
0042 MODULE_PARM_DESC(auto_delink_en, "auto delink mode (0=firmware, 1=software [default])");
0043 
0044 #ifdef CONFIG_REALTEK_AUTOPM
0045 static int ss_en = 1;
0046 module_param(ss_en, int, S_IRUGO | S_IWUSR);
0047 MODULE_PARM_DESC(ss_en, "enable selective suspend");
0048 
0049 static int ss_delay = 50;
0050 module_param(ss_delay, int, S_IRUGO | S_IWUSR);
0051 MODULE_PARM_DESC(ss_delay,
0052          "seconds to delay before entering selective suspend");
0053 
0054 enum RTS51X_STAT {
0055     RTS51X_STAT_INIT,
0056     RTS51X_STAT_IDLE,
0057     RTS51X_STAT_RUN,
0058     RTS51X_STAT_SS
0059 };
0060 
0061 #define POLLING_INTERVAL    50
0062 
0063 #define rts51x_set_stat(chip, stat) \
0064     ((chip)->state = (enum RTS51X_STAT)(stat))
0065 #define rts51x_get_stat(chip)       ((chip)->state)
0066 
0067 #define SET_LUN_READY(chip, lun)    ((chip)->lun_ready |= ((u8)1 << (lun)))
0068 #define CLR_LUN_READY(chip, lun)    ((chip)->lun_ready &= ~((u8)1 << (lun)))
0069 #define TST_LUN_READY(chip, lun)    ((chip)->lun_ready & ((u8)1 << (lun)))
0070 
0071 #endif
0072 
0073 struct rts51x_status {
0074     u16 vid;
0075     u16 pid;
0076     u8 cur_lun;
0077     u8 card_type;
0078     u8 total_lun;
0079     u16 fw_ver;
0080     u8 phy_exist;
0081     u8 multi_flag;
0082     u8 multi_card;
0083     u8 log_exist;
0084     union {
0085         u8 detailed_type1;
0086         u8 detailed_type2;
0087     } detailed_type;
0088     u8 function[2];
0089 };
0090 
0091 struct rts51x_chip {
0092     u16 vendor_id;
0093     u16 product_id;
0094     char max_lun;
0095 
0096     struct rts51x_status *status;
0097     int status_len;
0098 
0099     u32 flag;
0100     struct us_data *us;
0101 
0102 #ifdef CONFIG_REALTEK_AUTOPM
0103     struct timer_list rts51x_suspend_timer;
0104     unsigned long timer_expires;
0105     int pwr_state;
0106     u8 lun_ready;
0107     enum RTS51X_STAT state;
0108     int support_auto_delink;
0109 #endif
0110     /* used to back up the protocol chosen in probe1 phase */
0111     proto_cmnd proto_handler_backup;
0112 };
0113 
0114 /* flag definition */
0115 #define FLIDX_AUTO_DELINK       0x01
0116 
0117 #define SCSI_LUN(srb)           ((srb)->device->lun)
0118 
0119 /* Bit Operation */
0120 #define SET_BIT(data, idx)      ((data) |= 1 << (idx))
0121 #define CLR_BIT(data, idx)      ((data) &= ~(1 << (idx)))
0122 #define CHK_BIT(data, idx)      ((data) & (1 << (idx)))
0123 
0124 #define SET_AUTO_DELINK(chip)       ((chip)->flag |= FLIDX_AUTO_DELINK)
0125 #define CLR_AUTO_DELINK(chip)       ((chip)->flag &= ~FLIDX_AUTO_DELINK)
0126 #define CHK_AUTO_DELINK(chip)       ((chip)->flag & FLIDX_AUTO_DELINK)
0127 
0128 #define RTS51X_GET_VID(chip)        ((chip)->vendor_id)
0129 #define RTS51X_GET_PID(chip)        ((chip)->product_id)
0130 
0131 #define VENDOR_ID(chip)         ((chip)->status[0].vid)
0132 #define PRODUCT_ID(chip)        ((chip)->status[0].pid)
0133 #define FW_VERSION(chip)        ((chip)->status[0].fw_ver)
0134 #define STATUS_LEN(chip)        ((chip)->status_len)
0135 
0136 #define STATUS_SUCCESS      0
0137 #define STATUS_FAIL     1
0138 
0139 /* Check card reader function */
0140 #define SUPPORT_DETAILED_TYPE1(chip)    \
0141         CHK_BIT((chip)->status[0].function[0], 1)
0142 #define SUPPORT_OT(chip)        \
0143         CHK_BIT((chip)->status[0].function[0], 2)
0144 #define SUPPORT_OC(chip)        \
0145         CHK_BIT((chip)->status[0].function[0], 3)
0146 #define SUPPORT_AUTO_DELINK(chip)   \
0147         CHK_BIT((chip)->status[0].function[0], 4)
0148 #define SUPPORT_SDIO(chip)      \
0149         CHK_BIT((chip)->status[0].function[1], 0)
0150 #define SUPPORT_DETAILED_TYPE2(chip)    \
0151         CHK_BIT((chip)->status[0].function[1], 1)
0152 
0153 #define CHECK_PID(chip, pid)        (RTS51X_GET_PID(chip) == (pid))
0154 #define CHECK_FW_VER(chip, fw_ver)  (FW_VERSION(chip) == (fw_ver))
0155 #define CHECK_ID(chip, pid, fw_ver) \
0156         (CHECK_PID((chip), (pid)) && CHECK_FW_VER((chip), (fw_ver)))
0157 
0158 static int init_realtek_cr(struct us_data *us);
0159 
0160 /*
0161  * The table of devices
0162  */
0163 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
0164             vendorName, productName, useProtocol, useTransport, \
0165             initFunction, flags) \
0166 {\
0167     USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
0168     .driver_info = (flags) \
0169 }
0170 
0171 static const struct usb_device_id realtek_cr_ids[] = {
0172 #   include "unusual_realtek.h"
0173     {}          /* Terminating entry */
0174 };
0175 
0176 MODULE_DEVICE_TABLE(usb, realtek_cr_ids);
0177 
0178 #undef UNUSUAL_DEV
0179 
0180 /*
0181  * The flags table
0182  */
0183 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
0184             vendor_name, product_name, use_protocol, use_transport, \
0185             init_function, Flags) \
0186 { \
0187     .vendorName = vendor_name,  \
0188     .productName = product_name,    \
0189     .useProtocol = use_protocol,    \
0190     .useTransport = use_transport,  \
0191     .initFunction = init_function,  \
0192 }
0193 
0194 static struct us_unusual_dev realtek_cr_unusual_dev_list[] = {
0195 #   include "unusual_realtek.h"
0196     {}          /* Terminating entry */
0197 };
0198 
0199 #undef UNUSUAL_DEV
0200 
0201 static int rts51x_bulk_transport(struct us_data *us, u8 lun,
0202                  u8 *cmd, int cmd_len, u8 *buf, int buf_len,
0203                  enum dma_data_direction dir, int *act_len)
0204 {
0205     struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *)us->iobuf;
0206     struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *)us->iobuf;
0207     int result;
0208     unsigned int residue;
0209     unsigned int cswlen;
0210     unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
0211 
0212     /* set up the command wrapper */
0213     bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
0214     bcb->DataTransferLength = cpu_to_le32(buf_len);
0215     bcb->Flags = (dir == DMA_FROM_DEVICE) ? US_BULK_FLAG_IN : 0;
0216     bcb->Tag = ++us->tag;
0217     bcb->Lun = lun;
0218     bcb->Length = cmd_len;
0219 
0220     /* copy the command payload */
0221     memset(bcb->CDB, 0, sizeof(bcb->CDB));
0222     memcpy(bcb->CDB, cmd, bcb->Length);
0223 
0224     /* send it to out endpoint */
0225     result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0226                         bcb, cbwlen, NULL);
0227     if (result != USB_STOR_XFER_GOOD)
0228         return USB_STOR_TRANSPORT_ERROR;
0229 
0230     /* DATA STAGE */
0231     /* send/receive data payload, if there is any */
0232 
0233     if (buf && buf_len) {
0234         unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
0235             us->recv_bulk_pipe : us->send_bulk_pipe;
0236         result = usb_stor_bulk_transfer_buf(us, pipe,
0237                             buf, buf_len, NULL);
0238         if (result == USB_STOR_XFER_ERROR)
0239             return USB_STOR_TRANSPORT_ERROR;
0240     }
0241 
0242     /* get CSW for device status */
0243     result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
0244                         bcs, US_BULK_CS_WRAP_LEN, &cswlen);
0245     if (result != USB_STOR_XFER_GOOD)
0246         return USB_STOR_TRANSPORT_ERROR;
0247 
0248     /* check bulk status */
0249     if (bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN)) {
0250         usb_stor_dbg(us, "Signature mismatch: got %08X, expecting %08X\n",
0251                  le32_to_cpu(bcs->Signature), US_BULK_CS_SIGN);
0252         return USB_STOR_TRANSPORT_ERROR;
0253     }
0254 
0255     residue = bcs->Residue;
0256     if (bcs->Tag != us->tag)
0257         return USB_STOR_TRANSPORT_ERROR;
0258 
0259     /*
0260      * try to compute the actual residue, based on how much data
0261      * was really transferred and what the device tells us
0262      */
0263     if (residue)
0264         residue = residue < buf_len ? residue : buf_len;
0265 
0266     if (act_len)
0267         *act_len = buf_len - residue;
0268 
0269     /* based on the status code, we report good or bad */
0270     switch (bcs->Status) {
0271     case US_BULK_STAT_OK:
0272         /* command good -- note that data could be short */
0273         return USB_STOR_TRANSPORT_GOOD;
0274 
0275     case US_BULK_STAT_FAIL:
0276         /* command failed */
0277         return USB_STOR_TRANSPORT_FAILED;
0278 
0279     case US_BULK_STAT_PHASE:
0280         /*
0281          * phase error -- note that a transport reset will be
0282          * invoked by the invoke_transport() function
0283          */
0284         return USB_STOR_TRANSPORT_ERROR;
0285     }
0286 
0287     /* we should never get here, but if we do, we're in trouble */
0288     return USB_STOR_TRANSPORT_ERROR;
0289 }
0290 
0291 static int rts51x_bulk_transport_special(struct us_data *us, u8 lun,
0292                  u8 *cmd, int cmd_len, u8 *buf, int buf_len,
0293                  enum dma_data_direction dir, int *act_len)
0294 {
0295     struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
0296     struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
0297     int result;
0298     unsigned int cswlen;
0299     unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
0300 
0301     /* set up the command wrapper */
0302     bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
0303     bcb->DataTransferLength = cpu_to_le32(buf_len);
0304     bcb->Flags = (dir == DMA_FROM_DEVICE) ? US_BULK_FLAG_IN : 0;
0305     bcb->Tag = ++us->tag;
0306     bcb->Lun = lun;
0307     bcb->Length = cmd_len;
0308 
0309     /* copy the command payload */
0310     memset(bcb->CDB, 0, sizeof(bcb->CDB));
0311     memcpy(bcb->CDB, cmd, bcb->Length);
0312 
0313     /* send it to out endpoint */
0314     result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0315                 bcb, cbwlen, NULL);
0316     if (result != USB_STOR_XFER_GOOD)
0317         return USB_STOR_TRANSPORT_ERROR;
0318 
0319     /* DATA STAGE */
0320     /* send/receive data payload, if there is any */
0321 
0322     if (buf && buf_len) {
0323         unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
0324                 us->recv_bulk_pipe : us->send_bulk_pipe;
0325         result = usb_stor_bulk_transfer_buf(us, pipe,
0326                 buf, buf_len, NULL);
0327         if (result == USB_STOR_XFER_ERROR)
0328             return USB_STOR_TRANSPORT_ERROR;
0329     }
0330 
0331     /* get CSW for device status */
0332     result = usb_bulk_msg(us->pusb_dev, us->recv_bulk_pipe, bcs,
0333             US_BULK_CS_WRAP_LEN, &cswlen, 250);
0334     return result;
0335 }
0336 
0337 /* Determine what the maximum LUN supported is */
0338 static int rts51x_get_max_lun(struct us_data *us)
0339 {
0340     int result;
0341 
0342     /* issue the command */
0343     us->iobuf[0] = 0;
0344     result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
0345                       US_BULK_GET_MAX_LUN,
0346                       USB_DIR_IN | USB_TYPE_CLASS |
0347                       USB_RECIP_INTERFACE,
0348                       0, us->ifnum, us->iobuf, 1, 10 * HZ);
0349 
0350     usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
0351              result, us->iobuf[0]);
0352 
0353     /* if we have a successful request, return the result */
0354     if (result > 0)
0355         return us->iobuf[0];
0356 
0357     return 0;
0358 }
0359 
0360 static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
0361 {
0362     int retval;
0363     u8 cmnd[12] = { 0 };
0364     u8 *buf;
0365 
0366     buf = kmalloc(len, GFP_NOIO);
0367     if (buf == NULL)
0368         return -ENOMEM;
0369 
0370     usb_stor_dbg(us, "addr = 0x%x, len = %d\n", addr, len);
0371 
0372     cmnd[0] = 0xF0;
0373     cmnd[1] = 0x0D;
0374     cmnd[2] = (u8) (addr >> 8);
0375     cmnd[3] = (u8) addr;
0376     cmnd[4] = (u8) (len >> 8);
0377     cmnd[5] = (u8) len;
0378 
0379     retval = rts51x_bulk_transport(us, 0, cmnd, 12,
0380                        buf, len, DMA_FROM_DEVICE, NULL);
0381     if (retval != USB_STOR_TRANSPORT_GOOD) {
0382         kfree(buf);
0383         return -EIO;
0384     }
0385 
0386     memcpy(data, buf, len);
0387     kfree(buf);
0388     return 0;
0389 }
0390 
0391 static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
0392 {
0393     int retval;
0394     u8 cmnd[12] = { 0 };
0395     u8 *buf;
0396 
0397     buf = kmemdup(data, len, GFP_NOIO);
0398     if (buf == NULL)
0399         return USB_STOR_TRANSPORT_ERROR;
0400 
0401     usb_stor_dbg(us, "addr = 0x%x, len = %d\n", addr, len);
0402 
0403     cmnd[0] = 0xF0;
0404     cmnd[1] = 0x0E;
0405     cmnd[2] = (u8) (addr >> 8);
0406     cmnd[3] = (u8) addr;
0407     cmnd[4] = (u8) (len >> 8);
0408     cmnd[5] = (u8) len;
0409 
0410     retval = rts51x_bulk_transport(us, 0, cmnd, 12,
0411                        buf, len, DMA_TO_DEVICE, NULL);
0412     kfree(buf);
0413     if (retval != USB_STOR_TRANSPORT_GOOD)
0414         return -EIO;
0415 
0416     return 0;
0417 }
0418 
0419 static int rts51x_read_status(struct us_data *us,
0420                   u8 lun, u8 *status, int len, int *actlen)
0421 {
0422     int retval;
0423     u8 cmnd[12] = { 0 };
0424     u8 *buf;
0425 
0426     buf = kmalloc(len, GFP_NOIO);
0427     if (buf == NULL)
0428         return USB_STOR_TRANSPORT_ERROR;
0429 
0430     usb_stor_dbg(us, "lun = %d\n", lun);
0431 
0432     cmnd[0] = 0xF0;
0433     cmnd[1] = 0x09;
0434 
0435     retval = rts51x_bulk_transport(us, lun, cmnd, 12,
0436                        buf, len, DMA_FROM_DEVICE, actlen);
0437     if (retval != USB_STOR_TRANSPORT_GOOD) {
0438         kfree(buf);
0439         return -EIO;
0440     }
0441 
0442     memcpy(status, buf, len);
0443     kfree(buf);
0444     return 0;
0445 }
0446 
0447 static int rts51x_check_status(struct us_data *us, u8 lun)
0448 {
0449     struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
0450     int retval;
0451     u8 buf[16];
0452 
0453     retval = rts51x_read_status(us, lun, buf, 16, &(chip->status_len));
0454     if (retval != STATUS_SUCCESS)
0455         return -EIO;
0456 
0457     usb_stor_dbg(us, "chip->status_len = %d\n", chip->status_len);
0458 
0459     chip->status[lun].vid = ((u16) buf[0] << 8) | buf[1];
0460     chip->status[lun].pid = ((u16) buf[2] << 8) | buf[3];
0461     chip->status[lun].cur_lun = buf[4];
0462     chip->status[lun].card_type = buf[5];
0463     chip->status[lun].total_lun = buf[6];
0464     chip->status[lun].fw_ver = ((u16) buf[7] << 8) | buf[8];
0465     chip->status[lun].phy_exist = buf[9];
0466     chip->status[lun].multi_flag = buf[10];
0467     chip->status[lun].multi_card = buf[11];
0468     chip->status[lun].log_exist = buf[12];
0469     if (chip->status_len == 16) {
0470         chip->status[lun].detailed_type.detailed_type1 = buf[13];
0471         chip->status[lun].function[0] = buf[14];
0472         chip->status[lun].function[1] = buf[15];
0473     }
0474 
0475     return 0;
0476 }
0477 
0478 static int enable_oscillator(struct us_data *us)
0479 {
0480     int retval;
0481     u8 value;
0482 
0483     retval = rts51x_read_mem(us, 0xFE77, &value, 1);
0484     if (retval < 0)
0485         return -EIO;
0486 
0487     value |= 0x04;
0488     retval = rts51x_write_mem(us, 0xFE77, &value, 1);
0489     if (retval < 0)
0490         return -EIO;
0491 
0492     retval = rts51x_read_mem(us, 0xFE77, &value, 1);
0493     if (retval < 0)
0494         return -EIO;
0495 
0496     if (!(value & 0x04))
0497         return -EIO;
0498 
0499     return 0;
0500 }
0501 
0502 static int __do_config_autodelink(struct us_data *us, u8 *data, u16 len)
0503 {
0504     int retval;
0505     u8 cmnd[12] = {0};
0506     u8 *buf;
0507 
0508     usb_stor_dbg(us, "addr = 0xfe47, len = %d\n", len);
0509 
0510     buf = kmemdup(data, len, GFP_NOIO);
0511     if (!buf)
0512         return USB_STOR_TRANSPORT_ERROR;
0513 
0514     cmnd[0] = 0xF0;
0515     cmnd[1] = 0x0E;
0516     cmnd[2] = 0xfe;
0517     cmnd[3] = 0x47;
0518     cmnd[4] = (u8)(len >> 8);
0519     cmnd[5] = (u8)len;
0520 
0521     retval = rts51x_bulk_transport_special(us, 0, cmnd, 12, buf, len, DMA_TO_DEVICE, NULL);
0522     kfree(buf);
0523     if (retval != USB_STOR_TRANSPORT_GOOD) {
0524         return -EIO;
0525     }
0526 
0527     return 0;
0528 }
0529 
0530 static int do_config_autodelink(struct us_data *us, int enable, int force)
0531 {
0532     int retval;
0533     u8 value;
0534 
0535     retval = rts51x_read_mem(us, 0xFE47, &value, 1);
0536     if (retval < 0)
0537         return -EIO;
0538 
0539     if (enable) {
0540         if (force)
0541             value |= 0x03;
0542         else
0543             value |= 0x01;
0544     } else {
0545         value &= ~0x03;
0546     }
0547 
0548     usb_stor_dbg(us, "set 0xfe47 to 0x%x\n", value);
0549 
0550     /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
0551     retval = __do_config_autodelink(us, &value, 1);
0552     if (retval < 0)
0553         return -EIO;
0554 
0555     return 0;
0556 }
0557 
0558 static int config_autodelink_after_power_on(struct us_data *us)
0559 {
0560     struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
0561     int retval;
0562     u8 value;
0563 
0564     if (!CHK_AUTO_DELINK(chip))
0565         return 0;
0566 
0567     retval = rts51x_read_mem(us, 0xFE47, &value, 1);
0568     if (retval < 0)
0569         return -EIO;
0570 
0571     if (auto_delink_en) {
0572         CLR_BIT(value, 0);
0573         CLR_BIT(value, 1);
0574         SET_BIT(value, 2);
0575 
0576         if (CHECK_ID(chip, 0x0138, 0x3882))
0577             CLR_BIT(value, 2);
0578 
0579         SET_BIT(value, 7);
0580 
0581         /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
0582         retval = __do_config_autodelink(us, &value, 1);
0583         if (retval < 0)
0584             return -EIO;
0585 
0586         retval = enable_oscillator(us);
0587         if (retval == 0)
0588             (void)do_config_autodelink(us, 1, 0);
0589     } else {
0590         /* Autodelink controlled by firmware */
0591 
0592         SET_BIT(value, 2);
0593 
0594         if (CHECK_ID(chip, 0x0138, 0x3882))
0595             CLR_BIT(value, 2);
0596 
0597         if (CHECK_ID(chip, 0x0159, 0x5889) ||
0598             CHECK_ID(chip, 0x0138, 0x3880)) {
0599             CLR_BIT(value, 0);
0600             CLR_BIT(value, 7);
0601         }
0602 
0603         /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
0604         retval = __do_config_autodelink(us, &value, 1);
0605         if (retval < 0)
0606             return -EIO;
0607 
0608         if (CHECK_ID(chip, 0x0159, 0x5888)) {
0609             value = 0xFF;
0610             retval = rts51x_write_mem(us, 0xFE79, &value, 1);
0611             if (retval < 0)
0612                 return -EIO;
0613 
0614             value = 0x01;
0615             retval = rts51x_write_mem(us, 0x48, &value, 1);
0616             if (retval < 0)
0617                 return -EIO;
0618         }
0619     }
0620 
0621     return 0;
0622 }
0623 
0624 #ifdef CONFIG_PM
0625 static int config_autodelink_before_power_down(struct us_data *us)
0626 {
0627     struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
0628     int retval;
0629     u8 value;
0630 
0631     if (!CHK_AUTO_DELINK(chip))
0632         return 0;
0633 
0634     if (auto_delink_en) {
0635         retval = rts51x_read_mem(us, 0xFE77, &value, 1);
0636         if (retval < 0)
0637             return -EIO;
0638 
0639         SET_BIT(value, 2);
0640         retval = rts51x_write_mem(us, 0xFE77, &value, 1);
0641         if (retval < 0)
0642             return -EIO;
0643 
0644         if (CHECK_ID(chip, 0x0159, 0x5888)) {
0645             value = 0x01;
0646             retval = rts51x_write_mem(us, 0x48, &value, 1);
0647             if (retval < 0)
0648                 return -EIO;
0649         }
0650 
0651         retval = rts51x_read_mem(us, 0xFE47, &value, 1);
0652         if (retval < 0)
0653             return -EIO;
0654 
0655         SET_BIT(value, 0);
0656         if (CHECK_ID(chip, 0x0138, 0x3882))
0657             SET_BIT(value, 2);
0658         retval = rts51x_write_mem(us, 0xFE77, &value, 1);
0659         if (retval < 0)
0660             return -EIO;
0661     } else {
0662         if (CHECK_ID(chip, 0x0159, 0x5889) ||
0663             CHECK_ID(chip, 0x0138, 0x3880) ||
0664             CHECK_ID(chip, 0x0138, 0x3882)) {
0665             retval = rts51x_read_mem(us, 0xFE47, &value, 1);
0666             if (retval < 0)
0667                 return -EIO;
0668 
0669             if (CHECK_ID(chip, 0x0159, 0x5889) ||
0670                 CHECK_ID(chip, 0x0138, 0x3880)) {
0671                 SET_BIT(value, 0);
0672                 SET_BIT(value, 7);
0673             }
0674 
0675             if (CHECK_ID(chip, 0x0138, 0x3882))
0676                 SET_BIT(value, 2);
0677 
0678             /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
0679             retval = __do_config_autodelink(us, &value, 1);
0680             if (retval < 0)
0681                 return -EIO;
0682         }
0683 
0684         if (CHECK_ID(chip, 0x0159, 0x5888)) {
0685             value = 0x01;
0686             retval = rts51x_write_mem(us, 0x48, &value, 1);
0687             if (retval < 0)
0688                 return -EIO;
0689         }
0690     }
0691 
0692     return 0;
0693 }
0694 
0695 static void fw5895_init(struct us_data *us)
0696 {
0697     struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
0698     int retval;
0699     u8 val;
0700 
0701     if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
0702         usb_stor_dbg(us, "Not the specified device, return immediately!\n");
0703     } else {
0704         retval = rts51x_read_mem(us, 0xFD6F, &val, 1);
0705         if (retval == STATUS_SUCCESS && (val & 0x1F) == 0) {
0706             val = 0x1F;
0707             retval = rts51x_write_mem(us, 0xFD70, &val, 1);
0708             if (retval != STATUS_SUCCESS)
0709                 usb_stor_dbg(us, "Write memory fail\n");
0710         } else {
0711             usb_stor_dbg(us, "Read memory fail, OR (val & 0x1F) != 0\n");
0712         }
0713     }
0714 }
0715 #endif
0716 
0717 #ifdef CONFIG_REALTEK_AUTOPM
0718 static void fw5895_set_mmc_wp(struct us_data *us)
0719 {
0720     struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
0721     int retval;
0722     u8 buf[13];
0723 
0724     if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
0725         usb_stor_dbg(us, "Not the specified device, return immediately!\n");
0726     } else {
0727         retval = rts51x_read_mem(us, 0xFD6F, buf, 1);
0728         if (retval == STATUS_SUCCESS && (buf[0] & 0x24) == 0x24) {
0729             /* SD Exist and SD WP */
0730             retval = rts51x_read_mem(us, 0xD04E, buf, 1);
0731             if (retval == STATUS_SUCCESS) {
0732                 buf[0] |= 0x04;
0733                 retval = rts51x_write_mem(us, 0xFD70, buf, 1);
0734                 if (retval != STATUS_SUCCESS)
0735                     usb_stor_dbg(us, "Write memory fail\n");
0736             } else {
0737                 usb_stor_dbg(us, "Read memory fail\n");
0738             }
0739         } else {
0740             usb_stor_dbg(us, "Read memory fail, OR (buf[0]&0x24)!=0x24\n");
0741         }
0742     }
0743 }
0744 
0745 static void rts51x_modi_suspend_timer(struct rts51x_chip *chip)
0746 {
0747     struct us_data *us = chip->us;
0748 
0749     usb_stor_dbg(us, "state:%d\n", rts51x_get_stat(chip));
0750 
0751     chip->timer_expires = jiffies + msecs_to_jiffies(1000*ss_delay);
0752     mod_timer(&chip->rts51x_suspend_timer, chip->timer_expires);
0753 }
0754 
0755 static void rts51x_suspend_timer_fn(struct timer_list *t)
0756 {
0757     struct rts51x_chip *chip = from_timer(chip, t, rts51x_suspend_timer);
0758     struct us_data *us = chip->us;
0759 
0760     switch (rts51x_get_stat(chip)) {
0761     case RTS51X_STAT_INIT:
0762     case RTS51X_STAT_RUN:
0763         rts51x_modi_suspend_timer(chip);
0764         break;
0765     case RTS51X_STAT_IDLE:
0766     case RTS51X_STAT_SS:
0767         usb_stor_dbg(us, "RTS51X_STAT_SS, power.usage:%d\n",
0768                  atomic_read(&us->pusb_intf->dev.power.usage_count));
0769 
0770         if (atomic_read(&us->pusb_intf->dev.power.usage_count) > 0) {
0771             usb_stor_dbg(us, "Ready to enter SS state\n");
0772             rts51x_set_stat(chip, RTS51X_STAT_SS);
0773             /* ignore mass storage interface's children */
0774             pm_suspend_ignore_children(&us->pusb_intf->dev, true);
0775             usb_autopm_put_interface_async(us->pusb_intf);
0776             usb_stor_dbg(us, "RTS51X_STAT_SS 01, power.usage:%d\n",
0777                      atomic_read(&us->pusb_intf->dev.power.usage_count));
0778         }
0779         break;
0780     default:
0781         usb_stor_dbg(us, "Unknown state !!!\n");
0782         break;
0783     }
0784 }
0785 
0786 static inline int working_scsi(struct scsi_cmnd *srb)
0787 {
0788     if ((srb->cmnd[0] == TEST_UNIT_READY) ||
0789         (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL)) {
0790         return 0;
0791     }
0792 
0793     return 1;
0794 }
0795 
0796 static void rts51x_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
0797 {
0798     struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
0799     static int card_first_show = 1;
0800     static u8 media_not_present[] = { 0x70, 0, 0x02, 0, 0, 0, 0,
0801         10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0
0802     };
0803     static u8 invalid_cmd_field[] = { 0x70, 0, 0x05, 0, 0, 0, 0,
0804         10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0
0805     };
0806     int ret;
0807 
0808     if (working_scsi(srb)) {
0809         usb_stor_dbg(us, "working scsi, power.usage:%d\n",
0810                  atomic_read(&us->pusb_intf->dev.power.usage_count));
0811 
0812         if (atomic_read(&us->pusb_intf->dev.power.usage_count) <= 0) {
0813             ret = usb_autopm_get_interface(us->pusb_intf);
0814             usb_stor_dbg(us, "working scsi, ret=%d\n", ret);
0815         }
0816         if (rts51x_get_stat(chip) != RTS51X_STAT_RUN)
0817             rts51x_set_stat(chip, RTS51X_STAT_RUN);
0818         chip->proto_handler_backup(srb, us);
0819     } else {
0820         if (rts51x_get_stat(chip) == RTS51X_STAT_SS) {
0821             usb_stor_dbg(us, "NOT working scsi\n");
0822             if ((srb->cmnd[0] == TEST_UNIT_READY) &&
0823                 (chip->pwr_state == US_SUSPEND)) {
0824                 if (TST_LUN_READY(chip, srb->device->lun)) {
0825                     srb->result = SAM_STAT_GOOD;
0826                 } else {
0827                     srb->result = SAM_STAT_CHECK_CONDITION;
0828                     memcpy(srb->sense_buffer,
0829                            media_not_present,
0830                            US_SENSE_SIZE);
0831                 }
0832                 usb_stor_dbg(us, "TEST_UNIT_READY\n");
0833                 goto out;
0834             }
0835             if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
0836                 int prevent = srb->cmnd[4] & 0x1;
0837                 if (prevent) {
0838                     srb->result = SAM_STAT_CHECK_CONDITION;
0839                     memcpy(srb->sense_buffer,
0840                            invalid_cmd_field,
0841                            US_SENSE_SIZE);
0842                 } else {
0843                     srb->result = SAM_STAT_GOOD;
0844                 }
0845                 usb_stor_dbg(us, "ALLOW_MEDIUM_REMOVAL\n");
0846                 goto out;
0847             }
0848         } else {
0849             usb_stor_dbg(us, "NOT working scsi, not SS\n");
0850             chip->proto_handler_backup(srb, us);
0851             /* Check whether card is plugged in */
0852             if (srb->cmnd[0] == TEST_UNIT_READY) {
0853                 if (srb->result == SAM_STAT_GOOD) {
0854                     SET_LUN_READY(chip, srb->device->lun);
0855                     if (card_first_show) {
0856                         card_first_show = 0;
0857                         fw5895_set_mmc_wp(us);
0858                     }
0859                 } else {
0860                     CLR_LUN_READY(chip, srb->device->lun);
0861                     card_first_show = 1;
0862                 }
0863             }
0864             if (rts51x_get_stat(chip) != RTS51X_STAT_IDLE)
0865                 rts51x_set_stat(chip, RTS51X_STAT_IDLE);
0866         }
0867     }
0868 out:
0869     usb_stor_dbg(us, "state:%d\n", rts51x_get_stat(chip));
0870     if (rts51x_get_stat(chip) == RTS51X_STAT_RUN)
0871         rts51x_modi_suspend_timer(chip);
0872 }
0873 
0874 static int realtek_cr_autosuspend_setup(struct us_data *us)
0875 {
0876     struct rts51x_chip *chip;
0877     struct rts51x_status *status = NULL;
0878     u8 buf[16];
0879     int retval;
0880 
0881     chip = (struct rts51x_chip *)us->extra;
0882     chip->support_auto_delink = 0;
0883     chip->pwr_state = US_RESUME;
0884     chip->lun_ready = 0;
0885     rts51x_set_stat(chip, RTS51X_STAT_INIT);
0886 
0887     retval = rts51x_read_status(us, 0, buf, 16, &(chip->status_len));
0888     if (retval != STATUS_SUCCESS) {
0889         usb_stor_dbg(us, "Read status fail\n");
0890         return -EIO;
0891     }
0892     status = chip->status;
0893     status->vid = ((u16) buf[0] << 8) | buf[1];
0894     status->pid = ((u16) buf[2] << 8) | buf[3];
0895     status->cur_lun = buf[4];
0896     status->card_type = buf[5];
0897     status->total_lun = buf[6];
0898     status->fw_ver = ((u16) buf[7] << 8) | buf[8];
0899     status->phy_exist = buf[9];
0900     status->multi_flag = buf[10];
0901     status->multi_card = buf[11];
0902     status->log_exist = buf[12];
0903     if (chip->status_len == 16) {
0904         status->detailed_type.detailed_type1 = buf[13];
0905         status->function[0] = buf[14];
0906         status->function[1] = buf[15];
0907     }
0908 
0909     /* back up the proto_handler in us->extra */
0910     chip = (struct rts51x_chip *)(us->extra);
0911     chip->proto_handler_backup = us->proto_handler;
0912     /* Set the autosuspend_delay to 0 */
0913     pm_runtime_set_autosuspend_delay(&us->pusb_dev->dev, 0);
0914     /* override us->proto_handler setted in get_protocol() */
0915     us->proto_handler = rts51x_invoke_transport;
0916 
0917     chip->timer_expires = 0;
0918     timer_setup(&chip->rts51x_suspend_timer, rts51x_suspend_timer_fn, 0);
0919     fw5895_init(us);
0920 
0921     /* enable autosuspend function of the usb device */
0922     usb_enable_autosuspend(us->pusb_dev);
0923 
0924     return 0;
0925 }
0926 #endif
0927 
0928 static void realtek_cr_destructor(void *extra)
0929 {
0930     struct rts51x_chip *chip = extra;
0931 
0932     if (!chip)
0933         return;
0934 
0935 #ifdef CONFIG_REALTEK_AUTOPM
0936     if (ss_en) {
0937         del_timer(&chip->rts51x_suspend_timer);
0938         chip->timer_expires = 0;
0939     }
0940 #endif
0941     kfree(chip->status);
0942 }
0943 
0944 #ifdef CONFIG_PM
0945 static int realtek_cr_suspend(struct usb_interface *iface, pm_message_t message)
0946 {
0947     struct us_data *us = usb_get_intfdata(iface);
0948 
0949     /* wait until no command is running */
0950     mutex_lock(&us->dev_mutex);
0951 
0952     config_autodelink_before_power_down(us);
0953 
0954     mutex_unlock(&us->dev_mutex);
0955 
0956     return 0;
0957 }
0958 
0959 static int realtek_cr_resume(struct usb_interface *iface)
0960 {
0961     struct us_data *us = usb_get_intfdata(iface);
0962 
0963     fw5895_init(us);
0964     config_autodelink_after_power_on(us);
0965 
0966     return 0;
0967 }
0968 #else
0969 #define realtek_cr_suspend  NULL
0970 #define realtek_cr_resume   NULL
0971 #endif
0972 
0973 static int init_realtek_cr(struct us_data *us)
0974 {
0975     struct rts51x_chip *chip;
0976     int size, i, retval;
0977 
0978     chip = kzalloc(sizeof(struct rts51x_chip), GFP_KERNEL);
0979     if (!chip)
0980         return -ENOMEM;
0981 
0982     us->extra = chip;
0983     us->extra_destructor = realtek_cr_destructor;
0984     us->max_lun = chip->max_lun = rts51x_get_max_lun(us);
0985     chip->us = us;
0986 
0987     usb_stor_dbg(us, "chip->max_lun = %d\n", chip->max_lun);
0988 
0989     size = (chip->max_lun + 1) * sizeof(struct rts51x_status);
0990     chip->status = kzalloc(size, GFP_KERNEL);
0991     if (!chip->status)
0992         goto INIT_FAIL;
0993 
0994     for (i = 0; i <= (int)(chip->max_lun); i++) {
0995         retval = rts51x_check_status(us, (u8) i);
0996         if (retval < 0)
0997             goto INIT_FAIL;
0998     }
0999 
1000     if (CHECK_PID(chip, 0x0138) || CHECK_PID(chip, 0x0158) ||
1001         CHECK_PID(chip, 0x0159)) {
1002         if (CHECK_FW_VER(chip, 0x5888) || CHECK_FW_VER(chip, 0x5889) ||
1003                 CHECK_FW_VER(chip, 0x5901))
1004             SET_AUTO_DELINK(chip);
1005         if (STATUS_LEN(chip) == 16) {
1006             if (SUPPORT_AUTO_DELINK(chip))
1007                 SET_AUTO_DELINK(chip);
1008         }
1009     }
1010 #ifdef CONFIG_REALTEK_AUTOPM
1011     if (ss_en)
1012         realtek_cr_autosuspend_setup(us);
1013 #endif
1014 
1015     usb_stor_dbg(us, "chip->flag = 0x%x\n", chip->flag);
1016 
1017     (void)config_autodelink_after_power_on(us);
1018 
1019     return 0;
1020 
1021 INIT_FAIL:
1022     if (us->extra) {
1023         kfree(chip->status);
1024         kfree(us->extra);
1025         us->extra = NULL;
1026     }
1027 
1028     return -EIO;
1029 }
1030 
1031 static struct scsi_host_template realtek_cr_host_template;
1032 
1033 static int realtek_cr_probe(struct usb_interface *intf,
1034                 const struct usb_device_id *id)
1035 {
1036     struct us_data *us;
1037     int result;
1038 
1039     dev_dbg(&intf->dev, "Probe Realtek Card Reader!\n");
1040 
1041     result = usb_stor_probe1(&us, intf, id,
1042                  (id - realtek_cr_ids) +
1043                  realtek_cr_unusual_dev_list,
1044                  &realtek_cr_host_template);
1045     if (result)
1046         return result;
1047 
1048     result = usb_stor_probe2(us);
1049 
1050     return result;
1051 }
1052 
1053 static struct usb_driver realtek_cr_driver = {
1054     .name = DRV_NAME,
1055     .probe = realtek_cr_probe,
1056     .disconnect = usb_stor_disconnect,
1057     /* .suspend =      usb_stor_suspend, */
1058     /* .resume =       usb_stor_resume, */
1059     .reset_resume = usb_stor_reset_resume,
1060     .suspend = realtek_cr_suspend,
1061     .resume = realtek_cr_resume,
1062     .pre_reset = usb_stor_pre_reset,
1063     .post_reset = usb_stor_post_reset,
1064     .id_table = realtek_cr_ids,
1065     .soft_unbind = 1,
1066     .supports_autosuspend = 1,
1067     .no_dynamic_id = 1,
1068 };
1069 
1070 module_usb_stor_driver(realtek_cr_driver, realtek_cr_host_template, DRV_NAME);