1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78
79 //#include <MsCommon.h> // NUSED
80
81 //#include "include/drvConfig.h" // NUSED
82 //#include "include/drvCompiler.h" // NUSED
83 //#include "include/drvErrno.h" // NUSED
84 //#include "include/drvPorts.h" // NUSED
85 //#include "include/drvTimer.h" // NUSED
86 //#include "include/drvList.h" // NUSED
87 //#include "include/drvKernel.h" // NUSED
88
89 // USB related header files
90 //#include "include/drvUSBHost.h" // NUSED
91 #include "drvUsbd.h"
92
93 /**
94 * @brief create new urb
95 *
96 * @param int iso_packets
97 * @param int mem_flags
98 *
99 * @return allocated urb
100 */
ms_usb_alloc_urb(int mem_flags)101 struct urb *ms_usb_alloc_urb(int mem_flags)
102 {
103 struct urb *ms_urb;
104
105 ms_urb = (struct urb *)kmalloc(sizeof(struct urb), mem_flags);
106 if (!ms_urb)
107 {
108 ms_usbhost_err("alloc_urb: kmalloc failed%s","\n");
109 return NULL;
110 }
111 memset(ms_urb, 0, sizeof(*ms_urb));
112 ms_urb->count = (atomic_t)osapi_ATOMIC_INIT(1);
113 osapi_spin_lock_init(&ms_urb->lock);
114 return ms_urb;
115 }
116
117 /**
118 * @brief test urb reference count and free the memory
119 *
120 * @param struct urb *ms_urb
121 *
122 * @return none
123 */
ms_usb_free_urb(struct urb * ms_urb)124 void ms_usb_free_urb(struct urb *ms_urb)
125 {
126 if (ms_urb)
127 if (osapi_atomic_dec_and_test(&ms_urb->count))
128 kfree(ms_urb);
129 }
130
131 /**
132 * @brief increase urb reference count
133 *
134 * @param struct urb *urb
135 *
136 * @return current urb
137 */
ms_usb_get_urb(struct urb * ms_urb)138 struct urb * ms_usb_get_urb(struct urb *ms_urb)
139 {
140 if (ms_urb)
141 {
142 osapi_atomic_inc(&ms_urb->count);
143 return ms_urb;
144 }
145 else
146 return NULL;
147 }
148
149 /**
150 * @brief submit urb to the queue
151 *
152 * @param struct urb *ms_urb
153 * @param int mem_flags
154 *
155 * @return error code
156 */
ms_usb_submit_urb(struct urb * ms_urb,int mem_flags)157 int ms_usb_submit_urb(struct urb *ms_urb, int mem_flags)
158 {
159 int ms_pipe, temp_p, max_p;
160 struct usb_device *pUdev;
161 //struct ms_usb_bus_ops *usb_op;
162 int is_out;
163
164 if (!ms_urb || ms_urb->hcpriv || !ms_urb->complete_func)
165 return -EINVAL;
166 if (!(pUdev = ms_urb->dev) || (pUdev->eState < USB_STATE_DEFAULT) ||
167 (!pUdev->bus) || (pUdev->u32DevNum <= 0))
168 return -ENODEV;
169 //if (!(usb_op = pUdev->bus->bus_ops) || !usb_op->ms_hcd_submit_urb)
170 // return -ENODEV;
171
172 ms_urb->s32Status = -EINPROGRESS;
173 ms_urb->u32ActualLength = 0;
174
175 ms_pipe = ms_urb->u32Pipe;
176 temp_p = usb_pipetype (ms_pipe);
177 is_out = usb_pipeout (ms_pipe);
178
179 if (!usb_pipecontrol (ms_pipe) && pUdev->eState < USB_STATE_CONFIGURED)
180 return -ENODEV;
181
182 /* patch from linux, obsolete code */
183 //if (usb_endpoint_halted (pUdev, usb_pipeendpoint (ms_pipe), is_out))
184 // return -EPIPE;
185
186 max_p = usb_maxpacket (pUdev, ms_pipe, is_out);
187 if (max_p <= 0)
188 {
189 ms_usbhost_err ("<%s> wrong endpoint %d-%s on usb-%s-%s (bad maxpacket %d)",
190 __FUNCTION__,
191 usb_pipeendpoint (ms_pipe), is_out ? "OUT" : "IN",
192 pUdev->bus->bus_name, pUdev->devpath,
193 max_p);
194 return -EMSGSIZE;
195 }
196
197 if (ms_urb->u32TransferBufferLength < 0)
198 return -EMSGSIZE;
199
200 //only support interrupt pipe
201 if (temp_p == EP_INTERRUPT)
202 {
203 if (ms_urb->u32Interval <= 0)
204 return -EINVAL;
205 switch (pUdev->eSpeed)
206 {
207 case USB_HIGH_SPEED: /* units are microframes */
208 // NOTE usb handles 2^15
209 if (ms_urb->u32Interval > (1024 * 8))
210 ms_urb->u32Interval = 1024 * 8;
211 temp_p = 1024 * 8;
212 break;
213 case USB_FULL_SPEED: /* units are frames/msec */
214 case USB_LOW_SPEED:
215 {
216 if (ms_urb->u32Interval > 255)
217 return -EINVAL;
218 // NOTE ohci only handles up to 32
219 temp_p = 128;
220 }
221 break;
222 default:
223 return -EINVAL;
224 }
225 while (temp_p > ms_urb->u32Interval)
226 temp_p >>= 1;
227 ms_urb->u32Interval = temp_p;
228 }
229
230 //return usb_op->ms_hcd_submit_urb (ms_urb, mem_flags);
231 return ms_hcd_submit_urb (ms_urb, mem_flags);
232 }
233
234 /**
235 * @brief unlink urb from the queue
236 *
237 * @param struct urb *ms_urb
238 *
239 * @return error code
240 */
ms_usb_unlink_urb(struct urb * ms_urb)241 int ms_usb_unlink_urb(struct urb *ms_urb)
242 {
243 //if (ms_urb && ms_urb->dev && ms_urb->dev->bus && ms_urb->dev->bus->bus_ops)
244 // return ms_urb->dev->bus->bus_ops->ms_hcd_unlink_urb(ms_urb);
245 //else
246 // return -ENODEV;
247 if (!ms_urb)
248 return -EINVAL;
249 if (!ms_urb->dev)
250 return -ENODEV;
251 return ms_hcd_unlink_urb(ms_urb, -ECONNRESET);
252 }
253