Realtek cr: Remove unused Macros
[zen-stable.git] / drivers / usb / storage / realtek_cr.c
blob2ea0da23306a071116d6a7662bc9777b61cff50f
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
8 * later version.
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/>.
18 * Author:
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>
40 #include "usb.h"
41 #include "transport.h"
42 #include "protocol.h"
43 #include "debug.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 {
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];
72 struct rts51x_chip {
73 u16 vendor_id;
74 u16 product_id;
75 char max_lun;
77 struct rts51x_status *status;
78 int status_len;
80 u32 flag;
83 /* flag definition */
84 #define FLIDX_AUTO_DELINK 0x01
86 #define SCSI_LUN(srb) ((srb)->device->lun)
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)))
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);
142 #undef UNUSUAL_DEV
145 * The flags table
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 */
163 #undef UNUSUAL_DEV
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;
171 int result;
172 unsigned int residue;
173 unsigned int cswlen;
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;
181 bcb->Lun = lun;
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,
190 bcb, cbwlen, NULL);
191 if (result != USB_STOR_XFER_GOOD)
192 return USB_STOR_TRANSPORT_ERROR;
194 /* DATA STAGE */
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,
201 buf, buf_len, NULL);
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 */
225 if (residue)
226 residue = residue < buf_len ? residue : buf_len;
228 if (act_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:
238 /* command failed */
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)
255 int result;
257 /* issue the command */
258 us->iobuf[0] = 0;
259 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
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);
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 */
269 if (result > 0)
270 return us->iobuf[0];
272 return 0;
275 static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
277 int retval;
278 u8 cmnd[12] = { 0 };
280 US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
282 cmnd[0] = 0xF0;
283 cmnd[1] = 0x0D;
284 cmnd[2] = (u8) (addr >> 8);
285 cmnd[3] = (u8) addr;
286 cmnd[4] = (u8) (len >> 8);
287 cmnd[5] = (u8) len;
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;
294 return 0;
297 static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
299 int retval;
300 u8 cmnd[12] = { 0 };
302 US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
304 cmnd[0] = 0xF0;
305 cmnd[1] = 0x0E;
306 cmnd[2] = (u8) (addr >> 8);
307 cmnd[3] = (u8) addr;
308 cmnd[4] = (u8) (len >> 8);
309 cmnd[5] = (u8) len;
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;
316 return 0;
319 static int rts51x_read_status(struct us_data *us,
320 u8 lun, u8 *status, int len, int *actlen)
322 int retval;
323 u8 cmnd[12] = { 0 };
325 US_DEBUGP("%s, lun = %d\n", __func__, lun);
327 cmnd[0] = 0xF0;
328 cmnd[1] = 0x09;
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;
335 return 0;
338 static int rts51x_check_status(struct us_data *us, u8 lun)
340 struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
341 int retval;
342 u8 buf[16];
344 retval = rts51x_read_status(us, lun, buf, 16, &(chip->status_len));
345 if (retval < 0)
346 return -EIO;
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];
366 return 0;
369 static int enable_oscillator(struct us_data *us)
371 int retval;
372 u8 value;
374 retval = rts51x_read_mem(us, 0xFE77, &value, 1);
375 if (retval < 0)
376 return -EIO;
378 value |= 0x04;
379 retval = rts51x_write_mem(us, 0xFE77, &value, 1);
380 if (retval < 0)
381 return -EIO;
383 retval = rts51x_read_mem(us, 0xFE77, &value, 1);
384 if (retval < 0)
385 return -EIO;
387 if (!(value & 0x04))
388 return -EIO;
390 return 0;
393 static int do_config_autodelink(struct us_data *us, int enable, int force)
395 int retval;
396 u8 value;
398 retval = rts51x_read_mem(us, 0xFE47, &value, 1);
399 if (retval < 0)
400 return -EIO;
402 if (enable) {
403 if (force)
404 value |= 0x03;
405 else
406 value |= 0x01;
407 } else {
408 value &= ~0x03;
411 US_DEBUGP("In %s,set 0xfe47 to 0x%x\n", __func__, value);
413 retval = rts51x_write_mem(us, 0xFE47, &value, 1);
414 if (retval < 0)
415 return -EIO;
417 return 0;
420 static int config_autodelink_after_power_on(struct us_data *us)
422 struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
423 int retval;
424 u8 value;
426 if (!CHK_AUTO_DELINK(chip))
427 return 0;
429 retval = rts51x_read_mem(us, 0xFE47, &value, 1);
430 if (retval < 0)
431 return -EIO;
433 if (auto_delink_en) {
434 CLR_BIT(value, 0);
435 CLR_BIT(value, 1);
436 SET_BIT(value, 2);
438 if (CHECK_ID(chip, 0x0138, 0x3882))
439 CLR_BIT(value, 2);
441 SET_BIT(value, 7);
443 retval = rts51x_write_mem(us, 0xFE47, &value, 1);
444 if (retval < 0)
445 return -EIO;
447 retval = enable_oscillator(us);
448 if (retval == 0)
449 (void)do_config_autodelink(us, 1, 0);
450 } else {
451 /* Autodelink controlled by firmware */
453 SET_BIT(value, 2);
455 if (CHECK_ID(chip, 0x0138, 0x3882))
456 CLR_BIT(value, 2);
458 if (CHECK_ID(chip, 0x0159, 0x5889) ||
459 CHECK_ID(chip, 0x0138, 0x3880)) {
460 CLR_BIT(value, 0);
461 CLR_BIT(value, 7);
464 retval = rts51x_write_mem(us, 0xFE47, &value, 1);
465 if (retval < 0)
466 return -EIO;
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;
474 value = 0x01;
475 retval = rts51x_write_mem(us, 0x48, &value, 1);
476 if (retval < 0)
477 return -EIO;
481 return 0;
484 static int config_autodelink_before_power_down(struct us_data *us)
486 struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
487 int retval;
488 u8 value;
490 if (!CHK_AUTO_DELINK(chip))
491 return 0;
493 if (auto_delink_en) {
494 retval = rts51x_read_mem(us, 0xFE77, &value, 1);
495 if (retval < 0)
496 return -EIO;
498 SET_BIT(value, 2);
499 retval = rts51x_write_mem(us, 0xFE77, &value, 1);
500 if (retval < 0)
501 return -EIO;
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;
510 retval = rts51x_read_mem(us, 0xFE47, &value, 1);
511 if (retval < 0)
512 return -EIO;
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) ||
522 CHECK_ID(chip, 0x0138, 0x3880) ||
523 CHECK_ID(chip, 0x0138, 0x3882)) {
524 retval = rts51x_read_mem(us, 0xFE47, &value, 1);
525 if (retval < 0)
526 return -EIO;
528 if (CHECK_ID(chip, 0x0159, 0x5889) ||
529 CHECK_ID(chip, 0x0138, 0x3880)) {
530 SET_BIT(value, 0);
531 SET_BIT(value, 7);
534 if (CHECK_ID(chip, 0x0138, 0x3882))
535 SET_BIT(value, 2);
537 retval = rts51x_write_mem(us, 0xFE47, &value, 1);
538 if (retval < 0)
539 return -EIO;
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;
550 return 0;
553 static void realtek_cr_destructor(void *extra)
555 struct rts51x_chip *chip = (struct rts51x_chip *)extra;
557 if (!chip)
558 return;
560 kfree(chip->status);
563 #ifdef CONFIG_PM
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);
569 #endif
571 static int init_realtek_cr(struct us_data *us)
573 struct rts51x_chip *chip;
574 int size, i, retval;
576 chip = kzalloc(sizeof(struct rts51x_chip), GFP_KERNEL);
577 if (!chip)
578 return -ENOMEM;
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
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);
592 if (!chip->status)
593 goto INIT_FAIL;
595 for (i = 0; i <= (int)(chip->max_lun); i++) {
596 retval = rts51x_check_status(us, (u8) i);
597 if (retval < 0)
598 goto INIT_FAIL;
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);
613 return 0;
615 INIT_FAIL:
616 if (us->extra) {
617 kfree(chip->status);
618 kfree(us->extra);
619 us->extra = NULL;
622 return -EIO;
625 static int realtek_cr_probe(struct usb_interface *intf,
626 const struct usb_device_id *id)
628 struct us_data *us;
629 int result;
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);
636 if (result)
637 return result;
639 result = usb_stor_probe2(us);
641 return result;
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,
654 .soft_unbind = 1,
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);