Initial commit
[kernel/linux-3.0.git] / drivers / usb / host / shost / shost_kal.h
1 /****************************************************************************
2  *  (C) Copyright 2008 Samsung Electronics Co., Ltd., All rights reserved
3  *
4  * @file   s3c-otg-hcdi-kal.h
5  * @brief  header of s3c-otg-hcdi-kal \n
6  * @version
7  *  -# Jun 9,2008 v1.0 by SeungSoo Yang (ss1.yang@samsung.com) \n
8  *        : Creating the initial version of this code \n
9  *  -# Jul 15,2008 v1.2 by SeungSoo Yang (ss1.yang@samsung.com) \n
10  *        : Optimizing for performance \n
11  *  -# Aug 18,2008 v1.3 by SeungSoo Yang (ss1.yang@samsung.com) \n
12  *        : Modifying for successful rmmod & disconnecting \n
13  * @see None
14  ****************************************************************************/
15 /****************************************************************************
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  ****************************************************************************/
30
31 #ifndef _SHOST_KAL_H_
32 #define _SHOST_KAL_H_
33
34 #include <linux/io.h>           /* for readl, writel */
35 #include <linux/usb/ch9.h>
36 #include <linux/usb.h>
37 #include <linux/usb/hcd.h>
38 #include <mach/map.h>
39 #include <plat/regs-otg.h>
40
41 extern volatile         u8                      *g_pUDCBase;
42 extern struct           usb_hcd         *g_pUsbHcd;
43
44 #define ctrlr_base_reg_addr(offset) \
45                         ((volatile unsigned int *)((g_pUDCBase) + (offset)))
46 /**
47  * otg_kal_make_ep_null
48  *
49  * @brief make ep->hcpriv NULL
50  *
51  * @param [in] pdelete_ed : pointer of ed
52  *
53  * @return void \n
54  */
55 static  inline  void
56 otg_kal_make_ep_null(struct ed *pdelete_ed)
57 {
58         ((struct usb_host_endpoint *)(pdelete_ed->ed_private))->hcpriv = NULL;
59 }
60
61 /**
62  * otg_kal_is_ep_null
63  *
64  * @brief check ep->hcpriv is NULL or not
65  *
66  * @param [in] pdelete_ed : pointer of ed
67  *
68  * @return bool \n
69  */
70 static  inline  bool
71 otg_kal_is_ep_null(struct ed *pdelete_ed)
72 {
73         if (((struct usb_host_endpoint *)
74                                 (pdelete_ed->ed_private))->hcpriv == NULL)
75                 return true;
76         else
77                 return false;
78 }
79
80
81 /**
82  * int  otg_usbcore_get_calc_bustime()
83  *
84  * @brief get bus time of usbcore
85  *
86  * @param [in] speed : usb speed
87  *                [in] is_input : input or not
88  *                [in] is_isoch : isochronous or not
89  *                [in] byte_count : bytes
90  *
91  * @return bus time of usbcore \n
92  */
93 static  inline  int
94 otg_usbcore_get_calc_bustime(u8 speed, bool is_input,
95                 bool is_isoch, unsigned int byte_count)
96 {
97         unsigned int convert_speed = 0;
98
99         otg_dbg(OTG_DBG_OTGHCDI_KAL, "otg_usbcore_get_calc_bustime\n");
100 /*      enum usb_device_speed {
101                 USB_SPEED_UNKNOWN = 0,
102                 USB_SPEED_LOW, USB_SPEED_FULL,
103                 USB_SPEED_HIGH,
104                 USB_SPEED_VARIABLE, };*/
105         switch (speed) {
106         case HIGH_SPEED_OTG:
107                 convert_speed = USB_SPEED_HIGH; break;
108         case FULL_SPEED_OTG:
109                 convert_speed = USB_SPEED_FULL; break;
110         case LOW_SPEED_OTG:
111                 convert_speed = USB_SPEED_LOW; break;
112         default:
113                 convert_speed = USB_SPEED_UNKNOWN; break;
114         }
115         return usb_calc_bus_time(convert_speed, is_input,
116                         (unsigned int)is_isoch, byte_count);
117 }
118
119
120 /**
121  * void otg_usbcore_giveback(struct td td_p)
122  *
123  * @brief give-back a td as urb
124  *
125  * @param [in] td_p : pointer of struct td to give back
126  *
127  * @return void \n
128  */
129 static  inline  void
130 otg_usbcore_giveback(struct td *td_p)
131 {
132         struct urb *urb_p = NULL;
133
134         otg_dbg(OTG_DBG_OTGHCDI_KAL, "otg_usbcore_giveback\n");
135
136         if (td_p->td_private == NULL) {
137                 otg_err(OTG_DBG_OTGHCDI_KAL,
138                         "td_p->td_private == NULL\n");
139                 return;
140         }
141
142         urb_p = (struct urb *)td_p->td_private;
143
144         urb_p->actual_length    = (int)(td_p->transferred_szie);
145         urb_p->status                   = (int)(td_p->error_code);
146         urb_p->error_count              = (int)(td_p->err_cnt);
147         urb_p->hcpriv                   = NULL;
148
149         usb_hcd_giveback_urb(g_pUsbHcd, urb_p, urb_p->status);
150 }
151
152 /**
153  * void otg_usbcore_hc_died(void)
154  *
155  * @brief inform usbcore of hc die
156  *
157  * @return void \n
158  */
159 static  inline  void
160 otg_usbcore_hc_died(void)
161 {
162         otg_dbg(OTG_DBG_OTGHCDI_KAL, "otg_usbcore_hc_died\n");
163         usb_hc_died(g_pUsbHcd);
164 }
165
166 /**
167  * void otg_usbcore_poll_rh_status(void)
168  *
169  * @brief invoke usbcore's usb_hcd_poll_rh_status
170  *
171  * @param void
172  *
173  * @return void \n
174  */
175 static  inline  void
176 otg_usbcore_poll_rh_status(void)
177 {
178         usb_hcd_poll_rh_status(g_pUsbHcd);
179 }
180
181 /**
182  * void otg_usbcore_resume_roothub(void)
183  *
184  * @brief invoke usbcore's usb_hcd_resume_root_hub
185  *
186  * @param void
187  *
188  * @return void \n
189  */
190 static inline   void
191 otg_usbcore_resume_roothub(void)
192 {
193         otg_dbg(OTG_DBG_OTGHCDI_KAL,
194                  "otg_usbcore_resume_roothub\n");
195         usb_hcd_resume_root_hub(g_pUsbHcd);
196 };
197
198 /**
199  * int  otg_usbcore_inc_usb_bandwidth(u32 band_width)
200  *
201  * @brief       increase bandwidth of usb bus
202  *
203  * @param  [in] band_width : bandwidth to be increased
204  *
205  * @return USB_ERR_SUCCESS \n
206  */
207 static  inline  int
208 otg_usbcore_inc_usb_bandwidth(u32 band_width)
209 {
210         otg_dbg(OTG_DBG_OTGHCDI_KAL,
211                 "otg_usbcore_inc_usb_bandwidth\n");
212         hcd_to_bus(g_pUsbHcd)->bandwidth_allocated += band_width;
213         return USB_ERR_SUCCESS;
214 }
215
216 /**
217  * int otg_usbcore_des_usb_bandwidth(u32 uiBandwidth)
218  *
219  * @brief       decrease bandwidth of usb bus
220  *
221  * @param  [in] band_width : bandwidth to be decreased
222  *
223  * @return USB_ERR_SUCCESS \n
224  */
225 static  inline  int
226 otg_usbcore_des_usb_bandwidth(u32 band_width)
227 {
228         otg_dbg(OTG_DBG_OTGHCDI_KAL,
229                 "otg_usbcore_des_usb_bandwidth\n");
230         hcd_to_bus(g_pUsbHcd)->bandwidth_allocated -= band_width;
231         return USB_ERR_SUCCESS;
232 }
233
234 /**
235  * int otg_usbcore_inc_periodic_transfer_cnt(u8 transfer_type)
236  *
237  * @brief       increase count of periodic transfer
238  *
239  * @param  [in] transfer_type : type of transfer
240  *
241  * @return USB_ERR_SUCCESS : If success \n
242  *         USB_ERR_FAIL : If call fail \n
243  */
244 static  inline  int
245 otg_usbcore_inc_periodic_transfer_cnt(u8 transfer_type)
246 {
247         otg_dbg(OTG_DBG_OTGHCDI_KAL,
248                 "otg_usbcore_inc_periodic_transfer_cnt\n");
249
250         switch (transfer_type) {
251         case INT_TRANSFER:
252                 hcd_to_bus(g_pUsbHcd)->bandwidth_int_reqs++;
253                 break;
254         case ISOCH_TRANSFER:
255                 hcd_to_bus(g_pUsbHcd)->bandwidth_isoc_reqs++;
256                 break;
257         default:
258                 otg_err(OTG_DBG_OTGHCDI_KAL,
259                         "not proper TransferType\n");
260                 return USB_ERR_FAIL;
261         }
262         return USB_ERR_SUCCESS;
263 }
264
265 /**
266  * int otg_usbcore_des_periodic_transfer_cnt(u8 transfer_type)
267  *
268  * @brief       decrease count of periodic transfer
269  *
270  * @param  [in] transfer_type : type of transfer
271  *
272  * @return USB_ERR_SUCCESS : If success \n
273  *         USB_ERR_FAIL : If call fail \n
274  */
275 static  inline  int
276 otg_usbcore_des_periodic_transfer_cnt(u8 transfer_type)
277 {
278         otg_dbg(OTG_DBG_OTGHCDI_KAL,
279                 "otg_usbcore_des_periodic_transfer_cnt\n");
280
281         switch (transfer_type) {
282         case INT_TRANSFER:
283                 hcd_to_bus(g_pUsbHcd)->bandwidth_int_reqs--;
284                 break;
285         case ISOCH_TRANSFER:
286                 hcd_to_bus(g_pUsbHcd)->bandwidth_isoc_reqs--;
287                 break;
288         default:
289                 otg_err(OTG_DBG_OTGHCDI_KAL,
290                         "not proper TransferType\n");
291                 return USB_ERR_FAIL;
292         }
293         return USB_ERR_SUCCESS;
294 }
295
296 /**
297  * u32 read_reg_32(u32 offset)
298  *
299  * @brief Reads the content of a register.
300  *
301  * @param [in] offset : offset of address of register to read.
302  *
303  * @return contents of the register. \n
304  * @remark call readl()
305  */
306 static  inline  u32 read_reg_32(u32 offset)
307 {
308         volatile unsigned int *reg_addr_p = ctrlr_base_reg_addr(offset);
309
310         return *reg_addr_p;
311         /* return readl(reg_addr_p); */
312 };
313
314 /**
315  * void write_reg_32( u32 offset, const u32 value)
316  *
317  * @brief Writes a register with a 32 bit value.
318  *
319  * @param [in] offset : offset of address of register to write.
320  * @param [in] value : value to write
321  *
322  * @remark call writel()
323  */
324 static inline void write_reg_32(u32 offset, const u32 value)
325 {
326         volatile unsigned int *reg_addr_p = ctrlr_base_reg_addr(offset);
327
328         *reg_addr_p = value;
329         /* writel( value, reg_addr_p ); */
330 };
331
332 /**
333  * void update_reg_32(u32 offset, u32 value)
334  *
335  * @brief logic or operation
336  *
337  * @param [in] offset : offset of address of register to write.
338  * @param [in] value : value to or
339  *
340  */
341 static  inline  void update_reg_32(u32 offset, u32 value)
342 {
343         write_reg_32(offset, (read_reg_32(offset) | value));
344 }
345
346 /**
347  * void clear_reg_32(u32         offset, u32 value)
348  *
349  * @brief logic not operation
350  *
351  * @param [in] offset : offset of address of register to write.
352  * @param [in] value : value to not
353  *
354  */
355 static  inline  void clear_reg_32(u32    offset, u32 value)
356 {
357         write_reg_32(offset, (read_reg_32(offset) & ~value));
358 }
359
360 #endif /* _S3C_OTG_HCDI_KAL_H_ */
361