xref: /openbmc/linux/drivers/usb/storage/realtek_cr.c (revision 191648d0)
1 /* Driver for Realtek RTS51xx USB card reader
2  *
3  * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
4  *
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
8  * later version.
9  *
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.
14  *
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/>.
17  *
18  * Author:
19  *   wwang (wei_wang@realsil.com.cn)
20  *   No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
21  */
22 
23 #include <linux/module.h>
24 #include <linux/blkdev.h>
25 #include <linux/kthread.h>
26 #include <linux/sched.h>
27 #include <linux/kernel.h>
28 
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <linux/cdrom.h>
33 
34 #include <linux/usb.h>
35 #include <linux/slab.h>
36 #include <linux/usb_usual.h>
37 
38 #include "usb.h"
39 #include "transport.h"
40 #include "protocol.h"
41 #include "debug.h"
42 
43 MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
44 MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION("1.03");
47 
48 static int auto_delink_en = 1;
49 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
50 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
51 
52 #ifdef CONFIG_REALTEK_AUTOPM
53 static int ss_en = 1;
54 module_param(ss_en, int, S_IRUGO | S_IWUSR);
55 MODULE_PARM_DESC(ss_en, "enable selective suspend");
56 
57 static int ss_delay = 50;
58 module_param(ss_delay, int, S_IRUGO | S_IWUSR);
59 MODULE_PARM_DESC(ss_delay,
60 		 "seconds to delay before entering selective suspend");
61 
62 enum RTS51X_STAT {
63 	RTS51X_STAT_INIT,
64 	RTS51X_STAT_IDLE,
65 	RTS51X_STAT_RUN,
66 	RTS51X_STAT_SS
67 };
68 
69 #define POLLING_INTERVAL	50
70 
71 #define rts51x_set_stat(chip, stat)	\
72 	((chip)->state = (enum RTS51X_STAT)(stat))
73 #define rts51x_get_stat(chip)		((chip)->state)
74 
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)))
78 
79 #endif
80 
81 struct rts51x_status {
82 	u16 vid;
83 	u16 pid;
84 	u8 cur_lun;
85 	u8 card_type;
86 	u8 total_lun;
87 	u16 fw_ver;
88 	u8 phy_exist;
89 	u8 multi_flag;
90 	u8 multi_card;
91 	u8 log_exist;
92 	union {
93 		u8 detailed_type1;
94 		u8 detailed_type2;
95 	} detailed_type;
96 	u8 function[2];
97 };
98 
99 struct rts51x_chip {
100 	u16 vendor_id;
101 	u16 product_id;
102 	char max_lun;
103 
104 	struct rts51x_status *status;
105 	int status_len;
106 
107 	u32 flag;
108 #ifdef CONFIG_REALTEK_AUTOPM
109 	struct us_data *us;
110 	struct timer_list rts51x_suspend_timer;
111 	unsigned long timer_expires;
112 	int pwr_state;
113 	u8 lun_ready;
114 	enum RTS51X_STAT state;
115 	int support_auto_delink;
116 #endif
117 	/* used to back up the protocal choosen in probe1 phase */
118 	proto_cmnd proto_handler_backup;
119 };
120 
121 /* flag definition */
122 #define FLIDX_AUTO_DELINK		0x01
123 
124 #define SCSI_LUN(srb)			((srb)->device->lun)
125 
126 /* Bit Operation */
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)))
130 
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)
134 
135 #define RTS51X_GET_VID(chip)		((chip)->vendor_id)
136 #define RTS51X_GET_PID(chip)		((chip)->product_id)
137 
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)
142 
143 #define STATUS_SUCCESS		0
144 #define STATUS_FAIL		1
145 
146 /* Check card reader function */
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)
159 
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)))
164 
165 static int init_realtek_cr(struct us_data *us);
166 
167 /*
168  * The table of devices
169  */
170 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
171 		    vendorName, productName, useProtocol, useTransport, \
172 		    initFunction, flags) \
173 {\
174 	USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
175 	.driver_info = (flags) \
176 }
177 
178 static const struct usb_device_id realtek_cr_ids[] = {
179 #	include "unusual_realtek.h"
180 	{}			/* Terminating entry */
181 };
182 
183 MODULE_DEVICE_TABLE(usb, realtek_cr_ids);
184 
185 #undef UNUSUAL_DEV
186 
187 /*
188  * The flags table
189  */
190 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
191 		    vendor_name, product_name, use_protocol, use_transport, \
192 		    init_function, Flags) \
193 { \
194 	.vendorName = vendor_name,	\
195 	.productName = product_name,	\
196 	.useProtocol = use_protocol,	\
197 	.useTransport = use_transport,	\
198 	.initFunction = init_function,	\
199 }
200 
201 static struct us_unusual_dev realtek_cr_unusual_dev_list[] = {
202 #	include "unusual_realtek.h"
203 	{}			/* Terminating entry */
204 };
205 
206 #undef UNUSUAL_DEV
207 
208 static int rts51x_bulk_transport(struct us_data *us, u8 lun,
209 				 u8 *cmd, int cmd_len, u8 *buf, int buf_len,
210 				 enum dma_data_direction dir, int *act_len)
211 {
212 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *)us->iobuf;
213 	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *)us->iobuf;
214 	int result;
215 	unsigned int residue;
216 	unsigned int cswlen;
217 	unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
218 
219 	/* set up the command wrapper */
220 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
221 	bcb->DataTransferLength = cpu_to_le32(buf_len);
222 	bcb->Flags = (dir == DMA_FROM_DEVICE) ? US_BULK_FLAG_IN : 0;
223 	bcb->Tag = ++us->tag;
224 	bcb->Lun = lun;
225 	bcb->Length = cmd_len;
226 
227 	/* copy the command payload */
228 	memset(bcb->CDB, 0, sizeof(bcb->CDB));
229 	memcpy(bcb->CDB, cmd, bcb->Length);
230 
231 	/* send it to out endpoint */
232 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
233 					    bcb, cbwlen, NULL);
234 	if (result != USB_STOR_XFER_GOOD)
235 		return USB_STOR_TRANSPORT_ERROR;
236 
237 	/* DATA STAGE */
238 	/* send/receive data payload, if there is any */
239 
240 	if (buf && buf_len) {
241 		unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
242 		    us->recv_bulk_pipe : us->send_bulk_pipe;
243 		result = usb_stor_bulk_transfer_buf(us, pipe,
244 						    buf, buf_len, NULL);
245 		if (result == USB_STOR_XFER_ERROR)
246 			return USB_STOR_TRANSPORT_ERROR;
247 	}
248 
249 	/* get CSW for device status */
250 	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
251 					    bcs, US_BULK_CS_WRAP_LEN, &cswlen);
252 	if (result != USB_STOR_XFER_GOOD)
253 		return USB_STOR_TRANSPORT_ERROR;
254 
255 	/* check bulk status */
256 	if (bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN)) {
257 		usb_stor_dbg(us, "Signature mismatch: got %08X, expecting %08X\n",
258 			     le32_to_cpu(bcs->Signature), US_BULK_CS_SIGN);
259 		return USB_STOR_TRANSPORT_ERROR;
260 	}
261 
262 	residue = bcs->Residue;
263 	if (bcs->Tag != us->tag)
264 		return USB_STOR_TRANSPORT_ERROR;
265 
266 	/* try to compute the actual residue, based on how much data
267 	 * was really transferred and what the device tells us */
268 	if (residue)
269 		residue = residue < buf_len ? residue : buf_len;
270 
271 	if (act_len)
272 		*act_len = buf_len - residue;
273 
274 	/* based on the status code, we report good or bad */
275 	switch (bcs->Status) {
276 	case US_BULK_STAT_OK:
277 		/* command good -- note that data could be short */
278 		return USB_STOR_TRANSPORT_GOOD;
279 
280 	case US_BULK_STAT_FAIL:
281 		/* command failed */
282 		return USB_STOR_TRANSPORT_FAILED;
283 
284 	case US_BULK_STAT_PHASE:
285 		/* phase error -- note that a transport reset will be
286 		 * invoked by the invoke_transport() function
287 		 */
288 		return USB_STOR_TRANSPORT_ERROR;
289 	}
290 
291 	/* we should never get here, but if we do, we're in trouble */
292 	return USB_STOR_TRANSPORT_ERROR;
293 }
294 
295 static int rts51x_bulk_transport_special(struct us_data *us, u8 lun,
296 				 u8 *cmd, int cmd_len, u8 *buf, int buf_len,
297 				 enum dma_data_direction dir, int *act_len)
298 {
299 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
300 	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
301 	int result;
302 	unsigned int cswlen;
303 	unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
304 
305 	/* set up the command wrapper */
306 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
307 	bcb->DataTransferLength = cpu_to_le32(buf_len);
308 	bcb->Flags = (dir == DMA_FROM_DEVICE) ? US_BULK_FLAG_IN : 0;
309 	bcb->Tag = ++us->tag;
310 	bcb->Lun = lun;
311 	bcb->Length = cmd_len;
312 
313 	/* copy the command payload */
314 	memset(bcb->CDB, 0, sizeof(bcb->CDB));
315 	memcpy(bcb->CDB, cmd, bcb->Length);
316 
317 	/* send it to out endpoint */
318 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
319 				bcb, cbwlen, NULL);
320 	if (result != USB_STOR_XFER_GOOD)
321 		return USB_STOR_TRANSPORT_ERROR;
322 
323 	/* DATA STAGE */
324 	/* send/receive data payload, if there is any */
325 
326 	if (buf && buf_len) {
327 		unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
328 				us->recv_bulk_pipe : us->send_bulk_pipe;
329 		result = usb_stor_bulk_transfer_buf(us, pipe,
330 				buf, buf_len, NULL);
331 		if (result == USB_STOR_XFER_ERROR)
332 			return USB_STOR_TRANSPORT_ERROR;
333 	}
334 
335 	/* get CSW for device status */
336 	result = usb_bulk_msg(us->pusb_dev, us->recv_bulk_pipe, bcs,
337 			US_BULK_CS_WRAP_LEN, &cswlen, 250);
338 	return result;
339 }
340 
341 /* Determine what the maximum LUN supported is */
342 static int rts51x_get_max_lun(struct us_data *us)
343 {
344 	int result;
345 
346 	/* issue the command */
347 	us->iobuf[0] = 0;
348 	result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
349 				      US_BULK_GET_MAX_LUN,
350 				      USB_DIR_IN | USB_TYPE_CLASS |
351 				      USB_RECIP_INTERFACE,
352 				      0, us->ifnum, us->iobuf, 1, 10 * HZ);
353 
354 	usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
355 		     result, us->iobuf[0]);
356 
357 	/* if we have a successful request, return the result */
358 	if (result > 0)
359 		return us->iobuf[0];
360 
361 	return 0;
362 }
363 
364 static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
365 {
366 	int retval;
367 	u8 cmnd[12] = { 0 };
368 	u8 *buf;
369 
370 	buf = kmalloc(len, GFP_NOIO);
371 	if (buf == NULL)
372 		return USB_STOR_TRANSPORT_ERROR;
373 
374 	usb_stor_dbg(us, "addr = 0x%x, len = %d\n", addr, len);
375 
376 	cmnd[0] = 0xF0;
377 	cmnd[1] = 0x0D;
378 	cmnd[2] = (u8) (addr >> 8);
379 	cmnd[3] = (u8) addr;
380 	cmnd[4] = (u8) (len >> 8);
381 	cmnd[5] = (u8) len;
382 
383 	retval = rts51x_bulk_transport(us, 0, cmnd, 12,
384 				       buf, len, DMA_FROM_DEVICE, NULL);
385 	if (retval != USB_STOR_TRANSPORT_GOOD) {
386 		kfree(buf);
387 		return -EIO;
388 	}
389 
390 	memcpy(data, buf, len);
391 	kfree(buf);
392 	return 0;
393 }
394 
395 static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
396 {
397 	int retval;
398 	u8 cmnd[12] = { 0 };
399 	u8 *buf;
400 
401 	buf = kmemdup(data, len, GFP_NOIO);
402 	if (buf == NULL)
403 		return USB_STOR_TRANSPORT_ERROR;
404 
405 	usb_stor_dbg(us, "addr = 0x%x, len = %d\n", addr, len);
406 
407 	cmnd[0] = 0xF0;
408 	cmnd[1] = 0x0E;
409 	cmnd[2] = (u8) (addr >> 8);
410 	cmnd[3] = (u8) addr;
411 	cmnd[4] = (u8) (len >> 8);
412 	cmnd[5] = (u8) len;
413 
414 	retval = rts51x_bulk_transport(us, 0, cmnd, 12,
415 				       buf, len, DMA_TO_DEVICE, NULL);
416 	kfree(buf);
417 	if (retval != USB_STOR_TRANSPORT_GOOD)
418 		return -EIO;
419 
420 	return 0;
421 }
422 
423 static int rts51x_read_status(struct us_data *us,
424 			      u8 lun, u8 *status, int len, int *actlen)
425 {
426 	int retval;
427 	u8 cmnd[12] = { 0 };
428 	u8 *buf;
429 
430 	buf = kmalloc(len, GFP_NOIO);
431 	if (buf == NULL)
432 		return USB_STOR_TRANSPORT_ERROR;
433 
434 	usb_stor_dbg(us, "lun = %d\n", lun);
435 
436 	cmnd[0] = 0xF0;
437 	cmnd[1] = 0x09;
438 
439 	retval = rts51x_bulk_transport(us, lun, cmnd, 12,
440 				       buf, len, DMA_FROM_DEVICE, actlen);
441 	if (retval != USB_STOR_TRANSPORT_GOOD) {
442 		kfree(buf);
443 		return -EIO;
444 	}
445 
446 	memcpy(status, buf, len);
447 	kfree(buf);
448 	return 0;
449 }
450 
451 static int rts51x_check_status(struct us_data *us, u8 lun)
452 {
453 	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
454 	int retval;
455 	u8 buf[16];
456 
457 	retval = rts51x_read_status(us, lun, buf, 16, &(chip->status_len));
458 	if (retval != STATUS_SUCCESS)
459 		return -EIO;
460 
461 	usb_stor_dbg(us, "chip->status_len = %d\n", chip->status_len);
462 
463 	chip->status[lun].vid = ((u16) buf[0] << 8) | buf[1];
464 	chip->status[lun].pid = ((u16) buf[2] << 8) | buf[3];
465 	chip->status[lun].cur_lun = buf[4];
466 	chip->status[lun].card_type = buf[5];
467 	chip->status[lun].total_lun = buf[6];
468 	chip->status[lun].fw_ver = ((u16) buf[7] << 8) | buf[8];
469 	chip->status[lun].phy_exist = buf[9];
470 	chip->status[lun].multi_flag = buf[10];
471 	chip->status[lun].multi_card = buf[11];
472 	chip->status[lun].log_exist = buf[12];
473 	if (chip->status_len == 16) {
474 		chip->status[lun].detailed_type.detailed_type1 = buf[13];
475 		chip->status[lun].function[0] = buf[14];
476 		chip->status[lun].function[1] = buf[15];
477 	}
478 
479 	return 0;
480 }
481 
482 static int enable_oscillator(struct us_data *us)
483 {
484 	int retval;
485 	u8 value;
486 
487 	retval = rts51x_read_mem(us, 0xFE77, &value, 1);
488 	if (retval < 0)
489 		return -EIO;
490 
491 	value |= 0x04;
492 	retval = rts51x_write_mem(us, 0xFE77, &value, 1);
493 	if (retval < 0)
494 		return -EIO;
495 
496 	retval = rts51x_read_mem(us, 0xFE77, &value, 1);
497 	if (retval < 0)
498 		return -EIO;
499 
500 	if (!(value & 0x04))
501 		return -EIO;
502 
503 	return 0;
504 }
505 
506 static int __do_config_autodelink(struct us_data *us, u8 *data, u16 len)
507 {
508 	int retval;
509 	u8 cmnd[12] = {0};
510 	u8 *buf;
511 
512 	usb_stor_dbg(us, "addr = 0xfe47, len = %d\n", len);
513 
514 	buf = kmemdup(data, len, GFP_NOIO);
515 	if (!buf)
516 		return USB_STOR_TRANSPORT_ERROR;
517 
518 	cmnd[0] = 0xF0;
519 	cmnd[1] = 0x0E;
520 	cmnd[2] = 0xfe;
521 	cmnd[3] = 0x47;
522 	cmnd[4] = (u8)(len >> 8);
523 	cmnd[5] = (u8)len;
524 
525 	retval = rts51x_bulk_transport_special(us, 0, cmnd, 12, buf, len, DMA_TO_DEVICE, NULL);
526 	kfree(buf);
527 	if (retval != USB_STOR_TRANSPORT_GOOD) {
528 		return -EIO;
529 	}
530 
531 	return 0;
532 }
533 
534 static int do_config_autodelink(struct us_data *us, int enable, int force)
535 {
536 	int retval;
537 	u8 value;
538 
539 	retval = rts51x_read_mem(us, 0xFE47, &value, 1);
540 	if (retval < 0)
541 		return -EIO;
542 
543 	if (enable) {
544 		if (force)
545 			value |= 0x03;
546 		else
547 			value |= 0x01;
548 	} else {
549 		value &= ~0x03;
550 	}
551 
552 	usb_stor_dbg(us, "set 0xfe47 to 0x%x\n", value);
553 
554 	/* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
555 	retval = __do_config_autodelink(us, &value, 1);
556 	if (retval < 0)
557 		return -EIO;
558 
559 	return 0;
560 }
561 
562 static int config_autodelink_after_power_on(struct us_data *us)
563 {
564 	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
565 	int retval;
566 	u8 value;
567 
568 	if (!CHK_AUTO_DELINK(chip))
569 		return 0;
570 
571 	retval = rts51x_read_mem(us, 0xFE47, &value, 1);
572 	if (retval < 0)
573 		return -EIO;
574 
575 	if (auto_delink_en) {
576 		CLR_BIT(value, 0);
577 		CLR_BIT(value, 1);
578 		SET_BIT(value, 2);
579 
580 		if (CHECK_ID(chip, 0x0138, 0x3882))
581 			CLR_BIT(value, 2);
582 
583 		SET_BIT(value, 7);
584 
585 		/* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
586 		retval = __do_config_autodelink(us, &value, 1);
587 		if (retval < 0)
588 			return -EIO;
589 
590 		retval = enable_oscillator(us);
591 		if (retval == 0)
592 			(void)do_config_autodelink(us, 1, 0);
593 	} else {
594 		/* Autodelink controlled by firmware */
595 
596 		SET_BIT(value, 2);
597 
598 		if (CHECK_ID(chip, 0x0138, 0x3882))
599 			CLR_BIT(value, 2);
600 
601 		if (CHECK_ID(chip, 0x0159, 0x5889) ||
602 		    CHECK_ID(chip, 0x0138, 0x3880)) {
603 			CLR_BIT(value, 0);
604 			CLR_BIT(value, 7);
605 		}
606 
607 		/* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
608 		retval = __do_config_autodelink(us, &value, 1);
609 		if (retval < 0)
610 			return -EIO;
611 
612 		if (CHECK_ID(chip, 0x0159, 0x5888)) {
613 			value = 0xFF;
614 			retval = rts51x_write_mem(us, 0xFE79, &value, 1);
615 			if (retval < 0)
616 				return -EIO;
617 
618 			value = 0x01;
619 			retval = rts51x_write_mem(us, 0x48, &value, 1);
620 			if (retval < 0)
621 				return -EIO;
622 		}
623 	}
624 
625 	return 0;
626 }
627 
628 static int config_autodelink_before_power_down(struct us_data *us)
629 {
630 	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
631 	int retval;
632 	u8 value;
633 
634 	if (!CHK_AUTO_DELINK(chip))
635 		return 0;
636 
637 	if (auto_delink_en) {
638 		retval = rts51x_read_mem(us, 0xFE77, &value, 1);
639 		if (retval < 0)
640 			return -EIO;
641 
642 		SET_BIT(value, 2);
643 		retval = rts51x_write_mem(us, 0xFE77, &value, 1);
644 		if (retval < 0)
645 			return -EIO;
646 
647 		if (CHECK_ID(chip, 0x0159, 0x5888)) {
648 			value = 0x01;
649 			retval = rts51x_write_mem(us, 0x48, &value, 1);
650 			if (retval < 0)
651 				return -EIO;
652 		}
653 
654 		retval = rts51x_read_mem(us, 0xFE47, &value, 1);
655 		if (retval < 0)
656 			return -EIO;
657 
658 		SET_BIT(value, 0);
659 		if (CHECK_ID(chip, 0x0138, 0x3882))
660 			SET_BIT(value, 2);
661 		retval = rts51x_write_mem(us, 0xFE77, &value, 1);
662 		if (retval < 0)
663 			return -EIO;
664 	} else {
665 		if (CHECK_ID(chip, 0x0159, 0x5889) ||
666 		    CHECK_ID(chip, 0x0138, 0x3880) ||
667 		    CHECK_ID(chip, 0x0138, 0x3882)) {
668 			retval = rts51x_read_mem(us, 0xFE47, &value, 1);
669 			if (retval < 0)
670 				return -EIO;
671 
672 			if (CHECK_ID(chip, 0x0159, 0x5889) ||
673 			    CHECK_ID(chip, 0x0138, 0x3880)) {
674 				SET_BIT(value, 0);
675 				SET_BIT(value, 7);
676 			}
677 
678 			if (CHECK_ID(chip, 0x0138, 0x3882))
679 				SET_BIT(value, 2);
680 
681 			/* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
682 			retval = __do_config_autodelink(us, &value, 1);
683 			if (retval < 0)
684 				return -EIO;
685 		}
686 
687 		if (CHECK_ID(chip, 0x0159, 0x5888)) {
688 			value = 0x01;
689 			retval = rts51x_write_mem(us, 0x48, &value, 1);
690 			if (retval < 0)
691 				return -EIO;
692 		}
693 	}
694 
695 	return 0;
696 }
697 
698 static void fw5895_init(struct us_data *us)
699 {
700 	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
701 	int retval;
702 	u8 val;
703 
704 	if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
705 		usb_stor_dbg(us, "Not the specified device, return immediately!\n");
706 	} else {
707 		retval = rts51x_read_mem(us, 0xFD6F, &val, 1);
708 		if (retval == STATUS_SUCCESS && (val & 0x1F) == 0) {
709 			val = 0x1F;
710 			retval = rts51x_write_mem(us, 0xFD70, &val, 1);
711 			if (retval != STATUS_SUCCESS)
712 				usb_stor_dbg(us, "Write memory fail\n");
713 		} else {
714 			usb_stor_dbg(us, "Read memory fail, OR (val & 0x1F) != 0\n");
715 		}
716 	}
717 }
718 
719 #ifdef CONFIG_REALTEK_AUTOPM
720 static void fw5895_set_mmc_wp(struct us_data *us)
721 {
722 	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
723 	int retval;
724 	u8 buf[13];
725 
726 	if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
727 		usb_stor_dbg(us, "Not the specified device, return immediately!\n");
728 	} else {
729 		retval = rts51x_read_mem(us, 0xFD6F, buf, 1);
730 		if (retval == STATUS_SUCCESS && (buf[0] & 0x24) == 0x24) {
731 			/* SD Exist and SD WP */
732 			retval = rts51x_read_mem(us, 0xD04E, buf, 1);
733 			if (retval == STATUS_SUCCESS) {
734 				buf[0] |= 0x04;
735 				retval = rts51x_write_mem(us, 0xFD70, buf, 1);
736 				if (retval != STATUS_SUCCESS)
737 					usb_stor_dbg(us, "Write memory fail\n");
738 			} else {
739 				usb_stor_dbg(us, "Read memory fail\n");
740 			}
741 		} else {
742 			usb_stor_dbg(us, "Read memory fail, OR (buf[0]&0x24)!=0x24\n");
743 		}
744 	}
745 }
746 
747 static void rts51x_modi_suspend_timer(struct rts51x_chip *chip)
748 {
749 	struct us_data *us = chip->us;
750 
751 	usb_stor_dbg(us, "state:%d\n", rts51x_get_stat(chip));
752 
753 	chip->timer_expires = jiffies + msecs_to_jiffies(1000*ss_delay);
754 	mod_timer(&chip->rts51x_suspend_timer, chip->timer_expires);
755 }
756 
757 static void rts51x_suspend_timer_fn(unsigned long data)
758 {
759 	struct rts51x_chip *chip = (struct rts51x_chip *)data;
760 	struct us_data *us = chip->us;
761 
762 	switch (rts51x_get_stat(chip)) {
763 	case RTS51X_STAT_INIT:
764 	case RTS51X_STAT_RUN:
765 		rts51x_modi_suspend_timer(chip);
766 		break;
767 	case RTS51X_STAT_IDLE:
768 	case RTS51X_STAT_SS:
769 		usb_stor_dbg(us, "RTS51X_STAT_SS, intf->pm_usage_cnt:%d, power.usage:%d\n",
770 			     atomic_read(&us->pusb_intf->pm_usage_cnt),
771 			     atomic_read(&us->pusb_intf->dev.power.usage_count));
772 
773 		if (atomic_read(&us->pusb_intf->pm_usage_cnt) > 0) {
774 			usb_stor_dbg(us, "Ready to enter SS state\n");
775 			rts51x_set_stat(chip, RTS51X_STAT_SS);
776 			/* ignore mass storage interface's children */
777 			pm_suspend_ignore_children(&us->pusb_intf->dev, true);
778 			usb_autopm_put_interface_async(us->pusb_intf);
779 			usb_stor_dbg(us, "RTS51X_STAT_SS 01, intf->pm_usage_cnt:%d, power.usage:%d\n",
780 				     atomic_read(&us->pusb_intf->pm_usage_cnt),
781 				     atomic_read(&us->pusb_intf->dev.power.usage_count));
782 		}
783 		break;
784 	default:
785 		usb_stor_dbg(us, "Unknown state !!!\n");
786 		break;
787 	}
788 }
789 
790 static inline int working_scsi(struct scsi_cmnd *srb)
791 {
792 	if ((srb->cmnd[0] == TEST_UNIT_READY) ||
793 	    (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL)) {
794 		return 0;
795 	}
796 
797 	return 1;
798 }
799 
800 static void rts51x_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
801 {
802 	struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
803 	static int card_first_show = 1;
804 	static u8 media_not_present[] = { 0x70, 0, 0x02, 0, 0, 0, 0,
805 		10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0
806 	};
807 	static u8 invalid_cmd_field[] = { 0x70, 0, 0x05, 0, 0, 0, 0,
808 		10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0
809 	};
810 	int ret;
811 
812 	if (working_scsi(srb)) {
813 		usb_stor_dbg(us, "working scsi, intf->pm_usage_cnt:%d, power.usage:%d\n",
814 			     atomic_read(&us->pusb_intf->pm_usage_cnt),
815 			     atomic_read(&us->pusb_intf->dev.power.usage_count));
816 
817 		if (atomic_read(&us->pusb_intf->pm_usage_cnt) <= 0) {
818 			ret = usb_autopm_get_interface(us->pusb_intf);
819 			usb_stor_dbg(us, "working scsi, ret=%d\n", ret);
820 		}
821 		if (rts51x_get_stat(chip) != RTS51X_STAT_RUN)
822 			rts51x_set_stat(chip, RTS51X_STAT_RUN);
823 		chip->proto_handler_backup(srb, us);
824 	} else {
825 		if (rts51x_get_stat(chip) == RTS51X_STAT_SS) {
826 			usb_stor_dbg(us, "NOT working scsi\n");
827 			if ((srb->cmnd[0] == TEST_UNIT_READY) &&
828 			    (chip->pwr_state == US_SUSPEND)) {
829 				if (TST_LUN_READY(chip, srb->device->lun)) {
830 					srb->result = SAM_STAT_GOOD;
831 				} else {
832 					srb->result = SAM_STAT_CHECK_CONDITION;
833 					memcpy(srb->sense_buffer,
834 					       media_not_present,
835 					       US_SENSE_SIZE);
836 				}
837 				usb_stor_dbg(us, "TEST_UNIT_READY\n");
838 				goto out;
839 			}
840 			if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
841 				int prevent = srb->cmnd[4] & 0x1;
842 				if (prevent) {
843 					srb->result = SAM_STAT_CHECK_CONDITION;
844 					memcpy(srb->sense_buffer,
845 					       invalid_cmd_field,
846 					       US_SENSE_SIZE);
847 				} else {
848 					srb->result = SAM_STAT_GOOD;
849 				}
850 				usb_stor_dbg(us, "ALLOW_MEDIUM_REMOVAL\n");
851 				goto out;
852 			}
853 		} else {
854 			usb_stor_dbg(us, "NOT working scsi, not SS\n");
855 			chip->proto_handler_backup(srb, us);
856 			/* Check whether card is plugged in */
857 			if (srb->cmnd[0] == TEST_UNIT_READY) {
858 				if (srb->result == SAM_STAT_GOOD) {
859 					SET_LUN_READY(chip, srb->device->lun);
860 					if (card_first_show) {
861 						card_first_show = 0;
862 						fw5895_set_mmc_wp(us);
863 					}
864 				} else {
865 					CLR_LUN_READY(chip, srb->device->lun);
866 					card_first_show = 1;
867 				}
868 			}
869 			if (rts51x_get_stat(chip) != RTS51X_STAT_IDLE)
870 				rts51x_set_stat(chip, RTS51X_STAT_IDLE);
871 		}
872 	}
873 out:
874 	usb_stor_dbg(us, "state:%d\n", rts51x_get_stat(chip));
875 	if (rts51x_get_stat(chip) == RTS51X_STAT_RUN)
876 		rts51x_modi_suspend_timer(chip);
877 }
878 
879 static int realtek_cr_autosuspend_setup(struct us_data *us)
880 {
881 	struct rts51x_chip *chip;
882 	struct rts51x_status *status = NULL;
883 	u8 buf[16];
884 	int retval;
885 
886 	chip = (struct rts51x_chip *)us->extra;
887 	chip->support_auto_delink = 0;
888 	chip->pwr_state = US_RESUME;
889 	chip->lun_ready = 0;
890 	rts51x_set_stat(chip, RTS51X_STAT_INIT);
891 
892 	retval = rts51x_read_status(us, 0, buf, 16, &(chip->status_len));
893 	if (retval != STATUS_SUCCESS) {
894 		usb_stor_dbg(us, "Read status fail\n");
895 		return -EIO;
896 	}
897 	status = chip->status;
898 	status->vid = ((u16) buf[0] << 8) | buf[1];
899 	status->pid = ((u16) buf[2] << 8) | buf[3];
900 	status->cur_lun = buf[4];
901 	status->card_type = buf[5];
902 	status->total_lun = buf[6];
903 	status->fw_ver = ((u16) buf[7] << 8) | buf[8];
904 	status->phy_exist = buf[9];
905 	status->multi_flag = buf[10];
906 	status->multi_card = buf[11];
907 	status->log_exist = buf[12];
908 	if (chip->status_len == 16) {
909 		status->detailed_type.detailed_type1 = buf[13];
910 		status->function[0] = buf[14];
911 		status->function[1] = buf[15];
912 	}
913 
914 	/* back up the proto_handler in us->extra */
915 	chip = (struct rts51x_chip *)(us->extra);
916 	chip->proto_handler_backup = us->proto_handler;
917 	/* Set the autosuspend_delay to 0 */
918 	pm_runtime_set_autosuspend_delay(&us->pusb_dev->dev, 0);
919 	/* override us->proto_handler setted in get_protocol() */
920 	us->proto_handler = rts51x_invoke_transport;
921 
922 	chip->timer_expires = 0;
923 	setup_timer(&chip->rts51x_suspend_timer, rts51x_suspend_timer_fn,
924 			(unsigned long)chip);
925 	fw5895_init(us);
926 
927 	/* enable autosuspend funciton of the usb device */
928 	usb_enable_autosuspend(us->pusb_dev);
929 
930 	return 0;
931 }
932 #endif
933 
934 static void realtek_cr_destructor(void *extra)
935 {
936 	struct rts51x_chip *chip = (struct rts51x_chip *)extra;
937 	struct us_data *us;
938 
939 	if (!chip)
940 		return;
941 
942 	us = chip->us;
943 
944 #ifdef CONFIG_REALTEK_AUTOPM
945 	if (ss_en) {
946 		del_timer(&chip->rts51x_suspend_timer);
947 		chip->timer_expires = 0;
948 	}
949 #endif
950 	kfree(chip->status);
951 }
952 
953 #ifdef CONFIG_PM
954 static int realtek_cr_suspend(struct usb_interface *iface, pm_message_t message)
955 {
956 	struct us_data *us = usb_get_intfdata(iface);
957 
958 	/* wait until no command is running */
959 	mutex_lock(&us->dev_mutex);
960 
961 	config_autodelink_before_power_down(us);
962 
963 	mutex_unlock(&us->dev_mutex);
964 
965 	return 0;
966 }
967 
968 static int realtek_cr_resume(struct usb_interface *iface)
969 {
970 	struct us_data *us = usb_get_intfdata(iface);
971 
972 	fw5895_init(us);
973 	config_autodelink_after_power_on(us);
974 
975 	return 0;
976 }
977 #else
978 #define realtek_cr_suspend	NULL
979 #define realtek_cr_resume	NULL
980 #endif
981 
982 static int init_realtek_cr(struct us_data *us)
983 {
984 	struct rts51x_chip *chip;
985 	int size, i, retval;
986 
987 	chip = kzalloc(sizeof(struct rts51x_chip), GFP_KERNEL);
988 	if (!chip)
989 		return -ENOMEM;
990 
991 	us->extra = chip;
992 	us->extra_destructor = realtek_cr_destructor;
993 	us->max_lun = chip->max_lun = rts51x_get_max_lun(us);
994 
995 	usb_stor_dbg(us, "chip->max_lun = %d\n", chip->max_lun);
996 
997 	size = (chip->max_lun + 1) * sizeof(struct rts51x_status);
998 	chip->status = kzalloc(size, GFP_KERNEL);
999 	if (!chip->status)
1000 		goto INIT_FAIL;
1001 
1002 	for (i = 0; i <= (int)(chip->max_lun); i++) {
1003 		retval = rts51x_check_status(us, (u8) i);
1004 		if (retval < 0)
1005 			goto INIT_FAIL;
1006 	}
1007 
1008 	if (CHECK_FW_VER(chip, 0x5888) || CHECK_FW_VER(chip, 0x5889) ||
1009 	    CHECK_FW_VER(chip, 0x5901))
1010 		SET_AUTO_DELINK(chip);
1011 	if (STATUS_LEN(chip) == 16) {
1012 		if (SUPPORT_AUTO_DELINK(chip))
1013 			SET_AUTO_DELINK(chip);
1014 	}
1015 #ifdef CONFIG_REALTEK_AUTOPM
1016 	if (ss_en) {
1017 		chip->us = us;
1018 		realtek_cr_autosuspend_setup(us);
1019 	}
1020 #endif
1021 
1022 	usb_stor_dbg(us, "chip->flag = 0x%x\n", chip->flag);
1023 
1024 	(void)config_autodelink_after_power_on(us);
1025 
1026 	return 0;
1027 
1028 INIT_FAIL:
1029 	if (us->extra) {
1030 		kfree(chip->status);
1031 		kfree(us->extra);
1032 		us->extra = NULL;
1033 	}
1034 
1035 	return -EIO;
1036 }
1037 
1038 static int realtek_cr_probe(struct usb_interface *intf,
1039 			    const struct usb_device_id *id)
1040 {
1041 	struct us_data *us;
1042 	int result;
1043 
1044 	dev_dbg(&intf->dev, "Probe Realtek Card Reader!\n");
1045 
1046 	result = usb_stor_probe1(&us, intf, id,
1047 				 (id - realtek_cr_ids) +
1048 				 realtek_cr_unusual_dev_list);
1049 	if (result)
1050 		return result;
1051 
1052 	result = usb_stor_probe2(us);
1053 
1054 	return result;
1055 }
1056 
1057 static struct usb_driver realtek_cr_driver = {
1058 	.name = "ums-realtek",
1059 	.probe = realtek_cr_probe,
1060 	.disconnect = usb_stor_disconnect,
1061 	/* .suspend =      usb_stor_suspend, */
1062 	/* .resume =       usb_stor_resume, */
1063 	.reset_resume = usb_stor_reset_resume,
1064 	.suspend = realtek_cr_suspend,
1065 	.resume = realtek_cr_resume,
1066 	.pre_reset = usb_stor_pre_reset,
1067 	.post_reset = usb_stor_post_reset,
1068 	.id_table = realtek_cr_ids,
1069 	.soft_unbind = 1,
1070 	.supports_autosuspend = 1,
1071 	.no_dynamic_id = 1,
1072 };
1073 
1074 module_usb_driver(realtek_cr_driver);
1075