1 /* Driver for Realtek RTS51xx USB card reader
3 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 * wwang (wei_wang@realsil.com.cn)
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
23 #include <linux/module.h>
24 #include <linux/blkdev.h>
25 #include <linux/kthread.h>
26 #include <linux/sched.h>
27 #include <linux/workqueue.h>
28 #include <linux/kernel.h>
29 #include <linux/version.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_device.h>
34 #include <linux/cdrom.h>
36 #include <linux/usb.h>
37 #include <linux/slab.h>
38 #include <linux/usb_usual.h>
41 #include "transport.h"
45 MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
46 MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
47 MODULE_LICENSE("GPL");
48 MODULE_VERSION("1.03");
50 static int auto_delink_en
= 1;
51 module_param(auto_delink_en
, int, S_IRUGO
| S_IWUSR
);
52 MODULE_PARM_DESC(auto_delink_en
, "enable auto delink");
54 struct rts51x_status
{
77 struct rts51x_status
*status
;
84 #define FLIDX_AUTO_DELINK 0x01
86 #define SCSI_LUN(srb) ((srb)->device->lun)
89 #define SET_BIT(data, idx) ((data) |= 1 << (idx))
90 #define CLR_BIT(data, idx) ((data) &= ~(1 << (idx)))
91 #define CHK_BIT(data, idx) ((data) & (1 << (idx)))
93 #define SET_AUTO_DELINK(chip) ((chip)->flag |= FLIDX_AUTO_DELINK)
94 #define CLR_AUTO_DELINK(chip) ((chip)->flag &= ~FLIDX_AUTO_DELINK)
95 #define CHK_AUTO_DELINK(chip) ((chip)->flag & FLIDX_AUTO_DELINK)
97 #define RTS51X_GET_VID(chip) ((chip)->vendor_id)
98 #define RTS51X_GET_PID(chip) ((chip)->product_id)
100 #define FW_VERSION(chip) ((chip)->status[0].fw_ver)
101 #define STATUS_LEN(chip) ((chip)->status_len)
103 /* Check card reader function */
104 #define SUPPORT_DETAILED_TYPE1(chip) \
105 CHK_BIT((chip)->status[0].function[0], 1)
106 #define SUPPORT_OT(chip) \
107 CHK_BIT((chip)->status[0].function[0], 2)
108 #define SUPPORT_OC(chip) \
109 CHK_BIT((chip)->status[0].function[0], 3)
110 #define SUPPORT_AUTO_DELINK(chip) \
111 CHK_BIT((chip)->status[0].function[0], 4)
112 #define SUPPORT_SDIO(chip) \
113 CHK_BIT((chip)->status[0].function[1], 0)
114 #define SUPPORT_DETAILED_TYPE2(chip) \
115 CHK_BIT((chip)->status[0].function[1], 1)
117 #define CHECK_PID(chip, pid) (RTS51X_GET_PID(chip) == (pid))
118 #define CHECK_FW_VER(chip, fw_ver) (FW_VERSION(chip) == (fw_ver))
119 #define CHECK_ID(chip, pid, fw_ver) \
120 (CHECK_PID((chip), (pid)) && CHECK_FW_VER((chip), (fw_ver)))
122 static int init_realtek_cr(struct us_data
*us
);
125 * The table of devices
127 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
128 vendorName, productName, useProtocol, useTransport, \
129 initFunction, flags) \
131 USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
132 .driver_info = (flags)|(USB_US_TYPE_STOR<<24)\
135 static const struct usb_device_id realtek_cr_ids
[] = {
136 # include "unusual_realtek.h"
137 {} /* Terminating entry */
140 MODULE_DEVICE_TABLE(usb
, realtek_cr_ids
);
147 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
148 vendor_name, product_name, use_protocol, use_transport, \
149 init_function, Flags) \
151 .vendorName = vendor_name, \
152 .productName = product_name, \
153 .useProtocol = use_protocol, \
154 .useTransport = use_transport, \
155 .initFunction = init_function, \
158 static struct us_unusual_dev realtek_cr_unusual_dev_list
[] = {
159 # include "unusual_realtek.h"
160 {} /* Terminating entry */
165 static int rts51x_bulk_transport(struct us_data
*us
, u8 lun
,
166 u8
*cmd
, int cmd_len
, u8
*buf
, int buf_len
,
167 enum dma_data_direction dir
, int *act_len
)
169 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*)us
->iobuf
;
170 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*)us
->iobuf
;
172 unsigned int residue
;
174 unsigned int cbwlen
= US_BULK_CB_WRAP_LEN
;
176 /* set up the command wrapper */
177 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
178 bcb
->DataTransferLength
= cpu_to_le32(buf_len
);
179 bcb
->Flags
= (dir
== DMA_FROM_DEVICE
) ? 1 << 7 : 0;
180 bcb
->Tag
= ++us
->tag
;
182 bcb
->Length
= cmd_len
;
184 /* copy the command payload */
185 memset(bcb
->CDB
, 0, sizeof(bcb
->CDB
));
186 memcpy(bcb
->CDB
, cmd
, bcb
->Length
);
188 /* send it to out endpoint */
189 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
191 if (result
!= USB_STOR_XFER_GOOD
)
192 return USB_STOR_TRANSPORT_ERROR
;
195 /* send/receive data payload, if there is any */
197 if (buf
&& buf_len
) {
198 unsigned int pipe
= (dir
== DMA_FROM_DEVICE
) ?
199 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
200 result
= usb_stor_bulk_transfer_buf(us
, pipe
,
202 if (result
== USB_STOR_XFER_ERROR
)
203 return USB_STOR_TRANSPORT_ERROR
;
206 /* get CSW for device status */
207 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
208 bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
209 if (result
!= USB_STOR_XFER_GOOD
)
210 return USB_STOR_TRANSPORT_ERROR
;
212 /* check bulk status */
213 if (bcs
->Signature
!= cpu_to_le32(US_BULK_CS_SIGN
)) {
214 US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
215 le32_to_cpu(bcs
->Signature
), US_BULK_CS_SIGN
);
216 return USB_STOR_TRANSPORT_ERROR
;
219 residue
= bcs
->Residue
;
220 if (bcs
->Tag
!= us
->tag
)
221 return USB_STOR_TRANSPORT_ERROR
;
223 /* try to compute the actual residue, based on how much data
224 * was really transferred and what the device tells us */
226 residue
= residue
< buf_len
? residue
: buf_len
;
229 *act_len
= buf_len
- residue
;
231 /* based on the status code, we report good or bad */
232 switch (bcs
->Status
) {
233 case US_BULK_STAT_OK
:
234 /* command good -- note that data could be short */
235 return USB_STOR_TRANSPORT_GOOD
;
237 case US_BULK_STAT_FAIL
:
239 return USB_STOR_TRANSPORT_FAILED
;
241 case US_BULK_STAT_PHASE
:
242 /* phase error -- note that a transport reset will be
243 * invoked by the invoke_transport() function
245 return USB_STOR_TRANSPORT_ERROR
;
248 /* we should never get here, but if we do, we're in trouble */
249 return USB_STOR_TRANSPORT_ERROR
;
252 /* Determine what the maximum LUN supported is */
253 static int rts51x_get_max_lun(struct us_data
*us
)
257 /* issue the command */
259 result
= usb_stor_control_msg(us
, us
->recv_ctrl_pipe
,
261 USB_DIR_IN
| USB_TYPE_CLASS
|
263 0, us
->ifnum
, us
->iobuf
, 1, 10 * HZ
);
265 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
266 result
, us
->iobuf
[0]);
268 /* if we have a successful request, return the result */
275 static int rts51x_read_mem(struct us_data
*us
, u16 addr
, u8
*data
, u16 len
)
280 US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__
, addr
, len
);
284 cmnd
[2] = (u8
) (addr
>> 8);
286 cmnd
[4] = (u8
) (len
>> 8);
289 retval
= rts51x_bulk_transport(us
, 0, cmnd
, 12,
290 data
, len
, DMA_FROM_DEVICE
, NULL
);
291 if (retval
!= USB_STOR_TRANSPORT_GOOD
)
297 static int rts51x_write_mem(struct us_data
*us
, u16 addr
, u8
*data
, u16 len
)
302 US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__
, addr
, len
);
306 cmnd
[2] = (u8
) (addr
>> 8);
308 cmnd
[4] = (u8
) (len
>> 8);
311 retval
= rts51x_bulk_transport(us
, 0, cmnd
, 12,
312 data
, len
, DMA_TO_DEVICE
, NULL
);
313 if (retval
!= USB_STOR_TRANSPORT_GOOD
)
319 static int rts51x_read_status(struct us_data
*us
,
320 u8 lun
, u8
*status
, int len
, int *actlen
)
325 US_DEBUGP("%s, lun = %d\n", __func__
, lun
);
330 retval
= rts51x_bulk_transport(us
, lun
, cmnd
, 12,
331 status
, len
, DMA_FROM_DEVICE
, actlen
);
332 if (retval
!= USB_STOR_TRANSPORT_GOOD
)
338 static int rts51x_check_status(struct us_data
*us
, u8 lun
)
340 struct rts51x_chip
*chip
= (struct rts51x_chip
*)(us
->extra
);
344 retval
= rts51x_read_status(us
, lun
, buf
, 16, &(chip
->status_len
));
348 US_DEBUGP("chip->status_len = %d\n", chip
->status_len
);
350 chip
->status
[lun
].vid
= ((u16
) buf
[0] << 8) | buf
[1];
351 chip
->status
[lun
].pid
= ((u16
) buf
[2] << 8) | buf
[3];
352 chip
->status
[lun
].cur_lun
= buf
[4];
353 chip
->status
[lun
].card_type
= buf
[5];
354 chip
->status
[lun
].total_lun
= buf
[6];
355 chip
->status
[lun
].fw_ver
= ((u16
) buf
[7] << 8) | buf
[8];
356 chip
->status
[lun
].phy_exist
= buf
[9];
357 chip
->status
[lun
].multi_flag
= buf
[10];
358 chip
->status
[lun
].multi_card
= buf
[11];
359 chip
->status
[lun
].log_exist
= buf
[12];
360 if (chip
->status_len
== 16) {
361 chip
->status
[lun
].detailed_type
.detailed_type1
= buf
[13];
362 chip
->status
[lun
].function
[0] = buf
[14];
363 chip
->status
[lun
].function
[1] = buf
[15];
369 static int enable_oscillator(struct us_data
*us
)
374 retval
= rts51x_read_mem(us
, 0xFE77, &value
, 1);
379 retval
= rts51x_write_mem(us
, 0xFE77, &value
, 1);
383 retval
= rts51x_read_mem(us
, 0xFE77, &value
, 1);
393 static int do_config_autodelink(struct us_data
*us
, int enable
, int force
)
398 retval
= rts51x_read_mem(us
, 0xFE47, &value
, 1);
411 US_DEBUGP("In %s,set 0xfe47 to 0x%x\n", __func__
, value
);
413 retval
= rts51x_write_mem(us
, 0xFE47, &value
, 1);
420 static int config_autodelink_after_power_on(struct us_data
*us
)
422 struct rts51x_chip
*chip
= (struct rts51x_chip
*)(us
->extra
);
426 if (!CHK_AUTO_DELINK(chip
))
429 retval
= rts51x_read_mem(us
, 0xFE47, &value
, 1);
433 if (auto_delink_en
) {
438 if (CHECK_ID(chip
, 0x0138, 0x3882))
443 retval
= rts51x_write_mem(us
, 0xFE47, &value
, 1);
447 retval
= enable_oscillator(us
);
449 (void)do_config_autodelink(us
, 1, 0);
451 /* Autodelink controlled by firmware */
455 if (CHECK_ID(chip
, 0x0138, 0x3882))
458 if (CHECK_ID(chip
, 0x0159, 0x5889) ||
459 CHECK_ID(chip
, 0x0138, 0x3880)) {
464 retval
= rts51x_write_mem(us
, 0xFE47, &value
, 1);
468 if (CHECK_ID(chip
, 0x0159, 0x5888)) {
470 retval
= rts51x_write_mem(us
, 0xFE79, &value
, 1);
475 retval
= rts51x_write_mem(us
, 0x48, &value
, 1);
484 static int config_autodelink_before_power_down(struct us_data
*us
)
486 struct rts51x_chip
*chip
= (struct rts51x_chip
*)(us
->extra
);
490 if (!CHK_AUTO_DELINK(chip
))
493 if (auto_delink_en
) {
494 retval
= rts51x_read_mem(us
, 0xFE77, &value
, 1);
499 retval
= rts51x_write_mem(us
, 0xFE77, &value
, 1);
503 if (CHECK_ID(chip
, 0x0159, 0x5888)) {
505 retval
= rts51x_write_mem(us
, 0x48, &value
, 1);
510 retval
= rts51x_read_mem(us
, 0xFE47, &value
, 1);
515 if (CHECK_ID(chip
, 0x0138, 0x3882))
517 retval
= rts51x_write_mem(us
, 0xFE77, &value
, 1);
521 if (CHECK_ID(chip
, 0x0159, 0x5889) ||
522 CHECK_ID(chip
, 0x0138, 0x3880) ||
523 CHECK_ID(chip
, 0x0138, 0x3882)) {
524 retval
= rts51x_read_mem(us
, 0xFE47, &value
, 1);
528 if (CHECK_ID(chip
, 0x0159, 0x5889) ||
529 CHECK_ID(chip
, 0x0138, 0x3880)) {
534 if (CHECK_ID(chip
, 0x0138, 0x3882))
537 retval
= rts51x_write_mem(us
, 0xFE47, &value
, 1);
542 if (CHECK_ID(chip
, 0x0159, 0x5888)) {
544 retval
= rts51x_write_mem(us
, 0x48, &value
, 1);
553 static void realtek_cr_destructor(void *extra
)
555 struct rts51x_chip
*chip
= (struct rts51x_chip
*)extra
;
564 static void realtek_pm_hook(struct us_data
*us
, int pm_state
)
566 if (pm_state
== US_SUSPEND
)
567 (void)config_autodelink_before_power_down(us
);
571 static int init_realtek_cr(struct us_data
*us
)
573 struct rts51x_chip
*chip
;
576 chip
= kzalloc(sizeof(struct rts51x_chip
), GFP_KERNEL
);
581 us
->extra_destructor
= realtek_cr_destructor
;
583 us
->suspend_resume_hook
= realtek_pm_hook
;
586 us
->max_lun
= chip
->max_lun
= rts51x_get_max_lun(us
);
588 US_DEBUGP("chip->max_lun = %d\n", chip
->max_lun
);
590 size
= (chip
->max_lun
+ 1) * sizeof(struct rts51x_status
);
591 chip
->status
= kzalloc(size
, GFP_KERNEL
);
595 for (i
= 0; i
<= (int)(chip
->max_lun
); i
++) {
596 retval
= rts51x_check_status(us
, (u8
) i
);
601 if (CHECK_FW_VER(chip
, 0x5888) || CHECK_FW_VER(chip
, 0x5889) ||
602 CHECK_FW_VER(chip
, 0x5901))
603 SET_AUTO_DELINK(chip
);
604 if (STATUS_LEN(chip
) == 16) {
605 if (SUPPORT_AUTO_DELINK(chip
))
606 SET_AUTO_DELINK(chip
);
609 US_DEBUGP("chip->flag = 0x%x\n", chip
->flag
);
611 (void)config_autodelink_after_power_on(us
);
625 static int realtek_cr_probe(struct usb_interface
*intf
,
626 const struct usb_device_id
*id
)
631 US_DEBUGP("Probe Realtek Card Reader!\n");
633 result
= usb_stor_probe1(&us
, intf
, id
,
634 (id
- realtek_cr_ids
) +
635 realtek_cr_unusual_dev_list
);
639 result
= usb_stor_probe2(us
);
644 static struct usb_driver realtek_cr_driver
= {
645 .name
= "ums-realtek",
646 .probe
= realtek_cr_probe
,
647 .disconnect
= usb_stor_disconnect
,
648 .suspend
= usb_stor_suspend
,
649 .resume
= usb_stor_resume
,
650 .reset_resume
= usb_stor_reset_resume
,
651 .pre_reset
= usb_stor_pre_reset
,
652 .post_reset
= usb_stor_post_reset
,
653 .id_table
= realtek_cr_ids
,
657 static int __init
realtek_cr_init(void)
659 return usb_register(&realtek_cr_driver
);
662 static void __exit
realtek_cr_exit(void)
664 usb_deregister(&realtek_cr_driver
);
667 module_init(realtek_cr_init
);
668 module_exit(realtek_cr_exit
);