23 #include <linux/module.h>
26 #include <linux/sched.h>
27 #include <linux/kernel.h>
29 #include <scsi/scsi.h>
32 #include <linux/cdrom.h>
35 #include <linux/slab.h>
48 static int auto_delink_en = 1;
52 #ifdef CONFIG_REALTEK_AUTOPM
57 static int ss_delay = 50;
60 "seconds to delay before entering selective suspend");
69 #define POLLING_INTERVAL 50
71 #define rts51x_set_stat(chip, stat) \
72 ((chip)->state = (enum RTS51X_STAT)(stat))
73 #define rts51x_get_stat(chip) ((chip)->state)
75 #define SET_LUN_READY(chip, lun) ((chip)->lun_ready |= ((u8)1 << (lun)))
76 #define CLR_LUN_READY(chip, lun) ((chip)->lun_ready &= ~((u8)1 << (lun)))
77 #define TST_LUN_READY(chip, lun) ((chip)->lun_ready & ((u8)1 << (lun)))
108 #ifdef CONFIG_REALTEK_AUTOPM
114 enum RTS51X_STAT
state;
115 int support_auto_delink;
122 #define FLIDX_AUTO_DELINK 0x01
124 #define SCSI_LUN(srb) ((srb)->device->lun)
127 #define SET_BIT(data, idx) ((data) |= 1 << (idx))
128 #define CLR_BIT(data, idx) ((data) &= ~(1 << (idx)))
129 #define CHK_BIT(data, idx) ((data) & (1 << (idx)))
131 #define SET_AUTO_DELINK(chip) ((chip)->flag |= FLIDX_AUTO_DELINK)
132 #define CLR_AUTO_DELINK(chip) ((chip)->flag &= ~FLIDX_AUTO_DELINK)
133 #define CHK_AUTO_DELINK(chip) ((chip)->flag & FLIDX_AUTO_DELINK)
135 #define RTS51X_GET_VID(chip) ((chip)->vendor_id)
136 #define RTS51X_GET_PID(chip) ((chip)->product_id)
138 #define VENDOR_ID(chip) ((chip)->status[0].vid)
139 #define PRODUCT_ID(chip) ((chip)->status[0].pid)
140 #define FW_VERSION(chip) ((chip)->status[0].fw_ver)
141 #define STATUS_LEN(chip) ((chip)->status_len)
143 #define STATUS_SUCCESS 0
144 #define STATUS_FAIL 1
147 #define SUPPORT_DETAILED_TYPE1(chip) \
148 CHK_BIT((chip)->status[0].function[0], 1)
149 #define SUPPORT_OT(chip) \
150 CHK_BIT((chip)->status[0].function[0], 2)
151 #define SUPPORT_OC(chip) \
152 CHK_BIT((chip)->status[0].function[0], 3)
153 #define SUPPORT_AUTO_DELINK(chip) \
154 CHK_BIT((chip)->status[0].function[0], 4)
155 #define SUPPORT_SDIO(chip) \
156 CHK_BIT((chip)->status[0].function[1], 0)
157 #define SUPPORT_DETAILED_TYPE2(chip) \
158 CHK_BIT((chip)->status[0].function[1], 1)
160 #define CHECK_PID(chip, pid) (RTS51X_GET_PID(chip) == (pid))
161 #define CHECK_FW_VER(chip, fw_ver) (FW_VERSION(chip) == (fw_ver))
162 #define CHECK_ID(chip, pid, fw_ver) \
163 (CHECK_PID((chip), (pid)) && CHECK_FW_VER((chip), (fw_ver)))
165 static int init_realtek_cr(
struct us_data *us);
170 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
171 vendorName, productName, useProtocol, useTransport, \
172 initFunction, flags) \
174 USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
175 .driver_info = (flags) \
190 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
191 vendor_name, product_name, use_protocol, use_transport, \
192 init_function, Flags) \
194 .vendorName = vendor_name, \
195 .productName = product_name, \
196 .useProtocol = use_protocol, \
197 .useTransport = use_transport, \
198 .initFunction = init_function, \
208 static int rts51x_bulk_transport(
struct us_data *us,
u8 lun,
215 unsigned int residue;
240 if (buf && buf_len) {
257 US_DEBUGP(
"Signature mismatch: got %08X, expecting %08X\n",
262 residue = bcs->Residue;
263 if (bcs->Tag != us->
tag)
269 residue = residue < buf_len ? residue : buf_len;
272 *act_len = buf_len - residue;
275 switch (bcs->Status) {
295 static int rts51x_bulk_transport_special(
struct us_data *us,
u8 lun,
296 u8 *cmd,
int cmd_len,
u8 *buf,
int buf_len,
326 if (buf && buf_len) {
342 static int rts51x_get_max_lun(
struct us_data *us)
354 US_DEBUGP(
"GetMaxLUN command result is %d, data is %d\n",
355 result, us->
iobuf[0]);
374 US_DEBUGP(
"%s, addr = 0x%x, len = %d\n", __func__, addr, len);
378 cmnd[2] = (
u8) (addr >> 8);
380 cmnd[4] = (
u8) (len >> 8);
383 retval = rts51x_bulk_transport(us, 0, cmnd, 12,
395 static int rts51x_write_mem(
struct us_data *us,
u16 addr,
u8 *data,
u16 len)
405 US_DEBUGP(
"%s, addr = 0x%x, len = %d\n", __func__, addr, len);
409 cmnd[2] = (
u8) (addr >> 8);
411 cmnd[4] = (
u8) (len >> 8);
414 retval = rts51x_bulk_transport(us, 0, cmnd, 12,
434 US_DEBUGP(
"%s, lun = %d\n", __func__, lun);
439 retval = rts51x_bulk_transport(us, lun, cmnd, 12,
451 static int rts51x_check_status(
struct us_data *us,
u8 lun)
468 chip->
status[
lun].fw_ver = ((
u16) buf[7] << 8) | buf[8];
474 chip->
status[
lun].detailed_type.detailed_type1 = buf[13];
482 static int enable_oscillator(
struct us_data *us)
487 retval = rts51x_read_mem(us, 0xFE77, &value, 1);
492 retval = rts51x_write_mem(us, 0xFE77, &value, 1);
496 retval = rts51x_read_mem(us, 0xFE77, &value, 1);
506 static int __do_config_autodelink(
struct us_data *us,
u8 *data,
u16 len)
522 cmnd[4] = (
u8)(len >> 8);
525 retval = rts51x_bulk_transport_special(us, 0, cmnd, 12, buf, len,
DMA_TO_DEVICE,
NULL);
539 retval = rts51x_read_mem(us, 0xFE47, &value, 1);
552 US_DEBUGP(
"In %s,set 0xfe47 to 0x%x\n", __func__, value);
555 retval = __do_config_autodelink(us, &value, 1);
562 static int config_autodelink_after_power_on(
struct us_data *us)
573 retval = rts51x_read_mem(us, 0xFE47, &value, 1);
577 if (auto_delink_en) {
588 retval = __do_config_autodelink(us, &value, 1);
592 retval = enable_oscillator(us);
594 (
void)do_config_autodelink(us, 1, 0);
603 if (
CHECK_ID(chip, 0x0159, 0x5889) ||
610 retval = __do_config_autodelink(us, &value, 1);
614 if (
CHECK_ID(chip, 0x0159, 0x5888)) {
616 retval = rts51x_write_mem(us, 0xFE79, &value, 1);
621 retval = rts51x_write_mem(us, 0x48, &value, 1);
632 static int config_autodelink_before_power_down(
struct us_data *us)
643 if (auto_delink_en) {
644 retval = rts51x_read_mem(us, 0xFE77, &value, 1);
649 retval = rts51x_write_mem(us, 0xFE77, &value, 1);
653 if (
CHECK_ID(chip, 0x0159, 0x5888)) {
655 retval = rts51x_write_mem(us, 0x48, &value, 1);
660 retval = rts51x_read_mem(us, 0xFE47, &value, 1);
667 retval = rts51x_write_mem(us, 0xFE77, &value, 1);
671 if (
CHECK_ID(chip, 0x0159, 0x5889) ||
674 retval = rts51x_read_mem(us, 0xFE47, &value, 1);
678 if (
CHECK_ID(chip, 0x0159, 0x5889) ||
688 retval = __do_config_autodelink(us, &value, 1);
693 if (
CHECK_ID(chip, 0x0159, 0x5888)) {
695 retval = rts51x_write_mem(us, 0x48, &value, 1);
706 static void fw5895_init(
struct us_data *us)
715 US_DEBUGP(
"Not the specified device, return immediately!\n");
717 retval = rts51x_read_mem(us, 0xFD6F, &val, 1);
720 retval = rts51x_write_mem(us, 0xFD70, &val, 1);
724 US_DEBUGP(
"Read memory fail, OR (val & 0x1F) != 0\n");
731 #ifdef CONFIG_REALTEK_AUTOPM
732 static void fw5895_set_mmc_wp(
struct us_data *us)
741 US_DEBUGP(
"Not the specified device, return immediately!\n");
743 retval = rts51x_read_mem(us, 0xFD6F, buf, 1);
746 retval = rts51x_read_mem(us, 0xD04E, buf, 1);
749 retval = rts51x_write_mem(us, 0xFD70, buf, 1);
756 US_DEBUGP(
"Read memory fail, OR (buf[0]&0x24)!=0x24\n");
763 static void rts51x_modi_suspend_timer(
struct rts51x_chip *chip)
765 US_DEBUGP(
"%s: <---, state:%d\n", __func__, rts51x_get_stat(chip));
768 mod_timer(&chip->rts51x_suspend_timer, chip->timer_expires);
773 static void rts51x_suspend_timer_fn(
unsigned long data)
780 switch (rts51x_get_stat(chip)) {
781 case RTS51X_STAT_INIT:
782 case RTS51X_STAT_RUN:
783 rts51x_modi_suspend_timer(chip);
785 case RTS51X_STAT_IDLE:
787 US_DEBUGP(
"%s: RTS51X_STAT_SS, intf->pm_usage_cnt:%d,"
788 "power.usage:%d\n", __func__,
793 US_DEBUGP(
"%s: Ready to enter SS state.\n",
795 rts51x_set_stat(chip, RTS51X_STAT_SS);
797 pm_suspend_ignore_children(&us->
pusb_intf->dev,
true);
798 usb_autopm_put_interface_async(us->
pusb_intf);
800 "intf->pm_usage_cnt:%d, power.usage:%d\n",
808 US_DEBUGP(
"%s: Unknonwn state !!!\n", __func__);
828 static int card_first_show = 1;
830 10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0
833 10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0
839 if (working_scsi(srb)) {
840 US_DEBUGP(
"%s: working scsi, intf->pm_usage_cnt:%d,"
841 "power.usage:%d\n", __func__,
846 ret = usb_autopm_get_interface(us->
pusb_intf);
847 US_DEBUGP(
"%s: working scsi, ret=%d\n", __func__, ret);
849 if (rts51x_get_stat(chip) != RTS51X_STAT_RUN)
850 rts51x_set_stat(chip, RTS51X_STAT_RUN);
853 if (rts51x_get_stat(chip) == RTS51X_STAT_SS) {
854 US_DEBUGP(
"%s: NOT working scsi\n", __func__);
857 if (TST_LUN_READY(chip, srb->
device->lun)) {
870 int prevent = srb->
cmnd[4] & 0x1;
879 US_DEBUGP(
"%s: ALLOW_MEDIUM_REMOVAL--->\n",
884 US_DEBUGP(
"%s: NOT working scsi, not SS\n", __func__);
889 SET_LUN_READY(chip, srb->
device->lun);
890 if (card_first_show) {
892 fw5895_set_mmc_wp(us);
895 CLR_LUN_READY(chip, srb->
device->lun);
899 if (rts51x_get_stat(chip) != RTS51X_STAT_IDLE)
900 rts51x_set_stat(chip, RTS51X_STAT_IDLE);
904 US_DEBUGP(
"%s: state:%d\n", __func__, rts51x_get_stat(chip));
905 if (rts51x_get_stat(chip) == RTS51X_STAT_RUN)
906 rts51x_modi_suspend_timer(chip);
911 static int realtek_cr_autosuspend_setup(
struct us_data *us)
919 chip->support_auto_delink = 0;
922 rts51x_set_stat(chip, RTS51X_STAT_INIT);
930 status->
vid = ((
u16) buf[0] << 8) | buf[1];
931 status->
pid = ((
u16) buf[2] << 8) | buf[3];
935 status->
fw_ver = ((
u16) buf[7] << 8) | buf[8];
954 chip->timer_expires = 0;
955 setup_timer(&chip->rts51x_suspend_timer, rts51x_suspend_timer_fn,
956 (
unsigned long)chip);
960 usb_enable_autosuspend(us->
pusb_dev);
966 static void realtek_cr_destructor(
void *
extra)
974 #ifdef CONFIG_REALTEK_AUTOPM
977 chip->timer_expires = 0;
986 struct us_data *us = usb_get_intfdata(iface);
993 config_autodelink_before_power_down(us);
1004 struct us_data *us = usb_get_intfdata(iface);
1009 config_autodelink_after_power_on(us);
1016 #define realtek_cr_suspend NULL
1017 #define realtek_cr_resume NULL
1020 static int init_realtek_cr(
struct us_data *us)
1040 for (i = 0; i <= (
int)(chip->
max_lun); i++) {
1041 retval = rts51x_check_status(us, (
u8) i);
1053 #ifdef CONFIG_REALTEK_AUTOPM
1056 realtek_cr_autosuspend_setup(us);
1062 (
void)config_autodelink_after_power_on(us);
1082 US_DEBUGP(
"Probe Realtek Card Reader!\n");
1085 (
id - realtek_cr_ids) +
1086 realtek_cr_unusual_dev_list);
1095 static struct usb_driver realtek_cr_driver = {
1096 .name =
"ums-realtek",
1097 .probe = realtek_cr_probe,
1106 .id_table = realtek_cr_ids,
1108 .supports_autosuspend = 1,