realtek_cr.c 26.6 KB
Newer Older
W
wwang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* Driver for Realtek RTS51xx USB card reader
 *
 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author:
 *   wwang (wei_wang@realsil.com.cn)
 *   No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
 */

#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/kernel.h>

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <linux/cdrom.h>

#include <linux/usb.h>
#include <linux/slab.h>
#include <linux/usb_usual.h>

#include "usb.h"
#include "transport.h"
#include "protocol.h"
#include "debug.h"

MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.03");

static int auto_delink_en = 1;
module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(auto_delink_en, "enable auto delink");

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#ifdef CONFIG_REALTEK_AUTOPM
static int ss_en = 1;
module_param(ss_en, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(ss_en, "enable selective suspend");

static int ss_delay = 50;
module_param(ss_delay, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(ss_delay,
		 "seconds to delay before entering selective suspend");

enum RTS51X_STAT {
	RTS51X_STAT_INIT,
	RTS51X_STAT_IDLE,
	RTS51X_STAT_RUN,
	RTS51X_STAT_SS
};

#define POLLING_INTERVAL	50

#define rts51x_set_stat(chip, stat)	\
	((chip)->state = (enum RTS51X_STAT)(stat))
#define rts51x_get_stat(chip)		((chip)->state)

#define SET_LUN_READY(chip, lun)	((chip)->lun_ready |= ((u8)1 << (lun)))
#define CLR_LUN_READY(chip, lun)	((chip)->lun_ready &= ~((u8)1 << (lun)))
#define TST_LUN_READY(chip, lun)	((chip)->lun_ready & ((u8)1 << (lun)))

#endif

W
wwang 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
struct rts51x_status {
	u16 vid;
	u16 pid;
	u8 cur_lun;
	u8 card_type;
	u8 total_lun;
	u16 fw_ver;
	u8 phy_exist;
	u8 multi_flag;
	u8 multi_card;
	u8 log_exist;
	union {
		u8 detailed_type1;
		u8 detailed_type2;
	} detailed_type;
	u8 function[2];
};

struct rts51x_chip {
100 101 102
	u16 vendor_id;
	u16 product_id;
	char max_lun;
W
wwang 已提交
103

104 105
	struct rts51x_status *status;
	int status_len;
W
wwang 已提交
106

107 108
	u32 flag;
	struct us_data *us;
109 110

#ifdef CONFIG_REALTEK_AUTOPM
111 112 113 114 115 116 117 118 119
	struct timer_list rts51x_suspend_timer;
	unsigned long timer_expires;
	int pwr_state;
	u8 lun_ready;
	enum RTS51X_STAT state;
	int support_auto_delink;
#endif
	/* used to back up the protocal choosen in probe1 phase */
	proto_cmnd proto_handler_backup;
W
wwang 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
};

/* flag definition */
#define FLIDX_AUTO_DELINK		0x01

#define SCSI_LUN(srb)			((srb)->device->lun)

/* Bit Operation */
#define SET_BIT(data, idx)		((data) |= 1 << (idx))
#define CLR_BIT(data, idx)		((data) &= ~(1 << (idx)))
#define CHK_BIT(data, idx)		((data) & (1 << (idx)))

#define SET_AUTO_DELINK(chip)		((chip)->flag |= FLIDX_AUTO_DELINK)
#define CLR_AUTO_DELINK(chip)		((chip)->flag &= ~FLIDX_AUTO_DELINK)
#define CHK_AUTO_DELINK(chip)		((chip)->flag & FLIDX_AUTO_DELINK)

#define RTS51X_GET_VID(chip)		((chip)->vendor_id)
#define RTS51X_GET_PID(chip)		((chip)->product_id)

139 140
#define VENDOR_ID(chip)			((chip)->status[0].vid)
#define PRODUCT_ID(chip)		((chip)->status[0].pid)
W
wwang 已提交
141 142 143
#define FW_VERSION(chip)		((chip)->status[0].fw_ver)
#define STATUS_LEN(chip)		((chip)->status_len)

144 145 146
#define STATUS_SUCCESS		0
#define STATUS_FAIL		1

W
wwang 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/* Check card reader function */
#define SUPPORT_DETAILED_TYPE1(chip)	\
		CHK_BIT((chip)->status[0].function[0], 1)
#define SUPPORT_OT(chip)		\
		CHK_BIT((chip)->status[0].function[0], 2)
#define SUPPORT_OC(chip)		\
		CHK_BIT((chip)->status[0].function[0], 3)
#define SUPPORT_AUTO_DELINK(chip)	\
		CHK_BIT((chip)->status[0].function[0], 4)
#define SUPPORT_SDIO(chip)		\
		CHK_BIT((chip)->status[0].function[1], 0)
#define SUPPORT_DETAILED_TYPE2(chip)	\
		CHK_BIT((chip)->status[0].function[1], 1)

#define CHECK_PID(chip, pid)		(RTS51X_GET_PID(chip) == (pid))
#define CHECK_FW_VER(chip, fw_ver)	(FW_VERSION(chip) == (fw_ver))
#define CHECK_ID(chip, pid, fw_ver)	\
		(CHECK_PID((chip), (pid)) && CHECK_FW_VER((chip), (fw_ver)))

static int init_realtek_cr(struct us_data *us);

/*
 * The table of devices
 */
#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
		    vendorName, productName, useProtocol, useTransport, \
		    initFunction, flags) \
{\
	USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
S
Sebastian Andrzej Siewior 已提交
176
	.driver_info = (flags) \
W
wwang 已提交
177 178
}

179
static const struct usb_device_id realtek_cr_ids[] = {
W
wwang 已提交
180
#	include "unusual_realtek.h"
181
	{}			/* Terminating entry */
W
wwang 已提交
182
};
183

W
wwang 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
MODULE_DEVICE_TABLE(usb, realtek_cr_ids);

#undef UNUSUAL_DEV

/*
 * The flags table
 */
#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
		    vendor_name, product_name, use_protocol, use_transport, \
		    init_function, Flags) \
{ \
	.vendorName = vendor_name,	\
	.productName = product_name,	\
	.useProtocol = use_protocol,	\
	.useTransport = use_transport,	\
	.initFunction = init_function,	\
}

static struct us_unusual_dev realtek_cr_unusual_dev_list[] = {
#	include "unusual_realtek.h"
204
	{}			/* Terminating entry */
W
wwang 已提交
205 206 207 208 209 210 211 212
};

#undef UNUSUAL_DEV

static int rts51x_bulk_transport(struct us_data *us, u8 lun,
				 u8 *cmd, int cmd_len, u8 *buf, int buf_len,
				 enum dma_data_direction dir, int *act_len)
{
213 214
	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *)us->iobuf;
	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *)us->iobuf;
W
wwang 已提交
215 216 217 218 219 220 221 222
	int result;
	unsigned int residue;
	unsigned int cswlen;
	unsigned int cbwlen = US_BULK_CB_WRAP_LEN;

	/* set up the command wrapper */
	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
	bcb->DataTransferLength = cpu_to_le32(buf_len);
223
	bcb->Flags = (dir == DMA_FROM_DEVICE) ? US_BULK_FLAG_IN : 0;
W
wwang 已提交
224 225 226 227 228 229 230 231 232 233
	bcb->Tag = ++us->tag;
	bcb->Lun = lun;
	bcb->Length = cmd_len;

	/* copy the command payload */
	memset(bcb->CDB, 0, sizeof(bcb->CDB));
	memcpy(bcb->CDB, cmd, bcb->Length);

	/* send it to out endpoint */
	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
234
					    bcb, cbwlen, NULL);
W
wwang 已提交
235 236 237 238 239 240 241 242
	if (result != USB_STOR_XFER_GOOD)
		return USB_STOR_TRANSPORT_ERROR;

	/* DATA STAGE */
	/* send/receive data payload, if there is any */

	if (buf && buf_len) {
		unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
243
		    us->recv_bulk_pipe : us->send_bulk_pipe;
W
wwang 已提交
244
		result = usb_stor_bulk_transfer_buf(us, pipe,
245
						    buf, buf_len, NULL);
W
wwang 已提交
246 247 248 249 250 251
		if (result == USB_STOR_XFER_ERROR)
			return USB_STOR_TRANSPORT_ERROR;
	}

	/* get CSW for device status */
	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
252
					    bcs, US_BULK_CS_WRAP_LEN, &cswlen);
W
wwang 已提交
253 254 255 256 257
	if (result != USB_STOR_XFER_GOOD)
		return USB_STOR_TRANSPORT_ERROR;

	/* check bulk status */
	if (bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN)) {
258 259
		usb_stor_dbg(us, "Signature mismatch: got %08X, expecting %08X\n",
			     le32_to_cpu(bcs->Signature), US_BULK_CS_SIGN);
W
wwang 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
		return USB_STOR_TRANSPORT_ERROR;
	}

	residue = bcs->Residue;
	if (bcs->Tag != us->tag)
		return USB_STOR_TRANSPORT_ERROR;

	/* try to compute the actual residue, based on how much data
	 * was really transferred and what the device tells us */
	if (residue)
		residue = residue < buf_len ? residue : buf_len;

	if (act_len)
		*act_len = buf_len - residue;

	/* based on the status code, we report good or bad */
	switch (bcs->Status) {
	case US_BULK_STAT_OK:
		/* command good -- note that data could be short */
		return USB_STOR_TRANSPORT_GOOD;

	case US_BULK_STAT_FAIL:
		/* command failed */
		return USB_STOR_TRANSPORT_FAILED;

	case US_BULK_STAT_PHASE:
		/* phase error -- note that a transport reset will be
287 288
		 * invoked by the invoke_transport() function
		 */
W
wwang 已提交
289 290 291 292 293 294 295
		return USB_STOR_TRANSPORT_ERROR;
	}

	/* we should never get here, but if we do, we're in trouble */
	return USB_STOR_TRANSPORT_ERROR;
}

296 297 298 299 300 301 302 303 304 305 306 307 308
static int rts51x_bulk_transport_special(struct us_data *us, u8 lun,
				 u8 *cmd, int cmd_len, u8 *buf, int buf_len,
				 enum dma_data_direction dir, int *act_len)
{
	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
	int result;
	unsigned int cswlen;
	unsigned int cbwlen = US_BULK_CB_WRAP_LEN;

	/* set up the command wrapper */
	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
	bcb->DataTransferLength = cpu_to_le32(buf_len);
309
	bcb->Flags = (dir == DMA_FROM_DEVICE) ? US_BULK_FLAG_IN : 0;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	bcb->Tag = ++us->tag;
	bcb->Lun = lun;
	bcb->Length = cmd_len;

	/* copy the command payload */
	memset(bcb->CDB, 0, sizeof(bcb->CDB));
	memcpy(bcb->CDB, cmd, bcb->Length);

	/* send it to out endpoint */
	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
				bcb, cbwlen, NULL);
	if (result != USB_STOR_XFER_GOOD)
		return USB_STOR_TRANSPORT_ERROR;

	/* DATA STAGE */
	/* send/receive data payload, if there is any */

	if (buf && buf_len) {
		unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
				us->recv_bulk_pipe : us->send_bulk_pipe;
		result = usb_stor_bulk_transfer_buf(us, pipe,
				buf, buf_len, NULL);
		if (result == USB_STOR_XFER_ERROR)
			return USB_STOR_TRANSPORT_ERROR;
	}

	/* get CSW for device status */
	result = usb_bulk_msg(us->pusb_dev, us->recv_bulk_pipe, bcs,
			US_BULK_CS_WRAP_LEN, &cswlen, 250);
	return result;
}

W
wwang 已提交
342 343 344 345 346 347 348 349
/* Determine what the maximum LUN supported is */
static int rts51x_get_max_lun(struct us_data *us)
{
	int result;

	/* issue the command */
	us->iobuf[0] = 0;
	result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
350 351 352 353
				      US_BULK_GET_MAX_LUN,
				      USB_DIR_IN | USB_TYPE_CLASS |
				      USB_RECIP_INTERFACE,
				      0, us->ifnum, us->iobuf, 1, 10 * HZ);
W
wwang 已提交
354

355 356
	usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
		     result, us->iobuf[0]);
W
wwang 已提交
357 358 359 360 361 362 363 364 365 366 367

	/* if we have a successful request, return the result */
	if (result > 0)
		return us->iobuf[0];

	return 0;
}

static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
{
	int retval;
368
	u8 cmnd[12] = { 0 };
369 370 371 372 373
	u8 *buf;

	buf = kmalloc(len, GFP_NOIO);
	if (buf == NULL)
		return USB_STOR_TRANSPORT_ERROR;
W
wwang 已提交
374

375
	usb_stor_dbg(us, "addr = 0x%x, len = %d\n", addr, len);
W
wwang 已提交
376 377 378

	cmnd[0] = 0xF0;
	cmnd[1] = 0x0D;
379 380 381 382
	cmnd[2] = (u8) (addr >> 8);
	cmnd[3] = (u8) addr;
	cmnd[4] = (u8) (len >> 8);
	cmnd[5] = (u8) len;
W
wwang 已提交
383 384

	retval = rts51x_bulk_transport(us, 0, cmnd, 12,
385 386 387
				       buf, len, DMA_FROM_DEVICE, NULL);
	if (retval != USB_STOR_TRANSPORT_GOOD) {
		kfree(buf);
W
wwang 已提交
388
		return -EIO;
389
	}
W
wwang 已提交
390

391 392
	memcpy(data, buf, len);
	kfree(buf);
W
wwang 已提交
393 394 395 396 397 398
	return 0;
}

static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
{
	int retval;
399
	u8 cmnd[12] = { 0 };
400 401
	u8 *buf;

402
	buf = kmemdup(data, len, GFP_NOIO);
403 404
	if (buf == NULL)
		return USB_STOR_TRANSPORT_ERROR;
W
wwang 已提交
405

406
	usb_stor_dbg(us, "addr = 0x%x, len = %d\n", addr, len);
W
wwang 已提交
407 408 409

	cmnd[0] = 0xF0;
	cmnd[1] = 0x0E;
410 411 412 413
	cmnd[2] = (u8) (addr >> 8);
	cmnd[3] = (u8) addr;
	cmnd[4] = (u8) (len >> 8);
	cmnd[5] = (u8) len;
W
wwang 已提交
414 415

	retval = rts51x_bulk_transport(us, 0, cmnd, 12,
416 417
				       buf, len, DMA_TO_DEVICE, NULL);
	kfree(buf);
W
wwang 已提交
418 419 420 421 422 423 424 425 426 427
	if (retval != USB_STOR_TRANSPORT_GOOD)
		return -EIO;

	return 0;
}

static int rts51x_read_status(struct us_data *us,
			      u8 lun, u8 *status, int len, int *actlen)
{
	int retval;
428
	u8 cmnd[12] = { 0 };
429 430 431 432 433
	u8 *buf;

	buf = kmalloc(len, GFP_NOIO);
	if (buf == NULL)
		return USB_STOR_TRANSPORT_ERROR;
W
wwang 已提交
434

435
	usb_stor_dbg(us, "lun = %d\n", lun);
W
wwang 已提交
436 437 438 439 440

	cmnd[0] = 0xF0;
	cmnd[1] = 0x09;

	retval = rts51x_bulk_transport(us, lun, cmnd, 12,
441 442 443
				       buf, len, DMA_FROM_DEVICE, actlen);
	if (retval != USB_STOR_TRANSPORT_GOOD) {
		kfree(buf);
W
wwang 已提交
444
		return -EIO;
445
	}
W
wwang 已提交
446

447 448
	memcpy(status, buf, len);
	kfree(buf);
W
wwang 已提交
449 450 451 452 453 454 455 456 457 458
	return 0;
}

static int rts51x_check_status(struct us_data *us, u8 lun)
{
	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
	int retval;
	u8 buf[16];

	retval = rts51x_read_status(us, lun, buf, 16, &(chip->status_len));
J
Jingoo Han 已提交
459
	if (retval != STATUS_SUCCESS)
W
wwang 已提交
460 461
		return -EIO;

462
	usb_stor_dbg(us, "chip->status_len = %d\n", chip->status_len);
W
wwang 已提交
463

464 465
	chip->status[lun].vid = ((u16) buf[0] << 8) | buf[1];
	chip->status[lun].pid = ((u16) buf[2] << 8) | buf[3];
W
wwang 已提交
466 467 468
	chip->status[lun].cur_lun = buf[4];
	chip->status[lun].card_type = buf[5];
	chip->status[lun].total_lun = buf[6];
469
	chip->status[lun].fw_ver = ((u16) buf[7] << 8) | buf[8];
W
wwang 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
	chip->status[lun].phy_exist = buf[9];
	chip->status[lun].multi_flag = buf[10];
	chip->status[lun].multi_card = buf[11];
	chip->status[lun].log_exist = buf[12];
	if (chip->status_len == 16) {
		chip->status[lun].detailed_type.detailed_type1 = buf[13];
		chip->status[lun].function[0] = buf[14];
		chip->status[lun].function[1] = buf[15];
	}

	return 0;
}

static int enable_oscillator(struct us_data *us)
{
	int retval;
	u8 value;

	retval = rts51x_read_mem(us, 0xFE77, &value, 1);
	if (retval < 0)
		return -EIO;

	value |= 0x04;
	retval = rts51x_write_mem(us, 0xFE77, &value, 1);
	if (retval < 0)
		return -EIO;

	retval = rts51x_read_mem(us, 0xFE77, &value, 1);
	if (retval < 0)
		return -EIO;

	if (!(value & 0x04))
		return -EIO;

	return 0;
}

507 508 509 510
static int __do_config_autodelink(struct us_data *us, u8 *data, u16 len)
{
	int retval;
	u8 cmnd[12] = {0};
511
	u8 *buf;
512

513
	usb_stor_dbg(us, "addr = 0xfe47, len = %d\n", len);
514

515 516 517 518
	buf = kmemdup(data, len, GFP_NOIO);
	if (!buf)
		return USB_STOR_TRANSPORT_ERROR;

519 520
	cmnd[0] = 0xF0;
	cmnd[1] = 0x0E;
521 522
	cmnd[2] = 0xfe;
	cmnd[3] = 0x47;
523 524 525
	cmnd[4] = (u8)(len >> 8);
	cmnd[5] = (u8)len;

526 527
	retval = rts51x_bulk_transport_special(us, 0, cmnd, 12, buf, len, DMA_TO_DEVICE, NULL);
	kfree(buf);
528 529 530 531 532 533 534
	if (retval != USB_STOR_TRANSPORT_GOOD) {
		return -EIO;
	}

	return 0;
}

W
wwang 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
static int do_config_autodelink(struct us_data *us, int enable, int force)
{
	int retval;
	u8 value;

	retval = rts51x_read_mem(us, 0xFE47, &value, 1);
	if (retval < 0)
		return -EIO;

	if (enable) {
		if (force)
			value |= 0x03;
		else
			value |= 0x01;
	} else {
		value &= ~0x03;
	}

553
	usb_stor_dbg(us, "set 0xfe47 to 0x%x\n", value);
W
wwang 已提交
554

555 556
	/* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
	retval = __do_config_autodelink(us, &value, 1);
W
wwang 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	if (retval < 0)
		return -EIO;

	return 0;
}

static int config_autodelink_after_power_on(struct us_data *us)
{
	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
	int retval;
	u8 value;

	if (!CHK_AUTO_DELINK(chip))
		return 0;

	retval = rts51x_read_mem(us, 0xFE47, &value, 1);
	if (retval < 0)
		return -EIO;

	if (auto_delink_en) {
		CLR_BIT(value, 0);
		CLR_BIT(value, 1);
		SET_BIT(value, 2);

		if (CHECK_ID(chip, 0x0138, 0x3882))
			CLR_BIT(value, 2);

		SET_BIT(value, 7);

586 587
		/* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
		retval = __do_config_autodelink(us, &value, 1);
W
wwang 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
		if (retval < 0)
			return -EIO;

		retval = enable_oscillator(us);
		if (retval == 0)
			(void)do_config_autodelink(us, 1, 0);
	} else {
		/* Autodelink controlled by firmware */

		SET_BIT(value, 2);

		if (CHECK_ID(chip, 0x0138, 0x3882))
			CLR_BIT(value, 2);

		if (CHECK_ID(chip, 0x0159, 0x5889) ||
603
		    CHECK_ID(chip, 0x0138, 0x3880)) {
W
wwang 已提交
604 605 606 607
			CLR_BIT(value, 0);
			CLR_BIT(value, 7);
		}

608 609
		/* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
		retval = __do_config_autodelink(us, &value, 1);
W
wwang 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
		if (retval < 0)
			return -EIO;

		if (CHECK_ID(chip, 0x0159, 0x5888)) {
			value = 0xFF;
			retval = rts51x_write_mem(us, 0xFE79, &value, 1);
			if (retval < 0)
				return -EIO;

			value = 0x01;
			retval = rts51x_write_mem(us, 0x48, &value, 1);
			if (retval < 0)
				return -EIO;
		}
	}

	return 0;
}

static int config_autodelink_before_power_down(struct us_data *us)
{
	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
	int retval;
	u8 value;

	if (!CHK_AUTO_DELINK(chip))
		return 0;

	if (auto_delink_en) {
		retval = rts51x_read_mem(us, 0xFE77, &value, 1);
		if (retval < 0)
			return -EIO;

		SET_BIT(value, 2);
		retval = rts51x_write_mem(us, 0xFE77, &value, 1);
		if (retval < 0)
			return -EIO;

		if (CHECK_ID(chip, 0x0159, 0x5888)) {
			value = 0x01;
			retval = rts51x_write_mem(us, 0x48, &value, 1);
			if (retval < 0)
				return -EIO;
		}

		retval = rts51x_read_mem(us, 0xFE47, &value, 1);
		if (retval < 0)
			return -EIO;

		SET_BIT(value, 0);
		if (CHECK_ID(chip, 0x0138, 0x3882))
			SET_BIT(value, 2);
		retval = rts51x_write_mem(us, 0xFE77, &value, 1);
		if (retval < 0)
			return -EIO;
	} else {
		if (CHECK_ID(chip, 0x0159, 0x5889) ||
667 668
		    CHECK_ID(chip, 0x0138, 0x3880) ||
		    CHECK_ID(chip, 0x0138, 0x3882)) {
W
wwang 已提交
669 670 671 672 673
			retval = rts51x_read_mem(us, 0xFE47, &value, 1);
			if (retval < 0)
				return -EIO;

			if (CHECK_ID(chip, 0x0159, 0x5889) ||
674
			    CHECK_ID(chip, 0x0138, 0x3880)) {
W
wwang 已提交
675 676 677 678 679 680 681
				SET_BIT(value, 0);
				SET_BIT(value, 7);
			}

			if (CHECK_ID(chip, 0x0138, 0x3882))
				SET_BIT(value, 2);

682 683
			/* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
			retval = __do_config_autodelink(us, &value, 1);
W
wwang 已提交
684 685 686 687 688 689 690 691 692 693 694 695
			if (retval < 0)
				return -EIO;
		}

		if (CHECK_ID(chip, 0x0159, 0x5888)) {
			value = 0x01;
			retval = rts51x_write_mem(us, 0x48, &value, 1);
			if (retval < 0)
				return -EIO;
		}
	}

696 697 698 699 700 701 702 703 704 705
	return 0;
}

static void fw5895_init(struct us_data *us)
{
	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
	int retval;
	u8 val;

	if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
706
		usb_stor_dbg(us, "Not the specified device, return immediately!\n");
707 708 709 710 711 712
	} else {
		retval = rts51x_read_mem(us, 0xFD6F, &val, 1);
		if (retval == STATUS_SUCCESS && (val & 0x1F) == 0) {
			val = 0x1F;
			retval = rts51x_write_mem(us, 0xFD70, &val, 1);
			if (retval != STATUS_SUCCESS)
713
				usb_stor_dbg(us, "Write memory fail\n");
714
		} else {
715
			usb_stor_dbg(us, "Read memory fail, OR (val & 0x1F) != 0\n");
716 717 718 719 720 721 722 723 724 725 726 727
		}
	}
}

#ifdef CONFIG_REALTEK_AUTOPM
static void fw5895_set_mmc_wp(struct us_data *us)
{
	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
	int retval;
	u8 buf[13];

	if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
728
		usb_stor_dbg(us, "Not the specified device, return immediately!\n");
729 730 731 732 733 734 735 736 737
	} else {
		retval = rts51x_read_mem(us, 0xFD6F, buf, 1);
		if (retval == STATUS_SUCCESS && (buf[0] & 0x24) == 0x24) {
			/* SD Exist and SD WP */
			retval = rts51x_read_mem(us, 0xD04E, buf, 1);
			if (retval == STATUS_SUCCESS) {
				buf[0] |= 0x04;
				retval = rts51x_write_mem(us, 0xFD70, buf, 1);
				if (retval != STATUS_SUCCESS)
738
					usb_stor_dbg(us, "Write memory fail\n");
739
			} else {
740
				usb_stor_dbg(us, "Read memory fail\n");
741 742
			}
		} else {
743
			usb_stor_dbg(us, "Read memory fail, OR (buf[0]&0x24)!=0x24\n");
744 745 746 747 748 749
		}
	}
}

static void rts51x_modi_suspend_timer(struct rts51x_chip *chip)
{
750 751 752
	struct us_data *us = chip->us;

	usb_stor_dbg(us, "state:%d\n", rts51x_get_stat(chip));
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

	chip->timer_expires = jiffies + msecs_to_jiffies(1000*ss_delay);
	mod_timer(&chip->rts51x_suspend_timer, chip->timer_expires);
}

static void rts51x_suspend_timer_fn(unsigned long data)
{
	struct rts51x_chip *chip = (struct rts51x_chip *)data;
	struct us_data *us = chip->us;

	switch (rts51x_get_stat(chip)) {
	case RTS51X_STAT_INIT:
	case RTS51X_STAT_RUN:
		rts51x_modi_suspend_timer(chip);
		break;
	case RTS51X_STAT_IDLE:
	case RTS51X_STAT_SS:
770 771 772
		usb_stor_dbg(us, "RTS51X_STAT_SS, intf->pm_usage_cnt:%d, power.usage:%d\n",
			     atomic_read(&us->pusb_intf->pm_usage_cnt),
			     atomic_read(&us->pusb_intf->dev.power.usage_count));
773 774

		if (atomic_read(&us->pusb_intf->pm_usage_cnt) > 0) {
775
			usb_stor_dbg(us, "Ready to enter SS state\n");
776 777 778
			rts51x_set_stat(chip, RTS51X_STAT_SS);
			/* ignore mass storage interface's children */
			pm_suspend_ignore_children(&us->pusb_intf->dev, true);
779
			usb_autopm_put_interface_async(us->pusb_intf);
780 781 782
			usb_stor_dbg(us, "RTS51X_STAT_SS 01, intf->pm_usage_cnt:%d, power.usage:%d\n",
				     atomic_read(&us->pusb_intf->pm_usage_cnt),
				     atomic_read(&us->pusb_intf->dev.power.usage_count));
783 784 785
		}
		break;
	default:
786
		usb_stor_dbg(us, "Unknown state !!!\n");
787 788 789 790 791 792 793 794 795 796 797 798 799 800
		break;
	}
}

static inline int working_scsi(struct scsi_cmnd *srb)
{
	if ((srb->cmnd[0] == TEST_UNIT_READY) ||
	    (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL)) {
		return 0;
	}

	return 1;
}

801
static void rts51x_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
802 803 804 805 806 807 808 809 810 811 812 813
{
	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
	static int card_first_show = 1;
	static u8 media_not_present[] = { 0x70, 0, 0x02, 0, 0, 0, 0,
		10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0
	};
	static u8 invalid_cmd_field[] = { 0x70, 0, 0x05, 0, 0, 0, 0,
		10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0
	};
	int ret;

	if (working_scsi(srb)) {
814 815 816
		usb_stor_dbg(us, "working scsi, intf->pm_usage_cnt:%d, power.usage:%d\n",
			     atomic_read(&us->pusb_intf->pm_usage_cnt),
			     atomic_read(&us->pusb_intf->dev.power.usage_count));
817 818 819

		if (atomic_read(&us->pusb_intf->pm_usage_cnt) <= 0) {
			ret = usb_autopm_get_interface(us->pusb_intf);
820
			usb_stor_dbg(us, "working scsi, ret=%d\n", ret);
821 822 823 824 825 826
		}
		if (rts51x_get_stat(chip) != RTS51X_STAT_RUN)
			rts51x_set_stat(chip, RTS51X_STAT_RUN);
		chip->proto_handler_backup(srb, us);
	} else {
		if (rts51x_get_stat(chip) == RTS51X_STAT_SS) {
827
			usb_stor_dbg(us, "NOT working scsi\n");
828 829 830 831 832 833 834 835 836 837
			if ((srb->cmnd[0] == TEST_UNIT_READY) &&
			    (chip->pwr_state == US_SUSPEND)) {
				if (TST_LUN_READY(chip, srb->device->lun)) {
					srb->result = SAM_STAT_GOOD;
				} else {
					srb->result = SAM_STAT_CHECK_CONDITION;
					memcpy(srb->sense_buffer,
					       media_not_present,
					       US_SENSE_SIZE);
				}
838
				usb_stor_dbg(us, "TEST_UNIT_READY\n");
839 840 841 842 843 844 845 846 847 848 849 850
				goto out;
			}
			if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
				int prevent = srb->cmnd[4] & 0x1;
				if (prevent) {
					srb->result = SAM_STAT_CHECK_CONDITION;
					memcpy(srb->sense_buffer,
					       invalid_cmd_field,
					       US_SENSE_SIZE);
				} else {
					srb->result = SAM_STAT_GOOD;
				}
851
				usb_stor_dbg(us, "ALLOW_MEDIUM_REMOVAL\n");
852 853 854
				goto out;
			}
		} else {
855
			usb_stor_dbg(us, "NOT working scsi, not SS\n");
856
			chip->proto_handler_backup(srb, us);
857
			/* Check whether card is plugged in */
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
			if (srb->cmnd[0] == TEST_UNIT_READY) {
				if (srb->result == SAM_STAT_GOOD) {
					SET_LUN_READY(chip, srb->device->lun);
					if (card_first_show) {
						card_first_show = 0;
						fw5895_set_mmc_wp(us);
					}
				} else {
					CLR_LUN_READY(chip, srb->device->lun);
					card_first_show = 1;
				}
			}
			if (rts51x_get_stat(chip) != RTS51X_STAT_IDLE)
				rts51x_set_stat(chip, RTS51X_STAT_IDLE);
		}
	}
out:
875
	usb_stor_dbg(us, "state:%d\n", rts51x_get_stat(chip));
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
	if (rts51x_get_stat(chip) == RTS51X_STAT_RUN)
		rts51x_modi_suspend_timer(chip);
}

static int realtek_cr_autosuspend_setup(struct us_data *us)
{
	struct rts51x_chip *chip;
	struct rts51x_status *status = NULL;
	u8 buf[16];
	int retval;

	chip = (struct rts51x_chip *)us->extra;
	chip->support_auto_delink = 0;
	chip->pwr_state = US_RESUME;
	chip->lun_ready = 0;
	rts51x_set_stat(chip, RTS51X_STAT_INIT);

	retval = rts51x_read_status(us, 0, buf, 16, &(chip->status_len));
	if (retval != STATUS_SUCCESS) {
895
		usb_stor_dbg(us, "Read status fail\n");
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
		return -EIO;
	}
	status = chip->status;
	status->vid = ((u16) buf[0] << 8) | buf[1];
	status->pid = ((u16) buf[2] << 8) | buf[3];
	status->cur_lun = buf[4];
	status->card_type = buf[5];
	status->total_lun = buf[6];
	status->fw_ver = ((u16) buf[7] << 8) | buf[8];
	status->phy_exist = buf[9];
	status->multi_flag = buf[10];
	status->multi_card = buf[11];
	status->log_exist = buf[12];
	if (chip->status_len == 16) {
		status->detailed_type.detailed_type1 = buf[13];
		status->function[0] = buf[14];
		status->function[1] = buf[15];
	}

	/* back up the proto_handler in us->extra */
	chip = (struct rts51x_chip *)(us->extra);
	chip->proto_handler_backup = us->proto_handler;
	/* Set the autosuspend_delay to 0 */
	pm_runtime_set_autosuspend_delay(&us->pusb_dev->dev, 0);
	/* override us->proto_handler setted in get_protocol() */
	us->proto_handler = rts51x_invoke_transport;

	chip->timer_expires = 0;
	setup_timer(&chip->rts51x_suspend_timer, rts51x_suspend_timer_fn,
			(unsigned long)chip);
	fw5895_init(us);

	/* enable autosuspend funciton of the usb device */
	usb_enable_autosuspend(us->pusb_dev);

W
wwang 已提交
931 932
	return 0;
}
933
#endif
W
wwang 已提交
934 935 936

static void realtek_cr_destructor(void *extra)
{
937
	struct rts51x_chip *chip = extra;
938

W
wwang 已提交
939 940
	if (!chip)
		return;
941

942 943 944 945 946 947
#ifdef CONFIG_REALTEK_AUTOPM
	if (ss_en) {
		del_timer(&chip->rts51x_suspend_timer);
		chip->timer_expires = 0;
	}
#endif
W
wwang 已提交
948 949 950 951
	kfree(chip->status);
}

#ifdef CONFIG_PM
952
static int realtek_cr_suspend(struct usb_interface *iface, pm_message_t message)
W
wwang 已提交
953
{
954 955 956 957 958 959 960 961 962 963
	struct us_data *us = usb_get_intfdata(iface);

	/* wait until no command is running */
	mutex_lock(&us->dev_mutex);

	config_autodelink_before_power_down(us);

	mutex_unlock(&us->dev_mutex);

	return 0;
W
wwang 已提交
964
}
965 966 967 968 969 970 971 972 973 974 975 976 977

static int realtek_cr_resume(struct usb_interface *iface)
{
	struct us_data *us = usb_get_intfdata(iface);

	fw5895_init(us);
	config_autodelink_after_power_on(us);

	return 0;
}
#else
#define realtek_cr_suspend	NULL
#define realtek_cr_resume	NULL
W
wwang 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991
#endif

static int init_realtek_cr(struct us_data *us)
{
	struct rts51x_chip *chip;
	int size, i, retval;

	chip = kzalloc(sizeof(struct rts51x_chip), GFP_KERNEL);
	if (!chip)
		return -ENOMEM;

	us->extra = chip;
	us->extra_destructor = realtek_cr_destructor;
	us->max_lun = chip->max_lun = rts51x_get_max_lun(us);
992
	chip->us = us;
W
wwang 已提交
993

994
	usb_stor_dbg(us, "chip->max_lun = %d\n", chip->max_lun);
W
wwang 已提交
995 996 997 998 999 1000 1001

	size = (chip->max_lun + 1) * sizeof(struct rts51x_status);
	chip->status = kzalloc(size, GFP_KERNEL);
	if (!chip->status)
		goto INIT_FAIL;

	for (i = 0; i <= (int)(chip->max_lun); i++) {
1002
		retval = rts51x_check_status(us, (u8) i);
W
wwang 已提交
1003 1004 1005 1006 1007
		if (retval < 0)
			goto INIT_FAIL;
	}

	if (CHECK_FW_VER(chip, 0x5888) || CHECK_FW_VER(chip, 0x5889) ||
1008
	    CHECK_FW_VER(chip, 0x5901))
W
wwang 已提交
1009 1010 1011 1012 1013
		SET_AUTO_DELINK(chip);
	if (STATUS_LEN(chip) == 16) {
		if (SUPPORT_AUTO_DELINK(chip))
			SET_AUTO_DELINK(chip);
	}
1014
#ifdef CONFIG_REALTEK_AUTOPM
1015
	if (ss_en)
1016 1017
		realtek_cr_autosuspend_setup(us);
#endif
W
wwang 已提交
1018

1019
	usb_stor_dbg(us, "chip->flag = 0x%x\n", chip->flag);
W
wwang 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

	(void)config_autodelink_after_power_on(us);

	return 0;

INIT_FAIL:
	if (us->extra) {
		kfree(chip->status);
		kfree(us->extra);
		us->extra = NULL;
	}

	return -EIO;
}

static int realtek_cr_probe(struct usb_interface *intf,
1036
			    const struct usb_device_id *id)
W
wwang 已提交
1037 1038 1039 1040
{
	struct us_data *us;
	int result;

1041
	dev_dbg(&intf->dev, "Probe Realtek Card Reader!\n");
W
wwang 已提交
1042 1043

	result = usb_stor_probe1(&us, intf, id,
1044 1045
				 (id - realtek_cr_ids) +
				 realtek_cr_unusual_dev_list);
W
wwang 已提交
1046 1047 1048 1049
	if (result)
		return result;

	result = usb_stor_probe2(us);
1050

W
wwang 已提交
1051 1052 1053 1054
	return result;
}

static struct usb_driver realtek_cr_driver = {
1055 1056 1057
	.name = "ums-realtek",
	.probe = realtek_cr_probe,
	.disconnect = usb_stor_disconnect,
1058 1059
	/* .suspend =      usb_stor_suspend, */
	/* .resume =       usb_stor_resume, */
1060
	.reset_resume = usb_stor_reset_resume,
1061 1062
	.suspend = realtek_cr_suspend,
	.resume = realtek_cr_resume,
1063 1064 1065 1066
	.pre_reset = usb_stor_pre_reset,
	.post_reset = usb_stor_post_reset,
	.id_table = realtek_cr_ids,
	.soft_unbind = 1,
1067
	.supports_autosuspend = 1,
1068
	.no_dynamic_id = 1,
W
wwang 已提交
1069 1070
};

1071
module_usb_driver(realtek_cr_driver);