1 /* Driver for Realtek RTS51xx USB card reader
2 * Header file
3 *
4 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
5 *
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
9 * later version.
10 *
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.
15 *
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/>.
18 *
19 * Author:
20 * wwang (wei_wang@realsil.com.cn)
21 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
22 * Maintainer:
23 * Edwin Rong (edwin_rong@realsil.com.cn)
24 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
25 */
26
27 #ifndef __RTS51X_H
28 #define __RTS51X_H
29
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
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>
44
45 #define DRIVER_VERSION "v1.04"
46
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_SCAN_THREAD "rts5139-scan"
51 #define RTS51X_POLLING_THREAD "rts5139-polling"
52
53 #define POLLING_IN_THREAD
54 /* #define SCSI_SCAN_DELAY */
55 #define SUPPORT_FILE_OP
56
57 #define wait_timeout_x(task_state, msecs) \
58 do { \
59 set_current_state((task_state)); \
60 schedule_timeout((msecs) * HZ / 1000); \
61 } while (0)
62
63 #define wait_timeout(msecs) wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
64
65 #define SCSI_LUN(srb) ((srb)->device->lun)
66
67 /* Size of the DMA-mapped I/O buffer */
68 #define RTS51X_IOBUF_SIZE 1024
69 /* Size of the autosense data buffer */
70 #define RTS51X_SENSE_SIZE 18
71
72 /* Dynamic bitflag definitions (dflags): used in set_bit() etc. */
73 #define FLIDX_URB_ACTIVE 0 /* current_urb is in use */
74 #define FLIDX_SG_ACTIVE 1 /* current_sg is in use */
75 #define FLIDX_ABORTING 2 /* abort is in progress */
76 #define FLIDX_DISCONNECTING 3 /* disconnect in progress */
77 #define FLIDX_RESETTING 4 /* device reset in progress */
78 #define FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */
79 #define FLIDX_DONT_SCAN 6 /* don't scan (disconnect) */
80
81 struct rts51x_chip;
82
83 struct rts51x_usb {
84 /* The device we're working with
85 * It's important to note:
86 * (o) you must hold dev_mutex to change pusb_dev
87 */
88 struct mutex dev_mutex; /* protect pusb_dev */
89 struct usb_device *pusb_dev; /* this usb_device */
90 struct usb_interface *pusb_intf; /* this interface */
91
92 unsigned long dflags; /* dynamic atomic bitflags */
93
94 unsigned int send_bulk_pipe; /* cached pipe values */
95 unsigned int recv_bulk_pipe;
96 unsigned int send_ctrl_pipe;
97 unsigned int recv_ctrl_pipe;
98 unsigned int recv_intr_pipe;
99
100 u8 ifnum; /* interface number */
101 u8 ep_bInterval; /* interrupt interval */
102
103 /* control and bulk communications data */
104 struct urb *current_urb; /* USB requests */
105 struct urb *intr_urb; /* Interrupt USB request */
106 struct usb_ctrlrequest *cr; /* control requests */
107 struct usb_sg_request current_sg; /* scatter-gather req. */
108 unsigned char *iobuf; /* I/O buffer */
109 dma_addr_t cr_dma; /* buffer DMA addresses */
110 dma_addr_t iobuf_dma;
111 struct task_struct *ctl_thread; /* the control thread */
112 struct task_struct *polling_thread; /* the polling thread */
113
114 /* mutual exclusion and synchronization structures */
115 struct completion cmnd_ready; /* to sleep thread on */
116 struct completion control_exit; /* control thread exit */
117 struct completion polling_exit; /* polling thread exit */
118 struct completion notify; /* thread begin/end */
119 #ifdef SCSI_SCAN_DELAY
120 wait_queue_head_t delay_wait; /* wait during scan, reset */
121 struct completion scanning_done; /* wait for scan thread */
122 #endif
123 };
124
125 extern struct usb_driver rts51x_driver;
126
get_current_time(u8 * timeval_buf,int buf_len)127 static inline void get_current_time(u8 *timeval_buf, int buf_len)
128 {
129 struct timeval tv;
130
131 if (!timeval_buf || (buf_len < 8))
132 return;
133
134 do_gettimeofday(&tv);
135
136 timeval_buf[0] = (u8) (tv.tv_sec >> 24);
137 timeval_buf[1] = (u8) (tv.tv_sec >> 16);
138 timeval_buf[2] = (u8) (tv.tv_sec >> 8);
139 timeval_buf[3] = (u8) (tv.tv_sec);
140 timeval_buf[4] = (u8) (tv.tv_usec >> 24);
141 timeval_buf[5] = (u8) (tv.tv_usec >> 16);
142 timeval_buf[6] = (u8) (tv.tv_usec >> 8);
143 timeval_buf[7] = (u8) (tv.tv_usec);
144 }
145
146 #define SND_CTRL_PIPE(chip) ((chip)->usb->send_ctrl_pipe)
147 #define RCV_CTRL_PIPE(chip) ((chip)->usb->recv_ctrl_pipe)
148 #define SND_BULK_PIPE(chip) ((chip)->usb->send_bulk_pipe)
149 #define RCV_BULK_PIPE(chip) ((chip)->usb->recv_bulk_pipe)
150 #define RCV_INTR_PIPE(chip) ((chip)->usb->recv_intr_pipe)
151
152 /* The scsi_lock() and scsi_unlock() macros protect the sm_state and the
153 * single queue element srb for write access */
154 #define scsi_unlock(host) spin_unlock_irq(host->host_lock)
155 #define scsi_lock(host) spin_lock_irq(host->host_lock)
156
157 #define GET_PM_USAGE_CNT(chip) \
158 atomic_read(&((chip)->usb->pusb_intf->pm_usage_cnt))
159 #define SET_PM_USAGE_CNT(chip, cnt) \
160 atomic_set(&((chip)->usb->pusb_intf->pm_usage_cnt), (cnt))
161
162 /* Compatible macros while we switch over */
usb_buffer_alloc(struct usb_device * dev,size_t size,gfp_t mem_flags,dma_addr_t * dma)163 static inline void *usb_buffer_alloc(struct usb_device *dev, size_t size,
164 gfp_t mem_flags, dma_addr_t *dma)
165 {
166 return usb_alloc_coherent(dev, size, mem_flags, dma);
167 }
168
usb_buffer_free(struct usb_device * dev,size_t size,void * addr,dma_addr_t dma)169 static inline void usb_buffer_free(struct usb_device *dev, size_t size,
170 void *addr, dma_addr_t dma)
171 {
172 return usb_free_coherent(dev, size, addr, dma);
173 }
174
175 /* Convert between us_data and the corresponding Scsi_Host */
rts51x_to_host(struct rts51x_chip * chip)176 static inline struct Scsi_Host *rts51x_to_host(struct rts51x_chip *chip)
177 {
178 return container_of((void *)chip, struct Scsi_Host, hostdata);
179 }
180
host_to_rts51x(struct Scsi_Host * host)181 static inline struct rts51x_chip *host_to_rts51x(struct Scsi_Host *host)
182 {
183 return (struct rts51x_chip *)(host->hostdata);
184 }
185
186 /* struct scsi_cmnd transfer buffer access utilities */
187 enum xfer_buf_dir { TO_XFER_BUF, FROM_XFER_BUF };
188
189 /* General routines provided by the usb-storage standard core */
190 #ifdef CONFIG_PM
191 void rts51x_try_to_enter_ss(struct rts51x_chip *chip);
192 void rts51x_try_to_exit_ss(struct rts51x_chip *chip);
193 int rts51x_suspend(struct usb_interface *iface, pm_message_t message);
194 int rts51x_resume(struct usb_interface *iface);
195 int rts51x_reset_resume(struct usb_interface *iface);
196 #else
197 #define rts51x_suspend NULL
198 #define rts51x_resume NULL
199 #define rts51x_reset_resume NULL
200 #endif
201
202 extern struct scsi_host_template rts51x_host_template;
203
204 #endif /* __RTS51X_H */
205