wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 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/workqueue.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/version.h> |
| 30 | |
| 31 | #include <scsi/scsi.h> |
| 32 | #include <scsi/scsi_cmnd.h> |
| 33 | #include <scsi/scsi_device.h> |
| 34 | #include <linux/cdrom.h> |
| 35 | |
| 36 | #include <linux/usb.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/usb_usual.h> |
| 39 | |
| 40 | #include "usb.h" |
| 41 | #include "transport.h" |
| 42 | #include "protocol.h" |
| 43 | #include "debug.h" |
| 44 | |
| 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"); |
| 49 | |
| 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"); |
| 53 | |
| 54 | struct rts51x_status { |
| 55 | u16 vid; |
| 56 | u16 pid; |
| 57 | u8 cur_lun; |
| 58 | u8 card_type; |
| 59 | u8 total_lun; |
| 60 | u16 fw_ver; |
| 61 | u8 phy_exist; |
| 62 | u8 multi_flag; |
| 63 | u8 multi_card; |
| 64 | u8 log_exist; |
| 65 | union { |
| 66 | u8 detailed_type1; |
| 67 | u8 detailed_type2; |
| 68 | } detailed_type; |
| 69 | u8 function[2]; |
| 70 | }; |
| 71 | |
| 72 | struct rts51x_chip { |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 73 | u16 vendor_id; |
| 74 | u16 product_id; |
| 75 | char max_lun; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 76 | |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 77 | struct rts51x_status *status; |
| 78 | int status_len; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 79 | |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 80 | u32 flag; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | /* flag definition */ |
| 84 | #define FLIDX_AUTO_DELINK 0x01 |
| 85 | |
| 86 | #define SCSI_LUN(srb) ((srb)->device->lun) |
| 87 | |
| 88 | /* Bit Operation */ |
| 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))) |
| 92 | |
| 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) |
| 96 | |
| 97 | #define RTS51X_GET_VID(chip) ((chip)->vendor_id) |
| 98 | #define RTS51X_GET_PID(chip) ((chip)->product_id) |
| 99 | |
| 100 | #define FW_VERSION(chip) ((chip)->status[0].fw_ver) |
| 101 | #define STATUS_LEN(chip) ((chip)->status_len) |
| 102 | |
| 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) |
| 116 | |
| 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))) |
| 121 | |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 122 | static int init_realtek_cr(struct us_data *us); |
| 123 | |
| 124 | /* |
| 125 | * The table of devices |
| 126 | */ |
| 127 | #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ |
| 128 | vendorName, productName, useProtocol, useTransport, \ |
| 129 | initFunction, flags) \ |
| 130 | {\ |
| 131 | USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ |
| 132 | .driver_info = (flags)|(USB_US_TYPE_STOR<<24)\ |
| 133 | } |
| 134 | |
wwang | 8a9e658 | 2011-02-15 17:02:47 +0800 | [diff] [blame] | 135 | static const struct usb_device_id realtek_cr_ids[] = { |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 136 | # include "unusual_realtek.h" |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 137 | {} /* Terminating entry */ |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 138 | }; |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 139 | |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 140 | MODULE_DEVICE_TABLE(usb, realtek_cr_ids); |
| 141 | |
| 142 | #undef UNUSUAL_DEV |
| 143 | |
| 144 | /* |
| 145 | * The flags table |
| 146 | */ |
| 147 | #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ |
| 148 | vendor_name, product_name, use_protocol, use_transport, \ |
| 149 | init_function, Flags) \ |
| 150 | { \ |
| 151 | .vendorName = vendor_name, \ |
| 152 | .productName = product_name, \ |
| 153 | .useProtocol = use_protocol, \ |
| 154 | .useTransport = use_transport, \ |
| 155 | .initFunction = init_function, \ |
| 156 | } |
| 157 | |
| 158 | static struct us_unusual_dev realtek_cr_unusual_dev_list[] = { |
| 159 | # include "unusual_realtek.h" |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 160 | {} /* Terminating entry */ |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | #undef UNUSUAL_DEV |
| 164 | |
| 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) |
| 168 | { |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 169 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *)us->iobuf; |
| 170 | struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *)us->iobuf; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 171 | int result; |
| 172 | unsigned int residue; |
| 173 | unsigned int cswlen; |
| 174 | unsigned int cbwlen = US_BULK_CB_WRAP_LEN; |
| 175 | |
| 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; |
| 181 | bcb->Lun = lun; |
| 182 | bcb->Length = cmd_len; |
| 183 | |
| 184 | /* copy the command payload */ |
| 185 | memset(bcb->CDB, 0, sizeof(bcb->CDB)); |
| 186 | memcpy(bcb->CDB, cmd, bcb->Length); |
| 187 | |
| 188 | /* send it to out endpoint */ |
| 189 | result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 190 | bcb, cbwlen, NULL); |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 191 | if (result != USB_STOR_XFER_GOOD) |
| 192 | return USB_STOR_TRANSPORT_ERROR; |
| 193 | |
| 194 | /* DATA STAGE */ |
| 195 | /* send/receive data payload, if there is any */ |
| 196 | |
| 197 | if (buf && buf_len) { |
| 198 | unsigned int pipe = (dir == DMA_FROM_DEVICE) ? |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 199 | us->recv_bulk_pipe : us->send_bulk_pipe; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 200 | result = usb_stor_bulk_transfer_buf(us, pipe, |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 201 | buf, buf_len, NULL); |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 202 | if (result == USB_STOR_XFER_ERROR) |
| 203 | return USB_STOR_TRANSPORT_ERROR; |
| 204 | } |
| 205 | |
| 206 | /* get CSW for device status */ |
| 207 | result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 208 | bcs, US_BULK_CS_WRAP_LEN, &cswlen); |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 209 | if (result != USB_STOR_XFER_GOOD) |
| 210 | return USB_STOR_TRANSPORT_ERROR; |
| 211 | |
| 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", |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 215 | le32_to_cpu(bcs->Signature), US_BULK_CS_SIGN); |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 216 | return USB_STOR_TRANSPORT_ERROR; |
| 217 | } |
| 218 | |
| 219 | residue = bcs->Residue; |
| 220 | if (bcs->Tag != us->tag) |
| 221 | return USB_STOR_TRANSPORT_ERROR; |
| 222 | |
| 223 | /* try to compute the actual residue, based on how much data |
| 224 | * was really transferred and what the device tells us */ |
| 225 | if (residue) |
| 226 | residue = residue < buf_len ? residue : buf_len; |
| 227 | |
| 228 | if (act_len) |
| 229 | *act_len = buf_len - residue; |
| 230 | |
| 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; |
| 236 | |
| 237 | case US_BULK_STAT_FAIL: |
| 238 | /* command failed */ |
| 239 | return USB_STOR_TRANSPORT_FAILED; |
| 240 | |
| 241 | case US_BULK_STAT_PHASE: |
| 242 | /* phase error -- note that a transport reset will be |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 243 | * invoked by the invoke_transport() function |
| 244 | */ |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 245 | return USB_STOR_TRANSPORT_ERROR; |
| 246 | } |
| 247 | |
| 248 | /* we should never get here, but if we do, we're in trouble */ |
| 249 | return USB_STOR_TRANSPORT_ERROR; |
| 250 | } |
| 251 | |
| 252 | /* Determine what the maximum LUN supported is */ |
| 253 | static int rts51x_get_max_lun(struct us_data *us) |
| 254 | { |
| 255 | int result; |
| 256 | |
| 257 | /* issue the command */ |
| 258 | us->iobuf[0] = 0; |
| 259 | result = usb_stor_control_msg(us, us->recv_ctrl_pipe, |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 260 | US_BULK_GET_MAX_LUN, |
| 261 | USB_DIR_IN | USB_TYPE_CLASS | |
| 262 | USB_RECIP_INTERFACE, |
| 263 | 0, us->ifnum, us->iobuf, 1, 10 * HZ); |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 264 | |
| 265 | US_DEBUGP("GetMaxLUN command result is %d, data is %d\n", |
| 266 | result, us->iobuf[0]); |
| 267 | |
| 268 | /* if we have a successful request, return the result */ |
| 269 | if (result > 0) |
| 270 | return us->iobuf[0]; |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len) |
| 276 | { |
| 277 | int retval; |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 278 | u8 cmnd[12] = { 0 }; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 279 | |
| 280 | US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len); |
| 281 | |
| 282 | cmnd[0] = 0xF0; |
| 283 | cmnd[1] = 0x0D; |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 284 | cmnd[2] = (u8) (addr >> 8); |
| 285 | cmnd[3] = (u8) addr; |
| 286 | cmnd[4] = (u8) (len >> 8); |
| 287 | cmnd[5] = (u8) len; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 288 | |
| 289 | retval = rts51x_bulk_transport(us, 0, cmnd, 12, |
| 290 | data, len, DMA_FROM_DEVICE, NULL); |
| 291 | if (retval != USB_STOR_TRANSPORT_GOOD) |
| 292 | return -EIO; |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len) |
| 298 | { |
| 299 | int retval; |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 300 | u8 cmnd[12] = { 0 }; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 301 | |
| 302 | US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len); |
| 303 | |
| 304 | cmnd[0] = 0xF0; |
| 305 | cmnd[1] = 0x0E; |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 306 | cmnd[2] = (u8) (addr >> 8); |
| 307 | cmnd[3] = (u8) addr; |
| 308 | cmnd[4] = (u8) (len >> 8); |
| 309 | cmnd[5] = (u8) len; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 310 | |
| 311 | retval = rts51x_bulk_transport(us, 0, cmnd, 12, |
| 312 | data, len, DMA_TO_DEVICE, NULL); |
| 313 | if (retval != USB_STOR_TRANSPORT_GOOD) |
| 314 | return -EIO; |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int rts51x_read_status(struct us_data *us, |
| 320 | u8 lun, u8 *status, int len, int *actlen) |
| 321 | { |
| 322 | int retval; |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 323 | u8 cmnd[12] = { 0 }; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 324 | |
| 325 | US_DEBUGP("%s, lun = %d\n", __func__, lun); |
| 326 | |
| 327 | cmnd[0] = 0xF0; |
| 328 | cmnd[1] = 0x09; |
| 329 | |
| 330 | retval = rts51x_bulk_transport(us, lun, cmnd, 12, |
| 331 | status, len, DMA_FROM_DEVICE, actlen); |
| 332 | if (retval != USB_STOR_TRANSPORT_GOOD) |
| 333 | return -EIO; |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int rts51x_check_status(struct us_data *us, u8 lun) |
| 339 | { |
| 340 | struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra); |
| 341 | int retval; |
| 342 | u8 buf[16]; |
| 343 | |
| 344 | retval = rts51x_read_status(us, lun, buf, 16, &(chip->status_len)); |
| 345 | if (retval < 0) |
| 346 | return -EIO; |
| 347 | |
| 348 | US_DEBUGP("chip->status_len = %d\n", chip->status_len); |
| 349 | |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 350 | chip->status[lun].vid = ((u16) buf[0] << 8) | buf[1]; |
| 351 | chip->status[lun].pid = ((u16) buf[2] << 8) | buf[3]; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 352 | chip->status[lun].cur_lun = buf[4]; |
| 353 | chip->status[lun].card_type = buf[5]; |
| 354 | chip->status[lun].total_lun = buf[6]; |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 355 | chip->status[lun].fw_ver = ((u16) buf[7] << 8) | buf[8]; |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 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]; |
| 364 | } |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int enable_oscillator(struct us_data *us) |
| 370 | { |
| 371 | int retval; |
| 372 | u8 value; |
| 373 | |
| 374 | retval = rts51x_read_mem(us, 0xFE77, &value, 1); |
| 375 | if (retval < 0) |
| 376 | return -EIO; |
| 377 | |
| 378 | value |= 0x04; |
| 379 | retval = rts51x_write_mem(us, 0xFE77, &value, 1); |
| 380 | if (retval < 0) |
| 381 | return -EIO; |
| 382 | |
| 383 | retval = rts51x_read_mem(us, 0xFE77, &value, 1); |
| 384 | if (retval < 0) |
| 385 | return -EIO; |
| 386 | |
| 387 | if (!(value & 0x04)) |
| 388 | return -EIO; |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int do_config_autodelink(struct us_data *us, int enable, int force) |
| 394 | { |
| 395 | int retval; |
| 396 | u8 value; |
| 397 | |
| 398 | retval = rts51x_read_mem(us, 0xFE47, &value, 1); |
| 399 | if (retval < 0) |
| 400 | return -EIO; |
| 401 | |
| 402 | if (enable) { |
| 403 | if (force) |
| 404 | value |= 0x03; |
| 405 | else |
| 406 | value |= 0x01; |
| 407 | } else { |
| 408 | value &= ~0x03; |
| 409 | } |
| 410 | |
| 411 | US_DEBUGP("In %s,set 0xfe47 to 0x%x\n", __func__, value); |
| 412 | |
| 413 | retval = rts51x_write_mem(us, 0xFE47, &value, 1); |
| 414 | if (retval < 0) |
| 415 | return -EIO; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static int config_autodelink_after_power_on(struct us_data *us) |
| 421 | { |
| 422 | struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra); |
| 423 | int retval; |
| 424 | u8 value; |
| 425 | |
| 426 | if (!CHK_AUTO_DELINK(chip)) |
| 427 | return 0; |
| 428 | |
| 429 | retval = rts51x_read_mem(us, 0xFE47, &value, 1); |
| 430 | if (retval < 0) |
| 431 | return -EIO; |
| 432 | |
| 433 | if (auto_delink_en) { |
| 434 | CLR_BIT(value, 0); |
| 435 | CLR_BIT(value, 1); |
| 436 | SET_BIT(value, 2); |
| 437 | |
| 438 | if (CHECK_ID(chip, 0x0138, 0x3882)) |
| 439 | CLR_BIT(value, 2); |
| 440 | |
| 441 | SET_BIT(value, 7); |
| 442 | |
| 443 | retval = rts51x_write_mem(us, 0xFE47, &value, 1); |
| 444 | if (retval < 0) |
| 445 | return -EIO; |
| 446 | |
| 447 | retval = enable_oscillator(us); |
| 448 | if (retval == 0) |
| 449 | (void)do_config_autodelink(us, 1, 0); |
| 450 | } else { |
| 451 | /* Autodelink controlled by firmware */ |
| 452 | |
| 453 | SET_BIT(value, 2); |
| 454 | |
| 455 | if (CHECK_ID(chip, 0x0138, 0x3882)) |
| 456 | CLR_BIT(value, 2); |
| 457 | |
| 458 | if (CHECK_ID(chip, 0x0159, 0x5889) || |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 459 | CHECK_ID(chip, 0x0138, 0x3880)) { |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 460 | CLR_BIT(value, 0); |
| 461 | CLR_BIT(value, 7); |
| 462 | } |
| 463 | |
| 464 | retval = rts51x_write_mem(us, 0xFE47, &value, 1); |
| 465 | if (retval < 0) |
| 466 | return -EIO; |
| 467 | |
| 468 | if (CHECK_ID(chip, 0x0159, 0x5888)) { |
| 469 | value = 0xFF; |
| 470 | retval = rts51x_write_mem(us, 0xFE79, &value, 1); |
| 471 | if (retval < 0) |
| 472 | return -EIO; |
| 473 | |
| 474 | value = 0x01; |
| 475 | retval = rts51x_write_mem(us, 0x48, &value, 1); |
| 476 | if (retval < 0) |
| 477 | return -EIO; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static int config_autodelink_before_power_down(struct us_data *us) |
| 485 | { |
| 486 | struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra); |
| 487 | int retval; |
| 488 | u8 value; |
| 489 | |
| 490 | if (!CHK_AUTO_DELINK(chip)) |
| 491 | return 0; |
| 492 | |
| 493 | if (auto_delink_en) { |
| 494 | retval = rts51x_read_mem(us, 0xFE77, &value, 1); |
| 495 | if (retval < 0) |
| 496 | return -EIO; |
| 497 | |
| 498 | SET_BIT(value, 2); |
| 499 | retval = rts51x_write_mem(us, 0xFE77, &value, 1); |
| 500 | if (retval < 0) |
| 501 | return -EIO; |
| 502 | |
| 503 | if (CHECK_ID(chip, 0x0159, 0x5888)) { |
| 504 | value = 0x01; |
| 505 | retval = rts51x_write_mem(us, 0x48, &value, 1); |
| 506 | if (retval < 0) |
| 507 | return -EIO; |
| 508 | } |
| 509 | |
| 510 | retval = rts51x_read_mem(us, 0xFE47, &value, 1); |
| 511 | if (retval < 0) |
| 512 | return -EIO; |
| 513 | |
| 514 | SET_BIT(value, 0); |
| 515 | if (CHECK_ID(chip, 0x0138, 0x3882)) |
| 516 | SET_BIT(value, 2); |
| 517 | retval = rts51x_write_mem(us, 0xFE77, &value, 1); |
| 518 | if (retval < 0) |
| 519 | return -EIO; |
| 520 | } else { |
| 521 | if (CHECK_ID(chip, 0x0159, 0x5889) || |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 522 | CHECK_ID(chip, 0x0138, 0x3880) || |
| 523 | CHECK_ID(chip, 0x0138, 0x3882)) { |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 524 | retval = rts51x_read_mem(us, 0xFE47, &value, 1); |
| 525 | if (retval < 0) |
| 526 | return -EIO; |
| 527 | |
| 528 | if (CHECK_ID(chip, 0x0159, 0x5889) || |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 529 | CHECK_ID(chip, 0x0138, 0x3880)) { |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 530 | SET_BIT(value, 0); |
| 531 | SET_BIT(value, 7); |
| 532 | } |
| 533 | |
| 534 | if (CHECK_ID(chip, 0x0138, 0x3882)) |
| 535 | SET_BIT(value, 2); |
| 536 | |
| 537 | retval = rts51x_write_mem(us, 0xFE47, &value, 1); |
| 538 | if (retval < 0) |
| 539 | return -EIO; |
| 540 | } |
| 541 | |
| 542 | if (CHECK_ID(chip, 0x0159, 0x5888)) { |
| 543 | value = 0x01; |
| 544 | retval = rts51x_write_mem(us, 0x48, &value, 1); |
| 545 | if (retval < 0) |
| 546 | return -EIO; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static void realtek_cr_destructor(void *extra) |
| 554 | { |
| 555 | struct rts51x_chip *chip = (struct rts51x_chip *)extra; |
| 556 | |
| 557 | if (!chip) |
| 558 | return; |
| 559 | |
| 560 | kfree(chip->status); |
| 561 | } |
| 562 | |
| 563 | #ifdef CONFIG_PM |
wwang | 9812f74 | 2011-02-15 09:38:31 +0800 | [diff] [blame] | 564 | static void realtek_pm_hook(struct us_data *us, int pm_state) |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 565 | { |
| 566 | if (pm_state == US_SUSPEND) |
| 567 | (void)config_autodelink_before_power_down(us); |
| 568 | } |
| 569 | #endif |
| 570 | |
| 571 | static int init_realtek_cr(struct us_data *us) |
| 572 | { |
| 573 | struct rts51x_chip *chip; |
| 574 | int size, i, retval; |
| 575 | |
| 576 | chip = kzalloc(sizeof(struct rts51x_chip), GFP_KERNEL); |
| 577 | if (!chip) |
| 578 | return -ENOMEM; |
| 579 | |
| 580 | us->extra = chip; |
| 581 | us->extra_destructor = realtek_cr_destructor; |
| 582 | #ifdef CONFIG_PM |
| 583 | us->suspend_resume_hook = realtek_pm_hook; |
| 584 | #endif |
| 585 | |
| 586 | us->max_lun = chip->max_lun = rts51x_get_max_lun(us); |
| 587 | |
| 588 | US_DEBUGP("chip->max_lun = %d\n", chip->max_lun); |
| 589 | |
| 590 | size = (chip->max_lun + 1) * sizeof(struct rts51x_status); |
| 591 | chip->status = kzalloc(size, GFP_KERNEL); |
| 592 | if (!chip->status) |
| 593 | goto INIT_FAIL; |
| 594 | |
| 595 | for (i = 0; i <= (int)(chip->max_lun); i++) { |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 596 | retval = rts51x_check_status(us, (u8) i); |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 597 | if (retval < 0) |
| 598 | goto INIT_FAIL; |
| 599 | } |
| 600 | |
| 601 | if (CHECK_FW_VER(chip, 0x5888) || CHECK_FW_VER(chip, 0x5889) || |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 602 | CHECK_FW_VER(chip, 0x5901)) |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 603 | SET_AUTO_DELINK(chip); |
| 604 | if (STATUS_LEN(chip) == 16) { |
| 605 | if (SUPPORT_AUTO_DELINK(chip)) |
| 606 | SET_AUTO_DELINK(chip); |
| 607 | } |
| 608 | |
| 609 | US_DEBUGP("chip->flag = 0x%x\n", chip->flag); |
| 610 | |
| 611 | (void)config_autodelink_after_power_on(us); |
| 612 | |
| 613 | return 0; |
| 614 | |
| 615 | INIT_FAIL: |
| 616 | if (us->extra) { |
| 617 | kfree(chip->status); |
| 618 | kfree(us->extra); |
| 619 | us->extra = NULL; |
| 620 | } |
| 621 | |
| 622 | return -EIO; |
| 623 | } |
| 624 | |
| 625 | static int realtek_cr_probe(struct usb_interface *intf, |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 626 | const struct usb_device_id *id) |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 627 | { |
| 628 | struct us_data *us; |
| 629 | int result; |
| 630 | |
| 631 | US_DEBUGP("Probe Realtek Card Reader!\n"); |
| 632 | |
| 633 | result = usb_stor_probe1(&us, intf, id, |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 634 | (id - realtek_cr_ids) + |
| 635 | realtek_cr_unusual_dev_list); |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 636 | if (result) |
| 637 | return result; |
| 638 | |
| 639 | result = usb_stor_probe2(us); |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 640 | |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 641 | return result; |
| 642 | } |
| 643 | |
| 644 | static struct usb_driver realtek_cr_driver = { |
edwin_rong | 151d9fad | 2011-06-17 19:35:09 +0800 | [diff] [blame] | 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, |
| 654 | .soft_unbind = 1, |
wwang | 50a6cb9 | 2011-01-14 16:53:34 +0800 | [diff] [blame] | 655 | }; |
| 656 | |
| 657 | static int __init realtek_cr_init(void) |
| 658 | { |
| 659 | return usb_register(&realtek_cr_driver); |
| 660 | } |
| 661 | |
| 662 | static void __exit realtek_cr_exit(void) |
| 663 | { |
| 664 | usb_deregister(&realtek_cr_driver); |
| 665 | } |
| 666 | |
| 667 | module_init(realtek_cr_init); |
| 668 | module_exit(realtek_cr_exit); |