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>
37 #include <linux/version.h>
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_cmnd.h>
41 #include <scsi/scsi_device.h>
42 #include <scsi/scsi_devinfo.h>
43 #include <scsi/scsi_eh.h>
44 #include <scsi/scsi_host.h>
46 #define DRIVER_VERSION "v1.04"
48 #define RTS51X_DESC "Realtek RTS5139/29 USB card reader driver"
49 #define RTS51X_NAME "rts5139"
50 #define RTS51X_CTL_THREAD "rts5139-control"
51 #define RTS51X_SCAN_THREAD "rts5139-scan"
52 #define RTS51X_POLLING_THREAD "rts5139-polling"
54 #define POLLING_IN_THREAD
55 /* #define SCSI_SCAN_DELAY */
56 #define SUPPORT_FILE_OP
58 #define wait_timeout_x(task_state, msecs) \
60 set_current_state((task_state)); \
61 schedule_timeout((msecs) * HZ / 1000); \
64 #define wait_timeout(msecs) wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
66 #define SCSI_LUN(srb) ((srb)->device->lun)
68 /* Size of the DMA-mapped I/O buffer */
69 #define RTS51X_IOBUF_SIZE 1024
70 /* Size of the autosense data buffer */
71 #define RTS51X_SENSE_SIZE 18
73 /* Dynamic bitflag definitions (dflags): used in set_bit() etc. */
74 #define FLIDX_URB_ACTIVE 0 /* current_urb is in use */
75 #define FLIDX_SG_ACTIVE 1 /* current_sg is in use */
76 #define FLIDX_ABORTING 2 /* abort is in progress */
77 #define FLIDX_DISCONNECTING 3 /* disconnect in progress */
78 #define FLIDX_RESETTING 4 /* device reset in progress */
79 #define FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */
80 #define FLIDX_DONT_SCAN 6 /* don't scan (disconnect) */
85 /* The device we're working with
86 * It's important to note:
87 * (o) you must hold dev_mutex to change pusb_dev
89 struct mutex dev_mutex
; /* protect pusb_dev */
90 struct usb_device
*pusb_dev
; /* this usb_device */
91 struct usb_interface
*pusb_intf
; /* this interface */
93 unsigned long dflags
; /* dynamic atomic bitflags */
95 unsigned int send_bulk_pipe
; /* cached pipe values */
96 unsigned int recv_bulk_pipe
;
97 unsigned int send_ctrl_pipe
;
98 unsigned int recv_ctrl_pipe
;
99 unsigned int recv_intr_pipe
;
101 u8 ifnum
; /* interface number */
102 u8 ep_bInterval
; /* interrupt interval */
104 /* control and bulk communications data */
105 struct urb
*current_urb
; /* USB requests */
106 struct urb
*intr_urb
; /* Interrupt USB request */
107 struct usb_ctrlrequest
*cr
; /* control requests */
108 struct usb_sg_request current_sg
; /* scatter-gather req. */
109 unsigned char *iobuf
; /* I/O buffer */
110 dma_addr_t cr_dma
; /* buffer DMA addresses */
111 dma_addr_t iobuf_dma
;
112 struct task_struct
*ctl_thread
; /* the control thread */
113 struct task_struct
*polling_thread
; /* the polling thread */
115 /* mutual exclusion and synchronization structures */
116 struct completion cmnd_ready
; /* to sleep thread on */
117 struct completion control_exit
; /* control thread exit */
118 struct completion polling_exit
; /* polling thread exit */
119 struct completion notify
; /* thread begin/end */
120 #ifdef SCSI_SCAN_DELAY
121 wait_queue_head_t delay_wait
; /* wait during scan, reset */
122 struct completion scanning_done
; /* wait for scan thread */
126 extern struct usb_driver rts51x_driver
;
128 static inline void get_current_time(u8
*timeval_buf
, int buf_len
)
132 if (!timeval_buf
|| (buf_len
< 8))
135 do_gettimeofday(&tv
);
137 timeval_buf
[0] = (u8
) (tv
.tv_sec
>> 24);
138 timeval_buf
[1] = (u8
) (tv
.tv_sec
>> 16);
139 timeval_buf
[2] = (u8
) (tv
.tv_sec
>> 8);
140 timeval_buf
[3] = (u8
) (tv
.tv_sec
);
141 timeval_buf
[4] = (u8
) (tv
.tv_usec
>> 24);
142 timeval_buf
[5] = (u8
) (tv
.tv_usec
>> 16);
143 timeval_buf
[6] = (u8
) (tv
.tv_usec
>> 8);
144 timeval_buf
[7] = (u8
) (tv
.tv_usec
);
147 #define SND_CTRL_PIPE(chip) ((chip)->usb->send_ctrl_pipe)
148 #define RCV_CTRL_PIPE(chip) ((chip)->usb->recv_ctrl_pipe)
149 #define SND_BULK_PIPE(chip) ((chip)->usb->send_bulk_pipe)
150 #define RCV_BULK_PIPE(chip) ((chip)->usb->recv_bulk_pipe)
151 #define RCV_INTR_PIPE(chip) ((chip)->usb->recv_intr_pipe)
153 /* The scsi_lock() and scsi_unlock() macros protect the sm_state and the
154 * single queue element srb for write access */
155 #define scsi_unlock(host) spin_unlock_irq(host->host_lock)
156 #define scsi_lock(host) spin_lock_irq(host->host_lock)
158 #define GET_PM_USAGE_CNT(chip) \
159 atomic_read(&((chip)->usb->pusb_intf->pm_usage_cnt))
160 #define SET_PM_USAGE_CNT(chip, cnt) \
161 atomic_set(&((chip)->usb->pusb_intf->pm_usage_cnt), (cnt))
163 /* Compatible macros while we switch over */
164 static inline void *usb_buffer_alloc(struct usb_device
*dev
, size_t size
,
165 gfp_t mem_flags
, dma_addr_t
*dma
)
167 return usb_alloc_coherent(dev
, size
, mem_flags
, dma
);
170 static inline void usb_buffer_free(struct usb_device
*dev
, size_t size
,
171 void *addr
, dma_addr_t dma
)
173 return usb_free_coherent(dev
, size
, addr
, dma
);
176 /* Convert between us_data and the corresponding Scsi_Host */
177 static inline struct Scsi_Host
*rts51x_to_host(struct rts51x_chip
*chip
)
179 return container_of((void *)chip
, struct Scsi_Host
, hostdata
);
182 static inline struct rts51x_chip
*host_to_rts51x(struct Scsi_Host
*host
)
184 return (struct rts51x_chip
*)(host
->hostdata
);
187 /* struct scsi_cmnd transfer buffer access utilities */
188 enum xfer_buf_dir
{ TO_XFER_BUF
, FROM_XFER_BUF
};
190 /* General routines provided by the usb-storage standard core */
192 void rts51x_try_to_enter_ss(struct rts51x_chip
*chip
);
193 void rts51x_try_to_exit_ss(struct rts51x_chip
*chip
);
194 int rts51x_suspend(struct usb_interface
*iface
, pm_message_t message
);
195 int rts51x_resume(struct usb_interface
*iface
);
196 int rts51x_reset_resume(struct usb_interface
*iface
);
198 #define rts51x_suspend NULL
199 #define rts51x_resume NULL
200 #define rts51x_reset_resume NULL
203 extern struct scsi_host_template rts51x_host_template
;
205 #endif /* __RTS51X_H */