1 /* Driver for Realtek RTS51xx USB card reader
4 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 * wwang (wei_wang@realsil.com.cn)
21 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
23 * Edwin Rong (edwin_rong@realsil.com.cn)
24 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
30 #include <linux/usb.h>
31 #include <linux/usb_usual.h>
32 #include <linux/blkdev.h>
33 #include <linux/completion.h>
34 #include <linux/mutex.h>
35 #include <linux/cdrom.h>
36 #include <linux/kernel.h>
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_devinfo.h>
42 #include <scsi/scsi_eh.h>
43 #include <scsi/scsi_host.h>
45 #define DRIVER_VERSION "v1.04"
47 #define RTS51X_DESC "Realtek RTS5139/29 USB card reader driver"
48 #define RTS51X_NAME "rts5139"
49 #define RTS51X_CTL_THREAD "rts5139-control"
50 #define RTS51X_POLLING_THREAD "rts5139-polling"
52 #define POLLING_IN_THREAD
53 #define SUPPORT_FILE_OP
55 #define wait_timeout_x(task_state, msecs) \
57 set_current_state((task_state)); \
58 schedule_timeout((msecs) * HZ / 1000); \
61 #define wait_timeout(msecs) wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
63 #define SCSI_LUN(srb) ((srb)->device->lun)
65 /* Size of the DMA-mapped I/O buffer */
66 #define RTS51X_IOBUF_SIZE 1024
68 /* Dynamic bitflag definitions (dflags): used in set_bit() etc. */
69 #define FLIDX_URB_ACTIVE 0 /* current_urb is in use */
70 #define FLIDX_SG_ACTIVE 1 /* current_sg is in use */
71 #define FLIDX_ABORTING 2 /* abort is in progress */
72 #define FLIDX_DISCONNECTING 3 /* disconnect in progress */
73 #define FLIDX_RESETTING 4 /* device reset in progress */
74 #define FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */
79 /* The device we're working with
80 * It's important to note:
81 * (o) you must hold dev_mutex to change pusb_dev
83 struct mutex dev_mutex
; /* protect pusb_dev */
84 struct usb_device
*pusb_dev
; /* this usb_device */
85 struct usb_interface
*pusb_intf
; /* this interface */
87 unsigned long dflags
; /* dynamic atomic bitflags */
89 unsigned int send_bulk_pipe
; /* cached pipe values */
90 unsigned int recv_bulk_pipe
;
91 unsigned int send_ctrl_pipe
;
92 unsigned int recv_ctrl_pipe
;
93 unsigned int recv_intr_pipe
;
95 u8 ifnum
; /* interface number */
96 u8 ep_bInterval
; /* interrupt interval */
98 /* control and bulk communications data */
99 struct urb
*current_urb
; /* USB requests */
100 struct urb
*intr_urb
; /* Interrupt USB request */
101 struct usb_ctrlrequest
*cr
; /* control requests */
102 struct usb_sg_request current_sg
; /* scatter-gather req. */
103 unsigned char *iobuf
; /* I/O buffer */
104 dma_addr_t cr_dma
; /* buffer DMA addresses */
105 dma_addr_t iobuf_dma
;
106 struct task_struct
*ctl_thread
; /* the control thread */
107 struct task_struct
*polling_thread
; /* the polling thread */
109 /* mutual exclusion and synchronization structures */
110 struct completion cmnd_ready
; /* to sleep thread on */
111 struct completion control_exit
; /* control thread exit */
112 struct completion polling_exit
; /* polling thread exit */
113 struct completion notify
; /* thread begin/end */
116 extern struct usb_driver rts51x_driver
;
118 static inline void get_current_time(u8
*timeval_buf
, int buf_len
)
122 if (!timeval_buf
|| (buf_len
< 8))
125 do_gettimeofday(&tv
);
127 timeval_buf
[0] = (u8
) (tv
.tv_sec
>> 24);
128 timeval_buf
[1] = (u8
) (tv
.tv_sec
>> 16);
129 timeval_buf
[2] = (u8
) (tv
.tv_sec
>> 8);
130 timeval_buf
[3] = (u8
) (tv
.tv_sec
);
131 timeval_buf
[4] = (u8
) (tv
.tv_usec
>> 24);
132 timeval_buf
[5] = (u8
) (tv
.tv_usec
>> 16);
133 timeval_buf
[6] = (u8
) (tv
.tv_usec
>> 8);
134 timeval_buf
[7] = (u8
) (tv
.tv_usec
);
137 #define SND_CTRL_PIPE(chip) ((chip)->usb->send_ctrl_pipe)
138 #define RCV_CTRL_PIPE(chip) ((chip)->usb->recv_ctrl_pipe)
139 #define SND_BULK_PIPE(chip) ((chip)->usb->send_bulk_pipe)
140 #define RCV_BULK_PIPE(chip) ((chip)->usb->recv_bulk_pipe)
141 #define RCV_INTR_PIPE(chip) ((chip)->usb->recv_intr_pipe)
143 /* The scsi_lock() and scsi_unlock() macros protect the sm_state and the
144 * single queue element srb for write access */
145 #define scsi_unlock(host) spin_unlock_irq(host->host_lock)
146 #define scsi_lock(host) spin_lock_irq(host->host_lock)
148 #define GET_PM_USAGE_CNT(chip) \
149 atomic_read(&((chip)->usb->pusb_intf->pm_usage_cnt))
150 #define SET_PM_USAGE_CNT(chip, cnt) \
151 atomic_set(&((chip)->usb->pusb_intf->pm_usage_cnt), (cnt))
153 /* Compatible macros while we switch over */
154 static inline void *usb_buffer_alloc(struct usb_device
*dev
, size_t size
,
155 gfp_t mem_flags
, dma_addr_t
*dma
)
157 return usb_alloc_coherent(dev
, size
, mem_flags
, dma
);
160 static inline void usb_buffer_free(struct usb_device
*dev
, size_t size
,
161 void *addr
, dma_addr_t dma
)
163 return usb_free_coherent(dev
, size
, addr
, dma
);
166 /* Convert between us_data and the corresponding Scsi_Host */
167 static inline struct Scsi_Host
*rts51x_to_host(struct rts51x_chip
*chip
)
169 return container_of((void *)chip
, struct Scsi_Host
, hostdata
);
172 static inline struct rts51x_chip
*host_to_rts51x(struct Scsi_Host
*host
)
174 return (struct rts51x_chip
*)(host
->hostdata
);
177 /* struct scsi_cmnd transfer buffer access utilities */
178 enum xfer_buf_dir
{ TO_XFER_BUF
, FROM_XFER_BUF
};
180 /* General routines provided by the usb-storage standard core */
182 void rts51x_try_to_exit_ss(struct rts51x_chip
*chip
);
183 int rts51x_suspend(struct usb_interface
*iface
, pm_message_t message
);
184 int rts51x_resume(struct usb_interface
*iface
);
185 int rts51x_reset_resume(struct usb_interface
*iface
);
187 #define rts51x_suspend NULL
188 #define rts51x_resume NULL
189 #define rts51x_reset_resume NULL
192 extern struct scsi_host_template rts51x_host_template
;
194 #endif /* __RTS51X_H */